2013-07-20 01:28:34 +08:00
|
|
|
/* strfuncs.cc: string functions
|
2007-08-02 22:21:53 +08:00
|
|
|
|
|
|
|
This file is part of Cygwin.
|
|
|
|
|
|
|
|
This software is a copyrighted work licensed under the terms of the
|
|
|
|
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
|
|
|
|
details. */
|
|
|
|
|
|
|
|
#include "winsup.h"
|
2008-02-01 04:26:01 +08:00
|
|
|
#include <stdlib.h>
|
2012-03-08 17:36:11 +08:00
|
|
|
#include <sys/param.h>
|
2008-05-14 18:21:22 +08:00
|
|
|
#include <wchar.h>
|
2007-08-12 20:48:02 +08:00
|
|
|
#include <ntdll.h>
|
2008-02-01 04:26:01 +08:00
|
|
|
#include "path.h"
|
|
|
|
#include "fhandler.h"
|
|
|
|
#include "dtable.h"
|
|
|
|
#include "cygheap.h"
|
2007-08-02 22:21:53 +08:00
|
|
|
|
2009-11-02 19:42:04 +08:00
|
|
|
/* Transform characters invalid for Windows filenames to the Unicode private
|
|
|
|
use area in the U+f0XX range. The affected characters are all control
|
|
|
|
chars 1 <= c <= 31, as well as the characters " * : < > ? |. The backslash
|
|
|
|
is affected as well, but we can't transform it as long as we accept Win32
|
2010-04-23 19:07:35 +08:00
|
|
|
paths as input. */
|
2009-11-02 19:42:04 +08:00
|
|
|
static const WCHAR tfx_chars[] = {
|
2018-03-03 01:11:57 +08:00
|
|
|
0xf000 | 0, 0xf000 | 1, 0xf000 | 2, 0xf000 | 3,
|
2009-11-02 19:42:04 +08:00
|
|
|
0xf000 | 4, 0xf000 | 5, 0xf000 | 6, 0xf000 | 7,
|
|
|
|
0xf000 | 8, 0xf000 | 9, 0xf000 | 10, 0xf000 | 11,
|
|
|
|
0xf000 | 12, 0xf000 | 13, 0xf000 | 14, 0xf000 | 15,
|
|
|
|
0xf000 | 16, 0xf000 | 17, 0xf000 | 18, 0xf000 | 19,
|
|
|
|
0xf000 | 20, 0xf000 | 21, 0xf000 | 22, 0xf000 | 23,
|
|
|
|
0xf000 | 24, 0xf000 | 25, 0xf000 | 26, 0xf000 | 27,
|
|
|
|
0xf000 | 28, 0xf000 | 29, 0xf000 | 30, 0xf000 | 31,
|
2011-06-06 13:02:13 +08:00
|
|
|
' ', '!', 0xf000 | '"', '#',
|
|
|
|
'$', '%', '&', 39,
|
|
|
|
'(', ')', 0xf000 | '*', '+',
|
|
|
|
',', '-', '.', '\\',
|
|
|
|
'0', '1', '2', '3',
|
|
|
|
'4', '5', '6', '7',
|
|
|
|
'8', '9', 0xf000 | ':', ';',
|
2009-11-02 19:42:04 +08:00
|
|
|
0xf000 | '<', '=', 0xf000 | '>', 0xf000 | '?',
|
2011-06-06 13:02:13 +08:00
|
|
|
'@', 'A', 'B', 'C',
|
|
|
|
'D', 'E', 'F', 'G',
|
|
|
|
'H', 'I', 'J', 'K',
|
|
|
|
'L', 'M', 'N', 'O',
|
|
|
|
'P', 'Q', 'R', 'S',
|
|
|
|
'T', 'U', 'V', 'W',
|
|
|
|
'X', 'Y', 'Z', '[',
|
|
|
|
'\\', ']', '^', '_',
|
|
|
|
'`', 'a', 'b', 'c',
|
|
|
|
'd', 'e', 'f', 'g',
|
|
|
|
'h', 'i', 'j', 'k',
|
|
|
|
'l', 'm', 'n', 'o',
|
|
|
|
'p', 'q', 'r', 's',
|
|
|
|
't', 'u', 'v', 'w',
|
|
|
|
'x', 'y', 'z', '{',
|
2009-11-02 19:42:04 +08:00
|
|
|
0xf000 | '|', '}', '~', 127
|
|
|
|
};
|
|
|
|
|
2015-12-18 19:42:40 +08:00
|
|
|
/* This is the table for the reverse functionality in sys_wcstombs.
|
2010-04-23 19:07:35 +08:00
|
|
|
It differs deliberately in two code places (space and dot) to allow
|
|
|
|
converting back space and dot on filesystems only supporting DOS
|
|
|
|
filenames. */
|
|
|
|
static const WCHAR tfx_rev_chars[] = {
|
2018-03-03 01:11:57 +08:00
|
|
|
0xf000 | 0, 0xf000 | 1, 0xf000 | 2, 0xf000 | 3,
|
2010-04-23 19:07:35 +08:00
|
|
|
0xf000 | 4, 0xf000 | 5, 0xf000 | 6, 0xf000 | 7,
|
|
|
|
0xf000 | 8, 0xf000 | 9, 0xf000 | 10, 0xf000 | 11,
|
|
|
|
0xf000 | 12, 0xf000 | 13, 0xf000 | 14, 0xf000 | 15,
|
|
|
|
0xf000 | 16, 0xf000 | 17, 0xf000 | 18, 0xf000 | 19,
|
|
|
|
0xf000 | 20, 0xf000 | 21, 0xf000 | 22, 0xf000 | 23,
|
|
|
|
0xf000 | 24, 0xf000 | 25, 0xf000 | 26, 0xf000 | 27,
|
|
|
|
0xf000 | 28, 0xf000 | 29, 0xf000 | 30, 0xf000 | 31,
|
|
|
|
0xf000 | ' ', '!', 0xf000 | '"', '#',
|
2011-06-06 13:02:13 +08:00
|
|
|
'$', '%', '&', 39,
|
|
|
|
'(', ')', 0xf000 | '*', '+',
|
|
|
|
',', '-', 0xf000 | '.', '\\',
|
|
|
|
'0', '1', '2', '3',
|
|
|
|
'4', '5', '6', '7',
|
|
|
|
'8', '9', 0xf000 | ':', ';',
|
2010-04-23 19:07:35 +08:00
|
|
|
0xf000 | '<', '=', 0xf000 | '>', 0xf000 | '?',
|
2011-06-06 13:02:13 +08:00
|
|
|
'@', 'A', 'B', 'C',
|
|
|
|
'D', 'E', 'F', 'G',
|
|
|
|
'H', 'I', 'J', 'K',
|
|
|
|
'L', 'M', 'N', 'O',
|
|
|
|
'P', 'Q', 'R', 'S',
|
|
|
|
'T', 'U', 'V', 'W',
|
|
|
|
'X', 'Y', 'Z', '[',
|
|
|
|
'\\', ']', '^', '_',
|
|
|
|
'`', 'a', 'b', 'c',
|
|
|
|
'd', 'e', 'f', 'g',
|
|
|
|
'h', 'i', 'j', 'k',
|
|
|
|
'l', 'm', 'n', 'o',
|
|
|
|
'p', 'q', 'r', 's',
|
|
|
|
't', 'u', 'v', 'w',
|
|
|
|
'x', 'y', 'z', '{',
|
2010-04-23 19:07:35 +08:00
|
|
|
0xf000 | '|', '}', '~', 127
|
|
|
|
};
|
|
|
|
|
2009-11-02 19:42:04 +08:00
|
|
|
void
|
|
|
|
transform_chars (PWCHAR path, PWCHAR path_end)
|
|
|
|
{
|
|
|
|
for (; path <= path_end; ++path)
|
|
|
|
if (*path < 128)
|
|
|
|
*path = tfx_chars[*path];
|
|
|
|
}
|
|
|
|
|
2018-03-03 01:11:57 +08:00
|
|
|
PWCHAR
|
|
|
|
transform_chars_af_unix (PWCHAR out, const char *path, __socklen_t len)
|
|
|
|
{
|
|
|
|
len -= sizeof (__sa_family_t);
|
|
|
|
for (const unsigned char *p = (const unsigned char *) path; len-- > 0; ++p)
|
|
|
|
*out++ = (*p <= 0x7f) ? tfx_chars[*p] : *p;
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2023-02-21 05:29:37 +08:00
|
|
|
/* convert wint_t string to wchar_t string. Make sure dest
|
|
|
|
has room for at least twice as much characters to account
|
|
|
|
for surrogate pairs, plus a wchar_t NUL. */
|
|
|
|
extern "C" void
|
|
|
|
wcintowcs (wchar_t *dest, wint_t *src, size_t len)
|
|
|
|
{
|
|
|
|
while (*src && len-- > 0)
|
|
|
|
if (*src > 0xffff)
|
|
|
|
{
|
|
|
|
*dest++ = ((*src - 0x10000) >> 10) + 0xd800;
|
|
|
|
*dest++ = ((*src++ - 0x10000) & 0x3ff) + 0xdc00;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*dest++ = *src++;
|
|
|
|
*dest = '\0';
|
|
|
|
}
|
|
|
|
|
2023-02-22 17:03:03 +08:00
|
|
|
/* replacement function for wcrtomb, converting a UTF-32 char to a
|
|
|
|
multibyte string. */
|
|
|
|
extern "C" size_t
|
|
|
|
wirtomb (char *s, wint_t wi, mbstate_t *ps)
|
|
|
|
{
|
|
|
|
wchar_t wc[3] = { (wchar_t) wi, '\0', '\0' };
|
|
|
|
const wchar_t *wcp = wc;
|
|
|
|
size_t nwc = 1;
|
|
|
|
|
|
|
|
if (wi >= 0x10000)
|
|
|
|
{
|
|
|
|
wi -= 0x10000;
|
|
|
|
wc[0] = (wi >> 10) + 0xd800;
|
|
|
|
wc[1] = (wi & 0x3ff) + 0xdc00;
|
|
|
|
nwc = 2;
|
|
|
|
}
|
|
|
|
return wcsnrtombs (s, &wcp, nwc, SIZE_MAX, ps);
|
|
|
|
}
|
|
|
|
|
2023-02-14 19:20:20 +08:00
|
|
|
/* replacement function for mbrtowc, returning a wint_t representing
|
|
|
|
a UTF-32 value. */
|
2023-02-15 03:18:42 +08:00
|
|
|
extern "C" size_t
|
2023-02-14 19:20:20 +08:00
|
|
|
mbrtowi (wint_t *pwi, const char *s, size_t n, mbstate_t *ps)
|
|
|
|
{
|
|
|
|
size_t len, len2;
|
|
|
|
wchar_t w1, w2;
|
|
|
|
|
|
|
|
len = mbrtowc (&w1, s, n, ps);
|
|
|
|
if (len == (size_t) -1 || len == (size_t) -2)
|
|
|
|
return len;
|
|
|
|
*pwi = w1;
|
|
|
|
/* Convert surrogate pair to wint_t value */
|
|
|
|
if (len > 0 && w1 >= 0xd800 && w1 <= 0xdbff)
|
|
|
|
{
|
|
|
|
s += len;
|
|
|
|
n -= len;
|
|
|
|
len2 = mbrtowc (&w2, s, n, ps);
|
|
|
|
if (len2 > 0 && w2 >= 0xdc00 && w2 <= 0xdfff)
|
|
|
|
{
|
|
|
|
len += len2;
|
|
|
|
*pwi = (((w1 & 0x3ff) << 10) | (w2 & 0x3ff)) + 0x10000;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
len = (size_t) -1;
|
|
|
|
errno = EILSEQ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2023-03-01 17:44:52 +08:00
|
|
|
extern "C" size_t
|
|
|
|
mbsnrtowci(wint_t *dst, const char **src, size_t nms, size_t len, mbstate_t *ps)
|
|
|
|
{
|
|
|
|
wint_t *ptr = dst;
|
|
|
|
const char *tmp_src;
|
|
|
|
size_t max;
|
|
|
|
size_t count = 0;
|
|
|
|
size_t bytes;
|
|
|
|
|
|
|
|
if (dst == NULL)
|
|
|
|
{
|
|
|
|
/* Ignore original len value and do not alter src pointer if the
|
|
|
|
dst pointer is NULL. */
|
|
|
|
len = (size_t)-1;
|
|
|
|
tmp_src = *src;
|
|
|
|
src = &tmp_src;
|
|
|
|
}
|
|
|
|
max = len;
|
|
|
|
while (len > 0)
|
|
|
|
{
|
|
|
|
bytes = mbrtowi (ptr, *src, MB_CUR_MAX, ps);
|
|
|
|
if (bytes > 0)
|
|
|
|
{
|
|
|
|
*src += bytes;
|
|
|
|
nms -= bytes;
|
|
|
|
++count;
|
|
|
|
ptr = (dst == NULL) ? NULL : ptr + 1;
|
|
|
|
--len;
|
|
|
|
}
|
|
|
|
else if (bytes == 0)
|
|
|
|
{
|
|
|
|
*src = NULL;
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Deviation from standard: If the input is broken, the output
|
|
|
|
will be broken. I. e., we just copy the current byte over
|
|
|
|
into the wint_t destination and try to pick up on the next
|
|
|
|
byte. This is in line with the way fnmatch works. */
|
|
|
|
ps->__count = 0;
|
|
|
|
if (dst)
|
|
|
|
{
|
|
|
|
*ptr++ = (const wint_t) *(*src)++;
|
|
|
|
++count;
|
|
|
|
--nms;
|
|
|
|
--len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (size_t) max;
|
|
|
|
}
|
|
|
|
|
2009-04-06 18:50:11 +08:00
|
|
|
/* The SJIS, JIS and eucJP conversion in newlib does not use UTF as
|
2009-03-24 20:18:34 +08:00
|
|
|
wchar_t character representation. That's unfortunate for us since
|
|
|
|
we require UTF for the OS. What we do here is to have our own
|
|
|
|
implementation of the base functions for the conversion using
|
|
|
|
the MulitByteToWideChar/WideCharToMultiByte functions. */
|
|
|
|
|
2009-04-06 18:50:11 +08:00
|
|
|
/* FIXME: We can't support JIS (ISO-2022-JP) at all right now. It's a
|
|
|
|
stateful charset encoding. The translation from mbtowc to
|
|
|
|
MulitByteToWideChar is quite complex. Given that we support SJIS and
|
|
|
|
eucJP, the both most used Japanese charset encodings, this shouldn't
|
|
|
|
be such a big problem. */
|
|
|
|
|
2023-03-17 01:25:09 +08:00
|
|
|
/* GBK, GB18030, eucKR, and Big5 conversions are not available so far
|
|
|
|
in newlib. */
|
2009-03-24 20:18:34 +08:00
|
|
|
|
|
|
|
static int
|
|
|
|
__db_wctomb (struct _reent *r, char *s, wchar_t wchar, UINT cp)
|
|
|
|
{
|
|
|
|
if (s == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (wchar < 0x80)
|
|
|
|
{
|
|
|
|
*s = (char) wchar;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL def_used = false;
|
2009-04-06 18:50:11 +08:00
|
|
|
int ret = WideCharToMultiByte (cp, WC_NO_BEST_FIT_CHARS, &wchar, 1, s,
|
2009-05-15 03:49:37 +08:00
|
|
|
2, NULL, &def_used);
|
2009-03-24 20:18:34 +08:00
|
|
|
if (ret > 0 && !def_used)
|
|
|
|
return ret;
|
|
|
|
|
2022-01-18 17:13:04 +08:00
|
|
|
_REENT_ERRNO(r) = EILSEQ;
|
2009-03-24 20:18:34 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" int
|
2016-07-21 04:05:59 +08:00
|
|
|
__sjis_wctomb (struct _reent *r, char *s, wchar_t wchar, mbstate_t *state)
|
2007-08-02 22:21:53 +08:00
|
|
|
{
|
2009-03-24 20:18:34 +08:00
|
|
|
return __db_wctomb (r,s, wchar, 932);
|
2008-02-06 01:37:10 +08:00
|
|
|
}
|
|
|
|
|
2009-03-24 20:18:34 +08:00
|
|
|
extern "C" int
|
2016-07-21 04:05:59 +08:00
|
|
|
__eucjp_wctomb (struct _reent *r, char *s, wchar_t wchar, mbstate_t *state)
|
2009-03-24 20:18:34 +08:00
|
|
|
{
|
2009-04-06 18:50:11 +08:00
|
|
|
/* Unfortunately, the Windows eucJP codepage 20932 is not really 100%
|
|
|
|
compatible to eucJP. It's a cute approximation which makes it a
|
|
|
|
doublebyte codepage.
|
|
|
|
The JIS-X-0212 three byte codes (0x8f,0xa1-0xfe,0xa1-0xfe) are folded
|
|
|
|
into two byte codes as follows: The 0x8f is stripped, the next byte is
|
|
|
|
taken as is, the third byte is mapped into the lower 7-bit area by
|
|
|
|
masking it with 0x7f. So, for instance, the eucJP code 0x8f,0xdd,0xf8
|
|
|
|
becomes 0xdd,0x78 in CP 20932.
|
|
|
|
|
|
|
|
To be really eucJP compatible, we have to map the JIS-X-0212 characters
|
|
|
|
between CP 20932 and eucJP ourselves. */
|
|
|
|
if (s == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (wchar < 0x80)
|
|
|
|
{
|
|
|
|
*s = (char) wchar;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL def_used = false;
|
|
|
|
int ret = WideCharToMultiByte (20932, WC_NO_BEST_FIT_CHARS, &wchar, 1, s,
|
2009-05-15 03:49:37 +08:00
|
|
|
3, NULL, &def_used);
|
2009-04-06 18:50:11 +08:00
|
|
|
if (ret > 0 && !def_used)
|
|
|
|
{
|
|
|
|
/* CP20932 representation of JIS-X-0212 character? */
|
|
|
|
if (ret == 2 && (unsigned char) s[1] <= 0x7f)
|
|
|
|
{
|
|
|
|
/* Yes, convert to eucJP three byte sequence */
|
|
|
|
s[2] = s[1] | 0x80;
|
|
|
|
s[1] = s[0];
|
|
|
|
s[0] = 0x8f;
|
|
|
|
++ret;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-01-18 17:13:04 +08:00
|
|
|
_REENT_ERRNO(r) = EILSEQ;
|
2009-04-06 18:50:11 +08:00
|
|
|
return -1;
|
2009-03-24 20:18:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" int
|
2016-07-21 04:05:59 +08:00
|
|
|
__gbk_wctomb (struct _reent *r, char *s, wchar_t wchar, mbstate_t *state)
|
2009-03-24 20:18:34 +08:00
|
|
|
{
|
|
|
|
return __db_wctomb (r,s, wchar, 936);
|
|
|
|
}
|
|
|
|
|
2023-03-17 01:25:09 +08:00
|
|
|
extern "C" int
|
|
|
|
__gb18030_wctomb (struct _reent *r, char *s, wchar_t wchar, mbstate_t *state)
|
|
|
|
{
|
|
|
|
return __db_wctomb (r,s, wchar, 54936);
|
|
|
|
}
|
|
|
|
|
2009-03-24 20:18:34 +08:00
|
|
|
extern "C" int
|
2016-07-21 04:05:59 +08:00
|
|
|
__kr_wctomb (struct _reent *r, char *s, wchar_t wchar, mbstate_t *state)
|
2009-03-24 20:18:34 +08:00
|
|
|
{
|
2009-04-06 18:50:11 +08:00
|
|
|
return __db_wctomb (r,s, wchar, 949);
|
2009-03-24 20:18:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" int
|
2016-07-21 04:05:59 +08:00
|
|
|
__big5_wctomb (struct _reent *r, char *s, wchar_t wchar, mbstate_t *state)
|
2009-03-24 20:18:34 +08:00
|
|
|
{
|
|
|
|
return __db_wctomb (r,s, wchar, 950);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-04-06 18:50:11 +08:00
|
|
|
__db_mbtowc (struct _reent *r, wchar_t *pwc, const char *s, size_t n, UINT cp,
|
|
|
|
mbstate_t *state)
|
2009-03-24 20:18:34 +08:00
|
|
|
{
|
|
|
|
wchar_t dummy;
|
2007-08-02 22:21:53 +08:00
|
|
|
int ret;
|
|
|
|
|
2009-03-24 20:18:34 +08:00
|
|
|
if (s == NULL)
|
|
|
|
return 0; /* not state-dependent */
|
|
|
|
|
|
|
|
if (n == 0)
|
|
|
|
return -2;
|
2009-07-01 05:18:44 +08:00
|
|
|
|
2009-04-06 18:50:11 +08:00
|
|
|
if (pwc == NULL)
|
|
|
|
pwc = &dummy;
|
2009-03-24 20:18:34 +08:00
|
|
|
|
|
|
|
if (state->__count == 0)
|
|
|
|
{
|
|
|
|
if (*(unsigned char *) s < 0x80)
|
|
|
|
{
|
|
|
|
*pwc = *(unsigned char *) s;
|
|
|
|
return *s ? 1 : 0;
|
|
|
|
}
|
2012-03-08 17:36:11 +08:00
|
|
|
size_t cnt = MIN (n, 2);
|
2009-04-06 18:50:11 +08:00
|
|
|
ret = MultiByteToWideChar (cp, MB_ERR_INVALID_CHARS, s, cnt, pwc, 1);
|
2009-03-24 20:18:34 +08:00
|
|
|
if (ret)
|
2009-04-06 18:50:11 +08:00
|
|
|
return cnt;
|
2009-03-24 20:18:34 +08:00
|
|
|
if (n == 1)
|
|
|
|
{
|
2009-04-06 18:50:11 +08:00
|
|
|
state->__count = n;
|
2009-03-24 20:18:34 +08:00
|
|
|
state->__value.__wchb[0] = *s;
|
|
|
|
return -2;
|
|
|
|
}
|
2009-04-06 18:50:11 +08:00
|
|
|
/* These Win32 functions are really crappy. Assuming n is 2 but the
|
|
|
|
first byte is a singlebyte charcode, the function does not convert
|
|
|
|
that byte and return 1, rather it just returns 0. So, what we do
|
|
|
|
here is to check if the first byte returns a valid value... */
|
|
|
|
else if (MultiByteToWideChar (cp, MB_ERR_INVALID_CHARS, s, 1, pwc, 1))
|
|
|
|
return 1;
|
2022-01-18 17:13:04 +08:00
|
|
|
_REENT_ERRNO(r) = EILSEQ;
|
2009-03-24 20:18:34 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2009-04-06 18:50:11 +08:00
|
|
|
state->__value.__wchb[state->__count] = *s;
|
|
|
|
ret = MultiByteToWideChar (cp, MB_ERR_INVALID_CHARS,
|
|
|
|
(const char *) state->__value.__wchb, 2, pwc, 1);
|
2009-03-24 20:18:34 +08:00
|
|
|
if (!ret)
|
2007-10-24 00:26:28 +08:00
|
|
|
{
|
2022-01-18 17:13:04 +08:00
|
|
|
_REENT_ERRNO(r) = EILSEQ;
|
2009-03-24 20:18:34 +08:00
|
|
|
return -1;
|
2007-10-24 00:26:28 +08:00
|
|
|
}
|
2009-04-06 18:50:11 +08:00
|
|
|
state->__count = 0;
|
|
|
|
return 1;
|
2007-08-02 22:21:53 +08:00
|
|
|
}
|
|
|
|
|
2009-03-24 20:18:34 +08:00
|
|
|
extern "C" int
|
|
|
|
__sjis_mbtowc (struct _reent *r, wchar_t *pwc, const char *s, size_t n,
|
2016-07-21 04:05:59 +08:00
|
|
|
mbstate_t *state)
|
2009-03-24 20:18:34 +08:00
|
|
|
{
|
2010-01-24 20:29:49 +08:00
|
|
|
return __db_mbtowc (r, pwc, s, n, 932, state);
|
2009-03-24 20:18:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" int
|
|
|
|
__eucjp_mbtowc (struct _reent *r, wchar_t *pwc, const char *s, size_t n,
|
2016-07-21 04:05:59 +08:00
|
|
|
mbstate_t *state)
|
2009-03-24 20:18:34 +08:00
|
|
|
{
|
2009-04-06 18:50:11 +08:00
|
|
|
/* See comment in __eucjp_wctomb above. */
|
|
|
|
wchar_t dummy;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (s == NULL)
|
|
|
|
return 0; /* not state-dependent */
|
|
|
|
|
|
|
|
if (n == 0)
|
|
|
|
return -2;
|
2009-07-01 05:18:44 +08:00
|
|
|
|
2009-04-06 18:50:11 +08:00
|
|
|
if (pwc == NULL)
|
|
|
|
pwc = &dummy;
|
|
|
|
|
|
|
|
if (state->__count == 0)
|
|
|
|
{
|
|
|
|
if (*(unsigned char *) s < 0x80)
|
|
|
|
{
|
|
|
|
*pwc = *(unsigned char *) s;
|
|
|
|
return *s ? 1 : 0;
|
|
|
|
}
|
|
|
|
if (*(unsigned char *) s == 0x8f) /* JIS-X-0212 lead byte? */
|
|
|
|
{
|
|
|
|
/* Yes. Store sequence in mbstate and handle in the __count != 0
|
|
|
|
case at the end of the function. */
|
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < 3 && i < n; i++)
|
|
|
|
state->__value.__wchb[i] = s[i];
|
|
|
|
if ((state->__count = i) < 3) /* Incomplete sequence? */
|
|
|
|
return -2;
|
|
|
|
ret = 3;
|
|
|
|
goto jis_x_0212;
|
|
|
|
}
|
2012-03-08 17:36:11 +08:00
|
|
|
size_t cnt = MIN (n, 2);
|
2009-04-06 18:50:11 +08:00
|
|
|
if (MultiByteToWideChar (20932, MB_ERR_INVALID_CHARS, s, cnt, pwc, 1))
|
|
|
|
return cnt;
|
|
|
|
if (n == 1)
|
|
|
|
{
|
|
|
|
state->__count = 1;
|
|
|
|
state->__value.__wchb[0] = *s;
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
else if (MultiByteToWideChar (20932, MB_ERR_INVALID_CHARS, s, 1, pwc, 1))
|
|
|
|
return 1;
|
2022-01-18 17:13:04 +08:00
|
|
|
_REENT_ERRNO(r) = EILSEQ;
|
2009-04-06 18:50:11 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
state->__value.__wchb[state->__count++] = *s;
|
|
|
|
ret = 1;
|
|
|
|
jis_x_0212:
|
|
|
|
if (state->__value.__wchb[0] == 0x8f)
|
|
|
|
{
|
|
|
|
if (state->__count == 2)
|
|
|
|
{
|
|
|
|
if (n == 1)
|
|
|
|
return -2;
|
|
|
|
state->__value.__wchb[state->__count] = s[1];
|
|
|
|
ret = 2;
|
|
|
|
}
|
|
|
|
/* Ok, we have a full JIS-X-0212 sequence in mbstate. Convert it
|
|
|
|
to the CP 20932 representation and feed it to MultiByteToWideChar. */
|
|
|
|
state->__value.__wchb[0] = state->__value.__wchb[1];
|
|
|
|
state->__value.__wchb[1] = state->__value.__wchb[2] & 0x7f;
|
|
|
|
}
|
|
|
|
if (!MultiByteToWideChar (20932, MB_ERR_INVALID_CHARS,
|
|
|
|
(const char *) state->__value.__wchb, 2, pwc, 1))
|
|
|
|
{
|
2022-01-18 17:13:04 +08:00
|
|
|
_REENT_ERRNO(r) = EILSEQ;
|
2009-04-06 18:50:11 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
state->__count = 0;
|
|
|
|
return ret;
|
2009-03-24 20:18:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" int
|
|
|
|
__gbk_mbtowc (struct _reent *r, wchar_t *pwc, const char *s, size_t n,
|
2016-07-21 04:05:59 +08:00
|
|
|
mbstate_t *state)
|
2009-03-24 20:18:34 +08:00
|
|
|
{
|
|
|
|
return __db_mbtowc (r, pwc, s, n, 936, state);
|
|
|
|
}
|
|
|
|
|
2023-03-17 01:25:09 +08:00
|
|
|
extern "C" int
|
|
|
|
__gb18030_mbtowc (struct _reent *r, wchar_t *pwc, const char *s, size_t n,
|
|
|
|
mbstate_t *state)
|
|
|
|
{
|
|
|
|
return __db_mbtowc (r, pwc, s, n, 54936, state);
|
|
|
|
}
|
|
|
|
|
2009-03-24 20:18:34 +08:00
|
|
|
extern "C" int
|
|
|
|
__kr_mbtowc (struct _reent *r, wchar_t *pwc, const char *s, size_t n,
|
2016-07-21 04:05:59 +08:00
|
|
|
mbstate_t *state)
|
2009-03-24 20:18:34 +08:00
|
|
|
{
|
2009-04-06 18:50:11 +08:00
|
|
|
return __db_mbtowc (r, pwc, s, n, 949, state);
|
2009-03-24 20:18:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" int
|
|
|
|
__big5_mbtowc (struct _reent *r, wchar_t *pwc, const char *s, size_t n,
|
2016-07-21 04:05:59 +08:00
|
|
|
mbstate_t *state)
|
2009-03-24 20:18:34 +08:00
|
|
|
{
|
|
|
|
return __db_mbtowc (r, pwc, s, n, 950, state);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Our own sys_wcstombs/sys_mbstowcs functions differ from the
|
|
|
|
wcstombs/mbstowcs API in three ways:
|
|
|
|
|
|
|
|
- The UNICODE private use area is used in filenames to specify
|
|
|
|
characters not allowed in Windows filenames ('*', '?', etc).
|
|
|
|
The sys_wcstombs converts characters in the private use area
|
|
|
|
back to the corresponding ASCII chars.
|
|
|
|
|
|
|
|
- If a wide character in a filename has no representation in the current
|
|
|
|
multibyte charset, then usually you wouldn't be able to access the
|
|
|
|
file. To fix this problem, sys_wcstombs creates a replacement multibyte
|
|
|
|
sequences for the non-representable wide-char. The sequence starts with
|
2009-09-28 20:10:32 +08:00
|
|
|
an ASCII CAN (0x18, Ctrl-X), followed by the UTF-8 representation of the
|
|
|
|
character. The sys_(cp_)mbstowcs function detects ASCII CAN characters
|
2009-03-24 20:18:34 +08:00
|
|
|
in the input multibyte string and converts the following multibyte
|
|
|
|
sequence in by treating it as an UTF-8 char. If that fails, the ASCII
|
2009-09-28 20:10:32 +08:00
|
|
|
CAN was probably standalone and it gets just copied over as ASCII CAN.
|
2009-03-24 20:18:34 +08:00
|
|
|
|
2012-07-03 04:17:27 +08:00
|
|
|
- Three cases have to be distinguished for the return value:
|
|
|
|
|
|
|
|
- dst == NULL; len is ignored, the return value is the number of bytes
|
|
|
|
required for the string without the trailing NUL, just like the return
|
|
|
|
value of the wcstombs function.
|
|
|
|
|
|
|
|
- dst != NULL, len == (size_t) -1; the return value is the size in bytes
|
|
|
|
of the destination string without the trailing NUL. If the incoming
|
|
|
|
wide char string was not NUL-terminated, the target string won't be
|
|
|
|
NUL-terminated either.
|
|
|
|
|
|
|
|
- dst != NULL; len != (size_t) -1; the return value is the size in bytes
|
|
|
|
of the destination string without the trailing NUL. The target string
|
|
|
|
will be NUL-terminated, no matter what. If the result is truncated due
|
|
|
|
to buffer size, it's a bug in Cygwin and the buffer in the calling
|
|
|
|
function should be raised.
|
|
|
|
*/
|
2020-07-10 16:29:33 +08:00
|
|
|
size_t
|
|
|
|
_sys_wcstombs (char *dst, size_t len, const wchar_t *src, size_t nwc,
|
|
|
|
bool is_path)
|
2009-03-24 20:18:34 +08:00
|
|
|
{
|
|
|
|
char buf[10];
|
|
|
|
char *ptr = dst;
|
|
|
|
wchar_t *pwcs = (wchar_t *) src;
|
|
|
|
size_t n = 0;
|
|
|
|
mbstate_t ps;
|
2009-05-09 04:28:20 +08:00
|
|
|
save_errno save;
|
2016-07-21 04:05:59 +08:00
|
|
|
wctomb_p f_wctomb = __WCTOMB;
|
2009-03-24 20:18:34 +08:00
|
|
|
|
2016-07-21 04:05:59 +08:00
|
|
|
if (f_wctomb == __ascii_wctomb)
|
|
|
|
f_wctomb = __utf8_wctomb;
|
2009-03-24 20:18:34 +08:00
|
|
|
memset (&ps, 0, sizeof ps);
|
|
|
|
if (dst == NULL)
|
|
|
|
len = (size_t) -1;
|
|
|
|
while (n < len && nwc-- > 0)
|
|
|
|
{
|
|
|
|
wchar_t pw = *pwcs;
|
2009-09-28 20:10:32 +08:00
|
|
|
int bytes;
|
2009-11-02 19:42:04 +08:00
|
|
|
unsigned char cwc;
|
2009-09-28 20:10:32 +08:00
|
|
|
|
|
|
|
/* Convert UNICODE private use area. Reverse functionality for the
|
2011-06-06 13:02:13 +08:00
|
|
|
ASCII area <= 0x7f (only for path names) is transform_chars above.
|
2009-11-02 19:42:04 +08:00
|
|
|
Reverse functionality for invalid bytes in a multibyte sequence is
|
2020-07-10 16:29:33 +08:00
|
|
|
in _sys_mbstowcs below. */
|
Do not treat the command line or environment like paths
* dcrt0.cc (dll_crt0_1), environ.cc (environ_init, getwinenveq,
build_env), strfuncs.cc (sys_wcstombs, sys_wcstombs_alloc),
wchar.c (sys_wcstombs, sys_wcstombs_alloc): avoid mis-conversions
of text that does not, actually, refer to a path or file name
Detailed explanation:
Our WCS -> UTF conversion handles the private Unicode page specially
to allow for otherwise invalid file names. However, this handling makes
no sense for command-lines, nor environment variables, which we would
rather convert verbatim.
As a stop-gap solution, let's just introduce a version of the
sys_wcstombs() function that specifically excludes that file name
conversion magic.
The proper solution is to change sys_wcstombs() to assume that it is not
a path that wants to be converted, and introduce sys_wcstombs_path()
that does, but that is a bigger task which we leave for another patch.
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2015-12-17 23:22:49 +08:00
|
|
|
if (is_path && (pw & 0xff00) == 0xf000
|
2010-04-23 19:07:35 +08:00
|
|
|
&& (((cwc = (pw & 0xff)) <= 0x7f && tfx_rev_chars[cwc] >= 0xf000)
|
2009-11-02 19:42:04 +08:00
|
|
|
|| (cwc >= 0x80 && MB_CUR_MAX > 1)))
|
2009-06-04 01:23:39 +08:00
|
|
|
{
|
2009-11-02 19:42:04 +08:00
|
|
|
buf[0] = (char) cwc;
|
2009-09-28 20:10:32 +08:00
|
|
|
bytes = 1;
|
2009-11-02 19:42:04 +08:00
|
|
|
}
|
2009-09-28 20:10:32 +08:00
|
|
|
else
|
2009-07-01 05:18:44 +08:00
|
|
|
{
|
2016-07-21 04:05:59 +08:00
|
|
|
bytes = f_wctomb (_REENT, buf, pw, &ps);
|
|
|
|
if (bytes == -1 && f_wctomb != __utf8_wctomb)
|
2009-03-24 20:18:34 +08:00
|
|
|
{
|
2009-09-28 20:10:32 +08:00
|
|
|
/* Convert chars invalid in the current codepage to a sequence
|
|
|
|
ASCII CAN; UTF-8 representation of invalid char. */
|
|
|
|
buf[0] = 0x18; /* ASCII CAN */
|
2016-07-21 04:05:59 +08:00
|
|
|
bytes = __utf8_wctomb (_REENT, buf + 1, pw, &ps);
|
2009-09-28 20:10:32 +08:00
|
|
|
if (bytes == -1)
|
2009-03-24 20:18:34 +08:00
|
|
|
{
|
|
|
|
++pwcs;
|
|
|
|
ps.__count = 0;
|
|
|
|
continue;
|
|
|
|
}
|
2009-09-28 20:10:32 +08:00
|
|
|
++bytes; /* Add the ASCII CAN to the byte count. */
|
|
|
|
if (ps.__count == -4 && nwc > 0)
|
|
|
|
{
|
|
|
|
/* First half of a surrogate pair. */
|
|
|
|
++pwcs;
|
|
|
|
if ((*pwcs & 0xfc00) != 0xdc00) /* Invalid second half. */
|
|
|
|
{
|
|
|
|
++pwcs;
|
|
|
|
ps.__count = 0;
|
|
|
|
continue;
|
|
|
|
}
|
2016-07-21 04:05:59 +08:00
|
|
|
bytes += __utf8_wctomb (_REENT, buf + bytes, *pwcs, &ps);
|
2009-09-28 20:10:32 +08:00
|
|
|
nwc--;
|
|
|
|
}
|
2009-03-24 20:18:34 +08:00
|
|
|
}
|
2009-07-01 05:18:44 +08:00
|
|
|
}
|
2009-03-24 20:18:34 +08:00
|
|
|
if (n + bytes <= len)
|
2009-07-01 05:18:44 +08:00
|
|
|
{
|
|
|
|
if (dst)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < bytes; ++i)
|
|
|
|
*ptr++ = buf[i];
|
|
|
|
}
|
|
|
|
if (*pwcs++ == 0x00)
|
2009-03-24 20:18:34 +08:00
|
|
|
break;
|
2015-10-22 20:22:07 +08:00
|
|
|
n += bytes;
|
2009-07-01 05:18:44 +08:00
|
|
|
}
|
2009-03-24 20:18:34 +08:00
|
|
|
else
|
2009-07-01 05:18:44 +08:00
|
|
|
break;
|
2009-03-24 20:18:34 +08:00
|
|
|
}
|
2012-07-03 04:17:27 +08:00
|
|
|
if (n && dst && len != (size_t) -1)
|
2009-03-24 20:18:34 +08:00
|
|
|
{
|
|
|
|
n = (n < len) ? n : len - 1;
|
|
|
|
dst[n] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2008-02-01 04:26:01 +08:00
|
|
|
/* Allocate a buffer big enough for the string, always including the
|
2009-03-24 20:18:34 +08:00
|
|
|
terminating '\0'. The buffer pointer is returned in *dst_p, the return
|
2008-02-01 04:26:01 +08:00
|
|
|
value is the number of bytes written to the buffer, as usual.
|
|
|
|
The "type" argument determines where the resulting buffer is stored.
|
|
|
|
It's either one of the cygheap_types values, or it's "HEAP_NOTHEAP".
|
2008-02-04 20:00:19 +08:00
|
|
|
In the latter case the allocation uses simple calloc.
|
2008-02-16 01:53:11 +08:00
|
|
|
|
2008-02-04 20:00:19 +08:00
|
|
|
Note that this code is shared by cygserver (which requires it via
|
2008-02-16 01:53:11 +08:00
|
|
|
__small_vsprintf) and so when built there plain calloc is the
|
2008-02-04 20:00:19 +08:00
|
|
|
only choice. */
|
2020-07-10 16:29:33 +08:00
|
|
|
size_t
|
|
|
|
_sys_wcstombs_alloc (char **dst_p, int type, const wchar_t *src, size_t nwc,
|
Do not treat the command line or environment like paths
* dcrt0.cc (dll_crt0_1), environ.cc (environ_init, getwinenveq,
build_env), strfuncs.cc (sys_wcstombs, sys_wcstombs_alloc),
wchar.c (sys_wcstombs, sys_wcstombs_alloc): avoid mis-conversions
of text that does not, actually, refer to a path or file name
Detailed explanation:
Our WCS -> UTF conversion handles the private Unicode page specially
to allow for otherwise invalid file names. However, this handling makes
no sense for command-lines, nor environment variables, which we would
rather convert verbatim.
As a stop-gap solution, let's just introduce a version of the
sys_wcstombs() function that specifically excludes that file name
conversion magic.
The proper solution is to change sys_wcstombs() to assume that it is not
a path that wants to be converted, and introduce sys_wcstombs_path()
that does, but that is a bigger task which we leave for another patch.
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2015-12-17 23:22:49 +08:00
|
|
|
bool is_path)
|
2008-02-01 04:26:01 +08:00
|
|
|
{
|
2009-03-24 20:18:34 +08:00
|
|
|
size_t ret;
|
2008-02-01 04:26:01 +08:00
|
|
|
|
2020-07-10 16:29:33 +08:00
|
|
|
ret = _sys_wcstombs (NULL, (size_t) -1, src, nwc, is_path);
|
2009-03-24 20:18:34 +08:00
|
|
|
if (ret > 0)
|
2008-02-01 04:26:01 +08:00
|
|
|
{
|
2009-03-24 20:18:34 +08:00
|
|
|
size_t dlen = ret + 1;
|
2008-02-01 04:26:01 +08:00
|
|
|
|
|
|
|
if (type == HEAP_NOTHEAP)
|
2009-03-24 20:18:34 +08:00
|
|
|
*dst_p = (char *) calloc (dlen, sizeof (char));
|
2008-02-01 04:26:01 +08:00
|
|
|
else
|
2009-03-24 20:18:34 +08:00
|
|
|
*dst_p = (char *) ccalloc ((cygheap_types) type, dlen, sizeof (char));
|
|
|
|
if (!*dst_p)
|
2008-02-16 01:53:11 +08:00
|
|
|
return 0;
|
2020-07-10 16:29:33 +08:00
|
|
|
ret = _sys_wcstombs (*dst_p, dlen, src, nwc, is_path);
|
2008-02-01 04:26:01 +08:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-07-10 16:29:33 +08:00
|
|
|
/* _sys_mbstowcs is actually most of the time called as sys_mbstowcs with
|
2009-03-24 20:18:34 +08:00
|
|
|
a 0 codepage. If cp is not 0, the codepage is evaluated and used for the
|
|
|
|
conversion. This is so that fhandler_console can switch to an alternate
|
|
|
|
charset, which is the charset returned by GetConsoleCP (). Most of the
|
|
|
|
time this is used for box and line drawing characters. */
|
2020-07-10 16:29:33 +08:00
|
|
|
size_t
|
|
|
|
_sys_mbstowcs (mbtowc_p f_mbtowc, wchar_t *dst, size_t dlen, const char *src,
|
|
|
|
size_t nms)
|
2007-08-02 22:21:53 +08:00
|
|
|
{
|
2009-03-24 20:18:34 +08:00
|
|
|
wchar_t *ptr = dst;
|
2009-05-31 11:59:38 +08:00
|
|
|
unsigned const char *pmbs = (unsigned const char *) src;
|
2009-03-24 20:18:34 +08:00
|
|
|
size_t count = 0;
|
|
|
|
size_t len = dlen;
|
|
|
|
int bytes;
|
|
|
|
mbstate_t ps;
|
2009-05-09 04:28:20 +08:00
|
|
|
save_errno save;
|
2009-03-24 20:18:34 +08:00
|
|
|
|
|
|
|
memset (&ps, 0, sizeof ps);
|
|
|
|
if (dst == NULL)
|
|
|
|
len = (size_t)-1;
|
2009-03-25 00:42:36 +08:00
|
|
|
while (len > 0 && nms > 0)
|
2008-02-26 02:32:23 +08:00
|
|
|
{
|
2009-09-28 20:10:32 +08:00
|
|
|
/* ASCII CAN handling. */
|
|
|
|
if (*pmbs == 0x18)
|
2009-03-24 20:18:34 +08:00
|
|
|
{
|
2009-09-28 20:10:32 +08:00
|
|
|
/* Sanity check: If this is a lead CAN byte for a following UTF-8
|
2009-09-23 19:31:00 +08:00
|
|
|
sequence, there must be at least two more bytes left, and the
|
|
|
|
next byte must be a valid UTF-8 start byte. If the charset
|
|
|
|
isn't UTF-8 anyway, try to convert the following bytes as UTF-8
|
|
|
|
sequence. */
|
2016-07-21 04:05:59 +08:00
|
|
|
if (nms > 2 && pmbs[1] >= 0xc2 && pmbs[1] <= 0xf4
|
|
|
|
&& f_mbtowc != __utf8_mbtowc)
|
2009-03-24 20:18:34 +08:00
|
|
|
{
|
2009-09-23 19:31:00 +08:00
|
|
|
bytes = __utf8_mbtowc (_REENT, ptr, (const char *) pmbs + 1,
|
2016-07-21 04:05:59 +08:00
|
|
|
nms - 1, &ps);
|
2009-09-23 19:31:00 +08:00
|
|
|
if (bytes < 0)
|
|
|
|
{
|
2009-09-28 20:10:32 +08:00
|
|
|
/* Invalid UTF-8 sequence? Treat the ASCII CAN character as
|
|
|
|
stand-alone ASCII CAN char. */
|
2009-09-23 19:31:00 +08:00
|
|
|
bytes = 1;
|
|
|
|
if (dst)
|
2009-09-28 20:10:32 +08:00
|
|
|
*ptr = 0x18;
|
2009-09-23 19:31:00 +08:00
|
|
|
memset (&ps, 0, sizeof ps);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-09-28 20:10:32 +08:00
|
|
|
++bytes; /* Count CAN byte */
|
2009-09-23 19:31:00 +08:00
|
|
|
if (bytes > 1 && ps.__count == 4)
|
|
|
|
{
|
|
|
|
/* First half of a surrogate. */
|
|
|
|
wchar_t *ptr2 = dst ? ptr + 1 : NULL;
|
|
|
|
int bytes2 = __utf8_mbtowc (_REENT, ptr2,
|
|
|
|
(const char *) pmbs + bytes,
|
2016-07-21 04:05:59 +08:00
|
|
|
nms - bytes, &ps);
|
2009-09-23 19:31:00 +08:00
|
|
|
if (bytes2 < 0)
|
|
|
|
memset (&ps, 0, sizeof ps);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bytes += bytes2;
|
|
|
|
++count;
|
|
|
|
ptr = dst ? ptr + 1 : NULL;
|
|
|
|
--len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-09-28 20:10:32 +08:00
|
|
|
/* Otherwise it's just a simple ASCII CAN. */
|
2009-09-23 19:31:00 +08:00
|
|
|
else
|
2009-03-24 20:18:34 +08:00
|
|
|
{
|
2009-09-23 19:31:00 +08:00
|
|
|
bytes = 1;
|
|
|
|
if (dst)
|
2009-09-28 20:10:32 +08:00
|
|
|
*ptr = 0x18;
|
2009-03-24 20:18:34 +08:00
|
|
|
}
|
|
|
|
}
|
2009-06-04 01:23:39 +08:00
|
|
|
else if ((bytes = f_mbtowc (_REENT, ptr, (const char *) pmbs, nms,
|
2016-07-21 04:05:59 +08:00
|
|
|
&ps)) < 0)
|
2009-05-31 11:59:38 +08:00
|
|
|
{
|
2009-09-28 20:10:32 +08:00
|
|
|
/* The technique is based on a discussion here:
|
2009-05-31 11:59:38 +08:00
|
|
|
http://www.mail-archive.com/linux-utf8@nl.linux.org/msg00080.html
|
|
|
|
|
2020-07-06 19:17:53 +08:00
|
|
|
Invalid bytes in a multibyte sequence are converted to
|
2009-09-28 20:10:32 +08:00
|
|
|
the private use area which is already used to store ASCII
|
2011-05-02 23:28:35 +08:00
|
|
|
chars invalid in Windows filenames. This technque allows
|
2009-09-28 20:10:32 +08:00
|
|
|
to store them in a symmetric way. */
|
2009-09-23 19:31:00 +08:00
|
|
|
bytes = 1;
|
2009-06-04 01:23:39 +08:00
|
|
|
if (dst)
|
2009-09-28 20:10:32 +08:00
|
|
|
*ptr = L'\xf000' | *pmbs;
|
2009-09-22 17:44:32 +08:00
|
|
|
memset (&ps, 0, sizeof ps);
|
2009-05-31 11:59:38 +08:00
|
|
|
}
|
|
|
|
|
2009-03-24 20:18:34 +08:00
|
|
|
if (bytes > 0)
|
2009-07-01 05:18:44 +08:00
|
|
|
{
|
|
|
|
pmbs += bytes;
|
|
|
|
nms -= bytes;
|
|
|
|
++count;
|
2009-03-24 20:18:34 +08:00
|
|
|
ptr = dst ? ptr + 1 : NULL;
|
2009-07-01 05:18:44 +08:00
|
|
|
--len;
|
|
|
|
}
|
2009-03-24 20:18:34 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (bytes == 0)
|
|
|
|
++count;
|
|
|
|
break;
|
|
|
|
}
|
2008-02-26 02:32:23 +08:00
|
|
|
}
|
2009-03-24 20:18:34 +08:00
|
|
|
|
|
|
|
if (count && dst)
|
|
|
|
{
|
|
|
|
count = (count < dlen) ? count : dlen - 1;
|
|
|
|
dst[count] = L'\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
2007-08-02 22:21:53 +08:00
|
|
|
}
|
2007-08-12 20:48:02 +08:00
|
|
|
|
2008-02-01 04:26:01 +08:00
|
|
|
/* Same as sys_wcstombs_alloc, just backwards. */
|
2020-07-10 16:29:33 +08:00
|
|
|
size_t
|
2009-04-08 00:22:55 +08:00
|
|
|
sys_mbstowcs_alloc (wchar_t **dst_p, int type, const char *src, size_t nms)
|
2008-02-01 04:26:01 +08:00
|
|
|
{
|
2009-03-24 20:18:34 +08:00
|
|
|
size_t ret;
|
2008-02-01 04:26:01 +08:00
|
|
|
|
2009-03-24 20:18:34 +08:00
|
|
|
ret = sys_mbstowcs (NULL, (size_t) -1, src, nms);
|
|
|
|
if (ret > 0)
|
2008-02-01 04:26:01 +08:00
|
|
|
{
|
2009-03-24 20:18:34 +08:00
|
|
|
size_t dlen = ret + 1;
|
2008-02-26 02:32:23 +08:00
|
|
|
|
2008-02-01 04:26:01 +08:00
|
|
|
if (type == HEAP_NOTHEAP)
|
2009-04-08 00:22:55 +08:00
|
|
|
*dst_p = (wchar_t *) calloc (dlen, sizeof (wchar_t));
|
2008-02-01 04:26:01 +08:00
|
|
|
else
|
2009-04-08 00:22:55 +08:00
|
|
|
*dst_p = (wchar_t *) ccalloc ((cygheap_types) type, dlen,
|
|
|
|
sizeof (wchar_t));
|
2009-03-24 20:18:34 +08:00
|
|
|
if (!*dst_p)
|
2008-02-16 01:53:11 +08:00
|
|
|
return 0;
|
2009-03-24 20:18:34 +08:00
|
|
|
ret = sys_mbstowcs (*dst_p, dlen, src, nms);
|
2008-02-01 04:26:01 +08:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-01-19 17:41:54 +08:00
|
|
|
/* Copy string, until c or <nul> is encountered.
|
|
|
|
NUL-terminate the destination string (s1).
|
|
|
|
Return pointer to terminating byte in dst string. */
|
2022-06-07 00:00:45 +08:00
|
|
|
char *
|
2013-11-25 19:38:08 +08:00
|
|
|
strccpy (char *__restrict s1, const char **__restrict s2, char c)
|
2011-01-19 17:41:54 +08:00
|
|
|
{
|
|
|
|
while (**s2 && **s2 != c)
|
|
|
|
*s1++ = *((*s2)++);
|
|
|
|
*s1 = 0;
|
|
|
|
|
|
|
|
return s1;
|
|
|
|
}
|
|
|
|
|
2017-01-20 04:01:41 +08:00
|
|
|
const unsigned char case_folded_lower[] = {
|
|
|
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
|
|
|
|
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
|
|
|
|
32, '!', '"', '#', '$', '%', '&', 39, '(', ')', '*', '+', ',', '-', '.', '/',
|
|
|
|
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?',
|
|
|
|
'@', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
|
|
|
|
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '[', 92, ']', '^', '_',
|
|
|
|
'`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
|
|
|
|
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~', 127,
|
|
|
|
128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
|
|
|
|
144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
|
|
|
|
160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
|
|
|
|
176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
|
|
|
|
192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207,
|
|
|
|
208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223,
|
|
|
|
224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239,
|
|
|
|
240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255
|
|
|
|
};
|
|
|
|
|
|
|
|
const unsigned char case_folded_upper[] = {
|
|
|
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
|
|
|
|
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
|
|
|
|
32, '!', '"', '#', '$', '%', '&', 39, '(', ')', '*', '+', ',', '-', '.', '/',
|
|
|
|
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?',
|
|
|
|
'@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
|
|
|
|
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', 92, ']', '^', '_',
|
|
|
|
'`', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
|
|
|
|
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '{', '|', '}', '~', 127,
|
|
|
|
128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
|
|
|
|
144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
|
|
|
|
160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
|
|
|
|
176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
|
|
|
|
192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207,
|
|
|
|
208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223,
|
|
|
|
224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239,
|
|
|
|
240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255
|
|
|
|
};
|
|
|
|
|
|
|
|
const char isalpha_array[] = {
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
|
|
|
|
0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20, 0, 0, 0, 0, 0,
|
|
|
|
0,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
|
|
|
|
0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
|
|
|
};
|
|
|
|
|
2022-06-07 00:00:45 +08:00
|
|
|
extern "C" int
|
2017-01-20 04:01:41 +08:00
|
|
|
cygwin_wcscasecmp (const wchar_t *ws, const wchar_t *wt)
|
|
|
|
{
|
|
|
|
UNICODE_STRING us, ut;
|
|
|
|
|
|
|
|
RtlInitUnicodeString (&us, ws);
|
|
|
|
RtlInitUnicodeString (&ut, wt);
|
|
|
|
return RtlCompareUnicodeString (&us, &ut, TRUE);
|
|
|
|
}
|
|
|
|
|
2022-06-07 00:00:45 +08:00
|
|
|
extern "C" int
|
2017-01-20 04:01:41 +08:00
|
|
|
cygwin_wcsncasecmp (const wchar_t *ws, const wchar_t *wt, size_t n)
|
|
|
|
{
|
|
|
|
UNICODE_STRING us, ut;
|
|
|
|
size_t ls = 0, lt = 0;
|
|
|
|
|
|
|
|
while (ws[ls] && ls < n)
|
|
|
|
++ls;
|
|
|
|
RtlInitCountedUnicodeString (&us, ws, ls * sizeof (WCHAR));
|
|
|
|
while (wt[lt] && lt < n)
|
|
|
|
++lt;
|
|
|
|
RtlInitCountedUnicodeString (&ut, wt, lt * sizeof (WCHAR));
|
|
|
|
return RtlCompareUnicodeString (&us, &ut, TRUE);
|
|
|
|
}
|
|
|
|
|
2022-06-07 00:00:45 +08:00
|
|
|
extern "C" int
|
2017-01-20 04:01:41 +08:00
|
|
|
cygwin_strcasecmp (const char *cs, const char *ct)
|
|
|
|
{
|
|
|
|
UNICODE_STRING us, ut;
|
2020-07-06 19:17:53 +08:00
|
|
|
ULONG len, ulen;
|
|
|
|
|
|
|
|
len = strlen (cs) + 1;
|
|
|
|
ulen = len * sizeof (WCHAR);
|
|
|
|
RtlInitEmptyUnicodeString (&us, (PWCHAR) alloca (ulen), ulen);
|
|
|
|
us.Length = sys_mbstowcs (us.Buffer, len, cs) * sizeof (WCHAR);
|
|
|
|
|
|
|
|
len = strlen (ct) + 1;
|
|
|
|
ulen = len * sizeof (WCHAR);
|
|
|
|
RtlInitEmptyUnicodeString (&ut, (PWCHAR) alloca (ulen), ulen);
|
|
|
|
ut.Length = sys_mbstowcs (ut.Buffer, len, ct) * sizeof (WCHAR);
|
|
|
|
|
2017-01-20 04:01:41 +08:00
|
|
|
return RtlCompareUnicodeString (&us, &ut, TRUE);
|
|
|
|
}
|
|
|
|
|
2022-06-07 00:00:45 +08:00
|
|
|
extern "C" int
|
2017-01-20 04:01:41 +08:00
|
|
|
cygwin_strncasecmp (const char *cs, const char *ct, size_t n)
|
|
|
|
{
|
|
|
|
UNICODE_STRING us, ut;
|
2020-07-06 19:17:53 +08:00
|
|
|
ULONG ulen;
|
2017-01-20 04:01:41 +08:00
|
|
|
size_t ls = 0, lt = 0;
|
|
|
|
|
|
|
|
while (cs[ls] && ls < n)
|
|
|
|
++ls;
|
2020-07-06 19:17:53 +08:00
|
|
|
ulen = (ls + 1) * sizeof (WCHAR);
|
|
|
|
RtlInitEmptyUnicodeString (&us, (PWCHAR) alloca (ulen), ulen);
|
2017-01-20 04:01:41 +08:00
|
|
|
us.Length = sys_mbstowcs (us.Buffer, ls + 1, cs, ls) * sizeof (WCHAR);
|
2020-07-06 19:17:53 +08:00
|
|
|
|
2017-01-20 04:01:41 +08:00
|
|
|
while (ct[lt] && lt < n)
|
|
|
|
++lt;
|
2020-07-06 19:17:53 +08:00
|
|
|
ulen = (lt + 1) * sizeof (WCHAR);
|
|
|
|
RtlInitEmptyUnicodeString (&ut, (PWCHAR) alloca (ulen), ulen);
|
2017-01-20 04:01:41 +08:00
|
|
|
ut.Length = sys_mbstowcs (ut.Buffer, lt + 1, ct, lt) * sizeof (WCHAR);
|
2020-07-06 19:17:53 +08:00
|
|
|
|
2017-01-20 04:01:41 +08:00
|
|
|
return RtlCompareUnicodeString (&us, &ut, TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" char *
|
|
|
|
strlwr (char *string)
|
|
|
|
{
|
|
|
|
UNICODE_STRING us;
|
|
|
|
size_t len = (strlen (string) + 1) * sizeof (WCHAR);
|
|
|
|
|
|
|
|
us.MaximumLength = len; us.Buffer = (PWCHAR) alloca (len);
|
|
|
|
us.Length = sys_mbstowcs (us.Buffer, len, string) * sizeof (WCHAR)
|
|
|
|
- sizeof (WCHAR);
|
|
|
|
RtlDowncaseUnicodeString (&us, &us, FALSE);
|
|
|
|
sys_wcstombs (string, len / sizeof (WCHAR), us.Buffer);
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" char *
|
|
|
|
strupr (char *string)
|
|
|
|
{
|
|
|
|
UNICODE_STRING us;
|
|
|
|
size_t len = (strlen (string) + 1) * sizeof (WCHAR);
|
|
|
|
|
|
|
|
us.MaximumLength = len; us.Buffer = (PWCHAR) alloca (len);
|
|
|
|
us.Length = sys_mbstowcs (us.Buffer, len, string) * sizeof (WCHAR)
|
|
|
|
- sizeof (WCHAR);
|
|
|
|
RtlUpcaseUnicodeString (&us, &us, FALSE);
|
|
|
|
sys_wcstombs (string, len / sizeof (WCHAR), us.Buffer);
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* backslashify: Convert all forward slashes in src path to back slashes
|
|
|
|
in dst path. Add a trailing slash to dst when trailing_slash_p arg
|
|
|
|
is set to 1. */
|
|
|
|
|
|
|
|
void
|
|
|
|
backslashify (const char *src, char *dst, bool trailing_slash_p)
|
|
|
|
{
|
|
|
|
const char *start = src;
|
|
|
|
|
|
|
|
while (*src)
|
|
|
|
{
|
|
|
|
if (*src == '/')
|
|
|
|
*dst++ = '\\';
|
|
|
|
else
|
|
|
|
*dst++ = *src;
|
|
|
|
++src;
|
|
|
|
}
|
|
|
|
if (trailing_slash_p
|
|
|
|
&& src > start
|
|
|
|
&& !isdirsep (src[-1]))
|
|
|
|
*dst++ = '\\';
|
|
|
|
*dst++ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* slashify: Convert all back slashes in src path to forward slashes
|
|
|
|
in dst path. Add a trailing slash to dst when trailing_slash_p arg
|
|
|
|
is set to 1. */
|
|
|
|
|
|
|
|
void
|
|
|
|
slashify (const char *src, char *dst, bool trailing_slash_p)
|
|
|
|
{
|
|
|
|
const char *start = src;
|
|
|
|
|
|
|
|
while (*src)
|
|
|
|
{
|
|
|
|
if (*src == '\\')
|
|
|
|
*dst++ = '/';
|
|
|
|
else
|
|
|
|
*dst++ = *src;
|
|
|
|
++src;
|
|
|
|
}
|
|
|
|
if (trailing_slash_p
|
|
|
|
&& src > start
|
|
|
|
&& !isdirsep (src[-1]))
|
|
|
|
*dst++ = '/';
|
|
|
|
*dst++ = 0;
|
|
|
|
}
|
|
|
|
|
2007-08-12 20:48:02 +08:00
|
|
|
static WCHAR hex_wchars[] = L"0123456789abcdef";
|
|
|
|
|
2022-08-05 04:13:50 +08:00
|
|
|
NTSTATUS
|
2007-08-12 20:48:02 +08:00
|
|
|
RtlInt64ToHexUnicodeString (ULONGLONG value, PUNICODE_STRING dest,
|
|
|
|
BOOLEAN append)
|
|
|
|
{
|
|
|
|
USHORT len = append ? dest->Length : 0;
|
|
|
|
if (dest->MaximumLength - len < 16 * (int) sizeof (WCHAR))
|
|
|
|
return STATUS_BUFFER_OVERFLOW;
|
2009-04-08 00:22:55 +08:00
|
|
|
wchar_t *end = (PWCHAR) ((PBYTE) dest->Buffer + len);
|
2021-08-27 04:14:11 +08:00
|
|
|
PWCHAR p = end + 16;
|
2007-08-12 20:48:02 +08:00
|
|
|
while (p-- > end)
|
|
|
|
{
|
|
|
|
*p = hex_wchars[value & 0xf];
|
|
|
|
value >>= 4;
|
|
|
|
}
|
|
|
|
dest->Length += 16 * sizeof (WCHAR);
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|