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(MSVCRT___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 &MSVCRT___mb_cur_max;
172 /*********************************************************************
173 * _setmbcp (MSVCRT.@)
175 int CDECL _setmbcp(int cp)
197 newcp = MSVCRT___lc_codepage;
200 newcp = 20127; /* ASCII */
207 if (!GetCPInfo(newcp, &cpi))
209 WARN("Codepage %d not found\n", newcp);
210 *MSVCRT__errno() = MSVCRT_EINVAL;
214 /* setup the _mbctype */
215 memset(MSVCRT_mbctype, 0, sizeof(MSVCRT_mbctype));
217 bytes = cpi.LeadByte;
218 while (bytes[0] || bytes[1])
220 for (i = bytes[0]; i <= bytes[1]; i++)
221 MSVCRT_mbctype[i + 1] |= _M1;
225 if (cpi.MaxCharSize > 1)
227 /* trail bytes not available through kernel32 but stored in a structure in msvcrt */
228 struct cp_extra_info_t *cpextra = g_cpextrainfo;
230 g_mbcp_is_multibyte = 1;
233 if (cpextra->cp == 0 || cpextra->cp == newcp)
235 if (cpextra->cp == 0)
236 FIXME("trail bytes data not available for DBCS codepage %d - assuming all bytes\n", newcp);
238 bytes = cpextra->TrailBytes;
239 while (bytes[0] || bytes[1])
241 for (i = bytes[0]; i <= bytes[1]; i++)
242 MSVCRT_mbctype[i + 1] |= _M2;
251 g_mbcp_is_multibyte = 0;
253 /* we can't use GetStringTypeA directly because we don't have a locale - only a code page
256 for (i = 0; i < 256; i++)
257 if (!(MSVCRT_mbctype[i + 1] & _M1))
258 bufA[charcount++] = i;
260 ret = MultiByteToWideChar(newcp, 0, bufA, charcount, bufW, charcount);
261 if (ret != charcount)
262 ERR("MultiByteToWideChar of chars failed for cp %d, ret=%d (exp %d), error=%d\n", newcp, ret, charcount, GetLastError());
264 GetStringTypeW(CT_CTYPE1, bufW, charcount, chartypes);
266 curr_type = chartypes;
267 for (i = 0; i < 256; i++)
268 if (!(MSVCRT_mbctype[i + 1] & _M1))
270 if ((*curr_type) & C1_UPPER)
271 MSVCRT_mbctype[i + 1] |= _SBUP;
272 if ((*curr_type) & C1_LOWER)
273 MSVCRT_mbctype[i + 1] |= _SBLOW;
277 if (newcp == 932) /* CP932 only - set _MP and _MS */
279 /* On Windows it's possible to calculate the _MP and _MS from CT_CTYPE1
280 * and CT_CTYPE3. But as of Wine 0.9.43 we return wrong values what makes
281 * it hard. As this is set only for codepage 932 we hardcode it what gives
282 * also faster execution.
284 for (i = 161; i <= 165; i++)
285 MSVCRT_mbctype[i + 1] |= _MP;
286 for (i = 166; i <= 223; i++)
287 MSVCRT_mbctype[i + 1] |= _MS;
290 MSVCRT___lc_collate_cp = MSVCRT___lc_codepage = newcp;
291 TRACE("(%d) -> %d\n", cp, MSVCRT___lc_codepage);
295 /*********************************************************************
296 * _getmbcp (MSVCRT.@)
298 int CDECL _getmbcp(void)
300 return MSVCRT___lc_codepage;
303 /*********************************************************************
304 * _mbsnextc(MSVCRT.@)
306 unsigned int CDECL _mbsnextc(const unsigned char* str)
309 return *str << 8 | str[1];
313 /*********************************************************************
314 * _mbctolower(MSVCRT.@)
316 unsigned int CDECL _mbctolower(unsigned int c)
318 if (MSVCRT_isleadbyte(c))
320 FIXME("Handle MBC chars\n");
323 return tolower(c); /* ASCII CP or SB char */
326 /*********************************************************************
327 * _mbctoupper(MSVCRT.@)
329 unsigned int CDECL _mbctoupper(unsigned int c)
331 if (MSVCRT_isleadbyte(c))
333 FIXME("Handle MBC chars\n");
336 return toupper(c); /* ASCII CP or SB char */
339 /*********************************************************************
340 * _mbctombb (MSVCRT.@)
342 unsigned int CDECL _mbctombb(unsigned int c)
346 if(MSVCRT___lc_codepage == 932)
348 if(c >= 0x829f && c <= 0x82f1) /* Hiragana */
349 return mbctombb_932_kana[c - 0x829f];
350 if(c >= 0x8340 && c <= 0x8396 && c != 0x837f) /* Katakana */
351 return mbctombb_932_kana[c - 0x8340 - (c >= 0x837f ? 1 : 0)];
352 if(c >= 0x8140 && c <= 0x8197) /* Punctuation */
354 value = mbctombb_932_punct[c - 0x8140];
355 return value ? value : c;
357 if((c >= 0x824f && c <= 0x8258) || /* Fullwidth digits */
358 (c >= 0x8260 && c <= 0x8279)) /* Fullwidth capitals letters */
360 if(c >= 0x8281 && c <= 0x829a) /* Fullwidth small letters */
362 /* all other cases return c */
367 /*********************************************************************
368 * _mbcjistojms(MSVCRT.@)
370 * Converts a jis character to sjis.
371 * Based on description from
372 * http://www.slayers.ne.jp/~oouchi/code/jistosjis.html
374 unsigned int CDECL _mbcjistojms(unsigned int c)
376 /* Conversion takes place only when codepage is 932.
377 In all other cases, c is returned unchanged */
378 if(MSVCRT___lc_codepage == 932)
380 if(HIBYTE(c) >= 0x21 && HIBYTE(c) <= 0x7e &&
381 LOBYTE(c) >= 0x21 && LOBYTE(c) <= 0x7e)
391 c = (((HIBYTE(c) - 0x21)/2 + 0x81) << 8) | LOBYTE(c);
397 return 0; /* Codepage is 932, but c can't be converted */
403 /*********************************************************************
406 unsigned char* CDECL _mbsdec(const unsigned char* start, const unsigned char* cur)
408 if(MSVCRT___mb_cur_max > 1)
409 return (unsigned char *)(_ismbstrail(start,cur-1) ? cur - 2 : cur -1);
411 return (unsigned char *)cur - 1; /* ASCII CP or SB char */
414 /*********************************************************************
417 unsigned int CDECL _mbclen(const unsigned char* str)
419 return _ismbblead(*str) ? 2 : 1;
422 /*********************************************************************
425 unsigned char* CDECL _mbsinc(const unsigned char* str)
427 return (unsigned char *)(str + _mbclen(str));
430 /*********************************************************************
433 unsigned char* CDECL _mbsninc(const unsigned char* str, MSVCRT_size_t num)
438 while (num > 0 && *str)
440 if (_ismbblead(*str))
450 return (unsigned char*)str;
453 /*********************************************************************
456 MSVCRT_size_t CDECL _mbslen(const unsigned char* str)
458 MSVCRT_size_t len = 0;
461 if (_ismbblead(*str))
464 if (!*str) /* count only full chars */
473 /*********************************************************************
476 void CDECL _mbccpy(unsigned char* dest, const unsigned char* src)
480 *++dest = *++src; /* MB char */
483 /*********************************************************************
486 * The parameter n is the number or characters to copy, not the size of
487 * the buffer. Use _mbsnbcpy for a function analogical to strncpy
489 unsigned char* CDECL _mbsncpy(unsigned char* dst, const unsigned char* src, MSVCRT_size_t n)
491 unsigned char* ret = dst;
494 if (g_mbcp_is_multibyte)
499 if (_ismbblead(*src))
519 if (!(*dst++ = *src++)) break;
522 while (n--) *dst++ = 0;
526 /*********************************************************************
527 * _mbsnbcpy_s(MSVCRT.@)
529 * Unlike _mbsnbcpy this function does not pad the rest of the dest
532 int CDECL _mbsnbcpy_s(unsigned char* dst, MSVCRT_size_t size, const unsigned char* src, MSVCRT_size_t n)
534 MSVCRT_size_t pos = 0;
536 if(!dst || size == 0)
537 return MSVCRT_EINVAL;
541 return MSVCRT_EINVAL;
546 if(g_mbcp_is_multibyte)
554 return MSVCRT_ERANGE;
556 is_lead = (!is_lead && _ismbblead(*src));
561 if (is_lead) /* if string ends with a lead, remove it */
572 return MSVCRT_ERANGE;
585 return MSVCRT_ERANGE;
591 /*********************************************************************
592 * _mbsnbcpy(MSVCRT.@)
594 * Like strncpy this function doesn't enforce the string to be
597 unsigned char* CDECL _mbsnbcpy(unsigned char* dst, const unsigned char* src, MSVCRT_size_t n)
599 unsigned char* ret = dst;
602 if(g_mbcp_is_multibyte)
607 is_lead = (!is_lead && _ismbblead(*src));
612 if (is_lead) /* if string ends with a lead, remove it */
620 if (!(*dst++ = *src++)) break;
623 while (n--) *dst++ = 0;
627 /*********************************************************************
630 int CDECL _mbscmp(const unsigned char* str, const unsigned char* cmp)
632 if(MSVCRT___mb_cur_max > 1)
634 unsigned int strc, cmpc;
637 return *cmp ? -1 : 0;
640 strc = _mbsnextc(str);
641 cmpc = _mbsnextc(cmp);
643 return strc < cmpc ? -1 : 1;
644 str +=(strc > 255) ? 2 : 1;
645 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
648 return u_strcmp(str, cmp); /* ASCII CP */
651 /*********************************************************************
652 * _mbsicoll(MSVCRT.@)
653 * FIXME: handle locales.
655 int CDECL _mbsicoll(const unsigned char* str, const unsigned char* cmp)
657 if(MSVCRT___mb_cur_max > 1)
659 unsigned int strc, cmpc;
662 return *cmp ? -1 : 0;
665 strc = _mbctolower(_mbsnextc(str));
666 cmpc = _mbctolower(_mbsnextc(cmp));
668 return strc < cmpc ? -1 : 1;
669 str +=(strc > 255) ? 2 : 1;
670 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
673 return u_strcasecmp(str, cmp); /* ASCII CP */
676 /*********************************************************************
678 * Performs a case-sensitive comparison according to the current code page
680 * _NLSCMPERROR if error
681 * FIXME: handle locales.
683 int CDECL _mbscoll(const unsigned char* str, const unsigned char* cmp)
685 if(MSVCRT___mb_cur_max > 1)
687 unsigned int strc, cmpc;
690 return *cmp ? -1 : 0;
693 strc = _mbsnextc(str);
694 cmpc = _mbsnextc(cmp);
696 return strc < cmpc ? -1 : 1;
697 str +=(strc > 255) ? 2 : 1;
698 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
701 return u_strcmp(str, cmp); /* ASCII CP */
705 /*********************************************************************
708 int CDECL _mbsicmp(const unsigned char* str, const unsigned char* cmp)
710 if(MSVCRT___mb_cur_max > 1)
712 unsigned int strc, cmpc;
715 return *cmp ? -1 : 0;
718 strc = _mbctolower(_mbsnextc(str));
719 cmpc = _mbctolower(_mbsnextc(cmp));
721 return strc < cmpc ? -1 : 1;
722 str +=(strc > 255) ? 2 : 1;
723 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
726 return u_strcasecmp(str, cmp); /* ASCII CP */
729 /*********************************************************************
732 int CDECL _mbsncmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
737 if(MSVCRT___mb_cur_max > 1)
739 unsigned int strc, cmpc;
744 return *cmp ? -1 : 0;
747 strc = _mbsnextc(str);
748 cmpc = _mbsnextc(cmp);
750 return strc < cmpc ? -1 : 1;
751 inc=(strc > 255) ? 2 : 1; /* Equal, use same increment */
755 return 0; /* Matched len chars */
757 return u_strncmp(str, cmp, len); /* ASCII CP */
760 /*********************************************************************
761 * _mbsnbcmp(MSVCRT.@)
763 int CDECL _mbsnbcmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
767 if(MSVCRT___mb_cur_max > 1)
769 unsigned int strc, cmpc;
774 return *cmp ? -1 : 0;
777 if (MSVCRT_isleadbyte(*str))
779 strc=(len>=2)?_mbsnextc(str):0;
787 if (MSVCRT_isleadbyte(*cmp))
788 cmpc=(len>=2)?_mbsnextc(cmp):0;
792 return strc < cmpc ? -1 : 1;
797 return 0; /* Matched len chars */
799 return u_strncmp(str,cmp,len);
802 /*********************************************************************
803 * _mbsnicmp(MSVCRT.@)
805 * Compare two multibyte strings case insensitively to 'len' characters.
807 int CDECL _mbsnicmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
809 /* FIXME: No tolower() for mb strings yet */
810 if(MSVCRT___mb_cur_max > 1)
812 unsigned int strc, cmpc;
816 return *cmp ? -1 : 0;
819 strc = _mbctolower(_mbsnextc(str));
820 cmpc = _mbctolower(_mbsnextc(cmp));
822 return strc < cmpc ? -1 : 1;
823 str +=(strc > 255) ? 2 : 1;
824 cmp +=(strc > 255) ? 2 : 1; /* Equal, use same increment */
826 return 0; /* Matched len chars */
828 return u_strncasecmp(str, cmp, len); /* ASCII CP */
831 /*********************************************************************
832 * _mbsnbicmp(MSVCRT.@)
834 int CDECL _mbsnbicmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
838 if(MSVCRT___mb_cur_max > 1)
840 unsigned int strc, cmpc;
845 return *cmp ? -1 : 0;
848 if (MSVCRT_isleadbyte(*str))
850 strc=(len>=2)?_mbsnextc(str):0;
858 if (MSVCRT_isleadbyte(*cmp))
859 cmpc=(len>=2)?_mbsnextc(cmp):0;
862 strc = _mbctolower(strc);
863 cmpc = _mbctolower(cmpc);
865 return strc < cmpc ? -1 : 1;
870 return 0; /* Matched len bytes */
872 return u_strncasecmp(str,cmp,len);
875 /*********************************************************************
878 unsigned char * CDECL _mbscat( unsigned char *dst, const unsigned char *src )
880 strcat( (char *)dst, (const char *)src );
884 /*********************************************************************
887 unsigned char* CDECL _mbscpy( unsigned char *dst, const unsigned char *src )
889 strcpy( (char *)dst, (const char *)src );
893 /*********************************************************************
896 unsigned char * CDECL _mbsstr(const unsigned char *haystack, const unsigned char *needle)
898 return (unsigned char *)strstr( (const char *)haystack, (const char *)needle );
901 /*********************************************************************
904 * Find a multibyte character in a multibyte string.
906 unsigned char* CDECL _mbschr(const unsigned char* s, unsigned int x)
908 if(MSVCRT___mb_cur_max > 1)
915 return (unsigned char*)s;
918 s += c > 255 ? 2 : 1;
921 return u_strchr(s, x); /* ASCII CP */
924 /*********************************************************************
927 unsigned char* CDECL _mbsrchr(const unsigned char* s, unsigned int x)
929 if(MSVCRT___mb_cur_max > 1)
932 unsigned char* match=NULL;
938 match=(unsigned char*)s;
941 s +=(c > 255) ? 2 : 1;
944 return u_strrchr(s, x);
947 /*********************************************************************
950 * Find and extract tokens from strings
952 unsigned char* CDECL _mbstok(unsigned char *str, const unsigned char *delim)
954 thread_data_t *data = msvcrt_get_thread_data();
957 if(MSVCRT___mb_cur_max > 1)
962 if (!(str = data->mbstok_next)) return NULL;
964 while ((c = _mbsnextc(str)) && _mbschr(delim, c)) {
965 str += c > 255 ? 2 : 1;
967 if (!*str) return NULL;
969 while ((c = _mbsnextc(str)) && !_mbschr(delim, c)) {
970 str += c > 255 ? 2 : 1;
974 if (c > 255) *str++ = 0;
976 data->mbstok_next = str;
979 return u_strtok(str, delim); /* ASCII CP */
982 /*********************************************************************
985 int CDECL MSVCRT_mbtowc(MSVCRT_wchar_t *dst, const char* str, MSVCRT_size_t n)
987 /* temp var needed because MultiByteToWideChar wants non NULL destination */
988 MSVCRT_wchar_t tmpdst = '\0';
992 if(!MultiByteToWideChar(CP_ACP, 0, str, n, &tmpdst, 1))
996 /* return the number of bytes from src that have been used */
999 if(n >= 2 && MSVCRT_isleadbyte(*str) && str[1])
1004 /*********************************************************************
1005 * _mbbtombc(MSVCRT.@)
1007 unsigned int CDECL _mbbtombc(unsigned int c)
1009 if(MSVCRT___mb_cur_max > 1 &&
1010 ((c >= 0x20 && c <=0x7e) ||(c >= 0xa1 && c <= 0xdf)))
1012 /* FIXME: I can't get this function to return anything
1013 * different from what I pass it...
1016 return c; /* ASCII CP or no MB char */
1019 /*********************************************************************
1020 * _mbbtype(MSVCRT.@)
1022 int CDECL _mbbtype(unsigned char c, int type)
1026 if ((c >= 0x20 && c <= 0x7e) || (c >= 0xa1 && c <= 0xdf))
1028 else if ((c >= 0x40 && c <= 0x7e) || (c >= 0x80 && c <= 0xfc))
1031 return _MBC_ILLEGAL;
1035 if ((c >= 0x20 && c <= 0x7e) || (c >= 0xa1 && c <= 0xdf))
1037 else if ((c >= 0x81 && c <= 0x9f) || (c >= 0xe0 && c <= 0xfc))
1040 return _MBC_ILLEGAL;
1044 /*********************************************************************
1045 * _ismbbkana(MSVCRT.@)
1047 int CDECL _ismbbkana(unsigned int c)
1049 /* FIXME: use lc_ctype when supported, not lc_all */
1050 if(MSVCRT___lc_codepage == 932)
1052 /* Japanese/Katakana, CP 932 */
1053 return (c >= 0xa1 && c <= 0xdf);
1058 /*********************************************************************
1059 * _ismbcdigit(MSVCRT.@)
1061 int CDECL _ismbcdigit(unsigned int ch)
1063 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1064 return (get_char_typeW( wch ) & C1_DIGIT);
1067 /*********************************************************************
1068 * _ismbcgraph(MSVCRT.@)
1070 int CDECL _ismbcgraph(unsigned int ch)
1072 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1073 return (get_char_typeW( wch ) & (C1_UPPER | C1_LOWER | C1_DIGIT | C1_PUNCT | C1_ALPHA));
1076 /*********************************************************************
1077 * _ismbcalpha (MSVCRT.@)
1079 int CDECL _ismbcalpha(unsigned int ch)
1081 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1082 return (get_char_typeW( wch ) & C1_ALPHA);
1085 /*********************************************************************
1086 * _ismbclower (MSVCRT.@)
1088 int CDECL _ismbclower(unsigned int ch)
1090 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1091 return (get_char_typeW( wch ) & C1_UPPER);
1094 /*********************************************************************
1095 * _ismbcupper (MSVCRT.@)
1097 int CDECL _ismbcupper(unsigned int ch)
1099 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1100 return (get_char_typeW( wch ) & C1_LOWER);
1103 /*********************************************************************
1104 * _ismbcsymbol(MSVCRT.@)
1106 int CDECL _ismbcsymbol(unsigned int ch)
1108 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1110 if (!GetStringTypeW(CT_CTYPE3, &wch, 1, &ctype))
1112 WARN("GetStringTypeW failed on %x\n", ch);
1115 return ((ctype & C3_SYMBOL) != 0);
1118 /*********************************************************************
1119 * _ismbcalnum (MSVCRT.@)
1121 int CDECL _ismbcalnum(unsigned int ch)
1123 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1124 return (get_char_typeW( wch ) & (C1_ALPHA | C1_DIGIT));
1127 /*********************************************************************
1128 * _ismbcspace (MSVCRT.@)
1130 int CDECL _ismbcspace(unsigned int ch)
1132 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1133 return (get_char_typeW( wch ) & C1_SPACE);
1136 /*********************************************************************
1137 * _ismbcprint (MSVCRT.@)
1139 int CDECL _ismbcprint(unsigned int ch)
1141 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1142 return (get_char_typeW( wch ) & (C1_UPPER | C1_LOWER | C1_DIGIT | C1_PUNCT | C1_ALPHA | C1_SPACE));
1145 /*********************************************************************
1146 * _ismbcpunct(MSVCRT.@)
1148 int CDECL _ismbcpunct(unsigned int ch)
1150 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1151 return (get_char_typeW( wch ) & C1_PUNCT);
1154 /*********************************************************************
1155 * _ismbchira(MSVCRT.@)
1157 int CDECL _ismbchira(unsigned int c)
1159 /* FIXME: use lc_ctype when supported, not lc_all */
1160 if(MSVCRT___lc_codepage == 932)
1162 /* Japanese/Hiragana, CP 932 */
1163 return (c >= 0x829f && c <= 0x82f1);
1168 /*********************************************************************
1169 * _ismbckata(MSVCRT.@)
1171 int CDECL _ismbckata(unsigned int c)
1173 /* FIXME: use lc_ctype when supported, not lc_all */
1174 if(MSVCRT___lc_codepage == 932)
1177 return _ismbbkana(c);
1178 /* Japanese/Katakana, CP 932 */
1179 return (c >= 0x8340 && c <= 0x8396 && c != 0x837f);
1184 /*********************************************************************
1185 * _ismbblead(MSVCRT.@)
1187 int CDECL _ismbblead(unsigned int c)
1189 return (MSVCRT_mbctype[(c&0xff) + 1] & _M1) != 0;
1193 /*********************************************************************
1194 * _ismbbtrail(MSVCRT.@)
1196 int CDECL _ismbbtrail(unsigned int c)
1198 return (MSVCRT_mbctype[(c&0xff) + 1] & _M2) != 0;
1201 /*********************************************************************
1202 * _ismbclegal(MSVCRT.@)
1204 int CDECL _ismbclegal(unsigned int c)
1206 return _ismbblead(HIBYTE(c)) && _ismbbtrail(LOBYTE(c));
1209 /*********************************************************************
1210 * _ismbslead(MSVCRT.@)
1212 int CDECL _ismbslead(const unsigned char* start, const unsigned char* str)
1216 if(!g_mbcp_is_multibyte)
1219 /* Lead bytes can also be trail bytes so we need to analyse the string
1221 while (start <= str)
1225 lead = !lead && _ismbblead(*start);
1229 return lead ? -1 : 0;
1232 /*********************************************************************
1233 * _ismbstrail(MSVCRT.@)
1235 int CDECL _ismbstrail(const unsigned char* start, const unsigned char* str)
1237 /* Note: this function doesn't check _ismbbtrail */
1238 if ((str > start) && _ismbslead(start, str-1))
1244 /*********************************************************************
1245 * _mbsbtype (MSVCRT.@)
1247 int CDECL _mbsbtype(const unsigned char *str, MSVCRT_size_t count)
1250 const unsigned char *end = str + count;
1251 int mbcp = g_mbcp_is_multibyte;
1253 /* Lead bytes can also be trail bytes so we need to analyse the string.
1254 * Also we must return _MBC_ILLEGAL for chars past the end of the string
1256 while (str < end) /* Note: we skip the last byte - will check after the loop */
1259 return _MBC_ILLEGAL;
1260 lead = mbcp && !lead && _ismbblead(*str);
1265 if (_ismbbtrail(*str))
1268 return _MBC_ILLEGAL;
1270 if (_ismbblead(*str))
1276 /*********************************************************************
1279 unsigned char* CDECL _mbsset(unsigned char* str, unsigned int c)
1281 unsigned char* ret = str;
1283 if(MSVCRT___mb_cur_max == 1 || c < 256)
1284 return u__strset(str, c); /* ASCII CP or SB char */
1286 c &= 0xffff; /* Strip high bits */
1288 while(str[0] && str[1])
1294 str[0] = '\0'; /* FIXME: OK to shorten? */
1299 /*********************************************************************
1300 * _mbsnbset(MSVCRT.@)
1302 unsigned char* CDECL _mbsnbset(unsigned char *str, unsigned int c, MSVCRT_size_t len)
1304 unsigned char *ret = str;
1309 if(MSVCRT___mb_cur_max == 1 || c < 256)
1310 return u__strnset(str, c, len); /* ASCII CP or SB char */
1312 c &= 0xffff; /* Strip high bits */
1314 while(str[0] && str[1] && (len > 1))
1322 /* as per msdn pad with a blank character */
1329 /*********************************************************************
1330 * _mbsnset(MSVCRT.@)
1332 unsigned char* CDECL _mbsnset(unsigned char* str, unsigned int c, MSVCRT_size_t len)
1334 unsigned char *ret = str;
1339 if(MSVCRT___mb_cur_max == 1 || c < 256)
1340 return u__strnset(str, c, len); /* ASCII CP or SB char */
1342 c &= 0xffff; /* Strip high bits */
1344 while(str[0] && str[1] && len--)
1350 str[0] = '\0'; /* FIXME: OK to shorten? */
1355 /*********************************************************************
1356 * _mbsnccnt(MSVCRT.@)
1357 * 'c' is for 'character'.
1359 MSVCRT_size_t CDECL _mbsnccnt(const unsigned char* str, MSVCRT_size_t len)
1362 if(MSVCRT___mb_cur_max > 1)
1365 while(*str && len-- > 0)
1367 if(MSVCRT_isleadbyte(*str))
1380 return min(ret, len); /* ASCII CP */
1383 /*********************************************************************
1384 * _mbsnbcnt(MSVCRT.@)
1385 * 'b' is for byte count.
1387 MSVCRT_size_t CDECL _mbsnbcnt(const unsigned char* str, MSVCRT_size_t len)
1390 if(MSVCRT___mb_cur_max > 1)
1392 const unsigned char* xstr = str;
1393 while(*xstr && len-- > 0)
1395 if (MSVCRT_isleadbyte(*xstr++))
1401 return min(ret, len); /* ASCII CP */
1404 /*********************************************************************
1405 * _mbsnbcat(MSVCRT.@)
1407 unsigned char* CDECL _mbsnbcat(unsigned char* dst, const unsigned char* src, MSVCRT_size_t len)
1409 if(MSVCRT___mb_cur_max > 1)
1411 unsigned char *res = dst;
1413 if (MSVCRT_isleadbyte(*dst++)) {
1417 /* as per msdn overwrite the lead byte in front of '\0' */
1423 while (*src && len--) *dst++ = *src++;
1427 return u_strncat(dst, src, len); /* ASCII CP */
1431 /*********************************************************************
1432 * _mbsncat(MSVCRT.@)
1434 unsigned char* CDECL _mbsncat(unsigned char* dst, const unsigned char* src, MSVCRT_size_t len)
1436 if(MSVCRT___mb_cur_max > 1)
1438 unsigned char *res = dst;
1441 if (MSVCRT_isleadbyte(*dst++))
1444 while (*src && len--)
1447 if(MSVCRT_isleadbyte(*src++))
1453 return u_strncat(dst, src, len); /* ASCII CP */
1457 /*********************************************************************
1460 unsigned char* CDECL _mbslwr(unsigned char* s)
1462 unsigned char *ret = s;
1465 if (MSVCRT___mb_cur_max > 1)
1470 c = _mbctolower(_mbsnextc(s));
1471 /* Note that I assume that the size of the character is unchanged */
1480 else for ( ; *s; s++) *s = tolower(*s);
1485 /*********************************************************************
1488 unsigned char* CDECL _mbsupr(unsigned char* s)
1490 unsigned char *ret = s;
1493 if (MSVCRT___mb_cur_max > 1)
1498 c = _mbctoupper(_mbsnextc(s));
1499 /* Note that I assume that the size of the character is unchanged */
1508 else for ( ; *s; s++) *s = toupper(*s);
1513 /*********************************************************************
1514 * _mbsspn (MSVCRT.@)
1516 MSVCRT_size_t CDECL _mbsspn(const unsigned char* string, const unsigned char* set)
1518 const unsigned char *p, *q;
1520 for (p = string; *p; p++)
1522 if (MSVCRT_isleadbyte(*p))
1524 for (q = set; *q; q++)
1528 if ((*p == *q) && (p[1] == q[1]))
1532 if (!q[0] || !q[1]) break;
1536 for (q = set; *q; q++)
1545 /*********************************************************************
1546 * _mbsspnp (MSVCRT.@)
1548 unsigned char* CDECL _mbsspnp(const unsigned char* string, const unsigned char* set)
1550 const unsigned char *p, *q;
1552 for (p = string; *p; p++)
1554 if (MSVCRT_isleadbyte(*p))
1556 for (q = set; *q; q++)
1560 if ((*p == *q) && (p[1] == q[1]))
1564 if (!q[0] || !q[1]) break;
1568 for (q = set; *q; q++)
1576 return (unsigned char *)p;
1579 /*********************************************************************
1580 * _mbscspn(MSVCRT.@)
1582 MSVCRT_size_t CDECL _mbscspn(const unsigned char* str, const unsigned char* cmp)
1584 if (MSVCRT___mb_cur_max > 1)
1585 FIXME("don't handle double character case\n");
1586 return u_strcspn(str, cmp);
1589 /*********************************************************************
1590 * _mbsrev (MSVCRT.@)
1592 unsigned char* CDECL _mbsrev(unsigned char* str)
1594 int i, len = _mbslen(str);
1595 unsigned char *p, *temp=MSVCRT_malloc(len*2);
1600 /* unpack multibyte string to temp buffer */
1602 for(i=0; i<len; i++)
1604 if (MSVCRT_isleadbyte(*p))
1616 /* repack it in the reverse order */
1618 for(i=len-1; i>=0; i--)
1620 if(MSVCRT_isleadbyte(temp[i*2]))
1636 /*********************************************************************
1637 * _mbspbrk (MSVCRT.@)
1639 unsigned char* CDECL _mbspbrk(const unsigned char* str, const unsigned char* accept)
1641 const unsigned char* p;
1645 for(p = accept; *p; p += (MSVCRT_isleadbyte(*p)?2:1) )
1648 if( !MSVCRT_isleadbyte(*p) || ( *(p+1) == *(str+1) ) )
1649 return (unsigned char*)str;
1651 str += (MSVCRT_isleadbyte(*str)?2:1);
1658 * Functions depending on locale codepage
1661 /*********************************************************************
1664 * Unlike most of the multibyte string functions this function uses
1665 * the locale codepage, not the codepage set by _setmbcp
1667 int CDECL MSVCRT_mblen(const char* str, MSVCRT_size_t size)
1669 if (str && *str && size)
1671 if(MSVCRT___mb_cur_max == 1)
1672 return 1; /* ASCII CP */
1674 return !MSVCRT_isleadbyte(*str) ? 1 : (size>1 ? 2 : -1);
1679 /*********************************************************************
1680 * _mbstrlen(MSVCRT.@)
1682 * Unlike most of the multibyte string functions this function uses
1683 * the locale codepage, not the codepage set by _setmbcp
1685 MSVCRT_size_t CDECL _mbstrlen(const char* str)
1687 if(MSVCRT___mb_cur_max > 1)
1689 MSVCRT_size_t len = 0;
1692 /* FIXME: According to the documentation we are supposed to test for
1693 * multi-byte character validity. Whatever that means
1695 str += MSVCRT_isleadbyte(*str) ? 2 : 1;
1700 return strlen(str); /* ASCII CP */