winebuild: Support the mingw32 target without an i[3456]86- prefix.
[wine.git] / dlls / msvcrt / mbcs.c
blob0f15cb1ce5a7f56202bc515a8658bd546ea8fa63
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 * ___mb_cur_max_func(MSVCRT.@)
179 int* CDECL MSVCRT____mb_cur_max_func(void)
181 return &get_locinfo()->mb_cur_max;
184 /* ___mb_cur_max_l_func - not exported in native msvcrt */
185 int* CDECL ___mb_cur_max_l_func(MSVCRT__locale_t locale)
187 MSVCRT_pthreadlocinfo locinfo;
189 if(!locale)
190 locinfo = get_locinfo();
191 else
192 locinfo = locale->locinfo;
194 return &locinfo->mb_cur_max;
197 /*********************************************************************
198 * INTERNAL: _setmbcp_l
200 int _setmbcp_l(int cp, LCID lcid, MSVCRT_pthreadmbcinfo mbcinfo)
202 const char format[] = ".%d";
204 int newcp;
205 CPINFO cpi;
206 BYTE *bytes;
207 WORD chartypes[256];
208 char bufA[256];
209 WCHAR bufW[256];
210 int charcount;
211 int ret;
212 int i;
214 if(!mbcinfo)
215 mbcinfo = get_mbcinfo();
217 switch (cp)
219 case _MB_CP_ANSI:
220 newcp = GetACP();
221 break;
222 case _MB_CP_OEM:
223 newcp = GetOEMCP();
224 break;
225 case _MB_CP_LOCALE:
226 newcp = get_locinfo()->lc_codepage;
227 if(newcp)
228 break;
229 /* fall through (C locale) */
230 case _MB_CP_SBCS:
231 newcp = 20127; /* ASCII */
232 break;
233 default:
234 newcp = cp;
235 break;
238 if(lcid == -1) {
239 sprintf(bufA, format, newcp);
240 mbcinfo->mblcid = MSVCRT_locale_to_LCID(bufA);
241 } else {
242 mbcinfo->mblcid = lcid;
245 if(mbcinfo->mblcid == -1)
247 WARN("Can't assign LCID to codepage (%d)\n", mbcinfo->mblcid);
248 mbcinfo->mblcid = 0;
251 if (!GetCPInfo(newcp, &cpi))
253 WARN("Codepage %d not found\n", newcp);
254 *MSVCRT__errno() = MSVCRT_EINVAL;
255 return -1;
258 /* setup the _mbctype */
259 memset(mbcinfo->mbctype, 0, sizeof(unsigned char[257]));
260 memset(mbcinfo->mbcasemap, 0, sizeof(unsigned char[256]));
262 bytes = cpi.LeadByte;
263 while (bytes[0] || bytes[1])
265 for (i = bytes[0]; i <= bytes[1]; i++)
266 mbcinfo->mbctype[i + 1] |= _M1;
267 bytes += 2;
270 if (cpi.MaxCharSize > 1)
272 /* trail bytes not available through kernel32 but stored in a structure in msvcrt */
273 struct cp_extra_info_t *cpextra = g_cpextrainfo;
275 mbcinfo->ismbcodepage = 1;
276 while (TRUE)
278 if (cpextra->cp == 0 || cpextra->cp == newcp)
280 if (cpextra->cp == 0)
281 FIXME("trail bytes data not available for DBCS codepage %d - assuming all bytes\n", newcp);
283 bytes = cpextra->TrailBytes;
284 while (bytes[0] || bytes[1])
286 for (i = bytes[0]; i <= bytes[1]; i++)
287 mbcinfo->mbctype[i + 1] |= _M2;
288 bytes += 2;
290 break;
292 cpextra++;
295 else
296 mbcinfo->ismbcodepage = 0;
298 /* we can't use GetStringTypeA directly because we don't have a locale - only a code page
300 charcount = 0;
301 for (i = 0; i < 256; i++)
302 if (!(mbcinfo->mbctype[i + 1] & _M1))
303 bufA[charcount++] = i;
305 ret = MultiByteToWideChar(newcp, 0, bufA, charcount, bufW, charcount);
306 if (ret != charcount)
307 ERR("MultiByteToWideChar of chars failed for cp %d, ret=%d (exp %d), error=%d\n", newcp, ret, charcount, GetLastError());
309 GetStringTypeW(CT_CTYPE1, bufW, charcount, chartypes);
311 charcount = 0;
312 for (i = 0; i < 256; i++)
313 if (!(mbcinfo->mbctype[i + 1] & _M1))
315 if (chartypes[charcount] & C1_UPPER)
317 mbcinfo->mbctype[i + 1] |= _SBUP;
318 bufW[charcount] = tolowerW(bufW[charcount]);
320 else if (chartypes[charcount] & C1_LOWER)
322 mbcinfo->mbctype[i + 1] |= _SBLOW;
323 bufW[charcount] = toupperW(bufW[charcount]);
325 charcount++;
328 ret = WideCharToMultiByte(newcp, 0, bufW, charcount, bufA, charcount, NULL, NULL);
329 if (ret != charcount)
330 ERR("WideCharToMultiByte failed for cp %d, ret=%d (exp %d), error=%d\n", newcp, ret, charcount, GetLastError());
332 charcount = 0;
333 for (i = 0; i < 256; i++)
335 if(!(mbcinfo->mbctype[i + 1] & _M1))
337 if(mbcinfo->mbctype[i] & (C1_UPPER|C1_LOWER))
338 mbcinfo->mbcasemap[i] = bufA[charcount];
339 charcount++;
343 if (newcp == 932) /* CP932 only - set _MP and _MS */
345 /* On Windows it's possible to calculate the _MP and _MS from CT_CTYPE1
346 * and CT_CTYPE3. But as of Wine 0.9.43 we return wrong values what makes
347 * it hard. As this is set only for codepage 932 we hardcode it what gives
348 * also faster execution.
350 for (i = 161; i <= 165; i++)
351 mbcinfo->mbctype[i + 1] |= _MP;
352 for (i = 166; i <= 223; i++)
353 mbcinfo->mbctype[i + 1] |= _MS;
356 mbcinfo->mbcodepage = newcp;
357 if(MSVCRT_locale && mbcinfo == MSVCRT_locale->mbcinfo)
358 memcpy(MSVCRT_mbctype, MSVCRT_locale->mbcinfo->mbctype, sizeof(MSVCRT_mbctype));
360 return 0;
363 /*********************************************************************
364 * _setmbcp (MSVCRT.@)
366 int CDECL _setmbcp(int cp)
368 return _setmbcp_l(cp, -1, NULL);
371 /*********************************************************************
372 * _getmbcp (MSVCRT.@)
374 int CDECL _getmbcp(void)
376 return get_mbcinfo()->mbcodepage;
379 /*********************************************************************
380 * _mbsnextc(MSVCRT.@)
382 unsigned int CDECL _mbsnextc(const unsigned char* str)
384 if(_ismbblead(*str))
385 return *str << 8 | str[1];
386 return *str;
389 /*********************************************************************
390 * _mbctolower(MSVCRT.@)
392 unsigned int CDECL _mbctolower(unsigned int c)
394 if (_ismbblead(c))
396 FIXME("Handle MBC chars\n");
397 return c;
399 return tolower(c); /* ASCII CP or SB char */
402 /*********************************************************************
403 * _mbctoupper(MSVCRT.@)
405 unsigned int CDECL _mbctoupper(unsigned int c)
407 if (_ismbblead(c))
409 FIXME("Handle MBC chars\n");
410 return c;
412 return toupper(c); /* ASCII CP or SB char */
415 /*********************************************************************
416 * _mbctombb (MSVCRT.@)
418 unsigned int CDECL _mbctombb(unsigned int c)
420 unsigned int value;
422 if(get_mbcinfo()->mbcodepage == 932)
424 if(c >= 0x829f && c <= 0x82f1) /* Hiragana */
425 return mbctombb_932_kana[c - 0x829f];
426 if(c >= 0x8340 && c <= 0x8396 && c != 0x837f) /* Katakana */
427 return mbctombb_932_kana[c - 0x8340 - (c >= 0x837f ? 1 : 0)];
428 if(c >= 0x8140 && c <= 0x8197) /* Punctuation */
430 value = mbctombb_932_punct[c - 0x8140];
431 return value ? value : c;
433 if((c >= 0x824f && c <= 0x8258) || /* Fullwidth digits */
434 (c >= 0x8260 && c <= 0x8279)) /* Fullwidth capitals letters */
435 return c - 0x821f;
436 if(c >= 0x8281 && c <= 0x829a) /* Fullwidth small letters */
437 return c - 0x8220;
438 /* all other cases return c */
440 return c;
443 /*********************************************************************
444 * _mbcjistojms(MSVCRT.@)
446 * Converts a jis character to sjis.
447 * Based on description from
448 * http://www.slayers.ne.jp/~oouchi/code/jistosjis.html
450 unsigned int CDECL _mbcjistojms(unsigned int c)
452 /* Conversion takes place only when codepage is 932.
453 In all other cases, c is returned unchanged */
454 if(get_mbcinfo()->mbcodepage == 932)
456 if(HIBYTE(c) >= 0x21 && HIBYTE(c) <= 0x7e &&
457 LOBYTE(c) >= 0x21 && LOBYTE(c) <= 0x7e)
459 if(HIBYTE(c) % 2)
460 c += 0x1f;
461 else
462 c += 0x7d;
464 if(LOBYTE(c) >= 0x7F)
465 c += 0x1;
467 c = (((HIBYTE(c) - 0x21)/2 + 0x81) << 8) | LOBYTE(c);
469 if(HIBYTE(c) > 0x9f)
470 c += 0x4000;
472 else
473 return 0; /* Codepage is 932, but c can't be converted */
476 return c;
479 /*********************************************************************
480 * _mbcjmstojis(MSVCRT.@)
482 * Converts a sjis character to jis.
484 unsigned int CDECL _mbcjmstojis(unsigned int c)
486 /* Conversion takes place only when codepage is 932.
487 In all other cases, c is returned unchanged */
488 if(get_mbcinfo()->mbcodepage == 932)
490 if(_ismbclegal(c) && HIBYTE(c) < 0xf0)
492 if(HIBYTE(c) >= 0xe0)
493 c -= 0x4000;
495 c = (((HIBYTE(c) - 0x81)*2 + 0x21) << 8) | LOBYTE(c);
497 if(LOBYTE(c) > 0x7f)
498 c -= 0x1;
500 if(LOBYTE(c) > 0x9d)
501 c += 0x83;
502 else
503 c -= 0x1f;
505 else
506 return 0; /* Codepage is 932, but c can't be converted */
509 return c;
512 /*********************************************************************
513 * _mbsdec(MSVCRT.@)
515 unsigned char* CDECL _mbsdec(const unsigned char* start, const unsigned char* cur)
517 if(get_mbcinfo()->ismbcodepage)
518 return (unsigned char *)(_ismbstrail(start,cur-1) ? cur - 2 : cur -1);
520 return (unsigned char *)cur - 1; /* ASCII CP or SB char */
523 /*********************************************************************
524 * _mbclen(MSVCRT.@)
526 unsigned int CDECL _mbclen(const unsigned char* str)
528 return _ismbblead(*str) ? 2 : 1;
531 /*********************************************************************
532 * _mbsinc(MSVCRT.@)
534 unsigned char* CDECL _mbsinc(const unsigned char* str)
536 return (unsigned char *)(str + _mbclen(str));
539 /*********************************************************************
540 * _mbsninc(MSVCRT.@)
542 unsigned char* CDECL _mbsninc(const unsigned char* str, MSVCRT_size_t num)
544 if(!str)
545 return NULL;
547 while (num > 0 && *str)
549 if (_ismbblead(*str))
551 if (!*(str+1))
552 break;
553 str++;
555 str++;
556 num--;
559 return (unsigned char*)str;
562 /*********************************************************************
563 * _mbslen(MSVCRT.@)
565 MSVCRT_size_t CDECL _mbslen(const unsigned char* str)
567 MSVCRT_size_t len = 0;
568 while(*str)
570 if (_ismbblead(*str))
572 str++;
573 if (!*str) /* count only full chars */
574 break;
576 str++;
577 len++;
579 return len;
582 /*********************************************************************
583 * _mbccpy(MSVCRT.@)
585 void CDECL _mbccpy(unsigned char* dest, const unsigned char* src)
587 *dest = *src;
588 if(_ismbblead(*src))
589 *++dest = *++src; /* MB char */
592 /*********************************************************************
593 * _mbsncpy(MSVCRT.@)
594 * REMARKS
595 * The parameter n is the number or characters to copy, not the size of
596 * the buffer. Use _mbsnbcpy for a function analogical to strncpy
598 unsigned char* CDECL _mbsncpy(unsigned char* dst, const unsigned char* src, MSVCRT_size_t n)
600 unsigned char* ret = dst;
601 if(!n)
602 return dst;
603 if (get_mbcinfo()->ismbcodepage)
605 while (*src && n)
607 n--;
608 if (_ismbblead(*src))
610 if (!*(src+1))
612 *dst++ = 0;
613 *dst++ = 0;
614 break;
617 *dst++ = *src++;
620 *dst++ = *src++;
623 else
625 while (n)
627 n--;
628 if (!(*dst++ = *src++)) break;
631 while (n--) *dst++ = 0;
632 return ret;
635 /*********************************************************************
636 * _mbsnbcpy_s(MSVCRT.@)
637 * REMARKS
638 * Unlike _mbsnbcpy this function does not pad the rest of the dest
639 * string with 0
641 int CDECL _mbsnbcpy_s(unsigned char* dst, MSVCRT_size_t size, const unsigned char* src, MSVCRT_size_t n)
643 MSVCRT_size_t pos = 0;
645 if(!dst || size == 0)
646 return MSVCRT_EINVAL;
647 if(!src)
649 dst[0] = '\0';
650 return MSVCRT_EINVAL;
652 if(!n)
653 return 0;
655 if(get_mbcinfo()->ismbcodepage)
657 int is_lead = 0;
658 while (*src && n)
660 if(pos == size)
662 dst[0] = '\0';
663 return MSVCRT_ERANGE;
665 is_lead = (!is_lead && _ismbblead(*src));
666 n--;
667 dst[pos++] = *src++;
670 if (is_lead) /* if string ends with a lead, remove it */
671 dst[pos - 1] = 0;
673 else
675 while (n)
677 n--;
678 if(pos == size)
680 dst[0] = '\0';
681 return MSVCRT_ERANGE;
684 if(!(*src)) break;
685 dst[pos++] = *src++;
689 if(pos < size)
690 dst[pos] = '\0';
691 else
693 dst[0] = '\0';
694 return MSVCRT_ERANGE;
697 return 0;
700 /*********************************************************************
701 * _mbsnbcpy(MSVCRT.@)
702 * REMARKS
703 * Like strncpy this function doesn't enforce the string to be
704 * NUL-terminated
706 unsigned char* CDECL _mbsnbcpy(unsigned char* dst, const unsigned char* src, MSVCRT_size_t n)
708 unsigned char* ret = dst;
709 if(!n)
710 return dst;
711 if(get_mbcinfo()->ismbcodepage)
713 int is_lead = 0;
714 while (*src && n)
716 is_lead = (!is_lead && _ismbblead(*src));
717 n--;
718 *dst++ = *src++;
721 if (is_lead) /* if string ends with a lead, remove it */
722 *(dst - 1) = 0;
724 else
726 while (n)
728 n--;
729 if (!(*dst++ = *src++)) break;
732 while (n--) *dst++ = 0;
733 return ret;
736 /*********************************************************************
737 * _mbscmp(MSVCRT.@)
739 int CDECL _mbscmp(const unsigned char* str, const unsigned char* cmp)
741 if(get_mbcinfo()->ismbcodepage)
743 unsigned int strc, cmpc;
744 do {
745 if(!*str)
746 return *cmp ? -1 : 0;
747 if(!*cmp)
748 return 1;
749 strc = _mbsnextc(str);
750 cmpc = _mbsnextc(cmp);
751 if(strc != cmpc)
752 return strc < cmpc ? -1 : 1;
753 str +=(strc > 255) ? 2 : 1;
754 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
755 } while(1);
757 return u_strcmp(str, cmp); /* ASCII CP */
760 /*********************************************************************
761 * _mbsicoll(MSVCRT.@)
762 * FIXME: handle locales.
764 int CDECL _mbsicoll(const unsigned char* str, const unsigned char* cmp)
766 if(get_mbcinfo()->ismbcodepage)
768 unsigned int strc, cmpc;
769 do {
770 if(!*str)
771 return *cmp ? -1 : 0;
772 if(!*cmp)
773 return 1;
774 strc = _mbctolower(_mbsnextc(str));
775 cmpc = _mbctolower(_mbsnextc(cmp));
776 if(strc != cmpc)
777 return strc < cmpc ? -1 : 1;
778 str +=(strc > 255) ? 2 : 1;
779 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
780 } while(1);
782 return u_strcasecmp(str, cmp); /* ASCII CP */
785 /*********************************************************************
786 * _mbscoll(MSVCRT.@)
787 * Performs a case-sensitive comparison according to the current code page
788 * RETURN
789 * _NLSCMPERROR if error
790 * FIXME: handle locales.
792 int CDECL _mbscoll(const unsigned char* str, const unsigned char* cmp)
794 if(get_mbcinfo()->ismbcodepage)
796 unsigned int strc, cmpc;
797 do {
798 if(!*str)
799 return *cmp ? -1 : 0;
800 if(!*cmp)
801 return 1;
802 strc = _mbsnextc(str);
803 cmpc = _mbsnextc(cmp);
804 if(strc != cmpc)
805 return strc < cmpc ? -1 : 1;
806 str +=(strc > 255) ? 2 : 1;
807 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
808 } while(1);
810 return u_strcmp(str, cmp); /* ASCII CP */
814 /*********************************************************************
815 * _mbsicmp(MSVCRT.@)
817 int CDECL _mbsicmp(const unsigned char* str, const unsigned char* cmp)
819 if(get_mbcinfo()->ismbcodepage)
821 unsigned int strc, cmpc;
822 do {
823 if(!*str)
824 return *cmp ? -1 : 0;
825 if(!*cmp)
826 return 1;
827 strc = _mbctolower(_mbsnextc(str));
828 cmpc = _mbctolower(_mbsnextc(cmp));
829 if(strc != cmpc)
830 return strc < cmpc ? -1 : 1;
831 str +=(strc > 255) ? 2 : 1;
832 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
833 } while(1);
835 return u_strcasecmp(str, cmp); /* ASCII CP */
838 /*********************************************************************
839 * _mbsncmp(MSVCRT.@)
841 int CDECL _mbsncmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
843 if(!len)
844 return 0;
846 if(get_mbcinfo()->ismbcodepage)
848 unsigned int strc, cmpc;
849 while(len--)
851 int inc;
852 if(!*str)
853 return *cmp ? -1 : 0;
854 if(!*cmp)
855 return 1;
856 strc = _mbsnextc(str);
857 cmpc = _mbsnextc(cmp);
858 if(strc != cmpc)
859 return strc < cmpc ? -1 : 1;
860 inc=(strc > 255) ? 2 : 1; /* Equal, use same increment */
861 str += inc;
862 cmp += inc;
864 return 0; /* Matched len chars */
866 return u_strncmp(str, cmp, len); /* ASCII CP */
869 /*********************************************************************
870 * _mbsnbcmp(MSVCRT.@)
872 int CDECL _mbsnbcmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
874 if (!len)
875 return 0;
876 if(get_mbcinfo()->ismbcodepage)
878 unsigned int strc, cmpc;
879 while (len)
881 int clen;
882 if(!*str)
883 return *cmp ? -1 : 0;
884 if(!*cmp)
885 return 1;
886 if (_ismbblead(*str))
888 strc=(len>=2)?_mbsnextc(str):0;
889 clen=2;
891 else
893 strc=*str;
894 clen=1;
896 if (_ismbblead(*cmp))
897 cmpc=(len>=2)?_mbsnextc(cmp):0;
898 else
899 cmpc=*cmp;
900 if(strc != cmpc)
901 return strc < cmpc ? -1 : 1;
902 len -= clen;
903 str += clen;
904 cmp += clen;
906 return 0; /* Matched len chars */
908 return u_strncmp(str,cmp,len);
911 /*********************************************************************
912 * _mbsnicmp(MSVCRT.@)
914 * Compare two multibyte strings case insensitively to 'len' characters.
916 int CDECL _mbsnicmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
918 /* FIXME: No tolower() for mb strings yet */
919 if(get_mbcinfo()->ismbcodepage)
921 unsigned int strc, cmpc;
922 while(len--)
924 if(!*str)
925 return *cmp ? -1 : 0;
926 if(!*cmp)
927 return 1;
928 strc = _mbctolower(_mbsnextc(str));
929 cmpc = _mbctolower(_mbsnextc(cmp));
930 if(strc != cmpc)
931 return strc < cmpc ? -1 : 1;
932 str +=(strc > 255) ? 2 : 1;
933 cmp +=(strc > 255) ? 2 : 1; /* Equal, use same increment */
935 return 0; /* Matched len chars */
937 return u_strncasecmp(str, cmp, len); /* ASCII CP */
940 /*********************************************************************
941 * _mbsnbicmp(MSVCRT.@)
943 int CDECL _mbsnbicmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
945 if (!len)
946 return 0;
947 if(get_mbcinfo()->ismbcodepage)
949 unsigned int strc, cmpc;
950 while (len)
952 int clen;
953 if(!*str)
954 return *cmp ? -1 : 0;
955 if(!*cmp)
956 return 1;
957 if (_ismbblead(*str))
959 strc=(len>=2)?_mbsnextc(str):0;
960 clen=2;
962 else
964 strc=*str;
965 clen=1;
967 if (_ismbblead(*cmp))
968 cmpc=(len>=2)?_mbsnextc(cmp):0;
969 else
970 cmpc=*cmp;
971 strc = _mbctolower(strc);
972 cmpc = _mbctolower(cmpc);
973 if(strc != cmpc)
974 return strc < cmpc ? -1 : 1;
975 len -= clen;
976 str += clen;
977 cmp += clen;
979 return 0; /* Matched len bytes */
981 return u_strncasecmp(str,cmp,len);
984 /*********************************************************************
985 * _mbscat (MSVCRT.@)
987 unsigned char * CDECL _mbscat( unsigned char *dst, const unsigned char *src )
989 strcat( (char *)dst, (const char *)src );
990 return dst;
993 /*********************************************************************
994 * _mbscpy (MSVCRT.@)
996 unsigned char* CDECL _mbscpy( unsigned char *dst, const unsigned char *src )
998 strcpy( (char *)dst, (const char *)src );
999 return dst;
1002 /*********************************************************************
1003 * _mbsstr (MSVCRT.@)
1005 unsigned char * CDECL _mbsstr(const unsigned char *haystack, const unsigned char *needle)
1007 return (unsigned char *)strstr( (const char *)haystack, (const char *)needle );
1010 /*********************************************************************
1011 * _mbschr(MSVCRT.@)
1013 * Find a multibyte character in a multibyte string.
1015 unsigned char* CDECL _mbschr(const unsigned char* s, unsigned int x)
1017 if(get_mbcinfo()->ismbcodepage)
1019 unsigned int c;
1020 while (1)
1022 c = _mbsnextc(s);
1023 if (c == x)
1024 return (unsigned char*)s;
1025 if (!c)
1026 return NULL;
1027 s += c > 255 ? 2 : 1;
1030 return u_strchr(s, x); /* ASCII CP */
1033 /*********************************************************************
1034 * _mbsrchr(MSVCRT.@)
1036 unsigned char* CDECL _mbsrchr(const unsigned char* s, unsigned int x)
1038 if(get_mbcinfo()->ismbcodepage)
1040 unsigned int c;
1041 unsigned char* match=NULL;
1042 if(!s)
1043 return NULL;
1044 while (1) {
1045 c = _mbsnextc(s);
1046 if (c == x)
1047 match=(unsigned char*)s;
1048 if (!c)
1049 return match;
1050 s +=(c > 255) ? 2 : 1;
1053 return u_strrchr(s, x);
1056 /*********************************************************************
1057 * _mbstok(MSVCRT.@)
1059 * Find and extract tokens from strings
1061 unsigned char* CDECL _mbstok(unsigned char *str, const unsigned char *delim)
1063 thread_data_t *data = msvcrt_get_thread_data();
1064 unsigned char *ret;
1066 if(get_mbcinfo()->ismbcodepage)
1068 unsigned int c;
1070 if (!str)
1071 if (!(str = data->mbstok_next)) return NULL;
1073 while ((c = _mbsnextc(str)) && _mbschr(delim, c)) {
1074 str += c > 255 ? 2 : 1;
1076 if (!*str) return NULL;
1077 ret = str++;
1078 while ((c = _mbsnextc(str)) && !_mbschr(delim, c)) {
1079 str += c > 255 ? 2 : 1;
1081 if (*str) {
1082 *str++ = 0;
1083 if (c > 255) *str++ = 0;
1085 data->mbstok_next = str;
1086 return ret;
1088 return u_strtok(str, delim); /* ASCII CP */
1091 /*********************************************************************
1092 * _mbbtombc(MSVCRT.@)
1094 unsigned int CDECL _mbbtombc(unsigned int c)
1096 if(get_mbcinfo()->mbcodepage == 932)
1098 if(c >= 0x20 && c <= 0x7e) {
1099 if((c >= 0x41 && c <= 0x5a) || (c >= 0x61 && c <= 0x7a) || (c >= 0x30 && c <= 0x39))
1100 return mbbtombc_932[c - 0x20] | 0x8200;
1101 else
1102 return mbbtombc_932[c - 0x20] | 0x8100;
1104 else if(c >= 0xa1 && c <= 0xdf) {
1105 if(c >= 0xa6 && c <= 0xdd && c != 0xb0)
1106 return mbbtombc_932[c - 0xa1 + 0x5f] | 0x8300;
1107 else
1108 return mbbtombc_932[c - 0xa1 + 0x5f] | 0x8100;
1111 return c; /* not Japanese or no MB char */
1114 /*********************************************************************
1115 * _mbbtype(MSVCRT.@)
1117 int CDECL _mbbtype(unsigned char c, int type)
1119 if (type == 1)
1121 if ((c >= 0x20 && c <= 0x7e) || (c >= 0xa1 && c <= 0xdf))
1122 return _MBC_SINGLE;
1123 else if ((c >= 0x40 && c <= 0x7e) || (c >= 0x80 && c <= 0xfc))
1124 return _MBC_TRAIL;
1125 else
1126 return _MBC_ILLEGAL;
1128 else
1130 if ((c >= 0x20 && c <= 0x7e) || (c >= 0xa1 && c <= 0xdf))
1131 return _MBC_SINGLE;
1132 else if ((c >= 0x81 && c <= 0x9f) || (c >= 0xe0 && c <= 0xfc))
1133 return _MBC_LEAD;
1134 else
1135 return _MBC_ILLEGAL;
1139 /*********************************************************************
1140 * _ismbbkana(MSVCRT.@)
1142 int CDECL _ismbbkana(unsigned int c)
1144 /* FIXME: use lc_ctype when supported, not lc_all */
1145 if(get_mbcinfo()->mbcodepage == 932)
1147 /* Japanese/Katakana, CP 932 */
1148 return (c >= 0xa1 && c <= 0xdf);
1150 return 0;
1153 /*********************************************************************
1154 * _ismbcdigit(MSVCRT.@)
1156 int CDECL _ismbcdigit(unsigned int ch)
1158 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1159 return (get_char_typeW( wch ) & C1_DIGIT);
1162 /*********************************************************************
1163 * _ismbcgraph(MSVCRT.@)
1165 int CDECL _ismbcgraph(unsigned int ch)
1167 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1168 return (get_char_typeW( wch ) & (C1_UPPER | C1_LOWER | C1_DIGIT | C1_PUNCT | C1_ALPHA));
1171 /*********************************************************************
1172 * _ismbcalpha (MSVCRT.@)
1174 int CDECL _ismbcalpha(unsigned int ch)
1176 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1177 return (get_char_typeW( wch ) & C1_ALPHA);
1180 /*********************************************************************
1181 * _ismbclower (MSVCRT.@)
1183 int CDECL _ismbclower(unsigned int ch)
1185 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1186 return (get_char_typeW( wch ) & C1_UPPER);
1189 /*********************************************************************
1190 * _ismbcupper (MSVCRT.@)
1192 int CDECL _ismbcupper(unsigned int ch)
1194 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1195 return (get_char_typeW( wch ) & C1_LOWER);
1198 /*********************************************************************
1199 * _ismbcsymbol(MSVCRT.@)
1201 int CDECL _ismbcsymbol(unsigned int ch)
1203 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1204 WORD ctype;
1205 if (!GetStringTypeW(CT_CTYPE3, &wch, 1, &ctype))
1207 WARN("GetStringTypeW failed on %x\n", ch);
1208 return 0;
1210 return ((ctype & C3_SYMBOL) != 0);
1213 /*********************************************************************
1214 * _ismbcalnum (MSVCRT.@)
1216 int CDECL _ismbcalnum(unsigned int ch)
1218 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1219 return (get_char_typeW( wch ) & (C1_ALPHA | C1_DIGIT));
1222 /*********************************************************************
1223 * _ismbcspace (MSVCRT.@)
1225 int CDECL _ismbcspace(unsigned int ch)
1227 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1228 return (get_char_typeW( wch ) & C1_SPACE);
1231 /*********************************************************************
1232 * _ismbcprint (MSVCRT.@)
1234 int CDECL _ismbcprint(unsigned int ch)
1236 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1237 return (get_char_typeW( wch ) & (C1_UPPER | C1_LOWER | C1_DIGIT | C1_PUNCT | C1_ALPHA | C1_SPACE));
1240 /*********************************************************************
1241 * _ismbcpunct(MSVCRT.@)
1243 int CDECL _ismbcpunct(unsigned int ch)
1245 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1246 return (get_char_typeW( wch ) & C1_PUNCT);
1249 /*********************************************************************
1250 * _ismbchira(MSVCRT.@)
1252 int CDECL _ismbchira(unsigned int c)
1254 /* FIXME: use lc_ctype when supported, not lc_all */
1255 if(get_mbcinfo()->mbcodepage == 932)
1257 /* Japanese/Hiragana, CP 932 */
1258 return (c >= 0x829f && c <= 0x82f1);
1260 return 0;
1263 /*********************************************************************
1264 * _ismbckata(MSVCRT.@)
1266 int CDECL _ismbckata(unsigned int c)
1268 /* FIXME: use lc_ctype when supported, not lc_all */
1269 if(get_mbcinfo()->mbcodepage == 932)
1271 if(c < 256)
1272 return _ismbbkana(c);
1273 /* Japanese/Katakana, CP 932 */
1274 return (c >= 0x8340 && c <= 0x8396 && c != 0x837f);
1276 return 0;
1279 /*********************************************************************
1280 * _ismbblead(MSVCRT.@)
1282 int CDECL _ismbblead(unsigned int c)
1284 return (get_mbcinfo()->mbctype[(c&0xff) + 1] & _M1) != 0;
1288 /*********************************************************************
1289 * _ismbbtrail(MSVCRT.@)
1291 int CDECL _ismbbtrail(unsigned int c)
1293 return (get_mbcinfo()->mbctype[(c&0xff) + 1] & _M2) != 0;
1296 /*********************************************************************
1297 * _ismbclegal(MSVCRT.@)
1299 int CDECL _ismbclegal(unsigned int c)
1301 return _ismbblead(HIBYTE(c)) && _ismbbtrail(LOBYTE(c));
1304 /*********************************************************************
1305 * _ismbslead(MSVCRT.@)
1307 int CDECL _ismbslead(const unsigned char* start, const unsigned char* str)
1309 int lead = 0;
1311 if(!get_mbcinfo()->ismbcodepage)
1312 return 0;
1314 /* Lead bytes can also be trail bytes so we need to analyse the string
1316 while (start <= str)
1318 if (!*start)
1319 return 0;
1320 lead = !lead && _ismbblead(*start);
1321 start++;
1324 return lead ? -1 : 0;
1327 /*********************************************************************
1328 * _ismbstrail(MSVCRT.@)
1330 int CDECL _ismbstrail(const unsigned char* start, const unsigned char* str)
1332 /* Note: this function doesn't check _ismbbtrail */
1333 if ((str > start) && _ismbslead(start, str-1))
1334 return -1;
1335 else
1336 return 0;
1339 /*********************************************************************
1340 * _mbsbtype (MSVCRT.@)
1342 int CDECL _mbsbtype(const unsigned char *str, MSVCRT_size_t count)
1344 int lead = 0;
1345 const unsigned char *end = str + count;
1347 /* Lead bytes can also be trail bytes so we need to analyse the string.
1348 * Also we must return _MBC_ILLEGAL for chars past the end of the string
1350 while (str < end) /* Note: we skip the last byte - will check after the loop */
1352 if (!*str)
1353 return _MBC_ILLEGAL;
1354 lead = get_mbcinfo()->ismbcodepage && !lead && _ismbblead(*str);
1355 str++;
1358 if (lead)
1359 if (_ismbbtrail(*str))
1360 return _MBC_TRAIL;
1361 else
1362 return _MBC_ILLEGAL;
1363 else
1364 if (_ismbblead(*str))
1365 return _MBC_LEAD;
1366 else
1367 return _MBC_SINGLE;
1370 /*********************************************************************
1371 * _mbsset(MSVCRT.@)
1373 unsigned char* CDECL _mbsset(unsigned char* str, unsigned int c)
1375 unsigned char* ret = str;
1377 if(!get_mbcinfo()->ismbcodepage || c < 256)
1378 return u__strset(str, c); /* ASCII CP or SB char */
1380 c &= 0xffff; /* Strip high bits */
1382 while(str[0] && str[1])
1384 *str++ = c >> 8;
1385 *str++ = c & 0xff;
1387 if(str[0])
1388 str[0] = '\0'; /* FIXME: OK to shorten? */
1390 return ret;
1393 /*********************************************************************
1394 * _mbsnbset(MSVCRT.@)
1396 unsigned char* CDECL _mbsnbset(unsigned char *str, unsigned int c, MSVCRT_size_t len)
1398 unsigned char *ret = str;
1400 if(!len)
1401 return ret;
1403 if(!get_mbcinfo()->ismbcodepage || c < 256)
1404 return u__strnset(str, c, len); /* ASCII CP or SB char */
1406 c &= 0xffff; /* Strip high bits */
1408 while(str[0] && str[1] && (len > 1))
1410 *str++ = c >> 8;
1411 len--;
1412 *str++ = c & 0xff;
1413 len--;
1415 if(len && str[0]) {
1416 /* as per msdn pad with a blank character */
1417 str[0] = ' ';
1420 return ret;
1423 /*********************************************************************
1424 * _mbsnset(MSVCRT.@)
1426 unsigned char* CDECL _mbsnset(unsigned char* str, unsigned int c, MSVCRT_size_t len)
1428 unsigned char *ret = str;
1430 if(!len)
1431 return ret;
1433 if(!get_mbcinfo()->ismbcodepage || c < 256)
1434 return u__strnset(str, c, len); /* ASCII CP or SB char */
1436 c &= 0xffff; /* Strip high bits */
1438 while(str[0] && str[1] && len--)
1440 *str++ = c >> 8;
1441 *str++ = c & 0xff;
1443 if(len && str[0])
1444 str[0] = '\0'; /* FIXME: OK to shorten? */
1446 return ret;
1449 /*********************************************************************
1450 * _mbsnccnt(MSVCRT.@)
1451 * 'c' is for 'character'.
1453 MSVCRT_size_t CDECL _mbsnccnt(const unsigned char* str, MSVCRT_size_t len)
1455 MSVCRT_size_t ret;
1456 if(get_mbcinfo()->ismbcodepage)
1458 ret=0;
1459 while(*str && len-- > 0)
1461 if(_ismbblead(*str))
1463 if (!len)
1464 break;
1465 len--;
1466 str++;
1468 str++;
1469 ret++;
1471 return ret;
1473 ret=u_strlen(str);
1474 return min(ret, len); /* ASCII CP */
1477 /*********************************************************************
1478 * _mbsnbcnt(MSVCRT.@)
1479 * 'b' is for byte count.
1481 MSVCRT_size_t CDECL _mbsnbcnt(const unsigned char* str, MSVCRT_size_t len)
1483 MSVCRT_size_t ret;
1484 if(get_mbcinfo()->ismbcodepage)
1486 const unsigned char* xstr = str;
1487 while(*xstr && len-- > 0)
1489 if (_ismbblead(*xstr++))
1490 xstr++;
1492 return xstr-str;
1494 ret=u_strlen(str);
1495 return min(ret, len); /* ASCII CP */
1498 /*********************************************************************
1499 * _mbsnbcat(MSVCRT.@)
1501 unsigned char* CDECL _mbsnbcat(unsigned char* dst, const unsigned char* src, MSVCRT_size_t len)
1503 if(get_mbcinfo()->ismbcodepage)
1505 unsigned char *res = dst;
1506 while (*dst) {
1507 if (_ismbblead(*dst++)) {
1508 if (*dst) {
1509 dst++;
1510 } else {
1511 /* as per msdn overwrite the lead byte in front of '\0' */
1512 dst--;
1513 break;
1517 while (*src && len--) *dst++ = *src++;
1518 *dst = '\0';
1519 return res;
1521 return u_strncat(dst, src, len); /* ASCII CP */
1524 int CDECL _mbsnbcat_s(unsigned char *dst, MSVCRT_size_t size, const unsigned char *src, MSVCRT_size_t len)
1526 unsigned char *ptr = dst;
1527 MSVCRT_size_t i;
1529 if (!dst && !size && !src && !len)
1530 return 0;
1532 if (!dst || !size || !src)
1534 if (dst && size)
1535 *dst = '\0';
1537 *MSVCRT__errno() = MSVCRT_EINVAL;
1538 return MSVCRT_EINVAL;
1541 /* Find the null terminator of the destination buffer. */
1542 while (size && *ptr)
1543 size--, ptr++;
1545 if (!size)
1547 *dst = '\0';
1548 *MSVCRT__errno() = MSVCRT_EINVAL;
1549 return MSVCRT_EINVAL;
1552 /* If necessary, check that the character preceding the null terminator is
1553 * a lead byte and move the pointer back by one for later overwrite. */
1554 if (ptr != dst && get_mbcinfo()->ismbcodepage && _ismbblead(*(ptr - 1)))
1555 size++, ptr--;
1557 for (i = 0; *src && i < len; i++)
1559 *ptr++ = *src++;
1560 size--;
1562 if (!size)
1564 *dst = '\0';
1565 *MSVCRT__errno() = MSVCRT_ERANGE;
1566 return MSVCRT_ERANGE;
1570 *ptr = '\0';
1571 return 0;
1574 /*********************************************************************
1575 * _mbsncat(MSVCRT.@)
1577 unsigned char* CDECL _mbsncat(unsigned char* dst, const unsigned char* src, MSVCRT_size_t len)
1579 if(get_mbcinfo()->ismbcodepage)
1581 unsigned char *res = dst;
1582 while (*dst)
1584 if (_ismbblead(*dst++))
1585 dst++;
1587 while (*src && len--)
1589 *dst++ = *src;
1590 if(_ismbblead(*src++))
1591 *dst++ = *src++;
1593 *dst = '\0';
1594 return res;
1596 return u_strncat(dst, src, len); /* ASCII CP */
1600 /*********************************************************************
1601 * _mbslwr(MSVCRT.@)
1603 unsigned char* CDECL _mbslwr(unsigned char* s)
1605 unsigned char *ret = s;
1606 if (!s)
1607 return NULL;
1608 if (get_mbcinfo()->ismbcodepage)
1610 unsigned int c;
1611 while (*s)
1613 c = _mbctolower(_mbsnextc(s));
1614 /* Note that I assume that the size of the character is unchanged */
1615 if (c > 255)
1617 *s++=(c>>8);
1618 c=c & 0xff;
1620 *s++=c;
1623 else for ( ; *s; s++) *s = tolower(*s);
1624 return ret;
1628 /*********************************************************************
1629 * _mbslwr_s(MSVCRT.@)
1631 int CDECL _mbslwr_s(unsigned char* s, MSVCRT_size_t len)
1633 if (!s && !len)
1635 return 0;
1637 else if (!s || !len)
1639 *MSVCRT__errno() = MSVCRT_EINVAL;
1640 return MSVCRT_EINVAL;
1642 if (get_mbcinfo()->ismbcodepage)
1644 unsigned int c;
1645 for ( ; *s && len > 0; len--)
1647 c = _mbctolower(_mbsnextc(s));
1648 /* Note that I assume that the size of the character is unchanged */
1649 if (c > 255)
1651 *s++=(c>>8);
1652 c=c & 0xff;
1654 *s++=c;
1657 else for ( ; *s && len > 0; s++, len--) *s = tolower(*s);
1658 if (*s)
1660 *s = '\0';
1661 *MSVCRT__errno() = MSVCRT_EINVAL;
1662 return MSVCRT_EINVAL;
1664 return 0;
1668 /*********************************************************************
1669 * _mbsupr(MSVCRT.@)
1671 unsigned char* CDECL _mbsupr(unsigned char* s)
1673 unsigned char *ret = s;
1674 if (!s)
1675 return NULL;
1676 if (get_mbcinfo()->ismbcodepage)
1678 unsigned int c;
1679 while (*s)
1681 c = _mbctoupper(_mbsnextc(s));
1682 /* Note that I assume that the size of the character is unchanged */
1683 if (c > 255)
1685 *s++=(c>>8);
1686 c=c & 0xff;
1688 *s++=c;
1691 else for ( ; *s; s++) *s = toupper(*s);
1692 return ret;
1696 /*********************************************************************
1697 * _mbsupr_s(MSVCRT.@)
1699 int CDECL _mbsupr_s(unsigned char* s, MSVCRT_size_t len)
1701 if (!s && !len)
1703 return 0;
1705 else if (!s || !len)
1707 *MSVCRT__errno() = MSVCRT_EINVAL;
1708 return MSVCRT_EINVAL;
1710 if (get_mbcinfo()->ismbcodepage)
1712 unsigned int c;
1713 for ( ; *s && len > 0; len--)
1715 c = _mbctoupper(_mbsnextc(s));
1716 /* Note that I assume that the size of the character is unchanged */
1717 if (c > 255)
1719 *s++=(c>>8);
1720 c=c & 0xff;
1722 *s++=c;
1725 else for ( ; *s && len > 0; s++, len--) *s = toupper(*s);
1726 if (*s)
1728 *s = '\0';
1729 *MSVCRT__errno() = MSVCRT_EINVAL;
1730 return MSVCRT_EINVAL;
1732 return 0;
1736 /*********************************************************************
1737 * _mbsspn (MSVCRT.@)
1739 MSVCRT_size_t CDECL _mbsspn(const unsigned char* string, const unsigned char* set)
1741 const unsigned char *p, *q;
1743 for (p = string; *p; p++)
1745 if (_ismbblead(*p))
1747 for (q = set; *q; q++)
1749 if (!q[1])
1750 break;
1751 if ((*p == *q) && (p[1] == q[1]))
1752 break;
1753 q++;
1755 if (!q[0] || !q[1]) break;
1757 else
1759 for (q = set; *q; q++)
1760 if (*p == *q)
1761 break;
1762 if (!*q) break;
1765 return p - string;
1768 /*********************************************************************
1769 * _mbsspnp (MSVCRT.@)
1771 unsigned char* CDECL _mbsspnp(const unsigned char* string, const unsigned char* set)
1773 const unsigned char *p, *q;
1775 for (p = string; *p; p++)
1777 if (_ismbblead(*p))
1779 for (q = set; *q; q++)
1781 if (!q[1])
1782 break;
1783 if ((*p == *q) && (p[1] == q[1]))
1784 break;
1785 q++;
1787 if (!q[0] || !q[1]) break;
1789 else
1791 for (q = set; *q; q++)
1792 if (*p == *q)
1793 break;
1794 if (!*q) break;
1797 if (*p == '\0')
1798 return NULL;
1799 return (unsigned char *)p;
1802 /*********************************************************************
1803 * _mbscspn(MSVCRT.@)
1805 MSVCRT_size_t CDECL _mbscspn(const unsigned char* str, const unsigned char* cmp)
1807 if (get_mbcinfo()->ismbcodepage)
1808 FIXME("don't handle double character case\n");
1809 return u_strcspn(str, cmp);
1812 /*********************************************************************
1813 * _mbsrev (MSVCRT.@)
1815 unsigned char* CDECL _mbsrev(unsigned char* str)
1817 int i, len = _mbslen(str);
1818 unsigned char *p, *temp=MSVCRT_malloc(len*2);
1820 if(!temp)
1821 return str;
1823 /* unpack multibyte string to temp buffer */
1824 p=str;
1825 for(i=0; i<len; i++)
1827 if (_ismbblead(*p))
1829 temp[i*2]=*p++;
1830 temp[i*2+1]=*p++;
1832 else
1834 temp[i*2]=*p++;
1835 temp[i*2+1]=0;
1839 /* repack it in the reverse order */
1840 p=str;
1841 for(i=len-1; i>=0; i--)
1843 if(_ismbblead(temp[i*2]))
1845 *p++=temp[i*2];
1846 *p++=temp[i*2+1];
1848 else
1850 *p++=temp[i*2];
1854 MSVCRT_free(temp);
1856 return str;
1859 /*********************************************************************
1860 * _mbspbrk (MSVCRT.@)
1862 unsigned char* CDECL _mbspbrk(const unsigned char* str, const unsigned char* accept)
1864 const unsigned char* p;
1866 while(*str)
1868 for(p = accept; *p; p += (_ismbblead(*p)?2:1) )
1870 if (*p == *str)
1871 if( !_ismbblead(*p) || ( *(p+1) == *(str+1) ) )
1872 return (unsigned char*)str;
1874 str += (_ismbblead(*str)?2:1);
1876 return NULL;
1881 * Functions depending on locale codepage
1884 /*********************************************************************
1885 * mblen(MSVCRT.@)
1886 * REMARKS
1887 * Unlike most of the multibyte string functions this function uses
1888 * the locale codepage, not the codepage set by _setmbcp
1890 int CDECL MSVCRT_mblen(const char* str, MSVCRT_size_t size)
1892 if (str && *str && size)
1894 if(get_locinfo()->mb_cur_max == 1)
1895 return 1; /* ASCII CP */
1897 return !MSVCRT_isleadbyte(*str) ? 1 : (size>1 ? 2 : -1);
1899 return 0;
1902 /*********************************************************************
1903 * _mbstrlen_l(MSVCRT.@)
1905 MSVCRT_size_t CDECL _mbstrlen_l(const char* str, MSVCRT__locale_t locale)
1907 MSVCRT_pthreadlocinfo locinfo;
1909 if(!locale)
1910 locinfo = get_locinfo();
1911 else
1912 locinfo = locale->locinfo;
1914 if(locinfo->mb_cur_max > 1) {
1915 MSVCRT_size_t len;
1916 len = MultiByteToWideChar(locinfo->lc_codepage, MB_ERR_INVALID_CHARS,
1917 str, -1, NULL, 0);
1918 if (!len) {
1919 *MSVCRT__errno() = MSVCRT_EILSEQ;
1920 return -1;
1922 return len - 1;
1925 return strlen(str);
1928 /*********************************************************************
1929 * _mbstrlen(MSVCRT.@)
1931 MSVCRT_size_t CDECL _mbstrlen(const char* str)
1933 return _mbstrlen_l(str, NULL);
1936 /*********************************************************************
1937 * _mbtowc_l(MSVCRT.@)
1939 int CDECL MSVCRT_mbtowc_l(MSVCRT_wchar_t *dst, const char* str, MSVCRT_size_t n, MSVCRT__locale_t locale)
1941 MSVCRT_pthreadlocinfo locinfo;
1942 MSVCRT_wchar_t tmpdst = '\0';
1944 if(!locale)
1945 locinfo = get_locinfo();
1946 else
1947 locinfo = locale->locinfo;
1949 if(n <= 0 || !str)
1950 return 0;
1951 if(!MultiByteToWideChar(locinfo->lc_codepage, 0, str, n, &tmpdst, 1))
1952 return -1;
1953 if(dst)
1954 *dst = tmpdst;
1955 /* return the number of bytes from src that have been used */
1956 if(!*str)
1957 return 0;
1958 if(n >= 2 && MSVCRT__isleadbyte_l(*str, locale) && str[1])
1959 return 2;
1960 return 1;
1963 /*********************************************************************
1964 * mbtowc(MSVCRT.@)
1966 int CDECL MSVCRT_mbtowc(MSVCRT_wchar_t *dst, const char* str, MSVCRT_size_t n)
1968 return MSVCRT_mbtowc_l(dst, str, n, NULL);
1971 /*********************************************************************
1972 * _mbstowcs_l(MSVCRT.@)
1974 MSVCRT_size_t CDECL MSVCRT__mbstowcs_l(MSVCRT_wchar_t *wcstr, const char *mbstr,
1975 MSVCRT_size_t count, MSVCRT__locale_t locale)
1977 MSVCRT_pthreadlocinfo locinfo;
1978 MSVCRT_size_t i, size;
1980 if(!locale)
1981 locinfo = get_locinfo();
1982 else
1983 locinfo = locale->locinfo;
1985 /* Ignore count parameter */
1986 if(!wcstr)
1987 return MultiByteToWideChar(locinfo->lc_codepage, 0, mbstr, -1, NULL, 0)-1;
1989 for(i=0, size=0; i<count; i++) {
1990 if(mbstr[size] == '\0')
1991 break;
1993 size += (MSVCRT__isleadbyte_l(mbstr[size], locale) ? 2 : 1);
1996 size = MultiByteToWideChar(locinfo->lc_codepage, 0,
1997 mbstr, size, wcstr, count);
1999 if(size<count && wcstr)
2000 wcstr[size] = '\0';
2002 return size;
2005 /*********************************************************************
2006 * mbstowcs(MSVCRT.@)
2008 MSVCRT_size_t CDECL MSVCRT_mbstowcs(MSVCRT_wchar_t *wcstr,
2009 const char *mbstr, MSVCRT_size_t count)
2011 return MSVCRT__mbstowcs_l(wcstr, mbstr, count, NULL);
2014 /*********************************************************************
2015 * _mbstowcs_s_l(MSVCRT.@)
2017 int CDECL MSVCRT__mbstowcs_s_l(MSVCRT_size_t *ret, MSVCRT_wchar_t *wcstr,
2018 MSVCRT_size_t size, const char *mbstr,
2019 MSVCRT_size_t count, MSVCRT__locale_t locale)
2021 MSVCRT_size_t conv;
2023 if(!wcstr && !size) {
2024 conv = MSVCRT__mbstowcs_l(NULL, mbstr, 0, locale);
2025 if(ret)
2026 *ret = conv+1;
2027 return 0;
2030 if(!MSVCRT_CHECK_PMT(mbstr != NULL) || !MSVCRT_CHECK_PMT(wcstr != NULL)) {
2031 if(wcstr && size)
2032 wcstr[0] = '\0';
2033 *MSVCRT__errno() = MSVCRT_EINVAL;
2034 return MSVCRT_EINVAL;
2037 if(count==MSVCRT__TRUNCATE || size<count)
2038 conv = size;
2039 else
2040 conv = count;
2042 conv = MSVCRT__mbstowcs_l(wcstr, mbstr, conv, locale);
2043 if(conv<size)
2044 wcstr[conv++] = '\0';
2045 else if(conv==size && (count==MSVCRT__TRUNCATE || wcstr[conv-1]=='\0'))
2046 wcstr[conv-1] = '\0';
2047 else {
2048 MSVCRT_INVALID_PMT("wcstr[size] is too small");
2049 if(size)
2050 wcstr[0] = '\0';
2051 *MSVCRT__errno() = MSVCRT_ERANGE;
2052 return MSVCRT_ERANGE;
2055 if(ret)
2056 *ret = conv;
2057 return 0;
2060 /*********************************************************************
2061 * mbstowcs_s(MSVCRT.@)
2063 int CDECL MSVCRT__mbstowcs_s(MSVCRT_size_t *ret, MSVCRT_wchar_t *wcstr,
2064 MSVCRT_size_t size, const char *mbstr, MSVCRT_size_t count)
2066 return MSVCRT__mbstowcs_s_l(ret, wcstr, size, mbstr, count, NULL);