wined3d: Remove usage of gl_info parameter from select_card_amd_binary.
[wine.git] / dlls / msvcrt / mbcs.c
blobd2c12d3821e94fe5956a3e6455df7a73facedfdb
1 /*
2 * msvcrt.dll mbcs functions
4 * Copyright 1999 Alexandre Julliard
5 * Copyright 2000 Jon Griffths
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 * FIXME
22 * Not currently binary compatible with win32. MSVCRT_mbctype must be
23 * populated correctly and the ismb* functions should reference it.
26 #include <stdio.h>
28 #include "msvcrt.h"
29 #include "wine/unicode.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
34 unsigned char MSVCRT_mbctype[257] = { 0 };
36 /* It seems that the data about valid trail bytes is not available from kernel32
37 * so we have to store is here. The format is the same as for lead bytes in CPINFO */
38 struct cp_extra_info_t
40 int cp;
41 BYTE TrailBytes[MAX_LEADBYTES];
44 static struct cp_extra_info_t g_cpextrainfo[] =
46 {932, {0x40, 0x7e, 0x80, 0xfc, 0, 0}},
47 {936, {0x40, 0xfe, 0, 0}},
48 {949, {0x41, 0xfe, 0, 0}},
49 {950, {0x40, 0x7e, 0xa1, 0xfe, 0, 0}},
50 {1361, {0x31, 0x7e, 0x81, 0xfe, 0, 0}},
51 {20932, {1, 255, 0, 0}}, /* seems to give different results on different systems */
52 {0, {1, 255, 0, 0}} /* match all with FIXME */
55 /* Maps cp932 single byte character to multi byte character */
56 static const unsigned char mbbtombc_932[] = {
57 0x40,0x49,0x68,0x94,0x90,0x93,0x95,0x66,0x69,0x6a,0x96,0x7b,0x43,0x7c,0x44,0x5e,
58 0x4f,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x46,0x47,0x83,0x81,0x84,0x48,
59 0x97,0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,
60 0x6f,0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x6d,0x8f,0x6e,0x4f,0x76,
61 0x77,0x78,0x79,0x6d,0x8f,0x6e,0x4f,0x51,0x65,0x81,0x82,0x83,0x84,0x85,0x86,0x87,
62 0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f,0x90,0x91,0x92,0x93,0x94,0x95,0x50,
63 0x42,0x75,0x76,0x41,0x45,0x92,0x40,0x42,0x44,0x46,0x48,0x83,0x85,0x87,0x62,
64 0x5b,0x41,0x43,0x45,0x47,0x49,0x4a,0x4c,0x4e,0x50,0x52,0x54,0x56,0x58,0x5a,0x5c,
65 0x5e,0x60,0x63,0x65,0x67,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x71,0x74,0x77,0x7a,0x7d,
66 0x7e,0x80,0x81,0x82,0x84,0x86,0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8f,0x93,0x4a,0x4b };
68 /* Maps multibyte cp932 punctuation marks to single byte equivalents */
69 static const unsigned char mbctombb_932_punct[] = {
70 0x20,0xa4,0xa1,0x2c,0x2e,0xa5,0x3a,0x3b,0x3f,0x21,0xde,0xdf,0x00,0x00,0x00,0x5e,
71 0x7e,0x5f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xb0,0x00,0x00,0x2f,0x00,
72 0x00,0x00,0x7c,0x00,0x00,0x60,0x27,0x00,0x22,0x28,0x29,0x00,0x00,0x5b,0x5d,0x7b,
73 0x7d,0x00,0x00,0x00,0x00,0xa2,0xa3,0x00,0x00,0x00,0x00,0x2b,0x2d,0x00,0x00,0x00,
74 0x00,0x3d,0x00,0x3c,0x3e,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x5c,
75 0x24,0x00,0x00,0x25,0x23,0x26,0x2a,0x40};
77 /* Maps multibyte cp932 hiragana/katakana to single-byte equivalents */
78 static const unsigned char mbctombb_932_kana[] = {
79 0xa7,0xb1,0xa8,0xb2,0xa9,0xb3,0xaa,0xb4,0xab,0xb5,0xb6,0xb6,0xb7,0xb7,0xb8,0xb8,
80 0xb9,0xb9,0xba,0xba,0xbb,0xbb,0xbc,0xbc,0xbd,0xbd,0xbe,0xbe,0xbf,0xbf,0xc0,0xc0,
81 0xc1,0xc1,0xaf,0xc2,0xc2,0xc3,0xc3,0xc4,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xca,
82 0xca,0xcb,0xcb,0xcb,0xcc,0xcc,0xcc,0xcd,0xcd,0xcd,0xce,0xce,0xce,0xcf,0xd0,0xd1,
83 0xd2,0xd3,0xac,0xd4,0xad,0xd5,0xae,0xd6,0xd7,0xd8,0xd9,0xda,0xdb,0xdc,0xdc,0xb2,
84 0xb4,0xa6,0xdd,0xb3,0xb6,0xb9};
86 static MSVCRT_wchar_t msvcrt_mbc_to_wc(unsigned int ch)
88 MSVCRT_wchar_t chW;
89 char mbch[2];
90 int n_chars;
92 if (ch <= 0xff) {
93 mbch[0] = ch;
94 n_chars = 1;
95 } else {
96 mbch[0] = (ch >> 8) & 0xff;
97 mbch[1] = ch & 0xff;
98 n_chars = 2;
100 if (!MultiByteToWideChar(get_mbcinfo()->mbcodepage, 0, mbch, n_chars, &chW, 1))
102 WARN("MultiByteToWideChar failed on %x\n", ch);
103 return 0;
105 return chW;
108 static inline MSVCRT_size_t u_strlen( const unsigned char *str )
110 return strlen( (const char*) str );
113 static inline unsigned char* u_strncat( unsigned char* dst, const unsigned char* src, MSVCRT_size_t len )
115 return (unsigned char*)strncat( (char*)dst, (const char*)src, len);
118 static inline int u_strcmp( const unsigned char *s1, const unsigned char *s2 )
120 return strcmp( (const char*)s1, (const char*)s2 );
123 static inline int u_strcasecmp( const unsigned char *s1, const unsigned char *s2 )
125 return strcasecmp( (const char*)s1, (const char*)s2 );
128 static inline int u_strncmp( const unsigned char *s1, const unsigned char *s2, MSVCRT_size_t len )
130 return strncmp( (const char*)s1, (const char*)s2, len );
133 static inline int u_strncasecmp( const unsigned char *s1, const unsigned char *s2, MSVCRT_size_t len )
135 return strncasecmp( (const char*)s1, (const char*)s2, len );
138 static inline unsigned char *u_strchr( const unsigned char *s, unsigned char x )
140 return (unsigned char*) strchr( (const char*)s, x );
143 static inline unsigned char *u_strrchr( const unsigned char *s, unsigned char x )
145 return (unsigned char*) strrchr( (const char*)s, x );
148 static inline unsigned char *u__strset( unsigned char *s, unsigned char c )
150 return (unsigned char*) _strset( (char*)s, c);
153 static inline unsigned char *u__strnset( unsigned char *s, unsigned char c, MSVCRT_size_t len )
155 return (unsigned char*) MSVCRT__strnset( (char*)s, c, len );
158 static inline MSVCRT_size_t u_strcspn( const unsigned char *s, const unsigned char *rej )
160 return strcspn( (const char *)s, (const char*)rej );
163 /*********************************************************************
164 * __p__mbctype (MSVCRT.@)
166 unsigned char* CDECL __p__mbctype(void)
168 return get_mbcinfo()->mbctype;
171 /*********************************************************************
172 * __p___mb_cur_max(MSVCRT.@)
174 int* CDECL __p___mb_cur_max(void)
176 return &get_locinfo()->mb_cur_max;
179 /*********************************************************************
180 * ___mb_cur_max_func(MSVCRT.@)
182 int CDECL MSVCRT____mb_cur_max_func(void)
184 return get_locinfo()->mb_cur_max;
187 /*********************************************************************
188 * ___mb_cur_max_l_func (MSVCR80.@)
190 int* CDECL ___mb_cur_max_l_func(MSVCRT__locale_t locale)
192 MSVCRT_pthreadlocinfo locinfo;
194 if(!locale)
195 locinfo = get_locinfo();
196 else
197 locinfo = locale->locinfo;
199 return &locinfo->mb_cur_max;
202 /*********************************************************************
203 * INTERNAL: _setmbcp_l
205 int _setmbcp_l(int cp, LCID lcid, MSVCRT_pthreadmbcinfo mbcinfo)
207 const char format[] = ".%d";
209 int newcp;
210 CPINFO cpi;
211 BYTE *bytes;
212 WORD chartypes[256];
213 char bufA[256];
214 WCHAR bufW[256];
215 int charcount;
216 int ret;
217 int i;
219 if(!mbcinfo)
220 mbcinfo = get_mbcinfo();
222 switch (cp)
224 case _MB_CP_ANSI:
225 newcp = GetACP();
226 break;
227 case _MB_CP_OEM:
228 newcp = GetOEMCP();
229 break;
230 case _MB_CP_LOCALE:
231 newcp = get_locinfo()->lc_codepage;
232 if(newcp)
233 break;
234 /* fall through (C locale) */
235 case _MB_CP_SBCS:
236 newcp = 20127; /* ASCII */
237 break;
238 default:
239 newcp = cp;
240 break;
243 if(lcid == -1) {
244 sprintf(bufA, format, newcp);
245 mbcinfo->mblcid = MSVCRT_locale_to_LCID(bufA, NULL);
246 } else {
247 mbcinfo->mblcid = lcid;
250 if(mbcinfo->mblcid == -1)
252 WARN("Can't assign LCID to codepage (%d)\n", mbcinfo->mblcid);
253 mbcinfo->mblcid = 0;
256 if (!GetCPInfo(newcp, &cpi))
258 WARN("Codepage %d not found\n", newcp);
259 *MSVCRT__errno() = MSVCRT_EINVAL;
260 return -1;
263 /* setup the _mbctype */
264 memset(mbcinfo->mbctype, 0, sizeof(unsigned char[257]));
265 memset(mbcinfo->mbcasemap, 0, sizeof(unsigned char[256]));
267 bytes = cpi.LeadByte;
268 while (bytes[0] || bytes[1])
270 for (i = bytes[0]; i <= bytes[1]; i++)
271 mbcinfo->mbctype[i + 1] |= _M1;
272 bytes += 2;
275 if (cpi.MaxCharSize > 1)
277 /* trail bytes not available through kernel32 but stored in a structure in msvcrt */
278 struct cp_extra_info_t *cpextra = g_cpextrainfo;
280 mbcinfo->ismbcodepage = 1;
281 while (TRUE)
283 if (cpextra->cp == 0 || cpextra->cp == newcp)
285 if (cpextra->cp == 0)
286 FIXME("trail bytes data not available for DBCS codepage %d - assuming all bytes\n", newcp);
288 bytes = cpextra->TrailBytes;
289 while (bytes[0] || bytes[1])
291 for (i = bytes[0]; i <= bytes[1]; i++)
292 mbcinfo->mbctype[i + 1] |= _M2;
293 bytes += 2;
295 break;
297 cpextra++;
300 else
301 mbcinfo->ismbcodepage = 0;
303 /* we can't use GetStringTypeA directly because we don't have a locale - only a code page
305 charcount = 0;
306 for (i = 0; i < 256; i++)
307 if (!(mbcinfo->mbctype[i + 1] & _M1))
308 bufA[charcount++] = i;
310 ret = MultiByteToWideChar(newcp, 0, bufA, charcount, bufW, charcount);
311 if (ret != charcount)
312 ERR("MultiByteToWideChar of chars failed for cp %d, ret=%d (exp %d), error=%d\n", newcp, ret, charcount, GetLastError());
314 GetStringTypeW(CT_CTYPE1, bufW, charcount, chartypes);
316 charcount = 0;
317 for (i = 0; i < 256; i++)
318 if (!(mbcinfo->mbctype[i + 1] & _M1))
320 if (chartypes[charcount] & C1_UPPER)
322 mbcinfo->mbctype[i + 1] |= _SBUP;
323 bufW[charcount] = tolowerW(bufW[charcount]);
325 else if (chartypes[charcount] & C1_LOWER)
327 mbcinfo->mbctype[i + 1] |= _SBLOW;
328 bufW[charcount] = toupperW(bufW[charcount]);
330 charcount++;
333 ret = WideCharToMultiByte(newcp, 0, bufW, charcount, bufA, charcount, NULL, NULL);
334 if (ret != charcount)
335 ERR("WideCharToMultiByte failed for cp %d, ret=%d (exp %d), error=%d\n", newcp, ret, charcount, GetLastError());
337 charcount = 0;
338 for (i = 0; i < 256; i++)
340 if(!(mbcinfo->mbctype[i + 1] & _M1))
342 if(mbcinfo->mbctype[i] & (C1_UPPER|C1_LOWER))
343 mbcinfo->mbcasemap[i] = bufA[charcount];
344 charcount++;
348 if (newcp == 932) /* CP932 only - set _MP and _MS */
350 /* On Windows it's possible to calculate the _MP and _MS from CT_CTYPE1
351 * and CT_CTYPE3. But as of Wine 0.9.43 we return wrong values what makes
352 * it hard. As this is set only for codepage 932 we hardcode it what gives
353 * also faster execution.
355 for (i = 161; i <= 165; i++)
356 mbcinfo->mbctype[i + 1] |= _MP;
357 for (i = 166; i <= 223; i++)
358 mbcinfo->mbctype[i + 1] |= _MS;
361 mbcinfo->mbcodepage = newcp;
362 if(MSVCRT_locale && mbcinfo == MSVCRT_locale->mbcinfo)
363 memcpy(MSVCRT_mbctype, MSVCRT_locale->mbcinfo->mbctype, sizeof(MSVCRT_mbctype));
365 return 0;
368 /*********************************************************************
369 * _setmbcp (MSVCRT.@)
371 int CDECL _setmbcp(int cp)
373 return _setmbcp_l(cp, -1, NULL);
376 /*********************************************************************
377 * _getmbcp (MSVCRT.@)
379 int CDECL _getmbcp(void)
381 return get_mbcinfo()->mbcodepage;
384 /*********************************************************************
385 * _mbsnextc(MSVCRT.@)
387 unsigned int CDECL _mbsnextc(const unsigned char* str)
389 if(_ismbblead(*str))
390 return *str << 8 | str[1];
391 return *str;
394 /*********************************************************************
395 * _mbctolower(MSVCRT.@)
397 unsigned int CDECL _mbctolower(unsigned int c)
399 if (_ismbblead(c))
401 FIXME("Handle MBC chars\n");
402 return c;
404 return tolower(c); /* ASCII CP or SB char */
407 /*********************************************************************
408 * _mbctoupper(MSVCRT.@)
410 unsigned int CDECL _mbctoupper(unsigned int c)
412 if (_ismbblead(c))
414 FIXME("Handle MBC chars\n");
415 return c;
417 return toupper(c); /* ASCII CP or SB char */
420 /*********************************************************************
421 * _mbctombb (MSVCRT.@)
423 unsigned int CDECL _mbctombb(unsigned int c)
425 unsigned int value;
427 if(get_mbcinfo()->mbcodepage == 932)
429 if(c >= 0x829f && c <= 0x82f1) /* Hiragana */
430 return mbctombb_932_kana[c - 0x829f];
431 if(c >= 0x8340 && c <= 0x8396 && c != 0x837f) /* Katakana */
432 return mbctombb_932_kana[c - 0x8340 - (c >= 0x837f ? 1 : 0)];
433 if(c >= 0x8140 && c <= 0x8197) /* Punctuation */
435 value = mbctombb_932_punct[c - 0x8140];
436 return value ? value : c;
438 if((c >= 0x824f && c <= 0x8258) || /* Fullwidth digits */
439 (c >= 0x8260 && c <= 0x8279)) /* Fullwidth capitals letters */
440 return c - 0x821f;
441 if(c >= 0x8281 && c <= 0x829a) /* Fullwidth small letters */
442 return c - 0x8220;
443 /* all other cases return c */
445 return c;
448 /*********************************************************************
449 * _mbcjistojms(MSVCRT.@)
451 * Converts a jis character to sjis.
452 * Based on description from
453 * http://www.slayers.ne.jp/~oouchi/code/jistosjis.html
455 unsigned int CDECL _mbcjistojms(unsigned int c)
457 /* Conversion takes place only when codepage is 932.
458 In all other cases, c is returned unchanged */
459 if(get_mbcinfo()->mbcodepage == 932)
461 if(HIBYTE(c) >= 0x21 && HIBYTE(c) <= 0x7e &&
462 LOBYTE(c) >= 0x21 && LOBYTE(c) <= 0x7e)
464 if(HIBYTE(c) % 2)
465 c += 0x1f;
466 else
467 c += 0x7d;
469 if(LOBYTE(c) >= 0x7F)
470 c += 0x1;
472 c = (((HIBYTE(c) - 0x21)/2 + 0x81) << 8) | LOBYTE(c);
474 if(HIBYTE(c) > 0x9f)
475 c += 0x4000;
477 else
478 return 0; /* Codepage is 932, but c can't be converted */
481 return c;
484 /*********************************************************************
485 * _mbcjmstojis(MSVCRT.@)
487 * Converts a sjis character to jis.
489 unsigned int CDECL _mbcjmstojis(unsigned int c)
491 /* Conversion takes place only when codepage is 932.
492 In all other cases, c is returned unchanged */
493 if(get_mbcinfo()->mbcodepage == 932)
495 if(_ismbclegal(c) && HIBYTE(c) < 0xf0)
497 if(HIBYTE(c) >= 0xe0)
498 c -= 0x4000;
500 c = (((HIBYTE(c) - 0x81)*2 + 0x21) << 8) | LOBYTE(c);
502 if(LOBYTE(c) > 0x7f)
503 c -= 0x1;
505 if(LOBYTE(c) > 0x9d)
506 c += 0x83;
507 else
508 c -= 0x1f;
510 else
511 return 0; /* Codepage is 932, but c can't be converted */
514 return c;
517 /*********************************************************************
518 * _mbsdec(MSVCRT.@)
520 unsigned char* CDECL _mbsdec(const unsigned char* start, const unsigned char* cur)
522 if(get_mbcinfo()->ismbcodepage)
523 return (unsigned char *)(_ismbstrail(start,cur-1) ? cur - 2 : cur -1);
525 return (unsigned char *)cur - 1; /* ASCII CP or SB char */
528 /*********************************************************************
529 * _mbclen(MSVCRT.@)
531 unsigned int CDECL _mbclen(const unsigned char* str)
533 return _ismbblead(*str) ? 2 : 1;
536 /*********************************************************************
537 * _mbsinc(MSVCRT.@)
539 unsigned char* CDECL _mbsinc(const unsigned char* str)
541 return (unsigned char *)(str + _mbclen(str));
544 /*********************************************************************
545 * _mbsninc(MSVCRT.@)
547 unsigned char* CDECL _mbsninc(const unsigned char* str, MSVCRT_size_t num)
549 if(!str)
550 return NULL;
552 while (num > 0 && *str)
554 if (_ismbblead(*str))
556 if (!*(str+1))
557 break;
558 str++;
560 str++;
561 num--;
564 return (unsigned char*)str;
567 /*********************************************************************
568 * _mbslen(MSVCRT.@)
570 MSVCRT_size_t CDECL _mbslen(const unsigned char* str)
572 MSVCRT_size_t len = 0;
573 while(*str)
575 if (_ismbblead(*str))
577 str++;
578 if (!*str) /* count only full chars */
579 break;
581 str++;
582 len++;
584 return len;
587 /*********************************************************************
588 * _mbccpy(MSVCRT.@)
590 void CDECL _mbccpy(unsigned char* dest, const unsigned char* src)
592 *dest = *src;
593 if(_ismbblead(*src))
594 *++dest = *++src; /* MB char */
597 /*********************************************************************
598 * _mbsncpy(MSVCRT.@)
599 * REMARKS
600 * The parameter n is the number or characters to copy, not the size of
601 * the buffer. Use _mbsnbcpy for a function analogical to strncpy
603 unsigned char* CDECL _mbsncpy(unsigned char* dst, const unsigned char* src, MSVCRT_size_t n)
605 unsigned char* ret = dst;
606 if(!n)
607 return dst;
608 if (get_mbcinfo()->ismbcodepage)
610 while (*src && n)
612 n--;
613 if (_ismbblead(*src))
615 if (!*(src+1))
617 *dst++ = 0;
618 *dst++ = 0;
619 break;
622 *dst++ = *src++;
625 *dst++ = *src++;
628 else
630 while (n)
632 n--;
633 if (!(*dst++ = *src++)) break;
636 while (n--) *dst++ = 0;
637 return ret;
640 /*********************************************************************
641 * _mbsnbcpy_s_l(MSVCRT.@)
642 * REMARKS
643 * Unlike _mbsnbcpy this function does not pad the rest of the dest
644 * string with 0
646 int CDECL _mbsnbcpy_s_l(unsigned char* dst, MSVCRT_size_t size,
647 const unsigned char* src, MSVCRT_size_t n, MSVCRT__locale_t locale)
649 MSVCRT_size_t pos = 0;
651 if(!dst || size == 0)
652 return MSVCRT_EINVAL;
653 if(!src)
655 dst[0] = '\0';
656 return MSVCRT_EINVAL;
658 if(!n)
659 return 0;
661 if((locale ? locale->mbcinfo : get_mbcinfo())->ismbcodepage)
663 int is_lead = 0;
664 while (*src && n)
666 if(pos == size)
668 dst[0] = '\0';
669 return MSVCRT_ERANGE;
671 is_lead = (!is_lead && _ismbblead(*src));
672 n--;
673 dst[pos++] = *src++;
676 if (is_lead) /* if string ends with a lead, remove it */
677 dst[pos - 1] = 0;
679 else
681 while (n)
683 n--;
684 if(pos == size)
686 dst[0] = '\0';
687 return MSVCRT_ERANGE;
690 if(!(*src)) break;
691 dst[pos++] = *src++;
695 if(pos < size)
696 dst[pos] = '\0';
697 else
699 dst[0] = '\0';
700 return MSVCRT_ERANGE;
703 return 0;
706 /*********************************************************************
707 * _mbsnbcpy_s(MSVCRT.@)
709 int CDECL _mbsnbcpy_s(unsigned char* dst, MSVCRT_size_t size, const unsigned char* src, MSVCRT_size_t n)
711 return _mbsnbcpy_s_l(dst, size, src, n, NULL);
714 /*********************************************************************
715 * _mbscpy_s_l(MSVCRT.@)
717 int CDECL _mbscpy_s_l(unsigned char *dst, MSVCRT_size_t size,
718 const unsigned char *src, MSVCRT__locale_t locale)
720 return _mbsnbcpy_s_l(dst, size, src, -1, locale);
723 /*********************************************************************
724 * _mbscpy_s(MSVCRT.@)
726 int CDECL _mbscpy_s(unsigned char *dst, MSVCRT_size_t size, const unsigned char *src)
728 return _mbscpy_s_l(dst, size, src, NULL);
731 /*********************************************************************
732 * _mbsnbcpy(MSVCRT.@)
733 * REMARKS
734 * Like strncpy this function doesn't enforce the string to be
735 * NUL-terminated
737 unsigned char* CDECL _mbsnbcpy(unsigned char* dst, const unsigned char* src, MSVCRT_size_t n)
739 unsigned char* ret = dst;
740 if(!n)
741 return dst;
742 if(get_mbcinfo()->ismbcodepage)
744 int is_lead = 0;
745 while (*src && n)
747 is_lead = (!is_lead && _ismbblead(*src));
748 n--;
749 *dst++ = *src++;
752 if (is_lead) /* if string ends with a lead, remove it */
753 *(dst - 1) = 0;
755 else
757 while (n)
759 n--;
760 if (!(*dst++ = *src++)) break;
763 while (n--) *dst++ = 0;
764 return ret;
767 /*********************************************************************
768 * _mbscmp(MSVCRT.@)
770 int CDECL _mbscmp(const unsigned char* str, const unsigned char* cmp)
772 if(get_mbcinfo()->ismbcodepage)
774 unsigned int strc, cmpc;
775 do {
776 if(!*str)
777 return *cmp ? -1 : 0;
778 if(!*cmp)
779 return 1;
780 strc = _mbsnextc(str);
781 cmpc = _mbsnextc(cmp);
782 if(strc != cmpc)
783 return strc < cmpc ? -1 : 1;
784 str +=(strc > 255) ? 2 : 1;
785 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
786 } while(1);
788 return u_strcmp(str, cmp); /* ASCII CP */
791 /*********************************************************************
792 * _mbsnbicoll_l(MSVCRT.@)
794 int CDECL _mbsnbicoll_l(const unsigned char *str1, const unsigned char *str2, MSVCRT_size_t len, MSVCRT__locale_t locale)
796 MSVCRT_pthreadmbcinfo mbcinfo;
798 if(!locale)
799 mbcinfo = get_mbcinfo();
800 else
801 mbcinfo = locale->mbcinfo;
803 if(!mbcinfo->ismbcodepage)
804 return MSVCRT__strnicoll_l((const char*)str1, (const char*)str2, len, locale);
805 return CompareStringA(mbcinfo->mblcid, NORM_IGNORECASE, (const char*)str1, len, (const char*)str2, len)-CSTR_EQUAL;
808 /*********************************************************************
809 * _mbsicoll_l(MSVCRT.@)
811 int CDECL _mbsicoll_l(const unsigned char *str1, const unsigned char *str2, MSVCRT__locale_t locale)
813 return _mbsnbicoll_l(str1, str2, -1, locale);
816 /*********************************************************************
817 * _mbsnbicoll(MSVCRT.@)
819 int CDECL _mbsnbicoll(const unsigned char *str1, const unsigned char *str2, MSVCRT_size_t len)
821 return _mbsnbicoll_l(str1, str2, len, NULL);
824 /*********************************************************************
825 * _mbsicoll(MSVCRT.@)
827 int CDECL _mbsicoll(const unsigned char* str, const unsigned char* cmp)
829 return _mbsnbicoll_l(str, cmp, -1, NULL);
832 /*********************************************************************
833 * _mbsnbcoll_l(MSVCRT.@)
835 int CDECL _mbsnbcoll_l(const unsigned char *str1, const unsigned char *str2, MSVCRT_size_t len, MSVCRT__locale_t locale)
837 MSVCRT_pthreadmbcinfo mbcinfo;
839 if(!locale)
840 mbcinfo = get_mbcinfo();
841 else
842 mbcinfo = locale->mbcinfo;
844 if(!mbcinfo->ismbcodepage)
845 return MSVCRT__strncoll_l((const char*)str1, (const char*)str2, len, locale);
846 return CompareStringA(mbcinfo->mblcid, 0, (const char*)str1, len, (const char*)str2, len)-CSTR_EQUAL;
849 /*********************************************************************
850 * _mbscoll_l(MSVCRT.@)
852 int CDECL _mbscoll_l(const unsigned char *str1, const unsigned char *str2, MSVCRT__locale_t locale)
854 return _mbsnbcoll_l(str1, str2, -1, locale);
857 /*********************************************************************
858 * _mbsnbcoll(MSVCRT.@)
860 int CDECL _mbsnbcoll(const unsigned char *str1, const unsigned char *str2, MSVCRT_size_t len)
862 return _mbsnbcoll_l(str1, str2, len, NULL);
865 /*********************************************************************
866 * _mbscoll(MSVCRT.@)
868 int CDECL _mbscoll(const unsigned char* str, const unsigned char* cmp)
870 return _mbsnbcoll_l(str, cmp, -1, NULL);
873 /*********************************************************************
874 * _mbsicmp(MSVCRT.@)
876 int CDECL _mbsicmp(const unsigned char* str, const unsigned char* cmp)
878 if(get_mbcinfo()->ismbcodepage)
880 unsigned int strc, cmpc;
881 do {
882 if(!*str)
883 return *cmp ? -1 : 0;
884 if(!*cmp)
885 return 1;
886 strc = _mbctolower(_mbsnextc(str));
887 cmpc = _mbctolower(_mbsnextc(cmp));
888 if(strc != cmpc)
889 return strc < cmpc ? -1 : 1;
890 str +=(strc > 255) ? 2 : 1;
891 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
892 } while(1);
894 return u_strcasecmp(str, cmp); /* ASCII CP */
897 /*********************************************************************
898 * _mbsncmp(MSVCRT.@)
900 int CDECL _mbsncmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
902 if(!len)
903 return 0;
905 if(get_mbcinfo()->ismbcodepage)
907 unsigned int strc, cmpc;
908 while(len--)
910 int inc;
911 if(!*str)
912 return *cmp ? -1 : 0;
913 if(!*cmp)
914 return 1;
915 strc = _mbsnextc(str);
916 cmpc = _mbsnextc(cmp);
917 if(strc != cmpc)
918 return strc < cmpc ? -1 : 1;
919 inc=(strc > 255) ? 2 : 1; /* Equal, use same increment */
920 str += inc;
921 cmp += inc;
923 return 0; /* Matched len chars */
925 return u_strncmp(str, cmp, len); /* ASCII CP */
928 /*********************************************************************
929 * _mbsnbcmp(MSVCRT.@)
931 int CDECL _mbsnbcmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
933 if (!len)
934 return 0;
935 if(get_mbcinfo()->ismbcodepage)
937 unsigned int strc, cmpc;
938 while (len)
940 int clen;
941 if(!*str)
942 return *cmp ? -1 : 0;
943 if(!*cmp)
944 return 1;
945 if (_ismbblead(*str))
947 strc=(len>=2)?_mbsnextc(str):0;
948 clen=2;
950 else
952 strc=*str;
953 clen=1;
955 if (_ismbblead(*cmp))
956 cmpc=(len>=2)?_mbsnextc(cmp):0;
957 else
958 cmpc=*cmp;
959 if(strc != cmpc)
960 return strc < cmpc ? -1 : 1;
961 len -= clen;
962 str += clen;
963 cmp += clen;
965 return 0; /* Matched len chars */
967 return u_strncmp(str,cmp,len);
970 /*********************************************************************
971 * _mbsnicmp(MSVCRT.@)
973 * Compare two multibyte strings case insensitively to 'len' characters.
975 int CDECL _mbsnicmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
977 /* FIXME: No tolower() for mb strings yet */
978 if(get_mbcinfo()->ismbcodepage)
980 unsigned int strc, cmpc;
981 while(len--)
983 if(!*str)
984 return *cmp ? -1 : 0;
985 if(!*cmp)
986 return 1;
987 strc = _mbctolower(_mbsnextc(str));
988 cmpc = _mbctolower(_mbsnextc(cmp));
989 if(strc != cmpc)
990 return strc < cmpc ? -1 : 1;
991 str +=(strc > 255) ? 2 : 1;
992 cmp +=(strc > 255) ? 2 : 1; /* Equal, use same increment */
994 return 0; /* Matched len chars */
996 return u_strncasecmp(str, cmp, len); /* ASCII CP */
999 /*********************************************************************
1000 * _mbsnbicmp(MSVCRT.@)
1002 int CDECL _mbsnbicmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
1004 if (!len)
1005 return 0;
1006 if(get_mbcinfo()->ismbcodepage)
1008 unsigned int strc, cmpc;
1009 while (len)
1011 int clen;
1012 if(!*str)
1013 return *cmp ? -1 : 0;
1014 if(!*cmp)
1015 return 1;
1016 if (_ismbblead(*str))
1018 strc=(len>=2)?_mbsnextc(str):0;
1019 clen=2;
1021 else
1023 strc=*str;
1024 clen=1;
1026 if (_ismbblead(*cmp))
1027 cmpc=(len>=2)?_mbsnextc(cmp):0;
1028 else
1029 cmpc=*cmp;
1030 strc = _mbctolower(strc);
1031 cmpc = _mbctolower(cmpc);
1032 if(strc != cmpc)
1033 return strc < cmpc ? -1 : 1;
1034 len -= clen;
1035 str += clen;
1036 cmp += clen;
1038 return 0; /* Matched len bytes */
1040 return u_strncasecmp(str,cmp,len);
1043 /*********************************************************************
1044 * _mbscat (MSVCRT.@)
1046 unsigned char * CDECL _mbscat( unsigned char *dst, const unsigned char *src )
1048 strcat( (char *)dst, (const char *)src );
1049 return dst;
1052 /*********************************************************************
1053 * _mbscpy (MSVCRT.@)
1055 unsigned char* CDECL _mbscpy( unsigned char *dst, const unsigned char *src )
1057 strcpy( (char *)dst, (const char *)src );
1058 return dst;
1061 /*********************************************************************
1062 * _mbsstr (MSVCRT.@)
1064 unsigned char * CDECL _mbsstr(const unsigned char *haystack, const unsigned char *needle)
1066 return (unsigned char *)strstr( (const char *)haystack, (const char *)needle );
1069 /*********************************************************************
1070 * _mbschr(MSVCRT.@)
1072 * Find a multibyte character in a multibyte string.
1074 unsigned char* CDECL _mbschr(const unsigned char* s, unsigned int x)
1076 if(get_mbcinfo()->ismbcodepage)
1078 unsigned int c;
1079 while (1)
1081 c = _mbsnextc(s);
1082 if (c == x)
1083 return (unsigned char*)s;
1084 if (!c)
1085 return NULL;
1086 s += c > 255 ? 2 : 1;
1089 return u_strchr(s, x); /* ASCII CP */
1092 /*********************************************************************
1093 * _mbsrchr(MSVCRT.@)
1095 unsigned char* CDECL _mbsrchr(const unsigned char* s, unsigned int x)
1097 if(get_mbcinfo()->ismbcodepage)
1099 unsigned int c;
1100 unsigned char* match=NULL;
1101 if(!s)
1102 return NULL;
1103 while (1) {
1104 c = _mbsnextc(s);
1105 if (c == x)
1106 match=(unsigned char*)s;
1107 if (!c)
1108 return match;
1109 s +=(c > 255) ? 2 : 1;
1112 return u_strrchr(s, x);
1115 /*********************************************************************
1116 * _mbstok_s_l(MSVCRT.@)
1118 unsigned char* CDECL _mbstok_s_l(unsigned char *str, const unsigned char *delim,
1119 unsigned char **ctx, MSVCRT__locale_t locale)
1121 MSVCRT_pthreadmbcinfo mbcinfo;
1122 unsigned int c;
1124 if(!MSVCRT_CHECK_PMT(delim != NULL)) return NULL;
1125 if(!MSVCRT_CHECK_PMT(ctx != NULL)) return NULL;
1126 if(!MSVCRT_CHECK_PMT(str || *ctx)) return NULL;
1128 if(locale)
1129 mbcinfo = locale->mbcinfo;
1130 else
1131 mbcinfo = get_mbcinfo();
1133 if(!mbcinfo->ismbcodepage)
1134 return (unsigned char*)MSVCRT_strtok_s((char*)str, (const char*)delim, (char**)ctx);
1136 if(!str)
1137 str = *ctx;
1139 while((c=_mbsnextc(str)) && _mbschr(delim, c))
1140 str += c>255 ? 2 : 1;
1141 if(!*str)
1142 return NULL;
1144 *ctx = str + (c>255 ? 2 : 1);
1145 while((c=_mbsnextc(*ctx)) && !_mbschr(delim, c))
1146 *ctx += c>255 ? 2 : 1;
1147 if (**ctx) {
1148 *(*ctx)++ = 0;
1149 if(c > 255)
1150 *(*ctx)++ = 0;
1153 return str;
1157 /*********************************************************************
1158 * _mbstok_s(MSVCRT.@)
1160 unsigned char* CDECL _mbstok_s(unsigned char *str,
1161 const unsigned char *delim, unsigned char **ctx)
1163 return _mbstok_s_l(str, delim, ctx, NULL);
1166 /*********************************************************************
1167 * _mbstok_l(MSVCRT.@)
1169 unsigned char* CDECL _mbstok_l(unsigned char *str,
1170 const unsigned char *delim, MSVCRT__locale_t locale)
1172 return _mbstok_s_l(str, delim, &msvcrt_get_thread_data()->mbstok_next, locale);
1175 /*********************************************************************
1176 * _mbstok(MSVCRT.@)
1178 unsigned char* CDECL _mbstok(unsigned char *str, const unsigned char *delim)
1180 thread_data_t *data = msvcrt_get_thread_data();
1182 #if _MSVCR_VER == 0
1183 if(!str && !data->mbstok_next)
1184 return NULL;
1185 #endif
1187 return _mbstok_s_l(str, delim, &data->mbstok_next, NULL);
1190 /*********************************************************************
1191 * _mbbtombc(MSVCRT.@)
1193 unsigned int CDECL _mbbtombc(unsigned int c)
1195 if(get_mbcinfo()->mbcodepage == 932)
1197 if(c >= 0x20 && c <= 0x7e) {
1198 if((c >= 0x41 && c <= 0x5a) || (c >= 0x61 && c <= 0x7a) || (c >= 0x30 && c <= 0x39))
1199 return mbbtombc_932[c - 0x20] | 0x8200;
1200 else
1201 return mbbtombc_932[c - 0x20] | 0x8100;
1203 else if(c >= 0xa1 && c <= 0xdf) {
1204 if(c >= 0xa6 && c <= 0xdd && c != 0xb0)
1205 return mbbtombc_932[c - 0xa1 + 0x5f] | 0x8300;
1206 else
1207 return mbbtombc_932[c - 0xa1 + 0x5f] | 0x8100;
1210 return c; /* not Japanese or no MB char */
1213 /*********************************************************************
1214 * _mbbtype(MSVCRT.@)
1216 int CDECL _mbbtype(unsigned char c, int type)
1218 if (type == 1)
1220 if ((c >= 0x20 && c <= 0x7e) || (c >= 0xa1 && c <= 0xdf))
1221 return _MBC_SINGLE;
1222 else if ((c >= 0x40 && c <= 0x7e) || (c >= 0x80 && c <= 0xfc))
1223 return _MBC_TRAIL;
1224 else
1225 return _MBC_ILLEGAL;
1227 else
1229 if ((c >= 0x20 && c <= 0x7e) || (c >= 0xa1 && c <= 0xdf))
1230 return _MBC_SINGLE;
1231 else if ((c >= 0x81 && c <= 0x9f) || (c >= 0xe0 && c <= 0xfc))
1232 return _MBC_LEAD;
1233 else
1234 return _MBC_ILLEGAL;
1238 /*********************************************************************
1239 * _ismbbkana(MSVCRT.@)
1241 int CDECL _ismbbkana(unsigned int c)
1243 /* FIXME: use lc_ctype when supported, not lc_all */
1244 if(get_mbcinfo()->mbcodepage == 932)
1246 /* Japanese/Katakana, CP 932 */
1247 return (c >= 0xa1 && c <= 0xdf);
1249 return 0;
1252 /*********************************************************************
1253 * _ismbcdigit(MSVCRT.@)
1255 int CDECL _ismbcdigit(unsigned int ch)
1257 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1258 return (get_char_typeW( wch ) & C1_DIGIT);
1261 /*********************************************************************
1262 * _ismbcgraph(MSVCRT.@)
1264 int CDECL _ismbcgraph(unsigned int ch)
1266 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1267 return (get_char_typeW( wch ) & (C1_UPPER | C1_LOWER | C1_DIGIT | C1_PUNCT | C1_ALPHA));
1270 /*********************************************************************
1271 * _ismbcalpha (MSVCRT.@)
1273 int CDECL _ismbcalpha(unsigned int ch)
1275 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1276 return (get_char_typeW( wch ) & C1_ALPHA);
1279 /*********************************************************************
1280 * _ismbclower (MSVCRT.@)
1282 int CDECL _ismbclower(unsigned int ch)
1284 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1285 return (get_char_typeW( wch ) & C1_UPPER);
1288 /*********************************************************************
1289 * _ismbcupper (MSVCRT.@)
1291 int CDECL _ismbcupper(unsigned int ch)
1293 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1294 return (get_char_typeW( wch ) & C1_LOWER);
1297 /*********************************************************************
1298 * _ismbcsymbol(MSVCRT.@)
1300 int CDECL _ismbcsymbol(unsigned int ch)
1302 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1303 WORD ctype;
1304 if (!GetStringTypeW(CT_CTYPE3, &wch, 1, &ctype))
1306 WARN("GetStringTypeW failed on %x\n", ch);
1307 return 0;
1309 return ((ctype & C3_SYMBOL) != 0);
1312 /*********************************************************************
1313 * _ismbcalnum (MSVCRT.@)
1315 int CDECL _ismbcalnum(unsigned int ch)
1317 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1318 return (get_char_typeW( wch ) & (C1_ALPHA | C1_DIGIT));
1321 /*********************************************************************
1322 * _ismbcspace (MSVCRT.@)
1324 int CDECL _ismbcspace(unsigned int ch)
1326 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1327 return (get_char_typeW( wch ) & C1_SPACE);
1330 /*********************************************************************
1331 * _ismbcprint (MSVCRT.@)
1333 int CDECL _ismbcprint(unsigned int ch)
1335 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1336 return (get_char_typeW( wch ) & (C1_UPPER | C1_LOWER | C1_DIGIT | C1_PUNCT | C1_ALPHA | C1_SPACE));
1339 /*********************************************************************
1340 * _ismbcpunct(MSVCRT.@)
1342 int CDECL _ismbcpunct(unsigned int ch)
1344 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1345 return (get_char_typeW( wch ) & C1_PUNCT);
1348 /*********************************************************************
1349 * _ismbchira(MSVCRT.@)
1351 int CDECL _ismbchira(unsigned int c)
1353 /* FIXME: use lc_ctype when supported, not lc_all */
1354 if(get_mbcinfo()->mbcodepage == 932)
1356 /* Japanese/Hiragana, CP 932 */
1357 return (c >= 0x829f && c <= 0x82f1);
1359 return 0;
1362 /*********************************************************************
1363 * _ismbckata(MSVCRT.@)
1365 int CDECL _ismbckata(unsigned int c)
1367 /* FIXME: use lc_ctype when supported, not lc_all */
1368 if(get_mbcinfo()->mbcodepage == 932)
1370 if(c < 256)
1371 return _ismbbkana(c);
1372 /* Japanese/Katakana, CP 932 */
1373 return (c >= 0x8340 && c <= 0x8396 && c != 0x837f);
1375 return 0;
1378 /*********************************************************************
1379 * _ismbblead(MSVCRT.@)
1381 int CDECL _ismbblead(unsigned int c)
1383 return (get_mbcinfo()->mbctype[(c&0xff) + 1] & _M1) != 0;
1387 /*********************************************************************
1388 * _ismbbtrail(MSVCRT.@)
1390 int CDECL _ismbbtrail(unsigned int c)
1392 return (get_mbcinfo()->mbctype[(c&0xff) + 1] & _M2) != 0;
1395 /*********************************************************************
1396 * _ismbclegal(MSVCRT.@)
1398 int CDECL _ismbclegal(unsigned int c)
1400 return _ismbblead(HIBYTE(c)) && _ismbbtrail(LOBYTE(c));
1403 /*********************************************************************
1404 * _ismbslead(MSVCRT.@)
1406 int CDECL _ismbslead(const unsigned char* start, const unsigned char* str)
1408 int lead = 0;
1410 if(!get_mbcinfo()->ismbcodepage)
1411 return 0;
1413 /* Lead bytes can also be trail bytes so we need to analyse the string
1415 while (start <= str)
1417 if (!*start)
1418 return 0;
1419 lead = !lead && _ismbblead(*start);
1420 start++;
1423 return lead ? -1 : 0;
1426 /*********************************************************************
1427 * _ismbstrail(MSVCRT.@)
1429 int CDECL _ismbstrail(const unsigned char* start, const unsigned char* str)
1431 /* Note: this function doesn't check _ismbbtrail */
1432 if ((str > start) && _ismbslead(start, str-1))
1433 return -1;
1434 else
1435 return 0;
1438 /*********************************************************************
1439 * _mbsbtype (MSVCRT.@)
1441 int CDECL _mbsbtype(const unsigned char *str, MSVCRT_size_t count)
1443 int lead = 0;
1444 const unsigned char *end = str + count;
1446 /* Lead bytes can also be trail bytes so we need to analyse the string.
1447 * Also we must return _MBC_ILLEGAL for chars past the end of the string
1449 while (str < end) /* Note: we skip the last byte - will check after the loop */
1451 if (!*str)
1452 return _MBC_ILLEGAL;
1453 lead = get_mbcinfo()->ismbcodepage && !lead && _ismbblead(*str);
1454 str++;
1457 if (lead)
1458 if (_ismbbtrail(*str))
1459 return _MBC_TRAIL;
1460 else
1461 return _MBC_ILLEGAL;
1462 else
1463 if (_ismbblead(*str))
1464 return _MBC_LEAD;
1465 else
1466 return _MBC_SINGLE;
1469 /*********************************************************************
1470 * _mbsset(MSVCRT.@)
1472 unsigned char* CDECL _mbsset(unsigned char* str, unsigned int c)
1474 unsigned char* ret = str;
1476 if(!get_mbcinfo()->ismbcodepage || c < 256)
1477 return u__strset(str, c); /* ASCII CP or SB char */
1479 c &= 0xffff; /* Strip high bits */
1481 while(str[0] && str[1])
1483 *str++ = c >> 8;
1484 *str++ = c & 0xff;
1486 if(str[0])
1487 str[0] = '\0'; /* FIXME: OK to shorten? */
1489 return ret;
1492 /*********************************************************************
1493 * _mbsnbset(MSVCRT.@)
1495 unsigned char* CDECL _mbsnbset(unsigned char *str, unsigned int c, MSVCRT_size_t len)
1497 unsigned char *ret = str;
1499 if(!len)
1500 return ret;
1502 if(!get_mbcinfo()->ismbcodepage || c < 256)
1503 return u__strnset(str, c, len); /* ASCII CP or SB char */
1505 c &= 0xffff; /* Strip high bits */
1507 while(str[0] && str[1] && (len > 1))
1509 *str++ = c >> 8;
1510 len--;
1511 *str++ = c & 0xff;
1512 len--;
1514 if(len && str[0]) {
1515 /* as per msdn pad with a blank character */
1516 str[0] = ' ';
1519 return ret;
1522 /*********************************************************************
1523 * _mbsnset(MSVCRT.@)
1525 unsigned char* CDECL _mbsnset(unsigned char* str, unsigned int c, MSVCRT_size_t len)
1527 unsigned char *ret = str;
1529 if(!len)
1530 return ret;
1532 if(!get_mbcinfo()->ismbcodepage || c < 256)
1533 return u__strnset(str, c, len); /* ASCII CP or SB char */
1535 c &= 0xffff; /* Strip high bits */
1537 while(str[0] && str[1] && len--)
1539 *str++ = c >> 8;
1540 *str++ = c & 0xff;
1542 if(len && str[0])
1543 str[0] = '\0'; /* FIXME: OK to shorten? */
1545 return ret;
1548 /*********************************************************************
1549 * _mbsnccnt(MSVCRT.@)
1550 * 'c' is for 'character'.
1552 MSVCRT_size_t CDECL _mbsnccnt(const unsigned char* str, MSVCRT_size_t len)
1554 MSVCRT_size_t ret;
1555 if(get_mbcinfo()->ismbcodepage)
1557 ret=0;
1558 while(*str && len-- > 0)
1560 if(_ismbblead(*str))
1562 if (!len)
1563 break;
1564 len--;
1565 str++;
1567 str++;
1568 ret++;
1570 return ret;
1572 ret=u_strlen(str);
1573 return min(ret, len); /* ASCII CP */
1576 /*********************************************************************
1577 * _mbsnbcnt(MSVCRT.@)
1578 * 'b' is for byte count.
1580 MSVCRT_size_t CDECL _mbsnbcnt(const unsigned char* str, MSVCRT_size_t len)
1582 MSVCRT_size_t ret;
1583 if(get_mbcinfo()->ismbcodepage)
1585 const unsigned char* xstr = str;
1586 while(*xstr && len-- > 0)
1588 if (_ismbblead(*xstr++))
1589 xstr++;
1591 return xstr-str;
1593 ret=u_strlen(str);
1594 return min(ret, len); /* ASCII CP */
1597 /*********************************************************************
1598 * _mbsnbcat(MSVCRT.@)
1600 unsigned char* CDECL _mbsnbcat(unsigned char* dst, const unsigned char* src, MSVCRT_size_t len)
1602 if(get_mbcinfo()->ismbcodepage)
1604 unsigned char *res = dst;
1605 while (*dst) {
1606 if (_ismbblead(*dst++)) {
1607 if (*dst) {
1608 dst++;
1609 } else {
1610 /* as per msdn overwrite the lead byte in front of '\0' */
1611 dst--;
1612 break;
1616 while (*src && len--) *dst++ = *src++;
1617 *dst = '\0';
1618 return res;
1620 return u_strncat(dst, src, len); /* ASCII CP */
1623 int CDECL _mbsnbcat_s(unsigned char *dst, MSVCRT_size_t size, const unsigned char *src, MSVCRT_size_t len)
1625 unsigned char *ptr = dst;
1626 MSVCRT_size_t i;
1628 if (!dst && !size && !src && !len)
1629 return 0;
1631 if (!dst || !size || !src)
1633 if (dst && size)
1634 *dst = '\0';
1636 *MSVCRT__errno() = MSVCRT_EINVAL;
1637 return MSVCRT_EINVAL;
1640 /* Find the null terminator of the destination buffer. */
1641 while (size && *ptr)
1642 size--, ptr++;
1644 if (!size)
1646 *dst = '\0';
1647 *MSVCRT__errno() = MSVCRT_EINVAL;
1648 return MSVCRT_EINVAL;
1651 /* If necessary, check that the character preceding the null terminator is
1652 * a lead byte and move the pointer back by one for later overwrite. */
1653 if (ptr != dst && get_mbcinfo()->ismbcodepage && _ismbblead(*(ptr - 1)))
1654 size++, ptr--;
1656 for (i = 0; *src && i < len; i++)
1658 *ptr++ = *src++;
1659 size--;
1661 if (!size)
1663 *dst = '\0';
1664 *MSVCRT__errno() = MSVCRT_ERANGE;
1665 return MSVCRT_ERANGE;
1669 *ptr = '\0';
1670 return 0;
1673 /*********************************************************************
1674 * _mbsncat(MSVCRT.@)
1676 unsigned char* CDECL _mbsncat(unsigned char* dst, const unsigned char* src, MSVCRT_size_t len)
1678 if(get_mbcinfo()->ismbcodepage)
1680 unsigned char *res = dst;
1681 while (*dst)
1683 if (_ismbblead(*dst++))
1684 dst++;
1686 while (*src && len--)
1688 *dst++ = *src;
1689 if(_ismbblead(*src++))
1690 *dst++ = *src++;
1692 *dst = '\0';
1693 return res;
1695 return u_strncat(dst, src, len); /* ASCII CP */
1699 /*********************************************************************
1700 * _mbslwr(MSVCRT.@)
1702 unsigned char* CDECL _mbslwr(unsigned char* s)
1704 unsigned char *ret = s;
1705 if (!s)
1706 return NULL;
1707 if (get_mbcinfo()->ismbcodepage)
1709 unsigned int c;
1710 while (*s)
1712 c = _mbctolower(_mbsnextc(s));
1713 /* Note that I assume that the size of the character is unchanged */
1714 if (c > 255)
1716 *s++=(c>>8);
1717 c=c & 0xff;
1719 *s++=c;
1722 else for ( ; *s; s++) *s = tolower(*s);
1723 return ret;
1727 /*********************************************************************
1728 * _mbslwr_s(MSVCRT.@)
1730 int CDECL _mbslwr_s(unsigned char* s, MSVCRT_size_t len)
1732 if (!s && !len)
1734 return 0;
1736 else if (!s || !len)
1738 *MSVCRT__errno() = MSVCRT_EINVAL;
1739 return MSVCRT_EINVAL;
1741 if (get_mbcinfo()->ismbcodepage)
1743 unsigned int c;
1744 for ( ; *s && len > 0; len--)
1746 c = _mbctolower(_mbsnextc(s));
1747 /* Note that I assume that the size of the character is unchanged */
1748 if (c > 255)
1750 *s++=(c>>8);
1751 c=c & 0xff;
1753 *s++=c;
1756 else for ( ; *s && len > 0; s++, len--) *s = tolower(*s);
1757 if (*s)
1759 *s = '\0';
1760 *MSVCRT__errno() = MSVCRT_EINVAL;
1761 return MSVCRT_EINVAL;
1763 return 0;
1767 /*********************************************************************
1768 * _mbsupr(MSVCRT.@)
1770 unsigned char* CDECL _mbsupr(unsigned char* s)
1772 unsigned char *ret = s;
1773 if (!s)
1774 return NULL;
1775 if (get_mbcinfo()->ismbcodepage)
1777 unsigned int c;
1778 while (*s)
1780 c = _mbctoupper(_mbsnextc(s));
1781 /* Note that I assume that the size of the character is unchanged */
1782 if (c > 255)
1784 *s++=(c>>8);
1785 c=c & 0xff;
1787 *s++=c;
1790 else for ( ; *s; s++) *s = toupper(*s);
1791 return ret;
1795 /*********************************************************************
1796 * _mbsupr_s(MSVCRT.@)
1798 int CDECL _mbsupr_s(unsigned char* s, MSVCRT_size_t len)
1800 if (!s && !len)
1802 return 0;
1804 else if (!s || !len)
1806 *MSVCRT__errno() = MSVCRT_EINVAL;
1807 return MSVCRT_EINVAL;
1809 if (get_mbcinfo()->ismbcodepage)
1811 unsigned int c;
1812 for ( ; *s && len > 0; len--)
1814 c = _mbctoupper(_mbsnextc(s));
1815 /* Note that I assume that the size of the character is unchanged */
1816 if (c > 255)
1818 *s++=(c>>8);
1819 c=c & 0xff;
1821 *s++=c;
1824 else for ( ; *s && len > 0; s++, len--) *s = toupper(*s);
1825 if (*s)
1827 *s = '\0';
1828 *MSVCRT__errno() = MSVCRT_EINVAL;
1829 return MSVCRT_EINVAL;
1831 return 0;
1835 /*********************************************************************
1836 * _mbsspn (MSVCRT.@)
1838 MSVCRT_size_t CDECL _mbsspn(const unsigned char* string, const unsigned char* set)
1840 const unsigned char *p, *q;
1842 for (p = string; *p; p++)
1844 if (_ismbblead(*p))
1846 for (q = set; *q; q++)
1848 if (!q[1])
1849 break;
1850 if ((*p == *q) && (p[1] == q[1]))
1851 break;
1852 q++;
1854 if (!q[0] || !q[1]) break;
1856 else
1858 for (q = set; *q; q++)
1859 if (*p == *q)
1860 break;
1861 if (!*q) break;
1864 return p - string;
1867 /*********************************************************************
1868 * _mbsspnp (MSVCRT.@)
1870 unsigned char* CDECL _mbsspnp(const unsigned char* string, const unsigned char* set)
1872 const unsigned char *p, *q;
1874 for (p = string; *p; p++)
1876 if (_ismbblead(*p))
1878 for (q = set; *q; q++)
1880 if (!q[1])
1881 break;
1882 if ((*p == *q) && (p[1] == q[1]))
1883 break;
1884 q++;
1886 if (!q[0] || !q[1]) break;
1888 else
1890 for (q = set; *q; q++)
1891 if (*p == *q)
1892 break;
1893 if (!*q) break;
1896 if (*p == '\0')
1897 return NULL;
1898 return (unsigned char *)p;
1901 /*********************************************************************
1902 * _mbscspn(MSVCRT.@)
1904 MSVCRT_size_t CDECL _mbscspn(const unsigned char* str, const unsigned char* cmp)
1906 if (get_mbcinfo()->ismbcodepage)
1907 FIXME("don't handle double character case\n");
1908 return u_strcspn(str, cmp);
1911 /*********************************************************************
1912 * _mbsrev (MSVCRT.@)
1914 unsigned char* CDECL _mbsrev(unsigned char* str)
1916 int i, len = _mbslen(str);
1917 unsigned char *p, *temp=MSVCRT_malloc(len*2);
1919 if(!temp)
1920 return str;
1922 /* unpack multibyte string to temp buffer */
1923 p=str;
1924 for(i=0; i<len; i++)
1926 if (_ismbblead(*p))
1928 temp[i*2]=*p++;
1929 temp[i*2+1]=*p++;
1931 else
1933 temp[i*2]=*p++;
1934 temp[i*2+1]=0;
1938 /* repack it in the reverse order */
1939 p=str;
1940 for(i=len-1; i>=0; i--)
1942 if(_ismbblead(temp[i*2]))
1944 *p++=temp[i*2];
1945 *p++=temp[i*2+1];
1947 else
1949 *p++=temp[i*2];
1953 MSVCRT_free(temp);
1955 return str;
1958 /*********************************************************************
1959 * _mbspbrk (MSVCRT.@)
1961 unsigned char* CDECL _mbspbrk(const unsigned char* str, const unsigned char* accept)
1963 const unsigned char* p;
1965 while(*str)
1967 for(p = accept; *p; p += (_ismbblead(*p)?2:1) )
1969 if (*p == *str)
1970 if( !_ismbblead(*p) || ( *(p+1) == *(str+1) ) )
1971 return (unsigned char*)str;
1973 str += (_ismbblead(*str)?2:1);
1975 return NULL;
1980 * Functions depending on locale codepage
1983 /*********************************************************************
1984 * mblen(MSVCRT.@)
1985 * REMARKS
1986 * Unlike most of the multibyte string functions this function uses
1987 * the locale codepage, not the codepage set by _setmbcp
1989 int CDECL MSVCRT_mblen(const char* str, MSVCRT_size_t size)
1991 if (str && *str && size)
1993 if(get_locinfo()->mb_cur_max == 1)
1994 return 1; /* ASCII CP */
1996 return !MSVCRT_isleadbyte((unsigned char)*str) ? 1 : (size>1 ? 2 : -1);
1998 return 0;
2001 /*********************************************************************
2002 * mbrlen(MSVCRT.@)
2004 MSVCRT_size_t CDECL MSVCRT_mbrlen(const char *str, MSVCRT_size_t len, MSVCRT_mbstate_t *state)
2006 MSVCRT_mbstate_t s = (state ? *state : 0);
2007 MSVCRT_size_t ret;
2009 if(!len || !str || !*str)
2010 return 0;
2012 if(get_locinfo()->mb_cur_max == 1) {
2013 return 1;
2014 }else if(!s && MSVCRT_isleadbyte((unsigned char)*str)) {
2015 if(len == 1) {
2016 s = (unsigned char)*str;
2017 ret = -2;
2018 }else {
2019 ret = 2;
2021 }else if(!s) {
2022 ret = 1;
2023 }else {
2024 s = 0;
2025 ret = 2;
2028 if(state)
2029 *state = s;
2030 return ret;
2033 /*********************************************************************
2034 * _mbstrlen_l(MSVCRT.@)
2036 MSVCRT_size_t CDECL _mbstrlen_l(const char* str, MSVCRT__locale_t locale)
2038 MSVCRT_pthreadlocinfo locinfo;
2040 if(!locale)
2041 locinfo = get_locinfo();
2042 else
2043 locinfo = locale->locinfo;
2045 if(locinfo->mb_cur_max > 1) {
2046 MSVCRT_size_t len;
2047 len = MultiByteToWideChar(locinfo->lc_codepage, MB_ERR_INVALID_CHARS,
2048 str, -1, NULL, 0);
2049 if (!len) {
2050 *MSVCRT__errno() = MSVCRT_EILSEQ;
2051 return -1;
2053 return len - 1;
2056 return strlen(str);
2059 /*********************************************************************
2060 * _mbstrlen(MSVCRT.@)
2062 MSVCRT_size_t CDECL _mbstrlen(const char* str)
2064 return _mbstrlen_l(str, NULL);
2067 /*********************************************************************
2068 * _mbtowc_l(MSVCRT.@)
2070 int CDECL MSVCRT_mbtowc_l(MSVCRT_wchar_t *dst, const char* str, MSVCRT_size_t n, MSVCRT__locale_t locale)
2072 MSVCRT_pthreadlocinfo locinfo;
2073 MSVCRT_wchar_t tmpdst = '\0';
2075 if(!locale)
2076 locinfo = get_locinfo();
2077 else
2078 locinfo = locale->locinfo;
2080 if(n <= 0 || !str)
2081 return 0;
2082 if(!locinfo->lc_codepage)
2083 tmpdst = (unsigned char)*str;
2084 else if(!MultiByteToWideChar(locinfo->lc_codepage, 0, str, n, &tmpdst, 1))
2085 return -1;
2086 if(dst)
2087 *dst = tmpdst;
2088 /* return the number of bytes from src that have been used */
2089 if(!*str)
2090 return 0;
2091 if(n >= 2 && MSVCRT__isleadbyte_l((unsigned char)*str, locale) && str[1])
2092 return 2;
2093 return 1;
2096 /*********************************************************************
2097 * mbtowc(MSVCRT.@)
2099 int CDECL MSVCRT_mbtowc(MSVCRT_wchar_t *dst, const char* str, MSVCRT_size_t n)
2101 return MSVCRT_mbtowc_l(dst, str, n, NULL);
2104 /*********************************************************************
2105 * mbrtowc(MSVCRT.@)
2107 MSVCRT_size_t CDECL MSVCRT_mbrtowc(MSVCRT_wchar_t *dst, const char *str,
2108 MSVCRT_size_t n, MSVCRT_mbstate_t *state)
2110 MSVCRT_pthreadlocinfo locinfo = get_locinfo();
2111 MSVCRT_mbstate_t s = (state ? *state : 0);
2112 char tmpstr[2];
2113 int len = 0;
2115 if(dst)
2116 *dst = 0;
2118 if(!n || !str || !*str)
2119 return 0;
2121 if(locinfo->mb_cur_max == 1) {
2122 tmpstr[len++] = *str;
2123 }else if(!s && MSVCRT_isleadbyte((unsigned char)*str)) {
2124 if(n == 1) {
2125 s = (unsigned char)*str;
2126 len = -2;
2127 }else {
2128 tmpstr[0] = str[0];
2129 tmpstr[1] = str[1];
2130 len = 2;
2132 }else if(!s) {
2133 tmpstr[len++] = *str;
2134 }else {
2135 tmpstr[0] = s;
2136 tmpstr[1] = *str;
2137 len = 2;
2138 s = 0;
2141 if(len > 0) {
2142 if(!MultiByteToWideChar(locinfo->lc_codepage, 0, tmpstr, len, dst, dst ? 1 : 0))
2143 len = -1;
2146 if(state)
2147 *state = s;
2148 return len;
2151 /*********************************************************************
2152 * _mbstowcs_l(MSVCRT.@)
2154 MSVCRT_size_t CDECL MSVCRT__mbstowcs_l(MSVCRT_wchar_t *wcstr, const char *mbstr,
2155 MSVCRT_size_t count, MSVCRT__locale_t locale)
2157 MSVCRT_pthreadlocinfo locinfo;
2158 MSVCRT_size_t i, size;
2160 if(!mbstr) {
2161 *MSVCRT__errno() = MSVCRT_EINVAL;
2162 return -1;
2165 if(!locale)
2166 locinfo = get_locinfo();
2167 else
2168 locinfo = locale->locinfo;
2170 /* Ignore count parameter */
2171 if(!wcstr)
2172 return MultiByteToWideChar(locinfo->lc_codepage, 0, mbstr, -1, NULL, 0)-1;
2174 for(i=0, size=0; i<count; i++) {
2175 if(mbstr[size] == '\0')
2176 break;
2178 size += (MSVCRT__isleadbyte_l((unsigned char)mbstr[size], locale) ? 2 : 1);
2181 size = MultiByteToWideChar(locinfo->lc_codepage, 0,
2182 mbstr, size, wcstr, count);
2184 if(size<count && wcstr)
2185 wcstr[size] = '\0';
2187 return size;
2190 /*********************************************************************
2191 * mbstowcs(MSVCRT.@)
2193 MSVCRT_size_t CDECL MSVCRT_mbstowcs(MSVCRT_wchar_t *wcstr,
2194 const char *mbstr, MSVCRT_size_t count)
2196 return MSVCRT__mbstowcs_l(wcstr, mbstr, count, NULL);
2199 /*********************************************************************
2200 * _mbstowcs_s_l(MSVCRT.@)
2202 int CDECL MSVCRT__mbstowcs_s_l(MSVCRT_size_t *ret, MSVCRT_wchar_t *wcstr,
2203 MSVCRT_size_t size, const char *mbstr,
2204 MSVCRT_size_t count, MSVCRT__locale_t locale)
2206 MSVCRT_size_t conv;
2208 if(!wcstr && !size) {
2209 conv = MSVCRT__mbstowcs_l(NULL, mbstr, 0, locale);
2210 if(ret)
2211 *ret = conv+1;
2212 return 0;
2215 if (!MSVCRT_CHECK_PMT(wcstr != NULL)) return MSVCRT_EINVAL;
2216 if (!MSVCRT_CHECK_PMT(mbstr != NULL)) {
2217 if(size) wcstr[0] = '\0';
2218 return MSVCRT_EINVAL;
2221 if(count==MSVCRT__TRUNCATE || size<count)
2222 conv = size;
2223 else
2224 conv = count;
2226 conv = MSVCRT__mbstowcs_l(wcstr, mbstr, conv, locale);
2227 if(conv<size)
2228 wcstr[conv++] = '\0';
2229 else if(conv==size && (count==MSVCRT__TRUNCATE || wcstr[conv-1]=='\0'))
2230 wcstr[conv-1] = '\0';
2231 else {
2232 MSVCRT_INVALID_PMT("wcstr[size] is too small", MSVCRT_ERANGE);
2233 if(size)
2234 wcstr[0] = '\0';
2235 return MSVCRT_ERANGE;
2238 if(ret)
2239 *ret = conv;
2240 return 0;
2243 /*********************************************************************
2244 * mbstowcs_s(MSVCRT.@)
2246 int CDECL MSVCRT__mbstowcs_s(MSVCRT_size_t *ret, MSVCRT_wchar_t *wcstr,
2247 MSVCRT_size_t size, const char *mbstr, MSVCRT_size_t count)
2249 return MSVCRT__mbstowcs_s_l(ret, wcstr, size, mbstr, count, NULL);
2252 /*********************************************************************
2253 * mbsrtowcs(MSVCRT.@)
2255 MSVCRT_size_t CDECL MSVCRT_mbsrtowcs(MSVCRT_wchar_t *wcstr,
2256 const char **pmbstr, MSVCRT_size_t count, MSVCRT_mbstate_t *state)
2258 MSVCRT_mbstate_t s = (state ? *state : 0);
2259 MSVCRT_wchar_t tmpdst;
2260 MSVCRT_size_t ret = 0;
2262 if(!MSVCRT_CHECK_PMT(pmbstr != NULL))
2263 return -1;
2265 while(!wcstr || count>ret) {
2266 int ch_len = MSVCRT_mbrtowc(&tmpdst, *pmbstr, 2, &s);
2267 if(wcstr)
2268 wcstr[ret] = tmpdst;
2270 if(ch_len < 0) {
2271 return -1;
2272 }else if(ch_len == 0) {
2273 *pmbstr = NULL;
2274 return ret;
2277 *pmbstr += ch_len;
2278 ret++;
2281 return ret;