1 /* strfuncs.cc: misc funcs that don't belong anywhere else
3 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 2005, 2006, 2007, 2008, 2009, 2010, 2011 Red Hat, Inc.
6 This file is part of Cygwin.
8 This software is a copyrighted work licensed under the terms of the
9 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
22 /* Transform characters invalid for Windows filenames to the Unicode private
23 use area in the U+f0XX range. The affected characters are all control
24 chars 1 <= c <= 31, as well as the characters " * : < > ? |. The backslash
25 is affected as well, but we can't transform it as long as we accept Win32
27 static const WCHAR tfx_chars[] = {
28 0, 0xf000 | 1, 0xf000 | 2, 0xf000 | 3,
29 0xf000 | 4, 0xf000 | 5, 0xf000 | 6, 0xf000 | 7,
30 0xf000 | 8, 0xf000 | 9, 0xf000 | 10, 0xf000 | 11,
31 0xf000 | 12, 0xf000 | 13, 0xf000 | 14, 0xf000 | 15,
32 0xf000 | 16, 0xf000 | 17, 0xf000 | 18, 0xf000 | 19,
33 0xf000 | 20, 0xf000 | 21, 0xf000 | 22, 0xf000 | 23,
34 0xf000 | 24, 0xf000 | 25, 0xf000 | 26, 0xf000 | 27,
35 0xf000 | 28, 0xf000 | 29, 0xf000 | 30, 0xf000 | 31,
36 ' ', '!', 0xf000 | '"', '#',
38 '(', ')', 0xf000 | '*', '+',
42 '8', '9', 0xf000 | ':', ';',
43 0xf000 | '<', '=', 0xf000 | '>', 0xf000 | '?',
59 0xf000 | '|', '}', '~', 127
62 /* This is the table for the reverse functionality in sys_cp_wcstombs.
63 It differs deliberately in two code places (space and dot) to allow
64 converting back space and dot on filesystems only supporting DOS
66 static const WCHAR tfx_rev_chars[] = {
67 0, 0xf000 | 1, 0xf000 | 2, 0xf000 | 3,
68 0xf000 | 4, 0xf000 | 5, 0xf000 | 6, 0xf000 | 7,
69 0xf000 | 8, 0xf000 | 9, 0xf000 | 10, 0xf000 | 11,
70 0xf000 | 12, 0xf000 | 13, 0xf000 | 14, 0xf000 | 15,
71 0xf000 | 16, 0xf000 | 17, 0xf000 | 18, 0xf000 | 19,
72 0xf000 | 20, 0xf000 | 21, 0xf000 | 22, 0xf000 | 23,
73 0xf000 | 24, 0xf000 | 25, 0xf000 | 26, 0xf000 | 27,
74 0xf000 | 28, 0xf000 | 29, 0xf000 | 30, 0xf000 | 31,
75 0xf000 | ' ', '!', 0xf000 | '"', '#',
77 '(', ')', 0xf000 | '*', '+',
78 ',', '-', 0xf000 | '.', '\\',
81 '8', '9', 0xf000 | ':', ';',
82 0xf000 | '<', '=', 0xf000 | '>', 0xf000 | '?',
98 0xf000 | '|', '}', '~', 127
102 transform_chars (PWCHAR path, PWCHAR path_end)
104 for (; path <= path_end; ++path)
106 *path = tfx_chars[*path];
109 /* The SJIS, JIS and eucJP conversion in newlib does not use UTF as
110 wchar_t character representation. That's unfortunate for us since
111 we require UTF for the OS. What we do here is to have our own
112 implementation of the base functions for the conversion using
113 the MulitByteToWideChar/WideCharToMultiByte functions. */
115 /* FIXME: We can't support JIS (ISO-2022-JP) at all right now. It's a
116 stateful charset encoding. The translation from mbtowc to
117 MulitByteToWideChar is quite complex. Given that we support SJIS and
118 eucJP, the both most used Japanese charset encodings, this shouldn't
119 be such a big problem. */
121 /* GBK, eucKR, and Big5 conversions are not available so far in newlib. */
124 __db_wctomb (struct _reent *r, char *s, wchar_t wchar, UINT cp)
135 BOOL def_used = false;
136 int ret = WideCharToMultiByte (cp, WC_NO_BEST_FIT_CHARS, &wchar, 1, s,
138 if (ret > 0 && !def_used)
146 __sjis_wctomb (struct _reent *r, char *s, wchar_t wchar, const char *charset,
149 return __db_wctomb (r,s, wchar, 932);
153 __eucjp_wctomb (struct _reent *r, char *s, wchar_t wchar, const char *charset,
156 /* Unfortunately, the Windows eucJP codepage 20932 is not really 100%
157 compatible to eucJP. It's a cute approximation which makes it a
159 The JIS-X-0212 three byte codes (0x8f,0xa1-0xfe,0xa1-0xfe) are folded
160 into two byte codes as follows: The 0x8f is stripped, the next byte is
161 taken as is, the third byte is mapped into the lower 7-bit area by
162 masking it with 0x7f. So, for instance, the eucJP code 0x8f,0xdd,0xf8
163 becomes 0xdd,0x78 in CP 20932.
165 To be really eucJP compatible, we have to map the JIS-X-0212 characters
166 between CP 20932 and eucJP ourselves. */
176 BOOL def_used = false;
177 int ret = WideCharToMultiByte (20932, WC_NO_BEST_FIT_CHARS, &wchar, 1, s,
179 if (ret > 0 && !def_used)
181 /* CP20932 representation of JIS-X-0212 character? */
182 if (ret == 2 && (unsigned char) s[1] <= 0x7f)
184 /* Yes, convert to eucJP three byte sequence */
198 __gbk_wctomb (struct _reent *r, char *s, wchar_t wchar, const char *charset,
201 return __db_wctomb (r,s, wchar, 936);
205 __kr_wctomb (struct _reent *r, char *s, wchar_t wchar, const char *charset,
208 return __db_wctomb (r,s, wchar, 949);
212 __big5_wctomb (struct _reent *r, char *s, wchar_t wchar, const char *charset,
215 return __db_wctomb (r,s, wchar, 950);
219 __db_mbtowc (struct _reent *r, wchar_t *pwc, const char *s, size_t n, UINT cp,
226 return 0; /* not state-dependent */
234 if (state->__count == 0)
236 if (*(unsigned char *) s < 0x80)
238 *pwc = *(unsigned char *) s;
241 size_t cnt = min (n, 2);
242 ret = MultiByteToWideChar (cp, MB_ERR_INVALID_CHARS, s, cnt, pwc, 1);
248 state->__value.__wchb[0] = *s;
251 /* These Win32 functions are really crappy. Assuming n is 2 but the
252 first byte is a singlebyte charcode, the function does not convert
253 that byte and return 1, rather it just returns 0. So, what we do
254 here is to check if the first byte returns a valid value... */
255 else if (MultiByteToWideChar (cp, MB_ERR_INVALID_CHARS, s, 1, pwc, 1))
260 state->__value.__wchb[state->__count] = *s;
261 ret = MultiByteToWideChar (cp, MB_ERR_INVALID_CHARS,
262 (const char *) state->__value.__wchb, 2, pwc, 1);
273 __sjis_mbtowc (struct _reent *r, wchar_t *pwc, const char *s, size_t n,
274 const char *charset, mbstate_t *state)
276 return __db_mbtowc (r, pwc, s, n, 932, state);
280 __eucjp_mbtowc (struct _reent *r, wchar_t *pwc, const char *s, size_t n,
281 const char *charset, mbstate_t *state)
283 /* See comment in __eucjp_wctomb above. */
288 return 0; /* not state-dependent */
296 if (state->__count == 0)
298 if (*(unsigned char *) s < 0x80)
300 *pwc = *(unsigned char *) s;
303 if (*(unsigned char *) s == 0x8f) /* JIS-X-0212 lead byte? */
305 /* Yes. Store sequence in mbstate and handle in the __count != 0
306 case at the end of the function. */
308 for (i = 0; i < 3 && i < n; i++)
309 state->__value.__wchb[i] = s[i];
310 if ((state->__count = i) < 3) /* Incomplete sequence? */
315 size_t cnt = min (n, 2);
316 if (MultiByteToWideChar (20932, MB_ERR_INVALID_CHARS, s, cnt, pwc, 1))
321 state->__value.__wchb[0] = *s;
324 else if (MultiByteToWideChar (20932, MB_ERR_INVALID_CHARS, s, 1, pwc, 1))
329 state->__value.__wchb[state->__count++] = *s;
332 if (state->__value.__wchb[0] == 0x8f)
334 if (state->__count == 2)
338 state->__value.__wchb[state->__count] = s[1];
341 /* Ok, we have a full JIS-X-0212 sequence in mbstate. Convert it
342 to the CP 20932 representation and feed it to MultiByteToWideChar. */
343 state->__value.__wchb[0] = state->__value.__wchb[1];
344 state->__value.__wchb[1] = state->__value.__wchb[2] & 0x7f;
346 if (!MultiByteToWideChar (20932, MB_ERR_INVALID_CHARS,
347 (const char *) state->__value.__wchb, 2, pwc, 1))
357 __gbk_mbtowc (struct _reent *r, wchar_t *pwc, const char *s, size_t n,
358 const char *charset, mbstate_t *state)
360 return __db_mbtowc (r, pwc, s, n, 936, state);
364 __kr_mbtowc (struct _reent *r, wchar_t *pwc, const char *s, size_t n,
365 const char *charset, mbstate_t *state)
367 return __db_mbtowc (r, pwc, s, n, 949, state);
371 __big5_mbtowc (struct _reent *r, wchar_t *pwc, const char *s, size_t n,
372 const char *charset, mbstate_t *state)
374 return __db_mbtowc (r, pwc, s, n, 950, state);
377 /* Our own sys_wcstombs/sys_mbstowcs functions differ from the
378 wcstombs/mbstowcs API in three ways:
380 - The UNICODE private use area is used in filenames to specify
381 characters not allowed in Windows filenames ('*', '?', etc).
382 The sys_wcstombs converts characters in the private use area
383 back to the corresponding ASCII chars.
385 - If a wide character in a filename has no representation in the current
386 multibyte charset, then usually you wouldn't be able to access the
387 file. To fix this problem, sys_wcstombs creates a replacement multibyte
388 sequences for the non-representable wide-char. The sequence starts with
389 an ASCII CAN (0x18, Ctrl-X), followed by the UTF-8 representation of the
390 character. The sys_(cp_)mbstowcs function detects ASCII CAN characters
391 in the input multibyte string and converts the following multibyte
392 sequence in by treating it as an UTF-8 char. If that fails, the ASCII
393 CAN was probably standalone and it gets just copied over as ASCII CAN.
395 - The functions always create 0-terminated results, no matter what.
396 If the result is truncated due to buffer size, it's a bug in Cygwin
397 and the buffer in the calling function should be raised. */
399 sys_cp_wcstombs (wctomb_p f_wctomb, const char *charset, char *dst, size_t len,
400 const wchar_t *src, size_t nwc)
404 wchar_t *pwcs = (wchar_t *) src;
409 memset (&ps, 0, sizeof ps);
412 while (n < len && nwc-- > 0)
418 /* Convert UNICODE private use area. Reverse functionality for the
419 ASCII area <= 0x7f (only for path names) is transform_chars above.
420 Reverse functionality for invalid bytes in a multibyte sequence is
421 in sys_cp_mbstowcs below. */
422 if ((pw & 0xff00) == 0xf000
423 && (((cwc = (pw & 0xff)) <= 0x7f && tfx_rev_chars[cwc] >= 0xf000)
424 || (cwc >= 0x80 && MB_CUR_MAX > 1)))
431 bytes = f_wctomb (_REENT, buf, pw, charset, &ps);
432 if (bytes == -1 && *charset != 'U'/*TF-8*/)
434 /* Convert chars invalid in the current codepage to a sequence
435 ASCII CAN; UTF-8 representation of invalid char. */
436 buf[0] = 0x18; /* ASCII CAN */
437 bytes = __utf8_wctomb (_REENT, buf + 1, pw, charset, &ps);
444 ++bytes; /* Add the ASCII CAN to the byte count. */
445 if (ps.__count == -4 && nwc > 0)
447 /* First half of a surrogate pair. */
449 if ((*pwcs & 0xfc00) != 0xdc00) /* Invalid second half. */
455 bytes += __utf8_wctomb (_REENT, buf + bytes, *pwcs, charset,
461 if (n + bytes <= len)
466 for (int i = 0; i < bytes; ++i)
477 n = (n < len) ? n : len - 1;
485 sys_wcstombs (char *dst, size_t len, const wchar_t * src, size_t nwc)
487 return sys_cp_wcstombs (cygheap->locale.wctomb, cygheap->locale.charset,
491 /* Allocate a buffer big enough for the string, always including the
492 terminating '\0'. The buffer pointer is returned in *dst_p, the return
493 value is the number of bytes written to the buffer, as usual.
494 The "type" argument determines where the resulting buffer is stored.
495 It's either one of the cygheap_types values, or it's "HEAP_NOTHEAP".
496 In the latter case the allocation uses simple calloc.
498 Note that this code is shared by cygserver (which requires it via
499 __small_vsprintf) and so when built there plain calloc is the
502 sys_wcstombs_alloc (char **dst_p, int type, const wchar_t *src, size_t nwc)
506 ret = sys_wcstombs (NULL, (size_t) -1, src, nwc);
509 size_t dlen = ret + 1;
511 if (type == HEAP_NOTHEAP)
512 *dst_p = (char *) calloc (dlen, sizeof (char));
514 *dst_p = (char *) ccalloc ((cygheap_types) type, dlen, sizeof (char));
517 ret = sys_wcstombs (*dst_p, dlen, src, nwc);
522 /* sys_cp_mbstowcs is actually most of the time called as sys_mbstowcs with
523 a 0 codepage. If cp is not 0, the codepage is evaluated and used for the
524 conversion. This is so that fhandler_console can switch to an alternate
525 charset, which is the charset returned by GetConsoleCP (). Most of the
526 time this is used for box and line drawing characters. */
528 sys_cp_mbstowcs (mbtowc_p f_mbtowc, const char *charset, wchar_t *dst,
529 size_t dlen, const char *src, size_t nms)
532 unsigned const char *pmbs = (unsigned const char *) src;
539 memset (&ps, 0, sizeof ps);
542 while (len > 0 && nms > 0)
544 /* ASCII CAN handling. */
547 /* Sanity check: If this is a lead CAN byte for a following UTF-8
548 sequence, there must be at least two more bytes left, and the
549 next byte must be a valid UTF-8 start byte. If the charset
550 isn't UTF-8 anyway, try to convert the following bytes as UTF-8
552 if (nms > 2 && pmbs[1] >= 0xc2 && pmbs[1] <= 0xf4 && *charset != 'U'/*TF-8*/)
554 bytes = __utf8_mbtowc (_REENT, ptr, (const char *) pmbs + 1,
555 nms - 1, charset, &ps);
558 /* Invalid UTF-8 sequence? Treat the ASCII CAN character as
559 stand-alone ASCII CAN char. */
563 memset (&ps, 0, sizeof ps);
567 ++bytes; /* Count CAN byte */
568 if (bytes > 1 && ps.__count == 4)
570 /* First half of a surrogate. */
571 wchar_t *ptr2 = dst ? ptr + 1 : NULL;
572 int bytes2 = __utf8_mbtowc (_REENT, ptr2,
573 (const char *) pmbs + bytes,
574 nms - bytes, charset, &ps);
576 memset (&ps, 0, sizeof ps);
581 ptr = dst ? ptr + 1 : NULL;
587 /* Otherwise it's just a simple ASCII CAN. */
595 else if ((bytes = f_mbtowc (_REENT, ptr, (const char *) pmbs, nms,
598 /* The technique is based on a discussion here:
599 http://www.mail-archive.com/linux-utf8@nl.linux.org/msg00080.html
601 Invalid bytes in a multibyte secuence are converted to
602 the private use area which is already used to store ASCII
603 chars invalid in Windows filenames. This technque allows
604 to store them in a symmetric way. */
607 *ptr = L'\xf000' | *pmbs;
608 memset (&ps, 0, sizeof ps);
616 ptr = dst ? ptr + 1 : NULL;
629 count = (count < dlen) ? count : dlen - 1;
637 sys_mbstowcs (wchar_t * dst, size_t dlen, const char *src, size_t nms)
639 return sys_cp_mbstowcs (cygheap->locale.mbtowc, cygheap->locale.charset,
640 dst, dlen, src, nms);
643 /* Same as sys_wcstombs_alloc, just backwards. */
645 sys_mbstowcs_alloc (wchar_t **dst_p, int type, const char *src, size_t nms)
649 ret = sys_mbstowcs (NULL, (size_t) -1, src, nms);
652 size_t dlen = ret + 1;
654 if (type == HEAP_NOTHEAP)
655 *dst_p = (wchar_t *) calloc (dlen, sizeof (wchar_t));
657 *dst_p = (wchar_t *) ccalloc ((cygheap_types) type, dlen,
661 ret = sys_mbstowcs (*dst_p, dlen, src, nms);
666 /* Copy string, until c or <nul> is encountered.
667 NUL-terminate the destination string (s1).
668 Return pointer to terminating byte in dst string. */
670 strccpy (char *s1, const char **s2, char c)
672 while (**s2 && **s2 != c)
680 static WCHAR hex_wchars[] = L"0123456789abcdef";
683 RtlInt64ToHexUnicodeString (ULONGLONG value, PUNICODE_STRING dest,
686 USHORT len = append ? dest->Length : 0;
687 if (dest->MaximumLength - len < 16 * (int) sizeof (WCHAR))
688 return STATUS_BUFFER_OVERFLOW;
689 wchar_t *end = (PWCHAR) ((PBYTE) dest->Buffer + len);
690 register PWCHAR p = end + 16;
693 *p = hex_wchars[value & 0xf];
696 dest->Length += 16 * sizeof (WCHAR);
697 return STATUS_SUCCESS;