msvcrt: Remove MSVCRT_ prefix from string.c functions.
[wine.git] / dlls / msvcrt / mbcs.c
blob11a1be297054eeae18a342e5a6bd9986c6ce367b
1 /*
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
21 * FIXME
22 * Not currently binary compatible with win32. MSVCRT_mbctype must be
23 * populated correctly and the ismb* functions should reference it.
26 #include <stdio.h>
27 #include <limits.h>
28 #include <mbctype.h>
30 #include "msvcrt.h"
31 #include "mtdll.h"
32 #include "winnls.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
37 unsigned char MSVCRT_mbctype[257] = { 0 };
39 /* It seems that the data about valid trail bytes is not available from kernel32
40 * so we have to store is here. The format is the same as for lead bytes in CPINFO */
41 struct cp_extra_info_t
43 int cp;
44 BYTE TrailBytes[MAX_LEADBYTES];
47 static struct cp_extra_info_t g_cpextrainfo[] =
49 {932, {0x40, 0x7e, 0x80, 0xfc, 0, 0}},
50 {936, {0x40, 0xfe, 0, 0}},
51 {949, {0x41, 0xfe, 0, 0}},
52 {950, {0x40, 0x7e, 0xa1, 0xfe, 0, 0}},
53 {1361, {0x31, 0x7e, 0x81, 0xfe, 0, 0}},
54 {20932, {1, 255, 0, 0}}, /* seems to give different results on different systems */
55 {0, {1, 255, 0, 0}} /* match all with FIXME */
58 /* Maps cp932 single byte character to multi byte character */
59 static const unsigned char mbbtombc_932[] = {
60 0x40,0x49,0x68,0x94,0x90,0x93,0x95,0x66,0x69,0x6a,0x96,0x7b,0x43,0x7c,0x44,0x5e,
61 0x4f,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x46,0x47,0x83,0x81,0x84,0x48,
62 0x97,0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,
63 0x6f,0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x6d,0x8f,0x6e,0x4f,0x76,
64 0x77,0x78,0x79,0x6d,0x8f,0x6e,0x4f,0x51,0x65,0x81,0x82,0x83,0x84,0x85,0x86,0x87,
65 0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f,0x90,0x91,0x92,0x93,0x94,0x95,0x50,
66 0x42,0x75,0x76,0x41,0x45,0x92,0x40,0x42,0x44,0x46,0x48,0x83,0x85,0x87,0x62,
67 0x5b,0x41,0x43,0x45,0x47,0x49,0x4a,0x4c,0x4e,0x50,0x52,0x54,0x56,0x58,0x5a,0x5c,
68 0x5e,0x60,0x63,0x65,0x67,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x71,0x74,0x77,0x7a,0x7d,
69 0x7e,0x80,0x81,0x82,0x84,0x86,0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8f,0x93,0x4a,0x4b };
71 /* Maps multibyte cp932 punctuation marks to single byte equivalents */
72 static const unsigned char mbctombb_932_punct[] = {
73 0x20,0xa4,0xa1,0x2c,0x2e,0xa5,0x3a,0x3b,0x3f,0x21,0xde,0xdf,0x00,0x00,0x00,0x5e,
74 0x7e,0x5f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xb0,0x00,0x00,0x2f,0x00,
75 0x00,0x00,0x7c,0x00,0x00,0x60,0x27,0x00,0x22,0x28,0x29,0x00,0x00,0x5b,0x5d,0x7b,
76 0x7d,0x00,0x00,0x00,0x00,0xa2,0xa3,0x00,0x00,0x00,0x00,0x2b,0x2d,0x00,0x00,0x00,
77 0x00,0x3d,0x00,0x3c,0x3e,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x5c,
78 0x24,0x00,0x00,0x25,0x23,0x26,0x2a,0x40};
80 /* Maps multibyte cp932 hiragana/katakana to single-byte equivalents */
81 static const unsigned char mbctombb_932_kana[] = {
82 0xa7,0xb1,0xa8,0xb2,0xa9,0xb3,0xaa,0xb4,0xab,0xb5,0xb6,0xb6,0xb7,0xb7,0xb8,0xb8,
83 0xb9,0xb9,0xba,0xba,0xbb,0xbb,0xbc,0xbc,0xbd,0xbd,0xbe,0xbe,0xbf,0xbf,0xc0,0xc0,
84 0xc1,0xc1,0xaf,0xc2,0xc2,0xc3,0xc3,0xc4,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xca,
85 0xca,0xcb,0xcb,0xcb,0xcc,0xcc,0xcc,0xcd,0xcd,0xcd,0xce,0xce,0xce,0xcf,0xd0,0xd1,
86 0xd2,0xd3,0xac,0xd4,0xad,0xd5,0xae,0xd6,0xd7,0xd8,0xd9,0xda,0xdb,0xdc,0xdc,0xb2,
87 0xb4,0xa6,0xdd,0xb3,0xb6,0xb9};
89 static wchar_t msvcrt_mbc_to_wc_l(unsigned int ch, _locale_t locale)
91 pthreadmbcinfo mbcinfo;
92 wchar_t chW;
93 char mbch[2];
94 int n_chars;
96 if(locale)
97 mbcinfo = locale->mbcinfo;
98 else
99 mbcinfo = get_mbcinfo();
101 if (ch <= 0xff) {
102 mbch[0] = ch;
103 n_chars = 1;
104 } else {
105 mbch[0] = (ch >> 8) & 0xff;
106 mbch[1] = ch & 0xff;
107 n_chars = 2;
109 if (!MultiByteToWideChar(mbcinfo->mbcodepage, 0, mbch, n_chars, &chW, 1))
111 WARN("MultiByteToWideChar failed on %x\n", ch);
112 return 0;
114 return chW;
117 static wchar_t msvcrt_mbc_to_wc(unsigned int ch)
119 return msvcrt_mbc_to_wc_l(ch, NULL);
122 static inline size_t u_strlen( const unsigned char *str )
124 return strlen( (const char*) str );
127 static inline unsigned char* u_strncat( unsigned char* dst, const unsigned char* src, size_t len )
129 return (unsigned char*)strncat( (char*)dst, (const char*)src, len);
132 static inline int u_strcmp( const unsigned char *s1, const unsigned char *s2 )
134 return strcmp( (const char*)s1, (const char*)s2 );
137 static inline int u_strcasecmp( const unsigned char *s1, const unsigned char *s2 )
139 return _stricmp( (const char*)s1, (const char*)s2 );
142 static inline int u_strncmp( const unsigned char *s1, const unsigned char *s2, size_t len )
144 return strncmp( (const char*)s1, (const char*)s2, len );
147 static inline int u_strncasecmp( const unsigned char *s1, const unsigned char *s2, size_t len )
149 return _strnicmp( (const char*)s1, (const char*)s2, len );
152 static inline unsigned char *u_strchr( const unsigned char *s, unsigned char x )
154 return (unsigned char*) strchr( (const char*)s, x );
157 static inline unsigned char *u_strrchr( const unsigned char *s, unsigned char x )
159 return (unsigned char*) strrchr( (const char*)s, x );
162 static inline unsigned char *u__strset( unsigned char *s, unsigned char c )
164 return (unsigned char*) _strset( (char*)s, c);
167 static inline unsigned char *u__strnset( unsigned char *s, unsigned char c, size_t len )
169 return (unsigned char*) _strnset( (char*)s, c, len );
172 /*********************************************************************
173 * __p__mbctype (MSVCRT.@)
175 unsigned char* CDECL __p__mbctype(void)
177 return get_mbcinfo()->mbctype;
180 /*********************************************************************
181 * __p___mb_cur_max(MSVCRT.@)
183 int* CDECL __p___mb_cur_max(void)
185 return &get_locinfo()->mb_cur_max;
188 /*********************************************************************
189 * ___mb_cur_max_func(MSVCRT.@)
191 int CDECL ___mb_cur_max_func(void)
193 return get_locinfo()->mb_cur_max;
196 #if _MSVCR_VER>=80
197 /*********************************************************************
198 * ___mb_cur_max_l_func (MSVCR80.@)
200 int CDECL ___mb_cur_max_l_func(_locale_t locale)
202 pthreadlocinfo locinfo;
204 if(!locale)
205 locinfo = get_locinfo();
206 else
207 locinfo = locale->locinfo;
209 return locinfo->mb_cur_max;
211 #endif
213 threadmbcinfo* create_mbcinfo(int cp, LCID lcid, threadmbcinfo *old_mbcinfo)
215 threadmbcinfo *mbcinfo;
216 int newcp;
217 CPINFO cpi;
218 BYTE *bytes;
219 WORD chartypes[256];
220 char bufA[256];
221 WCHAR bufW[256], lowW[256], upW[256];
222 int charcount;
223 int ret;
224 int i;
226 if(old_mbcinfo && cp==old_mbcinfo->mbcodepage
227 && (lcid==-1 || lcid==old_mbcinfo->mblcid)) {
228 InterlockedIncrement(&old_mbcinfo->refcount);
229 return old_mbcinfo;
232 mbcinfo = malloc(sizeof(threadmbcinfo));
233 if(!mbcinfo)
234 return NULL;
235 mbcinfo->refcount = 1;
237 switch (cp)
239 case _MB_CP_ANSI:
240 newcp = GetACP();
241 break;
242 case _MB_CP_OEM:
243 newcp = GetOEMCP();
244 break;
245 case _MB_CP_LOCALE:
246 newcp = get_locinfo()->lc_codepage;
247 if(newcp)
248 break;
249 /* fall through (C locale) */
250 case _MB_CP_SBCS:
251 newcp = 20127; /* ASCII */
252 break;
253 default:
254 newcp = cp;
255 break;
258 if(lcid == -1) {
259 MSVCRT_sprintf(bufA, ".%d", newcp);
260 mbcinfo->mblcid = locale_to_LCID(bufA, NULL, NULL);
261 } else {
262 mbcinfo->mblcid = lcid;
265 if(mbcinfo->mblcid == -1)
267 WARN("Can't assign LCID to codepage (%d)\n", mbcinfo->mblcid);
268 mbcinfo->mblcid = 0;
271 if (!GetCPInfo(newcp, &cpi))
273 WARN("Codepage %d not found\n", newcp);
274 free(mbcinfo);
275 return NULL;
278 /* setup the _mbctype */
279 memset(mbcinfo->mbctype, 0, sizeof(unsigned char[257]));
280 memset(mbcinfo->mbcasemap, 0, sizeof(unsigned char[256]));
282 bytes = cpi.LeadByte;
283 while (bytes[0] || bytes[1])
285 for (i = bytes[0]; i <= bytes[1]; i++)
286 mbcinfo->mbctype[i + 1] |= _M1;
287 bytes += 2;
290 if (cpi.MaxCharSize > 1)
292 /* trail bytes not available through kernel32 but stored in a structure in msvcrt */
293 struct cp_extra_info_t *cpextra = g_cpextrainfo;
295 mbcinfo->ismbcodepage = 1;
296 while (TRUE)
298 if (cpextra->cp == 0 || cpextra->cp == newcp)
300 if (cpextra->cp == 0)
301 FIXME("trail bytes data not available for DBCS codepage %d - assuming all bytes\n", newcp);
303 bytes = cpextra->TrailBytes;
304 while (bytes[0] || bytes[1])
306 for (i = bytes[0]; i <= bytes[1]; i++)
307 mbcinfo->mbctype[i + 1] |= _M2;
308 bytes += 2;
310 break;
312 cpextra++;
315 else
316 mbcinfo->ismbcodepage = 0;
318 /* we can't use GetStringTypeA directly because we don't have a locale - only a code page
320 charcount = 0;
321 for (i = 0; i < 256; i++)
322 if (!(mbcinfo->mbctype[i + 1] & _M1))
323 bufA[charcount++] = i;
325 ret = MultiByteToWideChar(newcp, 0, bufA, charcount, bufW, charcount);
326 if (ret != charcount)
327 ERR("MultiByteToWideChar of chars failed for cp %d, ret=%d (exp %d), error=%d\n", newcp, ret, charcount, GetLastError());
329 GetStringTypeW(CT_CTYPE1, bufW, charcount, chartypes);
330 LCMapStringW(lcid, LCMAP_LOWERCASE, bufW, charcount, lowW, charcount);
331 LCMapStringW(lcid, LCMAP_UPPERCASE, bufW, charcount, upW, charcount);
333 charcount = 0;
334 for (i = 0; i < 256; i++)
335 if (!(mbcinfo->mbctype[i + 1] & _M1))
337 if (chartypes[charcount] & C1_UPPER)
339 mbcinfo->mbctype[i + 1] |= _SBUP;
340 bufW[charcount] = lowW[charcount];
342 else if (chartypes[charcount] & C1_LOWER)
344 mbcinfo->mbctype[i + 1] |= _SBLOW;
345 bufW[charcount] = upW[charcount];
347 charcount++;
350 ret = WideCharToMultiByte(newcp, 0, bufW, charcount, bufA, charcount, NULL, NULL);
351 if (ret != charcount)
352 ERR("WideCharToMultiByte failed for cp %d, ret=%d (exp %d), error=%d\n", newcp, ret, charcount, GetLastError());
354 charcount = 0;
355 for (i = 0; i < 256; i++)
357 if(!(mbcinfo->mbctype[i + 1] & _M1))
359 if(mbcinfo->mbctype[i] & (C1_UPPER|C1_LOWER))
360 mbcinfo->mbcasemap[i] = bufA[charcount];
361 charcount++;
365 if (newcp == 932) /* CP932 only - set _MP and _MS */
367 /* On Windows it's possible to calculate the _MP and _MS from CT_CTYPE1
368 * and CT_CTYPE3. But as of Wine 0.9.43 we return wrong values what makes
369 * it hard. As this is set only for codepage 932 we hardcode it what gives
370 * also faster execution.
372 for (i = 161; i <= 165; i++)
373 mbcinfo->mbctype[i + 1] |= _MP;
374 for (i = 166; i <= 223; i++)
375 mbcinfo->mbctype[i + 1] |= _MS;
378 mbcinfo->mbcodepage = newcp;
379 return mbcinfo;
382 /*********************************************************************
383 * _setmbcp (MSVCRT.@)
385 int CDECL _setmbcp(int cp)
387 thread_data_t *data = msvcrt_get_thread_data();
388 threadmbcinfo *mbcinfo;
390 mbcinfo = create_mbcinfo(cp, -1, get_mbcinfo());
391 if(!mbcinfo)
393 *_errno() = EINVAL;
394 return -1;
397 if(data->locale_flags & LOCALE_THREAD)
399 if(data->locale_flags & LOCALE_FREE)
400 free_mbcinfo(data->mbcinfo);
401 data->mbcinfo = mbcinfo;
403 else
405 _lock(_MB_CP_LOCK);
406 free_mbcinfo(MSVCRT_locale->mbcinfo);
407 MSVCRT_locale->mbcinfo = mbcinfo;
408 memcpy(MSVCRT_mbctype, MSVCRT_locale->mbcinfo->mbctype, sizeof(MSVCRT_mbctype));
409 _unlock(_MB_CP_LOCK);
411 return 0;
414 /*********************************************************************
415 * _getmbcp (MSVCRT.@)
417 int CDECL _getmbcp(void)
419 return get_mbcinfo()->mbcodepage;
422 /*********************************************************************
423 * _mbsnextc_l(MSVCRT.@)
425 unsigned int CDECL _mbsnextc_l(const unsigned char* str, _locale_t locale)
427 if(_ismbblead_l(*str, locale))
428 return *str << 8 | str[1];
429 return *str;
432 /*********************************************************************
433 * _mbsnextc(MSVCRT.@)
435 unsigned int CDECL _mbsnextc(const unsigned char* str)
437 return _mbsnextc_l(str, NULL);
440 /*********************************************************************
441 * _mbctolower(MSVCRT.@)
443 unsigned int CDECL _mbctolower(unsigned int c)
445 if (_ismbblead(c))
447 FIXME("Handle MBC chars\n");
448 return c;
450 return _tolower_l(c, NULL); /* ASCII CP or SB char */
453 /*********************************************************************
454 * _mbctoupper(MSVCRT.@)
456 unsigned int CDECL _mbctoupper(unsigned int c)
458 if (_ismbblead(c))
460 FIXME("Handle MBC chars\n");
461 return c;
463 return _toupper_l(c, NULL); /* ASCII CP or SB char */
466 /*********************************************************************
467 * _mbctombb (MSVCRT.@)
469 unsigned int CDECL _mbctombb(unsigned int c)
471 unsigned int value;
473 if(get_mbcinfo()->mbcodepage == 932)
475 if(c >= 0x829f && c <= 0x82f1) /* Hiragana */
476 return mbctombb_932_kana[c - 0x829f];
477 if(c >= 0x8340 && c <= 0x8396 && c != 0x837f) /* Katakana */
478 return mbctombb_932_kana[c - 0x8340 - (c >= 0x837f ? 1 : 0)];
479 if(c >= 0x8140 && c <= 0x8197) /* Punctuation */
481 value = mbctombb_932_punct[c - 0x8140];
482 return value ? value : c;
484 if((c >= 0x824f && c <= 0x8258) || /* Fullwidth digits */
485 (c >= 0x8260 && c <= 0x8279)) /* Fullwidth capitals letters */
486 return c - 0x821f;
487 if(c >= 0x8281 && c <= 0x829a) /* Fullwidth small letters */
488 return c - 0x8220;
489 /* all other cases return c */
491 return c;
494 /*********************************************************************
495 * _mbcjistojms(MSVCRT.@)
497 * Converts a jis character to sjis.
498 * Based on description from
499 * http://www.slayers.ne.jp/~oouchi/code/jistosjis.html
501 unsigned int CDECL _mbcjistojms(unsigned int c)
503 /* Conversion takes place only when codepage is 932.
504 In all other cases, c is returned unchanged */
505 if(get_mbcinfo()->mbcodepage == 932)
507 if(HIBYTE(c) >= 0x21 && HIBYTE(c) <= 0x7e &&
508 LOBYTE(c) >= 0x21 && LOBYTE(c) <= 0x7e)
510 if(HIBYTE(c) % 2)
511 c += 0x1f;
512 else
513 c += 0x7d;
515 if(LOBYTE(c) >= 0x7F)
516 c += 0x1;
518 c = (((HIBYTE(c) - 0x21)/2 + 0x81) << 8) | LOBYTE(c);
520 if(HIBYTE(c) > 0x9f)
521 c += 0x4000;
523 else
524 return 0; /* Codepage is 932, but c can't be converted */
527 return c;
530 /*********************************************************************
531 * _mbcjmstojis(MSVCRT.@)
533 * Converts a sjis character to jis.
535 unsigned int CDECL _mbcjmstojis(unsigned int c)
537 /* Conversion takes place only when codepage is 932.
538 In all other cases, c is returned unchanged */
539 if(get_mbcinfo()->mbcodepage == 932)
541 if(_ismbclegal(c) && HIBYTE(c) < 0xf0)
543 if(HIBYTE(c) >= 0xe0)
544 c -= 0x4000;
546 c = (((HIBYTE(c) - 0x81)*2 + 0x21) << 8) | LOBYTE(c);
548 if(LOBYTE(c) > 0x7f)
549 c -= 0x1;
551 if(LOBYTE(c) > 0x9d)
552 c += 0x83;
553 else
554 c -= 0x1f;
556 else
557 return 0; /* Codepage is 932, but c can't be converted */
560 return c;
563 /*********************************************************************
564 * _mbsdec(MSVCRT.@)
566 unsigned char* CDECL _mbsdec(const unsigned char* start, const unsigned char* cur)
568 if(start >= cur)
569 return NULL;
571 if(get_mbcinfo()->ismbcodepage)
572 return (unsigned char *)(_ismbstrail(start,cur-1) ? cur - 2 : cur -1);
574 return (unsigned char *)cur - 1; /* ASCII CP or SB char */
577 /*********************************************************************
578 * _mbclen(MSVCRT.@)
580 unsigned int CDECL _mbclen(const unsigned char* str)
582 return _ismbblead(*str) ? 2 : 1;
585 /*********************************************************************
586 * _mbsinc(MSVCRT.@)
588 unsigned char* CDECL _mbsinc(const unsigned char* str)
590 return (unsigned char *)(str + _mbclen(str));
593 /*********************************************************************
594 * _mbsninc(MSVCRT.@)
596 unsigned char* CDECL _mbsninc(const unsigned char* str, size_t num)
598 if(!str)
599 return NULL;
601 while (num > 0 && *str)
603 if (_ismbblead(*str))
605 if (!*(str+1))
606 break;
607 str++;
609 str++;
610 num--;
613 return (unsigned char*)str;
616 /*********************************************************************
617 * _mbsnlen_l(MSVCRT.@)
619 size_t CDECL _mbsnlen_l(const unsigned char *str,
620 size_t maxsize, _locale_t locale)
622 pthreadmbcinfo mbcinfo;
623 size_t i = 0, len = 0;
625 if(!locale)
626 mbcinfo = get_mbcinfo();
627 else
628 mbcinfo = locale->mbcinfo;
630 if(!mbcinfo->ismbcodepage)
631 return strnlen((const char*)str, maxsize);
633 while(i<maxsize && str[i])
635 if (_ismbblead_l(str[i], locale))
637 i++;
638 if (!str[i]) /* count only full chars */
639 break;
641 i++;
642 len++;
644 return i < maxsize ? len : maxsize;
647 /*********************************************************************
648 * _mbslen(MSVCRT.@)
650 size_t CDECL _mbslen(const unsigned char* str)
652 return _mbsnlen_l(str, -1, NULL);
655 /*********************************************************************
656 * _mbslen_l(MSVCRT.@)
658 size_t CDECL _mbslen_l(const unsigned char* str, _locale_t locale)
660 return _mbsnlen_l(str, -1, locale);
663 /*********************************************************************
664 * _mbsnlen(MSVCRT.@)
666 size_t CDECL _mbsnlen(const unsigned char* str, size_t maxsize)
668 return _mbsnlen_l(str, maxsize, NULL);
671 /*********************************************************************
672 * _mbccpy_s_l(MSVCRT.@)
674 int CDECL _mbccpy_s_l(unsigned char* dest, size_t maxsize,
675 int *copied, const unsigned char* src, _locale_t locale)
677 if(copied) *copied = 0;
678 if(!MSVCRT_CHECK_PMT(dest != NULL && maxsize >= 1)) return EINVAL;
679 dest[0] = 0;
680 if(!MSVCRT_CHECK_PMT(src != NULL)) return EINVAL;
682 if(_ismbblead_l(*src, locale)) {
683 if(!src[1]) {
684 if(copied) *copied = 1;
685 *_errno() = EILSEQ;
686 return EILSEQ;
689 if(maxsize < 2) {
690 MSVCRT_INVALID_PMT("dst buffer is too small", ERANGE);
691 return ERANGE;
694 *dest++ = *src++;
695 *dest = *src;
696 if(copied) *copied = 2;
697 }else {
698 *dest = *src;
699 if(copied) *copied = 1;
702 return 0;
705 /*********************************************************************
706 * _mbccpy(MSVCRT.@)
708 void CDECL _mbccpy(unsigned char* dest, const unsigned char* src)
710 _mbccpy_s_l(dest, 2, NULL, src, NULL);
713 /*********************************************************************
714 * _mbccpy_l(MSVCRT.@)
716 void CDECL _mbccpy_l(unsigned char* dest, const unsigned char* src,
717 _locale_t locale)
719 _mbccpy_s_l(dest, 2, NULL, src, locale);
722 /*********************************************************************
723 * _mbccpy_s(MSVCRT.@)
725 int CDECL _mbccpy_s(unsigned char* dest, size_t maxsize,
726 int *copied, const unsigned char* src)
728 return _mbccpy_s_l(dest, maxsize, copied, src, NULL);
731 /*********************************************************************
732 * _mbsncpy(MSVCRT.@)
733 * REMARKS
734 * The parameter n is the number or characters to copy, not the size of
735 * the buffer. Use _mbsnbcpy for a function analogical to strncpy
737 unsigned char* CDECL _mbsncpy(unsigned char* dst, const unsigned char* src, size_t n)
739 unsigned char* ret = dst;
740 if(!n)
741 return dst;
742 if (get_mbcinfo()->ismbcodepage)
744 while (*src && n)
746 n--;
747 if (_ismbblead(*src))
749 if (!*(src+1))
751 *dst++ = 0;
752 *dst++ = 0;
753 break;
756 *dst++ = *src++;
759 *dst++ = *src++;
762 else
764 while (n)
766 n--;
767 if (!(*dst++ = *src++)) break;
770 while (n--) *dst++ = 0;
771 return ret;
774 /*********************************************************************
775 * _mbsnbcpy_s_l(MSVCRT.@)
776 * REMARKS
777 * Unlike _mbsnbcpy this function does not pad the rest of the dest
778 * string with 0
780 int CDECL _mbsnbcpy_s_l(unsigned char* dst, size_t size,
781 const unsigned char* src, size_t n, _locale_t locale)
783 size_t pos = 0;
785 if(!dst || size == 0)
786 return EINVAL;
787 if(!src)
789 dst[0] = '\0';
790 return EINVAL;
792 if(!n)
793 return 0;
795 if((locale ? locale->mbcinfo : get_mbcinfo())->ismbcodepage)
797 BOOL is_lead = FALSE;
798 while (*src && n)
800 if(pos == size)
802 dst[0] = '\0';
803 return ERANGE;
805 is_lead = (!is_lead && _ismbblead(*src));
806 n--;
807 dst[pos++] = *src++;
810 if (is_lead) /* if string ends with a lead, remove it */
811 dst[pos - 1] = 0;
813 else
815 while (n)
817 n--;
818 if(pos == size)
820 dst[0] = '\0';
821 return ERANGE;
824 if(!(*src)) break;
825 dst[pos++] = *src++;
829 if(pos < size)
830 dst[pos] = '\0';
831 else
833 dst[0] = '\0';
834 return ERANGE;
837 return 0;
840 /*********************************************************************
841 * _mbsnbcpy_s(MSVCRT.@)
843 int CDECL _mbsnbcpy_s(unsigned char* dst, size_t size, const unsigned char* src, size_t n)
845 return _mbsnbcpy_s_l(dst, size, src, n, NULL);
848 /*********************************************************************
849 * _mbscpy_s_l(MSVCRT.@)
851 int CDECL _mbscpy_s_l(unsigned char *dst, size_t size,
852 const unsigned char *src, _locale_t locale)
854 return _mbsnbcpy_s_l(dst, size, src, -1, locale);
857 /*********************************************************************
858 * _mbscpy_s(MSVCRT.@)
860 int CDECL _mbscpy_s(unsigned char *dst, size_t size, const unsigned char *src)
862 return _mbscpy_s_l(dst, size, src, NULL);
865 /*********************************************************************
866 * _mbsnbcpy(MSVCRT.@)
867 * REMARKS
868 * Like strncpy this function doesn't enforce the string to be
869 * NUL-terminated
871 unsigned char* CDECL _mbsnbcpy(unsigned char* dst, const unsigned char* src, size_t n)
873 unsigned char* ret = dst;
874 if(!n)
875 return dst;
876 if(get_mbcinfo()->ismbcodepage)
878 BOOL is_lead = FALSE;
879 while (*src && n)
881 is_lead = (!is_lead && _ismbblead(*src));
882 n--;
883 *dst++ = *src++;
886 if (is_lead) /* if string ends with a lead, remove it */
887 *(dst - 1) = 0;
889 else
891 while (n)
893 n--;
894 if (!(*dst++ = *src++)) break;
897 while (n--) *dst++ = 0;
898 return ret;
901 /*********************************************************************
902 * _mbscmp_l(MSVCRT.@)
904 int CDECL _mbscmp_l(const unsigned char* str, const unsigned char* cmp, _locale_t locale)
906 pthreadmbcinfo mbcinfo;
908 if (!str || !cmp) return INT_MAX;
910 mbcinfo = locale ? locale->mbcinfo : get_mbcinfo();
912 if(mbcinfo->ismbcodepage)
914 unsigned int strc, cmpc;
915 do {
916 if(!*str)
917 return *cmp ? -1 : 0;
918 if(!*cmp)
919 return 1;
920 strc = _mbsnextc_l(str, locale);
921 cmpc = _mbsnextc_l(cmp, locale);
922 if(strc != cmpc)
923 return strc < cmpc ? -1 : 1;
924 str +=(strc > 255) ? 2 : 1;
925 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
926 } while(1);
928 return u_strcmp(str, cmp); /* ASCII CP */
931 /*********************************************************************
932 * _mbscmp(MSVCRT.@)
934 int CDECL _mbscmp(const unsigned char* str, const unsigned char* cmp)
936 return _mbscmp_l(str, cmp, NULL);
939 /*********************************************************************
940 * _mbsnbicoll_l(MSVCRT.@)
942 int CDECL _mbsnbicoll_l(const unsigned char *str1, const unsigned char *str2, size_t len, _locale_t locale)
944 pthreadmbcinfo mbcinfo;
946 if(!locale)
947 mbcinfo = get_mbcinfo();
948 else
949 mbcinfo = locale->mbcinfo;
951 if(!mbcinfo->ismbcodepage)
952 return _strnicoll_l((const char*)str1, (const char*)str2, len, locale);
953 return CompareStringA(mbcinfo->mblcid, NORM_IGNORECASE, (const char*)str1, len, (const char*)str2, len)-CSTR_EQUAL;
956 /*********************************************************************
957 * _mbsicoll_l(MSVCRT.@)
959 int CDECL _mbsicoll_l(const unsigned char *str1, const unsigned char *str2, _locale_t locale)
961 return _mbsnbicoll_l(str1, str2, -1, locale);
964 /*********************************************************************
965 * _mbsnbicoll(MSVCRT.@)
967 int CDECL _mbsnbicoll(const unsigned char *str1, const unsigned char *str2, size_t len)
969 return _mbsnbicoll_l(str1, str2, len, NULL);
972 /*********************************************************************
973 * _mbsicoll(MSVCRT.@)
975 int CDECL _mbsicoll(const unsigned char* str, const unsigned char* cmp)
977 #if _MSVCR_VER>=60 && _MSVCR_VER<=71
978 return CompareStringA(get_mbcinfo()->mblcid, NORM_IGNORECASE,
979 (const char*)str, -1, (const char*)cmp, -1)-CSTR_EQUAL;
980 #else
981 return _mbsnbicoll_l(str, cmp, -1, NULL);
982 #endif
985 /*********************************************************************
986 * _mbsnbcoll_l(MSVCRT.@)
988 int CDECL _mbsnbcoll_l(const unsigned char *str1, const unsigned char *str2, size_t len, _locale_t locale)
990 pthreadmbcinfo mbcinfo;
992 if(!locale)
993 mbcinfo = get_mbcinfo();
994 else
995 mbcinfo = locale->mbcinfo;
997 if(!mbcinfo->ismbcodepage)
998 return _strncoll_l((const char*)str1, (const char*)str2, len, locale);
999 return CompareStringA(mbcinfo->mblcid, 0, (const char*)str1, len, (const char*)str2, len)-CSTR_EQUAL;
1002 /*********************************************************************
1003 * _mbscoll_l(MSVCRT.@)
1005 int CDECL _mbscoll_l(const unsigned char *str1, const unsigned char *str2, _locale_t locale)
1007 return _mbsnbcoll_l(str1, str2, -1, locale);
1010 /*********************************************************************
1011 * _mbsnbcoll(MSVCRT.@)
1013 int CDECL _mbsnbcoll(const unsigned char *str1, const unsigned char *str2, size_t len)
1015 return _mbsnbcoll_l(str1, str2, len, NULL);
1018 /*********************************************************************
1019 * _mbscoll(MSVCRT.@)
1021 int CDECL _mbscoll(const unsigned char* str, const unsigned char* cmp)
1023 #if _MSVCR_VER>=60 && _MSVCR_VER<=71
1024 return CompareStringA(get_mbcinfo()->mblcid, 0,
1025 (const char*)str, -1, (const char*)cmp, -1)-CSTR_EQUAL;
1026 #else
1027 return _mbsnbcoll_l(str, cmp, -1, NULL);
1028 #endif
1031 /*********************************************************************
1032 * _mbsicmp(MSVCRT.@)
1034 int CDECL _mbsicmp(const unsigned char* str, const unsigned char* cmp)
1036 if(get_mbcinfo()->ismbcodepage)
1038 unsigned int strc, cmpc;
1039 do {
1040 if(!*str)
1041 return *cmp ? -1 : 0;
1042 if(!*cmp)
1043 return 1;
1044 strc = _mbctolower(_mbsnextc(str));
1045 cmpc = _mbctolower(_mbsnextc(cmp));
1046 if(strc != cmpc)
1047 return strc < cmpc ? -1 : 1;
1048 str +=(strc > 255) ? 2 : 1;
1049 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
1050 } while(1);
1052 return u_strcasecmp(str, cmp); /* ASCII CP */
1055 /*********************************************************************
1056 * _mbsncmp(MSVCRT.@)
1058 int CDECL _mbsncmp(const unsigned char* str, const unsigned char* cmp, size_t len)
1060 if(!len)
1061 return 0;
1063 if(get_mbcinfo()->ismbcodepage)
1065 unsigned int strc, cmpc;
1066 while(len--)
1068 int inc;
1069 if(!*str)
1070 return *cmp ? -1 : 0;
1071 if(!*cmp)
1072 return 1;
1073 strc = _mbsnextc(str);
1074 cmpc = _mbsnextc(cmp);
1075 if(strc != cmpc)
1076 return strc < cmpc ? -1 : 1;
1077 inc=(strc > 255) ? 2 : 1; /* Equal, use same increment */
1078 str += inc;
1079 cmp += inc;
1081 return 0; /* Matched len chars */
1083 return u_strncmp(str, cmp, len); /* ASCII CP */
1086 /*********************************************************************
1087 * _mbsnbcmp(MSVCRT.@)
1089 int CDECL _mbsnbcmp(const unsigned char* str, const unsigned char* cmp, size_t len)
1091 if (!len)
1092 return 0;
1093 if(get_mbcinfo()->ismbcodepage)
1095 unsigned int strc, cmpc;
1096 while (len)
1098 int clen;
1099 if(!*str)
1100 return *cmp ? -1 : 0;
1101 if(!*cmp)
1102 return 1;
1103 if (_ismbblead(*str))
1105 strc=(len>=2)?_mbsnextc(str):0;
1106 clen=2;
1108 else
1110 strc=*str;
1111 clen=1;
1113 if (_ismbblead(*cmp))
1114 cmpc=(len>=2)?_mbsnextc(cmp):0;
1115 else
1116 cmpc=*cmp;
1117 if(strc != cmpc)
1118 return strc < cmpc ? -1 : 1;
1119 len -= clen;
1120 str += clen;
1121 cmp += clen;
1123 return 0; /* Matched len chars */
1125 return u_strncmp(str,cmp,len);
1128 /*********************************************************************
1129 * _mbsnicmp(MSVCRT.@)
1131 * Compare two multibyte strings case insensitively to 'len' characters.
1133 int CDECL _mbsnicmp(const unsigned char* str, const unsigned char* cmp, size_t len)
1135 /* FIXME: No tolower() for mb strings yet */
1136 if(get_mbcinfo()->ismbcodepage)
1138 unsigned int strc, cmpc;
1139 while(len--)
1141 if(!*str)
1142 return *cmp ? -1 : 0;
1143 if(!*cmp)
1144 return 1;
1145 strc = _mbctolower(_mbsnextc(str));
1146 cmpc = _mbctolower(_mbsnextc(cmp));
1147 if(strc != cmpc)
1148 return strc < cmpc ? -1 : 1;
1149 str +=(strc > 255) ? 2 : 1;
1150 cmp +=(strc > 255) ? 2 : 1; /* Equal, use same increment */
1152 return 0; /* Matched len chars */
1154 return u_strncasecmp(str, cmp, len); /* ASCII CP */
1157 /*********************************************************************
1158 * _mbsnbicmp(MSVCRT.@)
1160 int CDECL _mbsnbicmp(const unsigned char* str, const unsigned char* cmp, size_t len)
1162 if (!len)
1163 return 0;
1164 if(get_mbcinfo()->ismbcodepage)
1166 unsigned int strc, cmpc;
1167 while (len)
1169 int clen;
1170 if(!*str)
1171 return *cmp ? -1 : 0;
1172 if(!*cmp)
1173 return 1;
1174 if (_ismbblead(*str))
1176 strc=(len>=2)?_mbsnextc(str):0;
1177 clen=2;
1179 else
1181 strc=*str;
1182 clen=1;
1184 if (_ismbblead(*cmp))
1185 cmpc=(len>=2)?_mbsnextc(cmp):0;
1186 else
1187 cmpc=*cmp;
1188 strc = _mbctolower(strc);
1189 cmpc = _mbctolower(cmpc);
1190 if(strc != cmpc)
1191 return strc < cmpc ? -1 : 1;
1192 len -= clen;
1193 str += clen;
1194 cmp += clen;
1196 return 0; /* Matched len bytes */
1198 return u_strncasecmp(str,cmp,len);
1201 /*********************************************************************
1202 * _mbscat (MSVCRT.@)
1204 unsigned char * CDECL _mbscat( unsigned char *dst, const unsigned char *src )
1206 strcat( (char *)dst, (const char *)src );
1207 return dst;
1210 /*********************************************************************
1211 * _mbscat_s_l (MSVCRT.@)
1213 int CDECL _mbscat_s_l( unsigned char *dst, size_t size,
1214 const unsigned char *src, _locale_t locale )
1216 size_t i, j;
1217 int ret = 0;
1219 if(!MSVCRT_CHECK_PMT(dst != NULL)) return EINVAL;
1220 if(!MSVCRT_CHECK_PMT(src != NULL)) return EINVAL;
1222 for(i=0; i<size; i++)
1223 if(!dst[i]) break;
1224 if(i == size) {
1225 MSVCRT_INVALID_PMT("dst is not NULL-terminated", EINVAL);
1226 if(size) dst[0] = 0;
1227 return EINVAL;
1230 if(i && _ismbblead_l(dst[i-1], locale)) {
1231 ret = EILSEQ;
1232 i--;
1235 for(j=0; src[j] && i+j<size; j++)
1236 dst[i+j] = src[j];
1237 if(i+j == size) {
1238 MSVCRT_INVALID_PMT("dst buffer is too small", ERANGE);
1239 dst[0] = 0;
1240 return ERANGE;
1243 if(j && _ismbblead_l(src[j-1], locale)) {
1244 ret = EILSEQ;
1245 j--;
1248 dst[i+j] = 0;
1249 return ret;
1252 /*********************************************************************
1253 * _mbscat_s (MSVCRT.@)
1255 int CDECL _mbscat_s( unsigned char *dst, size_t size, const unsigned char *src )
1257 return _mbscat_s_l(dst, size, src, NULL);
1260 /*********************************************************************
1261 * _mbscpy (MSVCRT.@)
1263 unsigned char* CDECL _mbscpy( unsigned char *dst, const unsigned char *src )
1265 strcpy( (char *)dst, (const char *)src );
1266 return dst;
1269 /*********************************************************************
1270 * _mbsstr (MSVCRT.@)
1272 unsigned char * CDECL _mbsstr(const unsigned char *haystack, const unsigned char *needle)
1274 return (unsigned char *)strstr( (const char *)haystack, (const char *)needle );
1277 /*********************************************************************
1278 * _mbschr(MSVCRT.@)
1280 * Find a multibyte character in a multibyte string.
1282 unsigned char* CDECL _mbschr(const unsigned char* s, unsigned int x)
1284 if(get_mbcinfo()->ismbcodepage)
1286 unsigned int c;
1287 while (1)
1289 c = _mbsnextc(s);
1290 if (c == x)
1291 return (unsigned char*)s;
1292 if (!c)
1293 return NULL;
1294 s += c > 255 ? 2 : 1;
1297 return u_strchr(s, x); /* ASCII CP */
1300 /*********************************************************************
1301 * _mbsrchr(MSVCRT.@)
1303 unsigned char* CDECL _mbsrchr(const unsigned char* s, unsigned int x)
1305 if(get_mbcinfo()->ismbcodepage)
1307 unsigned int c;
1308 unsigned char* match=NULL;
1309 if(!s)
1310 return NULL;
1311 while (1) {
1312 c = _mbsnextc(s);
1313 if (c == x)
1314 match=(unsigned char*)s;
1315 if (!c)
1316 return match;
1317 s +=(c > 255) ? 2 : 1;
1320 return u_strrchr(s, x);
1323 /*********************************************************************
1324 * _mbstok_s_l(MSVCRT.@)
1326 unsigned char* CDECL _mbstok_s_l(unsigned char *str, const unsigned char *delim,
1327 unsigned char **ctx, _locale_t locale)
1329 pthreadmbcinfo mbcinfo;
1330 unsigned int c;
1332 if(!MSVCRT_CHECK_PMT(delim != NULL)) return NULL;
1333 if(!MSVCRT_CHECK_PMT(ctx != NULL)) return NULL;
1334 if(!MSVCRT_CHECK_PMT(str || *ctx)) return NULL;
1336 if(locale)
1337 mbcinfo = locale->mbcinfo;
1338 else
1339 mbcinfo = get_mbcinfo();
1341 if(!mbcinfo->ismbcodepage)
1342 return (unsigned char*)strtok_s((char*)str, (const char*)delim, (char**)ctx);
1344 if(!str)
1345 str = *ctx;
1347 while((c=_mbsnextc(str)) && _mbschr(delim, c))
1348 str += c>255 ? 2 : 1;
1349 if(!*str)
1351 *ctx = str;
1352 return NULL;
1355 *ctx = str + (c>255 ? 2 : 1);
1356 while((c=_mbsnextc(*ctx)) && !_mbschr(delim, c))
1357 *ctx += c>255 ? 2 : 1;
1358 if (**ctx) {
1359 *(*ctx)++ = 0;
1360 if(c > 255)
1361 *(*ctx)++ = 0;
1364 return str;
1368 /*********************************************************************
1369 * _mbstok_s(MSVCRT.@)
1371 unsigned char* CDECL _mbstok_s(unsigned char *str,
1372 const unsigned char *delim, unsigned char **ctx)
1374 return _mbstok_s_l(str, delim, ctx, NULL);
1377 /*********************************************************************
1378 * _mbstok_l(MSVCRT.@)
1380 unsigned char* CDECL _mbstok_l(unsigned char *str,
1381 const unsigned char *delim, _locale_t locale)
1383 return _mbstok_s_l(str, delim, &msvcrt_get_thread_data()->mbstok_next, locale);
1386 /*********************************************************************
1387 * _mbstok(MSVCRT.@)
1389 unsigned char* CDECL _mbstok(unsigned char *str, const unsigned char *delim)
1391 thread_data_t *data = msvcrt_get_thread_data();
1393 #if _MSVCR_VER == 0
1394 if(!str && !data->mbstok_next)
1395 return NULL;
1396 #endif
1398 return _mbstok_s_l(str, delim, &data->mbstok_next, NULL);
1401 /*********************************************************************
1402 * _mbbtombc(MSVCRT.@)
1404 unsigned int CDECL _mbbtombc(unsigned int c)
1406 if(get_mbcinfo()->mbcodepage == 932)
1408 if(c >= 0x20 && c <= 0x7e) {
1409 if((c >= 0x41 && c <= 0x5a) || (c >= 0x61 && c <= 0x7a) || (c >= 0x30 && c <= 0x39))
1410 return mbbtombc_932[c - 0x20] | 0x8200;
1411 else
1412 return mbbtombc_932[c - 0x20] | 0x8100;
1414 else if(c >= 0xa1 && c <= 0xdf) {
1415 if(c >= 0xa6 && c <= 0xdd && c != 0xb0)
1416 return mbbtombc_932[c - 0xa1 + 0x5f] | 0x8300;
1417 else
1418 return mbbtombc_932[c - 0xa1 + 0x5f] | 0x8100;
1421 return c; /* not Japanese or no MB char */
1424 /*********************************************************************
1425 * _ismbbkana_l(MSVCRT.@)
1427 int CDECL _ismbbkana_l(unsigned int c, _locale_t locale)
1429 pthreadmbcinfo mbcinfo;
1431 if(locale)
1432 mbcinfo = locale->mbcinfo;
1433 else
1434 mbcinfo = get_mbcinfo();
1436 if(mbcinfo->mbcodepage == 932)
1438 /* Japanese/Katakana, CP 932 */
1439 return (c >= 0xa1 && c <= 0xdf);
1441 return 0;
1444 /*********************************************************************
1445 * _ismbbkana(MSVCRT.@)
1447 int CDECL _ismbbkana(unsigned int c)
1449 return _ismbbkana_l( c, NULL );
1452 /*********************************************************************
1453 * _ismbcdigit_l(MSVCRT.@)
1455 int CDECL _ismbcdigit_l(unsigned int ch, _locale_t locale)
1457 return MSVCRT__iswdigit_l( msvcrt_mbc_to_wc_l(ch, locale), locale );
1460 /*********************************************************************
1461 * _ismbcdigit(MSVCRT.@)
1463 int CDECL _ismbcdigit(unsigned int ch)
1465 return _ismbcdigit_l( ch, NULL );
1468 /*********************************************************************
1469 * _ismbcgraph_l(MSVCRT.@)
1471 int CDECL _ismbcgraph_l(unsigned int ch, _locale_t locale)
1473 return MSVCRT__iswgraph_l( msvcrt_mbc_to_wc_l(ch, locale), locale );
1476 /*********************************************************************
1477 * _ismbcgraph(MSVCRT.@)
1479 int CDECL _ismbcgraph(unsigned int ch)
1481 return _ismbcgraph_l( ch, NULL );
1484 /*********************************************************************
1485 * _ismbcalpha_l (MSVCRT.@)
1487 int CDECL _ismbcalpha_l(unsigned int ch, _locale_t locale)
1489 return MSVCRT__iswalpha_l( msvcrt_mbc_to_wc_l(ch, locale), locale );
1492 /*********************************************************************
1493 * _ismbcalpha (MSVCRT.@)
1495 int CDECL _ismbcalpha(unsigned int ch)
1497 return _ismbcalpha_l( ch, NULL );
1500 /*********************************************************************
1501 * _ismbclower_l (MSVCRT.@)
1503 int CDECL _ismbclower_l(unsigned int ch, _locale_t locale)
1505 return MSVCRT__iswlower_l( msvcrt_mbc_to_wc_l(ch, locale), locale );
1508 /*********************************************************************
1509 * _ismbclower (MSVCRT.@)
1511 int CDECL _ismbclower(unsigned int ch)
1513 return _ismbclower_l( ch, NULL );
1516 /*********************************************************************
1517 * _ismbcupper_l (MSVCRT.@)
1519 int CDECL _ismbcupper_l(unsigned int ch, _locale_t locale)
1521 return MSVCRT__iswupper_l( msvcrt_mbc_to_wc_l(ch, locale), locale );
1524 /*********************************************************************
1525 * _ismbcupper (MSVCRT.@)
1527 int CDECL _ismbcupper(unsigned int ch)
1529 return _ismbcupper_l( ch, NULL );
1532 /*********************************************************************
1533 * _ismbcsymbol(MSVCRT.@)
1535 int CDECL _ismbcsymbol(unsigned int ch)
1537 wchar_t wch = msvcrt_mbc_to_wc( ch );
1538 WORD ctype;
1539 if (!GetStringTypeW(CT_CTYPE3, &wch, 1, &ctype))
1541 WARN("GetStringTypeW failed on %x\n", ch);
1542 return 0;
1544 return ((ctype & C3_SYMBOL) != 0);
1547 /*********************************************************************
1548 * _ismbcalnum_l (MSVCRT.@)
1550 int CDECL _ismbcalnum_l(unsigned int ch, _locale_t locale)
1552 return MSVCRT__iswalnum_l( msvcrt_mbc_to_wc_l(ch, locale), locale );
1555 /*********************************************************************
1556 * _ismbcalnum (MSVCRT.@)
1558 int CDECL _ismbcalnum(unsigned int ch)
1560 return _ismbcalnum_l( ch, NULL );
1563 /*********************************************************************
1564 * _ismbcspace_l (MSVCRT.@)
1566 int CDECL _ismbcspace_l(unsigned int ch, _locale_t locale)
1568 return MSVCRT__iswspace_l( msvcrt_mbc_to_wc_l(ch, locale), locale );
1571 /*********************************************************************
1572 * _ismbcspace (MSVCRT.@)
1574 int CDECL _ismbcspace(unsigned int ch)
1576 return _ismbcspace_l( ch, NULL );
1579 /*********************************************************************
1580 * _ismbcprint_l (MSVCRT.@)
1582 int CDECL _ismbcprint_l(unsigned int ch, _locale_t locale)
1584 return MSVCRT__iswprint_l( msvcrt_mbc_to_wc_l(ch, locale), locale );
1587 /*********************************************************************
1588 * _ismbcprint (MSVCRT.@)
1590 int CDECL _ismbcprint(unsigned int ch)
1592 return _ismbcprint_l( ch, NULL );
1595 /*********************************************************************
1596 * _ismbcpunct_l (MSVCRT.@)
1598 int CDECL _ismbcpunct_l(unsigned int ch, _locale_t locale)
1600 return MSVCRT__iswpunct_l( msvcrt_mbc_to_wc_l(ch, locale), locale );
1603 /*********************************************************************
1604 * _ismbcpunct(MSVCRT.@)
1606 int CDECL _ismbcpunct(unsigned int ch)
1608 return _ismbcpunct_l( ch, NULL );
1611 /*********************************************************************
1612 * _ismbchira(MSVCRT.@)
1614 int CDECL _ismbchira(unsigned int c)
1616 if(get_mbcinfo()->mbcodepage == 932)
1618 /* Japanese/Hiragana, CP 932 */
1619 return (c >= 0x829f && c <= 0x82f1);
1621 return 0;
1624 /*********************************************************************
1625 * _ismbckata(MSVCRT.@)
1627 int CDECL _ismbckata(unsigned int c)
1629 if(get_mbcinfo()->mbcodepage == 932)
1631 /* Japanese/Katakana, CP 932 */
1632 return (c >= 0x8340 && c <= 0x8396 && c != 0x837f);
1634 return 0;
1638 /*********************************************************************
1639 * _ismbblead_l(MSVCRT.@)
1641 int CDECL _ismbblead_l(unsigned int c, _locale_t locale)
1643 pthreadmbcinfo mbcinfo;
1645 if(!locale)
1646 mbcinfo = get_mbcinfo();
1647 else
1648 mbcinfo = locale->mbcinfo;
1650 return (mbcinfo->mbctype[(c&0xff) + 1] & _M1) != 0;
1653 /*********************************************************************
1654 * _ismbblead(MSVCRT.@)
1656 int CDECL _ismbblead(unsigned int c)
1658 return _ismbblead_l(c, NULL);
1661 /*********************************************************************
1662 * _ismbbtrail_l(MSVCRT.@)
1664 int CDECL _ismbbtrail_l(unsigned int c, _locale_t locale)
1666 pthreadmbcinfo mbcinfo;
1668 if(!locale)
1669 mbcinfo = get_mbcinfo();
1670 else
1671 mbcinfo = locale->mbcinfo;
1673 return (mbcinfo->mbctype[(c&0xff) + 1] & _M2) != 0;
1676 /*********************************************************************
1677 * _ismbbtrail(MSVCRT.@)
1679 int CDECL _ismbbtrail(unsigned int c)
1681 return _ismbbtrail_l(c, NULL);
1684 /*********************************************************************
1685 * _ismbclegal_l(MSVCRT.@)
1687 int CDECL _ismbclegal_l(unsigned int c, _locale_t locale)
1689 return _ismbblead_l(HIBYTE(c), locale) && _ismbbtrail_l(LOBYTE(c), locale);
1692 /*********************************************************************
1693 * _ismbclegal(MSVCRT.@)
1695 int CDECL _ismbclegal(unsigned int c)
1697 return _ismbclegal_l(c, NULL);
1700 /*********************************************************************
1701 * _ismbslead(MSVCRT.@)
1703 int CDECL _ismbslead(const unsigned char* start, const unsigned char* str)
1705 int lead = 0;
1707 if(!get_mbcinfo()->ismbcodepage)
1708 return 0;
1710 /* Lead bytes can also be trail bytes so we need to analyse the string
1712 while (start <= str)
1714 if (!*start)
1715 return 0;
1716 lead = !lead && _ismbblead(*start);
1717 start++;
1720 return lead ? -1 : 0;
1723 /*********************************************************************
1724 * _ismbstrail(MSVCRT.@)
1726 int CDECL _ismbstrail(const unsigned char* start, const unsigned char* str)
1728 /* Note: this function doesn't check _ismbbtrail */
1729 if ((str > start) && _ismbslead(start, str-1))
1730 return -1;
1731 else
1732 return 0;
1735 /*********************************************************************
1736 * _mbbtype_l(MSVCRT.@)
1738 int CDECL _mbbtype_l(unsigned char c, int type, _locale_t locale)
1740 if (type == 1)
1741 return _ismbbtrail_l(c, locale) ? _MBC_TRAIL : _MBC_ILLEGAL;
1742 else
1743 return _ismbblead_l(c, locale) ? _MBC_LEAD
1744 : _isprint_l(c, locale) ? _MBC_SINGLE : _MBC_ILLEGAL;
1747 /*********************************************************************
1748 * _mbbtype(MSVCRT.@)
1750 int CDECL _mbbtype(unsigned char c, int type)
1752 return _mbbtype_l(c, type, NULL);
1755 /*********************************************************************
1756 * _mbsbtype (MSVCRT.@)
1758 int CDECL _mbsbtype(const unsigned char *str, size_t count)
1760 int lead = 0;
1761 const unsigned char *end = str + count;
1763 /* Lead bytes can also be trail bytes so we need to analyse the string.
1764 * Also we must return _MBC_ILLEGAL for chars past the end of the string
1766 while (str < end) /* Note: we skip the last byte - will check after the loop */
1768 if (!*str)
1769 return _MBC_ILLEGAL;
1770 lead = get_mbcinfo()->ismbcodepage && !lead && _ismbblead(*str);
1771 str++;
1774 if (lead)
1775 if (_ismbbtrail(*str))
1776 return _MBC_TRAIL;
1777 else
1778 return _MBC_ILLEGAL;
1779 else
1780 if (_ismbblead(*str))
1781 return _MBC_LEAD;
1782 else
1783 return _MBC_SINGLE;
1786 /*********************************************************************
1787 * _mbsset(MSVCRT.@)
1789 unsigned char* CDECL _mbsset(unsigned char* str, unsigned int c)
1791 unsigned char* ret = str;
1793 if(!get_mbcinfo()->ismbcodepage || c < 256)
1794 return u__strset(str, c); /* ASCII CP or SB char */
1796 c &= 0xffff; /* Strip high bits */
1798 while(str[0] && str[1])
1800 *str++ = c >> 8;
1801 *str++ = c & 0xff;
1803 if(str[0])
1804 str[0] = '\0'; /* FIXME: OK to shorten? */
1806 return ret;
1809 /*********************************************************************
1810 * _mbsnbset(MSVCRT.@)
1812 unsigned char* CDECL _mbsnbset(unsigned char *str, unsigned int c, size_t len)
1814 unsigned char *ret = str;
1816 if(!len)
1817 return ret;
1819 if(!get_mbcinfo()->ismbcodepage || c < 256)
1820 return u__strnset(str, c, len); /* ASCII CP or SB char */
1822 c &= 0xffff; /* Strip high bits */
1824 while(str[0] && str[1] && (len > 1))
1826 *str++ = c >> 8;
1827 len--;
1828 *str++ = c & 0xff;
1829 len--;
1831 if(len && str[0]) {
1832 /* as per msdn pad with a blank character */
1833 str[0] = ' ';
1836 return ret;
1839 /*********************************************************************
1840 * _mbsnset(MSVCRT.@)
1842 unsigned char* CDECL _mbsnset(unsigned char* str, unsigned int c, size_t len)
1844 unsigned char *ret = str;
1846 if(!len)
1847 return ret;
1849 if(!get_mbcinfo()->ismbcodepage || c < 256)
1850 return u__strnset(str, c, len); /* ASCII CP or SB char */
1852 c &= 0xffff; /* Strip high bits */
1854 while(str[0] && str[1] && len--)
1856 *str++ = c >> 8;
1857 *str++ = c & 0xff;
1859 if(len && str[0])
1860 str[0] = '\0'; /* FIXME: OK to shorten? */
1862 return ret;
1865 /*********************************************************************
1866 * _mbsnccnt(MSVCRT.@)
1867 * 'c' is for 'character'.
1869 size_t CDECL _mbsnccnt(const unsigned char* str, size_t len)
1871 size_t ret;
1872 if(get_mbcinfo()->ismbcodepage)
1874 ret=0;
1875 while(*str && len-- > 0)
1877 if(_ismbblead(*str))
1879 if (!len)
1880 break;
1881 len--;
1882 str++;
1884 str++;
1885 ret++;
1887 return ret;
1889 ret=u_strlen(str);
1890 return min(ret, len); /* ASCII CP */
1893 /*********************************************************************
1894 * _mbsnbcnt(MSVCRT.@)
1895 * 'b' is for byte count.
1897 size_t CDECL _mbsnbcnt(const unsigned char* str, size_t len)
1899 size_t ret;
1900 if(get_mbcinfo()->ismbcodepage)
1902 const unsigned char* xstr = str;
1903 while(*xstr && len-- > 0)
1905 if (_ismbblead(*xstr++))
1906 xstr++;
1908 return xstr-str;
1910 ret=u_strlen(str);
1911 return min(ret, len); /* ASCII CP */
1914 /*********************************************************************
1915 * _mbsnbcat(MSVCRT.@)
1917 unsigned char* CDECL _mbsnbcat(unsigned char* dst, const unsigned char* src, size_t len)
1919 if(get_mbcinfo()->ismbcodepage)
1921 unsigned char *res = dst;
1922 while (*dst) {
1923 if (_ismbblead(*dst++)) {
1924 if (*dst) {
1925 dst++;
1926 } else {
1927 /* as per msdn overwrite the lead byte in front of '\0' */
1928 dst--;
1929 break;
1933 while (*src && len--) *dst++ = *src++;
1934 *dst = '\0';
1935 return res;
1937 return u_strncat(dst, src, len); /* ASCII CP */
1940 int CDECL _mbsnbcat_s(unsigned char *dst, size_t size, const unsigned char *src, size_t len)
1942 unsigned char *ptr = dst;
1943 size_t i;
1945 if (!dst && !size && !src && !len)
1946 return 0;
1948 if (!dst || !size || !src)
1950 if (dst && size)
1951 *dst = '\0';
1953 *_errno() = EINVAL;
1954 return EINVAL;
1957 /* Find the null terminator of the destination buffer. */
1958 while (size && *ptr)
1959 size--, ptr++;
1961 if (!size)
1963 *dst = '\0';
1964 *_errno() = EINVAL;
1965 return EINVAL;
1968 /* If necessary, check that the character preceding the null terminator is
1969 * a lead byte and move the pointer back by one for later overwrite. */
1970 if (ptr != dst && get_mbcinfo()->ismbcodepage && _ismbblead(*(ptr - 1)))
1971 size++, ptr--;
1973 for (i = 0; *src && i < len; i++)
1975 *ptr++ = *src++;
1976 size--;
1978 if (!size)
1980 *dst = '\0';
1981 *_errno() = ERANGE;
1982 return ERANGE;
1986 *ptr = '\0';
1987 return 0;
1990 /*********************************************************************
1991 * _mbsncat(MSVCRT.@)
1993 unsigned char* CDECL _mbsncat(unsigned char* dst, const unsigned char* src, size_t len)
1995 if(get_mbcinfo()->ismbcodepage)
1997 unsigned char *res = dst;
1998 while (*dst)
2000 if (_ismbblead(*dst++))
2001 dst++;
2003 while (*src && len--)
2005 *dst++ = *src;
2006 if(_ismbblead(*src++))
2007 *dst++ = *src++;
2009 *dst = '\0';
2010 return res;
2012 return u_strncat(dst, src, len); /* ASCII CP */
2016 /*********************************************************************
2017 * _mbslwr(MSVCRT.@)
2019 unsigned char* CDECL _mbslwr(unsigned char* s)
2021 unsigned char *ret = s;
2022 if (!s)
2023 return NULL;
2024 if (get_mbcinfo()->ismbcodepage)
2026 unsigned int c;
2027 while (*s)
2029 c = _mbctolower(_mbsnextc(s));
2030 /* Note that I assume that the size of the character is unchanged */
2031 if (c > 255)
2033 *s++=(c>>8);
2034 c=c & 0xff;
2036 *s++=c;
2039 else for ( ; *s; s++) *s = _tolower_l(*s, NULL);
2040 return ret;
2044 /*********************************************************************
2045 * _mbslwr_s(MSVCRT.@)
2047 int CDECL _mbslwr_s(unsigned char* s, size_t len)
2049 if (!s && !len)
2051 return 0;
2053 else if (!s || !len)
2055 *_errno() = EINVAL;
2056 return EINVAL;
2058 if (get_mbcinfo()->ismbcodepage)
2060 unsigned int c;
2061 for ( ; *s && len > 0; len--)
2063 c = _mbctolower(_mbsnextc(s));
2064 /* Note that I assume that the size of the character is unchanged */
2065 if (c > 255)
2067 *s++=(c>>8);
2068 c=c & 0xff;
2070 *s++=c;
2073 else for ( ; *s && len > 0; s++, len--) *s = _tolower_l(*s, NULL);
2074 if (*s)
2076 *s = '\0';
2077 *_errno() = EINVAL;
2078 return EINVAL;
2080 return 0;
2084 /*********************************************************************
2085 * _mbsupr(MSVCRT.@)
2087 unsigned char* CDECL _mbsupr(unsigned char* s)
2089 unsigned char *ret = s;
2090 if (!s)
2091 return NULL;
2092 if (get_mbcinfo()->ismbcodepage)
2094 unsigned int c;
2095 while (*s)
2097 c = _mbctoupper(_mbsnextc(s));
2098 /* Note that I assume that the size of the character is unchanged */
2099 if (c > 255)
2101 *s++=(c>>8);
2102 c=c & 0xff;
2104 *s++=c;
2107 else for ( ; *s; s++) *s = _toupper_l(*s, NULL);
2108 return ret;
2112 /*********************************************************************
2113 * _mbsupr_s(MSVCRT.@)
2115 int CDECL _mbsupr_s(unsigned char* s, size_t len)
2117 if (!s && !len)
2119 return 0;
2121 else if (!s || !len)
2123 *_errno() = EINVAL;
2124 return EINVAL;
2126 if (get_mbcinfo()->ismbcodepage)
2128 unsigned int c;
2129 for ( ; *s && len > 0; len--)
2131 c = _mbctoupper(_mbsnextc(s));
2132 /* Note that I assume that the size of the character is unchanged */
2133 if (c > 255)
2135 *s++=(c>>8);
2136 c=c & 0xff;
2138 *s++=c;
2141 else for ( ; *s && len > 0; s++, len--) *s = _toupper_l(*s, NULL);
2142 if (*s)
2144 *s = '\0';
2145 *_errno() = EINVAL;
2146 return EINVAL;
2148 return 0;
2151 /*********************************************************************
2152 * _mbsspn_l (MSVCRT.@)
2154 size_t CDECL _mbsspn_l(const unsigned char* string,
2155 const unsigned char* set, _locale_t locale)
2157 const unsigned char *p, *q;
2159 for (p = string; *p; p++)
2161 for (q = set; *q; q++)
2163 if (_ismbblead_l(*q, locale))
2165 /* duplicate a bug in native implementation */
2166 if (!q[1]) break;
2168 if (p[0] == q[0] && p[1] == q[1])
2170 p++;
2171 break;
2173 q++;
2175 else
2177 if (p[0] == q[0]) break;
2180 if (!*q) break;
2182 return p - string;
2185 /*********************************************************************
2186 * _mbsspn (MSVCRT.@)
2188 size_t CDECL _mbsspn(const unsigned char* string, const unsigned char* set)
2190 return _mbsspn_l(string, set, NULL);
2193 /*********************************************************************
2194 * _mbsspnp (MSVCRT.@)
2196 unsigned char* CDECL _mbsspnp(const unsigned char* string, const unsigned char* set)
2198 string += _mbsspn( string, set );
2199 return *string ? (unsigned char*)string : NULL;
2202 /*********************************************************************
2203 * _mbscspn_l (MSVCRT.@)
2205 size_t CDECL _mbscspn_l(const unsigned char* str,
2206 const unsigned char* cmp, _locale_t locale)
2208 const unsigned char *p, *q;
2210 for (p = str; *p; p++)
2212 for (q = cmp; *q; q++)
2214 if (_ismbblead_l(*q, locale))
2216 /* duplicate a bug in native implementation */
2217 if (!q[1]) return 0;
2219 if (p[0] == q[0] && p[1] == q[1])
2220 return p - str;
2221 q++;
2223 else if (p[0] == q[0])
2224 return p - str;
2227 return p - str;
2230 /*********************************************************************
2231 * _mbscspn (MSVCRT.@)
2233 size_t CDECL _mbscspn(const unsigned char* str, const unsigned char* cmp)
2235 return _mbscspn_l(str, cmp, NULL);
2238 /*********************************************************************
2239 * _mbsrev (MSVCRT.@)
2241 unsigned char* CDECL _mbsrev(unsigned char* str)
2243 int i, len = _mbslen(str);
2244 unsigned char *p, *temp=malloc(len*2);
2246 if(!temp)
2247 return str;
2249 /* unpack multibyte string to temp buffer */
2250 p=str;
2251 for(i=0; i<len; i++)
2253 if (_ismbblead(*p))
2255 temp[i*2]=*p++;
2256 temp[i*2+1]=*p++;
2258 else
2260 temp[i*2]=*p++;
2261 temp[i*2+1]=0;
2265 /* repack it in the reverse order */
2266 p=str;
2267 for(i=len-1; i>=0; i--)
2269 if(_ismbblead(temp[i*2]))
2271 *p++=temp[i*2];
2272 *p++=temp[i*2+1];
2274 else
2276 *p++=temp[i*2];
2280 free(temp);
2282 return str;
2285 /*********************************************************************
2286 * _mbspbrk (MSVCRT.@)
2288 unsigned char* CDECL _mbspbrk(const unsigned char* str, const unsigned char* accept)
2290 const unsigned char* p;
2292 while(*str)
2294 for(p = accept; *p; p += (_ismbblead(*p)?2:1) )
2296 if (*p == *str)
2297 if( !_ismbblead(*p) || ( *(p+1) == *(str+1) ) )
2298 return (unsigned char*)str;
2300 str += (_ismbblead(*str)?2:1);
2302 return NULL;
2307 * Functions depending on locale codepage
2310 /*********************************************************************
2311 * mblen(MSVCRT.@)
2312 * REMARKS
2313 * Unlike most of the multibyte string functions this function uses
2314 * the locale codepage, not the codepage set by _setmbcp
2316 int CDECL mblen(const char* str, size_t size)
2318 if (str && *str && size)
2320 if(get_locinfo()->mb_cur_max == 1)
2321 return 1; /* ASCII CP */
2323 return !isleadbyte((unsigned char)*str) ? 1 : (size>1 ? 2 : -1);
2325 return 0;
2328 /*********************************************************************
2329 * mbrlen(MSVCRT.@)
2331 size_t CDECL mbrlen(const char *str, size_t len, mbstate_t *state)
2333 mbstate_t s = (state ? *state : 0);
2334 size_t ret;
2336 if(!len || !str || !*str)
2337 return 0;
2339 if(get_locinfo()->mb_cur_max == 1) {
2340 return 1;
2341 }else if(!s && isleadbyte((unsigned char)*str)) {
2342 if(len == 1) {
2343 s = (unsigned char)*str;
2344 ret = -2;
2345 }else {
2346 ret = 2;
2348 }else if(!s) {
2349 ret = 1;
2350 }else {
2351 s = 0;
2352 ret = 2;
2355 if(state)
2356 *state = s;
2357 return ret;
2360 /*********************************************************************
2361 * _mbstrlen_l(MSVCRT.@)
2363 size_t CDECL _mbstrlen_l(const char* str, _locale_t locale)
2365 pthreadlocinfo locinfo;
2367 if(!locale)
2368 locinfo = get_locinfo();
2369 else
2370 locinfo = locale->locinfo;
2372 if(locinfo->mb_cur_max > 1) {
2373 size_t len;
2374 len = MultiByteToWideChar(locinfo->lc_codepage, MB_ERR_INVALID_CHARS,
2375 str, -1, NULL, 0);
2376 if (!len) {
2377 *_errno() = EILSEQ;
2378 return -1;
2380 return len - 1;
2383 return strlen(str);
2386 /*********************************************************************
2387 * _mbstrlen(MSVCRT.@)
2389 size_t CDECL _mbstrlen(const char* str)
2391 return _mbstrlen_l(str, NULL);
2394 /*********************************************************************
2395 * _mbtowc_l(MSVCRT.@)
2397 int CDECL _mbtowc_l(wchar_t *dst, const char* str, size_t n, _locale_t locale)
2399 pthreadlocinfo locinfo;
2400 wchar_t tmpdst;
2402 if(!locale)
2403 locinfo = get_locinfo();
2404 else
2405 locinfo = locale->locinfo;
2407 if(n <= 0 || !str)
2408 return 0;
2410 if(!*str) {
2411 if(dst) *dst = 0;
2412 return 0;
2415 if(!locinfo->lc_codepage) {
2416 if(dst) *dst = (unsigned char)*str;
2417 return 1;
2419 if(n>=2 && _isleadbyte_l((unsigned char)*str, locale)) {
2420 if(!MultiByteToWideChar(locinfo->lc_codepage, 0, str, 2, &tmpdst, 1))
2421 return -1;
2422 if(dst) *dst = tmpdst;
2423 return 2;
2425 if(!MultiByteToWideChar(locinfo->lc_codepage, 0, str, 1, &tmpdst, 1))
2426 return -1;
2427 if(dst) *dst = tmpdst;
2428 return 1;
2431 /*********************************************************************
2432 * mbtowc(MSVCRT.@)
2434 int CDECL mbtowc(wchar_t *dst, const char* str, size_t n)
2436 return _mbtowc_l(dst, str, n, NULL);
2439 /*********************************************************************
2440 * btowc(MSVCRT.@)
2442 wint_t CDECL btowc(int c)
2444 unsigned char letter = c;
2445 wchar_t ret;
2447 if(c == EOF)
2448 return WEOF;
2449 if(!get_locinfo()->lc_codepage)
2450 return c & 255;
2451 if(!MultiByteToWideChar(get_locinfo()->lc_codepage,
2452 MB_ERR_INVALID_CHARS, (LPCSTR)&letter, 1, &ret, 1))
2453 return WEOF;
2455 return ret;
2458 /*********************************************************************
2459 * mbrtowc(MSVCRT.@)
2461 size_t CDECL mbrtowc(wchar_t *dst, const char *str,
2462 size_t n, mbstate_t *state)
2464 pthreadlocinfo locinfo = get_locinfo();
2465 mbstate_t s = (state ? *state : 0);
2466 char tmpstr[2];
2467 int len = 0;
2469 if(dst)
2470 *dst = 0;
2472 if(!n || !str || !*str)
2473 return 0;
2475 if(locinfo->mb_cur_max == 1) {
2476 tmpstr[len++] = *str;
2477 }else if(!s && isleadbyte((unsigned char)*str)) {
2478 if(n == 1) {
2479 s = (unsigned char)*str;
2480 len = -2;
2481 }else {
2482 tmpstr[0] = str[0];
2483 tmpstr[1] = str[1];
2484 len = 2;
2486 }else if(!s) {
2487 tmpstr[len++] = *str;
2488 }else {
2489 tmpstr[0] = s;
2490 tmpstr[1] = *str;
2491 len = 2;
2492 s = 0;
2495 if(len > 0) {
2496 if(!MultiByteToWideChar(locinfo->lc_codepage, 0, tmpstr, len, dst, dst ? 1 : 0))
2497 len = -1;
2500 if(state)
2501 *state = s;
2502 return len;
2505 /*********************************************************************
2506 * _mbstowcs_l(MSVCRT.@)
2508 size_t CDECL _mbstowcs_l(wchar_t *wcstr, const char *mbstr,
2509 size_t count, _locale_t locale)
2511 pthreadlocinfo locinfo;
2512 size_t i, size;
2514 if(!mbstr) {
2515 *_errno() = EINVAL;
2516 return -1;
2519 if(!locale)
2520 locinfo = get_locinfo();
2521 else
2522 locinfo = locale->locinfo;
2524 if(!locinfo->lc_codepage) {
2525 if(!wcstr)
2526 return strlen(mbstr);
2528 for(i=0; i<count; i++) {
2529 wcstr[i] = (unsigned char)mbstr[i];
2530 if(!wcstr[i]) break;
2532 return i;
2535 /* Ignore count parameter */
2536 if(!wcstr) {
2537 size = MultiByteToWideChar(locinfo->lc_codepage,
2538 MB_ERR_INVALID_CHARS, mbstr, -1, NULL, 0);
2539 if(!size) {
2540 *_errno() = EILSEQ;
2541 return -1;
2543 return size - 1;
2546 for(i=0, size=0; i<count; i++) {
2547 if(mbstr[size] == '\0')
2548 break;
2550 size += (_isleadbyte_l((unsigned char)mbstr[size], locale) ? 2 : 1);
2553 if(size) {
2554 size = MultiByteToWideChar(locinfo->lc_codepage,
2555 MB_ERR_INVALID_CHARS, mbstr, size, wcstr, count);
2556 if(!size) {
2557 if(count) wcstr[0] = '\0';
2558 *_errno() = EILSEQ;
2559 return -1;
2563 if(size<count)
2564 wcstr[size] = '\0';
2566 return size;
2569 /*********************************************************************
2570 * mbstowcs(MSVCRT.@)
2572 size_t CDECL mbstowcs(wchar_t *wcstr,
2573 const char *mbstr, size_t count)
2575 return _mbstowcs_l(wcstr, mbstr, count, NULL);
2578 /*********************************************************************
2579 * _mbstowcs_s_l(MSVCRT.@)
2581 int CDECL _mbstowcs_s_l(size_t *ret, wchar_t *wcstr,
2582 size_t size, const char *mbstr, size_t count, _locale_t locale)
2584 size_t conv;
2585 int err = 0;
2587 if(!wcstr && !size) {
2588 conv = _mbstowcs_l(NULL, mbstr, 0, locale);
2589 if(ret)
2590 *ret = conv+1;
2591 return 0;
2594 if (!MSVCRT_CHECK_PMT(wcstr != NULL)) return EINVAL;
2595 if (!MSVCRT_CHECK_PMT(mbstr != NULL)) {
2596 if(size) wcstr[0] = '\0';
2597 return EINVAL;
2600 if(count==MSVCRT__TRUNCATE || size<count)
2601 conv = size;
2602 else
2603 conv = count;
2605 conv = _mbstowcs_l(wcstr, mbstr, conv, locale);
2606 if(conv<size)
2607 wcstr[conv++] = '\0';
2608 else if(conv==size && count==MSVCRT__TRUNCATE && wcstr[conv-1]!='\0') {
2609 wcstr[conv-1] = '\0';
2610 err = STRUNCATE;
2611 }else if(conv==size && wcstr[conv-1]!='\0') {
2612 MSVCRT_INVALID_PMT("wcstr[size] is too small", ERANGE);
2613 if(size)
2614 wcstr[0] = '\0';
2615 return ERANGE;
2618 if(ret)
2619 *ret = conv;
2620 return err;
2623 /*********************************************************************
2624 * mbstowcs_s(MSVCRT.@)
2626 int CDECL _mbstowcs_s(size_t *ret, wchar_t *wcstr,
2627 size_t size, const char *mbstr, size_t count)
2629 return _mbstowcs_s_l(ret, wcstr, size, mbstr, count, NULL);
2632 /*********************************************************************
2633 * mbsrtowcs(MSVCRT.@)
2635 size_t CDECL mbsrtowcs(wchar_t *wcstr,
2636 const char **pmbstr, size_t count, mbstate_t *state)
2638 mbstate_t s = (state ? *state : 0);
2639 wchar_t tmpdst;
2640 size_t ret = 0;
2641 const char *p;
2643 if(!MSVCRT_CHECK_PMT(pmbstr != NULL))
2644 return -1;
2646 p = *pmbstr;
2647 while(!wcstr || count>ret) {
2648 int ch_len = mbrtowc(&tmpdst, p, 2, &s);
2649 if(wcstr)
2650 wcstr[ret] = tmpdst;
2652 if(ch_len < 0) {
2653 return -1;
2654 }else if(ch_len == 0) {
2655 if(wcstr) *pmbstr = NULL;
2656 return ret;
2659 p += ch_len;
2660 ret++;
2663 if(wcstr) *pmbstr = p;
2664 return ret;
2667 /*********************************************************************
2668 * mbsrtowcs_s(MSVCRT.@)
2670 int CDECL mbsrtowcs_s(size_t *ret, wchar_t *wcstr, size_t len,
2671 const char **mbstr, size_t count, mbstate_t *state)
2673 size_t tmp;
2675 if(!ret) ret = &tmp;
2676 if(!MSVCRT_CHECK_PMT(!!wcstr == !!len)) {
2677 *ret = -1;
2678 return EINVAL;
2681 *ret = mbsrtowcs(wcstr, mbstr, count>len ? len : count, state);
2682 if(*ret == -1) {
2683 if(wcstr) *wcstr = 0;
2684 return *_errno();
2686 (*ret)++;
2687 if(*ret > len) {
2688 /* no place for terminating '\0' */
2689 if(wcstr) *wcstr = 0;
2690 return 0;
2692 if(wcstr) wcstr[(*ret)-1] = 0;
2693 return 0;
2696 /*********************************************************************
2697 * _mbctohira (MSVCRT.@)
2699 * Converts a sjis katakana character to hiragana.
2701 unsigned int CDECL _mbctohira(unsigned int c)
2703 if(_ismbckata(c) && c <= 0x8393)
2704 return (c - 0x8340 - (c >= 0x837f ? 1 : 0)) + 0x829f;
2705 return c;
2708 /*********************************************************************
2709 * _mbctokata (MSVCRT.@)
2711 * Converts a sjis hiragana character to katakana.
2713 unsigned int CDECL _mbctokata(unsigned int c)
2715 if(_ismbchira(c))
2716 return (c - 0x829f) + 0x8340 + (c >= 0x82de ? 1 : 0);
2717 return c;
2720 /*********************************************************************
2721 * _ismbcl0_l (MSVCRT.@)
2723 int CDECL _ismbcl0_l(unsigned int c, _locale_t locale)
2725 pthreadmbcinfo mbcinfo;
2727 if(!locale)
2728 mbcinfo = get_mbcinfo();
2729 else
2730 mbcinfo = locale->mbcinfo;
2732 if(mbcinfo->mbcodepage == 932)
2734 /* JIS non-Kanji */
2735 return _ismbclegal_l(c, locale) && c >= 0x8140 && c <= 0x889e;
2738 return 0;
2741 /*********************************************************************
2742 * _ismbcl0 (MSVCRT.@)
2744 int CDECL _ismbcl0(unsigned int c)
2746 return _ismbcl0_l(c, NULL);
2749 /*********************************************************************
2750 * _ismbcl1_l (MSVCRT.@)
2752 int CDECL _ismbcl1_l(unsigned int c, _locale_t locale)
2754 pthreadmbcinfo mbcinfo;
2756 if(!locale)
2757 mbcinfo = get_mbcinfo();
2758 else
2759 mbcinfo = locale->mbcinfo;
2761 if(mbcinfo->mbcodepage == 932)
2763 /* JIS level-1 */
2764 return _ismbclegal_l(c, locale) && c >= 0x889f && c <= 0x9872;
2767 return 0;
2770 /*********************************************************************
2771 * _ismbcl1 (MSVCRT.@)
2773 int CDECL _ismbcl1(unsigned int c)
2775 return _ismbcl1_l(c, NULL);
2778 /*********************************************************************
2779 * _ismbcl2_l (MSVCRT.@)
2781 int CDECL _ismbcl2_l(unsigned int c, _locale_t locale)
2783 pthreadmbcinfo mbcinfo;
2785 if(!locale)
2786 mbcinfo = get_mbcinfo();
2787 else
2788 mbcinfo = locale->mbcinfo;
2790 if(mbcinfo->mbcodepage == 932)
2792 /* JIS level-2 */
2793 return _ismbclegal_l(c, locale) && c >= 0x989f && c <= 0xeaa4;
2796 return 0;
2799 /*********************************************************************
2800 * _ismbcl2 (MSVCRT.@)
2802 int CDECL _ismbcl2(unsigned int c)
2804 return _ismbcl2_l(c, NULL);