msvcrt: Test and fix _mbsinc/_mbsninc.
[wine/wine64.git] / dlls / msvcrt / mbcs.c
blob718e387133218acfa90a46be83ba7b96428f0682
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"
29 #include "msvcrt/mbctype.h"
30 #include "msvcrt/mbstring.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
34 unsigned char MSVCRT_mbctype[257];
35 static int g_mbcp_is_multibyte = 0;
37 int MSVCRT___mb_cur_max = 1;
38 extern int MSVCRT___lc_collate_cp;
40 /* It seems that the data about valid trail bytes is not available from kernel32
41 * so we have to store is here. The format is the same as for lead bytes in CPINFO */
42 struct cp_extra_info_t
44 int cp;
45 BYTE TrailBytes[MAX_LEADBYTES];
48 static struct cp_extra_info_t g_cpextrainfo[] =
50 {932, {0x40, 0x7e, 0x80, 0xfc, 0, 0}},
51 {936, {0x40, 0xfe, 0, 0}},
52 {949, {0x41, 0xfe, 0, 0}},
53 {950, {0x40, 0x7e, 0xa1, 0xfe, 0, 0}},
54 {20932, {1, 255, 0, 0}}, /* seems to give different results on different systems */
55 {0, {1, 255, 0, 0}} /* match all with FIXME */
58 static MSVCRT_wchar_t msvcrt_mbc_to_wc(unsigned int ch)
60 MSVCRT_wchar_t chW;
61 char mbch[2];
62 int n_chars;
64 if (ch <= 0xff) {
65 mbch[0] = ch;
66 n_chars = 1;
67 } else {
68 mbch[0] = (ch >> 8) & 0xff;
69 mbch[1] = ch & 0xff;
70 n_chars = 2;
72 if (!MultiByteToWideChar(MSVCRT___lc_codepage, 0, mbch, n_chars, &chW, 1))
74 WARN("MultiByteToWideChar failed on %x\n", ch);
75 return 0;
77 return chW;
80 static inline size_t u_strlen( const unsigned char *str )
82 return strlen( (const char*) str );
85 static inline unsigned char* u_strncat( unsigned char* dst, const unsigned char* src, size_t len )
87 return (unsigned char*)strncat( (char*)dst, (const char*)src, len);
90 static inline int u_strcmp( const unsigned char *s1, const unsigned char *s2 )
92 return strcmp( (const char*)s1, (const char*)s2 );
95 static inline int u_strcasecmp( const unsigned char *s1, const unsigned char *s2 )
97 return strcasecmp( (const char*)s1, (const char*)s2 );
100 static inline int u_strncmp( const unsigned char *s1, const unsigned char *s2, size_t len )
102 return strncmp( (const char*)s1, (const char*)s2, len );
105 static inline int u_strncasecmp( const unsigned char *s1, const unsigned char *s2, size_t len )
107 return strncasecmp( (const char*)s1, (const char*)s2, len );
110 static inline unsigned char *u_strchr( const unsigned char *s, unsigned char x )
112 return (unsigned char*) strchr( (const char*)s, x );
115 static inline unsigned char *u_strrchr( const unsigned char *s, unsigned char x )
117 return (unsigned char*) strrchr( (const char*)s, x );
120 static inline unsigned char *u_strtok( unsigned char *s, const unsigned char *delim )
122 return (unsigned char*) strtok( (char*)s, (const char*)delim );
125 static inline unsigned char *u__strset( unsigned char *s, unsigned char c )
127 return (unsigned char*) _strset( (char*)s, c);
130 static inline unsigned char *u__strnset( unsigned char *s, unsigned char c, size_t len )
132 return (unsigned char*) _strnset( (char*)s, c, len );
135 static inline size_t u_strcspn( const unsigned char *s, const unsigned char *rej )
137 return strcspn( (const char *)s, (const char*)rej );
140 /*********************************************************************
141 * __p__mbctype (MSVCRT.@)
143 unsigned char* CDECL __p__mbctype(void)
145 return MSVCRT_mbctype;
148 /*********************************************************************
149 * __p___mb_cur_max(MSVCRT.@)
151 int* CDECL __p___mb_cur_max(void)
153 return &MSVCRT___mb_cur_max;
156 /*********************************************************************
157 * _setmbcp (MSVCRT.@)
159 int CDECL _setmbcp(int cp)
161 int newcp;
162 CPINFO cpi;
163 BYTE *bytes;
164 WORD chartypes[256];
165 WORD *curr_type;
166 char bufA[256];
167 WCHAR bufW[256];
168 int charcount;
169 int ret;
170 int i;
172 switch (cp)
174 case _MB_CP_ANSI:
175 newcp = GetACP();
176 break;
177 case _MB_CP_OEM:
178 newcp = GetOEMCP();
179 break;
180 case _MB_CP_LOCALE:
181 newcp = MSVCRT___lc_codepage;
182 break;
183 case _MB_CP_SBCS:
184 newcp = 20127; /* ASCII */
185 break;
186 default:
187 newcp = cp;
188 break;
191 if (!GetCPInfo(newcp, &cpi))
193 WARN("Codepage %d not found\n", newcp);
194 msvcrt_set_errno(MSVCRT_EINVAL);
195 return -1;
198 /* setup the _mbctype */
199 memset(MSVCRT_mbctype, 0, sizeof(MSVCRT_mbctype));
201 bytes = cpi.LeadByte;
202 while (bytes[0] || bytes[1])
204 for (i = bytes[0]; i <= bytes[1]; i++)
205 MSVCRT_mbctype[i + 1] |= _M1;
206 bytes += 2;
209 if (cpi.MaxCharSize > 1)
211 /* trail bytes not available through kernel32 but stored in a structure in msvcrt */
212 struct cp_extra_info_t *cpextra = g_cpextrainfo;
214 g_mbcp_is_multibyte = 1;
215 while (TRUE)
217 if (cpextra->cp == 0 || cpextra->cp == newcp)
219 if (cpextra->cp == 0)
220 FIXME("trail bytes data not available for DBCS codepage %d - assuming all bytes\n", newcp);
222 bytes = cpextra->TrailBytes;
223 while (bytes[0] || bytes[1])
225 for (i = bytes[0]; i <= bytes[1]; i++)
226 MSVCRT_mbctype[i + 1] |= _M2;
227 bytes += 2;
229 break;
231 cpextra++;
234 else
235 g_mbcp_is_multibyte = 0;
237 /* we can't use GetStringTypeA directly because we don't have a locale - only a code page
239 charcount = 0;
240 for (i = 0; i < 256; i++)
241 if (!(MSVCRT_mbctype[i + 1] & _M1))
242 bufA[charcount++] = i;
244 ret = MultiByteToWideChar(newcp, 0, bufA, charcount, bufW, charcount);
245 if (ret != charcount)
246 ERR("MultiByteToWideChar of chars failed for cp %d, ret=%d (exp %d), error=%d\n", newcp, ret, charcount, GetLastError());
248 GetStringTypeW(CT_CTYPE1, bufW, charcount, chartypes);
250 curr_type = chartypes;
251 for (i = 0; i < 256; i++)
252 if (!(MSVCRT_mbctype[i + 1] & _M1))
254 if ((*curr_type) & C1_UPPER)
255 MSVCRT_mbctype[i + 1] |= _SBUP;
256 if ((*curr_type) & C1_LOWER)
257 MSVCRT_mbctype[i + 1] |= _SBLOW;
258 curr_type++;
261 if (newcp == 932) /* CP932 only - set _MP and _MS */
263 /* On Windows it's possible to calculate the _MP and _MS from CT_CTYPE1
264 * and CT_CTYPE3. But as of Wine 0.9.43 we return wrong values what makes
265 * it hard. As this is set only for codepage 932 we hardcode it what gives
266 * also faster execution.
268 for (i = 161; i <= 165; i++)
269 MSVCRT_mbctype[i + 1] |= _MP;
270 for (i = 166; i <= 223; i++)
271 MSVCRT_mbctype[i + 1] |= _MS;
274 MSVCRT___lc_collate_cp = MSVCRT___lc_codepage = newcp;
275 TRACE("(%d) -> %d\n", cp, MSVCRT___lc_codepage);
276 return 0;
279 /*********************************************************************
280 * _getmbcp (MSVCRT.@)
282 int CDECL _getmbcp(void)
284 return MSVCRT___lc_codepage;
287 /*********************************************************************
288 * _mbsnextc(MSVCRT.@)
290 unsigned int CDECL _mbsnextc(const unsigned char* str)
292 if(_ismbblead(*str))
293 return *str << 8 | str[1];
294 return *str;
297 /*********************************************************************
298 * _mbctolower(MSVCRT.@)
300 unsigned int CDECL _mbctolower(unsigned int c)
302 if (MSVCRT_isleadbyte(c))
304 FIXME("Handle MBC chars\n");
305 return c;
307 return tolower(c); /* ASCII CP or SB char */
310 /*********************************************************************
311 * _mbctoupper(MSVCRT.@)
313 unsigned int CDECL _mbctoupper(unsigned int c)
315 if (MSVCRT_isleadbyte(c))
317 FIXME("Handle MBC chars\n");
318 return c;
320 return toupper(c); /* ASCII CP or SB char */
323 /*********************************************************************
324 * _mbsdec(MSVCRT.@)
326 unsigned char* CDECL _mbsdec(const unsigned char* start, const unsigned char* cur)
328 if(MSVCRT___mb_cur_max > 1)
329 return (unsigned char *)(_ismbstrail(start,cur-1) ? cur - 2 : cur -1);
331 return (unsigned char *)cur - 1; /* ASCII CP or SB char */
334 /*********************************************************************
335 * _mbsinc(MSVCRT.@)
337 unsigned char* CDECL _mbsinc(const unsigned char* str)
339 return (unsigned char *)(str + _mbclen(str));
342 /*********************************************************************
343 * _mbsninc(MSVCRT.@)
345 unsigned char* CDECL _mbsninc(const unsigned char* str, MSVCRT_size_t num)
347 if(!str)
348 return NULL;
350 while (num > 0 && *str)
352 if (_ismbblead(*str))
354 if (!*(str+1))
355 break;
356 str++;
358 str++;
359 num--;
362 return (unsigned char*)str;
365 /*********************************************************************
366 * _mbclen(MSVCRT.@)
368 unsigned int CDECL _mbclen(const unsigned char* str)
370 return _ismbblead(*str) ? 2 : 1;
373 /*********************************************************************
374 * _mbslen(MSVCRT.@)
376 MSVCRT_size_t CDECL _mbslen(const unsigned char* str)
378 MSVCRT_size_t len = 0;
379 while(*str)
381 if (_ismbblead(*str))
383 str++;
384 if (!*str) /* count only full chars */
385 break;
387 str++;
388 len++;
390 return len;
393 /*********************************************************************
394 * _mbccpy(MSVCRT.@)
396 void CDECL _mbccpy(unsigned char* dest, const unsigned char* src)
398 *dest = *src;
399 if(_ismbblead(*src))
400 *++dest = *++src; /* MB char */
403 /*********************************************************************
404 * _mbsncpy(MSVCRT.@)
405 * REMARKS
406 * The parameter n is the number or characters to copy, not the size of
407 * the buffer. Use _mbsnbcpy for a function analogical to strncpy
409 unsigned char* CDECL _mbsncpy(unsigned char* dst, const unsigned char* src, MSVCRT_size_t n)
411 unsigned char* ret = dst;
412 if(!n)
413 return dst;
414 if (g_mbcp_is_multibyte)
416 while (*src && n)
418 n--;
419 if (_ismbblead(*src))
421 if (!*(src+1))
423 *dst++ = 0;
424 *dst++ = 0;
425 break;
428 *dst++ = *src++;
431 *dst++ = *src++;
434 else
436 while (n)
438 n--;
439 if (!(*dst++ = *src++)) break;
442 while (n--) *dst++ = 0;
443 return ret;
446 /*********************************************************************
447 * _mbsnbcpy(MSVCRT.@)
448 * REMARKS
449 * Like strncpy this function doesn't enforce the string to be
450 * NUL-terminated
452 unsigned char* CDECL _mbsnbcpy(unsigned char* dst, const unsigned char* src, MSVCRT_size_t n)
454 unsigned char* ret = dst;
455 if(!n)
456 return dst;
457 if(g_mbcp_is_multibyte)
459 int is_lead = 0;
460 while (*src && n)
462 is_lead = (!is_lead && _ismbblead(*src));
463 n--;
464 *dst++ = *src++;
467 if (is_lead) /* if string ends with a lead, remove it */
468 *(dst - 1) = 0;
470 else
472 while (n)
474 n--;
475 if (!(*dst++ = *src++)) break;
478 while (n--) *dst++ = 0;
479 return ret;
482 /*********************************************************************
483 * _mbscmp(MSVCRT.@)
485 int CDECL _mbscmp(const unsigned char* str, const unsigned char* cmp)
487 if(MSVCRT___mb_cur_max > 1)
489 unsigned int strc, cmpc;
490 do {
491 if(!*str)
492 return *cmp ? -1 : 0;
493 if(!*cmp)
494 return 1;
495 strc = _mbsnextc(str);
496 cmpc = _mbsnextc(cmp);
497 if(strc != cmpc)
498 return strc < cmpc ? -1 : 1;
499 str +=(strc > 255) ? 2 : 1;
500 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
501 } while(1);
503 return u_strcmp(str, cmp); /* ASCII CP */
506 /*********************************************************************
507 * _mbsicoll(MSVCRT.@)
508 * FIXME: handle locales.
510 int CDECL _mbsicoll(const unsigned char* str, const unsigned char* cmp)
512 if(MSVCRT___mb_cur_max > 1)
514 unsigned int strc, cmpc;
515 do {
516 if(!*str)
517 return *cmp ? -1 : 0;
518 if(!*cmp)
519 return 1;
520 strc = _mbctolower(_mbsnextc(str));
521 cmpc = _mbctolower(_mbsnextc(cmp));
522 if(strc != cmpc)
523 return strc < cmpc ? -1 : 1;
524 str +=(strc > 255) ? 2 : 1;
525 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
526 } while(1);
528 return u_strcasecmp(str, cmp); /* ASCII CP */
531 /*********************************************************************
532 * _mbscoll(MSVCRT.@)
533 * Performs a case-sensitive comparison according to the current code page
534 * RETURN
535 * _NLSCMPERROR if error
536 * FIXME: handle locales.
538 int CDECL _mbscoll(const unsigned char* str, const unsigned char* cmp)
540 if(MSVCRT___mb_cur_max > 1)
542 unsigned int strc, cmpc;
543 do {
544 if(!*str)
545 return *cmp ? -1 : 0;
546 if(!*cmp)
547 return 1;
548 strc = _mbsnextc(str);
549 cmpc = _mbsnextc(cmp);
550 if(strc != cmpc)
551 return strc < cmpc ? -1 : 1;
552 str +=(strc > 255) ? 2 : 1;
553 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
554 } while(1);
556 return u_strcmp(str, cmp); /* ASCII CP */
560 /*********************************************************************
561 * _mbsicmp(MSVCRT.@)
563 int CDECL _mbsicmp(const unsigned char* str, const unsigned char* cmp)
565 if(MSVCRT___mb_cur_max > 1)
567 unsigned int strc, cmpc;
568 do {
569 if(!*str)
570 return *cmp ? -1 : 0;
571 if(!*cmp)
572 return 1;
573 strc = _mbctolower(_mbsnextc(str));
574 cmpc = _mbctolower(_mbsnextc(cmp));
575 if(strc != cmpc)
576 return strc < cmpc ? -1 : 1;
577 str +=(strc > 255) ? 2 : 1;
578 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
579 } while(1);
581 return u_strcasecmp(str, cmp); /* ASCII CP */
584 /*********************************************************************
585 * _mbsncmp(MSVCRT.@)
587 int CDECL _mbsncmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
589 if(!len)
590 return 0;
592 if(MSVCRT___mb_cur_max > 1)
594 unsigned int strc, cmpc;
595 while(len--)
597 int inc;
598 if(!*str)
599 return *cmp ? -1 : 0;
600 if(!*cmp)
601 return 1;
602 strc = _mbsnextc(str);
603 cmpc = _mbsnextc(cmp);
604 if(strc != cmpc)
605 return strc < cmpc ? -1 : 1;
606 inc=(strc > 255) ? 2 : 1; /* Equal, use same increment */
607 str += inc;
608 cmp += inc;
610 return 0; /* Matched len chars */
612 return u_strncmp(str, cmp, len); /* ASCII CP */
615 /*********************************************************************
616 * _mbsnbcmp(MSVCRT.@)
618 int CDECL _mbsnbcmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
620 if (!len)
621 return 0;
622 if(MSVCRT___mb_cur_max > 1)
624 unsigned int strc, cmpc;
625 while (len)
627 int clen;
628 if(!*str)
629 return *cmp ? -1 : 0;
630 if(!*cmp)
631 return 1;
632 if (MSVCRT_isleadbyte(*str))
634 strc=(len>=2)?_mbsnextc(str):0;
635 clen=2;
637 else
639 strc=*str;
640 clen=1;
642 if (MSVCRT_isleadbyte(*cmp))
643 cmpc=(len>=2)?_mbsnextc(cmp):0;
644 else
645 cmpc=*str;
646 if(strc != cmpc)
647 return strc < cmpc ? -1 : 1;
648 len -= clen;
649 str += clen;
650 cmp += clen;
652 return 0; /* Matched len chars */
654 return u_strncmp(str,cmp,len);
657 /*********************************************************************
658 * _mbsnicmp(MSVCRT.@)
660 * Compare two multibyte strings case insensitively to 'len' characters.
662 int CDECL _mbsnicmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
664 /* FIXME: No tolower() for mb strings yet */
665 if(MSVCRT___mb_cur_max > 1)
667 unsigned int strc, cmpc;
668 while(len--)
670 if(!*str)
671 return *cmp ? -1 : 0;
672 if(!*cmp)
673 return 1;
674 strc = _mbctolower(_mbsnextc(str));
675 cmpc = _mbctolower(_mbsnextc(cmp));
676 if(strc != cmpc)
677 return strc < cmpc ? -1 : 1;
678 str +=(strc > 255) ? 2 : 1;
679 cmp +=(strc > 255) ? 2 : 1; /* Equal, use same increment */
681 return 0; /* Matched len chars */
683 return u_strncasecmp(str, cmp, len); /* ASCII CP */
686 /*********************************************************************
687 * _mbsnbicmp(MSVCRT.@)
689 int CDECL _mbsnbicmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
691 if (!len)
692 return 0;
693 if(MSVCRT___mb_cur_max > 1)
695 unsigned int strc, cmpc;
696 while (len)
698 int clen;
699 if(!*str)
700 return *cmp ? -1 : 0;
701 if(!*cmp)
702 return 1;
703 if (MSVCRT_isleadbyte(*str))
705 strc=(len>=2)?_mbsnextc(str):0;
706 clen=2;
708 else
710 strc=*str;
711 clen=1;
713 if (MSVCRT_isleadbyte(*cmp))
714 cmpc=(len>=2)?_mbsnextc(cmp):0;
715 else
716 cmpc=*str;
717 strc = _mbctolower(strc);
718 cmpc = _mbctolower(cmpc);
719 if(strc != cmpc)
720 return strc < cmpc ? -1 : 1;
721 len -= clen;
722 str += clen;
723 cmp += clen;
725 return 0; /* Matched len bytes */
727 return u_strncasecmp(str,cmp,len);
730 /*********************************************************************
731 * _mbscat (MSVCRT.@)
733 unsigned char * CDECL _mbscat( unsigned char *dst, const unsigned char *src )
735 strcat( (char *)dst, (const char *)src );
736 return dst;
739 /*********************************************************************
740 * _mbscpy (MSVCRT.@)
742 unsigned char* CDECL _mbscpy( unsigned char *dst, const unsigned char *src )
744 strcpy( (char *)dst, (const char *)src );
745 return dst;
748 /*********************************************************************
749 * _mbsstr (MSVCRT.@)
751 unsigned char * CDECL _mbsstr(const unsigned char *haystack, const unsigned char *needle)
753 return (unsigned char *)strstr( (const char *)haystack, (const char *)needle );
756 /*********************************************************************
757 * _mbschr(MSVCRT.@)
759 * Find a multibyte character in a multibyte string.
761 unsigned char* CDECL _mbschr(const unsigned char* s, unsigned int x)
763 if(MSVCRT___mb_cur_max > 1)
765 unsigned int c;
766 while (1)
768 c = _mbsnextc(s);
769 if (c == x)
770 return (unsigned char*)s;
771 if (!c)
772 return NULL;
773 s += c > 255 ? 2 : 1;
776 return u_strchr(s, x); /* ASCII CP */
779 /*********************************************************************
780 * _mbsrchr(MSVCRT.@)
782 unsigned char* CDECL _mbsrchr(const unsigned char* s, unsigned int x)
784 if(MSVCRT___mb_cur_max > 1)
786 unsigned int c;
787 unsigned char* match=NULL;
788 if(!s)
789 return NULL;
790 while (1) {
791 c = _mbsnextc(s);
792 if (c == x)
793 match=(unsigned char*)s;
794 if (!c)
795 return match;
796 s +=(c > 255) ? 2 : 1;
799 return u_strrchr(s, x);
802 /*********************************************************************
803 * _mbstok(MSVCRT.@)
805 * Find and extract tokens from strings
807 unsigned char* CDECL _mbstok(unsigned char *str, const unsigned char *delim)
809 thread_data_t *data = msvcrt_get_thread_data();
810 unsigned char *ret;
812 if(MSVCRT___mb_cur_max > 1)
814 unsigned int c;
816 if (!str)
817 if (!(str = data->mbstok_next)) return NULL;
819 while ((c = _mbsnextc(str)) && _mbschr(delim, c)) {
820 str += c > 255 ? 2 : 1;
822 if (!*str) return NULL;
823 ret = str++;
824 while ((c = _mbsnextc(str)) && !_mbschr(delim, c)) {
825 str += c > 255 ? 2 : 1;
827 if (*str) {
828 *str++ = 0;
829 if (c > 255) *str++ = 0;
831 data->mbstok_next = str;
832 return ret;
834 return u_strtok(str, delim); /* ASCII CP */
837 /*********************************************************************
838 * mbtowc(MSVCRT.@)
840 int CDECL MSVCRT_mbtowc(MSVCRT_wchar_t *dst, const char* str, MSVCRT_size_t n)
842 /* temp var needed because MultiByteToWideChar wants non NULL destination */
843 MSVCRT_wchar_t tmpdst = '\0';
845 if(n <= 0 || !str)
846 return 0;
847 if(!MultiByteToWideChar(CP_ACP, 0, str, n, &tmpdst, 1))
848 return -1;
849 if(dst)
850 *dst = tmpdst;
851 /* return the number of bytes from src that have been used */
852 if(!*str)
853 return 0;
854 if(n >= 2 && MSVCRT_isleadbyte(*str) && str[1])
855 return 2;
856 return 1;
859 /*********************************************************************
860 * _mbbtombc(MSVCRT.@)
862 unsigned int CDECL _mbbtombc(unsigned int c)
864 if(MSVCRT___mb_cur_max > 1 &&
865 ((c >= 0x20 && c <=0x7e) ||(c >= 0xa1 && c <= 0xdf)))
867 /* FIXME: I can't get this function to return anything
868 * different from what I pass it...
871 return c; /* ASCII CP or no MB char */
874 /*********************************************************************
875 * _mbbtype(MSVCRT.@)
877 int CDECL _mbbtype(unsigned char c, int type)
879 if (type == 1)
881 if ((c >= 0x20 && c <= 0x7e) || (c >= 0xa1 && c <= 0xdf))
882 return _MBC_SINGLE;
883 else if ((c >= 0x40 && c <= 0x7e) || (c >= 0x80 && c <= 0xfc))
884 return _MBC_TRAIL;
885 else
886 return _MBC_ILLEGAL;
888 else
890 if ((c >= 0x20 && c <= 0x7e) || (c >= 0xa1 && c <= 0xdf))
891 return _MBC_SINGLE;
892 else if ((c >= 0x81 && c <= 0x9f) || (c >= 0xe0 && c <= 0xfc))
893 return _MBC_LEAD;
894 else
895 return _MBC_ILLEGAL;
899 /*********************************************************************
900 * _ismbbkana(MSVCRT.@)
902 int CDECL _ismbbkana(unsigned int c)
904 /* FIXME: use lc_ctype when supported, not lc_all */
905 if(MSVCRT___lc_codepage == 932)
907 /* Japanese/Katakana, CP 932 */
908 return (c >= 0xa1 && c <= 0xdf);
910 return 0;
913 /*********************************************************************
914 * _ismbcdigit(MSVCRT.@)
916 int CDECL _ismbcdigit(unsigned int ch)
918 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
919 return (get_char_typeW( wch ) & C1_DIGIT);
922 /*********************************************************************
923 * _ismbcgraph(MSVCRT.@)
925 int CDECL _ismbcgraph(unsigned int ch)
927 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
928 return (get_char_typeW( wch ) & (C1_UPPER | C1_LOWER | C1_DIGIT | C1_PUNCT | C1_ALPHA));
931 /*********************************************************************
932 * _ismbcalpha (MSVCRT.@)
934 int CDECL _ismbcalpha(unsigned int ch)
936 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
937 return (get_char_typeW( wch ) & C1_ALPHA);
940 /*********************************************************************
941 * _ismbclower (MSVCRT.@)
943 int CDECL _ismbclower(unsigned int ch)
945 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
946 return (get_char_typeW( wch ) & C1_UPPER);
949 /*********************************************************************
950 * _ismbcupper (MSVCRT.@)
952 int CDECL _ismbcupper(unsigned int ch)
954 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
955 return (get_char_typeW( wch ) & C1_LOWER);
958 /*********************************************************************
959 * _ismbcsymbol(MSVCRT.@)
961 int CDECL _ismbcsymbol(unsigned int ch)
963 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
964 WORD ctype;
965 if (!GetStringTypeW(CT_CTYPE3, &wch, 1, &ctype))
967 WARN("GetStringTypeW failed on %x\n", ch);
968 return 0;
970 return ((ctype & C3_SYMBOL) != 0);
973 /*********************************************************************
974 * _ismbcalnum (MSVCRT.@)
976 int CDECL _ismbcalnum(unsigned int ch)
978 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
979 return (get_char_typeW( wch ) & (C1_ALPHA | C1_DIGIT));
982 /*********************************************************************
983 * _ismbcspace (MSVCRT.@)
985 int CDECL _ismbcspace(unsigned int ch)
987 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
988 return (get_char_typeW( wch ) & C1_SPACE);
991 /*********************************************************************
992 * _ismbcprint (MSVCRT.@)
994 int CDECL _ismbcprint(unsigned int ch)
996 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
997 return (get_char_typeW( wch ) & (C1_UPPER | C1_LOWER | C1_DIGIT | C1_PUNCT | C1_ALPHA | C1_SPACE));
1000 /*********************************************************************
1001 * _ismbcpunct(MSVCRT.@)
1003 int CDECL _ismbcpunct(unsigned int ch)
1005 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1006 return (get_char_typeW( wch ) & C1_PUNCT);
1009 /*********************************************************************
1010 * _ismbchira(MSVCRT.@)
1012 int CDECL _ismbchira(unsigned int c)
1014 /* FIXME: use lc_ctype when supported, not lc_all */
1015 if(MSVCRT___lc_codepage == 932)
1017 /* Japanese/Hiragana, CP 932 */
1018 return (c >= 0x829f && c <= 0x82f1);
1020 return 0;
1023 /*********************************************************************
1024 * _ismbckata(MSVCRT.@)
1026 int CDECL _ismbckata(unsigned int c)
1028 /* FIXME: use lc_ctype when supported, not lc_all */
1029 if(MSVCRT___lc_codepage == 932)
1031 if(c < 256)
1032 return _ismbbkana(c);
1033 /* Japanese/Katakana, CP 932 */
1034 return (c >= 0x8340 && c <= 0x8396 && c != 0x837f);
1036 return 0;
1039 /*********************************************************************
1040 * _ismbblead(MSVCRT.@)
1042 int CDECL _ismbblead(unsigned int c)
1044 return (MSVCRT_mbctype[(c&0xff) + 1] & _M1) != 0;
1048 /*********************************************************************
1049 * _ismbbtrail(MSVCRT.@)
1051 int CDECL _ismbbtrail(unsigned int c)
1053 return (MSVCRT_mbctype[(c&0xff) + 1] & _M2) != 0;
1056 /*********************************************************************
1057 * _ismbslead(MSVCRT.@)
1059 int CDECL _ismbslead(const unsigned char* start, const unsigned char* str)
1061 /* Lead bytes can also be trail bytes if caller messed up
1062 * iterating through the string...
1064 if(MSVCRT___mb_cur_max > 1)
1066 while(start < str)
1067 start += MSVCRT_isleadbyte(*str) ? 2 : 1;
1069 if(start == str)
1070 return MSVCRT_isleadbyte(*str);
1072 return 0; /* Must have been a trail, we skipped it */
1075 /*********************************************************************
1076 * _ismbstrail(MSVCRT.@)
1078 int CDECL _ismbstrail(const unsigned char* start, const unsigned char* str)
1080 /* Must not be a lead, and must be preceded by one */
1081 return !_ismbslead(start, str) && MSVCRT_isleadbyte(str[-1]);
1084 /*********************************************************************
1085 * _mbsset(MSVCRT.@)
1087 unsigned char* CDECL _mbsset(unsigned char* str, unsigned int c)
1089 unsigned char* ret = str;
1091 if(MSVCRT___mb_cur_max == 1 || c < 256)
1092 return u__strset(str, c); /* ASCII CP or SB char */
1094 c &= 0xffff; /* Strip high bits */
1096 while(str[0] && str[1])
1098 *str++ = c >> 8;
1099 *str++ = c & 0xff;
1101 if(str[0])
1102 str[0] = '\0'; /* FIXME: OK to shorten? */
1104 return ret;
1107 /*********************************************************************
1108 * _mbsnbset(MSVCRT.@)
1110 unsigned char* CDECL _mbsnbset(unsigned char *str, unsigned int c, MSVCRT_size_t len)
1112 unsigned char *ret = str;
1114 if(!len)
1115 return ret;
1117 if(MSVCRT___mb_cur_max == 1 || c < 256)
1118 return u__strnset(str, c, len); /* ASCII CP or SB char */
1120 c &= 0xffff; /* Strip high bits */
1122 while(str[0] && str[1] && (len > 1))
1124 *str++ = c >> 8;
1125 len--;
1126 *str++ = c & 0xff;
1127 len--;
1129 if(len && str[0]) {
1130 /* as per msdn pad with a blank character */
1131 str[0] = ' ';
1134 return ret;
1137 /*********************************************************************
1138 * _mbsnset(MSVCRT.@)
1140 unsigned char* CDECL _mbsnset(unsigned char* str, unsigned int c, MSVCRT_size_t len)
1142 unsigned char *ret = str;
1144 if(!len)
1145 return ret;
1147 if(MSVCRT___mb_cur_max == 1 || c < 256)
1148 return u__strnset(str, c, len); /* ASCII CP or SB char */
1150 c &= 0xffff; /* Strip high bits */
1152 while(str[0] && str[1] && len--)
1154 *str++ = c >> 8;
1155 *str++ = c & 0xff;
1157 if(len && str[0])
1158 str[0] = '\0'; /* FIXME: OK to shorten? */
1160 return ret;
1163 /*********************************************************************
1164 * _mbsnccnt(MSVCRT.@)
1165 * 'c' is for 'character'.
1167 MSVCRT_size_t CDECL _mbsnccnt(const unsigned char* str, MSVCRT_size_t len)
1169 MSVCRT_size_t ret;
1170 if(MSVCRT___mb_cur_max > 1)
1172 ret=0;
1173 while(*str && len-- > 0)
1175 if(MSVCRT_isleadbyte(*str))
1177 if (!len)
1178 break;
1179 len--;
1180 str++;
1182 str++;
1183 ret++;
1185 return ret;
1187 ret=u_strlen(str);
1188 return min(ret, len); /* ASCII CP */
1191 /*********************************************************************
1192 * _mbsnbcnt(MSVCRT.@)
1193 * 'b' is for byte count.
1195 MSVCRT_size_t CDECL _mbsnbcnt(const unsigned char* str, MSVCRT_size_t len)
1197 MSVCRT_size_t ret;
1198 if(MSVCRT___mb_cur_max > 1)
1200 const unsigned char* xstr = str;
1201 while(*xstr && len-- > 0)
1203 if (MSVCRT_isleadbyte(*xstr++))
1204 xstr++;
1206 return xstr-str;
1208 ret=u_strlen(str);
1209 return min(ret, len); /* ASCII CP */
1212 /*********************************************************************
1213 * _mbsnbcat(MSVCRT.@)
1215 unsigned char* CDECL _mbsnbcat(unsigned char* dst, const unsigned char* src, MSVCRT_size_t len)
1217 if(MSVCRT___mb_cur_max > 1)
1219 unsigned char *res = dst;
1220 while (*dst) {
1221 if (MSVCRT_isleadbyte(*dst++)) {
1222 if (*dst) {
1223 dst++;
1224 } else {
1225 /* as per msdn overwrite the lead byte in front of '\0' */
1226 dst--;
1227 break;
1231 while (*src && len--) *dst++ = *src++;
1232 *dst = '\0';
1233 return res;
1235 return u_strncat(dst, src, len); /* ASCII CP */
1239 /*********************************************************************
1240 * _mbsncat(MSVCRT.@)
1242 unsigned char* CDECL _mbsncat(unsigned char* dst, const unsigned char* src, MSVCRT_size_t len)
1244 if(MSVCRT___mb_cur_max > 1)
1246 unsigned char *res = dst;
1247 while (*dst)
1249 if (MSVCRT_isleadbyte(*dst++))
1250 dst++;
1252 while (*src && len--)
1254 *dst++ = *src;
1255 if(MSVCRT_isleadbyte(*src++))
1256 *dst++ = *src++;
1258 *dst = '\0';
1259 return res;
1261 return u_strncat(dst, src, len); /* ASCII CP */
1265 /*********************************************************************
1266 * _mbslwr(MSVCRT.@)
1268 unsigned char* CDECL _mbslwr(unsigned char* s)
1270 unsigned char *ret = s;
1271 if (!s)
1272 return NULL;
1273 if (MSVCRT___mb_cur_max > 1)
1275 unsigned int c;
1276 while (*s)
1278 c = _mbctolower(_mbsnextc(s));
1279 /* Note that I assume that the size of the character is unchanged */
1280 if (c > 255)
1282 *s++=(c>>8);
1283 c=c & 0xff;
1285 *s++=c;
1288 else for ( ; *s; s++) *s = tolower(*s);
1289 return ret;
1293 /*********************************************************************
1294 * _mbsupr(MSVCRT.@)
1296 unsigned char* CDECL _mbsupr(unsigned char* s)
1298 unsigned char *ret = s;
1299 if (!s)
1300 return NULL;
1301 if (MSVCRT___mb_cur_max > 1)
1303 unsigned int c;
1304 while (*s)
1306 c = _mbctoupper(_mbsnextc(s));
1307 /* Note that I assume that the size of the character is unchanged */
1308 if (c > 255)
1310 *s++=(c>>8);
1311 c=c & 0xff;
1313 *s++=c;
1316 else for ( ; *s; s++) *s = toupper(*s);
1317 return ret;
1321 /*********************************************************************
1322 * _mbsspn (MSVCRT.@)
1324 MSVCRT_size_t CDECL _mbsspn(const unsigned char* string, const unsigned char* set)
1326 const unsigned char *p, *q;
1328 for (p = string; *p; p++)
1330 if (MSVCRT_isleadbyte(*p))
1332 for (q = set; *q; q++)
1334 if (!q[1])
1335 break;
1336 if ((*p == *q) && (p[1] == q[1]))
1337 break;
1338 q++;
1340 if (!q[0] || !q[1]) break;
1342 else
1344 for (q = set; *q; q++)
1345 if (*p == *q)
1346 break;
1347 if (!*q) break;
1350 return p - string;
1353 /*********************************************************************
1354 * _mbsspnp (MSVCRT.@)
1356 unsigned char* CDECL _mbsspnp(const unsigned char* string, const unsigned char* set)
1358 const unsigned char *p, *q;
1360 for (p = string; *p; p++)
1362 if (MSVCRT_isleadbyte(*p))
1364 for (q = set; *q; q++)
1366 if (!q[1])
1367 break;
1368 if ((*p == *q) && (p[1] == q[1]))
1369 break;
1370 q++;
1372 if (!q[0] || !q[1]) break;
1374 else
1376 for (q = set; *q; q++)
1377 if (*p == *q)
1378 break;
1379 if (!*q) break;
1382 if (*p == '\0')
1383 return NULL;
1384 return (unsigned char *)p;
1387 /*********************************************************************
1388 * _mbscspn(MSVCRT.@)
1390 MSVCRT_size_t CDECL _mbscspn(const unsigned char* str, const unsigned char* cmp)
1392 if (MSVCRT___mb_cur_max > 1)
1393 FIXME("don't handle double character case\n");
1394 return u_strcspn(str, cmp);
1397 /*********************************************************************
1398 * _mbsrev (MSVCRT.@)
1400 unsigned char* CDECL _mbsrev(unsigned char* str)
1402 int i, len = _mbslen(str);
1403 unsigned char *p, *temp=MSVCRT_malloc(len*2);
1405 if(!temp)
1406 return str;
1408 /* unpack multibyte string to temp buffer */
1409 p=str;
1410 for(i=0; i<len; i++)
1412 if (MSVCRT_isleadbyte(*p))
1414 temp[i*2]=*p++;
1415 temp[i*2+1]=*p++;
1417 else
1419 temp[i*2]=*p++;
1420 temp[i*2+1]=0;
1424 /* repack it in the reverse order */
1425 p=str;
1426 for(i=len-1; i>=0; i--)
1428 if(MSVCRT_isleadbyte(temp[i*2]))
1430 *p++=temp[i*2];
1431 *p++=temp[i*2+1];
1433 else
1435 *p++=temp[i*2];
1439 MSVCRT_free(temp);
1441 return str;
1444 /*********************************************************************
1445 * _mbspbrk (MSVCRT.@)
1447 unsigned char* CDECL _mbspbrk(const unsigned char* str, const unsigned char* accept)
1449 const unsigned char* p;
1451 while(*str)
1453 for(p = accept; *p; p += (MSVCRT_isleadbyte(*p)?2:1) )
1455 if (*p == *str)
1456 if( !MSVCRT_isleadbyte(*p) || ( *(p+1) == *(str+1) ) )
1457 return (unsigned char*)str;
1459 str += (MSVCRT_isleadbyte(*str)?2:1);
1461 return NULL;
1466 * Functions depending on locale codepage
1469 /*********************************************************************
1470 * mblen(MSVCRT.@)
1471 * REMARKS
1472 * Unlike most of the multibyte string functions this function uses
1473 * the locale codepage, not the codepage set by _setmbcp
1475 int CDECL MSVCRT_mblen(const char* str, MSVCRT_size_t size)
1477 if (str && *str && size)
1479 if(MSVCRT___mb_cur_max == 1)
1480 return 1; /* ASCII CP */
1482 return !MSVCRT_isleadbyte(*str) ? 1 : (size>1 ? 2 : -1);
1484 return 0;
1487 /*********************************************************************
1488 * _mbstrlen(MSVCRT.@)
1489 * REMARKS
1490 * Unlike most of the multibyte string functions this function uses
1491 * the locale codepage, not the codepage set by _setmbcp
1493 MSVCRT_size_t CDECL _mbstrlen(const char* str)
1495 if(MSVCRT___mb_cur_max > 1)
1497 MSVCRT_size_t len = 0;
1498 while(*str)
1500 /* FIXME: According to the documentation we are supposed to test for
1501 * multi-byte character validity. Whatever that means
1503 str += MSVCRT_isleadbyte(*str) ? 2 : 1;
1504 len++;
1506 return len;
1508 return strlen(str); /* ASCII CP */