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.
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt
);
38 unsigned char MSVCRT_mbctype
[257] = { 0 };
40 /* It seems that the data about valid trail bytes is not available from kernel32
41 * so we have to store is here. The format is the same as for lead bytes in CPINFO */
42 struct cp_extra_info_t
45 BYTE TrailBytes
[MAX_LEADBYTES
];
48 static struct cp_extra_info_t g_cpextrainfo
[] =
50 {932, {0x40, 0x7e, 0x80, 0xfc, 0, 0}},
51 {936, {0x40, 0xfe, 0, 0}},
52 {949, {0x41, 0xfe, 0, 0}},
53 {950, {0x40, 0x7e, 0xa1, 0xfe, 0, 0}},
54 {1361, {0x31, 0x7e, 0x81, 0xfe, 0, 0}},
55 {20932, {1, 255, 0, 0}}, /* seems to give different results on different systems */
56 {0, {1, 255, 0, 0}} /* match all with FIXME */
59 /* Maps cp932 single byte character to multi byte character */
60 static const unsigned char mbbtombc_932
[] = {
61 0x40,0x49,0x68,0x94,0x90,0x93,0x95,0x66,0x69,0x6a,0x96,0x7b,0x43,0x7c,0x44,0x5e,
62 0x4f,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x46,0x47,0x83,0x81,0x84,0x48,
63 0x97,0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,
64 0x6f,0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x6d,0x8f,0x6e,0x4f,0x76,
65 0x77,0x78,0x79,0x6d,0x8f,0x6e,0x4f,0x51,0x65,0x81,0x82,0x83,0x84,0x85,0x86,0x87,
66 0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f,0x90,0x91,0x92,0x93,0x94,0x95,0x50,
67 0x42,0x75,0x76,0x41,0x45,0x92,0x40,0x42,0x44,0x46,0x48,0x83,0x85,0x87,0x62,
68 0x5b,0x41,0x43,0x45,0x47,0x49,0x4a,0x4c,0x4e,0x50,0x52,0x54,0x56,0x58,0x5a,0x5c,
69 0x5e,0x60,0x63,0x65,0x67,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x71,0x74,0x77,0x7a,0x7d,
70 0x7e,0x80,0x81,0x82,0x84,0x86,0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8f,0x93,0x4a,0x4b };
72 /* Maps multibyte cp932 punctuation marks to single byte equivalents */
73 static const unsigned char mbctombb_932_punct
[] = {
74 0x20,0xa4,0xa1,0x2c,0x2e,0xa5,0x3a,0x3b,0x3f,0x21,0xde,0xdf,0x00,0x00,0x00,0x5e,
75 0x7e,0x5f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xb0,0x00,0x00,0x2f,0x00,
76 0x00,0x00,0x7c,0x00,0x00,0x60,0x27,0x00,0x22,0x28,0x29,0x00,0x00,0x5b,0x5d,0x7b,
77 0x7d,0x00,0x00,0x00,0x00,0xa2,0xa3,0x00,0x00,0x00,0x00,0x2b,0x2d,0x00,0x00,0x00,
78 0x00,0x3d,0x00,0x3c,0x3e,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x5c,
79 0x24,0x00,0x00,0x25,0x23,0x26,0x2a,0x40};
81 /* Maps multibyte cp932 hiragana/katakana to single-byte equivalents */
82 static const unsigned char mbctombb_932_kana
[] = {
83 0xa7,0xb1,0xa8,0xb2,0xa9,0xb3,0xaa,0xb4,0xab,0xb5,0xb6,0xb6,0xb7,0xb7,0xb8,0xb8,
84 0xb9,0xb9,0xba,0xba,0xbb,0xbb,0xbc,0xbc,0xbd,0xbd,0xbe,0xbe,0xbf,0xbf,0xc0,0xc0,
85 0xc1,0xc1,0xaf,0xc2,0xc2,0xc3,0xc3,0xc4,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xca,
86 0xca,0xcb,0xcb,0xcb,0xcc,0xcc,0xcc,0xcd,0xcd,0xcd,0xce,0xce,0xce,0xcf,0xd0,0xd1,
87 0xd2,0xd3,0xac,0xd4,0xad,0xd5,0xae,0xd6,0xd7,0xd8,0xd9,0xda,0xdb,0xdc,0xdc,0xb2,
88 0xb4,0xa6,0xdd,0xb3,0xb6,0xb9};
90 static wchar_t msvcrt_mbc_to_wc_l(unsigned int ch
, _locale_t locale
)
92 pthreadmbcinfo mbcinfo
;
98 mbcinfo
= locale
->mbcinfo
;
100 mbcinfo
= get_mbcinfo();
106 mbch
[0] = (ch
>> 8) & 0xff;
110 if (!MultiByteToWideChar(mbcinfo
->mbcodepage
, 0, mbch
, n_chars
, &chW
, 1))
112 WARN("MultiByteToWideChar failed on %x\n", ch
);
118 static wchar_t msvcrt_mbc_to_wc(unsigned int ch
)
120 return msvcrt_mbc_to_wc_l(ch
, NULL
);
123 static inline size_t u_strlen( const unsigned char *str
)
125 return strlen( (const char*) str
);
128 static inline unsigned char* u_strncat( unsigned char* dst
, const unsigned char* src
, size_t len
)
130 return (unsigned char*)strncat( (char*)dst
, (const char*)src
, len
);
133 static inline int u_strcmp( const unsigned char *s1
, const unsigned char *s2
)
135 return strcmp( (const char*)s1
, (const char*)s2
);
138 static inline int u_strcasecmp( const unsigned char *s1
, const unsigned char *s2
)
140 return _stricmp( (const char*)s1
, (const char*)s2
);
143 static inline int u_strncmp( const unsigned char *s1
, const unsigned char *s2
, size_t len
)
145 return strncmp( (const char*)s1
, (const char*)s2
, len
);
148 static inline int u_strncasecmp( const unsigned char *s1
, const unsigned char *s2
, size_t len
)
150 return _strnicmp( (const char*)s1
, (const char*)s2
, len
);
153 static inline unsigned char *u_strchr( const unsigned char *s
, unsigned char x
)
155 return (unsigned char*) strchr( (const char*)s
, x
);
158 static inline unsigned char *u_strrchr( const unsigned char *s
, unsigned char x
)
160 return (unsigned char*) strrchr( (const char*)s
, x
);
163 static inline unsigned char *u__strset( unsigned char *s
, unsigned char c
)
165 return (unsigned char*) _strset( (char*)s
, c
);
168 static inline unsigned char *u__strnset( unsigned char *s
, unsigned char c
, size_t len
)
170 return (unsigned char*) _strnset( (char*)s
, c
, len
);
173 /*********************************************************************
174 * __p__mbctype (MSVCRT.@)
176 unsigned char* CDECL
__p__mbctype(void)
178 return get_mbcinfo()->mbctype
;
181 /*********************************************************************
182 * __p___mb_cur_max(MSVCRT.@)
184 int* CDECL
__p___mb_cur_max(void)
186 return &get_locinfo()->mb_cur_max
;
189 /*********************************************************************
190 * ___mb_cur_max_func(MSVCRT.@)
192 int CDECL
___mb_cur_max_func(void)
194 return get_locinfo()->mb_cur_max
;
198 /*********************************************************************
199 * ___mb_cur_max_l_func (MSVCR80.@)
201 int CDECL
___mb_cur_max_l_func(_locale_t locale
)
203 pthreadlocinfo locinfo
;
206 locinfo
= get_locinfo();
208 locinfo
= locale
->locinfo
;
210 return locinfo
->mb_cur_max
;
214 threadmbcinfo
* create_mbcinfo(int cp
, LCID lcid
, threadmbcinfo
*old_mbcinfo
)
216 threadmbcinfo
*mbcinfo
;
222 WCHAR bufW
[256], lowW
[256], upW
[256];
227 if(old_mbcinfo
&& cp
==old_mbcinfo
->mbcodepage
228 && (lcid
==-1 || lcid
==old_mbcinfo
->mblcid
)) {
229 InterlockedIncrement(&old_mbcinfo
->refcount
);
233 mbcinfo
= malloc(sizeof(threadmbcinfo
));
236 mbcinfo
->refcount
= 1;
247 newcp
= get_locinfo()->lc_codepage
;
250 /* fall through (C locale) */
252 newcp
= 20127; /* ASCII */
260 sprintf(bufA
, ".%d", newcp
);
261 mbcinfo
->mblcid
= locale_to_LCID(bufA
, NULL
, NULL
);
263 mbcinfo
->mblcid
= lcid
;
266 if(mbcinfo
->mblcid
== -1)
268 WARN("Can't assign LCID to codepage (%d)\n", mbcinfo
->mblcid
);
272 if (!GetCPInfo(newcp
, &cpi
))
274 WARN("Codepage %d not found\n", newcp
);
279 /* setup the _mbctype */
280 memset(mbcinfo
->mbctype
, 0, sizeof(unsigned char[257]));
281 memset(mbcinfo
->mbcasemap
, 0, sizeof(unsigned char[256]));
283 bytes
= cpi
.LeadByte
;
284 while (bytes
[0] || bytes
[1])
286 for (i
= bytes
[0]; i
<= bytes
[1]; i
++)
287 mbcinfo
->mbctype
[i
+ 1] |= _M1
;
291 if (cpi
.MaxCharSize
> 1)
293 /* trail bytes not available through kernel32 but stored in a structure in msvcrt */
294 struct cp_extra_info_t
*cpextra
= g_cpextrainfo
;
296 mbcinfo
->ismbcodepage
= 1;
299 if (cpextra
->cp
== 0 || cpextra
->cp
== newcp
)
301 if (cpextra
->cp
== 0)
302 FIXME("trail bytes data not available for DBCS codepage %d - assuming all bytes\n", newcp
);
304 bytes
= cpextra
->TrailBytes
;
305 while (bytes
[0] || bytes
[1])
307 for (i
= bytes
[0]; i
<= bytes
[1]; i
++)
308 mbcinfo
->mbctype
[i
+ 1] |= _M2
;
317 mbcinfo
->ismbcodepage
= 0;
319 /* we can't use GetStringTypeA directly because we don't have a locale - only a code page
322 for (i
= 0; i
< 256; i
++)
323 if (!(mbcinfo
->mbctype
[i
+ 1] & _M1
))
324 bufA
[charcount
++] = i
;
326 ret
= MultiByteToWideChar(newcp
, 0, bufA
, charcount
, bufW
, charcount
);
327 if (ret
!= charcount
)
328 ERR("MultiByteToWideChar of chars failed for cp %d, ret=%d (exp %d), error=%d\n", newcp
, ret
, charcount
, GetLastError());
330 GetStringTypeW(CT_CTYPE1
, bufW
, charcount
, chartypes
);
331 LCMapStringW(lcid
, LCMAP_LOWERCASE
, bufW
, charcount
, lowW
, charcount
);
332 LCMapStringW(lcid
, LCMAP_UPPERCASE
, bufW
, charcount
, upW
, charcount
);
335 for (i
= 0; i
< 256; i
++)
336 if (!(mbcinfo
->mbctype
[i
+ 1] & _M1
))
338 if (chartypes
[charcount
] & C1_UPPER
)
340 mbcinfo
->mbctype
[i
+ 1] |= _SBUP
;
341 bufW
[charcount
] = lowW
[charcount
];
343 else if (chartypes
[charcount
] & C1_LOWER
)
345 mbcinfo
->mbctype
[i
+ 1] |= _SBLOW
;
346 bufW
[charcount
] = upW
[charcount
];
351 ret
= WideCharToMultiByte(newcp
, 0, bufW
, charcount
, bufA
, charcount
, NULL
, NULL
);
352 if (ret
!= charcount
)
353 ERR("WideCharToMultiByte failed for cp %d, ret=%d (exp %d), error=%d\n", newcp
, ret
, charcount
, GetLastError());
356 for (i
= 0; i
< 256; i
++)
358 if(!(mbcinfo
->mbctype
[i
+ 1] & _M1
))
360 if(mbcinfo
->mbctype
[i
] & (C1_UPPER
|C1_LOWER
))
361 mbcinfo
->mbcasemap
[i
] = bufA
[charcount
];
366 if (newcp
== 932) /* CP932 only - set _MP and _MS */
368 /* On Windows it's possible to calculate the _MP and _MS from CT_CTYPE1
369 * and CT_CTYPE3. But as of Wine 0.9.43 we return wrong values what makes
370 * it hard. As this is set only for codepage 932 we hardcode it what gives
371 * also faster execution.
373 for (i
= 161; i
<= 165; i
++)
374 mbcinfo
->mbctype
[i
+ 1] |= _MP
;
375 for (i
= 166; i
<= 223; i
++)
376 mbcinfo
->mbctype
[i
+ 1] |= _MS
;
379 mbcinfo
->mbcodepage
= newcp
;
383 /*********************************************************************
384 * _setmbcp (MSVCRT.@)
386 int CDECL
_setmbcp(int cp
)
388 thread_data_t
*data
= msvcrt_get_thread_data();
389 threadmbcinfo
*mbcinfo
;
391 mbcinfo
= create_mbcinfo(cp
, -1, get_mbcinfo());
398 if(data
->locale_flags
& LOCALE_THREAD
)
400 if(data
->locale_flags
& LOCALE_FREE
)
401 free_mbcinfo(data
->mbcinfo
);
402 data
->mbcinfo
= mbcinfo
;
407 free_mbcinfo(MSVCRT_locale
->mbcinfo
);
408 MSVCRT_locale
->mbcinfo
= mbcinfo
;
409 memcpy(MSVCRT_mbctype
, MSVCRT_locale
->mbcinfo
->mbctype
, sizeof(MSVCRT_mbctype
));
410 _unlock(_MB_CP_LOCK
);
415 /*********************************************************************
416 * _getmbcp (MSVCRT.@)
418 int CDECL
_getmbcp(void)
420 return get_mbcinfo()->mbcodepage
;
423 /*********************************************************************
424 * _mbsnextc_l(MSVCRT.@)
426 unsigned int CDECL
_mbsnextc_l(const unsigned char* str
, _locale_t locale
)
428 if(_ismbblead_l(*str
, locale
))
429 return *str
<< 8 | str
[1];
433 /*********************************************************************
434 * _mbsnextc(MSVCRT.@)
436 unsigned int CDECL
_mbsnextc(const unsigned char* str
)
438 return _mbsnextc_l(str
, NULL
);
441 /*********************************************************************
442 * _mbctolower(MSVCRT.@)
444 unsigned int CDECL
_mbctolower(unsigned int c
)
448 FIXME("Handle MBC chars\n");
451 return _tolower_l(c
, NULL
); /* ASCII CP or SB char */
454 /*********************************************************************
455 * _mbctoupper(MSVCRT.@)
457 unsigned int CDECL
_mbctoupper(unsigned int c
)
461 FIXME("Handle MBC chars\n");
464 return _toupper_l(c
, NULL
); /* ASCII CP or SB char */
467 /*********************************************************************
468 * _mbctombb (MSVCRT.@)
470 unsigned int CDECL
_mbctombb(unsigned int c
)
474 if(get_mbcinfo()->mbcodepage
== 932)
476 if(c
>= 0x829f && c
<= 0x82f1) /* Hiragana */
477 return mbctombb_932_kana
[c
- 0x829f];
478 if(c
>= 0x8340 && c
<= 0x8396 && c
!= 0x837f) /* Katakana */
479 return mbctombb_932_kana
[c
- 0x8340 - (c
>= 0x837f ? 1 : 0)];
480 if(c
>= 0x8140 && c
<= 0x8197) /* Punctuation */
482 value
= mbctombb_932_punct
[c
- 0x8140];
483 return value
? value
: c
;
485 if((c
>= 0x824f && c
<= 0x8258) || /* Fullwidth digits */
486 (c
>= 0x8260 && c
<= 0x8279)) /* Fullwidth capitals letters */
488 if(c
>= 0x8281 && c
<= 0x829a) /* Fullwidth small letters */
490 /* all other cases return c */
495 /*********************************************************************
496 * _mbcjistojms(MSVCRT.@)
498 * Converts a jis character to sjis.
499 * Based on description from
500 * http://www.slayers.ne.jp/~oouchi/code/jistosjis.html
502 unsigned int CDECL
_mbcjistojms(unsigned int c
)
504 /* Conversion takes place only when codepage is 932.
505 In all other cases, c is returned unchanged */
506 if(get_mbcinfo()->mbcodepage
== 932)
508 if(HIBYTE(c
) >= 0x21 && HIBYTE(c
) <= 0x7e &&
509 LOBYTE(c
) >= 0x21 && LOBYTE(c
) <= 0x7e)
516 if(LOBYTE(c
) >= 0x7F)
519 c
= (((HIBYTE(c
) - 0x21)/2 + 0x81) << 8) | LOBYTE(c
);
525 return 0; /* Codepage is 932, but c can't be converted */
531 /*********************************************************************
532 * _mbcjmstojis(MSVCRT.@)
534 * Converts a sjis character to jis.
536 unsigned int CDECL
_mbcjmstojis(unsigned int c
)
538 /* Conversion takes place only when codepage is 932.
539 In all other cases, c is returned unchanged */
540 if(get_mbcinfo()->mbcodepage
== 932)
542 if(_ismbclegal(c
) && HIBYTE(c
) < 0xf0)
544 if(HIBYTE(c
) >= 0xe0)
547 c
= (((HIBYTE(c
) - 0x81)*2 + 0x21) << 8) | LOBYTE(c
);
558 return 0; /* Codepage is 932, but c can't be converted */
564 /*********************************************************************
567 unsigned char* CDECL
_mbsdec(const unsigned char* start
, const unsigned char* cur
)
572 if(get_mbcinfo()->ismbcodepage
)
573 return (unsigned char *)(_ismbstrail(start
,cur
-1) ? cur
- 2 : cur
-1);
575 return (unsigned char *)cur
- 1; /* ASCII CP or SB char */
578 /*********************************************************************
581 size_t CDECL
_mbclen(const unsigned char* str
)
583 return _ismbblead(*str
) ? 2 : 1;
586 /*********************************************************************
589 unsigned char* CDECL
_mbsinc(const unsigned char* str
)
591 return (unsigned char *)(str
+ _mbclen(str
));
594 /*********************************************************************
597 unsigned char* CDECL
_mbsninc(const unsigned char* str
, size_t num
)
602 while (num
> 0 && *str
)
604 if (_ismbblead(*str
))
614 return (unsigned char*)str
;
617 /*********************************************************************
618 * _mbsnlen_l(MSVCRT.@)
620 size_t CDECL
_mbsnlen_l(const unsigned char *str
,
621 size_t maxsize
, _locale_t locale
)
623 pthreadmbcinfo mbcinfo
;
624 size_t i
= 0, len
= 0;
627 mbcinfo
= get_mbcinfo();
629 mbcinfo
= locale
->mbcinfo
;
631 if(!mbcinfo
->ismbcodepage
)
632 return strnlen((const char*)str
, maxsize
);
634 while(i
<maxsize
&& str
[i
])
636 if (_ismbblead_l(str
[i
], locale
))
639 if (!str
[i
]) /* count only full chars */
645 return i
< maxsize
? len
: maxsize
;
648 /*********************************************************************
651 size_t CDECL
_mbslen(const unsigned char* str
)
653 return _mbsnlen_l(str
, -1, NULL
);
656 /*********************************************************************
657 * _mbslen_l(MSVCRT.@)
659 size_t CDECL
_mbslen_l(const unsigned char* str
, _locale_t locale
)
661 return _mbsnlen_l(str
, -1, locale
);
664 /*********************************************************************
667 size_t CDECL
_mbsnlen(const unsigned char* str
, size_t maxsize
)
669 return _mbsnlen_l(str
, maxsize
, NULL
);
672 /*********************************************************************
673 * _mbccpy_s_l(MSVCRT.@)
675 int CDECL
_mbccpy_s_l(unsigned char* dest
, size_t maxsize
,
676 int *copied
, const unsigned char* src
, _locale_t locale
)
678 if(copied
) *copied
= 0;
679 if(!MSVCRT_CHECK_PMT(dest
!= NULL
&& maxsize
>= 1)) return EINVAL
;
681 if(!MSVCRT_CHECK_PMT(src
!= NULL
)) return EINVAL
;
683 if(_ismbblead_l(*src
, locale
)) {
685 if(copied
) *copied
= 1;
691 MSVCRT_INVALID_PMT("dst buffer is too small", ERANGE
);
697 if(copied
) *copied
= 2;
700 if(copied
) *copied
= 1;
706 /*********************************************************************
709 void CDECL
_mbccpy(unsigned char* dest
, const unsigned char* src
)
711 _mbccpy_s_l(dest
, 2, NULL
, src
, NULL
);
714 /*********************************************************************
715 * _mbccpy_l(MSVCRT.@)
717 void CDECL
_mbccpy_l(unsigned char* dest
, const unsigned char* src
,
720 _mbccpy_s_l(dest
, 2, NULL
, src
, locale
);
723 /*********************************************************************
724 * _mbccpy_s(MSVCRT.@)
726 int CDECL
_mbccpy_s(unsigned char* dest
, size_t maxsize
,
727 int *copied
, const unsigned char* src
)
729 return _mbccpy_s_l(dest
, maxsize
, copied
, src
, NULL
);
732 /*********************************************************************
735 * The parameter n is the number or characters to copy, not the size of
736 * the buffer. Use _mbsnbcpy for a function analogical to strncpy
738 unsigned char* CDECL
_mbsncpy(unsigned char* dst
, const unsigned char* src
, size_t n
)
740 unsigned char* ret
= dst
;
743 if (get_mbcinfo()->ismbcodepage
)
748 if (_ismbblead(*src
))
768 if (!(*dst
++ = *src
++)) break;
771 while (n
--) *dst
++ = 0;
775 /*********************************************************************
776 * _mbsnbcpy_s_l(MSVCRT.@)
778 * Unlike _mbsnbcpy this function does not pad the rest of the dest
781 int CDECL
_mbsnbcpy_s_l(unsigned char* dst
, size_t size
,
782 const unsigned char* src
, size_t n
, _locale_t locale
)
786 if(!dst
|| size
== 0)
796 if((locale
? locale
->mbcinfo
: get_mbcinfo())->ismbcodepage
)
798 BOOL is_lead
= FALSE
;
806 is_lead
= (!is_lead
&& _ismbblead(*src
));
811 if (is_lead
) /* if string ends with a lead, remove it */
841 /*********************************************************************
842 * _mbsnbcpy_s(MSVCRT.@)
844 int CDECL
_mbsnbcpy_s(unsigned char* dst
, size_t size
, const unsigned char* src
, size_t n
)
846 return _mbsnbcpy_s_l(dst
, size
, src
, n
, NULL
);
849 /*********************************************************************
850 * _mbscpy_s_l(MSVCRT.@)
852 int CDECL
_mbscpy_s_l(unsigned char *dst
, size_t size
,
853 const unsigned char *src
, _locale_t locale
)
855 return _mbsnbcpy_s_l(dst
, size
, src
, -1, locale
);
858 /*********************************************************************
859 * _mbscpy_s(MSVCRT.@)
861 int CDECL
_mbscpy_s(unsigned char *dst
, size_t size
, const unsigned char *src
)
863 return _mbscpy_s_l(dst
, size
, src
, NULL
);
866 /*********************************************************************
867 * _mbsnbcpy(MSVCRT.@)
869 * Like strncpy this function doesn't enforce the string to be
872 unsigned char* CDECL
_mbsnbcpy(unsigned char* dst
, const unsigned char* src
, size_t n
)
874 unsigned char* ret
= dst
;
877 if(get_mbcinfo()->ismbcodepage
)
879 BOOL is_lead
= FALSE
;
882 is_lead
= (!is_lead
&& _ismbblead(*src
));
887 if (is_lead
) /* if string ends with a lead, remove it */
895 if (!(*dst
++ = *src
++)) break;
898 while (n
--) *dst
++ = 0;
902 /*********************************************************************
903 * _mbscmp_l(MSVCRT.@)
905 int CDECL
_mbscmp_l(const unsigned char* str
, const unsigned char* cmp
, _locale_t locale
)
907 pthreadmbcinfo mbcinfo
;
909 if (!str
|| !cmp
) return INT_MAX
;
911 mbcinfo
= locale
? locale
->mbcinfo
: get_mbcinfo();
913 if(mbcinfo
->ismbcodepage
)
915 unsigned int strc
, cmpc
;
918 return *cmp
? -1 : 0;
921 strc
= _mbsnextc_l(str
, locale
);
922 cmpc
= _mbsnextc_l(cmp
, locale
);
924 return strc
< cmpc
? -1 : 1;
925 str
+=(strc
> 255) ? 2 : 1;
926 cmp
+=(strc
> 255) ? 2 : 1; /* equal, use same increment */
929 return u_strcmp(str
, cmp
); /* ASCII CP */
932 /*********************************************************************
935 int CDECL
_mbscmp(const unsigned char* str
, const unsigned char* cmp
)
937 return _mbscmp_l(str
, cmp
, NULL
);
940 /*********************************************************************
941 * _mbsnbicoll_l(MSVCRT.@)
943 int CDECL
_mbsnbicoll_l(const unsigned char *str1
, const unsigned char *str2
, size_t len
, _locale_t locale
)
945 pthreadmbcinfo mbcinfo
;
948 mbcinfo
= get_mbcinfo();
950 mbcinfo
= locale
->mbcinfo
;
952 if(!mbcinfo
->ismbcodepage
)
953 return _strnicoll_l((const char*)str1
, (const char*)str2
, len
, locale
);
954 return CompareStringA(mbcinfo
->mblcid
, NORM_IGNORECASE
, (const char*)str1
, len
, (const char*)str2
, len
)-CSTR_EQUAL
;
957 /*********************************************************************
958 * _mbsicoll_l(MSVCRT.@)
960 int CDECL
_mbsicoll_l(const unsigned char *str1
, const unsigned char *str2
, _locale_t locale
)
962 return _mbsnbicoll_l(str1
, str2
, -1, locale
);
965 /*********************************************************************
966 * _mbsnbicoll(MSVCRT.@)
968 int CDECL
_mbsnbicoll(const unsigned char *str1
, const unsigned char *str2
, size_t len
)
970 return _mbsnbicoll_l(str1
, str2
, len
, NULL
);
973 /*********************************************************************
974 * _mbsicoll(MSVCRT.@)
976 int CDECL
_mbsicoll(const unsigned char* str
, const unsigned char* cmp
)
978 #if _MSVCR_VER>=60 && _MSVCR_VER<=71
979 return CompareStringA(get_mbcinfo()->mblcid
, NORM_IGNORECASE
,
980 (const char*)str
, -1, (const char*)cmp
, -1)-CSTR_EQUAL
;
982 return _mbsnbicoll_l(str
, cmp
, -1, NULL
);
986 /*********************************************************************
987 * _mbsnbcoll_l(MSVCRT.@)
989 int CDECL
_mbsnbcoll_l(const unsigned char *str1
, const unsigned char *str2
, size_t len
, _locale_t locale
)
991 pthreadmbcinfo mbcinfo
;
994 mbcinfo
= get_mbcinfo();
996 mbcinfo
= locale
->mbcinfo
;
998 if(!mbcinfo
->ismbcodepage
)
999 return _strncoll_l((const char*)str1
, (const char*)str2
, len
, locale
);
1000 return CompareStringA(mbcinfo
->mblcid
, 0, (const char*)str1
, len
, (const char*)str2
, len
)-CSTR_EQUAL
;
1003 /*********************************************************************
1004 * _mbscoll_l(MSVCRT.@)
1006 int CDECL
_mbscoll_l(const unsigned char *str1
, const unsigned char *str2
, _locale_t locale
)
1008 return _mbsnbcoll_l(str1
, str2
, -1, locale
);
1011 /*********************************************************************
1012 * _mbsnbcoll(MSVCRT.@)
1014 int CDECL
_mbsnbcoll(const unsigned char *str1
, const unsigned char *str2
, size_t len
)
1016 return _mbsnbcoll_l(str1
, str2
, len
, NULL
);
1019 /*********************************************************************
1020 * _mbscoll(MSVCRT.@)
1022 int CDECL
_mbscoll(const unsigned char* str
, const unsigned char* cmp
)
1024 #if _MSVCR_VER>=60 && _MSVCR_VER<=71
1025 return CompareStringA(get_mbcinfo()->mblcid
, 0,
1026 (const char*)str
, -1, (const char*)cmp
, -1)-CSTR_EQUAL
;
1028 return _mbsnbcoll_l(str
, cmp
, -1, NULL
);
1032 /*********************************************************************
1033 * _mbsicmp(MSVCRT.@)
1035 int CDECL
_mbsicmp(const unsigned char* str
, const unsigned char* cmp
)
1037 if(get_mbcinfo()->ismbcodepage
)
1039 unsigned int strc
, cmpc
;
1042 return *cmp
? -1 : 0;
1045 strc
= _mbctolower(_mbsnextc(str
));
1046 cmpc
= _mbctolower(_mbsnextc(cmp
));
1048 return strc
< cmpc
? -1 : 1;
1049 str
+=(strc
> 255) ? 2 : 1;
1050 cmp
+=(strc
> 255) ? 2 : 1; /* equal, use same increment */
1053 return u_strcasecmp(str
, cmp
); /* ASCII CP */
1056 /*********************************************************************
1057 * _mbsncmp(MSVCRT.@)
1059 int CDECL
_mbsncmp(const unsigned char* str
, const unsigned char* cmp
, size_t len
)
1064 if(get_mbcinfo()->ismbcodepage
)
1066 unsigned int strc
, cmpc
;
1071 return *cmp
? -1 : 0;
1074 strc
= _mbsnextc(str
);
1075 cmpc
= _mbsnextc(cmp
);
1077 return strc
< cmpc
? -1 : 1;
1078 inc
=(strc
> 255) ? 2 : 1; /* Equal, use same increment */
1082 return 0; /* Matched len chars */
1084 return u_strncmp(str
, cmp
, len
); /* ASCII CP */
1087 /*********************************************************************
1088 * _mbsnbcmp(MSVCRT.@)
1090 int CDECL
_mbsnbcmp(const unsigned char* str
, const unsigned char* cmp
, size_t len
)
1094 if(get_mbcinfo()->ismbcodepage
)
1096 unsigned int strc
, cmpc
;
1101 return *cmp
? -1 : 0;
1104 if (_ismbblead(*str
))
1106 strc
=(len
>=2)?_mbsnextc(str
):0;
1114 if (_ismbblead(*cmp
))
1115 cmpc
=(len
>=2)?_mbsnextc(cmp
):0;
1119 return strc
< cmpc
? -1 : 1;
1124 return 0; /* Matched len chars */
1126 return u_strncmp(str
,cmp
,len
);
1129 /*********************************************************************
1130 * _mbsnicmp(MSVCRT.@)
1132 * Compare two multibyte strings case insensitively to 'len' characters.
1134 int CDECL
_mbsnicmp(const unsigned char* str
, const unsigned char* cmp
, size_t len
)
1136 /* FIXME: No tolower() for mb strings yet */
1137 if(get_mbcinfo()->ismbcodepage
)
1139 unsigned int strc
, cmpc
;
1143 return *cmp
? -1 : 0;
1146 strc
= _mbctolower(_mbsnextc(str
));
1147 cmpc
= _mbctolower(_mbsnextc(cmp
));
1149 return strc
< cmpc
? -1 : 1;
1150 str
+=(strc
> 255) ? 2 : 1;
1151 cmp
+=(strc
> 255) ? 2 : 1; /* Equal, use same increment */
1153 return 0; /* Matched len chars */
1155 return u_strncasecmp(str
, cmp
, len
); /* ASCII CP */
1158 /*********************************************************************
1159 * _mbsnbicmp(MSVCRT.@)
1161 int CDECL
_mbsnbicmp(const unsigned char* str
, const unsigned char* cmp
, size_t len
)
1165 if(get_mbcinfo()->ismbcodepage
)
1167 unsigned int strc
, cmpc
;
1172 return *cmp
? -1 : 0;
1175 if (_ismbblead(*str
))
1177 strc
=(len
>=2)?_mbsnextc(str
):0;
1185 if (_ismbblead(*cmp
))
1186 cmpc
=(len
>=2)?_mbsnextc(cmp
):0;
1189 strc
= _mbctolower(strc
);
1190 cmpc
= _mbctolower(cmpc
);
1192 return strc
< cmpc
? -1 : 1;
1197 return 0; /* Matched len bytes */
1199 return u_strncasecmp(str
,cmp
,len
);
1202 /*********************************************************************
1203 * _mbscat (MSVCRT.@)
1205 unsigned char * CDECL
_mbscat( unsigned char *dst
, const unsigned char *src
)
1207 strcat( (char *)dst
, (const char *)src
);
1211 /*********************************************************************
1212 * _mbscat_s_l (MSVCRT.@)
1214 int CDECL
_mbscat_s_l( unsigned char *dst
, size_t size
,
1215 const unsigned char *src
, _locale_t locale
)
1220 if(!MSVCRT_CHECK_PMT(dst
!= NULL
)) return EINVAL
;
1221 if(!MSVCRT_CHECK_PMT(src
!= NULL
)) return EINVAL
;
1223 for(i
=0; i
<size
; i
++)
1226 MSVCRT_INVALID_PMT("dst is not NULL-terminated", EINVAL
);
1227 if(size
) dst
[0] = 0;
1231 if(i
&& _ismbblead_l(dst
[i
-1], locale
)) {
1236 for(j
=0; src
[j
] && i
+j
<size
; j
++)
1239 MSVCRT_INVALID_PMT("dst buffer is too small", ERANGE
);
1244 if(j
&& _ismbblead_l(src
[j
-1], locale
)) {
1253 /*********************************************************************
1254 * _mbscat_s (MSVCRT.@)
1256 int CDECL
_mbscat_s( unsigned char *dst
, size_t size
, const unsigned char *src
)
1258 return _mbscat_s_l(dst
, size
, src
, NULL
);
1261 /*********************************************************************
1262 * _mbscpy (MSVCRT.@)
1264 unsigned char* CDECL
_mbscpy( unsigned char *dst
, const unsigned char *src
)
1266 strcpy( (char *)dst
, (const char *)src
);
1270 /*********************************************************************
1271 * _mbsstr (MSVCRT.@)
1273 unsigned char * CDECL
_mbsstr(const unsigned char *haystack
, const unsigned char *needle
)
1275 return (unsigned char *)strstr( (const char *)haystack
, (const char *)needle
);
1278 /*********************************************************************
1281 * Find a multibyte character in a multibyte string.
1283 unsigned char* CDECL
_mbschr(const unsigned char* s
, unsigned int x
)
1285 if(get_mbcinfo()->ismbcodepage
)
1292 return (unsigned char*)s
;
1295 s
+= c
> 255 ? 2 : 1;
1298 return u_strchr(s
, x
); /* ASCII CP */
1301 /*********************************************************************
1302 * _mbsrchr(MSVCRT.@)
1304 unsigned char* CDECL
_mbsrchr(const unsigned char* s
, unsigned int x
)
1306 if(get_mbcinfo()->ismbcodepage
)
1309 unsigned char* match
=NULL
;
1315 match
=(unsigned char*)s
;
1318 s
+=(c
> 255) ? 2 : 1;
1321 return u_strrchr(s
, x
);
1324 /*********************************************************************
1325 * _mbstok_s_l(MSVCRT.@)
1327 unsigned char* CDECL
_mbstok_s_l(unsigned char *str
, const unsigned char *delim
,
1328 unsigned char **ctx
, _locale_t locale
)
1330 pthreadmbcinfo mbcinfo
;
1333 if(!MSVCRT_CHECK_PMT(delim
!= NULL
)) return NULL
;
1334 if(!MSVCRT_CHECK_PMT(ctx
!= NULL
)) return NULL
;
1335 if(!MSVCRT_CHECK_PMT(str
|| *ctx
)) return NULL
;
1338 mbcinfo
= locale
->mbcinfo
;
1340 mbcinfo
= get_mbcinfo();
1342 if(!mbcinfo
->ismbcodepage
)
1343 return (unsigned char*)strtok_s((char*)str
, (const char*)delim
, (char**)ctx
);
1348 while((c
=_mbsnextc(str
)) && _mbschr(delim
, c
))
1349 str
+= c
>255 ? 2 : 1;
1356 *ctx
= str
+ (c
>255 ? 2 : 1);
1357 while((c
=_mbsnextc(*ctx
)) && !_mbschr(delim
, c
))
1358 *ctx
+= c
>255 ? 2 : 1;
1369 /*********************************************************************
1370 * _mbstok_s(MSVCRT.@)
1372 unsigned char* CDECL
_mbstok_s(unsigned char *str
,
1373 const unsigned char *delim
, unsigned char **ctx
)
1375 return _mbstok_s_l(str
, delim
, ctx
, NULL
);
1378 /*********************************************************************
1379 * _mbstok_l(MSVCRT.@)
1381 unsigned char* CDECL
_mbstok_l(unsigned char *str
,
1382 const unsigned char *delim
, _locale_t locale
)
1384 return _mbstok_s_l(str
, delim
, &msvcrt_get_thread_data()->mbstok_next
, locale
);
1387 /*********************************************************************
1390 unsigned char* CDECL
_mbstok(unsigned char *str
, const unsigned char *delim
)
1392 thread_data_t
*data
= msvcrt_get_thread_data();
1395 if(!str
&& !data
->mbstok_next
)
1399 return _mbstok_s_l(str
, delim
, &data
->mbstok_next
, NULL
);
1402 /*********************************************************************
1403 * _mbbtombc(MSVCRT.@)
1405 unsigned int CDECL
_mbbtombc(unsigned int c
)
1407 if(get_mbcinfo()->mbcodepage
== 932)
1409 if(c
>= 0x20 && c
<= 0x7e) {
1410 if((c
>= 0x41 && c
<= 0x5a) || (c
>= 0x61 && c
<= 0x7a) || (c
>= 0x30 && c
<= 0x39))
1411 return mbbtombc_932
[c
- 0x20] | 0x8200;
1413 return mbbtombc_932
[c
- 0x20] | 0x8100;
1415 else if(c
>= 0xa1 && c
<= 0xdf) {
1416 if(c
>= 0xa6 && c
<= 0xdd && c
!= 0xb0)
1417 return mbbtombc_932
[c
- 0xa1 + 0x5f] | 0x8300;
1419 return mbbtombc_932
[c
- 0xa1 + 0x5f] | 0x8100;
1422 return c
; /* not Japanese or no MB char */
1425 /*********************************************************************
1426 * _ismbbkana_l(MSVCRT.@)
1428 int CDECL
_ismbbkana_l(unsigned int c
, _locale_t locale
)
1430 pthreadmbcinfo mbcinfo
;
1433 mbcinfo
= locale
->mbcinfo
;
1435 mbcinfo
= get_mbcinfo();
1437 if(mbcinfo
->mbcodepage
== 932)
1439 /* Japanese/Katakana, CP 932 */
1440 return (c
>= 0xa1 && c
<= 0xdf);
1445 /*********************************************************************
1446 * _ismbbkana(MSVCRT.@)
1448 int CDECL
_ismbbkana(unsigned int c
)
1450 return _ismbbkana_l( c
, NULL
);
1453 /*********************************************************************
1454 * _ismbcdigit_l(MSVCRT.@)
1456 int CDECL
_ismbcdigit_l(unsigned int ch
, _locale_t locale
)
1458 return _iswdigit_l( msvcrt_mbc_to_wc_l(ch
, locale
), locale
);
1461 /*********************************************************************
1462 * _ismbcdigit(MSVCRT.@)
1464 int CDECL
_ismbcdigit(unsigned int ch
)
1466 return _ismbcdigit_l( ch
, NULL
);
1469 /*********************************************************************
1470 * _ismbcgraph_l(MSVCRT.@)
1472 int CDECL
_ismbcgraph_l(unsigned int ch
, _locale_t locale
)
1474 return _iswgraph_l( msvcrt_mbc_to_wc_l(ch
, locale
), locale
);
1477 /*********************************************************************
1478 * _ismbcgraph(MSVCRT.@)
1480 int CDECL
_ismbcgraph(unsigned int ch
)
1482 return _ismbcgraph_l( ch
, NULL
);
1485 /*********************************************************************
1486 * _ismbcalpha_l (MSVCRT.@)
1488 int CDECL
_ismbcalpha_l(unsigned int ch
, _locale_t locale
)
1490 return _iswalpha_l( msvcrt_mbc_to_wc_l(ch
, locale
), locale
);
1493 /*********************************************************************
1494 * _ismbcalpha (MSVCRT.@)
1496 int CDECL
_ismbcalpha(unsigned int ch
)
1498 return _ismbcalpha_l( ch
, NULL
);
1501 /*********************************************************************
1502 * _ismbclower_l (MSVCRT.@)
1504 int CDECL
_ismbclower_l(unsigned int ch
, _locale_t locale
)
1506 return _iswlower_l( msvcrt_mbc_to_wc_l(ch
, locale
), locale
);
1509 /*********************************************************************
1510 * _ismbclower (MSVCRT.@)
1512 int CDECL
_ismbclower(unsigned int ch
)
1514 return _ismbclower_l( ch
, NULL
);
1517 /*********************************************************************
1518 * _ismbcupper_l (MSVCRT.@)
1520 int CDECL
_ismbcupper_l(unsigned int ch
, _locale_t locale
)
1522 return _iswupper_l( msvcrt_mbc_to_wc_l(ch
, locale
), locale
);
1525 /*********************************************************************
1526 * _ismbcupper (MSVCRT.@)
1528 int CDECL
_ismbcupper(unsigned int ch
)
1530 return _ismbcupper_l( ch
, NULL
);
1533 /*********************************************************************
1534 * _ismbcsymbol(MSVCRT.@)
1536 int CDECL
_ismbcsymbol(unsigned int ch
)
1538 wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1540 if (!GetStringTypeW(CT_CTYPE3
, &wch
, 1, &ctype
))
1542 WARN("GetStringTypeW failed on %x\n", ch
);
1545 return ((ctype
& C3_SYMBOL
) != 0);
1548 /*********************************************************************
1549 * _ismbcalnum_l (MSVCRT.@)
1551 int CDECL
_ismbcalnum_l(unsigned int ch
, _locale_t locale
)
1553 return _iswalnum_l( msvcrt_mbc_to_wc_l(ch
, locale
), locale
);
1556 /*********************************************************************
1557 * _ismbcalnum (MSVCRT.@)
1559 int CDECL
_ismbcalnum(unsigned int ch
)
1561 return _ismbcalnum_l( ch
, NULL
);
1564 /*********************************************************************
1565 * _ismbcspace_l (MSVCRT.@)
1567 int CDECL
_ismbcspace_l(unsigned int ch
, _locale_t locale
)
1569 return _iswspace_l( msvcrt_mbc_to_wc_l(ch
, locale
), locale
);
1572 /*********************************************************************
1573 * _ismbcspace (MSVCRT.@)
1575 int CDECL
_ismbcspace(unsigned int ch
)
1577 return _ismbcspace_l( ch
, NULL
);
1580 /*********************************************************************
1581 * _ismbcprint_l (MSVCRT.@)
1583 int CDECL
_ismbcprint_l(unsigned int ch
, _locale_t locale
)
1585 return _iswprint_l( msvcrt_mbc_to_wc_l(ch
, locale
), locale
);
1588 /*********************************************************************
1589 * _ismbcprint (MSVCRT.@)
1591 int CDECL
_ismbcprint(unsigned int ch
)
1593 return _ismbcprint_l( ch
, NULL
);
1596 /*********************************************************************
1597 * _ismbcpunct_l (MSVCRT.@)
1599 int CDECL
_ismbcpunct_l(unsigned int ch
, _locale_t locale
)
1601 return _iswpunct_l( msvcrt_mbc_to_wc_l(ch
, locale
), locale
);
1604 /*********************************************************************
1605 * _ismbcpunct(MSVCRT.@)
1607 int CDECL
_ismbcpunct(unsigned int ch
)
1609 return _ismbcpunct_l( ch
, NULL
);
1612 /*********************************************************************
1613 * _ismbchira(MSVCRT.@)
1615 int CDECL
_ismbchira(unsigned int c
)
1617 if(get_mbcinfo()->mbcodepage
== 932)
1619 /* Japanese/Hiragana, CP 932 */
1620 return (c
>= 0x829f && c
<= 0x82f1);
1625 /*********************************************************************
1626 * _ismbckata(MSVCRT.@)
1628 int CDECL
_ismbckata(unsigned int c
)
1630 if(get_mbcinfo()->mbcodepage
== 932)
1632 /* Japanese/Katakana, CP 932 */
1633 return (c
>= 0x8340 && c
<= 0x8396 && c
!= 0x837f);
1639 /*********************************************************************
1640 * _ismbblead_l(MSVCRT.@)
1642 int CDECL
_ismbblead_l(unsigned int c
, _locale_t locale
)
1644 pthreadmbcinfo mbcinfo
;
1647 mbcinfo
= get_mbcinfo();
1649 mbcinfo
= locale
->mbcinfo
;
1651 return (mbcinfo
->mbctype
[(c
&0xff) + 1] & _M1
) != 0;
1654 /*********************************************************************
1655 * _ismbblead(MSVCRT.@)
1657 int CDECL
_ismbblead(unsigned int c
)
1659 return _ismbblead_l(c
, NULL
);
1662 /*********************************************************************
1663 * _ismbbtrail_l(MSVCRT.@)
1665 int CDECL
_ismbbtrail_l(unsigned int c
, _locale_t locale
)
1667 pthreadmbcinfo mbcinfo
;
1670 mbcinfo
= get_mbcinfo();
1672 mbcinfo
= locale
->mbcinfo
;
1674 return (mbcinfo
->mbctype
[(c
&0xff) + 1] & _M2
) != 0;
1677 /*********************************************************************
1678 * _ismbbtrail(MSVCRT.@)
1680 int CDECL
_ismbbtrail(unsigned int c
)
1682 return _ismbbtrail_l(c
, NULL
);
1685 /*********************************************************************
1686 * _ismbclegal_l(MSVCRT.@)
1688 int CDECL
_ismbclegal_l(unsigned int c
, _locale_t locale
)
1690 return _ismbblead_l(HIBYTE(c
), locale
) && _ismbbtrail_l(LOBYTE(c
), locale
);
1693 /*********************************************************************
1694 * _ismbclegal(MSVCRT.@)
1696 int CDECL
_ismbclegal(unsigned int c
)
1698 return _ismbclegal_l(c
, NULL
);
1701 /*********************************************************************
1702 * _ismbslead(MSVCRT.@)
1704 int CDECL
_ismbslead(const unsigned char* start
, const unsigned char* str
)
1708 if(!get_mbcinfo()->ismbcodepage
)
1711 /* Lead bytes can also be trail bytes so we need to analyse the string
1713 while (start
<= str
)
1717 lead
= !lead
&& _ismbblead(*start
);
1721 return lead
? -1 : 0;
1724 /*********************************************************************
1725 * _ismbstrail(MSVCRT.@)
1727 int CDECL
_ismbstrail(const unsigned char* start
, const unsigned char* str
)
1729 /* Note: this function doesn't check _ismbbtrail */
1730 if ((str
> start
) && _ismbslead(start
, str
-1))
1736 /*********************************************************************
1737 * _mbbtype_l(MSVCRT.@)
1739 int CDECL
_mbbtype_l(unsigned char c
, int type
, _locale_t locale
)
1742 return _ismbbtrail_l(c
, locale
) ? _MBC_TRAIL
: _MBC_ILLEGAL
;
1744 return _ismbblead_l(c
, locale
) ? _MBC_LEAD
1745 : _isprint_l(c
, locale
) ? _MBC_SINGLE
: _MBC_ILLEGAL
;
1748 /*********************************************************************
1749 * _mbbtype(MSVCRT.@)
1751 int CDECL
_mbbtype(unsigned char c
, int type
)
1753 return _mbbtype_l(c
, type
, NULL
);
1756 /*********************************************************************
1757 * _mbsbtype (MSVCRT.@)
1759 int CDECL
_mbsbtype(const unsigned char *str
, size_t count
)
1762 const unsigned char *end
= str
+ count
;
1764 /* Lead bytes can also be trail bytes so we need to analyse the string.
1765 * Also we must return _MBC_ILLEGAL for chars past the end of the string
1767 while (str
< end
) /* Note: we skip the last byte - will check after the loop */
1770 return _MBC_ILLEGAL
;
1771 lead
= get_mbcinfo()->ismbcodepage
&& !lead
&& _ismbblead(*str
);
1776 if (_ismbbtrail(*str
))
1779 return _MBC_ILLEGAL
;
1781 if (_ismbblead(*str
))
1787 /*********************************************************************
1790 unsigned char* CDECL
_mbsset(unsigned char* str
, unsigned int c
)
1792 unsigned char* ret
= str
;
1794 if(!get_mbcinfo()->ismbcodepage
|| c
< 256)
1795 return u__strset(str
, c
); /* ASCII CP or SB char */
1797 c
&= 0xffff; /* Strip high bits */
1799 while(str
[0] && str
[1])
1805 str
[0] = '\0'; /* FIXME: OK to shorten? */
1810 /*********************************************************************
1811 * _mbsnbset(MSVCRT.@)
1813 unsigned char* CDECL
_mbsnbset(unsigned char *str
, unsigned int c
, size_t len
)
1815 unsigned char *ret
= str
;
1820 if(!get_mbcinfo()->ismbcodepage
|| c
< 256)
1821 return u__strnset(str
, c
, len
); /* ASCII CP or SB char */
1823 c
&= 0xffff; /* Strip high bits */
1825 while(str
[0] && str
[1] && (len
> 1))
1833 /* as per msdn pad with a blank character */
1840 /*********************************************************************
1841 * _mbsnset(MSVCRT.@)
1843 unsigned char* CDECL
_mbsnset(unsigned char* str
, unsigned int c
, size_t len
)
1845 unsigned char *ret
= str
;
1850 if(!get_mbcinfo()->ismbcodepage
|| c
< 256)
1851 return u__strnset(str
, c
, len
); /* ASCII CP or SB char */
1853 c
&= 0xffff; /* Strip high bits */
1855 while(str
[0] && str
[1] && len
--)
1861 str
[0] = '\0'; /* FIXME: OK to shorten? */
1866 /*********************************************************************
1867 * _mbsnccnt(MSVCRT.@)
1868 * 'c' is for 'character'.
1870 size_t CDECL
_mbsnccnt(const unsigned char* str
, size_t len
)
1873 if(get_mbcinfo()->ismbcodepage
)
1876 while(*str
&& len
-- > 0)
1878 if(_ismbblead(*str
))
1891 return min(ret
, len
); /* ASCII CP */
1894 /*********************************************************************
1895 * _mbsnbcnt(MSVCRT.@)
1896 * 'b' is for byte count.
1898 size_t CDECL
_mbsnbcnt(const unsigned char* str
, size_t len
)
1901 if(get_mbcinfo()->ismbcodepage
)
1903 const unsigned char* xstr
= str
;
1904 while(*xstr
&& len
-- > 0)
1906 if (_ismbblead(*xstr
++))
1912 return min(ret
, len
); /* ASCII CP */
1915 /*********************************************************************
1916 * _mbsnbcat(MSVCRT.@)
1918 unsigned char* CDECL
_mbsnbcat(unsigned char* dst
, const unsigned char* src
, size_t len
)
1920 if(get_mbcinfo()->ismbcodepage
)
1922 unsigned char *res
= dst
;
1924 if (_ismbblead(*dst
++)) {
1928 /* as per msdn overwrite the lead byte in front of '\0' */
1934 while (*src
&& len
--) *dst
++ = *src
++;
1938 return u_strncat(dst
, src
, len
); /* ASCII CP */
1941 int CDECL
_mbsnbcat_s(unsigned char *dst
, size_t size
, const unsigned char *src
, size_t len
)
1943 unsigned char *ptr
= dst
;
1946 if (!dst
&& !size
&& !src
&& !len
)
1949 if (!dst
|| !size
|| !src
)
1958 /* Find the null terminator of the destination buffer. */
1959 while (size
&& *ptr
)
1969 /* If necessary, check that the character preceding the null terminator is
1970 * a lead byte and move the pointer back by one for later overwrite. */
1971 if (ptr
!= dst
&& get_mbcinfo()->ismbcodepage
&& _ismbblead(*(ptr
- 1)))
1974 for (i
= 0; *src
&& i
< len
; i
++)
1991 /*********************************************************************
1992 * _mbsncat(MSVCRT.@)
1994 unsigned char* CDECL
_mbsncat(unsigned char* dst
, const unsigned char* src
, size_t len
)
1996 if(get_mbcinfo()->ismbcodepage
)
1998 unsigned char *res
= dst
;
2001 if (_ismbblead(*dst
++))
2004 while (*src
&& len
--)
2007 if(_ismbblead(*src
++))
2013 return u_strncat(dst
, src
, len
); /* ASCII CP */
2017 /*********************************************************************
2020 unsigned char* CDECL
_mbslwr(unsigned char* s
)
2022 unsigned char *ret
= s
;
2025 if (get_mbcinfo()->ismbcodepage
)
2030 c
= _mbctolower(_mbsnextc(s
));
2031 /* Note that I assume that the size of the character is unchanged */
2040 else for ( ; *s
; s
++) *s
= _tolower_l(*s
, NULL
);
2045 /*********************************************************************
2046 * _mbslwr_s(MSVCRT.@)
2048 int CDECL
_mbslwr_s(unsigned char* s
, size_t len
)
2054 else if (!s
|| !len
)
2059 if (get_mbcinfo()->ismbcodepage
)
2062 for ( ; *s
&& len
> 0; len
--)
2064 c
= _mbctolower(_mbsnextc(s
));
2065 /* Note that I assume that the size of the character is unchanged */
2074 else for ( ; *s
&& len
> 0; s
++, len
--) *s
= _tolower_l(*s
, NULL
);
2085 /*********************************************************************
2088 unsigned char* CDECL
_mbsupr(unsigned char* s
)
2090 unsigned char *ret
= s
;
2093 if (get_mbcinfo()->ismbcodepage
)
2098 c
= _mbctoupper(_mbsnextc(s
));
2099 /* Note that I assume that the size of the character is unchanged */
2108 else for ( ; *s
; s
++) *s
= _toupper_l(*s
, NULL
);
2113 /*********************************************************************
2114 * _mbsupr_s(MSVCRT.@)
2116 int CDECL
_mbsupr_s(unsigned char* s
, size_t len
)
2122 else if (!s
|| !len
)
2127 if (get_mbcinfo()->ismbcodepage
)
2130 for ( ; *s
&& len
> 0; len
--)
2132 c
= _mbctoupper(_mbsnextc(s
));
2133 /* Note that I assume that the size of the character is unchanged */
2142 else for ( ; *s
&& len
> 0; s
++, len
--) *s
= _toupper_l(*s
, NULL
);
2152 /*********************************************************************
2153 * _mbsspn_l (MSVCRT.@)
2155 size_t CDECL
_mbsspn_l(const unsigned char* string
,
2156 const unsigned char* set
, _locale_t locale
)
2158 const unsigned char *p
, *q
;
2160 for (p
= string
; *p
; p
++)
2162 for (q
= set
; *q
; q
++)
2164 if (_ismbblead_l(*q
, locale
))
2166 /* duplicate a bug in native implementation */
2169 if (p
[0] == q
[0] && p
[1] == q
[1])
2178 if (p
[0] == q
[0]) break;
2186 /*********************************************************************
2187 * _mbsspn (MSVCRT.@)
2189 size_t CDECL
_mbsspn(const unsigned char* string
, const unsigned char* set
)
2191 return _mbsspn_l(string
, set
, NULL
);
2194 /*********************************************************************
2195 * _mbsspnp (MSVCRT.@)
2197 unsigned char* CDECL
_mbsspnp(const unsigned char* string
, const unsigned char* set
)
2199 string
+= _mbsspn( string
, set
);
2200 return *string
? (unsigned char*)string
: NULL
;
2203 /*********************************************************************
2204 * _mbscspn_l (MSVCRT.@)
2206 size_t CDECL
_mbscspn_l(const unsigned char* str
,
2207 const unsigned char* cmp
, _locale_t locale
)
2209 const unsigned char *p
, *q
;
2211 for (p
= str
; *p
; p
++)
2213 for (q
= cmp
; *q
; q
++)
2215 if (_ismbblead_l(*q
, locale
))
2217 /* duplicate a bug in native implementation */
2218 if (!q
[1]) return 0;
2220 if (p
[0] == q
[0] && p
[1] == q
[1])
2224 else if (p
[0] == q
[0])
2231 /*********************************************************************
2232 * _mbscspn (MSVCRT.@)
2234 size_t CDECL
_mbscspn(const unsigned char* str
, const unsigned char* cmp
)
2236 return _mbscspn_l(str
, cmp
, NULL
);
2239 /*********************************************************************
2240 * _mbsrev (MSVCRT.@)
2242 unsigned char* CDECL
_mbsrev(unsigned char* str
)
2244 int i
, len
= _mbslen(str
);
2245 unsigned char *p
, *temp
=malloc(len
*2);
2250 /* unpack multibyte string to temp buffer */
2252 for(i
=0; i
<len
; i
++)
2266 /* repack it in the reverse order */
2268 for(i
=len
-1; i
>=0; i
--)
2270 if(_ismbblead(temp
[i
*2]))
2286 /*********************************************************************
2287 * _mbspbrk (MSVCRT.@)
2289 unsigned char* CDECL
_mbspbrk(const unsigned char* str
, const unsigned char* accept
)
2291 const unsigned char* p
;
2295 for(p
= accept
; *p
; p
+= (_ismbblead(*p
)?2:1) )
2298 if( !_ismbblead(*p
) || ( *(p
+1) == *(str
+1) ) )
2299 return (unsigned char*)str
;
2301 str
+= (_ismbblead(*str
)?2:1);
2308 * Functions depending on locale codepage
2311 /*********************************************************************
2314 * Unlike most of the multibyte string functions this function uses
2315 * the locale codepage, not the codepage set by _setmbcp
2317 int CDECL
mblen(const char* str
, size_t size
)
2319 if (str
&& *str
&& size
)
2321 if(get_locinfo()->mb_cur_max
== 1)
2322 return 1; /* ASCII CP */
2324 return !isleadbyte((unsigned char)*str
) ? 1 : (size
>1 ? 2 : -1);
2329 /*********************************************************************
2332 size_t CDECL
mbrlen(const char *str
, size_t len
, mbstate_t *state
)
2334 mbstate_t s
= (state
? *state
: 0);
2337 if(!len
|| !str
|| !*str
)
2340 if(get_locinfo()->mb_cur_max
== 1) {
2342 }else if(!s
&& isleadbyte((unsigned char)*str
)) {
2344 s
= (unsigned char)*str
;
2361 /*********************************************************************
2362 * _mbstrlen_l(MSVCRT.@)
2364 size_t CDECL
_mbstrlen_l(const char* str
, _locale_t locale
)
2366 pthreadlocinfo locinfo
;
2369 locinfo
= get_locinfo();
2371 locinfo
= locale
->locinfo
;
2373 if(locinfo
->mb_cur_max
> 1) {
2375 len
= MultiByteToWideChar(locinfo
->lc_codepage
, MB_ERR_INVALID_CHARS
,
2387 /*********************************************************************
2388 * _mbstrlen(MSVCRT.@)
2390 size_t CDECL
_mbstrlen(const char* str
)
2392 return _mbstrlen_l(str
, NULL
);
2395 /*********************************************************************
2396 * _mbtowc_l(MSVCRT.@)
2398 int CDECL
_mbtowc_l(wchar_t *dst
, const char* str
, size_t n
, _locale_t locale
)
2400 pthreadlocinfo locinfo
;
2404 locinfo
= get_locinfo();
2406 locinfo
= locale
->locinfo
;
2416 if(!locinfo
->lc_codepage
) {
2417 if(dst
) *dst
= (unsigned char)*str
;
2420 if(n
>=2 && _isleadbyte_l((unsigned char)*str
, locale
)) {
2421 if(!MultiByteToWideChar(locinfo
->lc_codepage
, 0, str
, 2, &tmpdst
, 1))
2423 if(dst
) *dst
= tmpdst
;
2426 if(!MultiByteToWideChar(locinfo
->lc_codepage
, 0, str
, 1, &tmpdst
, 1))
2428 if(dst
) *dst
= tmpdst
;
2432 /*********************************************************************
2435 int CDECL
mbtowc(wchar_t *dst
, const char* str
, size_t n
)
2437 return _mbtowc_l(dst
, str
, n
, NULL
);
2440 /*********************************************************************
2443 wint_t CDECL
btowc(int c
)
2445 unsigned char letter
= c
;
2450 if(!get_locinfo()->lc_codepage
)
2452 if(!MultiByteToWideChar(get_locinfo()->lc_codepage
,
2453 MB_ERR_INVALID_CHARS
, (LPCSTR
)&letter
, 1, &ret
, 1))
2459 /*********************************************************************
2462 size_t CDECL
mbrtowc(wchar_t *dst
, const char *str
,
2463 size_t n
, mbstate_t *state
)
2465 pthreadlocinfo locinfo
= get_locinfo();
2466 mbstate_t s
= (state
? *state
: 0);
2473 if(!n
|| !str
|| !*str
)
2476 if(locinfo
->mb_cur_max
== 1) {
2477 tmpstr
[len
++] = *str
;
2478 }else if(!s
&& isleadbyte((unsigned char)*str
)) {
2480 s
= (unsigned char)*str
;
2488 tmpstr
[len
++] = *str
;
2497 if(!MultiByteToWideChar(locinfo
->lc_codepage
, 0, tmpstr
, len
, dst
, dst
? 1 : 0))
2506 /*********************************************************************
2507 * _mbstowcs_l(MSVCRT.@)
2509 size_t CDECL
_mbstowcs_l(wchar_t *wcstr
, const char *mbstr
,
2510 size_t count
, _locale_t locale
)
2512 pthreadlocinfo locinfo
;
2521 locinfo
= get_locinfo();
2523 locinfo
= locale
->locinfo
;
2525 if(!locinfo
->lc_codepage
) {
2527 return strlen(mbstr
);
2529 for(i
=0; i
<count
; i
++) {
2530 wcstr
[i
] = (unsigned char)mbstr
[i
];
2531 if(!wcstr
[i
]) break;
2536 /* Ignore count parameter */
2538 size
= MultiByteToWideChar(locinfo
->lc_codepage
,
2539 MB_ERR_INVALID_CHARS
, mbstr
, -1, NULL
, 0);
2547 for(i
=0, size
=0; i
<count
; i
++) {
2548 if(mbstr
[size
] == '\0')
2551 size
+= (_isleadbyte_l((unsigned char)mbstr
[size
], locale
) ? 2 : 1);
2555 size
= MultiByteToWideChar(locinfo
->lc_codepage
,
2556 MB_ERR_INVALID_CHARS
, mbstr
, size
, wcstr
, count
);
2558 if(count
) wcstr
[0] = '\0';
2570 /*********************************************************************
2571 * mbstowcs(MSVCRT.@)
2573 size_t CDECL
mbstowcs(wchar_t *wcstr
,
2574 const char *mbstr
, size_t count
)
2576 return _mbstowcs_l(wcstr
, mbstr
, count
, NULL
);
2579 /*********************************************************************
2580 * _mbstowcs_s_l(MSVCRT.@)
2582 int CDECL
_mbstowcs_s_l(size_t *ret
, wchar_t *wcstr
,
2583 size_t size
, const char *mbstr
, size_t count
, _locale_t locale
)
2588 if(!wcstr
&& !size
) {
2589 conv
= _mbstowcs_l(NULL
, mbstr
, 0, locale
);
2595 if (!MSVCRT_CHECK_PMT(wcstr
!= NULL
)) return EINVAL
;
2596 if (!MSVCRT_CHECK_PMT(mbstr
!= NULL
)) {
2597 if(size
) wcstr
[0] = '\0';
2601 if(count
==_TRUNCATE
|| size
<count
)
2606 conv
= _mbstowcs_l(wcstr
, mbstr
, conv
, locale
);
2608 wcstr
[conv
++] = '\0';
2609 else if(conv
==size
&& count
==_TRUNCATE
&& wcstr
[conv
-1]!='\0') {
2610 wcstr
[conv
-1] = '\0';
2612 }else if(conv
==size
&& wcstr
[conv
-1]!='\0') {
2613 MSVCRT_INVALID_PMT("wcstr[size] is too small", ERANGE
);
2624 /*********************************************************************
2625 * mbstowcs_s(MSVCRT.@)
2627 int CDECL
_mbstowcs_s(size_t *ret
, wchar_t *wcstr
,
2628 size_t size
, const char *mbstr
, size_t count
)
2630 return _mbstowcs_s_l(ret
, wcstr
, size
, mbstr
, count
, NULL
);
2633 /*********************************************************************
2634 * mbsrtowcs(MSVCRT.@)
2636 size_t CDECL
mbsrtowcs(wchar_t *wcstr
,
2637 const char **pmbstr
, size_t count
, mbstate_t *state
)
2639 mbstate_t s
= (state
? *state
: 0);
2644 if(!MSVCRT_CHECK_PMT(pmbstr
!= NULL
))
2648 while(!wcstr
|| count
>ret
) {
2649 int ch_len
= mbrtowc(&tmpdst
, p
, 2, &s
);
2651 wcstr
[ret
] = tmpdst
;
2655 }else if(ch_len
== 0) {
2656 if(wcstr
) *pmbstr
= NULL
;
2664 if(wcstr
) *pmbstr
= p
;
2668 /*********************************************************************
2669 * mbsrtowcs_s(MSVCRT.@)
2671 int CDECL
mbsrtowcs_s(size_t *ret
, wchar_t *wcstr
, size_t len
,
2672 const char **mbstr
, size_t count
, mbstate_t *state
)
2676 if(!ret
) ret
= &tmp
;
2677 if(!MSVCRT_CHECK_PMT(!!wcstr
== !!len
)) {
2682 *ret
= mbsrtowcs(wcstr
, mbstr
, count
>len
? len
: count
, state
);
2684 if(wcstr
) *wcstr
= 0;
2689 /* no place for terminating '\0' */
2690 if(wcstr
) *wcstr
= 0;
2693 if(wcstr
) wcstr
[(*ret
)-1] = 0;
2697 /*********************************************************************
2698 * _mbctohira (MSVCRT.@)
2700 * Converts a sjis katakana character to hiragana.
2702 unsigned int CDECL
_mbctohira(unsigned int c
)
2704 if(_ismbckata(c
) && c
<= 0x8393)
2705 return (c
- 0x8340 - (c
>= 0x837f ? 1 : 0)) + 0x829f;
2709 /*********************************************************************
2710 * _mbctokata (MSVCRT.@)
2712 * Converts a sjis hiragana character to katakana.
2714 unsigned int CDECL
_mbctokata(unsigned int c
)
2717 return (c
- 0x829f) + 0x8340 + (c
>= 0x82de ? 1 : 0);
2721 /*********************************************************************
2722 * _ismbcl0_l (MSVCRT.@)
2724 int CDECL
_ismbcl0_l(unsigned int c
, _locale_t locale
)
2726 pthreadmbcinfo mbcinfo
;
2729 mbcinfo
= get_mbcinfo();
2731 mbcinfo
= locale
->mbcinfo
;
2733 if(mbcinfo
->mbcodepage
== 932)
2736 return _ismbclegal_l(c
, locale
) && c
>= 0x8140 && c
<= 0x889e;
2742 /*********************************************************************
2743 * _ismbcl0 (MSVCRT.@)
2745 int CDECL
_ismbcl0(unsigned int c
)
2747 return _ismbcl0_l(c
, NULL
);
2750 /*********************************************************************
2751 * _ismbcl1_l (MSVCRT.@)
2753 int CDECL
_ismbcl1_l(unsigned int c
, _locale_t locale
)
2755 pthreadmbcinfo mbcinfo
;
2758 mbcinfo
= get_mbcinfo();
2760 mbcinfo
= locale
->mbcinfo
;
2762 if(mbcinfo
->mbcodepage
== 932)
2765 return _ismbclegal_l(c
, locale
) && c
>= 0x889f && c
<= 0x9872;
2771 /*********************************************************************
2772 * _ismbcl1 (MSVCRT.@)
2774 int CDECL
_ismbcl1(unsigned int c
)
2776 return _ismbcl1_l(c
, NULL
);
2779 /*********************************************************************
2780 * _ismbcl2_l (MSVCRT.@)
2782 int CDECL
_ismbcl2_l(unsigned int c
, _locale_t locale
)
2784 pthreadmbcinfo mbcinfo
;
2787 mbcinfo
= get_mbcinfo();
2789 mbcinfo
= locale
->mbcinfo
;
2791 if(mbcinfo
->mbcodepage
== 932)
2794 return _ismbclegal_l(c
, locale
) && c
>= 0x989f && c
<= 0xeaa4;
2800 /*********************************************************************
2801 * _ismbcl2 (MSVCRT.@)
2803 int CDECL
_ismbcl2(unsigned int c
)
2805 return _ismbcl2_l(c
, NULL
);