2 * msvcrt.dll mbcs functions
4 * Copyright 1999 Alexandre Julliard
5 * Copyright 2000 Jon Griffths
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 * Not currently binary compatible with win32. MSVCRT_mbctype must be
23 * populated correctly and the ismb* functions should reference it.
29 #include "wine/unicode.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt
);
34 unsigned char MSVCRT_mbctype
[257] = { 0 };
36 /* It seems that the data about valid trail bytes is not available from kernel32
37 * so we have to store is here. The format is the same as for lead bytes in CPINFO */
38 struct cp_extra_info_t
41 BYTE TrailBytes
[MAX_LEADBYTES
];
44 static struct cp_extra_info_t g_cpextrainfo
[] =
46 {932, {0x40, 0x7e, 0x80, 0xfc, 0, 0}},
47 {936, {0x40, 0xfe, 0, 0}},
48 {949, {0x41, 0xfe, 0, 0}},
49 {950, {0x40, 0x7e, 0xa1, 0xfe, 0, 0}},
50 {1361, {0x31, 0x7e, 0x81, 0xfe, 0, 0}},
51 {20932, {1, 255, 0, 0}}, /* seems to give different results on different systems */
52 {0, {1, 255, 0, 0}} /* match all with FIXME */
55 /* Maps cp932 single byte character to multi byte character */
56 static const unsigned char mbbtombc_932
[] = {
57 0x40,0x49,0x68,0x94,0x90,0x93,0x95,0x66,0x69,0x6a,0x96,0x7b,0x43,0x7c,0x44,0x5e,
58 0x4f,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x46,0x47,0x83,0x81,0x84,0x48,
59 0x97,0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,
60 0x6f,0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x6d,0x8f,0x6e,0x4f,0x76,
61 0x77,0x78,0x79,0x6d,0x8f,0x6e,0x4f,0x51,0x65,0x81,0x82,0x83,0x84,0x85,0x86,0x87,
62 0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f,0x90,0x91,0x92,0x93,0x94,0x95,0x50,
63 0x42,0x75,0x76,0x41,0x45,0x92,0x40,0x42,0x44,0x46,0x48,0x83,0x85,0x87,0x62,
64 0x5b,0x41,0x43,0x45,0x47,0x49,0x4a,0x4c,0x4e,0x50,0x52,0x54,0x56,0x58,0x5a,0x5c,
65 0x5e,0x60,0x63,0x65,0x67,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x71,0x74,0x77,0x7a,0x7d,
66 0x7e,0x80,0x81,0x82,0x84,0x86,0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8f,0x93,0x4a,0x4b };
68 /* Maps multibyte cp932 punctuation marks to single byte equivalents */
69 static const unsigned char mbctombb_932_punct
[] = {
70 0x20,0xa4,0xa1,0x2c,0x2e,0xa5,0x3a,0x3b,0x3f,0x21,0xde,0xdf,0x00,0x00,0x00,0x5e,
71 0x7e,0x5f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xb0,0x00,0x00,0x2f,0x00,
72 0x00,0x00,0x7c,0x00,0x00,0x60,0x27,0x00,0x22,0x28,0x29,0x00,0x00,0x5b,0x5d,0x7b,
73 0x7d,0x00,0x00,0x00,0x00,0xa2,0xa3,0x00,0x00,0x00,0x00,0x2b,0x2d,0x00,0x00,0x00,
74 0x00,0x3d,0x00,0x3c,0x3e,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x5c,
75 0x24,0x00,0x00,0x25,0x23,0x26,0x2a,0x40};
77 /* Maps multibyte cp932 hiragana/katakana to single-byte equivalents */
78 static const unsigned char mbctombb_932_kana
[] = {
79 0xa7,0xb1,0xa8,0xb2,0xa9,0xb3,0xaa,0xb4,0xab,0xb5,0xb6,0xb6,0xb7,0xb7,0xb8,0xb8,
80 0xb9,0xb9,0xba,0xba,0xbb,0xbb,0xbc,0xbc,0xbd,0xbd,0xbe,0xbe,0xbf,0xbf,0xc0,0xc0,
81 0xc1,0xc1,0xaf,0xc2,0xc2,0xc3,0xc3,0xc4,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xca,
82 0xca,0xcb,0xcb,0xcb,0xcc,0xcc,0xcc,0xcd,0xcd,0xcd,0xce,0xce,0xce,0xcf,0xd0,0xd1,
83 0xd2,0xd3,0xac,0xd4,0xad,0xd5,0xae,0xd6,0xd7,0xd8,0xd9,0xda,0xdb,0xdc,0xdc,0xb2,
84 0xb4,0xa6,0xdd,0xb3,0xb6,0xb9};
86 static MSVCRT_wchar_t
msvcrt_mbc_to_wc(unsigned int ch
)
96 mbch
[0] = (ch
>> 8) & 0xff;
100 if (!MultiByteToWideChar(get_mbcinfo()->mbcodepage
, 0, mbch
, n_chars
, &chW
, 1))
102 WARN("MultiByteToWideChar failed on %x\n", ch
);
108 static inline MSVCRT_size_t
u_strlen( const unsigned char *str
)
110 return strlen( (const char*) str
);
113 static inline unsigned char* u_strncat( unsigned char* dst
, const unsigned char* src
, MSVCRT_size_t len
)
115 return (unsigned char*)strncat( (char*)dst
, (const char*)src
, len
);
118 static inline int u_strcmp( const unsigned char *s1
, const unsigned char *s2
)
120 return strcmp( (const char*)s1
, (const char*)s2
);
123 static inline int u_strcasecmp( const unsigned char *s1
, const unsigned char *s2
)
125 return strcasecmp( (const char*)s1
, (const char*)s2
);
128 static inline int u_strncmp( const unsigned char *s1
, const unsigned char *s2
, MSVCRT_size_t len
)
130 return strncmp( (const char*)s1
, (const char*)s2
, len
);
133 static inline int u_strncasecmp( const unsigned char *s1
, const unsigned char *s2
, MSVCRT_size_t len
)
135 return strncasecmp( (const char*)s1
, (const char*)s2
, len
);
138 static inline unsigned char *u_strchr( const unsigned char *s
, unsigned char x
)
140 return (unsigned char*) strchr( (const char*)s
, x
);
143 static inline unsigned char *u_strrchr( const unsigned char *s
, unsigned char x
)
145 return (unsigned char*) strrchr( (const char*)s
, x
);
148 static inline unsigned char *u_strtok( unsigned char *s
, const unsigned char *delim
)
150 return (unsigned char*) strtok( (char*)s
, (const char*)delim
);
153 static inline unsigned char *u__strset( unsigned char *s
, unsigned char c
)
155 return (unsigned char*) _strset( (char*)s
, c
);
158 static inline unsigned char *u__strnset( unsigned char *s
, unsigned char c
, MSVCRT_size_t len
)
160 return (unsigned char*) MSVCRT__strnset( (char*)s
, c
, len
);
163 static inline MSVCRT_size_t
u_strcspn( const unsigned char *s
, const unsigned char *rej
)
165 return strcspn( (const char *)s
, (const char*)rej
);
168 /*********************************************************************
169 * __p__mbctype (MSVCRT.@)
171 unsigned char* CDECL
__p__mbctype(void)
173 return get_mbcinfo()->mbctype
;
176 /*********************************************************************
177 * ___mb_cur_max_func(MSVCRT.@)
179 int* CDECL
MSVCRT____mb_cur_max_func(void)
181 return &get_locinfo()->mb_cur_max
;
184 /* ___mb_cur_max_l_func - not exported in native msvcrt */
185 int* CDECL
___mb_cur_max_l_func(MSVCRT__locale_t locale
)
187 MSVCRT_pthreadlocinfo locinfo
;
190 locinfo
= get_locinfo();
192 locinfo
= locale
->locinfo
;
194 return &locinfo
->mb_cur_max
;
197 /*********************************************************************
198 * INTERNAL: _setmbcp_l
200 int _setmbcp_l(int cp
, LCID lcid
, MSVCRT_pthreadmbcinfo mbcinfo
)
202 const char format
[] = ".%d";
215 mbcinfo
= get_mbcinfo();
226 newcp
= get_locinfo()->lc_codepage
;
229 /* fall through (C locale) */
231 newcp
= 20127; /* ASCII */
239 sprintf(bufA
, format
, newcp
);
240 mbcinfo
->mblcid
= MSVCRT_locale_to_LCID(bufA
);
242 mbcinfo
->mblcid
= lcid
;
245 if(mbcinfo
->mblcid
== -1)
247 WARN("Can't assign LCID to codepage (%d)\n", mbcinfo
->mblcid
);
251 if (!GetCPInfo(newcp
, &cpi
))
253 WARN("Codepage %d not found\n", newcp
);
254 *MSVCRT__errno() = MSVCRT_EINVAL
;
258 /* setup the _mbctype */
259 memset(mbcinfo
->mbctype
, 0, sizeof(unsigned char[257]));
260 memset(mbcinfo
->mbcasemap
, 0, sizeof(unsigned char[256]));
262 bytes
= cpi
.LeadByte
;
263 while (bytes
[0] || bytes
[1])
265 for (i
= bytes
[0]; i
<= bytes
[1]; i
++)
266 mbcinfo
->mbctype
[i
+ 1] |= _M1
;
270 if (cpi
.MaxCharSize
> 1)
272 /* trail bytes not available through kernel32 but stored in a structure in msvcrt */
273 struct cp_extra_info_t
*cpextra
= g_cpextrainfo
;
275 mbcinfo
->ismbcodepage
= 1;
278 if (cpextra
->cp
== 0 || cpextra
->cp
== newcp
)
280 if (cpextra
->cp
== 0)
281 FIXME("trail bytes data not available for DBCS codepage %d - assuming all bytes\n", newcp
);
283 bytes
= cpextra
->TrailBytes
;
284 while (bytes
[0] || bytes
[1])
286 for (i
= bytes
[0]; i
<= bytes
[1]; i
++)
287 mbcinfo
->mbctype
[i
+ 1] |= _M2
;
296 mbcinfo
->ismbcodepage
= 0;
298 /* we can't use GetStringTypeA directly because we don't have a locale - only a code page
301 for (i
= 0; i
< 256; i
++)
302 if (!(mbcinfo
->mbctype
[i
+ 1] & _M1
))
303 bufA
[charcount
++] = i
;
305 ret
= MultiByteToWideChar(newcp
, 0, bufA
, charcount
, bufW
, charcount
);
306 if (ret
!= charcount
)
307 ERR("MultiByteToWideChar of chars failed for cp %d, ret=%d (exp %d), error=%d\n", newcp
, ret
, charcount
, GetLastError());
309 GetStringTypeW(CT_CTYPE1
, bufW
, charcount
, chartypes
);
312 for (i
= 0; i
< 256; i
++)
313 if (!(mbcinfo
->mbctype
[i
+ 1] & _M1
))
315 if (chartypes
[charcount
] & C1_UPPER
)
317 mbcinfo
->mbctype
[i
+ 1] |= _SBUP
;
318 bufW
[charcount
] = tolowerW(bufW
[charcount
]);
320 else if (chartypes
[charcount
] & C1_LOWER
)
322 mbcinfo
->mbctype
[i
+ 1] |= _SBLOW
;
323 bufW
[charcount
] = toupperW(bufW
[charcount
]);
328 ret
= WideCharToMultiByte(newcp
, 0, bufW
, charcount
, bufA
, charcount
, NULL
, NULL
);
329 if (ret
!= charcount
)
330 ERR("WideCharToMultiByte failed for cp %d, ret=%d (exp %d), error=%d\n", newcp
, ret
, charcount
, GetLastError());
333 for (i
= 0; i
< 256; i
++)
335 if(!(mbcinfo
->mbctype
[i
+ 1] & _M1
))
337 if(mbcinfo
->mbctype
[i
] & (C1_UPPER
|C1_LOWER
))
338 mbcinfo
->mbcasemap
[i
] = bufA
[charcount
];
343 if (newcp
== 932) /* CP932 only - set _MP and _MS */
345 /* On Windows it's possible to calculate the _MP and _MS from CT_CTYPE1
346 * and CT_CTYPE3. But as of Wine 0.9.43 we return wrong values what makes
347 * it hard. As this is set only for codepage 932 we hardcode it what gives
348 * also faster execution.
350 for (i
= 161; i
<= 165; i
++)
351 mbcinfo
->mbctype
[i
+ 1] |= _MP
;
352 for (i
= 166; i
<= 223; i
++)
353 mbcinfo
->mbctype
[i
+ 1] |= _MS
;
356 mbcinfo
->mbcodepage
= newcp
;
357 if(MSVCRT_locale
&& mbcinfo
== MSVCRT_locale
->mbcinfo
)
358 memcpy(MSVCRT_mbctype
, MSVCRT_locale
->mbcinfo
->mbctype
, sizeof(MSVCRT_mbctype
));
363 /*********************************************************************
364 * _setmbcp (MSVCRT.@)
366 int CDECL
_setmbcp(int cp
)
368 return _setmbcp_l(cp
, -1, NULL
);
371 /*********************************************************************
372 * _getmbcp (MSVCRT.@)
374 int CDECL
_getmbcp(void)
376 return get_mbcinfo()->mbcodepage
;
379 /*********************************************************************
380 * _mbsnextc(MSVCRT.@)
382 unsigned int CDECL
_mbsnextc(const unsigned char* str
)
385 return *str
<< 8 | str
[1];
389 /*********************************************************************
390 * _mbctolower(MSVCRT.@)
392 unsigned int CDECL
_mbctolower(unsigned int c
)
396 FIXME("Handle MBC chars\n");
399 return tolower(c
); /* ASCII CP or SB char */
402 /*********************************************************************
403 * _mbctoupper(MSVCRT.@)
405 unsigned int CDECL
_mbctoupper(unsigned int c
)
409 FIXME("Handle MBC chars\n");
412 return toupper(c
); /* ASCII CP or SB char */
415 /*********************************************************************
416 * _mbctombb (MSVCRT.@)
418 unsigned int CDECL
_mbctombb(unsigned int c
)
422 if(get_mbcinfo()->mbcodepage
== 932)
424 if(c
>= 0x829f && c
<= 0x82f1) /* Hiragana */
425 return mbctombb_932_kana
[c
- 0x829f];
426 if(c
>= 0x8340 && c
<= 0x8396 && c
!= 0x837f) /* Katakana */
427 return mbctombb_932_kana
[c
- 0x8340 - (c
>= 0x837f ? 1 : 0)];
428 if(c
>= 0x8140 && c
<= 0x8197) /* Punctuation */
430 value
= mbctombb_932_punct
[c
- 0x8140];
431 return value
? value
: c
;
433 if((c
>= 0x824f && c
<= 0x8258) || /* Fullwidth digits */
434 (c
>= 0x8260 && c
<= 0x8279)) /* Fullwidth capitals letters */
436 if(c
>= 0x8281 && c
<= 0x829a) /* Fullwidth small letters */
438 /* all other cases return c */
443 /*********************************************************************
444 * _mbcjistojms(MSVCRT.@)
446 * Converts a jis character to sjis.
447 * Based on description from
448 * http://www.slayers.ne.jp/~oouchi/code/jistosjis.html
450 unsigned int CDECL
_mbcjistojms(unsigned int c
)
452 /* Conversion takes place only when codepage is 932.
453 In all other cases, c is returned unchanged */
454 if(get_mbcinfo()->mbcodepage
== 932)
456 if(HIBYTE(c
) >= 0x21 && HIBYTE(c
) <= 0x7e &&
457 LOBYTE(c
) >= 0x21 && LOBYTE(c
) <= 0x7e)
464 if(LOBYTE(c
) >= 0x7F)
467 c
= (((HIBYTE(c
) - 0x21)/2 + 0x81) << 8) | LOBYTE(c
);
473 return 0; /* Codepage is 932, but c can't be converted */
479 /*********************************************************************
480 * _mbcjmstojis(MSVCRT.@)
482 * Converts a sjis character to jis.
484 unsigned int CDECL
_mbcjmstojis(unsigned int c
)
486 /* Conversion takes place only when codepage is 932.
487 In all other cases, c is returned unchanged */
488 if(get_mbcinfo()->mbcodepage
== 932)
490 if(_ismbclegal(c
) && HIBYTE(c
) < 0xf0)
492 if(HIBYTE(c
) >= 0xe0)
495 c
= (((HIBYTE(c
) - 0x81)*2 + 0x21) << 8) | LOBYTE(c
);
506 return 0; /* Codepage is 932, but c can't be converted */
512 /*********************************************************************
515 unsigned char* CDECL
_mbsdec(const unsigned char* start
, const unsigned char* cur
)
517 if(get_mbcinfo()->ismbcodepage
)
518 return (unsigned char *)(_ismbstrail(start
,cur
-1) ? cur
- 2 : cur
-1);
520 return (unsigned char *)cur
- 1; /* ASCII CP or SB char */
523 /*********************************************************************
526 unsigned int CDECL
_mbclen(const unsigned char* str
)
528 return _ismbblead(*str
) ? 2 : 1;
531 /*********************************************************************
534 unsigned char* CDECL
_mbsinc(const unsigned char* str
)
536 return (unsigned char *)(str
+ _mbclen(str
));
539 /*********************************************************************
542 unsigned char* CDECL
_mbsninc(const unsigned char* str
, MSVCRT_size_t num
)
547 while (num
> 0 && *str
)
549 if (_ismbblead(*str
))
559 return (unsigned char*)str
;
562 /*********************************************************************
565 MSVCRT_size_t CDECL
_mbslen(const unsigned char* str
)
567 MSVCRT_size_t len
= 0;
570 if (_ismbblead(*str
))
573 if (!*str
) /* count only full chars */
582 /*********************************************************************
585 void CDECL
_mbccpy(unsigned char* dest
, const unsigned char* src
)
589 *++dest
= *++src
; /* MB char */
592 /*********************************************************************
595 * The parameter n is the number or characters to copy, not the size of
596 * the buffer. Use _mbsnbcpy for a function analogical to strncpy
598 unsigned char* CDECL
_mbsncpy(unsigned char* dst
, const unsigned char* src
, MSVCRT_size_t n
)
600 unsigned char* ret
= dst
;
603 if (get_mbcinfo()->ismbcodepage
)
608 if (_ismbblead(*src
))
628 if (!(*dst
++ = *src
++)) break;
631 while (n
--) *dst
++ = 0;
635 /*********************************************************************
636 * _mbsnbcpy_s(MSVCRT.@)
638 * Unlike _mbsnbcpy this function does not pad the rest of the dest
641 int CDECL
_mbsnbcpy_s(unsigned char* dst
, MSVCRT_size_t size
, const unsigned char* src
, MSVCRT_size_t n
)
643 MSVCRT_size_t pos
= 0;
645 if(!dst
|| size
== 0)
646 return MSVCRT_EINVAL
;
650 return MSVCRT_EINVAL
;
655 if(get_mbcinfo()->ismbcodepage
)
663 return MSVCRT_ERANGE
;
665 is_lead
= (!is_lead
&& _ismbblead(*src
));
670 if (is_lead
) /* if string ends with a lead, remove it */
681 return MSVCRT_ERANGE
;
694 return MSVCRT_ERANGE
;
700 /*********************************************************************
701 * _mbsnbcpy(MSVCRT.@)
703 * Like strncpy this function doesn't enforce the string to be
706 unsigned char* CDECL
_mbsnbcpy(unsigned char* dst
, const unsigned char* src
, MSVCRT_size_t n
)
708 unsigned char* ret
= dst
;
711 if(get_mbcinfo()->ismbcodepage
)
716 is_lead
= (!is_lead
&& _ismbblead(*src
));
721 if (is_lead
) /* if string ends with a lead, remove it */
729 if (!(*dst
++ = *src
++)) break;
732 while (n
--) *dst
++ = 0;
736 /*********************************************************************
739 int CDECL
_mbscmp(const unsigned char* str
, const unsigned char* cmp
)
741 if(get_mbcinfo()->ismbcodepage
)
743 unsigned int strc
, cmpc
;
746 return *cmp
? -1 : 0;
749 strc
= _mbsnextc(str
);
750 cmpc
= _mbsnextc(cmp
);
752 return strc
< cmpc
? -1 : 1;
753 str
+=(strc
> 255) ? 2 : 1;
754 cmp
+=(strc
> 255) ? 2 : 1; /* equal, use same increment */
757 return u_strcmp(str
, cmp
); /* ASCII CP */
760 /*********************************************************************
761 * _mbsicoll(MSVCRT.@)
762 * FIXME: handle locales.
764 int CDECL
_mbsicoll(const unsigned char* str
, const unsigned char* cmp
)
766 if(get_mbcinfo()->ismbcodepage
)
768 unsigned int strc
, cmpc
;
771 return *cmp
? -1 : 0;
774 strc
= _mbctolower(_mbsnextc(str
));
775 cmpc
= _mbctolower(_mbsnextc(cmp
));
777 return strc
< cmpc
? -1 : 1;
778 str
+=(strc
> 255) ? 2 : 1;
779 cmp
+=(strc
> 255) ? 2 : 1; /* equal, use same increment */
782 return u_strcasecmp(str
, cmp
); /* ASCII CP */
785 /*********************************************************************
787 * Performs a case-sensitive comparison according to the current code page
789 * _NLSCMPERROR if error
790 * FIXME: handle locales.
792 int CDECL
_mbscoll(const unsigned char* str
, const unsigned char* cmp
)
794 if(get_mbcinfo()->ismbcodepage
)
796 unsigned int strc
, cmpc
;
799 return *cmp
? -1 : 0;
802 strc
= _mbsnextc(str
);
803 cmpc
= _mbsnextc(cmp
);
805 return strc
< cmpc
? -1 : 1;
806 str
+=(strc
> 255) ? 2 : 1;
807 cmp
+=(strc
> 255) ? 2 : 1; /* equal, use same increment */
810 return u_strcmp(str
, cmp
); /* ASCII CP */
814 /*********************************************************************
817 int CDECL
_mbsicmp(const unsigned char* str
, const unsigned char* cmp
)
819 if(get_mbcinfo()->ismbcodepage
)
821 unsigned int strc
, cmpc
;
824 return *cmp
? -1 : 0;
827 strc
= _mbctolower(_mbsnextc(str
));
828 cmpc
= _mbctolower(_mbsnextc(cmp
));
830 return strc
< cmpc
? -1 : 1;
831 str
+=(strc
> 255) ? 2 : 1;
832 cmp
+=(strc
> 255) ? 2 : 1; /* equal, use same increment */
835 return u_strcasecmp(str
, cmp
); /* ASCII CP */
838 /*********************************************************************
841 int CDECL
_mbsncmp(const unsigned char* str
, const unsigned char* cmp
, MSVCRT_size_t len
)
846 if(get_mbcinfo()->ismbcodepage
)
848 unsigned int strc
, cmpc
;
853 return *cmp
? -1 : 0;
856 strc
= _mbsnextc(str
);
857 cmpc
= _mbsnextc(cmp
);
859 return strc
< cmpc
? -1 : 1;
860 inc
=(strc
> 255) ? 2 : 1; /* Equal, use same increment */
864 return 0; /* Matched len chars */
866 return u_strncmp(str
, cmp
, len
); /* ASCII CP */
869 /*********************************************************************
870 * _mbsnbcmp(MSVCRT.@)
872 int CDECL
_mbsnbcmp(const unsigned char* str
, const unsigned char* cmp
, MSVCRT_size_t len
)
876 if(get_mbcinfo()->ismbcodepage
)
878 unsigned int strc
, cmpc
;
883 return *cmp
? -1 : 0;
886 if (_ismbblead(*str
))
888 strc
=(len
>=2)?_mbsnextc(str
):0;
896 if (_ismbblead(*cmp
))
897 cmpc
=(len
>=2)?_mbsnextc(cmp
):0;
901 return strc
< cmpc
? -1 : 1;
906 return 0; /* Matched len chars */
908 return u_strncmp(str
,cmp
,len
);
911 /*********************************************************************
912 * _mbsnicmp(MSVCRT.@)
914 * Compare two multibyte strings case insensitively to 'len' characters.
916 int CDECL
_mbsnicmp(const unsigned char* str
, const unsigned char* cmp
, MSVCRT_size_t len
)
918 /* FIXME: No tolower() for mb strings yet */
919 if(get_mbcinfo()->ismbcodepage
)
921 unsigned int strc
, cmpc
;
925 return *cmp
? -1 : 0;
928 strc
= _mbctolower(_mbsnextc(str
));
929 cmpc
= _mbctolower(_mbsnextc(cmp
));
931 return strc
< cmpc
? -1 : 1;
932 str
+=(strc
> 255) ? 2 : 1;
933 cmp
+=(strc
> 255) ? 2 : 1; /* Equal, use same increment */
935 return 0; /* Matched len chars */
937 return u_strncasecmp(str
, cmp
, len
); /* ASCII CP */
940 /*********************************************************************
941 * _mbsnbicmp(MSVCRT.@)
943 int CDECL
_mbsnbicmp(const unsigned char* str
, const unsigned char* cmp
, MSVCRT_size_t len
)
947 if(get_mbcinfo()->ismbcodepage
)
949 unsigned int strc
, cmpc
;
954 return *cmp
? -1 : 0;
957 if (_ismbblead(*str
))
959 strc
=(len
>=2)?_mbsnextc(str
):0;
967 if (_ismbblead(*cmp
))
968 cmpc
=(len
>=2)?_mbsnextc(cmp
):0;
971 strc
= _mbctolower(strc
);
972 cmpc
= _mbctolower(cmpc
);
974 return strc
< cmpc
? -1 : 1;
979 return 0; /* Matched len bytes */
981 return u_strncasecmp(str
,cmp
,len
);
984 /*********************************************************************
987 unsigned char * CDECL
_mbscat( unsigned char *dst
, const unsigned char *src
)
989 strcat( (char *)dst
, (const char *)src
);
993 /*********************************************************************
996 unsigned char* CDECL
_mbscpy( unsigned char *dst
, const unsigned char *src
)
998 strcpy( (char *)dst
, (const char *)src
);
1002 /*********************************************************************
1003 * _mbsstr (MSVCRT.@)
1005 unsigned char * CDECL
_mbsstr(const unsigned char *haystack
, const unsigned char *needle
)
1007 return (unsigned char *)strstr( (const char *)haystack
, (const char *)needle
);
1010 /*********************************************************************
1013 * Find a multibyte character in a multibyte string.
1015 unsigned char* CDECL
_mbschr(const unsigned char* s
, unsigned int x
)
1017 if(get_mbcinfo()->ismbcodepage
)
1024 return (unsigned char*)s
;
1027 s
+= c
> 255 ? 2 : 1;
1030 return u_strchr(s
, x
); /* ASCII CP */
1033 /*********************************************************************
1034 * _mbsrchr(MSVCRT.@)
1036 unsigned char* CDECL
_mbsrchr(const unsigned char* s
, unsigned int x
)
1038 if(get_mbcinfo()->ismbcodepage
)
1041 unsigned char* match
=NULL
;
1047 match
=(unsigned char*)s
;
1050 s
+=(c
> 255) ? 2 : 1;
1053 return u_strrchr(s
, x
);
1056 /*********************************************************************
1059 * Find and extract tokens from strings
1061 unsigned char* CDECL
_mbstok(unsigned char *str
, const unsigned char *delim
)
1063 thread_data_t
*data
= msvcrt_get_thread_data();
1066 if(get_mbcinfo()->ismbcodepage
)
1071 if (!(str
= data
->mbstok_next
)) return NULL
;
1073 while ((c
= _mbsnextc(str
)) && _mbschr(delim
, c
)) {
1074 str
+= c
> 255 ? 2 : 1;
1076 if (!*str
) return NULL
;
1078 while ((c
= _mbsnextc(str
)) && !_mbschr(delim
, c
)) {
1079 str
+= c
> 255 ? 2 : 1;
1083 if (c
> 255) *str
++ = 0;
1085 data
->mbstok_next
= str
;
1088 return u_strtok(str
, delim
); /* ASCII CP */
1091 /*********************************************************************
1092 * _mbbtombc(MSVCRT.@)
1094 unsigned int CDECL
_mbbtombc(unsigned int c
)
1096 if(get_mbcinfo()->mbcodepage
== 932)
1098 if(c
>= 0x20 && c
<= 0x7e) {
1099 if((c
>= 0x41 && c
<= 0x5a) || (c
>= 0x61 && c
<= 0x7a) || (c
>= 0x30 && c
<= 0x39))
1100 return mbbtombc_932
[c
- 0x20] | 0x8200;
1102 return mbbtombc_932
[c
- 0x20] | 0x8100;
1104 else if(c
>= 0xa1 && c
<= 0xdf) {
1105 if(c
>= 0xa6 && c
<= 0xdd && c
!= 0xb0)
1106 return mbbtombc_932
[c
- 0xa1 + 0x5f] | 0x8300;
1108 return mbbtombc_932
[c
- 0xa1 + 0x5f] | 0x8100;
1111 return c
; /* not Japanese or no MB char */
1114 /*********************************************************************
1115 * _mbbtype(MSVCRT.@)
1117 int CDECL
_mbbtype(unsigned char c
, int type
)
1121 if ((c
>= 0x20 && c
<= 0x7e) || (c
>= 0xa1 && c
<= 0xdf))
1123 else if ((c
>= 0x40 && c
<= 0x7e) || (c
>= 0x80 && c
<= 0xfc))
1126 return _MBC_ILLEGAL
;
1130 if ((c
>= 0x20 && c
<= 0x7e) || (c
>= 0xa1 && c
<= 0xdf))
1132 else if ((c
>= 0x81 && c
<= 0x9f) || (c
>= 0xe0 && c
<= 0xfc))
1135 return _MBC_ILLEGAL
;
1139 /*********************************************************************
1140 * _ismbbkana(MSVCRT.@)
1142 int CDECL
_ismbbkana(unsigned int c
)
1144 /* FIXME: use lc_ctype when supported, not lc_all */
1145 if(get_mbcinfo()->mbcodepage
== 932)
1147 /* Japanese/Katakana, CP 932 */
1148 return (c
>= 0xa1 && c
<= 0xdf);
1153 /*********************************************************************
1154 * _ismbcdigit(MSVCRT.@)
1156 int CDECL
_ismbcdigit(unsigned int ch
)
1158 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1159 return (get_char_typeW( wch
) & C1_DIGIT
);
1162 /*********************************************************************
1163 * _ismbcgraph(MSVCRT.@)
1165 int CDECL
_ismbcgraph(unsigned int ch
)
1167 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1168 return (get_char_typeW( wch
) & (C1_UPPER
| C1_LOWER
| C1_DIGIT
| C1_PUNCT
| C1_ALPHA
));
1171 /*********************************************************************
1172 * _ismbcalpha (MSVCRT.@)
1174 int CDECL
_ismbcalpha(unsigned int ch
)
1176 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1177 return (get_char_typeW( wch
) & C1_ALPHA
);
1180 /*********************************************************************
1181 * _ismbclower (MSVCRT.@)
1183 int CDECL
_ismbclower(unsigned int ch
)
1185 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1186 return (get_char_typeW( wch
) & C1_UPPER
);
1189 /*********************************************************************
1190 * _ismbcupper (MSVCRT.@)
1192 int CDECL
_ismbcupper(unsigned int ch
)
1194 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1195 return (get_char_typeW( wch
) & C1_LOWER
);
1198 /*********************************************************************
1199 * _ismbcsymbol(MSVCRT.@)
1201 int CDECL
_ismbcsymbol(unsigned int ch
)
1203 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1205 if (!GetStringTypeW(CT_CTYPE3
, &wch
, 1, &ctype
))
1207 WARN("GetStringTypeW failed on %x\n", ch
);
1210 return ((ctype
& C3_SYMBOL
) != 0);
1213 /*********************************************************************
1214 * _ismbcalnum (MSVCRT.@)
1216 int CDECL
_ismbcalnum(unsigned int ch
)
1218 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1219 return (get_char_typeW( wch
) & (C1_ALPHA
| C1_DIGIT
));
1222 /*********************************************************************
1223 * _ismbcspace (MSVCRT.@)
1225 int CDECL
_ismbcspace(unsigned int ch
)
1227 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1228 return (get_char_typeW( wch
) & C1_SPACE
);
1231 /*********************************************************************
1232 * _ismbcprint (MSVCRT.@)
1234 int CDECL
_ismbcprint(unsigned int ch
)
1236 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1237 return (get_char_typeW( wch
) & (C1_UPPER
| C1_LOWER
| C1_DIGIT
| C1_PUNCT
| C1_ALPHA
| C1_SPACE
));
1240 /*********************************************************************
1241 * _ismbcpunct(MSVCRT.@)
1243 int CDECL
_ismbcpunct(unsigned int ch
)
1245 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1246 return (get_char_typeW( wch
) & C1_PUNCT
);
1249 /*********************************************************************
1250 * _ismbchira(MSVCRT.@)
1252 int CDECL
_ismbchira(unsigned int c
)
1254 /* FIXME: use lc_ctype when supported, not lc_all */
1255 if(get_mbcinfo()->mbcodepage
== 932)
1257 /* Japanese/Hiragana, CP 932 */
1258 return (c
>= 0x829f && c
<= 0x82f1);
1263 /*********************************************************************
1264 * _ismbckata(MSVCRT.@)
1266 int CDECL
_ismbckata(unsigned int c
)
1268 /* FIXME: use lc_ctype when supported, not lc_all */
1269 if(get_mbcinfo()->mbcodepage
== 932)
1272 return _ismbbkana(c
);
1273 /* Japanese/Katakana, CP 932 */
1274 return (c
>= 0x8340 && c
<= 0x8396 && c
!= 0x837f);
1279 /*********************************************************************
1280 * _ismbblead(MSVCRT.@)
1282 int CDECL
_ismbblead(unsigned int c
)
1284 return (get_mbcinfo()->mbctype
[(c
&0xff) + 1] & _M1
) != 0;
1288 /*********************************************************************
1289 * _ismbbtrail(MSVCRT.@)
1291 int CDECL
_ismbbtrail(unsigned int c
)
1293 return (get_mbcinfo()->mbctype
[(c
&0xff) + 1] & _M2
) != 0;
1296 /*********************************************************************
1297 * _ismbclegal(MSVCRT.@)
1299 int CDECL
_ismbclegal(unsigned int c
)
1301 return _ismbblead(HIBYTE(c
)) && _ismbbtrail(LOBYTE(c
));
1304 /*********************************************************************
1305 * _ismbslead(MSVCRT.@)
1307 int CDECL
_ismbslead(const unsigned char* start
, const unsigned char* str
)
1311 if(!get_mbcinfo()->ismbcodepage
)
1314 /* Lead bytes can also be trail bytes so we need to analyse the string
1316 while (start
<= str
)
1320 lead
= !lead
&& _ismbblead(*start
);
1324 return lead
? -1 : 0;
1327 /*********************************************************************
1328 * _ismbstrail(MSVCRT.@)
1330 int CDECL
_ismbstrail(const unsigned char* start
, const unsigned char* str
)
1332 /* Note: this function doesn't check _ismbbtrail */
1333 if ((str
> start
) && _ismbslead(start
, str
-1))
1339 /*********************************************************************
1340 * _mbsbtype (MSVCRT.@)
1342 int CDECL
_mbsbtype(const unsigned char *str
, MSVCRT_size_t count
)
1345 const unsigned char *end
= str
+ count
;
1347 /* Lead bytes can also be trail bytes so we need to analyse the string.
1348 * Also we must return _MBC_ILLEGAL for chars past the end of the string
1350 while (str
< end
) /* Note: we skip the last byte - will check after the loop */
1353 return _MBC_ILLEGAL
;
1354 lead
= get_mbcinfo()->ismbcodepage
&& !lead
&& _ismbblead(*str
);
1359 if (_ismbbtrail(*str
))
1362 return _MBC_ILLEGAL
;
1364 if (_ismbblead(*str
))
1370 /*********************************************************************
1373 unsigned char* CDECL
_mbsset(unsigned char* str
, unsigned int c
)
1375 unsigned char* ret
= str
;
1377 if(!get_mbcinfo()->ismbcodepage
|| c
< 256)
1378 return u__strset(str
, c
); /* ASCII CP or SB char */
1380 c
&= 0xffff; /* Strip high bits */
1382 while(str
[0] && str
[1])
1388 str
[0] = '\0'; /* FIXME: OK to shorten? */
1393 /*********************************************************************
1394 * _mbsnbset(MSVCRT.@)
1396 unsigned char* CDECL
_mbsnbset(unsigned char *str
, unsigned int c
, MSVCRT_size_t len
)
1398 unsigned char *ret
= str
;
1403 if(!get_mbcinfo()->ismbcodepage
|| c
< 256)
1404 return u__strnset(str
, c
, len
); /* ASCII CP or SB char */
1406 c
&= 0xffff; /* Strip high bits */
1408 while(str
[0] && str
[1] && (len
> 1))
1416 /* as per msdn pad with a blank character */
1423 /*********************************************************************
1424 * _mbsnset(MSVCRT.@)
1426 unsigned char* CDECL
_mbsnset(unsigned char* str
, unsigned int c
, MSVCRT_size_t len
)
1428 unsigned char *ret
= str
;
1433 if(!get_mbcinfo()->ismbcodepage
|| c
< 256)
1434 return u__strnset(str
, c
, len
); /* ASCII CP or SB char */
1436 c
&= 0xffff; /* Strip high bits */
1438 while(str
[0] && str
[1] && len
--)
1444 str
[0] = '\0'; /* FIXME: OK to shorten? */
1449 /*********************************************************************
1450 * _mbsnccnt(MSVCRT.@)
1451 * 'c' is for 'character'.
1453 MSVCRT_size_t CDECL
_mbsnccnt(const unsigned char* str
, MSVCRT_size_t len
)
1456 if(get_mbcinfo()->ismbcodepage
)
1459 while(*str
&& len
-- > 0)
1461 if(_ismbblead(*str
))
1474 return min(ret
, len
); /* ASCII CP */
1477 /*********************************************************************
1478 * _mbsnbcnt(MSVCRT.@)
1479 * 'b' is for byte count.
1481 MSVCRT_size_t CDECL
_mbsnbcnt(const unsigned char* str
, MSVCRT_size_t len
)
1484 if(get_mbcinfo()->ismbcodepage
)
1486 const unsigned char* xstr
= str
;
1487 while(*xstr
&& len
-- > 0)
1489 if (_ismbblead(*xstr
++))
1495 return min(ret
, len
); /* ASCII CP */
1498 /*********************************************************************
1499 * _mbsnbcat(MSVCRT.@)
1501 unsigned char* CDECL
_mbsnbcat(unsigned char* dst
, const unsigned char* src
, MSVCRT_size_t len
)
1503 if(get_mbcinfo()->ismbcodepage
)
1505 unsigned char *res
= dst
;
1507 if (_ismbblead(*dst
++)) {
1511 /* as per msdn overwrite the lead byte in front of '\0' */
1517 while (*src
&& len
--) *dst
++ = *src
++;
1521 return u_strncat(dst
, src
, len
); /* ASCII CP */
1524 int CDECL
_mbsnbcat_s(unsigned char *dst
, MSVCRT_size_t size
, const unsigned char *src
, MSVCRT_size_t len
)
1526 unsigned char *ptr
= dst
;
1529 if (!dst
&& !size
&& !src
&& !len
)
1532 if (!dst
|| !size
|| !src
)
1537 *MSVCRT__errno() = MSVCRT_EINVAL
;
1538 return MSVCRT_EINVAL
;
1541 /* Find the null terminator of the destination buffer. */
1542 while (size
&& *ptr
)
1548 *MSVCRT__errno() = MSVCRT_EINVAL
;
1549 return MSVCRT_EINVAL
;
1552 /* If necessary, check that the character preceding the null terminator is
1553 * a lead byte and move the pointer back by one for later overwrite. */
1554 if (ptr
!= dst
&& get_mbcinfo()->ismbcodepage
&& _ismbblead(*(ptr
- 1)))
1557 for (i
= 0; *src
&& i
< len
; i
++)
1565 *MSVCRT__errno() = MSVCRT_ERANGE
;
1566 return MSVCRT_ERANGE
;
1574 /*********************************************************************
1575 * _mbsncat(MSVCRT.@)
1577 unsigned char* CDECL
_mbsncat(unsigned char* dst
, const unsigned char* src
, MSVCRT_size_t len
)
1579 if(get_mbcinfo()->ismbcodepage
)
1581 unsigned char *res
= dst
;
1584 if (_ismbblead(*dst
++))
1587 while (*src
&& len
--)
1590 if(_ismbblead(*src
++))
1596 return u_strncat(dst
, src
, len
); /* ASCII CP */
1600 /*********************************************************************
1603 unsigned char* CDECL
_mbslwr(unsigned char* s
)
1605 unsigned char *ret
= s
;
1608 if (get_mbcinfo()->ismbcodepage
)
1613 c
= _mbctolower(_mbsnextc(s
));
1614 /* Note that I assume that the size of the character is unchanged */
1623 else for ( ; *s
; s
++) *s
= tolower(*s
);
1628 /*********************************************************************
1629 * _mbslwr_s(MSVCRT.@)
1631 int CDECL
_mbslwr_s(unsigned char* s
, MSVCRT_size_t len
)
1637 else if (!s
|| !len
)
1639 *MSVCRT__errno() = MSVCRT_EINVAL
;
1640 return MSVCRT_EINVAL
;
1642 if (get_mbcinfo()->ismbcodepage
)
1645 for ( ; *s
&& len
> 0; len
--)
1647 c
= _mbctolower(_mbsnextc(s
));
1648 /* Note that I assume that the size of the character is unchanged */
1657 else for ( ; *s
&& len
> 0; s
++, len
--) *s
= tolower(*s
);
1661 *MSVCRT__errno() = MSVCRT_EINVAL
;
1662 return MSVCRT_EINVAL
;
1668 /*********************************************************************
1671 unsigned char* CDECL
_mbsupr(unsigned char* s
)
1673 unsigned char *ret
= s
;
1676 if (get_mbcinfo()->ismbcodepage
)
1681 c
= _mbctoupper(_mbsnextc(s
));
1682 /* Note that I assume that the size of the character is unchanged */
1691 else for ( ; *s
; s
++) *s
= toupper(*s
);
1696 /*********************************************************************
1697 * _mbsupr_s(MSVCRT.@)
1699 int CDECL
_mbsupr_s(unsigned char* s
, MSVCRT_size_t len
)
1705 else if (!s
|| !len
)
1707 *MSVCRT__errno() = MSVCRT_EINVAL
;
1708 return MSVCRT_EINVAL
;
1710 if (get_mbcinfo()->ismbcodepage
)
1713 for ( ; *s
&& len
> 0; len
--)
1715 c
= _mbctoupper(_mbsnextc(s
));
1716 /* Note that I assume that the size of the character is unchanged */
1725 else for ( ; *s
&& len
> 0; s
++, len
--) *s
= toupper(*s
);
1729 *MSVCRT__errno() = MSVCRT_EINVAL
;
1730 return MSVCRT_EINVAL
;
1736 /*********************************************************************
1737 * _mbsspn (MSVCRT.@)
1739 MSVCRT_size_t CDECL
_mbsspn(const unsigned char* string
, const unsigned char* set
)
1741 const unsigned char *p
, *q
;
1743 for (p
= string
; *p
; p
++)
1747 for (q
= set
; *q
; q
++)
1751 if ((*p
== *q
) && (p
[1] == q
[1]))
1755 if (!q
[0] || !q
[1]) break;
1759 for (q
= set
; *q
; q
++)
1768 /*********************************************************************
1769 * _mbsspnp (MSVCRT.@)
1771 unsigned char* CDECL
_mbsspnp(const unsigned char* string
, const unsigned char* set
)
1773 const unsigned char *p
, *q
;
1775 for (p
= string
; *p
; p
++)
1779 for (q
= set
; *q
; q
++)
1783 if ((*p
== *q
) && (p
[1] == q
[1]))
1787 if (!q
[0] || !q
[1]) break;
1791 for (q
= set
; *q
; q
++)
1799 return (unsigned char *)p
;
1802 /*********************************************************************
1803 * _mbscspn(MSVCRT.@)
1805 MSVCRT_size_t CDECL
_mbscspn(const unsigned char* str
, const unsigned char* cmp
)
1807 if (get_mbcinfo()->ismbcodepage
)
1808 FIXME("don't handle double character case\n");
1809 return u_strcspn(str
, cmp
);
1812 /*********************************************************************
1813 * _mbsrev (MSVCRT.@)
1815 unsigned char* CDECL
_mbsrev(unsigned char* str
)
1817 int i
, len
= _mbslen(str
);
1818 unsigned char *p
, *temp
=MSVCRT_malloc(len
*2);
1823 /* unpack multibyte string to temp buffer */
1825 for(i
=0; i
<len
; i
++)
1839 /* repack it in the reverse order */
1841 for(i
=len
-1; i
>=0; i
--)
1843 if(_ismbblead(temp
[i
*2]))
1859 /*********************************************************************
1860 * _mbspbrk (MSVCRT.@)
1862 unsigned char* CDECL
_mbspbrk(const unsigned char* str
, const unsigned char* accept
)
1864 const unsigned char* p
;
1868 for(p
= accept
; *p
; p
+= (_ismbblead(*p
)?2:1) )
1871 if( !_ismbblead(*p
) || ( *(p
+1) == *(str
+1) ) )
1872 return (unsigned char*)str
;
1874 str
+= (_ismbblead(*str
)?2:1);
1881 * Functions depending on locale codepage
1884 /*********************************************************************
1887 * Unlike most of the multibyte string functions this function uses
1888 * the locale codepage, not the codepage set by _setmbcp
1890 int CDECL
MSVCRT_mblen(const char* str
, MSVCRT_size_t size
)
1892 if (str
&& *str
&& size
)
1894 if(get_locinfo()->mb_cur_max
== 1)
1895 return 1; /* ASCII CP */
1897 return !MSVCRT_isleadbyte(*str
) ? 1 : (size
>1 ? 2 : -1);
1902 /*********************************************************************
1903 * _mbstrlen_l(MSVCRT.@)
1905 MSVCRT_size_t CDECL
_mbstrlen_l(const char* str
, MSVCRT__locale_t locale
)
1907 MSVCRT_pthreadlocinfo locinfo
;
1910 locinfo
= get_locinfo();
1912 locinfo
= locale
->locinfo
;
1914 if(locinfo
->mb_cur_max
> 1) {
1916 len
= MultiByteToWideChar(locinfo
->lc_codepage
, MB_ERR_INVALID_CHARS
,
1919 *MSVCRT__errno() = MSVCRT_EILSEQ
;
1928 /*********************************************************************
1929 * _mbstrlen(MSVCRT.@)
1931 MSVCRT_size_t CDECL
_mbstrlen(const char* str
)
1933 return _mbstrlen_l(str
, NULL
);
1936 /*********************************************************************
1937 * _mbtowc_l(MSVCRT.@)
1939 int CDECL
MSVCRT_mbtowc_l(MSVCRT_wchar_t
*dst
, const char* str
, MSVCRT_size_t n
, MSVCRT__locale_t locale
)
1941 MSVCRT_pthreadlocinfo locinfo
;
1942 MSVCRT_wchar_t tmpdst
= '\0';
1945 locinfo
= get_locinfo();
1947 locinfo
= locale
->locinfo
;
1951 if(!MultiByteToWideChar(locinfo
->lc_codepage
, 0, str
, n
, &tmpdst
, 1))
1955 /* return the number of bytes from src that have been used */
1958 if(n
>= 2 && MSVCRT__isleadbyte_l(*str
, locale
) && str
[1])
1963 /*********************************************************************
1966 int CDECL
MSVCRT_mbtowc(MSVCRT_wchar_t
*dst
, const char* str
, MSVCRT_size_t n
)
1968 return MSVCRT_mbtowc_l(dst
, str
, n
, NULL
);
1971 /*********************************************************************
1972 * _mbstowcs_l(MSVCRT.@)
1974 MSVCRT_size_t CDECL
MSVCRT__mbstowcs_l(MSVCRT_wchar_t
*wcstr
, const char *mbstr
,
1975 MSVCRT_size_t count
, MSVCRT__locale_t locale
)
1977 MSVCRT_pthreadlocinfo locinfo
;
1978 MSVCRT_size_t i
, size
;
1981 locinfo
= get_locinfo();
1983 locinfo
= locale
->locinfo
;
1985 /* Ignore count parameter */
1987 return MultiByteToWideChar(locinfo
->lc_codepage
, 0, mbstr
, -1, NULL
, 0)-1;
1989 for(i
=0, size
=0; i
<count
; i
++) {
1990 if(mbstr
[size
] == '\0')
1993 size
+= (MSVCRT__isleadbyte_l(mbstr
[size
], locale
) ? 2 : 1);
1996 size
= MultiByteToWideChar(locinfo
->lc_codepage
, 0,
1997 mbstr
, size
, wcstr
, count
);
1999 if(size
<count
&& wcstr
)
2005 /*********************************************************************
2006 * mbstowcs(MSVCRT.@)
2008 MSVCRT_size_t CDECL
MSVCRT_mbstowcs(MSVCRT_wchar_t
*wcstr
,
2009 const char *mbstr
, MSVCRT_size_t count
)
2011 return MSVCRT__mbstowcs_l(wcstr
, mbstr
, count
, NULL
);
2014 /*********************************************************************
2015 * _mbstowcs_s_l(MSVCRT.@)
2017 int CDECL
MSVCRT__mbstowcs_s_l(MSVCRT_size_t
*ret
, MSVCRT_wchar_t
*wcstr
,
2018 MSVCRT_size_t size
, const char *mbstr
,
2019 MSVCRT_size_t count
, MSVCRT__locale_t locale
)
2023 if(!wcstr
&& !size
) {
2024 conv
= MSVCRT__mbstowcs_l(NULL
, mbstr
, 0, locale
);
2030 if(!MSVCRT_CHECK_PMT(mbstr
!= NULL
) || !MSVCRT_CHECK_PMT(wcstr
!= NULL
)) {
2033 *MSVCRT__errno() = MSVCRT_EINVAL
;
2034 return MSVCRT_EINVAL
;
2037 if(count
==MSVCRT__TRUNCATE
|| size
<count
)
2042 conv
= MSVCRT__mbstowcs_l(wcstr
, mbstr
, conv
, locale
);
2044 wcstr
[conv
++] = '\0';
2045 else if(conv
==size
&& (count
==MSVCRT__TRUNCATE
|| wcstr
[conv
-1]=='\0'))
2046 wcstr
[conv
-1] = '\0';
2048 MSVCRT_INVALID_PMT("wcstr[size] is too small");
2051 *MSVCRT__errno() = MSVCRT_ERANGE
;
2052 return MSVCRT_ERANGE
;
2060 /*********************************************************************
2061 * mbstowcs_s(MSVCRT.@)
2063 int CDECL
MSVCRT__mbstowcs_s(MSVCRT_size_t
*ret
, MSVCRT_wchar_t
*wcstr
,
2064 MSVCRT_size_t size
, const char *mbstr
, MSVCRT_size_t count
)
2066 return MSVCRT__mbstowcs_s_l(ret
, wcstr
, size
, mbstr
, count
, NULL
);