msvcrt: Update comments for non-exported functions.
[wine/multimedia.git] / dlls / msvcrt / mbcs.c
blobdd14abd336bf0b8c40e1185f45bb5c6b7cf75405
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 /*********************************************************************
188 * ___mb_cur_max_l_func (MSVCR80.@)
190 int* CDECL ___mb_cur_max_l_func(MSVCRT__locale_t locale)
192 MSVCRT_pthreadlocinfo locinfo;
194 if(!locale)
195 locinfo = get_locinfo();
196 else
197 locinfo = locale->locinfo;
199 return &locinfo->mb_cur_max;
202 /*********************************************************************
203 * INTERNAL: _setmbcp_l
205 int _setmbcp_l(int cp, LCID lcid, MSVCRT_pthreadmbcinfo mbcinfo)
207 const char format[] = ".%d";
209 int newcp;
210 CPINFO cpi;
211 BYTE *bytes;
212 WORD chartypes[256];
213 char bufA[256];
214 WCHAR bufW[256];
215 int charcount;
216 int ret;
217 int i;
219 if(!mbcinfo)
220 mbcinfo = get_mbcinfo();
222 switch (cp)
224 case _MB_CP_ANSI:
225 newcp = GetACP();
226 break;
227 case _MB_CP_OEM:
228 newcp = GetOEMCP();
229 break;
230 case _MB_CP_LOCALE:
231 newcp = get_locinfo()->lc_codepage;
232 if(newcp)
233 break;
234 /* fall through (C locale) */
235 case _MB_CP_SBCS:
236 newcp = 20127; /* ASCII */
237 break;
238 default:
239 newcp = cp;
240 break;
243 if(lcid == -1) {
244 sprintf(bufA, format, newcp);
245 mbcinfo->mblcid = MSVCRT_locale_to_LCID(bufA, NULL);
246 } else {
247 mbcinfo->mblcid = lcid;
250 if(mbcinfo->mblcid == -1)
252 WARN("Can't assign LCID to codepage (%d)\n", mbcinfo->mblcid);
253 mbcinfo->mblcid = 0;
256 if (!GetCPInfo(newcp, &cpi))
258 WARN("Codepage %d not found\n", newcp);
259 *MSVCRT__errno() = MSVCRT_EINVAL;
260 return -1;
263 /* setup the _mbctype */
264 memset(mbcinfo->mbctype, 0, sizeof(unsigned char[257]));
265 memset(mbcinfo->mbcasemap, 0, sizeof(unsigned char[256]));
267 bytes = cpi.LeadByte;
268 while (bytes[0] || bytes[1])
270 for (i = bytes[0]; i <= bytes[1]; i++)
271 mbcinfo->mbctype[i + 1] |= _M1;
272 bytes += 2;
275 if (cpi.MaxCharSize > 1)
277 /* trail bytes not available through kernel32 but stored in a structure in msvcrt */
278 struct cp_extra_info_t *cpextra = g_cpextrainfo;
280 mbcinfo->ismbcodepage = 1;
281 while (TRUE)
283 if (cpextra->cp == 0 || cpextra->cp == newcp)
285 if (cpextra->cp == 0)
286 FIXME("trail bytes data not available for DBCS codepage %d - assuming all bytes\n", newcp);
288 bytes = cpextra->TrailBytes;
289 while (bytes[0] || bytes[1])
291 for (i = bytes[0]; i <= bytes[1]; i++)
292 mbcinfo->mbctype[i + 1] |= _M2;
293 bytes += 2;
295 break;
297 cpextra++;
300 else
301 mbcinfo->ismbcodepage = 0;
303 /* we can't use GetStringTypeA directly because we don't have a locale - only a code page
305 charcount = 0;
306 for (i = 0; i < 256; i++)
307 if (!(mbcinfo->mbctype[i + 1] & _M1))
308 bufA[charcount++] = i;
310 ret = MultiByteToWideChar(newcp, 0, bufA, charcount, bufW, charcount);
311 if (ret != charcount)
312 ERR("MultiByteToWideChar of chars failed for cp %d, ret=%d (exp %d), error=%d\n", newcp, ret, charcount, GetLastError());
314 GetStringTypeW(CT_CTYPE1, bufW, charcount, chartypes);
316 charcount = 0;
317 for (i = 0; i < 256; i++)
318 if (!(mbcinfo->mbctype[i + 1] & _M1))
320 if (chartypes[charcount] & C1_UPPER)
322 mbcinfo->mbctype[i + 1] |= _SBUP;
323 bufW[charcount] = tolowerW(bufW[charcount]);
325 else if (chartypes[charcount] & C1_LOWER)
327 mbcinfo->mbctype[i + 1] |= _SBLOW;
328 bufW[charcount] = toupperW(bufW[charcount]);
330 charcount++;
333 ret = WideCharToMultiByte(newcp, 0, bufW, charcount, bufA, charcount, NULL, NULL);
334 if (ret != charcount)
335 ERR("WideCharToMultiByte failed for cp %d, ret=%d (exp %d), error=%d\n", newcp, ret, charcount, GetLastError());
337 charcount = 0;
338 for (i = 0; i < 256; i++)
340 if(!(mbcinfo->mbctype[i + 1] & _M1))
342 if(mbcinfo->mbctype[i] & (C1_UPPER|C1_LOWER))
343 mbcinfo->mbcasemap[i] = bufA[charcount];
344 charcount++;
348 if (newcp == 932) /* CP932 only - set _MP and _MS */
350 /* On Windows it's possible to calculate the _MP and _MS from CT_CTYPE1
351 * and CT_CTYPE3. But as of Wine 0.9.43 we return wrong values what makes
352 * it hard. As this is set only for codepage 932 we hardcode it what gives
353 * also faster execution.
355 for (i = 161; i <= 165; i++)
356 mbcinfo->mbctype[i + 1] |= _MP;
357 for (i = 166; i <= 223; i++)
358 mbcinfo->mbctype[i + 1] |= _MS;
361 mbcinfo->mbcodepage = newcp;
362 if(MSVCRT_locale && mbcinfo == MSVCRT_locale->mbcinfo)
363 memcpy(MSVCRT_mbctype, MSVCRT_locale->mbcinfo->mbctype, sizeof(MSVCRT_mbctype));
365 return 0;
368 /*********************************************************************
369 * _setmbcp (MSVCRT.@)
371 int CDECL _setmbcp(int cp)
373 return _setmbcp_l(cp, -1, NULL);
376 /*********************************************************************
377 * _getmbcp (MSVCRT.@)
379 int CDECL _getmbcp(void)
381 return get_mbcinfo()->mbcodepage;
384 /*********************************************************************
385 * _mbsnextc(MSVCRT.@)
387 unsigned int CDECL _mbsnextc(const unsigned char* str)
389 if(_ismbblead(*str))
390 return *str << 8 | str[1];
391 return *str;
394 /*********************************************************************
395 * _mbctolower(MSVCRT.@)
397 unsigned int CDECL _mbctolower(unsigned int c)
399 if (_ismbblead(c))
401 FIXME("Handle MBC chars\n");
402 return c;
404 return tolower(c); /* ASCII CP or SB char */
407 /*********************************************************************
408 * _mbctoupper(MSVCRT.@)
410 unsigned int CDECL _mbctoupper(unsigned int c)
412 if (_ismbblead(c))
414 FIXME("Handle MBC chars\n");
415 return c;
417 return toupper(c); /* ASCII CP or SB char */
420 /*********************************************************************
421 * _mbctombb (MSVCRT.@)
423 unsigned int CDECL _mbctombb(unsigned int c)
425 unsigned int value;
427 if(get_mbcinfo()->mbcodepage == 932)
429 if(c >= 0x829f && c <= 0x82f1) /* Hiragana */
430 return mbctombb_932_kana[c - 0x829f];
431 if(c >= 0x8340 && c <= 0x8396 && c != 0x837f) /* Katakana */
432 return mbctombb_932_kana[c - 0x8340 - (c >= 0x837f ? 1 : 0)];
433 if(c >= 0x8140 && c <= 0x8197) /* Punctuation */
435 value = mbctombb_932_punct[c - 0x8140];
436 return value ? value : c;
438 if((c >= 0x824f && c <= 0x8258) || /* Fullwidth digits */
439 (c >= 0x8260 && c <= 0x8279)) /* Fullwidth capitals letters */
440 return c - 0x821f;
441 if(c >= 0x8281 && c <= 0x829a) /* Fullwidth small letters */
442 return c - 0x8220;
443 /* all other cases return c */
445 return c;
448 /*********************************************************************
449 * _mbcjistojms(MSVCRT.@)
451 * Converts a jis character to sjis.
452 * Based on description from
453 * http://www.slayers.ne.jp/~oouchi/code/jistosjis.html
455 unsigned int CDECL _mbcjistojms(unsigned int c)
457 /* Conversion takes place only when codepage is 932.
458 In all other cases, c is returned unchanged */
459 if(get_mbcinfo()->mbcodepage == 932)
461 if(HIBYTE(c) >= 0x21 && HIBYTE(c) <= 0x7e &&
462 LOBYTE(c) >= 0x21 && LOBYTE(c) <= 0x7e)
464 if(HIBYTE(c) % 2)
465 c += 0x1f;
466 else
467 c += 0x7d;
469 if(LOBYTE(c) >= 0x7F)
470 c += 0x1;
472 c = (((HIBYTE(c) - 0x21)/2 + 0x81) << 8) | LOBYTE(c);
474 if(HIBYTE(c) > 0x9f)
475 c += 0x4000;
477 else
478 return 0; /* Codepage is 932, but c can't be converted */
481 return c;
484 /*********************************************************************
485 * _mbcjmstojis(MSVCRT.@)
487 * Converts a sjis character to jis.
489 unsigned int CDECL _mbcjmstojis(unsigned int c)
491 /* Conversion takes place only when codepage is 932.
492 In all other cases, c is returned unchanged */
493 if(get_mbcinfo()->mbcodepage == 932)
495 if(_ismbclegal(c) && HIBYTE(c) < 0xf0)
497 if(HIBYTE(c) >= 0xe0)
498 c -= 0x4000;
500 c = (((HIBYTE(c) - 0x81)*2 + 0x21) << 8) | LOBYTE(c);
502 if(LOBYTE(c) > 0x7f)
503 c -= 0x1;
505 if(LOBYTE(c) > 0x9d)
506 c += 0x83;
507 else
508 c -= 0x1f;
510 else
511 return 0; /* Codepage is 932, but c can't be converted */
514 return c;
517 /*********************************************************************
518 * _mbsdec(MSVCRT.@)
520 unsigned char* CDECL _mbsdec(const unsigned char* start, const unsigned char* cur)
522 if(get_mbcinfo()->ismbcodepage)
523 return (unsigned char *)(_ismbstrail(start,cur-1) ? cur - 2 : cur -1);
525 return (unsigned char *)cur - 1; /* ASCII CP or SB char */
528 /*********************************************************************
529 * _mbclen(MSVCRT.@)
531 unsigned int CDECL _mbclen(const unsigned char* str)
533 return _ismbblead(*str) ? 2 : 1;
536 /*********************************************************************
537 * _mbsinc(MSVCRT.@)
539 unsigned char* CDECL _mbsinc(const unsigned char* str)
541 return (unsigned char *)(str + _mbclen(str));
544 /*********************************************************************
545 * _mbsninc(MSVCRT.@)
547 unsigned char* CDECL _mbsninc(const unsigned char* str, MSVCRT_size_t num)
549 if(!str)
550 return NULL;
552 while (num > 0 && *str)
554 if (_ismbblead(*str))
556 if (!*(str+1))
557 break;
558 str++;
560 str++;
561 num--;
564 return (unsigned char*)str;
567 /*********************************************************************
568 * _mbslen(MSVCRT.@)
570 MSVCRT_size_t CDECL _mbslen(const unsigned char* str)
572 MSVCRT_size_t len = 0;
573 while(*str)
575 if (_ismbblead(*str))
577 str++;
578 if (!*str) /* count only full chars */
579 break;
581 str++;
582 len++;
584 return len;
587 /*********************************************************************
588 * _mbccpy(MSVCRT.@)
590 void CDECL _mbccpy(unsigned char* dest, const unsigned char* src)
592 *dest = *src;
593 if(_ismbblead(*src))
594 *++dest = *++src; /* MB char */
597 /*********************************************************************
598 * _mbsncpy(MSVCRT.@)
599 * REMARKS
600 * The parameter n is the number or characters to copy, not the size of
601 * the buffer. Use _mbsnbcpy for a function analogical to strncpy
603 unsigned char* CDECL _mbsncpy(unsigned char* dst, const unsigned char* src, MSVCRT_size_t n)
605 unsigned char* ret = dst;
606 if(!n)
607 return dst;
608 if (get_mbcinfo()->ismbcodepage)
610 while (*src && n)
612 n--;
613 if (_ismbblead(*src))
615 if (!*(src+1))
617 *dst++ = 0;
618 *dst++ = 0;
619 break;
622 *dst++ = *src++;
625 *dst++ = *src++;
628 else
630 while (n)
632 n--;
633 if (!(*dst++ = *src++)) break;
636 while (n--) *dst++ = 0;
637 return ret;
640 /*********************************************************************
641 * _mbsnbcpy_s(MSVCRT.@)
642 * REMARKS
643 * Unlike _mbsnbcpy this function does not pad the rest of the dest
644 * string with 0
646 int CDECL _mbsnbcpy_s(unsigned char* dst, MSVCRT_size_t size, const unsigned char* src, MSVCRT_size_t n)
648 MSVCRT_size_t pos = 0;
650 if(!dst || size == 0)
651 return MSVCRT_EINVAL;
652 if(!src)
654 dst[0] = '\0';
655 return MSVCRT_EINVAL;
657 if(!n)
658 return 0;
660 if(get_mbcinfo()->ismbcodepage)
662 int is_lead = 0;
663 while (*src && n)
665 if(pos == size)
667 dst[0] = '\0';
668 return MSVCRT_ERANGE;
670 is_lead = (!is_lead && _ismbblead(*src));
671 n--;
672 dst[pos++] = *src++;
675 if (is_lead) /* if string ends with a lead, remove it */
676 dst[pos - 1] = 0;
678 else
680 while (n)
682 n--;
683 if(pos == size)
685 dst[0] = '\0';
686 return MSVCRT_ERANGE;
689 if(!(*src)) break;
690 dst[pos++] = *src++;
694 if(pos < size)
695 dst[pos] = '\0';
696 else
698 dst[0] = '\0';
699 return MSVCRT_ERANGE;
702 return 0;
705 /*********************************************************************
706 * _mbsnbcpy(MSVCRT.@)
707 * REMARKS
708 * Like strncpy this function doesn't enforce the string to be
709 * NUL-terminated
711 unsigned char* CDECL _mbsnbcpy(unsigned char* dst, const unsigned char* src, MSVCRT_size_t n)
713 unsigned char* ret = dst;
714 if(!n)
715 return dst;
716 if(get_mbcinfo()->ismbcodepage)
718 int is_lead = 0;
719 while (*src && n)
721 is_lead = (!is_lead && _ismbblead(*src));
722 n--;
723 *dst++ = *src++;
726 if (is_lead) /* if string ends with a lead, remove it */
727 *(dst - 1) = 0;
729 else
731 while (n)
733 n--;
734 if (!(*dst++ = *src++)) break;
737 while (n--) *dst++ = 0;
738 return ret;
741 /*********************************************************************
742 * _mbscmp(MSVCRT.@)
744 int CDECL _mbscmp(const unsigned char* str, const unsigned char* cmp)
746 if(get_mbcinfo()->ismbcodepage)
748 unsigned int strc, cmpc;
749 do {
750 if(!*str)
751 return *cmp ? -1 : 0;
752 if(!*cmp)
753 return 1;
754 strc = _mbsnextc(str);
755 cmpc = _mbsnextc(cmp);
756 if(strc != cmpc)
757 return strc < cmpc ? -1 : 1;
758 str +=(strc > 255) ? 2 : 1;
759 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
760 } while(1);
762 return u_strcmp(str, cmp); /* ASCII CP */
765 /*********************************************************************
766 * _mbsnbicoll_l(MSVCRT.@)
768 int CDECL _mbsnbicoll_l(const unsigned char *str1, const unsigned char *str2, MSVCRT_size_t len, MSVCRT__locale_t locale)
770 MSVCRT_pthreadmbcinfo mbcinfo;
772 if(!locale)
773 mbcinfo = get_mbcinfo();
774 else
775 mbcinfo = locale->mbcinfo;
777 if(!mbcinfo->ismbcodepage)
778 return MSVCRT__strnicoll_l((const char*)str1, (const char*)str2, len, locale);
779 return CompareStringA(mbcinfo->mblcid, NORM_IGNORECASE, (const char*)str1, len, (const char*)str2, len)-CSTR_EQUAL;
782 /*********************************************************************
783 * _mbsicoll_l(MSVCRT.@)
785 int CDECL _mbsicoll_l(const unsigned char *str1, const unsigned char *str2, MSVCRT__locale_t locale)
787 return _mbsnbicoll_l(str1, str2, -1, locale);
790 /*********************************************************************
791 * _mbsnbicoll(MSVCRT.@)
793 int CDECL _mbsnbicoll(const unsigned char *str1, const unsigned char *str2, MSVCRT_size_t len)
795 return _mbsnbicoll_l(str1, str2, len, NULL);
798 /*********************************************************************
799 * _mbsicoll(MSVCRT.@)
801 int CDECL _mbsicoll(const unsigned char* str, const unsigned char* cmp)
803 return _mbsnbicoll_l(str, cmp, -1, NULL);
806 /*********************************************************************
807 * _mbsnbcoll_l(MSVCRT.@)
809 int CDECL _mbsnbcoll_l(const unsigned char *str1, const unsigned char *str2, MSVCRT_size_t len, MSVCRT__locale_t locale)
811 MSVCRT_pthreadmbcinfo mbcinfo;
813 if(!locale)
814 mbcinfo = get_mbcinfo();
815 else
816 mbcinfo = locale->mbcinfo;
818 if(!mbcinfo->ismbcodepage)
819 return MSVCRT__strncoll_l((const char*)str1, (const char*)str2, len, locale);
820 return CompareStringA(mbcinfo->mblcid, 0, (const char*)str1, len, (const char*)str2, len)-CSTR_EQUAL;
823 /*********************************************************************
824 * _mbscoll_l(MSVCRT.@)
826 int CDECL _mbscoll_l(const unsigned char *str1, const unsigned char *str2, MSVCRT__locale_t locale)
828 return _mbsnbcoll_l(str1, str2, -1, locale);
831 /*********************************************************************
832 * _mbsnbcoll(MSVCRT.@)
834 int CDECL _mbsnbcoll(const unsigned char *str1, const unsigned char *str2, MSVCRT_size_t len)
836 return _mbsnbcoll_l(str1, str2, len, NULL);
839 /*********************************************************************
840 * _mbscoll(MSVCRT.@)
842 int CDECL _mbscoll(const unsigned char* str, const unsigned char* cmp)
844 return _mbsnbcoll_l(str, cmp, -1, NULL);
847 /*********************************************************************
848 * _mbsicmp(MSVCRT.@)
850 int CDECL _mbsicmp(const unsigned char* str, const unsigned char* cmp)
852 if(get_mbcinfo()->ismbcodepage)
854 unsigned int strc, cmpc;
855 do {
856 if(!*str)
857 return *cmp ? -1 : 0;
858 if(!*cmp)
859 return 1;
860 strc = _mbctolower(_mbsnextc(str));
861 cmpc = _mbctolower(_mbsnextc(cmp));
862 if(strc != cmpc)
863 return strc < cmpc ? -1 : 1;
864 str +=(strc > 255) ? 2 : 1;
865 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
866 } while(1);
868 return u_strcasecmp(str, cmp); /* ASCII CP */
871 /*********************************************************************
872 * _mbsncmp(MSVCRT.@)
874 int CDECL _mbsncmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
876 if(!len)
877 return 0;
879 if(get_mbcinfo()->ismbcodepage)
881 unsigned int strc, cmpc;
882 while(len--)
884 int inc;
885 if(!*str)
886 return *cmp ? -1 : 0;
887 if(!*cmp)
888 return 1;
889 strc = _mbsnextc(str);
890 cmpc = _mbsnextc(cmp);
891 if(strc != cmpc)
892 return strc < cmpc ? -1 : 1;
893 inc=(strc > 255) ? 2 : 1; /* Equal, use same increment */
894 str += inc;
895 cmp += inc;
897 return 0; /* Matched len chars */
899 return u_strncmp(str, cmp, len); /* ASCII CP */
902 /*********************************************************************
903 * _mbsnbcmp(MSVCRT.@)
905 int CDECL _mbsnbcmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
907 if (!len)
908 return 0;
909 if(get_mbcinfo()->ismbcodepage)
911 unsigned int strc, cmpc;
912 while (len)
914 int clen;
915 if(!*str)
916 return *cmp ? -1 : 0;
917 if(!*cmp)
918 return 1;
919 if (_ismbblead(*str))
921 strc=(len>=2)?_mbsnextc(str):0;
922 clen=2;
924 else
926 strc=*str;
927 clen=1;
929 if (_ismbblead(*cmp))
930 cmpc=(len>=2)?_mbsnextc(cmp):0;
931 else
932 cmpc=*cmp;
933 if(strc != cmpc)
934 return strc < cmpc ? -1 : 1;
935 len -= clen;
936 str += clen;
937 cmp += clen;
939 return 0; /* Matched len chars */
941 return u_strncmp(str,cmp,len);
944 /*********************************************************************
945 * _mbsnicmp(MSVCRT.@)
947 * Compare two multibyte strings case insensitively to 'len' characters.
949 int CDECL _mbsnicmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
951 /* FIXME: No tolower() for mb strings yet */
952 if(get_mbcinfo()->ismbcodepage)
954 unsigned int strc, cmpc;
955 while(len--)
957 if(!*str)
958 return *cmp ? -1 : 0;
959 if(!*cmp)
960 return 1;
961 strc = _mbctolower(_mbsnextc(str));
962 cmpc = _mbctolower(_mbsnextc(cmp));
963 if(strc != cmpc)
964 return strc < cmpc ? -1 : 1;
965 str +=(strc > 255) ? 2 : 1;
966 cmp +=(strc > 255) ? 2 : 1; /* Equal, use same increment */
968 return 0; /* Matched len chars */
970 return u_strncasecmp(str, cmp, len); /* ASCII CP */
973 /*********************************************************************
974 * _mbsnbicmp(MSVCRT.@)
976 int CDECL _mbsnbicmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
978 if (!len)
979 return 0;
980 if(get_mbcinfo()->ismbcodepage)
982 unsigned int strc, cmpc;
983 while (len)
985 int clen;
986 if(!*str)
987 return *cmp ? -1 : 0;
988 if(!*cmp)
989 return 1;
990 if (_ismbblead(*str))
992 strc=(len>=2)?_mbsnextc(str):0;
993 clen=2;
995 else
997 strc=*str;
998 clen=1;
1000 if (_ismbblead(*cmp))
1001 cmpc=(len>=2)?_mbsnextc(cmp):0;
1002 else
1003 cmpc=*cmp;
1004 strc = _mbctolower(strc);
1005 cmpc = _mbctolower(cmpc);
1006 if(strc != cmpc)
1007 return strc < cmpc ? -1 : 1;
1008 len -= clen;
1009 str += clen;
1010 cmp += clen;
1012 return 0; /* Matched len bytes */
1014 return u_strncasecmp(str,cmp,len);
1017 /*********************************************************************
1018 * _mbscat (MSVCRT.@)
1020 unsigned char * CDECL _mbscat( unsigned char *dst, const unsigned char *src )
1022 strcat( (char *)dst, (const char *)src );
1023 return dst;
1026 /*********************************************************************
1027 * _mbscpy (MSVCRT.@)
1029 unsigned char* CDECL _mbscpy( unsigned char *dst, const unsigned char *src )
1031 strcpy( (char *)dst, (const char *)src );
1032 return dst;
1035 /*********************************************************************
1036 * _mbsstr (MSVCRT.@)
1038 unsigned char * CDECL _mbsstr(const unsigned char *haystack, const unsigned char *needle)
1040 return (unsigned char *)strstr( (const char *)haystack, (const char *)needle );
1043 /*********************************************************************
1044 * _mbschr(MSVCRT.@)
1046 * Find a multibyte character in a multibyte string.
1048 unsigned char* CDECL _mbschr(const unsigned char* s, unsigned int x)
1050 if(get_mbcinfo()->ismbcodepage)
1052 unsigned int c;
1053 while (1)
1055 c = _mbsnextc(s);
1056 if (c == x)
1057 return (unsigned char*)s;
1058 if (!c)
1059 return NULL;
1060 s += c > 255 ? 2 : 1;
1063 return u_strchr(s, x); /* ASCII CP */
1066 /*********************************************************************
1067 * _mbsrchr(MSVCRT.@)
1069 unsigned char* CDECL _mbsrchr(const unsigned char* s, unsigned int x)
1071 if(get_mbcinfo()->ismbcodepage)
1073 unsigned int c;
1074 unsigned char* match=NULL;
1075 if(!s)
1076 return NULL;
1077 while (1) {
1078 c = _mbsnextc(s);
1079 if (c == x)
1080 match=(unsigned char*)s;
1081 if (!c)
1082 return match;
1083 s +=(c > 255) ? 2 : 1;
1086 return u_strrchr(s, x);
1089 /*********************************************************************
1090 * _mbstok_s_l(MSVCRT.@)
1092 unsigned char* CDECL _mbstok_s_l(unsigned char *str, const unsigned char *delim,
1093 unsigned char **ctx, MSVCRT__locale_t locale)
1095 MSVCRT_pthreadmbcinfo mbcinfo;
1096 unsigned int c;
1098 if(!MSVCRT_CHECK_PMT(delim != NULL)) return NULL;
1099 if(!MSVCRT_CHECK_PMT(ctx != NULL)) return NULL;
1100 if(!MSVCRT_CHECK_PMT(str || *ctx)) return NULL;
1102 if(locale)
1103 mbcinfo = locale->mbcinfo;
1104 else
1105 mbcinfo = get_mbcinfo();
1107 if(!mbcinfo->ismbcodepage)
1108 return (unsigned char*)MSVCRT_strtok_s((char*)str, (const char*)delim, (char**)ctx);
1110 if(!str)
1111 str = *ctx;
1113 while((c=_mbsnextc(str)) && _mbschr(delim, c))
1114 str += c>255 ? 2 : 1;
1115 if(!*str)
1116 return NULL;
1118 *ctx = str + (c>255 ? 2 : 1);
1119 while((c=_mbsnextc(*ctx)) && !_mbschr(delim, c))
1120 *ctx += c>255 ? 2 : 1;
1121 if (**ctx) {
1122 *(*ctx)++ = 0;
1123 if(c > 255)
1124 *(*ctx)++ = 0;
1127 return str;
1131 /*********************************************************************
1132 * _mbstok_s(MSVCRT.@)
1134 unsigned char* CDECL _mbstok_s(unsigned char *str,
1135 const unsigned char *delim, unsigned char **ctx)
1137 return _mbstok_s_l(str, delim, ctx, NULL);
1140 /*********************************************************************
1141 * _mbstok_l(MSVCRT.@)
1143 unsigned char* CDECL _mbstok_l(unsigned char *str,
1144 const unsigned char *delim, MSVCRT__locale_t locale)
1146 return _mbstok_s_l(str, delim, &msvcrt_get_thread_data()->mbstok_next, locale);
1149 /*********************************************************************
1150 * _mbstok(MSVCRT.@)
1152 unsigned char* CDECL _mbstok(unsigned char *str, const unsigned char *delim)
1154 thread_data_t *data = msvcrt_get_thread_data();
1156 #if _MSVCR_VER == 0
1157 if(!str && !data->mbstok_next)
1158 return NULL;
1159 #endif
1161 return _mbstok_s_l(str, delim, &data->mbstok_next, NULL);
1164 /*********************************************************************
1165 * _mbbtombc(MSVCRT.@)
1167 unsigned int CDECL _mbbtombc(unsigned int c)
1169 if(get_mbcinfo()->mbcodepage == 932)
1171 if(c >= 0x20 && c <= 0x7e) {
1172 if((c >= 0x41 && c <= 0x5a) || (c >= 0x61 && c <= 0x7a) || (c >= 0x30 && c <= 0x39))
1173 return mbbtombc_932[c - 0x20] | 0x8200;
1174 else
1175 return mbbtombc_932[c - 0x20] | 0x8100;
1177 else if(c >= 0xa1 && c <= 0xdf) {
1178 if(c >= 0xa6 && c <= 0xdd && c != 0xb0)
1179 return mbbtombc_932[c - 0xa1 + 0x5f] | 0x8300;
1180 else
1181 return mbbtombc_932[c - 0xa1 + 0x5f] | 0x8100;
1184 return c; /* not Japanese or no MB char */
1187 /*********************************************************************
1188 * _mbbtype(MSVCRT.@)
1190 int CDECL _mbbtype(unsigned char c, int type)
1192 if (type == 1)
1194 if ((c >= 0x20 && c <= 0x7e) || (c >= 0xa1 && c <= 0xdf))
1195 return _MBC_SINGLE;
1196 else if ((c >= 0x40 && c <= 0x7e) || (c >= 0x80 && c <= 0xfc))
1197 return _MBC_TRAIL;
1198 else
1199 return _MBC_ILLEGAL;
1201 else
1203 if ((c >= 0x20 && c <= 0x7e) || (c >= 0xa1 && c <= 0xdf))
1204 return _MBC_SINGLE;
1205 else if ((c >= 0x81 && c <= 0x9f) || (c >= 0xe0 && c <= 0xfc))
1206 return _MBC_LEAD;
1207 else
1208 return _MBC_ILLEGAL;
1212 /*********************************************************************
1213 * _ismbbkana(MSVCRT.@)
1215 int CDECL _ismbbkana(unsigned int c)
1217 /* FIXME: use lc_ctype when supported, not lc_all */
1218 if(get_mbcinfo()->mbcodepage == 932)
1220 /* Japanese/Katakana, CP 932 */
1221 return (c >= 0xa1 && c <= 0xdf);
1223 return 0;
1226 /*********************************************************************
1227 * _ismbcdigit(MSVCRT.@)
1229 int CDECL _ismbcdigit(unsigned int ch)
1231 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1232 return (get_char_typeW( wch ) & C1_DIGIT);
1235 /*********************************************************************
1236 * _ismbcgraph(MSVCRT.@)
1238 int CDECL _ismbcgraph(unsigned int ch)
1240 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1241 return (get_char_typeW( wch ) & (C1_UPPER | C1_LOWER | C1_DIGIT | C1_PUNCT | C1_ALPHA));
1244 /*********************************************************************
1245 * _ismbcalpha (MSVCRT.@)
1247 int CDECL _ismbcalpha(unsigned int ch)
1249 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1250 return (get_char_typeW( wch ) & C1_ALPHA);
1253 /*********************************************************************
1254 * _ismbclower (MSVCRT.@)
1256 int CDECL _ismbclower(unsigned int ch)
1258 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1259 return (get_char_typeW( wch ) & C1_UPPER);
1262 /*********************************************************************
1263 * _ismbcupper (MSVCRT.@)
1265 int CDECL _ismbcupper(unsigned int ch)
1267 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1268 return (get_char_typeW( wch ) & C1_LOWER);
1271 /*********************************************************************
1272 * _ismbcsymbol(MSVCRT.@)
1274 int CDECL _ismbcsymbol(unsigned int ch)
1276 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1277 WORD ctype;
1278 if (!GetStringTypeW(CT_CTYPE3, &wch, 1, &ctype))
1280 WARN("GetStringTypeW failed on %x\n", ch);
1281 return 0;
1283 return ((ctype & C3_SYMBOL) != 0);
1286 /*********************************************************************
1287 * _ismbcalnum (MSVCRT.@)
1289 int CDECL _ismbcalnum(unsigned int ch)
1291 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1292 return (get_char_typeW( wch ) & (C1_ALPHA | C1_DIGIT));
1295 /*********************************************************************
1296 * _ismbcspace (MSVCRT.@)
1298 int CDECL _ismbcspace(unsigned int ch)
1300 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1301 return (get_char_typeW( wch ) & C1_SPACE);
1304 /*********************************************************************
1305 * _ismbcprint (MSVCRT.@)
1307 int CDECL _ismbcprint(unsigned int ch)
1309 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1310 return (get_char_typeW( wch ) & (C1_UPPER | C1_LOWER | C1_DIGIT | C1_PUNCT | C1_ALPHA | C1_SPACE));
1313 /*********************************************************************
1314 * _ismbcpunct(MSVCRT.@)
1316 int CDECL _ismbcpunct(unsigned int ch)
1318 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1319 return (get_char_typeW( wch ) & C1_PUNCT);
1322 /*********************************************************************
1323 * _ismbchira(MSVCRT.@)
1325 int CDECL _ismbchira(unsigned int c)
1327 /* FIXME: use lc_ctype when supported, not lc_all */
1328 if(get_mbcinfo()->mbcodepage == 932)
1330 /* Japanese/Hiragana, CP 932 */
1331 return (c >= 0x829f && c <= 0x82f1);
1333 return 0;
1336 /*********************************************************************
1337 * _ismbckata(MSVCRT.@)
1339 int CDECL _ismbckata(unsigned int c)
1341 /* FIXME: use lc_ctype when supported, not lc_all */
1342 if(get_mbcinfo()->mbcodepage == 932)
1344 if(c < 256)
1345 return _ismbbkana(c);
1346 /* Japanese/Katakana, CP 932 */
1347 return (c >= 0x8340 && c <= 0x8396 && c != 0x837f);
1349 return 0;
1352 /*********************************************************************
1353 * _ismbblead(MSVCRT.@)
1355 int CDECL _ismbblead(unsigned int c)
1357 return (get_mbcinfo()->mbctype[(c&0xff) + 1] & _M1) != 0;
1361 /*********************************************************************
1362 * _ismbbtrail(MSVCRT.@)
1364 int CDECL _ismbbtrail(unsigned int c)
1366 return (get_mbcinfo()->mbctype[(c&0xff) + 1] & _M2) != 0;
1369 /*********************************************************************
1370 * _ismbclegal(MSVCRT.@)
1372 int CDECL _ismbclegal(unsigned int c)
1374 return _ismbblead(HIBYTE(c)) && _ismbbtrail(LOBYTE(c));
1377 /*********************************************************************
1378 * _ismbslead(MSVCRT.@)
1380 int CDECL _ismbslead(const unsigned char* start, const unsigned char* str)
1382 int lead = 0;
1384 if(!get_mbcinfo()->ismbcodepage)
1385 return 0;
1387 /* Lead bytes can also be trail bytes so we need to analyse the string
1389 while (start <= str)
1391 if (!*start)
1392 return 0;
1393 lead = !lead && _ismbblead(*start);
1394 start++;
1397 return lead ? -1 : 0;
1400 /*********************************************************************
1401 * _ismbstrail(MSVCRT.@)
1403 int CDECL _ismbstrail(const unsigned char* start, const unsigned char* str)
1405 /* Note: this function doesn't check _ismbbtrail */
1406 if ((str > start) && _ismbslead(start, str-1))
1407 return -1;
1408 else
1409 return 0;
1412 /*********************************************************************
1413 * _mbsbtype (MSVCRT.@)
1415 int CDECL _mbsbtype(const unsigned char *str, MSVCRT_size_t count)
1417 int lead = 0;
1418 const unsigned char *end = str + count;
1420 /* Lead bytes can also be trail bytes so we need to analyse the string.
1421 * Also we must return _MBC_ILLEGAL for chars past the end of the string
1423 while (str < end) /* Note: we skip the last byte - will check after the loop */
1425 if (!*str)
1426 return _MBC_ILLEGAL;
1427 lead = get_mbcinfo()->ismbcodepage && !lead && _ismbblead(*str);
1428 str++;
1431 if (lead)
1432 if (_ismbbtrail(*str))
1433 return _MBC_TRAIL;
1434 else
1435 return _MBC_ILLEGAL;
1436 else
1437 if (_ismbblead(*str))
1438 return _MBC_LEAD;
1439 else
1440 return _MBC_SINGLE;
1443 /*********************************************************************
1444 * _mbsset(MSVCRT.@)
1446 unsigned char* CDECL _mbsset(unsigned char* str, unsigned int c)
1448 unsigned char* ret = str;
1450 if(!get_mbcinfo()->ismbcodepage || c < 256)
1451 return u__strset(str, c); /* ASCII CP or SB char */
1453 c &= 0xffff; /* Strip high bits */
1455 while(str[0] && str[1])
1457 *str++ = c >> 8;
1458 *str++ = c & 0xff;
1460 if(str[0])
1461 str[0] = '\0'; /* FIXME: OK to shorten? */
1463 return ret;
1466 /*********************************************************************
1467 * _mbsnbset(MSVCRT.@)
1469 unsigned char* CDECL _mbsnbset(unsigned char *str, unsigned int c, MSVCRT_size_t len)
1471 unsigned char *ret = str;
1473 if(!len)
1474 return ret;
1476 if(!get_mbcinfo()->ismbcodepage || c < 256)
1477 return u__strnset(str, c, len); /* ASCII CP or SB char */
1479 c &= 0xffff; /* Strip high bits */
1481 while(str[0] && str[1] && (len > 1))
1483 *str++ = c >> 8;
1484 len--;
1485 *str++ = c & 0xff;
1486 len--;
1488 if(len && str[0]) {
1489 /* as per msdn pad with a blank character */
1490 str[0] = ' ';
1493 return ret;
1496 /*********************************************************************
1497 * _mbsnset(MSVCRT.@)
1499 unsigned char* CDECL _mbsnset(unsigned char* str, unsigned int c, MSVCRT_size_t len)
1501 unsigned char *ret = str;
1503 if(!len)
1504 return ret;
1506 if(!get_mbcinfo()->ismbcodepage || c < 256)
1507 return u__strnset(str, c, len); /* ASCII CP or SB char */
1509 c &= 0xffff; /* Strip high bits */
1511 while(str[0] && str[1] && len--)
1513 *str++ = c >> 8;
1514 *str++ = c & 0xff;
1516 if(len && str[0])
1517 str[0] = '\0'; /* FIXME: OK to shorten? */
1519 return ret;
1522 /*********************************************************************
1523 * _mbsnccnt(MSVCRT.@)
1524 * 'c' is for 'character'.
1526 MSVCRT_size_t CDECL _mbsnccnt(const unsigned char* str, MSVCRT_size_t len)
1528 MSVCRT_size_t ret;
1529 if(get_mbcinfo()->ismbcodepage)
1531 ret=0;
1532 while(*str && len-- > 0)
1534 if(_ismbblead(*str))
1536 if (!len)
1537 break;
1538 len--;
1539 str++;
1541 str++;
1542 ret++;
1544 return ret;
1546 ret=u_strlen(str);
1547 return min(ret, len); /* ASCII CP */
1550 /*********************************************************************
1551 * _mbsnbcnt(MSVCRT.@)
1552 * 'b' is for byte count.
1554 MSVCRT_size_t CDECL _mbsnbcnt(const unsigned char* str, MSVCRT_size_t len)
1556 MSVCRT_size_t ret;
1557 if(get_mbcinfo()->ismbcodepage)
1559 const unsigned char* xstr = str;
1560 while(*xstr && len-- > 0)
1562 if (_ismbblead(*xstr++))
1563 xstr++;
1565 return xstr-str;
1567 ret=u_strlen(str);
1568 return min(ret, len); /* ASCII CP */
1571 /*********************************************************************
1572 * _mbsnbcat(MSVCRT.@)
1574 unsigned char* CDECL _mbsnbcat(unsigned char* dst, const unsigned char* src, MSVCRT_size_t len)
1576 if(get_mbcinfo()->ismbcodepage)
1578 unsigned char *res = dst;
1579 while (*dst) {
1580 if (_ismbblead(*dst++)) {
1581 if (*dst) {
1582 dst++;
1583 } else {
1584 /* as per msdn overwrite the lead byte in front of '\0' */
1585 dst--;
1586 break;
1590 while (*src && len--) *dst++ = *src++;
1591 *dst = '\0';
1592 return res;
1594 return u_strncat(dst, src, len); /* ASCII CP */
1597 int CDECL _mbsnbcat_s(unsigned char *dst, MSVCRT_size_t size, const unsigned char *src, MSVCRT_size_t len)
1599 unsigned char *ptr = dst;
1600 MSVCRT_size_t i;
1602 if (!dst && !size && !src && !len)
1603 return 0;
1605 if (!dst || !size || !src)
1607 if (dst && size)
1608 *dst = '\0';
1610 *MSVCRT__errno() = MSVCRT_EINVAL;
1611 return MSVCRT_EINVAL;
1614 /* Find the null terminator of the destination buffer. */
1615 while (size && *ptr)
1616 size--, ptr++;
1618 if (!size)
1620 *dst = '\0';
1621 *MSVCRT__errno() = MSVCRT_EINVAL;
1622 return MSVCRT_EINVAL;
1625 /* If necessary, check that the character preceding the null terminator is
1626 * a lead byte and move the pointer back by one for later overwrite. */
1627 if (ptr != dst && get_mbcinfo()->ismbcodepage && _ismbblead(*(ptr - 1)))
1628 size++, ptr--;
1630 for (i = 0; *src && i < len; i++)
1632 *ptr++ = *src++;
1633 size--;
1635 if (!size)
1637 *dst = '\0';
1638 *MSVCRT__errno() = MSVCRT_ERANGE;
1639 return MSVCRT_ERANGE;
1643 *ptr = '\0';
1644 return 0;
1647 /*********************************************************************
1648 * _mbsncat(MSVCRT.@)
1650 unsigned char* CDECL _mbsncat(unsigned char* dst, const unsigned char* src, MSVCRT_size_t len)
1652 if(get_mbcinfo()->ismbcodepage)
1654 unsigned char *res = dst;
1655 while (*dst)
1657 if (_ismbblead(*dst++))
1658 dst++;
1660 while (*src && len--)
1662 *dst++ = *src;
1663 if(_ismbblead(*src++))
1664 *dst++ = *src++;
1666 *dst = '\0';
1667 return res;
1669 return u_strncat(dst, src, len); /* ASCII CP */
1673 /*********************************************************************
1674 * _mbslwr(MSVCRT.@)
1676 unsigned char* CDECL _mbslwr(unsigned char* s)
1678 unsigned char *ret = s;
1679 if (!s)
1680 return NULL;
1681 if (get_mbcinfo()->ismbcodepage)
1683 unsigned int c;
1684 while (*s)
1686 c = _mbctolower(_mbsnextc(s));
1687 /* Note that I assume that the size of the character is unchanged */
1688 if (c > 255)
1690 *s++=(c>>8);
1691 c=c & 0xff;
1693 *s++=c;
1696 else for ( ; *s; s++) *s = tolower(*s);
1697 return ret;
1701 /*********************************************************************
1702 * _mbslwr_s(MSVCRT.@)
1704 int CDECL _mbslwr_s(unsigned char* s, MSVCRT_size_t len)
1706 if (!s && !len)
1708 return 0;
1710 else if (!s || !len)
1712 *MSVCRT__errno() = MSVCRT_EINVAL;
1713 return MSVCRT_EINVAL;
1715 if (get_mbcinfo()->ismbcodepage)
1717 unsigned int c;
1718 for ( ; *s && len > 0; len--)
1720 c = _mbctolower(_mbsnextc(s));
1721 /* Note that I assume that the size of the character is unchanged */
1722 if (c > 255)
1724 *s++=(c>>8);
1725 c=c & 0xff;
1727 *s++=c;
1730 else for ( ; *s && len > 0; s++, len--) *s = tolower(*s);
1731 if (*s)
1733 *s = '\0';
1734 *MSVCRT__errno() = MSVCRT_EINVAL;
1735 return MSVCRT_EINVAL;
1737 return 0;
1741 /*********************************************************************
1742 * _mbsupr(MSVCRT.@)
1744 unsigned char* CDECL _mbsupr(unsigned char* s)
1746 unsigned char *ret = s;
1747 if (!s)
1748 return NULL;
1749 if (get_mbcinfo()->ismbcodepage)
1751 unsigned int c;
1752 while (*s)
1754 c = _mbctoupper(_mbsnextc(s));
1755 /* Note that I assume that the size of the character is unchanged */
1756 if (c > 255)
1758 *s++=(c>>8);
1759 c=c & 0xff;
1761 *s++=c;
1764 else for ( ; *s; s++) *s = toupper(*s);
1765 return ret;
1769 /*********************************************************************
1770 * _mbsupr_s(MSVCRT.@)
1772 int CDECL _mbsupr_s(unsigned char* s, MSVCRT_size_t len)
1774 if (!s && !len)
1776 return 0;
1778 else if (!s || !len)
1780 *MSVCRT__errno() = MSVCRT_EINVAL;
1781 return MSVCRT_EINVAL;
1783 if (get_mbcinfo()->ismbcodepage)
1785 unsigned int c;
1786 for ( ; *s && len > 0; len--)
1788 c = _mbctoupper(_mbsnextc(s));
1789 /* Note that I assume that the size of the character is unchanged */
1790 if (c > 255)
1792 *s++=(c>>8);
1793 c=c & 0xff;
1795 *s++=c;
1798 else for ( ; *s && len > 0; s++, len--) *s = toupper(*s);
1799 if (*s)
1801 *s = '\0';
1802 *MSVCRT__errno() = MSVCRT_EINVAL;
1803 return MSVCRT_EINVAL;
1805 return 0;
1809 /*********************************************************************
1810 * _mbsspn (MSVCRT.@)
1812 MSVCRT_size_t CDECL _mbsspn(const unsigned char* string, const unsigned char* set)
1814 const unsigned char *p, *q;
1816 for (p = string; *p; p++)
1818 if (_ismbblead(*p))
1820 for (q = set; *q; q++)
1822 if (!q[1])
1823 break;
1824 if ((*p == *q) && (p[1] == q[1]))
1825 break;
1826 q++;
1828 if (!q[0] || !q[1]) break;
1830 else
1832 for (q = set; *q; q++)
1833 if (*p == *q)
1834 break;
1835 if (!*q) break;
1838 return p - string;
1841 /*********************************************************************
1842 * _mbsspnp (MSVCRT.@)
1844 unsigned char* CDECL _mbsspnp(const unsigned char* string, const unsigned char* set)
1846 const unsigned char *p, *q;
1848 for (p = string; *p; p++)
1850 if (_ismbblead(*p))
1852 for (q = set; *q; q++)
1854 if (!q[1])
1855 break;
1856 if ((*p == *q) && (p[1] == q[1]))
1857 break;
1858 q++;
1860 if (!q[0] || !q[1]) break;
1862 else
1864 for (q = set; *q; q++)
1865 if (*p == *q)
1866 break;
1867 if (!*q) break;
1870 if (*p == '\0')
1871 return NULL;
1872 return (unsigned char *)p;
1875 /*********************************************************************
1876 * _mbscspn(MSVCRT.@)
1878 MSVCRT_size_t CDECL _mbscspn(const unsigned char* str, const unsigned char* cmp)
1880 if (get_mbcinfo()->ismbcodepage)
1881 FIXME("don't handle double character case\n");
1882 return u_strcspn(str, cmp);
1885 /*********************************************************************
1886 * _mbsrev (MSVCRT.@)
1888 unsigned char* CDECL _mbsrev(unsigned char* str)
1890 int i, len = _mbslen(str);
1891 unsigned char *p, *temp=MSVCRT_malloc(len*2);
1893 if(!temp)
1894 return str;
1896 /* unpack multibyte string to temp buffer */
1897 p=str;
1898 for(i=0; i<len; i++)
1900 if (_ismbblead(*p))
1902 temp[i*2]=*p++;
1903 temp[i*2+1]=*p++;
1905 else
1907 temp[i*2]=*p++;
1908 temp[i*2+1]=0;
1912 /* repack it in the reverse order */
1913 p=str;
1914 for(i=len-1; i>=0; i--)
1916 if(_ismbblead(temp[i*2]))
1918 *p++=temp[i*2];
1919 *p++=temp[i*2+1];
1921 else
1923 *p++=temp[i*2];
1927 MSVCRT_free(temp);
1929 return str;
1932 /*********************************************************************
1933 * _mbspbrk (MSVCRT.@)
1935 unsigned char* CDECL _mbspbrk(const unsigned char* str, const unsigned char* accept)
1937 const unsigned char* p;
1939 while(*str)
1941 for(p = accept; *p; p += (_ismbblead(*p)?2:1) )
1943 if (*p == *str)
1944 if( !_ismbblead(*p) || ( *(p+1) == *(str+1) ) )
1945 return (unsigned char*)str;
1947 str += (_ismbblead(*str)?2:1);
1949 return NULL;
1954 * Functions depending on locale codepage
1957 /*********************************************************************
1958 * mblen(MSVCRT.@)
1959 * REMARKS
1960 * Unlike most of the multibyte string functions this function uses
1961 * the locale codepage, not the codepage set by _setmbcp
1963 int CDECL MSVCRT_mblen(const char* str, MSVCRT_size_t size)
1965 if (str && *str && size)
1967 if(get_locinfo()->mb_cur_max == 1)
1968 return 1; /* ASCII CP */
1970 return !MSVCRT_isleadbyte((unsigned char)*str) ? 1 : (size>1 ? 2 : -1);
1972 return 0;
1975 /*********************************************************************
1976 * mbrlen(MSVCRT.@)
1978 MSVCRT_size_t CDECL MSVCRT_mbrlen(const char *str, MSVCRT_size_t len, MSVCRT_mbstate_t *state)
1980 MSVCRT_mbstate_t s = (state ? *state : 0);
1981 MSVCRT_size_t ret;
1983 if(!len || !str || !*str)
1984 return 0;
1986 if(get_locinfo()->mb_cur_max == 1) {
1987 return 1;
1988 }else if(!s && MSVCRT_isleadbyte((unsigned char)*str)) {
1989 if(len == 1) {
1990 s = (unsigned char)*str;
1991 ret = -2;
1992 }else {
1993 ret = 2;
1995 }else if(!s) {
1996 ret = 1;
1997 }else {
1998 s = 0;
1999 ret = 2;
2002 if(state)
2003 *state = s;
2004 return ret;
2007 /*********************************************************************
2008 * _mbstrlen_l(MSVCRT.@)
2010 MSVCRT_size_t CDECL _mbstrlen_l(const char* str, MSVCRT__locale_t locale)
2012 MSVCRT_pthreadlocinfo locinfo;
2014 if(!locale)
2015 locinfo = get_locinfo();
2016 else
2017 locinfo = locale->locinfo;
2019 if(locinfo->mb_cur_max > 1) {
2020 MSVCRT_size_t len;
2021 len = MultiByteToWideChar(locinfo->lc_codepage, MB_ERR_INVALID_CHARS,
2022 str, -1, NULL, 0);
2023 if (!len) {
2024 *MSVCRT__errno() = MSVCRT_EILSEQ;
2025 return -1;
2027 return len - 1;
2030 return strlen(str);
2033 /*********************************************************************
2034 * _mbstrlen(MSVCRT.@)
2036 MSVCRT_size_t CDECL _mbstrlen(const char* str)
2038 return _mbstrlen_l(str, NULL);
2041 /*********************************************************************
2042 * _mbtowc_l(MSVCRT.@)
2044 int CDECL MSVCRT_mbtowc_l(MSVCRT_wchar_t *dst, const char* str, MSVCRT_size_t n, MSVCRT__locale_t locale)
2046 MSVCRT_pthreadlocinfo locinfo;
2047 MSVCRT_wchar_t tmpdst = '\0';
2049 if(!locale)
2050 locinfo = get_locinfo();
2051 else
2052 locinfo = locale->locinfo;
2054 if(n <= 0 || !str)
2055 return 0;
2056 if(!locinfo->lc_codepage)
2057 tmpdst = (unsigned char)*str;
2058 else if(!MultiByteToWideChar(locinfo->lc_codepage, 0, str, n, &tmpdst, 1))
2059 return -1;
2060 if(dst)
2061 *dst = tmpdst;
2062 /* return the number of bytes from src that have been used */
2063 if(!*str)
2064 return 0;
2065 if(n >= 2 && MSVCRT__isleadbyte_l((unsigned char)*str, locale) && str[1])
2066 return 2;
2067 return 1;
2070 /*********************************************************************
2071 * mbtowc(MSVCRT.@)
2073 int CDECL MSVCRT_mbtowc(MSVCRT_wchar_t *dst, const char* str, MSVCRT_size_t n)
2075 return MSVCRT_mbtowc_l(dst, str, n, NULL);
2078 /*********************************************************************
2079 * mbrtowc(MSVCRT.@)
2081 MSVCRT_size_t CDECL MSVCRT_mbrtowc(MSVCRT_wchar_t *dst, const char *str,
2082 MSVCRT_size_t n, MSVCRT_mbstate_t *state)
2084 MSVCRT_pthreadlocinfo locinfo = get_locinfo();
2085 MSVCRT_mbstate_t s = (state ? *state : 0);
2086 char tmpstr[2];
2087 int len = 0;
2089 if(dst)
2090 *dst = 0;
2092 if(!n || !str || !*str)
2093 return 0;
2095 if(locinfo->mb_cur_max == 1) {
2096 tmpstr[len++] = *str;
2097 }else if(!s && MSVCRT_isleadbyte((unsigned char)*str)) {
2098 if(n == 1) {
2099 s = (unsigned char)*str;
2100 len = -2;
2101 }else {
2102 tmpstr[0] = str[0];
2103 tmpstr[1] = str[1];
2104 len = 2;
2106 }else if(!s) {
2107 tmpstr[len++] = *str;
2108 }else {
2109 tmpstr[0] = s;
2110 tmpstr[1] = *str;
2111 len = 2;
2112 s = 0;
2115 if(len > 0) {
2116 if(!MultiByteToWideChar(locinfo->lc_codepage, 0, tmpstr, len, dst, dst ? 1 : 0))
2117 len = -1;
2120 if(state)
2121 *state = s;
2122 return len;
2125 /*********************************************************************
2126 * _mbstowcs_l(MSVCRT.@)
2128 MSVCRT_size_t CDECL MSVCRT__mbstowcs_l(MSVCRT_wchar_t *wcstr, const char *mbstr,
2129 MSVCRT_size_t count, MSVCRT__locale_t locale)
2131 MSVCRT_pthreadlocinfo locinfo;
2132 MSVCRT_size_t i, size;
2134 if(!mbstr) {
2135 *MSVCRT__errno() = MSVCRT_EINVAL;
2136 return -1;
2139 if(!locale)
2140 locinfo = get_locinfo();
2141 else
2142 locinfo = locale->locinfo;
2144 /* Ignore count parameter */
2145 if(!wcstr)
2146 return MultiByteToWideChar(locinfo->lc_codepage, 0, mbstr, -1, NULL, 0)-1;
2148 for(i=0, size=0; i<count; i++) {
2149 if(mbstr[size] == '\0')
2150 break;
2152 size += (MSVCRT__isleadbyte_l((unsigned char)mbstr[size], locale) ? 2 : 1);
2155 size = MultiByteToWideChar(locinfo->lc_codepage, 0,
2156 mbstr, size, wcstr, count);
2158 if(size<count && wcstr)
2159 wcstr[size] = '\0';
2161 return size;
2164 /*********************************************************************
2165 * mbstowcs(MSVCRT.@)
2167 MSVCRT_size_t CDECL MSVCRT_mbstowcs(MSVCRT_wchar_t *wcstr,
2168 const char *mbstr, MSVCRT_size_t count)
2170 return MSVCRT__mbstowcs_l(wcstr, mbstr, count, NULL);
2173 /*********************************************************************
2174 * _mbstowcs_s_l(MSVCRT.@)
2176 int CDECL MSVCRT__mbstowcs_s_l(MSVCRT_size_t *ret, MSVCRT_wchar_t *wcstr,
2177 MSVCRT_size_t size, const char *mbstr,
2178 MSVCRT_size_t count, MSVCRT__locale_t locale)
2180 MSVCRT_size_t conv;
2182 if(!wcstr && !size) {
2183 conv = MSVCRT__mbstowcs_l(NULL, mbstr, 0, locale);
2184 if(ret)
2185 *ret = conv+1;
2186 return 0;
2189 if (!MSVCRT_CHECK_PMT(wcstr != NULL)) return MSVCRT_EINVAL;
2190 if (!MSVCRT_CHECK_PMT(mbstr != NULL)) {
2191 if(size) wcstr[0] = '\0';
2192 return MSVCRT_EINVAL;
2195 if(count==MSVCRT__TRUNCATE || size<count)
2196 conv = size;
2197 else
2198 conv = count;
2200 conv = MSVCRT__mbstowcs_l(wcstr, mbstr, conv, locale);
2201 if(conv<size)
2202 wcstr[conv++] = '\0';
2203 else if(conv==size && (count==MSVCRT__TRUNCATE || wcstr[conv-1]=='\0'))
2204 wcstr[conv-1] = '\0';
2205 else {
2206 MSVCRT_INVALID_PMT("wcstr[size] is too small", MSVCRT_ERANGE);
2207 if(size)
2208 wcstr[0] = '\0';
2209 return MSVCRT_ERANGE;
2212 if(ret)
2213 *ret = conv;
2214 return 0;
2217 /*********************************************************************
2218 * mbstowcs_s(MSVCRT.@)
2220 int CDECL MSVCRT__mbstowcs_s(MSVCRT_size_t *ret, MSVCRT_wchar_t *wcstr,
2221 MSVCRT_size_t size, const char *mbstr, MSVCRT_size_t count)
2223 return MSVCRT__mbstowcs_s_l(ret, wcstr, size, mbstr, count, NULL);
2226 /*********************************************************************
2227 * mbsrtowcs(MSVCRT.@)
2229 MSVCRT_size_t CDECL MSVCRT_mbsrtowcs(MSVCRT_wchar_t *wcstr,
2230 const char **pmbstr, MSVCRT_size_t count, MSVCRT_mbstate_t *state)
2232 MSVCRT_mbstate_t s = (state ? *state : 0);
2233 MSVCRT_wchar_t tmpdst;
2234 MSVCRT_size_t ret = 0;
2236 if(!MSVCRT_CHECK_PMT(pmbstr != NULL))
2237 return -1;
2239 while(!wcstr || count>ret) {
2240 int ch_len = MSVCRT_mbrtowc(&tmpdst, *pmbstr, 2, &s);
2241 if(wcstr)
2242 wcstr[ret] = tmpdst;
2244 if(ch_len < 0) {
2245 return -1;
2246 }else if(ch_len == 0) {
2247 *pmbstr = NULL;
2248 return ret;
2251 *pmbstr += ch_len;
2252 ret++;
2255 return ret;