windowscodecs/tests: Use BOOL type where appropriate.
[wine.git] / dlls / msvcrt / mbcs.c
blobb25649b6638bf1c8df93a42d3c6c538252b05a67
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>
28 #include "msvcrt.h"
29 #include "wine/unicode.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
34 unsigned char MSVCRT_mbctype[257] = { 0 };
36 /* It seems that the data about valid trail bytes is not available from kernel32
37 * so we have to store is here. The format is the same as for lead bytes in CPINFO */
38 struct cp_extra_info_t
40 int cp;
41 BYTE TrailBytes[MAX_LEADBYTES];
44 static struct cp_extra_info_t g_cpextrainfo[] =
46 {932, {0x40, 0x7e, 0x80, 0xfc, 0, 0}},
47 {936, {0x40, 0xfe, 0, 0}},
48 {949, {0x41, 0xfe, 0, 0}},
49 {950, {0x40, 0x7e, 0xa1, 0xfe, 0, 0}},
50 {1361, {0x31, 0x7e, 0x81, 0xfe, 0, 0}},
51 {20932, {1, 255, 0, 0}}, /* seems to give different results on different systems */
52 {0, {1, 255, 0, 0}} /* match all with FIXME */
55 /* Maps cp932 single byte character to multi byte character */
56 static const unsigned char mbbtombc_932[] = {
57 0x40,0x49,0x68,0x94,0x90,0x93,0x95,0x66,0x69,0x6a,0x96,0x7b,0x43,0x7c,0x44,0x5e,
58 0x4f,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x46,0x47,0x83,0x81,0x84,0x48,
59 0x97,0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,
60 0x6f,0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x6d,0x8f,0x6e,0x4f,0x76,
61 0x77,0x78,0x79,0x6d,0x8f,0x6e,0x4f,0x51,0x65,0x81,0x82,0x83,0x84,0x85,0x86,0x87,
62 0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f,0x90,0x91,0x92,0x93,0x94,0x95,0x50,
63 0x42,0x75,0x76,0x41,0x45,0x92,0x40,0x42,0x44,0x46,0x48,0x83,0x85,0x87,0x62,
64 0x5b,0x41,0x43,0x45,0x47,0x49,0x4a,0x4c,0x4e,0x50,0x52,0x54,0x56,0x58,0x5a,0x5c,
65 0x5e,0x60,0x63,0x65,0x67,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x71,0x74,0x77,0x7a,0x7d,
66 0x7e,0x80,0x81,0x82,0x84,0x86,0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8f,0x93,0x4a,0x4b };
68 /* Maps multibyte cp932 punctuation marks to single byte equivalents */
69 static const unsigned char mbctombb_932_punct[] = {
70 0x20,0xa4,0xa1,0x2c,0x2e,0xa5,0x3a,0x3b,0x3f,0x21,0xde,0xdf,0x00,0x00,0x00,0x5e,
71 0x7e,0x5f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xb0,0x00,0x00,0x2f,0x00,
72 0x00,0x00,0x7c,0x00,0x00,0x60,0x27,0x00,0x22,0x28,0x29,0x00,0x00,0x5b,0x5d,0x7b,
73 0x7d,0x00,0x00,0x00,0x00,0xa2,0xa3,0x00,0x00,0x00,0x00,0x2b,0x2d,0x00,0x00,0x00,
74 0x00,0x3d,0x00,0x3c,0x3e,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x5c,
75 0x24,0x00,0x00,0x25,0x23,0x26,0x2a,0x40};
77 /* Maps multibyte cp932 hiragana/katakana to single-byte equivalents */
78 static const unsigned char mbctombb_932_kana[] = {
79 0xa7,0xb1,0xa8,0xb2,0xa9,0xb3,0xaa,0xb4,0xab,0xb5,0xb6,0xb6,0xb7,0xb7,0xb8,0xb8,
80 0xb9,0xb9,0xba,0xba,0xbb,0xbb,0xbc,0xbc,0xbd,0xbd,0xbe,0xbe,0xbf,0xbf,0xc0,0xc0,
81 0xc1,0xc1,0xaf,0xc2,0xc2,0xc3,0xc3,0xc4,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xca,
82 0xca,0xcb,0xcb,0xcb,0xcc,0xcc,0xcc,0xcd,0xcd,0xcd,0xce,0xce,0xce,0xcf,0xd0,0xd1,
83 0xd2,0xd3,0xac,0xd4,0xad,0xd5,0xae,0xd6,0xd7,0xd8,0xd9,0xda,0xdb,0xdc,0xdc,0xb2,
84 0xb4,0xa6,0xdd,0xb3,0xb6,0xb9};
86 static MSVCRT_wchar_t msvcrt_mbc_to_wc(unsigned int ch)
88 MSVCRT_wchar_t chW;
89 char mbch[2];
90 int n_chars;
92 if (ch <= 0xff) {
93 mbch[0] = ch;
94 n_chars = 1;
95 } else {
96 mbch[0] = (ch >> 8) & 0xff;
97 mbch[1] = ch & 0xff;
98 n_chars = 2;
100 if (!MultiByteToWideChar(get_mbcinfo()->mbcodepage, 0, mbch, n_chars, &chW, 1))
102 WARN("MultiByteToWideChar failed on %x\n", ch);
103 return 0;
105 return chW;
108 static inline MSVCRT_size_t u_strlen( const unsigned char *str )
110 return strlen( (const char*) str );
113 static inline unsigned char* u_strncat( unsigned char* dst, const unsigned char* src, MSVCRT_size_t len )
115 return (unsigned char*)strncat( (char*)dst, (const char*)src, len);
118 static inline int u_strcmp( const unsigned char *s1, const unsigned char *s2 )
120 return strcmp( (const char*)s1, (const char*)s2 );
123 static inline int u_strcasecmp( const unsigned char *s1, const unsigned char *s2 )
125 return strcasecmp( (const char*)s1, (const char*)s2 );
128 static inline int u_strncmp( const unsigned char *s1, const unsigned char *s2, MSVCRT_size_t len )
130 return strncmp( (const char*)s1, (const char*)s2, len );
133 static inline int u_strncasecmp( const unsigned char *s1, const unsigned char *s2, MSVCRT_size_t len )
135 return strncasecmp( (const char*)s1, (const char*)s2, len );
138 static inline unsigned char *u_strchr( const unsigned char *s, unsigned char x )
140 return (unsigned char*) strchr( (const char*)s, x );
143 static inline unsigned char *u_strrchr( const unsigned char *s, unsigned char x )
145 return (unsigned char*) strrchr( (const char*)s, x );
148 static inline unsigned char *u__strset( unsigned char *s, unsigned char c )
150 return (unsigned char*) _strset( (char*)s, c);
153 static inline unsigned char *u__strnset( unsigned char *s, unsigned char c, MSVCRT_size_t len )
155 return (unsigned char*) MSVCRT__strnset( (char*)s, c, len );
158 static inline MSVCRT_size_t u_strcspn( const unsigned char *s, const unsigned char *rej )
160 return strcspn( (const char *)s, (const char*)rej );
163 /*********************************************************************
164 * __p__mbctype (MSVCRT.@)
166 unsigned char* CDECL __p__mbctype(void)
168 return get_mbcinfo()->mbctype;
171 /*********************************************************************
172 * __p___mb_cur_max(MSVCRT.@)
174 int* CDECL __p___mb_cur_max(void)
176 return &get_locinfo()->mb_cur_max;
179 /*********************************************************************
180 * ___mb_cur_max_func(MSVCRT.@)
182 int CDECL MSVCRT____mb_cur_max_func(void)
184 return get_locinfo()->mb_cur_max;
187 /* ___mb_cur_max_l_func - not exported in native msvcrt */
188 int* CDECL ___mb_cur_max_l_func(MSVCRT__locale_t locale)
190 MSVCRT_pthreadlocinfo locinfo;
192 if(!locale)
193 locinfo = get_locinfo();
194 else
195 locinfo = locale->locinfo;
197 return &locinfo->mb_cur_max;
200 /*********************************************************************
201 * INTERNAL: _setmbcp_l
203 int _setmbcp_l(int cp, LCID lcid, MSVCRT_pthreadmbcinfo mbcinfo)
205 const char format[] = ".%d";
207 int newcp;
208 CPINFO cpi;
209 BYTE *bytes;
210 WORD chartypes[256];
211 char bufA[256];
212 WCHAR bufW[256];
213 int charcount;
214 int ret;
215 int i;
217 if(!mbcinfo)
218 mbcinfo = get_mbcinfo();
220 switch (cp)
222 case _MB_CP_ANSI:
223 newcp = GetACP();
224 break;
225 case _MB_CP_OEM:
226 newcp = GetOEMCP();
227 break;
228 case _MB_CP_LOCALE:
229 newcp = get_locinfo()->lc_codepage;
230 if(newcp)
231 break;
232 /* fall through (C locale) */
233 case _MB_CP_SBCS:
234 newcp = 20127; /* ASCII */
235 break;
236 default:
237 newcp = cp;
238 break;
241 if(lcid == -1) {
242 sprintf(bufA, format, newcp);
243 mbcinfo->mblcid = MSVCRT_locale_to_LCID(bufA, NULL);
244 } else {
245 mbcinfo->mblcid = lcid;
248 if(mbcinfo->mblcid == -1)
250 WARN("Can't assign LCID to codepage (%d)\n", mbcinfo->mblcid);
251 mbcinfo->mblcid = 0;
254 if (!GetCPInfo(newcp, &cpi))
256 WARN("Codepage %d not found\n", newcp);
257 *MSVCRT__errno() = MSVCRT_EINVAL;
258 return -1;
261 /* setup the _mbctype */
262 memset(mbcinfo->mbctype, 0, sizeof(unsigned char[257]));
263 memset(mbcinfo->mbcasemap, 0, sizeof(unsigned char[256]));
265 bytes = cpi.LeadByte;
266 while (bytes[0] || bytes[1])
268 for (i = bytes[0]; i <= bytes[1]; i++)
269 mbcinfo->mbctype[i + 1] |= _M1;
270 bytes += 2;
273 if (cpi.MaxCharSize > 1)
275 /* trail bytes not available through kernel32 but stored in a structure in msvcrt */
276 struct cp_extra_info_t *cpextra = g_cpextrainfo;
278 mbcinfo->ismbcodepage = 1;
279 while (TRUE)
281 if (cpextra->cp == 0 || cpextra->cp == newcp)
283 if (cpextra->cp == 0)
284 FIXME("trail bytes data not available for DBCS codepage %d - assuming all bytes\n", newcp);
286 bytes = cpextra->TrailBytes;
287 while (bytes[0] || bytes[1])
289 for (i = bytes[0]; i <= bytes[1]; i++)
290 mbcinfo->mbctype[i + 1] |= _M2;
291 bytes += 2;
293 break;
295 cpextra++;
298 else
299 mbcinfo->ismbcodepage = 0;
301 /* we can't use GetStringTypeA directly because we don't have a locale - only a code page
303 charcount = 0;
304 for (i = 0; i < 256; i++)
305 if (!(mbcinfo->mbctype[i + 1] & _M1))
306 bufA[charcount++] = i;
308 ret = MultiByteToWideChar(newcp, 0, bufA, charcount, bufW, charcount);
309 if (ret != charcount)
310 ERR("MultiByteToWideChar of chars failed for cp %d, ret=%d (exp %d), error=%d\n", newcp, ret, charcount, GetLastError());
312 GetStringTypeW(CT_CTYPE1, bufW, charcount, chartypes);
314 charcount = 0;
315 for (i = 0; i < 256; i++)
316 if (!(mbcinfo->mbctype[i + 1] & _M1))
318 if (chartypes[charcount] & C1_UPPER)
320 mbcinfo->mbctype[i + 1] |= _SBUP;
321 bufW[charcount] = tolowerW(bufW[charcount]);
323 else if (chartypes[charcount] & C1_LOWER)
325 mbcinfo->mbctype[i + 1] |= _SBLOW;
326 bufW[charcount] = toupperW(bufW[charcount]);
328 charcount++;
331 ret = WideCharToMultiByte(newcp, 0, bufW, charcount, bufA, charcount, NULL, NULL);
332 if (ret != charcount)
333 ERR("WideCharToMultiByte failed for cp %d, ret=%d (exp %d), error=%d\n", newcp, ret, charcount, GetLastError());
335 charcount = 0;
336 for (i = 0; i < 256; i++)
338 if(!(mbcinfo->mbctype[i + 1] & _M1))
340 if(mbcinfo->mbctype[i] & (C1_UPPER|C1_LOWER))
341 mbcinfo->mbcasemap[i] = bufA[charcount];
342 charcount++;
346 if (newcp == 932) /* CP932 only - set _MP and _MS */
348 /* On Windows it's possible to calculate the _MP and _MS from CT_CTYPE1
349 * and CT_CTYPE3. But as of Wine 0.9.43 we return wrong values what makes
350 * it hard. As this is set only for codepage 932 we hardcode it what gives
351 * also faster execution.
353 for (i = 161; i <= 165; i++)
354 mbcinfo->mbctype[i + 1] |= _MP;
355 for (i = 166; i <= 223; i++)
356 mbcinfo->mbctype[i + 1] |= _MS;
359 mbcinfo->mbcodepage = newcp;
360 if(MSVCRT_locale && mbcinfo == MSVCRT_locale->mbcinfo)
361 memcpy(MSVCRT_mbctype, MSVCRT_locale->mbcinfo->mbctype, sizeof(MSVCRT_mbctype));
363 return 0;
366 /*********************************************************************
367 * _setmbcp (MSVCRT.@)
369 int CDECL _setmbcp(int cp)
371 return _setmbcp_l(cp, -1, NULL);
374 /*********************************************************************
375 * _getmbcp (MSVCRT.@)
377 int CDECL _getmbcp(void)
379 return get_mbcinfo()->mbcodepage;
382 /*********************************************************************
383 * _mbsnextc(MSVCRT.@)
385 unsigned int CDECL _mbsnextc(const unsigned char* str)
387 if(_ismbblead(*str))
388 return *str << 8 | str[1];
389 return *str;
392 /*********************************************************************
393 * _mbctolower(MSVCRT.@)
395 unsigned int CDECL _mbctolower(unsigned int c)
397 if (_ismbblead(c))
399 FIXME("Handle MBC chars\n");
400 return c;
402 return tolower(c); /* ASCII CP or SB char */
405 /*********************************************************************
406 * _mbctoupper(MSVCRT.@)
408 unsigned int CDECL _mbctoupper(unsigned int c)
410 if (_ismbblead(c))
412 FIXME("Handle MBC chars\n");
413 return c;
415 return toupper(c); /* ASCII CP or SB char */
418 /*********************************************************************
419 * _mbctombb (MSVCRT.@)
421 unsigned int CDECL _mbctombb(unsigned int c)
423 unsigned int value;
425 if(get_mbcinfo()->mbcodepage == 932)
427 if(c >= 0x829f && c <= 0x82f1) /* Hiragana */
428 return mbctombb_932_kana[c - 0x829f];
429 if(c >= 0x8340 && c <= 0x8396 && c != 0x837f) /* Katakana */
430 return mbctombb_932_kana[c - 0x8340 - (c >= 0x837f ? 1 : 0)];
431 if(c >= 0x8140 && c <= 0x8197) /* Punctuation */
433 value = mbctombb_932_punct[c - 0x8140];
434 return value ? value : c;
436 if((c >= 0x824f && c <= 0x8258) || /* Fullwidth digits */
437 (c >= 0x8260 && c <= 0x8279)) /* Fullwidth capitals letters */
438 return c - 0x821f;
439 if(c >= 0x8281 && c <= 0x829a) /* Fullwidth small letters */
440 return c - 0x8220;
441 /* all other cases return c */
443 return c;
446 /*********************************************************************
447 * _mbcjistojms(MSVCRT.@)
449 * Converts a jis character to sjis.
450 * Based on description from
451 * http://www.slayers.ne.jp/~oouchi/code/jistosjis.html
453 unsigned int CDECL _mbcjistojms(unsigned int c)
455 /* Conversion takes place only when codepage is 932.
456 In all other cases, c is returned unchanged */
457 if(get_mbcinfo()->mbcodepage == 932)
459 if(HIBYTE(c) >= 0x21 && HIBYTE(c) <= 0x7e &&
460 LOBYTE(c) >= 0x21 && LOBYTE(c) <= 0x7e)
462 if(HIBYTE(c) % 2)
463 c += 0x1f;
464 else
465 c += 0x7d;
467 if(LOBYTE(c) >= 0x7F)
468 c += 0x1;
470 c = (((HIBYTE(c) - 0x21)/2 + 0x81) << 8) | LOBYTE(c);
472 if(HIBYTE(c) > 0x9f)
473 c += 0x4000;
475 else
476 return 0; /* Codepage is 932, but c can't be converted */
479 return c;
482 /*********************************************************************
483 * _mbcjmstojis(MSVCRT.@)
485 * Converts a sjis character to jis.
487 unsigned int CDECL _mbcjmstojis(unsigned int c)
489 /* Conversion takes place only when codepage is 932.
490 In all other cases, c is returned unchanged */
491 if(get_mbcinfo()->mbcodepage == 932)
493 if(_ismbclegal(c) && HIBYTE(c) < 0xf0)
495 if(HIBYTE(c) >= 0xe0)
496 c -= 0x4000;
498 c = (((HIBYTE(c) - 0x81)*2 + 0x21) << 8) | LOBYTE(c);
500 if(LOBYTE(c) > 0x7f)
501 c -= 0x1;
503 if(LOBYTE(c) > 0x9d)
504 c += 0x83;
505 else
506 c -= 0x1f;
508 else
509 return 0; /* Codepage is 932, but c can't be converted */
512 return c;
515 /*********************************************************************
516 * _mbsdec(MSVCRT.@)
518 unsigned char* CDECL _mbsdec(const unsigned char* start, const unsigned char* cur)
520 if(get_mbcinfo()->ismbcodepage)
521 return (unsigned char *)(_ismbstrail(start,cur-1) ? cur - 2 : cur -1);
523 return (unsigned char *)cur - 1; /* ASCII CP or SB char */
526 /*********************************************************************
527 * _mbclen(MSVCRT.@)
529 unsigned int CDECL _mbclen(const unsigned char* str)
531 return _ismbblead(*str) ? 2 : 1;
534 /*********************************************************************
535 * _mbsinc(MSVCRT.@)
537 unsigned char* CDECL _mbsinc(const unsigned char* str)
539 return (unsigned char *)(str + _mbclen(str));
542 /*********************************************************************
543 * _mbsninc(MSVCRT.@)
545 unsigned char* CDECL _mbsninc(const unsigned char* str, MSVCRT_size_t num)
547 if(!str)
548 return NULL;
550 while (num > 0 && *str)
552 if (_ismbblead(*str))
554 if (!*(str+1))
555 break;
556 str++;
558 str++;
559 num--;
562 return (unsigned char*)str;
565 /*********************************************************************
566 * _mbslen(MSVCRT.@)
568 MSVCRT_size_t CDECL _mbslen(const unsigned char* str)
570 MSVCRT_size_t len = 0;
571 while(*str)
573 if (_ismbblead(*str))
575 str++;
576 if (!*str) /* count only full chars */
577 break;
579 str++;
580 len++;
582 return len;
585 /*********************************************************************
586 * _mbccpy(MSVCRT.@)
588 void CDECL _mbccpy(unsigned char* dest, const unsigned char* src)
590 *dest = *src;
591 if(_ismbblead(*src))
592 *++dest = *++src; /* MB char */
595 /*********************************************************************
596 * _mbsncpy(MSVCRT.@)
597 * REMARKS
598 * The parameter n is the number or characters to copy, not the size of
599 * the buffer. Use _mbsnbcpy for a function analogical to strncpy
601 unsigned char* CDECL _mbsncpy(unsigned char* dst, const unsigned char* src, MSVCRT_size_t n)
603 unsigned char* ret = dst;
604 if(!n)
605 return dst;
606 if (get_mbcinfo()->ismbcodepage)
608 while (*src && n)
610 n--;
611 if (_ismbblead(*src))
613 if (!*(src+1))
615 *dst++ = 0;
616 *dst++ = 0;
617 break;
620 *dst++ = *src++;
623 *dst++ = *src++;
626 else
628 while (n)
630 n--;
631 if (!(*dst++ = *src++)) break;
634 while (n--) *dst++ = 0;
635 return ret;
638 /*********************************************************************
639 * _mbsnbcpy_s(MSVCRT.@)
640 * REMARKS
641 * Unlike _mbsnbcpy this function does not pad the rest of the dest
642 * string with 0
644 int CDECL _mbsnbcpy_s(unsigned char* dst, MSVCRT_size_t size, const unsigned char* src, MSVCRT_size_t n)
646 MSVCRT_size_t pos = 0;
648 if(!dst || size == 0)
649 return MSVCRT_EINVAL;
650 if(!src)
652 dst[0] = '\0';
653 return MSVCRT_EINVAL;
655 if(!n)
656 return 0;
658 if(get_mbcinfo()->ismbcodepage)
660 int is_lead = 0;
661 while (*src && n)
663 if(pos == size)
665 dst[0] = '\0';
666 return MSVCRT_ERANGE;
668 is_lead = (!is_lead && _ismbblead(*src));
669 n--;
670 dst[pos++] = *src++;
673 if (is_lead) /* if string ends with a lead, remove it */
674 dst[pos - 1] = 0;
676 else
678 while (n)
680 n--;
681 if(pos == size)
683 dst[0] = '\0';
684 return MSVCRT_ERANGE;
687 if(!(*src)) break;
688 dst[pos++] = *src++;
692 if(pos < size)
693 dst[pos] = '\0';
694 else
696 dst[0] = '\0';
697 return MSVCRT_ERANGE;
700 return 0;
703 /*********************************************************************
704 * _mbsnbcpy(MSVCRT.@)
705 * REMARKS
706 * Like strncpy this function doesn't enforce the string to be
707 * NUL-terminated
709 unsigned char* CDECL _mbsnbcpy(unsigned char* dst, const unsigned char* src, MSVCRT_size_t n)
711 unsigned char* ret = dst;
712 if(!n)
713 return dst;
714 if(get_mbcinfo()->ismbcodepage)
716 int is_lead = 0;
717 while (*src && n)
719 is_lead = (!is_lead && _ismbblead(*src));
720 n--;
721 *dst++ = *src++;
724 if (is_lead) /* if string ends with a lead, remove it */
725 *(dst - 1) = 0;
727 else
729 while (n)
731 n--;
732 if (!(*dst++ = *src++)) break;
735 while (n--) *dst++ = 0;
736 return ret;
739 /*********************************************************************
740 * _mbscmp(MSVCRT.@)
742 int CDECL _mbscmp(const unsigned char* str, const unsigned char* cmp)
744 if(get_mbcinfo()->ismbcodepage)
746 unsigned int strc, cmpc;
747 do {
748 if(!*str)
749 return *cmp ? -1 : 0;
750 if(!*cmp)
751 return 1;
752 strc = _mbsnextc(str);
753 cmpc = _mbsnextc(cmp);
754 if(strc != cmpc)
755 return strc < cmpc ? -1 : 1;
756 str +=(strc > 255) ? 2 : 1;
757 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
758 } while(1);
760 return u_strcmp(str, cmp); /* ASCII CP */
763 /*********************************************************************
764 * _mbsnbicoll_l(MSVCRT.@)
766 int CDECL _mbsnbicoll_l(const unsigned char *str1, const unsigned char *str2, MSVCRT_size_t len, MSVCRT__locale_t locale)
768 MSVCRT_pthreadmbcinfo mbcinfo;
770 if(!locale)
771 mbcinfo = get_mbcinfo();
772 else
773 mbcinfo = locale->mbcinfo;
775 if(!mbcinfo->ismbcodepage)
776 return MSVCRT__strnicoll_l((const char*)str1, (const char*)str2, len, locale);
777 return CompareStringA(mbcinfo->mblcid, NORM_IGNORECASE, (const char*)str1, len, (const char*)str2, len)-CSTR_EQUAL;
780 /*********************************************************************
781 * _mbsicoll_l(MSVCRT.@)
783 int CDECL _mbsicoll_l(const unsigned char *str1, const unsigned char *str2, MSVCRT__locale_t locale)
785 return _mbsnbicoll_l(str1, str2, -1, locale);
788 /*********************************************************************
789 * _mbsnbicoll(MSVCRT.@)
791 int CDECL _mbsnbicoll(const unsigned char *str1, const unsigned char *str2, MSVCRT_size_t len)
793 return _mbsnbicoll_l(str1, str2, len, NULL);
796 /*********************************************************************
797 * _mbsicoll(MSVCRT.@)
799 int CDECL _mbsicoll(const unsigned char* str, const unsigned char* cmp)
801 return _mbsnbicoll_l(str, cmp, -1, NULL);
804 /*********************************************************************
805 * _mbsnbcoll_l(MSVCRT.@)
807 int CDECL _mbsnbcoll_l(const unsigned char *str1, const unsigned char *str2, MSVCRT_size_t len, MSVCRT__locale_t locale)
809 MSVCRT_pthreadmbcinfo mbcinfo;
811 if(!locale)
812 mbcinfo = get_mbcinfo();
813 else
814 mbcinfo = locale->mbcinfo;
816 if(!mbcinfo->ismbcodepage)
817 return MSVCRT__strncoll_l((const char*)str1, (const char*)str2, len, locale);
818 return CompareStringA(mbcinfo->mblcid, 0, (const char*)str1, len, (const char*)str2, len)-CSTR_EQUAL;
821 /*********************************************************************
822 * _mbscoll_l(MSVCRT.@)
824 int CDECL _mbscoll_l(const unsigned char *str1, const unsigned char *str2, MSVCRT__locale_t locale)
826 return _mbsnbcoll_l(str1, str2, -1, locale);
829 /*********************************************************************
830 * _mbsnbcoll(MSVCRT.@)
832 int CDECL _mbsnbcoll(const unsigned char *str1, const unsigned char *str2, MSVCRT_size_t len)
834 return _mbsnbcoll_l(str1, str2, len, NULL);
837 /*********************************************************************
838 * _mbscoll(MSVCRT.@)
840 int CDECL _mbscoll(const unsigned char* str, const unsigned char* cmp)
842 return _mbsnbcoll_l(str, cmp, -1, NULL);
845 /*********************************************************************
846 * _mbsicmp(MSVCRT.@)
848 int CDECL _mbsicmp(const unsigned char* str, const unsigned char* cmp)
850 if(get_mbcinfo()->ismbcodepage)
852 unsigned int strc, cmpc;
853 do {
854 if(!*str)
855 return *cmp ? -1 : 0;
856 if(!*cmp)
857 return 1;
858 strc = _mbctolower(_mbsnextc(str));
859 cmpc = _mbctolower(_mbsnextc(cmp));
860 if(strc != cmpc)
861 return strc < cmpc ? -1 : 1;
862 str +=(strc > 255) ? 2 : 1;
863 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
864 } while(1);
866 return u_strcasecmp(str, cmp); /* ASCII CP */
869 /*********************************************************************
870 * _mbsncmp(MSVCRT.@)
872 int CDECL _mbsncmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
874 if(!len)
875 return 0;
877 if(get_mbcinfo()->ismbcodepage)
879 unsigned int strc, cmpc;
880 while(len--)
882 int inc;
883 if(!*str)
884 return *cmp ? -1 : 0;
885 if(!*cmp)
886 return 1;
887 strc = _mbsnextc(str);
888 cmpc = _mbsnextc(cmp);
889 if(strc != cmpc)
890 return strc < cmpc ? -1 : 1;
891 inc=(strc > 255) ? 2 : 1; /* Equal, use same increment */
892 str += inc;
893 cmp += inc;
895 return 0; /* Matched len chars */
897 return u_strncmp(str, cmp, len); /* ASCII CP */
900 /*********************************************************************
901 * _mbsnbcmp(MSVCRT.@)
903 int CDECL _mbsnbcmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
905 if (!len)
906 return 0;
907 if(get_mbcinfo()->ismbcodepage)
909 unsigned int strc, cmpc;
910 while (len)
912 int clen;
913 if(!*str)
914 return *cmp ? -1 : 0;
915 if(!*cmp)
916 return 1;
917 if (_ismbblead(*str))
919 strc=(len>=2)?_mbsnextc(str):0;
920 clen=2;
922 else
924 strc=*str;
925 clen=1;
927 if (_ismbblead(*cmp))
928 cmpc=(len>=2)?_mbsnextc(cmp):0;
929 else
930 cmpc=*cmp;
931 if(strc != cmpc)
932 return strc < cmpc ? -1 : 1;
933 len -= clen;
934 str += clen;
935 cmp += clen;
937 return 0; /* Matched len chars */
939 return u_strncmp(str,cmp,len);
942 /*********************************************************************
943 * _mbsnicmp(MSVCRT.@)
945 * Compare two multibyte strings case insensitively to 'len' characters.
947 int CDECL _mbsnicmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
949 /* FIXME: No tolower() for mb strings yet */
950 if(get_mbcinfo()->ismbcodepage)
952 unsigned int strc, cmpc;
953 while(len--)
955 if(!*str)
956 return *cmp ? -1 : 0;
957 if(!*cmp)
958 return 1;
959 strc = _mbctolower(_mbsnextc(str));
960 cmpc = _mbctolower(_mbsnextc(cmp));
961 if(strc != cmpc)
962 return strc < cmpc ? -1 : 1;
963 str +=(strc > 255) ? 2 : 1;
964 cmp +=(strc > 255) ? 2 : 1; /* Equal, use same increment */
966 return 0; /* Matched len chars */
968 return u_strncasecmp(str, cmp, len); /* ASCII CP */
971 /*********************************************************************
972 * _mbsnbicmp(MSVCRT.@)
974 int CDECL _mbsnbicmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
976 if (!len)
977 return 0;
978 if(get_mbcinfo()->ismbcodepage)
980 unsigned int strc, cmpc;
981 while (len)
983 int clen;
984 if(!*str)
985 return *cmp ? -1 : 0;
986 if(!*cmp)
987 return 1;
988 if (_ismbblead(*str))
990 strc=(len>=2)?_mbsnextc(str):0;
991 clen=2;
993 else
995 strc=*str;
996 clen=1;
998 if (_ismbblead(*cmp))
999 cmpc=(len>=2)?_mbsnextc(cmp):0;
1000 else
1001 cmpc=*cmp;
1002 strc = _mbctolower(strc);
1003 cmpc = _mbctolower(cmpc);
1004 if(strc != cmpc)
1005 return strc < cmpc ? -1 : 1;
1006 len -= clen;
1007 str += clen;
1008 cmp += clen;
1010 return 0; /* Matched len bytes */
1012 return u_strncasecmp(str,cmp,len);
1015 /*********************************************************************
1016 * _mbscat (MSVCRT.@)
1018 unsigned char * CDECL _mbscat( unsigned char *dst, const unsigned char *src )
1020 strcat( (char *)dst, (const char *)src );
1021 return dst;
1024 /*********************************************************************
1025 * _mbscpy (MSVCRT.@)
1027 unsigned char* CDECL _mbscpy( unsigned char *dst, const unsigned char *src )
1029 strcpy( (char *)dst, (const char *)src );
1030 return dst;
1033 /*********************************************************************
1034 * _mbsstr (MSVCRT.@)
1036 unsigned char * CDECL _mbsstr(const unsigned char *haystack, const unsigned char *needle)
1038 return (unsigned char *)strstr( (const char *)haystack, (const char *)needle );
1041 /*********************************************************************
1042 * _mbschr(MSVCRT.@)
1044 * Find a multibyte character in a multibyte string.
1046 unsigned char* CDECL _mbschr(const unsigned char* s, unsigned int x)
1048 if(get_mbcinfo()->ismbcodepage)
1050 unsigned int c;
1051 while (1)
1053 c = _mbsnextc(s);
1054 if (c == x)
1055 return (unsigned char*)s;
1056 if (!c)
1057 return NULL;
1058 s += c > 255 ? 2 : 1;
1061 return u_strchr(s, x); /* ASCII CP */
1064 /*********************************************************************
1065 * _mbsrchr(MSVCRT.@)
1067 unsigned char* CDECL _mbsrchr(const unsigned char* s, unsigned int x)
1069 if(get_mbcinfo()->ismbcodepage)
1071 unsigned int c;
1072 unsigned char* match=NULL;
1073 if(!s)
1074 return NULL;
1075 while (1) {
1076 c = _mbsnextc(s);
1077 if (c == x)
1078 match=(unsigned char*)s;
1079 if (!c)
1080 return match;
1081 s +=(c > 255) ? 2 : 1;
1084 return u_strrchr(s, x);
1087 /*********************************************************************
1088 * _mbstok_s_l(MSVCRT.@)
1090 unsigned char* CDECL _mbstok_s_l(unsigned char *str, const unsigned char *delim,
1091 unsigned char **ctx, MSVCRT__locale_t locale)
1093 MSVCRT_pthreadmbcinfo mbcinfo;
1094 unsigned int c;
1096 if(!MSVCRT_CHECK_PMT(delim != NULL)) return NULL;
1097 if(!MSVCRT_CHECK_PMT(ctx != NULL)) return NULL;
1098 if(!MSVCRT_CHECK_PMT(str || *ctx)) return NULL;
1100 if(locale)
1101 mbcinfo = locale->mbcinfo;
1102 else
1103 mbcinfo = get_mbcinfo();
1105 if(!mbcinfo->ismbcodepage)
1106 return (unsigned char*)MSVCRT_strtok_s((char*)str, (const char*)delim, (char**)ctx);
1108 if(!str)
1109 str = *ctx;
1111 while((c=_mbsnextc(str)) && _mbschr(delim, c))
1112 str += c>255 ? 2 : 1;
1113 if(!*str)
1114 return NULL;
1116 *ctx = str + (c>255 ? 2 : 1);
1117 while((c=_mbsnextc(*ctx)) && !_mbschr(delim, c))
1118 *ctx += c>255 ? 2 : 1;
1119 if (**ctx) {
1120 *(*ctx)++ = 0;
1121 if(c > 255)
1122 *(*ctx)++ = 0;
1125 return str;
1129 /*********************************************************************
1130 * _mbstok_s(MSVCRT.@)
1132 unsigned char* CDECL _mbstok_s(unsigned char *str,
1133 const unsigned char *delim, unsigned char **ctx)
1135 return _mbstok_s_l(str, delim, ctx, NULL);
1138 /*********************************************************************
1139 * _mbstok_l(MSVCRT.@)
1141 unsigned char* CDECL _mbstok_l(unsigned char *str,
1142 const unsigned char *delim, MSVCRT__locale_t locale)
1144 return _mbstok_s_l(str, delim, &msvcrt_get_thread_data()->mbstok_next, locale);
1147 /*********************************************************************
1148 * _mbstok(MSVCRT.@)
1150 unsigned char* CDECL _mbstok(unsigned char *str, const unsigned char *delim)
1152 thread_data_t *data = msvcrt_get_thread_data();
1154 if(!str && !data->mbstok_next)
1155 return NULL;
1157 return _mbstok_s_l(str, delim, &data->mbstok_next, NULL);
1160 /*********************************************************************
1161 * _mbbtombc(MSVCRT.@)
1163 unsigned int CDECL _mbbtombc(unsigned int c)
1165 if(get_mbcinfo()->mbcodepage == 932)
1167 if(c >= 0x20 && c <= 0x7e) {
1168 if((c >= 0x41 && c <= 0x5a) || (c >= 0x61 && c <= 0x7a) || (c >= 0x30 && c <= 0x39))
1169 return mbbtombc_932[c - 0x20] | 0x8200;
1170 else
1171 return mbbtombc_932[c - 0x20] | 0x8100;
1173 else if(c >= 0xa1 && c <= 0xdf) {
1174 if(c >= 0xa6 && c <= 0xdd && c != 0xb0)
1175 return mbbtombc_932[c - 0xa1 + 0x5f] | 0x8300;
1176 else
1177 return mbbtombc_932[c - 0xa1 + 0x5f] | 0x8100;
1180 return c; /* not Japanese or no MB char */
1183 /*********************************************************************
1184 * _mbbtype(MSVCRT.@)
1186 int CDECL _mbbtype(unsigned char c, int type)
1188 if (type == 1)
1190 if ((c >= 0x20 && c <= 0x7e) || (c >= 0xa1 && c <= 0xdf))
1191 return _MBC_SINGLE;
1192 else if ((c >= 0x40 && c <= 0x7e) || (c >= 0x80 && c <= 0xfc))
1193 return _MBC_TRAIL;
1194 else
1195 return _MBC_ILLEGAL;
1197 else
1199 if ((c >= 0x20 && c <= 0x7e) || (c >= 0xa1 && c <= 0xdf))
1200 return _MBC_SINGLE;
1201 else if ((c >= 0x81 && c <= 0x9f) || (c >= 0xe0 && c <= 0xfc))
1202 return _MBC_LEAD;
1203 else
1204 return _MBC_ILLEGAL;
1208 /*********************************************************************
1209 * _ismbbkana(MSVCRT.@)
1211 int CDECL _ismbbkana(unsigned int c)
1213 /* FIXME: use lc_ctype when supported, not lc_all */
1214 if(get_mbcinfo()->mbcodepage == 932)
1216 /* Japanese/Katakana, CP 932 */
1217 return (c >= 0xa1 && c <= 0xdf);
1219 return 0;
1222 /*********************************************************************
1223 * _ismbcdigit(MSVCRT.@)
1225 int CDECL _ismbcdigit(unsigned int ch)
1227 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1228 return (get_char_typeW( wch ) & C1_DIGIT);
1231 /*********************************************************************
1232 * _ismbcgraph(MSVCRT.@)
1234 int CDECL _ismbcgraph(unsigned int ch)
1236 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1237 return (get_char_typeW( wch ) & (C1_UPPER | C1_LOWER | C1_DIGIT | C1_PUNCT | C1_ALPHA));
1240 /*********************************************************************
1241 * _ismbcalpha (MSVCRT.@)
1243 int CDECL _ismbcalpha(unsigned int ch)
1245 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1246 return (get_char_typeW( wch ) & C1_ALPHA);
1249 /*********************************************************************
1250 * _ismbclower (MSVCRT.@)
1252 int CDECL _ismbclower(unsigned int ch)
1254 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1255 return (get_char_typeW( wch ) & C1_UPPER);
1258 /*********************************************************************
1259 * _ismbcupper (MSVCRT.@)
1261 int CDECL _ismbcupper(unsigned int ch)
1263 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1264 return (get_char_typeW( wch ) & C1_LOWER);
1267 /*********************************************************************
1268 * _ismbcsymbol(MSVCRT.@)
1270 int CDECL _ismbcsymbol(unsigned int ch)
1272 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1273 WORD ctype;
1274 if (!GetStringTypeW(CT_CTYPE3, &wch, 1, &ctype))
1276 WARN("GetStringTypeW failed on %x\n", ch);
1277 return 0;
1279 return ((ctype & C3_SYMBOL) != 0);
1282 /*********************************************************************
1283 * _ismbcalnum (MSVCRT.@)
1285 int CDECL _ismbcalnum(unsigned int ch)
1287 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1288 return (get_char_typeW( wch ) & (C1_ALPHA | C1_DIGIT));
1291 /*********************************************************************
1292 * _ismbcspace (MSVCRT.@)
1294 int CDECL _ismbcspace(unsigned int ch)
1296 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1297 return (get_char_typeW( wch ) & C1_SPACE);
1300 /*********************************************************************
1301 * _ismbcprint (MSVCRT.@)
1303 int CDECL _ismbcprint(unsigned int ch)
1305 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1306 return (get_char_typeW( wch ) & (C1_UPPER | C1_LOWER | C1_DIGIT | C1_PUNCT | C1_ALPHA | C1_SPACE));
1309 /*********************************************************************
1310 * _ismbcpunct(MSVCRT.@)
1312 int CDECL _ismbcpunct(unsigned int ch)
1314 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1315 return (get_char_typeW( wch ) & C1_PUNCT);
1318 /*********************************************************************
1319 * _ismbchira(MSVCRT.@)
1321 int CDECL _ismbchira(unsigned int c)
1323 /* FIXME: use lc_ctype when supported, not lc_all */
1324 if(get_mbcinfo()->mbcodepage == 932)
1326 /* Japanese/Hiragana, CP 932 */
1327 return (c >= 0x829f && c <= 0x82f1);
1329 return 0;
1332 /*********************************************************************
1333 * _ismbckata(MSVCRT.@)
1335 int CDECL _ismbckata(unsigned int c)
1337 /* FIXME: use lc_ctype when supported, not lc_all */
1338 if(get_mbcinfo()->mbcodepage == 932)
1340 if(c < 256)
1341 return _ismbbkana(c);
1342 /* Japanese/Katakana, CP 932 */
1343 return (c >= 0x8340 && c <= 0x8396 && c != 0x837f);
1345 return 0;
1348 /*********************************************************************
1349 * _ismbblead(MSVCRT.@)
1351 int CDECL _ismbblead(unsigned int c)
1353 return (get_mbcinfo()->mbctype[(c&0xff) + 1] & _M1) != 0;
1357 /*********************************************************************
1358 * _ismbbtrail(MSVCRT.@)
1360 int CDECL _ismbbtrail(unsigned int c)
1362 return (get_mbcinfo()->mbctype[(c&0xff) + 1] & _M2) != 0;
1365 /*********************************************************************
1366 * _ismbclegal(MSVCRT.@)
1368 int CDECL _ismbclegal(unsigned int c)
1370 return _ismbblead(HIBYTE(c)) && _ismbbtrail(LOBYTE(c));
1373 /*********************************************************************
1374 * _ismbslead(MSVCRT.@)
1376 int CDECL _ismbslead(const unsigned char* start, const unsigned char* str)
1378 int lead = 0;
1380 if(!get_mbcinfo()->ismbcodepage)
1381 return 0;
1383 /* Lead bytes can also be trail bytes so we need to analyse the string
1385 while (start <= str)
1387 if (!*start)
1388 return 0;
1389 lead = !lead && _ismbblead(*start);
1390 start++;
1393 return lead ? -1 : 0;
1396 /*********************************************************************
1397 * _ismbstrail(MSVCRT.@)
1399 int CDECL _ismbstrail(const unsigned char* start, const unsigned char* str)
1401 /* Note: this function doesn't check _ismbbtrail */
1402 if ((str > start) && _ismbslead(start, str-1))
1403 return -1;
1404 else
1405 return 0;
1408 /*********************************************************************
1409 * _mbsbtype (MSVCRT.@)
1411 int CDECL _mbsbtype(const unsigned char *str, MSVCRT_size_t count)
1413 int lead = 0;
1414 const unsigned char *end = str + count;
1416 /* Lead bytes can also be trail bytes so we need to analyse the string.
1417 * Also we must return _MBC_ILLEGAL for chars past the end of the string
1419 while (str < end) /* Note: we skip the last byte - will check after the loop */
1421 if (!*str)
1422 return _MBC_ILLEGAL;
1423 lead = get_mbcinfo()->ismbcodepage && !lead && _ismbblead(*str);
1424 str++;
1427 if (lead)
1428 if (_ismbbtrail(*str))
1429 return _MBC_TRAIL;
1430 else
1431 return _MBC_ILLEGAL;
1432 else
1433 if (_ismbblead(*str))
1434 return _MBC_LEAD;
1435 else
1436 return _MBC_SINGLE;
1439 /*********************************************************************
1440 * _mbsset(MSVCRT.@)
1442 unsigned char* CDECL _mbsset(unsigned char* str, unsigned int c)
1444 unsigned char* ret = str;
1446 if(!get_mbcinfo()->ismbcodepage || c < 256)
1447 return u__strset(str, c); /* ASCII CP or SB char */
1449 c &= 0xffff; /* Strip high bits */
1451 while(str[0] && str[1])
1453 *str++ = c >> 8;
1454 *str++ = c & 0xff;
1456 if(str[0])
1457 str[0] = '\0'; /* FIXME: OK to shorten? */
1459 return ret;
1462 /*********************************************************************
1463 * _mbsnbset(MSVCRT.@)
1465 unsigned char* CDECL _mbsnbset(unsigned char *str, unsigned int c, MSVCRT_size_t len)
1467 unsigned char *ret = str;
1469 if(!len)
1470 return ret;
1472 if(!get_mbcinfo()->ismbcodepage || c < 256)
1473 return u__strnset(str, c, len); /* ASCII CP or SB char */
1475 c &= 0xffff; /* Strip high bits */
1477 while(str[0] && str[1] && (len > 1))
1479 *str++ = c >> 8;
1480 len--;
1481 *str++ = c & 0xff;
1482 len--;
1484 if(len && str[0]) {
1485 /* as per msdn pad with a blank character */
1486 str[0] = ' ';
1489 return ret;
1492 /*********************************************************************
1493 * _mbsnset(MSVCRT.@)
1495 unsigned char* CDECL _mbsnset(unsigned char* str, unsigned int c, MSVCRT_size_t len)
1497 unsigned char *ret = str;
1499 if(!len)
1500 return ret;
1502 if(!get_mbcinfo()->ismbcodepage || c < 256)
1503 return u__strnset(str, c, len); /* ASCII CP or SB char */
1505 c &= 0xffff; /* Strip high bits */
1507 while(str[0] && str[1] && len--)
1509 *str++ = c >> 8;
1510 *str++ = c & 0xff;
1512 if(len && str[0])
1513 str[0] = '\0'; /* FIXME: OK to shorten? */
1515 return ret;
1518 /*********************************************************************
1519 * _mbsnccnt(MSVCRT.@)
1520 * 'c' is for 'character'.
1522 MSVCRT_size_t CDECL _mbsnccnt(const unsigned char* str, MSVCRT_size_t len)
1524 MSVCRT_size_t ret;
1525 if(get_mbcinfo()->ismbcodepage)
1527 ret=0;
1528 while(*str && len-- > 0)
1530 if(_ismbblead(*str))
1532 if (!len)
1533 break;
1534 len--;
1535 str++;
1537 str++;
1538 ret++;
1540 return ret;
1542 ret=u_strlen(str);
1543 return min(ret, len); /* ASCII CP */
1546 /*********************************************************************
1547 * _mbsnbcnt(MSVCRT.@)
1548 * 'b' is for byte count.
1550 MSVCRT_size_t CDECL _mbsnbcnt(const unsigned char* str, MSVCRT_size_t len)
1552 MSVCRT_size_t ret;
1553 if(get_mbcinfo()->ismbcodepage)
1555 const unsigned char* xstr = str;
1556 while(*xstr && len-- > 0)
1558 if (_ismbblead(*xstr++))
1559 xstr++;
1561 return xstr-str;
1563 ret=u_strlen(str);
1564 return min(ret, len); /* ASCII CP */
1567 /*********************************************************************
1568 * _mbsnbcat(MSVCRT.@)
1570 unsigned char* CDECL _mbsnbcat(unsigned char* dst, const unsigned char* src, MSVCRT_size_t len)
1572 if(get_mbcinfo()->ismbcodepage)
1574 unsigned char *res = dst;
1575 while (*dst) {
1576 if (_ismbblead(*dst++)) {
1577 if (*dst) {
1578 dst++;
1579 } else {
1580 /* as per msdn overwrite the lead byte in front of '\0' */
1581 dst--;
1582 break;
1586 while (*src && len--) *dst++ = *src++;
1587 *dst = '\0';
1588 return res;
1590 return u_strncat(dst, src, len); /* ASCII CP */
1593 int CDECL _mbsnbcat_s(unsigned char *dst, MSVCRT_size_t size, const unsigned char *src, MSVCRT_size_t len)
1595 unsigned char *ptr = dst;
1596 MSVCRT_size_t i;
1598 if (!dst && !size && !src && !len)
1599 return 0;
1601 if (!dst || !size || !src)
1603 if (dst && size)
1604 *dst = '\0';
1606 *MSVCRT__errno() = MSVCRT_EINVAL;
1607 return MSVCRT_EINVAL;
1610 /* Find the null terminator of the destination buffer. */
1611 while (size && *ptr)
1612 size--, ptr++;
1614 if (!size)
1616 *dst = '\0';
1617 *MSVCRT__errno() = MSVCRT_EINVAL;
1618 return MSVCRT_EINVAL;
1621 /* If necessary, check that the character preceding the null terminator is
1622 * a lead byte and move the pointer back by one for later overwrite. */
1623 if (ptr != dst && get_mbcinfo()->ismbcodepage && _ismbblead(*(ptr - 1)))
1624 size++, ptr--;
1626 for (i = 0; *src && i < len; i++)
1628 *ptr++ = *src++;
1629 size--;
1631 if (!size)
1633 *dst = '\0';
1634 *MSVCRT__errno() = MSVCRT_ERANGE;
1635 return MSVCRT_ERANGE;
1639 *ptr = '\0';
1640 return 0;
1643 /*********************************************************************
1644 * _mbsncat(MSVCRT.@)
1646 unsigned char* CDECL _mbsncat(unsigned char* dst, const unsigned char* src, MSVCRT_size_t len)
1648 if(get_mbcinfo()->ismbcodepage)
1650 unsigned char *res = dst;
1651 while (*dst)
1653 if (_ismbblead(*dst++))
1654 dst++;
1656 while (*src && len--)
1658 *dst++ = *src;
1659 if(_ismbblead(*src++))
1660 *dst++ = *src++;
1662 *dst = '\0';
1663 return res;
1665 return u_strncat(dst, src, len); /* ASCII CP */
1669 /*********************************************************************
1670 * _mbslwr(MSVCRT.@)
1672 unsigned char* CDECL _mbslwr(unsigned char* s)
1674 unsigned char *ret = s;
1675 if (!s)
1676 return NULL;
1677 if (get_mbcinfo()->ismbcodepage)
1679 unsigned int c;
1680 while (*s)
1682 c = _mbctolower(_mbsnextc(s));
1683 /* Note that I assume that the size of the character is unchanged */
1684 if (c > 255)
1686 *s++=(c>>8);
1687 c=c & 0xff;
1689 *s++=c;
1692 else for ( ; *s; s++) *s = tolower(*s);
1693 return ret;
1697 /*********************************************************************
1698 * _mbslwr_s(MSVCRT.@)
1700 int CDECL _mbslwr_s(unsigned char* s, MSVCRT_size_t len)
1702 if (!s && !len)
1704 return 0;
1706 else if (!s || !len)
1708 *MSVCRT__errno() = MSVCRT_EINVAL;
1709 return MSVCRT_EINVAL;
1711 if (get_mbcinfo()->ismbcodepage)
1713 unsigned int c;
1714 for ( ; *s && len > 0; len--)
1716 c = _mbctolower(_mbsnextc(s));
1717 /* Note that I assume that the size of the character is unchanged */
1718 if (c > 255)
1720 *s++=(c>>8);
1721 c=c & 0xff;
1723 *s++=c;
1726 else for ( ; *s && len > 0; s++, len--) *s = tolower(*s);
1727 if (*s)
1729 *s = '\0';
1730 *MSVCRT__errno() = MSVCRT_EINVAL;
1731 return MSVCRT_EINVAL;
1733 return 0;
1737 /*********************************************************************
1738 * _mbsupr(MSVCRT.@)
1740 unsigned char* CDECL _mbsupr(unsigned char* s)
1742 unsigned char *ret = s;
1743 if (!s)
1744 return NULL;
1745 if (get_mbcinfo()->ismbcodepage)
1747 unsigned int c;
1748 while (*s)
1750 c = _mbctoupper(_mbsnextc(s));
1751 /* Note that I assume that the size of the character is unchanged */
1752 if (c > 255)
1754 *s++=(c>>8);
1755 c=c & 0xff;
1757 *s++=c;
1760 else for ( ; *s; s++) *s = toupper(*s);
1761 return ret;
1765 /*********************************************************************
1766 * _mbsupr_s(MSVCRT.@)
1768 int CDECL _mbsupr_s(unsigned char* s, MSVCRT_size_t len)
1770 if (!s && !len)
1772 return 0;
1774 else if (!s || !len)
1776 *MSVCRT__errno() = MSVCRT_EINVAL;
1777 return MSVCRT_EINVAL;
1779 if (get_mbcinfo()->ismbcodepage)
1781 unsigned int c;
1782 for ( ; *s && len > 0; len--)
1784 c = _mbctoupper(_mbsnextc(s));
1785 /* Note that I assume that the size of the character is unchanged */
1786 if (c > 255)
1788 *s++=(c>>8);
1789 c=c & 0xff;
1791 *s++=c;
1794 else for ( ; *s && len > 0; s++, len--) *s = toupper(*s);
1795 if (*s)
1797 *s = '\0';
1798 *MSVCRT__errno() = MSVCRT_EINVAL;
1799 return MSVCRT_EINVAL;
1801 return 0;
1805 /*********************************************************************
1806 * _mbsspn (MSVCRT.@)
1808 MSVCRT_size_t CDECL _mbsspn(const unsigned char* string, const unsigned char* set)
1810 const unsigned char *p, *q;
1812 for (p = string; *p; p++)
1814 if (_ismbblead(*p))
1816 for (q = set; *q; q++)
1818 if (!q[1])
1819 break;
1820 if ((*p == *q) && (p[1] == q[1]))
1821 break;
1822 q++;
1824 if (!q[0] || !q[1]) break;
1826 else
1828 for (q = set; *q; q++)
1829 if (*p == *q)
1830 break;
1831 if (!*q) break;
1834 return p - string;
1837 /*********************************************************************
1838 * _mbsspnp (MSVCRT.@)
1840 unsigned char* CDECL _mbsspnp(const unsigned char* string, const unsigned char* set)
1842 const unsigned char *p, *q;
1844 for (p = string; *p; p++)
1846 if (_ismbblead(*p))
1848 for (q = set; *q; q++)
1850 if (!q[1])
1851 break;
1852 if ((*p == *q) && (p[1] == q[1]))
1853 break;
1854 q++;
1856 if (!q[0] || !q[1]) break;
1858 else
1860 for (q = set; *q; q++)
1861 if (*p == *q)
1862 break;
1863 if (!*q) break;
1866 if (*p == '\0')
1867 return NULL;
1868 return (unsigned char *)p;
1871 /*********************************************************************
1872 * _mbscspn(MSVCRT.@)
1874 MSVCRT_size_t CDECL _mbscspn(const unsigned char* str, const unsigned char* cmp)
1876 if (get_mbcinfo()->ismbcodepage)
1877 FIXME("don't handle double character case\n");
1878 return u_strcspn(str, cmp);
1881 /*********************************************************************
1882 * _mbsrev (MSVCRT.@)
1884 unsigned char* CDECL _mbsrev(unsigned char* str)
1886 int i, len = _mbslen(str);
1887 unsigned char *p, *temp=MSVCRT_malloc(len*2);
1889 if(!temp)
1890 return str;
1892 /* unpack multibyte string to temp buffer */
1893 p=str;
1894 for(i=0; i<len; i++)
1896 if (_ismbblead(*p))
1898 temp[i*2]=*p++;
1899 temp[i*2+1]=*p++;
1901 else
1903 temp[i*2]=*p++;
1904 temp[i*2+1]=0;
1908 /* repack it in the reverse order */
1909 p=str;
1910 for(i=len-1; i>=0; i--)
1912 if(_ismbblead(temp[i*2]))
1914 *p++=temp[i*2];
1915 *p++=temp[i*2+1];
1917 else
1919 *p++=temp[i*2];
1923 MSVCRT_free(temp);
1925 return str;
1928 /*********************************************************************
1929 * _mbspbrk (MSVCRT.@)
1931 unsigned char* CDECL _mbspbrk(const unsigned char* str, const unsigned char* accept)
1933 const unsigned char* p;
1935 while(*str)
1937 for(p = accept; *p; p += (_ismbblead(*p)?2:1) )
1939 if (*p == *str)
1940 if( !_ismbblead(*p) || ( *(p+1) == *(str+1) ) )
1941 return (unsigned char*)str;
1943 str += (_ismbblead(*str)?2:1);
1945 return NULL;
1950 * Functions depending on locale codepage
1953 /*********************************************************************
1954 * mblen(MSVCRT.@)
1955 * REMARKS
1956 * Unlike most of the multibyte string functions this function uses
1957 * the locale codepage, not the codepage set by _setmbcp
1959 int CDECL MSVCRT_mblen(const char* str, MSVCRT_size_t size)
1961 if (str && *str && size)
1963 if(get_locinfo()->mb_cur_max == 1)
1964 return 1; /* ASCII CP */
1966 return !MSVCRT_isleadbyte((unsigned char)*str) ? 1 : (size>1 ? 2 : -1);
1968 return 0;
1971 /*********************************************************************
1972 * mbrlen(MSVCRT.@)
1974 MSVCRT_size_t CDECL MSVCRT_mbrlen(const char *str, MSVCRT_size_t len, MSVCRT_mbstate_t *state)
1976 MSVCRT_mbstate_t s = (state ? *state : 0);
1977 MSVCRT_size_t ret;
1979 if(!len || !str || !*str)
1980 return 0;
1982 if(get_locinfo()->mb_cur_max == 1) {
1983 return 1;
1984 }else if(!s && MSVCRT_isleadbyte((unsigned char)*str)) {
1985 if(len == 1) {
1986 s = (unsigned char)*str;
1987 ret = -2;
1988 }else {
1989 ret = 2;
1991 }else if(!s) {
1992 ret = 1;
1993 }else {
1994 s = 0;
1995 ret = 2;
1998 if(state)
1999 *state = s;
2000 return ret;
2003 /*********************************************************************
2004 * _mbstrlen_l(MSVCRT.@)
2006 MSVCRT_size_t CDECL _mbstrlen_l(const char* str, MSVCRT__locale_t locale)
2008 MSVCRT_pthreadlocinfo locinfo;
2010 if(!locale)
2011 locinfo = get_locinfo();
2012 else
2013 locinfo = locale->locinfo;
2015 if(locinfo->mb_cur_max > 1) {
2016 MSVCRT_size_t len;
2017 len = MultiByteToWideChar(locinfo->lc_codepage, MB_ERR_INVALID_CHARS,
2018 str, -1, NULL, 0);
2019 if (!len) {
2020 *MSVCRT__errno() = MSVCRT_EILSEQ;
2021 return -1;
2023 return len - 1;
2026 return strlen(str);
2029 /*********************************************************************
2030 * _mbstrlen(MSVCRT.@)
2032 MSVCRT_size_t CDECL _mbstrlen(const char* str)
2034 return _mbstrlen_l(str, NULL);
2037 /*********************************************************************
2038 * _mbtowc_l(MSVCRT.@)
2040 int CDECL MSVCRT_mbtowc_l(MSVCRT_wchar_t *dst, const char* str, MSVCRT_size_t n, MSVCRT__locale_t locale)
2042 MSVCRT_pthreadlocinfo locinfo;
2043 MSVCRT_wchar_t tmpdst = '\0';
2045 if(!locale)
2046 locinfo = get_locinfo();
2047 else
2048 locinfo = locale->locinfo;
2050 if(n <= 0 || !str)
2051 return 0;
2052 if(!locinfo->lc_codepage)
2053 tmpdst = (unsigned char)*str;
2054 else if(!MultiByteToWideChar(locinfo->lc_codepage, 0, str, n, &tmpdst, 1))
2055 return -1;
2056 if(dst)
2057 *dst = tmpdst;
2058 /* return the number of bytes from src that have been used */
2059 if(!*str)
2060 return 0;
2061 if(n >= 2 && MSVCRT__isleadbyte_l((unsigned char)*str, locale) && str[1])
2062 return 2;
2063 return 1;
2066 /*********************************************************************
2067 * mbtowc(MSVCRT.@)
2069 int CDECL MSVCRT_mbtowc(MSVCRT_wchar_t *dst, const char* str, MSVCRT_size_t n)
2071 return MSVCRT_mbtowc_l(dst, str, n, NULL);
2074 /*********************************************************************
2075 * mbrtowc(MSVCRT.@)
2077 MSVCRT_size_t CDECL MSVCRT_mbrtowc(MSVCRT_wchar_t *dst, const char *str,
2078 MSVCRT_size_t n, MSVCRT_mbstate_t *state)
2080 MSVCRT_pthreadlocinfo locinfo = get_locinfo();
2081 MSVCRT_mbstate_t s = (state ? *state : 0);
2082 char tmpstr[2];
2083 int len = 0;
2085 if(dst)
2086 *dst = 0;
2088 if(!n || !str || !*str)
2089 return 0;
2091 if(locinfo->mb_cur_max == 1) {
2092 tmpstr[len++] = *str;
2093 }else if(!s && MSVCRT_isleadbyte((unsigned char)*str)) {
2094 if(n == 1) {
2095 s = (unsigned char)*str;
2096 len = -2;
2097 }else {
2098 tmpstr[0] = str[0];
2099 tmpstr[1] = str[1];
2100 len = 2;
2102 }else if(!s) {
2103 tmpstr[len++] = *str;
2104 }else {
2105 tmpstr[0] = s;
2106 tmpstr[1] = *str;
2107 len = 2;
2108 s = 0;
2111 if(len > 0) {
2112 if(!MultiByteToWideChar(locinfo->lc_codepage, 0, tmpstr, len, dst, dst ? 1 : 0))
2113 len = -1;
2116 if(state)
2117 *state = s;
2118 return len;
2121 /*********************************************************************
2122 * _mbstowcs_l(MSVCRT.@)
2124 MSVCRT_size_t CDECL MSVCRT__mbstowcs_l(MSVCRT_wchar_t *wcstr, const char *mbstr,
2125 MSVCRT_size_t count, MSVCRT__locale_t locale)
2127 MSVCRT_pthreadlocinfo locinfo;
2128 MSVCRT_size_t i, size;
2130 if(!locale)
2131 locinfo = get_locinfo();
2132 else
2133 locinfo = locale->locinfo;
2135 /* Ignore count parameter */
2136 if(!wcstr)
2137 return MultiByteToWideChar(locinfo->lc_codepage, 0, mbstr, -1, NULL, 0)-1;
2139 for(i=0, size=0; i<count; i++) {
2140 if(mbstr[size] == '\0')
2141 break;
2143 size += (MSVCRT__isleadbyte_l((unsigned char)mbstr[size], locale) ? 2 : 1);
2146 size = MultiByteToWideChar(locinfo->lc_codepage, 0,
2147 mbstr, size, wcstr, count);
2149 if(size<count && wcstr)
2150 wcstr[size] = '\0';
2152 return size;
2155 /*********************************************************************
2156 * mbstowcs(MSVCRT.@)
2158 MSVCRT_size_t CDECL MSVCRT_mbstowcs(MSVCRT_wchar_t *wcstr,
2159 const char *mbstr, MSVCRT_size_t count)
2161 return MSVCRT__mbstowcs_l(wcstr, mbstr, count, NULL);
2164 /*********************************************************************
2165 * _mbstowcs_s_l(MSVCRT.@)
2167 int CDECL MSVCRT__mbstowcs_s_l(MSVCRT_size_t *ret, MSVCRT_wchar_t *wcstr,
2168 MSVCRT_size_t size, const char *mbstr,
2169 MSVCRT_size_t count, MSVCRT__locale_t locale)
2171 MSVCRT_size_t conv;
2173 if(!wcstr && !size) {
2174 conv = MSVCRT__mbstowcs_l(NULL, mbstr, 0, locale);
2175 if(ret)
2176 *ret = conv+1;
2177 return 0;
2180 if (!MSVCRT_CHECK_PMT(wcstr != NULL)) return MSVCRT_EINVAL;
2181 if (!MSVCRT_CHECK_PMT(mbstr != NULL)) {
2182 if(size) wcstr[0] = '\0';
2183 return MSVCRT_EINVAL;
2186 if(count==MSVCRT__TRUNCATE || size<count)
2187 conv = size;
2188 else
2189 conv = count;
2191 conv = MSVCRT__mbstowcs_l(wcstr, mbstr, conv, locale);
2192 if(conv<size)
2193 wcstr[conv++] = '\0';
2194 else if(conv==size && (count==MSVCRT__TRUNCATE || wcstr[conv-1]=='\0'))
2195 wcstr[conv-1] = '\0';
2196 else {
2197 MSVCRT_INVALID_PMT("wcstr[size] is too small", MSVCRT_ERANGE);
2198 if(size)
2199 wcstr[0] = '\0';
2200 return MSVCRT_ERANGE;
2203 if(ret)
2204 *ret = conv;
2205 return 0;
2208 /*********************************************************************
2209 * mbstowcs_s(MSVCRT.@)
2211 int CDECL MSVCRT__mbstowcs_s(MSVCRT_size_t *ret, MSVCRT_wchar_t *wcstr,
2212 MSVCRT_size_t size, const char *mbstr, MSVCRT_size_t count)
2214 return MSVCRT__mbstowcs_s_l(ret, wcstr, size, mbstr, count, NULL);
2217 /*********************************************************************
2218 * mbsrtowcs(MSVCRT.@)
2220 MSVCRT_size_t CDECL MSVCRT_mbsrtowcs(MSVCRT_wchar_t *wcstr,
2221 const char **pmbstr, MSVCRT_size_t count, MSVCRT_mbstate_t *state)
2223 MSVCRT_mbstate_t s = (state ? *state : 0);
2224 MSVCRT_wchar_t tmpdst;
2225 MSVCRT_size_t ret = 0;
2227 if(!MSVCRT_CHECK_PMT(pmbstr != NULL))
2228 return -1;
2230 while(!wcstr || count>ret) {
2231 int ch_len = MSVCRT_mbrtowc(&tmpdst, *pmbstr, 2, &s);
2232 if(wcstr)
2233 wcstr[ret] = tmpdst;
2235 if(ch_len < 0) {
2236 return -1;
2237 }else if(ch_len == 0) {
2238 *pmbstr = NULL;
2239 return ret;
2242 *pmbstr += ch_len;
2243 ret++;
2246 return ret;