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.
29 #include "wine/unicode.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt
);
34 unsigned char MSVCRT_mbctype
[257] = { 0 };
36 /* It seems that the data about valid trail bytes is not available from kernel32
37 * so we have to store is here. The format is the same as for lead bytes in CPINFO */
38 struct cp_extra_info_t
41 BYTE TrailBytes
[MAX_LEADBYTES
];
44 static struct cp_extra_info_t g_cpextrainfo
[] =
46 {932, {0x40, 0x7e, 0x80, 0xfc, 0, 0}},
47 {936, {0x40, 0xfe, 0, 0}},
48 {949, {0x41, 0xfe, 0, 0}},
49 {950, {0x40, 0x7e, 0xa1, 0xfe, 0, 0}},
50 {1361, {0x31, 0x7e, 0x81, 0xfe, 0, 0}},
51 {20932, {1, 255, 0, 0}}, /* seems to give different results on different systems */
52 {0, {1, 255, 0, 0}} /* match all with FIXME */
55 /* Maps multibyte cp932 punctuation marks to single byte equivalents */
56 static const unsigned char mbctombb_932_punct
[] = {
57 0x20,0xa4,0xa1,0x2c,0x2e,0xa5,0x3a,0x3b,0x3f,0x21,0xde,0xdf,0x00,0x00,0x00,0x5e,
58 0x7e,0x5f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xb0,0x00,0x00,0x2f,0x00,
59 0x00,0x00,0x7c,0x00,0x00,0x60,0x27,0x00,0x22,0x28,0x29,0x00,0x00,0x5b,0x5d,0x7b,
60 0x7d,0x00,0x00,0x00,0x00,0xa2,0xa3,0x00,0x00,0x00,0x00,0x2b,0x2d,0x00,0x00,0x00,
61 0x00,0x3d,0x00,0x3c,0x3e,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x5c,
62 0x24,0x00,0x00,0x25,0x23,0x26,0x2a,0x40};
64 /* Maps multibyte cp932 hiragana/katakana to single-byte equivalents */
65 static const unsigned char mbctombb_932_kana
[] = {
66 0xa7,0xb1,0xa8,0xb2,0xa9,0xb3,0xaa,0xb4,0xab,0xb5,0xb6,0xb6,0xb7,0xb7,0xb8,0xb8,
67 0xb9,0xb9,0xba,0xba,0xbb,0xbb,0xbc,0xbc,0xbd,0xbd,0xbe,0xbe,0xbf,0xbf,0xc0,0xc0,
68 0xc1,0xc1,0xaf,0xc2,0xc2,0xc3,0xc3,0xc4,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xca,
69 0xca,0xcb,0xcb,0xcb,0xcc,0xcc,0xcc,0xcd,0xcd,0xcd,0xce,0xce,0xce,0xcf,0xd0,0xd1,
70 0xd2,0xd3,0xac,0xd4,0xad,0xd5,0xae,0xd6,0xd7,0xd8,0xd9,0xda,0xdb,0xdc,0xdc,0xb2,
71 0xb4,0xa6,0xdd,0xb3,0xb6,0xb9};
73 static MSVCRT_wchar_t
msvcrt_mbc_to_wc(unsigned int ch
)
83 mbch
[0] = (ch
>> 8) & 0xff;
87 if (!MultiByteToWideChar(get_mbcinfo()->mbcodepage
, 0, mbch
, n_chars
, &chW
, 1))
89 WARN("MultiByteToWideChar failed on %x\n", ch
);
95 static inline MSVCRT_size_t
u_strlen( const unsigned char *str
)
97 return strlen( (const char*) str
);
100 static inline unsigned char* u_strncat( unsigned char* dst
, const unsigned char* src
, MSVCRT_size_t len
)
102 return (unsigned char*)strncat( (char*)dst
, (const char*)src
, len
);
105 static inline int u_strcmp( const unsigned char *s1
, const unsigned char *s2
)
107 return strcmp( (const char*)s1
, (const char*)s2
);
110 static inline int u_strcasecmp( const unsigned char *s1
, const unsigned char *s2
)
112 return strcasecmp( (const char*)s1
, (const char*)s2
);
115 static inline int u_strncmp( const unsigned char *s1
, const unsigned char *s2
, MSVCRT_size_t len
)
117 return strncmp( (const char*)s1
, (const char*)s2
, len
);
120 static inline int u_strncasecmp( const unsigned char *s1
, const unsigned char *s2
, MSVCRT_size_t len
)
122 return strncasecmp( (const char*)s1
, (const char*)s2
, len
);
125 static inline unsigned char *u_strchr( const unsigned char *s
, unsigned char x
)
127 return (unsigned char*) strchr( (const char*)s
, x
);
130 static inline unsigned char *u_strrchr( const unsigned char *s
, unsigned char x
)
132 return (unsigned char*) strrchr( (const char*)s
, x
);
135 static inline unsigned char *u_strtok( unsigned char *s
, const unsigned char *delim
)
137 return (unsigned char*) strtok( (char*)s
, (const char*)delim
);
140 static inline unsigned char *u__strset( unsigned char *s
, unsigned char c
)
142 return (unsigned char*) _strset( (char*)s
, c
);
145 static inline unsigned char *u__strnset( unsigned char *s
, unsigned char c
, MSVCRT_size_t len
)
147 return (unsigned char*) MSVCRT__strnset( (char*)s
, c
, len
);
150 static inline MSVCRT_size_t
u_strcspn( const unsigned char *s
, const unsigned char *rej
)
152 return strcspn( (const char *)s
, (const char*)rej
);
155 /*********************************************************************
156 * __p__mbctype (MSVCRT.@)
158 unsigned char* CDECL
__p__mbctype(void)
160 return get_mbcinfo()->mbctype
;
163 /*********************************************************************
164 * ___mb_cur_max_func(MSVCRT.@)
166 int* CDECL
MSVCRT____mb_cur_max_func(void)
168 return &get_locinfo()->mb_cur_max
;
171 /* ___mb_cur_max_l_func - not exported in native msvcrt */
172 int* CDECL
___mb_cur_max_l_func(MSVCRT__locale_t locale
)
174 MSVCRT_pthreadlocinfo locinfo
;
177 locinfo
= get_locinfo();
179 locinfo
= locale
->locinfo
;
181 return &locinfo
->mb_cur_max
;
184 /*********************************************************************
185 * _setmbcp (MSVCRT.@)
187 int CDECL
_setmbcp(int cp
)
189 const char format
[] = ".%d";
191 MSVCRT_pthreadmbcinfo mbcinfo
= get_mbcinfo();
213 newcp
= get_locinfo()->lc_codepage
;
216 /* fall through (C locale) */
218 newcp
= 20127; /* ASCII */
225 sprintf(buf
, format
, newcp
);
226 mbcinfo
->mblcid
= MSVCRT_locale_to_LCID(buf
);
227 if(mbcinfo
->mblcid
== -1)
229 WARN("Can't assign LCID to codepage (%d)\n", mbcinfo
->mblcid
);
233 if (!GetCPInfo(newcp
, &cpi
))
235 WARN("Codepage %d not found\n", newcp
);
236 *MSVCRT__errno() = MSVCRT_EINVAL
;
240 /* setup the _mbctype */
241 memset(mbcinfo
->mbctype
, 0, sizeof(unsigned char[257]));
242 memset(mbcinfo
->mbcasemap
, 0, sizeof(unsigned char[256]));
244 bytes
= cpi
.LeadByte
;
245 while (bytes
[0] || bytes
[1])
247 for (i
= bytes
[0]; i
<= bytes
[1]; i
++)
248 mbcinfo
->mbctype
[i
+ 1] |= _M1
;
252 if (cpi
.MaxCharSize
> 1)
254 /* trail bytes not available through kernel32 but stored in a structure in msvcrt */
255 struct cp_extra_info_t
*cpextra
= g_cpextrainfo
;
257 mbcinfo
->ismbcodepage
= 1;
260 if (cpextra
->cp
== 0 || cpextra
->cp
== newcp
)
262 if (cpextra
->cp
== 0)
263 FIXME("trail bytes data not available for DBCS codepage %d - assuming all bytes\n", newcp
);
265 bytes
= cpextra
->TrailBytes
;
266 while (bytes
[0] || bytes
[1])
268 for (i
= bytes
[0]; i
<= bytes
[1]; i
++)
269 mbcinfo
->mbctype
[i
+ 1] |= _M2
;
278 mbcinfo
->ismbcodepage
= 0;
280 /* we can't use GetStringTypeA directly because we don't have a locale - only a code page
283 for (i
= 0; i
< 256; i
++)
284 if (!(mbcinfo
->mbctype
[i
+ 1] & _M1
))
285 bufA
[charcount
++] = i
;
287 ret
= MultiByteToWideChar(newcp
, 0, bufA
, charcount
, bufW
, charcount
);
288 if (ret
!= charcount
)
289 ERR("MultiByteToWideChar of chars failed for cp %d, ret=%d (exp %d), error=%d\n", newcp
, ret
, charcount
, GetLastError());
291 GetStringTypeW(CT_CTYPE1
, bufW
, charcount
, chartypes
);
293 curr_type
= chartypes
;
294 for (i
= 0; i
< 256; i
++)
295 if (!(mbcinfo
->mbctype
[i
+ 1] & _M1
))
297 if ((*curr_type
) & C1_UPPER
)
298 mbcinfo
->mbctype
[i
+ 1] |= _SBUP
;
299 if ((*curr_type
) & C1_LOWER
)
300 mbcinfo
->mbctype
[i
+ 1] |= _SBLOW
;
304 if (newcp
== 932) /* CP932 only - set _MP and _MS */
306 /* On Windows it's possible to calculate the _MP and _MS from CT_CTYPE1
307 * and CT_CTYPE3. But as of Wine 0.9.43 we return wrong values what makes
308 * it hard. As this is set only for codepage 932 we hardcode it what gives
309 * also faster execution.
311 for (i
= 161; i
<= 165; i
++)
312 mbcinfo
->mbctype
[i
+ 1] |= _MP
;
313 for (i
= 166; i
<= 223; i
++)
314 mbcinfo
->mbctype
[i
+ 1] |= _MS
;
317 mbcinfo
->mbcodepage
= newcp
;
318 if(mbcinfo
== MSVCRT_locale
->mbcinfo
)
319 memcpy(MSVCRT_mbctype
, MSVCRT_locale
->mbcinfo
->mbctype
, sizeof(MSVCRT_mbctype
));
324 /*********************************************************************
325 * _getmbcp (MSVCRT.@)
327 int CDECL
_getmbcp(void)
329 return get_mbcinfo()->mbcodepage
;
332 /*********************************************************************
333 * _mbsnextc(MSVCRT.@)
335 unsigned int CDECL
_mbsnextc(const unsigned char* str
)
338 return *str
<< 8 | str
[1];
342 /*********************************************************************
343 * _mbctolower(MSVCRT.@)
345 unsigned int CDECL
_mbctolower(unsigned int c
)
347 if (MSVCRT_isleadbyte(c
))
349 FIXME("Handle MBC chars\n");
352 return tolower(c
); /* ASCII CP or SB char */
355 /*********************************************************************
356 * _mbctoupper(MSVCRT.@)
358 unsigned int CDECL
_mbctoupper(unsigned int c
)
360 if (MSVCRT_isleadbyte(c
))
362 FIXME("Handle MBC chars\n");
365 return toupper(c
); /* ASCII CP or SB char */
368 /*********************************************************************
369 * _mbctombb (MSVCRT.@)
371 unsigned int CDECL
_mbctombb(unsigned int c
)
375 if(get_mbcinfo()->mbcodepage
== 932)
377 if(c
>= 0x829f && c
<= 0x82f1) /* Hiragana */
378 return mbctombb_932_kana
[c
- 0x829f];
379 if(c
>= 0x8340 && c
<= 0x8396 && c
!= 0x837f) /* Katakana */
380 return mbctombb_932_kana
[c
- 0x8340 - (c
>= 0x837f ? 1 : 0)];
381 if(c
>= 0x8140 && c
<= 0x8197) /* Punctuation */
383 value
= mbctombb_932_punct
[c
- 0x8140];
384 return value
? value
: c
;
386 if((c
>= 0x824f && c
<= 0x8258) || /* Fullwidth digits */
387 (c
>= 0x8260 && c
<= 0x8279)) /* Fullwidth capitals letters */
389 if(c
>= 0x8281 && c
<= 0x829a) /* Fullwidth small letters */
391 /* all other cases return c */
396 /*********************************************************************
397 * _mbcjistojms(MSVCRT.@)
399 * Converts a jis character to sjis.
400 * Based on description from
401 * http://www.slayers.ne.jp/~oouchi/code/jistosjis.html
403 unsigned int CDECL
_mbcjistojms(unsigned int c
)
405 /* Conversion takes place only when codepage is 932.
406 In all other cases, c is returned unchanged */
407 if(get_mbcinfo()->mbcodepage
== 932)
409 if(HIBYTE(c
) >= 0x21 && HIBYTE(c
) <= 0x7e &&
410 LOBYTE(c
) >= 0x21 && LOBYTE(c
) <= 0x7e)
420 c
= (((HIBYTE(c
) - 0x21)/2 + 0x81) << 8) | LOBYTE(c
);
426 return 0; /* Codepage is 932, but c can't be converted */
432 /*********************************************************************
435 unsigned char* CDECL
_mbsdec(const unsigned char* start
, const unsigned char* cur
)
437 if(get_mbcinfo()->ismbcodepage
)
438 return (unsigned char *)(_ismbstrail(start
,cur
-1) ? cur
- 2 : cur
-1);
440 return (unsigned char *)cur
- 1; /* ASCII CP or SB char */
443 /*********************************************************************
446 unsigned int CDECL
_mbclen(const unsigned char* str
)
448 return _ismbblead(*str
) ? 2 : 1;
451 /*********************************************************************
454 unsigned char* CDECL
_mbsinc(const unsigned char* str
)
456 return (unsigned char *)(str
+ _mbclen(str
));
459 /*********************************************************************
462 unsigned char* CDECL
_mbsninc(const unsigned char* str
, MSVCRT_size_t num
)
467 while (num
> 0 && *str
)
469 if (_ismbblead(*str
))
479 return (unsigned char*)str
;
482 /*********************************************************************
485 MSVCRT_size_t CDECL
_mbslen(const unsigned char* str
)
487 MSVCRT_size_t len
= 0;
490 if (_ismbblead(*str
))
493 if (!*str
) /* count only full chars */
502 /*********************************************************************
505 void CDECL
_mbccpy(unsigned char* dest
, const unsigned char* src
)
509 *++dest
= *++src
; /* MB char */
512 /*********************************************************************
515 * The parameter n is the number or characters to copy, not the size of
516 * the buffer. Use _mbsnbcpy for a function analogical to strncpy
518 unsigned char* CDECL
_mbsncpy(unsigned char* dst
, const unsigned char* src
, MSVCRT_size_t n
)
520 unsigned char* ret
= dst
;
523 if (get_mbcinfo()->ismbcodepage
)
528 if (_ismbblead(*src
))
548 if (!(*dst
++ = *src
++)) break;
551 while (n
--) *dst
++ = 0;
555 /*********************************************************************
556 * _mbsnbcpy_s(MSVCRT.@)
558 * Unlike _mbsnbcpy this function does not pad the rest of the dest
561 int CDECL
_mbsnbcpy_s(unsigned char* dst
, MSVCRT_size_t size
, const unsigned char* src
, MSVCRT_size_t n
)
563 MSVCRT_size_t pos
= 0;
565 if(!dst
|| size
== 0)
566 return MSVCRT_EINVAL
;
570 return MSVCRT_EINVAL
;
575 if(get_mbcinfo()->ismbcodepage
)
583 return MSVCRT_ERANGE
;
585 is_lead
= (!is_lead
&& _ismbblead(*src
));
590 if (is_lead
) /* if string ends with a lead, remove it */
601 return MSVCRT_ERANGE
;
614 return MSVCRT_ERANGE
;
620 /*********************************************************************
621 * _mbsnbcpy(MSVCRT.@)
623 * Like strncpy this function doesn't enforce the string to be
626 unsigned char* CDECL
_mbsnbcpy(unsigned char* dst
, const unsigned char* src
, MSVCRT_size_t n
)
628 unsigned char* ret
= dst
;
631 if(get_mbcinfo()->ismbcodepage
)
636 is_lead
= (!is_lead
&& _ismbblead(*src
));
641 if (is_lead
) /* if string ends with a lead, remove it */
649 if (!(*dst
++ = *src
++)) break;
652 while (n
--) *dst
++ = 0;
656 /*********************************************************************
659 int CDECL
_mbscmp(const unsigned char* str
, const unsigned char* cmp
)
661 if(get_mbcinfo()->ismbcodepage
)
663 unsigned int strc
, cmpc
;
666 return *cmp
? -1 : 0;
669 strc
= _mbsnextc(str
);
670 cmpc
= _mbsnextc(cmp
);
672 return strc
< cmpc
? -1 : 1;
673 str
+=(strc
> 255) ? 2 : 1;
674 cmp
+=(strc
> 255) ? 2 : 1; /* equal, use same increment */
677 return u_strcmp(str
, cmp
); /* ASCII CP */
680 /*********************************************************************
681 * _mbsicoll(MSVCRT.@)
682 * FIXME: handle locales.
684 int CDECL
_mbsicoll(const unsigned char* str
, const unsigned char* cmp
)
686 if(get_mbcinfo()->ismbcodepage
)
688 unsigned int strc
, cmpc
;
691 return *cmp
? -1 : 0;
694 strc
= _mbctolower(_mbsnextc(str
));
695 cmpc
= _mbctolower(_mbsnextc(cmp
));
697 return strc
< cmpc
? -1 : 1;
698 str
+=(strc
> 255) ? 2 : 1;
699 cmp
+=(strc
> 255) ? 2 : 1; /* equal, use same increment */
702 return u_strcasecmp(str
, cmp
); /* ASCII CP */
705 /*********************************************************************
707 * Performs a case-sensitive comparison according to the current code page
709 * _NLSCMPERROR if error
710 * FIXME: handle locales.
712 int CDECL
_mbscoll(const unsigned char* str
, const unsigned char* cmp
)
714 if(get_mbcinfo()->ismbcodepage
)
716 unsigned int strc
, cmpc
;
719 return *cmp
? -1 : 0;
722 strc
= _mbsnextc(str
);
723 cmpc
= _mbsnextc(cmp
);
725 return strc
< cmpc
? -1 : 1;
726 str
+=(strc
> 255) ? 2 : 1;
727 cmp
+=(strc
> 255) ? 2 : 1; /* equal, use same increment */
730 return u_strcmp(str
, cmp
); /* ASCII CP */
734 /*********************************************************************
737 int CDECL
_mbsicmp(const unsigned char* str
, const unsigned char* cmp
)
739 if(get_mbcinfo()->ismbcodepage
)
741 unsigned int strc
, cmpc
;
744 return *cmp
? -1 : 0;
747 strc
= _mbctolower(_mbsnextc(str
));
748 cmpc
= _mbctolower(_mbsnextc(cmp
));
750 return strc
< cmpc
? -1 : 1;
751 str
+=(strc
> 255) ? 2 : 1;
752 cmp
+=(strc
> 255) ? 2 : 1; /* equal, use same increment */
755 return u_strcasecmp(str
, cmp
); /* ASCII CP */
758 /*********************************************************************
761 int CDECL
_mbsncmp(const unsigned char* str
, const unsigned char* cmp
, MSVCRT_size_t len
)
766 if(get_mbcinfo()->ismbcodepage
)
768 unsigned int strc
, cmpc
;
773 return *cmp
? -1 : 0;
776 strc
= _mbsnextc(str
);
777 cmpc
= _mbsnextc(cmp
);
779 return strc
< cmpc
? -1 : 1;
780 inc
=(strc
> 255) ? 2 : 1; /* Equal, use same increment */
784 return 0; /* Matched len chars */
786 return u_strncmp(str
, cmp
, len
); /* ASCII CP */
789 /*********************************************************************
790 * _mbsnbcmp(MSVCRT.@)
792 int CDECL
_mbsnbcmp(const unsigned char* str
, const unsigned char* cmp
, MSVCRT_size_t len
)
796 if(get_mbcinfo()->ismbcodepage
)
798 unsigned int strc
, cmpc
;
803 return *cmp
? -1 : 0;
806 if (MSVCRT_isleadbyte(*str
))
808 strc
=(len
>=2)?_mbsnextc(str
):0;
816 if (MSVCRT_isleadbyte(*cmp
))
817 cmpc
=(len
>=2)?_mbsnextc(cmp
):0;
821 return strc
< cmpc
? -1 : 1;
826 return 0; /* Matched len chars */
828 return u_strncmp(str
,cmp
,len
);
831 /*********************************************************************
832 * _mbsnicmp(MSVCRT.@)
834 * Compare two multibyte strings case insensitively to 'len' characters.
836 int CDECL
_mbsnicmp(const unsigned char* str
, const unsigned char* cmp
, MSVCRT_size_t len
)
838 /* FIXME: No tolower() for mb strings yet */
839 if(get_mbcinfo()->ismbcodepage
)
841 unsigned int strc
, cmpc
;
845 return *cmp
? -1 : 0;
848 strc
= _mbctolower(_mbsnextc(str
));
849 cmpc
= _mbctolower(_mbsnextc(cmp
));
851 return strc
< cmpc
? -1 : 1;
852 str
+=(strc
> 255) ? 2 : 1;
853 cmp
+=(strc
> 255) ? 2 : 1; /* Equal, use same increment */
855 return 0; /* Matched len chars */
857 return u_strncasecmp(str
, cmp
, len
); /* ASCII CP */
860 /*********************************************************************
861 * _mbsnbicmp(MSVCRT.@)
863 int CDECL
_mbsnbicmp(const unsigned char* str
, const unsigned char* cmp
, MSVCRT_size_t len
)
867 if(get_mbcinfo()->ismbcodepage
)
869 unsigned int strc
, cmpc
;
874 return *cmp
? -1 : 0;
877 if (MSVCRT_isleadbyte(*str
))
879 strc
=(len
>=2)?_mbsnextc(str
):0;
887 if (MSVCRT_isleadbyte(*cmp
))
888 cmpc
=(len
>=2)?_mbsnextc(cmp
):0;
891 strc
= _mbctolower(strc
);
892 cmpc
= _mbctolower(cmpc
);
894 return strc
< cmpc
? -1 : 1;
899 return 0; /* Matched len bytes */
901 return u_strncasecmp(str
,cmp
,len
);
904 /*********************************************************************
907 unsigned char * CDECL
_mbscat( unsigned char *dst
, const unsigned char *src
)
909 strcat( (char *)dst
, (const char *)src
);
913 /*********************************************************************
916 unsigned char* CDECL
_mbscpy( unsigned char *dst
, const unsigned char *src
)
918 strcpy( (char *)dst
, (const char *)src
);
922 /*********************************************************************
925 unsigned char * CDECL
_mbsstr(const unsigned char *haystack
, const unsigned char *needle
)
927 return (unsigned char *)strstr( (const char *)haystack
, (const char *)needle
);
930 /*********************************************************************
933 * Find a multibyte character in a multibyte string.
935 unsigned char* CDECL
_mbschr(const unsigned char* s
, unsigned int x
)
937 if(get_mbcinfo()->ismbcodepage
)
944 return (unsigned char*)s
;
947 s
+= c
> 255 ? 2 : 1;
950 return u_strchr(s
, x
); /* ASCII CP */
953 /*********************************************************************
956 unsigned char* CDECL
_mbsrchr(const unsigned char* s
, unsigned int x
)
958 if(get_mbcinfo()->ismbcodepage
)
961 unsigned char* match
=NULL
;
967 match
=(unsigned char*)s
;
970 s
+=(c
> 255) ? 2 : 1;
973 return u_strrchr(s
, x
);
976 /*********************************************************************
979 * Find and extract tokens from strings
981 unsigned char* CDECL
_mbstok(unsigned char *str
, const unsigned char *delim
)
983 thread_data_t
*data
= msvcrt_get_thread_data();
986 if(get_mbcinfo()->ismbcodepage
)
991 if (!(str
= data
->mbstok_next
)) return NULL
;
993 while ((c
= _mbsnextc(str
)) && _mbschr(delim
, c
)) {
994 str
+= c
> 255 ? 2 : 1;
996 if (!*str
) return NULL
;
998 while ((c
= _mbsnextc(str
)) && !_mbschr(delim
, c
)) {
999 str
+= c
> 255 ? 2 : 1;
1003 if (c
> 255) *str
++ = 0;
1005 data
->mbstok_next
= str
;
1008 return u_strtok(str
, delim
); /* ASCII CP */
1011 /*********************************************************************
1014 int CDECL
MSVCRT_mbtowc(MSVCRT_wchar_t
*dst
, const char* str
, MSVCRT_size_t n
)
1016 /* temp var needed because MultiByteToWideChar wants non NULL destination */
1017 MSVCRT_wchar_t tmpdst
= '\0';
1021 if(!MultiByteToWideChar(CP_ACP
, 0, str
, n
, &tmpdst
, 1))
1025 /* return the number of bytes from src that have been used */
1028 if(n
>= 2 && MSVCRT_isleadbyte(*str
) && str
[1])
1033 /*********************************************************************
1034 * _mbbtombc(MSVCRT.@)
1036 unsigned int CDECL
_mbbtombc(unsigned int c
)
1038 if(get_mbcinfo()->ismbcodepage
&&
1039 ((c
>= 0x20 && c
<=0x7e) ||(c
>= 0xa1 && c
<= 0xdf)))
1041 /* FIXME: I can't get this function to return anything
1042 * different from what I pass it...
1045 return c
; /* ASCII CP or no MB char */
1048 /*********************************************************************
1049 * _mbbtype(MSVCRT.@)
1051 int CDECL
_mbbtype(unsigned char c
, int type
)
1055 if ((c
>= 0x20 && c
<= 0x7e) || (c
>= 0xa1 && c
<= 0xdf))
1057 else if ((c
>= 0x40 && c
<= 0x7e) || (c
>= 0x80 && c
<= 0xfc))
1060 return _MBC_ILLEGAL
;
1064 if ((c
>= 0x20 && c
<= 0x7e) || (c
>= 0xa1 && c
<= 0xdf))
1066 else if ((c
>= 0x81 && c
<= 0x9f) || (c
>= 0xe0 && c
<= 0xfc))
1069 return _MBC_ILLEGAL
;
1073 /*********************************************************************
1074 * _ismbbkana(MSVCRT.@)
1076 int CDECL
_ismbbkana(unsigned int c
)
1078 /* FIXME: use lc_ctype when supported, not lc_all */
1079 if(get_mbcinfo()->mbcodepage
== 932)
1081 /* Japanese/Katakana, CP 932 */
1082 return (c
>= 0xa1 && c
<= 0xdf);
1087 /*********************************************************************
1088 * _ismbcdigit(MSVCRT.@)
1090 int CDECL
_ismbcdigit(unsigned int ch
)
1092 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1093 return (get_char_typeW( wch
) & C1_DIGIT
);
1096 /*********************************************************************
1097 * _ismbcgraph(MSVCRT.@)
1099 int CDECL
_ismbcgraph(unsigned int ch
)
1101 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1102 return (get_char_typeW( wch
) & (C1_UPPER
| C1_LOWER
| C1_DIGIT
| C1_PUNCT
| C1_ALPHA
));
1105 /*********************************************************************
1106 * _ismbcalpha (MSVCRT.@)
1108 int CDECL
_ismbcalpha(unsigned int ch
)
1110 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1111 return (get_char_typeW( wch
) & C1_ALPHA
);
1114 /*********************************************************************
1115 * _ismbclower (MSVCRT.@)
1117 int CDECL
_ismbclower(unsigned int ch
)
1119 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1120 return (get_char_typeW( wch
) & C1_UPPER
);
1123 /*********************************************************************
1124 * _ismbcupper (MSVCRT.@)
1126 int CDECL
_ismbcupper(unsigned int ch
)
1128 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1129 return (get_char_typeW( wch
) & C1_LOWER
);
1132 /*********************************************************************
1133 * _ismbcsymbol(MSVCRT.@)
1135 int CDECL
_ismbcsymbol(unsigned int ch
)
1137 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1139 if (!GetStringTypeW(CT_CTYPE3
, &wch
, 1, &ctype
))
1141 WARN("GetStringTypeW failed on %x\n", ch
);
1144 return ((ctype
& C3_SYMBOL
) != 0);
1147 /*********************************************************************
1148 * _ismbcalnum (MSVCRT.@)
1150 int CDECL
_ismbcalnum(unsigned int ch
)
1152 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1153 return (get_char_typeW( wch
) & (C1_ALPHA
| C1_DIGIT
));
1156 /*********************************************************************
1157 * _ismbcspace (MSVCRT.@)
1159 int CDECL
_ismbcspace(unsigned int ch
)
1161 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1162 return (get_char_typeW( wch
) & C1_SPACE
);
1165 /*********************************************************************
1166 * _ismbcprint (MSVCRT.@)
1168 int CDECL
_ismbcprint(unsigned int ch
)
1170 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1171 return (get_char_typeW( wch
) & (C1_UPPER
| C1_LOWER
| C1_DIGIT
| C1_PUNCT
| C1_ALPHA
| C1_SPACE
));
1174 /*********************************************************************
1175 * _ismbcpunct(MSVCRT.@)
1177 int CDECL
_ismbcpunct(unsigned int ch
)
1179 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1180 return (get_char_typeW( wch
) & C1_PUNCT
);
1183 /*********************************************************************
1184 * _ismbchira(MSVCRT.@)
1186 int CDECL
_ismbchira(unsigned int c
)
1188 /* FIXME: use lc_ctype when supported, not lc_all */
1189 if(get_mbcinfo()->mbcodepage
== 932)
1191 /* Japanese/Hiragana, CP 932 */
1192 return (c
>= 0x829f && c
<= 0x82f1);
1197 /*********************************************************************
1198 * _ismbckata(MSVCRT.@)
1200 int CDECL
_ismbckata(unsigned int c
)
1202 /* FIXME: use lc_ctype when supported, not lc_all */
1203 if(get_mbcinfo()->mbcodepage
== 932)
1206 return _ismbbkana(c
);
1207 /* Japanese/Katakana, CP 932 */
1208 return (c
>= 0x8340 && c
<= 0x8396 && c
!= 0x837f);
1213 /*********************************************************************
1214 * _ismbblead(MSVCRT.@)
1216 int CDECL
_ismbblead(unsigned int c
)
1218 return (get_mbcinfo()->mbctype
[(c
&0xff) + 1] & _M1
) != 0;
1222 /*********************************************************************
1223 * _ismbbtrail(MSVCRT.@)
1225 int CDECL
_ismbbtrail(unsigned int c
)
1227 return (get_mbcinfo()->mbctype
[(c
&0xff) + 1] & _M2
) != 0;
1230 /*********************************************************************
1231 * _ismbclegal(MSVCRT.@)
1233 int CDECL
_ismbclegal(unsigned int c
)
1235 return _ismbblead(HIBYTE(c
)) && _ismbbtrail(LOBYTE(c
));
1238 /*********************************************************************
1239 * _ismbslead(MSVCRT.@)
1241 int CDECL
_ismbslead(const unsigned char* start
, const unsigned char* str
)
1245 if(!get_mbcinfo()->ismbcodepage
)
1248 /* Lead bytes can also be trail bytes so we need to analyse the string
1250 while (start
<= str
)
1254 lead
= !lead
&& _ismbblead(*start
);
1258 return lead
? -1 : 0;
1261 /*********************************************************************
1262 * _ismbstrail(MSVCRT.@)
1264 int CDECL
_ismbstrail(const unsigned char* start
, const unsigned char* str
)
1266 /* Note: this function doesn't check _ismbbtrail */
1267 if ((str
> start
) && _ismbslead(start
, str
-1))
1273 /*********************************************************************
1274 * _mbsbtype (MSVCRT.@)
1276 int CDECL
_mbsbtype(const unsigned char *str
, MSVCRT_size_t count
)
1279 const unsigned char *end
= str
+ count
;
1281 /* Lead bytes can also be trail bytes so we need to analyse the string.
1282 * Also we must return _MBC_ILLEGAL for chars past the end of the string
1284 while (str
< end
) /* Note: we skip the last byte - will check after the loop */
1287 return _MBC_ILLEGAL
;
1288 lead
= get_mbcinfo()->ismbcodepage
&& !lead
&& _ismbblead(*str
);
1293 if (_ismbbtrail(*str
))
1296 return _MBC_ILLEGAL
;
1298 if (_ismbblead(*str
))
1304 /*********************************************************************
1307 unsigned char* CDECL
_mbsset(unsigned char* str
, unsigned int c
)
1309 unsigned char* ret
= str
;
1311 if(!get_mbcinfo()->ismbcodepage
|| c
< 256)
1312 return u__strset(str
, c
); /* ASCII CP or SB char */
1314 c
&= 0xffff; /* Strip high bits */
1316 while(str
[0] && str
[1])
1322 str
[0] = '\0'; /* FIXME: OK to shorten? */
1327 /*********************************************************************
1328 * _mbsnbset(MSVCRT.@)
1330 unsigned char* CDECL
_mbsnbset(unsigned char *str
, unsigned int c
, MSVCRT_size_t len
)
1332 unsigned char *ret
= str
;
1337 if(!get_mbcinfo()->ismbcodepage
|| c
< 256)
1338 return u__strnset(str
, c
, len
); /* ASCII CP or SB char */
1340 c
&= 0xffff; /* Strip high bits */
1342 while(str
[0] && str
[1] && (len
> 1))
1350 /* as per msdn pad with a blank character */
1357 /*********************************************************************
1358 * _mbsnset(MSVCRT.@)
1360 unsigned char* CDECL
_mbsnset(unsigned char* str
, unsigned int c
, MSVCRT_size_t len
)
1362 unsigned char *ret
= str
;
1367 if(!get_mbcinfo()->ismbcodepage
|| c
< 256)
1368 return u__strnset(str
, c
, len
); /* ASCII CP or SB char */
1370 c
&= 0xffff; /* Strip high bits */
1372 while(str
[0] && str
[1] && len
--)
1378 str
[0] = '\0'; /* FIXME: OK to shorten? */
1383 /*********************************************************************
1384 * _mbsnccnt(MSVCRT.@)
1385 * 'c' is for 'character'.
1387 MSVCRT_size_t CDECL
_mbsnccnt(const unsigned char* str
, MSVCRT_size_t len
)
1390 if(get_mbcinfo()->ismbcodepage
)
1393 while(*str
&& len
-- > 0)
1395 if(MSVCRT_isleadbyte(*str
))
1408 return min(ret
, len
); /* ASCII CP */
1411 /*********************************************************************
1412 * _mbsnbcnt(MSVCRT.@)
1413 * 'b' is for byte count.
1415 MSVCRT_size_t CDECL
_mbsnbcnt(const unsigned char* str
, MSVCRT_size_t len
)
1418 if(get_mbcinfo()->ismbcodepage
)
1420 const unsigned char* xstr
= str
;
1421 while(*xstr
&& len
-- > 0)
1423 if (MSVCRT_isleadbyte(*xstr
++))
1429 return min(ret
, len
); /* ASCII CP */
1432 /*********************************************************************
1433 * _mbsnbcat(MSVCRT.@)
1435 unsigned char* CDECL
_mbsnbcat(unsigned char* dst
, const unsigned char* src
, MSVCRT_size_t len
)
1437 if(get_mbcinfo()->ismbcodepage
)
1439 unsigned char *res
= dst
;
1441 if (MSVCRT_isleadbyte(*dst
++)) {
1445 /* as per msdn overwrite the lead byte in front of '\0' */
1451 while (*src
&& len
--) *dst
++ = *src
++;
1455 return u_strncat(dst
, src
, len
); /* ASCII CP */
1458 int CDECL
_mbsnbcat_s(unsigned char *dst
, MSVCRT_size_t size
, const unsigned char *src
, MSVCRT_size_t len
)
1460 unsigned char *ptr
= dst
;
1463 if (!dst
&& !size
&& !src
&& !len
)
1466 if (!dst
|| !size
|| !src
)
1471 *MSVCRT__errno() = MSVCRT_EINVAL
;
1472 return MSVCRT_EINVAL
;
1475 /* Find the null terminator of the destination buffer. */
1476 while (size
&& *ptr
)
1482 *MSVCRT__errno() = MSVCRT_EINVAL
;
1483 return MSVCRT_EINVAL
;
1486 /* If necessary, check that the character preceding the null terminator is
1487 * a lead byte and move the pointer back by one for later overwrite. */
1488 if (ptr
!= dst
&& get_mbcinfo()->ismbcodepage
&& MSVCRT_isleadbyte(*(ptr
- 1)))
1491 for (i
= 0; *src
&& i
< len
; i
++)
1499 *MSVCRT__errno() = MSVCRT_ERANGE
;
1500 return MSVCRT_ERANGE
;
1508 /*********************************************************************
1509 * _mbsncat(MSVCRT.@)
1511 unsigned char* CDECL
_mbsncat(unsigned char* dst
, const unsigned char* src
, MSVCRT_size_t len
)
1513 if(get_mbcinfo()->ismbcodepage
)
1515 unsigned char *res
= dst
;
1518 if (MSVCRT_isleadbyte(*dst
++))
1521 while (*src
&& len
--)
1524 if(MSVCRT_isleadbyte(*src
++))
1530 return u_strncat(dst
, src
, len
); /* ASCII CP */
1534 /*********************************************************************
1537 unsigned char* CDECL
_mbslwr(unsigned char* s
)
1539 unsigned char *ret
= s
;
1542 if (get_mbcinfo()->ismbcodepage
)
1547 c
= _mbctolower(_mbsnextc(s
));
1548 /* Note that I assume that the size of the character is unchanged */
1557 else for ( ; *s
; s
++) *s
= tolower(*s
);
1562 /*********************************************************************
1563 * _mbslwr_s(MSVCRT.@)
1565 int CDECL
_mbslwr_s(unsigned char* s
, MSVCRT_size_t len
)
1571 else if (!s
|| !len
)
1573 *MSVCRT__errno() = MSVCRT_EINVAL
;
1574 return MSVCRT_EINVAL
;
1576 if (get_mbcinfo()->ismbcodepage
)
1579 for ( ; *s
&& len
> 0; len
--)
1581 c
= _mbctolower(_mbsnextc(s
));
1582 /* Note that I assume that the size of the character is unchanged */
1591 else for ( ; *s
&& len
> 0; s
++, len
--) *s
= tolower(*s
);
1595 *MSVCRT__errno() = MSVCRT_EINVAL
;
1596 return MSVCRT_EINVAL
;
1602 /*********************************************************************
1605 unsigned char* CDECL
_mbsupr(unsigned char* s
)
1607 unsigned char *ret
= s
;
1610 if (get_mbcinfo()->ismbcodepage
)
1615 c
= _mbctoupper(_mbsnextc(s
));
1616 /* Note that I assume that the size of the character is unchanged */
1625 else for ( ; *s
; s
++) *s
= toupper(*s
);
1630 /*********************************************************************
1631 * _mbsupr_s(MSVCRT.@)
1633 int CDECL
_mbsupr_s(unsigned char* s
, MSVCRT_size_t len
)
1639 else if (!s
|| !len
)
1641 *MSVCRT__errno() = MSVCRT_EINVAL
;
1642 return MSVCRT_EINVAL
;
1644 if (get_mbcinfo()->ismbcodepage
)
1647 for ( ; *s
&& len
> 0; len
--)
1649 c
= _mbctoupper(_mbsnextc(s
));
1650 /* Note that I assume that the size of the character is unchanged */
1659 else for ( ; *s
&& len
> 0; s
++, len
--) *s
= toupper(*s
);
1663 *MSVCRT__errno() = MSVCRT_EINVAL
;
1664 return MSVCRT_EINVAL
;
1670 /*********************************************************************
1671 * _mbsspn (MSVCRT.@)
1673 MSVCRT_size_t CDECL
_mbsspn(const unsigned char* string
, const unsigned char* set
)
1675 const unsigned char *p
, *q
;
1677 for (p
= string
; *p
; p
++)
1679 if (MSVCRT_isleadbyte(*p
))
1681 for (q
= set
; *q
; q
++)
1685 if ((*p
== *q
) && (p
[1] == q
[1]))
1689 if (!q
[0] || !q
[1]) break;
1693 for (q
= set
; *q
; q
++)
1702 /*********************************************************************
1703 * _mbsspnp (MSVCRT.@)
1705 unsigned char* CDECL
_mbsspnp(const unsigned char* string
, const unsigned char* set
)
1707 const unsigned char *p
, *q
;
1709 for (p
= string
; *p
; p
++)
1711 if (MSVCRT_isleadbyte(*p
))
1713 for (q
= set
; *q
; q
++)
1717 if ((*p
== *q
) && (p
[1] == q
[1]))
1721 if (!q
[0] || !q
[1]) break;
1725 for (q
= set
; *q
; q
++)
1733 return (unsigned char *)p
;
1736 /*********************************************************************
1737 * _mbscspn(MSVCRT.@)
1739 MSVCRT_size_t CDECL
_mbscspn(const unsigned char* str
, const unsigned char* cmp
)
1741 if (get_mbcinfo()->ismbcodepage
)
1742 FIXME("don't handle double character case\n");
1743 return u_strcspn(str
, cmp
);
1746 /*********************************************************************
1747 * _mbsrev (MSVCRT.@)
1749 unsigned char* CDECL
_mbsrev(unsigned char* str
)
1751 int i
, len
= _mbslen(str
);
1752 unsigned char *p
, *temp
=MSVCRT_malloc(len
*2);
1757 /* unpack multibyte string to temp buffer */
1759 for(i
=0; i
<len
; i
++)
1761 if (MSVCRT_isleadbyte(*p
))
1773 /* repack it in the reverse order */
1775 for(i
=len
-1; i
>=0; i
--)
1777 if(MSVCRT_isleadbyte(temp
[i
*2]))
1793 /*********************************************************************
1794 * _mbspbrk (MSVCRT.@)
1796 unsigned char* CDECL
_mbspbrk(const unsigned char* str
, const unsigned char* accept
)
1798 const unsigned char* p
;
1802 for(p
= accept
; *p
; p
+= (MSVCRT_isleadbyte(*p
)?2:1) )
1805 if( !MSVCRT_isleadbyte(*p
) || ( *(p
+1) == *(str
+1) ) )
1806 return (unsigned char*)str
;
1808 str
+= (MSVCRT_isleadbyte(*str
)?2:1);
1815 * Functions depending on locale codepage
1818 /*********************************************************************
1821 * Unlike most of the multibyte string functions this function uses
1822 * the locale codepage, not the codepage set by _setmbcp
1824 int CDECL
MSVCRT_mblen(const char* str
, MSVCRT_size_t size
)
1826 if (str
&& *str
&& size
)
1828 if(get_locinfo()->mb_cur_max
== 1)
1829 return 1; /* ASCII CP */
1831 return !MSVCRT_isleadbyte(*str
) ? 1 : (size
>1 ? 2 : -1);
1836 /*********************************************************************
1837 * _mbstrlen_l(MSVCRT.@)
1839 MSVCRT_size_t CDECL
_mbstrlen_l(const char* str
, MSVCRT__locale_t locale
)
1841 MSVCRT_pthreadlocinfo locinfo
;
1844 locinfo
= get_locinfo();
1846 locinfo
= locale
->locinfo
;
1848 if(locinfo
->mb_cur_max
> 1) {
1849 MSVCRT_size_t len
= 0;
1851 /* FIXME: According to the documentation we are supposed to test for
1852 * multi-byte character validity. Whatever that means
1854 str
+= MSVCRT_isleadbyte(*str
) ? 2 : 1;
1863 /*********************************************************************
1864 * _mbstrlen(MSVCRT.@)
1866 MSVCRT_size_t CDECL
_mbstrlen(const char* str
)
1868 return _mbstrlen_l(str
, NULL
);
1871 /*********************************************************************
1872 * _mbstowcs_l(MSVCRT.@)
1874 MSVCRT_size_t CDECL
MSVCRT__mbstowcs_l(MSVCRT_wchar_t
*wcstr
, const char *mbstr
,
1875 MSVCRT_size_t count
, MSVCRT__locale_t locale
)
1877 MSVCRT_pthreadlocinfo locinfo
;
1878 MSVCRT_size_t i
, size
;
1881 locinfo
= get_locinfo();
1883 locinfo
= locale
->locinfo
;
1885 /* Ignore count parameter */
1887 return MultiByteToWideChar(locinfo
->lc_codepage
, 0, mbstr
, -1, NULL
, 0)-1;
1889 for(i
=0, size
=0; i
<count
; i
++) {
1890 if(mbstr
[size
] == '\0')
1893 size
+= (MSVCRT_isleadbyte(mbstr
[size
]) ? 2 : 1);
1896 size
= MultiByteToWideChar(locinfo
->lc_codepage
, 0,
1897 mbstr
, size
, wcstr
, count
);
1899 if(size
<count
&& wcstr
)
1905 /*********************************************************************
1906 * mbstowcs(MSVCRT.@)
1908 MSVCRT_size_t CDECL
MSVCRT_mbstowcs(MSVCRT_wchar_t
*wcstr
,
1909 const char *mbstr
, MSVCRT_size_t count
)
1911 return MSVCRT__mbstowcs_l(wcstr
, mbstr
, count
, NULL
);
1914 /*********************************************************************
1915 * _mbstowcs_s_l(MSVCRT.@)
1917 int CDECL
MSVCRT__mbstowcs_s_l(MSVCRT_size_t
*ret
, MSVCRT_wchar_t
*wcstr
,
1918 MSVCRT_size_t size
, const char *mbstr
,
1919 MSVCRT_size_t count
, MSVCRT__locale_t locale
)
1923 if(!wcstr
&& !size
) {
1924 conv
= MSVCRT__mbstowcs_l(NULL
, mbstr
, 0, locale
);
1930 if(!MSVCRT_CHECK_PMT(mbstr
!= NULL
) || !MSVCRT_CHECK_PMT(wcstr
!= NULL
)) {
1933 *MSVCRT__errno() = MSVCRT_EINVAL
;
1934 return MSVCRT_EINVAL
;
1937 if(count
==MSVCRT__TRUNCATE
|| size
<count
)
1942 conv
= MSVCRT__mbstowcs_l(wcstr
, mbstr
, conv
, locale
);
1944 wcstr
[conv
++] = '\0';
1945 else if(conv
==size
&& (count
==MSVCRT__TRUNCATE
|| wcstr
[conv
-1]=='\0'))
1946 wcstr
[conv
-1] = '\0';
1948 MSVCRT_INVALID_PMT("wcstr[size] is too small");
1951 *MSVCRT__errno() = MSVCRT_ERANGE
;
1952 return MSVCRT_ERANGE
;
1960 /*********************************************************************
1961 * mbstowcs_s(MSVCRT.@)
1963 int CDECL
MSVCRT__mbstowcs_s(MSVCRT_size_t
*ret
, MSVCRT_wchar_t
*wcstr
,
1964 MSVCRT_size_t size
, const char *mbstr
, MSVCRT_size_t count
)
1966 return MSVCRT__mbstowcs_s_l(ret
, wcstr
, size
, mbstr
, count
, NULL
);