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.
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt
);
37 unsigned char MSVCRT_mbctype
[257] = { 0 };
39 /* It seems that the data about valid trail bytes is not available from kernel32
40 * so we have to store is here. The format is the same as for lead bytes in CPINFO */
41 struct cp_extra_info_t
44 BYTE TrailBytes
[MAX_LEADBYTES
];
47 static struct cp_extra_info_t g_cpextrainfo
[] =
49 {932, {0x40, 0x7e, 0x80, 0xfc, 0, 0}},
50 {936, {0x40, 0xfe, 0, 0}},
51 {949, {0x41, 0xfe, 0, 0}},
52 {950, {0x40, 0x7e, 0xa1, 0xfe, 0, 0}},
53 {1361, {0x31, 0x7e, 0x81, 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 /* Maps cp932 single byte character to multi byte character */
59 static const unsigned char mbbtombc_932
[] = {
60 0x40,0x49,0x68,0x94,0x90,0x93,0x95,0x66,0x69,0x6a,0x96,0x7b,0x43,0x7c,0x44,0x5e,
61 0x4f,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x46,0x47,0x83,0x81,0x84,0x48,
62 0x97,0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,
63 0x6f,0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x6d,0x8f,0x6e,0x4f,0x76,
64 0x77,0x78,0x79,0x6d,0x8f,0x6e,0x4f,0x51,0x65,0x81,0x82,0x83,0x84,0x85,0x86,0x87,
65 0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f,0x90,0x91,0x92,0x93,0x94,0x95,0x50,
66 0x42,0x75,0x76,0x41,0x45,0x92,0x40,0x42,0x44,0x46,0x48,0x83,0x85,0x87,0x62,
67 0x5b,0x41,0x43,0x45,0x47,0x49,0x4a,0x4c,0x4e,0x50,0x52,0x54,0x56,0x58,0x5a,0x5c,
68 0x5e,0x60,0x63,0x65,0x67,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x71,0x74,0x77,0x7a,0x7d,
69 0x7e,0x80,0x81,0x82,0x84,0x86,0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8f,0x93,0x4a,0x4b };
71 /* Maps multibyte cp932 punctuation marks to single byte equivalents */
72 static const unsigned char mbctombb_932_punct
[] = {
73 0x20,0xa4,0xa1,0x2c,0x2e,0xa5,0x3a,0x3b,0x3f,0x21,0xde,0xdf,0x00,0x00,0x00,0x5e,
74 0x7e,0x5f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xb0,0x00,0x00,0x2f,0x00,
75 0x00,0x00,0x7c,0x00,0x00,0x60,0x27,0x00,0x22,0x28,0x29,0x00,0x00,0x5b,0x5d,0x7b,
76 0x7d,0x00,0x00,0x00,0x00,0xa2,0xa3,0x00,0x00,0x00,0x00,0x2b,0x2d,0x00,0x00,0x00,
77 0x00,0x3d,0x00,0x3c,0x3e,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x5c,
78 0x24,0x00,0x00,0x25,0x23,0x26,0x2a,0x40};
80 /* Maps multibyte cp932 hiragana/katakana to single-byte equivalents */
81 static const unsigned char mbctombb_932_kana
[] = {
82 0xa7,0xb1,0xa8,0xb2,0xa9,0xb3,0xaa,0xb4,0xab,0xb5,0xb6,0xb6,0xb7,0xb7,0xb8,0xb8,
83 0xb9,0xb9,0xba,0xba,0xbb,0xbb,0xbc,0xbc,0xbd,0xbd,0xbe,0xbe,0xbf,0xbf,0xc0,0xc0,
84 0xc1,0xc1,0xaf,0xc2,0xc2,0xc3,0xc3,0xc4,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xca,
85 0xca,0xcb,0xcb,0xcb,0xcc,0xcc,0xcc,0xcd,0xcd,0xcd,0xce,0xce,0xce,0xcf,0xd0,0xd1,
86 0xd2,0xd3,0xac,0xd4,0xad,0xd5,0xae,0xd6,0xd7,0xd8,0xd9,0xda,0xdb,0xdc,0xdc,0xb2,
87 0xb4,0xa6,0xdd,0xb3,0xb6,0xb9};
89 static wchar_t msvcrt_mbc_to_wc_l(unsigned int ch
, _locale_t locale
)
91 pthreadmbcinfo mbcinfo
;
97 mbcinfo
= locale
->mbcinfo
;
99 mbcinfo
= get_mbcinfo();
105 mbch
[0] = (ch
>> 8) & 0xff;
109 if (!MultiByteToWideChar(mbcinfo
->mbcodepage
, 0, mbch
, n_chars
, &chW
, 1))
111 WARN("MultiByteToWideChar failed on %x\n", ch
);
117 static wchar_t msvcrt_mbc_to_wc(unsigned int ch
)
119 return msvcrt_mbc_to_wc_l(ch
, NULL
);
122 static inline size_t u_strlen( const unsigned char *str
)
124 return strlen( (const char*) str
);
127 static inline unsigned char* u_strncat( unsigned char* dst
, const unsigned char* src
, size_t len
)
129 return (unsigned char*)strncat( (char*)dst
, (const char*)src
, len
);
132 static inline int u_strcmp( const unsigned char *s1
, const unsigned char *s2
)
134 return strcmp( (const char*)s1
, (const char*)s2
);
137 static inline int u_strcasecmp( const unsigned char *s1
, const unsigned char *s2
)
139 return _stricmp( (const char*)s1
, (const char*)s2
);
142 static inline int u_strncmp( const unsigned char *s1
, const unsigned char *s2
, size_t len
)
144 return strncmp( (const char*)s1
, (const char*)s2
, len
);
147 static inline int u_strncasecmp( const unsigned char *s1
, const unsigned char *s2
, size_t len
)
149 return _strnicmp( (const char*)s1
, (const char*)s2
, len
);
152 static inline unsigned char *u_strchr( const unsigned char *s
, unsigned char x
)
154 return (unsigned char*) strchr( (const char*)s
, x
);
157 static inline unsigned char *u_strrchr( const unsigned char *s
, unsigned char x
)
159 return (unsigned char*) strrchr( (const char*)s
, x
);
162 static inline unsigned char *u__strset( unsigned char *s
, unsigned char c
)
164 return (unsigned char*) _strset( (char*)s
, c
);
167 static inline unsigned char *u__strnset( unsigned char *s
, unsigned char c
, size_t len
)
169 return (unsigned char*) _strnset( (char*)s
, c
, len
);
172 /*********************************************************************
173 * __p__mbctype (MSVCRT.@)
175 unsigned char* CDECL
__p__mbctype(void)
177 return get_mbcinfo()->mbctype
;
180 /*********************************************************************
181 * __p___mb_cur_max(MSVCRT.@)
183 int* CDECL
__p___mb_cur_max(void)
185 return &get_locinfo()->mb_cur_max
;
188 /*********************************************************************
189 * ___mb_cur_max_func(MSVCRT.@)
191 int CDECL
___mb_cur_max_func(void)
193 return get_locinfo()->mb_cur_max
;
197 /*********************************************************************
198 * ___mb_cur_max_l_func (MSVCR80.@)
200 int CDECL
___mb_cur_max_l_func(_locale_t locale
)
202 pthreadlocinfo locinfo
;
205 locinfo
= get_locinfo();
207 locinfo
= locale
->locinfo
;
209 return locinfo
->mb_cur_max
;
213 threadmbcinfo
* create_mbcinfo(int cp
, LCID lcid
, threadmbcinfo
*old_mbcinfo
)
215 threadmbcinfo
*mbcinfo
;
221 WCHAR bufW
[256], lowW
[256], upW
[256];
226 if(old_mbcinfo
&& cp
==old_mbcinfo
->mbcodepage
227 && (lcid
==-1 || lcid
==old_mbcinfo
->mblcid
)) {
228 InterlockedIncrement(&old_mbcinfo
->refcount
);
232 mbcinfo
= malloc(sizeof(threadmbcinfo
));
235 mbcinfo
->refcount
= 1;
246 newcp
= get_locinfo()->lc_codepage
;
249 /* fall through (C locale) */
251 newcp
= 20127; /* ASCII */
259 MSVCRT_sprintf(bufA
, ".%d", newcp
);
260 mbcinfo
->mblcid
= locale_to_LCID(bufA
, NULL
, NULL
);
262 mbcinfo
->mblcid
= lcid
;
265 if(mbcinfo
->mblcid
== -1)
267 WARN("Can't assign LCID to codepage (%d)\n", mbcinfo
->mblcid
);
271 if (!GetCPInfo(newcp
, &cpi
))
273 WARN("Codepage %d not found\n", newcp
);
278 /* setup the _mbctype */
279 memset(mbcinfo
->mbctype
, 0, sizeof(unsigned char[257]));
280 memset(mbcinfo
->mbcasemap
, 0, sizeof(unsigned char[256]));
282 bytes
= cpi
.LeadByte
;
283 while (bytes
[0] || bytes
[1])
285 for (i
= bytes
[0]; i
<= bytes
[1]; i
++)
286 mbcinfo
->mbctype
[i
+ 1] |= _M1
;
290 if (cpi
.MaxCharSize
> 1)
292 /* trail bytes not available through kernel32 but stored in a structure in msvcrt */
293 struct cp_extra_info_t
*cpextra
= g_cpextrainfo
;
295 mbcinfo
->ismbcodepage
= 1;
298 if (cpextra
->cp
== 0 || cpextra
->cp
== newcp
)
300 if (cpextra
->cp
== 0)
301 FIXME("trail bytes data not available for DBCS codepage %d - assuming all bytes\n", newcp
);
303 bytes
= cpextra
->TrailBytes
;
304 while (bytes
[0] || bytes
[1])
306 for (i
= bytes
[0]; i
<= bytes
[1]; i
++)
307 mbcinfo
->mbctype
[i
+ 1] |= _M2
;
316 mbcinfo
->ismbcodepage
= 0;
318 /* we can't use GetStringTypeA directly because we don't have a locale - only a code page
321 for (i
= 0; i
< 256; i
++)
322 if (!(mbcinfo
->mbctype
[i
+ 1] & _M1
))
323 bufA
[charcount
++] = i
;
325 ret
= MultiByteToWideChar(newcp
, 0, bufA
, charcount
, bufW
, charcount
);
326 if (ret
!= charcount
)
327 ERR("MultiByteToWideChar of chars failed for cp %d, ret=%d (exp %d), error=%d\n", newcp
, ret
, charcount
, GetLastError());
329 GetStringTypeW(CT_CTYPE1
, bufW
, charcount
, chartypes
);
330 LCMapStringW(lcid
, LCMAP_LOWERCASE
, bufW
, charcount
, lowW
, charcount
);
331 LCMapStringW(lcid
, LCMAP_UPPERCASE
, bufW
, charcount
, upW
, charcount
);
334 for (i
= 0; i
< 256; i
++)
335 if (!(mbcinfo
->mbctype
[i
+ 1] & _M1
))
337 if (chartypes
[charcount
] & C1_UPPER
)
339 mbcinfo
->mbctype
[i
+ 1] |= _SBUP
;
340 bufW
[charcount
] = lowW
[charcount
];
342 else if (chartypes
[charcount
] & C1_LOWER
)
344 mbcinfo
->mbctype
[i
+ 1] |= _SBLOW
;
345 bufW
[charcount
] = upW
[charcount
];
350 ret
= WideCharToMultiByte(newcp
, 0, bufW
, charcount
, bufA
, charcount
, NULL
, NULL
);
351 if (ret
!= charcount
)
352 ERR("WideCharToMultiByte failed for cp %d, ret=%d (exp %d), error=%d\n", newcp
, ret
, charcount
, GetLastError());
355 for (i
= 0; i
< 256; i
++)
357 if(!(mbcinfo
->mbctype
[i
+ 1] & _M1
))
359 if(mbcinfo
->mbctype
[i
] & (C1_UPPER
|C1_LOWER
))
360 mbcinfo
->mbcasemap
[i
] = bufA
[charcount
];
365 if (newcp
== 932) /* CP932 only - set _MP and _MS */
367 /* On Windows it's possible to calculate the _MP and _MS from CT_CTYPE1
368 * and CT_CTYPE3. But as of Wine 0.9.43 we return wrong values what makes
369 * it hard. As this is set only for codepage 932 we hardcode it what gives
370 * also faster execution.
372 for (i
= 161; i
<= 165; i
++)
373 mbcinfo
->mbctype
[i
+ 1] |= _MP
;
374 for (i
= 166; i
<= 223; i
++)
375 mbcinfo
->mbctype
[i
+ 1] |= _MS
;
378 mbcinfo
->mbcodepage
= newcp
;
382 /*********************************************************************
383 * _setmbcp (MSVCRT.@)
385 int CDECL
_setmbcp(int cp
)
387 thread_data_t
*data
= msvcrt_get_thread_data();
388 threadmbcinfo
*mbcinfo
;
390 mbcinfo
= create_mbcinfo(cp
, -1, get_mbcinfo());
397 if(data
->locale_flags
& LOCALE_THREAD
)
399 if(data
->locale_flags
& LOCALE_FREE
)
400 free_mbcinfo(data
->mbcinfo
);
401 data
->mbcinfo
= mbcinfo
;
406 free_mbcinfo(MSVCRT_locale
->mbcinfo
);
407 MSVCRT_locale
->mbcinfo
= mbcinfo
;
408 memcpy(MSVCRT_mbctype
, MSVCRT_locale
->mbcinfo
->mbctype
, sizeof(MSVCRT_mbctype
));
409 _unlock(_MB_CP_LOCK
);
414 /*********************************************************************
415 * _getmbcp (MSVCRT.@)
417 int CDECL
_getmbcp(void)
419 return get_mbcinfo()->mbcodepage
;
422 /*********************************************************************
423 * _mbsnextc_l(MSVCRT.@)
425 unsigned int CDECL
_mbsnextc_l(const unsigned char* str
, _locale_t locale
)
427 if(_ismbblead_l(*str
, locale
))
428 return *str
<< 8 | str
[1];
432 /*********************************************************************
433 * _mbsnextc(MSVCRT.@)
435 unsigned int CDECL
_mbsnextc(const unsigned char* str
)
437 return _mbsnextc_l(str
, NULL
);
440 /*********************************************************************
441 * _mbctolower(MSVCRT.@)
443 unsigned int CDECL
_mbctolower(unsigned int c
)
447 FIXME("Handle MBC chars\n");
450 return _tolower_l(c
, NULL
); /* ASCII CP or SB char */
453 /*********************************************************************
454 * _mbctoupper(MSVCRT.@)
456 unsigned int CDECL
_mbctoupper(unsigned int c
)
460 FIXME("Handle MBC chars\n");
463 return _toupper_l(c
, NULL
); /* ASCII CP or SB char */
466 /*********************************************************************
467 * _mbctombb (MSVCRT.@)
469 unsigned int CDECL
_mbctombb(unsigned int c
)
473 if(get_mbcinfo()->mbcodepage
== 932)
475 if(c
>= 0x829f && c
<= 0x82f1) /* Hiragana */
476 return mbctombb_932_kana
[c
- 0x829f];
477 if(c
>= 0x8340 && c
<= 0x8396 && c
!= 0x837f) /* Katakana */
478 return mbctombb_932_kana
[c
- 0x8340 - (c
>= 0x837f ? 1 : 0)];
479 if(c
>= 0x8140 && c
<= 0x8197) /* Punctuation */
481 value
= mbctombb_932_punct
[c
- 0x8140];
482 return value
? value
: c
;
484 if((c
>= 0x824f && c
<= 0x8258) || /* Fullwidth digits */
485 (c
>= 0x8260 && c
<= 0x8279)) /* Fullwidth capitals letters */
487 if(c
>= 0x8281 && c
<= 0x829a) /* Fullwidth small letters */
489 /* all other cases return c */
494 /*********************************************************************
495 * _mbcjistojms(MSVCRT.@)
497 * Converts a jis character to sjis.
498 * Based on description from
499 * http://www.slayers.ne.jp/~oouchi/code/jistosjis.html
501 unsigned int CDECL
_mbcjistojms(unsigned int c
)
503 /* Conversion takes place only when codepage is 932.
504 In all other cases, c is returned unchanged */
505 if(get_mbcinfo()->mbcodepage
== 932)
507 if(HIBYTE(c
) >= 0x21 && HIBYTE(c
) <= 0x7e &&
508 LOBYTE(c
) >= 0x21 && LOBYTE(c
) <= 0x7e)
515 if(LOBYTE(c
) >= 0x7F)
518 c
= (((HIBYTE(c
) - 0x21)/2 + 0x81) << 8) | LOBYTE(c
);
524 return 0; /* Codepage is 932, but c can't be converted */
530 /*********************************************************************
531 * _mbcjmstojis(MSVCRT.@)
533 * Converts a sjis character to jis.
535 unsigned int CDECL
_mbcjmstojis(unsigned int c
)
537 /* Conversion takes place only when codepage is 932.
538 In all other cases, c is returned unchanged */
539 if(get_mbcinfo()->mbcodepage
== 932)
541 if(_ismbclegal(c
) && HIBYTE(c
) < 0xf0)
543 if(HIBYTE(c
) >= 0xe0)
546 c
= (((HIBYTE(c
) - 0x81)*2 + 0x21) << 8) | LOBYTE(c
);
557 return 0; /* Codepage is 932, but c can't be converted */
563 /*********************************************************************
566 unsigned char* CDECL
_mbsdec(const unsigned char* start
, const unsigned char* cur
)
571 if(get_mbcinfo()->ismbcodepage
)
572 return (unsigned char *)(_ismbstrail(start
,cur
-1) ? cur
- 2 : cur
-1);
574 return (unsigned char *)cur
- 1; /* ASCII CP or SB char */
577 /*********************************************************************
580 unsigned int CDECL
_mbclen(const unsigned char* str
)
582 return _ismbblead(*str
) ? 2 : 1;
585 /*********************************************************************
588 unsigned char* CDECL
_mbsinc(const unsigned char* str
)
590 return (unsigned char *)(str
+ _mbclen(str
));
593 /*********************************************************************
596 unsigned char* CDECL
_mbsninc(const unsigned char* str
, size_t num
)
601 while (num
> 0 && *str
)
603 if (_ismbblead(*str
))
613 return (unsigned char*)str
;
616 /*********************************************************************
617 * _mbsnlen_l(MSVCRT.@)
619 size_t CDECL
_mbsnlen_l(const unsigned char *str
,
620 size_t maxsize
, _locale_t locale
)
622 pthreadmbcinfo mbcinfo
;
623 size_t i
= 0, len
= 0;
626 mbcinfo
= get_mbcinfo();
628 mbcinfo
= locale
->mbcinfo
;
630 if(!mbcinfo
->ismbcodepage
)
631 return strnlen((const char*)str
, maxsize
);
633 while(i
<maxsize
&& str
[i
])
635 if (_ismbblead_l(str
[i
], locale
))
638 if (!str
[i
]) /* count only full chars */
644 return i
< maxsize
? len
: maxsize
;
647 /*********************************************************************
650 size_t CDECL
_mbslen(const unsigned char* str
)
652 return _mbsnlen_l(str
, -1, NULL
);
655 /*********************************************************************
656 * _mbslen_l(MSVCRT.@)
658 size_t CDECL
_mbslen_l(const unsigned char* str
, _locale_t locale
)
660 return _mbsnlen_l(str
, -1, locale
);
663 /*********************************************************************
666 size_t CDECL
_mbsnlen(const unsigned char* str
, size_t maxsize
)
668 return _mbsnlen_l(str
, maxsize
, NULL
);
671 /*********************************************************************
672 * _mbccpy_s_l(MSVCRT.@)
674 int CDECL
_mbccpy_s_l(unsigned char* dest
, size_t maxsize
,
675 int *copied
, const unsigned char* src
, _locale_t locale
)
677 if(copied
) *copied
= 0;
678 if(!MSVCRT_CHECK_PMT(dest
!= NULL
&& maxsize
>= 1)) return EINVAL
;
680 if(!MSVCRT_CHECK_PMT(src
!= NULL
)) return EINVAL
;
682 if(_ismbblead_l(*src
, locale
)) {
684 if(copied
) *copied
= 1;
690 MSVCRT_INVALID_PMT("dst buffer is too small", ERANGE
);
696 if(copied
) *copied
= 2;
699 if(copied
) *copied
= 1;
705 /*********************************************************************
708 void CDECL
_mbccpy(unsigned char* dest
, const unsigned char* src
)
710 _mbccpy_s_l(dest
, 2, NULL
, src
, NULL
);
713 /*********************************************************************
714 * _mbccpy_l(MSVCRT.@)
716 void CDECL
_mbccpy_l(unsigned char* dest
, const unsigned char* src
,
719 _mbccpy_s_l(dest
, 2, NULL
, src
, locale
);
722 /*********************************************************************
723 * _mbccpy_s(MSVCRT.@)
725 int CDECL
_mbccpy_s(unsigned char* dest
, size_t maxsize
,
726 int *copied
, const unsigned char* src
)
728 return _mbccpy_s_l(dest
, maxsize
, copied
, src
, NULL
);
731 /*********************************************************************
734 * The parameter n is the number or characters to copy, not the size of
735 * the buffer. Use _mbsnbcpy for a function analogical to strncpy
737 unsigned char* CDECL
_mbsncpy(unsigned char* dst
, const unsigned char* src
, size_t n
)
739 unsigned char* ret
= dst
;
742 if (get_mbcinfo()->ismbcodepage
)
747 if (_ismbblead(*src
))
767 if (!(*dst
++ = *src
++)) break;
770 while (n
--) *dst
++ = 0;
774 /*********************************************************************
775 * _mbsnbcpy_s_l(MSVCRT.@)
777 * Unlike _mbsnbcpy this function does not pad the rest of the dest
780 int CDECL
_mbsnbcpy_s_l(unsigned char* dst
, size_t size
,
781 const unsigned char* src
, size_t n
, _locale_t locale
)
785 if(!dst
|| size
== 0)
795 if((locale
? locale
->mbcinfo
: get_mbcinfo())->ismbcodepage
)
797 BOOL is_lead
= FALSE
;
805 is_lead
= (!is_lead
&& _ismbblead(*src
));
810 if (is_lead
) /* if string ends with a lead, remove it */
840 /*********************************************************************
841 * _mbsnbcpy_s(MSVCRT.@)
843 int CDECL
_mbsnbcpy_s(unsigned char* dst
, size_t size
, const unsigned char* src
, size_t n
)
845 return _mbsnbcpy_s_l(dst
, size
, src
, n
, NULL
);
848 /*********************************************************************
849 * _mbscpy_s_l(MSVCRT.@)
851 int CDECL
_mbscpy_s_l(unsigned char *dst
, size_t size
,
852 const unsigned char *src
, _locale_t locale
)
854 return _mbsnbcpy_s_l(dst
, size
, src
, -1, locale
);
857 /*********************************************************************
858 * _mbscpy_s(MSVCRT.@)
860 int CDECL
_mbscpy_s(unsigned char *dst
, size_t size
, const unsigned char *src
)
862 return _mbscpy_s_l(dst
, size
, src
, NULL
);
865 /*********************************************************************
866 * _mbsnbcpy(MSVCRT.@)
868 * Like strncpy this function doesn't enforce the string to be
871 unsigned char* CDECL
_mbsnbcpy(unsigned char* dst
, const unsigned char* src
, size_t n
)
873 unsigned char* ret
= dst
;
876 if(get_mbcinfo()->ismbcodepage
)
878 BOOL is_lead
= FALSE
;
881 is_lead
= (!is_lead
&& _ismbblead(*src
));
886 if (is_lead
) /* if string ends with a lead, remove it */
894 if (!(*dst
++ = *src
++)) break;
897 while (n
--) *dst
++ = 0;
901 /*********************************************************************
902 * _mbscmp_l(MSVCRT.@)
904 int CDECL
_mbscmp_l(const unsigned char* str
, const unsigned char* cmp
, _locale_t locale
)
906 pthreadmbcinfo mbcinfo
;
908 if (!str
|| !cmp
) return INT_MAX
;
910 mbcinfo
= locale
? locale
->mbcinfo
: get_mbcinfo();
912 if(mbcinfo
->ismbcodepage
)
914 unsigned int strc
, cmpc
;
917 return *cmp
? -1 : 0;
920 strc
= _mbsnextc_l(str
, locale
);
921 cmpc
= _mbsnextc_l(cmp
, locale
);
923 return strc
< cmpc
? -1 : 1;
924 str
+=(strc
> 255) ? 2 : 1;
925 cmp
+=(strc
> 255) ? 2 : 1; /* equal, use same increment */
928 return u_strcmp(str
, cmp
); /* ASCII CP */
931 /*********************************************************************
934 int CDECL
_mbscmp(const unsigned char* str
, const unsigned char* cmp
)
936 return _mbscmp_l(str
, cmp
, NULL
);
939 /*********************************************************************
940 * _mbsnbicoll_l(MSVCRT.@)
942 int CDECL
_mbsnbicoll_l(const unsigned char *str1
, const unsigned char *str2
, size_t len
, _locale_t locale
)
944 pthreadmbcinfo mbcinfo
;
947 mbcinfo
= get_mbcinfo();
949 mbcinfo
= locale
->mbcinfo
;
951 if(!mbcinfo
->ismbcodepage
)
952 return _strnicoll_l((const char*)str1
, (const char*)str2
, len
, locale
);
953 return CompareStringA(mbcinfo
->mblcid
, NORM_IGNORECASE
, (const char*)str1
, len
, (const char*)str2
, len
)-CSTR_EQUAL
;
956 /*********************************************************************
957 * _mbsicoll_l(MSVCRT.@)
959 int CDECL
_mbsicoll_l(const unsigned char *str1
, const unsigned char *str2
, _locale_t locale
)
961 return _mbsnbicoll_l(str1
, str2
, -1, locale
);
964 /*********************************************************************
965 * _mbsnbicoll(MSVCRT.@)
967 int CDECL
_mbsnbicoll(const unsigned char *str1
, const unsigned char *str2
, size_t len
)
969 return _mbsnbicoll_l(str1
, str2
, len
, NULL
);
972 /*********************************************************************
973 * _mbsicoll(MSVCRT.@)
975 int CDECL
_mbsicoll(const unsigned char* str
, const unsigned char* cmp
)
977 #if _MSVCR_VER>=60 && _MSVCR_VER<=71
978 return CompareStringA(get_mbcinfo()->mblcid
, NORM_IGNORECASE
,
979 (const char*)str
, -1, (const char*)cmp
, -1)-CSTR_EQUAL
;
981 return _mbsnbicoll_l(str
, cmp
, -1, NULL
);
985 /*********************************************************************
986 * _mbsnbcoll_l(MSVCRT.@)
988 int CDECL
_mbsnbcoll_l(const unsigned char *str1
, const unsigned char *str2
, size_t len
, _locale_t locale
)
990 pthreadmbcinfo mbcinfo
;
993 mbcinfo
= get_mbcinfo();
995 mbcinfo
= locale
->mbcinfo
;
997 if(!mbcinfo
->ismbcodepage
)
998 return _strncoll_l((const char*)str1
, (const char*)str2
, len
, locale
);
999 return CompareStringA(mbcinfo
->mblcid
, 0, (const char*)str1
, len
, (const char*)str2
, len
)-CSTR_EQUAL
;
1002 /*********************************************************************
1003 * _mbscoll_l(MSVCRT.@)
1005 int CDECL
_mbscoll_l(const unsigned char *str1
, const unsigned char *str2
, _locale_t locale
)
1007 return _mbsnbcoll_l(str1
, str2
, -1, locale
);
1010 /*********************************************************************
1011 * _mbsnbcoll(MSVCRT.@)
1013 int CDECL
_mbsnbcoll(const unsigned char *str1
, const unsigned char *str2
, size_t len
)
1015 return _mbsnbcoll_l(str1
, str2
, len
, NULL
);
1018 /*********************************************************************
1019 * _mbscoll(MSVCRT.@)
1021 int CDECL
_mbscoll(const unsigned char* str
, const unsigned char* cmp
)
1023 #if _MSVCR_VER>=60 && _MSVCR_VER<=71
1024 return CompareStringA(get_mbcinfo()->mblcid
, 0,
1025 (const char*)str
, -1, (const char*)cmp
, -1)-CSTR_EQUAL
;
1027 return _mbsnbcoll_l(str
, cmp
, -1, NULL
);
1031 /*********************************************************************
1032 * _mbsicmp(MSVCRT.@)
1034 int CDECL
_mbsicmp(const unsigned char* str
, const unsigned char* cmp
)
1036 if(get_mbcinfo()->ismbcodepage
)
1038 unsigned int strc
, cmpc
;
1041 return *cmp
? -1 : 0;
1044 strc
= _mbctolower(_mbsnextc(str
));
1045 cmpc
= _mbctolower(_mbsnextc(cmp
));
1047 return strc
< cmpc
? -1 : 1;
1048 str
+=(strc
> 255) ? 2 : 1;
1049 cmp
+=(strc
> 255) ? 2 : 1; /* equal, use same increment */
1052 return u_strcasecmp(str
, cmp
); /* ASCII CP */
1055 /*********************************************************************
1056 * _mbsncmp(MSVCRT.@)
1058 int CDECL
_mbsncmp(const unsigned char* str
, const unsigned char* cmp
, size_t len
)
1063 if(get_mbcinfo()->ismbcodepage
)
1065 unsigned int strc
, cmpc
;
1070 return *cmp
? -1 : 0;
1073 strc
= _mbsnextc(str
);
1074 cmpc
= _mbsnextc(cmp
);
1076 return strc
< cmpc
? -1 : 1;
1077 inc
=(strc
> 255) ? 2 : 1; /* Equal, use same increment */
1081 return 0; /* Matched len chars */
1083 return u_strncmp(str
, cmp
, len
); /* ASCII CP */
1086 /*********************************************************************
1087 * _mbsnbcmp(MSVCRT.@)
1089 int CDECL
_mbsnbcmp(const unsigned char* str
, const unsigned char* cmp
, size_t len
)
1093 if(get_mbcinfo()->ismbcodepage
)
1095 unsigned int strc
, cmpc
;
1100 return *cmp
? -1 : 0;
1103 if (_ismbblead(*str
))
1105 strc
=(len
>=2)?_mbsnextc(str
):0;
1113 if (_ismbblead(*cmp
))
1114 cmpc
=(len
>=2)?_mbsnextc(cmp
):0;
1118 return strc
< cmpc
? -1 : 1;
1123 return 0; /* Matched len chars */
1125 return u_strncmp(str
,cmp
,len
);
1128 /*********************************************************************
1129 * _mbsnicmp(MSVCRT.@)
1131 * Compare two multibyte strings case insensitively to 'len' characters.
1133 int CDECL
_mbsnicmp(const unsigned char* str
, const unsigned char* cmp
, size_t len
)
1135 /* FIXME: No tolower() for mb strings yet */
1136 if(get_mbcinfo()->ismbcodepage
)
1138 unsigned int strc
, cmpc
;
1142 return *cmp
? -1 : 0;
1145 strc
= _mbctolower(_mbsnextc(str
));
1146 cmpc
= _mbctolower(_mbsnextc(cmp
));
1148 return strc
< cmpc
? -1 : 1;
1149 str
+=(strc
> 255) ? 2 : 1;
1150 cmp
+=(strc
> 255) ? 2 : 1; /* Equal, use same increment */
1152 return 0; /* Matched len chars */
1154 return u_strncasecmp(str
, cmp
, len
); /* ASCII CP */
1157 /*********************************************************************
1158 * _mbsnbicmp(MSVCRT.@)
1160 int CDECL
_mbsnbicmp(const unsigned char* str
, const unsigned char* cmp
, size_t len
)
1164 if(get_mbcinfo()->ismbcodepage
)
1166 unsigned int strc
, cmpc
;
1171 return *cmp
? -1 : 0;
1174 if (_ismbblead(*str
))
1176 strc
=(len
>=2)?_mbsnextc(str
):0;
1184 if (_ismbblead(*cmp
))
1185 cmpc
=(len
>=2)?_mbsnextc(cmp
):0;
1188 strc
= _mbctolower(strc
);
1189 cmpc
= _mbctolower(cmpc
);
1191 return strc
< cmpc
? -1 : 1;
1196 return 0; /* Matched len bytes */
1198 return u_strncasecmp(str
,cmp
,len
);
1201 /*********************************************************************
1202 * _mbscat (MSVCRT.@)
1204 unsigned char * CDECL
_mbscat( unsigned char *dst
, const unsigned char *src
)
1206 strcat( (char *)dst
, (const char *)src
);
1210 /*********************************************************************
1211 * _mbscat_s_l (MSVCRT.@)
1213 int CDECL
_mbscat_s_l( unsigned char *dst
, size_t size
,
1214 const unsigned char *src
, _locale_t locale
)
1219 if(!MSVCRT_CHECK_PMT(dst
!= NULL
)) return EINVAL
;
1220 if(!MSVCRT_CHECK_PMT(src
!= NULL
)) return EINVAL
;
1222 for(i
=0; i
<size
; i
++)
1225 MSVCRT_INVALID_PMT("dst is not NULL-terminated", EINVAL
);
1226 if(size
) dst
[0] = 0;
1230 if(i
&& _ismbblead_l(dst
[i
-1], locale
)) {
1235 for(j
=0; src
[j
] && i
+j
<size
; j
++)
1238 MSVCRT_INVALID_PMT("dst buffer is too small", ERANGE
);
1243 if(j
&& _ismbblead_l(src
[j
-1], locale
)) {
1252 /*********************************************************************
1253 * _mbscat_s (MSVCRT.@)
1255 int CDECL
_mbscat_s( unsigned char *dst
, size_t size
, const unsigned char *src
)
1257 return _mbscat_s_l(dst
, size
, src
, NULL
);
1260 /*********************************************************************
1261 * _mbscpy (MSVCRT.@)
1263 unsigned char* CDECL
_mbscpy( unsigned char *dst
, const unsigned char *src
)
1265 strcpy( (char *)dst
, (const char *)src
);
1269 /*********************************************************************
1270 * _mbsstr (MSVCRT.@)
1272 unsigned char * CDECL
_mbsstr(const unsigned char *haystack
, const unsigned char *needle
)
1274 return (unsigned char *)strstr( (const char *)haystack
, (const char *)needle
);
1277 /*********************************************************************
1280 * Find a multibyte character in a multibyte string.
1282 unsigned char* CDECL
_mbschr(const unsigned char* s
, unsigned int x
)
1284 if(get_mbcinfo()->ismbcodepage
)
1291 return (unsigned char*)s
;
1294 s
+= c
> 255 ? 2 : 1;
1297 return u_strchr(s
, x
); /* ASCII CP */
1300 /*********************************************************************
1301 * _mbsrchr(MSVCRT.@)
1303 unsigned char* CDECL
_mbsrchr(const unsigned char* s
, unsigned int x
)
1305 if(get_mbcinfo()->ismbcodepage
)
1308 unsigned char* match
=NULL
;
1314 match
=(unsigned char*)s
;
1317 s
+=(c
> 255) ? 2 : 1;
1320 return u_strrchr(s
, x
);
1323 /*********************************************************************
1324 * _mbstok_s_l(MSVCRT.@)
1326 unsigned char* CDECL
_mbstok_s_l(unsigned char *str
, const unsigned char *delim
,
1327 unsigned char **ctx
, _locale_t locale
)
1329 pthreadmbcinfo mbcinfo
;
1332 if(!MSVCRT_CHECK_PMT(delim
!= NULL
)) return NULL
;
1333 if(!MSVCRT_CHECK_PMT(ctx
!= NULL
)) return NULL
;
1334 if(!MSVCRT_CHECK_PMT(str
|| *ctx
)) return NULL
;
1337 mbcinfo
= locale
->mbcinfo
;
1339 mbcinfo
= get_mbcinfo();
1341 if(!mbcinfo
->ismbcodepage
)
1342 return (unsigned char*)strtok_s((char*)str
, (const char*)delim
, (char**)ctx
);
1347 while((c
=_mbsnextc(str
)) && _mbschr(delim
, c
))
1348 str
+= c
>255 ? 2 : 1;
1355 *ctx
= str
+ (c
>255 ? 2 : 1);
1356 while((c
=_mbsnextc(*ctx
)) && !_mbschr(delim
, c
))
1357 *ctx
+= c
>255 ? 2 : 1;
1368 /*********************************************************************
1369 * _mbstok_s(MSVCRT.@)
1371 unsigned char* CDECL
_mbstok_s(unsigned char *str
,
1372 const unsigned char *delim
, unsigned char **ctx
)
1374 return _mbstok_s_l(str
, delim
, ctx
, NULL
);
1377 /*********************************************************************
1378 * _mbstok_l(MSVCRT.@)
1380 unsigned char* CDECL
_mbstok_l(unsigned char *str
,
1381 const unsigned char *delim
, _locale_t locale
)
1383 return _mbstok_s_l(str
, delim
, &msvcrt_get_thread_data()->mbstok_next
, locale
);
1386 /*********************************************************************
1389 unsigned char* CDECL
_mbstok(unsigned char *str
, const unsigned char *delim
)
1391 thread_data_t
*data
= msvcrt_get_thread_data();
1394 if(!str
&& !data
->mbstok_next
)
1398 return _mbstok_s_l(str
, delim
, &data
->mbstok_next
, NULL
);
1401 /*********************************************************************
1402 * _mbbtombc(MSVCRT.@)
1404 unsigned int CDECL
_mbbtombc(unsigned int c
)
1406 if(get_mbcinfo()->mbcodepage
== 932)
1408 if(c
>= 0x20 && c
<= 0x7e) {
1409 if((c
>= 0x41 && c
<= 0x5a) || (c
>= 0x61 && c
<= 0x7a) || (c
>= 0x30 && c
<= 0x39))
1410 return mbbtombc_932
[c
- 0x20] | 0x8200;
1412 return mbbtombc_932
[c
- 0x20] | 0x8100;
1414 else if(c
>= 0xa1 && c
<= 0xdf) {
1415 if(c
>= 0xa6 && c
<= 0xdd && c
!= 0xb0)
1416 return mbbtombc_932
[c
- 0xa1 + 0x5f] | 0x8300;
1418 return mbbtombc_932
[c
- 0xa1 + 0x5f] | 0x8100;
1421 return c
; /* not Japanese or no MB char */
1424 /*********************************************************************
1425 * _ismbbkana_l(MSVCRT.@)
1427 int CDECL
_ismbbkana_l(unsigned int c
, _locale_t locale
)
1429 pthreadmbcinfo mbcinfo
;
1432 mbcinfo
= locale
->mbcinfo
;
1434 mbcinfo
= get_mbcinfo();
1436 if(mbcinfo
->mbcodepage
== 932)
1438 /* Japanese/Katakana, CP 932 */
1439 return (c
>= 0xa1 && c
<= 0xdf);
1444 /*********************************************************************
1445 * _ismbbkana(MSVCRT.@)
1447 int CDECL
_ismbbkana(unsigned int c
)
1449 return _ismbbkana_l( c
, NULL
);
1452 /*********************************************************************
1453 * _ismbcdigit_l(MSVCRT.@)
1455 int CDECL
_ismbcdigit_l(unsigned int ch
, _locale_t locale
)
1457 return MSVCRT__iswdigit_l( msvcrt_mbc_to_wc_l(ch
, locale
), locale
);
1460 /*********************************************************************
1461 * _ismbcdigit(MSVCRT.@)
1463 int CDECL
_ismbcdigit(unsigned int ch
)
1465 return _ismbcdigit_l( ch
, NULL
);
1468 /*********************************************************************
1469 * _ismbcgraph_l(MSVCRT.@)
1471 int CDECL
_ismbcgraph_l(unsigned int ch
, _locale_t locale
)
1473 return MSVCRT__iswgraph_l( msvcrt_mbc_to_wc_l(ch
, locale
), locale
);
1476 /*********************************************************************
1477 * _ismbcgraph(MSVCRT.@)
1479 int CDECL
_ismbcgraph(unsigned int ch
)
1481 return _ismbcgraph_l( ch
, NULL
);
1484 /*********************************************************************
1485 * _ismbcalpha_l (MSVCRT.@)
1487 int CDECL
_ismbcalpha_l(unsigned int ch
, _locale_t locale
)
1489 return MSVCRT__iswalpha_l( msvcrt_mbc_to_wc_l(ch
, locale
), locale
);
1492 /*********************************************************************
1493 * _ismbcalpha (MSVCRT.@)
1495 int CDECL
_ismbcalpha(unsigned int ch
)
1497 return _ismbcalpha_l( ch
, NULL
);
1500 /*********************************************************************
1501 * _ismbclower_l (MSVCRT.@)
1503 int CDECL
_ismbclower_l(unsigned int ch
, _locale_t locale
)
1505 return MSVCRT__iswlower_l( msvcrt_mbc_to_wc_l(ch
, locale
), locale
);
1508 /*********************************************************************
1509 * _ismbclower (MSVCRT.@)
1511 int CDECL
_ismbclower(unsigned int ch
)
1513 return _ismbclower_l( ch
, NULL
);
1516 /*********************************************************************
1517 * _ismbcupper_l (MSVCRT.@)
1519 int CDECL
_ismbcupper_l(unsigned int ch
, _locale_t locale
)
1521 return MSVCRT__iswupper_l( msvcrt_mbc_to_wc_l(ch
, locale
), locale
);
1524 /*********************************************************************
1525 * _ismbcupper (MSVCRT.@)
1527 int CDECL
_ismbcupper(unsigned int ch
)
1529 return _ismbcupper_l( ch
, NULL
);
1532 /*********************************************************************
1533 * _ismbcsymbol(MSVCRT.@)
1535 int CDECL
_ismbcsymbol(unsigned int ch
)
1537 wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1539 if (!GetStringTypeW(CT_CTYPE3
, &wch
, 1, &ctype
))
1541 WARN("GetStringTypeW failed on %x\n", ch
);
1544 return ((ctype
& C3_SYMBOL
) != 0);
1547 /*********************************************************************
1548 * _ismbcalnum_l (MSVCRT.@)
1550 int CDECL
_ismbcalnum_l(unsigned int ch
, _locale_t locale
)
1552 return MSVCRT__iswalnum_l( msvcrt_mbc_to_wc_l(ch
, locale
), locale
);
1555 /*********************************************************************
1556 * _ismbcalnum (MSVCRT.@)
1558 int CDECL
_ismbcalnum(unsigned int ch
)
1560 return _ismbcalnum_l( ch
, NULL
);
1563 /*********************************************************************
1564 * _ismbcspace_l (MSVCRT.@)
1566 int CDECL
_ismbcspace_l(unsigned int ch
, _locale_t locale
)
1568 return MSVCRT__iswspace_l( msvcrt_mbc_to_wc_l(ch
, locale
), locale
);
1571 /*********************************************************************
1572 * _ismbcspace (MSVCRT.@)
1574 int CDECL
_ismbcspace(unsigned int ch
)
1576 return _ismbcspace_l( ch
, NULL
);
1579 /*********************************************************************
1580 * _ismbcprint_l (MSVCRT.@)
1582 int CDECL
_ismbcprint_l(unsigned int ch
, _locale_t locale
)
1584 return MSVCRT__iswprint_l( msvcrt_mbc_to_wc_l(ch
, locale
), locale
);
1587 /*********************************************************************
1588 * _ismbcprint (MSVCRT.@)
1590 int CDECL
_ismbcprint(unsigned int ch
)
1592 return _ismbcprint_l( ch
, NULL
);
1595 /*********************************************************************
1596 * _ismbcpunct_l (MSVCRT.@)
1598 int CDECL
_ismbcpunct_l(unsigned int ch
, _locale_t locale
)
1600 return MSVCRT__iswpunct_l( msvcrt_mbc_to_wc_l(ch
, locale
), locale
);
1603 /*********************************************************************
1604 * _ismbcpunct(MSVCRT.@)
1606 int CDECL
_ismbcpunct(unsigned int ch
)
1608 return _ismbcpunct_l( ch
, NULL
);
1611 /*********************************************************************
1612 * _ismbchira(MSVCRT.@)
1614 int CDECL
_ismbchira(unsigned int c
)
1616 if(get_mbcinfo()->mbcodepage
== 932)
1618 /* Japanese/Hiragana, CP 932 */
1619 return (c
>= 0x829f && c
<= 0x82f1);
1624 /*********************************************************************
1625 * _ismbckata(MSVCRT.@)
1627 int CDECL
_ismbckata(unsigned int c
)
1629 if(get_mbcinfo()->mbcodepage
== 932)
1631 /* Japanese/Katakana, CP 932 */
1632 return (c
>= 0x8340 && c
<= 0x8396 && c
!= 0x837f);
1638 /*********************************************************************
1639 * _ismbblead_l(MSVCRT.@)
1641 int CDECL
_ismbblead_l(unsigned int c
, _locale_t locale
)
1643 pthreadmbcinfo mbcinfo
;
1646 mbcinfo
= get_mbcinfo();
1648 mbcinfo
= locale
->mbcinfo
;
1650 return (mbcinfo
->mbctype
[(c
&0xff) + 1] & _M1
) != 0;
1653 /*********************************************************************
1654 * _ismbblead(MSVCRT.@)
1656 int CDECL
_ismbblead(unsigned int c
)
1658 return _ismbblead_l(c
, NULL
);
1661 /*********************************************************************
1662 * _ismbbtrail_l(MSVCRT.@)
1664 int CDECL
_ismbbtrail_l(unsigned int c
, _locale_t locale
)
1666 pthreadmbcinfo mbcinfo
;
1669 mbcinfo
= get_mbcinfo();
1671 mbcinfo
= locale
->mbcinfo
;
1673 return (mbcinfo
->mbctype
[(c
&0xff) + 1] & _M2
) != 0;
1676 /*********************************************************************
1677 * _ismbbtrail(MSVCRT.@)
1679 int CDECL
_ismbbtrail(unsigned int c
)
1681 return _ismbbtrail_l(c
, NULL
);
1684 /*********************************************************************
1685 * _ismbclegal_l(MSVCRT.@)
1687 int CDECL
_ismbclegal_l(unsigned int c
, _locale_t locale
)
1689 return _ismbblead_l(HIBYTE(c
), locale
) && _ismbbtrail_l(LOBYTE(c
), locale
);
1692 /*********************************************************************
1693 * _ismbclegal(MSVCRT.@)
1695 int CDECL
_ismbclegal(unsigned int c
)
1697 return _ismbclegal_l(c
, NULL
);
1700 /*********************************************************************
1701 * _ismbslead(MSVCRT.@)
1703 int CDECL
_ismbslead(const unsigned char* start
, const unsigned char* str
)
1707 if(!get_mbcinfo()->ismbcodepage
)
1710 /* Lead bytes can also be trail bytes so we need to analyse the string
1712 while (start
<= str
)
1716 lead
= !lead
&& _ismbblead(*start
);
1720 return lead
? -1 : 0;
1723 /*********************************************************************
1724 * _ismbstrail(MSVCRT.@)
1726 int CDECL
_ismbstrail(const unsigned char* start
, const unsigned char* str
)
1728 /* Note: this function doesn't check _ismbbtrail */
1729 if ((str
> start
) && _ismbslead(start
, str
-1))
1735 /*********************************************************************
1736 * _mbbtype_l(MSVCRT.@)
1738 int CDECL
_mbbtype_l(unsigned char c
, int type
, _locale_t locale
)
1741 return _ismbbtrail_l(c
, locale
) ? _MBC_TRAIL
: _MBC_ILLEGAL
;
1743 return _ismbblead_l(c
, locale
) ? _MBC_LEAD
1744 : _isprint_l(c
, locale
) ? _MBC_SINGLE
: _MBC_ILLEGAL
;
1747 /*********************************************************************
1748 * _mbbtype(MSVCRT.@)
1750 int CDECL
_mbbtype(unsigned char c
, int type
)
1752 return _mbbtype_l(c
, type
, NULL
);
1755 /*********************************************************************
1756 * _mbsbtype (MSVCRT.@)
1758 int CDECL
_mbsbtype(const unsigned char *str
, size_t count
)
1761 const unsigned char *end
= str
+ count
;
1763 /* Lead bytes can also be trail bytes so we need to analyse the string.
1764 * Also we must return _MBC_ILLEGAL for chars past the end of the string
1766 while (str
< end
) /* Note: we skip the last byte - will check after the loop */
1769 return _MBC_ILLEGAL
;
1770 lead
= get_mbcinfo()->ismbcodepage
&& !lead
&& _ismbblead(*str
);
1775 if (_ismbbtrail(*str
))
1778 return _MBC_ILLEGAL
;
1780 if (_ismbblead(*str
))
1786 /*********************************************************************
1789 unsigned char* CDECL
_mbsset(unsigned char* str
, unsigned int c
)
1791 unsigned char* ret
= str
;
1793 if(!get_mbcinfo()->ismbcodepage
|| c
< 256)
1794 return u__strset(str
, c
); /* ASCII CP or SB char */
1796 c
&= 0xffff; /* Strip high bits */
1798 while(str
[0] && str
[1])
1804 str
[0] = '\0'; /* FIXME: OK to shorten? */
1809 /*********************************************************************
1810 * _mbsnbset(MSVCRT.@)
1812 unsigned char* CDECL
_mbsnbset(unsigned char *str
, unsigned int c
, size_t len
)
1814 unsigned char *ret
= str
;
1819 if(!get_mbcinfo()->ismbcodepage
|| c
< 256)
1820 return u__strnset(str
, c
, len
); /* ASCII CP or SB char */
1822 c
&= 0xffff; /* Strip high bits */
1824 while(str
[0] && str
[1] && (len
> 1))
1832 /* as per msdn pad with a blank character */
1839 /*********************************************************************
1840 * _mbsnset(MSVCRT.@)
1842 unsigned char* CDECL
_mbsnset(unsigned char* str
, unsigned int c
, size_t len
)
1844 unsigned char *ret
= str
;
1849 if(!get_mbcinfo()->ismbcodepage
|| c
< 256)
1850 return u__strnset(str
, c
, len
); /* ASCII CP or SB char */
1852 c
&= 0xffff; /* Strip high bits */
1854 while(str
[0] && str
[1] && len
--)
1860 str
[0] = '\0'; /* FIXME: OK to shorten? */
1865 /*********************************************************************
1866 * _mbsnccnt(MSVCRT.@)
1867 * 'c' is for 'character'.
1869 size_t CDECL
_mbsnccnt(const unsigned char* str
, size_t len
)
1872 if(get_mbcinfo()->ismbcodepage
)
1875 while(*str
&& len
-- > 0)
1877 if(_ismbblead(*str
))
1890 return min(ret
, len
); /* ASCII CP */
1893 /*********************************************************************
1894 * _mbsnbcnt(MSVCRT.@)
1895 * 'b' is for byte count.
1897 size_t CDECL
_mbsnbcnt(const unsigned char* str
, size_t len
)
1900 if(get_mbcinfo()->ismbcodepage
)
1902 const unsigned char* xstr
= str
;
1903 while(*xstr
&& len
-- > 0)
1905 if (_ismbblead(*xstr
++))
1911 return min(ret
, len
); /* ASCII CP */
1914 /*********************************************************************
1915 * _mbsnbcat(MSVCRT.@)
1917 unsigned char* CDECL
_mbsnbcat(unsigned char* dst
, const unsigned char* src
, size_t len
)
1919 if(get_mbcinfo()->ismbcodepage
)
1921 unsigned char *res
= dst
;
1923 if (_ismbblead(*dst
++)) {
1927 /* as per msdn overwrite the lead byte in front of '\0' */
1933 while (*src
&& len
--) *dst
++ = *src
++;
1937 return u_strncat(dst
, src
, len
); /* ASCII CP */
1940 int CDECL
_mbsnbcat_s(unsigned char *dst
, size_t size
, const unsigned char *src
, size_t len
)
1942 unsigned char *ptr
= dst
;
1945 if (!dst
&& !size
&& !src
&& !len
)
1948 if (!dst
|| !size
|| !src
)
1957 /* Find the null terminator of the destination buffer. */
1958 while (size
&& *ptr
)
1968 /* If necessary, check that the character preceding the null terminator is
1969 * a lead byte and move the pointer back by one for later overwrite. */
1970 if (ptr
!= dst
&& get_mbcinfo()->ismbcodepage
&& _ismbblead(*(ptr
- 1)))
1973 for (i
= 0; *src
&& i
< len
; i
++)
1990 /*********************************************************************
1991 * _mbsncat(MSVCRT.@)
1993 unsigned char* CDECL
_mbsncat(unsigned char* dst
, const unsigned char* src
, size_t len
)
1995 if(get_mbcinfo()->ismbcodepage
)
1997 unsigned char *res
= dst
;
2000 if (_ismbblead(*dst
++))
2003 while (*src
&& len
--)
2006 if(_ismbblead(*src
++))
2012 return u_strncat(dst
, src
, len
); /* ASCII CP */
2016 /*********************************************************************
2019 unsigned char* CDECL
_mbslwr(unsigned char* s
)
2021 unsigned char *ret
= s
;
2024 if (get_mbcinfo()->ismbcodepage
)
2029 c
= _mbctolower(_mbsnextc(s
));
2030 /* Note that I assume that the size of the character is unchanged */
2039 else for ( ; *s
; s
++) *s
= _tolower_l(*s
, NULL
);
2044 /*********************************************************************
2045 * _mbslwr_s(MSVCRT.@)
2047 int CDECL
_mbslwr_s(unsigned char* s
, size_t len
)
2053 else if (!s
|| !len
)
2058 if (get_mbcinfo()->ismbcodepage
)
2061 for ( ; *s
&& len
> 0; len
--)
2063 c
= _mbctolower(_mbsnextc(s
));
2064 /* Note that I assume that the size of the character is unchanged */
2073 else for ( ; *s
&& len
> 0; s
++, len
--) *s
= _tolower_l(*s
, NULL
);
2084 /*********************************************************************
2087 unsigned char* CDECL
_mbsupr(unsigned char* s
)
2089 unsigned char *ret
= s
;
2092 if (get_mbcinfo()->ismbcodepage
)
2097 c
= _mbctoupper(_mbsnextc(s
));
2098 /* Note that I assume that the size of the character is unchanged */
2107 else for ( ; *s
; s
++) *s
= _toupper_l(*s
, NULL
);
2112 /*********************************************************************
2113 * _mbsupr_s(MSVCRT.@)
2115 int CDECL
_mbsupr_s(unsigned char* s
, size_t len
)
2121 else if (!s
|| !len
)
2126 if (get_mbcinfo()->ismbcodepage
)
2129 for ( ; *s
&& len
> 0; len
--)
2131 c
= _mbctoupper(_mbsnextc(s
));
2132 /* Note that I assume that the size of the character is unchanged */
2141 else for ( ; *s
&& len
> 0; s
++, len
--) *s
= _toupper_l(*s
, NULL
);
2151 /*********************************************************************
2152 * _mbsspn_l (MSVCRT.@)
2154 size_t CDECL
_mbsspn_l(const unsigned char* string
,
2155 const unsigned char* set
, _locale_t locale
)
2157 const unsigned char *p
, *q
;
2159 for (p
= string
; *p
; p
++)
2161 for (q
= set
; *q
; q
++)
2163 if (_ismbblead_l(*q
, locale
))
2165 /* duplicate a bug in native implementation */
2168 if (p
[0] == q
[0] && p
[1] == q
[1])
2177 if (p
[0] == q
[0]) break;
2185 /*********************************************************************
2186 * _mbsspn (MSVCRT.@)
2188 size_t CDECL
_mbsspn(const unsigned char* string
, const unsigned char* set
)
2190 return _mbsspn_l(string
, set
, NULL
);
2193 /*********************************************************************
2194 * _mbsspnp (MSVCRT.@)
2196 unsigned char* CDECL
_mbsspnp(const unsigned char* string
, const unsigned char* set
)
2198 string
+= _mbsspn( string
, set
);
2199 return *string
? (unsigned char*)string
: NULL
;
2202 /*********************************************************************
2203 * _mbscspn_l (MSVCRT.@)
2205 size_t CDECL
_mbscspn_l(const unsigned char* str
,
2206 const unsigned char* cmp
, _locale_t locale
)
2208 const unsigned char *p
, *q
;
2210 for (p
= str
; *p
; p
++)
2212 for (q
= cmp
; *q
; q
++)
2214 if (_ismbblead_l(*q
, locale
))
2216 /* duplicate a bug in native implementation */
2217 if (!q
[1]) return 0;
2219 if (p
[0] == q
[0] && p
[1] == q
[1])
2223 else if (p
[0] == q
[0])
2230 /*********************************************************************
2231 * _mbscspn (MSVCRT.@)
2233 size_t CDECL
_mbscspn(const unsigned char* str
, const unsigned char* cmp
)
2235 return _mbscspn_l(str
, cmp
, NULL
);
2238 /*********************************************************************
2239 * _mbsrev (MSVCRT.@)
2241 unsigned char* CDECL
_mbsrev(unsigned char* str
)
2243 int i
, len
= _mbslen(str
);
2244 unsigned char *p
, *temp
=malloc(len
*2);
2249 /* unpack multibyte string to temp buffer */
2251 for(i
=0; i
<len
; i
++)
2265 /* repack it in the reverse order */
2267 for(i
=len
-1; i
>=0; i
--)
2269 if(_ismbblead(temp
[i
*2]))
2285 /*********************************************************************
2286 * _mbspbrk (MSVCRT.@)
2288 unsigned char* CDECL
_mbspbrk(const unsigned char* str
, const unsigned char* accept
)
2290 const unsigned char* p
;
2294 for(p
= accept
; *p
; p
+= (_ismbblead(*p
)?2:1) )
2297 if( !_ismbblead(*p
) || ( *(p
+1) == *(str
+1) ) )
2298 return (unsigned char*)str
;
2300 str
+= (_ismbblead(*str
)?2:1);
2307 * Functions depending on locale codepage
2310 /*********************************************************************
2313 * Unlike most of the multibyte string functions this function uses
2314 * the locale codepage, not the codepage set by _setmbcp
2316 int CDECL
mblen(const char* str
, size_t size
)
2318 if (str
&& *str
&& size
)
2320 if(get_locinfo()->mb_cur_max
== 1)
2321 return 1; /* ASCII CP */
2323 return !isleadbyte((unsigned char)*str
) ? 1 : (size
>1 ? 2 : -1);
2328 /*********************************************************************
2331 size_t CDECL
mbrlen(const char *str
, size_t len
, mbstate_t *state
)
2333 mbstate_t s
= (state
? *state
: 0);
2336 if(!len
|| !str
|| !*str
)
2339 if(get_locinfo()->mb_cur_max
== 1) {
2341 }else if(!s
&& isleadbyte((unsigned char)*str
)) {
2343 s
= (unsigned char)*str
;
2360 /*********************************************************************
2361 * _mbstrlen_l(MSVCRT.@)
2363 size_t CDECL
_mbstrlen_l(const char* str
, _locale_t locale
)
2365 pthreadlocinfo locinfo
;
2368 locinfo
= get_locinfo();
2370 locinfo
= locale
->locinfo
;
2372 if(locinfo
->mb_cur_max
> 1) {
2374 len
= MultiByteToWideChar(locinfo
->lc_codepage
, MB_ERR_INVALID_CHARS
,
2386 /*********************************************************************
2387 * _mbstrlen(MSVCRT.@)
2389 size_t CDECL
_mbstrlen(const char* str
)
2391 return _mbstrlen_l(str
, NULL
);
2394 /*********************************************************************
2395 * _mbtowc_l(MSVCRT.@)
2397 int CDECL
_mbtowc_l(wchar_t *dst
, const char* str
, size_t n
, _locale_t locale
)
2399 pthreadlocinfo locinfo
;
2403 locinfo
= get_locinfo();
2405 locinfo
= locale
->locinfo
;
2415 if(!locinfo
->lc_codepage
) {
2416 if(dst
) *dst
= (unsigned char)*str
;
2419 if(n
>=2 && _isleadbyte_l((unsigned char)*str
, locale
)) {
2420 if(!MultiByteToWideChar(locinfo
->lc_codepage
, 0, str
, 2, &tmpdst
, 1))
2422 if(dst
) *dst
= tmpdst
;
2425 if(!MultiByteToWideChar(locinfo
->lc_codepage
, 0, str
, 1, &tmpdst
, 1))
2427 if(dst
) *dst
= tmpdst
;
2431 /*********************************************************************
2434 int CDECL
mbtowc(wchar_t *dst
, const char* str
, size_t n
)
2436 return _mbtowc_l(dst
, str
, n
, NULL
);
2439 /*********************************************************************
2442 wint_t CDECL
btowc(int c
)
2444 unsigned char letter
= c
;
2449 if(!get_locinfo()->lc_codepage
)
2451 if(!MultiByteToWideChar(get_locinfo()->lc_codepage
,
2452 MB_ERR_INVALID_CHARS
, (LPCSTR
)&letter
, 1, &ret
, 1))
2458 /*********************************************************************
2461 size_t CDECL
mbrtowc(wchar_t *dst
, const char *str
,
2462 size_t n
, mbstate_t *state
)
2464 pthreadlocinfo locinfo
= get_locinfo();
2465 mbstate_t s
= (state
? *state
: 0);
2472 if(!n
|| !str
|| !*str
)
2475 if(locinfo
->mb_cur_max
== 1) {
2476 tmpstr
[len
++] = *str
;
2477 }else if(!s
&& isleadbyte((unsigned char)*str
)) {
2479 s
= (unsigned char)*str
;
2487 tmpstr
[len
++] = *str
;
2496 if(!MultiByteToWideChar(locinfo
->lc_codepage
, 0, tmpstr
, len
, dst
, dst
? 1 : 0))
2505 /*********************************************************************
2506 * _mbstowcs_l(MSVCRT.@)
2508 size_t CDECL
_mbstowcs_l(wchar_t *wcstr
, const char *mbstr
,
2509 size_t count
, _locale_t locale
)
2511 pthreadlocinfo locinfo
;
2520 locinfo
= get_locinfo();
2522 locinfo
= locale
->locinfo
;
2524 if(!locinfo
->lc_codepage
) {
2526 return strlen(mbstr
);
2528 for(i
=0; i
<count
; i
++) {
2529 wcstr
[i
] = (unsigned char)mbstr
[i
];
2530 if(!wcstr
[i
]) break;
2535 /* Ignore count parameter */
2537 size
= MultiByteToWideChar(locinfo
->lc_codepage
,
2538 MB_ERR_INVALID_CHARS
, mbstr
, -1, NULL
, 0);
2546 for(i
=0, size
=0; i
<count
; i
++) {
2547 if(mbstr
[size
] == '\0')
2550 size
+= (_isleadbyte_l((unsigned char)mbstr
[size
], locale
) ? 2 : 1);
2554 size
= MultiByteToWideChar(locinfo
->lc_codepage
,
2555 MB_ERR_INVALID_CHARS
, mbstr
, size
, wcstr
, count
);
2557 if(count
) wcstr
[0] = '\0';
2569 /*********************************************************************
2570 * mbstowcs(MSVCRT.@)
2572 size_t CDECL
mbstowcs(wchar_t *wcstr
,
2573 const char *mbstr
, size_t count
)
2575 return _mbstowcs_l(wcstr
, mbstr
, count
, NULL
);
2578 /*********************************************************************
2579 * _mbstowcs_s_l(MSVCRT.@)
2581 int CDECL
_mbstowcs_s_l(size_t *ret
, wchar_t *wcstr
,
2582 size_t size
, const char *mbstr
, size_t count
, _locale_t locale
)
2587 if(!wcstr
&& !size
) {
2588 conv
= _mbstowcs_l(NULL
, mbstr
, 0, locale
);
2594 if (!MSVCRT_CHECK_PMT(wcstr
!= NULL
)) return EINVAL
;
2595 if (!MSVCRT_CHECK_PMT(mbstr
!= NULL
)) {
2596 if(size
) wcstr
[0] = '\0';
2600 if(count
==MSVCRT__TRUNCATE
|| size
<count
)
2605 conv
= _mbstowcs_l(wcstr
, mbstr
, conv
, locale
);
2607 wcstr
[conv
++] = '\0';
2608 else if(conv
==size
&& count
==MSVCRT__TRUNCATE
&& wcstr
[conv
-1]!='\0') {
2609 wcstr
[conv
-1] = '\0';
2611 }else if(conv
==size
&& wcstr
[conv
-1]!='\0') {
2612 MSVCRT_INVALID_PMT("wcstr[size] is too small", ERANGE
);
2623 /*********************************************************************
2624 * mbstowcs_s(MSVCRT.@)
2626 int CDECL
_mbstowcs_s(size_t *ret
, wchar_t *wcstr
,
2627 size_t size
, const char *mbstr
, size_t count
)
2629 return _mbstowcs_s_l(ret
, wcstr
, size
, mbstr
, count
, NULL
);
2632 /*********************************************************************
2633 * mbsrtowcs(MSVCRT.@)
2635 size_t CDECL
mbsrtowcs(wchar_t *wcstr
,
2636 const char **pmbstr
, size_t count
, mbstate_t *state
)
2638 mbstate_t s
= (state
? *state
: 0);
2643 if(!MSVCRT_CHECK_PMT(pmbstr
!= NULL
))
2647 while(!wcstr
|| count
>ret
) {
2648 int ch_len
= mbrtowc(&tmpdst
, p
, 2, &s
);
2650 wcstr
[ret
] = tmpdst
;
2654 }else if(ch_len
== 0) {
2655 if(wcstr
) *pmbstr
= NULL
;
2663 if(wcstr
) *pmbstr
= p
;
2667 /*********************************************************************
2668 * mbsrtowcs_s(MSVCRT.@)
2670 int CDECL
mbsrtowcs_s(size_t *ret
, wchar_t *wcstr
, size_t len
,
2671 const char **mbstr
, size_t count
, mbstate_t *state
)
2675 if(!ret
) ret
= &tmp
;
2676 if(!MSVCRT_CHECK_PMT(!!wcstr
== !!len
)) {
2681 *ret
= mbsrtowcs(wcstr
, mbstr
, count
>len
? len
: count
, state
);
2683 if(wcstr
) *wcstr
= 0;
2688 /* no place for terminating '\0' */
2689 if(wcstr
) *wcstr
= 0;
2692 if(wcstr
) wcstr
[(*ret
)-1] = 0;
2696 /*********************************************************************
2697 * _mbctohira (MSVCRT.@)
2699 * Converts a sjis katakana character to hiragana.
2701 unsigned int CDECL
_mbctohira(unsigned int c
)
2703 if(_ismbckata(c
) && c
<= 0x8393)
2704 return (c
- 0x8340 - (c
>= 0x837f ? 1 : 0)) + 0x829f;
2708 /*********************************************************************
2709 * _mbctokata (MSVCRT.@)
2711 * Converts a sjis hiragana character to katakana.
2713 unsigned int CDECL
_mbctokata(unsigned int c
)
2716 return (c
- 0x829f) + 0x8340 + (c
>= 0x82de ? 1 : 0);
2720 /*********************************************************************
2721 * _ismbcl0_l (MSVCRT.@)
2723 int CDECL
_ismbcl0_l(unsigned int c
, _locale_t locale
)
2725 pthreadmbcinfo mbcinfo
;
2728 mbcinfo
= get_mbcinfo();
2730 mbcinfo
= locale
->mbcinfo
;
2732 if(mbcinfo
->mbcodepage
== 932)
2735 return _ismbclegal_l(c
, locale
) && c
>= 0x8140 && c
<= 0x889e;
2741 /*********************************************************************
2742 * _ismbcl0 (MSVCRT.@)
2744 int CDECL
_ismbcl0(unsigned int c
)
2746 return _ismbcl0_l(c
, NULL
);
2749 /*********************************************************************
2750 * _ismbcl1_l (MSVCRT.@)
2752 int CDECL
_ismbcl1_l(unsigned int c
, _locale_t locale
)
2754 pthreadmbcinfo mbcinfo
;
2757 mbcinfo
= get_mbcinfo();
2759 mbcinfo
= locale
->mbcinfo
;
2761 if(mbcinfo
->mbcodepage
== 932)
2764 return _ismbclegal_l(c
, locale
) && c
>= 0x889f && c
<= 0x9872;
2770 /*********************************************************************
2771 * _ismbcl1 (MSVCRT.@)
2773 int CDECL
_ismbcl1(unsigned int c
)
2775 return _ismbcl1_l(c
, NULL
);
2778 /*********************************************************************
2779 * _ismbcl2_l (MSVCRT.@)
2781 int CDECL
_ismbcl2_l(unsigned int c
, _locale_t locale
)
2783 pthreadmbcinfo mbcinfo
;
2786 mbcinfo
= get_mbcinfo();
2788 mbcinfo
= locale
->mbcinfo
;
2790 if(mbcinfo
->mbcodepage
== 932)
2793 return _ismbclegal_l(c
, locale
) && c
>= 0x989f && c
<= 0xeaa4;
2799 /*********************************************************************
2800 * _ismbcl2 (MSVCRT.@)
2802 int CDECL
_ismbcl2(unsigned int c
)
2804 return _ismbcl2_l(c
, NULL
);