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
;
189 /*********************************************************************
190 * ___mb_cur_max_l_func (MSVCR80.@)
192 int* CDECL
___mb_cur_max_l_func(MSVCRT__locale_t locale
)
194 MSVCRT_pthreadlocinfo locinfo
;
197 locinfo
= get_locinfo();
199 locinfo
= locale
->locinfo
;
201 return &locinfo
->mb_cur_max
;
205 /*********************************************************************
206 * INTERNAL: _setmbcp_l
208 int _setmbcp_l(int cp
, LCID lcid
, MSVCRT_pthreadmbcinfo mbcinfo
)
210 const char format
[] = ".%d";
223 mbcinfo
= get_mbcinfo();
234 newcp
= get_locinfo()->lc_codepage
;
237 /* fall through (C locale) */
239 newcp
= 20127; /* ASCII */
247 sprintf(bufA
, format
, newcp
);
248 mbcinfo
->mblcid
= MSVCRT_locale_to_LCID(bufA
, NULL
);
250 mbcinfo
->mblcid
= lcid
;
253 if(mbcinfo
->mblcid
== -1)
255 WARN("Can't assign LCID to codepage (%d)\n", mbcinfo
->mblcid
);
259 if (!GetCPInfo(newcp
, &cpi
))
261 WARN("Codepage %d not found\n", newcp
);
262 *MSVCRT__errno() = MSVCRT_EINVAL
;
266 /* setup the _mbctype */
267 memset(mbcinfo
->mbctype
, 0, sizeof(unsigned char[257]));
268 memset(mbcinfo
->mbcasemap
, 0, sizeof(unsigned char[256]));
270 bytes
= cpi
.LeadByte
;
271 while (bytes
[0] || bytes
[1])
273 for (i
= bytes
[0]; i
<= bytes
[1]; i
++)
274 mbcinfo
->mbctype
[i
+ 1] |= _M1
;
278 if (cpi
.MaxCharSize
> 1)
280 /* trail bytes not available through kernel32 but stored in a structure in msvcrt */
281 struct cp_extra_info_t
*cpextra
= g_cpextrainfo
;
283 mbcinfo
->ismbcodepage
= 1;
286 if (cpextra
->cp
== 0 || cpextra
->cp
== newcp
)
288 if (cpextra
->cp
== 0)
289 FIXME("trail bytes data not available for DBCS codepage %d - assuming all bytes\n", newcp
);
291 bytes
= cpextra
->TrailBytes
;
292 while (bytes
[0] || bytes
[1])
294 for (i
= bytes
[0]; i
<= bytes
[1]; i
++)
295 mbcinfo
->mbctype
[i
+ 1] |= _M2
;
304 mbcinfo
->ismbcodepage
= 0;
306 /* we can't use GetStringTypeA directly because we don't have a locale - only a code page
309 for (i
= 0; i
< 256; i
++)
310 if (!(mbcinfo
->mbctype
[i
+ 1] & _M1
))
311 bufA
[charcount
++] = i
;
313 ret
= MultiByteToWideChar(newcp
, 0, bufA
, charcount
, bufW
, charcount
);
314 if (ret
!= charcount
)
315 ERR("MultiByteToWideChar of chars failed for cp %d, ret=%d (exp %d), error=%d\n", newcp
, ret
, charcount
, GetLastError());
317 GetStringTypeW(CT_CTYPE1
, bufW
, charcount
, chartypes
);
320 for (i
= 0; i
< 256; i
++)
321 if (!(mbcinfo
->mbctype
[i
+ 1] & _M1
))
323 if (chartypes
[charcount
] & C1_UPPER
)
325 mbcinfo
->mbctype
[i
+ 1] |= _SBUP
;
326 bufW
[charcount
] = tolowerW(bufW
[charcount
]);
328 else if (chartypes
[charcount
] & C1_LOWER
)
330 mbcinfo
->mbctype
[i
+ 1] |= _SBLOW
;
331 bufW
[charcount
] = toupperW(bufW
[charcount
]);
336 ret
= WideCharToMultiByte(newcp
, 0, bufW
, charcount
, bufA
, charcount
, NULL
, NULL
);
337 if (ret
!= charcount
)
338 ERR("WideCharToMultiByte failed for cp %d, ret=%d (exp %d), error=%d\n", newcp
, ret
, charcount
, GetLastError());
341 for (i
= 0; i
< 256; i
++)
343 if(!(mbcinfo
->mbctype
[i
+ 1] & _M1
))
345 if(mbcinfo
->mbctype
[i
] & (C1_UPPER
|C1_LOWER
))
346 mbcinfo
->mbcasemap
[i
] = bufA
[charcount
];
351 if (newcp
== 932) /* CP932 only - set _MP and _MS */
353 /* On Windows it's possible to calculate the _MP and _MS from CT_CTYPE1
354 * and CT_CTYPE3. But as of Wine 0.9.43 we return wrong values what makes
355 * it hard. As this is set only for codepage 932 we hardcode it what gives
356 * also faster execution.
358 for (i
= 161; i
<= 165; i
++)
359 mbcinfo
->mbctype
[i
+ 1] |= _MP
;
360 for (i
= 166; i
<= 223; i
++)
361 mbcinfo
->mbctype
[i
+ 1] |= _MS
;
364 mbcinfo
->mbcodepage
= newcp
;
365 if(MSVCRT_locale
&& mbcinfo
== MSVCRT_locale
->mbcinfo
)
366 memcpy(MSVCRT_mbctype
, MSVCRT_locale
->mbcinfo
->mbctype
, sizeof(MSVCRT_mbctype
));
371 /*********************************************************************
372 * _setmbcp (MSVCRT.@)
374 int CDECL
_setmbcp(int cp
)
376 return _setmbcp_l(cp
, -1, NULL
);
379 /*********************************************************************
380 * _getmbcp (MSVCRT.@)
382 int CDECL
_getmbcp(void)
384 return get_mbcinfo()->mbcodepage
;
387 /*********************************************************************
388 * _mbsnextc(MSVCRT.@)
390 unsigned int CDECL
_mbsnextc(const unsigned char* str
)
393 return *str
<< 8 | str
[1];
397 /*********************************************************************
398 * _mbctolower(MSVCRT.@)
400 unsigned int CDECL
_mbctolower(unsigned int c
)
404 FIXME("Handle MBC chars\n");
407 return tolower(c
); /* ASCII CP or SB char */
410 /*********************************************************************
411 * _mbctoupper(MSVCRT.@)
413 unsigned int CDECL
_mbctoupper(unsigned int c
)
417 FIXME("Handle MBC chars\n");
420 return toupper(c
); /* ASCII CP or SB char */
423 /*********************************************************************
424 * _mbctombb (MSVCRT.@)
426 unsigned int CDECL
_mbctombb(unsigned int c
)
430 if(get_mbcinfo()->mbcodepage
== 932)
432 if(c
>= 0x829f && c
<= 0x82f1) /* Hiragana */
433 return mbctombb_932_kana
[c
- 0x829f];
434 if(c
>= 0x8340 && c
<= 0x8396 && c
!= 0x837f) /* Katakana */
435 return mbctombb_932_kana
[c
- 0x8340 - (c
>= 0x837f ? 1 : 0)];
436 if(c
>= 0x8140 && c
<= 0x8197) /* Punctuation */
438 value
= mbctombb_932_punct
[c
- 0x8140];
439 return value
? value
: c
;
441 if((c
>= 0x824f && c
<= 0x8258) || /* Fullwidth digits */
442 (c
>= 0x8260 && c
<= 0x8279)) /* Fullwidth capitals letters */
444 if(c
>= 0x8281 && c
<= 0x829a) /* Fullwidth small letters */
446 /* all other cases return c */
451 /*********************************************************************
452 * _mbcjistojms(MSVCRT.@)
454 * Converts a jis character to sjis.
455 * Based on description from
456 * http://www.slayers.ne.jp/~oouchi/code/jistosjis.html
458 unsigned int CDECL
_mbcjistojms(unsigned int c
)
460 /* Conversion takes place only when codepage is 932.
461 In all other cases, c is returned unchanged */
462 if(get_mbcinfo()->mbcodepage
== 932)
464 if(HIBYTE(c
) >= 0x21 && HIBYTE(c
) <= 0x7e &&
465 LOBYTE(c
) >= 0x21 && LOBYTE(c
) <= 0x7e)
472 if(LOBYTE(c
) >= 0x7F)
475 c
= (((HIBYTE(c
) - 0x21)/2 + 0x81) << 8) | LOBYTE(c
);
481 return 0; /* Codepage is 932, but c can't be converted */
487 /*********************************************************************
488 * _mbcjmstojis(MSVCRT.@)
490 * Converts a sjis character to jis.
492 unsigned int CDECL
_mbcjmstojis(unsigned int c
)
494 /* Conversion takes place only when codepage is 932.
495 In all other cases, c is returned unchanged */
496 if(get_mbcinfo()->mbcodepage
== 932)
498 if(_ismbclegal(c
) && HIBYTE(c
) < 0xf0)
500 if(HIBYTE(c
) >= 0xe0)
503 c
= (((HIBYTE(c
) - 0x81)*2 + 0x21) << 8) | LOBYTE(c
);
514 return 0; /* Codepage is 932, but c can't be converted */
520 /*********************************************************************
523 unsigned char* CDECL
_mbsdec(const unsigned char* start
, const unsigned char* cur
)
528 if(get_mbcinfo()->ismbcodepage
)
529 return (unsigned char *)(_ismbstrail(start
,cur
-1) ? cur
- 2 : cur
-1);
531 return (unsigned char *)cur
- 1; /* ASCII CP or SB char */
534 /*********************************************************************
537 unsigned int CDECL
_mbclen(const unsigned char* str
)
539 return _ismbblead(*str
) ? 2 : 1;
542 /*********************************************************************
545 unsigned char* CDECL
_mbsinc(const unsigned char* str
)
547 return (unsigned char *)(str
+ _mbclen(str
));
550 /*********************************************************************
553 unsigned char* CDECL
_mbsninc(const unsigned char* str
, MSVCRT_size_t num
)
558 while (num
> 0 && *str
)
560 if (_ismbblead(*str
))
570 return (unsigned char*)str
;
573 /*********************************************************************
574 * _mbsnlen_l(MSVCRT.@)
576 MSVCRT_size_t CDECL
_mbsnlen_l(const unsigned char *str
,
577 MSVCRT_size_t maxsize
, MSVCRT__locale_t locale
)
579 MSVCRT_pthreadmbcinfo mbcinfo
;
580 MSVCRT_size_t i
= 0, len
= 0;
583 mbcinfo
= get_mbcinfo();
585 mbcinfo
= locale
->mbcinfo
;
587 if(!mbcinfo
->ismbcodepage
)
588 return MSVCRT_strnlen((const char*)str
, maxsize
);
590 while(i
<maxsize
&& str
[i
])
592 if (_ismbblead_l(str
[i
], locale
))
595 if (!str
[i
]) /* count only full chars */
601 return i
< maxsize
? len
: maxsize
;
604 /*********************************************************************
607 MSVCRT_size_t CDECL
_mbslen(const unsigned char* str
)
609 return _mbsnlen_l(str
, -1, NULL
);
612 /*********************************************************************
613 * _mbslen_l(MSVCRT.@)
615 MSVCRT_size_t CDECL
_mbslen_l(const unsigned char* str
, MSVCRT__locale_t locale
)
617 return _mbsnlen_l(str
, -1, locale
);
620 /*********************************************************************
623 MSVCRT_size_t CDECL
_mbsnlen(const unsigned char* str
, MSVCRT_size_t maxsize
)
625 return _mbsnlen_l(str
, maxsize
, NULL
);
628 /*********************************************************************
629 * _mbccpy_s_l(MSVCRT.@)
631 int CDECL
_mbccpy_s_l(unsigned char* dest
, MSVCRT_size_t maxsize
,
632 int *copied
, const unsigned char* src
, MSVCRT__locale_t locale
)
634 if(copied
) *copied
= 0;
635 if(!MSVCRT_CHECK_PMT(dest
!= NULL
&& maxsize
>= 1)) return MSVCRT_EINVAL
;
637 if(!MSVCRT_CHECK_PMT(src
!= NULL
)) return MSVCRT_EINVAL
;
639 if(_ismbblead_l(*src
, locale
)) {
641 if(copied
) *copied
= 1;
642 *MSVCRT__errno() = MSVCRT_EILSEQ
;
643 return MSVCRT_EILSEQ
;
647 MSVCRT_INVALID_PMT("dst buffer is too small", MSVCRT_ERANGE
);
648 return MSVCRT_ERANGE
;
653 if(copied
) *copied
= 2;
656 if(copied
) *copied
= 1;
662 /*********************************************************************
665 void CDECL
_mbccpy(unsigned char* dest
, const unsigned char* src
)
667 _mbccpy_s_l(dest
, 2, NULL
, src
, NULL
);
670 /*********************************************************************
671 * _mbccpy_l(MSVCRT.@)
673 void CDECL
_mbccpy_l(unsigned char* dest
, const unsigned char* src
,
674 MSVCRT__locale_t locale
)
676 _mbccpy_s_l(dest
, 2, NULL
, src
, locale
);
679 /*********************************************************************
680 * _mbccpy_s(MSVCRT.@)
682 int CDECL
_mbccpy_s(unsigned char* dest
, MSVCRT_size_t maxsize
,
683 int *copied
, const unsigned char* src
)
685 return _mbccpy_s_l(dest
, maxsize
, copied
, src
, NULL
);
688 /*********************************************************************
691 * The parameter n is the number or characters to copy, not the size of
692 * the buffer. Use _mbsnbcpy for a function analogical to strncpy
694 unsigned char* CDECL
_mbsncpy(unsigned char* dst
, const unsigned char* src
, MSVCRT_size_t n
)
696 unsigned char* ret
= dst
;
699 if (get_mbcinfo()->ismbcodepage
)
704 if (_ismbblead(*src
))
724 if (!(*dst
++ = *src
++)) break;
727 while (n
--) *dst
++ = 0;
731 /*********************************************************************
732 * _mbsnbcpy_s_l(MSVCRT.@)
734 * Unlike _mbsnbcpy this function does not pad the rest of the dest
737 int CDECL
_mbsnbcpy_s_l(unsigned char* dst
, MSVCRT_size_t size
,
738 const unsigned char* src
, MSVCRT_size_t n
, MSVCRT__locale_t locale
)
740 MSVCRT_size_t pos
= 0;
742 if(!dst
|| size
== 0)
743 return MSVCRT_EINVAL
;
747 return MSVCRT_EINVAL
;
752 if((locale
? locale
->mbcinfo
: get_mbcinfo())->ismbcodepage
)
754 BOOL is_lead
= FALSE
;
760 return MSVCRT_ERANGE
;
762 is_lead
= (!is_lead
&& _ismbblead(*src
));
767 if (is_lead
) /* if string ends with a lead, remove it */
778 return MSVCRT_ERANGE
;
791 return MSVCRT_ERANGE
;
797 /*********************************************************************
798 * _mbsnbcpy_s(MSVCRT.@)
800 int CDECL
_mbsnbcpy_s(unsigned char* dst
, MSVCRT_size_t size
, const unsigned char* src
, MSVCRT_size_t n
)
802 return _mbsnbcpy_s_l(dst
, size
, src
, n
, NULL
);
805 /*********************************************************************
806 * _mbscpy_s_l(MSVCRT.@)
808 int CDECL
_mbscpy_s_l(unsigned char *dst
, MSVCRT_size_t size
,
809 const unsigned char *src
, MSVCRT__locale_t locale
)
811 return _mbsnbcpy_s_l(dst
, size
, src
, -1, locale
);
814 /*********************************************************************
815 * _mbscpy_s(MSVCRT.@)
817 int CDECL
_mbscpy_s(unsigned char *dst
, MSVCRT_size_t size
, const unsigned char *src
)
819 return _mbscpy_s_l(dst
, size
, src
, NULL
);
822 /*********************************************************************
823 * _mbsnbcpy(MSVCRT.@)
825 * Like strncpy this function doesn't enforce the string to be
828 unsigned char* CDECL
_mbsnbcpy(unsigned char* dst
, const unsigned char* src
, MSVCRT_size_t n
)
830 unsigned char* ret
= dst
;
833 if(get_mbcinfo()->ismbcodepage
)
835 BOOL is_lead
= FALSE
;
838 is_lead
= (!is_lead
&& _ismbblead(*src
));
843 if (is_lead
) /* if string ends with a lead, remove it */
851 if (!(*dst
++ = *src
++)) break;
854 while (n
--) *dst
++ = 0;
858 /*********************************************************************
861 int CDECL
_mbscmp(const unsigned char* str
, const unsigned char* cmp
)
863 if (!str
|| !cmp
) return INT_MAX
;
865 if(get_mbcinfo()->ismbcodepage
)
867 unsigned int strc
, cmpc
;
870 return *cmp
? -1 : 0;
873 strc
= _mbsnextc(str
);
874 cmpc
= _mbsnextc(cmp
);
876 return strc
< cmpc
? -1 : 1;
877 str
+=(strc
> 255) ? 2 : 1;
878 cmp
+=(strc
> 255) ? 2 : 1; /* equal, use same increment */
881 return u_strcmp(str
, cmp
); /* ASCII CP */
884 /*********************************************************************
885 * _mbsnbicoll_l(MSVCRT.@)
887 int CDECL
_mbsnbicoll_l(const unsigned char *str1
, const unsigned char *str2
, MSVCRT_size_t len
, MSVCRT__locale_t locale
)
889 MSVCRT_pthreadmbcinfo mbcinfo
;
892 mbcinfo
= get_mbcinfo();
894 mbcinfo
= locale
->mbcinfo
;
896 if(!mbcinfo
->ismbcodepage
)
897 return MSVCRT__strnicoll_l((const char*)str1
, (const char*)str2
, len
, locale
);
898 return CompareStringA(mbcinfo
->mblcid
, NORM_IGNORECASE
, (const char*)str1
, len
, (const char*)str2
, len
)-CSTR_EQUAL
;
901 /*********************************************************************
902 * _mbsicoll_l(MSVCRT.@)
904 int CDECL
_mbsicoll_l(const unsigned char *str1
, const unsigned char *str2
, MSVCRT__locale_t locale
)
906 return _mbsnbicoll_l(str1
, str2
, -1, locale
);
909 /*********************************************************************
910 * _mbsnbicoll(MSVCRT.@)
912 int CDECL
_mbsnbicoll(const unsigned char *str1
, const unsigned char *str2
, MSVCRT_size_t len
)
914 return _mbsnbicoll_l(str1
, str2
, len
, NULL
);
917 /*********************************************************************
918 * _mbsicoll(MSVCRT.@)
920 int CDECL
_mbsicoll(const unsigned char* str
, const unsigned char* cmp
)
922 #if _MSVCR_VER>=60 && _MSVCR_VER<=71
923 return CompareStringA(get_mbcinfo()->mblcid
, NORM_IGNORECASE
,
924 (const char*)str
, -1, (const char*)cmp
, -1)-CSTR_EQUAL
;
926 return _mbsnbicoll_l(str
, cmp
, -1, NULL
);
930 /*********************************************************************
931 * _mbsnbcoll_l(MSVCRT.@)
933 int CDECL
_mbsnbcoll_l(const unsigned char *str1
, const unsigned char *str2
, MSVCRT_size_t len
, MSVCRT__locale_t locale
)
935 MSVCRT_pthreadmbcinfo mbcinfo
;
938 mbcinfo
= get_mbcinfo();
940 mbcinfo
= locale
->mbcinfo
;
942 if(!mbcinfo
->ismbcodepage
)
943 return MSVCRT__strncoll_l((const char*)str1
, (const char*)str2
, len
, locale
);
944 return CompareStringA(mbcinfo
->mblcid
, 0, (const char*)str1
, len
, (const char*)str2
, len
)-CSTR_EQUAL
;
947 /*********************************************************************
948 * _mbscoll_l(MSVCRT.@)
950 int CDECL
_mbscoll_l(const unsigned char *str1
, const unsigned char *str2
, MSVCRT__locale_t locale
)
952 return _mbsnbcoll_l(str1
, str2
, -1, locale
);
955 /*********************************************************************
956 * _mbsnbcoll(MSVCRT.@)
958 int CDECL
_mbsnbcoll(const unsigned char *str1
, const unsigned char *str2
, MSVCRT_size_t len
)
960 return _mbsnbcoll_l(str1
, str2
, len
, NULL
);
963 /*********************************************************************
966 int CDECL
_mbscoll(const unsigned char* str
, const unsigned char* cmp
)
968 #if _MSVCR_VER>=60 && _MSVCR_VER<=71
969 return CompareStringA(get_mbcinfo()->mblcid
, 0,
970 (const char*)str
, -1, (const char*)cmp
, -1)-CSTR_EQUAL
;
972 return _mbsnbcoll_l(str
, cmp
, -1, NULL
);
976 /*********************************************************************
979 int CDECL
_mbsicmp(const unsigned char* str
, const unsigned char* cmp
)
981 if(get_mbcinfo()->ismbcodepage
)
983 unsigned int strc
, cmpc
;
986 return *cmp
? -1 : 0;
989 strc
= _mbctolower(_mbsnextc(str
));
990 cmpc
= _mbctolower(_mbsnextc(cmp
));
992 return strc
< cmpc
? -1 : 1;
993 str
+=(strc
> 255) ? 2 : 1;
994 cmp
+=(strc
> 255) ? 2 : 1; /* equal, use same increment */
997 return u_strcasecmp(str
, cmp
); /* ASCII CP */
1000 /*********************************************************************
1001 * _mbsncmp(MSVCRT.@)
1003 int CDECL
_mbsncmp(const unsigned char* str
, const unsigned char* cmp
, MSVCRT_size_t len
)
1008 if(get_mbcinfo()->ismbcodepage
)
1010 unsigned int strc
, cmpc
;
1015 return *cmp
? -1 : 0;
1018 strc
= _mbsnextc(str
);
1019 cmpc
= _mbsnextc(cmp
);
1021 return strc
< cmpc
? -1 : 1;
1022 inc
=(strc
> 255) ? 2 : 1; /* Equal, use same increment */
1026 return 0; /* Matched len chars */
1028 return u_strncmp(str
, cmp
, len
); /* ASCII CP */
1031 /*********************************************************************
1032 * _mbsnbcmp(MSVCRT.@)
1034 int CDECL
_mbsnbcmp(const unsigned char* str
, const unsigned char* cmp
, MSVCRT_size_t len
)
1038 if(get_mbcinfo()->ismbcodepage
)
1040 unsigned int strc
, cmpc
;
1045 return *cmp
? -1 : 0;
1048 if (_ismbblead(*str
))
1050 strc
=(len
>=2)?_mbsnextc(str
):0;
1058 if (_ismbblead(*cmp
))
1059 cmpc
=(len
>=2)?_mbsnextc(cmp
):0;
1063 return strc
< cmpc
? -1 : 1;
1068 return 0; /* Matched len chars */
1070 return u_strncmp(str
,cmp
,len
);
1073 /*********************************************************************
1074 * _mbsnicmp(MSVCRT.@)
1076 * Compare two multibyte strings case insensitively to 'len' characters.
1078 int CDECL
_mbsnicmp(const unsigned char* str
, const unsigned char* cmp
, MSVCRT_size_t len
)
1080 /* FIXME: No tolower() for mb strings yet */
1081 if(get_mbcinfo()->ismbcodepage
)
1083 unsigned int strc
, cmpc
;
1087 return *cmp
? -1 : 0;
1090 strc
= _mbctolower(_mbsnextc(str
));
1091 cmpc
= _mbctolower(_mbsnextc(cmp
));
1093 return strc
< cmpc
? -1 : 1;
1094 str
+=(strc
> 255) ? 2 : 1;
1095 cmp
+=(strc
> 255) ? 2 : 1; /* Equal, use same increment */
1097 return 0; /* Matched len chars */
1099 return u_strncasecmp(str
, cmp
, len
); /* ASCII CP */
1102 /*********************************************************************
1103 * _mbsnbicmp(MSVCRT.@)
1105 int CDECL
_mbsnbicmp(const unsigned char* str
, const unsigned char* cmp
, MSVCRT_size_t len
)
1109 if(get_mbcinfo()->ismbcodepage
)
1111 unsigned int strc
, cmpc
;
1116 return *cmp
? -1 : 0;
1119 if (_ismbblead(*str
))
1121 strc
=(len
>=2)?_mbsnextc(str
):0;
1129 if (_ismbblead(*cmp
))
1130 cmpc
=(len
>=2)?_mbsnextc(cmp
):0;
1133 strc
= _mbctolower(strc
);
1134 cmpc
= _mbctolower(cmpc
);
1136 return strc
< cmpc
? -1 : 1;
1141 return 0; /* Matched len bytes */
1143 return u_strncasecmp(str
,cmp
,len
);
1146 /*********************************************************************
1147 * _mbscat (MSVCRT.@)
1149 unsigned char * CDECL
_mbscat( unsigned char *dst
, const unsigned char *src
)
1151 strcat( (char *)dst
, (const char *)src
);
1155 /*********************************************************************
1156 * _mbscat_s_l (MSVCRT.@)
1158 int CDECL
_mbscat_s_l( unsigned char *dst
, MSVCRT_size_t size
,
1159 const unsigned char *src
, MSVCRT__locale_t locale
)
1164 if(!MSVCRT_CHECK_PMT(dst
!= NULL
)) return MSVCRT_EINVAL
;
1165 if(!MSVCRT_CHECK_PMT(src
!= NULL
)) return MSVCRT_EINVAL
;
1167 for(i
=0; i
<size
; i
++)
1170 MSVCRT_INVALID_PMT("dst is not NULL-terminated", MSVCRT_EINVAL
);
1171 if(size
) dst
[0] = 0;
1172 return MSVCRT_EINVAL
;
1175 if(i
&& _ismbblead_l(dst
[i
-1], locale
)) {
1176 ret
= MSVCRT_EILSEQ
;
1180 for(j
=0; src
[j
] && i
+j
<size
; j
++)
1183 MSVCRT_INVALID_PMT("dst buffer is too small", MSVCRT_ERANGE
);
1185 return MSVCRT_ERANGE
;
1188 if(j
&& _ismbblead_l(src
[j
-1], locale
)) {
1189 ret
= MSVCRT_EILSEQ
;
1197 /*********************************************************************
1198 * _mbscat_s (MSVCRT.@)
1200 int CDECL
_mbscat_s( unsigned char *dst
, MSVCRT_size_t size
, const unsigned char *src
)
1202 return _mbscat_s_l(dst
, size
, src
, NULL
);
1205 /*********************************************************************
1206 * _mbscpy (MSVCRT.@)
1208 unsigned char* CDECL
_mbscpy( unsigned char *dst
, const unsigned char *src
)
1210 strcpy( (char *)dst
, (const char *)src
);
1214 /*********************************************************************
1215 * _mbsstr (MSVCRT.@)
1217 unsigned char * CDECL
_mbsstr(const unsigned char *haystack
, const unsigned char *needle
)
1219 return (unsigned char *)strstr( (const char *)haystack
, (const char *)needle
);
1222 /*********************************************************************
1225 * Find a multibyte character in a multibyte string.
1227 unsigned char* CDECL
_mbschr(const unsigned char* s
, unsigned int x
)
1229 if(get_mbcinfo()->ismbcodepage
)
1236 return (unsigned char*)s
;
1239 s
+= c
> 255 ? 2 : 1;
1242 return u_strchr(s
, x
); /* ASCII CP */
1245 /*********************************************************************
1246 * _mbsrchr(MSVCRT.@)
1248 unsigned char* CDECL
_mbsrchr(const unsigned char* s
, unsigned int x
)
1250 if(get_mbcinfo()->ismbcodepage
)
1253 unsigned char* match
=NULL
;
1259 match
=(unsigned char*)s
;
1262 s
+=(c
> 255) ? 2 : 1;
1265 return u_strrchr(s
, x
);
1268 /*********************************************************************
1269 * _mbstok_s_l(MSVCRT.@)
1271 unsigned char* CDECL
_mbstok_s_l(unsigned char *str
, const unsigned char *delim
,
1272 unsigned char **ctx
, MSVCRT__locale_t locale
)
1274 MSVCRT_pthreadmbcinfo mbcinfo
;
1277 if(!MSVCRT_CHECK_PMT(delim
!= NULL
)) return NULL
;
1278 if(!MSVCRT_CHECK_PMT(ctx
!= NULL
)) return NULL
;
1279 if(!MSVCRT_CHECK_PMT(str
|| *ctx
)) return NULL
;
1282 mbcinfo
= locale
->mbcinfo
;
1284 mbcinfo
= get_mbcinfo();
1286 if(!mbcinfo
->ismbcodepage
)
1287 return (unsigned char*)MSVCRT_strtok_s((char*)str
, (const char*)delim
, (char**)ctx
);
1292 while((c
=_mbsnextc(str
)) && _mbschr(delim
, c
))
1293 str
+= c
>255 ? 2 : 1;
1300 *ctx
= str
+ (c
>255 ? 2 : 1);
1301 while((c
=_mbsnextc(*ctx
)) && !_mbschr(delim
, c
))
1302 *ctx
+= c
>255 ? 2 : 1;
1313 /*********************************************************************
1314 * _mbstok_s(MSVCRT.@)
1316 unsigned char* CDECL
_mbstok_s(unsigned char *str
,
1317 const unsigned char *delim
, unsigned char **ctx
)
1319 return _mbstok_s_l(str
, delim
, ctx
, NULL
);
1322 /*********************************************************************
1323 * _mbstok_l(MSVCRT.@)
1325 unsigned char* CDECL
_mbstok_l(unsigned char *str
,
1326 const unsigned char *delim
, MSVCRT__locale_t locale
)
1328 return _mbstok_s_l(str
, delim
, &msvcrt_get_thread_data()->mbstok_next
, locale
);
1331 /*********************************************************************
1334 unsigned char* CDECL
_mbstok(unsigned char *str
, const unsigned char *delim
)
1336 thread_data_t
*data
= msvcrt_get_thread_data();
1339 if(!str
&& !data
->mbstok_next
)
1343 return _mbstok_s_l(str
, delim
, &data
->mbstok_next
, NULL
);
1346 /*********************************************************************
1347 * _mbbtombc(MSVCRT.@)
1349 unsigned int CDECL
_mbbtombc(unsigned int c
)
1351 if(get_mbcinfo()->mbcodepage
== 932)
1353 if(c
>= 0x20 && c
<= 0x7e) {
1354 if((c
>= 0x41 && c
<= 0x5a) || (c
>= 0x61 && c
<= 0x7a) || (c
>= 0x30 && c
<= 0x39))
1355 return mbbtombc_932
[c
- 0x20] | 0x8200;
1357 return mbbtombc_932
[c
- 0x20] | 0x8100;
1359 else if(c
>= 0xa1 && c
<= 0xdf) {
1360 if(c
>= 0xa6 && c
<= 0xdd && c
!= 0xb0)
1361 return mbbtombc_932
[c
- 0xa1 + 0x5f] | 0x8300;
1363 return mbbtombc_932
[c
- 0xa1 + 0x5f] | 0x8100;
1366 return c
; /* not Japanese or no MB char */
1369 /*********************************************************************
1370 * _mbbtype(MSVCRT.@)
1372 int CDECL
_mbbtype(unsigned char c
, int type
)
1376 if ((c
>= 0x20 && c
<= 0x7e) || (c
>= 0xa1 && c
<= 0xdf))
1378 else if ((c
>= 0x40 && c
<= 0x7e) || (c
>= 0x80 && c
<= 0xfc))
1381 return _MBC_ILLEGAL
;
1385 if ((c
>= 0x20 && c
<= 0x7e) || (c
>= 0xa1 && c
<= 0xdf))
1387 else if ((c
>= 0x81 && c
<= 0x9f) || (c
>= 0xe0 && c
<= 0xfc))
1390 return _MBC_ILLEGAL
;
1394 /*********************************************************************
1395 * _ismbbkana(MSVCRT.@)
1397 int CDECL
_ismbbkana(unsigned int c
)
1399 /* FIXME: use lc_ctype when supported, not lc_all */
1400 if(get_mbcinfo()->mbcodepage
== 932)
1402 /* Japanese/Katakana, CP 932 */
1403 return (c
>= 0xa1 && c
<= 0xdf);
1408 /*********************************************************************
1409 * _ismbcdigit(MSVCRT.@)
1411 int CDECL
_ismbcdigit(unsigned int ch
)
1413 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1414 return (get_char_typeW( wch
) & C1_DIGIT
);
1417 /*********************************************************************
1418 * _ismbcgraph(MSVCRT.@)
1420 int CDECL
_ismbcgraph(unsigned int ch
)
1422 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1423 return (get_char_typeW( wch
) & (C1_UPPER
| C1_LOWER
| C1_DIGIT
| C1_PUNCT
| C1_ALPHA
));
1426 /*********************************************************************
1427 * _ismbcalpha (MSVCRT.@)
1429 int CDECL
_ismbcalpha(unsigned int ch
)
1431 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1432 return (get_char_typeW( wch
) & C1_ALPHA
);
1435 /*********************************************************************
1436 * _ismbclower (MSVCRT.@)
1438 int CDECL
_ismbclower(unsigned int ch
)
1440 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1441 return (get_char_typeW( wch
) & C1_UPPER
);
1444 /*********************************************************************
1445 * _ismbcupper (MSVCRT.@)
1447 int CDECL
_ismbcupper(unsigned int ch
)
1449 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1450 return (get_char_typeW( wch
) & C1_LOWER
);
1453 /*********************************************************************
1454 * _ismbcsymbol(MSVCRT.@)
1456 int CDECL
_ismbcsymbol(unsigned int ch
)
1458 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1460 if (!GetStringTypeW(CT_CTYPE3
, &wch
, 1, &ctype
))
1462 WARN("GetStringTypeW failed on %x\n", ch
);
1465 return ((ctype
& C3_SYMBOL
) != 0);
1468 /*********************************************************************
1469 * _ismbcalnum (MSVCRT.@)
1471 int CDECL
_ismbcalnum(unsigned int ch
)
1473 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1474 return (get_char_typeW( wch
) & (C1_ALPHA
| C1_DIGIT
));
1477 /*********************************************************************
1478 * _ismbcspace (MSVCRT.@)
1480 int CDECL
_ismbcspace(unsigned int ch
)
1482 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1483 return (get_char_typeW( wch
) & C1_SPACE
);
1486 /*********************************************************************
1487 * _ismbcprint (MSVCRT.@)
1489 int CDECL
_ismbcprint(unsigned int ch
)
1491 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1492 return (get_char_typeW( wch
) & (C1_UPPER
| C1_LOWER
| C1_DIGIT
| C1_PUNCT
| C1_ALPHA
| C1_SPACE
));
1495 /*********************************************************************
1496 * _ismbcpunct(MSVCRT.@)
1498 int CDECL
_ismbcpunct(unsigned int ch
)
1500 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1501 return (get_char_typeW( wch
) & C1_PUNCT
);
1504 /*********************************************************************
1505 * _ismbchira(MSVCRT.@)
1507 int CDECL
_ismbchira(unsigned int c
)
1509 if(get_mbcinfo()->mbcodepage
== 932)
1511 /* Japanese/Hiragana, CP 932 */
1512 return (c
>= 0x829f && c
<= 0x82f1);
1517 /*********************************************************************
1518 * _ismbckata(MSVCRT.@)
1520 int CDECL
_ismbckata(unsigned int c
)
1522 if(get_mbcinfo()->mbcodepage
== 932)
1524 /* Japanese/Katakana, CP 932 */
1525 return (c
>= 0x8340 && c
<= 0x8396 && c
!= 0x837f);
1531 /*********************************************************************
1532 * _ismbblead_l(MSVCRT.@)
1534 int CDECL
_ismbblead_l(unsigned int c
, MSVCRT__locale_t locale
)
1536 MSVCRT_pthreadmbcinfo mbcinfo
;
1539 mbcinfo
= get_mbcinfo();
1541 mbcinfo
= locale
->mbcinfo
;
1543 return (mbcinfo
->mbctype
[(c
&0xff) + 1] & _M1
) != 0;
1546 /*********************************************************************
1547 * _ismbblead(MSVCRT.@)
1549 int CDECL
_ismbblead(unsigned int c
)
1551 return _ismbblead_l(c
, NULL
);
1555 /*********************************************************************
1556 * _ismbbtrail(MSVCRT.@)
1558 int CDECL
_ismbbtrail(unsigned int c
)
1560 return (get_mbcinfo()->mbctype
[(c
&0xff) + 1] & _M2
) != 0;
1563 /*********************************************************************
1564 * _ismbclegal(MSVCRT.@)
1566 int CDECL
_ismbclegal(unsigned int c
)
1568 return _ismbblead(HIBYTE(c
)) && _ismbbtrail(LOBYTE(c
));
1571 /*********************************************************************
1572 * _ismbslead(MSVCRT.@)
1574 int CDECL
_ismbslead(const unsigned char* start
, const unsigned char* str
)
1578 if(!get_mbcinfo()->ismbcodepage
)
1581 /* Lead bytes can also be trail bytes so we need to analyse the string
1583 while (start
<= str
)
1587 lead
= !lead
&& _ismbblead(*start
);
1591 return lead
? -1 : 0;
1594 /*********************************************************************
1595 * _ismbstrail(MSVCRT.@)
1597 int CDECL
_ismbstrail(const unsigned char* start
, const unsigned char* str
)
1599 /* Note: this function doesn't check _ismbbtrail */
1600 if ((str
> start
) && _ismbslead(start
, str
-1))
1606 /*********************************************************************
1607 * _mbsbtype (MSVCRT.@)
1609 int CDECL
_mbsbtype(const unsigned char *str
, MSVCRT_size_t count
)
1612 const unsigned char *end
= str
+ count
;
1614 /* Lead bytes can also be trail bytes so we need to analyse the string.
1615 * Also we must return _MBC_ILLEGAL for chars past the end of the string
1617 while (str
< end
) /* Note: we skip the last byte - will check after the loop */
1620 return _MBC_ILLEGAL
;
1621 lead
= get_mbcinfo()->ismbcodepage
&& !lead
&& _ismbblead(*str
);
1626 if (_ismbbtrail(*str
))
1629 return _MBC_ILLEGAL
;
1631 if (_ismbblead(*str
))
1637 /*********************************************************************
1640 unsigned char* CDECL
_mbsset(unsigned char* str
, unsigned int c
)
1642 unsigned char* ret
= str
;
1644 if(!get_mbcinfo()->ismbcodepage
|| c
< 256)
1645 return u__strset(str
, c
); /* ASCII CP or SB char */
1647 c
&= 0xffff; /* Strip high bits */
1649 while(str
[0] && str
[1])
1655 str
[0] = '\0'; /* FIXME: OK to shorten? */
1660 /*********************************************************************
1661 * _mbsnbset(MSVCRT.@)
1663 unsigned char* CDECL
_mbsnbset(unsigned char *str
, unsigned int c
, MSVCRT_size_t len
)
1665 unsigned char *ret
= str
;
1670 if(!get_mbcinfo()->ismbcodepage
|| c
< 256)
1671 return u__strnset(str
, c
, len
); /* ASCII CP or SB char */
1673 c
&= 0xffff; /* Strip high bits */
1675 while(str
[0] && str
[1] && (len
> 1))
1683 /* as per msdn pad with a blank character */
1690 /*********************************************************************
1691 * _mbsnset(MSVCRT.@)
1693 unsigned char* CDECL
_mbsnset(unsigned char* str
, unsigned int c
, MSVCRT_size_t len
)
1695 unsigned char *ret
= str
;
1700 if(!get_mbcinfo()->ismbcodepage
|| c
< 256)
1701 return u__strnset(str
, c
, len
); /* ASCII CP or SB char */
1703 c
&= 0xffff; /* Strip high bits */
1705 while(str
[0] && str
[1] && len
--)
1711 str
[0] = '\0'; /* FIXME: OK to shorten? */
1716 /*********************************************************************
1717 * _mbsnccnt(MSVCRT.@)
1718 * 'c' is for 'character'.
1720 MSVCRT_size_t CDECL
_mbsnccnt(const unsigned char* str
, MSVCRT_size_t len
)
1723 if(get_mbcinfo()->ismbcodepage
)
1726 while(*str
&& len
-- > 0)
1728 if(_ismbblead(*str
))
1741 return min(ret
, len
); /* ASCII CP */
1744 /*********************************************************************
1745 * _mbsnbcnt(MSVCRT.@)
1746 * 'b' is for byte count.
1748 MSVCRT_size_t CDECL
_mbsnbcnt(const unsigned char* str
, MSVCRT_size_t len
)
1751 if(get_mbcinfo()->ismbcodepage
)
1753 const unsigned char* xstr
= str
;
1754 while(*xstr
&& len
-- > 0)
1756 if (_ismbblead(*xstr
++))
1762 return min(ret
, len
); /* ASCII CP */
1765 /*********************************************************************
1766 * _mbsnbcat(MSVCRT.@)
1768 unsigned char* CDECL
_mbsnbcat(unsigned char* dst
, const unsigned char* src
, MSVCRT_size_t len
)
1770 if(get_mbcinfo()->ismbcodepage
)
1772 unsigned char *res
= dst
;
1774 if (_ismbblead(*dst
++)) {
1778 /* as per msdn overwrite the lead byte in front of '\0' */
1784 while (*src
&& len
--) *dst
++ = *src
++;
1788 return u_strncat(dst
, src
, len
); /* ASCII CP */
1791 int CDECL
_mbsnbcat_s(unsigned char *dst
, MSVCRT_size_t size
, const unsigned char *src
, MSVCRT_size_t len
)
1793 unsigned char *ptr
= dst
;
1796 if (!dst
&& !size
&& !src
&& !len
)
1799 if (!dst
|| !size
|| !src
)
1804 *MSVCRT__errno() = MSVCRT_EINVAL
;
1805 return MSVCRT_EINVAL
;
1808 /* Find the null terminator of the destination buffer. */
1809 while (size
&& *ptr
)
1815 *MSVCRT__errno() = MSVCRT_EINVAL
;
1816 return MSVCRT_EINVAL
;
1819 /* If necessary, check that the character preceding the null terminator is
1820 * a lead byte and move the pointer back by one for later overwrite. */
1821 if (ptr
!= dst
&& get_mbcinfo()->ismbcodepage
&& _ismbblead(*(ptr
- 1)))
1824 for (i
= 0; *src
&& i
< len
; i
++)
1832 *MSVCRT__errno() = MSVCRT_ERANGE
;
1833 return MSVCRT_ERANGE
;
1841 /*********************************************************************
1842 * _mbsncat(MSVCRT.@)
1844 unsigned char* CDECL
_mbsncat(unsigned char* dst
, const unsigned char* src
, MSVCRT_size_t len
)
1846 if(get_mbcinfo()->ismbcodepage
)
1848 unsigned char *res
= dst
;
1851 if (_ismbblead(*dst
++))
1854 while (*src
&& len
--)
1857 if(_ismbblead(*src
++))
1863 return u_strncat(dst
, src
, len
); /* ASCII CP */
1867 /*********************************************************************
1870 unsigned char* CDECL
_mbslwr(unsigned char* s
)
1872 unsigned char *ret
= s
;
1875 if (get_mbcinfo()->ismbcodepage
)
1880 c
= _mbctolower(_mbsnextc(s
));
1881 /* Note that I assume that the size of the character is unchanged */
1890 else for ( ; *s
; s
++) *s
= tolower(*s
);
1895 /*********************************************************************
1896 * _mbslwr_s(MSVCRT.@)
1898 int CDECL
_mbslwr_s(unsigned char* s
, MSVCRT_size_t len
)
1904 else if (!s
|| !len
)
1906 *MSVCRT__errno() = MSVCRT_EINVAL
;
1907 return MSVCRT_EINVAL
;
1909 if (get_mbcinfo()->ismbcodepage
)
1912 for ( ; *s
&& len
> 0; len
--)
1914 c
= _mbctolower(_mbsnextc(s
));
1915 /* Note that I assume that the size of the character is unchanged */
1924 else for ( ; *s
&& len
> 0; s
++, len
--) *s
= tolower(*s
);
1928 *MSVCRT__errno() = MSVCRT_EINVAL
;
1929 return MSVCRT_EINVAL
;
1935 /*********************************************************************
1938 unsigned char* CDECL
_mbsupr(unsigned char* s
)
1940 unsigned char *ret
= s
;
1943 if (get_mbcinfo()->ismbcodepage
)
1948 c
= _mbctoupper(_mbsnextc(s
));
1949 /* Note that I assume that the size of the character is unchanged */
1958 else for ( ; *s
; s
++) *s
= toupper(*s
);
1963 /*********************************************************************
1964 * _mbsupr_s(MSVCRT.@)
1966 int CDECL
_mbsupr_s(unsigned char* s
, MSVCRT_size_t len
)
1972 else if (!s
|| !len
)
1974 *MSVCRT__errno() = MSVCRT_EINVAL
;
1975 return MSVCRT_EINVAL
;
1977 if (get_mbcinfo()->ismbcodepage
)
1980 for ( ; *s
&& len
> 0; len
--)
1982 c
= _mbctoupper(_mbsnextc(s
));
1983 /* Note that I assume that the size of the character is unchanged */
1992 else for ( ; *s
&& len
> 0; s
++, len
--) *s
= toupper(*s
);
1996 *MSVCRT__errno() = MSVCRT_EINVAL
;
1997 return MSVCRT_EINVAL
;
2003 /*********************************************************************
2004 * _mbsspn (MSVCRT.@)
2006 MSVCRT_size_t CDECL
_mbsspn(const unsigned char* string
, const unsigned char* set
)
2008 const unsigned char *p
, *q
;
2010 for (p
= string
; *p
; p
++)
2014 for (q
= set
; *q
; q
++)
2018 if ((*p
== *q
) && (p
[1] == q
[1]))
2022 if (!q
[0] || !q
[1]) break;
2026 for (q
= set
; *q
; q
++)
2035 /*********************************************************************
2036 * _mbsspnp (MSVCRT.@)
2038 unsigned char* CDECL
_mbsspnp(const unsigned char* string
, const unsigned char* set
)
2040 const unsigned char *p
, *q
;
2042 for (p
= string
; *p
; p
++)
2046 for (q
= set
; *q
; q
++)
2050 if ((*p
== *q
) && (p
[1] == q
[1]))
2054 if (!q
[0] || !q
[1]) break;
2058 for (q
= set
; *q
; q
++)
2066 return (unsigned char *)p
;
2069 /*********************************************************************
2070 * _mbscspn(MSVCRT.@)
2072 MSVCRT_size_t CDECL
_mbscspn(const unsigned char* str
, const unsigned char* cmp
)
2074 if (get_mbcinfo()->ismbcodepage
)
2075 FIXME("don't handle double character case\n");
2076 return u_strcspn(str
, cmp
);
2079 /*********************************************************************
2080 * _mbsrev (MSVCRT.@)
2082 unsigned char* CDECL
_mbsrev(unsigned char* str
)
2084 int i
, len
= _mbslen(str
);
2085 unsigned char *p
, *temp
=MSVCRT_malloc(len
*2);
2090 /* unpack multibyte string to temp buffer */
2092 for(i
=0; i
<len
; i
++)
2106 /* repack it in the reverse order */
2108 for(i
=len
-1; i
>=0; i
--)
2110 if(_ismbblead(temp
[i
*2]))
2126 /*********************************************************************
2127 * _mbspbrk (MSVCRT.@)
2129 unsigned char* CDECL
_mbspbrk(const unsigned char* str
, const unsigned char* accept
)
2131 const unsigned char* p
;
2135 for(p
= accept
; *p
; p
+= (_ismbblead(*p
)?2:1) )
2138 if( !_ismbblead(*p
) || ( *(p
+1) == *(str
+1) ) )
2139 return (unsigned char*)str
;
2141 str
+= (_ismbblead(*str
)?2:1);
2148 * Functions depending on locale codepage
2151 /*********************************************************************
2154 * Unlike most of the multibyte string functions this function uses
2155 * the locale codepage, not the codepage set by _setmbcp
2157 int CDECL
MSVCRT_mblen(const char* str
, MSVCRT_size_t size
)
2159 if (str
&& *str
&& size
)
2161 if(get_locinfo()->mb_cur_max
== 1)
2162 return 1; /* ASCII CP */
2164 return !MSVCRT_isleadbyte((unsigned char)*str
) ? 1 : (size
>1 ? 2 : -1);
2169 /*********************************************************************
2172 MSVCRT_size_t CDECL
MSVCRT_mbrlen(const char *str
, MSVCRT_size_t len
, MSVCRT_mbstate_t
*state
)
2174 MSVCRT_mbstate_t s
= (state
? *state
: 0);
2177 if(!len
|| !str
|| !*str
)
2180 if(get_locinfo()->mb_cur_max
== 1) {
2182 }else if(!s
&& MSVCRT_isleadbyte((unsigned char)*str
)) {
2184 s
= (unsigned char)*str
;
2201 /*********************************************************************
2202 * _mbstrlen_l(MSVCRT.@)
2204 MSVCRT_size_t CDECL
_mbstrlen_l(const char* str
, MSVCRT__locale_t locale
)
2206 MSVCRT_pthreadlocinfo locinfo
;
2209 locinfo
= get_locinfo();
2211 locinfo
= locale
->locinfo
;
2213 if(locinfo
->mb_cur_max
> 1) {
2215 len
= MultiByteToWideChar(locinfo
->lc_codepage
, MB_ERR_INVALID_CHARS
,
2218 *MSVCRT__errno() = MSVCRT_EILSEQ
;
2227 /*********************************************************************
2228 * _mbstrlen(MSVCRT.@)
2230 MSVCRT_size_t CDECL
_mbstrlen(const char* str
)
2232 return _mbstrlen_l(str
, NULL
);
2235 /*********************************************************************
2236 * _mbtowc_l(MSVCRT.@)
2238 int CDECL
MSVCRT_mbtowc_l(MSVCRT_wchar_t
*dst
, const char* str
, MSVCRT_size_t n
, MSVCRT__locale_t locale
)
2240 MSVCRT_pthreadlocinfo locinfo
;
2241 MSVCRT_wchar_t tmpdst
= '\0';
2244 locinfo
= get_locinfo();
2246 locinfo
= locale
->locinfo
;
2256 if(!locinfo
->lc_codepage
) {
2257 if(dst
) *dst
= (unsigned char)*str
;
2260 if(n
>=2 && MSVCRT__isleadbyte_l((unsigned char)*str
, locale
)) {
2261 if(!MultiByteToWideChar(locinfo
->lc_codepage
, 0, str
, 2, &tmpdst
, 1))
2263 if(dst
) *dst
= tmpdst
;
2266 if(!MultiByteToWideChar(locinfo
->lc_codepage
, 0, str
, 1, &tmpdst
, 1))
2268 if(dst
) *dst
= tmpdst
;
2272 /*********************************************************************
2275 int CDECL
MSVCRT_mbtowc(MSVCRT_wchar_t
*dst
, const char* str
, MSVCRT_size_t n
)
2277 return MSVCRT_mbtowc_l(dst
, str
, n
, NULL
);
2280 /*********************************************************************
2283 MSVCRT_size_t CDECL
MSVCRT_mbrtowc(MSVCRT_wchar_t
*dst
, const char *str
,
2284 MSVCRT_size_t n
, MSVCRT_mbstate_t
*state
)
2286 MSVCRT_pthreadlocinfo locinfo
= get_locinfo();
2287 MSVCRT_mbstate_t s
= (state
? *state
: 0);
2294 if(!n
|| !str
|| !*str
)
2297 if(locinfo
->mb_cur_max
== 1) {
2298 tmpstr
[len
++] = *str
;
2299 }else if(!s
&& MSVCRT_isleadbyte((unsigned char)*str
)) {
2301 s
= (unsigned char)*str
;
2309 tmpstr
[len
++] = *str
;
2318 if(!MultiByteToWideChar(locinfo
->lc_codepage
, 0, tmpstr
, len
, dst
, dst
? 1 : 0))
2327 /*********************************************************************
2328 * _mbstowcs_l(MSVCRT.@)
2330 MSVCRT_size_t CDECL
MSVCRT__mbstowcs_l(MSVCRT_wchar_t
*wcstr
, const char *mbstr
,
2331 MSVCRT_size_t count
, MSVCRT__locale_t locale
)
2333 MSVCRT_pthreadlocinfo locinfo
;
2334 MSVCRT_size_t i
, size
;
2337 *MSVCRT__errno() = MSVCRT_EINVAL
;
2342 locinfo
= get_locinfo();
2344 locinfo
= locale
->locinfo
;
2346 if(!locinfo
->lc_codepage
) {
2348 return strlen(mbstr
);
2350 for(i
=0; i
<count
; i
++) {
2351 wcstr
[i
] = (unsigned char)mbstr
[i
];
2352 if(!wcstr
[i
]) break;
2357 /* Ignore count parameter */
2359 return MultiByteToWideChar(locinfo
->lc_codepage
, 0, mbstr
, -1, NULL
, 0)-1;
2361 for(i
=0, size
=0; i
<count
; i
++) {
2362 if(mbstr
[size
] == '\0')
2365 size
+= (MSVCRT__isleadbyte_l((unsigned char)mbstr
[size
], locale
) ? 2 : 1);
2369 size
= MultiByteToWideChar(locinfo
->lc_codepage
, 0,
2370 mbstr
, size
, wcstr
, count
);
2372 if(count
) wcstr
[0] = '\0';
2373 *MSVCRT__errno() = MSVCRT_EILSEQ
;
2378 if(size
<count
&& wcstr
)
2384 /*********************************************************************
2385 * mbstowcs(MSVCRT.@)
2387 MSVCRT_size_t CDECL
MSVCRT_mbstowcs(MSVCRT_wchar_t
*wcstr
,
2388 const char *mbstr
, MSVCRT_size_t count
)
2390 return MSVCRT__mbstowcs_l(wcstr
, mbstr
, count
, NULL
);
2393 /*********************************************************************
2394 * _mbstowcs_s_l(MSVCRT.@)
2396 int CDECL
MSVCRT__mbstowcs_s_l(MSVCRT_size_t
*ret
, MSVCRT_wchar_t
*wcstr
,
2397 MSVCRT_size_t size
, const char *mbstr
,
2398 MSVCRT_size_t count
, MSVCRT__locale_t locale
)
2402 if(!wcstr
&& !size
) {
2403 conv
= MSVCRT__mbstowcs_l(NULL
, mbstr
, 0, locale
);
2409 if (!MSVCRT_CHECK_PMT(wcstr
!= NULL
)) return MSVCRT_EINVAL
;
2410 if (!MSVCRT_CHECK_PMT(mbstr
!= NULL
)) {
2411 if(size
) wcstr
[0] = '\0';
2412 return MSVCRT_EINVAL
;
2415 if(count
==MSVCRT__TRUNCATE
|| size
<count
)
2420 conv
= MSVCRT__mbstowcs_l(wcstr
, mbstr
, conv
, locale
);
2422 wcstr
[conv
++] = '\0';
2423 else if(conv
==size
&& (count
==MSVCRT__TRUNCATE
|| wcstr
[conv
-1]=='\0'))
2424 wcstr
[conv
-1] = '\0';
2426 MSVCRT_INVALID_PMT("wcstr[size] is too small", MSVCRT_ERANGE
);
2429 return MSVCRT_ERANGE
;
2437 /*********************************************************************
2438 * mbstowcs_s(MSVCRT.@)
2440 int CDECL
MSVCRT__mbstowcs_s(MSVCRT_size_t
*ret
, MSVCRT_wchar_t
*wcstr
,
2441 MSVCRT_size_t size
, const char *mbstr
, MSVCRT_size_t count
)
2443 return MSVCRT__mbstowcs_s_l(ret
, wcstr
, size
, mbstr
, count
, NULL
);
2446 /*********************************************************************
2447 * mbsrtowcs(MSVCRT.@)
2449 MSVCRT_size_t CDECL
MSVCRT_mbsrtowcs(MSVCRT_wchar_t
*wcstr
,
2450 const char **pmbstr
, MSVCRT_size_t count
, MSVCRT_mbstate_t
*state
)
2452 MSVCRT_mbstate_t s
= (state
? *state
: 0);
2453 MSVCRT_wchar_t tmpdst
;
2454 MSVCRT_size_t ret
= 0;
2457 if(!MSVCRT_CHECK_PMT(pmbstr
!= NULL
))
2461 while(!wcstr
|| count
>ret
) {
2462 int ch_len
= MSVCRT_mbrtowc(&tmpdst
, p
, 2, &s
);
2464 wcstr
[ret
] = tmpdst
;
2468 }else if(ch_len
== 0) {
2469 if(wcstr
) *pmbstr
= NULL
;
2477 if(wcstr
) *pmbstr
= p
;
2481 /*********************************************************************
2482 * mbsrtowcs_s(MSVCRT.@)
2484 int CDECL
MSVCRT_mbsrtowcs_s(MSVCRT_size_t
*ret
, MSVCRT_wchar_t
*wcstr
, MSVCRT_size_t len
,
2485 const char **mbstr
, MSVCRT_size_t count
, MSVCRT_mbstate_t
*state
)
2489 if(!ret
) ret
= &tmp
;
2490 if(!MSVCRT_CHECK_PMT(!!wcstr
== !!len
)) {
2492 return MSVCRT_EINVAL
;
2495 *ret
= MSVCRT_mbsrtowcs(wcstr
, mbstr
, count
>len
? len
: count
, state
);
2497 if(wcstr
) *wcstr
= 0;
2498 return *MSVCRT__errno();
2502 /* no place for terminating '\0' */
2503 if(wcstr
) *wcstr
= 0;
2506 if(wcstr
) wcstr
[(*ret
)-1] = 0;
2510 /*********************************************************************
2511 * _mbctohira (MSVCRT.@)
2513 * Converts a sjis katakana character to hiragana.
2515 unsigned int CDECL
_mbctohira(unsigned int c
)
2517 if(_ismbckata(c
) && c
<= 0x8393)
2518 return (c
- 0x8340 - (c
>= 0x837f ? 1 : 0)) + 0x829f;
2522 /*********************************************************************
2523 * _mbctokata (MSVCRT.@)
2525 * Converts a sjis hiragana character to katakana.
2527 unsigned int CDECL
_mbctokata(unsigned int c
)
2530 return (c
- 0x829f) + 0x8340 + (c
>= 0x82de ? 1 : 0);
2535 /*********************************************************************
2536 * _ismbcl0 (MSVCRT.@)
2538 int CDECL
_ismbcl0(unsigned int c
)
2540 if(get_mbcinfo()->mbcodepage
== 932)
2543 return (c
>= 0x8140 && c
<= 0x889e);