dplayx: Add a trailing '\n' to a TRACE().
[wine/multimedia.git] / dlls / msvcrt / mbcs.c
blobfdc5422c64ac1eab569f368265cd46fd60ed0d1e
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_strtok( unsigned char *s, const unsigned char *delim )
150 return (unsigned char*) strtok( (char*)s, (const char*)delim );
153 static inline unsigned char *u__strset( unsigned char *s, unsigned char c )
155 return (unsigned char*) _strset( (char*)s, c);
158 static inline unsigned char *u__strnset( unsigned char *s, unsigned char c, MSVCRT_size_t len )
160 return (unsigned char*) MSVCRT__strnset( (char*)s, c, len );
163 static inline MSVCRT_size_t u_strcspn( const unsigned char *s, const unsigned char *rej )
165 return strcspn( (const char *)s, (const char*)rej );
168 /*********************************************************************
169 * __p__mbctype (MSVCRT.@)
171 unsigned char* CDECL __p__mbctype(void)
173 return get_mbcinfo()->mbctype;
176 /*********************************************************************
177 * __p___mb_cur_max(MSVCRT.@)
179 int* CDECL __p___mb_cur_max(void)
181 return &get_locinfo()->mb_cur_max;
184 /*********************************************************************
185 * ___mb_cur_max_func(MSVCRT.@)
187 int CDECL MSVCRT____mb_cur_max_func(void)
189 return get_locinfo()->mb_cur_max;
192 /* ___mb_cur_max_l_func - not exported in native msvcrt */
193 int* CDECL ___mb_cur_max_l_func(MSVCRT__locale_t locale)
195 MSVCRT_pthreadlocinfo locinfo;
197 if(!locale)
198 locinfo = get_locinfo();
199 else
200 locinfo = locale->locinfo;
202 return &locinfo->mb_cur_max;
205 /*********************************************************************
206 * INTERNAL: _setmbcp_l
208 int _setmbcp_l(int cp, LCID lcid, MSVCRT_pthreadmbcinfo mbcinfo)
210 const char format[] = ".%d";
212 int newcp;
213 CPINFO cpi;
214 BYTE *bytes;
215 WORD chartypes[256];
216 char bufA[256];
217 WCHAR bufW[256];
218 int charcount;
219 int ret;
220 int i;
222 if(!mbcinfo)
223 mbcinfo = get_mbcinfo();
225 switch (cp)
227 case _MB_CP_ANSI:
228 newcp = GetACP();
229 break;
230 case _MB_CP_OEM:
231 newcp = GetOEMCP();
232 break;
233 case _MB_CP_LOCALE:
234 newcp = get_locinfo()->lc_codepage;
235 if(newcp)
236 break;
237 /* fall through (C locale) */
238 case _MB_CP_SBCS:
239 newcp = 20127; /* ASCII */
240 break;
241 default:
242 newcp = cp;
243 break;
246 if(lcid == -1) {
247 sprintf(bufA, format, newcp);
248 mbcinfo->mblcid = MSVCRT_locale_to_LCID(bufA, NULL);
249 } else {
250 mbcinfo->mblcid = lcid;
253 if(mbcinfo->mblcid == -1)
255 WARN("Can't assign LCID to codepage (%d)\n", mbcinfo->mblcid);
256 mbcinfo->mblcid = 0;
259 if (!GetCPInfo(newcp, &cpi))
261 WARN("Codepage %d not found\n", newcp);
262 *MSVCRT__errno() = MSVCRT_EINVAL;
263 return -1;
266 /* setup the _mbctype */
267 memset(mbcinfo->mbctype, 0, sizeof(unsigned char[257]));
268 memset(mbcinfo->mbcasemap, 0, sizeof(unsigned char[256]));
270 bytes = cpi.LeadByte;
271 while (bytes[0] || bytes[1])
273 for (i = bytes[0]; i <= bytes[1]; i++)
274 mbcinfo->mbctype[i + 1] |= _M1;
275 bytes += 2;
278 if (cpi.MaxCharSize > 1)
280 /* trail bytes not available through kernel32 but stored in a structure in msvcrt */
281 struct cp_extra_info_t *cpextra = g_cpextrainfo;
283 mbcinfo->ismbcodepage = 1;
284 while (TRUE)
286 if (cpextra->cp == 0 || cpextra->cp == newcp)
288 if (cpextra->cp == 0)
289 FIXME("trail bytes data not available for DBCS codepage %d - assuming all bytes\n", newcp);
291 bytes = cpextra->TrailBytes;
292 while (bytes[0] || bytes[1])
294 for (i = bytes[0]; i <= bytes[1]; i++)
295 mbcinfo->mbctype[i + 1] |= _M2;
296 bytes += 2;
298 break;
300 cpextra++;
303 else
304 mbcinfo->ismbcodepage = 0;
306 /* we can't use GetStringTypeA directly because we don't have a locale - only a code page
308 charcount = 0;
309 for (i = 0; i < 256; i++)
310 if (!(mbcinfo->mbctype[i + 1] & _M1))
311 bufA[charcount++] = i;
313 ret = MultiByteToWideChar(newcp, 0, bufA, charcount, bufW, charcount);
314 if (ret != charcount)
315 ERR("MultiByteToWideChar of chars failed for cp %d, ret=%d (exp %d), error=%d\n", newcp, ret, charcount, GetLastError());
317 GetStringTypeW(CT_CTYPE1, bufW, charcount, chartypes);
319 charcount = 0;
320 for (i = 0; i < 256; i++)
321 if (!(mbcinfo->mbctype[i + 1] & _M1))
323 if (chartypes[charcount] & C1_UPPER)
325 mbcinfo->mbctype[i + 1] |= _SBUP;
326 bufW[charcount] = tolowerW(bufW[charcount]);
328 else if (chartypes[charcount] & C1_LOWER)
330 mbcinfo->mbctype[i + 1] |= _SBLOW;
331 bufW[charcount] = toupperW(bufW[charcount]);
333 charcount++;
336 ret = WideCharToMultiByte(newcp, 0, bufW, charcount, bufA, charcount, NULL, NULL);
337 if (ret != charcount)
338 ERR("WideCharToMultiByte failed for cp %d, ret=%d (exp %d), error=%d\n", newcp, ret, charcount, GetLastError());
340 charcount = 0;
341 for (i = 0; i < 256; i++)
343 if(!(mbcinfo->mbctype[i + 1] & _M1))
345 if(mbcinfo->mbctype[i] & (C1_UPPER|C1_LOWER))
346 mbcinfo->mbcasemap[i] = bufA[charcount];
347 charcount++;
351 if (newcp == 932) /* CP932 only - set _MP and _MS */
353 /* On Windows it's possible to calculate the _MP and _MS from CT_CTYPE1
354 * and CT_CTYPE3. But as of Wine 0.9.43 we return wrong values what makes
355 * it hard. As this is set only for codepage 932 we hardcode it what gives
356 * also faster execution.
358 for (i = 161; i <= 165; i++)
359 mbcinfo->mbctype[i + 1] |= _MP;
360 for (i = 166; i <= 223; i++)
361 mbcinfo->mbctype[i + 1] |= _MS;
364 mbcinfo->mbcodepage = newcp;
365 if(MSVCRT_locale && mbcinfo == MSVCRT_locale->mbcinfo)
366 memcpy(MSVCRT_mbctype, MSVCRT_locale->mbcinfo->mbctype, sizeof(MSVCRT_mbctype));
368 return 0;
371 /*********************************************************************
372 * _setmbcp (MSVCRT.@)
374 int CDECL _setmbcp(int cp)
376 return _setmbcp_l(cp, -1, NULL);
379 /*********************************************************************
380 * _getmbcp (MSVCRT.@)
382 int CDECL _getmbcp(void)
384 return get_mbcinfo()->mbcodepage;
387 /*********************************************************************
388 * _mbsnextc(MSVCRT.@)
390 unsigned int CDECL _mbsnextc(const unsigned char* str)
392 if(_ismbblead(*str))
393 return *str << 8 | str[1];
394 return *str;
397 /*********************************************************************
398 * _mbctolower(MSVCRT.@)
400 unsigned int CDECL _mbctolower(unsigned int c)
402 if (_ismbblead(c))
404 FIXME("Handle MBC chars\n");
405 return c;
407 return tolower(c); /* ASCII CP or SB char */
410 /*********************************************************************
411 * _mbctoupper(MSVCRT.@)
413 unsigned int CDECL _mbctoupper(unsigned int c)
415 if (_ismbblead(c))
417 FIXME("Handle MBC chars\n");
418 return c;
420 return toupper(c); /* ASCII CP or SB char */
423 /*********************************************************************
424 * _mbctombb (MSVCRT.@)
426 unsigned int CDECL _mbctombb(unsigned int c)
428 unsigned int value;
430 if(get_mbcinfo()->mbcodepage == 932)
432 if(c >= 0x829f && c <= 0x82f1) /* Hiragana */
433 return mbctombb_932_kana[c - 0x829f];
434 if(c >= 0x8340 && c <= 0x8396 && c != 0x837f) /* Katakana */
435 return mbctombb_932_kana[c - 0x8340 - (c >= 0x837f ? 1 : 0)];
436 if(c >= 0x8140 && c <= 0x8197) /* Punctuation */
438 value = mbctombb_932_punct[c - 0x8140];
439 return value ? value : c;
441 if((c >= 0x824f && c <= 0x8258) || /* Fullwidth digits */
442 (c >= 0x8260 && c <= 0x8279)) /* Fullwidth capitals letters */
443 return c - 0x821f;
444 if(c >= 0x8281 && c <= 0x829a) /* Fullwidth small letters */
445 return c - 0x8220;
446 /* all other cases return c */
448 return c;
451 /*********************************************************************
452 * _mbcjistojms(MSVCRT.@)
454 * Converts a jis character to sjis.
455 * Based on description from
456 * http://www.slayers.ne.jp/~oouchi/code/jistosjis.html
458 unsigned int CDECL _mbcjistojms(unsigned int c)
460 /* Conversion takes place only when codepage is 932.
461 In all other cases, c is returned unchanged */
462 if(get_mbcinfo()->mbcodepage == 932)
464 if(HIBYTE(c) >= 0x21 && HIBYTE(c) <= 0x7e &&
465 LOBYTE(c) >= 0x21 && LOBYTE(c) <= 0x7e)
467 if(HIBYTE(c) % 2)
468 c += 0x1f;
469 else
470 c += 0x7d;
472 if(LOBYTE(c) >= 0x7F)
473 c += 0x1;
475 c = (((HIBYTE(c) - 0x21)/2 + 0x81) << 8) | LOBYTE(c);
477 if(HIBYTE(c) > 0x9f)
478 c += 0x4000;
480 else
481 return 0; /* Codepage is 932, but c can't be converted */
484 return c;
487 /*********************************************************************
488 * _mbcjmstojis(MSVCRT.@)
490 * Converts a sjis character to jis.
492 unsigned int CDECL _mbcjmstojis(unsigned int c)
494 /* Conversion takes place only when codepage is 932.
495 In all other cases, c is returned unchanged */
496 if(get_mbcinfo()->mbcodepage == 932)
498 if(_ismbclegal(c) && HIBYTE(c) < 0xf0)
500 if(HIBYTE(c) >= 0xe0)
501 c -= 0x4000;
503 c = (((HIBYTE(c) - 0x81)*2 + 0x21) << 8) | LOBYTE(c);
505 if(LOBYTE(c) > 0x7f)
506 c -= 0x1;
508 if(LOBYTE(c) > 0x9d)
509 c += 0x83;
510 else
511 c -= 0x1f;
513 else
514 return 0; /* Codepage is 932, but c can't be converted */
517 return c;
520 /*********************************************************************
521 * _mbsdec(MSVCRT.@)
523 unsigned char* CDECL _mbsdec(const unsigned char* start, const unsigned char* cur)
525 if(get_mbcinfo()->ismbcodepage)
526 return (unsigned char *)(_ismbstrail(start,cur-1) ? cur - 2 : cur -1);
528 return (unsigned char *)cur - 1; /* ASCII CP or SB char */
531 /*********************************************************************
532 * _mbclen(MSVCRT.@)
534 unsigned int CDECL _mbclen(const unsigned char* str)
536 return _ismbblead(*str) ? 2 : 1;
539 /*********************************************************************
540 * _mbsinc(MSVCRT.@)
542 unsigned char* CDECL _mbsinc(const unsigned char* str)
544 return (unsigned char *)(str + _mbclen(str));
547 /*********************************************************************
548 * _mbsninc(MSVCRT.@)
550 unsigned char* CDECL _mbsninc(const unsigned char* str, MSVCRT_size_t num)
552 if(!str)
553 return NULL;
555 while (num > 0 && *str)
557 if (_ismbblead(*str))
559 if (!*(str+1))
560 break;
561 str++;
563 str++;
564 num--;
567 return (unsigned char*)str;
570 /*********************************************************************
571 * _mbslen(MSVCRT.@)
573 MSVCRT_size_t CDECL _mbslen(const unsigned char* str)
575 MSVCRT_size_t len = 0;
576 while(*str)
578 if (_ismbblead(*str))
580 str++;
581 if (!*str) /* count only full chars */
582 break;
584 str++;
585 len++;
587 return len;
590 /*********************************************************************
591 * _mbccpy(MSVCRT.@)
593 void CDECL _mbccpy(unsigned char* dest, const unsigned char* src)
595 *dest = *src;
596 if(_ismbblead(*src))
597 *++dest = *++src; /* MB char */
600 /*********************************************************************
601 * _mbsncpy(MSVCRT.@)
602 * REMARKS
603 * The parameter n is the number or characters to copy, not the size of
604 * the buffer. Use _mbsnbcpy for a function analogical to strncpy
606 unsigned char* CDECL _mbsncpy(unsigned char* dst, const unsigned char* src, MSVCRT_size_t n)
608 unsigned char* ret = dst;
609 if(!n)
610 return dst;
611 if (get_mbcinfo()->ismbcodepage)
613 while (*src && n)
615 n--;
616 if (_ismbblead(*src))
618 if (!*(src+1))
620 *dst++ = 0;
621 *dst++ = 0;
622 break;
625 *dst++ = *src++;
628 *dst++ = *src++;
631 else
633 while (n)
635 n--;
636 if (!(*dst++ = *src++)) break;
639 while (n--) *dst++ = 0;
640 return ret;
643 /*********************************************************************
644 * _mbsnbcpy_s(MSVCRT.@)
645 * REMARKS
646 * Unlike _mbsnbcpy this function does not pad the rest of the dest
647 * string with 0
649 int CDECL _mbsnbcpy_s(unsigned char* dst, MSVCRT_size_t size, const unsigned char* src, MSVCRT_size_t n)
651 MSVCRT_size_t pos = 0;
653 if(!dst || size == 0)
654 return MSVCRT_EINVAL;
655 if(!src)
657 dst[0] = '\0';
658 return MSVCRT_EINVAL;
660 if(!n)
661 return 0;
663 if(get_mbcinfo()->ismbcodepage)
665 int is_lead = 0;
666 while (*src && n)
668 if(pos == size)
670 dst[0] = '\0';
671 return MSVCRT_ERANGE;
673 is_lead = (!is_lead && _ismbblead(*src));
674 n--;
675 dst[pos++] = *src++;
678 if (is_lead) /* if string ends with a lead, remove it */
679 dst[pos - 1] = 0;
681 else
683 while (n)
685 n--;
686 if(pos == size)
688 dst[0] = '\0';
689 return MSVCRT_ERANGE;
692 if(!(*src)) break;
693 dst[pos++] = *src++;
697 if(pos < size)
698 dst[pos] = '\0';
699 else
701 dst[0] = '\0';
702 return MSVCRT_ERANGE;
705 return 0;
708 /*********************************************************************
709 * _mbsnbcpy(MSVCRT.@)
710 * REMARKS
711 * Like strncpy this function doesn't enforce the string to be
712 * NUL-terminated
714 unsigned char* CDECL _mbsnbcpy(unsigned char* dst, const unsigned char* src, MSVCRT_size_t n)
716 unsigned char* ret = dst;
717 if(!n)
718 return dst;
719 if(get_mbcinfo()->ismbcodepage)
721 int is_lead = 0;
722 while (*src && n)
724 is_lead = (!is_lead && _ismbblead(*src));
725 n--;
726 *dst++ = *src++;
729 if (is_lead) /* if string ends with a lead, remove it */
730 *(dst - 1) = 0;
732 else
734 while (n)
736 n--;
737 if (!(*dst++ = *src++)) break;
740 while (n--) *dst++ = 0;
741 return ret;
744 /*********************************************************************
745 * _mbscmp(MSVCRT.@)
747 int CDECL _mbscmp(const unsigned char* str, const unsigned char* cmp)
749 if(get_mbcinfo()->ismbcodepage)
751 unsigned int strc, cmpc;
752 do {
753 if(!*str)
754 return *cmp ? -1 : 0;
755 if(!*cmp)
756 return 1;
757 strc = _mbsnextc(str);
758 cmpc = _mbsnextc(cmp);
759 if(strc != cmpc)
760 return strc < cmpc ? -1 : 1;
761 str +=(strc > 255) ? 2 : 1;
762 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
763 } while(1);
765 return u_strcmp(str, cmp); /* ASCII CP */
768 /*********************************************************************
769 * _mbsnbicoll_l(MSVCRT.@)
771 int CDECL _mbsnbicoll_l(const unsigned char *str1, const unsigned char *str2, MSVCRT_size_t len, MSVCRT__locale_t locale)
773 MSVCRT_pthreadmbcinfo mbcinfo;
775 if(!locale)
776 mbcinfo = get_mbcinfo();
777 else
778 mbcinfo = locale->mbcinfo;
780 if(!mbcinfo->ismbcodepage)
781 return MSVCRT__strnicoll_l((const char*)str1, (const char*)str2, len, locale);
782 return CompareStringA(mbcinfo->mblcid, NORM_IGNORECASE, (const char*)str1, len, (const char*)str2, len)-CSTR_EQUAL;
785 /*********************************************************************
786 * _mbsicoll_l(MSVCRT.@)
788 int CDECL _mbsicoll_l(const unsigned char *str1, const unsigned char *str2, MSVCRT__locale_t locale)
790 return _mbsnbicoll_l(str1, str2, -1, locale);
793 /*********************************************************************
794 * _mbsnbicoll(MSVCRT.@)
796 int CDECL _mbsnbicoll(const unsigned char *str1, const unsigned char *str2, MSVCRT_size_t len)
798 return _mbsnbicoll_l(str1, str2, len, NULL);
801 /*********************************************************************
802 * _mbsicoll(MSVCRT.@)
804 int CDECL _mbsicoll(const unsigned char* str, const unsigned char* cmp)
806 return _mbsnbicoll_l(str, cmp, -1, NULL);
809 /*********************************************************************
810 * _mbsnbcoll_l(MSVCRT.@)
812 int CDECL _mbsnbcoll_l(const unsigned char *str1, const unsigned char *str2, MSVCRT_size_t len, MSVCRT__locale_t locale)
814 MSVCRT_pthreadmbcinfo mbcinfo;
816 if(!locale)
817 mbcinfo = get_mbcinfo();
818 else
819 mbcinfo = locale->mbcinfo;
821 if(!mbcinfo->ismbcodepage)
822 return MSVCRT_strncoll_l((const char*)str1, (const char*)str2, len, locale);
823 return CompareStringA(mbcinfo->mblcid, 0, (const char*)str1, len, (const char*)str2, len)-CSTR_EQUAL;
826 /*********************************************************************
827 * _mbscoll_l(MSVCRT.@)
829 int CDECL _mbscoll_l(const unsigned char *str1, const unsigned char *str2, MSVCRT__locale_t locale)
831 return _mbsnbcoll_l(str1, str2, -1, locale);
834 /*********************************************************************
835 * _mbsnbcoll(MSVCRT.@)
837 int CDECL _mbsnbcoll(const unsigned char *str1, const unsigned char *str2, MSVCRT_size_t len)
839 return _mbsnbcoll_l(str1, str2, len, NULL);
842 /*********************************************************************
843 * _mbscoll(MSVCRT.@)
845 int CDECL _mbscoll(const unsigned char* str, const unsigned char* cmp)
847 return _mbsnbcoll_l(str, cmp, -1, NULL);
850 /*********************************************************************
851 * _mbsicmp(MSVCRT.@)
853 int CDECL _mbsicmp(const unsigned char* str, const unsigned char* cmp)
855 if(get_mbcinfo()->ismbcodepage)
857 unsigned int strc, cmpc;
858 do {
859 if(!*str)
860 return *cmp ? -1 : 0;
861 if(!*cmp)
862 return 1;
863 strc = _mbctolower(_mbsnextc(str));
864 cmpc = _mbctolower(_mbsnextc(cmp));
865 if(strc != cmpc)
866 return strc < cmpc ? -1 : 1;
867 str +=(strc > 255) ? 2 : 1;
868 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
869 } while(1);
871 return u_strcasecmp(str, cmp); /* ASCII CP */
874 /*********************************************************************
875 * _mbsncmp(MSVCRT.@)
877 int CDECL _mbsncmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
879 if(!len)
880 return 0;
882 if(get_mbcinfo()->ismbcodepage)
884 unsigned int strc, cmpc;
885 while(len--)
887 int inc;
888 if(!*str)
889 return *cmp ? -1 : 0;
890 if(!*cmp)
891 return 1;
892 strc = _mbsnextc(str);
893 cmpc = _mbsnextc(cmp);
894 if(strc != cmpc)
895 return strc < cmpc ? -1 : 1;
896 inc=(strc > 255) ? 2 : 1; /* Equal, use same increment */
897 str += inc;
898 cmp += inc;
900 return 0; /* Matched len chars */
902 return u_strncmp(str, cmp, len); /* ASCII CP */
905 /*********************************************************************
906 * _mbsnbcmp(MSVCRT.@)
908 int CDECL _mbsnbcmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
910 if (!len)
911 return 0;
912 if(get_mbcinfo()->ismbcodepage)
914 unsigned int strc, cmpc;
915 while (len)
917 int clen;
918 if(!*str)
919 return *cmp ? -1 : 0;
920 if(!*cmp)
921 return 1;
922 if (_ismbblead(*str))
924 strc=(len>=2)?_mbsnextc(str):0;
925 clen=2;
927 else
929 strc=*str;
930 clen=1;
932 if (_ismbblead(*cmp))
933 cmpc=(len>=2)?_mbsnextc(cmp):0;
934 else
935 cmpc=*cmp;
936 if(strc != cmpc)
937 return strc < cmpc ? -1 : 1;
938 len -= clen;
939 str += clen;
940 cmp += clen;
942 return 0; /* Matched len chars */
944 return u_strncmp(str,cmp,len);
947 /*********************************************************************
948 * _mbsnicmp(MSVCRT.@)
950 * Compare two multibyte strings case insensitively to 'len' characters.
952 int CDECL _mbsnicmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
954 /* FIXME: No tolower() for mb strings yet */
955 if(get_mbcinfo()->ismbcodepage)
957 unsigned int strc, cmpc;
958 while(len--)
960 if(!*str)
961 return *cmp ? -1 : 0;
962 if(!*cmp)
963 return 1;
964 strc = _mbctolower(_mbsnextc(str));
965 cmpc = _mbctolower(_mbsnextc(cmp));
966 if(strc != cmpc)
967 return strc < cmpc ? -1 : 1;
968 str +=(strc > 255) ? 2 : 1;
969 cmp +=(strc > 255) ? 2 : 1; /* Equal, use same increment */
971 return 0; /* Matched len chars */
973 return u_strncasecmp(str, cmp, len); /* ASCII CP */
976 /*********************************************************************
977 * _mbsnbicmp(MSVCRT.@)
979 int CDECL _mbsnbicmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
981 if (!len)
982 return 0;
983 if(get_mbcinfo()->ismbcodepage)
985 unsigned int strc, cmpc;
986 while (len)
988 int clen;
989 if(!*str)
990 return *cmp ? -1 : 0;
991 if(!*cmp)
992 return 1;
993 if (_ismbblead(*str))
995 strc=(len>=2)?_mbsnextc(str):0;
996 clen=2;
998 else
1000 strc=*str;
1001 clen=1;
1003 if (_ismbblead(*cmp))
1004 cmpc=(len>=2)?_mbsnextc(cmp):0;
1005 else
1006 cmpc=*cmp;
1007 strc = _mbctolower(strc);
1008 cmpc = _mbctolower(cmpc);
1009 if(strc != cmpc)
1010 return strc < cmpc ? -1 : 1;
1011 len -= clen;
1012 str += clen;
1013 cmp += clen;
1015 return 0; /* Matched len bytes */
1017 return u_strncasecmp(str,cmp,len);
1020 /*********************************************************************
1021 * _mbscat (MSVCRT.@)
1023 unsigned char * CDECL _mbscat( unsigned char *dst, const unsigned char *src )
1025 strcat( (char *)dst, (const char *)src );
1026 return dst;
1029 /*********************************************************************
1030 * _mbscpy (MSVCRT.@)
1032 unsigned char* CDECL _mbscpy( unsigned char *dst, const unsigned char *src )
1034 strcpy( (char *)dst, (const char *)src );
1035 return dst;
1038 /*********************************************************************
1039 * _mbsstr (MSVCRT.@)
1041 unsigned char * CDECL _mbsstr(const unsigned char *haystack, const unsigned char *needle)
1043 return (unsigned char *)strstr( (const char *)haystack, (const char *)needle );
1046 /*********************************************************************
1047 * _mbschr(MSVCRT.@)
1049 * Find a multibyte character in a multibyte string.
1051 unsigned char* CDECL _mbschr(const unsigned char* s, unsigned int x)
1053 if(get_mbcinfo()->ismbcodepage)
1055 unsigned int c;
1056 while (1)
1058 c = _mbsnextc(s);
1059 if (c == x)
1060 return (unsigned char*)s;
1061 if (!c)
1062 return NULL;
1063 s += c > 255 ? 2 : 1;
1066 return u_strchr(s, x); /* ASCII CP */
1069 /*********************************************************************
1070 * _mbsrchr(MSVCRT.@)
1072 unsigned char* CDECL _mbsrchr(const unsigned char* s, unsigned int x)
1074 if(get_mbcinfo()->ismbcodepage)
1076 unsigned int c;
1077 unsigned char* match=NULL;
1078 if(!s)
1079 return NULL;
1080 while (1) {
1081 c = _mbsnextc(s);
1082 if (c == x)
1083 match=(unsigned char*)s;
1084 if (!c)
1085 return match;
1086 s +=(c > 255) ? 2 : 1;
1089 return u_strrchr(s, x);
1092 /*********************************************************************
1093 * _mbstok(MSVCRT.@)
1095 * Find and extract tokens from strings
1097 unsigned char* CDECL _mbstok(unsigned char *str, const unsigned char *delim)
1099 thread_data_t *data = msvcrt_get_thread_data();
1100 unsigned char *ret;
1102 if(get_mbcinfo()->ismbcodepage)
1104 unsigned int c;
1106 if (!str)
1107 if (!(str = data->mbstok_next)) return NULL;
1109 while ((c = _mbsnextc(str)) && _mbschr(delim, c)) {
1110 str += c > 255 ? 2 : 1;
1112 if (!*str) return NULL;
1113 ret = str++;
1114 while ((c = _mbsnextc(str)) && !_mbschr(delim, c)) {
1115 str += c > 255 ? 2 : 1;
1117 if (*str) {
1118 *str++ = 0;
1119 if (c > 255) *str++ = 0;
1121 data->mbstok_next = str;
1122 return ret;
1124 return u_strtok(str, delim); /* ASCII CP */
1127 /*********************************************************************
1128 * _mbbtombc(MSVCRT.@)
1130 unsigned int CDECL _mbbtombc(unsigned int c)
1132 if(get_mbcinfo()->mbcodepage == 932)
1134 if(c >= 0x20 && c <= 0x7e) {
1135 if((c >= 0x41 && c <= 0x5a) || (c >= 0x61 && c <= 0x7a) || (c >= 0x30 && c <= 0x39))
1136 return mbbtombc_932[c - 0x20] | 0x8200;
1137 else
1138 return mbbtombc_932[c - 0x20] | 0x8100;
1140 else if(c >= 0xa1 && c <= 0xdf) {
1141 if(c >= 0xa6 && c <= 0xdd && c != 0xb0)
1142 return mbbtombc_932[c - 0xa1 + 0x5f] | 0x8300;
1143 else
1144 return mbbtombc_932[c - 0xa1 + 0x5f] | 0x8100;
1147 return c; /* not Japanese or no MB char */
1150 /*********************************************************************
1151 * _mbbtype(MSVCRT.@)
1153 int CDECL _mbbtype(unsigned char c, int type)
1155 if (type == 1)
1157 if ((c >= 0x20 && c <= 0x7e) || (c >= 0xa1 && c <= 0xdf))
1158 return _MBC_SINGLE;
1159 else if ((c >= 0x40 && c <= 0x7e) || (c >= 0x80 && c <= 0xfc))
1160 return _MBC_TRAIL;
1161 else
1162 return _MBC_ILLEGAL;
1164 else
1166 if ((c >= 0x20 && c <= 0x7e) || (c >= 0xa1 && c <= 0xdf))
1167 return _MBC_SINGLE;
1168 else if ((c >= 0x81 && c <= 0x9f) || (c >= 0xe0 && c <= 0xfc))
1169 return _MBC_LEAD;
1170 else
1171 return _MBC_ILLEGAL;
1175 /*********************************************************************
1176 * _ismbbkana(MSVCRT.@)
1178 int CDECL _ismbbkana(unsigned int c)
1180 /* FIXME: use lc_ctype when supported, not lc_all */
1181 if(get_mbcinfo()->mbcodepage == 932)
1183 /* Japanese/Katakana, CP 932 */
1184 return (c >= 0xa1 && c <= 0xdf);
1186 return 0;
1189 /*********************************************************************
1190 * _ismbcdigit(MSVCRT.@)
1192 int CDECL _ismbcdigit(unsigned int ch)
1194 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1195 return (get_char_typeW( wch ) & C1_DIGIT);
1198 /*********************************************************************
1199 * _ismbcgraph(MSVCRT.@)
1201 int CDECL _ismbcgraph(unsigned int ch)
1203 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1204 return (get_char_typeW( wch ) & (C1_UPPER | C1_LOWER | C1_DIGIT | C1_PUNCT | C1_ALPHA));
1207 /*********************************************************************
1208 * _ismbcalpha (MSVCRT.@)
1210 int CDECL _ismbcalpha(unsigned int ch)
1212 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1213 return (get_char_typeW( wch ) & C1_ALPHA);
1216 /*********************************************************************
1217 * _ismbclower (MSVCRT.@)
1219 int CDECL _ismbclower(unsigned int ch)
1221 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1222 return (get_char_typeW( wch ) & C1_UPPER);
1225 /*********************************************************************
1226 * _ismbcupper (MSVCRT.@)
1228 int CDECL _ismbcupper(unsigned int ch)
1230 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1231 return (get_char_typeW( wch ) & C1_LOWER);
1234 /*********************************************************************
1235 * _ismbcsymbol(MSVCRT.@)
1237 int CDECL _ismbcsymbol(unsigned int ch)
1239 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1240 WORD ctype;
1241 if (!GetStringTypeW(CT_CTYPE3, &wch, 1, &ctype))
1243 WARN("GetStringTypeW failed on %x\n", ch);
1244 return 0;
1246 return ((ctype & C3_SYMBOL) != 0);
1249 /*********************************************************************
1250 * _ismbcalnum (MSVCRT.@)
1252 int CDECL _ismbcalnum(unsigned int ch)
1254 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1255 return (get_char_typeW( wch ) & (C1_ALPHA | C1_DIGIT));
1258 /*********************************************************************
1259 * _ismbcspace (MSVCRT.@)
1261 int CDECL _ismbcspace(unsigned int ch)
1263 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1264 return (get_char_typeW( wch ) & C1_SPACE);
1267 /*********************************************************************
1268 * _ismbcprint (MSVCRT.@)
1270 int CDECL _ismbcprint(unsigned int ch)
1272 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1273 return (get_char_typeW( wch ) & (C1_UPPER | C1_LOWER | C1_DIGIT | C1_PUNCT | C1_ALPHA | C1_SPACE));
1276 /*********************************************************************
1277 * _ismbcpunct(MSVCRT.@)
1279 int CDECL _ismbcpunct(unsigned int ch)
1281 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1282 return (get_char_typeW( wch ) & C1_PUNCT);
1285 /*********************************************************************
1286 * _ismbchira(MSVCRT.@)
1288 int CDECL _ismbchira(unsigned int c)
1290 /* FIXME: use lc_ctype when supported, not lc_all */
1291 if(get_mbcinfo()->mbcodepage == 932)
1293 /* Japanese/Hiragana, CP 932 */
1294 return (c >= 0x829f && c <= 0x82f1);
1296 return 0;
1299 /*********************************************************************
1300 * _ismbckata(MSVCRT.@)
1302 int CDECL _ismbckata(unsigned int c)
1304 /* FIXME: use lc_ctype when supported, not lc_all */
1305 if(get_mbcinfo()->mbcodepage == 932)
1307 if(c < 256)
1308 return _ismbbkana(c);
1309 /* Japanese/Katakana, CP 932 */
1310 return (c >= 0x8340 && c <= 0x8396 && c != 0x837f);
1312 return 0;
1315 /*********************************************************************
1316 * _ismbblead(MSVCRT.@)
1318 int CDECL _ismbblead(unsigned int c)
1320 return (get_mbcinfo()->mbctype[(c&0xff) + 1] & _M1) != 0;
1324 /*********************************************************************
1325 * _ismbbtrail(MSVCRT.@)
1327 int CDECL _ismbbtrail(unsigned int c)
1329 return (get_mbcinfo()->mbctype[(c&0xff) + 1] & _M2) != 0;
1332 /*********************************************************************
1333 * _ismbclegal(MSVCRT.@)
1335 int CDECL _ismbclegal(unsigned int c)
1337 return _ismbblead(HIBYTE(c)) && _ismbbtrail(LOBYTE(c));
1340 /*********************************************************************
1341 * _ismbslead(MSVCRT.@)
1343 int CDECL _ismbslead(const unsigned char* start, const unsigned char* str)
1345 int lead = 0;
1347 if(!get_mbcinfo()->ismbcodepage)
1348 return 0;
1350 /* Lead bytes can also be trail bytes so we need to analyse the string
1352 while (start <= str)
1354 if (!*start)
1355 return 0;
1356 lead = !lead && _ismbblead(*start);
1357 start++;
1360 return lead ? -1 : 0;
1363 /*********************************************************************
1364 * _ismbstrail(MSVCRT.@)
1366 int CDECL _ismbstrail(const unsigned char* start, const unsigned char* str)
1368 /* Note: this function doesn't check _ismbbtrail */
1369 if ((str > start) && _ismbslead(start, str-1))
1370 return -1;
1371 else
1372 return 0;
1375 /*********************************************************************
1376 * _mbsbtype (MSVCRT.@)
1378 int CDECL _mbsbtype(const unsigned char *str, MSVCRT_size_t count)
1380 int lead = 0;
1381 const unsigned char *end = str + count;
1383 /* Lead bytes can also be trail bytes so we need to analyse the string.
1384 * Also we must return _MBC_ILLEGAL for chars past the end of the string
1386 while (str < end) /* Note: we skip the last byte - will check after the loop */
1388 if (!*str)
1389 return _MBC_ILLEGAL;
1390 lead = get_mbcinfo()->ismbcodepage && !lead && _ismbblead(*str);
1391 str++;
1394 if (lead)
1395 if (_ismbbtrail(*str))
1396 return _MBC_TRAIL;
1397 else
1398 return _MBC_ILLEGAL;
1399 else
1400 if (_ismbblead(*str))
1401 return _MBC_LEAD;
1402 else
1403 return _MBC_SINGLE;
1406 /*********************************************************************
1407 * _mbsset(MSVCRT.@)
1409 unsigned char* CDECL _mbsset(unsigned char* str, unsigned int c)
1411 unsigned char* ret = str;
1413 if(!get_mbcinfo()->ismbcodepage || c < 256)
1414 return u__strset(str, c); /* ASCII CP or SB char */
1416 c &= 0xffff; /* Strip high bits */
1418 while(str[0] && str[1])
1420 *str++ = c >> 8;
1421 *str++ = c & 0xff;
1423 if(str[0])
1424 str[0] = '\0'; /* FIXME: OK to shorten? */
1426 return ret;
1429 /*********************************************************************
1430 * _mbsnbset(MSVCRT.@)
1432 unsigned char* CDECL _mbsnbset(unsigned char *str, unsigned int c, MSVCRT_size_t len)
1434 unsigned char *ret = str;
1436 if(!len)
1437 return ret;
1439 if(!get_mbcinfo()->ismbcodepage || c < 256)
1440 return u__strnset(str, c, len); /* ASCII CP or SB char */
1442 c &= 0xffff; /* Strip high bits */
1444 while(str[0] && str[1] && (len > 1))
1446 *str++ = c >> 8;
1447 len--;
1448 *str++ = c & 0xff;
1449 len--;
1451 if(len && str[0]) {
1452 /* as per msdn pad with a blank character */
1453 str[0] = ' ';
1456 return ret;
1459 /*********************************************************************
1460 * _mbsnset(MSVCRT.@)
1462 unsigned char* CDECL _mbsnset(unsigned char* str, unsigned int c, MSVCRT_size_t len)
1464 unsigned char *ret = str;
1466 if(!len)
1467 return ret;
1469 if(!get_mbcinfo()->ismbcodepage || c < 256)
1470 return u__strnset(str, c, len); /* ASCII CP or SB char */
1472 c &= 0xffff; /* Strip high bits */
1474 while(str[0] && str[1] && len--)
1476 *str++ = c >> 8;
1477 *str++ = c & 0xff;
1479 if(len && str[0])
1480 str[0] = '\0'; /* FIXME: OK to shorten? */
1482 return ret;
1485 /*********************************************************************
1486 * _mbsnccnt(MSVCRT.@)
1487 * 'c' is for 'character'.
1489 MSVCRT_size_t CDECL _mbsnccnt(const unsigned char* str, MSVCRT_size_t len)
1491 MSVCRT_size_t ret;
1492 if(get_mbcinfo()->ismbcodepage)
1494 ret=0;
1495 while(*str && len-- > 0)
1497 if(_ismbblead(*str))
1499 if (!len)
1500 break;
1501 len--;
1502 str++;
1504 str++;
1505 ret++;
1507 return ret;
1509 ret=u_strlen(str);
1510 return min(ret, len); /* ASCII CP */
1513 /*********************************************************************
1514 * _mbsnbcnt(MSVCRT.@)
1515 * 'b' is for byte count.
1517 MSVCRT_size_t CDECL _mbsnbcnt(const unsigned char* str, MSVCRT_size_t len)
1519 MSVCRT_size_t ret;
1520 if(get_mbcinfo()->ismbcodepage)
1522 const unsigned char* xstr = str;
1523 while(*xstr && len-- > 0)
1525 if (_ismbblead(*xstr++))
1526 xstr++;
1528 return xstr-str;
1530 ret=u_strlen(str);
1531 return min(ret, len); /* ASCII CP */
1534 /*********************************************************************
1535 * _mbsnbcat(MSVCRT.@)
1537 unsigned char* CDECL _mbsnbcat(unsigned char* dst, const unsigned char* src, MSVCRT_size_t len)
1539 if(get_mbcinfo()->ismbcodepage)
1541 unsigned char *res = dst;
1542 while (*dst) {
1543 if (_ismbblead(*dst++)) {
1544 if (*dst) {
1545 dst++;
1546 } else {
1547 /* as per msdn overwrite the lead byte in front of '\0' */
1548 dst--;
1549 break;
1553 while (*src && len--) *dst++ = *src++;
1554 *dst = '\0';
1555 return res;
1557 return u_strncat(dst, src, len); /* ASCII CP */
1560 int CDECL _mbsnbcat_s(unsigned char *dst, MSVCRT_size_t size, const unsigned char *src, MSVCRT_size_t len)
1562 unsigned char *ptr = dst;
1563 MSVCRT_size_t i;
1565 if (!dst && !size && !src && !len)
1566 return 0;
1568 if (!dst || !size || !src)
1570 if (dst && size)
1571 *dst = '\0';
1573 *MSVCRT__errno() = MSVCRT_EINVAL;
1574 return MSVCRT_EINVAL;
1577 /* Find the null terminator of the destination buffer. */
1578 while (size && *ptr)
1579 size--, ptr++;
1581 if (!size)
1583 *dst = '\0';
1584 *MSVCRT__errno() = MSVCRT_EINVAL;
1585 return MSVCRT_EINVAL;
1588 /* If necessary, check that the character preceding the null terminator is
1589 * a lead byte and move the pointer back by one for later overwrite. */
1590 if (ptr != dst && get_mbcinfo()->ismbcodepage && _ismbblead(*(ptr - 1)))
1591 size++, ptr--;
1593 for (i = 0; *src && i < len; i++)
1595 *ptr++ = *src++;
1596 size--;
1598 if (!size)
1600 *dst = '\0';
1601 *MSVCRT__errno() = MSVCRT_ERANGE;
1602 return MSVCRT_ERANGE;
1606 *ptr = '\0';
1607 return 0;
1610 /*********************************************************************
1611 * _mbsncat(MSVCRT.@)
1613 unsigned char* CDECL _mbsncat(unsigned char* dst, const unsigned char* src, MSVCRT_size_t len)
1615 if(get_mbcinfo()->ismbcodepage)
1617 unsigned char *res = dst;
1618 while (*dst)
1620 if (_ismbblead(*dst++))
1621 dst++;
1623 while (*src && len--)
1625 *dst++ = *src;
1626 if(_ismbblead(*src++))
1627 *dst++ = *src++;
1629 *dst = '\0';
1630 return res;
1632 return u_strncat(dst, src, len); /* ASCII CP */
1636 /*********************************************************************
1637 * _mbslwr(MSVCRT.@)
1639 unsigned char* CDECL _mbslwr(unsigned char* s)
1641 unsigned char *ret = s;
1642 if (!s)
1643 return NULL;
1644 if (get_mbcinfo()->ismbcodepage)
1646 unsigned int c;
1647 while (*s)
1649 c = _mbctolower(_mbsnextc(s));
1650 /* Note that I assume that the size of the character is unchanged */
1651 if (c > 255)
1653 *s++=(c>>8);
1654 c=c & 0xff;
1656 *s++=c;
1659 else for ( ; *s; s++) *s = tolower(*s);
1660 return ret;
1664 /*********************************************************************
1665 * _mbslwr_s(MSVCRT.@)
1667 int CDECL _mbslwr_s(unsigned char* s, MSVCRT_size_t len)
1669 if (!s && !len)
1671 return 0;
1673 else if (!s || !len)
1675 *MSVCRT__errno() = MSVCRT_EINVAL;
1676 return MSVCRT_EINVAL;
1678 if (get_mbcinfo()->ismbcodepage)
1680 unsigned int c;
1681 for ( ; *s && len > 0; len--)
1683 c = _mbctolower(_mbsnextc(s));
1684 /* Note that I assume that the size of the character is unchanged */
1685 if (c > 255)
1687 *s++=(c>>8);
1688 c=c & 0xff;
1690 *s++=c;
1693 else for ( ; *s && len > 0; s++, len--) *s = tolower(*s);
1694 if (*s)
1696 *s = '\0';
1697 *MSVCRT__errno() = MSVCRT_EINVAL;
1698 return MSVCRT_EINVAL;
1700 return 0;
1704 /*********************************************************************
1705 * _mbsupr(MSVCRT.@)
1707 unsigned char* CDECL _mbsupr(unsigned char* s)
1709 unsigned char *ret = s;
1710 if (!s)
1711 return NULL;
1712 if (get_mbcinfo()->ismbcodepage)
1714 unsigned int c;
1715 while (*s)
1717 c = _mbctoupper(_mbsnextc(s));
1718 /* Note that I assume that the size of the character is unchanged */
1719 if (c > 255)
1721 *s++=(c>>8);
1722 c=c & 0xff;
1724 *s++=c;
1727 else for ( ; *s; s++) *s = toupper(*s);
1728 return ret;
1732 /*********************************************************************
1733 * _mbsupr_s(MSVCRT.@)
1735 int CDECL _mbsupr_s(unsigned char* s, MSVCRT_size_t len)
1737 if (!s && !len)
1739 return 0;
1741 else if (!s || !len)
1743 *MSVCRT__errno() = MSVCRT_EINVAL;
1744 return MSVCRT_EINVAL;
1746 if (get_mbcinfo()->ismbcodepage)
1748 unsigned int c;
1749 for ( ; *s && len > 0; len--)
1751 c = _mbctoupper(_mbsnextc(s));
1752 /* Note that I assume that the size of the character is unchanged */
1753 if (c > 255)
1755 *s++=(c>>8);
1756 c=c & 0xff;
1758 *s++=c;
1761 else for ( ; *s && len > 0; s++, len--) *s = toupper(*s);
1762 if (*s)
1764 *s = '\0';
1765 *MSVCRT__errno() = MSVCRT_EINVAL;
1766 return MSVCRT_EINVAL;
1768 return 0;
1772 /*********************************************************************
1773 * _mbsspn (MSVCRT.@)
1775 MSVCRT_size_t CDECL _mbsspn(const unsigned char* string, const unsigned char* set)
1777 const unsigned char *p, *q;
1779 for (p = string; *p; p++)
1781 if (_ismbblead(*p))
1783 for (q = set; *q; q++)
1785 if (!q[1])
1786 break;
1787 if ((*p == *q) && (p[1] == q[1]))
1788 break;
1789 q++;
1791 if (!q[0] || !q[1]) break;
1793 else
1795 for (q = set; *q; q++)
1796 if (*p == *q)
1797 break;
1798 if (!*q) break;
1801 return p - string;
1804 /*********************************************************************
1805 * _mbsspnp (MSVCRT.@)
1807 unsigned char* CDECL _mbsspnp(const unsigned char* string, const unsigned char* set)
1809 const unsigned char *p, *q;
1811 for (p = string; *p; p++)
1813 if (_ismbblead(*p))
1815 for (q = set; *q; q++)
1817 if (!q[1])
1818 break;
1819 if ((*p == *q) && (p[1] == q[1]))
1820 break;
1821 q++;
1823 if (!q[0] || !q[1]) break;
1825 else
1827 for (q = set; *q; q++)
1828 if (*p == *q)
1829 break;
1830 if (!*q) break;
1833 if (*p == '\0')
1834 return NULL;
1835 return (unsigned char *)p;
1838 /*********************************************************************
1839 * _mbscspn(MSVCRT.@)
1841 MSVCRT_size_t CDECL _mbscspn(const unsigned char* str, const unsigned char* cmp)
1843 if (get_mbcinfo()->ismbcodepage)
1844 FIXME("don't handle double character case\n");
1845 return u_strcspn(str, cmp);
1848 /*********************************************************************
1849 * _mbsrev (MSVCRT.@)
1851 unsigned char* CDECL _mbsrev(unsigned char* str)
1853 int i, len = _mbslen(str);
1854 unsigned char *p, *temp=MSVCRT_malloc(len*2);
1856 if(!temp)
1857 return str;
1859 /* unpack multibyte string to temp buffer */
1860 p=str;
1861 for(i=0; i<len; i++)
1863 if (_ismbblead(*p))
1865 temp[i*2]=*p++;
1866 temp[i*2+1]=*p++;
1868 else
1870 temp[i*2]=*p++;
1871 temp[i*2+1]=0;
1875 /* repack it in the reverse order */
1876 p=str;
1877 for(i=len-1; i>=0; i--)
1879 if(_ismbblead(temp[i*2]))
1881 *p++=temp[i*2];
1882 *p++=temp[i*2+1];
1884 else
1886 *p++=temp[i*2];
1890 MSVCRT_free(temp);
1892 return str;
1895 /*********************************************************************
1896 * _mbspbrk (MSVCRT.@)
1898 unsigned char* CDECL _mbspbrk(const unsigned char* str, const unsigned char* accept)
1900 const unsigned char* p;
1902 while(*str)
1904 for(p = accept; *p; p += (_ismbblead(*p)?2:1) )
1906 if (*p == *str)
1907 if( !_ismbblead(*p) || ( *(p+1) == *(str+1) ) )
1908 return (unsigned char*)str;
1910 str += (_ismbblead(*str)?2:1);
1912 return NULL;
1917 * Functions depending on locale codepage
1920 /*********************************************************************
1921 * mblen(MSVCRT.@)
1922 * REMARKS
1923 * Unlike most of the multibyte string functions this function uses
1924 * the locale codepage, not the codepage set by _setmbcp
1926 int CDECL MSVCRT_mblen(const char* str, MSVCRT_size_t size)
1928 if (str && *str && size)
1930 if(get_locinfo()->mb_cur_max == 1)
1931 return 1; /* ASCII CP */
1933 return !MSVCRT_isleadbyte((unsigned char)*str) ? 1 : (size>1 ? 2 : -1);
1935 return 0;
1938 /*********************************************************************
1939 * mbrlen(MSVCRT.@)
1941 MSVCRT_size_t CDECL MSVCRT_mbrlen(const char *str, MSVCRT_size_t len, MSVCRT_mbstate_t *state)
1943 MSVCRT_mbstate_t s = (state ? *state : 0);
1944 MSVCRT_size_t ret;
1946 if(!len || !str || !*str)
1947 return 0;
1949 if(get_locinfo()->mb_cur_max == 1) {
1950 return 1;
1951 }else if(!s && MSVCRT_isleadbyte((unsigned char)*str)) {
1952 if(len == 1) {
1953 s = (unsigned char)*str;
1954 ret = -2;
1955 }else {
1956 ret = 2;
1958 }else if(!s) {
1959 ret = 1;
1960 }else {
1961 s = 0;
1962 ret = 2;
1965 if(state)
1966 *state = s;
1967 return ret;
1970 /*********************************************************************
1971 * _mbstrlen_l(MSVCRT.@)
1973 MSVCRT_size_t CDECL _mbstrlen_l(const char* str, MSVCRT__locale_t locale)
1975 MSVCRT_pthreadlocinfo locinfo;
1977 if(!locale)
1978 locinfo = get_locinfo();
1979 else
1980 locinfo = locale->locinfo;
1982 if(locinfo->mb_cur_max > 1) {
1983 MSVCRT_size_t len;
1984 len = MultiByteToWideChar(locinfo->lc_codepage, MB_ERR_INVALID_CHARS,
1985 str, -1, NULL, 0);
1986 if (!len) {
1987 *MSVCRT__errno() = MSVCRT_EILSEQ;
1988 return -1;
1990 return len - 1;
1993 return strlen(str);
1996 /*********************************************************************
1997 * _mbstrlen(MSVCRT.@)
1999 MSVCRT_size_t CDECL _mbstrlen(const char* str)
2001 return _mbstrlen_l(str, NULL);
2004 /*********************************************************************
2005 * _mbtowc_l(MSVCRT.@)
2007 int CDECL MSVCRT_mbtowc_l(MSVCRT_wchar_t *dst, const char* str, MSVCRT_size_t n, MSVCRT__locale_t locale)
2009 MSVCRT_pthreadlocinfo locinfo;
2010 MSVCRT_wchar_t tmpdst = '\0';
2012 if(!locale)
2013 locinfo = get_locinfo();
2014 else
2015 locinfo = locale->locinfo;
2017 if(n <= 0 || !str)
2018 return 0;
2019 if(!locinfo->lc_codepage)
2020 tmpdst = (unsigned char)*str;
2021 else if(!MultiByteToWideChar(locinfo->lc_codepage, 0, str, n, &tmpdst, 1))
2022 return -1;
2023 if(dst)
2024 *dst = tmpdst;
2025 /* return the number of bytes from src that have been used */
2026 if(!*str)
2027 return 0;
2028 if(n >= 2 && MSVCRT__isleadbyte_l((unsigned char)*str, locale) && str[1])
2029 return 2;
2030 return 1;
2033 /*********************************************************************
2034 * mbtowc(MSVCRT.@)
2036 int CDECL MSVCRT_mbtowc(MSVCRT_wchar_t *dst, const char* str, MSVCRT_size_t n)
2038 return MSVCRT_mbtowc_l(dst, str, n, NULL);
2041 /*********************************************************************
2042 * mbrtowc(MSVCRT.@)
2044 MSVCRT_size_t CDECL MSVCRT_mbrtowc(MSVCRT_wchar_t *dst, const char *str,
2045 MSVCRT_size_t n, MSVCRT_mbstate_t *state)
2047 MSVCRT_pthreadlocinfo locinfo = get_locinfo();
2048 MSVCRT_mbstate_t s = (state ? *state : 0);
2049 char tmpstr[2];
2050 int len = 0;
2052 if(dst)
2053 *dst = 0;
2055 if(!n || !str || !*str)
2056 return 0;
2058 if(locinfo->mb_cur_max == 1) {
2059 tmpstr[len++] = *str;
2060 }else if(!s && MSVCRT_isleadbyte((unsigned char)*str)) {
2061 if(n == 1) {
2062 s = (unsigned char)*str;
2063 len = -2;
2064 }else {
2065 tmpstr[0] = str[0];
2066 tmpstr[1] = str[1];
2067 len = 2;
2069 }else if(!s) {
2070 tmpstr[len++] = *str;
2071 }else {
2072 tmpstr[0] = s;
2073 tmpstr[1] = *str;
2074 len = 2;
2075 s = 0;
2078 if(len > 0) {
2079 if(!MultiByteToWideChar(locinfo->lc_codepage, 0, tmpstr, len, dst, dst ? 1 : 0))
2080 len = -1;
2083 if(state)
2084 *state = s;
2085 return len;
2088 /*********************************************************************
2089 * _mbstowcs_l(MSVCRT.@)
2091 MSVCRT_size_t CDECL MSVCRT__mbstowcs_l(MSVCRT_wchar_t *wcstr, const char *mbstr,
2092 MSVCRT_size_t count, MSVCRT__locale_t locale)
2094 MSVCRT_pthreadlocinfo locinfo;
2095 MSVCRT_size_t i, size;
2097 if(!locale)
2098 locinfo = get_locinfo();
2099 else
2100 locinfo = locale->locinfo;
2102 /* Ignore count parameter */
2103 if(!wcstr)
2104 return MultiByteToWideChar(locinfo->lc_codepage, 0, mbstr, -1, NULL, 0)-1;
2106 for(i=0, size=0; i<count; i++) {
2107 if(mbstr[size] == '\0')
2108 break;
2110 size += (MSVCRT__isleadbyte_l((unsigned char)mbstr[size], locale) ? 2 : 1);
2113 size = MultiByteToWideChar(locinfo->lc_codepage, 0,
2114 mbstr, size, wcstr, count);
2116 if(size<count && wcstr)
2117 wcstr[size] = '\0';
2119 return size;
2122 /*********************************************************************
2123 * mbstowcs(MSVCRT.@)
2125 MSVCRT_size_t CDECL MSVCRT_mbstowcs(MSVCRT_wchar_t *wcstr,
2126 const char *mbstr, MSVCRT_size_t count)
2128 return MSVCRT__mbstowcs_l(wcstr, mbstr, count, NULL);
2131 /*********************************************************************
2132 * _mbstowcs_s_l(MSVCRT.@)
2134 int CDECL MSVCRT__mbstowcs_s_l(MSVCRT_size_t *ret, MSVCRT_wchar_t *wcstr,
2135 MSVCRT_size_t size, const char *mbstr,
2136 MSVCRT_size_t count, MSVCRT__locale_t locale)
2138 MSVCRT_size_t conv;
2140 if(!wcstr && !size) {
2141 conv = MSVCRT__mbstowcs_l(NULL, mbstr, 0, locale);
2142 if(ret)
2143 *ret = conv+1;
2144 return 0;
2147 if (!MSVCRT_CHECK_PMT(wcstr != NULL)) return MSVCRT_EINVAL;
2148 if (!MSVCRT_CHECK_PMT(mbstr != NULL)) {
2149 if(size) wcstr[0] = '\0';
2150 return MSVCRT_EINVAL;
2153 if(count==MSVCRT__TRUNCATE || size<count)
2154 conv = size;
2155 else
2156 conv = count;
2158 conv = MSVCRT__mbstowcs_l(wcstr, mbstr, conv, locale);
2159 if(conv<size)
2160 wcstr[conv++] = '\0';
2161 else if(conv==size && (count==MSVCRT__TRUNCATE || wcstr[conv-1]=='\0'))
2162 wcstr[conv-1] = '\0';
2163 else {
2164 MSVCRT_INVALID_PMT("wcstr[size] is too small", MSVCRT_ERANGE);
2165 if(size)
2166 wcstr[0] = '\0';
2167 return MSVCRT_ERANGE;
2170 if(ret)
2171 *ret = conv;
2172 return 0;
2175 /*********************************************************************
2176 * mbstowcs_s(MSVCRT.@)
2178 int CDECL MSVCRT__mbstowcs_s(MSVCRT_size_t *ret, MSVCRT_wchar_t *wcstr,
2179 MSVCRT_size_t size, const char *mbstr, MSVCRT_size_t count)
2181 return MSVCRT__mbstowcs_s_l(ret, wcstr, size, mbstr, count, NULL);
2184 /*********************************************************************
2185 * mbsrtowcs(MSVCRT.@)
2187 MSVCRT_size_t CDECL MSVCRT_mbsrtowcs(MSVCRT_wchar_t *wcstr,
2188 const char **pmbstr, MSVCRT_size_t count, MSVCRT_mbstate_t *state)
2190 MSVCRT_mbstate_t s = (state ? *state : 0);
2191 MSVCRT_wchar_t tmpdst;
2192 MSVCRT_size_t ret = 0;
2194 if(!MSVCRT_CHECK_PMT(pmbstr != NULL))
2195 return -1;
2197 while(!wcstr || count>ret) {
2198 int ch_len = MSVCRT_mbrtowc(&tmpdst, *pmbstr, 2, &s);
2199 if(wcstr)
2200 wcstr[ret] = tmpdst;
2202 if(ch_len < 0) {
2203 return -1;
2204 }else if(ch_len == 0) {
2205 *pmbstr = NULL;
2206 return ret;
2209 *pmbstr += ch_len;
2210 ret++;
2213 return ret;