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 static MSVCRT_wchar_t msvcrt_mbc_to_wc(unsigned int ch)
66 mbch[0] = (ch >> 8) & 0xff;
70 if (!MultiByteToWideChar(MSVCRT___lc_codepage, 0, mbch, n_chars, &chW, 1))
72 WARN("MultiByteToWideChar failed on %x\n", ch);
78 static inline MSVCRT_size_t u_strlen( const unsigned char *str )
80 return strlen( (const char*) str );
83 static inline unsigned char* u_strncat( unsigned char* dst, const unsigned char* src, MSVCRT_size_t len )
85 return (unsigned char*)strncat( (char*)dst, (const char*)src, len);
88 static inline int u_strcmp( const unsigned char *s1, const unsigned char *s2 )
90 return strcmp( (const char*)s1, (const char*)s2 );
93 static inline int u_strcasecmp( const unsigned char *s1, const unsigned char *s2 )
95 return strcasecmp( (const char*)s1, (const char*)s2 );
98 static inline int u_strncmp( const unsigned char *s1, const unsigned char *s2, MSVCRT_size_t len )
100 return strncmp( (const char*)s1, (const char*)s2, len );
103 static inline int u_strncasecmp( const unsigned char *s1, const unsigned char *s2, MSVCRT_size_t len )
105 return strncasecmp( (const char*)s1, (const char*)s2, len );
108 static inline unsigned char *u_strchr( const unsigned char *s, unsigned char x )
110 return (unsigned char*) strchr( (const char*)s, x );
113 static inline unsigned char *u_strrchr( const unsigned char *s, unsigned char x )
115 return (unsigned char*) strrchr( (const char*)s, x );
118 static inline unsigned char *u_strtok( unsigned char *s, const unsigned char *delim )
120 return (unsigned char*) strtok( (char*)s, (const char*)delim );
123 static inline unsigned char *u__strset( unsigned char *s, unsigned char c )
125 return (unsigned char*) _strset( (char*)s, c);
128 static inline unsigned char *u__strnset( unsigned char *s, unsigned char c, MSVCRT_size_t len )
130 return (unsigned char*) _strnset( (char*)s, c, len );
133 static inline MSVCRT_size_t u_strcspn( const unsigned char *s, const unsigned char *rej )
135 return strcspn( (const char *)s, (const char*)rej );
138 /*********************************************************************
139 * __p__mbctype (MSVCRT.@)
141 unsigned char* CDECL __p__mbctype(void)
143 return MSVCRT_mbctype;
146 /*********************************************************************
147 * ___mb_cur_max_func(MSVCRT.@)
149 int* CDECL MSVCRT____mb_cur_max_func(void)
151 return &MSVCRT___mb_cur_max;
154 /*********************************************************************
155 * _setmbcp (MSVCRT.@)
157 int CDECL _setmbcp(int cp)
179 newcp = MSVCRT___lc_codepage;
182 newcp = 20127; /* ASCII */
189 if (!GetCPInfo(newcp, &cpi))
191 WARN("Codepage %d not found\n", newcp);
192 *MSVCRT__errno() = MSVCRT_EINVAL;
196 /* setup the _mbctype */
197 memset(MSVCRT_mbctype, 0, sizeof(MSVCRT_mbctype));
199 bytes = cpi.LeadByte;
200 while (bytes[0] || bytes[1])
202 for (i = bytes[0]; i <= bytes[1]; i++)
203 MSVCRT_mbctype[i + 1] |= _M1;
207 if (cpi.MaxCharSize > 1)
209 /* trail bytes not available through kernel32 but stored in a structure in msvcrt */
210 struct cp_extra_info_t *cpextra = g_cpextrainfo;
212 g_mbcp_is_multibyte = 1;
215 if (cpextra->cp == 0 || cpextra->cp == newcp)
217 if (cpextra->cp == 0)
218 FIXME("trail bytes data not available for DBCS codepage %d - assuming all bytes\n", newcp);
220 bytes = cpextra->TrailBytes;
221 while (bytes[0] || bytes[1])
223 for (i = bytes[0]; i <= bytes[1]; i++)
224 MSVCRT_mbctype[i + 1] |= _M2;
233 g_mbcp_is_multibyte = 0;
235 /* we can't use GetStringTypeA directly because we don't have a locale - only a code page
238 for (i = 0; i < 256; i++)
239 if (!(MSVCRT_mbctype[i + 1] & _M1))
240 bufA[charcount++] = i;
242 ret = MultiByteToWideChar(newcp, 0, bufA, charcount, bufW, charcount);
243 if (ret != charcount)
244 ERR("MultiByteToWideChar of chars failed for cp %d, ret=%d (exp %d), error=%d\n", newcp, ret, charcount, GetLastError());
246 GetStringTypeW(CT_CTYPE1, bufW, charcount, chartypes);
248 curr_type = chartypes;
249 for (i = 0; i < 256; i++)
250 if (!(MSVCRT_mbctype[i + 1] & _M1))
252 if ((*curr_type) & C1_UPPER)
253 MSVCRT_mbctype[i + 1] |= _SBUP;
254 if ((*curr_type) & C1_LOWER)
255 MSVCRT_mbctype[i + 1] |= _SBLOW;
259 if (newcp == 932) /* CP932 only - set _MP and _MS */
261 /* On Windows it's possible to calculate the _MP and _MS from CT_CTYPE1
262 * and CT_CTYPE3. But as of Wine 0.9.43 we return wrong values what makes
263 * it hard. As this is set only for codepage 932 we hardcode it what gives
264 * also faster execution.
266 for (i = 161; i <= 165; i++)
267 MSVCRT_mbctype[i + 1] |= _MP;
268 for (i = 166; i <= 223; i++)
269 MSVCRT_mbctype[i + 1] |= _MS;
272 MSVCRT___lc_collate_cp = MSVCRT___lc_codepage = newcp;
273 TRACE("(%d) -> %d\n", cp, MSVCRT___lc_codepage);
277 /*********************************************************************
278 * _getmbcp (MSVCRT.@)
280 int CDECL _getmbcp(void)
282 return MSVCRT___lc_codepage;
285 /*********************************************************************
286 * _mbsnextc(MSVCRT.@)
288 unsigned int CDECL _mbsnextc(const unsigned char* str)
291 return *str << 8 | str[1];
295 /*********************************************************************
296 * _mbctolower(MSVCRT.@)
298 unsigned int CDECL _mbctolower(unsigned int c)
300 if (MSVCRT_isleadbyte(c))
302 FIXME("Handle MBC chars\n");
305 return tolower(c); /* ASCII CP or SB char */
308 /*********************************************************************
309 * _mbctoupper(MSVCRT.@)
311 unsigned int CDECL _mbctoupper(unsigned int c)
313 if (MSVCRT_isleadbyte(c))
315 FIXME("Handle MBC chars\n");
318 return toupper(c); /* ASCII CP or SB char */
321 /*********************************************************************
322 * _mbcjistojms(MSVCRT.@)
324 * Converts a jis character to sjis.
325 * Based on description from
326 * http://www.slayers.ne.jp/~oouchi/code/jistosjis.html
328 unsigned int CDECL _mbcjistojms(unsigned int c)
330 /* Conversion takes place only when codepage is 932.
331 In all other cases, c is returned unchanged */
332 if(MSVCRT___lc_codepage == 932)
334 if(HIBYTE(c) >= 0x21 && HIBYTE(c) <= 0x7e &&
335 LOBYTE(c) >= 0x21 && LOBYTE(c) <= 0x7e)
345 c = (((HIBYTE(c) - 0x21)/2 + 0x81) << 8) | LOBYTE(c);
351 return 0; /* Codepage is 932, but c can't be converted */
357 /*********************************************************************
360 unsigned char* CDECL _mbsdec(const unsigned char* start, const unsigned char* cur)
362 if(MSVCRT___mb_cur_max > 1)
363 return (unsigned char *)(_ismbstrail(start,cur-1) ? cur - 2 : cur -1);
365 return (unsigned char *)cur - 1; /* ASCII CP or SB char */
368 /*********************************************************************
371 unsigned int CDECL _mbclen(const unsigned char* str)
373 return _ismbblead(*str) ? 2 : 1;
376 /*********************************************************************
379 unsigned char* CDECL _mbsinc(const unsigned char* str)
381 return (unsigned char *)(str + _mbclen(str));
384 /*********************************************************************
387 unsigned char* CDECL _mbsninc(const unsigned char* str, MSVCRT_size_t num)
392 while (num > 0 && *str)
394 if (_ismbblead(*str))
404 return (unsigned char*)str;
407 /*********************************************************************
410 MSVCRT_size_t CDECL _mbslen(const unsigned char* str)
412 MSVCRT_size_t len = 0;
415 if (_ismbblead(*str))
418 if (!*str) /* count only full chars */
427 /*********************************************************************
430 void CDECL _mbccpy(unsigned char* dest, const unsigned char* src)
434 *++dest = *++src; /* MB char */
437 /*********************************************************************
440 * The parameter n is the number or characters to copy, not the size of
441 * the buffer. Use _mbsnbcpy for a function analogical to strncpy
443 unsigned char* CDECL _mbsncpy(unsigned char* dst, const unsigned char* src, MSVCRT_size_t n)
445 unsigned char* ret = dst;
448 if (g_mbcp_is_multibyte)
453 if (_ismbblead(*src))
473 if (!(*dst++ = *src++)) break;
476 while (n--) *dst++ = 0;
480 /*********************************************************************
481 * _mbsnbcpy_s(MSVCRT.@)
483 * Unlike _mbsnbcpy this function does not pad the rest of the dest
486 int CDECL _mbsnbcpy_s(unsigned char* dst, MSVCRT_size_t size, const unsigned char* src, MSVCRT_size_t n)
488 MSVCRT_size_t pos = 0;
490 if(!dst || size == 0)
491 return MSVCRT_EINVAL;
495 return MSVCRT_EINVAL;
500 if(g_mbcp_is_multibyte)
508 return MSVCRT_ERANGE;
510 is_lead = (!is_lead && _ismbblead(*src));
515 if (is_lead) /* if string ends with a lead, remove it */
526 return MSVCRT_ERANGE;
539 return MSVCRT_ERANGE;
545 /*********************************************************************
546 * _mbsnbcpy(MSVCRT.@)
548 * Like strncpy this function doesn't enforce the string to be
551 unsigned char* CDECL _mbsnbcpy(unsigned char* dst, const unsigned char* src, MSVCRT_size_t n)
553 unsigned char* ret = dst;
556 if(g_mbcp_is_multibyte)
561 is_lead = (!is_lead && _ismbblead(*src));
566 if (is_lead) /* if string ends with a lead, remove it */
574 if (!(*dst++ = *src++)) break;
577 while (n--) *dst++ = 0;
581 /*********************************************************************
584 int CDECL _mbscmp(const unsigned char* str, const unsigned char* cmp)
586 if(MSVCRT___mb_cur_max > 1)
588 unsigned int strc, cmpc;
591 return *cmp ? -1 : 0;
594 strc = _mbsnextc(str);
595 cmpc = _mbsnextc(cmp);
597 return strc < cmpc ? -1 : 1;
598 str +=(strc > 255) ? 2 : 1;
599 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
602 return u_strcmp(str, cmp); /* ASCII CP */
605 /*********************************************************************
606 * _mbsicoll(MSVCRT.@)
607 * FIXME: handle locales.
609 int CDECL _mbsicoll(const unsigned char* str, const unsigned char* cmp)
611 if(MSVCRT___mb_cur_max > 1)
613 unsigned int strc, cmpc;
616 return *cmp ? -1 : 0;
619 strc = _mbctolower(_mbsnextc(str));
620 cmpc = _mbctolower(_mbsnextc(cmp));
622 return strc < cmpc ? -1 : 1;
623 str +=(strc > 255) ? 2 : 1;
624 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
627 return u_strcasecmp(str, cmp); /* ASCII CP */
630 /*********************************************************************
632 * Performs a case-sensitive comparison according to the current code page
634 * _NLSCMPERROR if error
635 * FIXME: handle locales.
637 int CDECL _mbscoll(const unsigned char* str, const unsigned char* cmp)
639 if(MSVCRT___mb_cur_max > 1)
641 unsigned int strc, cmpc;
644 return *cmp ? -1 : 0;
647 strc = _mbsnextc(str);
648 cmpc = _mbsnextc(cmp);
650 return strc < cmpc ? -1 : 1;
651 str +=(strc > 255) ? 2 : 1;
652 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
655 return u_strcmp(str, cmp); /* ASCII CP */
659 /*********************************************************************
662 int CDECL _mbsicmp(const unsigned char* str, const unsigned char* cmp)
664 if(MSVCRT___mb_cur_max > 1)
666 unsigned int strc, cmpc;
669 return *cmp ? -1 : 0;
672 strc = _mbctolower(_mbsnextc(str));
673 cmpc = _mbctolower(_mbsnextc(cmp));
675 return strc < cmpc ? -1 : 1;
676 str +=(strc > 255) ? 2 : 1;
677 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
680 return u_strcasecmp(str, cmp); /* ASCII CP */
683 /*********************************************************************
686 int CDECL _mbsncmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
691 if(MSVCRT___mb_cur_max > 1)
693 unsigned int strc, cmpc;
698 return *cmp ? -1 : 0;
701 strc = _mbsnextc(str);
702 cmpc = _mbsnextc(cmp);
704 return strc < cmpc ? -1 : 1;
705 inc=(strc > 255) ? 2 : 1; /* Equal, use same increment */
709 return 0; /* Matched len chars */
711 return u_strncmp(str, cmp, len); /* ASCII CP */
714 /*********************************************************************
715 * _mbsnbcmp(MSVCRT.@)
717 int CDECL _mbsnbcmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
721 if(MSVCRT___mb_cur_max > 1)
723 unsigned int strc, cmpc;
728 return *cmp ? -1 : 0;
731 if (MSVCRT_isleadbyte(*str))
733 strc=(len>=2)?_mbsnextc(str):0;
741 if (MSVCRT_isleadbyte(*cmp))
742 cmpc=(len>=2)?_mbsnextc(cmp):0;
746 return strc < cmpc ? -1 : 1;
751 return 0; /* Matched len chars */
753 return u_strncmp(str,cmp,len);
756 /*********************************************************************
757 * _mbsnicmp(MSVCRT.@)
759 * Compare two multibyte strings case insensitively to 'len' characters.
761 int CDECL _mbsnicmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
763 /* FIXME: No tolower() for mb strings yet */
764 if(MSVCRT___mb_cur_max > 1)
766 unsigned int strc, cmpc;
770 return *cmp ? -1 : 0;
773 strc = _mbctolower(_mbsnextc(str));
774 cmpc = _mbctolower(_mbsnextc(cmp));
776 return strc < cmpc ? -1 : 1;
777 str +=(strc > 255) ? 2 : 1;
778 cmp +=(strc > 255) ? 2 : 1; /* Equal, use same increment */
780 return 0; /* Matched len chars */
782 return u_strncasecmp(str, cmp, len); /* ASCII CP */
785 /*********************************************************************
786 * _mbsnbicmp(MSVCRT.@)
788 int CDECL _mbsnbicmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
792 if(MSVCRT___mb_cur_max > 1)
794 unsigned int strc, cmpc;
799 return *cmp ? -1 : 0;
802 if (MSVCRT_isleadbyte(*str))
804 strc=(len>=2)?_mbsnextc(str):0;
812 if (MSVCRT_isleadbyte(*cmp))
813 cmpc=(len>=2)?_mbsnextc(cmp):0;
816 strc = _mbctolower(strc);
817 cmpc = _mbctolower(cmpc);
819 return strc < cmpc ? -1 : 1;
824 return 0; /* Matched len bytes */
826 return u_strncasecmp(str,cmp,len);
829 /*********************************************************************
832 unsigned char * CDECL _mbscat( unsigned char *dst, const unsigned char *src )
834 strcat( (char *)dst, (const char *)src );
838 /*********************************************************************
841 unsigned char* CDECL _mbscpy( unsigned char *dst, const unsigned char *src )
843 strcpy( (char *)dst, (const char *)src );
847 /*********************************************************************
850 unsigned char * CDECL _mbsstr(const unsigned char *haystack, const unsigned char *needle)
852 return (unsigned char *)strstr( (const char *)haystack, (const char *)needle );
855 /*********************************************************************
858 * Find a multibyte character in a multibyte string.
860 unsigned char* CDECL _mbschr(const unsigned char* s, unsigned int x)
862 if(MSVCRT___mb_cur_max > 1)
869 return (unsigned char*)s;
872 s += c > 255 ? 2 : 1;
875 return u_strchr(s, x); /* ASCII CP */
878 /*********************************************************************
881 unsigned char* CDECL _mbsrchr(const unsigned char* s, unsigned int x)
883 if(MSVCRT___mb_cur_max > 1)
886 unsigned char* match=NULL;
892 match=(unsigned char*)s;
895 s +=(c > 255) ? 2 : 1;
898 return u_strrchr(s, x);
901 /*********************************************************************
904 * Find and extract tokens from strings
906 unsigned char* CDECL _mbstok(unsigned char *str, const unsigned char *delim)
908 thread_data_t *data = msvcrt_get_thread_data();
911 if(MSVCRT___mb_cur_max > 1)
916 if (!(str = data->mbstok_next)) return NULL;
918 while ((c = _mbsnextc(str)) && _mbschr(delim, c)) {
919 str += c > 255 ? 2 : 1;
921 if (!*str) return NULL;
923 while ((c = _mbsnextc(str)) && !_mbschr(delim, c)) {
924 str += c > 255 ? 2 : 1;
928 if (c > 255) *str++ = 0;
930 data->mbstok_next = str;
933 return u_strtok(str, delim); /* ASCII CP */
936 /*********************************************************************
939 int CDECL MSVCRT_mbtowc(MSVCRT_wchar_t *dst, const char* str, MSVCRT_size_t n)
941 /* temp var needed because MultiByteToWideChar wants non NULL destination */
942 MSVCRT_wchar_t tmpdst = '\0';
946 if(!MultiByteToWideChar(CP_ACP, 0, str, n, &tmpdst, 1))
950 /* return the number of bytes from src that have been used */
953 if(n >= 2 && MSVCRT_isleadbyte(*str) && str[1])
958 /*********************************************************************
959 * _mbbtombc(MSVCRT.@)
961 unsigned int CDECL _mbbtombc(unsigned int c)
963 if(MSVCRT___mb_cur_max > 1 &&
964 ((c >= 0x20 && c <=0x7e) ||(c >= 0xa1 && c <= 0xdf)))
966 /* FIXME: I can't get this function to return anything
967 * different from what I pass it...
970 return c; /* ASCII CP or no MB char */
973 /*********************************************************************
976 int CDECL _mbbtype(unsigned char c, int type)
980 if ((c >= 0x20 && c <= 0x7e) || (c >= 0xa1 && c <= 0xdf))
982 else if ((c >= 0x40 && c <= 0x7e) || (c >= 0x80 && c <= 0xfc))
989 if ((c >= 0x20 && c <= 0x7e) || (c >= 0xa1 && c <= 0xdf))
991 else if ((c >= 0x81 && c <= 0x9f) || (c >= 0xe0 && c <= 0xfc))
998 /*********************************************************************
999 * _ismbbkana(MSVCRT.@)
1001 int CDECL _ismbbkana(unsigned int c)
1003 /* FIXME: use lc_ctype when supported, not lc_all */
1004 if(MSVCRT___lc_codepage == 932)
1006 /* Japanese/Katakana, CP 932 */
1007 return (c >= 0xa1 && c <= 0xdf);
1012 /*********************************************************************
1013 * _ismbcdigit(MSVCRT.@)
1015 int CDECL _ismbcdigit(unsigned int ch)
1017 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1018 return (get_char_typeW( wch ) & C1_DIGIT);
1021 /*********************************************************************
1022 * _ismbcgraph(MSVCRT.@)
1024 int CDECL _ismbcgraph(unsigned int ch)
1026 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1027 return (get_char_typeW( wch ) & (C1_UPPER | C1_LOWER | C1_DIGIT | C1_PUNCT | C1_ALPHA));
1030 /*********************************************************************
1031 * _ismbcalpha (MSVCRT.@)
1033 int CDECL _ismbcalpha(unsigned int ch)
1035 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1036 return (get_char_typeW( wch ) & C1_ALPHA);
1039 /*********************************************************************
1040 * _ismbclower (MSVCRT.@)
1042 int CDECL _ismbclower(unsigned int ch)
1044 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1045 return (get_char_typeW( wch ) & C1_UPPER);
1048 /*********************************************************************
1049 * _ismbcupper (MSVCRT.@)
1051 int CDECL _ismbcupper(unsigned int ch)
1053 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1054 return (get_char_typeW( wch ) & C1_LOWER);
1057 /*********************************************************************
1058 * _ismbcsymbol(MSVCRT.@)
1060 int CDECL _ismbcsymbol(unsigned int ch)
1062 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1064 if (!GetStringTypeW(CT_CTYPE3, &wch, 1, &ctype))
1066 WARN("GetStringTypeW failed on %x\n", ch);
1069 return ((ctype & C3_SYMBOL) != 0);
1072 /*********************************************************************
1073 * _ismbcalnum (MSVCRT.@)
1075 int CDECL _ismbcalnum(unsigned int ch)
1077 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1078 return (get_char_typeW( wch ) & (C1_ALPHA | C1_DIGIT));
1081 /*********************************************************************
1082 * _ismbcspace (MSVCRT.@)
1084 int CDECL _ismbcspace(unsigned int ch)
1086 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1087 return (get_char_typeW( wch ) & C1_SPACE);
1090 /*********************************************************************
1091 * _ismbcprint (MSVCRT.@)
1093 int CDECL _ismbcprint(unsigned int ch)
1095 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1096 return (get_char_typeW( wch ) & (C1_UPPER | C1_LOWER | C1_DIGIT | C1_PUNCT | C1_ALPHA | C1_SPACE));
1099 /*********************************************************************
1100 * _ismbcpunct(MSVCRT.@)
1102 int CDECL _ismbcpunct(unsigned int ch)
1104 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1105 return (get_char_typeW( wch ) & C1_PUNCT);
1108 /*********************************************************************
1109 * _ismbchira(MSVCRT.@)
1111 int CDECL _ismbchira(unsigned int c)
1113 /* FIXME: use lc_ctype when supported, not lc_all */
1114 if(MSVCRT___lc_codepage == 932)
1116 /* Japanese/Hiragana, CP 932 */
1117 return (c >= 0x829f && c <= 0x82f1);
1122 /*********************************************************************
1123 * _ismbckata(MSVCRT.@)
1125 int CDECL _ismbckata(unsigned int c)
1127 /* FIXME: use lc_ctype when supported, not lc_all */
1128 if(MSVCRT___lc_codepage == 932)
1131 return _ismbbkana(c);
1132 /* Japanese/Katakana, CP 932 */
1133 return (c >= 0x8340 && c <= 0x8396 && c != 0x837f);
1138 /*********************************************************************
1139 * _ismbblead(MSVCRT.@)
1141 int CDECL _ismbblead(unsigned int c)
1143 return (MSVCRT_mbctype[(c&0xff) + 1] & _M1) != 0;
1147 /*********************************************************************
1148 * _ismbbtrail(MSVCRT.@)
1150 int CDECL _ismbbtrail(unsigned int c)
1152 return (MSVCRT_mbctype[(c&0xff) + 1] & _M2) != 0;
1155 /*********************************************************************
1156 * _ismbclegal(MSVCRT.@)
1158 int CDECL _ismbclegal(unsigned int c)
1160 return _ismbblead(HIBYTE(c)) && _ismbbtrail(LOBYTE(c));
1163 /*********************************************************************
1164 * _ismbslead(MSVCRT.@)
1166 int CDECL _ismbslead(const unsigned char* start, const unsigned char* str)
1170 if(!g_mbcp_is_multibyte)
1173 /* Lead bytes can also be trail bytes so we need to analyse the string
1175 while (start <= str)
1179 lead = !lead && _ismbblead(*start);
1183 return lead ? -1 : 0;
1186 /*********************************************************************
1187 * _ismbstrail(MSVCRT.@)
1189 int CDECL _ismbstrail(const unsigned char* start, const unsigned char* str)
1191 /* Note: this function doesn't check _ismbbtrail */
1192 if ((str > start) && _ismbslead(start, str-1))
1198 /*********************************************************************
1199 * _mbsbtype (MSVCRT.@)
1201 int CDECL _mbsbtype(const unsigned char *str, MSVCRT_size_t count)
1204 const unsigned char *end = str + count;
1205 int mbcp = g_mbcp_is_multibyte;
1207 /* Lead bytes can also be trail bytes so we need to analyse the string.
1208 * Also we must return _MBC_ILLEGAL for chars past the end of the string
1210 while (str < end) /* Note: we skip the last byte - will check after the loop */
1213 return _MBC_ILLEGAL;
1214 lead = mbcp && !lead && _ismbblead(*str);
1219 if (_ismbbtrail(*str))
1222 return _MBC_ILLEGAL;
1224 if (_ismbblead(*str))
1230 /*********************************************************************
1233 unsigned char* CDECL _mbsset(unsigned char* str, unsigned int c)
1235 unsigned char* ret = str;
1237 if(MSVCRT___mb_cur_max == 1 || c < 256)
1238 return u__strset(str, c); /* ASCII CP or SB char */
1240 c &= 0xffff; /* Strip high bits */
1242 while(str[0] && str[1])
1248 str[0] = '\0'; /* FIXME: OK to shorten? */
1253 /*********************************************************************
1254 * _mbsnbset(MSVCRT.@)
1256 unsigned char* CDECL _mbsnbset(unsigned char *str, unsigned int c, MSVCRT_size_t len)
1258 unsigned char *ret = str;
1263 if(MSVCRT___mb_cur_max == 1 || c < 256)
1264 return u__strnset(str, c, len); /* ASCII CP or SB char */
1266 c &= 0xffff; /* Strip high bits */
1268 while(str[0] && str[1] && (len > 1))
1276 /* as per msdn pad with a blank character */
1283 /*********************************************************************
1284 * _mbsnset(MSVCRT.@)
1286 unsigned char* CDECL _mbsnset(unsigned char* str, unsigned int c, MSVCRT_size_t len)
1288 unsigned char *ret = str;
1293 if(MSVCRT___mb_cur_max == 1 || c < 256)
1294 return u__strnset(str, c, len); /* ASCII CP or SB char */
1296 c &= 0xffff; /* Strip high bits */
1298 while(str[0] && str[1] && len--)
1304 str[0] = '\0'; /* FIXME: OK to shorten? */
1309 /*********************************************************************
1310 * _mbsnccnt(MSVCRT.@)
1311 * 'c' is for 'character'.
1313 MSVCRT_size_t CDECL _mbsnccnt(const unsigned char* str, MSVCRT_size_t len)
1316 if(MSVCRT___mb_cur_max > 1)
1319 while(*str && len-- > 0)
1321 if(MSVCRT_isleadbyte(*str))
1334 return min(ret, len); /* ASCII CP */
1337 /*********************************************************************
1338 * _mbsnbcnt(MSVCRT.@)
1339 * 'b' is for byte count.
1341 MSVCRT_size_t CDECL _mbsnbcnt(const unsigned char* str, MSVCRT_size_t len)
1344 if(MSVCRT___mb_cur_max > 1)
1346 const unsigned char* xstr = str;
1347 while(*xstr && len-- > 0)
1349 if (MSVCRT_isleadbyte(*xstr++))
1355 return min(ret, len); /* ASCII CP */
1358 /*********************************************************************
1359 * _mbsnbcat(MSVCRT.@)
1361 unsigned char* CDECL _mbsnbcat(unsigned char* dst, const unsigned char* src, MSVCRT_size_t len)
1363 if(MSVCRT___mb_cur_max > 1)
1365 unsigned char *res = dst;
1367 if (MSVCRT_isleadbyte(*dst++)) {
1371 /* as per msdn overwrite the lead byte in front of '\0' */
1377 while (*src && len--) *dst++ = *src++;
1381 return u_strncat(dst, src, len); /* ASCII CP */
1385 /*********************************************************************
1386 * _mbsncat(MSVCRT.@)
1388 unsigned char* CDECL _mbsncat(unsigned char* dst, const unsigned char* src, MSVCRT_size_t len)
1390 if(MSVCRT___mb_cur_max > 1)
1392 unsigned char *res = dst;
1395 if (MSVCRT_isleadbyte(*dst++))
1398 while (*src && len--)
1401 if(MSVCRT_isleadbyte(*src++))
1407 return u_strncat(dst, src, len); /* ASCII CP */
1411 /*********************************************************************
1414 unsigned char* CDECL _mbslwr(unsigned char* s)
1416 unsigned char *ret = s;
1419 if (MSVCRT___mb_cur_max > 1)
1424 c = _mbctolower(_mbsnextc(s));
1425 /* Note that I assume that the size of the character is unchanged */
1434 else for ( ; *s; s++) *s = tolower(*s);
1439 /*********************************************************************
1442 unsigned char* CDECL _mbsupr(unsigned char* s)
1444 unsigned char *ret = s;
1447 if (MSVCRT___mb_cur_max > 1)
1452 c = _mbctoupper(_mbsnextc(s));
1453 /* Note that I assume that the size of the character is unchanged */
1462 else for ( ; *s; s++) *s = toupper(*s);
1467 /*********************************************************************
1468 * _mbsspn (MSVCRT.@)
1470 MSVCRT_size_t CDECL _mbsspn(const unsigned char* string, const unsigned char* set)
1472 const unsigned char *p, *q;
1474 for (p = string; *p; p++)
1476 if (MSVCRT_isleadbyte(*p))
1478 for (q = set; *q; q++)
1482 if ((*p == *q) && (p[1] == q[1]))
1486 if (!q[0] || !q[1]) break;
1490 for (q = set; *q; q++)
1499 /*********************************************************************
1500 * _mbsspnp (MSVCRT.@)
1502 unsigned char* CDECL _mbsspnp(const unsigned char* string, const unsigned char* set)
1504 const unsigned char *p, *q;
1506 for (p = string; *p; p++)
1508 if (MSVCRT_isleadbyte(*p))
1510 for (q = set; *q; q++)
1514 if ((*p == *q) && (p[1] == q[1]))
1518 if (!q[0] || !q[1]) break;
1522 for (q = set; *q; q++)
1530 return (unsigned char *)p;
1533 /*********************************************************************
1534 * _mbscspn(MSVCRT.@)
1536 MSVCRT_size_t CDECL _mbscspn(const unsigned char* str, const unsigned char* cmp)
1538 if (MSVCRT___mb_cur_max > 1)
1539 FIXME("don't handle double character case\n");
1540 return u_strcspn(str, cmp);
1543 /*********************************************************************
1544 * _mbsrev (MSVCRT.@)
1546 unsigned char* CDECL _mbsrev(unsigned char* str)
1548 int i, len = _mbslen(str);
1549 unsigned char *p, *temp=MSVCRT_malloc(len*2);
1554 /* unpack multibyte string to temp buffer */
1556 for(i=0; i<len; i++)
1558 if (MSVCRT_isleadbyte(*p))
1570 /* repack it in the reverse order */
1572 for(i=len-1; i>=0; i--)
1574 if(MSVCRT_isleadbyte(temp[i*2]))
1590 /*********************************************************************
1591 * _mbspbrk (MSVCRT.@)
1593 unsigned char* CDECL _mbspbrk(const unsigned char* str, const unsigned char* accept)
1595 const unsigned char* p;
1599 for(p = accept; *p; p += (MSVCRT_isleadbyte(*p)?2:1) )
1602 if( !MSVCRT_isleadbyte(*p) || ( *(p+1) == *(str+1) ) )
1603 return (unsigned char*)str;
1605 str += (MSVCRT_isleadbyte(*str)?2:1);
1612 * Functions depending on locale codepage
1615 /*********************************************************************
1618 * Unlike most of the multibyte string functions this function uses
1619 * the locale codepage, not the codepage set by _setmbcp
1621 int CDECL MSVCRT_mblen(const char* str, MSVCRT_size_t size)
1623 if (str && *str && size)
1625 if(MSVCRT___mb_cur_max == 1)
1626 return 1; /* ASCII CP */
1628 return !MSVCRT_isleadbyte(*str) ? 1 : (size>1 ? 2 : -1);
1633 /*********************************************************************
1634 * _mbstrlen(MSVCRT.@)
1636 * Unlike most of the multibyte string functions this function uses
1637 * the locale codepage, not the codepage set by _setmbcp
1639 MSVCRT_size_t CDECL _mbstrlen(const char* str)
1641 if(MSVCRT___mb_cur_max > 1)
1643 MSVCRT_size_t len = 0;
1646 /* FIXME: According to the documentation we are supposed to test for
1647 * multi-byte character validity. Whatever that means
1649 str += MSVCRT_isleadbyte(*str) ? 2 : 1;
1654 return strlen(str); /* ASCII CP */