2 * msvcrt.dll mbcs functions
4 * Copyright 1999 Alexandre Julliard
5 * Copyright 2000 Jon Griffths
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 * Not currently binary compatible with win32. MSVCRT_mbctype must be
23 * populated correctly and the ismb* functions should reference it.
30 #include "wine/unicode.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt
);
35 unsigned char MSVCRT_mbctype
[257] = { 0 };
37 /* It seems that the data about valid trail bytes is not available from kernel32
38 * so we have to store is here. The format is the same as for lead bytes in CPINFO */
39 struct cp_extra_info_t
42 BYTE TrailBytes
[MAX_LEADBYTES
];
45 static struct cp_extra_info_t g_cpextrainfo
[] =
47 {932, {0x40, 0x7e, 0x80, 0xfc, 0, 0}},
48 {936, {0x40, 0xfe, 0, 0}},
49 {949, {0x41, 0xfe, 0, 0}},
50 {950, {0x40, 0x7e, 0xa1, 0xfe, 0, 0}},
51 {1361, {0x31, 0x7e, 0x81, 0xfe, 0, 0}},
52 {20932, {1, 255, 0, 0}}, /* seems to give different results on different systems */
53 {0, {1, 255, 0, 0}} /* match all with FIXME */
56 /* Maps cp932 single byte character to multi byte character */
57 static const unsigned char mbbtombc_932
[] = {
58 0x40,0x49,0x68,0x94,0x90,0x93,0x95,0x66,0x69,0x6a,0x96,0x7b,0x43,0x7c,0x44,0x5e,
59 0x4f,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x46,0x47,0x83,0x81,0x84,0x48,
60 0x97,0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,
61 0x6f,0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x6d,0x8f,0x6e,0x4f,0x76,
62 0x77,0x78,0x79,0x6d,0x8f,0x6e,0x4f,0x51,0x65,0x81,0x82,0x83,0x84,0x85,0x86,0x87,
63 0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f,0x90,0x91,0x92,0x93,0x94,0x95,0x50,
64 0x42,0x75,0x76,0x41,0x45,0x92,0x40,0x42,0x44,0x46,0x48,0x83,0x85,0x87,0x62,
65 0x5b,0x41,0x43,0x45,0x47,0x49,0x4a,0x4c,0x4e,0x50,0x52,0x54,0x56,0x58,0x5a,0x5c,
66 0x5e,0x60,0x63,0x65,0x67,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x71,0x74,0x77,0x7a,0x7d,
67 0x7e,0x80,0x81,0x82,0x84,0x86,0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8f,0x93,0x4a,0x4b };
69 /* Maps multibyte cp932 punctuation marks to single byte equivalents */
70 static const unsigned char mbctombb_932_punct
[] = {
71 0x20,0xa4,0xa1,0x2c,0x2e,0xa5,0x3a,0x3b,0x3f,0x21,0xde,0xdf,0x00,0x00,0x00,0x5e,
72 0x7e,0x5f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xb0,0x00,0x00,0x2f,0x00,
73 0x00,0x00,0x7c,0x00,0x00,0x60,0x27,0x00,0x22,0x28,0x29,0x00,0x00,0x5b,0x5d,0x7b,
74 0x7d,0x00,0x00,0x00,0x00,0xa2,0xa3,0x00,0x00,0x00,0x00,0x2b,0x2d,0x00,0x00,0x00,
75 0x00,0x3d,0x00,0x3c,0x3e,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x5c,
76 0x24,0x00,0x00,0x25,0x23,0x26,0x2a,0x40};
78 /* Maps multibyte cp932 hiragana/katakana to single-byte equivalents */
79 static const unsigned char mbctombb_932_kana
[] = {
80 0xa7,0xb1,0xa8,0xb2,0xa9,0xb3,0xaa,0xb4,0xab,0xb5,0xb6,0xb6,0xb7,0xb7,0xb8,0xb8,
81 0xb9,0xb9,0xba,0xba,0xbb,0xbb,0xbc,0xbc,0xbd,0xbd,0xbe,0xbe,0xbf,0xbf,0xc0,0xc0,
82 0xc1,0xc1,0xaf,0xc2,0xc2,0xc3,0xc3,0xc4,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xca,
83 0xca,0xcb,0xcb,0xcb,0xcc,0xcc,0xcc,0xcd,0xcd,0xcd,0xce,0xce,0xce,0xcf,0xd0,0xd1,
84 0xd2,0xd3,0xac,0xd4,0xad,0xd5,0xae,0xd6,0xd7,0xd8,0xd9,0xda,0xdb,0xdc,0xdc,0xb2,
85 0xb4,0xa6,0xdd,0xb3,0xb6,0xb9};
87 static MSVCRT_wchar_t
msvcrt_mbc_to_wc(unsigned int ch
)
97 mbch
[0] = (ch
>> 8) & 0xff;
101 if (!MultiByteToWideChar(get_mbcinfo()->mbcodepage
, 0, mbch
, n_chars
, &chW
, 1))
103 WARN("MultiByteToWideChar failed on %x\n", ch
);
109 static inline MSVCRT_size_t
u_strlen( const unsigned char *str
)
111 return strlen( (const char*) str
);
114 static inline unsigned char* u_strncat( unsigned char* dst
, const unsigned char* src
, MSVCRT_size_t len
)
116 return (unsigned char*)strncat( (char*)dst
, (const char*)src
, len
);
119 static inline int u_strcmp( const unsigned char *s1
, const unsigned char *s2
)
121 return strcmp( (const char*)s1
, (const char*)s2
);
124 static inline int u_strcasecmp( const unsigned char *s1
, const unsigned char *s2
)
126 return strcasecmp( (const char*)s1
, (const char*)s2
);
129 static inline int u_strncmp( const unsigned char *s1
, const unsigned char *s2
, MSVCRT_size_t len
)
131 return strncmp( (const char*)s1
, (const char*)s2
, len
);
134 static inline int u_strncasecmp( const unsigned char *s1
, const unsigned char *s2
, MSVCRT_size_t len
)
136 return strncasecmp( (const char*)s1
, (const char*)s2
, len
);
139 static inline unsigned char *u_strchr( const unsigned char *s
, unsigned char x
)
141 return (unsigned char*) strchr( (const char*)s
, x
);
144 static inline unsigned char *u_strrchr( const unsigned char *s
, unsigned char x
)
146 return (unsigned char*) strrchr( (const char*)s
, x
);
149 static inline unsigned char *u__strset( unsigned char *s
, unsigned char c
)
151 return (unsigned char*) _strset( (char*)s
, c
);
154 static inline unsigned char *u__strnset( unsigned char *s
, unsigned char c
, MSVCRT_size_t len
)
156 return (unsigned char*) MSVCRT__strnset( (char*)s
, c
, len
);
159 static inline MSVCRT_size_t
u_strcspn( const unsigned char *s
, const unsigned char *rej
)
161 return strcspn( (const char *)s
, (const char*)rej
);
164 /*********************************************************************
165 * __p__mbctype (MSVCRT.@)
167 unsigned char* CDECL
__p__mbctype(void)
169 return get_mbcinfo()->mbctype
;
172 /*********************************************************************
173 * __p___mb_cur_max(MSVCRT.@)
175 int* CDECL
__p___mb_cur_max(void)
177 return &get_locinfo()->mb_cur_max
;
180 /*********************************************************************
181 * ___mb_cur_max_func(MSVCRT.@)
183 int CDECL
MSVCRT____mb_cur_max_func(void)
185 return get_locinfo()->mb_cur_max
;
188 /*********************************************************************
189 * ___mb_cur_max_l_func (MSVCR80.@)
191 int* CDECL
___mb_cur_max_l_func(MSVCRT__locale_t locale
)
193 MSVCRT_pthreadlocinfo locinfo
;
196 locinfo
= get_locinfo();
198 locinfo
= locale
->locinfo
;
200 return &locinfo
->mb_cur_max
;
203 /*********************************************************************
204 * INTERNAL: _setmbcp_l
206 int _setmbcp_l(int cp
, LCID lcid
, MSVCRT_pthreadmbcinfo mbcinfo
)
208 const char format
[] = ".%d";
221 mbcinfo
= get_mbcinfo();
232 newcp
= get_locinfo()->lc_codepage
;
235 /* fall through (C locale) */
237 newcp
= 20127; /* ASCII */
245 sprintf(bufA
, format
, newcp
);
246 mbcinfo
->mblcid
= MSVCRT_locale_to_LCID(bufA
, NULL
);
248 mbcinfo
->mblcid
= lcid
;
251 if(mbcinfo
->mblcid
== -1)
253 WARN("Can't assign LCID to codepage (%d)\n", mbcinfo
->mblcid
);
257 if (!GetCPInfo(newcp
, &cpi
))
259 WARN("Codepage %d not found\n", newcp
);
260 *MSVCRT__errno() = MSVCRT_EINVAL
;
264 /* setup the _mbctype */
265 memset(mbcinfo
->mbctype
, 0, sizeof(unsigned char[257]));
266 memset(mbcinfo
->mbcasemap
, 0, sizeof(unsigned char[256]));
268 bytes
= cpi
.LeadByte
;
269 while (bytes
[0] || bytes
[1])
271 for (i
= bytes
[0]; i
<= bytes
[1]; i
++)
272 mbcinfo
->mbctype
[i
+ 1] |= _M1
;
276 if (cpi
.MaxCharSize
> 1)
278 /* trail bytes not available through kernel32 but stored in a structure in msvcrt */
279 struct cp_extra_info_t
*cpextra
= g_cpextrainfo
;
281 mbcinfo
->ismbcodepage
= 1;
284 if (cpextra
->cp
== 0 || cpextra
->cp
== newcp
)
286 if (cpextra
->cp
== 0)
287 FIXME("trail bytes data not available for DBCS codepage %d - assuming all bytes\n", newcp
);
289 bytes
= cpextra
->TrailBytes
;
290 while (bytes
[0] || bytes
[1])
292 for (i
= bytes
[0]; i
<= bytes
[1]; i
++)
293 mbcinfo
->mbctype
[i
+ 1] |= _M2
;
302 mbcinfo
->ismbcodepage
= 0;
304 /* we can't use GetStringTypeA directly because we don't have a locale - only a code page
307 for (i
= 0; i
< 256; i
++)
308 if (!(mbcinfo
->mbctype
[i
+ 1] & _M1
))
309 bufA
[charcount
++] = i
;
311 ret
= MultiByteToWideChar(newcp
, 0, bufA
, charcount
, bufW
, charcount
);
312 if (ret
!= charcount
)
313 ERR("MultiByteToWideChar of chars failed for cp %d, ret=%d (exp %d), error=%d\n", newcp
, ret
, charcount
, GetLastError());
315 GetStringTypeW(CT_CTYPE1
, bufW
, charcount
, chartypes
);
318 for (i
= 0; i
< 256; i
++)
319 if (!(mbcinfo
->mbctype
[i
+ 1] & _M1
))
321 if (chartypes
[charcount
] & C1_UPPER
)
323 mbcinfo
->mbctype
[i
+ 1] |= _SBUP
;
324 bufW
[charcount
] = tolowerW(bufW
[charcount
]);
326 else if (chartypes
[charcount
] & C1_LOWER
)
328 mbcinfo
->mbctype
[i
+ 1] |= _SBLOW
;
329 bufW
[charcount
] = toupperW(bufW
[charcount
]);
334 ret
= WideCharToMultiByte(newcp
, 0, bufW
, charcount
, bufA
, charcount
, NULL
, NULL
);
335 if (ret
!= charcount
)
336 ERR("WideCharToMultiByte failed for cp %d, ret=%d (exp %d), error=%d\n", newcp
, ret
, charcount
, GetLastError());
339 for (i
= 0; i
< 256; i
++)
341 if(!(mbcinfo
->mbctype
[i
+ 1] & _M1
))
343 if(mbcinfo
->mbctype
[i
] & (C1_UPPER
|C1_LOWER
))
344 mbcinfo
->mbcasemap
[i
] = bufA
[charcount
];
349 if (newcp
== 932) /* CP932 only - set _MP and _MS */
351 /* On Windows it's possible to calculate the _MP and _MS from CT_CTYPE1
352 * and CT_CTYPE3. But as of Wine 0.9.43 we return wrong values what makes
353 * it hard. As this is set only for codepage 932 we hardcode it what gives
354 * also faster execution.
356 for (i
= 161; i
<= 165; i
++)
357 mbcinfo
->mbctype
[i
+ 1] |= _MP
;
358 for (i
= 166; i
<= 223; i
++)
359 mbcinfo
->mbctype
[i
+ 1] |= _MS
;
362 mbcinfo
->mbcodepage
= newcp
;
363 if(MSVCRT_locale
&& mbcinfo
== MSVCRT_locale
->mbcinfo
)
364 memcpy(MSVCRT_mbctype
, MSVCRT_locale
->mbcinfo
->mbctype
, sizeof(MSVCRT_mbctype
));
369 /*********************************************************************
370 * _setmbcp (MSVCRT.@)
372 int CDECL
_setmbcp(int cp
)
374 return _setmbcp_l(cp
, -1, NULL
);
377 /*********************************************************************
378 * _getmbcp (MSVCRT.@)
380 int CDECL
_getmbcp(void)
382 return get_mbcinfo()->mbcodepage
;
385 /*********************************************************************
386 * _mbsnextc(MSVCRT.@)
388 unsigned int CDECL
_mbsnextc(const unsigned char* str
)
391 return *str
<< 8 | str
[1];
395 /*********************************************************************
396 * _mbctolower(MSVCRT.@)
398 unsigned int CDECL
_mbctolower(unsigned int c
)
402 FIXME("Handle MBC chars\n");
405 return tolower(c
); /* ASCII CP or SB char */
408 /*********************************************************************
409 * _mbctoupper(MSVCRT.@)
411 unsigned int CDECL
_mbctoupper(unsigned int c
)
415 FIXME("Handle MBC chars\n");
418 return toupper(c
); /* ASCII CP or SB char */
421 /*********************************************************************
422 * _mbctombb (MSVCRT.@)
424 unsigned int CDECL
_mbctombb(unsigned int c
)
428 if(get_mbcinfo()->mbcodepage
== 932)
430 if(c
>= 0x829f && c
<= 0x82f1) /* Hiragana */
431 return mbctombb_932_kana
[c
- 0x829f];
432 if(c
>= 0x8340 && c
<= 0x8396 && c
!= 0x837f) /* Katakana */
433 return mbctombb_932_kana
[c
- 0x8340 - (c
>= 0x837f ? 1 : 0)];
434 if(c
>= 0x8140 && c
<= 0x8197) /* Punctuation */
436 value
= mbctombb_932_punct
[c
- 0x8140];
437 return value
? value
: c
;
439 if((c
>= 0x824f && c
<= 0x8258) || /* Fullwidth digits */
440 (c
>= 0x8260 && c
<= 0x8279)) /* Fullwidth capitals letters */
442 if(c
>= 0x8281 && c
<= 0x829a) /* Fullwidth small letters */
444 /* all other cases return c */
449 /*********************************************************************
450 * _mbcjistojms(MSVCRT.@)
452 * Converts a jis character to sjis.
453 * Based on description from
454 * http://www.slayers.ne.jp/~oouchi/code/jistosjis.html
456 unsigned int CDECL
_mbcjistojms(unsigned int c
)
458 /* Conversion takes place only when codepage is 932.
459 In all other cases, c is returned unchanged */
460 if(get_mbcinfo()->mbcodepage
== 932)
462 if(HIBYTE(c
) >= 0x21 && HIBYTE(c
) <= 0x7e &&
463 LOBYTE(c
) >= 0x21 && LOBYTE(c
) <= 0x7e)
470 if(LOBYTE(c
) >= 0x7F)
473 c
= (((HIBYTE(c
) - 0x21)/2 + 0x81) << 8) | LOBYTE(c
);
479 return 0; /* Codepage is 932, but c can't be converted */
485 /*********************************************************************
486 * _mbcjmstojis(MSVCRT.@)
488 * Converts a sjis character to jis.
490 unsigned int CDECL
_mbcjmstojis(unsigned int c
)
492 /* Conversion takes place only when codepage is 932.
493 In all other cases, c is returned unchanged */
494 if(get_mbcinfo()->mbcodepage
== 932)
496 if(_ismbclegal(c
) && HIBYTE(c
) < 0xf0)
498 if(HIBYTE(c
) >= 0xe0)
501 c
= (((HIBYTE(c
) - 0x81)*2 + 0x21) << 8) | LOBYTE(c
);
512 return 0; /* Codepage is 932, but c can't be converted */
518 /*********************************************************************
521 unsigned char* CDECL
_mbsdec(const unsigned char* start
, const unsigned char* cur
)
526 if(get_mbcinfo()->ismbcodepage
)
527 return (unsigned char *)(_ismbstrail(start
,cur
-1) ? cur
- 2 : cur
-1);
529 return (unsigned char *)cur
- 1; /* ASCII CP or SB char */
532 /*********************************************************************
535 unsigned int CDECL
_mbclen(const unsigned char* str
)
537 return _ismbblead(*str
) ? 2 : 1;
540 /*********************************************************************
543 unsigned char* CDECL
_mbsinc(const unsigned char* str
)
545 return (unsigned char *)(str
+ _mbclen(str
));
548 /*********************************************************************
551 unsigned char* CDECL
_mbsninc(const unsigned char* str
, MSVCRT_size_t num
)
556 while (num
> 0 && *str
)
558 if (_ismbblead(*str
))
568 return (unsigned char*)str
;
571 /*********************************************************************
572 * _mbsnlen_l(MSVCRT.@)
574 MSVCRT_size_t CDECL
_mbsnlen_l(const unsigned char *str
,
575 MSVCRT_size_t maxsize
, MSVCRT__locale_t locale
)
577 MSVCRT_pthreadmbcinfo mbcinfo
;
578 MSVCRT_size_t i
= 0, len
= 0;
581 mbcinfo
= get_mbcinfo();
583 mbcinfo
= locale
->mbcinfo
;
585 if(!mbcinfo
->ismbcodepage
)
586 return MSVCRT_strnlen((const char*)str
, maxsize
);
588 while(i
<maxsize
&& str
[i
])
590 if (_ismbblead_l(str
[i
], locale
))
593 if (!str
[i
]) /* count only full chars */
599 return i
< maxsize
? len
: maxsize
;
602 /*********************************************************************
605 MSVCRT_size_t CDECL
_mbslen(const unsigned char* str
)
607 return _mbsnlen_l(str
, -1, NULL
);
610 /*********************************************************************
611 * _mbslen_l(MSVCRT.@)
613 MSVCRT_size_t CDECL
_mbslen_l(const unsigned char* str
, MSVCRT__locale_t locale
)
615 return _mbsnlen_l(str
, -1, locale
);
618 /*********************************************************************
621 MSVCRT_size_t CDECL
_mbsnlen(const unsigned char* str
, MSVCRT_size_t maxsize
)
623 return _mbsnlen_l(str
, maxsize
, NULL
);
626 /*********************************************************************
627 * _mbccpy_s_l(MSVCRT.@)
629 int CDECL
_mbccpy_s_l(unsigned char* dest
, MSVCRT_size_t maxsize
,
630 int *copied
, const unsigned char* src
, MSVCRT__locale_t locale
)
632 if(copied
) *copied
= 0;
633 if(!MSVCRT_CHECK_PMT(dest
!= NULL
&& maxsize
>= 1)) return MSVCRT_EINVAL
;
635 if(!MSVCRT_CHECK_PMT(src
!= NULL
)) return MSVCRT_EINVAL
;
637 if(_ismbblead_l(*src
, locale
)) {
639 if(copied
) *copied
= 1;
640 *MSVCRT__errno() = MSVCRT_EILSEQ
;
641 return MSVCRT_EILSEQ
;
645 MSVCRT_INVALID_PMT("dst buffer is too small", MSVCRT_ERANGE
);
646 return MSVCRT_ERANGE
;
651 if(copied
) *copied
= 2;
654 if(copied
) *copied
= 1;
660 /*********************************************************************
663 void CDECL
_mbccpy(unsigned char* dest
, const unsigned char* src
)
665 _mbccpy_s_l(dest
, 2, NULL
, src
, NULL
);
668 /*********************************************************************
669 * _mbccpy_l(MSVCRT.@)
671 void CDECL
_mbccpy_l(unsigned char* dest
, const unsigned char* src
,
672 MSVCRT__locale_t locale
)
674 _mbccpy_s_l(dest
, 2, NULL
, src
, locale
);
677 /*********************************************************************
678 * _mbccpy_s(MSVCRT.@)
680 int CDECL
_mbccpy_s(unsigned char* dest
, MSVCRT_size_t maxsize
,
681 int *copied
, const unsigned char* src
)
683 return _mbccpy_s_l(dest
, maxsize
, copied
, src
, NULL
);
686 /*********************************************************************
689 * The parameter n is the number or characters to copy, not the size of
690 * the buffer. Use _mbsnbcpy for a function analogical to strncpy
692 unsigned char* CDECL
_mbsncpy(unsigned char* dst
, const unsigned char* src
, MSVCRT_size_t n
)
694 unsigned char* ret
= dst
;
697 if (get_mbcinfo()->ismbcodepage
)
702 if (_ismbblead(*src
))
722 if (!(*dst
++ = *src
++)) break;
725 while (n
--) *dst
++ = 0;
729 /*********************************************************************
730 * _mbsnbcpy_s_l(MSVCRT.@)
732 * Unlike _mbsnbcpy this function does not pad the rest of the dest
735 int CDECL
_mbsnbcpy_s_l(unsigned char* dst
, MSVCRT_size_t size
,
736 const unsigned char* src
, MSVCRT_size_t n
, MSVCRT__locale_t locale
)
738 MSVCRT_size_t pos
= 0;
740 if(!dst
|| size
== 0)
741 return MSVCRT_EINVAL
;
745 return MSVCRT_EINVAL
;
750 if((locale
? locale
->mbcinfo
: get_mbcinfo())->ismbcodepage
)
752 BOOL is_lead
= FALSE
;
758 return MSVCRT_ERANGE
;
760 is_lead
= (!is_lead
&& _ismbblead(*src
));
765 if (is_lead
) /* if string ends with a lead, remove it */
776 return MSVCRT_ERANGE
;
789 return MSVCRT_ERANGE
;
795 /*********************************************************************
796 * _mbsnbcpy_s(MSVCRT.@)
798 int CDECL
_mbsnbcpy_s(unsigned char* dst
, MSVCRT_size_t size
, const unsigned char* src
, MSVCRT_size_t n
)
800 return _mbsnbcpy_s_l(dst
, size
, src
, n
, NULL
);
803 /*********************************************************************
804 * _mbscpy_s_l(MSVCRT.@)
806 int CDECL
_mbscpy_s_l(unsigned char *dst
, MSVCRT_size_t size
,
807 const unsigned char *src
, MSVCRT__locale_t locale
)
809 return _mbsnbcpy_s_l(dst
, size
, src
, -1, locale
);
812 /*********************************************************************
813 * _mbscpy_s(MSVCRT.@)
815 int CDECL
_mbscpy_s(unsigned char *dst
, MSVCRT_size_t size
, const unsigned char *src
)
817 return _mbscpy_s_l(dst
, size
, src
, NULL
);
820 /*********************************************************************
821 * _mbsnbcpy(MSVCRT.@)
823 * Like strncpy this function doesn't enforce the string to be
826 unsigned char* CDECL
_mbsnbcpy(unsigned char* dst
, const unsigned char* src
, MSVCRT_size_t n
)
828 unsigned char* ret
= dst
;
831 if(get_mbcinfo()->ismbcodepage
)
833 BOOL is_lead
= FALSE
;
836 is_lead
= (!is_lead
&& _ismbblead(*src
));
841 if (is_lead
) /* if string ends with a lead, remove it */
849 if (!(*dst
++ = *src
++)) break;
852 while (n
--) *dst
++ = 0;
856 /*********************************************************************
859 int CDECL
_mbscmp(const unsigned char* str
, const unsigned char* cmp
)
861 if (!str
|| !cmp
) return INT_MAX
;
863 if(get_mbcinfo()->ismbcodepage
)
865 unsigned int strc
, cmpc
;
868 return *cmp
? -1 : 0;
871 strc
= _mbsnextc(str
);
872 cmpc
= _mbsnextc(cmp
);
874 return strc
< cmpc
? -1 : 1;
875 str
+=(strc
> 255) ? 2 : 1;
876 cmp
+=(strc
> 255) ? 2 : 1; /* equal, use same increment */
879 return u_strcmp(str
, cmp
); /* ASCII CP */
882 /*********************************************************************
883 * _mbsnbicoll_l(MSVCRT.@)
885 int CDECL
_mbsnbicoll_l(const unsigned char *str1
, const unsigned char *str2
, MSVCRT_size_t len
, MSVCRT__locale_t locale
)
887 MSVCRT_pthreadmbcinfo mbcinfo
;
890 mbcinfo
= get_mbcinfo();
892 mbcinfo
= locale
->mbcinfo
;
894 if(!mbcinfo
->ismbcodepage
)
895 return MSVCRT__strnicoll_l((const char*)str1
, (const char*)str2
, len
, locale
);
896 return CompareStringA(mbcinfo
->mblcid
, NORM_IGNORECASE
, (const char*)str1
, len
, (const char*)str2
, len
)-CSTR_EQUAL
;
899 /*********************************************************************
900 * _mbsicoll_l(MSVCRT.@)
902 int CDECL
_mbsicoll_l(const unsigned char *str1
, const unsigned char *str2
, MSVCRT__locale_t locale
)
904 return _mbsnbicoll_l(str1
, str2
, -1, locale
);
907 /*********************************************************************
908 * _mbsnbicoll(MSVCRT.@)
910 int CDECL
_mbsnbicoll(const unsigned char *str1
, const unsigned char *str2
, MSVCRT_size_t len
)
912 return _mbsnbicoll_l(str1
, str2
, len
, NULL
);
915 /*********************************************************************
916 * _mbsicoll(MSVCRT.@)
918 int CDECL
_mbsicoll(const unsigned char* str
, const unsigned char* cmp
)
920 #if _MSVCR_VER>=60 && _MSVCR_VER<=71
921 return CompareStringA(get_mbcinfo()->mblcid
, NORM_IGNORECASE
,
922 (const char*)str
, -1, (const char*)cmp
, -1)-CSTR_EQUAL
;
924 return _mbsnbicoll_l(str
, cmp
, -1, NULL
);
928 /*********************************************************************
929 * _mbsnbcoll_l(MSVCRT.@)
931 int CDECL
_mbsnbcoll_l(const unsigned char *str1
, const unsigned char *str2
, MSVCRT_size_t len
, MSVCRT__locale_t locale
)
933 MSVCRT_pthreadmbcinfo mbcinfo
;
936 mbcinfo
= get_mbcinfo();
938 mbcinfo
= locale
->mbcinfo
;
940 if(!mbcinfo
->ismbcodepage
)
941 return MSVCRT__strncoll_l((const char*)str1
, (const char*)str2
, len
, locale
);
942 return CompareStringA(mbcinfo
->mblcid
, 0, (const char*)str1
, len
, (const char*)str2
, len
)-CSTR_EQUAL
;
945 /*********************************************************************
946 * _mbscoll_l(MSVCRT.@)
948 int CDECL
_mbscoll_l(const unsigned char *str1
, const unsigned char *str2
, MSVCRT__locale_t locale
)
950 return _mbsnbcoll_l(str1
, str2
, -1, locale
);
953 /*********************************************************************
954 * _mbsnbcoll(MSVCRT.@)
956 int CDECL
_mbsnbcoll(const unsigned char *str1
, const unsigned char *str2
, MSVCRT_size_t len
)
958 return _mbsnbcoll_l(str1
, str2
, len
, NULL
);
961 /*********************************************************************
964 int CDECL
_mbscoll(const unsigned char* str
, const unsigned char* cmp
)
966 #if _MSVCR_VER>=60 && _MSVCR_VER<=71
967 return CompareStringA(get_mbcinfo()->mblcid
, 0,
968 (const char*)str
, -1, (const char*)cmp
, -1)-CSTR_EQUAL
;
970 return _mbsnbcoll_l(str
, cmp
, -1, NULL
);
974 /*********************************************************************
977 int CDECL
_mbsicmp(const unsigned char* str
, const unsigned char* cmp
)
979 if(get_mbcinfo()->ismbcodepage
)
981 unsigned int strc
, cmpc
;
984 return *cmp
? -1 : 0;
987 strc
= _mbctolower(_mbsnextc(str
));
988 cmpc
= _mbctolower(_mbsnextc(cmp
));
990 return strc
< cmpc
? -1 : 1;
991 str
+=(strc
> 255) ? 2 : 1;
992 cmp
+=(strc
> 255) ? 2 : 1; /* equal, use same increment */
995 return u_strcasecmp(str
, cmp
); /* ASCII CP */
998 /*********************************************************************
1001 int CDECL
_mbsncmp(const unsigned char* str
, const unsigned char* cmp
, MSVCRT_size_t len
)
1006 if(get_mbcinfo()->ismbcodepage
)
1008 unsigned int strc
, cmpc
;
1013 return *cmp
? -1 : 0;
1016 strc
= _mbsnextc(str
);
1017 cmpc
= _mbsnextc(cmp
);
1019 return strc
< cmpc
? -1 : 1;
1020 inc
=(strc
> 255) ? 2 : 1; /* Equal, use same increment */
1024 return 0; /* Matched len chars */
1026 return u_strncmp(str
, cmp
, len
); /* ASCII CP */
1029 /*********************************************************************
1030 * _mbsnbcmp(MSVCRT.@)
1032 int CDECL
_mbsnbcmp(const unsigned char* str
, const unsigned char* cmp
, MSVCRT_size_t len
)
1036 if(get_mbcinfo()->ismbcodepage
)
1038 unsigned int strc
, cmpc
;
1043 return *cmp
? -1 : 0;
1046 if (_ismbblead(*str
))
1048 strc
=(len
>=2)?_mbsnextc(str
):0;
1056 if (_ismbblead(*cmp
))
1057 cmpc
=(len
>=2)?_mbsnextc(cmp
):0;
1061 return strc
< cmpc
? -1 : 1;
1066 return 0; /* Matched len chars */
1068 return u_strncmp(str
,cmp
,len
);
1071 /*********************************************************************
1072 * _mbsnicmp(MSVCRT.@)
1074 * Compare two multibyte strings case insensitively to 'len' characters.
1076 int CDECL
_mbsnicmp(const unsigned char* str
, const unsigned char* cmp
, MSVCRT_size_t len
)
1078 /* FIXME: No tolower() for mb strings yet */
1079 if(get_mbcinfo()->ismbcodepage
)
1081 unsigned int strc
, cmpc
;
1085 return *cmp
? -1 : 0;
1088 strc
= _mbctolower(_mbsnextc(str
));
1089 cmpc
= _mbctolower(_mbsnextc(cmp
));
1091 return strc
< cmpc
? -1 : 1;
1092 str
+=(strc
> 255) ? 2 : 1;
1093 cmp
+=(strc
> 255) ? 2 : 1; /* Equal, use same increment */
1095 return 0; /* Matched len chars */
1097 return u_strncasecmp(str
, cmp
, len
); /* ASCII CP */
1100 /*********************************************************************
1101 * _mbsnbicmp(MSVCRT.@)
1103 int CDECL
_mbsnbicmp(const unsigned char* str
, const unsigned char* cmp
, MSVCRT_size_t len
)
1107 if(get_mbcinfo()->ismbcodepage
)
1109 unsigned int strc
, cmpc
;
1114 return *cmp
? -1 : 0;
1117 if (_ismbblead(*str
))
1119 strc
=(len
>=2)?_mbsnextc(str
):0;
1127 if (_ismbblead(*cmp
))
1128 cmpc
=(len
>=2)?_mbsnextc(cmp
):0;
1131 strc
= _mbctolower(strc
);
1132 cmpc
= _mbctolower(cmpc
);
1134 return strc
< cmpc
? -1 : 1;
1139 return 0; /* Matched len bytes */
1141 return u_strncasecmp(str
,cmp
,len
);
1144 /*********************************************************************
1145 * _mbscat (MSVCRT.@)
1147 unsigned char * CDECL
_mbscat( unsigned char *dst
, const unsigned char *src
)
1149 strcat( (char *)dst
, (const char *)src
);
1153 /*********************************************************************
1154 * _mbscat_s_l (MSVCRT.@)
1156 int CDECL
_mbscat_s_l( unsigned char *dst
, MSVCRT_size_t size
,
1157 const unsigned char *src
, MSVCRT__locale_t locale
)
1162 if(!MSVCRT_CHECK_PMT(dst
!= NULL
)) return MSVCRT_EINVAL
;
1163 if(!MSVCRT_CHECK_PMT(src
!= NULL
)) return MSVCRT_EINVAL
;
1165 for(i
=0; i
<size
; i
++)
1168 MSVCRT_INVALID_PMT("dst is not NULL-terminated", MSVCRT_EINVAL
);
1169 if(size
) dst
[0] = 0;
1170 return MSVCRT_EINVAL
;
1173 if(i
&& _ismbblead_l(dst
[i
-1], locale
)) {
1174 ret
= MSVCRT_EILSEQ
;
1178 for(j
=0; src
[j
] && i
+j
<size
; j
++)
1181 MSVCRT_INVALID_PMT("dst buffer is too small", MSVCRT_ERANGE
);
1183 return MSVCRT_ERANGE
;
1186 if(j
&& _ismbblead_l(src
[j
-1], locale
)) {
1187 ret
= MSVCRT_EILSEQ
;
1195 /*********************************************************************
1196 * _mbscat_s (MSVCRT.@)
1198 int CDECL
_mbscat_s( unsigned char *dst
, MSVCRT_size_t size
, const unsigned char *src
)
1200 return _mbscat_s_l(dst
, size
, src
, NULL
);
1203 /*********************************************************************
1204 * _mbscpy (MSVCRT.@)
1206 unsigned char* CDECL
_mbscpy( unsigned char *dst
, const unsigned char *src
)
1208 strcpy( (char *)dst
, (const char *)src
);
1212 /*********************************************************************
1213 * _mbsstr (MSVCRT.@)
1215 unsigned char * CDECL
_mbsstr(const unsigned char *haystack
, const unsigned char *needle
)
1217 return (unsigned char *)strstr( (const char *)haystack
, (const char *)needle
);
1220 /*********************************************************************
1223 * Find a multibyte character in a multibyte string.
1225 unsigned char* CDECL
_mbschr(const unsigned char* s
, unsigned int x
)
1227 if(get_mbcinfo()->ismbcodepage
)
1234 return (unsigned char*)s
;
1237 s
+= c
> 255 ? 2 : 1;
1240 return u_strchr(s
, x
); /* ASCII CP */
1243 /*********************************************************************
1244 * _mbsrchr(MSVCRT.@)
1246 unsigned char* CDECL
_mbsrchr(const unsigned char* s
, unsigned int x
)
1248 if(get_mbcinfo()->ismbcodepage
)
1251 unsigned char* match
=NULL
;
1257 match
=(unsigned char*)s
;
1260 s
+=(c
> 255) ? 2 : 1;
1263 return u_strrchr(s
, x
);
1266 /*********************************************************************
1267 * _mbstok_s_l(MSVCRT.@)
1269 unsigned char* CDECL
_mbstok_s_l(unsigned char *str
, const unsigned char *delim
,
1270 unsigned char **ctx
, MSVCRT__locale_t locale
)
1272 MSVCRT_pthreadmbcinfo mbcinfo
;
1275 if(!MSVCRT_CHECK_PMT(delim
!= NULL
)) return NULL
;
1276 if(!MSVCRT_CHECK_PMT(ctx
!= NULL
)) return NULL
;
1277 if(!MSVCRT_CHECK_PMT(str
|| *ctx
)) return NULL
;
1280 mbcinfo
= locale
->mbcinfo
;
1282 mbcinfo
= get_mbcinfo();
1284 if(!mbcinfo
->ismbcodepage
)
1285 return (unsigned char*)MSVCRT_strtok_s((char*)str
, (const char*)delim
, (char**)ctx
);
1290 while((c
=_mbsnextc(str
)) && _mbschr(delim
, c
))
1291 str
+= c
>255 ? 2 : 1;
1298 *ctx
= str
+ (c
>255 ? 2 : 1);
1299 while((c
=_mbsnextc(*ctx
)) && !_mbschr(delim
, c
))
1300 *ctx
+= c
>255 ? 2 : 1;
1311 /*********************************************************************
1312 * _mbstok_s(MSVCRT.@)
1314 unsigned char* CDECL
_mbstok_s(unsigned char *str
,
1315 const unsigned char *delim
, unsigned char **ctx
)
1317 return _mbstok_s_l(str
, delim
, ctx
, NULL
);
1320 /*********************************************************************
1321 * _mbstok_l(MSVCRT.@)
1323 unsigned char* CDECL
_mbstok_l(unsigned char *str
,
1324 const unsigned char *delim
, MSVCRT__locale_t locale
)
1326 return _mbstok_s_l(str
, delim
, &msvcrt_get_thread_data()->mbstok_next
, locale
);
1329 /*********************************************************************
1332 unsigned char* CDECL
_mbstok(unsigned char *str
, const unsigned char *delim
)
1334 thread_data_t
*data
= msvcrt_get_thread_data();
1337 if(!str
&& !data
->mbstok_next
)
1341 return _mbstok_s_l(str
, delim
, &data
->mbstok_next
, NULL
);
1344 /*********************************************************************
1345 * _mbbtombc(MSVCRT.@)
1347 unsigned int CDECL
_mbbtombc(unsigned int c
)
1349 if(get_mbcinfo()->mbcodepage
== 932)
1351 if(c
>= 0x20 && c
<= 0x7e) {
1352 if((c
>= 0x41 && c
<= 0x5a) || (c
>= 0x61 && c
<= 0x7a) || (c
>= 0x30 && c
<= 0x39))
1353 return mbbtombc_932
[c
- 0x20] | 0x8200;
1355 return mbbtombc_932
[c
- 0x20] | 0x8100;
1357 else if(c
>= 0xa1 && c
<= 0xdf) {
1358 if(c
>= 0xa6 && c
<= 0xdd && c
!= 0xb0)
1359 return mbbtombc_932
[c
- 0xa1 + 0x5f] | 0x8300;
1361 return mbbtombc_932
[c
- 0xa1 + 0x5f] | 0x8100;
1364 return c
; /* not Japanese or no MB char */
1367 /*********************************************************************
1368 * _mbbtype(MSVCRT.@)
1370 int CDECL
_mbbtype(unsigned char c
, int type
)
1374 if ((c
>= 0x20 && c
<= 0x7e) || (c
>= 0xa1 && c
<= 0xdf))
1376 else if ((c
>= 0x40 && c
<= 0x7e) || (c
>= 0x80 && c
<= 0xfc))
1379 return _MBC_ILLEGAL
;
1383 if ((c
>= 0x20 && c
<= 0x7e) || (c
>= 0xa1 && c
<= 0xdf))
1385 else if ((c
>= 0x81 && c
<= 0x9f) || (c
>= 0xe0 && c
<= 0xfc))
1388 return _MBC_ILLEGAL
;
1392 /*********************************************************************
1393 * _ismbbkana(MSVCRT.@)
1395 int CDECL
_ismbbkana(unsigned int c
)
1397 /* FIXME: use lc_ctype when supported, not lc_all */
1398 if(get_mbcinfo()->mbcodepage
== 932)
1400 /* Japanese/Katakana, CP 932 */
1401 return (c
>= 0xa1 && c
<= 0xdf);
1406 /*********************************************************************
1407 * _ismbcdigit(MSVCRT.@)
1409 int CDECL
_ismbcdigit(unsigned int ch
)
1411 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1412 return (get_char_typeW( wch
) & C1_DIGIT
);
1415 /*********************************************************************
1416 * _ismbcgraph(MSVCRT.@)
1418 int CDECL
_ismbcgraph(unsigned int ch
)
1420 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1421 return (get_char_typeW( wch
) & (C1_UPPER
| C1_LOWER
| C1_DIGIT
| C1_PUNCT
| C1_ALPHA
));
1424 /*********************************************************************
1425 * _ismbcalpha (MSVCRT.@)
1427 int CDECL
_ismbcalpha(unsigned int ch
)
1429 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1430 return (get_char_typeW( wch
) & C1_ALPHA
);
1433 /*********************************************************************
1434 * _ismbclower (MSVCRT.@)
1436 int CDECL
_ismbclower(unsigned int ch
)
1438 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1439 return (get_char_typeW( wch
) & C1_UPPER
);
1442 /*********************************************************************
1443 * _ismbcupper (MSVCRT.@)
1445 int CDECL
_ismbcupper(unsigned int ch
)
1447 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1448 return (get_char_typeW( wch
) & C1_LOWER
);
1451 /*********************************************************************
1452 * _ismbcsymbol(MSVCRT.@)
1454 int CDECL
_ismbcsymbol(unsigned int ch
)
1456 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1458 if (!GetStringTypeW(CT_CTYPE3
, &wch
, 1, &ctype
))
1460 WARN("GetStringTypeW failed on %x\n", ch
);
1463 return ((ctype
& C3_SYMBOL
) != 0);
1466 /*********************************************************************
1467 * _ismbcalnum (MSVCRT.@)
1469 int CDECL
_ismbcalnum(unsigned int ch
)
1471 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1472 return (get_char_typeW( wch
) & (C1_ALPHA
| C1_DIGIT
));
1475 /*********************************************************************
1476 * _ismbcspace (MSVCRT.@)
1478 int CDECL
_ismbcspace(unsigned int ch
)
1480 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1481 return (get_char_typeW( wch
) & C1_SPACE
);
1484 /*********************************************************************
1485 * _ismbcprint (MSVCRT.@)
1487 int CDECL
_ismbcprint(unsigned int ch
)
1489 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1490 return (get_char_typeW( wch
) & (C1_UPPER
| C1_LOWER
| C1_DIGIT
| C1_PUNCT
| C1_ALPHA
| C1_SPACE
));
1493 /*********************************************************************
1494 * _ismbcpunct(MSVCRT.@)
1496 int CDECL
_ismbcpunct(unsigned int ch
)
1498 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1499 return (get_char_typeW( wch
) & C1_PUNCT
);
1502 /*********************************************************************
1503 * _ismbchira(MSVCRT.@)
1505 int CDECL
_ismbchira(unsigned int c
)
1507 if(get_mbcinfo()->mbcodepage
== 932)
1509 /* Japanese/Hiragana, CP 932 */
1510 return (c
>= 0x829f && c
<= 0x82f1);
1515 /*********************************************************************
1516 * _ismbckata(MSVCRT.@)
1518 int CDECL
_ismbckata(unsigned int c
)
1520 if(get_mbcinfo()->mbcodepage
== 932)
1522 /* Japanese/Katakana, CP 932 */
1523 return (c
>= 0x8340 && c
<= 0x8396 && c
!= 0x837f);
1529 /*********************************************************************
1530 * _ismbblead_l(MSVCRT.@)
1532 int CDECL
_ismbblead_l(unsigned int c
, MSVCRT__locale_t locale
)
1534 MSVCRT_pthreadmbcinfo mbcinfo
;
1537 mbcinfo
= get_mbcinfo();
1539 mbcinfo
= locale
->mbcinfo
;
1541 return (mbcinfo
->mbctype
[(c
&0xff) + 1] & _M1
) != 0;
1544 /*********************************************************************
1545 * _ismbblead(MSVCRT.@)
1547 int CDECL
_ismbblead(unsigned int c
)
1549 return _ismbblead_l(c
, NULL
);
1553 /*********************************************************************
1554 * _ismbbtrail(MSVCRT.@)
1556 int CDECL
_ismbbtrail(unsigned int c
)
1558 return (get_mbcinfo()->mbctype
[(c
&0xff) + 1] & _M2
) != 0;
1561 /*********************************************************************
1562 * _ismbclegal(MSVCRT.@)
1564 int CDECL
_ismbclegal(unsigned int c
)
1566 return _ismbblead(HIBYTE(c
)) && _ismbbtrail(LOBYTE(c
));
1569 /*********************************************************************
1570 * _ismbslead(MSVCRT.@)
1572 int CDECL
_ismbslead(const unsigned char* start
, const unsigned char* str
)
1576 if(!get_mbcinfo()->ismbcodepage
)
1579 /* Lead bytes can also be trail bytes so we need to analyse the string
1581 while (start
<= str
)
1585 lead
= !lead
&& _ismbblead(*start
);
1589 return lead
? -1 : 0;
1592 /*********************************************************************
1593 * _ismbstrail(MSVCRT.@)
1595 int CDECL
_ismbstrail(const unsigned char* start
, const unsigned char* str
)
1597 /* Note: this function doesn't check _ismbbtrail */
1598 if ((str
> start
) && _ismbslead(start
, str
-1))
1604 /*********************************************************************
1605 * _mbsbtype (MSVCRT.@)
1607 int CDECL
_mbsbtype(const unsigned char *str
, MSVCRT_size_t count
)
1610 const unsigned char *end
= str
+ count
;
1612 /* Lead bytes can also be trail bytes so we need to analyse the string.
1613 * Also we must return _MBC_ILLEGAL for chars past the end of the string
1615 while (str
< end
) /* Note: we skip the last byte - will check after the loop */
1618 return _MBC_ILLEGAL
;
1619 lead
= get_mbcinfo()->ismbcodepage
&& !lead
&& _ismbblead(*str
);
1624 if (_ismbbtrail(*str
))
1627 return _MBC_ILLEGAL
;
1629 if (_ismbblead(*str
))
1635 /*********************************************************************
1638 unsigned char* CDECL
_mbsset(unsigned char* str
, unsigned int c
)
1640 unsigned char* ret
= str
;
1642 if(!get_mbcinfo()->ismbcodepage
|| c
< 256)
1643 return u__strset(str
, c
); /* ASCII CP or SB char */
1645 c
&= 0xffff; /* Strip high bits */
1647 while(str
[0] && str
[1])
1653 str
[0] = '\0'; /* FIXME: OK to shorten? */
1658 /*********************************************************************
1659 * _mbsnbset(MSVCRT.@)
1661 unsigned char* CDECL
_mbsnbset(unsigned char *str
, unsigned int c
, MSVCRT_size_t len
)
1663 unsigned char *ret
= str
;
1668 if(!get_mbcinfo()->ismbcodepage
|| c
< 256)
1669 return u__strnset(str
, c
, len
); /* ASCII CP or SB char */
1671 c
&= 0xffff; /* Strip high bits */
1673 while(str
[0] && str
[1] && (len
> 1))
1681 /* as per msdn pad with a blank character */
1688 /*********************************************************************
1689 * _mbsnset(MSVCRT.@)
1691 unsigned char* CDECL
_mbsnset(unsigned char* str
, unsigned int c
, MSVCRT_size_t len
)
1693 unsigned char *ret
= str
;
1698 if(!get_mbcinfo()->ismbcodepage
|| c
< 256)
1699 return u__strnset(str
, c
, len
); /* ASCII CP or SB char */
1701 c
&= 0xffff; /* Strip high bits */
1703 while(str
[0] && str
[1] && len
--)
1709 str
[0] = '\0'; /* FIXME: OK to shorten? */
1714 /*********************************************************************
1715 * _mbsnccnt(MSVCRT.@)
1716 * 'c' is for 'character'.
1718 MSVCRT_size_t CDECL
_mbsnccnt(const unsigned char* str
, MSVCRT_size_t len
)
1721 if(get_mbcinfo()->ismbcodepage
)
1724 while(*str
&& len
-- > 0)
1726 if(_ismbblead(*str
))
1739 return min(ret
, len
); /* ASCII CP */
1742 /*********************************************************************
1743 * _mbsnbcnt(MSVCRT.@)
1744 * 'b' is for byte count.
1746 MSVCRT_size_t CDECL
_mbsnbcnt(const unsigned char* str
, MSVCRT_size_t len
)
1749 if(get_mbcinfo()->ismbcodepage
)
1751 const unsigned char* xstr
= str
;
1752 while(*xstr
&& len
-- > 0)
1754 if (_ismbblead(*xstr
++))
1760 return min(ret
, len
); /* ASCII CP */
1763 /*********************************************************************
1764 * _mbsnbcat(MSVCRT.@)
1766 unsigned char* CDECL
_mbsnbcat(unsigned char* dst
, const unsigned char* src
, MSVCRT_size_t len
)
1768 if(get_mbcinfo()->ismbcodepage
)
1770 unsigned char *res
= dst
;
1772 if (_ismbblead(*dst
++)) {
1776 /* as per msdn overwrite the lead byte in front of '\0' */
1782 while (*src
&& len
--) *dst
++ = *src
++;
1786 return u_strncat(dst
, src
, len
); /* ASCII CP */
1789 int CDECL
_mbsnbcat_s(unsigned char *dst
, MSVCRT_size_t size
, const unsigned char *src
, MSVCRT_size_t len
)
1791 unsigned char *ptr
= dst
;
1794 if (!dst
&& !size
&& !src
&& !len
)
1797 if (!dst
|| !size
|| !src
)
1802 *MSVCRT__errno() = MSVCRT_EINVAL
;
1803 return MSVCRT_EINVAL
;
1806 /* Find the null terminator of the destination buffer. */
1807 while (size
&& *ptr
)
1813 *MSVCRT__errno() = MSVCRT_EINVAL
;
1814 return MSVCRT_EINVAL
;
1817 /* If necessary, check that the character preceding the null terminator is
1818 * a lead byte and move the pointer back by one for later overwrite. */
1819 if (ptr
!= dst
&& get_mbcinfo()->ismbcodepage
&& _ismbblead(*(ptr
- 1)))
1822 for (i
= 0; *src
&& i
< len
; i
++)
1830 *MSVCRT__errno() = MSVCRT_ERANGE
;
1831 return MSVCRT_ERANGE
;
1839 /*********************************************************************
1840 * _mbsncat(MSVCRT.@)
1842 unsigned char* CDECL
_mbsncat(unsigned char* dst
, const unsigned char* src
, MSVCRT_size_t len
)
1844 if(get_mbcinfo()->ismbcodepage
)
1846 unsigned char *res
= dst
;
1849 if (_ismbblead(*dst
++))
1852 while (*src
&& len
--)
1855 if(_ismbblead(*src
++))
1861 return u_strncat(dst
, src
, len
); /* ASCII CP */
1865 /*********************************************************************
1868 unsigned char* CDECL
_mbslwr(unsigned char* s
)
1870 unsigned char *ret
= s
;
1873 if (get_mbcinfo()->ismbcodepage
)
1878 c
= _mbctolower(_mbsnextc(s
));
1879 /* Note that I assume that the size of the character is unchanged */
1888 else for ( ; *s
; s
++) *s
= tolower(*s
);
1893 /*********************************************************************
1894 * _mbslwr_s(MSVCRT.@)
1896 int CDECL
_mbslwr_s(unsigned char* s
, MSVCRT_size_t len
)
1902 else if (!s
|| !len
)
1904 *MSVCRT__errno() = MSVCRT_EINVAL
;
1905 return MSVCRT_EINVAL
;
1907 if (get_mbcinfo()->ismbcodepage
)
1910 for ( ; *s
&& len
> 0; len
--)
1912 c
= _mbctolower(_mbsnextc(s
));
1913 /* Note that I assume that the size of the character is unchanged */
1922 else for ( ; *s
&& len
> 0; s
++, len
--) *s
= tolower(*s
);
1926 *MSVCRT__errno() = MSVCRT_EINVAL
;
1927 return MSVCRT_EINVAL
;
1933 /*********************************************************************
1936 unsigned char* CDECL
_mbsupr(unsigned char* s
)
1938 unsigned char *ret
= s
;
1941 if (get_mbcinfo()->ismbcodepage
)
1946 c
= _mbctoupper(_mbsnextc(s
));
1947 /* Note that I assume that the size of the character is unchanged */
1956 else for ( ; *s
; s
++) *s
= toupper(*s
);
1961 /*********************************************************************
1962 * _mbsupr_s(MSVCRT.@)
1964 int CDECL
_mbsupr_s(unsigned char* s
, MSVCRT_size_t len
)
1970 else if (!s
|| !len
)
1972 *MSVCRT__errno() = MSVCRT_EINVAL
;
1973 return MSVCRT_EINVAL
;
1975 if (get_mbcinfo()->ismbcodepage
)
1978 for ( ; *s
&& len
> 0; len
--)
1980 c
= _mbctoupper(_mbsnextc(s
));
1981 /* Note that I assume that the size of the character is unchanged */
1990 else for ( ; *s
&& len
> 0; s
++, len
--) *s
= toupper(*s
);
1994 *MSVCRT__errno() = MSVCRT_EINVAL
;
1995 return MSVCRT_EINVAL
;
2001 /*********************************************************************
2002 * _mbsspn (MSVCRT.@)
2004 MSVCRT_size_t CDECL
_mbsspn(const unsigned char* string
, const unsigned char* set
)
2006 const unsigned char *p
, *q
;
2008 for (p
= string
; *p
; p
++)
2012 for (q
= set
; *q
; q
++)
2016 if ((*p
== *q
) && (p
[1] == q
[1]))
2020 if (!q
[0] || !q
[1]) break;
2024 for (q
= set
; *q
; q
++)
2033 /*********************************************************************
2034 * _mbsspnp (MSVCRT.@)
2036 unsigned char* CDECL
_mbsspnp(const unsigned char* string
, const unsigned char* set
)
2038 const unsigned char *p
, *q
;
2040 for (p
= string
; *p
; p
++)
2044 for (q
= set
; *q
; q
++)
2048 if ((*p
== *q
) && (p
[1] == q
[1]))
2052 if (!q
[0] || !q
[1]) break;
2056 for (q
= set
; *q
; q
++)
2064 return (unsigned char *)p
;
2067 /*********************************************************************
2068 * _mbscspn(MSVCRT.@)
2070 MSVCRT_size_t CDECL
_mbscspn(const unsigned char* str
, const unsigned char* cmp
)
2072 if (get_mbcinfo()->ismbcodepage
)
2073 FIXME("don't handle double character case\n");
2074 return u_strcspn(str
, cmp
);
2077 /*********************************************************************
2078 * _mbsrev (MSVCRT.@)
2080 unsigned char* CDECL
_mbsrev(unsigned char* str
)
2082 int i
, len
= _mbslen(str
);
2083 unsigned char *p
, *temp
=MSVCRT_malloc(len
*2);
2088 /* unpack multibyte string to temp buffer */
2090 for(i
=0; i
<len
; i
++)
2104 /* repack it in the reverse order */
2106 for(i
=len
-1; i
>=0; i
--)
2108 if(_ismbblead(temp
[i
*2]))
2124 /*********************************************************************
2125 * _mbspbrk (MSVCRT.@)
2127 unsigned char* CDECL
_mbspbrk(const unsigned char* str
, const unsigned char* accept
)
2129 const unsigned char* p
;
2133 for(p
= accept
; *p
; p
+= (_ismbblead(*p
)?2:1) )
2136 if( !_ismbblead(*p
) || ( *(p
+1) == *(str
+1) ) )
2137 return (unsigned char*)str
;
2139 str
+= (_ismbblead(*str
)?2:1);
2146 * Functions depending on locale codepage
2149 /*********************************************************************
2152 * Unlike most of the multibyte string functions this function uses
2153 * the locale codepage, not the codepage set by _setmbcp
2155 int CDECL
MSVCRT_mblen(const char* str
, MSVCRT_size_t size
)
2157 if (str
&& *str
&& size
)
2159 if(get_locinfo()->mb_cur_max
== 1)
2160 return 1; /* ASCII CP */
2162 return !MSVCRT_isleadbyte((unsigned char)*str
) ? 1 : (size
>1 ? 2 : -1);
2167 /*********************************************************************
2170 MSVCRT_size_t CDECL
MSVCRT_mbrlen(const char *str
, MSVCRT_size_t len
, MSVCRT_mbstate_t
*state
)
2172 MSVCRT_mbstate_t s
= (state
? *state
: 0);
2175 if(!len
|| !str
|| !*str
)
2178 if(get_locinfo()->mb_cur_max
== 1) {
2180 }else if(!s
&& MSVCRT_isleadbyte((unsigned char)*str
)) {
2182 s
= (unsigned char)*str
;
2199 /*********************************************************************
2200 * _mbstrlen_l(MSVCRT.@)
2202 MSVCRT_size_t CDECL
_mbstrlen_l(const char* str
, MSVCRT__locale_t locale
)
2204 MSVCRT_pthreadlocinfo locinfo
;
2207 locinfo
= get_locinfo();
2209 locinfo
= locale
->locinfo
;
2211 if(locinfo
->mb_cur_max
> 1) {
2213 len
= MultiByteToWideChar(locinfo
->lc_codepage
, MB_ERR_INVALID_CHARS
,
2216 *MSVCRT__errno() = MSVCRT_EILSEQ
;
2225 /*********************************************************************
2226 * _mbstrlen(MSVCRT.@)
2228 MSVCRT_size_t CDECL
_mbstrlen(const char* str
)
2230 return _mbstrlen_l(str
, NULL
);
2233 /*********************************************************************
2234 * _mbtowc_l(MSVCRT.@)
2236 int CDECL
MSVCRT_mbtowc_l(MSVCRT_wchar_t
*dst
, const char* str
, MSVCRT_size_t n
, MSVCRT__locale_t locale
)
2238 MSVCRT_pthreadlocinfo locinfo
;
2239 MSVCRT_wchar_t tmpdst
= '\0';
2242 locinfo
= get_locinfo();
2244 locinfo
= locale
->locinfo
;
2254 if(!locinfo
->lc_codepage
) {
2255 if(dst
) *dst
= (unsigned char)*str
;
2258 if(n
>=2 && MSVCRT__isleadbyte_l((unsigned char)*str
, locale
)) {
2259 if(!MultiByteToWideChar(locinfo
->lc_codepage
, 0, str
, 2, &tmpdst
, 1))
2261 if(dst
) *dst
= tmpdst
;
2264 if(!MultiByteToWideChar(locinfo
->lc_codepage
, 0, str
, 1, &tmpdst
, 1))
2266 if(dst
) *dst
= tmpdst
;
2270 /*********************************************************************
2273 int CDECL
MSVCRT_mbtowc(MSVCRT_wchar_t
*dst
, const char* str
, MSVCRT_size_t n
)
2275 return MSVCRT_mbtowc_l(dst
, str
, n
, NULL
);
2278 /*********************************************************************
2281 MSVCRT_size_t CDECL
MSVCRT_mbrtowc(MSVCRT_wchar_t
*dst
, const char *str
,
2282 MSVCRT_size_t n
, MSVCRT_mbstate_t
*state
)
2284 MSVCRT_pthreadlocinfo locinfo
= get_locinfo();
2285 MSVCRT_mbstate_t s
= (state
? *state
: 0);
2292 if(!n
|| !str
|| !*str
)
2295 if(locinfo
->mb_cur_max
== 1) {
2296 tmpstr
[len
++] = *str
;
2297 }else if(!s
&& MSVCRT_isleadbyte((unsigned char)*str
)) {
2299 s
= (unsigned char)*str
;
2307 tmpstr
[len
++] = *str
;
2316 if(!MultiByteToWideChar(locinfo
->lc_codepage
, 0, tmpstr
, len
, dst
, dst
? 1 : 0))
2325 /*********************************************************************
2326 * _mbstowcs_l(MSVCRT.@)
2328 MSVCRT_size_t CDECL
MSVCRT__mbstowcs_l(MSVCRT_wchar_t
*wcstr
, const char *mbstr
,
2329 MSVCRT_size_t count
, MSVCRT__locale_t locale
)
2331 MSVCRT_pthreadlocinfo locinfo
;
2332 MSVCRT_size_t i
, size
;
2335 *MSVCRT__errno() = MSVCRT_EINVAL
;
2340 locinfo
= get_locinfo();
2342 locinfo
= locale
->locinfo
;
2344 if(!locinfo
->lc_codepage
) {
2346 return strlen(mbstr
);
2348 for(i
=0; i
<count
; i
++) {
2349 wcstr
[i
] = (unsigned char)mbstr
[i
];
2350 if(!wcstr
[i
]) break;
2355 /* Ignore count parameter */
2357 return MultiByteToWideChar(locinfo
->lc_codepage
, 0, mbstr
, -1, NULL
, 0)-1;
2359 for(i
=0, size
=0; i
<count
; i
++) {
2360 if(mbstr
[size
] == '\0')
2363 size
+= (MSVCRT__isleadbyte_l((unsigned char)mbstr
[size
], locale
) ? 2 : 1);
2367 size
= MultiByteToWideChar(locinfo
->lc_codepage
, 0,
2368 mbstr
, size
, wcstr
, count
);
2370 if(count
) wcstr
[0] = '\0';
2371 *MSVCRT__errno() = MSVCRT_EILSEQ
;
2376 if(size
<count
&& wcstr
)
2382 /*********************************************************************
2383 * mbstowcs(MSVCRT.@)
2385 MSVCRT_size_t CDECL
MSVCRT_mbstowcs(MSVCRT_wchar_t
*wcstr
,
2386 const char *mbstr
, MSVCRT_size_t count
)
2388 return MSVCRT__mbstowcs_l(wcstr
, mbstr
, count
, NULL
);
2391 /*********************************************************************
2392 * _mbstowcs_s_l(MSVCRT.@)
2394 int CDECL
MSVCRT__mbstowcs_s_l(MSVCRT_size_t
*ret
, MSVCRT_wchar_t
*wcstr
,
2395 MSVCRT_size_t size
, const char *mbstr
,
2396 MSVCRT_size_t count
, MSVCRT__locale_t locale
)
2400 if(!wcstr
&& !size
) {
2401 conv
= MSVCRT__mbstowcs_l(NULL
, mbstr
, 0, locale
);
2407 if (!MSVCRT_CHECK_PMT(wcstr
!= NULL
)) return MSVCRT_EINVAL
;
2408 if (!MSVCRT_CHECK_PMT(mbstr
!= NULL
)) {
2409 if(size
) wcstr
[0] = '\0';
2410 return MSVCRT_EINVAL
;
2413 if(count
==MSVCRT__TRUNCATE
|| size
<count
)
2418 conv
= MSVCRT__mbstowcs_l(wcstr
, mbstr
, conv
, locale
);
2420 wcstr
[conv
++] = '\0';
2421 else if(conv
==size
&& (count
==MSVCRT__TRUNCATE
|| wcstr
[conv
-1]=='\0'))
2422 wcstr
[conv
-1] = '\0';
2424 MSVCRT_INVALID_PMT("wcstr[size] is too small", MSVCRT_ERANGE
);
2427 return MSVCRT_ERANGE
;
2435 /*********************************************************************
2436 * mbstowcs_s(MSVCRT.@)
2438 int CDECL
MSVCRT__mbstowcs_s(MSVCRT_size_t
*ret
, MSVCRT_wchar_t
*wcstr
,
2439 MSVCRT_size_t size
, const char *mbstr
, MSVCRT_size_t count
)
2441 return MSVCRT__mbstowcs_s_l(ret
, wcstr
, size
, mbstr
, count
, NULL
);
2444 /*********************************************************************
2445 * mbsrtowcs(MSVCRT.@)
2447 MSVCRT_size_t CDECL
MSVCRT_mbsrtowcs(MSVCRT_wchar_t
*wcstr
,
2448 const char **pmbstr
, MSVCRT_size_t count
, MSVCRT_mbstate_t
*state
)
2450 MSVCRT_mbstate_t s
= (state
? *state
: 0);
2451 MSVCRT_wchar_t tmpdst
;
2452 MSVCRT_size_t ret
= 0;
2455 if(!MSVCRT_CHECK_PMT(pmbstr
!= NULL
))
2459 while(!wcstr
|| count
>ret
) {
2460 int ch_len
= MSVCRT_mbrtowc(&tmpdst
, p
, 2, &s
);
2462 wcstr
[ret
] = tmpdst
;
2466 }else if(ch_len
== 0) {
2467 if(wcstr
) *pmbstr
= NULL
;
2475 if(wcstr
) *pmbstr
= p
;
2479 /*********************************************************************
2480 * mbsrtowcs_s(MSVCRT.@)
2482 int CDECL
MSVCRT_mbsrtowcs_s(MSVCRT_size_t
*ret
, MSVCRT_wchar_t
*wcstr
, MSVCRT_size_t len
,
2483 const char **mbstr
, MSVCRT_size_t count
, MSVCRT_mbstate_t
*state
)
2487 if(!ret
) ret
= &tmp
;
2488 if(!MSVCRT_CHECK_PMT(!!wcstr
== !!len
)) {
2490 return MSVCRT_EINVAL
;
2493 *ret
= MSVCRT_mbsrtowcs(wcstr
, mbstr
, count
>len
? len
: count
, state
);
2495 if(wcstr
) *wcstr
= 0;
2496 return *MSVCRT__errno();
2500 /* no place for terminating '\0' */
2501 if(wcstr
) *wcstr
= 0;
2504 if(wcstr
) wcstr
[(*ret
)-1] = 0;
2508 /*********************************************************************
2509 * _mbctohira (MSVCRT.@)
2511 * Converts a sjis katakana character to hiragana.
2513 unsigned int CDECL
_mbctohira(unsigned int c
)
2515 if(_ismbckata(c
) && c
<= 0x8393)
2516 return (c
- 0x8340 - (c
>= 0x837f ? 1 : 0)) + 0x829f;
2520 /*********************************************************************
2521 * _mbctokata (MSVCRT.@)
2523 * Converts a sjis hiragana character to katakana.
2525 unsigned int CDECL
_mbctokata(unsigned int c
)
2528 return (c
- 0x829f) + 0x8340 + (c
>= 0x82de ? 1 : 0);
2533 /*********************************************************************
2534 * _ismbcl0 (MSVCRT.@)
2536 int CDECL
_ismbcl0(unsigned int c
)
2538 if(get_mbcinfo()->mbcodepage
== 932)
2541 return (c
>= 0x8140 && c
<= 0x889e);