cmd: del /a: test deleting readonly files, with fix.
[wine.git] / dlls / msvcrt / mbcs.c
blobcaadc3260ed33bc51e97bf2b848ffe32a24519c0
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 "msvcrt.h"
27 #include "wine/unicode.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
32 unsigned char MSVCRT_mbctype[257] = { 0 };
33 static int g_mbcp_is_multibyte = 0;
35 int MSVCRT___mb_cur_max = 1;
37 /* It seems that the data about valid trail bytes is not available from kernel32
38 * so we have to store is here. The format is the same as for lead bytes in CPINFO */
39 struct cp_extra_info_t
41 int cp;
42 BYTE TrailBytes[MAX_LEADBYTES];
45 static struct cp_extra_info_t g_cpextrainfo[] =
47 {932, {0x40, 0x7e, 0x80, 0xfc, 0, 0}},
48 {936, {0x40, 0xfe, 0, 0}},
49 {949, {0x41, 0xfe, 0, 0}},
50 {950, {0x40, 0x7e, 0xa1, 0xfe, 0, 0}},
51 {1361, {0x31, 0x7e, 0x81, 0xfe, 0, 0}},
52 {20932, {1, 255, 0, 0}}, /* seems to give different results on different systems */
53 {0, {1, 255, 0, 0}} /* match all with FIXME */
56 /* Maps multibyte cp932 punctuation marks to single byte equivalents */
57 static const unsigned char mbctombb_932_punct[] = {
58 0x20,0xa4,0xa1,0x2c,0x2e,0xa5,0x3a,0x3b,0x3f,0x21,0xde,0xdf,0x00,0x00,0x00,0x5e,
59 0x7e,0x5f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xb0,0x00,0x00,0x2f,0x00,
60 0x00,0x00,0x7c,0x00,0x00,0x60,0x27,0x00,0x22,0x28,0x29,0x00,0x00,0x5b,0x5d,0x7b,
61 0x7d,0x00,0x00,0x00,0x00,0xa2,0xa3,0x00,0x00,0x00,0x00,0x2b,0x2d,0x00,0x00,0x00,
62 0x00,0x3d,0x00,0x3c,0x3e,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x5c,
63 0x24,0x00,0x00,0x25,0x23,0x26,0x2a,0x40};
65 /* Maps multibyte cp932 hiragana/katakana to single-byte equivalents */
66 static const unsigned char mbctombb_932_kana[] = {
67 0xa7,0xb1,0xa8,0xb2,0xa9,0xb3,0xaa,0xb4,0xab,0xb5,0xb6,0xb6,0xb7,0xb7,0xb8,0xb8,
68 0xb9,0xb9,0xba,0xba,0xbb,0xbb,0xbc,0xbc,0xbd,0xbd,0xbe,0xbe,0xbf,0xbf,0xc0,0xc0,
69 0xc1,0xc1,0xaf,0xc2,0xc2,0xc3,0xc3,0xc4,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xca,
70 0xca,0xcb,0xcb,0xcb,0xcc,0xcc,0xcc,0xcd,0xcd,0xcd,0xce,0xce,0xce,0xcf,0xd0,0xd1,
71 0xd2,0xd3,0xac,0xd4,0xad,0xd5,0xae,0xd6,0xd7,0xd8,0xd9,0xda,0xdb,0xdc,0xdc,0xb2,
72 0xb4,0xa6,0xdd,0xb3,0xb6,0xb9};
74 static MSVCRT_wchar_t msvcrt_mbc_to_wc(unsigned int ch)
76 MSVCRT_wchar_t chW;
77 char mbch[2];
78 int n_chars;
80 if (ch <= 0xff) {
81 mbch[0] = ch;
82 n_chars = 1;
83 } else {
84 mbch[0] = (ch >> 8) & 0xff;
85 mbch[1] = ch & 0xff;
86 n_chars = 2;
88 if (!MultiByteToWideChar(get_locale()->locinfo->lc_codepage, 0, mbch, n_chars, &chW, 1))
90 WARN("MultiByteToWideChar failed on %x\n", ch);
91 return 0;
93 return chW;
96 static inline MSVCRT_size_t u_strlen( const unsigned char *str )
98 return strlen( (const char*) str );
101 static inline unsigned char* u_strncat( unsigned char* dst, const unsigned char* src, MSVCRT_size_t len )
103 return (unsigned char*)strncat( (char*)dst, (const char*)src, len);
106 static inline int u_strcmp( const unsigned char *s1, const unsigned char *s2 )
108 return strcmp( (const char*)s1, (const char*)s2 );
111 static inline int u_strcasecmp( const unsigned char *s1, const unsigned char *s2 )
113 return strcasecmp( (const char*)s1, (const char*)s2 );
116 static inline int u_strncmp( const unsigned char *s1, const unsigned char *s2, MSVCRT_size_t len )
118 return strncmp( (const char*)s1, (const char*)s2, len );
121 static inline int u_strncasecmp( const unsigned char *s1, const unsigned char *s2, MSVCRT_size_t len )
123 return strncasecmp( (const char*)s1, (const char*)s2, len );
126 static inline unsigned char *u_strchr( const unsigned char *s, unsigned char x )
128 return (unsigned char*) strchr( (const char*)s, x );
131 static inline unsigned char *u_strrchr( const unsigned char *s, unsigned char x )
133 return (unsigned char*) strrchr( (const char*)s, x );
136 static inline unsigned char *u_strtok( unsigned char *s, const unsigned char *delim )
138 return (unsigned char*) strtok( (char*)s, (const char*)delim );
141 static inline unsigned char *u__strset( unsigned char *s, unsigned char c )
143 return (unsigned char*) _strset( (char*)s, c);
146 static inline unsigned char *u__strnset( unsigned char *s, unsigned char c, MSVCRT_size_t len )
148 return (unsigned char*) MSVCRT__strnset( (char*)s, c, len );
151 static inline MSVCRT_size_t u_strcspn( const unsigned char *s, const unsigned char *rej )
153 return strcspn( (const char *)s, (const char*)rej );
156 /*********************************************************************
157 * __p__mbctype (MSVCRT.@)
159 unsigned char* CDECL __p__mbctype(void)
161 return MSVCRT_mbctype;
164 /*********************************************************************
165 * ___mb_cur_max_func(MSVCRT.@)
167 int* CDECL MSVCRT____mb_cur_max_func(void)
169 return &get_locale()->locinfo->mb_cur_max;
172 /* ___mb_cur_max_l_func - not exported in native msvcrt */
173 int* CDECL ___mb_cur_max_l_func(MSVCRT__locale_t locale)
175 if(!locale)
176 locale = get_locale();
178 return &locale->locinfo->mb_cur_max;
181 /*********************************************************************
182 * _setmbcp (MSVCRT.@)
184 int CDECL _setmbcp(int cp)
186 MSVCRT__locale_t locale = get_locale();
187 int newcp;
188 CPINFO cpi;
189 BYTE *bytes;
190 WORD chartypes[256];
191 WORD *curr_type;
192 char bufA[256];
193 WCHAR bufW[256];
194 int charcount;
195 int ret;
196 int i;
198 switch (cp)
200 case _MB_CP_ANSI:
201 newcp = GetACP();
202 break;
203 case _MB_CP_OEM:
204 newcp = GetOEMCP();
205 break;
206 case _MB_CP_LOCALE:
207 newcp = locale->locinfo->lc_codepage;
208 break;
209 case _MB_CP_SBCS:
210 newcp = 20127; /* ASCII */
211 break;
212 default:
213 newcp = cp;
214 break;
217 if (!GetCPInfo(newcp, &cpi))
219 WARN("Codepage %d not found\n", newcp);
220 *MSVCRT__errno() = MSVCRT_EINVAL;
221 return -1;
224 /* setup the _mbctype */
225 memset(MSVCRT_mbctype, 0, sizeof(MSVCRT_mbctype));
227 bytes = cpi.LeadByte;
228 while (bytes[0] || bytes[1])
230 for (i = bytes[0]; i <= bytes[1]; i++)
231 MSVCRT_mbctype[i + 1] |= _M1;
232 bytes += 2;
235 if (cpi.MaxCharSize > 1)
237 /* trail bytes not available through kernel32 but stored in a structure in msvcrt */
238 struct cp_extra_info_t *cpextra = g_cpextrainfo;
240 g_mbcp_is_multibyte = 1;
241 while (TRUE)
243 if (cpextra->cp == 0 || cpextra->cp == newcp)
245 if (cpextra->cp == 0)
246 FIXME("trail bytes data not available for DBCS codepage %d - assuming all bytes\n", newcp);
248 bytes = cpextra->TrailBytes;
249 while (bytes[0] || bytes[1])
251 for (i = bytes[0]; i <= bytes[1]; i++)
252 MSVCRT_mbctype[i + 1] |= _M2;
253 bytes += 2;
255 break;
257 cpextra++;
260 else
261 g_mbcp_is_multibyte = 0;
263 /* we can't use GetStringTypeA directly because we don't have a locale - only a code page
265 charcount = 0;
266 for (i = 0; i < 256; i++)
267 if (!(MSVCRT_mbctype[i + 1] & _M1))
268 bufA[charcount++] = i;
270 ret = MultiByteToWideChar(newcp, 0, bufA, charcount, bufW, charcount);
271 if (ret != charcount)
272 ERR("MultiByteToWideChar of chars failed for cp %d, ret=%d (exp %d), error=%d\n", newcp, ret, charcount, GetLastError());
274 GetStringTypeW(CT_CTYPE1, bufW, charcount, chartypes);
276 curr_type = chartypes;
277 for (i = 0; i < 256; i++)
278 if (!(MSVCRT_mbctype[i + 1] & _M1))
280 if ((*curr_type) & C1_UPPER)
281 MSVCRT_mbctype[i + 1] |= _SBUP;
282 if ((*curr_type) & C1_LOWER)
283 MSVCRT_mbctype[i + 1] |= _SBLOW;
284 curr_type++;
287 if (newcp == 932) /* CP932 only - set _MP and _MS */
289 /* On Windows it's possible to calculate the _MP and _MS from CT_CTYPE1
290 * and CT_CTYPE3. But as of Wine 0.9.43 we return wrong values what makes
291 * it hard. As this is set only for codepage 932 we hardcode it what gives
292 * also faster execution.
294 for (i = 161; i <= 165; i++)
295 MSVCRT_mbctype[i + 1] |= _MP;
296 for (i = 166; i <= 223; i++)
297 MSVCRT_mbctype[i + 1] |= _MS;
300 locale->locinfo->lc_collate_cp = newcp;
301 locale->locinfo->lc_codepage = newcp;
302 TRACE("(%d) -> %d\n", cp, locale->locinfo->lc_codepage);
303 return 0;
306 /*********************************************************************
307 * _getmbcp (MSVCRT.@)
309 int CDECL _getmbcp(void)
311 /* FIXME: get_locale()->mbcinfo->mbcodepage should be used here */
312 return get_locale()->locinfo->lc_codepage;
315 /*********************************************************************
316 * _mbsnextc(MSVCRT.@)
318 unsigned int CDECL _mbsnextc(const unsigned char* str)
320 if(_ismbblead(*str))
321 return *str << 8 | str[1];
322 return *str;
325 /*********************************************************************
326 * _mbctolower(MSVCRT.@)
328 unsigned int CDECL _mbctolower(unsigned int c)
330 if (MSVCRT_isleadbyte(c))
332 FIXME("Handle MBC chars\n");
333 return c;
335 return tolower(c); /* ASCII CP or SB char */
338 /*********************************************************************
339 * _mbctoupper(MSVCRT.@)
341 unsigned int CDECL _mbctoupper(unsigned int c)
343 if (MSVCRT_isleadbyte(c))
345 FIXME("Handle MBC chars\n");
346 return c;
348 return toupper(c); /* ASCII CP or SB char */
351 /*********************************************************************
352 * _mbctombb (MSVCRT.@)
354 unsigned int CDECL _mbctombb(unsigned int c)
356 unsigned int value;
358 if(get_locale()->locinfo->lc_codepage == 932)
360 if(c >= 0x829f && c <= 0x82f1) /* Hiragana */
361 return mbctombb_932_kana[c - 0x829f];
362 if(c >= 0x8340 && c <= 0x8396 && c != 0x837f) /* Katakana */
363 return mbctombb_932_kana[c - 0x8340 - (c >= 0x837f ? 1 : 0)];
364 if(c >= 0x8140 && c <= 0x8197) /* Punctuation */
366 value = mbctombb_932_punct[c - 0x8140];
367 return value ? value : c;
369 if((c >= 0x824f && c <= 0x8258) || /* Fullwidth digits */
370 (c >= 0x8260 && c <= 0x8279)) /* Fullwidth capitals letters */
371 return c - 0x821f;
372 if(c >= 0x8281 && c <= 0x829a) /* Fullwidth small letters */
373 return c - 0x8220;
374 /* all other cases return c */
376 return c;
379 /*********************************************************************
380 * _mbcjistojms(MSVCRT.@)
382 * Converts a jis character to sjis.
383 * Based on description from
384 * http://www.slayers.ne.jp/~oouchi/code/jistosjis.html
386 unsigned int CDECL _mbcjistojms(unsigned int c)
388 /* Conversion takes place only when codepage is 932.
389 In all other cases, c is returned unchanged */
390 if(get_locale()->locinfo->lc_codepage == 932)
392 if(HIBYTE(c) >= 0x21 && HIBYTE(c) <= 0x7e &&
393 LOBYTE(c) >= 0x21 && LOBYTE(c) <= 0x7e)
395 if(HIBYTE(c) % 2)
396 c += 0x1f;
397 else
398 c += 0x7d;
400 if(LOBYTE(c) > 0x7F)
401 c += 0x1;
403 c = (((HIBYTE(c) - 0x21)/2 + 0x81) << 8) | LOBYTE(c);
405 if(HIBYTE(c) > 0x9f)
406 c += 0x4000;
408 else
409 return 0; /* Codepage is 932, but c can't be converted */
412 return c;
415 /*********************************************************************
416 * _mbsdec(MSVCRT.@)
418 unsigned char* CDECL _mbsdec(const unsigned char* start, const unsigned char* cur)
420 if(get_locale()->locinfo->mb_cur_max > 1)
421 return (unsigned char *)(_ismbstrail(start,cur-1) ? cur - 2 : cur -1);
423 return (unsigned char *)cur - 1; /* ASCII CP or SB char */
426 /*********************************************************************
427 * _mbclen(MSVCRT.@)
429 unsigned int CDECL _mbclen(const unsigned char* str)
431 return _ismbblead(*str) ? 2 : 1;
434 /*********************************************************************
435 * _mbsinc(MSVCRT.@)
437 unsigned char* CDECL _mbsinc(const unsigned char* str)
439 return (unsigned char *)(str + _mbclen(str));
442 /*********************************************************************
443 * _mbsninc(MSVCRT.@)
445 unsigned char* CDECL _mbsninc(const unsigned char* str, MSVCRT_size_t num)
447 if(!str)
448 return NULL;
450 while (num > 0 && *str)
452 if (_ismbblead(*str))
454 if (!*(str+1))
455 break;
456 str++;
458 str++;
459 num--;
462 return (unsigned char*)str;
465 /*********************************************************************
466 * _mbslen(MSVCRT.@)
468 MSVCRT_size_t CDECL _mbslen(const unsigned char* str)
470 MSVCRT_size_t len = 0;
471 while(*str)
473 if (_ismbblead(*str))
475 str++;
476 if (!*str) /* count only full chars */
477 break;
479 str++;
480 len++;
482 return len;
485 /*********************************************************************
486 * _mbccpy(MSVCRT.@)
488 void CDECL _mbccpy(unsigned char* dest, const unsigned char* src)
490 *dest = *src;
491 if(_ismbblead(*src))
492 *++dest = *++src; /* MB char */
495 /*********************************************************************
496 * _mbsncpy(MSVCRT.@)
497 * REMARKS
498 * The parameter n is the number or characters to copy, not the size of
499 * the buffer. Use _mbsnbcpy for a function analogical to strncpy
501 unsigned char* CDECL _mbsncpy(unsigned char* dst, const unsigned char* src, MSVCRT_size_t n)
503 unsigned char* ret = dst;
504 if(!n)
505 return dst;
506 if (g_mbcp_is_multibyte)
508 while (*src && n)
510 n--;
511 if (_ismbblead(*src))
513 if (!*(src+1))
515 *dst++ = 0;
516 *dst++ = 0;
517 break;
520 *dst++ = *src++;
523 *dst++ = *src++;
526 else
528 while (n)
530 n--;
531 if (!(*dst++ = *src++)) break;
534 while (n--) *dst++ = 0;
535 return ret;
538 /*********************************************************************
539 * _mbsnbcpy_s(MSVCRT.@)
540 * REMARKS
541 * Unlike _mbsnbcpy this function does not pad the rest of the dest
542 * string with 0
544 int CDECL _mbsnbcpy_s(unsigned char* dst, MSVCRT_size_t size, const unsigned char* src, MSVCRT_size_t n)
546 MSVCRT_size_t pos = 0;
548 if(!dst || size == 0)
549 return MSVCRT_EINVAL;
550 if(!src)
552 dst[0] = '\0';
553 return MSVCRT_EINVAL;
555 if(!n)
556 return 0;
558 if(g_mbcp_is_multibyte)
560 int is_lead = 0;
561 while (*src && n)
563 if(pos == size)
565 dst[0] = '\0';
566 return MSVCRT_ERANGE;
568 is_lead = (!is_lead && _ismbblead(*src));
569 n--;
570 dst[pos++] = *src++;
573 if (is_lead) /* if string ends with a lead, remove it */
574 dst[pos - 1] = 0;
576 else
578 while (n)
580 n--;
581 if(pos == size)
583 dst[0] = '\0';
584 return MSVCRT_ERANGE;
587 if(!(*src)) break;
588 dst[pos++] = *src++;
592 if(pos < size)
593 dst[pos] = '\0';
594 else
596 dst[0] = '\0';
597 return MSVCRT_ERANGE;
600 return 0;
603 /*********************************************************************
604 * _mbsnbcpy(MSVCRT.@)
605 * REMARKS
606 * Like strncpy this function doesn't enforce the string to be
607 * NUL-terminated
609 unsigned char* CDECL _mbsnbcpy(unsigned char* dst, const unsigned char* src, MSVCRT_size_t n)
611 unsigned char* ret = dst;
612 if(!n)
613 return dst;
614 if(g_mbcp_is_multibyte)
616 int is_lead = 0;
617 while (*src && n)
619 is_lead = (!is_lead && _ismbblead(*src));
620 n--;
621 *dst++ = *src++;
624 if (is_lead) /* if string ends with a lead, remove it */
625 *(dst - 1) = 0;
627 else
629 while (n)
631 n--;
632 if (!(*dst++ = *src++)) break;
635 while (n--) *dst++ = 0;
636 return ret;
639 /*********************************************************************
640 * _mbscmp(MSVCRT.@)
642 int CDECL _mbscmp(const unsigned char* str, const unsigned char* cmp)
644 if(get_locale()->locinfo->mb_cur_max > 1)
646 unsigned int strc, cmpc;
647 do {
648 if(!*str)
649 return *cmp ? -1 : 0;
650 if(!*cmp)
651 return 1;
652 strc = _mbsnextc(str);
653 cmpc = _mbsnextc(cmp);
654 if(strc != cmpc)
655 return strc < cmpc ? -1 : 1;
656 str +=(strc > 255) ? 2 : 1;
657 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
658 } while(1);
660 return u_strcmp(str, cmp); /* ASCII CP */
663 /*********************************************************************
664 * _mbsicoll(MSVCRT.@)
665 * FIXME: handle locales.
667 int CDECL _mbsicoll(const unsigned char* str, const unsigned char* cmp)
669 if(get_locale()->locinfo->mb_cur_max > 1)
671 unsigned int strc, cmpc;
672 do {
673 if(!*str)
674 return *cmp ? -1 : 0;
675 if(!*cmp)
676 return 1;
677 strc = _mbctolower(_mbsnextc(str));
678 cmpc = _mbctolower(_mbsnextc(cmp));
679 if(strc != cmpc)
680 return strc < cmpc ? -1 : 1;
681 str +=(strc > 255) ? 2 : 1;
682 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
683 } while(1);
685 return u_strcasecmp(str, cmp); /* ASCII CP */
688 /*********************************************************************
689 * _mbscoll(MSVCRT.@)
690 * Performs a case-sensitive comparison according to the current code page
691 * RETURN
692 * _NLSCMPERROR if error
693 * FIXME: handle locales.
695 int CDECL _mbscoll(const unsigned char* str, const unsigned char* cmp)
697 if(get_locale()->locinfo->mb_cur_max > 1)
699 unsigned int strc, cmpc;
700 do {
701 if(!*str)
702 return *cmp ? -1 : 0;
703 if(!*cmp)
704 return 1;
705 strc = _mbsnextc(str);
706 cmpc = _mbsnextc(cmp);
707 if(strc != cmpc)
708 return strc < cmpc ? -1 : 1;
709 str +=(strc > 255) ? 2 : 1;
710 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
711 } while(1);
713 return u_strcmp(str, cmp); /* ASCII CP */
717 /*********************************************************************
718 * _mbsicmp(MSVCRT.@)
720 int CDECL _mbsicmp(const unsigned char* str, const unsigned char* cmp)
722 if(get_locale()->locinfo->mb_cur_max > 1)
724 unsigned int strc, cmpc;
725 do {
726 if(!*str)
727 return *cmp ? -1 : 0;
728 if(!*cmp)
729 return 1;
730 strc = _mbctolower(_mbsnextc(str));
731 cmpc = _mbctolower(_mbsnextc(cmp));
732 if(strc != cmpc)
733 return strc < cmpc ? -1 : 1;
734 str +=(strc > 255) ? 2 : 1;
735 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
736 } while(1);
738 return u_strcasecmp(str, cmp); /* ASCII CP */
741 /*********************************************************************
742 * _mbsncmp(MSVCRT.@)
744 int CDECL _mbsncmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
746 if(!len)
747 return 0;
749 if(get_locale()->locinfo->mb_cur_max > 1)
751 unsigned int strc, cmpc;
752 while(len--)
754 int inc;
755 if(!*str)
756 return *cmp ? -1 : 0;
757 if(!*cmp)
758 return 1;
759 strc = _mbsnextc(str);
760 cmpc = _mbsnextc(cmp);
761 if(strc != cmpc)
762 return strc < cmpc ? -1 : 1;
763 inc=(strc > 255) ? 2 : 1; /* Equal, use same increment */
764 str += inc;
765 cmp += inc;
767 return 0; /* Matched len chars */
769 return u_strncmp(str, cmp, len); /* ASCII CP */
772 /*********************************************************************
773 * _mbsnbcmp(MSVCRT.@)
775 int CDECL _mbsnbcmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
777 if (!len)
778 return 0;
779 if(get_locale()->locinfo->mb_cur_max > 1)
781 unsigned int strc, cmpc;
782 while (len)
784 int clen;
785 if(!*str)
786 return *cmp ? -1 : 0;
787 if(!*cmp)
788 return 1;
789 if (MSVCRT_isleadbyte(*str))
791 strc=(len>=2)?_mbsnextc(str):0;
792 clen=2;
794 else
796 strc=*str;
797 clen=1;
799 if (MSVCRT_isleadbyte(*cmp))
800 cmpc=(len>=2)?_mbsnextc(cmp):0;
801 else
802 cmpc=*str;
803 if(strc != cmpc)
804 return strc < cmpc ? -1 : 1;
805 len -= clen;
806 str += clen;
807 cmp += clen;
809 return 0; /* Matched len chars */
811 return u_strncmp(str,cmp,len);
814 /*********************************************************************
815 * _mbsnicmp(MSVCRT.@)
817 * Compare two multibyte strings case insensitively to 'len' characters.
819 int CDECL _mbsnicmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
821 /* FIXME: No tolower() for mb strings yet */
822 if(get_locale()->locinfo->mb_cur_max > 1)
824 unsigned int strc, cmpc;
825 while(len--)
827 if(!*str)
828 return *cmp ? -1 : 0;
829 if(!*cmp)
830 return 1;
831 strc = _mbctolower(_mbsnextc(str));
832 cmpc = _mbctolower(_mbsnextc(cmp));
833 if(strc != cmpc)
834 return strc < cmpc ? -1 : 1;
835 str +=(strc > 255) ? 2 : 1;
836 cmp +=(strc > 255) ? 2 : 1; /* Equal, use same increment */
838 return 0; /* Matched len chars */
840 return u_strncasecmp(str, cmp, len); /* ASCII CP */
843 /*********************************************************************
844 * _mbsnbicmp(MSVCRT.@)
846 int CDECL _mbsnbicmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
848 if (!len)
849 return 0;
850 if(get_locale()->locinfo->mb_cur_max > 1)
852 unsigned int strc, cmpc;
853 while (len)
855 int clen;
856 if(!*str)
857 return *cmp ? -1 : 0;
858 if(!*cmp)
859 return 1;
860 if (MSVCRT_isleadbyte(*str))
862 strc=(len>=2)?_mbsnextc(str):0;
863 clen=2;
865 else
867 strc=*str;
868 clen=1;
870 if (MSVCRT_isleadbyte(*cmp))
871 cmpc=(len>=2)?_mbsnextc(cmp):0;
872 else
873 cmpc=*str;
874 strc = _mbctolower(strc);
875 cmpc = _mbctolower(cmpc);
876 if(strc != cmpc)
877 return strc < cmpc ? -1 : 1;
878 len -= clen;
879 str += clen;
880 cmp += clen;
882 return 0; /* Matched len bytes */
884 return u_strncasecmp(str,cmp,len);
887 /*********************************************************************
888 * _mbscat (MSVCRT.@)
890 unsigned char * CDECL _mbscat( unsigned char *dst, const unsigned char *src )
892 strcat( (char *)dst, (const char *)src );
893 return dst;
896 /*********************************************************************
897 * _mbscpy (MSVCRT.@)
899 unsigned char* CDECL _mbscpy( unsigned char *dst, const unsigned char *src )
901 strcpy( (char *)dst, (const char *)src );
902 return dst;
905 /*********************************************************************
906 * _mbsstr (MSVCRT.@)
908 unsigned char * CDECL _mbsstr(const unsigned char *haystack, const unsigned char *needle)
910 return (unsigned char *)strstr( (const char *)haystack, (const char *)needle );
913 /*********************************************************************
914 * _mbschr(MSVCRT.@)
916 * Find a multibyte character in a multibyte string.
918 unsigned char* CDECL _mbschr(const unsigned char* s, unsigned int x)
920 if(get_locale()->locinfo->mb_cur_max > 1)
922 unsigned int c;
923 while (1)
925 c = _mbsnextc(s);
926 if (c == x)
927 return (unsigned char*)s;
928 if (!c)
929 return NULL;
930 s += c > 255 ? 2 : 1;
933 return u_strchr(s, x); /* ASCII CP */
936 /*********************************************************************
937 * _mbsrchr(MSVCRT.@)
939 unsigned char* CDECL _mbsrchr(const unsigned char* s, unsigned int x)
941 if(get_locale()->locinfo->mb_cur_max > 1)
943 unsigned int c;
944 unsigned char* match=NULL;
945 if(!s)
946 return NULL;
947 while (1) {
948 c = _mbsnextc(s);
949 if (c == x)
950 match=(unsigned char*)s;
951 if (!c)
952 return match;
953 s +=(c > 255) ? 2 : 1;
956 return u_strrchr(s, x);
959 /*********************************************************************
960 * _mbstok(MSVCRT.@)
962 * Find and extract tokens from strings
964 unsigned char* CDECL _mbstok(unsigned char *str, const unsigned char *delim)
966 thread_data_t *data = msvcrt_get_thread_data();
967 unsigned char *ret;
969 if(get_locale()->locinfo->mb_cur_max > 1)
971 unsigned int c;
973 if (!str)
974 if (!(str = data->mbstok_next)) return NULL;
976 while ((c = _mbsnextc(str)) && _mbschr(delim, c)) {
977 str += c > 255 ? 2 : 1;
979 if (!*str) return NULL;
980 ret = str++;
981 while ((c = _mbsnextc(str)) && !_mbschr(delim, c)) {
982 str += c > 255 ? 2 : 1;
984 if (*str) {
985 *str++ = 0;
986 if (c > 255) *str++ = 0;
988 data->mbstok_next = str;
989 return ret;
991 return u_strtok(str, delim); /* ASCII CP */
994 /*********************************************************************
995 * mbtowc(MSVCRT.@)
997 int CDECL MSVCRT_mbtowc(MSVCRT_wchar_t *dst, const char* str, MSVCRT_size_t n)
999 /* temp var needed because MultiByteToWideChar wants non NULL destination */
1000 MSVCRT_wchar_t tmpdst = '\0';
1002 if(n <= 0 || !str)
1003 return 0;
1004 if(!MultiByteToWideChar(CP_ACP, 0, str, n, &tmpdst, 1))
1005 return -1;
1006 if(dst)
1007 *dst = tmpdst;
1008 /* return the number of bytes from src that have been used */
1009 if(!*str)
1010 return 0;
1011 if(n >= 2 && MSVCRT_isleadbyte(*str) && str[1])
1012 return 2;
1013 return 1;
1016 /*********************************************************************
1017 * _mbbtombc(MSVCRT.@)
1019 unsigned int CDECL _mbbtombc(unsigned int c)
1021 if(get_locale()->locinfo->mb_cur_max > 1 &&
1022 ((c >= 0x20 && c <=0x7e) ||(c >= 0xa1 && c <= 0xdf)))
1024 /* FIXME: I can't get this function to return anything
1025 * different from what I pass it...
1028 return c; /* ASCII CP or no MB char */
1031 /*********************************************************************
1032 * _mbbtype(MSVCRT.@)
1034 int CDECL _mbbtype(unsigned char c, int type)
1036 if (type == 1)
1038 if ((c >= 0x20 && c <= 0x7e) || (c >= 0xa1 && c <= 0xdf))
1039 return _MBC_SINGLE;
1040 else if ((c >= 0x40 && c <= 0x7e) || (c >= 0x80 && c <= 0xfc))
1041 return _MBC_TRAIL;
1042 else
1043 return _MBC_ILLEGAL;
1045 else
1047 if ((c >= 0x20 && c <= 0x7e) || (c >= 0xa1 && c <= 0xdf))
1048 return _MBC_SINGLE;
1049 else if ((c >= 0x81 && c <= 0x9f) || (c >= 0xe0 && c <= 0xfc))
1050 return _MBC_LEAD;
1051 else
1052 return _MBC_ILLEGAL;
1056 /*********************************************************************
1057 * _ismbbkana(MSVCRT.@)
1059 int CDECL _ismbbkana(unsigned int c)
1061 /* FIXME: use lc_ctype when supported, not lc_all */
1062 if(get_locale()->locinfo->lc_codepage == 932)
1064 /* Japanese/Katakana, CP 932 */
1065 return (c >= 0xa1 && c <= 0xdf);
1067 return 0;
1070 /*********************************************************************
1071 * _ismbcdigit(MSVCRT.@)
1073 int CDECL _ismbcdigit(unsigned int ch)
1075 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1076 return (get_char_typeW( wch ) & C1_DIGIT);
1079 /*********************************************************************
1080 * _ismbcgraph(MSVCRT.@)
1082 int CDECL _ismbcgraph(unsigned int ch)
1084 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1085 return (get_char_typeW( wch ) & (C1_UPPER | C1_LOWER | C1_DIGIT | C1_PUNCT | C1_ALPHA));
1088 /*********************************************************************
1089 * _ismbcalpha (MSVCRT.@)
1091 int CDECL _ismbcalpha(unsigned int ch)
1093 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1094 return (get_char_typeW( wch ) & C1_ALPHA);
1097 /*********************************************************************
1098 * _ismbclower (MSVCRT.@)
1100 int CDECL _ismbclower(unsigned int ch)
1102 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1103 return (get_char_typeW( wch ) & C1_UPPER);
1106 /*********************************************************************
1107 * _ismbcupper (MSVCRT.@)
1109 int CDECL _ismbcupper(unsigned int ch)
1111 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1112 return (get_char_typeW( wch ) & C1_LOWER);
1115 /*********************************************************************
1116 * _ismbcsymbol(MSVCRT.@)
1118 int CDECL _ismbcsymbol(unsigned int ch)
1120 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1121 WORD ctype;
1122 if (!GetStringTypeW(CT_CTYPE3, &wch, 1, &ctype))
1124 WARN("GetStringTypeW failed on %x\n", ch);
1125 return 0;
1127 return ((ctype & C3_SYMBOL) != 0);
1130 /*********************************************************************
1131 * _ismbcalnum (MSVCRT.@)
1133 int CDECL _ismbcalnum(unsigned int ch)
1135 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1136 return (get_char_typeW( wch ) & (C1_ALPHA | C1_DIGIT));
1139 /*********************************************************************
1140 * _ismbcspace (MSVCRT.@)
1142 int CDECL _ismbcspace(unsigned int ch)
1144 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1145 return (get_char_typeW( wch ) & C1_SPACE);
1148 /*********************************************************************
1149 * _ismbcprint (MSVCRT.@)
1151 int CDECL _ismbcprint(unsigned int ch)
1153 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1154 return (get_char_typeW( wch ) & (C1_UPPER | C1_LOWER | C1_DIGIT | C1_PUNCT | C1_ALPHA | C1_SPACE));
1157 /*********************************************************************
1158 * _ismbcpunct(MSVCRT.@)
1160 int CDECL _ismbcpunct(unsigned int ch)
1162 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1163 return (get_char_typeW( wch ) & C1_PUNCT);
1166 /*********************************************************************
1167 * _ismbchira(MSVCRT.@)
1169 int CDECL _ismbchira(unsigned int c)
1171 /* FIXME: use lc_ctype when supported, not lc_all */
1172 if(get_locale()->locinfo->lc_codepage == 932)
1174 /* Japanese/Hiragana, CP 932 */
1175 return (c >= 0x829f && c <= 0x82f1);
1177 return 0;
1180 /*********************************************************************
1181 * _ismbckata(MSVCRT.@)
1183 int CDECL _ismbckata(unsigned int c)
1185 /* FIXME: use lc_ctype when supported, not lc_all */
1186 if(get_locale()->locinfo->lc_codepage == 932)
1188 if(c < 256)
1189 return _ismbbkana(c);
1190 /* Japanese/Katakana, CP 932 */
1191 return (c >= 0x8340 && c <= 0x8396 && c != 0x837f);
1193 return 0;
1196 /*********************************************************************
1197 * _ismbblead(MSVCRT.@)
1199 int CDECL _ismbblead(unsigned int c)
1201 return (MSVCRT_mbctype[(c&0xff) + 1] & _M1) != 0;
1205 /*********************************************************************
1206 * _ismbbtrail(MSVCRT.@)
1208 int CDECL _ismbbtrail(unsigned int c)
1210 return (MSVCRT_mbctype[(c&0xff) + 1] & _M2) != 0;
1213 /*********************************************************************
1214 * _ismbclegal(MSVCRT.@)
1216 int CDECL _ismbclegal(unsigned int c)
1218 return _ismbblead(HIBYTE(c)) && _ismbbtrail(LOBYTE(c));
1221 /*********************************************************************
1222 * _ismbslead(MSVCRT.@)
1224 int CDECL _ismbslead(const unsigned char* start, const unsigned char* str)
1226 int lead = 0;
1228 if(!g_mbcp_is_multibyte)
1229 return 0;
1231 /* Lead bytes can also be trail bytes so we need to analyse the string
1233 while (start <= str)
1235 if (!*start)
1236 return 0;
1237 lead = !lead && _ismbblead(*start);
1238 start++;
1241 return lead ? -1 : 0;
1244 /*********************************************************************
1245 * _ismbstrail(MSVCRT.@)
1247 int CDECL _ismbstrail(const unsigned char* start, const unsigned char* str)
1249 /* Note: this function doesn't check _ismbbtrail */
1250 if ((str > start) && _ismbslead(start, str-1))
1251 return -1;
1252 else
1253 return 0;
1256 /*********************************************************************
1257 * _mbsbtype (MSVCRT.@)
1259 int CDECL _mbsbtype(const unsigned char *str, MSVCRT_size_t count)
1261 int lead = 0;
1262 const unsigned char *end = str + count;
1263 int mbcp = g_mbcp_is_multibyte;
1265 /* Lead bytes can also be trail bytes so we need to analyse the string.
1266 * Also we must return _MBC_ILLEGAL for chars past the end of the string
1268 while (str < end) /* Note: we skip the last byte - will check after the loop */
1270 if (!*str)
1271 return _MBC_ILLEGAL;
1272 lead = mbcp && !lead && _ismbblead(*str);
1273 str++;
1276 if (lead)
1277 if (_ismbbtrail(*str))
1278 return _MBC_TRAIL;
1279 else
1280 return _MBC_ILLEGAL;
1281 else
1282 if (_ismbblead(*str))
1283 return _MBC_LEAD;
1284 else
1285 return _MBC_SINGLE;
1288 /*********************************************************************
1289 * _mbsset(MSVCRT.@)
1291 unsigned char* CDECL _mbsset(unsigned char* str, unsigned int c)
1293 unsigned char* ret = str;
1295 if(get_locale()->locinfo->mb_cur_max == 1 || c < 256)
1296 return u__strset(str, c); /* ASCII CP or SB char */
1298 c &= 0xffff; /* Strip high bits */
1300 while(str[0] && str[1])
1302 *str++ = c >> 8;
1303 *str++ = c & 0xff;
1305 if(str[0])
1306 str[0] = '\0'; /* FIXME: OK to shorten? */
1308 return ret;
1311 /*********************************************************************
1312 * _mbsnbset(MSVCRT.@)
1314 unsigned char* CDECL _mbsnbset(unsigned char *str, unsigned int c, MSVCRT_size_t len)
1316 unsigned char *ret = str;
1318 if(!len)
1319 return ret;
1321 if(get_locale()->locinfo->mb_cur_max == 1 || c < 256)
1322 return u__strnset(str, c, len); /* ASCII CP or SB char */
1324 c &= 0xffff; /* Strip high bits */
1326 while(str[0] && str[1] && (len > 1))
1328 *str++ = c >> 8;
1329 len--;
1330 *str++ = c & 0xff;
1331 len--;
1333 if(len && str[0]) {
1334 /* as per msdn pad with a blank character */
1335 str[0] = ' ';
1338 return ret;
1341 /*********************************************************************
1342 * _mbsnset(MSVCRT.@)
1344 unsigned char* CDECL _mbsnset(unsigned char* str, unsigned int c, MSVCRT_size_t len)
1346 unsigned char *ret = str;
1348 if(!len)
1349 return ret;
1351 if(get_locale()->locinfo->mb_cur_max == 1 || c < 256)
1352 return u__strnset(str, c, len); /* ASCII CP or SB char */
1354 c &= 0xffff; /* Strip high bits */
1356 while(str[0] && str[1] && len--)
1358 *str++ = c >> 8;
1359 *str++ = c & 0xff;
1361 if(len && str[0])
1362 str[0] = '\0'; /* FIXME: OK to shorten? */
1364 return ret;
1367 /*********************************************************************
1368 * _mbsnccnt(MSVCRT.@)
1369 * 'c' is for 'character'.
1371 MSVCRT_size_t CDECL _mbsnccnt(const unsigned char* str, MSVCRT_size_t len)
1373 MSVCRT_size_t ret;
1374 if(get_locale()->locinfo->mb_cur_max > 1)
1376 ret=0;
1377 while(*str && len-- > 0)
1379 if(MSVCRT_isleadbyte(*str))
1381 if (!len)
1382 break;
1383 len--;
1384 str++;
1386 str++;
1387 ret++;
1389 return ret;
1391 ret=u_strlen(str);
1392 return min(ret, len); /* ASCII CP */
1395 /*********************************************************************
1396 * _mbsnbcnt(MSVCRT.@)
1397 * 'b' is for byte count.
1399 MSVCRT_size_t CDECL _mbsnbcnt(const unsigned char* str, MSVCRT_size_t len)
1401 MSVCRT_size_t ret;
1402 if(get_locale()->locinfo->mb_cur_max > 1)
1404 const unsigned char* xstr = str;
1405 while(*xstr && len-- > 0)
1407 if (MSVCRT_isleadbyte(*xstr++))
1408 xstr++;
1410 return xstr-str;
1412 ret=u_strlen(str);
1413 return min(ret, len); /* ASCII CP */
1416 /*********************************************************************
1417 * _mbsnbcat(MSVCRT.@)
1419 unsigned char* CDECL _mbsnbcat(unsigned char* dst, const unsigned char* src, MSVCRT_size_t len)
1421 if(get_locale()->locinfo->mb_cur_max > 1)
1423 unsigned char *res = dst;
1424 while (*dst) {
1425 if (MSVCRT_isleadbyte(*dst++)) {
1426 if (*dst) {
1427 dst++;
1428 } else {
1429 /* as per msdn overwrite the lead byte in front of '\0' */
1430 dst--;
1431 break;
1435 while (*src && len--) *dst++ = *src++;
1436 *dst = '\0';
1437 return res;
1439 return u_strncat(dst, src, len); /* ASCII CP */
1442 int CDECL _mbsnbcat_s(unsigned char *dst, MSVCRT_size_t size, const unsigned char *src, MSVCRT_size_t len)
1444 unsigned char *ptr = dst;
1445 MSVCRT_size_t i;
1447 if (!dst && !size && !src && !len)
1448 return 0;
1450 if (!dst || !size || !src)
1452 if (dst && size)
1453 *dst = '\0';
1455 *MSVCRT__errno() = MSVCRT_EINVAL;
1456 return MSVCRT_EINVAL;
1459 /* Find the null terminator of the destination buffer. */
1460 while (size && *ptr)
1461 size--, ptr++;
1463 if (!size)
1465 *dst = '\0';
1466 *MSVCRT__errno() = MSVCRT_EINVAL;
1467 return MSVCRT_EINVAL;
1470 /* If necessary, check that the character preceding the null terminator is
1471 * a lead byte and move the pointer back by one for later overwrite. */
1472 if (ptr != dst && get_locale()->locinfo->mb_cur_max > 1 && MSVCRT_isleadbyte(*(ptr - 1)))
1473 size++, ptr--;
1475 for (i = 0; *src && i < len; i++)
1477 *ptr++ = *src++;
1478 size--;
1480 if (!size)
1482 *dst = '\0';
1483 *MSVCRT__errno() = MSVCRT_ERANGE;
1484 return MSVCRT_ERANGE;
1488 *ptr = '\0';
1489 return 0;
1492 /*********************************************************************
1493 * _mbsncat(MSVCRT.@)
1495 unsigned char* CDECL _mbsncat(unsigned char* dst, const unsigned char* src, MSVCRT_size_t len)
1497 if(get_locale()->locinfo->mb_cur_max > 1)
1499 unsigned char *res = dst;
1500 while (*dst)
1502 if (MSVCRT_isleadbyte(*dst++))
1503 dst++;
1505 while (*src && len--)
1507 *dst++ = *src;
1508 if(MSVCRT_isleadbyte(*src++))
1509 *dst++ = *src++;
1511 *dst = '\0';
1512 return res;
1514 return u_strncat(dst, src, len); /* ASCII CP */
1518 /*********************************************************************
1519 * _mbslwr(MSVCRT.@)
1521 unsigned char* CDECL _mbslwr(unsigned char* s)
1523 unsigned char *ret = s;
1524 if (!s)
1525 return NULL;
1526 if (get_locale()->locinfo->mb_cur_max > 1)
1528 unsigned int c;
1529 while (*s)
1531 c = _mbctolower(_mbsnextc(s));
1532 /* Note that I assume that the size of the character is unchanged */
1533 if (c > 255)
1535 *s++=(c>>8);
1536 c=c & 0xff;
1538 *s++=c;
1541 else for ( ; *s; s++) *s = tolower(*s);
1542 return ret;
1546 /*********************************************************************
1547 * _mbslwr_s(MSVCRT.@)
1549 int CDECL _mbslwr_s(unsigned char* s, MSVCRT_size_t len)
1551 if (!s && !len)
1553 return 0;
1555 else if (!s || !len)
1557 *MSVCRT__errno() = MSVCRT_EINVAL;
1558 return MSVCRT_EINVAL;
1560 if (get_locale()->locinfo->mb_cur_max > 1)
1562 unsigned int c;
1563 for ( ; *s && len > 0; len--)
1565 c = _mbctolower(_mbsnextc(s));
1566 /* Note that I assume that the size of the character is unchanged */
1567 if (c > 255)
1569 *s++=(c>>8);
1570 c=c & 0xff;
1572 *s++=c;
1575 else for ( ; *s && len > 0; s++, len--) *s = tolower(*s);
1576 if (*s)
1578 *s = '\0';
1579 *MSVCRT__errno() = MSVCRT_EINVAL;
1580 return MSVCRT_EINVAL;
1582 return 0;
1586 /*********************************************************************
1587 * _mbsupr(MSVCRT.@)
1589 unsigned char* CDECL _mbsupr(unsigned char* s)
1591 unsigned char *ret = s;
1592 if (!s)
1593 return NULL;
1594 if (get_locale()->locinfo->mb_cur_max > 1)
1596 unsigned int c;
1597 while (*s)
1599 c = _mbctoupper(_mbsnextc(s));
1600 /* Note that I assume that the size of the character is unchanged */
1601 if (c > 255)
1603 *s++=(c>>8);
1604 c=c & 0xff;
1606 *s++=c;
1609 else for ( ; *s; s++) *s = toupper(*s);
1610 return ret;
1614 /*********************************************************************
1615 * _mbsupr_s(MSVCRT.@)
1617 int CDECL _mbsupr_s(unsigned char* s, MSVCRT_size_t len)
1619 if (!s && !len)
1621 return 0;
1623 else if (!s || !len)
1625 *MSVCRT__errno() = MSVCRT_EINVAL;
1626 return MSVCRT_EINVAL;
1628 if (get_locale()->locinfo->mb_cur_max > 1)
1630 unsigned int c;
1631 for ( ; *s && len > 0; len--)
1633 c = _mbctoupper(_mbsnextc(s));
1634 /* Note that I assume that the size of the character is unchanged */
1635 if (c > 255)
1637 *s++=(c>>8);
1638 c=c & 0xff;
1640 *s++=c;
1643 else for ( ; *s && len > 0; s++, len--) *s = toupper(*s);
1644 if (*s)
1646 *s = '\0';
1647 *MSVCRT__errno() = MSVCRT_EINVAL;
1648 return MSVCRT_EINVAL;
1650 return 0;
1654 /*********************************************************************
1655 * _mbsspn (MSVCRT.@)
1657 MSVCRT_size_t CDECL _mbsspn(const unsigned char* string, const unsigned char* set)
1659 const unsigned char *p, *q;
1661 for (p = string; *p; p++)
1663 if (MSVCRT_isleadbyte(*p))
1665 for (q = set; *q; q++)
1667 if (!q[1])
1668 break;
1669 if ((*p == *q) && (p[1] == q[1]))
1670 break;
1671 q++;
1673 if (!q[0] || !q[1]) break;
1675 else
1677 for (q = set; *q; q++)
1678 if (*p == *q)
1679 break;
1680 if (!*q) break;
1683 return p - string;
1686 /*********************************************************************
1687 * _mbsspnp (MSVCRT.@)
1689 unsigned char* CDECL _mbsspnp(const unsigned char* string, const unsigned char* set)
1691 const unsigned char *p, *q;
1693 for (p = string; *p; p++)
1695 if (MSVCRT_isleadbyte(*p))
1697 for (q = set; *q; q++)
1699 if (!q[1])
1700 break;
1701 if ((*p == *q) && (p[1] == q[1]))
1702 break;
1703 q++;
1705 if (!q[0] || !q[1]) break;
1707 else
1709 for (q = set; *q; q++)
1710 if (*p == *q)
1711 break;
1712 if (!*q) break;
1715 if (*p == '\0')
1716 return NULL;
1717 return (unsigned char *)p;
1720 /*********************************************************************
1721 * _mbscspn(MSVCRT.@)
1723 MSVCRT_size_t CDECL _mbscspn(const unsigned char* str, const unsigned char* cmp)
1725 if (get_locale()->locinfo->mb_cur_max > 1)
1726 FIXME("don't handle double character case\n");
1727 return u_strcspn(str, cmp);
1730 /*********************************************************************
1731 * _mbsrev (MSVCRT.@)
1733 unsigned char* CDECL _mbsrev(unsigned char* str)
1735 int i, len = _mbslen(str);
1736 unsigned char *p, *temp=MSVCRT_malloc(len*2);
1738 if(!temp)
1739 return str;
1741 /* unpack multibyte string to temp buffer */
1742 p=str;
1743 for(i=0; i<len; i++)
1745 if (MSVCRT_isleadbyte(*p))
1747 temp[i*2]=*p++;
1748 temp[i*2+1]=*p++;
1750 else
1752 temp[i*2]=*p++;
1753 temp[i*2+1]=0;
1757 /* repack it in the reverse order */
1758 p=str;
1759 for(i=len-1; i>=0; i--)
1761 if(MSVCRT_isleadbyte(temp[i*2]))
1763 *p++=temp[i*2];
1764 *p++=temp[i*2+1];
1766 else
1768 *p++=temp[i*2];
1772 MSVCRT_free(temp);
1774 return str;
1777 /*********************************************************************
1778 * _mbspbrk (MSVCRT.@)
1780 unsigned char* CDECL _mbspbrk(const unsigned char* str, const unsigned char* accept)
1782 const unsigned char* p;
1784 while(*str)
1786 for(p = accept; *p; p += (MSVCRT_isleadbyte(*p)?2:1) )
1788 if (*p == *str)
1789 if( !MSVCRT_isleadbyte(*p) || ( *(p+1) == *(str+1) ) )
1790 return (unsigned char*)str;
1792 str += (MSVCRT_isleadbyte(*str)?2:1);
1794 return NULL;
1799 * Functions depending on locale codepage
1802 /*********************************************************************
1803 * mblen(MSVCRT.@)
1804 * REMARKS
1805 * Unlike most of the multibyte string functions this function uses
1806 * the locale codepage, not the codepage set by _setmbcp
1808 int CDECL MSVCRT_mblen(const char* str, MSVCRT_size_t size)
1810 if (str && *str && size)
1812 if(get_locale()->locinfo->mb_cur_max == 1)
1813 return 1; /* ASCII CP */
1815 return !MSVCRT_isleadbyte(*str) ? 1 : (size>1 ? 2 : -1);
1817 return 0;
1820 /*********************************************************************
1821 * _mbstrlen_l(MSVCRT.@)
1823 MSVCRT_size_t CDECL _mbstrlen_l(const char* str, MSVCRT__locale_t locale)
1825 if(!locale)
1826 locale = get_locale();
1828 if(locale->locinfo->mb_cur_max > 1) {
1829 MSVCRT_size_t len = 0;
1830 while(*str) {
1831 /* FIXME: According to the documentation we are supposed to test for
1832 * multi-byte character validity. Whatever that means
1834 str += MSVCRT_isleadbyte(*str) ? 2 : 1;
1835 len++;
1837 return len;
1840 return strlen(str);
1843 /*********************************************************************
1844 * _mbstrlen(MSVCRT.@)
1846 MSVCRT_size_t CDECL _mbstrlen(const char* str)
1848 return _mbstrlen_l(str, NULL);
1851 /*********************************************************************
1852 * _mbstowcs_l(MSVCRT.@)
1854 MSVCRT_size_t CDECL MSVCRT__mbstowcs_l(MSVCRT_wchar_t *wcstr, const char *mbstr,
1855 MSVCRT_size_t count, MSVCRT__locale_t locale)
1857 MSVCRT_size_t i, size;
1859 if(!locale)
1860 locale = get_locale();
1862 /* Ignore count parameter */
1863 if(!wcstr)
1864 return MultiByteToWideChar(locale->locinfo->lc_codepage, 0, mbstr, -1, NULL, 0)-1;
1866 for(i=0, size=0; i<count; i++) {
1867 if(mbstr[size] == '\0')
1868 break;
1870 size += (MSVCRT_isleadbyte(mbstr[size]) ? 2 : 1);
1873 size = MultiByteToWideChar(locale->locinfo->lc_codepage, 0,
1874 mbstr, size, wcstr, count);
1876 if(size<count && wcstr)
1877 wcstr[size] = '\0';
1879 return size;
1882 /*********************************************************************
1883 * mbstowcs(MSVCRT.@)
1885 MSVCRT_size_t CDECL MSVCRT_mbstowcs(MSVCRT_wchar_t *wcstr,
1886 const char *mbstr, MSVCRT_size_t count)
1888 return MSVCRT__mbstowcs_l(wcstr, mbstr, count, NULL);
1891 /*********************************************************************
1892 * _mbstowcs_s_l(MSVCRT.@)
1894 int CDECL MSVCRT__mbstowcs_s_l(MSVCRT_size_t *ret, MSVCRT_wchar_t *wcstr,
1895 MSVCRT_size_t size, const char *mbstr,
1896 MSVCRT_size_t count, MSVCRT__locale_t locale)
1898 MSVCRT_size_t conv;
1900 if(!wcstr && !size) {
1901 conv = MSVCRT__mbstowcs_l(NULL, mbstr, 0, locale);
1902 if(ret)
1903 *ret = conv+1;
1904 return 0;
1907 if(!MSVCRT_CHECK_PMT(mbstr != NULL) || !MSVCRT_CHECK_PMT(wcstr != NULL)) {
1908 if(wcstr && size)
1909 wcstr[0] = '\0';
1910 *MSVCRT__errno() = MSVCRT_EINVAL;
1911 return MSVCRT_EINVAL;
1914 if(count==MSVCRT__TRUNCATE || size<count)
1915 conv = size;
1916 else
1917 conv = count;
1919 conv = MSVCRT__mbstowcs_l(wcstr, mbstr, conv, locale);
1920 if(conv<size)
1921 wcstr[conv++] = '\0';
1922 else if(conv==size && (count==MSVCRT__TRUNCATE || wcstr[conv-1]=='\0'))
1923 wcstr[conv-1] = '\0';
1924 else {
1925 MSVCRT_INVALID_PMT("wcstr[size] is too small");
1926 if(size)
1927 wcstr[0] = '\0';
1928 *MSVCRT__errno() = MSVCRT_ERANGE;
1929 return MSVCRT_ERANGE;
1932 if(ret)
1933 *ret = conv;
1934 return 0;
1937 /*********************************************************************
1938 * mbstowcs_s(MSVCRT.@)
1940 int CDECL MSVCRT__mbstowcs_s(MSVCRT_size_t *ret, MSVCRT_wchar_t *wcstr,
1941 MSVCRT_size_t size, const char *mbstr, MSVCRT_size_t count)
1943 return MSVCRT__mbstowcs_s_l(ret, wcstr, size, mbstr, count, NULL);