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"
29 #include "msvcrt/mbctype.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
33 unsigned char MSVCRT_mbctype[257];
34 int MSVCRT___mb_cur_max = 1;
35 extern int MSVCRT___lc_collate_cp;
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 {20932, {1, 255, 0, 0}},
52 {0, {1, 255, 0, 0}} /* match all with FIXME */
55 static MSVCRT_wchar_t msvcrt_mbc_to_wc(unsigned int ch)
65 mbch[0] = (ch >> 8) & 0xff;
69 if (!MultiByteToWideChar(MSVCRT___lc_codepage, 0, mbch, n_chars, &chW, 1))
71 WARN("MultiByteToWideChar failed on %x\n", ch);
77 static inline size_t u_strlen( const unsigned char *str )
79 return strlen( (const char*) str );
82 static inline unsigned char* u_strncat( unsigned char* dst, const unsigned char* src, size_t len )
84 return (unsigned char*)strncat( (char*)dst, (const char*)src, len);
87 static inline int u_strcmp( const unsigned char *s1, const unsigned char *s2 )
89 return strcmp( (const char*)s1, (const char*)s2 );
92 static inline int u_strcasecmp( const unsigned char *s1, const unsigned char *s2 )
94 return strcasecmp( (const char*)s1, (const char*)s2 );
97 static inline int u_strncmp( const unsigned char *s1, const unsigned char *s2, size_t len )
99 return strncmp( (const char*)s1, (const char*)s2, len );
102 static inline int u_strncasecmp( const unsigned char *s1, const unsigned char *s2, size_t len )
104 return strncasecmp( (const char*)s1, (const char*)s2, len );
107 static inline unsigned char *u_strchr( const unsigned char *s, unsigned char x )
109 return (unsigned char*) strchr( (const char*)s, x );
112 static inline unsigned char *u_strrchr( const unsigned char *s, unsigned char x )
114 return (unsigned char*) strrchr( (const char*)s, x );
117 static inline unsigned char *u_strtok( unsigned char *s, const unsigned char *delim )
119 return (unsigned char*) strtok( (char*)s, (const char*)delim );
122 static inline unsigned char *u__strset( unsigned char *s, unsigned char c )
124 return (unsigned char*) _strset( (char*)s, c);
127 static inline unsigned char *u__strnset( unsigned char *s, unsigned char c, size_t len )
129 return (unsigned char*) _strnset( (char*)s, c, len );
132 static inline size_t u_strcspn( const unsigned char *s, const unsigned char *rej )
134 return strcspn( (const char *)s, (const char*)rej );
137 /*********************************************************************
138 * __p__mbctype (MSVCRT.@)
140 unsigned char* CDECL __p__mbctype(void)
142 return MSVCRT_mbctype;
145 /*********************************************************************
146 * __p___mb_cur_max(MSVCRT.@)
148 int* CDECL __p___mb_cur_max(void)
150 return &MSVCRT___mb_cur_max;
153 /*********************************************************************
154 * _setmbcp (MSVCRT.@)
156 int CDECL _setmbcp(int cp)
178 newcp = MSVCRT___lc_codepage;
181 newcp = 20127; /* ASCII */
188 if (!GetCPInfo(newcp, &cpi))
190 WARN("Codepage %d not found\n", newcp);
191 msvcrt_set_errno(MSVCRT_EINVAL);
195 /* setup the _mbctype */
196 memset(MSVCRT_mbctype, 0, sizeof(MSVCRT_mbctype));
198 bytes = cpi.LeadByte;
199 while (bytes[0] || bytes[1])
201 for (i = bytes[0]; i <= bytes[1]; i++)
202 MSVCRT_mbctype[i + 1] |= _M1;
206 if (cpi.MaxCharSize > 1)
208 /* trail bytes not available through kernel32 but stored in a structure in msvcrt */
209 struct cp_extra_info_t *cpextra = g_cpextrainfo;
212 if (cpextra->cp == 0 || cpextra->cp == newcp)
214 if (cpextra->cp == 0)
215 FIXME("trail bytes data not available for DBCS codepage %d - assuming all bytes\n", newcp);
217 bytes = cpextra->TrailBytes;
218 while (bytes[0] || bytes[1])
220 for (i = bytes[0]; i <= bytes[1]; i++)
221 MSVCRT_mbctype[i + 1] |= _M2;
230 /* we can't use GetStringTypeA directly because we don't have a locale - only a code page
233 for (i = 0; i < 256; i++)
234 if (!(MSVCRT_mbctype[i + 1] & _M1))
235 bufA[charcount++] = i;
237 ret = MultiByteToWideChar(newcp, 0, bufA, charcount, bufW, charcount);
238 if (ret != charcount)
239 ERR("MultiByteToWideChar of chars failed for cp %d, ret=%d (exp %d), error=%d\n", newcp, ret, charcount, GetLastError());
241 GetStringTypeW(CT_CTYPE1, bufW, charcount, chartypes);
243 curr_type = chartypes;
244 for (i = 0; i < 256; i++)
245 if (!(MSVCRT_mbctype[i + 1] & _M1))
247 if ((*curr_type) & C1_UPPER)
248 MSVCRT_mbctype[i + 1] |= _SBUP;
249 if ((*curr_type) & C1_LOWER)
250 MSVCRT_mbctype[i + 1] |= _SBLOW;
254 if (newcp == 932) /* CP932 only - set _MP and _MS */
256 /* On Windows it's possible to calculate the _MP and _MS from CT_CTYPE1
257 * and CT_CTYPE3. But as of Wine 0.9.43 we return wrong values what makes
258 * it hard. As this is set only for codepage 932 we hardcode it what gives
259 * also faster execution.
261 for (i = 161; i <= 165; i++)
262 MSVCRT_mbctype[i + 1] |= _MP;
263 for (i = 166; i <= 223; i++)
264 MSVCRT_mbctype[i + 1] |= _MS;
267 MSVCRT___lc_collate_cp = MSVCRT___lc_codepage = newcp;
268 TRACE("(%d) -> %d\n", cp, MSVCRT___lc_codepage);
272 /*********************************************************************
273 * _getmbcp (MSVCRT.@)
275 int CDECL _getmbcp(void)
277 return MSVCRT___lc_codepage;
280 /*********************************************************************
281 * _mbsnextc(MSVCRT.@)
283 unsigned int CDECL _mbsnextc(const unsigned char* str)
286 return *str << 8 | str[1];
290 /*********************************************************************
291 * _mbctolower(MSVCRT.@)
293 unsigned int CDECL _mbctolower(unsigned int c)
295 if (MSVCRT_isleadbyte(c))
297 FIXME("Handle MBC chars\n");
300 return tolower(c); /* ASCII CP or SB char */
303 /*********************************************************************
304 * _mbctoupper(MSVCRT.@)
306 unsigned int CDECL _mbctoupper(unsigned int c)
308 if (MSVCRT_isleadbyte(c))
310 FIXME("Handle MBC chars\n");
313 return toupper(c); /* ASCII CP or SB char */
316 /*********************************************************************
319 unsigned char* CDECL _mbsdec(const unsigned char* start, const unsigned char* cur)
321 if(MSVCRT___mb_cur_max > 1)
322 return (unsigned char *)(_ismbstrail(start,cur-1) ? cur - 2 : cur -1);
324 return (unsigned char *)cur - 1; /* ASCII CP or SB char */
327 /*********************************************************************
330 unsigned char* CDECL _mbsinc(const unsigned char* str)
332 if(MSVCRT___mb_cur_max > 1 && MSVCRT_isleadbyte(*str))
333 return (unsigned char*)str + 2; /* MB char */
335 return (unsigned char*)str + 1; /* ASCII CP or SB char */
338 /*********************************************************************
341 unsigned char* CDECL _mbsninc(const unsigned char* str, MSVCRT_size_t num)
345 if(MSVCRT___mb_cur_max > 1)
349 return (unsigned char*)str;
351 return (unsigned char*)str + num; /* ASCII CP */
354 /*********************************************************************
357 unsigned int CDECL _mbclen(const unsigned char* str)
359 return MSVCRT_isleadbyte(*str) ? 2 : 1;
362 /*********************************************************************
365 int CDECL MSVCRT_mblen(const char* str, MSVCRT_size_t size)
367 if (str && *str && size)
369 if(MSVCRT___mb_cur_max == 1)
370 return 1; /* ASCII CP */
372 return !MSVCRT_isleadbyte(*str) ? 1 : (size>1 ? 2 : -1);
377 /*********************************************************************
380 MSVCRT_size_t CDECL _mbslen(const unsigned char* str)
382 if(MSVCRT___mb_cur_max > 1)
384 MSVCRT_size_t len = 0;
387 str += MSVCRT_isleadbyte(*str) ? 2 : 1;
392 return u_strlen(str); /* ASCII CP */
395 /*********************************************************************
396 * _mbstrlen(MSVCRT.@)
398 MSVCRT_size_t CDECL _mbstrlen(const char* str)
400 if(MSVCRT___mb_cur_max > 1)
402 MSVCRT_size_t len = 0;
405 /* FIXME: According to the documentation we are supposed to test for
406 * multi-byte character validity. Whatever that means
408 str += MSVCRT_isleadbyte(*str) ? 2 : 1;
413 return strlen(str); /* ASCII CP */
416 /*********************************************************************
419 void CDECL _mbccpy(unsigned char* dest, const unsigned char* src)
422 if(MSVCRT___mb_cur_max > 1 && MSVCRT_isleadbyte(*src))
423 *++dest = *++src; /* MB char */
426 /*********************************************************************
429 unsigned char* CDECL _mbsncpy(unsigned char* dst, const unsigned char* src, MSVCRT_size_t n)
431 unsigned char* ret = dst;
434 if(MSVCRT___mb_cur_max > 1)
440 if (MSVCRT_isleadbyte(*src++))
449 if (!(*dst++ = *src++)) break;
452 while (n--) *dst++ = 0;
456 /*********************************************************************
457 * _mbsnbcpy(MSVCRT.@)
459 unsigned char* CDECL _mbsnbcpy(unsigned char* dst, const unsigned char* src, MSVCRT_size_t n)
461 unsigned char* ret = dst;
464 if(MSVCRT___mb_cur_max > 1)
466 while (*src && (n > 1))
470 if (MSVCRT_isleadbyte(*src++))
476 if (*src && n && !MSVCRT_isleadbyte(*src))
478 /* If the last character is a multi-byte character then
479 * we cannot copy it since we have only one byte left
490 if (!(*dst++ = *src++)) break;
493 while (n--) *dst++ = 0;
497 /*********************************************************************
500 int CDECL _mbscmp(const unsigned char* str, const unsigned char* cmp)
502 if(MSVCRT___mb_cur_max > 1)
504 unsigned int strc, cmpc;
507 return *cmp ? -1 : 0;
510 strc = _mbsnextc(str);
511 cmpc = _mbsnextc(cmp);
513 return strc < cmpc ? -1 : 1;
514 str +=(strc > 255) ? 2 : 1;
515 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
518 return u_strcmp(str, cmp); /* ASCII CP */
521 /*********************************************************************
522 * _mbsicoll(MSVCRT.@)
523 * FIXME: handle locales.
525 int CDECL _mbsicoll(const unsigned char* str, const unsigned char* cmp)
527 if(MSVCRT___mb_cur_max > 1)
529 unsigned int strc, cmpc;
532 return *cmp ? -1 : 0;
535 strc = _mbctolower(_mbsnextc(str));
536 cmpc = _mbctolower(_mbsnextc(cmp));
538 return strc < cmpc ? -1 : 1;
539 str +=(strc > 255) ? 2 : 1;
540 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
543 return u_strcasecmp(str, cmp); /* ASCII CP */
546 /*********************************************************************
548 * Performs a case-sensitive comparison according to the current code page
550 * _NLSCMPERROR if error
551 * FIXME: handle locales.
553 int CDECL _mbscoll(const unsigned char* str, const unsigned char* cmp)
555 if(MSVCRT___mb_cur_max > 1)
557 unsigned int strc, cmpc;
560 return *cmp ? -1 : 0;
563 strc = _mbsnextc(str);
564 cmpc = _mbsnextc(cmp);
566 return strc < cmpc ? -1 : 1;
567 str +=(strc > 255) ? 2 : 1;
568 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
571 return u_strcmp(str, cmp); /* ASCII CP */
575 /*********************************************************************
578 int CDECL _mbsicmp(const unsigned char* str, const unsigned char* cmp)
580 if(MSVCRT___mb_cur_max > 1)
582 unsigned int strc, cmpc;
585 return *cmp ? -1 : 0;
588 strc = _mbctolower(_mbsnextc(str));
589 cmpc = _mbctolower(_mbsnextc(cmp));
591 return strc < cmpc ? -1 : 1;
592 str +=(strc > 255) ? 2 : 1;
593 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
596 return u_strcasecmp(str, cmp); /* ASCII CP */
599 /*********************************************************************
602 int CDECL _mbsncmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
607 if(MSVCRT___mb_cur_max > 1)
609 unsigned int strc, cmpc;
614 return *cmp ? -1 : 0;
617 strc = _mbsnextc(str);
618 cmpc = _mbsnextc(cmp);
620 return strc < cmpc ? -1 : 1;
621 inc=(strc > 255) ? 2 : 1; /* Equal, use same increment */
625 return 0; /* Matched len chars */
627 return u_strncmp(str, cmp, len); /* ASCII CP */
630 /*********************************************************************
631 * _mbsnbcmp(MSVCRT.@)
633 int CDECL _mbsnbcmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
637 if(MSVCRT___mb_cur_max > 1)
639 unsigned int strc, cmpc;
644 return *cmp ? -1 : 0;
647 if (MSVCRT_isleadbyte(*str))
649 strc=(len>=2)?_mbsnextc(str):0;
657 if (MSVCRT_isleadbyte(*cmp))
658 cmpc=(len>=2)?_mbsnextc(cmp):0;
662 return strc < cmpc ? -1 : 1;
667 return 0; /* Matched len chars */
669 return u_strncmp(str,cmp,len);
672 /*********************************************************************
673 * _mbsnicmp(MSVCRT.@)
675 * Compare two multibyte strings case insensitively to 'len' characters.
677 int CDECL _mbsnicmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
679 /* FIXME: No tolower() for mb strings yet */
680 if(MSVCRT___mb_cur_max > 1)
682 unsigned int strc, cmpc;
686 return *cmp ? -1 : 0;
689 strc = _mbctolower(_mbsnextc(str));
690 cmpc = _mbctolower(_mbsnextc(cmp));
692 return strc < cmpc ? -1 : 1;
693 str +=(strc > 255) ? 2 : 1;
694 cmp +=(strc > 255) ? 2 : 1; /* Equal, use same increment */
696 return 0; /* Matched len chars */
698 return u_strncasecmp(str, cmp, len); /* ASCII CP */
701 /*********************************************************************
702 * _mbsnbicmp(MSVCRT.@)
704 int CDECL _mbsnbicmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
708 if(MSVCRT___mb_cur_max > 1)
710 unsigned int strc, cmpc;
715 return *cmp ? -1 : 0;
718 if (MSVCRT_isleadbyte(*str))
720 strc=(len>=2)?_mbsnextc(str):0;
728 if (MSVCRT_isleadbyte(*cmp))
729 cmpc=(len>=2)?_mbsnextc(cmp):0;
732 strc = _mbctolower(strc);
733 cmpc = _mbctolower(cmpc);
735 return strc < cmpc ? -1 : 1;
740 return 0; /* Matched len bytes */
742 return u_strncasecmp(str,cmp,len);
745 /*********************************************************************
748 unsigned char * CDECL _mbscat( unsigned char *dst, const unsigned char *src )
750 strcat( (char *)dst, (const char *)src );
754 /*********************************************************************
757 unsigned char* CDECL _mbscpy( unsigned char *dst, const unsigned char *src )
759 strcpy( (char *)dst, (const char *)src );
763 /*********************************************************************
766 unsigned char * CDECL _mbsstr(const unsigned char *haystack, const unsigned char *needle)
768 return (unsigned char *)strstr( (const char *)haystack, (const char *)needle );
771 /*********************************************************************
774 * Find a multibyte character in a multibyte string.
776 unsigned char* CDECL _mbschr(const unsigned char* s, unsigned int x)
778 if(MSVCRT___mb_cur_max > 1)
785 return (unsigned char*)s;
788 s += c > 255 ? 2 : 1;
791 return u_strchr(s, x); /* ASCII CP */
794 /*********************************************************************
797 unsigned char* CDECL _mbsrchr(const unsigned char* s, unsigned int x)
799 if(MSVCRT___mb_cur_max > 1)
802 unsigned char* match=NULL;
808 match=(unsigned char*)s;
811 s +=(c > 255) ? 2 : 1;
814 return u_strrchr(s, x);
817 /*********************************************************************
820 * Find and extract tokens from strings
822 unsigned char* CDECL _mbstok(unsigned char *str, const unsigned char *delim)
824 thread_data_t *data = msvcrt_get_thread_data();
827 if(MSVCRT___mb_cur_max > 1)
832 if (!(str = data->mbstok_next)) return NULL;
834 while ((c = _mbsnextc(str)) && _mbschr(delim, c)) {
835 str += c > 255 ? 2 : 1;
837 if (!*str) return NULL;
839 while ((c = _mbsnextc(str)) && !_mbschr(delim, c)) {
840 str += c > 255 ? 2 : 1;
844 if (c > 255) *str++ = 0;
846 data->mbstok_next = str;
849 return u_strtok(str, delim); /* ASCII CP */
852 /*********************************************************************
855 int CDECL MSVCRT_mbtowc(MSVCRT_wchar_t *dst, const char* str, MSVCRT_size_t n)
857 /* temp var needed because MultiByteToWideChar wants non NULL destination */
858 MSVCRT_wchar_t tmpdst = '\0';
862 if(!MultiByteToWideChar(CP_ACP, 0, str, n, &tmpdst, 1))
866 /* return the number of bytes from src that have been used */
869 if(n >= 2 && MSVCRT_isleadbyte(*str) && str[1])
874 /*********************************************************************
875 * _mbbtombc(MSVCRT.@)
877 unsigned int CDECL _mbbtombc(unsigned int c)
879 if(MSVCRT___mb_cur_max > 1 &&
880 ((c >= 0x20 && c <=0x7e) ||(c >= 0xa1 && c <= 0xdf)))
882 /* FIXME: I can't get this function to return anything
883 * different from what I pass it...
886 return c; /* ASCII CP or no MB char */
889 /*********************************************************************
892 int CDECL _mbbtype(unsigned char c, int type)
896 if ((c >= 0x20 && c <= 0x7e) || (c >= 0xa1 && c <= 0xdf))
898 else if ((c >= 0x40 && c <= 0x7e) || (c >= 0x80 && c <= 0xfc))
905 if ((c >= 0x20 && c <= 0x7e) || (c >= 0xa1 && c <= 0xdf))
907 else if ((c >= 0x81 && c <= 0x9f) || (c >= 0xe0 && c <= 0xfc))
914 /*********************************************************************
915 * _ismbbkana(MSVCRT.@)
917 int CDECL _ismbbkana(unsigned int c)
919 /* FIXME: use lc_ctype when supported, not lc_all */
920 if(MSVCRT___lc_codepage == 932)
922 /* Japanese/Katakana, CP 932 */
923 return (c >= 0xa1 && c <= 0xdf);
928 /*********************************************************************
929 * _ismbcdigit(MSVCRT.@)
931 int CDECL _ismbcdigit(unsigned int ch)
933 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
934 return (get_char_typeW( wch ) & C1_DIGIT);
937 /*********************************************************************
938 * _ismbcgraph(MSVCRT.@)
940 int CDECL _ismbcgraph(unsigned int ch)
942 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
943 return (get_char_typeW( wch ) & (C1_UPPER | C1_LOWER | C1_DIGIT | C1_PUNCT | C1_ALPHA));
946 /*********************************************************************
947 * _ismbcalpha (MSVCRT.@)
949 int CDECL _ismbcalpha(unsigned int ch)
951 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
952 return (get_char_typeW( wch ) & C1_ALPHA);
955 /*********************************************************************
956 * _ismbclower (MSVCRT.@)
958 int CDECL _ismbclower(unsigned int ch)
960 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
961 return (get_char_typeW( wch ) & C1_UPPER);
964 /*********************************************************************
965 * _ismbcupper (MSVCRT.@)
967 int CDECL _ismbcupper(unsigned int ch)
969 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
970 return (get_char_typeW( wch ) & C1_LOWER);
973 /*********************************************************************
974 * _ismbcsymbol(MSVCRT.@)
976 int CDECL _ismbcsymbol(unsigned int ch)
978 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
980 if (!GetStringTypeW(CT_CTYPE3, &wch, 1, &ctype))
982 WARN("GetStringTypeW failed on %x\n", ch);
985 return ((ctype & C3_SYMBOL) != 0);
988 /*********************************************************************
989 * _ismbcalnum (MSVCRT.@)
991 int CDECL _ismbcalnum(unsigned int ch)
993 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
994 return (get_char_typeW( wch ) & (C1_ALPHA | C1_DIGIT));
997 /*********************************************************************
998 * _ismbcspace (MSVCRT.@)
1000 int CDECL _ismbcspace(unsigned int ch)
1002 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1003 return (get_char_typeW( wch ) & C1_SPACE);
1006 /*********************************************************************
1007 * _ismbcprint (MSVCRT.@)
1009 int CDECL _ismbcprint(unsigned int ch)
1011 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1012 return (get_char_typeW( wch ) & (C1_UPPER | C1_LOWER | C1_DIGIT | C1_PUNCT | C1_ALPHA | C1_SPACE));
1015 /*********************************************************************
1016 * _ismbcpunct(MSVCRT.@)
1018 int CDECL _ismbcpunct(unsigned int ch)
1020 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1021 return (get_char_typeW( wch ) & C1_PUNCT);
1024 /*********************************************************************
1025 * _ismbchira(MSVCRT.@)
1027 int CDECL _ismbchira(unsigned int c)
1029 /* FIXME: use lc_ctype when supported, not lc_all */
1030 if(MSVCRT___lc_codepage == 932)
1032 /* Japanese/Hiragana, CP 932 */
1033 return (c >= 0x829f && c <= 0x82f1);
1038 /*********************************************************************
1039 * _ismbckata(MSVCRT.@)
1041 int CDECL _ismbckata(unsigned int c)
1043 /* FIXME: use lc_ctype when supported, not lc_all */
1044 if(MSVCRT___lc_codepage == 932)
1047 return _ismbbkana(c);
1048 /* Japanese/Katakana, CP 932 */
1049 return (c >= 0x8340 && c <= 0x8396 && c != 0x837f);
1054 /*********************************************************************
1055 * _ismbblead(MSVCRT.@)
1057 int CDECL _ismbblead(unsigned int c)
1059 return (MSVCRT_mbctype[(c&0xff) + 1] & _M1) != 0;
1063 /*********************************************************************
1064 * _ismbbtrail(MSVCRT.@)
1066 int CDECL _ismbbtrail(unsigned int c)
1068 return (MSVCRT_mbctype[(c&0xff) + 1] & _M2) != 0;
1071 /*********************************************************************
1072 * _ismbslead(MSVCRT.@)
1074 int CDECL _ismbslead(const unsigned char* start, const unsigned char* str)
1076 /* Lead bytes can also be trail bytes if caller messed up
1077 * iterating through the string...
1079 if(MSVCRT___mb_cur_max > 1)
1082 start += MSVCRT_isleadbyte(*str) ? 2 : 1;
1085 return MSVCRT_isleadbyte(*str);
1087 return 0; /* Must have been a trail, we skipped it */
1090 /*********************************************************************
1091 * _ismbstrail(MSVCRT.@)
1093 int CDECL _ismbstrail(const unsigned char* start, const unsigned char* str)
1095 /* Must not be a lead, and must be preceded by one */
1096 return !_ismbslead(start, str) && MSVCRT_isleadbyte(str[-1]);
1099 /*********************************************************************
1102 unsigned char* CDECL _mbsset(unsigned char* str, unsigned int c)
1104 unsigned char* ret = str;
1106 if(MSVCRT___mb_cur_max == 1 || c < 256)
1107 return u__strset(str, c); /* ASCII CP or SB char */
1109 c &= 0xffff; /* Strip high bits */
1111 while(str[0] && str[1])
1117 str[0] = '\0'; /* FIXME: OK to shorten? */
1122 /*********************************************************************
1123 * _mbsnbset(MSVCRT.@)
1125 unsigned char* CDECL _mbsnbset(unsigned char *str, unsigned int c, MSVCRT_size_t len)
1127 unsigned char *ret = str;
1132 if(MSVCRT___mb_cur_max == 1 || c < 256)
1133 return u__strnset(str, c, len); /* ASCII CP or SB char */
1135 c &= 0xffff; /* Strip high bits */
1137 while(str[0] && str[1] && (len > 1))
1145 /* as per msdn pad with a blank character */
1152 /*********************************************************************
1153 * _mbsnset(MSVCRT.@)
1155 unsigned char* CDECL _mbsnset(unsigned char* str, unsigned int c, MSVCRT_size_t len)
1157 unsigned char *ret = str;
1162 if(MSVCRT___mb_cur_max == 1 || c < 256)
1163 return u__strnset(str, c, len); /* ASCII CP or SB char */
1165 c &= 0xffff; /* Strip high bits */
1167 while(str[0] && str[1] && len--)
1173 str[0] = '\0'; /* FIXME: OK to shorten? */
1178 /*********************************************************************
1179 * _mbsnccnt(MSVCRT.@)
1180 * 'c' is for 'character'.
1182 MSVCRT_size_t CDECL _mbsnccnt(const unsigned char* str, MSVCRT_size_t len)
1185 if(MSVCRT___mb_cur_max > 1)
1188 while(*str && len-- > 0)
1190 if(MSVCRT_isleadbyte(*str))
1203 return min(ret, len); /* ASCII CP */
1206 /*********************************************************************
1207 * _mbsnbcnt(MSVCRT.@)
1208 * 'b' is for byte count.
1210 MSVCRT_size_t CDECL _mbsnbcnt(const unsigned char* str, MSVCRT_size_t len)
1213 if(MSVCRT___mb_cur_max > 1)
1215 const unsigned char* xstr = str;
1216 while(*xstr && len-- > 0)
1218 if (MSVCRT_isleadbyte(*xstr++))
1224 return min(ret, len); /* ASCII CP */
1227 /*********************************************************************
1228 * _mbsnbcat(MSVCRT.@)
1230 unsigned char* CDECL _mbsnbcat(unsigned char* dst, const unsigned char* src, MSVCRT_size_t len)
1232 if(MSVCRT___mb_cur_max > 1)
1234 unsigned char *res = dst;
1236 if (MSVCRT_isleadbyte(*dst++)) {
1240 /* as per msdn overwrite the lead byte in front of '\0' */
1246 while (*src && len--) *dst++ = *src++;
1250 return u_strncat(dst, src, len); /* ASCII CP */
1254 /*********************************************************************
1255 * _mbsncat(MSVCRT.@)
1257 unsigned char* CDECL _mbsncat(unsigned char* dst, const unsigned char* src, MSVCRT_size_t len)
1259 if(MSVCRT___mb_cur_max > 1)
1261 unsigned char *res = dst;
1264 if (MSVCRT_isleadbyte(*dst++))
1267 while (*src && len--)
1270 if(MSVCRT_isleadbyte(*src++))
1276 return u_strncat(dst, src, len); /* ASCII CP */
1280 /*********************************************************************
1283 unsigned char* CDECL _mbslwr(unsigned char* s)
1285 unsigned char *ret = s;
1288 if (MSVCRT___mb_cur_max > 1)
1293 c = _mbctolower(_mbsnextc(s));
1294 /* Note that I assume that the size of the character is unchanged */
1303 else for ( ; *s; s++) *s = tolower(*s);
1308 /*********************************************************************
1311 unsigned char* CDECL _mbsupr(unsigned char* s)
1313 unsigned char *ret = s;
1316 if (MSVCRT___mb_cur_max > 1)
1321 c = _mbctoupper(_mbsnextc(s));
1322 /* Note that I assume that the size of the character is unchanged */
1331 else for ( ; *s; s++) *s = toupper(*s);
1336 /*********************************************************************
1337 * _mbsspn (MSVCRT.@)
1339 MSVCRT_size_t CDECL _mbsspn(const unsigned char* string, const unsigned char* set)
1341 const unsigned char *p, *q;
1343 for (p = string; *p; p++)
1345 if (MSVCRT_isleadbyte(*p))
1347 for (q = set; *q; q++)
1351 if ((*p == *q) && (p[1] == q[1]))
1355 if (!q[0] || !q[1]) break;
1359 for (q = set; *q; q++)
1368 /*********************************************************************
1369 * _mbsspnp (MSVCRT.@)
1371 const unsigned char* CDECL _mbsspnp(const unsigned char* string, const unsigned char* set)
1373 const unsigned char *p, *q;
1375 for (p = string; *p; p++)
1377 if (MSVCRT_isleadbyte(*p))
1379 for (q = set; *q; q++)
1383 if ((*p == *q) && (p[1] == q[1]))
1387 if (!q[0] || !q[1]) break;
1391 for (q = set; *q; q++)
1402 /*********************************************************************
1403 * _mbscspn(MSVCRT.@)
1405 MSVCRT_size_t CDECL _mbscspn(const unsigned char* str, const unsigned char* cmp)
1407 if (MSVCRT___mb_cur_max > 1)
1408 FIXME("don't handle double character case\n");
1409 return u_strcspn(str, cmp);
1412 /*********************************************************************
1413 * _mbsrev (MSVCRT.@)
1415 unsigned char* CDECL _mbsrev(unsigned char* str)
1417 int i, len = _mbslen(str);
1418 unsigned char *p, *temp=MSVCRT_malloc(len*2);
1423 /* unpack multibyte string to temp buffer */
1425 for(i=0; i<len; i++)
1427 if (MSVCRT_isleadbyte(*p))
1439 /* repack it in the reverse order */
1441 for(i=len-1; i>=0; i--)
1443 if(MSVCRT_isleadbyte(temp[i*2]))
1459 /*********************************************************************
1460 * _mbspbrk (MSVCRT.@)
1462 unsigned char* CDECL _mbspbrk(const unsigned char* str, const unsigned char* accept)
1464 const unsigned char* p;
1468 for(p = accept; *p; p += (MSVCRT_isleadbyte(*p)?2:1) )
1471 if( !MSVCRT_isleadbyte(*p) || ( *(p+1) == *(str+1) ) )
1472 return (unsigned char*)str;
1474 str += (MSVCRT_isleadbyte(*str)?2:1);