2 * msvcrt.dll mbcs functions
4 * Copyright 1999 Alexandre Julliard
5 * Copyright 2000 Jon Griffths
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 * Not currently binary compatible with win32. MSVCRT_mbctype must be
23 * populated correctly and the ismb* functions should reference it.
29 #include "wine/unicode.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
34 unsigned char MSVCRT_mbctype[257] = { 0 };
36 /* It seems that the data about valid trail bytes is not available from kernel32
37 * so we have to store is here. The format is the same as for lead bytes in CPINFO */
38 struct cp_extra_info_t
41 BYTE TrailBytes[MAX_LEADBYTES];
44 static struct cp_extra_info_t g_cpextrainfo[] =
46 {932, {0x40, 0x7e, 0x80, 0xfc, 0, 0}},
47 {936, {0x40, 0xfe, 0, 0}},
48 {949, {0x41, 0xfe, 0, 0}},
49 {950, {0x40, 0x7e, 0xa1, 0xfe, 0, 0}},
50 {1361, {0x31, 0x7e, 0x81, 0xfe, 0, 0}},
51 {20932, {1, 255, 0, 0}}, /* seems to give different results on different systems */
52 {0, {1, 255, 0, 0}} /* match all with FIXME */
55 /* Maps multibyte cp932 punctuation marks to single byte equivalents */
56 static const unsigned char mbctombb_932_punct[] = {
57 0x20,0xa4,0xa1,0x2c,0x2e,0xa5,0x3a,0x3b,0x3f,0x21,0xde,0xdf,0x00,0x00,0x00,0x5e,
58 0x7e,0x5f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xb0,0x00,0x00,0x2f,0x00,
59 0x00,0x00,0x7c,0x00,0x00,0x60,0x27,0x00,0x22,0x28,0x29,0x00,0x00,0x5b,0x5d,0x7b,
60 0x7d,0x00,0x00,0x00,0x00,0xa2,0xa3,0x00,0x00,0x00,0x00,0x2b,0x2d,0x00,0x00,0x00,
61 0x00,0x3d,0x00,0x3c,0x3e,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x5c,
62 0x24,0x00,0x00,0x25,0x23,0x26,0x2a,0x40};
64 /* Maps multibyte cp932 hiragana/katakana to single-byte equivalents */
65 static const unsigned char mbctombb_932_kana[] = {
66 0xa7,0xb1,0xa8,0xb2,0xa9,0xb3,0xaa,0xb4,0xab,0xb5,0xb6,0xb6,0xb7,0xb7,0xb8,0xb8,
67 0xb9,0xb9,0xba,0xba,0xbb,0xbb,0xbc,0xbc,0xbd,0xbd,0xbe,0xbe,0xbf,0xbf,0xc0,0xc0,
68 0xc1,0xc1,0xaf,0xc2,0xc2,0xc3,0xc3,0xc4,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xca,
69 0xca,0xcb,0xcb,0xcb,0xcc,0xcc,0xcc,0xcd,0xcd,0xcd,0xce,0xce,0xce,0xcf,0xd0,0xd1,
70 0xd2,0xd3,0xac,0xd4,0xad,0xd5,0xae,0xd6,0xd7,0xd8,0xd9,0xda,0xdb,0xdc,0xdc,0xb2,
71 0xb4,0xa6,0xdd,0xb3,0xb6,0xb9};
73 static MSVCRT_wchar_t msvcrt_mbc_to_wc(unsigned int ch)
83 mbch[0] = (ch >> 8) & 0xff;
87 if (!MultiByteToWideChar(get_mbcinfo()->mbcodepage, 0, mbch, n_chars, &chW, 1))
89 WARN("MultiByteToWideChar failed on %x\n", ch);
95 static inline MSVCRT_size_t u_strlen( const unsigned char *str )
97 return strlen( (const char*) str );
100 static inline unsigned char* u_strncat( unsigned char* dst, const unsigned char* src, MSVCRT_size_t len )
102 return (unsigned char*)strncat( (char*)dst, (const char*)src, len);
105 static inline int u_strcmp( const unsigned char *s1, const unsigned char *s2 )
107 return strcmp( (const char*)s1, (const char*)s2 );
110 static inline int u_strcasecmp( const unsigned char *s1, const unsigned char *s2 )
112 return strcasecmp( (const char*)s1, (const char*)s2 );
115 static inline int u_strncmp( const unsigned char *s1, const unsigned char *s2, MSVCRT_size_t len )
117 return strncmp( (const char*)s1, (const char*)s2, len );
120 static inline int u_strncasecmp( const unsigned char *s1, const unsigned char *s2, MSVCRT_size_t len )
122 return strncasecmp( (const char*)s1, (const char*)s2, len );
125 static inline unsigned char *u_strchr( const unsigned char *s, unsigned char x )
127 return (unsigned char*) strchr( (const char*)s, x );
130 static inline unsigned char *u_strrchr( const unsigned char *s, unsigned char x )
132 return (unsigned char*) strrchr( (const char*)s, x );
135 static inline unsigned char *u_strtok( unsigned char *s, const unsigned char *delim )
137 return (unsigned char*) strtok( (char*)s, (const char*)delim );
140 static inline unsigned char *u__strset( unsigned char *s, unsigned char c )
142 return (unsigned char*) _strset( (char*)s, c);
145 static inline unsigned char *u__strnset( unsigned char *s, unsigned char c, MSVCRT_size_t len )
147 return (unsigned char*) MSVCRT__strnset( (char*)s, c, len );
150 static inline MSVCRT_size_t u_strcspn( const unsigned char *s, const unsigned char *rej )
152 return strcspn( (const char *)s, (const char*)rej );
155 /*********************************************************************
156 * __p__mbctype (MSVCRT.@)
158 unsigned char* CDECL __p__mbctype(void)
160 return get_mbcinfo()->mbctype;
163 /*********************************************************************
164 * ___mb_cur_max_func(MSVCRT.@)
166 int* CDECL MSVCRT____mb_cur_max_func(void)
168 return &get_locinfo()->mb_cur_max;
171 /* ___mb_cur_max_l_func - not exported in native msvcrt */
172 int* CDECL ___mb_cur_max_l_func(MSVCRT__locale_t locale)
174 MSVCRT_pthreadlocinfo locinfo;
177 locinfo = get_locinfo();
179 locinfo = locale->locinfo;
181 return &locinfo->mb_cur_max;
184 /*********************************************************************
185 * INTERNAL: _setmbcp_l
187 int _setmbcp_l(int cp, LCID lcid, MSVCRT_pthreadmbcinfo mbcinfo)
189 const char format[] = ".%d";
202 mbcinfo = get_mbcinfo();
213 newcp = get_locinfo()->lc_codepage;
216 /* fall through (C locale) */
218 newcp = 20127; /* ASCII */
226 sprintf(bufA, format, newcp);
227 mbcinfo->mblcid = MSVCRT_locale_to_LCID(bufA);
229 mbcinfo->mblcid = lcid;
232 if(mbcinfo->mblcid == -1)
234 WARN("Can't assign LCID to codepage (%d)\n", mbcinfo->mblcid);
238 if (!GetCPInfo(newcp, &cpi))
240 WARN("Codepage %d not found\n", newcp);
241 *MSVCRT__errno() = MSVCRT_EINVAL;
245 /* setup the _mbctype */
246 memset(mbcinfo->mbctype, 0, sizeof(unsigned char[257]));
247 memset(mbcinfo->mbcasemap, 0, sizeof(unsigned char[256]));
249 bytes = cpi.LeadByte;
250 while (bytes[0] || bytes[1])
252 for (i = bytes[0]; i <= bytes[1]; i++)
253 mbcinfo->mbctype[i + 1] |= _M1;
257 if (cpi.MaxCharSize > 1)
259 /* trail bytes not available through kernel32 but stored in a structure in msvcrt */
260 struct cp_extra_info_t *cpextra = g_cpextrainfo;
262 mbcinfo->ismbcodepage = 1;
265 if (cpextra->cp == 0 || cpextra->cp == newcp)
267 if (cpextra->cp == 0)
268 FIXME("trail bytes data not available for DBCS codepage %d - assuming all bytes\n", newcp);
270 bytes = cpextra->TrailBytes;
271 while (bytes[0] || bytes[1])
273 for (i = bytes[0]; i <= bytes[1]; i++)
274 mbcinfo->mbctype[i + 1] |= _M2;
283 mbcinfo->ismbcodepage = 0;
285 /* we can't use GetStringTypeA directly because we don't have a locale - only a code page
288 for (i = 0; i < 256; i++)
289 if (!(mbcinfo->mbctype[i + 1] & _M1))
290 bufA[charcount++] = i;
292 ret = MultiByteToWideChar(newcp, 0, bufA, charcount, bufW, charcount);
293 if (ret != charcount)
294 ERR("MultiByteToWideChar of chars failed for cp %d, ret=%d (exp %d), error=%d\n", newcp, ret, charcount, GetLastError());
296 GetStringTypeW(CT_CTYPE1, bufW, charcount, chartypes);
299 for (i = 0; i < 256; i++)
300 if (!(mbcinfo->mbctype[i + 1] & _M1))
302 if (chartypes[charcount] & C1_UPPER)
304 mbcinfo->mbctype[i + 1] |= _SBUP;
305 bufW[charcount] = tolowerW(bufW[charcount]);
307 else if (chartypes[charcount] & C1_LOWER)
309 mbcinfo->mbctype[i + 1] |= _SBLOW;
310 bufW[charcount] = toupperW(bufW[charcount]);
315 ret = WideCharToMultiByte(newcp, 0, bufW, charcount, bufA, charcount, NULL, NULL);
316 if (ret != charcount)
317 ERR("WideCharToMultiByte failed for cp %d, ret=%d (exp %d), error=%d\n", newcp, ret, charcount, GetLastError());
320 for (i = 0; i < 256; i++)
322 if(!(mbcinfo->mbctype[i + 1] & _M1))
324 if(mbcinfo->mbctype[i] & (C1_UPPER|C1_LOWER))
325 mbcinfo->mbcasemap[i] = bufA[charcount];
330 if (newcp == 932) /* CP932 only - set _MP and _MS */
332 /* On Windows it's possible to calculate the _MP and _MS from CT_CTYPE1
333 * and CT_CTYPE3. But as of Wine 0.9.43 we return wrong values what makes
334 * it hard. As this is set only for codepage 932 we hardcode it what gives
335 * also faster execution.
337 for (i = 161; i <= 165; i++)
338 mbcinfo->mbctype[i + 1] |= _MP;
339 for (i = 166; i <= 223; i++)
340 mbcinfo->mbctype[i + 1] |= _MS;
343 mbcinfo->mbcodepage = newcp;
344 if(MSVCRT_locale && mbcinfo == MSVCRT_locale->mbcinfo)
345 memcpy(MSVCRT_mbctype, MSVCRT_locale->mbcinfo->mbctype, sizeof(MSVCRT_mbctype));
350 /*********************************************************************
351 * _setmbcp (MSVCRT.@)
353 int CDECL _setmbcp(int cp)
355 return _setmbcp_l(cp, -1, NULL);
358 /*********************************************************************
359 * _getmbcp (MSVCRT.@)
361 int CDECL _getmbcp(void)
363 return get_mbcinfo()->mbcodepage;
366 /*********************************************************************
367 * _mbsnextc(MSVCRT.@)
369 unsigned int CDECL _mbsnextc(const unsigned char* str)
372 return *str << 8 | str[1];
376 /*********************************************************************
377 * _mbctolower(MSVCRT.@)
379 unsigned int CDECL _mbctolower(unsigned int c)
383 FIXME("Handle MBC chars\n");
386 return tolower(c); /* ASCII CP or SB char */
389 /*********************************************************************
390 * _mbctoupper(MSVCRT.@)
392 unsigned int CDECL _mbctoupper(unsigned int c)
396 FIXME("Handle MBC chars\n");
399 return toupper(c); /* ASCII CP or SB char */
402 /*********************************************************************
403 * _mbctombb (MSVCRT.@)
405 unsigned int CDECL _mbctombb(unsigned int c)
409 if(get_mbcinfo()->mbcodepage == 932)
411 if(c >= 0x829f && c <= 0x82f1) /* Hiragana */
412 return mbctombb_932_kana[c - 0x829f];
413 if(c >= 0x8340 && c <= 0x8396 && c != 0x837f) /* Katakana */
414 return mbctombb_932_kana[c - 0x8340 - (c >= 0x837f ? 1 : 0)];
415 if(c >= 0x8140 && c <= 0x8197) /* Punctuation */
417 value = mbctombb_932_punct[c - 0x8140];
418 return value ? value : c;
420 if((c >= 0x824f && c <= 0x8258) || /* Fullwidth digits */
421 (c >= 0x8260 && c <= 0x8279)) /* Fullwidth capitals letters */
423 if(c >= 0x8281 && c <= 0x829a) /* Fullwidth small letters */
425 /* all other cases return c */
430 /*********************************************************************
431 * _mbcjistojms(MSVCRT.@)
433 * Converts a jis character to sjis.
434 * Based on description from
435 * http://www.slayers.ne.jp/~oouchi/code/jistosjis.html
437 unsigned int CDECL _mbcjistojms(unsigned int c)
439 /* Conversion takes place only when codepage is 932.
440 In all other cases, c is returned unchanged */
441 if(get_mbcinfo()->mbcodepage == 932)
443 if(HIBYTE(c) >= 0x21 && HIBYTE(c) <= 0x7e &&
444 LOBYTE(c) >= 0x21 && LOBYTE(c) <= 0x7e)
454 c = (((HIBYTE(c) - 0x21)/2 + 0x81) << 8) | LOBYTE(c);
460 return 0; /* Codepage is 932, but c can't be converted */
466 /*********************************************************************
469 unsigned char* CDECL _mbsdec(const unsigned char* start, const unsigned char* cur)
471 if(get_mbcinfo()->ismbcodepage)
472 return (unsigned char *)(_ismbstrail(start,cur-1) ? cur - 2 : cur -1);
474 return (unsigned char *)cur - 1; /* ASCII CP or SB char */
477 /*********************************************************************
480 unsigned int CDECL _mbclen(const unsigned char* str)
482 return _ismbblead(*str) ? 2 : 1;
485 /*********************************************************************
488 unsigned char* CDECL _mbsinc(const unsigned char* str)
490 return (unsigned char *)(str + _mbclen(str));
493 /*********************************************************************
496 unsigned char* CDECL _mbsninc(const unsigned char* str, MSVCRT_size_t num)
501 while (num > 0 && *str)
503 if (_ismbblead(*str))
513 return (unsigned char*)str;
516 /*********************************************************************
519 MSVCRT_size_t CDECL _mbslen(const unsigned char* str)
521 MSVCRT_size_t len = 0;
524 if (_ismbblead(*str))
527 if (!*str) /* count only full chars */
536 /*********************************************************************
539 void CDECL _mbccpy(unsigned char* dest, const unsigned char* src)
543 *++dest = *++src; /* MB char */
546 /*********************************************************************
549 * The parameter n is the number or characters to copy, not the size of
550 * the buffer. Use _mbsnbcpy for a function analogical to strncpy
552 unsigned char* CDECL _mbsncpy(unsigned char* dst, const unsigned char* src, MSVCRT_size_t n)
554 unsigned char* ret = dst;
557 if (get_mbcinfo()->ismbcodepage)
562 if (_ismbblead(*src))
582 if (!(*dst++ = *src++)) break;
585 while (n--) *dst++ = 0;
589 /*********************************************************************
590 * _mbsnbcpy_s(MSVCRT.@)
592 * Unlike _mbsnbcpy this function does not pad the rest of the dest
595 int CDECL _mbsnbcpy_s(unsigned char* dst, MSVCRT_size_t size, const unsigned char* src, MSVCRT_size_t n)
597 MSVCRT_size_t pos = 0;
599 if(!dst || size == 0)
600 return MSVCRT_EINVAL;
604 return MSVCRT_EINVAL;
609 if(get_mbcinfo()->ismbcodepage)
617 return MSVCRT_ERANGE;
619 is_lead = (!is_lead && _ismbblead(*src));
624 if (is_lead) /* if string ends with a lead, remove it */
635 return MSVCRT_ERANGE;
648 return MSVCRT_ERANGE;
654 /*********************************************************************
655 * _mbsnbcpy(MSVCRT.@)
657 * Like strncpy this function doesn't enforce the string to be
660 unsigned char* CDECL _mbsnbcpy(unsigned char* dst, const unsigned char* src, MSVCRT_size_t n)
662 unsigned char* ret = dst;
665 if(get_mbcinfo()->ismbcodepage)
670 is_lead = (!is_lead && _ismbblead(*src));
675 if (is_lead) /* if string ends with a lead, remove it */
683 if (!(*dst++ = *src++)) break;
686 while (n--) *dst++ = 0;
690 /*********************************************************************
693 int CDECL _mbscmp(const unsigned char* str, const unsigned char* cmp)
695 if(get_mbcinfo()->ismbcodepage)
697 unsigned int strc, cmpc;
700 return *cmp ? -1 : 0;
703 strc = _mbsnextc(str);
704 cmpc = _mbsnextc(cmp);
706 return strc < cmpc ? -1 : 1;
707 str +=(strc > 255) ? 2 : 1;
708 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
711 return u_strcmp(str, cmp); /* ASCII CP */
714 /*********************************************************************
715 * _mbsicoll(MSVCRT.@)
716 * FIXME: handle locales.
718 int CDECL _mbsicoll(const unsigned char* str, const unsigned char* cmp)
720 if(get_mbcinfo()->ismbcodepage)
722 unsigned int strc, cmpc;
725 return *cmp ? -1 : 0;
728 strc = _mbctolower(_mbsnextc(str));
729 cmpc = _mbctolower(_mbsnextc(cmp));
731 return strc < cmpc ? -1 : 1;
732 str +=(strc > 255) ? 2 : 1;
733 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
736 return u_strcasecmp(str, cmp); /* ASCII CP */
739 /*********************************************************************
741 * Performs a case-sensitive comparison according to the current code page
743 * _NLSCMPERROR if error
744 * FIXME: handle locales.
746 int CDECL _mbscoll(const unsigned char* str, const unsigned char* cmp)
748 if(get_mbcinfo()->ismbcodepage)
750 unsigned int strc, cmpc;
753 return *cmp ? -1 : 0;
756 strc = _mbsnextc(str);
757 cmpc = _mbsnextc(cmp);
759 return strc < cmpc ? -1 : 1;
760 str +=(strc > 255) ? 2 : 1;
761 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
764 return u_strcmp(str, cmp); /* ASCII CP */
768 /*********************************************************************
771 int CDECL _mbsicmp(const unsigned char* str, const unsigned char* cmp)
773 if(get_mbcinfo()->ismbcodepage)
775 unsigned int strc, cmpc;
778 return *cmp ? -1 : 0;
781 strc = _mbctolower(_mbsnextc(str));
782 cmpc = _mbctolower(_mbsnextc(cmp));
784 return strc < cmpc ? -1 : 1;
785 str +=(strc > 255) ? 2 : 1;
786 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
789 return u_strcasecmp(str, cmp); /* ASCII CP */
792 /*********************************************************************
795 int CDECL _mbsncmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
800 if(get_mbcinfo()->ismbcodepage)
802 unsigned int strc, cmpc;
807 return *cmp ? -1 : 0;
810 strc = _mbsnextc(str);
811 cmpc = _mbsnextc(cmp);
813 return strc < cmpc ? -1 : 1;
814 inc=(strc > 255) ? 2 : 1; /* Equal, use same increment */
818 return 0; /* Matched len chars */
820 return u_strncmp(str, cmp, len); /* ASCII CP */
823 /*********************************************************************
824 * _mbsnbcmp(MSVCRT.@)
826 int CDECL _mbsnbcmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
830 if(get_mbcinfo()->ismbcodepage)
832 unsigned int strc, cmpc;
837 return *cmp ? -1 : 0;
840 if (_ismbblead(*str))
842 strc=(len>=2)?_mbsnextc(str):0;
850 if (_ismbblead(*cmp))
851 cmpc=(len>=2)?_mbsnextc(cmp):0;
855 return strc < cmpc ? -1 : 1;
860 return 0; /* Matched len chars */
862 return u_strncmp(str,cmp,len);
865 /*********************************************************************
866 * _mbsnicmp(MSVCRT.@)
868 * Compare two multibyte strings case insensitively to 'len' characters.
870 int CDECL _mbsnicmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
872 /* FIXME: No tolower() for mb strings yet */
873 if(get_mbcinfo()->ismbcodepage)
875 unsigned int strc, cmpc;
879 return *cmp ? -1 : 0;
882 strc = _mbctolower(_mbsnextc(str));
883 cmpc = _mbctolower(_mbsnextc(cmp));
885 return strc < cmpc ? -1 : 1;
886 str +=(strc > 255) ? 2 : 1;
887 cmp +=(strc > 255) ? 2 : 1; /* Equal, use same increment */
889 return 0; /* Matched len chars */
891 return u_strncasecmp(str, cmp, len); /* ASCII CP */
894 /*********************************************************************
895 * _mbsnbicmp(MSVCRT.@)
897 int CDECL _mbsnbicmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
901 if(get_mbcinfo()->ismbcodepage)
903 unsigned int strc, cmpc;
908 return *cmp ? -1 : 0;
911 if (_ismbblead(*str))
913 strc=(len>=2)?_mbsnextc(str):0;
921 if (_ismbblead(*cmp))
922 cmpc=(len>=2)?_mbsnextc(cmp):0;
925 strc = _mbctolower(strc);
926 cmpc = _mbctolower(cmpc);
928 return strc < cmpc ? -1 : 1;
933 return 0; /* Matched len bytes */
935 return u_strncasecmp(str,cmp,len);
938 /*********************************************************************
941 unsigned char * CDECL _mbscat( unsigned char *dst, const unsigned char *src )
943 strcat( (char *)dst, (const char *)src );
947 /*********************************************************************
950 unsigned char* CDECL _mbscpy( unsigned char *dst, const unsigned char *src )
952 strcpy( (char *)dst, (const char *)src );
956 /*********************************************************************
959 unsigned char * CDECL _mbsstr(const unsigned char *haystack, const unsigned char *needle)
961 return (unsigned char *)strstr( (const char *)haystack, (const char *)needle );
964 /*********************************************************************
967 * Find a multibyte character in a multibyte string.
969 unsigned char* CDECL _mbschr(const unsigned char* s, unsigned int x)
971 if(get_mbcinfo()->ismbcodepage)
978 return (unsigned char*)s;
981 s += c > 255 ? 2 : 1;
984 return u_strchr(s, x); /* ASCII CP */
987 /*********************************************************************
990 unsigned char* CDECL _mbsrchr(const unsigned char* s, unsigned int x)
992 if(get_mbcinfo()->ismbcodepage)
995 unsigned char* match=NULL;
1001 match=(unsigned char*)s;
1004 s +=(c > 255) ? 2 : 1;
1007 return u_strrchr(s, x);
1010 /*********************************************************************
1013 * Find and extract tokens from strings
1015 unsigned char* CDECL _mbstok(unsigned char *str, const unsigned char *delim)
1017 thread_data_t *data = msvcrt_get_thread_data();
1020 if(get_mbcinfo()->ismbcodepage)
1025 if (!(str = data->mbstok_next)) return NULL;
1027 while ((c = _mbsnextc(str)) && _mbschr(delim, c)) {
1028 str += c > 255 ? 2 : 1;
1030 if (!*str) return NULL;
1032 while ((c = _mbsnextc(str)) && !_mbschr(delim, c)) {
1033 str += c > 255 ? 2 : 1;
1037 if (c > 255) *str++ = 0;
1039 data->mbstok_next = str;
1042 return u_strtok(str, delim); /* ASCII CP */
1045 /*********************************************************************
1046 * _mbbtombc(MSVCRT.@)
1048 unsigned int CDECL _mbbtombc(unsigned int c)
1050 if(get_mbcinfo()->ismbcodepage &&
1051 ((c >= 0x20 && c <=0x7e) ||(c >= 0xa1 && c <= 0xdf)))
1053 /* FIXME: I can't get this function to return anything
1054 * different from what I pass it...
1057 return c; /* ASCII CP or no MB char */
1060 /*********************************************************************
1061 * _mbbtype(MSVCRT.@)
1063 int CDECL _mbbtype(unsigned char c, int type)
1067 if ((c >= 0x20 && c <= 0x7e) || (c >= 0xa1 && c <= 0xdf))
1069 else if ((c >= 0x40 && c <= 0x7e) || (c >= 0x80 && c <= 0xfc))
1072 return _MBC_ILLEGAL;
1076 if ((c >= 0x20 && c <= 0x7e) || (c >= 0xa1 && c <= 0xdf))
1078 else if ((c >= 0x81 && c <= 0x9f) || (c >= 0xe0 && c <= 0xfc))
1081 return _MBC_ILLEGAL;
1085 /*********************************************************************
1086 * _ismbbkana(MSVCRT.@)
1088 int CDECL _ismbbkana(unsigned int c)
1090 /* FIXME: use lc_ctype when supported, not lc_all */
1091 if(get_mbcinfo()->mbcodepage == 932)
1093 /* Japanese/Katakana, CP 932 */
1094 return (c >= 0xa1 && c <= 0xdf);
1099 /*********************************************************************
1100 * _ismbcdigit(MSVCRT.@)
1102 int CDECL _ismbcdigit(unsigned int ch)
1104 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1105 return (get_char_typeW( wch ) & C1_DIGIT);
1108 /*********************************************************************
1109 * _ismbcgraph(MSVCRT.@)
1111 int CDECL _ismbcgraph(unsigned int ch)
1113 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1114 return (get_char_typeW( wch ) & (C1_UPPER | C1_LOWER | C1_DIGIT | C1_PUNCT | C1_ALPHA));
1117 /*********************************************************************
1118 * _ismbcalpha (MSVCRT.@)
1120 int CDECL _ismbcalpha(unsigned int ch)
1122 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1123 return (get_char_typeW( wch ) & C1_ALPHA);
1126 /*********************************************************************
1127 * _ismbclower (MSVCRT.@)
1129 int CDECL _ismbclower(unsigned int ch)
1131 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1132 return (get_char_typeW( wch ) & C1_UPPER);
1135 /*********************************************************************
1136 * _ismbcupper (MSVCRT.@)
1138 int CDECL _ismbcupper(unsigned int ch)
1140 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1141 return (get_char_typeW( wch ) & C1_LOWER);
1144 /*********************************************************************
1145 * _ismbcsymbol(MSVCRT.@)
1147 int CDECL _ismbcsymbol(unsigned int ch)
1149 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1151 if (!GetStringTypeW(CT_CTYPE3, &wch, 1, &ctype))
1153 WARN("GetStringTypeW failed on %x\n", ch);
1156 return ((ctype & C3_SYMBOL) != 0);
1159 /*********************************************************************
1160 * _ismbcalnum (MSVCRT.@)
1162 int CDECL _ismbcalnum(unsigned int ch)
1164 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1165 return (get_char_typeW( wch ) & (C1_ALPHA | C1_DIGIT));
1168 /*********************************************************************
1169 * _ismbcspace (MSVCRT.@)
1171 int CDECL _ismbcspace(unsigned int ch)
1173 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1174 return (get_char_typeW( wch ) & C1_SPACE);
1177 /*********************************************************************
1178 * _ismbcprint (MSVCRT.@)
1180 int CDECL _ismbcprint(unsigned int ch)
1182 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1183 return (get_char_typeW( wch ) & (C1_UPPER | C1_LOWER | C1_DIGIT | C1_PUNCT | C1_ALPHA | C1_SPACE));
1186 /*********************************************************************
1187 * _ismbcpunct(MSVCRT.@)
1189 int CDECL _ismbcpunct(unsigned int ch)
1191 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1192 return (get_char_typeW( wch ) & C1_PUNCT);
1195 /*********************************************************************
1196 * _ismbchira(MSVCRT.@)
1198 int CDECL _ismbchira(unsigned int c)
1200 /* FIXME: use lc_ctype when supported, not lc_all */
1201 if(get_mbcinfo()->mbcodepage == 932)
1203 /* Japanese/Hiragana, CP 932 */
1204 return (c >= 0x829f && c <= 0x82f1);
1209 /*********************************************************************
1210 * _ismbckata(MSVCRT.@)
1212 int CDECL _ismbckata(unsigned int c)
1214 /* FIXME: use lc_ctype when supported, not lc_all */
1215 if(get_mbcinfo()->mbcodepage == 932)
1218 return _ismbbkana(c);
1219 /* Japanese/Katakana, CP 932 */
1220 return (c >= 0x8340 && c <= 0x8396 && c != 0x837f);
1225 /*********************************************************************
1226 * _ismbblead(MSVCRT.@)
1228 int CDECL _ismbblead(unsigned int c)
1230 return (get_mbcinfo()->mbctype[(c&0xff) + 1] & _M1) != 0;
1234 /*********************************************************************
1235 * _ismbbtrail(MSVCRT.@)
1237 int CDECL _ismbbtrail(unsigned int c)
1239 return (get_mbcinfo()->mbctype[(c&0xff) + 1] & _M2) != 0;
1242 /*********************************************************************
1243 * _ismbclegal(MSVCRT.@)
1245 int CDECL _ismbclegal(unsigned int c)
1247 return _ismbblead(HIBYTE(c)) && _ismbbtrail(LOBYTE(c));
1250 /*********************************************************************
1251 * _ismbslead(MSVCRT.@)
1253 int CDECL _ismbslead(const unsigned char* start, const unsigned char* str)
1257 if(!get_mbcinfo()->ismbcodepage)
1260 /* Lead bytes can also be trail bytes so we need to analyse the string
1262 while (start <= str)
1266 lead = !lead && _ismbblead(*start);
1270 return lead ? -1 : 0;
1273 /*********************************************************************
1274 * _ismbstrail(MSVCRT.@)
1276 int CDECL _ismbstrail(const unsigned char* start, const unsigned char* str)
1278 /* Note: this function doesn't check _ismbbtrail */
1279 if ((str > start) && _ismbslead(start, str-1))
1285 /*********************************************************************
1286 * _mbsbtype (MSVCRT.@)
1288 int CDECL _mbsbtype(const unsigned char *str, MSVCRT_size_t count)
1291 const unsigned char *end = str + count;
1293 /* Lead bytes can also be trail bytes so we need to analyse the string.
1294 * Also we must return _MBC_ILLEGAL for chars past the end of the string
1296 while (str < end) /* Note: we skip the last byte - will check after the loop */
1299 return _MBC_ILLEGAL;
1300 lead = get_mbcinfo()->ismbcodepage && !lead && _ismbblead(*str);
1305 if (_ismbbtrail(*str))
1308 return _MBC_ILLEGAL;
1310 if (_ismbblead(*str))
1316 /*********************************************************************
1319 unsigned char* CDECL _mbsset(unsigned char* str, unsigned int c)
1321 unsigned char* ret = str;
1323 if(!get_mbcinfo()->ismbcodepage || c < 256)
1324 return u__strset(str, c); /* ASCII CP or SB char */
1326 c &= 0xffff; /* Strip high bits */
1328 while(str[0] && str[1])
1334 str[0] = '\0'; /* FIXME: OK to shorten? */
1339 /*********************************************************************
1340 * _mbsnbset(MSVCRT.@)
1342 unsigned char* CDECL _mbsnbset(unsigned char *str, unsigned int c, MSVCRT_size_t len)
1344 unsigned char *ret = str;
1349 if(!get_mbcinfo()->ismbcodepage || c < 256)
1350 return u__strnset(str, c, len); /* ASCII CP or SB char */
1352 c &= 0xffff; /* Strip high bits */
1354 while(str[0] && str[1] && (len > 1))
1362 /* as per msdn pad with a blank character */
1369 /*********************************************************************
1370 * _mbsnset(MSVCRT.@)
1372 unsigned char* CDECL _mbsnset(unsigned char* str, unsigned int c, MSVCRT_size_t len)
1374 unsigned char *ret = str;
1379 if(!get_mbcinfo()->ismbcodepage || c < 256)
1380 return u__strnset(str, c, len); /* ASCII CP or SB char */
1382 c &= 0xffff; /* Strip high bits */
1384 while(str[0] && str[1] && len--)
1390 str[0] = '\0'; /* FIXME: OK to shorten? */
1395 /*********************************************************************
1396 * _mbsnccnt(MSVCRT.@)
1397 * 'c' is for 'character'.
1399 MSVCRT_size_t CDECL _mbsnccnt(const unsigned char* str, MSVCRT_size_t len)
1402 if(get_mbcinfo()->ismbcodepage)
1405 while(*str && len-- > 0)
1407 if(_ismbblead(*str))
1420 return min(ret, len); /* ASCII CP */
1423 /*********************************************************************
1424 * _mbsnbcnt(MSVCRT.@)
1425 * 'b' is for byte count.
1427 MSVCRT_size_t CDECL _mbsnbcnt(const unsigned char* str, MSVCRT_size_t len)
1430 if(get_mbcinfo()->ismbcodepage)
1432 const unsigned char* xstr = str;
1433 while(*xstr && len-- > 0)
1435 if (_ismbblead(*xstr++))
1441 return min(ret, len); /* ASCII CP */
1444 /*********************************************************************
1445 * _mbsnbcat(MSVCRT.@)
1447 unsigned char* CDECL _mbsnbcat(unsigned char* dst, const unsigned char* src, MSVCRT_size_t len)
1449 if(get_mbcinfo()->ismbcodepage)
1451 unsigned char *res = dst;
1453 if (_ismbblead(*dst++)) {
1457 /* as per msdn overwrite the lead byte in front of '\0' */
1463 while (*src && len--) *dst++ = *src++;
1467 return u_strncat(dst, src, len); /* ASCII CP */
1470 int CDECL _mbsnbcat_s(unsigned char *dst, MSVCRT_size_t size, const unsigned char *src, MSVCRT_size_t len)
1472 unsigned char *ptr = dst;
1475 if (!dst && !size && !src && !len)
1478 if (!dst || !size || !src)
1483 *MSVCRT__errno() = MSVCRT_EINVAL;
1484 return MSVCRT_EINVAL;
1487 /* Find the null terminator of the destination buffer. */
1488 while (size && *ptr)
1494 *MSVCRT__errno() = MSVCRT_EINVAL;
1495 return MSVCRT_EINVAL;
1498 /* If necessary, check that the character preceding the null terminator is
1499 * a lead byte and move the pointer back by one for later overwrite. */
1500 if (ptr != dst && get_mbcinfo()->ismbcodepage && _ismbblead(*(ptr - 1)))
1503 for (i = 0; *src && i < len; i++)
1511 *MSVCRT__errno() = MSVCRT_ERANGE;
1512 return MSVCRT_ERANGE;
1520 /*********************************************************************
1521 * _mbsncat(MSVCRT.@)
1523 unsigned char* CDECL _mbsncat(unsigned char* dst, const unsigned char* src, MSVCRT_size_t len)
1525 if(get_mbcinfo()->ismbcodepage)
1527 unsigned char *res = dst;
1530 if (_ismbblead(*dst++))
1533 while (*src && len--)
1536 if(_ismbblead(*src++))
1542 return u_strncat(dst, src, len); /* ASCII CP */
1546 /*********************************************************************
1549 unsigned char* CDECL _mbslwr(unsigned char* s)
1551 unsigned char *ret = s;
1554 if (get_mbcinfo()->ismbcodepage)
1559 c = _mbctolower(_mbsnextc(s));
1560 /* Note that I assume that the size of the character is unchanged */
1569 else for ( ; *s; s++) *s = tolower(*s);
1574 /*********************************************************************
1575 * _mbslwr_s(MSVCRT.@)
1577 int CDECL _mbslwr_s(unsigned char* s, MSVCRT_size_t len)
1583 else if (!s || !len)
1585 *MSVCRT__errno() = MSVCRT_EINVAL;
1586 return MSVCRT_EINVAL;
1588 if (get_mbcinfo()->ismbcodepage)
1591 for ( ; *s && len > 0; len--)
1593 c = _mbctolower(_mbsnextc(s));
1594 /* Note that I assume that the size of the character is unchanged */
1603 else for ( ; *s && len > 0; s++, len--) *s = tolower(*s);
1607 *MSVCRT__errno() = MSVCRT_EINVAL;
1608 return MSVCRT_EINVAL;
1614 /*********************************************************************
1617 unsigned char* CDECL _mbsupr(unsigned char* s)
1619 unsigned char *ret = s;
1622 if (get_mbcinfo()->ismbcodepage)
1627 c = _mbctoupper(_mbsnextc(s));
1628 /* Note that I assume that the size of the character is unchanged */
1637 else for ( ; *s; s++) *s = toupper(*s);
1642 /*********************************************************************
1643 * _mbsupr_s(MSVCRT.@)
1645 int CDECL _mbsupr_s(unsigned char* s, MSVCRT_size_t len)
1651 else if (!s || !len)
1653 *MSVCRT__errno() = MSVCRT_EINVAL;
1654 return MSVCRT_EINVAL;
1656 if (get_mbcinfo()->ismbcodepage)
1659 for ( ; *s && len > 0; len--)
1661 c = _mbctoupper(_mbsnextc(s));
1662 /* Note that I assume that the size of the character is unchanged */
1671 else for ( ; *s && len > 0; s++, len--) *s = toupper(*s);
1675 *MSVCRT__errno() = MSVCRT_EINVAL;
1676 return MSVCRT_EINVAL;
1682 /*********************************************************************
1683 * _mbsspn (MSVCRT.@)
1685 MSVCRT_size_t CDECL _mbsspn(const unsigned char* string, const unsigned char* set)
1687 const unsigned char *p, *q;
1689 for (p = string; *p; p++)
1693 for (q = set; *q; q++)
1697 if ((*p == *q) && (p[1] == q[1]))
1701 if (!q[0] || !q[1]) break;
1705 for (q = set; *q; q++)
1714 /*********************************************************************
1715 * _mbsspnp (MSVCRT.@)
1717 unsigned char* CDECL _mbsspnp(const unsigned char* string, const unsigned char* set)
1719 const unsigned char *p, *q;
1721 for (p = string; *p; p++)
1725 for (q = set; *q; q++)
1729 if ((*p == *q) && (p[1] == q[1]))
1733 if (!q[0] || !q[1]) break;
1737 for (q = set; *q; q++)
1745 return (unsigned char *)p;
1748 /*********************************************************************
1749 * _mbscspn(MSVCRT.@)
1751 MSVCRT_size_t CDECL _mbscspn(const unsigned char* str, const unsigned char* cmp)
1753 if (get_mbcinfo()->ismbcodepage)
1754 FIXME("don't handle double character case\n");
1755 return u_strcspn(str, cmp);
1758 /*********************************************************************
1759 * _mbsrev (MSVCRT.@)
1761 unsigned char* CDECL _mbsrev(unsigned char* str)
1763 int i, len = _mbslen(str);
1764 unsigned char *p, *temp=MSVCRT_malloc(len*2);
1769 /* unpack multibyte string to temp buffer */
1771 for(i=0; i<len; i++)
1785 /* repack it in the reverse order */
1787 for(i=len-1; i>=0; i--)
1789 if(_ismbblead(temp[i*2]))
1805 /*********************************************************************
1806 * _mbspbrk (MSVCRT.@)
1808 unsigned char* CDECL _mbspbrk(const unsigned char* str, const unsigned char* accept)
1810 const unsigned char* p;
1814 for(p = accept; *p; p += (_ismbblead(*p)?2:1) )
1817 if( !_ismbblead(*p) || ( *(p+1) == *(str+1) ) )
1818 return (unsigned char*)str;
1820 str += (_ismbblead(*str)?2:1);
1827 * Functions depending on locale codepage
1830 /*********************************************************************
1833 * Unlike most of the multibyte string functions this function uses
1834 * the locale codepage, not the codepage set by _setmbcp
1836 int CDECL MSVCRT_mblen(const char* str, MSVCRT_size_t size)
1838 if (str && *str && size)
1840 if(get_locinfo()->mb_cur_max == 1)
1841 return 1; /* ASCII CP */
1843 return !MSVCRT_isleadbyte(*str) ? 1 : (size>1 ? 2 : -1);
1848 /*********************************************************************
1849 * _mbstrlen_l(MSVCRT.@)
1851 MSVCRT_size_t CDECL _mbstrlen_l(const char* str, MSVCRT__locale_t locale)
1853 MSVCRT_pthreadlocinfo locinfo;
1856 locinfo = get_locinfo();
1858 locinfo = locale->locinfo;
1860 if(locinfo->mb_cur_max > 1) {
1861 MSVCRT_size_t len = 0;
1863 /* FIXME: According to the documentation we are supposed to test for
1864 * multi-byte character validity. Whatever that means
1866 str += MSVCRT_isleadbyte(*str) ? 2 : 1;
1875 /*********************************************************************
1876 * _mbstrlen(MSVCRT.@)
1878 MSVCRT_size_t CDECL _mbstrlen(const char* str)
1880 return _mbstrlen_l(str, NULL);
1883 /*********************************************************************
1884 * _mbtowc_l(MSVCRT.@)
1886 int CDECL MSVCRT_mbtowc_l(MSVCRT_wchar_t *dst, const char* str, MSVCRT_size_t n, MSVCRT__locale_t locale)
1888 MSVCRT_pthreadlocinfo locinfo;
1889 MSVCRT_wchar_t tmpdst = '\0';
1892 locinfo = get_locinfo();
1894 locinfo = locale->locinfo;
1898 if(!MultiByteToWideChar(locinfo->lc_codepage, 0, str, n, &tmpdst, 1))
1902 /* return the number of bytes from src that have been used */
1905 if(n >= 2 && MSVCRT__isleadbyte_l(*str, locale) && str[1])
1910 /*********************************************************************
1913 int CDECL MSVCRT_mbtowc(MSVCRT_wchar_t *dst, const char* str, MSVCRT_size_t n)
1915 return MSVCRT_mbtowc_l(dst, str, n, NULL);
1918 /*********************************************************************
1919 * _mbstowcs_l(MSVCRT.@)
1921 MSVCRT_size_t CDECL MSVCRT__mbstowcs_l(MSVCRT_wchar_t *wcstr, const char *mbstr,
1922 MSVCRT_size_t count, MSVCRT__locale_t locale)
1924 MSVCRT_pthreadlocinfo locinfo;
1925 MSVCRT_size_t i, size;
1928 locinfo = get_locinfo();
1930 locinfo = locale->locinfo;
1932 /* Ignore count parameter */
1934 return MultiByteToWideChar(locinfo->lc_codepage, 0, mbstr, -1, NULL, 0)-1;
1936 for(i=0, size=0; i<count; i++) {
1937 if(mbstr[size] == '\0')
1940 size += (MSVCRT__isleadbyte_l(mbstr[size], locale) ? 2 : 1);
1943 size = MultiByteToWideChar(locinfo->lc_codepage, 0,
1944 mbstr, size, wcstr, count);
1946 if(size<count && wcstr)
1952 /*********************************************************************
1953 * mbstowcs(MSVCRT.@)
1955 MSVCRT_size_t CDECL MSVCRT_mbstowcs(MSVCRT_wchar_t *wcstr,
1956 const char *mbstr, MSVCRT_size_t count)
1958 return MSVCRT__mbstowcs_l(wcstr, mbstr, count, NULL);
1961 /*********************************************************************
1962 * _mbstowcs_s_l(MSVCRT.@)
1964 int CDECL MSVCRT__mbstowcs_s_l(MSVCRT_size_t *ret, MSVCRT_wchar_t *wcstr,
1965 MSVCRT_size_t size, const char *mbstr,
1966 MSVCRT_size_t count, MSVCRT__locale_t locale)
1970 if(!wcstr && !size) {
1971 conv = MSVCRT__mbstowcs_l(NULL, mbstr, 0, locale);
1977 if(!MSVCRT_CHECK_PMT(mbstr != NULL) || !MSVCRT_CHECK_PMT(wcstr != NULL)) {
1980 *MSVCRT__errno() = MSVCRT_EINVAL;
1981 return MSVCRT_EINVAL;
1984 if(count==MSVCRT__TRUNCATE || size<count)
1989 conv = MSVCRT__mbstowcs_l(wcstr, mbstr, conv, locale);
1991 wcstr[conv++] = '\0';
1992 else if(conv==size && (count==MSVCRT__TRUNCATE || wcstr[conv-1]=='\0'))
1993 wcstr[conv-1] = '\0';
1995 MSVCRT_INVALID_PMT("wcstr[size] is too small");
1998 *MSVCRT__errno() = MSVCRT_ERANGE;
1999 return MSVCRT_ERANGE;
2007 /*********************************************************************
2008 * mbstowcs_s(MSVCRT.@)
2010 int CDECL MSVCRT__mbstowcs_s(MSVCRT_size_t *ret, MSVCRT_wchar_t *wcstr,
2011 MSVCRT_size_t size, const char *mbstr, MSVCRT_size_t count)
2013 return MSVCRT__mbstowcs_s_l(ret, wcstr, size, mbstr, count, NULL);