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.
30 #include "wine/unicode.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt
);
35 unsigned char MSVCRT_mbctype
[257] = { 0 };
37 /* It seems that the data about valid trail bytes is not available from kernel32
38 * so we have to store is here. The format is the same as for lead bytes in CPINFO */
39 struct cp_extra_info_t
42 BYTE TrailBytes
[MAX_LEADBYTES
];
45 static struct cp_extra_info_t g_cpextrainfo
[] =
47 {932, {0x40, 0x7e, 0x80, 0xfc, 0, 0}},
48 {936, {0x40, 0xfe, 0, 0}},
49 {949, {0x41, 0xfe, 0, 0}},
50 {950, {0x40, 0x7e, 0xa1, 0xfe, 0, 0}},
51 {1361, {0x31, 0x7e, 0x81, 0xfe, 0, 0}},
52 {20932, {1, 255, 0, 0}}, /* seems to give different results on different systems */
53 {0, {1, 255, 0, 0}} /* match all with FIXME */
56 /* Maps cp932 single byte character to multi byte character */
57 static const unsigned char mbbtombc_932
[] = {
58 0x40,0x49,0x68,0x94,0x90,0x93,0x95,0x66,0x69,0x6a,0x96,0x7b,0x43,0x7c,0x44,0x5e,
59 0x4f,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x46,0x47,0x83,0x81,0x84,0x48,
60 0x97,0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,
61 0x6f,0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x6d,0x8f,0x6e,0x4f,0x76,
62 0x77,0x78,0x79,0x6d,0x8f,0x6e,0x4f,0x51,0x65,0x81,0x82,0x83,0x84,0x85,0x86,0x87,
63 0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f,0x90,0x91,0x92,0x93,0x94,0x95,0x50,
64 0x42,0x75,0x76,0x41,0x45,0x92,0x40,0x42,0x44,0x46,0x48,0x83,0x85,0x87,0x62,
65 0x5b,0x41,0x43,0x45,0x47,0x49,0x4a,0x4c,0x4e,0x50,0x52,0x54,0x56,0x58,0x5a,0x5c,
66 0x5e,0x60,0x63,0x65,0x67,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x71,0x74,0x77,0x7a,0x7d,
67 0x7e,0x80,0x81,0x82,0x84,0x86,0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8f,0x93,0x4a,0x4b };
69 /* Maps multibyte cp932 punctuation marks to single byte equivalents */
70 static const unsigned char mbctombb_932_punct
[] = {
71 0x20,0xa4,0xa1,0x2c,0x2e,0xa5,0x3a,0x3b,0x3f,0x21,0xde,0xdf,0x00,0x00,0x00,0x5e,
72 0x7e,0x5f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xb0,0x00,0x00,0x2f,0x00,
73 0x00,0x00,0x7c,0x00,0x00,0x60,0x27,0x00,0x22,0x28,0x29,0x00,0x00,0x5b,0x5d,0x7b,
74 0x7d,0x00,0x00,0x00,0x00,0xa2,0xa3,0x00,0x00,0x00,0x00,0x2b,0x2d,0x00,0x00,0x00,
75 0x00,0x3d,0x00,0x3c,0x3e,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x5c,
76 0x24,0x00,0x00,0x25,0x23,0x26,0x2a,0x40};
78 /* Maps multibyte cp932 hiragana/katakana to single-byte equivalents */
79 static const unsigned char mbctombb_932_kana
[] = {
80 0xa7,0xb1,0xa8,0xb2,0xa9,0xb3,0xaa,0xb4,0xab,0xb5,0xb6,0xb6,0xb7,0xb7,0xb8,0xb8,
81 0xb9,0xb9,0xba,0xba,0xbb,0xbb,0xbc,0xbc,0xbd,0xbd,0xbe,0xbe,0xbf,0xbf,0xc0,0xc0,
82 0xc1,0xc1,0xaf,0xc2,0xc2,0xc3,0xc3,0xc4,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xca,
83 0xca,0xcb,0xcb,0xcb,0xcc,0xcc,0xcc,0xcd,0xcd,0xcd,0xce,0xce,0xce,0xcf,0xd0,0xd1,
84 0xd2,0xd3,0xac,0xd4,0xad,0xd5,0xae,0xd6,0xd7,0xd8,0xd9,0xda,0xdb,0xdc,0xdc,0xb2,
85 0xb4,0xa6,0xdd,0xb3,0xb6,0xb9};
87 static MSVCRT_wchar_t
msvcrt_mbc_to_wc(unsigned int ch
)
97 mbch
[0] = (ch
>> 8) & 0xff;
101 if (!MultiByteToWideChar(get_mbcinfo()->mbcodepage
, 0, mbch
, n_chars
, &chW
, 1))
103 WARN("MultiByteToWideChar failed on %x\n", ch
);
109 static inline MSVCRT_size_t
u_strlen( const unsigned char *str
)
111 return strlen( (const char*) str
);
114 static inline unsigned char* u_strncat( unsigned char* dst
, const unsigned char* src
, MSVCRT_size_t len
)
116 return (unsigned char*)strncat( (char*)dst
, (const char*)src
, len
);
119 static inline int u_strcmp( const unsigned char *s1
, const unsigned char *s2
)
121 return strcmp( (const char*)s1
, (const char*)s2
);
124 static inline int u_strcasecmp( const unsigned char *s1
, const unsigned char *s2
)
126 return strcasecmp( (const char*)s1
, (const char*)s2
);
129 static inline int u_strncmp( const unsigned char *s1
, const unsigned char *s2
, MSVCRT_size_t len
)
131 return strncmp( (const char*)s1
, (const char*)s2
, len
);
134 static inline int u_strncasecmp( const unsigned char *s1
, const unsigned char *s2
, MSVCRT_size_t len
)
136 return strncasecmp( (const char*)s1
, (const char*)s2
, len
);
139 static inline unsigned char *u_strchr( const unsigned char *s
, unsigned char x
)
141 return (unsigned char*) strchr( (const char*)s
, x
);
144 static inline unsigned char *u_strrchr( const unsigned char *s
, unsigned char x
)
146 return (unsigned char*) strrchr( (const char*)s
, x
);
149 static inline unsigned char *u__strset( unsigned char *s
, unsigned char c
)
151 return (unsigned char*) _strset( (char*)s
, c
);
154 static inline unsigned char *u__strnset( unsigned char *s
, unsigned char c
, MSVCRT_size_t len
)
156 return (unsigned char*) MSVCRT__strnset( (char*)s
, c
, len
);
159 static inline MSVCRT_size_t
u_strcspn( const unsigned char *s
, const unsigned char *rej
)
161 return strcspn( (const char *)s
, (const char*)rej
);
164 /*********************************************************************
165 * __p__mbctype (MSVCRT.@)
167 unsigned char* CDECL
__p__mbctype(void)
169 return get_mbcinfo()->mbctype
;
172 /*********************************************************************
173 * __p___mb_cur_max(MSVCRT.@)
175 int* CDECL
__p___mb_cur_max(void)
177 return &get_locinfo()->mb_cur_max
;
180 /*********************************************************************
181 * ___mb_cur_max_func(MSVCRT.@)
183 int CDECL
MSVCRT____mb_cur_max_func(void)
185 return get_locinfo()->mb_cur_max
;
188 /*********************************************************************
189 * ___mb_cur_max_l_func (MSVCR80.@)
191 int* CDECL
___mb_cur_max_l_func(MSVCRT__locale_t locale
)
193 MSVCRT_pthreadlocinfo locinfo
;
196 locinfo
= get_locinfo();
198 locinfo
= locale
->locinfo
;
200 return &locinfo
->mb_cur_max
;
203 /*********************************************************************
204 * INTERNAL: _setmbcp_l
206 int _setmbcp_l(int cp
, LCID lcid
, MSVCRT_pthreadmbcinfo mbcinfo
)
208 const char format
[] = ".%d";
221 mbcinfo
= get_mbcinfo();
232 newcp
= get_locinfo()->lc_codepage
;
235 /* fall through (C locale) */
237 newcp
= 20127; /* ASCII */
245 sprintf(bufA
, format
, newcp
);
246 mbcinfo
->mblcid
= MSVCRT_locale_to_LCID(bufA
, NULL
);
248 mbcinfo
->mblcid
= lcid
;
251 if(mbcinfo
->mblcid
== -1)
253 WARN("Can't assign LCID to codepage (%d)\n", mbcinfo
->mblcid
);
257 if (!GetCPInfo(newcp
, &cpi
))
259 WARN("Codepage %d not found\n", newcp
);
260 *MSVCRT__errno() = MSVCRT_EINVAL
;
264 /* setup the _mbctype */
265 memset(mbcinfo
->mbctype
, 0, sizeof(unsigned char[257]));
266 memset(mbcinfo
->mbcasemap
, 0, sizeof(unsigned char[256]));
268 bytes
= cpi
.LeadByte
;
269 while (bytes
[0] || bytes
[1])
271 for (i
= bytes
[0]; i
<= bytes
[1]; i
++)
272 mbcinfo
->mbctype
[i
+ 1] |= _M1
;
276 if (cpi
.MaxCharSize
> 1)
278 /* trail bytes not available through kernel32 but stored in a structure in msvcrt */
279 struct cp_extra_info_t
*cpextra
= g_cpextrainfo
;
281 mbcinfo
->ismbcodepage
= 1;
284 if (cpextra
->cp
== 0 || cpextra
->cp
== newcp
)
286 if (cpextra
->cp
== 0)
287 FIXME("trail bytes data not available for DBCS codepage %d - assuming all bytes\n", newcp
);
289 bytes
= cpextra
->TrailBytes
;
290 while (bytes
[0] || bytes
[1])
292 for (i
= bytes
[0]; i
<= bytes
[1]; i
++)
293 mbcinfo
->mbctype
[i
+ 1] |= _M2
;
302 mbcinfo
->ismbcodepage
= 0;
304 /* we can't use GetStringTypeA directly because we don't have a locale - only a code page
307 for (i
= 0; i
< 256; i
++)
308 if (!(mbcinfo
->mbctype
[i
+ 1] & _M1
))
309 bufA
[charcount
++] = i
;
311 ret
= MultiByteToWideChar(newcp
, 0, bufA
, charcount
, bufW
, charcount
);
312 if (ret
!= charcount
)
313 ERR("MultiByteToWideChar of chars failed for cp %d, ret=%d (exp %d), error=%d\n", newcp
, ret
, charcount
, GetLastError());
315 GetStringTypeW(CT_CTYPE1
, bufW
, charcount
, chartypes
);
318 for (i
= 0; i
< 256; i
++)
319 if (!(mbcinfo
->mbctype
[i
+ 1] & _M1
))
321 if (chartypes
[charcount
] & C1_UPPER
)
323 mbcinfo
->mbctype
[i
+ 1] |= _SBUP
;
324 bufW
[charcount
] = tolowerW(bufW
[charcount
]);
326 else if (chartypes
[charcount
] & C1_LOWER
)
328 mbcinfo
->mbctype
[i
+ 1] |= _SBLOW
;
329 bufW
[charcount
] = toupperW(bufW
[charcount
]);
334 ret
= WideCharToMultiByte(newcp
, 0, bufW
, charcount
, bufA
, charcount
, NULL
, NULL
);
335 if (ret
!= charcount
)
336 ERR("WideCharToMultiByte failed for cp %d, ret=%d (exp %d), error=%d\n", newcp
, ret
, charcount
, GetLastError());
339 for (i
= 0; i
< 256; i
++)
341 if(!(mbcinfo
->mbctype
[i
+ 1] & _M1
))
343 if(mbcinfo
->mbctype
[i
] & (C1_UPPER
|C1_LOWER
))
344 mbcinfo
->mbcasemap
[i
] = bufA
[charcount
];
349 if (newcp
== 932) /* CP932 only - set _MP and _MS */
351 /* On Windows it's possible to calculate the _MP and _MS from CT_CTYPE1
352 * and CT_CTYPE3. But as of Wine 0.9.43 we return wrong values what makes
353 * it hard. As this is set only for codepage 932 we hardcode it what gives
354 * also faster execution.
356 for (i
= 161; i
<= 165; i
++)
357 mbcinfo
->mbctype
[i
+ 1] |= _MP
;
358 for (i
= 166; i
<= 223; i
++)
359 mbcinfo
->mbctype
[i
+ 1] |= _MS
;
362 mbcinfo
->mbcodepage
= newcp
;
363 if(MSVCRT_locale
&& mbcinfo
== MSVCRT_locale
->mbcinfo
)
364 memcpy(MSVCRT_mbctype
, MSVCRT_locale
->mbcinfo
->mbctype
, sizeof(MSVCRT_mbctype
));
369 /*********************************************************************
370 * _setmbcp (MSVCRT.@)
372 int CDECL
_setmbcp(int cp
)
374 return _setmbcp_l(cp
, -1, NULL
);
377 /*********************************************************************
378 * _getmbcp (MSVCRT.@)
380 int CDECL
_getmbcp(void)
382 return get_mbcinfo()->mbcodepage
;
385 /*********************************************************************
386 * _mbsnextc(MSVCRT.@)
388 unsigned int CDECL
_mbsnextc(const unsigned char* str
)
391 return *str
<< 8 | str
[1];
395 /*********************************************************************
396 * _mbctolower(MSVCRT.@)
398 unsigned int CDECL
_mbctolower(unsigned int c
)
402 FIXME("Handle MBC chars\n");
405 return tolower(c
); /* ASCII CP or SB char */
408 /*********************************************************************
409 * _mbctoupper(MSVCRT.@)
411 unsigned int CDECL
_mbctoupper(unsigned int c
)
415 FIXME("Handle MBC chars\n");
418 return toupper(c
); /* ASCII CP or SB char */
421 /*********************************************************************
422 * _mbctombb (MSVCRT.@)
424 unsigned int CDECL
_mbctombb(unsigned int c
)
428 if(get_mbcinfo()->mbcodepage
== 932)
430 if(c
>= 0x829f && c
<= 0x82f1) /* Hiragana */
431 return mbctombb_932_kana
[c
- 0x829f];
432 if(c
>= 0x8340 && c
<= 0x8396 && c
!= 0x837f) /* Katakana */
433 return mbctombb_932_kana
[c
- 0x8340 - (c
>= 0x837f ? 1 : 0)];
434 if(c
>= 0x8140 && c
<= 0x8197) /* Punctuation */
436 value
= mbctombb_932_punct
[c
- 0x8140];
437 return value
? value
: c
;
439 if((c
>= 0x824f && c
<= 0x8258) || /* Fullwidth digits */
440 (c
>= 0x8260 && c
<= 0x8279)) /* Fullwidth capitals letters */
442 if(c
>= 0x8281 && c
<= 0x829a) /* Fullwidth small letters */
444 /* all other cases return c */
449 /*********************************************************************
450 * _mbcjistojms(MSVCRT.@)
452 * Converts a jis character to sjis.
453 * Based on description from
454 * http://www.slayers.ne.jp/~oouchi/code/jistosjis.html
456 unsigned int CDECL
_mbcjistojms(unsigned int c
)
458 /* Conversion takes place only when codepage is 932.
459 In all other cases, c is returned unchanged */
460 if(get_mbcinfo()->mbcodepage
== 932)
462 if(HIBYTE(c
) >= 0x21 && HIBYTE(c
) <= 0x7e &&
463 LOBYTE(c
) >= 0x21 && LOBYTE(c
) <= 0x7e)
470 if(LOBYTE(c
) >= 0x7F)
473 c
= (((HIBYTE(c
) - 0x21)/2 + 0x81) << 8) | LOBYTE(c
);
479 return 0; /* Codepage is 932, but c can't be converted */
485 /*********************************************************************
486 * _mbcjmstojis(MSVCRT.@)
488 * Converts a sjis character to jis.
490 unsigned int CDECL
_mbcjmstojis(unsigned int c
)
492 /* Conversion takes place only when codepage is 932.
493 In all other cases, c is returned unchanged */
494 if(get_mbcinfo()->mbcodepage
== 932)
496 if(_ismbclegal(c
) && HIBYTE(c
) < 0xf0)
498 if(HIBYTE(c
) >= 0xe0)
501 c
= (((HIBYTE(c
) - 0x81)*2 + 0x21) << 8) | LOBYTE(c
);
512 return 0; /* Codepage is 932, but c can't be converted */
518 /*********************************************************************
521 unsigned char* CDECL
_mbsdec(const unsigned char* start
, const unsigned char* cur
)
526 if(get_mbcinfo()->ismbcodepage
)
527 return (unsigned char *)(_ismbstrail(start
,cur
-1) ? cur
- 2 : cur
-1);
529 return (unsigned char *)cur
- 1; /* ASCII CP or SB char */
532 /*********************************************************************
535 unsigned int CDECL
_mbclen(const unsigned char* str
)
537 return _ismbblead(*str
) ? 2 : 1;
540 /*********************************************************************
543 unsigned char* CDECL
_mbsinc(const unsigned char* str
)
545 return (unsigned char *)(str
+ _mbclen(str
));
548 /*********************************************************************
551 unsigned char* CDECL
_mbsninc(const unsigned char* str
, MSVCRT_size_t num
)
556 while (num
> 0 && *str
)
558 if (_ismbblead(*str
))
568 return (unsigned char*)str
;
571 /*********************************************************************
574 MSVCRT_size_t CDECL
_mbslen(const unsigned char* str
)
576 MSVCRT_size_t len
= 0;
579 if (_ismbblead(*str
))
582 if (!*str
) /* count only full chars */
591 /*********************************************************************
594 void CDECL
_mbccpy(unsigned char* dest
, const unsigned char* src
)
598 *++dest
= *++src
; /* MB char */
601 /*********************************************************************
604 * The parameter n is the number or characters to copy, not the size of
605 * the buffer. Use _mbsnbcpy for a function analogical to strncpy
607 unsigned char* CDECL
_mbsncpy(unsigned char* dst
, const unsigned char* src
, MSVCRT_size_t n
)
609 unsigned char* ret
= dst
;
612 if (get_mbcinfo()->ismbcodepage
)
617 if (_ismbblead(*src
))
637 if (!(*dst
++ = *src
++)) break;
640 while (n
--) *dst
++ = 0;
644 /*********************************************************************
645 * _mbsnbcpy_s_l(MSVCRT.@)
647 * Unlike _mbsnbcpy this function does not pad the rest of the dest
650 int CDECL
_mbsnbcpy_s_l(unsigned char* dst
, MSVCRT_size_t size
,
651 const unsigned char* src
, MSVCRT_size_t n
, MSVCRT__locale_t locale
)
653 MSVCRT_size_t pos
= 0;
655 if(!dst
|| size
== 0)
656 return MSVCRT_EINVAL
;
660 return MSVCRT_EINVAL
;
665 if((locale
? locale
->mbcinfo
: get_mbcinfo())->ismbcodepage
)
673 return MSVCRT_ERANGE
;
675 is_lead
= (!is_lead
&& _ismbblead(*src
));
680 if (is_lead
) /* if string ends with a lead, remove it */
691 return MSVCRT_ERANGE
;
704 return MSVCRT_ERANGE
;
710 /*********************************************************************
711 * _mbsnbcpy_s(MSVCRT.@)
713 int CDECL
_mbsnbcpy_s(unsigned char* dst
, MSVCRT_size_t size
, const unsigned char* src
, MSVCRT_size_t n
)
715 return _mbsnbcpy_s_l(dst
, size
, src
, n
, NULL
);
718 /*********************************************************************
719 * _mbscpy_s_l(MSVCRT.@)
721 int CDECL
_mbscpy_s_l(unsigned char *dst
, MSVCRT_size_t size
,
722 const unsigned char *src
, MSVCRT__locale_t locale
)
724 return _mbsnbcpy_s_l(dst
, size
, src
, -1, locale
);
727 /*********************************************************************
728 * _mbscpy_s(MSVCRT.@)
730 int CDECL
_mbscpy_s(unsigned char *dst
, MSVCRT_size_t size
, const unsigned char *src
)
732 return _mbscpy_s_l(dst
, size
, src
, NULL
);
735 /*********************************************************************
736 * _mbsnbcpy(MSVCRT.@)
738 * Like strncpy this function doesn't enforce the string to be
741 unsigned char* CDECL
_mbsnbcpy(unsigned char* dst
, const unsigned char* src
, MSVCRT_size_t n
)
743 unsigned char* ret
= dst
;
746 if(get_mbcinfo()->ismbcodepage
)
751 is_lead
= (!is_lead
&& _ismbblead(*src
));
756 if (is_lead
) /* if string ends with a lead, remove it */
764 if (!(*dst
++ = *src
++)) break;
767 while (n
--) *dst
++ = 0;
771 /*********************************************************************
774 int CDECL
_mbscmp(const unsigned char* str
, const unsigned char* cmp
)
776 if (!str
|| !cmp
) return INT_MAX
;
778 if(get_mbcinfo()->ismbcodepage
)
780 unsigned int strc
, cmpc
;
783 return *cmp
? -1 : 0;
786 strc
= _mbsnextc(str
);
787 cmpc
= _mbsnextc(cmp
);
789 return strc
< cmpc
? -1 : 1;
790 str
+=(strc
> 255) ? 2 : 1;
791 cmp
+=(strc
> 255) ? 2 : 1; /* equal, use same increment */
794 return u_strcmp(str
, cmp
); /* ASCII CP */
797 /*********************************************************************
798 * _mbsnbicoll_l(MSVCRT.@)
800 int CDECL
_mbsnbicoll_l(const unsigned char *str1
, const unsigned char *str2
, MSVCRT_size_t len
, MSVCRT__locale_t locale
)
802 MSVCRT_pthreadmbcinfo mbcinfo
;
805 mbcinfo
= get_mbcinfo();
807 mbcinfo
= locale
->mbcinfo
;
809 if(!mbcinfo
->ismbcodepage
)
810 return MSVCRT__strnicoll_l((const char*)str1
, (const char*)str2
, len
, locale
);
811 return CompareStringA(mbcinfo
->mblcid
, NORM_IGNORECASE
, (const char*)str1
, len
, (const char*)str2
, len
)-CSTR_EQUAL
;
814 /*********************************************************************
815 * _mbsicoll_l(MSVCRT.@)
817 int CDECL
_mbsicoll_l(const unsigned char *str1
, const unsigned char *str2
, MSVCRT__locale_t locale
)
819 return _mbsnbicoll_l(str1
, str2
, -1, locale
);
822 /*********************************************************************
823 * _mbsnbicoll(MSVCRT.@)
825 int CDECL
_mbsnbicoll(const unsigned char *str1
, const unsigned char *str2
, MSVCRT_size_t len
)
827 return _mbsnbicoll_l(str1
, str2
, len
, NULL
);
830 /*********************************************************************
831 * _mbsicoll(MSVCRT.@)
833 int CDECL
_mbsicoll(const unsigned char* str
, const unsigned char* cmp
)
835 #if _MSVCR_VER>=60 && _MSVCR_VER<=71
836 return CompareStringA(get_mbcinfo()->mblcid
, NORM_IGNORECASE
,
837 (const char*)str
, -1, (const char*)cmp
, -1)-CSTR_EQUAL
;
839 return _mbsnbicoll_l(str
, cmp
, -1, NULL
);
843 /*********************************************************************
844 * _mbsnbcoll_l(MSVCRT.@)
846 int CDECL
_mbsnbcoll_l(const unsigned char *str1
, const unsigned char *str2
, MSVCRT_size_t len
, MSVCRT__locale_t locale
)
848 MSVCRT_pthreadmbcinfo mbcinfo
;
851 mbcinfo
= get_mbcinfo();
853 mbcinfo
= locale
->mbcinfo
;
855 if(!mbcinfo
->ismbcodepage
)
856 return MSVCRT__strncoll_l((const char*)str1
, (const char*)str2
, len
, locale
);
857 return CompareStringA(mbcinfo
->mblcid
, 0, (const char*)str1
, len
, (const char*)str2
, len
)-CSTR_EQUAL
;
860 /*********************************************************************
861 * _mbscoll_l(MSVCRT.@)
863 int CDECL
_mbscoll_l(const unsigned char *str1
, const unsigned char *str2
, MSVCRT__locale_t locale
)
865 return _mbsnbcoll_l(str1
, str2
, -1, locale
);
868 /*********************************************************************
869 * _mbsnbcoll(MSVCRT.@)
871 int CDECL
_mbsnbcoll(const unsigned char *str1
, const unsigned char *str2
, MSVCRT_size_t len
)
873 return _mbsnbcoll_l(str1
, str2
, len
, NULL
);
876 /*********************************************************************
879 int CDECL
_mbscoll(const unsigned char* str
, const unsigned char* cmp
)
881 #if _MSVCR_VER>=60 && _MSVCR_VER<=71
882 return CompareStringA(get_mbcinfo()->mblcid
, 0,
883 (const char*)str
, -1, (const char*)cmp
, -1)-CSTR_EQUAL
;
885 return _mbsnbcoll_l(str
, cmp
, -1, NULL
);
889 /*********************************************************************
892 int CDECL
_mbsicmp(const unsigned char* str
, const unsigned char* cmp
)
894 if(get_mbcinfo()->ismbcodepage
)
896 unsigned int strc
, cmpc
;
899 return *cmp
? -1 : 0;
902 strc
= _mbctolower(_mbsnextc(str
));
903 cmpc
= _mbctolower(_mbsnextc(cmp
));
905 return strc
< cmpc
? -1 : 1;
906 str
+=(strc
> 255) ? 2 : 1;
907 cmp
+=(strc
> 255) ? 2 : 1; /* equal, use same increment */
910 return u_strcasecmp(str
, cmp
); /* ASCII CP */
913 /*********************************************************************
916 int CDECL
_mbsncmp(const unsigned char* str
, const unsigned char* cmp
, MSVCRT_size_t len
)
921 if(get_mbcinfo()->ismbcodepage
)
923 unsigned int strc
, cmpc
;
928 return *cmp
? -1 : 0;
931 strc
= _mbsnextc(str
);
932 cmpc
= _mbsnextc(cmp
);
934 return strc
< cmpc
? -1 : 1;
935 inc
=(strc
> 255) ? 2 : 1; /* Equal, use same increment */
939 return 0; /* Matched len chars */
941 return u_strncmp(str
, cmp
, len
); /* ASCII CP */
944 /*********************************************************************
945 * _mbsnbcmp(MSVCRT.@)
947 int CDECL
_mbsnbcmp(const unsigned char* str
, const unsigned char* cmp
, MSVCRT_size_t len
)
951 if(get_mbcinfo()->ismbcodepage
)
953 unsigned int strc
, cmpc
;
958 return *cmp
? -1 : 0;
961 if (_ismbblead(*str
))
963 strc
=(len
>=2)?_mbsnextc(str
):0;
971 if (_ismbblead(*cmp
))
972 cmpc
=(len
>=2)?_mbsnextc(cmp
):0;
976 return strc
< cmpc
? -1 : 1;
981 return 0; /* Matched len chars */
983 return u_strncmp(str
,cmp
,len
);
986 /*********************************************************************
987 * _mbsnicmp(MSVCRT.@)
989 * Compare two multibyte strings case insensitively to 'len' characters.
991 int CDECL
_mbsnicmp(const unsigned char* str
, const unsigned char* cmp
, MSVCRT_size_t len
)
993 /* FIXME: No tolower() for mb strings yet */
994 if(get_mbcinfo()->ismbcodepage
)
996 unsigned int strc
, cmpc
;
1000 return *cmp
? -1 : 0;
1003 strc
= _mbctolower(_mbsnextc(str
));
1004 cmpc
= _mbctolower(_mbsnextc(cmp
));
1006 return strc
< cmpc
? -1 : 1;
1007 str
+=(strc
> 255) ? 2 : 1;
1008 cmp
+=(strc
> 255) ? 2 : 1; /* Equal, use same increment */
1010 return 0; /* Matched len chars */
1012 return u_strncasecmp(str
, cmp
, len
); /* ASCII CP */
1015 /*********************************************************************
1016 * _mbsnbicmp(MSVCRT.@)
1018 int CDECL
_mbsnbicmp(const unsigned char* str
, const unsigned char* cmp
, MSVCRT_size_t len
)
1022 if(get_mbcinfo()->ismbcodepage
)
1024 unsigned int strc
, cmpc
;
1029 return *cmp
? -1 : 0;
1032 if (_ismbblead(*str
))
1034 strc
=(len
>=2)?_mbsnextc(str
):0;
1042 if (_ismbblead(*cmp
))
1043 cmpc
=(len
>=2)?_mbsnextc(cmp
):0;
1046 strc
= _mbctolower(strc
);
1047 cmpc
= _mbctolower(cmpc
);
1049 return strc
< cmpc
? -1 : 1;
1054 return 0; /* Matched len bytes */
1056 return u_strncasecmp(str
,cmp
,len
);
1059 /*********************************************************************
1060 * _mbscat (MSVCRT.@)
1062 unsigned char * CDECL
_mbscat( unsigned char *dst
, const unsigned char *src
)
1064 strcat( (char *)dst
, (const char *)src
);
1068 /*********************************************************************
1069 * _mbscat_s_l (MSVCRT.@)
1071 int CDECL
_mbscat_s_l( unsigned char *dst
, MSVCRT_size_t size
,
1072 const unsigned char *src
, MSVCRT__locale_t locale
)
1077 if(!MSVCRT_CHECK_PMT(dst
!= NULL
)) return MSVCRT_EINVAL
;
1078 if(!MSVCRT_CHECK_PMT(src
!= NULL
)) return MSVCRT_EINVAL
;
1080 for(i
=0; i
<size
; i
++)
1083 MSVCRT_INVALID_PMT("dst is not NULL-terminated", MSVCRT_EINVAL
);
1084 if(size
) dst
[0] = 0;
1085 return MSVCRT_EINVAL
;
1088 if(i
&& _ismbblead_l(dst
[i
-1], locale
)) {
1089 ret
= MSVCRT_EILSEQ
;
1093 for(j
=0; src
[j
] && i
+j
<size
; j
++)
1096 MSVCRT_INVALID_PMT("dst buffer is too small", MSVCRT_ERANGE
);
1098 return MSVCRT_ERANGE
;
1101 if(j
&& _ismbblead_l(src
[j
-1], locale
)) {
1102 ret
= MSVCRT_EILSEQ
;
1110 /*********************************************************************
1111 * _mbscat_s (MSVCRT.@)
1113 int CDECL
_mbscat_s( unsigned char *dst
, MSVCRT_size_t size
, const unsigned char *src
)
1115 return _mbscat_s_l(dst
, size
, src
, NULL
);
1118 /*********************************************************************
1119 * _mbscpy (MSVCRT.@)
1121 unsigned char* CDECL
_mbscpy( unsigned char *dst
, const unsigned char *src
)
1123 strcpy( (char *)dst
, (const char *)src
);
1127 /*********************************************************************
1128 * _mbsstr (MSVCRT.@)
1130 unsigned char * CDECL
_mbsstr(const unsigned char *haystack
, const unsigned char *needle
)
1132 return (unsigned char *)strstr( (const char *)haystack
, (const char *)needle
);
1135 /*********************************************************************
1138 * Find a multibyte character in a multibyte string.
1140 unsigned char* CDECL
_mbschr(const unsigned char* s
, unsigned int x
)
1142 if(get_mbcinfo()->ismbcodepage
)
1149 return (unsigned char*)s
;
1152 s
+= c
> 255 ? 2 : 1;
1155 return u_strchr(s
, x
); /* ASCII CP */
1158 /*********************************************************************
1159 * _mbsrchr(MSVCRT.@)
1161 unsigned char* CDECL
_mbsrchr(const unsigned char* s
, unsigned int x
)
1163 if(get_mbcinfo()->ismbcodepage
)
1166 unsigned char* match
=NULL
;
1172 match
=(unsigned char*)s
;
1175 s
+=(c
> 255) ? 2 : 1;
1178 return u_strrchr(s
, x
);
1181 /*********************************************************************
1182 * _mbstok_s_l(MSVCRT.@)
1184 unsigned char* CDECL
_mbstok_s_l(unsigned char *str
, const unsigned char *delim
,
1185 unsigned char **ctx
, MSVCRT__locale_t locale
)
1187 MSVCRT_pthreadmbcinfo mbcinfo
;
1190 if(!MSVCRT_CHECK_PMT(delim
!= NULL
)) return NULL
;
1191 if(!MSVCRT_CHECK_PMT(ctx
!= NULL
)) return NULL
;
1192 if(!MSVCRT_CHECK_PMT(str
|| *ctx
)) return NULL
;
1195 mbcinfo
= locale
->mbcinfo
;
1197 mbcinfo
= get_mbcinfo();
1199 if(!mbcinfo
->ismbcodepage
)
1200 return (unsigned char*)MSVCRT_strtok_s((char*)str
, (const char*)delim
, (char**)ctx
);
1205 while((c
=_mbsnextc(str
)) && _mbschr(delim
, c
))
1206 str
+= c
>255 ? 2 : 1;
1210 *ctx
= str
+ (c
>255 ? 2 : 1);
1211 while((c
=_mbsnextc(*ctx
)) && !_mbschr(delim
, c
))
1212 *ctx
+= c
>255 ? 2 : 1;
1223 /*********************************************************************
1224 * _mbstok_s(MSVCRT.@)
1226 unsigned char* CDECL
_mbstok_s(unsigned char *str
,
1227 const unsigned char *delim
, unsigned char **ctx
)
1229 return _mbstok_s_l(str
, delim
, ctx
, NULL
);
1232 /*********************************************************************
1233 * _mbstok_l(MSVCRT.@)
1235 unsigned char* CDECL
_mbstok_l(unsigned char *str
,
1236 const unsigned char *delim
, MSVCRT__locale_t locale
)
1238 return _mbstok_s_l(str
, delim
, &msvcrt_get_thread_data()->mbstok_next
, locale
);
1241 /*********************************************************************
1244 unsigned char* CDECL
_mbstok(unsigned char *str
, const unsigned char *delim
)
1246 thread_data_t
*data
= msvcrt_get_thread_data();
1249 if(!str
&& !data
->mbstok_next
)
1253 return _mbstok_s_l(str
, delim
, &data
->mbstok_next
, NULL
);
1256 /*********************************************************************
1257 * _mbbtombc(MSVCRT.@)
1259 unsigned int CDECL
_mbbtombc(unsigned int c
)
1261 if(get_mbcinfo()->mbcodepage
== 932)
1263 if(c
>= 0x20 && c
<= 0x7e) {
1264 if((c
>= 0x41 && c
<= 0x5a) || (c
>= 0x61 && c
<= 0x7a) || (c
>= 0x30 && c
<= 0x39))
1265 return mbbtombc_932
[c
- 0x20] | 0x8200;
1267 return mbbtombc_932
[c
- 0x20] | 0x8100;
1269 else if(c
>= 0xa1 && c
<= 0xdf) {
1270 if(c
>= 0xa6 && c
<= 0xdd && c
!= 0xb0)
1271 return mbbtombc_932
[c
- 0xa1 + 0x5f] | 0x8300;
1273 return mbbtombc_932
[c
- 0xa1 + 0x5f] | 0x8100;
1276 return c
; /* not Japanese or no MB char */
1279 /*********************************************************************
1280 * _mbbtype(MSVCRT.@)
1282 int CDECL
_mbbtype(unsigned char c
, int type
)
1286 if ((c
>= 0x20 && c
<= 0x7e) || (c
>= 0xa1 && c
<= 0xdf))
1288 else if ((c
>= 0x40 && c
<= 0x7e) || (c
>= 0x80 && c
<= 0xfc))
1291 return _MBC_ILLEGAL
;
1295 if ((c
>= 0x20 && c
<= 0x7e) || (c
>= 0xa1 && c
<= 0xdf))
1297 else if ((c
>= 0x81 && c
<= 0x9f) || (c
>= 0xe0 && c
<= 0xfc))
1300 return _MBC_ILLEGAL
;
1304 /*********************************************************************
1305 * _ismbbkana(MSVCRT.@)
1307 int CDECL
_ismbbkana(unsigned int c
)
1309 /* FIXME: use lc_ctype when supported, not lc_all */
1310 if(get_mbcinfo()->mbcodepage
== 932)
1312 /* Japanese/Katakana, CP 932 */
1313 return (c
>= 0xa1 && c
<= 0xdf);
1318 /*********************************************************************
1319 * _ismbcdigit(MSVCRT.@)
1321 int CDECL
_ismbcdigit(unsigned int ch
)
1323 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1324 return (get_char_typeW( wch
) & C1_DIGIT
);
1327 /*********************************************************************
1328 * _ismbcgraph(MSVCRT.@)
1330 int CDECL
_ismbcgraph(unsigned int ch
)
1332 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1333 return (get_char_typeW( wch
) & (C1_UPPER
| C1_LOWER
| C1_DIGIT
| C1_PUNCT
| C1_ALPHA
));
1336 /*********************************************************************
1337 * _ismbcalpha (MSVCRT.@)
1339 int CDECL
_ismbcalpha(unsigned int ch
)
1341 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1342 return (get_char_typeW( wch
) & C1_ALPHA
);
1345 /*********************************************************************
1346 * _ismbclower (MSVCRT.@)
1348 int CDECL
_ismbclower(unsigned int ch
)
1350 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1351 return (get_char_typeW( wch
) & C1_UPPER
);
1354 /*********************************************************************
1355 * _ismbcupper (MSVCRT.@)
1357 int CDECL
_ismbcupper(unsigned int ch
)
1359 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1360 return (get_char_typeW( wch
) & C1_LOWER
);
1363 /*********************************************************************
1364 * _ismbcsymbol(MSVCRT.@)
1366 int CDECL
_ismbcsymbol(unsigned int ch
)
1368 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1370 if (!GetStringTypeW(CT_CTYPE3
, &wch
, 1, &ctype
))
1372 WARN("GetStringTypeW failed on %x\n", ch
);
1375 return ((ctype
& C3_SYMBOL
) != 0);
1378 /*********************************************************************
1379 * _ismbcalnum (MSVCRT.@)
1381 int CDECL
_ismbcalnum(unsigned int ch
)
1383 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1384 return (get_char_typeW( wch
) & (C1_ALPHA
| C1_DIGIT
));
1387 /*********************************************************************
1388 * _ismbcspace (MSVCRT.@)
1390 int CDECL
_ismbcspace(unsigned int ch
)
1392 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1393 return (get_char_typeW( wch
) & C1_SPACE
);
1396 /*********************************************************************
1397 * _ismbcprint (MSVCRT.@)
1399 int CDECL
_ismbcprint(unsigned int ch
)
1401 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1402 return (get_char_typeW( wch
) & (C1_UPPER
| C1_LOWER
| C1_DIGIT
| C1_PUNCT
| C1_ALPHA
| C1_SPACE
));
1405 /*********************************************************************
1406 * _ismbcpunct(MSVCRT.@)
1408 int CDECL
_ismbcpunct(unsigned int ch
)
1410 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1411 return (get_char_typeW( wch
) & C1_PUNCT
);
1414 /*********************************************************************
1415 * _ismbchira(MSVCRT.@)
1417 int CDECL
_ismbchira(unsigned int c
)
1419 if(get_mbcinfo()->mbcodepage
== 932)
1421 /* Japanese/Hiragana, CP 932 */
1422 return (c
>= 0x829f && c
<= 0x82f1);
1427 /*********************************************************************
1428 * _ismbckata(MSVCRT.@)
1430 int CDECL
_ismbckata(unsigned int c
)
1432 if(get_mbcinfo()->mbcodepage
== 932)
1434 /* Japanese/Katakana, CP 932 */
1435 return (c
>= 0x8340 && c
<= 0x8396 && c
!= 0x837f);
1441 /*********************************************************************
1442 * _ismbblead_l(MSVCRT.@)
1444 int CDECL
_ismbblead_l(unsigned int c
, MSVCRT__locale_t locale
)
1446 MSVCRT_pthreadmbcinfo mbcinfo
;
1449 mbcinfo
= get_mbcinfo();
1451 mbcinfo
= locale
->mbcinfo
;
1453 return (mbcinfo
->mbctype
[(c
&0xff) + 1] & _M1
) != 0;
1456 /*********************************************************************
1457 * _ismbblead(MSVCRT.@)
1459 int CDECL
_ismbblead(unsigned int c
)
1461 return _ismbblead_l(c
, NULL
);
1465 /*********************************************************************
1466 * _ismbbtrail(MSVCRT.@)
1468 int CDECL
_ismbbtrail(unsigned int c
)
1470 return (get_mbcinfo()->mbctype
[(c
&0xff) + 1] & _M2
) != 0;
1473 /*********************************************************************
1474 * _ismbclegal(MSVCRT.@)
1476 int CDECL
_ismbclegal(unsigned int c
)
1478 return _ismbblead(HIBYTE(c
)) && _ismbbtrail(LOBYTE(c
));
1481 /*********************************************************************
1482 * _ismbslead(MSVCRT.@)
1484 int CDECL
_ismbslead(const unsigned char* start
, const unsigned char* str
)
1488 if(!get_mbcinfo()->ismbcodepage
)
1491 /* Lead bytes can also be trail bytes so we need to analyse the string
1493 while (start
<= str
)
1497 lead
= !lead
&& _ismbblead(*start
);
1501 return lead
? -1 : 0;
1504 /*********************************************************************
1505 * _ismbstrail(MSVCRT.@)
1507 int CDECL
_ismbstrail(const unsigned char* start
, const unsigned char* str
)
1509 /* Note: this function doesn't check _ismbbtrail */
1510 if ((str
> start
) && _ismbslead(start
, str
-1))
1516 /*********************************************************************
1517 * _mbsbtype (MSVCRT.@)
1519 int CDECL
_mbsbtype(const unsigned char *str
, MSVCRT_size_t count
)
1522 const unsigned char *end
= str
+ count
;
1524 /* Lead bytes can also be trail bytes so we need to analyse the string.
1525 * Also we must return _MBC_ILLEGAL for chars past the end of the string
1527 while (str
< end
) /* Note: we skip the last byte - will check after the loop */
1530 return _MBC_ILLEGAL
;
1531 lead
= get_mbcinfo()->ismbcodepage
&& !lead
&& _ismbblead(*str
);
1536 if (_ismbbtrail(*str
))
1539 return _MBC_ILLEGAL
;
1541 if (_ismbblead(*str
))
1547 /*********************************************************************
1550 unsigned char* CDECL
_mbsset(unsigned char* str
, unsigned int c
)
1552 unsigned char* ret
= str
;
1554 if(!get_mbcinfo()->ismbcodepage
|| c
< 256)
1555 return u__strset(str
, c
); /* ASCII CP or SB char */
1557 c
&= 0xffff; /* Strip high bits */
1559 while(str
[0] && str
[1])
1565 str
[0] = '\0'; /* FIXME: OK to shorten? */
1570 /*********************************************************************
1571 * _mbsnbset(MSVCRT.@)
1573 unsigned char* CDECL
_mbsnbset(unsigned char *str
, unsigned int c
, MSVCRT_size_t len
)
1575 unsigned char *ret
= str
;
1580 if(!get_mbcinfo()->ismbcodepage
|| c
< 256)
1581 return u__strnset(str
, c
, len
); /* ASCII CP or SB char */
1583 c
&= 0xffff; /* Strip high bits */
1585 while(str
[0] && str
[1] && (len
> 1))
1593 /* as per msdn pad with a blank character */
1600 /*********************************************************************
1601 * _mbsnset(MSVCRT.@)
1603 unsigned char* CDECL
_mbsnset(unsigned char* str
, unsigned int c
, MSVCRT_size_t len
)
1605 unsigned char *ret
= str
;
1610 if(!get_mbcinfo()->ismbcodepage
|| c
< 256)
1611 return u__strnset(str
, c
, len
); /* ASCII CP or SB char */
1613 c
&= 0xffff; /* Strip high bits */
1615 while(str
[0] && str
[1] && len
--)
1621 str
[0] = '\0'; /* FIXME: OK to shorten? */
1626 /*********************************************************************
1627 * _mbsnccnt(MSVCRT.@)
1628 * 'c' is for 'character'.
1630 MSVCRT_size_t CDECL
_mbsnccnt(const unsigned char* str
, MSVCRT_size_t len
)
1633 if(get_mbcinfo()->ismbcodepage
)
1636 while(*str
&& len
-- > 0)
1638 if(_ismbblead(*str
))
1651 return min(ret
, len
); /* ASCII CP */
1654 /*********************************************************************
1655 * _mbsnbcnt(MSVCRT.@)
1656 * 'b' is for byte count.
1658 MSVCRT_size_t CDECL
_mbsnbcnt(const unsigned char* str
, MSVCRT_size_t len
)
1661 if(get_mbcinfo()->ismbcodepage
)
1663 const unsigned char* xstr
= str
;
1664 while(*xstr
&& len
-- > 0)
1666 if (_ismbblead(*xstr
++))
1672 return min(ret
, len
); /* ASCII CP */
1675 /*********************************************************************
1676 * _mbsnbcat(MSVCRT.@)
1678 unsigned char* CDECL
_mbsnbcat(unsigned char* dst
, const unsigned char* src
, MSVCRT_size_t len
)
1680 if(get_mbcinfo()->ismbcodepage
)
1682 unsigned char *res
= dst
;
1684 if (_ismbblead(*dst
++)) {
1688 /* as per msdn overwrite the lead byte in front of '\0' */
1694 while (*src
&& len
--) *dst
++ = *src
++;
1698 return u_strncat(dst
, src
, len
); /* ASCII CP */
1701 int CDECL
_mbsnbcat_s(unsigned char *dst
, MSVCRT_size_t size
, const unsigned char *src
, MSVCRT_size_t len
)
1703 unsigned char *ptr
= dst
;
1706 if (!dst
&& !size
&& !src
&& !len
)
1709 if (!dst
|| !size
|| !src
)
1714 *MSVCRT__errno() = MSVCRT_EINVAL
;
1715 return MSVCRT_EINVAL
;
1718 /* Find the null terminator of the destination buffer. */
1719 while (size
&& *ptr
)
1725 *MSVCRT__errno() = MSVCRT_EINVAL
;
1726 return MSVCRT_EINVAL
;
1729 /* If necessary, check that the character preceding the null terminator is
1730 * a lead byte and move the pointer back by one for later overwrite. */
1731 if (ptr
!= dst
&& get_mbcinfo()->ismbcodepage
&& _ismbblead(*(ptr
- 1)))
1734 for (i
= 0; *src
&& i
< len
; i
++)
1742 *MSVCRT__errno() = MSVCRT_ERANGE
;
1743 return MSVCRT_ERANGE
;
1751 /*********************************************************************
1752 * _mbsncat(MSVCRT.@)
1754 unsigned char* CDECL
_mbsncat(unsigned char* dst
, const unsigned char* src
, MSVCRT_size_t len
)
1756 if(get_mbcinfo()->ismbcodepage
)
1758 unsigned char *res
= dst
;
1761 if (_ismbblead(*dst
++))
1764 while (*src
&& len
--)
1767 if(_ismbblead(*src
++))
1773 return u_strncat(dst
, src
, len
); /* ASCII CP */
1777 /*********************************************************************
1780 unsigned char* CDECL
_mbslwr(unsigned char* s
)
1782 unsigned char *ret
= s
;
1785 if (get_mbcinfo()->ismbcodepage
)
1790 c
= _mbctolower(_mbsnextc(s
));
1791 /* Note that I assume that the size of the character is unchanged */
1800 else for ( ; *s
; s
++) *s
= tolower(*s
);
1805 /*********************************************************************
1806 * _mbslwr_s(MSVCRT.@)
1808 int CDECL
_mbslwr_s(unsigned char* s
, MSVCRT_size_t len
)
1814 else if (!s
|| !len
)
1816 *MSVCRT__errno() = MSVCRT_EINVAL
;
1817 return MSVCRT_EINVAL
;
1819 if (get_mbcinfo()->ismbcodepage
)
1822 for ( ; *s
&& len
> 0; len
--)
1824 c
= _mbctolower(_mbsnextc(s
));
1825 /* Note that I assume that the size of the character is unchanged */
1834 else for ( ; *s
&& len
> 0; s
++, len
--) *s
= tolower(*s
);
1838 *MSVCRT__errno() = MSVCRT_EINVAL
;
1839 return MSVCRT_EINVAL
;
1845 /*********************************************************************
1848 unsigned char* CDECL
_mbsupr(unsigned char* s
)
1850 unsigned char *ret
= s
;
1853 if (get_mbcinfo()->ismbcodepage
)
1858 c
= _mbctoupper(_mbsnextc(s
));
1859 /* Note that I assume that the size of the character is unchanged */
1868 else for ( ; *s
; s
++) *s
= toupper(*s
);
1873 /*********************************************************************
1874 * _mbsupr_s(MSVCRT.@)
1876 int CDECL
_mbsupr_s(unsigned char* s
, MSVCRT_size_t len
)
1882 else if (!s
|| !len
)
1884 *MSVCRT__errno() = MSVCRT_EINVAL
;
1885 return MSVCRT_EINVAL
;
1887 if (get_mbcinfo()->ismbcodepage
)
1890 for ( ; *s
&& len
> 0; len
--)
1892 c
= _mbctoupper(_mbsnextc(s
));
1893 /* Note that I assume that the size of the character is unchanged */
1902 else for ( ; *s
&& len
> 0; s
++, len
--) *s
= toupper(*s
);
1906 *MSVCRT__errno() = MSVCRT_EINVAL
;
1907 return MSVCRT_EINVAL
;
1913 /*********************************************************************
1914 * _mbsspn (MSVCRT.@)
1916 MSVCRT_size_t CDECL
_mbsspn(const unsigned char* string
, const unsigned char* set
)
1918 const unsigned char *p
, *q
;
1920 for (p
= string
; *p
; p
++)
1924 for (q
= set
; *q
; q
++)
1928 if ((*p
== *q
) && (p
[1] == q
[1]))
1932 if (!q
[0] || !q
[1]) break;
1936 for (q
= set
; *q
; q
++)
1945 /*********************************************************************
1946 * _mbsspnp (MSVCRT.@)
1948 unsigned char* CDECL
_mbsspnp(const unsigned char* string
, const unsigned char* set
)
1950 const unsigned char *p
, *q
;
1952 for (p
= string
; *p
; p
++)
1956 for (q
= set
; *q
; q
++)
1960 if ((*p
== *q
) && (p
[1] == q
[1]))
1964 if (!q
[0] || !q
[1]) break;
1968 for (q
= set
; *q
; q
++)
1976 return (unsigned char *)p
;
1979 /*********************************************************************
1980 * _mbscspn(MSVCRT.@)
1982 MSVCRT_size_t CDECL
_mbscspn(const unsigned char* str
, const unsigned char* cmp
)
1984 if (get_mbcinfo()->ismbcodepage
)
1985 FIXME("don't handle double character case\n");
1986 return u_strcspn(str
, cmp
);
1989 /*********************************************************************
1990 * _mbsrev (MSVCRT.@)
1992 unsigned char* CDECL
_mbsrev(unsigned char* str
)
1994 int i
, len
= _mbslen(str
);
1995 unsigned char *p
, *temp
=MSVCRT_malloc(len
*2);
2000 /* unpack multibyte string to temp buffer */
2002 for(i
=0; i
<len
; i
++)
2016 /* repack it in the reverse order */
2018 for(i
=len
-1; i
>=0; i
--)
2020 if(_ismbblead(temp
[i
*2]))
2036 /*********************************************************************
2037 * _mbspbrk (MSVCRT.@)
2039 unsigned char* CDECL
_mbspbrk(const unsigned char* str
, const unsigned char* accept
)
2041 const unsigned char* p
;
2045 for(p
= accept
; *p
; p
+= (_ismbblead(*p
)?2:1) )
2048 if( !_ismbblead(*p
) || ( *(p
+1) == *(str
+1) ) )
2049 return (unsigned char*)str
;
2051 str
+= (_ismbblead(*str
)?2:1);
2058 * Functions depending on locale codepage
2061 /*********************************************************************
2064 * Unlike most of the multibyte string functions this function uses
2065 * the locale codepage, not the codepage set by _setmbcp
2067 int CDECL
MSVCRT_mblen(const char* str
, MSVCRT_size_t size
)
2069 if (str
&& *str
&& size
)
2071 if(get_locinfo()->mb_cur_max
== 1)
2072 return 1; /* ASCII CP */
2074 return !MSVCRT_isleadbyte((unsigned char)*str
) ? 1 : (size
>1 ? 2 : -1);
2079 /*********************************************************************
2082 MSVCRT_size_t CDECL
MSVCRT_mbrlen(const char *str
, MSVCRT_size_t len
, MSVCRT_mbstate_t
*state
)
2084 MSVCRT_mbstate_t s
= (state
? *state
: 0);
2087 if(!len
|| !str
|| !*str
)
2090 if(get_locinfo()->mb_cur_max
== 1) {
2092 }else if(!s
&& MSVCRT_isleadbyte((unsigned char)*str
)) {
2094 s
= (unsigned char)*str
;
2111 /*********************************************************************
2112 * _mbstrlen_l(MSVCRT.@)
2114 MSVCRT_size_t CDECL
_mbstrlen_l(const char* str
, MSVCRT__locale_t locale
)
2116 MSVCRT_pthreadlocinfo locinfo
;
2119 locinfo
= get_locinfo();
2121 locinfo
= locale
->locinfo
;
2123 if(locinfo
->mb_cur_max
> 1) {
2125 len
= MultiByteToWideChar(locinfo
->lc_codepage
, MB_ERR_INVALID_CHARS
,
2128 *MSVCRT__errno() = MSVCRT_EILSEQ
;
2137 /*********************************************************************
2138 * _mbstrlen(MSVCRT.@)
2140 MSVCRT_size_t CDECL
_mbstrlen(const char* str
)
2142 return _mbstrlen_l(str
, NULL
);
2145 /*********************************************************************
2146 * _mbtowc_l(MSVCRT.@)
2148 int CDECL
MSVCRT_mbtowc_l(MSVCRT_wchar_t
*dst
, const char* str
, MSVCRT_size_t n
, MSVCRT__locale_t locale
)
2150 MSVCRT_pthreadlocinfo locinfo
;
2151 MSVCRT_wchar_t tmpdst
= '\0';
2154 locinfo
= get_locinfo();
2156 locinfo
= locale
->locinfo
;
2160 if(!locinfo
->lc_codepage
)
2161 tmpdst
= (unsigned char)*str
;
2162 else if(!MultiByteToWideChar(locinfo
->lc_codepage
, 0, str
, n
, &tmpdst
, 1))
2166 /* return the number of bytes from src that have been used */
2169 if(n
>= 2 && MSVCRT__isleadbyte_l((unsigned char)*str
, locale
) && str
[1])
2174 /*********************************************************************
2177 int CDECL
MSVCRT_mbtowc(MSVCRT_wchar_t
*dst
, const char* str
, MSVCRT_size_t n
)
2179 return MSVCRT_mbtowc_l(dst
, str
, n
, NULL
);
2182 /*********************************************************************
2185 MSVCRT_size_t CDECL
MSVCRT_mbrtowc(MSVCRT_wchar_t
*dst
, const char *str
,
2186 MSVCRT_size_t n
, MSVCRT_mbstate_t
*state
)
2188 MSVCRT_pthreadlocinfo locinfo
= get_locinfo();
2189 MSVCRT_mbstate_t s
= (state
? *state
: 0);
2196 if(!n
|| !str
|| !*str
)
2199 if(locinfo
->mb_cur_max
== 1) {
2200 tmpstr
[len
++] = *str
;
2201 }else if(!s
&& MSVCRT_isleadbyte((unsigned char)*str
)) {
2203 s
= (unsigned char)*str
;
2211 tmpstr
[len
++] = *str
;
2220 if(!MultiByteToWideChar(locinfo
->lc_codepage
, 0, tmpstr
, len
, dst
, dst
? 1 : 0))
2229 /*********************************************************************
2230 * _mbstowcs_l(MSVCRT.@)
2232 MSVCRT_size_t CDECL
MSVCRT__mbstowcs_l(MSVCRT_wchar_t
*wcstr
, const char *mbstr
,
2233 MSVCRT_size_t count
, MSVCRT__locale_t locale
)
2235 MSVCRT_pthreadlocinfo locinfo
;
2236 MSVCRT_size_t i
, size
;
2239 *MSVCRT__errno() = MSVCRT_EINVAL
;
2244 locinfo
= get_locinfo();
2246 locinfo
= locale
->locinfo
;
2248 /* Ignore count parameter */
2250 return MultiByteToWideChar(locinfo
->lc_codepage
, 0, mbstr
, -1, NULL
, 0)-1;
2252 for(i
=0, size
=0; i
<count
; i
++) {
2253 if(mbstr
[size
] == '\0')
2256 size
+= (MSVCRT__isleadbyte_l((unsigned char)mbstr
[size
], locale
) ? 2 : 1);
2259 size
= MultiByteToWideChar(locinfo
->lc_codepage
, 0,
2260 mbstr
, size
, wcstr
, count
);
2262 if(size
<count
&& wcstr
)
2268 /*********************************************************************
2269 * mbstowcs(MSVCRT.@)
2271 MSVCRT_size_t CDECL
MSVCRT_mbstowcs(MSVCRT_wchar_t
*wcstr
,
2272 const char *mbstr
, MSVCRT_size_t count
)
2274 return MSVCRT__mbstowcs_l(wcstr
, mbstr
, count
, NULL
);
2277 /*********************************************************************
2278 * _mbstowcs_s_l(MSVCRT.@)
2280 int CDECL
MSVCRT__mbstowcs_s_l(MSVCRT_size_t
*ret
, MSVCRT_wchar_t
*wcstr
,
2281 MSVCRT_size_t size
, const char *mbstr
,
2282 MSVCRT_size_t count
, MSVCRT__locale_t locale
)
2286 if(!wcstr
&& !size
) {
2287 conv
= MSVCRT__mbstowcs_l(NULL
, mbstr
, 0, locale
);
2293 if (!MSVCRT_CHECK_PMT(wcstr
!= NULL
)) return MSVCRT_EINVAL
;
2294 if (!MSVCRT_CHECK_PMT(mbstr
!= NULL
)) {
2295 if(size
) wcstr
[0] = '\0';
2296 return MSVCRT_EINVAL
;
2299 if(count
==MSVCRT__TRUNCATE
|| size
<count
)
2304 conv
= MSVCRT__mbstowcs_l(wcstr
, mbstr
, conv
, locale
);
2306 wcstr
[conv
++] = '\0';
2307 else if(conv
==size
&& (count
==MSVCRT__TRUNCATE
|| wcstr
[conv
-1]=='\0'))
2308 wcstr
[conv
-1] = '\0';
2310 MSVCRT_INVALID_PMT("wcstr[size] is too small", MSVCRT_ERANGE
);
2313 return MSVCRT_ERANGE
;
2321 /*********************************************************************
2322 * mbstowcs_s(MSVCRT.@)
2324 int CDECL
MSVCRT__mbstowcs_s(MSVCRT_size_t
*ret
, MSVCRT_wchar_t
*wcstr
,
2325 MSVCRT_size_t size
, const char *mbstr
, MSVCRT_size_t count
)
2327 return MSVCRT__mbstowcs_s_l(ret
, wcstr
, size
, mbstr
, count
, NULL
);
2330 /*********************************************************************
2331 * mbsrtowcs(MSVCRT.@)
2333 MSVCRT_size_t CDECL
MSVCRT_mbsrtowcs(MSVCRT_wchar_t
*wcstr
,
2334 const char **pmbstr
, MSVCRT_size_t count
, MSVCRT_mbstate_t
*state
)
2336 MSVCRT_mbstate_t s
= (state
? *state
: 0);
2337 MSVCRT_wchar_t tmpdst
;
2338 MSVCRT_size_t ret
= 0;
2340 if(!MSVCRT_CHECK_PMT(pmbstr
!= NULL
))
2343 while(!wcstr
|| count
>ret
) {
2344 int ch_len
= MSVCRT_mbrtowc(&tmpdst
, *pmbstr
, 2, &s
);
2346 wcstr
[ret
] = tmpdst
;
2350 }else if(ch_len
== 0) {
2362 /*********************************************************************
2363 * _mbctohira (MSVCRT.@)
2365 * Converts a sjis katakana character to hiragana.
2367 unsigned int CDECL
_mbctohira(unsigned int c
)
2369 if(_ismbckata(c
) && c
<= 0x8393)
2370 return (c
- 0x8340 - (c
>= 0x837f ? 1 : 0)) + 0x829f;
2374 /*********************************************************************
2375 * _mbctokata (MSVCRT.@)
2377 * Converts a sjis hiragana character to katakana.
2379 unsigned int CDECL
_mbctokata(unsigned int c
)
2382 return (c
- 0x829f) + 0x8340 + (c
>= 0x82de ? 1 : 0);