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.
27 #include "wine/unicode.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
32 unsigned char MSVCRT_mbctype[257] = { 0 };
33 static int g_mbcp_is_multibyte = 0;
35 int MSVCRT___mb_cur_max = 1;
37 /* It seems that the data about valid trail bytes is not available from kernel32
38 * so we have to store is here. The format is the same as for lead bytes in CPINFO */
39 struct cp_extra_info_t
42 BYTE TrailBytes[MAX_LEADBYTES];
45 static struct cp_extra_info_t g_cpextrainfo[] =
47 {932, {0x40, 0x7e, 0x80, 0xfc, 0, 0}},
48 {936, {0x40, 0xfe, 0, 0}},
49 {949, {0x41, 0xfe, 0, 0}},
50 {950, {0x40, 0x7e, 0xa1, 0xfe, 0, 0}},
51 {1361, {0x31, 0x7e, 0x81, 0xfe, 0, 0}},
52 {20932, {1, 255, 0, 0}}, /* seems to give different results on different systems */
53 {0, {1, 255, 0, 0}} /* match all with FIXME */
56 /* Maps multibyte cp932 punctuation marks to single byte equivalents */
57 static const unsigned char mbctombb_932_punct[] = {
58 0x20,0xa4,0xa1,0x2c,0x2e,0xa5,0x3a,0x3b,0x3f,0x21,0xde,0xdf,0x00,0x00,0x00,0x5e,
59 0x7e,0x5f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xb0,0x00,0x00,0x2f,0x00,
60 0x00,0x00,0x7c,0x00,0x00,0x60,0x27,0x00,0x22,0x28,0x29,0x00,0x00,0x5b,0x5d,0x7b,
61 0x7d,0x00,0x00,0x00,0x00,0xa2,0xa3,0x00,0x00,0x00,0x00,0x2b,0x2d,0x00,0x00,0x00,
62 0x00,0x3d,0x00,0x3c,0x3e,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x5c,
63 0x24,0x00,0x00,0x25,0x23,0x26,0x2a,0x40};
65 /* Maps multibyte cp932 hiragana/katakana to single-byte equivalents */
66 static const unsigned char mbctombb_932_kana[] = {
67 0xa7,0xb1,0xa8,0xb2,0xa9,0xb3,0xaa,0xb4,0xab,0xb5,0xb6,0xb6,0xb7,0xb7,0xb8,0xb8,
68 0xb9,0xb9,0xba,0xba,0xbb,0xbb,0xbc,0xbc,0xbd,0xbd,0xbe,0xbe,0xbf,0xbf,0xc0,0xc0,
69 0xc1,0xc1,0xaf,0xc2,0xc2,0xc3,0xc3,0xc4,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xca,
70 0xca,0xcb,0xcb,0xcb,0xcc,0xcc,0xcc,0xcd,0xcd,0xcd,0xce,0xce,0xce,0xcf,0xd0,0xd1,
71 0xd2,0xd3,0xac,0xd4,0xad,0xd5,0xae,0xd6,0xd7,0xd8,0xd9,0xda,0xdb,0xdc,0xdc,0xb2,
72 0xb4,0xa6,0xdd,0xb3,0xb6,0xb9};
74 static MSVCRT_wchar_t msvcrt_mbc_to_wc(unsigned int ch)
84 mbch[0] = (ch >> 8) & 0xff;
88 if (!MultiByteToWideChar(get_locale()->locinfo->lc_codepage, 0, mbch, n_chars, &chW, 1))
90 WARN("MultiByteToWideChar failed on %x\n", ch);
96 static inline MSVCRT_size_t u_strlen( const unsigned char *str )
98 return strlen( (const char*) str );
101 static inline unsigned char* u_strncat( unsigned char* dst, const unsigned char* src, MSVCRT_size_t len )
103 return (unsigned char*)strncat( (char*)dst, (const char*)src, len);
106 static inline int u_strcmp( const unsigned char *s1, const unsigned char *s2 )
108 return strcmp( (const char*)s1, (const char*)s2 );
111 static inline int u_strcasecmp( const unsigned char *s1, const unsigned char *s2 )
113 return strcasecmp( (const char*)s1, (const char*)s2 );
116 static inline int u_strncmp( const unsigned char *s1, const unsigned char *s2, MSVCRT_size_t len )
118 return strncmp( (const char*)s1, (const char*)s2, len );
121 static inline int u_strncasecmp( const unsigned char *s1, const unsigned char *s2, MSVCRT_size_t len )
123 return strncasecmp( (const char*)s1, (const char*)s2, len );
126 static inline unsigned char *u_strchr( const unsigned char *s, unsigned char x )
128 return (unsigned char*) strchr( (const char*)s, x );
131 static inline unsigned char *u_strrchr( const unsigned char *s, unsigned char x )
133 return (unsigned char*) strrchr( (const char*)s, x );
136 static inline unsigned char *u_strtok( unsigned char *s, const unsigned char *delim )
138 return (unsigned char*) strtok( (char*)s, (const char*)delim );
141 static inline unsigned char *u__strset( unsigned char *s, unsigned char c )
143 return (unsigned char*) _strset( (char*)s, c);
146 static inline unsigned char *u__strnset( unsigned char *s, unsigned char c, MSVCRT_size_t len )
148 return (unsigned char*) MSVCRT__strnset( (char*)s, c, len );
151 static inline MSVCRT_size_t u_strcspn( const unsigned char *s, const unsigned char *rej )
153 return strcspn( (const char *)s, (const char*)rej );
156 /*********************************************************************
157 * __p__mbctype (MSVCRT.@)
159 unsigned char* CDECL __p__mbctype(void)
161 return MSVCRT_mbctype;
164 /*********************************************************************
165 * ___mb_cur_max_func(MSVCRT.@)
167 int* CDECL MSVCRT____mb_cur_max_func(void)
169 return &get_locale()->locinfo->mb_cur_max;
172 /* ___mb_cur_max_l_func - not exported in native msvcrt */
173 int* CDECL ___mb_cur_max_l_func(MSVCRT__locale_t locale)
176 locale = get_locale();
178 return &locale->locinfo->mb_cur_max;
181 /*********************************************************************
182 * _setmbcp (MSVCRT.@)
184 int CDECL _setmbcp(int cp)
186 MSVCRT__locale_t locale = get_locale();
207 newcp = locale->locinfo->lc_codepage;
210 newcp = 20127; /* ASCII */
217 if (!GetCPInfo(newcp, &cpi))
219 WARN("Codepage %d not found\n", newcp);
220 *MSVCRT__errno() = MSVCRT_EINVAL;
224 /* setup the _mbctype */
225 memset(MSVCRT_mbctype, 0, sizeof(MSVCRT_mbctype));
227 bytes = cpi.LeadByte;
228 while (bytes[0] || bytes[1])
230 for (i = bytes[0]; i <= bytes[1]; i++)
231 MSVCRT_mbctype[i + 1] |= _M1;
235 if (cpi.MaxCharSize > 1)
237 /* trail bytes not available through kernel32 but stored in a structure in msvcrt */
238 struct cp_extra_info_t *cpextra = g_cpextrainfo;
240 g_mbcp_is_multibyte = 1;
243 if (cpextra->cp == 0 || cpextra->cp == newcp)
245 if (cpextra->cp == 0)
246 FIXME("trail bytes data not available for DBCS codepage %d - assuming all bytes\n", newcp);
248 bytes = cpextra->TrailBytes;
249 while (bytes[0] || bytes[1])
251 for (i = bytes[0]; i <= bytes[1]; i++)
252 MSVCRT_mbctype[i + 1] |= _M2;
261 g_mbcp_is_multibyte = 0;
263 /* we can't use GetStringTypeA directly because we don't have a locale - only a code page
266 for (i = 0; i < 256; i++)
267 if (!(MSVCRT_mbctype[i + 1] & _M1))
268 bufA[charcount++] = i;
270 ret = MultiByteToWideChar(newcp, 0, bufA, charcount, bufW, charcount);
271 if (ret != charcount)
272 ERR("MultiByteToWideChar of chars failed for cp %d, ret=%d (exp %d), error=%d\n", newcp, ret, charcount, GetLastError());
274 GetStringTypeW(CT_CTYPE1, bufW, charcount, chartypes);
276 curr_type = chartypes;
277 for (i = 0; i < 256; i++)
278 if (!(MSVCRT_mbctype[i + 1] & _M1))
280 if ((*curr_type) & C1_UPPER)
281 MSVCRT_mbctype[i + 1] |= _SBUP;
282 if ((*curr_type) & C1_LOWER)
283 MSVCRT_mbctype[i + 1] |= _SBLOW;
287 if (newcp == 932) /* CP932 only - set _MP and _MS */
289 /* On Windows it's possible to calculate the _MP and _MS from CT_CTYPE1
290 * and CT_CTYPE3. But as of Wine 0.9.43 we return wrong values what makes
291 * it hard. As this is set only for codepage 932 we hardcode it what gives
292 * also faster execution.
294 for (i = 161; i <= 165; i++)
295 MSVCRT_mbctype[i + 1] |= _MP;
296 for (i = 166; i <= 223; i++)
297 MSVCRT_mbctype[i + 1] |= _MS;
300 locale->locinfo->lc_collate_cp = newcp;
301 locale->locinfo->lc_codepage = newcp;
302 TRACE("(%d) -> %d\n", cp, locale->locinfo->lc_codepage);
306 /*********************************************************************
307 * _getmbcp (MSVCRT.@)
309 int CDECL _getmbcp(void)
311 /* FIXME: get_locale()->mbcinfo->mbcodepage should be used here */
312 return get_locale()->locinfo->lc_codepage;
315 /*********************************************************************
316 * _mbsnextc(MSVCRT.@)
318 unsigned int CDECL _mbsnextc(const unsigned char* str)
321 return *str << 8 | str[1];
325 /*********************************************************************
326 * _mbctolower(MSVCRT.@)
328 unsigned int CDECL _mbctolower(unsigned int c)
330 if (MSVCRT_isleadbyte(c))
332 FIXME("Handle MBC chars\n");
335 return tolower(c); /* ASCII CP or SB char */
338 /*********************************************************************
339 * _mbctoupper(MSVCRT.@)
341 unsigned int CDECL _mbctoupper(unsigned int c)
343 if (MSVCRT_isleadbyte(c))
345 FIXME("Handle MBC chars\n");
348 return toupper(c); /* ASCII CP or SB char */
351 /*********************************************************************
352 * _mbctombb (MSVCRT.@)
354 unsigned int CDECL _mbctombb(unsigned int c)
358 if(get_locale()->locinfo->lc_codepage == 932)
360 if(c >= 0x829f && c <= 0x82f1) /* Hiragana */
361 return mbctombb_932_kana[c - 0x829f];
362 if(c >= 0x8340 && c <= 0x8396 && c != 0x837f) /* Katakana */
363 return mbctombb_932_kana[c - 0x8340 - (c >= 0x837f ? 1 : 0)];
364 if(c >= 0x8140 && c <= 0x8197) /* Punctuation */
366 value = mbctombb_932_punct[c - 0x8140];
367 return value ? value : c;
369 if((c >= 0x824f && c <= 0x8258) || /* Fullwidth digits */
370 (c >= 0x8260 && c <= 0x8279)) /* Fullwidth capitals letters */
372 if(c >= 0x8281 && c <= 0x829a) /* Fullwidth small letters */
374 /* all other cases return c */
379 /*********************************************************************
380 * _mbcjistojms(MSVCRT.@)
382 * Converts a jis character to sjis.
383 * Based on description from
384 * http://www.slayers.ne.jp/~oouchi/code/jistosjis.html
386 unsigned int CDECL _mbcjistojms(unsigned int c)
388 /* Conversion takes place only when codepage is 932.
389 In all other cases, c is returned unchanged */
390 if(get_locale()->locinfo->lc_codepage == 932)
392 if(HIBYTE(c) >= 0x21 && HIBYTE(c) <= 0x7e &&
393 LOBYTE(c) >= 0x21 && LOBYTE(c) <= 0x7e)
403 c = (((HIBYTE(c) - 0x21)/2 + 0x81) << 8) | LOBYTE(c);
409 return 0; /* Codepage is 932, but c can't be converted */
415 /*********************************************************************
418 unsigned char* CDECL _mbsdec(const unsigned char* start, const unsigned char* cur)
420 if(get_locale()->locinfo->mb_cur_max > 1)
421 return (unsigned char *)(_ismbstrail(start,cur-1) ? cur - 2 : cur -1);
423 return (unsigned char *)cur - 1; /* ASCII CP or SB char */
426 /*********************************************************************
429 unsigned int CDECL _mbclen(const unsigned char* str)
431 return _ismbblead(*str) ? 2 : 1;
434 /*********************************************************************
437 unsigned char* CDECL _mbsinc(const unsigned char* str)
439 return (unsigned char *)(str + _mbclen(str));
442 /*********************************************************************
445 unsigned char* CDECL _mbsninc(const unsigned char* str, MSVCRT_size_t num)
450 while (num > 0 && *str)
452 if (_ismbblead(*str))
462 return (unsigned char*)str;
465 /*********************************************************************
468 MSVCRT_size_t CDECL _mbslen(const unsigned char* str)
470 MSVCRT_size_t len = 0;
473 if (_ismbblead(*str))
476 if (!*str) /* count only full chars */
485 /*********************************************************************
488 void CDECL _mbccpy(unsigned char* dest, const unsigned char* src)
492 *++dest = *++src; /* MB char */
495 /*********************************************************************
498 * The parameter n is the number or characters to copy, not the size of
499 * the buffer. Use _mbsnbcpy for a function analogical to strncpy
501 unsigned char* CDECL _mbsncpy(unsigned char* dst, const unsigned char* src, MSVCRT_size_t n)
503 unsigned char* ret = dst;
506 if (g_mbcp_is_multibyte)
511 if (_ismbblead(*src))
531 if (!(*dst++ = *src++)) break;
534 while (n--) *dst++ = 0;
538 /*********************************************************************
539 * _mbsnbcpy_s(MSVCRT.@)
541 * Unlike _mbsnbcpy this function does not pad the rest of the dest
544 int CDECL _mbsnbcpy_s(unsigned char* dst, MSVCRT_size_t size, const unsigned char* src, MSVCRT_size_t n)
546 MSVCRT_size_t pos = 0;
548 if(!dst || size == 0)
549 return MSVCRT_EINVAL;
553 return MSVCRT_EINVAL;
558 if(g_mbcp_is_multibyte)
566 return MSVCRT_ERANGE;
568 is_lead = (!is_lead && _ismbblead(*src));
573 if (is_lead) /* if string ends with a lead, remove it */
584 return MSVCRT_ERANGE;
597 return MSVCRT_ERANGE;
603 /*********************************************************************
604 * _mbsnbcpy(MSVCRT.@)
606 * Like strncpy this function doesn't enforce the string to be
609 unsigned char* CDECL _mbsnbcpy(unsigned char* dst, const unsigned char* src, MSVCRT_size_t n)
611 unsigned char* ret = dst;
614 if(g_mbcp_is_multibyte)
619 is_lead = (!is_lead && _ismbblead(*src));
624 if (is_lead) /* if string ends with a lead, remove it */
632 if (!(*dst++ = *src++)) break;
635 while (n--) *dst++ = 0;
639 /*********************************************************************
642 int CDECL _mbscmp(const unsigned char* str, const unsigned char* cmp)
644 if(get_locale()->locinfo->mb_cur_max > 1)
646 unsigned int strc, cmpc;
649 return *cmp ? -1 : 0;
652 strc = _mbsnextc(str);
653 cmpc = _mbsnextc(cmp);
655 return strc < cmpc ? -1 : 1;
656 str +=(strc > 255) ? 2 : 1;
657 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
660 return u_strcmp(str, cmp); /* ASCII CP */
663 /*********************************************************************
664 * _mbsicoll(MSVCRT.@)
665 * FIXME: handle locales.
667 int CDECL _mbsicoll(const unsigned char* str, const unsigned char* cmp)
669 if(get_locale()->locinfo->mb_cur_max > 1)
671 unsigned int strc, cmpc;
674 return *cmp ? -1 : 0;
677 strc = _mbctolower(_mbsnextc(str));
678 cmpc = _mbctolower(_mbsnextc(cmp));
680 return strc < cmpc ? -1 : 1;
681 str +=(strc > 255) ? 2 : 1;
682 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
685 return u_strcasecmp(str, cmp); /* ASCII CP */
688 /*********************************************************************
690 * Performs a case-sensitive comparison according to the current code page
692 * _NLSCMPERROR if error
693 * FIXME: handle locales.
695 int CDECL _mbscoll(const unsigned char* str, const unsigned char* cmp)
697 if(get_locale()->locinfo->mb_cur_max > 1)
699 unsigned int strc, cmpc;
702 return *cmp ? -1 : 0;
705 strc = _mbsnextc(str);
706 cmpc = _mbsnextc(cmp);
708 return strc < cmpc ? -1 : 1;
709 str +=(strc > 255) ? 2 : 1;
710 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
713 return u_strcmp(str, cmp); /* ASCII CP */
717 /*********************************************************************
720 int CDECL _mbsicmp(const unsigned char* str, const unsigned char* cmp)
722 if(get_locale()->locinfo->mb_cur_max > 1)
724 unsigned int strc, cmpc;
727 return *cmp ? -1 : 0;
730 strc = _mbctolower(_mbsnextc(str));
731 cmpc = _mbctolower(_mbsnextc(cmp));
733 return strc < cmpc ? -1 : 1;
734 str +=(strc > 255) ? 2 : 1;
735 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
738 return u_strcasecmp(str, cmp); /* ASCII CP */
741 /*********************************************************************
744 int CDECL _mbsncmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
749 if(get_locale()->locinfo->mb_cur_max > 1)
751 unsigned int strc, cmpc;
756 return *cmp ? -1 : 0;
759 strc = _mbsnextc(str);
760 cmpc = _mbsnextc(cmp);
762 return strc < cmpc ? -1 : 1;
763 inc=(strc > 255) ? 2 : 1; /* Equal, use same increment */
767 return 0; /* Matched len chars */
769 return u_strncmp(str, cmp, len); /* ASCII CP */
772 /*********************************************************************
773 * _mbsnbcmp(MSVCRT.@)
775 int CDECL _mbsnbcmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
779 if(get_locale()->locinfo->mb_cur_max > 1)
781 unsigned int strc, cmpc;
786 return *cmp ? -1 : 0;
789 if (MSVCRT_isleadbyte(*str))
791 strc=(len>=2)?_mbsnextc(str):0;
799 if (MSVCRT_isleadbyte(*cmp))
800 cmpc=(len>=2)?_mbsnextc(cmp):0;
804 return strc < cmpc ? -1 : 1;
809 return 0; /* Matched len chars */
811 return u_strncmp(str,cmp,len);
814 /*********************************************************************
815 * _mbsnicmp(MSVCRT.@)
817 * Compare two multibyte strings case insensitively to 'len' characters.
819 int CDECL _mbsnicmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
821 /* FIXME: No tolower() for mb strings yet */
822 if(get_locale()->locinfo->mb_cur_max > 1)
824 unsigned int strc, cmpc;
828 return *cmp ? -1 : 0;
831 strc = _mbctolower(_mbsnextc(str));
832 cmpc = _mbctolower(_mbsnextc(cmp));
834 return strc < cmpc ? -1 : 1;
835 str +=(strc > 255) ? 2 : 1;
836 cmp +=(strc > 255) ? 2 : 1; /* Equal, use same increment */
838 return 0; /* Matched len chars */
840 return u_strncasecmp(str, cmp, len); /* ASCII CP */
843 /*********************************************************************
844 * _mbsnbicmp(MSVCRT.@)
846 int CDECL _mbsnbicmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
850 if(get_locale()->locinfo->mb_cur_max > 1)
852 unsigned int strc, cmpc;
857 return *cmp ? -1 : 0;
860 if (MSVCRT_isleadbyte(*str))
862 strc=(len>=2)?_mbsnextc(str):0;
870 if (MSVCRT_isleadbyte(*cmp))
871 cmpc=(len>=2)?_mbsnextc(cmp):0;
874 strc = _mbctolower(strc);
875 cmpc = _mbctolower(cmpc);
877 return strc < cmpc ? -1 : 1;
882 return 0; /* Matched len bytes */
884 return u_strncasecmp(str,cmp,len);
887 /*********************************************************************
890 unsigned char * CDECL _mbscat( unsigned char *dst, const unsigned char *src )
892 strcat( (char *)dst, (const char *)src );
896 /*********************************************************************
899 unsigned char* CDECL _mbscpy( unsigned char *dst, const unsigned char *src )
901 strcpy( (char *)dst, (const char *)src );
905 /*********************************************************************
908 unsigned char * CDECL _mbsstr(const unsigned char *haystack, const unsigned char *needle)
910 return (unsigned char *)strstr( (const char *)haystack, (const char *)needle );
913 /*********************************************************************
916 * Find a multibyte character in a multibyte string.
918 unsigned char* CDECL _mbschr(const unsigned char* s, unsigned int x)
920 if(get_locale()->locinfo->mb_cur_max > 1)
927 return (unsigned char*)s;
930 s += c > 255 ? 2 : 1;
933 return u_strchr(s, x); /* ASCII CP */
936 /*********************************************************************
939 unsigned char* CDECL _mbsrchr(const unsigned char* s, unsigned int x)
941 if(get_locale()->locinfo->mb_cur_max > 1)
944 unsigned char* match=NULL;
950 match=(unsigned char*)s;
953 s +=(c > 255) ? 2 : 1;
956 return u_strrchr(s, x);
959 /*********************************************************************
962 * Find and extract tokens from strings
964 unsigned char* CDECL _mbstok(unsigned char *str, const unsigned char *delim)
966 thread_data_t *data = msvcrt_get_thread_data();
969 if(get_locale()->locinfo->mb_cur_max > 1)
974 if (!(str = data->mbstok_next)) return NULL;
976 while ((c = _mbsnextc(str)) && _mbschr(delim, c)) {
977 str += c > 255 ? 2 : 1;
979 if (!*str) return NULL;
981 while ((c = _mbsnextc(str)) && !_mbschr(delim, c)) {
982 str += c > 255 ? 2 : 1;
986 if (c > 255) *str++ = 0;
988 data->mbstok_next = str;
991 return u_strtok(str, delim); /* ASCII CP */
994 /*********************************************************************
997 int CDECL MSVCRT_mbtowc(MSVCRT_wchar_t *dst, const char* str, MSVCRT_size_t n)
999 /* temp var needed because MultiByteToWideChar wants non NULL destination */
1000 MSVCRT_wchar_t tmpdst = '\0';
1004 if(!MultiByteToWideChar(CP_ACP, 0, str, n, &tmpdst, 1))
1008 /* return the number of bytes from src that have been used */
1011 if(n >= 2 && MSVCRT_isleadbyte(*str) && str[1])
1016 /*********************************************************************
1017 * _mbbtombc(MSVCRT.@)
1019 unsigned int CDECL _mbbtombc(unsigned int c)
1021 if(get_locale()->locinfo->mb_cur_max > 1 &&
1022 ((c >= 0x20 && c <=0x7e) ||(c >= 0xa1 && c <= 0xdf)))
1024 /* FIXME: I can't get this function to return anything
1025 * different from what I pass it...
1028 return c; /* ASCII CP or no MB char */
1031 /*********************************************************************
1032 * _mbbtype(MSVCRT.@)
1034 int CDECL _mbbtype(unsigned char c, int type)
1038 if ((c >= 0x20 && c <= 0x7e) || (c >= 0xa1 && c <= 0xdf))
1040 else if ((c >= 0x40 && c <= 0x7e) || (c >= 0x80 && c <= 0xfc))
1043 return _MBC_ILLEGAL;
1047 if ((c >= 0x20 && c <= 0x7e) || (c >= 0xa1 && c <= 0xdf))
1049 else if ((c >= 0x81 && c <= 0x9f) || (c >= 0xe0 && c <= 0xfc))
1052 return _MBC_ILLEGAL;
1056 /*********************************************************************
1057 * _ismbbkana(MSVCRT.@)
1059 int CDECL _ismbbkana(unsigned int c)
1061 /* FIXME: use lc_ctype when supported, not lc_all */
1062 if(get_locale()->locinfo->lc_codepage == 932)
1064 /* Japanese/Katakana, CP 932 */
1065 return (c >= 0xa1 && c <= 0xdf);
1070 /*********************************************************************
1071 * _ismbcdigit(MSVCRT.@)
1073 int CDECL _ismbcdigit(unsigned int ch)
1075 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1076 return (get_char_typeW( wch ) & C1_DIGIT);
1079 /*********************************************************************
1080 * _ismbcgraph(MSVCRT.@)
1082 int CDECL _ismbcgraph(unsigned int ch)
1084 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1085 return (get_char_typeW( wch ) & (C1_UPPER | C1_LOWER | C1_DIGIT | C1_PUNCT | C1_ALPHA));
1088 /*********************************************************************
1089 * _ismbcalpha (MSVCRT.@)
1091 int CDECL _ismbcalpha(unsigned int ch)
1093 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1094 return (get_char_typeW( wch ) & C1_ALPHA);
1097 /*********************************************************************
1098 * _ismbclower (MSVCRT.@)
1100 int CDECL _ismbclower(unsigned int ch)
1102 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1103 return (get_char_typeW( wch ) & C1_UPPER);
1106 /*********************************************************************
1107 * _ismbcupper (MSVCRT.@)
1109 int CDECL _ismbcupper(unsigned int ch)
1111 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1112 return (get_char_typeW( wch ) & C1_LOWER);
1115 /*********************************************************************
1116 * _ismbcsymbol(MSVCRT.@)
1118 int CDECL _ismbcsymbol(unsigned int ch)
1120 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1122 if (!GetStringTypeW(CT_CTYPE3, &wch, 1, &ctype))
1124 WARN("GetStringTypeW failed on %x\n", ch);
1127 return ((ctype & C3_SYMBOL) != 0);
1130 /*********************************************************************
1131 * _ismbcalnum (MSVCRT.@)
1133 int CDECL _ismbcalnum(unsigned int ch)
1135 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1136 return (get_char_typeW( wch ) & (C1_ALPHA | C1_DIGIT));
1139 /*********************************************************************
1140 * _ismbcspace (MSVCRT.@)
1142 int CDECL _ismbcspace(unsigned int ch)
1144 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1145 return (get_char_typeW( wch ) & C1_SPACE);
1148 /*********************************************************************
1149 * _ismbcprint (MSVCRT.@)
1151 int CDECL _ismbcprint(unsigned int ch)
1153 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1154 return (get_char_typeW( wch ) & (C1_UPPER | C1_LOWER | C1_DIGIT | C1_PUNCT | C1_ALPHA | C1_SPACE));
1157 /*********************************************************************
1158 * _ismbcpunct(MSVCRT.@)
1160 int CDECL _ismbcpunct(unsigned int ch)
1162 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1163 return (get_char_typeW( wch ) & C1_PUNCT);
1166 /*********************************************************************
1167 * _ismbchira(MSVCRT.@)
1169 int CDECL _ismbchira(unsigned int c)
1171 /* FIXME: use lc_ctype when supported, not lc_all */
1172 if(get_locale()->locinfo->lc_codepage == 932)
1174 /* Japanese/Hiragana, CP 932 */
1175 return (c >= 0x829f && c <= 0x82f1);
1180 /*********************************************************************
1181 * _ismbckata(MSVCRT.@)
1183 int CDECL _ismbckata(unsigned int c)
1185 /* FIXME: use lc_ctype when supported, not lc_all */
1186 if(get_locale()->locinfo->lc_codepage == 932)
1189 return _ismbbkana(c);
1190 /* Japanese/Katakana, CP 932 */
1191 return (c >= 0x8340 && c <= 0x8396 && c != 0x837f);
1196 /*********************************************************************
1197 * _ismbblead(MSVCRT.@)
1199 int CDECL _ismbblead(unsigned int c)
1201 return (MSVCRT_mbctype[(c&0xff) + 1] & _M1) != 0;
1205 /*********************************************************************
1206 * _ismbbtrail(MSVCRT.@)
1208 int CDECL _ismbbtrail(unsigned int c)
1210 return (MSVCRT_mbctype[(c&0xff) + 1] & _M2) != 0;
1213 /*********************************************************************
1214 * _ismbclegal(MSVCRT.@)
1216 int CDECL _ismbclegal(unsigned int c)
1218 return _ismbblead(HIBYTE(c)) && _ismbbtrail(LOBYTE(c));
1221 /*********************************************************************
1222 * _ismbslead(MSVCRT.@)
1224 int CDECL _ismbslead(const unsigned char* start, const unsigned char* str)
1228 if(!g_mbcp_is_multibyte)
1231 /* Lead bytes can also be trail bytes so we need to analyse the string
1233 while (start <= str)
1237 lead = !lead && _ismbblead(*start);
1241 return lead ? -1 : 0;
1244 /*********************************************************************
1245 * _ismbstrail(MSVCRT.@)
1247 int CDECL _ismbstrail(const unsigned char* start, const unsigned char* str)
1249 /* Note: this function doesn't check _ismbbtrail */
1250 if ((str > start) && _ismbslead(start, str-1))
1256 /*********************************************************************
1257 * _mbsbtype (MSVCRT.@)
1259 int CDECL _mbsbtype(const unsigned char *str, MSVCRT_size_t count)
1262 const unsigned char *end = str + count;
1263 int mbcp = g_mbcp_is_multibyte;
1265 /* Lead bytes can also be trail bytes so we need to analyse the string.
1266 * Also we must return _MBC_ILLEGAL for chars past the end of the string
1268 while (str < end) /* Note: we skip the last byte - will check after the loop */
1271 return _MBC_ILLEGAL;
1272 lead = mbcp && !lead && _ismbblead(*str);
1277 if (_ismbbtrail(*str))
1280 return _MBC_ILLEGAL;
1282 if (_ismbblead(*str))
1288 /*********************************************************************
1291 unsigned char* CDECL _mbsset(unsigned char* str, unsigned int c)
1293 unsigned char* ret = str;
1295 if(get_locale()->locinfo->mb_cur_max == 1 || c < 256)
1296 return u__strset(str, c); /* ASCII CP or SB char */
1298 c &= 0xffff; /* Strip high bits */
1300 while(str[0] && str[1])
1306 str[0] = '\0'; /* FIXME: OK to shorten? */
1311 /*********************************************************************
1312 * _mbsnbset(MSVCRT.@)
1314 unsigned char* CDECL _mbsnbset(unsigned char *str, unsigned int c, MSVCRT_size_t len)
1316 unsigned char *ret = str;
1321 if(get_locale()->locinfo->mb_cur_max == 1 || c < 256)
1322 return u__strnset(str, c, len); /* ASCII CP or SB char */
1324 c &= 0xffff; /* Strip high bits */
1326 while(str[0] && str[1] && (len > 1))
1334 /* as per msdn pad with a blank character */
1341 /*********************************************************************
1342 * _mbsnset(MSVCRT.@)
1344 unsigned char* CDECL _mbsnset(unsigned char* str, unsigned int c, MSVCRT_size_t len)
1346 unsigned char *ret = str;
1351 if(get_locale()->locinfo->mb_cur_max == 1 || c < 256)
1352 return u__strnset(str, c, len); /* ASCII CP or SB char */
1354 c &= 0xffff; /* Strip high bits */
1356 while(str[0] && str[1] && len--)
1362 str[0] = '\0'; /* FIXME: OK to shorten? */
1367 /*********************************************************************
1368 * _mbsnccnt(MSVCRT.@)
1369 * 'c' is for 'character'.
1371 MSVCRT_size_t CDECL _mbsnccnt(const unsigned char* str, MSVCRT_size_t len)
1374 if(get_locale()->locinfo->mb_cur_max > 1)
1377 while(*str && len-- > 0)
1379 if(MSVCRT_isleadbyte(*str))
1392 return min(ret, len); /* ASCII CP */
1395 /*********************************************************************
1396 * _mbsnbcnt(MSVCRT.@)
1397 * 'b' is for byte count.
1399 MSVCRT_size_t CDECL _mbsnbcnt(const unsigned char* str, MSVCRT_size_t len)
1402 if(get_locale()->locinfo->mb_cur_max > 1)
1404 const unsigned char* xstr = str;
1405 while(*xstr && len-- > 0)
1407 if (MSVCRT_isleadbyte(*xstr++))
1413 return min(ret, len); /* ASCII CP */
1416 /*********************************************************************
1417 * _mbsnbcat(MSVCRT.@)
1419 unsigned char* CDECL _mbsnbcat(unsigned char* dst, const unsigned char* src, MSVCRT_size_t len)
1421 if(get_locale()->locinfo->mb_cur_max > 1)
1423 unsigned char *res = dst;
1425 if (MSVCRT_isleadbyte(*dst++)) {
1429 /* as per msdn overwrite the lead byte in front of '\0' */
1435 while (*src && len--) *dst++ = *src++;
1439 return u_strncat(dst, src, len); /* ASCII CP */
1443 /*********************************************************************
1444 * _mbsncat(MSVCRT.@)
1446 unsigned char* CDECL _mbsncat(unsigned char* dst, const unsigned char* src, MSVCRT_size_t len)
1448 if(get_locale()->locinfo->mb_cur_max > 1)
1450 unsigned char *res = dst;
1453 if (MSVCRT_isleadbyte(*dst++))
1456 while (*src && len--)
1459 if(MSVCRT_isleadbyte(*src++))
1465 return u_strncat(dst, src, len); /* ASCII CP */
1469 /*********************************************************************
1472 unsigned char* CDECL _mbslwr(unsigned char* s)
1474 unsigned char *ret = s;
1477 if (get_locale()->locinfo->mb_cur_max > 1)
1482 c = _mbctolower(_mbsnextc(s));
1483 /* Note that I assume that the size of the character is unchanged */
1492 else for ( ; *s; s++) *s = tolower(*s);
1497 /*********************************************************************
1500 unsigned char* CDECL _mbsupr(unsigned char* s)
1502 unsigned char *ret = s;
1505 if (get_locale()->locinfo->mb_cur_max > 1)
1510 c = _mbctoupper(_mbsnextc(s));
1511 /* Note that I assume that the size of the character is unchanged */
1520 else for ( ; *s; s++) *s = toupper(*s);
1525 /*********************************************************************
1526 * _mbsspn (MSVCRT.@)
1528 MSVCRT_size_t CDECL _mbsspn(const unsigned char* string, const unsigned char* set)
1530 const unsigned char *p, *q;
1532 for (p = string; *p; p++)
1534 if (MSVCRT_isleadbyte(*p))
1536 for (q = set; *q; q++)
1540 if ((*p == *q) && (p[1] == q[1]))
1544 if (!q[0] || !q[1]) break;
1548 for (q = set; *q; q++)
1557 /*********************************************************************
1558 * _mbsspnp (MSVCRT.@)
1560 unsigned char* CDECL _mbsspnp(const unsigned char* string, const unsigned char* set)
1562 const unsigned char *p, *q;
1564 for (p = string; *p; p++)
1566 if (MSVCRT_isleadbyte(*p))
1568 for (q = set; *q; q++)
1572 if ((*p == *q) && (p[1] == q[1]))
1576 if (!q[0] || !q[1]) break;
1580 for (q = set; *q; q++)
1588 return (unsigned char *)p;
1591 /*********************************************************************
1592 * _mbscspn(MSVCRT.@)
1594 MSVCRT_size_t CDECL _mbscspn(const unsigned char* str, const unsigned char* cmp)
1596 if (get_locale()->locinfo->mb_cur_max > 1)
1597 FIXME("don't handle double character case\n");
1598 return u_strcspn(str, cmp);
1601 /*********************************************************************
1602 * _mbsrev (MSVCRT.@)
1604 unsigned char* CDECL _mbsrev(unsigned char* str)
1606 int i, len = _mbslen(str);
1607 unsigned char *p, *temp=MSVCRT_malloc(len*2);
1612 /* unpack multibyte string to temp buffer */
1614 for(i=0; i<len; i++)
1616 if (MSVCRT_isleadbyte(*p))
1628 /* repack it in the reverse order */
1630 for(i=len-1; i>=0; i--)
1632 if(MSVCRT_isleadbyte(temp[i*2]))
1648 /*********************************************************************
1649 * _mbspbrk (MSVCRT.@)
1651 unsigned char* CDECL _mbspbrk(const unsigned char* str, const unsigned char* accept)
1653 const unsigned char* p;
1657 for(p = accept; *p; p += (MSVCRT_isleadbyte(*p)?2:1) )
1660 if( !MSVCRT_isleadbyte(*p) || ( *(p+1) == *(str+1) ) )
1661 return (unsigned char*)str;
1663 str += (MSVCRT_isleadbyte(*str)?2:1);
1670 * Functions depending on locale codepage
1673 /*********************************************************************
1676 * Unlike most of the multibyte string functions this function uses
1677 * the locale codepage, not the codepage set by _setmbcp
1679 int CDECL MSVCRT_mblen(const char* str, MSVCRT_size_t size)
1681 if (str && *str && size)
1683 if(get_locale()->locinfo->mb_cur_max == 1)
1684 return 1; /* ASCII CP */
1686 return !MSVCRT_isleadbyte(*str) ? 1 : (size>1 ? 2 : -1);
1691 /*********************************************************************
1692 * _mbstrlen_l(MSVCRT.@)
1694 MSVCRT_size_t CDECL _mbstrlen_l(const char* str, MSVCRT__locale_t locale)
1697 locale = get_locale();
1699 if(locale->locinfo->mb_cur_max > 1) {
1700 MSVCRT_size_t len = 0;
1702 /* FIXME: According to the documentation we are supposed to test for
1703 * multi-byte character validity. Whatever that means
1705 str += MSVCRT_isleadbyte(*str) ? 2 : 1;
1714 /*********************************************************************
1715 * _mbstrlen(MSVCRT.@)
1717 MSVCRT_size_t CDECL _mbstrlen(const char* str)
1719 return _mbstrlen_l(str, NULL);