blob: 9b7f3f6ae6739599bbd349dea1663e4907ef3888 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar693e40c2013-02-26 14:56:42 +01002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * winclip.c
12 *
Bram Moolenaar1266d672017-02-01 13:43:36 +010013 * Routines for Win32 clipboard handling.
Bram Moolenaar693e40c2013-02-26 14:56:42 +010014 * Also used by Cygwin, using os_unix.c.
15 */
16
Bram Moolenaar693e40c2013-02-26 14:56:42 +010017#include "vimio.h"
18#include "vim.h"
19
20/*
21 * Compile only the clipboard handling features when compiling for cygwin
22 * posix environment.
23 */
24#ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
Bram Moolenaar693e40c2013-02-26 14:56:42 +010025# define WIN32_LEAN_AND_MEAN
26# include <windows.h>
27# include "winclip.pro"
28#endif
29
30/*
31 * When generating prototypes for Win32 on Unix, these lines make the syntax
32 * errors disappear. They do not need to be correct.
33 */
34#ifdef PROTO
35#define WINAPI
36#define WINBASEAPI
37typedef int DWORD;
38typedef int LPBOOL;
39typedef int LPCSTR;
40typedef int LPCWSTR;
41typedef int LPSTR;
42typedef int LPWSTR;
43typedef int UINT;
44#endif
45
Bram Moolenaar693e40c2013-02-26 14:56:42 +010046/*
47 * Convert an UTF-8 string to UTF-16.
48 * "instr[inlen]" is the input. "inlen" is in bytes.
49 * When "outstr" is NULL only return the number of UTF-16 words produced.
50 * Otherwise "outstr" must be a buffer of sufficient size.
51 * Returns the number of UTF-16 words produced.
52 */
53 int
54utf8_to_utf16(char_u *instr, int inlen, short_u *outstr, int *unconvlenp)
55{
56 int outlen = 0;
57 char_u *p = instr;
58 int todo = inlen;
59 int l;
60 int ch;
61
62 while (todo > 0)
63 {
64 /* Only convert if we have a complete sequence. */
65 l = utf_ptr2len_len(p, todo);
66 if (l > todo)
67 {
68 /* Return length of incomplete sequence. */
69 if (unconvlenp != NULL)
70 *unconvlenp = todo;
71 break;
72 }
73
74 ch = utf_ptr2char(p);
75 if (ch >= 0x10000)
76 {
77 /* non-BMP character, encoding with surrogate pairs */
78 ++outlen;
79 if (outstr != NULL)
80 {
81 *outstr++ = (0xD800 - (0x10000 >> 10)) + (ch >> 10);
82 *outstr++ = 0xDC00 | (ch & 0x3FF);
83 }
84 }
85 else if (outstr != NULL)
86 *outstr++ = ch;
87 ++outlen;
88 p += l;
89 todo -= l;
90 }
91
92 return outlen;
93}
94
95/*
96 * Convert an UTF-16 string to UTF-8.
97 * The input is "instr[inlen]" with "inlen" in number of UTF-16 words.
98 * When "outstr" is NULL only return the required number of bytes.
99 * Otherwise "outstr" must be a buffer of sufficient size.
100 * Return the number of bytes produced.
101 */
102 int
103utf16_to_utf8(short_u *instr, int inlen, char_u *outstr)
104{
105 int outlen = 0;
106 int todo = inlen;
107 short_u *p = instr;
108 int l;
109 int ch, ch2;
110
111 while (todo > 0)
112 {
113 ch = *p;
114 if (ch >= 0xD800 && ch <= 0xDBFF && todo > 1)
115 {
116 /* surrogate pairs handling */
117 ch2 = p[1];
118 if (ch2 >= 0xDC00 && ch2 <= 0xDFFF)
119 {
120 ch = ((ch - 0xD800) << 10) + (ch2 & 0x3FF) + 0x10000;
121 ++p;
122 --todo;
123 }
124 }
125 if (outstr != NULL)
126 {
127 l = utf_char2bytes(ch, outstr);
128 outstr += l;
129 }
130 else
131 l = utf_char2len(ch);
132 ++p;
133 outlen += l;
134 --todo;
135 }
136
137 return outlen;
138}
139
140/*
141 * Call MultiByteToWideChar() and allocate memory for the result.
142 * Returns the result in "*out[*outlen]" with an extra zero appended.
143 * "outlen" is in words.
144 */
145 void
146MultiByteToWideChar_alloc(UINT cp, DWORD flags,
147 LPCSTR in, int inlen,
148 LPWSTR *out, int *outlen)
149{
150 *outlen = MultiByteToWideChar(cp, flags, in, inlen, 0, 0);
151 /* Add one one word to avoid a zero-length alloc(). */
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200152 *out = ALLOC_MULT(WCHAR, *outlen + 1);
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100153 if (*out != NULL)
154 {
155 MultiByteToWideChar(cp, flags, in, inlen, *out, *outlen);
156 (*out)[*outlen] = 0;
157 }
158}
159
160/*
161 * Call WideCharToMultiByte() and allocate memory for the result.
162 * Returns the result in "*out[*outlen]" with an extra NUL appended.
163 */
164 void
165WideCharToMultiByte_alloc(UINT cp, DWORD flags,
166 LPCWSTR in, int inlen,
167 LPSTR *out, int *outlen,
168 LPCSTR def, LPBOOL useddef)
169{
170 *outlen = WideCharToMultiByte(cp, flags, in, inlen, NULL, 0, def, useddef);
171 /* Add one one byte to avoid a zero-length alloc(). */
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200172 *out = alloc(*outlen + 1);
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100173 if (*out != NULL)
174 {
175 WideCharToMultiByte(cp, flags, in, inlen, *out, *outlen, def, useddef);
176 (*out)[*outlen] = 0;
177 }
178}
179
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100180
181#ifdef FEAT_CLIPBOARD
182/*
183 * Clipboard stuff, for cutting and pasting text to other windows.
184 */
185
186 void
187win_clip_init(void)
188{
189 clip_init(TRUE);
190
191 /*
192 * Vim's own clipboard format recognises whether the text is char, line,
193 * or rectangular block. Only useful for copying between two Vims.
194 * "VimClipboard" was used for previous versions, using the first
195 * character to specify MCHAR, MLINE or MBLOCK.
196 */
197 clip_star.format = RegisterClipboardFormat("VimClipboard2");
198 clip_star.format_raw = RegisterClipboardFormat("VimRawBytes");
199}
200
201/* Type used for the clipboard type of Vim's data. */
202typedef struct
203{
204 int type; /* MCHAR, MBLOCK or MLINE */
205 int txtlen; /* length of CF_TEXT in bytes */
206 int ucslen; /* length of CF_UNICODETEXT in words */
207 int rawlen; /* length of clip_star.format_raw, including encoding,
208 excluding terminating NUL */
209} VimClipType_t;
210
211/*
212 * Make vim the owner of the current selection. Return OK upon success.
213 */
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100214 int
Bram Moolenaar1266d672017-02-01 13:43:36 +0100215clip_mch_own_selection(VimClipboard *cbd UNUSED)
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100216{
217 /*
218 * Never actually own the clipboard. If another application sets the
219 * clipboard, we don't want to think that we still own it.
220 */
221 return FAIL;
222}
223
224/*
225 * Make vim NOT the owner of the current selection.
226 */
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100227 void
Bram Moolenaar1266d672017-02-01 13:43:36 +0100228clip_mch_lose_selection(VimClipboard *cbd UNUSED)
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100229{
230 /* Nothing needs to be done here */
231}
232
233/*
234 * Copy "str[*size]" into allocated memory, changing CR-NL to NL.
235 * Return the allocated result and the size in "*size".
236 * Returns NULL when out of memory.
237 */
238 static char_u *
239crnl_to_nl(const char_u *str, int *size)
240{
241 int pos = 0;
242 int str_len = *size;
243 char_u *ret;
244 char_u *retp;
245
246 /* Avoid allocating zero bytes, it generates an error message. */
Bram Moolenaar18a4ba22019-05-24 19:39:03 +0200247 ret = alloc(str_len == 0 ? 1 : str_len);
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100248 if (ret != NULL)
249 {
250 retp = ret;
251 for (pos = 0; pos < str_len; ++pos)
252 {
253 if (str[pos] == '\r' && str[pos + 1] == '\n')
254 {
255 ++pos;
256 --(*size);
257 }
258 *retp++ = str[pos];
259 }
260 }
261
262 return ret;
263}
264
265/*
266 * Wait for another process to Close the Clipboard.
267 * Returns TRUE for success.
268 */
269 static int
270vim_open_clipboard(void)
271{
272 int delay = 10;
273
274 while (!OpenClipboard(NULL))
275 {
276 if (delay > 500)
277 return FALSE; /* waited too long, give up */
278 Sleep(delay);
279 delay *= 2; /* wait for 10, 20, 40, 80, etc. msec */
280 }
281 return TRUE;
282}
283
284/*
285 * Get the current selection and put it in the clipboard register.
286 *
287 * NOTE: Must use GlobalLock/Unlock here to ensure Win32s compatibility.
288 * On NT/W95 the clipboard data is a fixed global memory object and
289 * so its handle = its pointer.
290 * On Win32s, however, co-operation with the Win16 system means that
291 * the clipboard data is moveable and its handle is not a pointer at all,
292 * so we can't just cast the return value of GetClipboardData to (char_u*).
293 * <VN>
294 */
295 void
296clip_mch_request_selection(VimClipboard *cbd)
297{
298 VimClipType_t metadata = { -1, -1, -1, -1 };
299 HGLOBAL hMem = NULL;
300 char_u *str = NULL;
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100301 char_u *to_free = NULL;
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100302 HGLOBAL rawh = NULL;
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100303 int str_size = 0;
304 int maxlen;
305 size_t n;
306
307 /*
308 * Don't pass GetActiveWindow() as an argument to OpenClipboard() because
309 * then we can't paste back into the same window for some reason - webb.
310 */
311 if (!vim_open_clipboard())
312 return;
313
314 /* Check for vim's own clipboard format first. This only gets the type of
315 * the data, still need to use CF_UNICODETEXT or CF_TEXT for the text. */
316 if (IsClipboardFormatAvailable(cbd->format))
317 {
318 VimClipType_t *meta_p;
319 HGLOBAL meta_h;
320
321 /* We have metadata on the clipboard; try to get it. */
322 if ((meta_h = GetClipboardData(cbd->format)) != NULL
323 && (meta_p = (VimClipType_t *)GlobalLock(meta_h)) != NULL)
324 {
325 /* The size of "VimClipType_t" changed, "rawlen" was added later.
326 * Only copy what is available for backwards compatibility. */
327 n = sizeof(VimClipType_t);
328 if (GlobalSize(meta_h) < n)
329 n = GlobalSize(meta_h);
330 memcpy(&metadata, meta_p, n);
331 GlobalUnlock(meta_h);
332 }
333 }
334
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100335 /* Check for Vim's raw clipboard format first. This is used without
336 * conversion, but only if 'encoding' matches. */
337 if (IsClipboardFormatAvailable(cbd->format_raw)
338 && metadata.rawlen > (int)STRLEN(p_enc))
339 {
340 /* We have raw data on the clipboard; try to get it. */
341 if ((rawh = GetClipboardData(cbd->format_raw)) != NULL)
342 {
343 char_u *rawp;
344
345 rawp = (char_u *)GlobalLock(rawh);
346 if (rawp != NULL && STRCMP(p_enc, rawp) == 0)
347 {
348 n = STRLEN(p_enc) + 1;
349 str = rawp + n;
350 str_size = (int)(metadata.rawlen - n);
351 }
352 else
353 {
354 GlobalUnlock(rawh);
355 rawh = NULL;
356 }
357 }
358 }
359 if (str == NULL)
360 {
Bram Moolenaar264b74f2019-01-24 17:18:42 +0100361 /* Try to get the clipboard in Unicode if it's not an empty string. */
362 if (IsClipboardFormatAvailable(CF_UNICODETEXT) && metadata.ucslen != 0)
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100363 {
Bram Moolenaar264b74f2019-01-24 17:18:42 +0100364 HGLOBAL hMemW;
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100365
Bram Moolenaar264b74f2019-01-24 17:18:42 +0100366 if ((hMemW = GetClipboardData(CF_UNICODETEXT)) != NULL)
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100367 {
Bram Moolenaar264b74f2019-01-24 17:18:42 +0100368 WCHAR *hMemWstr = (WCHAR *)GlobalLock(hMemW);
369
370 /* Use the length of our metadata if possible, but limit it to
371 * the GlobalSize() for safety. */
372 maxlen = (int)(GlobalSize(hMemW) / sizeof(WCHAR));
373 if (metadata.ucslen >= 0)
374 {
375 if (metadata.ucslen > maxlen)
376 str_size = maxlen;
377 else
378 str_size = metadata.ucslen;
379 }
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100380 else
Bram Moolenaar264b74f2019-01-24 17:18:42 +0100381 {
382 for (str_size = 0; str_size < maxlen; ++str_size)
383 if (hMemWstr[str_size] == NUL)
384 break;
385 }
386 to_free = str = utf16_to_enc((short_u *)hMemWstr, &str_size);
387 GlobalUnlock(hMemW);
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100388 }
Bram Moolenaar264b74f2019-01-24 17:18:42 +0100389 }
Bram Moolenaarc69efcb2019-02-27 14:12:01 +0100390 /* Get the clipboard in the Active codepage. */
391 else if (IsClipboardFormatAvailable(CF_TEXT))
Bram Moolenaar264b74f2019-01-24 17:18:42 +0100392 {
393 if ((hMem = GetClipboardData(CF_TEXT)) != NULL)
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100394 {
Bram Moolenaar264b74f2019-01-24 17:18:42 +0100395 str = (char_u *)GlobalLock(hMem);
396
397 /* The length is either what our metadata says or the strlen().
398 * But limit it to the GlobalSize() for safety. */
399 maxlen = (int)GlobalSize(hMem);
400 if (metadata.txtlen >= 0)
401 {
402 if (metadata.txtlen > maxlen)
403 str_size = maxlen;
404 else
405 str_size = metadata.txtlen;
406 }
407 else
408 {
409 for (str_size = 0; str_size < maxlen; ++str_size)
410 if (str[str_size] == NUL)
411 break;
412 }
413
Bram Moolenaar264b74f2019-01-24 17:18:42 +0100414 /* The text is in the active codepage. Convert to
415 * 'encoding', going through UTF-16. */
416 acp_to_enc(str, str_size, &to_free, &maxlen);
417 if (to_free != NULL)
418 {
419 str_size = maxlen;
420 str = to_free;
421 }
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100422 }
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100423 }
424 }
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100425
426 if (str != NULL && *str != NUL)
427 {
428 char_u *temp_clipboard;
429
430 /* If the type is not known detect it. */
431 if (metadata.type == -1)
432 metadata.type = MAUTO;
433
434 /* Translate <CR><NL> into <NL>. */
435 temp_clipboard = crnl_to_nl(str, &str_size);
436 if (temp_clipboard != NULL)
437 {
438 clip_yank_selection(metadata.type, temp_clipboard, str_size, cbd);
439 vim_free(temp_clipboard);
440 }
441 }
442
443 /* unlock the global object */
444 if (hMem != NULL)
445 GlobalUnlock(hMem);
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100446 if (rawh != NULL)
447 GlobalUnlock(rawh);
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100448 CloseClipboard();
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100449 vim_free(to_free);
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100450}
451
452/*
453 * Send the current selection to the clipboard.
454 */
455 void
456clip_mch_set_selection(VimClipboard *cbd)
457{
458 char_u *str = NULL;
459 VimClipType_t metadata;
460 long_u txtlen;
461 HGLOBAL hMemRaw = NULL;
462 HGLOBAL hMem = NULL;
463 HGLOBAL hMemVim = NULL;
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100464 HGLOBAL hMemW = NULL;
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100465
466 /* If the '*' register isn't already filled in, fill it in now */
467 cbd->owned = TRUE;
468 clip_get_selection(cbd);
469 cbd->owned = FALSE;
470
471 /* Get the text to be put on the clipboard, with CR-LF. */
472 metadata.type = clip_convert_selection(&str, &txtlen, cbd);
473 if (metadata.type < 0)
474 return;
475 metadata.txtlen = (int)txtlen;
476 metadata.ucslen = 0;
477 metadata.rawlen = 0;
478
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100479 /* Always set the raw bytes: 'encoding', NUL and the text. This is used
480 * when copy/paste from/to Vim with the same 'encoding', so that illegal
481 * bytes can also be copied and no conversion is needed. */
482 {
483 LPSTR lpszMemRaw;
484
485 metadata.rawlen = (int)(txtlen + STRLEN(p_enc) + 1);
486 hMemRaw = (LPSTR)GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE,
487 metadata.rawlen + 1);
488 lpszMemRaw = (LPSTR)GlobalLock(hMemRaw);
489 if (lpszMemRaw != NULL)
490 {
491 STRCPY(lpszMemRaw, p_enc);
492 memcpy(lpszMemRaw + STRLEN(p_enc) + 1, str, txtlen + 1);
493 GlobalUnlock(hMemRaw);
494 }
495 else
496 metadata.rawlen = 0;
497 }
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100498
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100499 {
500 WCHAR *out;
501 int len = metadata.txtlen;
502
503 /* Convert the text to UTF-16. This is put on the clipboard as
504 * CF_UNICODETEXT. */
505 out = (WCHAR *)enc_to_utf16(str, &len);
506 if (out != NULL)
507 {
508 WCHAR *lpszMemW;
509
510 /* Convert the text for CF_TEXT to Active codepage. Otherwise it's
511 * p_enc, which has no relation to the Active codepage. */
512 metadata.txtlen = WideCharToMultiByte(GetACP(), 0, out, len,
513 NULL, 0, 0, 0);
514 vim_free(str);
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200515 str = alloc(metadata.txtlen == 0 ? 1 : metadata.txtlen);
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100516 if (str == NULL)
517 {
518 vim_free(out);
519 return; /* out of memory */
520 }
521 WideCharToMultiByte(GetACP(), 0, out, len,
Bram Moolenaar2982e702013-07-01 21:08:48 +0200522 (LPSTR)str, metadata.txtlen, 0, 0);
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100523
524 /* Allocate memory for the UTF-16 text, add one NUL word to
525 * terminate the string. */
526 hMemW = (LPSTR)GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE,
527 (len + 1) * sizeof(WCHAR));
528 lpszMemW = (WCHAR *)GlobalLock(hMemW);
529 if (lpszMemW != NULL)
530 {
531 memcpy(lpszMemW, out, len * sizeof(WCHAR));
532 lpszMemW[len] = NUL;
533 GlobalUnlock(hMemW);
534 }
535 vim_free(out);
536 metadata.ucslen = len;
537 }
538 }
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100539
540 /* Allocate memory for the text, add one NUL byte to terminate the string.
541 */
542 hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, metadata.txtlen + 1);
543 {
544 LPSTR lpszMem = (LPSTR)GlobalLock(hMem);
545
546 if (lpszMem)
547 {
Bram Moolenaar2982e702013-07-01 21:08:48 +0200548 vim_strncpy((char_u *)lpszMem, str, metadata.txtlen);
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100549 GlobalUnlock(hMem);
550 }
551 }
552
553 /* Set up metadata: */
554 {
555 VimClipType_t *lpszMemVim = NULL;
556
557 hMemVim = GlobalAlloc(GMEM_MOVEABLE|GMEM_DDESHARE,
558 sizeof(VimClipType_t));
559 lpszMemVim = (VimClipType_t *)GlobalLock(hMemVim);
560 memcpy(lpszMemVim, &metadata, sizeof(metadata));
561 GlobalUnlock(hMemVim);
562 }
563
564 /*
565 * Open the clipboard, clear it and put our text on it.
566 * Always set our Vim format. Put Unicode and plain text on it.
567 *
568 * Don't pass GetActiveWindow() as an argument to OpenClipboard()
569 * because then we can't paste back into the same window for some
570 * reason - webb.
571 */
572 if (vim_open_clipboard())
573 {
574 if (EmptyClipboard())
575 {
576 SetClipboardData(cbd->format, hMemVim);
577 hMemVim = 0;
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100578 if (hMemW != NULL)
579 {
580 if (SetClipboardData(CF_UNICODETEXT, hMemW) != NULL)
581 hMemW = NULL;
582 }
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100583 /* Always use CF_TEXT. On Win98 Notepad won't obtain the
584 * CF_UNICODETEXT text, only CF_TEXT. */
585 SetClipboardData(CF_TEXT, hMem);
586 hMem = 0;
587 }
588 CloseClipboard();
589 }
590
591 vim_free(str);
592 /* Free any allocations we didn't give to the clipboard: */
593 if (hMemRaw)
594 GlobalFree(hMemRaw);
595 if (hMem)
596 GlobalFree(hMem);
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100597 if (hMemW)
598 GlobalFree(hMemW);
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100599 if (hMemVim)
600 GlobalFree(hMemVim);
601}
602
603#endif /* FEAT_CLIPBOARD */
604
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100605/*
606 * Note: the following two functions are only guaranteed to work when using
607 * valid MS-Windows codepages or when iconv() is available.
608 */
609
610/*
611 * Convert "str" from 'encoding' to UTF-16.
612 * Input in "str" with length "*lenp". When "lenp" is NULL, use strlen().
613 * Output is returned as an allocated string. "*lenp" is set to the length of
614 * the result. A trailing NUL is always added.
615 * Returns NULL when out of memory.
616 */
617 short_u *
618enc_to_utf16(char_u *str, int *lenp)
619{
620 vimconv_T conv;
621 WCHAR *ret;
622 char_u *allocbuf = NULL;
623 int len_loc;
624 int length;
625
626 if (lenp == NULL)
627 {
628 len_loc = (int)STRLEN(str) + 1;
629 lenp = &len_loc;
630 }
631
632 if (enc_codepage > 0)
633 {
634 /* We can do any CP### -> UTF-16 in one pass, and we can do it
635 * without iconv() (convert_* may need iconv). */
Bram Moolenaar2982e702013-07-01 21:08:48 +0200636 MultiByteToWideChar_alloc(enc_codepage, 0, (LPCSTR)str, *lenp,
637 &ret, &length);
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100638 }
639 else
640 {
641 /* Use "latin1" by default, we might be called before we have p_enc
642 * set up. Convert to utf-8 first, works better with iconv(). Does
643 * nothing if 'encoding' is "utf-8". */
644 conv.vc_type = CONV_NONE;
645 if (convert_setup(&conv, p_enc ? p_enc : (char_u *)"latin1",
646 (char_u *)"utf-8") == FAIL)
647 return NULL;
648 if (conv.vc_type != CONV_NONE)
649 {
650 str = allocbuf = string_convert(&conv, str, lenp);
651 if (str == NULL)
652 return NULL;
653 }
654 convert_setup(&conv, NULL, NULL);
655
656 length = utf8_to_utf16(str, *lenp, NULL, NULL);
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200657 ret = ALLOC_MULT(WCHAR, length + 1);
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100658 if (ret != NULL)
659 {
660 utf8_to_utf16(str, *lenp, (short_u *)ret, NULL);
661 ret[length] = 0;
662 }
663
664 vim_free(allocbuf);
665 }
666
667 *lenp = length;
668 return (short_u *)ret;
669}
670
671/*
672 * Convert an UTF-16 string to 'encoding'.
673 * Input in "str" with length (counted in wide characters) "*lenp". When
674 * "lenp" is NULL, use wcslen().
675 * Output is returned as an allocated string. If "*lenp" is not NULL it is
676 * set to the length of the result.
677 * Returns NULL when out of memory.
678 */
679 char_u *
680utf16_to_enc(short_u *str, int *lenp)
681{
682 vimconv_T conv;
683 char_u *utf8_str = NULL, *enc_str = NULL;
684 int len_loc;
685
686 if (lenp == NULL)
687 {
688 len_loc = (int)wcslen(str) + 1;
689 lenp = &len_loc;
690 }
691
692 if (enc_codepage > 0)
693 {
694 /* We can do any UTF-16 -> CP### in one pass. */
695 int length;
696
697 WideCharToMultiByte_alloc(enc_codepage, 0, str, *lenp,
698 (LPSTR *)&enc_str, &length, 0, 0);
699 *lenp = length;
700 return enc_str;
701 }
702
703 /* Avoid allocating zero bytes, it generates an error message. */
704 utf8_str = alloc(utf16_to_utf8(str, *lenp == 0 ? 1 : *lenp, NULL));
705 if (utf8_str != NULL)
706 {
707 *lenp = utf16_to_utf8(str, *lenp, utf8_str);
708
709 /* We might be called before we have p_enc set up. */
710 conv.vc_type = CONV_NONE;
711 convert_setup(&conv, (char_u *)"utf-8",
712 p_enc? p_enc: (char_u *)"latin1");
713 if (conv.vc_type == CONV_NONE)
714 {
715 /* p_enc is utf-8, so we're done. */
716 enc_str = utf8_str;
717 }
718 else
719 {
720 enc_str = string_convert(&conv, utf8_str, lenp);
721 vim_free(utf8_str);
722 }
723
724 convert_setup(&conv, NULL, NULL);
725 }
726
727 return enc_str;
728}
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100729
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100730/*
731 * Convert from the active codepage to 'encoding'.
732 * Input is "str[str_size]".
733 * The result is in allocated memory: "out[outlen]". With terminating NUL.
734 */
735 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +0100736acp_to_enc(
737 char_u *str,
738 int str_size,
739 char_u **out,
740 int *outlen)
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100741
742{
743 LPWSTR widestr;
744
Bram Moolenaar2982e702013-07-01 21:08:48 +0200745 MultiByteToWideChar_alloc(GetACP(), 0, (LPCSTR)str, str_size,
746 &widestr, outlen);
Bram Moolenaar693e40c2013-02-26 14:56:42 +0100747 if (widestr != NULL)
748 {
749 ++*outlen; /* Include the 0 after the string */
750 *out = utf16_to_enc((short_u *)widestr, outlen);
751 vim_free(widestr);
752 }
753}
Bram Moolenaarb1692e22014-03-12 19:24:37 +0100754
755/*
756 * Convert from 'encoding' to the active codepage.
757 * Input is "str[str_size]".
758 * The result is in allocated memory: "out[outlen]". With terminating NUL.
759 */
760 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +0100761enc_to_acp(
762 char_u *str,
763 int str_size,
764 char_u **out,
765 int *outlen)
Bram Moolenaarb1692e22014-03-12 19:24:37 +0100766
767{
768 LPWSTR widestr;
769 int len = str_size;
770
771 widestr = (WCHAR *)enc_to_utf16(str, &len);
772 if (widestr != NULL)
773 {
774 WideCharToMultiByte_alloc(GetACP(), 0, widestr, len,
775 (LPSTR *)out, outlen, 0, 0);
776 vim_free(widestr);
777 }
778}