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"
30 #include "msvcrt/errno.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
34 unsigned char MSVCRT_mbctype[257];
35 static int g_mbcp_is_multibyte = 0;
37 int MSVCRT___mb_cur_max = 1;
38 extern int MSVCRT___lc_collate_cp;
40 /* It seems that the data about valid trail bytes is not available from kernel32
41 * so we have to store is here. The format is the same as for lead bytes in CPINFO */
42 struct cp_extra_info_t
45 BYTE TrailBytes[MAX_LEADBYTES];
48 static struct cp_extra_info_t g_cpextrainfo[] =
50 {932, {0x40, 0x7e, 0x80, 0xfc, 0, 0}},
51 {936, {0x40, 0xfe, 0, 0}},
52 {949, {0x41, 0xfe, 0, 0}},
53 {950, {0x40, 0x7e, 0xa1, 0xfe, 0, 0}},
54 {20932, {1, 255, 0, 0}}, /* seems to give different results on different systems */
55 {0, {1, 255, 0, 0}} /* match all with FIXME */
58 static MSVCRT_wchar_t msvcrt_mbc_to_wc(unsigned int ch)
68 mbch[0] = (ch >> 8) & 0xff;
72 if (!MultiByteToWideChar(MSVCRT___lc_codepage, 0, mbch, n_chars, &chW, 1))
74 WARN("MultiByteToWideChar failed on %x\n", ch);
80 static inline size_t u_strlen( const unsigned char *str )
82 return strlen( (const char*) str );
85 static inline unsigned char* u_strncat( unsigned char* dst, const unsigned char* src, size_t len )
87 return (unsigned char*)strncat( (char*)dst, (const char*)src, len);
90 static inline int u_strcmp( const unsigned char *s1, const unsigned char *s2 )
92 return strcmp( (const char*)s1, (const char*)s2 );
95 static inline int u_strcasecmp( const unsigned char *s1, const unsigned char *s2 )
97 return strcasecmp( (const char*)s1, (const char*)s2 );
100 static inline int u_strncmp( const unsigned char *s1, const unsigned char *s2, size_t len )
102 return strncmp( (const char*)s1, (const char*)s2, len );
105 static inline int u_strncasecmp( const unsigned char *s1, const unsigned char *s2, size_t len )
107 return strncasecmp( (const char*)s1, (const char*)s2, len );
110 static inline unsigned char *u_strchr( const unsigned char *s, unsigned char x )
112 return (unsigned char*) strchr( (const char*)s, x );
115 static inline unsigned char *u_strrchr( const unsigned char *s, unsigned char x )
117 return (unsigned char*) strrchr( (const char*)s, x );
120 static inline unsigned char *u_strtok( unsigned char *s, const unsigned char *delim )
122 return (unsigned char*) strtok( (char*)s, (const char*)delim );
125 static inline unsigned char *u__strset( unsigned char *s, unsigned char c )
127 return (unsigned char*) _strset( (char*)s, c);
130 static inline unsigned char *u__strnset( unsigned char *s, unsigned char c, size_t len )
132 return (unsigned char*) _strnset( (char*)s, c, len );
135 static inline size_t u_strcspn( const unsigned char *s, const unsigned char *rej )
137 return strcspn( (const char *)s, (const char*)rej );
140 /*********************************************************************
141 * __p__mbctype (MSVCRT.@)
143 unsigned char* CDECL __p__mbctype(void)
145 return MSVCRT_mbctype;
148 /*********************************************************************
149 * __p___mb_cur_max(MSVCRT.@)
151 int* CDECL __p___mb_cur_max(void)
153 return &MSVCRT___mb_cur_max;
156 /*********************************************************************
157 * _setmbcp (MSVCRT.@)
159 int CDECL _setmbcp(int cp)
181 newcp = MSVCRT___lc_codepage;
184 newcp = 20127; /* ASCII */
191 if (!GetCPInfo(newcp, &cpi))
193 WARN("Codepage %d not found\n", newcp);
194 msvcrt_set_errno(MSVCRT_EINVAL);
198 /* setup the _mbctype */
199 memset(MSVCRT_mbctype, 0, sizeof(MSVCRT_mbctype));
201 bytes = cpi.LeadByte;
202 while (bytes[0] || bytes[1])
204 for (i = bytes[0]; i <= bytes[1]; i++)
205 MSVCRT_mbctype[i + 1] |= _M1;
209 if (cpi.MaxCharSize > 1)
211 /* trail bytes not available through kernel32 but stored in a structure in msvcrt */
212 struct cp_extra_info_t *cpextra = g_cpextrainfo;
214 g_mbcp_is_multibyte = 1;
217 if (cpextra->cp == 0 || cpextra->cp == newcp)
219 if (cpextra->cp == 0)
220 FIXME("trail bytes data not available for DBCS codepage %d - assuming all bytes\n", newcp);
222 bytes = cpextra->TrailBytes;
223 while (bytes[0] || bytes[1])
225 for (i = bytes[0]; i <= bytes[1]; i++)
226 MSVCRT_mbctype[i + 1] |= _M2;
235 g_mbcp_is_multibyte = 0;
237 /* we can't use GetStringTypeA directly because we don't have a locale - only a code page
240 for (i = 0; i < 256; i++)
241 if (!(MSVCRT_mbctype[i + 1] & _M1))
242 bufA[charcount++] = i;
244 ret = MultiByteToWideChar(newcp, 0, bufA, charcount, bufW, charcount);
245 if (ret != charcount)
246 ERR("MultiByteToWideChar of chars failed for cp %d, ret=%d (exp %d), error=%d\n", newcp, ret, charcount, GetLastError());
248 GetStringTypeW(CT_CTYPE1, bufW, charcount, chartypes);
250 curr_type = chartypes;
251 for (i = 0; i < 256; i++)
252 if (!(MSVCRT_mbctype[i + 1] & _M1))
254 if ((*curr_type) & C1_UPPER)
255 MSVCRT_mbctype[i + 1] |= _SBUP;
256 if ((*curr_type) & C1_LOWER)
257 MSVCRT_mbctype[i + 1] |= _SBLOW;
261 if (newcp == 932) /* CP932 only - set _MP and _MS */
263 /* On Windows it's possible to calculate the _MP and _MS from CT_CTYPE1
264 * and CT_CTYPE3. But as of Wine 0.9.43 we return wrong values what makes
265 * it hard. As this is set only for codepage 932 we hardcode it what gives
266 * also faster execution.
268 for (i = 161; i <= 165; i++)
269 MSVCRT_mbctype[i + 1] |= _MP;
270 for (i = 166; i <= 223; i++)
271 MSVCRT_mbctype[i + 1] |= _MS;
274 MSVCRT___lc_collate_cp = MSVCRT___lc_codepage = newcp;
275 TRACE("(%d) -> %d\n", cp, MSVCRT___lc_codepage);
279 /*********************************************************************
280 * _getmbcp (MSVCRT.@)
282 int CDECL _getmbcp(void)
284 return MSVCRT___lc_codepage;
287 /*********************************************************************
288 * _mbsnextc(MSVCRT.@)
290 unsigned int CDECL _mbsnextc(const unsigned char* str)
293 return *str << 8 | str[1];
297 /*********************************************************************
298 * _mbctolower(MSVCRT.@)
300 unsigned int CDECL _mbctolower(unsigned int c)
302 if (MSVCRT_isleadbyte(c))
304 FIXME("Handle MBC chars\n");
307 return tolower(c); /* ASCII CP or SB char */
310 /*********************************************************************
311 * _mbctoupper(MSVCRT.@)
313 unsigned int CDECL _mbctoupper(unsigned int c)
315 if (MSVCRT_isleadbyte(c))
317 FIXME("Handle MBC chars\n");
320 return toupper(c); /* ASCII CP or SB char */
323 /*********************************************************************
326 unsigned char* CDECL _mbsdec(const unsigned char* start, const unsigned char* cur)
328 if(MSVCRT___mb_cur_max > 1)
329 return (unsigned char *)(_ismbstrail(start,cur-1) ? cur - 2 : cur -1);
331 return (unsigned char *)cur - 1; /* ASCII CP or SB char */
334 /*********************************************************************
337 unsigned int CDECL _mbclen(const unsigned char* str)
339 return _ismbblead(*str) ? 2 : 1;
342 /*********************************************************************
345 unsigned char* CDECL _mbsinc(const unsigned char* str)
347 return (unsigned char *)(str + _mbclen(str));
350 /*********************************************************************
353 unsigned char* CDECL _mbsninc(const unsigned char* str, MSVCRT_size_t num)
358 while (num > 0 && *str)
360 if (_ismbblead(*str))
370 return (unsigned char*)str;
373 /*********************************************************************
376 MSVCRT_size_t CDECL _mbslen(const unsigned char* str)
378 MSVCRT_size_t len = 0;
381 if (_ismbblead(*str))
384 if (!*str) /* count only full chars */
393 /*********************************************************************
396 void CDECL _mbccpy(unsigned char* dest, const unsigned char* src)
400 *++dest = *++src; /* MB char */
403 /*********************************************************************
406 * The parameter n is the number or characters to copy, not the size of
407 * the buffer. Use _mbsnbcpy for a function analogical to strncpy
409 unsigned char* CDECL _mbsncpy(unsigned char* dst, const unsigned char* src, MSVCRT_size_t n)
411 unsigned char* ret = dst;
414 if (g_mbcp_is_multibyte)
419 if (_ismbblead(*src))
439 if (!(*dst++ = *src++)) break;
442 while (n--) *dst++ = 0;
446 /*********************************************************************
447 * _mbsnbcpy_s(MSVCRT.@)
449 * Unlike _mbsnbcpy this function does not pad the rest of the dest
452 int CDECL _mbsnbcpy_s(unsigned char* dst, MSVCRT_size_t size, const unsigned char* src, MSVCRT_size_t n)
454 MSVCRT_size_t pos = 0;
456 if(!dst || size == 0)
466 if(g_mbcp_is_multibyte)
476 is_lead = (!is_lead && _ismbblead(*src));
481 if (is_lead) /* if string ends with a lead, remove it */
511 /*********************************************************************
512 * _mbsnbcpy(MSVCRT.@)
514 * Like strncpy this function doesn't enforce the string to be
517 unsigned char* CDECL _mbsnbcpy(unsigned char* dst, const unsigned char* src, MSVCRT_size_t n)
519 unsigned char* ret = dst;
522 if(g_mbcp_is_multibyte)
527 is_lead = (!is_lead && _ismbblead(*src));
532 if (is_lead) /* if string ends with a lead, remove it */
540 if (!(*dst++ = *src++)) break;
543 while (n--) *dst++ = 0;
547 /*********************************************************************
550 int CDECL _mbscmp(const unsigned char* str, const unsigned char* cmp)
552 if(MSVCRT___mb_cur_max > 1)
554 unsigned int strc, cmpc;
557 return *cmp ? -1 : 0;
560 strc = _mbsnextc(str);
561 cmpc = _mbsnextc(cmp);
563 return strc < cmpc ? -1 : 1;
564 str +=(strc > 255) ? 2 : 1;
565 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
568 return u_strcmp(str, cmp); /* ASCII CP */
571 /*********************************************************************
572 * _mbsicoll(MSVCRT.@)
573 * FIXME: handle locales.
575 int CDECL _mbsicoll(const unsigned char* str, const unsigned char* cmp)
577 if(MSVCRT___mb_cur_max > 1)
579 unsigned int strc, cmpc;
582 return *cmp ? -1 : 0;
585 strc = _mbctolower(_mbsnextc(str));
586 cmpc = _mbctolower(_mbsnextc(cmp));
588 return strc < cmpc ? -1 : 1;
589 str +=(strc > 255) ? 2 : 1;
590 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
593 return u_strcasecmp(str, cmp); /* ASCII CP */
596 /*********************************************************************
598 * Performs a case-sensitive comparison according to the current code page
600 * _NLSCMPERROR if error
601 * FIXME: handle locales.
603 int CDECL _mbscoll(const unsigned char* str, const unsigned char* cmp)
605 if(MSVCRT___mb_cur_max > 1)
607 unsigned int strc, cmpc;
610 return *cmp ? -1 : 0;
613 strc = _mbsnextc(str);
614 cmpc = _mbsnextc(cmp);
616 return strc < cmpc ? -1 : 1;
617 str +=(strc > 255) ? 2 : 1;
618 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
621 return u_strcmp(str, cmp); /* ASCII CP */
625 /*********************************************************************
628 int CDECL _mbsicmp(const unsigned char* str, const unsigned char* cmp)
630 if(MSVCRT___mb_cur_max > 1)
632 unsigned int strc, cmpc;
635 return *cmp ? -1 : 0;
638 strc = _mbctolower(_mbsnextc(str));
639 cmpc = _mbctolower(_mbsnextc(cmp));
641 return strc < cmpc ? -1 : 1;
642 str +=(strc > 255) ? 2 : 1;
643 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
646 return u_strcasecmp(str, cmp); /* ASCII CP */
649 /*********************************************************************
652 int CDECL _mbsncmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
657 if(MSVCRT___mb_cur_max > 1)
659 unsigned int strc, cmpc;
664 return *cmp ? -1 : 0;
667 strc = _mbsnextc(str);
668 cmpc = _mbsnextc(cmp);
670 return strc < cmpc ? -1 : 1;
671 inc=(strc > 255) ? 2 : 1; /* Equal, use same increment */
675 return 0; /* Matched len chars */
677 return u_strncmp(str, cmp, len); /* ASCII CP */
680 /*********************************************************************
681 * _mbsnbcmp(MSVCRT.@)
683 int CDECL _mbsnbcmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
687 if(MSVCRT___mb_cur_max > 1)
689 unsigned int strc, cmpc;
694 return *cmp ? -1 : 0;
697 if (MSVCRT_isleadbyte(*str))
699 strc=(len>=2)?_mbsnextc(str):0;
707 if (MSVCRT_isleadbyte(*cmp))
708 cmpc=(len>=2)?_mbsnextc(cmp):0;
712 return strc < cmpc ? -1 : 1;
717 return 0; /* Matched len chars */
719 return u_strncmp(str,cmp,len);
722 /*********************************************************************
723 * _mbsnicmp(MSVCRT.@)
725 * Compare two multibyte strings case insensitively to 'len' characters.
727 int CDECL _mbsnicmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
729 /* FIXME: No tolower() for mb strings yet */
730 if(MSVCRT___mb_cur_max > 1)
732 unsigned int strc, cmpc;
736 return *cmp ? -1 : 0;
739 strc = _mbctolower(_mbsnextc(str));
740 cmpc = _mbctolower(_mbsnextc(cmp));
742 return strc < cmpc ? -1 : 1;
743 str +=(strc > 255) ? 2 : 1;
744 cmp +=(strc > 255) ? 2 : 1; /* Equal, use same increment */
746 return 0; /* Matched len chars */
748 return u_strncasecmp(str, cmp, len); /* ASCII CP */
751 /*********************************************************************
752 * _mbsnbicmp(MSVCRT.@)
754 int CDECL _mbsnbicmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
758 if(MSVCRT___mb_cur_max > 1)
760 unsigned int strc, cmpc;
765 return *cmp ? -1 : 0;
768 if (MSVCRT_isleadbyte(*str))
770 strc=(len>=2)?_mbsnextc(str):0;
778 if (MSVCRT_isleadbyte(*cmp))
779 cmpc=(len>=2)?_mbsnextc(cmp):0;
782 strc = _mbctolower(strc);
783 cmpc = _mbctolower(cmpc);
785 return strc < cmpc ? -1 : 1;
790 return 0; /* Matched len bytes */
792 return u_strncasecmp(str,cmp,len);
795 /*********************************************************************
798 unsigned char * CDECL _mbscat( unsigned char *dst, const unsigned char *src )
800 strcat( (char *)dst, (const char *)src );
804 /*********************************************************************
807 unsigned char* CDECL _mbscpy( unsigned char *dst, const unsigned char *src )
809 strcpy( (char *)dst, (const char *)src );
813 /*********************************************************************
816 unsigned char * CDECL _mbsstr(const unsigned char *haystack, const unsigned char *needle)
818 return (unsigned char *)strstr( (const char *)haystack, (const char *)needle );
821 /*********************************************************************
824 * Find a multibyte character in a multibyte string.
826 unsigned char* CDECL _mbschr(const unsigned char* s, unsigned int x)
828 if(MSVCRT___mb_cur_max > 1)
835 return (unsigned char*)s;
838 s += c > 255 ? 2 : 1;
841 return u_strchr(s, x); /* ASCII CP */
844 /*********************************************************************
847 unsigned char* CDECL _mbsrchr(const unsigned char* s, unsigned int x)
849 if(MSVCRT___mb_cur_max > 1)
852 unsigned char* match=NULL;
858 match=(unsigned char*)s;
861 s +=(c > 255) ? 2 : 1;
864 return u_strrchr(s, x);
867 /*********************************************************************
870 * Find and extract tokens from strings
872 unsigned char* CDECL _mbstok(unsigned char *str, const unsigned char *delim)
874 thread_data_t *data = msvcrt_get_thread_data();
877 if(MSVCRT___mb_cur_max > 1)
882 if (!(str = data->mbstok_next)) return NULL;
884 while ((c = _mbsnextc(str)) && _mbschr(delim, c)) {
885 str += c > 255 ? 2 : 1;
887 if (!*str) return NULL;
889 while ((c = _mbsnextc(str)) && !_mbschr(delim, c)) {
890 str += c > 255 ? 2 : 1;
894 if (c > 255) *str++ = 0;
896 data->mbstok_next = str;
899 return u_strtok(str, delim); /* ASCII CP */
902 /*********************************************************************
905 int CDECL MSVCRT_mbtowc(MSVCRT_wchar_t *dst, const char* str, MSVCRT_size_t n)
907 /* temp var needed because MultiByteToWideChar wants non NULL destination */
908 MSVCRT_wchar_t tmpdst = '\0';
912 if(!MultiByteToWideChar(CP_ACP, 0, str, n, &tmpdst, 1))
916 /* return the number of bytes from src that have been used */
919 if(n >= 2 && MSVCRT_isleadbyte(*str) && str[1])
924 /*********************************************************************
925 * _mbbtombc(MSVCRT.@)
927 unsigned int CDECL _mbbtombc(unsigned int c)
929 if(MSVCRT___mb_cur_max > 1 &&
930 ((c >= 0x20 && c <=0x7e) ||(c >= 0xa1 && c <= 0xdf)))
932 /* FIXME: I can't get this function to return anything
933 * different from what I pass it...
936 return c; /* ASCII CP or no MB char */
939 /*********************************************************************
942 int CDECL _mbbtype(unsigned char c, int type)
946 if ((c >= 0x20 && c <= 0x7e) || (c >= 0xa1 && c <= 0xdf))
948 else if ((c >= 0x40 && c <= 0x7e) || (c >= 0x80 && c <= 0xfc))
955 if ((c >= 0x20 && c <= 0x7e) || (c >= 0xa1 && c <= 0xdf))
957 else if ((c >= 0x81 && c <= 0x9f) || (c >= 0xe0 && c <= 0xfc))
964 /*********************************************************************
965 * _ismbbkana(MSVCRT.@)
967 int CDECL _ismbbkana(unsigned int c)
969 /* FIXME: use lc_ctype when supported, not lc_all */
970 if(MSVCRT___lc_codepage == 932)
972 /* Japanese/Katakana, CP 932 */
973 return (c >= 0xa1 && c <= 0xdf);
978 /*********************************************************************
979 * _ismbcdigit(MSVCRT.@)
981 int CDECL _ismbcdigit(unsigned int ch)
983 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
984 return (get_char_typeW( wch ) & C1_DIGIT);
987 /*********************************************************************
988 * _ismbcgraph(MSVCRT.@)
990 int CDECL _ismbcgraph(unsigned int ch)
992 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
993 return (get_char_typeW( wch ) & (C1_UPPER | C1_LOWER | C1_DIGIT | C1_PUNCT | C1_ALPHA));
996 /*********************************************************************
997 * _ismbcalpha (MSVCRT.@)
999 int CDECL _ismbcalpha(unsigned int ch)
1001 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1002 return (get_char_typeW( wch ) & C1_ALPHA);
1005 /*********************************************************************
1006 * _ismbclower (MSVCRT.@)
1008 int CDECL _ismbclower(unsigned int ch)
1010 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1011 return (get_char_typeW( wch ) & C1_UPPER);
1014 /*********************************************************************
1015 * _ismbcupper (MSVCRT.@)
1017 int CDECL _ismbcupper(unsigned int ch)
1019 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1020 return (get_char_typeW( wch ) & C1_LOWER);
1023 /*********************************************************************
1024 * _ismbcsymbol(MSVCRT.@)
1026 int CDECL _ismbcsymbol(unsigned int ch)
1028 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1030 if (!GetStringTypeW(CT_CTYPE3, &wch, 1, &ctype))
1032 WARN("GetStringTypeW failed on %x\n", ch);
1035 return ((ctype & C3_SYMBOL) != 0);
1038 /*********************************************************************
1039 * _ismbcalnum (MSVCRT.@)
1041 int CDECL _ismbcalnum(unsigned int ch)
1043 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1044 return (get_char_typeW( wch ) & (C1_ALPHA | C1_DIGIT));
1047 /*********************************************************************
1048 * _ismbcspace (MSVCRT.@)
1050 int CDECL _ismbcspace(unsigned int ch)
1052 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1053 return (get_char_typeW( wch ) & C1_SPACE);
1056 /*********************************************************************
1057 * _ismbcprint (MSVCRT.@)
1059 int CDECL _ismbcprint(unsigned int ch)
1061 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1062 return (get_char_typeW( wch ) & (C1_UPPER | C1_LOWER | C1_DIGIT | C1_PUNCT | C1_ALPHA | C1_SPACE));
1065 /*********************************************************************
1066 * _ismbcpunct(MSVCRT.@)
1068 int CDECL _ismbcpunct(unsigned int ch)
1070 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1071 return (get_char_typeW( wch ) & C1_PUNCT);
1074 /*********************************************************************
1075 * _ismbchira(MSVCRT.@)
1077 int CDECL _ismbchira(unsigned int c)
1079 /* FIXME: use lc_ctype when supported, not lc_all */
1080 if(MSVCRT___lc_codepage == 932)
1082 /* Japanese/Hiragana, CP 932 */
1083 return (c >= 0x829f && c <= 0x82f1);
1088 /*********************************************************************
1089 * _ismbckata(MSVCRT.@)
1091 int CDECL _ismbckata(unsigned int c)
1093 /* FIXME: use lc_ctype when supported, not lc_all */
1094 if(MSVCRT___lc_codepage == 932)
1097 return _ismbbkana(c);
1098 /* Japanese/Katakana, CP 932 */
1099 return (c >= 0x8340 && c <= 0x8396 && c != 0x837f);
1104 /*********************************************************************
1105 * _ismbblead(MSVCRT.@)
1107 int CDECL _ismbblead(unsigned int c)
1109 return (MSVCRT_mbctype[(c&0xff) + 1] & _M1) != 0;
1113 /*********************************************************************
1114 * _ismbbtrail(MSVCRT.@)
1116 int CDECL _ismbbtrail(unsigned int c)
1118 return (MSVCRT_mbctype[(c&0xff) + 1] & _M2) != 0;
1121 /*********************************************************************
1122 * _ismbslead(MSVCRT.@)
1124 int CDECL _ismbslead(const unsigned char* start, const unsigned char* str)
1128 if(!g_mbcp_is_multibyte)
1131 /* Lead bytes can also be trail bytes so we need to analyse the string
1133 while (start <= str)
1137 lead = !lead && _ismbblead(*start);
1141 return lead ? -1 : 0;
1144 /*********************************************************************
1145 * _ismbstrail(MSVCRT.@)
1147 int CDECL _ismbstrail(const unsigned char* start, const unsigned char* str)
1149 /* Note: this function doesn't check _ismbbtrail */
1150 if ((str > start) && _ismbslead(start, str-1))
1156 /*********************************************************************
1157 * _mbsbtype (MSVCRT.@)
1159 int CDECL _mbsbtype(const unsigned char *str, MSVCRT_size_t count)
1162 const unsigned char *end = str + count;
1163 int mbcp = g_mbcp_is_multibyte;
1165 /* Lead bytes can also be trail bytes so we need to analyse the string.
1166 * Also we must return _MBC_ILLEGAL for chars past the end of the string
1168 while (str < end) /* Note: we skip the last byte - will check after the loop */
1171 return _MBC_ILLEGAL;
1172 lead = mbcp && !lead && _ismbblead(*str);
1177 if (_ismbbtrail(*str))
1180 return _MBC_ILLEGAL;
1182 if (_ismbblead(*str))
1188 /*********************************************************************
1191 unsigned char* CDECL _mbsset(unsigned char* str, unsigned int c)
1193 unsigned char* ret = str;
1195 if(MSVCRT___mb_cur_max == 1 || c < 256)
1196 return u__strset(str, c); /* ASCII CP or SB char */
1198 c &= 0xffff; /* Strip high bits */
1200 while(str[0] && str[1])
1206 str[0] = '\0'; /* FIXME: OK to shorten? */
1211 /*********************************************************************
1212 * _mbsnbset(MSVCRT.@)
1214 unsigned char* CDECL _mbsnbset(unsigned char *str, unsigned int c, MSVCRT_size_t len)
1216 unsigned char *ret = str;
1221 if(MSVCRT___mb_cur_max == 1 || c < 256)
1222 return u__strnset(str, c, len); /* ASCII CP or SB char */
1224 c &= 0xffff; /* Strip high bits */
1226 while(str[0] && str[1] && (len > 1))
1234 /* as per msdn pad with a blank character */
1241 /*********************************************************************
1242 * _mbsnset(MSVCRT.@)
1244 unsigned char* CDECL _mbsnset(unsigned char* str, unsigned int c, MSVCRT_size_t len)
1246 unsigned char *ret = str;
1251 if(MSVCRT___mb_cur_max == 1 || c < 256)
1252 return u__strnset(str, c, len); /* ASCII CP or SB char */
1254 c &= 0xffff; /* Strip high bits */
1256 while(str[0] && str[1] && len--)
1262 str[0] = '\0'; /* FIXME: OK to shorten? */
1267 /*********************************************************************
1268 * _mbsnccnt(MSVCRT.@)
1269 * 'c' is for 'character'.
1271 MSVCRT_size_t CDECL _mbsnccnt(const unsigned char* str, MSVCRT_size_t len)
1274 if(MSVCRT___mb_cur_max > 1)
1277 while(*str && len-- > 0)
1279 if(MSVCRT_isleadbyte(*str))
1292 return min(ret, len); /* ASCII CP */
1295 /*********************************************************************
1296 * _mbsnbcnt(MSVCRT.@)
1297 * 'b' is for byte count.
1299 MSVCRT_size_t CDECL _mbsnbcnt(const unsigned char* str, MSVCRT_size_t len)
1302 if(MSVCRT___mb_cur_max > 1)
1304 const unsigned char* xstr = str;
1305 while(*xstr && len-- > 0)
1307 if (MSVCRT_isleadbyte(*xstr++))
1313 return min(ret, len); /* ASCII CP */
1316 /*********************************************************************
1317 * _mbsnbcat(MSVCRT.@)
1319 unsigned char* CDECL _mbsnbcat(unsigned char* dst, const unsigned char* src, MSVCRT_size_t len)
1321 if(MSVCRT___mb_cur_max > 1)
1323 unsigned char *res = dst;
1325 if (MSVCRT_isleadbyte(*dst++)) {
1329 /* as per msdn overwrite the lead byte in front of '\0' */
1335 while (*src && len--) *dst++ = *src++;
1339 return u_strncat(dst, src, len); /* ASCII CP */
1343 /*********************************************************************
1344 * _mbsncat(MSVCRT.@)
1346 unsigned char* CDECL _mbsncat(unsigned char* dst, const unsigned char* src, MSVCRT_size_t len)
1348 if(MSVCRT___mb_cur_max > 1)
1350 unsigned char *res = dst;
1353 if (MSVCRT_isleadbyte(*dst++))
1356 while (*src && len--)
1359 if(MSVCRT_isleadbyte(*src++))
1365 return u_strncat(dst, src, len); /* ASCII CP */
1369 /*********************************************************************
1372 unsigned char* CDECL _mbslwr(unsigned char* s)
1374 unsigned char *ret = s;
1377 if (MSVCRT___mb_cur_max > 1)
1382 c = _mbctolower(_mbsnextc(s));
1383 /* Note that I assume that the size of the character is unchanged */
1392 else for ( ; *s; s++) *s = tolower(*s);
1397 /*********************************************************************
1400 unsigned char* CDECL _mbsupr(unsigned char* s)
1402 unsigned char *ret = s;
1405 if (MSVCRT___mb_cur_max > 1)
1410 c = _mbctoupper(_mbsnextc(s));
1411 /* Note that I assume that the size of the character is unchanged */
1420 else for ( ; *s; s++) *s = toupper(*s);
1425 /*********************************************************************
1426 * _mbsspn (MSVCRT.@)
1428 MSVCRT_size_t CDECL _mbsspn(const unsigned char* string, const unsigned char* set)
1430 const unsigned char *p, *q;
1432 for (p = string; *p; p++)
1434 if (MSVCRT_isleadbyte(*p))
1436 for (q = set; *q; q++)
1440 if ((*p == *q) && (p[1] == q[1]))
1444 if (!q[0] || !q[1]) break;
1448 for (q = set; *q; q++)
1457 /*********************************************************************
1458 * _mbsspnp (MSVCRT.@)
1460 unsigned char* CDECL _mbsspnp(const unsigned char* string, const unsigned char* set)
1462 const unsigned char *p, *q;
1464 for (p = string; *p; p++)
1466 if (MSVCRT_isleadbyte(*p))
1468 for (q = set; *q; q++)
1472 if ((*p == *q) && (p[1] == q[1]))
1476 if (!q[0] || !q[1]) break;
1480 for (q = set; *q; q++)
1488 return (unsigned char *)p;
1491 /*********************************************************************
1492 * _mbscspn(MSVCRT.@)
1494 MSVCRT_size_t CDECL _mbscspn(const unsigned char* str, const unsigned char* cmp)
1496 if (MSVCRT___mb_cur_max > 1)
1497 FIXME("don't handle double character case\n");
1498 return u_strcspn(str, cmp);
1501 /*********************************************************************
1502 * _mbsrev (MSVCRT.@)
1504 unsigned char* CDECL _mbsrev(unsigned char* str)
1506 int i, len = _mbslen(str);
1507 unsigned char *p, *temp=MSVCRT_malloc(len*2);
1512 /* unpack multibyte string to temp buffer */
1514 for(i=0; i<len; i++)
1516 if (MSVCRT_isleadbyte(*p))
1528 /* repack it in the reverse order */
1530 for(i=len-1; i>=0; i--)
1532 if(MSVCRT_isleadbyte(temp[i*2]))
1548 /*********************************************************************
1549 * _mbspbrk (MSVCRT.@)
1551 unsigned char* CDECL _mbspbrk(const unsigned char* str, const unsigned char* accept)
1553 const unsigned char* p;
1557 for(p = accept; *p; p += (MSVCRT_isleadbyte(*p)?2:1) )
1560 if( !MSVCRT_isleadbyte(*p) || ( *(p+1) == *(str+1) ) )
1561 return (unsigned char*)str;
1563 str += (MSVCRT_isleadbyte(*str)?2:1);
1570 * Functions depending on locale codepage
1573 /*********************************************************************
1576 * Unlike most of the multibyte string functions this function uses
1577 * the locale codepage, not the codepage set by _setmbcp
1579 int CDECL MSVCRT_mblen(const char* str, MSVCRT_size_t size)
1581 if (str && *str && size)
1583 if(MSVCRT___mb_cur_max == 1)
1584 return 1; /* ASCII CP */
1586 return !MSVCRT_isleadbyte(*str) ? 1 : (size>1 ? 2 : -1);
1591 /*********************************************************************
1592 * _mbstrlen(MSVCRT.@)
1594 * Unlike most of the multibyte string functions this function uses
1595 * the locale codepage, not the codepage set by _setmbcp
1597 MSVCRT_size_t CDECL _mbstrlen(const char* str)
1599 if(MSVCRT___mb_cur_max > 1)
1601 MSVCRT_size_t len = 0;
1604 /* FIXME: According to the documentation we are supposed to test for
1605 * multi-byte character validity. Whatever that means
1607 str += MSVCRT_isleadbyte(*str) ? 2 : 1;
1612 return strlen(str); /* ASCII CP */