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/mbstring.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 char* CDECL _mbsinc(const unsigned char* str)
339 return (unsigned char *)(str + _mbclen(str));
342 /*********************************************************************
345 unsigned char* CDECL _mbsninc(const unsigned char* str, MSVCRT_size_t num)
350 while (num > 0 && *str)
352 if (_ismbblead(*str))
362 return (unsigned char*)str;
365 /*********************************************************************
368 unsigned int CDECL _mbclen(const unsigned char* str)
370 return _ismbblead(*str) ? 2 : 1;
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(MSVCRT.@)
449 * Like strncpy this function doesn't enforce the string to be
452 unsigned char* CDECL _mbsnbcpy(unsigned char* dst, const unsigned char* src, MSVCRT_size_t n)
454 unsigned char* ret = dst;
457 if(g_mbcp_is_multibyte)
462 is_lead = (!is_lead && _ismbblead(*src));
467 if (is_lead) /* if string ends with a lead, remove it */
475 if (!(*dst++ = *src++)) break;
478 while (n--) *dst++ = 0;
482 /*********************************************************************
485 int CDECL _mbscmp(const unsigned char* str, const unsigned char* cmp)
487 if(MSVCRT___mb_cur_max > 1)
489 unsigned int strc, cmpc;
492 return *cmp ? -1 : 0;
495 strc = _mbsnextc(str);
496 cmpc = _mbsnextc(cmp);
498 return strc < cmpc ? -1 : 1;
499 str +=(strc > 255) ? 2 : 1;
500 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
503 return u_strcmp(str, cmp); /* ASCII CP */
506 /*********************************************************************
507 * _mbsicoll(MSVCRT.@)
508 * FIXME: handle locales.
510 int CDECL _mbsicoll(const unsigned char* str, const unsigned char* cmp)
512 if(MSVCRT___mb_cur_max > 1)
514 unsigned int strc, cmpc;
517 return *cmp ? -1 : 0;
520 strc = _mbctolower(_mbsnextc(str));
521 cmpc = _mbctolower(_mbsnextc(cmp));
523 return strc < cmpc ? -1 : 1;
524 str +=(strc > 255) ? 2 : 1;
525 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
528 return u_strcasecmp(str, cmp); /* ASCII CP */
531 /*********************************************************************
533 * Performs a case-sensitive comparison according to the current code page
535 * _NLSCMPERROR if error
536 * FIXME: handle locales.
538 int CDECL _mbscoll(const unsigned char* str, const unsigned char* cmp)
540 if(MSVCRT___mb_cur_max > 1)
542 unsigned int strc, cmpc;
545 return *cmp ? -1 : 0;
548 strc = _mbsnextc(str);
549 cmpc = _mbsnextc(cmp);
551 return strc < cmpc ? -1 : 1;
552 str +=(strc > 255) ? 2 : 1;
553 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
556 return u_strcmp(str, cmp); /* ASCII CP */
560 /*********************************************************************
563 int CDECL _mbsicmp(const unsigned char* str, const unsigned char* cmp)
565 if(MSVCRT___mb_cur_max > 1)
567 unsigned int strc, cmpc;
570 return *cmp ? -1 : 0;
573 strc = _mbctolower(_mbsnextc(str));
574 cmpc = _mbctolower(_mbsnextc(cmp));
576 return strc < cmpc ? -1 : 1;
577 str +=(strc > 255) ? 2 : 1;
578 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
581 return u_strcasecmp(str, cmp); /* ASCII CP */
584 /*********************************************************************
587 int CDECL _mbsncmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
592 if(MSVCRT___mb_cur_max > 1)
594 unsigned int strc, cmpc;
599 return *cmp ? -1 : 0;
602 strc = _mbsnextc(str);
603 cmpc = _mbsnextc(cmp);
605 return strc < cmpc ? -1 : 1;
606 inc=(strc > 255) ? 2 : 1; /* Equal, use same increment */
610 return 0; /* Matched len chars */
612 return u_strncmp(str, cmp, len); /* ASCII CP */
615 /*********************************************************************
616 * _mbsnbcmp(MSVCRT.@)
618 int CDECL _mbsnbcmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
622 if(MSVCRT___mb_cur_max > 1)
624 unsigned int strc, cmpc;
629 return *cmp ? -1 : 0;
632 if (MSVCRT_isleadbyte(*str))
634 strc=(len>=2)?_mbsnextc(str):0;
642 if (MSVCRT_isleadbyte(*cmp))
643 cmpc=(len>=2)?_mbsnextc(cmp):0;
647 return strc < cmpc ? -1 : 1;
652 return 0; /* Matched len chars */
654 return u_strncmp(str,cmp,len);
657 /*********************************************************************
658 * _mbsnicmp(MSVCRT.@)
660 * Compare two multibyte strings case insensitively to 'len' characters.
662 int CDECL _mbsnicmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
664 /* FIXME: No tolower() for mb strings yet */
665 if(MSVCRT___mb_cur_max > 1)
667 unsigned int strc, cmpc;
671 return *cmp ? -1 : 0;
674 strc = _mbctolower(_mbsnextc(str));
675 cmpc = _mbctolower(_mbsnextc(cmp));
677 return strc < cmpc ? -1 : 1;
678 str +=(strc > 255) ? 2 : 1;
679 cmp +=(strc > 255) ? 2 : 1; /* Equal, use same increment */
681 return 0; /* Matched len chars */
683 return u_strncasecmp(str, cmp, len); /* ASCII CP */
686 /*********************************************************************
687 * _mbsnbicmp(MSVCRT.@)
689 int CDECL _mbsnbicmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
693 if(MSVCRT___mb_cur_max > 1)
695 unsigned int strc, cmpc;
700 return *cmp ? -1 : 0;
703 if (MSVCRT_isleadbyte(*str))
705 strc=(len>=2)?_mbsnextc(str):0;
713 if (MSVCRT_isleadbyte(*cmp))
714 cmpc=(len>=2)?_mbsnextc(cmp):0;
717 strc = _mbctolower(strc);
718 cmpc = _mbctolower(cmpc);
720 return strc < cmpc ? -1 : 1;
725 return 0; /* Matched len bytes */
727 return u_strncasecmp(str,cmp,len);
730 /*********************************************************************
733 unsigned char * CDECL _mbscat( unsigned char *dst, const unsigned char *src )
735 strcat( (char *)dst, (const char *)src );
739 /*********************************************************************
742 unsigned char* CDECL _mbscpy( unsigned char *dst, const unsigned char *src )
744 strcpy( (char *)dst, (const char *)src );
748 /*********************************************************************
751 unsigned char * CDECL _mbsstr(const unsigned char *haystack, const unsigned char *needle)
753 return (unsigned char *)strstr( (const char *)haystack, (const char *)needle );
756 /*********************************************************************
759 * Find a multibyte character in a multibyte string.
761 unsigned char* CDECL _mbschr(const unsigned char* s, unsigned int x)
763 if(MSVCRT___mb_cur_max > 1)
770 return (unsigned char*)s;
773 s += c > 255 ? 2 : 1;
776 return u_strchr(s, x); /* ASCII CP */
779 /*********************************************************************
782 unsigned char* CDECL _mbsrchr(const unsigned char* s, unsigned int x)
784 if(MSVCRT___mb_cur_max > 1)
787 unsigned char* match=NULL;
793 match=(unsigned char*)s;
796 s +=(c > 255) ? 2 : 1;
799 return u_strrchr(s, x);
802 /*********************************************************************
805 * Find and extract tokens from strings
807 unsigned char* CDECL _mbstok(unsigned char *str, const unsigned char *delim)
809 thread_data_t *data = msvcrt_get_thread_data();
812 if(MSVCRT___mb_cur_max > 1)
817 if (!(str = data->mbstok_next)) return NULL;
819 while ((c = _mbsnextc(str)) && _mbschr(delim, c)) {
820 str += c > 255 ? 2 : 1;
822 if (!*str) return NULL;
824 while ((c = _mbsnextc(str)) && !_mbschr(delim, c)) {
825 str += c > 255 ? 2 : 1;
829 if (c > 255) *str++ = 0;
831 data->mbstok_next = str;
834 return u_strtok(str, delim); /* ASCII CP */
837 /*********************************************************************
840 int CDECL MSVCRT_mbtowc(MSVCRT_wchar_t *dst, const char* str, MSVCRT_size_t n)
842 /* temp var needed because MultiByteToWideChar wants non NULL destination */
843 MSVCRT_wchar_t tmpdst = '\0';
847 if(!MultiByteToWideChar(CP_ACP, 0, str, n, &tmpdst, 1))
851 /* return the number of bytes from src that have been used */
854 if(n >= 2 && MSVCRT_isleadbyte(*str) && str[1])
859 /*********************************************************************
860 * _mbbtombc(MSVCRT.@)
862 unsigned int CDECL _mbbtombc(unsigned int c)
864 if(MSVCRT___mb_cur_max > 1 &&
865 ((c >= 0x20 && c <=0x7e) ||(c >= 0xa1 && c <= 0xdf)))
867 /* FIXME: I can't get this function to return anything
868 * different from what I pass it...
871 return c; /* ASCII CP or no MB char */
874 /*********************************************************************
877 int CDECL _mbbtype(unsigned char c, int type)
881 if ((c >= 0x20 && c <= 0x7e) || (c >= 0xa1 && c <= 0xdf))
883 else if ((c >= 0x40 && c <= 0x7e) || (c >= 0x80 && c <= 0xfc))
890 if ((c >= 0x20 && c <= 0x7e) || (c >= 0xa1 && c <= 0xdf))
892 else if ((c >= 0x81 && c <= 0x9f) || (c >= 0xe0 && c <= 0xfc))
899 /*********************************************************************
900 * _ismbbkana(MSVCRT.@)
902 int CDECL _ismbbkana(unsigned int c)
904 /* FIXME: use lc_ctype when supported, not lc_all */
905 if(MSVCRT___lc_codepage == 932)
907 /* Japanese/Katakana, CP 932 */
908 return (c >= 0xa1 && c <= 0xdf);
913 /*********************************************************************
914 * _ismbcdigit(MSVCRT.@)
916 int CDECL _ismbcdigit(unsigned int ch)
918 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
919 return (get_char_typeW( wch ) & C1_DIGIT);
922 /*********************************************************************
923 * _ismbcgraph(MSVCRT.@)
925 int CDECL _ismbcgraph(unsigned int ch)
927 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
928 return (get_char_typeW( wch ) & (C1_UPPER | C1_LOWER | C1_DIGIT | C1_PUNCT | C1_ALPHA));
931 /*********************************************************************
932 * _ismbcalpha (MSVCRT.@)
934 int CDECL _ismbcalpha(unsigned int ch)
936 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
937 return (get_char_typeW( wch ) & C1_ALPHA);
940 /*********************************************************************
941 * _ismbclower (MSVCRT.@)
943 int CDECL _ismbclower(unsigned int ch)
945 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
946 return (get_char_typeW( wch ) & C1_UPPER);
949 /*********************************************************************
950 * _ismbcupper (MSVCRT.@)
952 int CDECL _ismbcupper(unsigned int ch)
954 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
955 return (get_char_typeW( wch ) & C1_LOWER);
958 /*********************************************************************
959 * _ismbcsymbol(MSVCRT.@)
961 int CDECL _ismbcsymbol(unsigned int ch)
963 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
965 if (!GetStringTypeW(CT_CTYPE3, &wch, 1, &ctype))
967 WARN("GetStringTypeW failed on %x\n", ch);
970 return ((ctype & C3_SYMBOL) != 0);
973 /*********************************************************************
974 * _ismbcalnum (MSVCRT.@)
976 int CDECL _ismbcalnum(unsigned int ch)
978 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
979 return (get_char_typeW( wch ) & (C1_ALPHA | C1_DIGIT));
982 /*********************************************************************
983 * _ismbcspace (MSVCRT.@)
985 int CDECL _ismbcspace(unsigned int ch)
987 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
988 return (get_char_typeW( wch ) & C1_SPACE);
991 /*********************************************************************
992 * _ismbcprint (MSVCRT.@)
994 int CDECL _ismbcprint(unsigned int ch)
996 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
997 return (get_char_typeW( wch ) & (C1_UPPER | C1_LOWER | C1_DIGIT | C1_PUNCT | C1_ALPHA | C1_SPACE));
1000 /*********************************************************************
1001 * _ismbcpunct(MSVCRT.@)
1003 int CDECL _ismbcpunct(unsigned int ch)
1005 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1006 return (get_char_typeW( wch ) & C1_PUNCT);
1009 /*********************************************************************
1010 * _ismbchira(MSVCRT.@)
1012 int CDECL _ismbchira(unsigned int c)
1014 /* FIXME: use lc_ctype when supported, not lc_all */
1015 if(MSVCRT___lc_codepage == 932)
1017 /* Japanese/Hiragana, CP 932 */
1018 return (c >= 0x829f && c <= 0x82f1);
1023 /*********************************************************************
1024 * _ismbckata(MSVCRT.@)
1026 int CDECL _ismbckata(unsigned int c)
1028 /* FIXME: use lc_ctype when supported, not lc_all */
1029 if(MSVCRT___lc_codepage == 932)
1032 return _ismbbkana(c);
1033 /* Japanese/Katakana, CP 932 */
1034 return (c >= 0x8340 && c <= 0x8396 && c != 0x837f);
1039 /*********************************************************************
1040 * _ismbblead(MSVCRT.@)
1042 int CDECL _ismbblead(unsigned int c)
1044 return (MSVCRT_mbctype[(c&0xff) + 1] & _M1) != 0;
1048 /*********************************************************************
1049 * _ismbbtrail(MSVCRT.@)
1051 int CDECL _ismbbtrail(unsigned int c)
1053 return (MSVCRT_mbctype[(c&0xff) + 1] & _M2) != 0;
1056 /*********************************************************************
1057 * _ismbslead(MSVCRT.@)
1059 int CDECL _ismbslead(const unsigned char* start, const unsigned char* str)
1063 if(!g_mbcp_is_multibyte)
1066 /* Lead bytes can also be trail bytes so we need to analise the string
1068 while (start <= str)
1072 lead = !lead && _ismbblead(*start);
1076 return lead ? -1 : 0;
1079 /*********************************************************************
1080 * _ismbstrail(MSVCRT.@)
1082 int CDECL _ismbstrail(const unsigned char* start, const unsigned char* str)
1084 /* Note: this function doesn't check _ismbbtrail */
1085 if ((str > start) && _ismbslead(start, str-1))
1091 /*********************************************************************
1092 * _mbsbtype (MSVCRT.@)
1094 int CDECL _mbsbtype(const unsigned char *str, MSVCRT_size_t count)
1097 const unsigned char *end = str + count;
1098 int mbcp = g_mbcp_is_multibyte;
1100 /* Lead bytes can also be trail bytes so we need to analyse the string.
1101 * Also we must return _MBC_ILLEGAL for chars past the end of the string
1103 while (str < end) /* Note: we skip the last byte - will check after the loop */
1106 return _MBC_ILLEGAL;
1107 lead = mbcp && !lead && _ismbblead(*str);
1112 if (_ismbbtrail(*str))
1115 return _MBC_ILLEGAL;
1117 if (_ismbblead(*str))
1123 /*********************************************************************
1126 unsigned char* CDECL _mbsset(unsigned char* str, unsigned int c)
1128 unsigned char* ret = str;
1130 if(MSVCRT___mb_cur_max == 1 || c < 256)
1131 return u__strset(str, c); /* ASCII CP or SB char */
1133 c &= 0xffff; /* Strip high bits */
1135 while(str[0] && str[1])
1141 str[0] = '\0'; /* FIXME: OK to shorten? */
1146 /*********************************************************************
1147 * _mbsnbset(MSVCRT.@)
1149 unsigned char* CDECL _mbsnbset(unsigned char *str, unsigned int c, MSVCRT_size_t len)
1151 unsigned char *ret = str;
1156 if(MSVCRT___mb_cur_max == 1 || c < 256)
1157 return u__strnset(str, c, len); /* ASCII CP or SB char */
1159 c &= 0xffff; /* Strip high bits */
1161 while(str[0] && str[1] && (len > 1))
1169 /* as per msdn pad with a blank character */
1176 /*********************************************************************
1177 * _mbsnset(MSVCRT.@)
1179 unsigned char* CDECL _mbsnset(unsigned char* str, unsigned int c, MSVCRT_size_t len)
1181 unsigned char *ret = str;
1186 if(MSVCRT___mb_cur_max == 1 || c < 256)
1187 return u__strnset(str, c, len); /* ASCII CP or SB char */
1189 c &= 0xffff; /* Strip high bits */
1191 while(str[0] && str[1] && len--)
1197 str[0] = '\0'; /* FIXME: OK to shorten? */
1202 /*********************************************************************
1203 * _mbsnccnt(MSVCRT.@)
1204 * 'c' is for 'character'.
1206 MSVCRT_size_t CDECL _mbsnccnt(const unsigned char* str, MSVCRT_size_t len)
1209 if(MSVCRT___mb_cur_max > 1)
1212 while(*str && len-- > 0)
1214 if(MSVCRT_isleadbyte(*str))
1227 return min(ret, len); /* ASCII CP */
1230 /*********************************************************************
1231 * _mbsnbcnt(MSVCRT.@)
1232 * 'b' is for byte count.
1234 MSVCRT_size_t CDECL _mbsnbcnt(const unsigned char* str, MSVCRT_size_t len)
1237 if(MSVCRT___mb_cur_max > 1)
1239 const unsigned char* xstr = str;
1240 while(*xstr && len-- > 0)
1242 if (MSVCRT_isleadbyte(*xstr++))
1248 return min(ret, len); /* ASCII CP */
1251 /*********************************************************************
1252 * _mbsnbcat(MSVCRT.@)
1254 unsigned char* CDECL _mbsnbcat(unsigned char* dst, const unsigned char* src, MSVCRT_size_t len)
1256 if(MSVCRT___mb_cur_max > 1)
1258 unsigned char *res = dst;
1260 if (MSVCRT_isleadbyte(*dst++)) {
1264 /* as per msdn overwrite the lead byte in front of '\0' */
1270 while (*src && len--) *dst++ = *src++;
1274 return u_strncat(dst, src, len); /* ASCII CP */
1278 /*********************************************************************
1279 * _mbsncat(MSVCRT.@)
1281 unsigned char* CDECL _mbsncat(unsigned char* dst, const unsigned char* src, MSVCRT_size_t len)
1283 if(MSVCRT___mb_cur_max > 1)
1285 unsigned char *res = dst;
1288 if (MSVCRT_isleadbyte(*dst++))
1291 while (*src && len--)
1294 if(MSVCRT_isleadbyte(*src++))
1300 return u_strncat(dst, src, len); /* ASCII CP */
1304 /*********************************************************************
1307 unsigned char* CDECL _mbslwr(unsigned char* s)
1309 unsigned char *ret = s;
1312 if (MSVCRT___mb_cur_max > 1)
1317 c = _mbctolower(_mbsnextc(s));
1318 /* Note that I assume that the size of the character is unchanged */
1327 else for ( ; *s; s++) *s = tolower(*s);
1332 /*********************************************************************
1335 unsigned char* CDECL _mbsupr(unsigned char* s)
1337 unsigned char *ret = s;
1340 if (MSVCRT___mb_cur_max > 1)
1345 c = _mbctoupper(_mbsnextc(s));
1346 /* Note that I assume that the size of the character is unchanged */
1355 else for ( ; *s; s++) *s = toupper(*s);
1360 /*********************************************************************
1361 * _mbsspn (MSVCRT.@)
1363 MSVCRT_size_t CDECL _mbsspn(const unsigned char* string, const unsigned char* set)
1365 const unsigned char *p, *q;
1367 for (p = string; *p; p++)
1369 if (MSVCRT_isleadbyte(*p))
1371 for (q = set; *q; q++)
1375 if ((*p == *q) && (p[1] == q[1]))
1379 if (!q[0] || !q[1]) break;
1383 for (q = set; *q; q++)
1392 /*********************************************************************
1393 * _mbsspnp (MSVCRT.@)
1395 unsigned char* CDECL _mbsspnp(const unsigned char* string, const unsigned char* set)
1397 const unsigned char *p, *q;
1399 for (p = string; *p; p++)
1401 if (MSVCRT_isleadbyte(*p))
1403 for (q = set; *q; q++)
1407 if ((*p == *q) && (p[1] == q[1]))
1411 if (!q[0] || !q[1]) break;
1415 for (q = set; *q; q++)
1423 return (unsigned char *)p;
1426 /*********************************************************************
1427 * _mbscspn(MSVCRT.@)
1429 MSVCRT_size_t CDECL _mbscspn(const unsigned char* str, const unsigned char* cmp)
1431 if (MSVCRT___mb_cur_max > 1)
1432 FIXME("don't handle double character case\n");
1433 return u_strcspn(str, cmp);
1436 /*********************************************************************
1437 * _mbsrev (MSVCRT.@)
1439 unsigned char* CDECL _mbsrev(unsigned char* str)
1441 int i, len = _mbslen(str);
1442 unsigned char *p, *temp=MSVCRT_malloc(len*2);
1447 /* unpack multibyte string to temp buffer */
1449 for(i=0; i<len; i++)
1451 if (MSVCRT_isleadbyte(*p))
1463 /* repack it in the reverse order */
1465 for(i=len-1; i>=0; i--)
1467 if(MSVCRT_isleadbyte(temp[i*2]))
1483 /*********************************************************************
1484 * _mbspbrk (MSVCRT.@)
1486 unsigned char* CDECL _mbspbrk(const unsigned char* str, const unsigned char* accept)
1488 const unsigned char* p;
1492 for(p = accept; *p; p += (MSVCRT_isleadbyte(*p)?2:1) )
1495 if( !MSVCRT_isleadbyte(*p) || ( *(p+1) == *(str+1) ) )
1496 return (unsigned char*)str;
1498 str += (MSVCRT_isleadbyte(*str)?2:1);
1505 * Functions depending on locale codepage
1508 /*********************************************************************
1511 * Unlike most of the multibyte string functions this function uses
1512 * the locale codepage, not the codepage set by _setmbcp
1514 int CDECL MSVCRT_mblen(const char* str, MSVCRT_size_t size)
1516 if (str && *str && size)
1518 if(MSVCRT___mb_cur_max == 1)
1519 return 1; /* ASCII CP */
1521 return !MSVCRT_isleadbyte(*str) ? 1 : (size>1 ? 2 : -1);
1526 /*********************************************************************
1527 * _mbstrlen(MSVCRT.@)
1529 * Unlike most of the multibyte string functions this function uses
1530 * the locale codepage, not the codepage set by _setmbcp
1532 MSVCRT_size_t CDECL _mbstrlen(const char* str)
1534 if(MSVCRT___mb_cur_max > 1)
1536 MSVCRT_size_t len = 0;
1539 /* FIXME: According to the documentation we are supposed to test for
1540 * multi-byte character validity. Whatever that means
1542 str += MSVCRT_isleadbyte(*str) ? 2 : 1;
1547 return strlen(str); /* ASCII CP */