winmm: Avoid explicitly casting the pointer returned from Heap(Re)Alloc.
[wine.git] / dlls / msvcrt / mbcs.c
blobc8390288d4a5fe9220d88f6ba32f69128b2254bd
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>
29 #include <mbstring.h>
31 #include "msvcrt.h"
32 #include "mtdll.h"
33 #include "winnls.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
38 unsigned char MSVCRT_mbctype[257] = { 0 };
40 /* It seems that the data about valid trail bytes is not available from kernel32
41 * so we have to store is here. The format is the same as for lead bytes in CPINFO */
42 struct cp_extra_info_t
44 int cp;
45 BYTE TrailBytes[MAX_LEADBYTES];
48 static struct cp_extra_info_t g_cpextrainfo[] =
50 {932, {0x40, 0x7e, 0x80, 0xfc, 0, 0}},
51 {936, {0x40, 0xfe, 0, 0}},
52 {949, {0x41, 0xfe, 0, 0}},
53 {950, {0x40, 0x7e, 0xa1, 0xfe, 0, 0}},
54 {1361, {0x31, 0x7e, 0x81, 0xfe, 0, 0}},
55 {20932, {1, 255, 0, 0}}, /* seems to give different results on different systems */
56 {0, {1, 255, 0, 0}} /* match all with FIXME */
59 /* Maps cp932 single byte character to multi byte character */
60 static const unsigned char mbbtombc_932[] = {
61 0x40,0x49,0x68,0x94,0x90,0x93,0x95,0x66,0x69,0x6a,0x96,0x7b,0x43,0x7c,0x44,0x5e,
62 0x4f,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x46,0x47,0x83,0x81,0x84,0x48,
63 0x97,0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,
64 0x6f,0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x6d,0x8f,0x6e,0x4f,0x76,
65 0x77,0x78,0x79,0x6d,0x8f,0x6e,0x4f,0x51,0x65,0x81,0x82,0x83,0x84,0x85,0x86,0x87,
66 0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f,0x90,0x91,0x92,0x93,0x94,0x95,0x50,
67 0x42,0x75,0x76,0x41,0x45,0x92,0x40,0x42,0x44,0x46,0x48,0x83,0x85,0x87,0x62,
68 0x5b,0x41,0x43,0x45,0x47,0x49,0x4a,0x4c,0x4e,0x50,0x52,0x54,0x56,0x58,0x5a,0x5c,
69 0x5e,0x60,0x63,0x65,0x67,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x71,0x74,0x77,0x7a,0x7d,
70 0x7e,0x80,0x81,0x82,0x84,0x86,0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8f,0x93,0x4a,0x4b };
72 /* Maps multibyte cp932 punctuation marks to single byte equivalents */
73 static const unsigned char mbctombb_932_punct[] = {
74 0x20,0xa4,0xa1,0x2c,0x2e,0xa5,0x3a,0x3b,0x3f,0x21,0xde,0xdf,0x00,0x00,0x00,0x5e,
75 0x7e,0x5f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xb0,0x00,0x00,0x2f,0x00,
76 0x00,0x00,0x7c,0x00,0x00,0x60,0x27,0x00,0x22,0x28,0x29,0x00,0x00,0x5b,0x5d,0x7b,
77 0x7d,0x00,0x00,0x00,0x00,0xa2,0xa3,0x00,0x00,0x00,0x00,0x2b,0x2d,0x00,0x00,0x00,
78 0x00,0x3d,0x00,0x3c,0x3e,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x5c,
79 0x24,0x00,0x00,0x25,0x23,0x26,0x2a,0x40};
81 /* Maps multibyte cp932 hiragana/katakana to single-byte equivalents */
82 static const unsigned char mbctombb_932_kana[] = {
83 0xa7,0xb1,0xa8,0xb2,0xa9,0xb3,0xaa,0xb4,0xab,0xb5,0xb6,0xb6,0xb7,0xb7,0xb8,0xb8,
84 0xb9,0xb9,0xba,0xba,0xbb,0xbb,0xbc,0xbc,0xbd,0xbd,0xbe,0xbe,0xbf,0xbf,0xc0,0xc0,
85 0xc1,0xc1,0xaf,0xc2,0xc2,0xc3,0xc3,0xc4,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xca,
86 0xca,0xcb,0xcb,0xcb,0xcc,0xcc,0xcc,0xcd,0xcd,0xcd,0xce,0xce,0xce,0xcf,0xd0,0xd1,
87 0xd2,0xd3,0xac,0xd4,0xad,0xd5,0xae,0xd6,0xd7,0xd8,0xd9,0xda,0xdb,0xdc,0xdc,0xb2,
88 0xb4,0xa6,0xdd,0xb3,0xb6,0xb9};
90 static wchar_t msvcrt_mbc_to_wc_l(unsigned int ch, _locale_t locale)
92 pthreadmbcinfo mbcinfo;
93 wchar_t chW;
94 char mbch[2];
95 int n_chars;
97 if(locale)
98 mbcinfo = locale->mbcinfo;
99 else
100 mbcinfo = get_mbcinfo();
102 if (ch <= 0xff) {
103 mbch[0] = ch;
104 n_chars = 1;
105 } else {
106 mbch[0] = (ch >> 8) & 0xff;
107 mbch[1] = ch & 0xff;
108 n_chars = 2;
110 if (!MultiByteToWideChar(mbcinfo->mbcodepage, 0, mbch, n_chars, &chW, 1))
112 WARN("MultiByteToWideChar failed on %x\n", ch);
113 return 0;
115 return chW;
118 static inline size_t u_strlen( const unsigned char *str )
120 return strlen( (const char*) str );
123 static inline unsigned char* u_strncat( unsigned char* dst, const unsigned char* src, size_t len )
125 return (unsigned char*)strncat( (char*)dst, (const char*)src, len);
128 static inline int u_strcmp( const unsigned char *s1, const unsigned char *s2 )
130 return strcmp( (const char*)s1, (const char*)s2 );
133 static inline int u_strcasecmp( const unsigned char *s1, const unsigned char *s2 )
135 return _stricmp( (const char*)s1, (const char*)s2 );
138 static inline int u_strncmp( const unsigned char *s1, const unsigned char *s2, size_t len )
140 return strncmp( (const char*)s1, (const char*)s2, len );
143 static inline int u_strncasecmp( const unsigned char *s1, const unsigned char *s2, size_t len )
145 return _strnicmp( (const char*)s1, (const char*)s2, len );
148 static inline unsigned char *u_strchr( const unsigned char *s, unsigned char x )
150 return (unsigned char*) strchr( (const char*)s, x );
153 static inline unsigned char *u_strrchr( const unsigned char *s, unsigned char x )
155 return (unsigned char*) strrchr( (const char*)s, x );
158 static inline unsigned char *u__strset( unsigned char *s, unsigned char c )
160 return (unsigned char*) _strset( (char*)s, c);
163 static inline unsigned char *u__strnset( unsigned char *s, unsigned char c, size_t len )
165 return (unsigned char*) _strnset( (char*)s, c, len );
168 /*********************************************************************
169 * __p__mbctype (MSVCRT.@)
171 unsigned char* CDECL __p__mbctype(void)
173 return get_mbcinfo()->mbctype;
176 /*********************************************************************
177 * __p___mb_cur_max(MSVCRT.@)
179 int* CDECL __p___mb_cur_max(void)
181 return &get_locinfo()->mb_cur_max;
184 /*********************************************************************
185 * ___mb_cur_max_func(MSVCRT.@)
187 int CDECL ___mb_cur_max_func(void)
189 return get_locinfo()->mb_cur_max;
192 #if _MSVCR_VER>=80
193 /*********************************************************************
194 * ___mb_cur_max_l_func (MSVCR80.@)
196 int CDECL ___mb_cur_max_l_func(_locale_t locale)
198 pthreadlocinfo locinfo;
200 if(!locale)
201 locinfo = get_locinfo();
202 else
203 locinfo = locale->locinfo;
205 return locinfo->mb_cur_max;
207 #endif
209 threadmbcinfo* create_mbcinfo(int cp, LCID lcid, threadmbcinfo *old_mbcinfo)
211 threadmbcinfo *mbcinfo;
212 int newcp;
213 CPINFO cpi;
214 BYTE *bytes;
215 WORD chartypes[256];
216 char bufA[256];
217 WCHAR bufW[256], lowW[256], upW[256];
218 int charcount, maxchar;
219 int ret;
220 int i;
222 if(old_mbcinfo && cp==old_mbcinfo->mbcodepage
223 && (lcid==-1 || lcid==old_mbcinfo->mblcid)) {
224 InterlockedIncrement(&old_mbcinfo->refcount);
225 return old_mbcinfo;
228 mbcinfo = malloc(sizeof(threadmbcinfo));
229 if(!mbcinfo)
230 return NULL;
231 mbcinfo->refcount = 1;
233 switch (cp)
235 case _MB_CP_ANSI:
236 newcp = GetACP();
237 break;
238 case _MB_CP_OEM:
239 newcp = GetOEMCP();
240 break;
241 case _MB_CP_LOCALE:
242 newcp = get_locinfo()->lc_codepage;
243 if(newcp)
244 break;
245 /* fall through (C locale) */
246 case _MB_CP_SBCS:
247 newcp = 20127; /* ASCII */
248 break;
249 default:
250 newcp = cp;
251 break;
254 if(lcid == -1) {
255 WCHAR wbuf[LOCALE_NAME_MAX_LENGTH];
256 sprintf(bufA, ".%d", newcp);
257 mbcinfo->mblcid = locale_to_sname(bufA, NULL, NULL, wbuf) ? LocaleNameToLCID(wbuf, LOCALE_ALLOW_NEUTRAL_NAMES) : -1;
258 } else {
259 mbcinfo->mblcid = lcid;
262 if(mbcinfo->mblcid == -1)
264 WARN("Can't assign LCID to codepage (%d)\n", mbcinfo->mblcid);
265 mbcinfo->mblcid = 0;
268 if (!GetCPInfo(newcp, &cpi))
270 WARN("Codepage %d not found\n", newcp);
271 free(mbcinfo);
272 return NULL;
275 /* setup the _mbctype */
276 memset(mbcinfo->mbctype, 0, sizeof(unsigned char[257]));
277 memset(mbcinfo->mbcasemap, 0, sizeof(unsigned char[256]));
279 bytes = cpi.LeadByte;
280 while (bytes[0] || bytes[1])
282 for (i = bytes[0]; i <= bytes[1]; i++)
283 mbcinfo->mbctype[i + 1] |= _M1;
284 bytes += 2;
287 if (cpi.MaxCharSize == 2)
289 /* trail bytes not available through kernel32 but stored in a structure in msvcrt */
290 struct cp_extra_info_t *cpextra = g_cpextrainfo;
292 mbcinfo->ismbcodepage = 1;
293 while (TRUE)
295 if (cpextra->cp == 0 || cpextra->cp == newcp)
297 if (cpextra->cp == 0)
298 FIXME("trail bytes data not available for DBCS codepage %d - assuming all bytes\n", newcp);
300 bytes = cpextra->TrailBytes;
301 while (bytes[0] || bytes[1])
303 for (i = bytes[0]; i <= bytes[1]; i++)
304 mbcinfo->mbctype[i + 1] |= _M2;
305 bytes += 2;
307 break;
309 cpextra++;
312 else
313 mbcinfo->ismbcodepage = 0;
315 maxchar = (newcp == CP_UTF8) ? 128 : 256;
317 /* we can't use GetStringTypeA directly because we don't have a locale - only a code page
319 charcount = 0;
320 for (i = 0; i < maxchar; i++)
321 if (!(mbcinfo->mbctype[i + 1] & _M1))
322 bufA[charcount++] = i;
324 ret = MultiByteToWideChar(newcp, 0, bufA, charcount, bufW, charcount);
325 if (ret != charcount)
327 ERR("MultiByteToWideChar of chars failed for cp %d, ret=%d (exp %d), error=%ld\n",
328 newcp, ret, charcount, GetLastError());
331 GetStringTypeW(CT_CTYPE1, bufW, charcount, chartypes);
332 LCMapStringW(mbcinfo->mblcid, LCMAP_LOWERCASE, bufW, charcount, lowW, charcount);
333 LCMapStringW(mbcinfo->mblcid, LCMAP_UPPERCASE, bufW, charcount, upW, charcount);
335 charcount = 0;
336 for (i = 0; i < maxchar; i++)
337 if (!(mbcinfo->mbctype[i + 1] & _M1))
339 if (chartypes[charcount] & C1_UPPER)
341 mbcinfo->mbctype[i + 1] |= _SBUP;
342 bufW[charcount] = lowW[charcount];
344 else if (chartypes[charcount] & C1_LOWER)
346 mbcinfo->mbctype[i + 1] |= _SBLOW;
347 bufW[charcount] = upW[charcount];
349 charcount++;
352 ret = WideCharToMultiByte(newcp, 0, bufW, charcount, bufA, charcount, NULL, NULL);
353 if (ret != charcount)
355 ERR("WideCharToMultiByte failed for cp %d, ret=%d (exp %d), error=%ld\n",
356 newcp, ret, charcount, GetLastError());
359 charcount = 0;
360 for (i = 0; i < maxchar; i++)
362 if(!(mbcinfo->mbctype[i + 1] & _M1))
364 if(mbcinfo->mbctype[i + 1] & (_SBUP | _SBLOW))
365 mbcinfo->mbcasemap[i] = bufA[charcount];
366 charcount++;
370 if (newcp == 932) /* CP932 only - set _MP and _MS */
372 /* On Windows it's possible to calculate the _MP and _MS from CT_CTYPE1
373 * and CT_CTYPE3. But as of Wine 0.9.43 we return wrong values what makes
374 * it hard. As this is set only for codepage 932 we hardcode it what gives
375 * also faster execution.
377 for (i = 161; i <= 165; i++)
378 mbcinfo->mbctype[i + 1] |= _MP;
379 for (i = 166; i <= 223; i++)
380 mbcinfo->mbctype[i + 1] |= _MS;
383 mbcinfo->mbcodepage = newcp;
384 return mbcinfo;
387 /*********************************************************************
388 * _setmbcp (MSVCRT.@)
390 int CDECL _setmbcp(int cp)
392 thread_data_t *data = msvcrt_get_thread_data();
393 threadmbcinfo *mbcinfo;
395 mbcinfo = create_mbcinfo(cp, -1, get_mbcinfo());
396 if(!mbcinfo)
398 *_errno() = EINVAL;
399 return -1;
402 if(data->locale_flags & LOCALE_THREAD)
404 if(data->locale_flags & LOCALE_FREE)
405 free_mbcinfo(data->mbcinfo);
406 data->mbcinfo = mbcinfo;
408 else
410 _lock(_MB_CP_LOCK);
411 free_mbcinfo(MSVCRT_locale->mbcinfo);
412 MSVCRT_locale->mbcinfo = mbcinfo;
413 memcpy(MSVCRT_mbctype, MSVCRT_locale->mbcinfo->mbctype, sizeof(MSVCRT_mbctype));
414 _unlock(_MB_CP_LOCK);
416 return 0;
419 /*********************************************************************
420 * _getmbcp (MSVCRT.@)
422 int CDECL _getmbcp(void)
424 return get_mbcinfo()->mbcodepage;
427 /*********************************************************************
428 * _mbsnextc_l(MSVCRT.@)
430 unsigned int CDECL _mbsnextc_l(const unsigned char* str, _locale_t locale)
432 if(_ismbblead_l(*str, locale))
433 return *str << 8 | str[1];
434 return *str;
437 /*********************************************************************
438 * _mbsnextc(MSVCRT.@)
440 unsigned int CDECL _mbsnextc(const unsigned char* str)
442 return _mbsnextc_l(str, NULL);
445 /*********************************************************************
446 * _mbctolower_l(MSVCRT.@)
448 unsigned int CDECL _mbctolower_l(unsigned int c, _locale_t locale)
450 unsigned char str[2], ret[2];
451 pthreadmbcinfo mbcinfo;
453 if(!locale)
454 mbcinfo = get_mbcinfo();
455 else
456 mbcinfo = locale->mbcinfo;
458 if (c > 0xff)
460 if (!_ismbblead_l((c >> 8) & 0xff, locale))
461 return c;
463 str[0] = c >> 8;
464 str[1] = c;
465 switch(__crtLCMapStringA(mbcinfo->mblcid, LCMAP_LOWERCASE,
466 (char*)str, 2, (char*)ret, 2, mbcinfo->mbcodepage, 0))
468 case 0:
469 return c;
470 case 1:
471 return ret[0];
472 default:
473 return ret[1] + (ret[0] << 8);
477 return mbcinfo->mbctype[c + 1] & _SBUP ? mbcinfo->mbcasemap[c] : c;
480 /*********************************************************************
481 * _mbctolower(MSVCRT.@)
483 unsigned int CDECL _mbctolower(unsigned int c)
485 return _mbctolower_l(c, NULL);
488 /*********************************************************************
489 * _mbctoupper_l(MSVCRT.@)
491 unsigned int CDECL _mbctoupper_l(unsigned int c, _locale_t locale)
493 unsigned char str[2], ret[2];
494 pthreadmbcinfo mbcinfo;
496 if(!locale)
497 mbcinfo = get_mbcinfo();
498 else
499 mbcinfo = locale->mbcinfo;
501 if (c > 0xff)
503 if (!_ismbblead_l((c >> 8) & 0xff, locale))
504 return c;
506 str[0] = c >> 8;
507 str[1] = c;
508 switch(__crtLCMapStringA(mbcinfo->mblcid, LCMAP_UPPERCASE,
509 (char*)str, 2, (char*)ret, 2, mbcinfo->mbcodepage, 0))
511 case 0:
512 return c;
513 case 1:
514 return ret[0];
515 default:
516 return ret[1] + (ret[0] << 8);
520 return mbcinfo->mbctype[c + 1] & _SBLOW ? mbcinfo->mbcasemap[c] : c;
523 /*********************************************************************
524 * _mbctoupper(MSVCRT.@)
526 unsigned int CDECL _mbctoupper(unsigned int c)
528 return _mbctoupper_l(c, NULL);
531 /*********************************************************************
532 * _mbctombb_l (MSVCRT.@)
534 unsigned int CDECL _mbctombb_l(unsigned int c, _locale_t locale)
536 pthreadmbcinfo mbcinfo;
537 unsigned int value;
539 if(locale)
540 mbcinfo = locale->mbcinfo;
541 else
542 mbcinfo = get_mbcinfo();
544 if(mbcinfo->mbcodepage == 932)
546 if(c >= 0x829f && c <= 0x82f1) /* Hiragana */
547 return mbctombb_932_kana[c - 0x829f];
548 if(c >= 0x8340 && c <= 0x8396 && c != 0x837f) /* Katakana */
549 return mbctombb_932_kana[c - 0x8340 - (c >= 0x837f ? 1 : 0)];
550 if(c >= 0x8140 && c <= 0x8197) /* Punctuation */
552 value = mbctombb_932_punct[c - 0x8140];
553 return value ? value : c;
555 if((c >= 0x824f && c <= 0x8258) || /* Fullwidth digits */
556 (c >= 0x8260 && c <= 0x8279)) /* Fullwidth capitals letters */
557 return c - 0x821f;
558 if(c >= 0x8281 && c <= 0x829a) /* Fullwidth small letters */
559 return c - 0x8220;
560 /* all other cases return c */
562 return c;
565 /*********************************************************************
566 * _mbctombb (MSVCRT.@)
568 unsigned int CDECL _mbctombb(unsigned int c)
570 return _mbctombb_l(c, NULL);
573 /*********************************************************************
574 * _mbcjistojms_l(MSVCRT.@)
576 * Converts a jis character to sjis.
577 * Based on description from
578 * http://www.slayers.ne.jp/~oouchi/code/jistosjis.html
580 unsigned int CDECL _mbcjistojms_l(unsigned int c, _locale_t locale)
582 pthreadmbcinfo mbcinfo;
584 if(locale)
585 mbcinfo = locale->mbcinfo;
586 else
587 mbcinfo = get_mbcinfo();
589 /* Conversion takes place only when codepage is 932.
590 In all other cases, c is returned unchanged */
591 if(mbcinfo->mbcodepage == 932)
593 if(HIBYTE(c) >= 0x21 && HIBYTE(c) <= 0x7e &&
594 LOBYTE(c) >= 0x21 && LOBYTE(c) <= 0x7e)
596 if(HIBYTE(c) % 2)
597 c += 0x1f;
598 else
599 c += 0x7d;
601 if(LOBYTE(c) >= 0x7F)
602 c += 0x1;
604 c = (((HIBYTE(c) - 0x21)/2 + 0x81) << 8) | LOBYTE(c);
606 if(HIBYTE(c) > 0x9f)
607 c += 0x4000;
609 else
610 return 0; /* Codepage is 932, but c can't be converted */
613 return c;
616 /*********************************************************************
617 * _mbcjistojms(MSVCRT.@)
619 unsigned int CDECL _mbcjistojms(unsigned int c)
621 return _mbcjistojms_l(c, NULL);
624 /*********************************************************************
625 * _mbcjmstojis_l(MSVCRT.@)
627 * Converts a sjis character to jis.
629 unsigned int CDECL _mbcjmstojis_l(unsigned int c, _locale_t locale)
631 pthreadmbcinfo mbcinfo;
633 if(locale)
634 mbcinfo = locale->mbcinfo;
635 else
636 mbcinfo = get_mbcinfo();
638 /* Conversion takes place only when codepage is 932.
639 In all other cases, c is returned unchanged */
640 if(mbcinfo->mbcodepage == 932)
642 if(_ismbclegal_l(c, locale) && HIBYTE(c) < 0xf0)
644 if(HIBYTE(c) >= 0xe0)
645 c -= 0x4000;
647 c = (((HIBYTE(c) - 0x81)*2 + 0x21) << 8) | LOBYTE(c);
649 if(LOBYTE(c) > 0x7f)
650 c -= 0x1;
652 if(LOBYTE(c) > 0x9d)
653 c += 0x83;
654 else
655 c -= 0x1f;
657 else
658 return 0; /* Codepage is 932, but c can't be converted */
661 return c;
664 /*********************************************************************
665 * _mbcjmstojis(MSVCRT.@)
667 unsigned int CDECL _mbcjmstojis(unsigned int c)
669 return _mbcjmstojis_l(c, NULL);
672 /*********************************************************************
673 * _mbsdec(MSVCRT.@)
675 unsigned char* CDECL _mbsdec(const unsigned char* start, const unsigned char* cur)
677 if(start >= cur)
678 return NULL;
680 if(get_mbcinfo()->ismbcodepage)
681 return (unsigned char *)(_ismbstrail(start,cur-1) ? cur - 2 : cur -1);
683 return (unsigned char *)cur - 1; /* ASCII CP or SB char */
686 /*********************************************************************
687 * _mbclen_l(MSVCRT.@)
689 size_t CDECL _mbclen_l(const unsigned char* str, _locale_t locale)
691 return _ismbblead_l(*str, locale) && str[1] ? 2 : 1;
694 /*********************************************************************
695 * _mbclen(MSVCRT.@)
697 size_t CDECL _mbclen(const unsigned char* str)
699 return _mbclen_l(str, NULL);
702 /*********************************************************************
703 * _mbsinc_l(MSVCRT.@)
705 unsigned char* CDECL _mbsinc_l(const unsigned char* str, _locale_t locale)
707 return (unsigned char *)(str + _mbclen_l(str, locale));
710 /*********************************************************************
711 * _mbsinc(MSVCRT.@)
713 unsigned char* CDECL _mbsinc(const unsigned char* str)
715 return _mbsinc_l(str, NULL);
718 /*********************************************************************
719 * _mbsninc(MSVCRT.@)
721 unsigned char* CDECL _mbsninc(const unsigned char* str, size_t num)
723 if(!str)
724 return NULL;
726 while (num > 0 && *str)
728 if (_ismbblead(*str))
730 if (!*(str+1))
731 break;
732 str++;
734 str++;
735 num--;
738 return (unsigned char*)str;
741 /*********************************************************************
742 * _mbsnlen_l(MSVCRT.@)
744 size_t CDECL _mbsnlen_l(const unsigned char *str,
745 size_t maxsize, _locale_t locale)
747 pthreadmbcinfo mbcinfo;
748 size_t i = 0, len = 0;
750 if(!locale)
751 mbcinfo = get_mbcinfo();
752 else
753 mbcinfo = locale->mbcinfo;
755 if(!mbcinfo->ismbcodepage)
756 return strnlen((const char*)str, maxsize);
758 while(i<maxsize && str[i])
760 if (_ismbblead_l(str[i], locale))
762 i++;
763 if (!str[i]) /* count only full chars */
764 break;
766 i++;
767 len++;
769 return i < maxsize ? len : maxsize;
772 /*********************************************************************
773 * _mbslen(MSVCRT.@)
775 size_t CDECL _mbslen(const unsigned char* str)
777 return _mbsnlen_l(str, -1, NULL);
780 /*********************************************************************
781 * _mbslen_l(MSVCRT.@)
783 size_t CDECL _mbslen_l(const unsigned char* str, _locale_t locale)
785 return _mbsnlen_l(str, -1, locale);
788 /*********************************************************************
789 * _mbsnlen(MSVCRT.@)
791 size_t CDECL _mbsnlen(const unsigned char* str, size_t maxsize)
793 return _mbsnlen_l(str, maxsize, NULL);
796 /*********************************************************************
797 * _mbccpy_s_l(MSVCRT.@)
799 int CDECL _mbccpy_s_l(unsigned char* dest, size_t maxsize,
800 int *copied, const unsigned char* src, _locale_t locale)
802 if(copied) *copied = 0;
803 if(!MSVCRT_CHECK_PMT(dest != NULL && maxsize >= 1)) return EINVAL;
804 dest[0] = 0;
805 if(!MSVCRT_CHECK_PMT(src != NULL)) return EINVAL;
807 if(_ismbblead_l(*src, locale)) {
808 if(!src[1]) {
809 if(copied) *copied = 1;
810 *_errno() = EILSEQ;
811 return EILSEQ;
814 if(maxsize < 2) {
815 MSVCRT_INVALID_PMT("dst buffer is too small", ERANGE);
816 return ERANGE;
819 *dest++ = *src++;
820 *dest = *src;
821 if(copied) *copied = 2;
822 }else {
823 *dest = *src;
824 if(copied) *copied = 1;
827 return 0;
830 /*********************************************************************
831 * _mbccpy(MSVCRT.@)
833 void CDECL _mbccpy(unsigned char* dest, const unsigned char* src)
835 _mbccpy_s_l(dest, 2, NULL, src, NULL);
838 /*********************************************************************
839 * _mbccpy_l(MSVCRT.@)
841 void CDECL _mbccpy_l(unsigned char* dest, const unsigned char* src,
842 _locale_t locale)
844 _mbccpy_s_l(dest, 2, NULL, src, locale);
847 /*********************************************************************
848 * _mbccpy_s(MSVCRT.@)
850 int CDECL _mbccpy_s(unsigned char* dest, size_t maxsize,
851 int *copied, const unsigned char* src)
853 return _mbccpy_s_l(dest, maxsize, copied, src, NULL);
856 /*********************************************************************
857 * _mbsncpy(MSVCRT.@)
858 * REMARKS
859 * The parameter n is the number or characters to copy, not the size of
860 * the buffer. Use _mbsnbcpy for a function analogical to strncpy
862 unsigned char* CDECL _mbsncpy(unsigned char* dst, const unsigned char* src, size_t n)
864 unsigned char* ret = dst;
865 if(!n)
866 return dst;
867 if (get_mbcinfo()->ismbcodepage)
869 while (*src && n)
871 n--;
872 if (_ismbblead(*src))
874 if (!*(src+1))
876 *dst++ = 0;
877 *dst++ = 0;
878 break;
881 *dst++ = *src++;
884 *dst++ = *src++;
887 else
889 while (n)
891 n--;
892 if (!(*dst++ = *src++)) break;
895 while (n--) *dst++ = 0;
896 return ret;
899 /*********************************************************************
900 * _mbsnbcpy_s_l(MSVCRT.@)
901 * REMARKS
902 * Unlike _mbsnbcpy this function does not pad the rest of the dest
903 * string with 0
905 int CDECL _mbsnbcpy_s_l(unsigned char* dst, size_t size,
906 const unsigned char* src, size_t n, _locale_t locale)
908 size_t pos = 0;
910 if(!dst || size == 0)
911 return EINVAL;
912 if(!src)
914 dst[0] = '\0';
915 return EINVAL;
917 if(!n)
918 return 0;
920 if((locale ? locale->mbcinfo : get_mbcinfo())->ismbcodepage)
922 BOOL is_lead = FALSE;
923 while (*src && n)
925 if(pos == size)
927 dst[0] = '\0';
928 return ERANGE;
930 is_lead = (!is_lead && _ismbblead(*src));
931 n--;
932 dst[pos++] = *src++;
935 if (is_lead) /* if string ends with a lead, remove it */
936 dst[pos - 1] = 0;
938 else
940 while (n)
942 n--;
943 if(pos == size)
945 dst[0] = '\0';
946 return ERANGE;
949 if(!(*src)) break;
950 dst[pos++] = *src++;
954 if(pos < size)
955 dst[pos] = '\0';
956 else
958 dst[0] = '\0';
959 return ERANGE;
962 return 0;
965 /*********************************************************************
966 * _mbsnbcpy_s(MSVCRT.@)
968 int CDECL _mbsnbcpy_s(unsigned char* dst, size_t size, const unsigned char* src, size_t n)
970 return _mbsnbcpy_s_l(dst, size, src, n, NULL);
973 /*********************************************************************
974 * _mbscpy_s_l(MSVCRT.@)
976 int CDECL _mbscpy_s_l(unsigned char *dst, size_t size,
977 const unsigned char *src, _locale_t locale)
979 return _mbsnbcpy_s_l(dst, size, src, -1, locale);
982 /*********************************************************************
983 * _mbscpy_s(MSVCRT.@)
985 int CDECL _mbscpy_s(unsigned char *dst, size_t size, const unsigned char *src)
987 return _mbscpy_s_l(dst, size, src, NULL);
990 /*********************************************************************
991 * _mbsnbcpy(MSVCRT.@)
992 * REMARKS
993 * Like strncpy this function doesn't enforce the string to be
994 * NUL-terminated
996 unsigned char* CDECL _mbsnbcpy(unsigned char* dst, const unsigned char* src, size_t n)
998 unsigned char* ret = dst;
999 if(!n)
1000 return dst;
1001 if(get_mbcinfo()->ismbcodepage)
1003 BOOL is_lead = FALSE;
1004 while (*src && n)
1006 is_lead = (!is_lead && _ismbblead(*src));
1007 n--;
1008 *dst++ = *src++;
1011 if (is_lead) /* if string ends with a lead, remove it */
1012 *(dst - 1) = 0;
1014 else
1016 while (n)
1018 n--;
1019 if (!(*dst++ = *src++)) break;
1022 while (n--) *dst++ = 0;
1023 return ret;
1026 /*********************************************************************
1027 * _mbscmp_l(MSVCRT.@)
1029 int CDECL _mbscmp_l(const unsigned char* str, const unsigned char* cmp, _locale_t locale)
1031 pthreadmbcinfo mbcinfo;
1033 if (!MSVCRT_CHECK_PMT(str && cmp))
1034 return _NLSCMPERROR;
1036 mbcinfo = locale ? locale->mbcinfo : get_mbcinfo();
1038 if(mbcinfo->ismbcodepage)
1040 unsigned int strc, cmpc;
1041 do {
1042 if(!*str)
1043 return *cmp ? -1 : 0;
1044 if(!*cmp)
1045 return 1;
1046 strc = _mbsnextc_l(str, locale);
1047 cmpc = _mbsnextc_l(cmp, locale);
1048 if(strc != cmpc)
1049 return strc < cmpc ? -1 : 1;
1050 str +=(strc > 255) ? 2 : 1;
1051 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
1052 } while(1);
1054 return u_strcmp(str, cmp); /* ASCII CP */
1057 /*********************************************************************
1058 * _mbscmp(MSVCRT.@)
1060 int CDECL _mbscmp(const unsigned char* str, const unsigned char* cmp)
1062 return _mbscmp_l(str, cmp, NULL);
1065 /*********************************************************************
1066 * _mbsnbicoll_l(MSVCRT.@)
1068 int CDECL _mbsnbicoll_l(const unsigned char *str1, const unsigned char *str2, size_t len, _locale_t locale)
1070 pthreadmbcinfo mbcinfo;
1072 if(!locale)
1073 mbcinfo = get_mbcinfo();
1074 else
1075 mbcinfo = locale->mbcinfo;
1077 if(!mbcinfo->ismbcodepage)
1078 return _strnicoll_l((const char*)str1, (const char*)str2, len, locale);
1079 return CompareStringA(mbcinfo->mblcid, NORM_IGNORECASE, (const char*)str1, len, (const char*)str2, len)-CSTR_EQUAL;
1082 /*********************************************************************
1083 * _mbsicoll_l(MSVCRT.@)
1085 int CDECL _mbsicoll_l(const unsigned char *str1, const unsigned char *str2, _locale_t locale)
1087 return _mbsnbicoll_l(str1, str2, -1, locale);
1090 /*********************************************************************
1091 * _mbsnbicoll(MSVCRT.@)
1093 int CDECL _mbsnbicoll(const unsigned char *str1, const unsigned char *str2, size_t len)
1095 return _mbsnbicoll_l(str1, str2, len, NULL);
1098 /*********************************************************************
1099 * _mbsicoll(MSVCRT.@)
1101 int CDECL _mbsicoll(const unsigned char* str, const unsigned char* cmp)
1103 #if _MSVCR_VER>=60 && _MSVCR_VER<=71
1104 return CompareStringA(get_mbcinfo()->mblcid, NORM_IGNORECASE,
1105 (const char*)str, -1, (const char*)cmp, -1)-CSTR_EQUAL;
1106 #else
1107 return _mbsnbicoll_l(str, cmp, -1, NULL);
1108 #endif
1111 /*********************************************************************
1112 * _mbsnbcoll_l(MSVCRT.@)
1114 int CDECL _mbsnbcoll_l(const unsigned char *str1, const unsigned char *str2, size_t len, _locale_t locale)
1116 pthreadmbcinfo mbcinfo;
1118 if(!locale)
1119 mbcinfo = get_mbcinfo();
1120 else
1121 mbcinfo = locale->mbcinfo;
1123 if(!mbcinfo->ismbcodepage)
1124 return _strncoll_l((const char*)str1, (const char*)str2, len, locale);
1125 return CompareStringA(mbcinfo->mblcid, 0, (const char*)str1, len, (const char*)str2, len)-CSTR_EQUAL;
1128 /*********************************************************************
1129 * _mbscoll_l(MSVCRT.@)
1131 int CDECL _mbscoll_l(const unsigned char *str1, const unsigned char *str2, _locale_t locale)
1133 return _mbsnbcoll_l(str1, str2, -1, locale);
1136 /*********************************************************************
1137 * _mbsnbcoll(MSVCRT.@)
1139 int CDECL _mbsnbcoll(const unsigned char *str1, const unsigned char *str2, size_t len)
1141 return _mbsnbcoll_l(str1, str2, len, NULL);
1144 /*********************************************************************
1145 * _mbscoll(MSVCRT.@)
1147 int CDECL _mbscoll(const unsigned char* str, const unsigned char* cmp)
1149 #if _MSVCR_VER>=60 && _MSVCR_VER<=71
1150 return CompareStringA(get_mbcinfo()->mblcid, 0,
1151 (const char*)str, -1, (const char*)cmp, -1)-CSTR_EQUAL;
1152 #else
1153 return _mbsnbcoll_l(str, cmp, -1, NULL);
1154 #endif
1157 /*********************************************************************
1158 * _mbsicmp_l(MSVCRT.@)
1160 int CDECL _mbsicmp_l(const unsigned char* str, const unsigned char* cmp, _locale_t locale)
1162 pthreadmbcinfo mbcinfo;
1164 if(!MSVCRT_CHECK_PMT(str && cmp))
1165 return _NLSCMPERROR;
1167 if(!locale)
1168 mbcinfo = get_mbcinfo();
1169 else
1170 mbcinfo = locale->mbcinfo;
1171 if(mbcinfo->ismbcodepage)
1173 unsigned int strc, cmpc;
1174 do {
1175 if(!*str)
1176 return *cmp ? -1 : 0;
1177 if(!*cmp)
1178 return 1;
1179 strc = _mbctolower_l(_mbsnextc_l(str, locale), locale);
1180 cmpc = _mbctolower_l(_mbsnextc_l(cmp, locale), locale);
1181 if(strc != cmpc)
1182 return strc < cmpc ? -1 : 1;
1183 str +=(strc > 255) ? 2 : 1;
1184 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
1185 } while(1);
1187 return u_strcasecmp(str, cmp); /* ASCII CP */
1190 /*********************************************************************
1191 * _mbsicmp(MSVCRT.@)
1193 int CDECL _mbsicmp(const unsigned char* str, const unsigned char* cmp)
1195 return _mbsicmp_l(str, cmp, NULL);
1198 /*********************************************************************
1199 * _mbsncmp(MSVCRT.@)
1201 int CDECL _mbsncmp(const unsigned char* str, const unsigned char* cmp, size_t len)
1203 if(!len)
1204 return 0;
1206 if(get_mbcinfo()->ismbcodepage)
1208 unsigned int strc, cmpc;
1209 while(len--)
1211 int inc;
1212 if(!*str)
1213 return *cmp ? -1 : 0;
1214 if(!*cmp)
1215 return 1;
1216 strc = _mbsnextc(str);
1217 cmpc = _mbsnextc(cmp);
1218 if(strc != cmpc)
1219 return strc < cmpc ? -1 : 1;
1220 inc=(strc > 255) ? 2 : 1; /* Equal, use same increment */
1221 str += inc;
1222 cmp += inc;
1224 return 0; /* Matched len chars */
1226 return u_strncmp(str, cmp, len); /* ASCII CP */
1229 /*********************************************************************
1230 * _mbsnbcmp(MSVCRT.@)
1232 int CDECL _mbsnbcmp(const unsigned char* str, const unsigned char* cmp, size_t len)
1234 if (!len)
1235 return 0;
1236 if(get_mbcinfo()->ismbcodepage)
1238 unsigned int strc, cmpc;
1239 while (len)
1241 int clen;
1242 if(!*str)
1243 return *cmp ? -1 : 0;
1244 if(!*cmp)
1245 return 1;
1246 if (_ismbblead(*str))
1248 strc=(len>=2)?_mbsnextc(str):0;
1249 clen=2;
1251 else
1253 strc=*str;
1254 clen=1;
1256 if (_ismbblead(*cmp))
1257 cmpc=(len>=2)?_mbsnextc(cmp):0;
1258 else
1259 cmpc=*cmp;
1260 if(strc != cmpc)
1261 return strc < cmpc ? -1 : 1;
1262 len -= clen;
1263 str += clen;
1264 cmp += clen;
1266 return 0; /* Matched len chars */
1268 return u_strncmp(str,cmp,len);
1271 /*********************************************************************
1272 * _mbsnicmp(MSVCRT.@)
1274 * Compare two multibyte strings case insensitively to 'len' characters.
1276 int CDECL _mbsnicmp(const unsigned char* str, const unsigned char* cmp, size_t len)
1278 /* FIXME: No tolower() for mb strings yet */
1279 if(get_mbcinfo()->ismbcodepage)
1281 unsigned int strc, cmpc;
1282 while(len--)
1284 if(!*str)
1285 return *cmp ? -1 : 0;
1286 if(!*cmp)
1287 return 1;
1288 strc = _mbctolower(_mbsnextc(str));
1289 cmpc = _mbctolower(_mbsnextc(cmp));
1290 if(strc != cmpc)
1291 return strc < cmpc ? -1 : 1;
1292 str +=(strc > 255) ? 2 : 1;
1293 cmp +=(strc > 255) ? 2 : 1; /* Equal, use same increment */
1295 return 0; /* Matched len chars */
1297 return u_strncasecmp(str, cmp, len); /* ASCII CP */
1300 /*********************************************************************
1301 * _mbsnbicmp(MSVCRT.@)
1303 int CDECL _mbsnbicmp(const unsigned char* str, const unsigned char* cmp, size_t len)
1305 if (!len)
1306 return 0;
1307 if(get_mbcinfo()->ismbcodepage)
1309 unsigned int strc, cmpc;
1310 while (len)
1312 int clen;
1313 if(!*str)
1314 return *cmp ? -1 : 0;
1315 if(!*cmp)
1316 return 1;
1317 if (_ismbblead(*str))
1319 strc=(len>=2)?_mbsnextc(str):0;
1320 clen=2;
1322 else
1324 strc=*str;
1325 clen=1;
1327 if (_ismbblead(*cmp))
1328 cmpc=(len>=2)?_mbsnextc(cmp):0;
1329 else
1330 cmpc=*cmp;
1331 strc = _mbctolower(strc);
1332 cmpc = _mbctolower(cmpc);
1333 if(strc != cmpc)
1334 return strc < cmpc ? -1 : 1;
1335 len -= clen;
1336 str += clen;
1337 cmp += clen;
1339 return 0; /* Matched len bytes */
1341 return u_strncasecmp(str,cmp,len);
1344 /*********************************************************************
1345 * _mbscat (MSVCRT.@)
1347 unsigned char * CDECL _mbscat( unsigned char *dst, const unsigned char *src )
1349 strcat( (char *)dst, (const char *)src );
1350 return dst;
1353 /*********************************************************************
1354 * _mbscat_s_l (MSVCRT.@)
1356 int CDECL _mbscat_s_l( unsigned char *dst, size_t size,
1357 const unsigned char *src, _locale_t locale )
1359 size_t i, j;
1360 int ret = 0;
1362 if(!MSVCRT_CHECK_PMT(dst != NULL)) return EINVAL;
1363 if(!MSVCRT_CHECK_PMT(src != NULL)) return EINVAL;
1365 for(i=0; i<size; i++)
1366 if(!dst[i]) break;
1367 if(i == size) {
1368 MSVCRT_INVALID_PMT("dst is not NULL-terminated", EINVAL);
1369 if(size) dst[0] = 0;
1370 return EINVAL;
1373 if(i && _ismbblead_l(dst[i-1], locale)) {
1374 ret = EILSEQ;
1375 i--;
1378 for(j=0; src[j] && i+j<size; j++)
1379 dst[i+j] = src[j];
1380 if(i+j == size) {
1381 MSVCRT_INVALID_PMT("dst buffer is too small", ERANGE);
1382 dst[0] = 0;
1383 return ERANGE;
1386 if(j && _ismbblead_l(src[j-1], locale)) {
1387 ret = EILSEQ;
1388 j--;
1391 dst[i+j] = 0;
1392 return ret;
1395 /*********************************************************************
1396 * _mbscat_s (MSVCRT.@)
1398 int CDECL _mbscat_s( unsigned char *dst, size_t size, const unsigned char *src )
1400 return _mbscat_s_l(dst, size, src, NULL);
1403 /*********************************************************************
1404 * _mbscpy (MSVCRT.@)
1406 unsigned char* CDECL _mbscpy( unsigned char *dst, const unsigned char *src )
1408 strcpy( (char *)dst, (const char *)src );
1409 return dst;
1412 /*********************************************************************
1413 * _mbsstr (MSVCRT.@)
1415 unsigned char * CDECL _mbsstr(const unsigned char *haystack, const unsigned char *needle)
1417 return (unsigned char *)strstr( (const char *)haystack, (const char *)needle );
1420 /*********************************************************************
1421 * _mbschr_l(MSVCRT.@)
1423 unsigned char* CDECL _mbschr_l(const unsigned char* s, unsigned int x, _locale_t locale)
1425 pthreadmbcinfo mbcinfo;
1427 if(!MSVCRT_CHECK_PMT(s))
1428 return NULL;
1430 if(locale)
1431 mbcinfo = locale->mbcinfo;
1432 else
1433 mbcinfo = get_mbcinfo();
1434 if(mbcinfo->ismbcodepage)
1436 unsigned int c;
1437 while (1)
1439 c = _mbsnextc_l(s, locale);
1440 if (c == x)
1441 return (unsigned char*)s;
1442 if (!c)
1443 return NULL;
1444 s += c > 255 ? 2 : 1;
1447 return u_strchr(s, x); /* ASCII CP */
1450 /*********************************************************************
1451 * _mbschr(MSVCRT.@)
1453 unsigned char* CDECL _mbschr(const unsigned char* s, unsigned int x)
1455 return _mbschr_l(s, x, NULL);
1458 /*********************************************************************
1459 * _mbsrchr(MSVCRT.@)
1461 unsigned char* CDECL _mbsrchr(const unsigned char* s, unsigned int x)
1463 if(get_mbcinfo()->ismbcodepage)
1465 unsigned int c;
1466 unsigned char* match=NULL;
1467 if(!s)
1468 return NULL;
1469 while (1) {
1470 c = _mbsnextc(s);
1471 if (c == x)
1472 match=(unsigned char*)s;
1473 if (!c)
1474 return match;
1475 s +=(c > 255) ? 2 : 1;
1478 return u_strrchr(s, x);
1481 /*********************************************************************
1482 * _mbstok_s_l(MSVCRT.@)
1484 unsigned char* CDECL _mbstok_s_l(unsigned char *str, const unsigned char *delim,
1485 unsigned char **ctx, _locale_t locale)
1487 pthreadmbcinfo mbcinfo;
1488 unsigned int c;
1490 if(!MSVCRT_CHECK_PMT(delim != NULL)) return NULL;
1491 if(!MSVCRT_CHECK_PMT(ctx != NULL)) return NULL;
1492 if(!MSVCRT_CHECK_PMT(str || *ctx)) return NULL;
1494 if(locale)
1495 mbcinfo = locale->mbcinfo;
1496 else
1497 mbcinfo = get_mbcinfo();
1499 if(!mbcinfo->ismbcodepage)
1500 return (unsigned char*)strtok_s((char*)str, (const char*)delim, (char**)ctx);
1502 if(!str)
1503 str = *ctx;
1505 while((c=_mbsnextc(str)) && _mbschr(delim, c))
1506 str += c>255 ? 2 : 1;
1507 if(!*str)
1509 *ctx = str;
1510 return NULL;
1513 *ctx = str + (c>255 ? 2 : 1);
1514 while((c=_mbsnextc(*ctx)) && !_mbschr(delim, c))
1515 *ctx += c>255 ? 2 : 1;
1516 if (**ctx) {
1517 *(*ctx)++ = 0;
1518 if(c > 255)
1519 *(*ctx)++ = 0;
1522 return str;
1526 /*********************************************************************
1527 * _mbstok_s(MSVCRT.@)
1529 unsigned char* CDECL _mbstok_s(unsigned char *str,
1530 const unsigned char *delim, unsigned char **ctx)
1532 return _mbstok_s_l(str, delim, ctx, NULL);
1535 /*********************************************************************
1536 * _mbstok_l(MSVCRT.@)
1538 unsigned char* CDECL _mbstok_l(unsigned char *str,
1539 const unsigned char *delim, _locale_t locale)
1541 return _mbstok_s_l(str, delim, &msvcrt_get_thread_data()->mbstok_next, locale);
1544 /*********************************************************************
1545 * _mbstok(MSVCRT.@)
1547 unsigned char* CDECL _mbstok(unsigned char *str, const unsigned char *delim)
1549 thread_data_t *data = msvcrt_get_thread_data();
1551 #if _MSVCR_VER == 0
1552 if(!str && !data->mbstok_next)
1553 return NULL;
1554 #endif
1556 return _mbstok_s_l(str, delim, &data->mbstok_next, NULL);
1559 /*********************************************************************
1560 * _mbbtombc_l(MSVCRT.@)
1562 unsigned int CDECL _mbbtombc_l(unsigned int c, _locale_t locale)
1564 pthreadmbcinfo mbcinfo;
1566 if(locale)
1567 mbcinfo = locale->mbcinfo;
1568 else
1569 mbcinfo = get_mbcinfo();
1571 if(mbcinfo->mbcodepage == 932)
1573 if(c >= 0x20 && c <= 0x7e) {
1574 if((c >= 0x41 && c <= 0x5a) || (c >= 0x61 && c <= 0x7a) || (c >= 0x30 && c <= 0x39))
1575 return mbbtombc_932[c - 0x20] | 0x8200;
1576 else
1577 return mbbtombc_932[c - 0x20] | 0x8100;
1579 else if(c >= 0xa1 && c <= 0xdf) {
1580 if(c >= 0xa6 && c <= 0xdd && c != 0xb0)
1581 return mbbtombc_932[c - 0xa1 + 0x5f] | 0x8300;
1582 else
1583 return mbbtombc_932[c - 0xa1 + 0x5f] | 0x8100;
1586 return c; /* not Japanese or no MB char */
1589 /*********************************************************************
1590 * _mbbtombc(MSVCRT.@)
1592 unsigned int CDECL _mbbtombc(unsigned int c)
1594 return _mbbtombc_l(c, NULL);
1597 /*********************************************************************
1598 * _ismbbkana_l(MSVCRT.@)
1600 int CDECL _ismbbkana_l(unsigned int c, _locale_t locale)
1602 pthreadmbcinfo mbcinfo;
1604 if(locale)
1605 mbcinfo = locale->mbcinfo;
1606 else
1607 mbcinfo = get_mbcinfo();
1609 if(mbcinfo->mbcodepage == 932)
1611 /* Japanese/Katakana, CP 932 */
1612 return (c >= 0xa1 && c <= 0xdf);
1614 return 0;
1617 /*********************************************************************
1618 * _ismbbkana(MSVCRT.@)
1620 int CDECL _ismbbkana(unsigned int c)
1622 return _ismbbkana_l( c, NULL );
1625 /*********************************************************************
1626 * _ismbcdigit_l(MSVCRT.@)
1628 int CDECL _ismbcdigit_l(unsigned int ch, _locale_t locale)
1630 return _iswdigit_l( msvcrt_mbc_to_wc_l(ch, locale), locale );
1633 /*********************************************************************
1634 * _ismbcdigit(MSVCRT.@)
1636 int CDECL _ismbcdigit(unsigned int ch)
1638 return _ismbcdigit_l( ch, NULL );
1641 /*********************************************************************
1642 * _ismbcgraph_l(MSVCRT.@)
1644 int CDECL _ismbcgraph_l(unsigned int ch, _locale_t locale)
1646 return _iswgraph_l( msvcrt_mbc_to_wc_l(ch, locale), locale );
1649 /*********************************************************************
1650 * _ismbcgraph(MSVCRT.@)
1652 int CDECL _ismbcgraph(unsigned int ch)
1654 return _ismbcgraph_l( ch, NULL );
1657 /*********************************************************************
1658 * _ismbcalpha_l (MSVCRT.@)
1660 int CDECL _ismbcalpha_l(unsigned int ch, _locale_t locale)
1662 return _iswalpha_l( msvcrt_mbc_to_wc_l(ch, locale), locale );
1665 /*********************************************************************
1666 * _ismbcalpha (MSVCRT.@)
1668 int CDECL _ismbcalpha(unsigned int ch)
1670 return _ismbcalpha_l( ch, NULL );
1673 /*********************************************************************
1674 * _ismbclower_l (MSVCRT.@)
1676 int CDECL _ismbclower_l(unsigned int ch, _locale_t locale)
1678 return _iswlower_l( msvcrt_mbc_to_wc_l(ch, locale), locale );
1681 /*********************************************************************
1682 * _ismbclower (MSVCRT.@)
1684 int CDECL _ismbclower(unsigned int ch)
1686 return _ismbclower_l( ch, NULL );
1689 /*********************************************************************
1690 * _ismbcupper_l (MSVCRT.@)
1692 int CDECL _ismbcupper_l(unsigned int ch, _locale_t locale)
1694 return _iswupper_l( msvcrt_mbc_to_wc_l(ch, locale), locale );
1697 /*********************************************************************
1698 * _ismbcupper (MSVCRT.@)
1700 int CDECL _ismbcupper(unsigned int ch)
1702 return _ismbcupper_l( ch, NULL );
1705 /*********************************************************************
1706 * _ismbcsymbol_l(MSVCRT.@)
1708 int CDECL _ismbcsymbol_l(unsigned int ch, _locale_t locale)
1710 wchar_t wch = msvcrt_mbc_to_wc_l( ch, locale );
1711 WORD ctype;
1712 if (!GetStringTypeW(CT_CTYPE3, &wch, 1, &ctype))
1714 WARN("GetStringTypeW failed on %x\n", ch);
1715 return 0;
1717 return ((ctype & C3_SYMBOL) != 0);
1720 /*********************************************************************
1721 * _ismbcsymbol(MSVCRT.@)
1723 int CDECL _ismbcsymbol(unsigned int ch)
1725 return _ismbcsymbol_l(ch, NULL);
1728 /*********************************************************************
1729 * _ismbcalnum_l (MSVCRT.@)
1731 int CDECL _ismbcalnum_l(unsigned int ch, _locale_t locale)
1733 return _iswalnum_l( msvcrt_mbc_to_wc_l(ch, locale), locale );
1736 /*********************************************************************
1737 * _ismbcalnum (MSVCRT.@)
1739 int CDECL _ismbcalnum(unsigned int ch)
1741 return _ismbcalnum_l( ch, NULL );
1744 /*********************************************************************
1745 * _ismbcspace_l (MSVCRT.@)
1747 int CDECL _ismbcspace_l(unsigned int ch, _locale_t locale)
1749 return _iswspace_l( msvcrt_mbc_to_wc_l(ch, locale), locale );
1752 /*********************************************************************
1753 * _ismbcspace (MSVCRT.@)
1755 int CDECL _ismbcspace(unsigned int ch)
1757 return _ismbcspace_l( ch, NULL );
1760 /*********************************************************************
1761 * _ismbcprint_l (MSVCRT.@)
1763 int CDECL _ismbcprint_l(unsigned int ch, _locale_t locale)
1765 return _iswprint_l( msvcrt_mbc_to_wc_l(ch, locale), locale );
1768 /*********************************************************************
1769 * _ismbcprint (MSVCRT.@)
1771 int CDECL _ismbcprint(unsigned int ch)
1773 return _ismbcprint_l( ch, NULL );
1776 /*********************************************************************
1777 * _ismbcpunct_l (MSVCRT.@)
1779 int CDECL _ismbcpunct_l(unsigned int ch, _locale_t locale)
1781 return _iswpunct_l( msvcrt_mbc_to_wc_l(ch, locale), locale );
1784 /*********************************************************************
1785 * _ismbcpunct(MSVCRT.@)
1787 int CDECL _ismbcpunct(unsigned int ch)
1789 return _ismbcpunct_l( ch, NULL );
1792 /*********************************************************************
1793 * _ismbchira_l(MSVCRT.@)
1795 int CDECL _ismbchira_l(unsigned int c, _locale_t locale)
1797 pthreadmbcinfo mbcinfo;
1799 if(locale)
1800 mbcinfo = locale->mbcinfo;
1801 else
1802 mbcinfo = get_mbcinfo();
1804 if(mbcinfo->mbcodepage == 932)
1806 /* Japanese/Hiragana, CP 932 */
1807 return (c >= 0x829f && c <= 0x82f1);
1809 return 0;
1812 /*********************************************************************
1813 * _ismbchira(MSVCRT.@)
1815 int CDECL _ismbchira(unsigned int c)
1817 return _ismbchira_l(c, NULL);
1820 /*********************************************************************
1821 * _ismbckata_l(MSVCRT.@)
1823 int CDECL _ismbckata_l(unsigned int c, _locale_t locale)
1825 pthreadmbcinfo mbcinfo;
1827 if(locale)
1828 mbcinfo = locale->mbcinfo;
1829 else
1830 mbcinfo = get_mbcinfo();
1832 if(mbcinfo->mbcodepage == 932)
1834 /* Japanese/Katakana, CP 932 */
1835 return (c >= 0x8340 && c <= 0x8396 && c != 0x837f);
1837 return 0;
1840 /*********************************************************************
1841 * _ismbckata(MSVCRT.@)
1843 int CDECL _ismbckata(unsigned int c)
1845 return _ismbckata_l(c, NULL);
1848 /*********************************************************************
1849 * _ismbblead_l(MSVCRT.@)
1851 int CDECL _ismbblead_l(unsigned int c, _locale_t locale)
1853 pthreadmbcinfo mbcinfo;
1855 if(!locale)
1856 mbcinfo = get_mbcinfo();
1857 else
1858 mbcinfo = locale->mbcinfo;
1860 return (mbcinfo->mbctype[(c&0xff) + 1] & _M1) != 0;
1863 /*********************************************************************
1864 * _ismbblead(MSVCRT.@)
1866 int CDECL _ismbblead(unsigned int c)
1868 return _ismbblead_l(c, NULL);
1871 /*********************************************************************
1872 * _ismbbtrail_l(MSVCRT.@)
1874 int CDECL _ismbbtrail_l(unsigned int c, _locale_t locale)
1876 pthreadmbcinfo mbcinfo;
1878 if(!locale)
1879 mbcinfo = get_mbcinfo();
1880 else
1881 mbcinfo = locale->mbcinfo;
1883 return (mbcinfo->mbctype[(c&0xff) + 1] & _M2) != 0;
1886 /*********************************************************************
1887 * _ismbbtrail(MSVCRT.@)
1889 int CDECL _ismbbtrail(unsigned int c)
1891 return _ismbbtrail_l(c, NULL);
1894 /*********************************************************************
1895 * _ismbclegal_l(MSVCRT.@)
1897 int CDECL _ismbclegal_l(unsigned int c, _locale_t locale)
1899 return _ismbblead_l(HIBYTE(c), locale) && _ismbbtrail_l(LOBYTE(c), locale);
1902 /*********************************************************************
1903 * _ismbclegal(MSVCRT.@)
1905 int CDECL _ismbclegal(unsigned int c)
1907 return _ismbclegal_l(c, NULL);
1910 /*********************************************************************
1911 * _ismbslead_l(MSVCRT.@)
1913 int CDECL _ismbslead_l(const unsigned char* start, const unsigned char* str, _locale_t locale)
1915 pthreadmbcinfo mbcinfo;
1916 int lead = 0;
1918 if (!MSVCRT_CHECK_PMT(start && str))
1919 return 0;
1921 if(locale)
1922 mbcinfo = locale->mbcinfo;
1923 else
1924 mbcinfo = get_mbcinfo();
1926 if(!mbcinfo->ismbcodepage)
1927 return 0;
1929 /* Lead bytes can also be trail bytes so we need to analyse the string
1931 while (start <= str)
1933 if (!*start)
1934 return 0;
1935 lead = !lead && _ismbblead_l(*start, locale);
1936 start++;
1939 return lead ? -1 : 0;
1942 /*********************************************************************
1943 * _ismbslead(MSVCRT.@)
1945 int CDECL _ismbslead(const unsigned char* start, const unsigned char* str)
1947 return _ismbslead_l(start, str, NULL);
1950 /*********************************************************************
1951 * _ismbstrail_l(MSVCRT.@)
1953 int CDECL _ismbstrail_l(const unsigned char* start, const unsigned char* str, _locale_t locale)
1955 if (!MSVCRT_CHECK_PMT(start && str))
1956 return 0;
1958 /* Note: this function doesn't check _ismbbtrail */
1959 if ((str > start) && _ismbslead_l(start, str-1, locale))
1960 return -1;
1961 else
1962 return 0;
1965 /*********************************************************************
1966 * _ismbstrail(MSVCRT.@)
1968 int CDECL _ismbstrail(const unsigned char* start, const unsigned char* str)
1970 return _ismbstrail_l(start, str, NULL);
1973 /*********************************************************************
1974 * _mbbtype_l(MSVCRT.@)
1976 int CDECL _mbbtype_l(unsigned char c, int type, _locale_t locale)
1978 if (type == 1)
1979 return _ismbbtrail_l(c, locale) ? _MBC_TRAIL : _MBC_ILLEGAL;
1980 else
1981 return _ismbblead_l(c, locale) ? _MBC_LEAD
1982 : _isprint_l(c, locale) ? _MBC_SINGLE : _MBC_ILLEGAL;
1985 /*********************************************************************
1986 * _mbbtype(MSVCRT.@)
1988 int CDECL _mbbtype(unsigned char c, int type)
1990 return _mbbtype_l(c, type, NULL);
1993 /*********************************************************************
1994 * _mbsbtype (MSVCRT.@)
1996 int CDECL _mbsbtype(const unsigned char *str, size_t count)
1998 int lead = 0;
1999 const unsigned char *end = str + count;
2001 /* Lead bytes can also be trail bytes so we need to analyse the string.
2002 * Also we must return _MBC_ILLEGAL for chars past the end of the string
2004 while (str < end) /* Note: we skip the last byte - will check after the loop */
2006 if (!*str)
2007 return _MBC_ILLEGAL;
2008 lead = get_mbcinfo()->ismbcodepage && !lead && _ismbblead(*str);
2009 str++;
2012 if (lead)
2013 if (_ismbbtrail(*str))
2014 return _MBC_TRAIL;
2015 else
2016 return _MBC_ILLEGAL;
2017 else
2018 if (_ismbblead(*str))
2019 return _MBC_LEAD;
2020 else
2021 return _MBC_SINGLE;
2024 /*********************************************************************
2025 * _mbsset(MSVCRT.@)
2027 unsigned char* CDECL _mbsset(unsigned char* str, unsigned int c)
2029 unsigned char* ret = str;
2031 if(!get_mbcinfo()->ismbcodepage || c < 256)
2032 return u__strset(str, c); /* ASCII CP or SB char */
2034 c &= 0xffff; /* Strip high bits */
2036 while(str[0] && str[1])
2038 *str++ = c >> 8;
2039 *str++ = c & 0xff;
2041 if(str[0])
2042 str[0] = '\0'; /* FIXME: OK to shorten? */
2044 return ret;
2047 /*********************************************************************
2048 * _mbsnbset(MSVCRT.@)
2050 unsigned char* CDECL _mbsnbset(unsigned char *str, unsigned int c, size_t len)
2052 unsigned char *ret = str;
2054 if(!len)
2055 return ret;
2057 if(!get_mbcinfo()->ismbcodepage || c < 256)
2058 return u__strnset(str, c, len); /* ASCII CP or SB char */
2060 c &= 0xffff; /* Strip high bits */
2062 while(str[0] && str[1] && (len > 1))
2064 *str++ = c >> 8;
2065 len--;
2066 *str++ = c & 0xff;
2067 len--;
2069 if(len && str[0]) {
2070 /* as per msdn pad with a blank character */
2071 str[0] = ' ';
2074 return ret;
2077 /*********************************************************************
2078 * _mbsnset(MSVCRT.@)
2080 unsigned char* CDECL _mbsnset(unsigned char* str, unsigned int c, size_t len)
2082 unsigned char *ret = str;
2084 if(!len)
2085 return ret;
2087 if(!get_mbcinfo()->ismbcodepage || c < 256)
2088 return u__strnset(str, c, len); /* ASCII CP or SB char */
2090 c &= 0xffff; /* Strip high bits */
2092 while(str[0] && str[1] && len--)
2094 *str++ = c >> 8;
2095 *str++ = c & 0xff;
2097 if(len && str[0])
2098 str[0] = '\0'; /* FIXME: OK to shorten? */
2100 return ret;
2103 /*********************************************************************
2104 * _mbsnccnt(MSVCRT.@)
2105 * 'c' is for 'character'.
2107 size_t CDECL _mbsnccnt(const unsigned char* str, size_t len)
2109 size_t ret;
2110 if(get_mbcinfo()->ismbcodepage)
2112 ret=0;
2113 while(*str && len-- > 0)
2115 if(_ismbblead(*str))
2117 if (!len)
2118 break;
2119 len--;
2120 str++;
2122 str++;
2123 ret++;
2125 return ret;
2127 ret=u_strlen(str);
2128 return min(ret, len); /* ASCII CP */
2131 /*********************************************************************
2132 * _mbsnbcnt(MSVCRT.@)
2133 * 'b' is for byte count.
2135 size_t CDECL _mbsnbcnt(const unsigned char* str, size_t len)
2137 size_t ret;
2138 if(get_mbcinfo()->ismbcodepage)
2140 const unsigned char* xstr = str;
2141 while(*xstr && len-- > 0)
2143 if (_ismbblead(*xstr++))
2144 xstr++;
2146 return xstr-str;
2148 ret=u_strlen(str);
2149 return min(ret, len); /* ASCII CP */
2152 /*********************************************************************
2153 * _mbsnbcat(MSVCRT.@)
2155 unsigned char* CDECL _mbsnbcat(unsigned char* dst, const unsigned char* src, size_t len)
2157 if(get_mbcinfo()->ismbcodepage)
2159 unsigned char *res = dst;
2160 while (*dst) {
2161 if (_ismbblead(*dst++)) {
2162 if (*dst) {
2163 dst++;
2164 } else {
2165 /* as per msdn overwrite the lead byte in front of '\0' */
2166 dst--;
2167 break;
2171 while (*src && len--) *dst++ = *src++;
2172 *dst = '\0';
2173 return res;
2175 return u_strncat(dst, src, len); /* ASCII CP */
2178 int CDECL _mbsnbcat_s(unsigned char *dst, size_t size, const unsigned char *src, size_t len)
2180 unsigned char *ptr = dst;
2181 size_t i;
2183 if (!dst && !size && !src && !len)
2184 return 0;
2186 if (!dst || !size || !src)
2188 if (dst && size)
2189 *dst = '\0';
2191 *_errno() = EINVAL;
2192 return EINVAL;
2195 /* Find the null terminator of the destination buffer. */
2196 while (size && *ptr)
2197 size--, ptr++;
2199 if (!size)
2201 *dst = '\0';
2202 *_errno() = EINVAL;
2203 return EINVAL;
2206 /* If necessary, check that the character preceding the null terminator is
2207 * a lead byte and move the pointer back by one for later overwrite. */
2208 if (ptr != dst && get_mbcinfo()->ismbcodepage && _ismbblead(*(ptr - 1)))
2209 size++, ptr--;
2211 for (i = 0; *src && i < len; i++)
2213 *ptr++ = *src++;
2214 size--;
2216 if (!size)
2218 *dst = '\0';
2219 *_errno() = ERANGE;
2220 return ERANGE;
2224 *ptr = '\0';
2225 return 0;
2228 /*********************************************************************
2229 * _mbsncat(MSVCRT.@)
2231 unsigned char* CDECL _mbsncat(unsigned char* dst, const unsigned char* src, size_t len)
2233 if(get_mbcinfo()->ismbcodepage)
2235 unsigned char *res = dst;
2236 while (*dst)
2238 if (_ismbblead(*dst++))
2239 dst++;
2241 while (*src && len--)
2243 *dst++ = *src;
2244 if(_ismbblead(*src++))
2245 *dst++ = *src++;
2247 *dst = '\0';
2248 return res;
2250 return u_strncat(dst, src, len); /* ASCII CP */
2254 /*********************************************************************
2255 * _mbslwr(MSVCRT.@)
2257 unsigned char* CDECL _mbslwr(unsigned char* s)
2259 unsigned char *ret = s;
2260 if (!s)
2261 return NULL;
2262 if (get_mbcinfo()->ismbcodepage)
2264 unsigned int c;
2265 while (*s)
2267 c = _mbctolower(_mbsnextc(s));
2268 /* Note that I assume that the size of the character is unchanged */
2269 if (c > 255)
2271 *s++=(c>>8);
2272 c=c & 0xff;
2274 *s++=c;
2277 else for ( ; *s; s++) *s = _tolower_l(*s, NULL);
2278 return ret;
2281 /*********************************************************************
2282 * _mbslwr_s_l(MSVCRT.@)
2284 int CDECL _mbslwr_s_l(unsigned char* s, size_t len, _locale_t locale)
2286 unsigned char *p = s;
2288 if (!s && !len)
2289 return 0;
2290 if (!MSVCRT_CHECK_PMT(s && len))
2291 return EINVAL;
2293 if (get_mbcinfo()->ismbcodepage)
2295 unsigned int c;
2296 for ( ; *s && len > 0; len--)
2298 c = _mbctolower_l(_mbsnextc_l(s, locale), locale);
2299 /* Note that I assume that the size of the character is unchanged */
2300 if (c > 255)
2302 *s++=(c>>8);
2303 c=c & 0xff;
2305 *s++=c;
2308 else
2310 for ( ; *s && len > 0; s++, len--)
2311 *s = _tolower_l(*s, locale);
2314 if (!MSVCRT_CHECK_PMT(len))
2316 *p = 0;
2317 return EINVAL;
2319 *s = 0;
2320 return 0;
2323 /*********************************************************************
2324 * _mbslwr_s(MSVCRT.@)
2326 int CDECL _mbslwr_s(unsigned char* str, size_t len)
2328 return _mbslwr_s_l(str, len, NULL);
2331 /*********************************************************************
2332 * _mbsupr(MSVCRT.@)
2334 unsigned char* CDECL _mbsupr(unsigned char* s)
2336 unsigned char *ret = s;
2337 if (!s)
2338 return NULL;
2339 if (get_mbcinfo()->ismbcodepage)
2341 unsigned int c;
2342 while (*s)
2344 c = _mbctoupper(_mbsnextc(s));
2345 /* Note that I assume that the size of the character is unchanged */
2346 if (c > 255)
2348 *s++=(c>>8);
2349 c=c & 0xff;
2351 *s++=c;
2354 else for ( ; *s; s++) *s = _toupper_l(*s, NULL);
2355 return ret;
2358 /*********************************************************************
2359 * _mbsupr_s_l(MSVCRT.@)
2361 int CDECL _mbsupr_s_l(unsigned char* s, size_t len, _locale_t locale)
2363 unsigned char *p = s;
2365 if (!s && !len)
2366 return 0;
2367 if (!MSVCRT_CHECK_PMT(s && len))
2368 return EINVAL;
2370 if (get_mbcinfo()->ismbcodepage)
2372 unsigned int c;
2373 for ( ; *s && len > 0; len--)
2375 c = _mbctoupper_l(_mbsnextc_l(s, locale), locale);
2376 /* Note that I assume that the size of the character is unchanged */
2377 if (c > 255)
2379 *s++=(c>>8);
2380 c=c & 0xff;
2382 *s++=c;
2385 else
2387 for ( ; *s && len > 0; s++, len--)
2388 *s = _toupper_l(*s, locale);
2391 if (!MSVCRT_CHECK_PMT(len))
2393 *p = 0;
2394 return EINVAL;
2396 *s = 0;
2397 return 0;
2400 /*********************************************************************
2401 * _mbsupr_s(MSVCRT.@)
2403 int CDECL _mbsupr_s(unsigned char* s, size_t len)
2405 return _mbsupr_s_l(s, len, NULL);
2408 /*********************************************************************
2409 * _mbsspn_l (MSVCRT.@)
2411 size_t CDECL _mbsspn_l(const unsigned char* string,
2412 const unsigned char* set, _locale_t locale)
2414 const unsigned char *p, *q;
2416 for (p = string; *p; p++)
2418 for (q = set; *q; q++)
2420 if (_ismbblead_l(*q, locale))
2422 /* duplicate a bug in native implementation */
2423 if (!q[1]) break;
2425 if (p[0] == q[0] && p[1] == q[1])
2427 p++;
2428 break;
2430 q++;
2432 else
2434 if (p[0] == q[0]) break;
2437 if (!*q) break;
2439 return p - string;
2442 /*********************************************************************
2443 * _mbsspn (MSVCRT.@)
2445 size_t CDECL _mbsspn(const unsigned char* string, const unsigned char* set)
2447 return _mbsspn_l(string, set, NULL);
2450 /*********************************************************************
2451 * _mbsspnp (MSVCRT.@)
2453 unsigned char* CDECL _mbsspnp(const unsigned char* string, const unsigned char* set)
2455 string += _mbsspn( string, set );
2456 return *string ? (unsigned char*)string : NULL;
2459 /*********************************************************************
2460 * _mbscspn_l (MSVCRT.@)
2462 size_t CDECL _mbscspn_l(const unsigned char* str,
2463 const unsigned char* cmp, _locale_t locale)
2465 const unsigned char *p, *q;
2467 for (p = str; *p; p++)
2469 for (q = cmp; *q; q++)
2471 if (_ismbblead_l(*q, locale))
2473 /* duplicate a bug in native implementation */
2474 if (!q[1]) return 0;
2476 if (p[0] == q[0] && p[1] == q[1])
2477 return p - str;
2478 q++;
2480 else if (p[0] == q[0])
2481 return p - str;
2484 return p - str;
2487 /*********************************************************************
2488 * _mbscspn (MSVCRT.@)
2490 size_t CDECL _mbscspn(const unsigned char* str, const unsigned char* cmp)
2492 return _mbscspn_l(str, cmp, NULL);
2495 /*********************************************************************
2496 * _mbsrev (MSVCRT.@)
2498 unsigned char* CDECL _mbsrev(unsigned char* str)
2500 int i, len = _mbslen(str);
2501 unsigned char *p, *temp=malloc(len*2);
2503 if(!temp)
2504 return str;
2506 /* unpack multibyte string to temp buffer */
2507 p=str;
2508 for(i=0; i<len; i++)
2510 if (_ismbblead(*p))
2512 temp[i*2]=*p++;
2513 temp[i*2+1]=*p++;
2515 else
2517 temp[i*2]=*p++;
2518 temp[i*2+1]=0;
2522 /* repack it in the reverse order */
2523 p=str;
2524 for(i=len-1; i>=0; i--)
2526 if(_ismbblead(temp[i*2]))
2528 *p++=temp[i*2];
2529 *p++=temp[i*2+1];
2531 else
2533 *p++=temp[i*2];
2537 free(temp);
2539 return str;
2542 /*********************************************************************
2543 * _mbspbrk (MSVCRT.@)
2545 unsigned char* CDECL _mbspbrk(const unsigned char* str, const unsigned char* accept)
2547 const unsigned char* p;
2549 while(*str)
2551 for(p = accept; *p; p += (_ismbblead(*p)?2:1) )
2553 if (*p == *str)
2554 if( !_ismbblead(*p) || ( *(p+1) == *(str+1) ) )
2555 return (unsigned char*)str;
2557 str += (_ismbblead(*str)?2:1);
2559 return NULL;
2564 * Functions depending on locale codepage
2567 /*********************************************************************
2568 * mblen(MSVCRT.@)
2569 * REMARKS
2570 * Unlike most of the multibyte string functions this function uses
2571 * the locale codepage, not the codepage set by _setmbcp
2573 int CDECL mblen(const char* str, size_t size)
2575 if (str && *str && size)
2577 if(get_locinfo()->mb_cur_max == 1)
2578 return 1; /* ASCII CP */
2580 return !isleadbyte((unsigned char)*str) ? 1 : (size>1 ? 2 : -1);
2582 return 0;
2585 /*********************************************************************
2586 * mbrlen(MSVCRT.@)
2588 size_t CDECL mbrlen(const char *str, size_t len, mbstate_t *state)
2590 mbstate_t s = (state ? *state : 0);
2591 size_t ret;
2593 if(!len || !str || !*str)
2594 return 0;
2596 if(get_locinfo()->mb_cur_max == 1) {
2597 return 1;
2598 }else if(!s && isleadbyte((unsigned char)*str)) {
2599 if(len == 1) {
2600 s = (unsigned char)*str;
2601 ret = -2;
2602 }else {
2603 ret = 2;
2605 }else if(!s) {
2606 ret = 1;
2607 }else {
2608 s = 0;
2609 ret = 2;
2612 if(state)
2613 *state = s;
2614 return ret;
2617 /*********************************************************************
2618 * _mbstrlen_l(MSVCRT.@)
2620 size_t CDECL _mbstrlen_l(const char* str, _locale_t locale)
2622 pthreadlocinfo locinfo;
2624 if(!locale)
2625 locinfo = get_locinfo();
2626 else
2627 locinfo = locale->locinfo;
2629 if(locinfo->mb_cur_max > 1) {
2630 size_t len;
2631 len = MultiByteToWideChar(locinfo->lc_codepage, MB_ERR_INVALID_CHARS,
2632 str, -1, NULL, 0);
2633 if (!len) {
2634 *_errno() = EILSEQ;
2635 return -1;
2637 return len - 1;
2640 return strlen(str);
2643 /*********************************************************************
2644 * _mbstrlen(MSVCRT.@)
2646 size_t CDECL _mbstrlen(const char* str)
2648 return _mbstrlen_l(str, NULL);
2651 /*********************************************************************
2652 * _mbtowc_l(MSVCRT.@)
2654 int CDECL _mbtowc_l(wchar_t *dst, const char* str, size_t n, _locale_t locale)
2656 pthreadlocinfo locinfo;
2657 wchar_t tmpdst;
2659 if(!locale)
2660 locinfo = get_locinfo();
2661 else
2662 locinfo = locale->locinfo;
2664 if(n <= 0 || !str)
2665 return 0;
2667 if(!*str) {
2668 if(dst) *dst = 0;
2669 return 0;
2672 if(!locinfo->lc_codepage) {
2673 if(dst) *dst = (unsigned char)*str;
2674 return 1;
2676 if(n>=2 && _isleadbyte_l((unsigned char)*str, locale)) {
2677 if(!MultiByteToWideChar(locinfo->lc_codepage, 0, str, 2, &tmpdst, 1))
2678 return -1;
2679 if(dst) *dst = tmpdst;
2680 return 2;
2682 if(!MultiByteToWideChar(locinfo->lc_codepage, 0, str, 1, &tmpdst, 1))
2683 return -1;
2684 if(dst) *dst = tmpdst;
2685 return 1;
2688 /*********************************************************************
2689 * mbtowc(MSVCRT.@)
2691 int CDECL mbtowc(wchar_t *dst, const char* str, size_t n)
2693 return _mbtowc_l(dst, str, n, NULL);
2696 /*********************************************************************
2697 * btowc(MSVCRT.@)
2699 wint_t CDECL btowc(int c)
2701 unsigned char letter = c;
2702 wchar_t ret;
2704 if(c == EOF)
2705 return WEOF;
2706 if(!get_locinfo()->lc_codepage)
2707 return c & 255;
2708 if(!MultiByteToWideChar(get_locinfo()->lc_codepage,
2709 MB_ERR_INVALID_CHARS, (LPCSTR)&letter, 1, &ret, 1))
2710 return WEOF;
2712 return ret;
2715 /*********************************************************************
2716 * mbrtowc(MSVCRT.@)
2718 size_t CDECL mbrtowc(wchar_t *dst, const char *str,
2719 size_t n, mbstate_t *state)
2721 pthreadlocinfo locinfo = get_locinfo();
2722 mbstate_t s = (state ? *state : 0);
2723 char tmpstr[2];
2724 int len = 0;
2726 if(dst)
2727 *dst = 0;
2729 if(!n || !str || !*str)
2730 return 0;
2732 if(locinfo->mb_cur_max == 1) {
2733 tmpstr[len++] = *str;
2734 }else if(!s && isleadbyte((unsigned char)*str)) {
2735 if(n == 1) {
2736 s = (unsigned char)*str;
2737 len = -2;
2738 }else {
2739 tmpstr[0] = str[0];
2740 tmpstr[1] = str[1];
2741 len = 2;
2743 }else if(!s) {
2744 tmpstr[len++] = *str;
2745 }else {
2746 tmpstr[0] = s;
2747 tmpstr[1] = *str;
2748 len = 2;
2749 s = 0;
2752 if(len > 0) {
2753 if(!MultiByteToWideChar(locinfo->lc_codepage, 0, tmpstr, len, dst, dst ? 1 : 0))
2754 len = -1;
2757 if(state)
2758 *state = s;
2759 return len;
2762 /*********************************************************************
2763 * _mbstowcs_l(MSVCRT.@)
2765 size_t CDECL _mbstowcs_l(wchar_t *wcstr, const char *mbstr,
2766 size_t count, _locale_t locale)
2768 pthreadlocinfo locinfo;
2769 size_t i, size;
2771 if(!mbstr) {
2772 *_errno() = EINVAL;
2773 return -1;
2776 if(!locale)
2777 locinfo = get_locinfo();
2778 else
2779 locinfo = locale->locinfo;
2781 if(!locinfo->lc_codepage) {
2782 if(!wcstr)
2783 return strlen(mbstr);
2785 for(i=0; i<count; i++) {
2786 wcstr[i] = (unsigned char)mbstr[i];
2787 if(!wcstr[i]) break;
2789 return i;
2792 /* Ignore count parameter */
2793 if(!wcstr) {
2794 size = MultiByteToWideChar(locinfo->lc_codepage,
2795 MB_ERR_INVALID_CHARS, mbstr, -1, NULL, 0);
2796 if(!size) {
2797 *_errno() = EILSEQ;
2798 return -1;
2800 return size - 1;
2803 for(i=0, size=0; i<count; i++) {
2804 if(mbstr[size] == '\0')
2805 break;
2807 size += (_isleadbyte_l((unsigned char)mbstr[size], locale) ? 2 : 1);
2810 if(size) {
2811 size = MultiByteToWideChar(locinfo->lc_codepage,
2812 MB_ERR_INVALID_CHARS, mbstr, size, wcstr, count);
2813 if(!size) {
2814 if(count) wcstr[0] = '\0';
2815 *_errno() = EILSEQ;
2816 return -1;
2820 if(size<count)
2821 wcstr[size] = '\0';
2823 return size;
2826 /*********************************************************************
2827 * mbstowcs(MSVCRT.@)
2829 size_t CDECL mbstowcs(wchar_t *wcstr,
2830 const char *mbstr, size_t count)
2832 return _mbstowcs_l(wcstr, mbstr, count, NULL);
2835 /*********************************************************************
2836 * _mbstowcs_s_l(MSVCRT.@)
2838 int CDECL _mbstowcs_s_l(size_t *ret, wchar_t *wcstr,
2839 size_t size, const char *mbstr, size_t count, _locale_t locale)
2841 size_t conv;
2842 int err = 0;
2844 if(!wcstr && !size) {
2845 conv = _mbstowcs_l(NULL, mbstr, 0, locale);
2846 if(ret)
2847 *ret = conv+1;
2848 return 0;
2851 if (!MSVCRT_CHECK_PMT(wcstr != NULL)) return EINVAL;
2852 if (!MSVCRT_CHECK_PMT(mbstr != NULL)) {
2853 if(size) wcstr[0] = '\0';
2854 return EINVAL;
2857 if(count==_TRUNCATE || size<count)
2858 conv = size;
2859 else
2860 conv = count;
2862 conv = _mbstowcs_l(wcstr, mbstr, conv, locale);
2863 if(conv<size)
2864 wcstr[conv++] = '\0';
2865 else if(conv==size && count==_TRUNCATE && wcstr[conv-1]!='\0') {
2866 wcstr[conv-1] = '\0';
2867 err = STRUNCATE;
2868 }else if(conv==size && wcstr[conv-1]!='\0') {
2869 MSVCRT_INVALID_PMT("wcstr[size] is too small", ERANGE);
2870 if(size)
2871 wcstr[0] = '\0';
2872 return ERANGE;
2875 if(ret)
2876 *ret = conv;
2877 return err;
2880 /*********************************************************************
2881 * mbstowcs_s(MSVCRT.@)
2883 int CDECL _mbstowcs_s(size_t *ret, wchar_t *wcstr,
2884 size_t size, const char *mbstr, size_t count)
2886 return _mbstowcs_s_l(ret, wcstr, size, mbstr, count, NULL);
2889 /*********************************************************************
2890 * mbsrtowcs(MSVCRT.@)
2892 size_t CDECL mbsrtowcs(wchar_t *wcstr,
2893 const char **pmbstr, size_t count, mbstate_t *state)
2895 mbstate_t s = (state ? *state : 0);
2896 wchar_t tmpdst;
2897 size_t ret = 0;
2898 const char *p;
2900 if(!MSVCRT_CHECK_PMT(pmbstr != NULL))
2901 return -1;
2903 p = *pmbstr;
2904 while(!wcstr || count>ret) {
2905 int ch_len = mbrtowc(&tmpdst, p, 2, &s);
2906 if(wcstr)
2907 wcstr[ret] = tmpdst;
2909 if(ch_len < 0) {
2910 return -1;
2911 }else if(ch_len == 0) {
2912 if(wcstr) *pmbstr = NULL;
2913 return ret;
2916 p += ch_len;
2917 ret++;
2920 if(wcstr) *pmbstr = p;
2921 return ret;
2924 /*********************************************************************
2925 * mbsrtowcs_s(MSVCRT.@)
2927 int CDECL mbsrtowcs_s(size_t *ret, wchar_t *wcstr, size_t len,
2928 const char **mbstr, size_t count, mbstate_t *state)
2930 size_t tmp;
2932 if(!ret) ret = &tmp;
2933 if(!MSVCRT_CHECK_PMT(!!wcstr == !!len)) {
2934 *ret = -1;
2935 return EINVAL;
2938 *ret = mbsrtowcs(wcstr, mbstr, count>len ? len : count, state);
2939 if(*ret == -1) {
2940 if(wcstr) *wcstr = 0;
2941 return *_errno();
2943 (*ret)++;
2944 if(*ret > len) {
2945 /* no place for terminating '\0' */
2946 if(wcstr) *wcstr = 0;
2947 return 0;
2949 if(wcstr) wcstr[(*ret)-1] = 0;
2950 return 0;
2953 /*********************************************************************
2954 * _mbctohira_l (MSVCRT.@)
2956 * Converts a sjis katakana character to hiragana.
2958 unsigned int CDECL _mbctohira_l(unsigned int c, _locale_t locale)
2960 if(_ismbckata_l(c, locale) && c <= 0x8393)
2961 return (c - 0x8340 - (c >= 0x837f ? 1 : 0)) + 0x829f;
2962 return c;
2965 /*********************************************************************
2966 * _mbctohira (MSVCRT.@)
2968 unsigned int CDECL _mbctohira(unsigned int c)
2970 return _mbctohira_l(c, NULL);
2973 /*********************************************************************
2974 * _mbctokata_l (MSVCRT.@)
2976 * Converts a sjis hiragana character to katakana.
2978 unsigned int CDECL _mbctokata_l(unsigned int c, _locale_t locale)
2980 if(_ismbchira_l(c, locale))
2981 return (c - 0x829f) + 0x8340 + (c >= 0x82de ? 1 : 0);
2982 return c;
2986 /*********************************************************************
2987 * _mbctokata (MSVCRT.@)
2989 unsigned int CDECL _mbctokata(unsigned int c)
2991 return _mbctokata_l(c, NULL);
2994 /*********************************************************************
2995 * _ismbcl0_l (MSVCRT.@)
2997 int CDECL _ismbcl0_l(unsigned int c, _locale_t locale)
2999 pthreadmbcinfo mbcinfo;
3001 if(!locale)
3002 mbcinfo = get_mbcinfo();
3003 else
3004 mbcinfo = locale->mbcinfo;
3006 if(mbcinfo->mbcodepage == 932)
3008 /* JIS non-Kanji */
3009 return _ismbclegal_l(c, locale) && c >= 0x8140 && c <= 0x889e;
3012 return 0;
3015 /*********************************************************************
3016 * _ismbcl0 (MSVCRT.@)
3018 int CDECL _ismbcl0(unsigned int c)
3020 return _ismbcl0_l(c, NULL);
3023 /*********************************************************************
3024 * _ismbcl1_l (MSVCRT.@)
3026 int CDECL _ismbcl1_l(unsigned int c, _locale_t locale)
3028 pthreadmbcinfo mbcinfo;
3030 if(!locale)
3031 mbcinfo = get_mbcinfo();
3032 else
3033 mbcinfo = locale->mbcinfo;
3035 if(mbcinfo->mbcodepage == 932)
3037 /* JIS level-1 */
3038 return _ismbclegal_l(c, locale) && c >= 0x889f && c <= 0x9872;
3041 return 0;
3044 /*********************************************************************
3045 * _ismbcl1 (MSVCRT.@)
3047 int CDECL _ismbcl1(unsigned int c)
3049 return _ismbcl1_l(c, NULL);
3052 /*********************************************************************
3053 * _ismbcl2_l (MSVCRT.@)
3055 int CDECL _ismbcl2_l(unsigned int c, _locale_t locale)
3057 pthreadmbcinfo mbcinfo;
3059 if(!locale)
3060 mbcinfo = get_mbcinfo();
3061 else
3062 mbcinfo = locale->mbcinfo;
3064 if(mbcinfo->mbcodepage == 932)
3066 /* JIS level-2 */
3067 return _ismbclegal_l(c, locale) && c >= 0x989f && c <= 0xeaa4;
3070 return 0;
3073 /*********************************************************************
3074 * _ismbcl2 (MSVCRT.@)
3076 int CDECL _ismbcl2(unsigned int c)
3078 return _ismbcl2_l(c, NULL);