d3d9: Some caps fixes.
[wine.git] / dlls / msvcrt / mbcs.c
blob38c249b7490b4b1e271084bd68495395b3d68aef
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"
31 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
33 unsigned char MSVCRT_mbctype[257];
34 int MSVCRT___mb_cur_max = 1;
35 extern int MSVCRT___lc_collate_cp;
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 {20932, {1, 255, 0, 0}},
52 {0, {1, 255, 0, 0}} /* match all with FIXME */
55 static MSVCRT_wchar_t msvcrt_mbc_to_wc(unsigned int ch)
57 MSVCRT_wchar_t chW;
58 char mbch[2];
59 int n_chars;
61 if (ch <= 0xff) {
62 mbch[0] = ch;
63 n_chars = 1;
64 } else {
65 mbch[0] = (ch >> 8) & 0xff;
66 mbch[1] = ch & 0xff;
67 n_chars = 2;
69 if (!MultiByteToWideChar(MSVCRT___lc_codepage, 0, mbch, n_chars, &chW, 1))
71 WARN("MultiByteToWideChar failed on %x\n", ch);
72 return 0;
74 return chW;
77 static inline size_t u_strlen( const unsigned char *str )
79 return strlen( (const char*) str );
82 static inline unsigned char* u_strncat( unsigned char* dst, const unsigned char* src, size_t len )
84 return (unsigned char*)strncat( (char*)dst, (const char*)src, len);
87 static inline int u_strcmp( const unsigned char *s1, const unsigned char *s2 )
89 return strcmp( (const char*)s1, (const char*)s2 );
92 static inline int u_strcasecmp( const unsigned char *s1, const unsigned char *s2 )
94 return strcasecmp( (const char*)s1, (const char*)s2 );
97 static inline int u_strncmp( const unsigned char *s1, const unsigned char *s2, size_t len )
99 return strncmp( (const char*)s1, (const char*)s2, len );
102 static inline int u_strncasecmp( const unsigned char *s1, const unsigned char *s2, size_t len )
104 return strncasecmp( (const char*)s1, (const char*)s2, len );
107 static inline unsigned char *u_strchr( const unsigned char *s, unsigned char x )
109 return (unsigned char*) strchr( (const char*)s, x );
112 static inline unsigned char *u_strrchr( const unsigned char *s, unsigned char x )
114 return (unsigned char*) strrchr( (const char*)s, x );
117 static inline unsigned char *u_strtok( unsigned char *s, const unsigned char *delim )
119 return (unsigned char*) strtok( (char*)s, (const char*)delim );
122 static inline unsigned char *u__strset( unsigned char *s, unsigned char c )
124 return (unsigned char*) _strset( (char*)s, c);
127 static inline unsigned char *u__strnset( unsigned char *s, unsigned char c, size_t len )
129 return (unsigned char*) _strnset( (char*)s, c, len );
132 static inline size_t u_strcspn( const unsigned char *s, const unsigned char *rej )
134 return strcspn( (const char *)s, (const char*)rej );
137 /*********************************************************************
138 * __p__mbctype (MSVCRT.@)
140 unsigned char* CDECL __p__mbctype(void)
142 return MSVCRT_mbctype;
145 /*********************************************************************
146 * __p___mb_cur_max(MSVCRT.@)
148 int* CDECL __p___mb_cur_max(void)
150 return &MSVCRT___mb_cur_max;
153 /*********************************************************************
154 * _setmbcp (MSVCRT.@)
156 int CDECL _setmbcp(int cp)
158 int newcp;
159 CPINFO cpi;
160 BYTE *bytes;
161 WORD chartypes[256];
162 WORD *curr_type;
163 char bufA[256];
164 WCHAR bufW[256];
165 int charcount;
166 int ret;
167 int i;
169 switch (cp)
171 case _MB_CP_ANSI:
172 newcp = GetACP();
173 break;
174 case _MB_CP_OEM:
175 newcp = GetOEMCP();
176 break;
177 case _MB_CP_LOCALE:
178 newcp = MSVCRT___lc_codepage;
179 break;
180 case _MB_CP_SBCS:
181 newcp = 20127; /* ASCII */
182 break;
183 default:
184 newcp = cp;
185 break;
188 if (!GetCPInfo(newcp, &cpi))
190 WARN("Codepage %d not found\n", newcp);
191 msvcrt_set_errno(MSVCRT_EINVAL);
192 return -1;
195 /* setup the _mbctype */
196 memset(MSVCRT_mbctype, 0, sizeof(MSVCRT_mbctype));
198 bytes = cpi.LeadByte;
199 while (bytes[0] || bytes[1])
201 for (i = bytes[0]; i <= bytes[1]; i++)
202 MSVCRT_mbctype[i + 1] |= _M1;
203 bytes += 2;
206 if (cpi.MaxCharSize > 1)
208 /* trail bytes not available through kernel32 but stored in a structure in msvcrt */
209 struct cp_extra_info_t *cpextra = g_cpextrainfo;
210 while (TRUE)
212 if (cpextra->cp == 0 || cpextra->cp == newcp)
214 if (cpextra->cp == 0)
215 FIXME("trail bytes data not available for DBCS codepage %d - assuming all bytes\n", newcp);
217 bytes = cpextra->TrailBytes;
218 while (bytes[0] || bytes[1])
220 for (i = bytes[0]; i <= bytes[1]; i++)
221 MSVCRT_mbctype[i + 1] |= _M2;
222 bytes += 2;
224 break;
226 cpextra++;
230 /* we can't use GetStringTypeA directly because we don't have a locale - only a code page
232 charcount = 0;
233 for (i = 0; i < 256; i++)
234 if (!(MSVCRT_mbctype[i + 1] & _M1))
235 bufA[charcount++] = i;
237 ret = MultiByteToWideChar(newcp, 0, bufA, charcount, bufW, charcount);
238 if (ret != charcount)
239 ERR("MultiByteToWideChar of chars failed for cp %d, ret=%d (exp %d), error=%d\n", newcp, ret, charcount, GetLastError());
241 GetStringTypeW(CT_CTYPE1, bufW, charcount, chartypes);
243 curr_type = chartypes;
244 for (i = 0; i < 256; i++)
245 if (!(MSVCRT_mbctype[i + 1] & _M1))
247 if ((*curr_type) & C1_UPPER)
248 MSVCRT_mbctype[i + 1] |= _SBUP;
249 if ((*curr_type) & C1_LOWER)
250 MSVCRT_mbctype[i + 1] |= _SBLOW;
251 curr_type++;
254 if (newcp == 932) /* CP932 only - set _MP and _MS */
256 /* On Windows it's possible to calculate the _MP and _MS from CT_CTYPE1
257 * and CT_CTYPE3. But as of Wine 0.9.43 we return wrong values what makes
258 * it hard. As this is set only for codepage 932 we hardcode it what gives
259 * also faster execution.
261 for (i = 161; i <= 165; i++)
262 MSVCRT_mbctype[i + 1] |= _MP;
263 for (i = 166; i <= 223; i++)
264 MSVCRT_mbctype[i + 1] |= _MS;
267 MSVCRT___lc_collate_cp = MSVCRT___lc_codepage = newcp;
268 TRACE("(%d) -> %d\n", cp, MSVCRT___lc_codepage);
269 return 0;
272 /*********************************************************************
273 * _getmbcp (MSVCRT.@)
275 int CDECL _getmbcp(void)
277 return MSVCRT___lc_codepage;
280 /*********************************************************************
281 * _mbsnextc(MSVCRT.@)
283 unsigned int CDECL _mbsnextc(const unsigned char* str)
285 if(_ismbblead(*str))
286 return *str << 8 | str[1];
287 return *str;
290 /*********************************************************************
291 * _mbctolower(MSVCRT.@)
293 unsigned int CDECL _mbctolower(unsigned int c)
295 if (MSVCRT_isleadbyte(c))
297 FIXME("Handle MBC chars\n");
298 return c;
300 return tolower(c); /* ASCII CP or SB char */
303 /*********************************************************************
304 * _mbctoupper(MSVCRT.@)
306 unsigned int CDECL _mbctoupper(unsigned int c)
308 if (MSVCRT_isleadbyte(c))
310 FIXME("Handle MBC chars\n");
311 return c;
313 return toupper(c); /* ASCII CP or SB char */
316 /*********************************************************************
317 * _mbsdec(MSVCRT.@)
319 unsigned char* CDECL _mbsdec(const unsigned char* start, const unsigned char* cur)
321 if(MSVCRT___mb_cur_max > 1)
322 return (unsigned char *)(_ismbstrail(start,cur-1) ? cur - 2 : cur -1);
324 return (unsigned char *)cur - 1; /* ASCII CP or SB char */
327 /*********************************************************************
328 * _mbsinc(MSVCRT.@)
330 unsigned char* CDECL _mbsinc(const unsigned char* str)
332 if(MSVCRT___mb_cur_max > 1 && MSVCRT_isleadbyte(*str))
333 return (unsigned char*)str + 2; /* MB char */
335 return (unsigned char*)str + 1; /* ASCII CP or SB char */
338 /*********************************************************************
339 * _mbsninc(MSVCRT.@)
341 unsigned char* CDECL _mbsninc(const unsigned char* str, MSVCRT_size_t num)
343 if(!str || num < 1)
344 return NULL;
345 if(MSVCRT___mb_cur_max > 1)
347 while(num--)
348 str = _mbsinc(str);
349 return (unsigned char*)str;
351 return (unsigned char*)str + num; /* ASCII CP */
354 /*********************************************************************
355 * _mbclen(MSVCRT.@)
357 unsigned int CDECL _mbclen(const unsigned char* str)
359 return _ismbblead(*str) ? 2 : 1;
362 /*********************************************************************
363 * _mbslen(MSVCRT.@)
365 MSVCRT_size_t CDECL _mbslen(const unsigned char* str)
367 MSVCRT_size_t len = 0;
368 while(*str)
370 if (_ismbblead(*str))
372 str++;
373 if (!*str) /* count only full chars */
374 break;
376 str++;
377 len++;
379 return len;
382 /*********************************************************************
383 * _mbccpy(MSVCRT.@)
385 void CDECL _mbccpy(unsigned char* dest, const unsigned char* src)
387 *dest = *src;
388 if(MSVCRT___mb_cur_max > 1 && MSVCRT_isleadbyte(*src))
389 *++dest = *++src; /* MB char */
392 /*********************************************************************
393 * _mbsncpy(MSVCRT.@)
395 unsigned char* CDECL _mbsncpy(unsigned char* dst, const unsigned char* src, MSVCRT_size_t n)
397 unsigned char* ret = dst;
398 if(!n)
399 return dst;
400 if(MSVCRT___mb_cur_max > 1)
402 while (*src && n)
404 n--;
405 *dst++ = *src;
406 if (MSVCRT_isleadbyte(*src++))
407 *dst++ = *src++;
410 else
412 while (n)
414 n--;
415 if (!(*dst++ = *src++)) break;
418 while (n--) *dst++ = 0;
419 return ret;
422 /*********************************************************************
423 * _mbsnbcpy(MSVCRT.@)
425 unsigned char* CDECL _mbsnbcpy(unsigned char* dst, const unsigned char* src, MSVCRT_size_t n)
427 unsigned char* ret = dst;
428 if(!n)
429 return dst;
430 if(MSVCRT___mb_cur_max > 1)
432 while (*src && (n > 1))
434 n--;
435 *dst++ = *src;
436 if (MSVCRT_isleadbyte(*src++))
438 *dst++ = *src++;
439 n--;
442 if (*src && n && !MSVCRT_isleadbyte(*src))
444 /* If the last character is a multi-byte character then
445 * we cannot copy it since we have only one byte left
447 *dst++ = *src;
448 n--;
451 else
453 while (n)
455 n--;
456 if (!(*dst++ = *src++)) break;
459 while (n--) *dst++ = 0;
460 return ret;
463 /*********************************************************************
464 * _mbscmp(MSVCRT.@)
466 int CDECL _mbscmp(const unsigned char* str, const unsigned char* cmp)
468 if(MSVCRT___mb_cur_max > 1)
470 unsigned int strc, cmpc;
471 do {
472 if(!*str)
473 return *cmp ? -1 : 0;
474 if(!*cmp)
475 return 1;
476 strc = _mbsnextc(str);
477 cmpc = _mbsnextc(cmp);
478 if(strc != cmpc)
479 return strc < cmpc ? -1 : 1;
480 str +=(strc > 255) ? 2 : 1;
481 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
482 } while(1);
484 return u_strcmp(str, cmp); /* ASCII CP */
487 /*********************************************************************
488 * _mbsicoll(MSVCRT.@)
489 * FIXME: handle locales.
491 int CDECL _mbsicoll(const unsigned char* str, const unsigned char* cmp)
493 if(MSVCRT___mb_cur_max > 1)
495 unsigned int strc, cmpc;
496 do {
497 if(!*str)
498 return *cmp ? -1 : 0;
499 if(!*cmp)
500 return 1;
501 strc = _mbctolower(_mbsnextc(str));
502 cmpc = _mbctolower(_mbsnextc(cmp));
503 if(strc != cmpc)
504 return strc < cmpc ? -1 : 1;
505 str +=(strc > 255) ? 2 : 1;
506 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
507 } while(1);
509 return u_strcasecmp(str, cmp); /* ASCII CP */
512 /*********************************************************************
513 * _mbscoll(MSVCRT.@)
514 * Performs a case-sensitive comparison according to the current code page
515 * RETURN
516 * _NLSCMPERROR if error
517 * FIXME: handle locales.
519 int CDECL _mbscoll(const unsigned char* str, const unsigned char* cmp)
521 if(MSVCRT___mb_cur_max > 1)
523 unsigned int strc, cmpc;
524 do {
525 if(!*str)
526 return *cmp ? -1 : 0;
527 if(!*cmp)
528 return 1;
529 strc = _mbsnextc(str);
530 cmpc = _mbsnextc(cmp);
531 if(strc != cmpc)
532 return strc < cmpc ? -1 : 1;
533 str +=(strc > 255) ? 2 : 1;
534 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
535 } while(1);
537 return u_strcmp(str, cmp); /* ASCII CP */
541 /*********************************************************************
542 * _mbsicmp(MSVCRT.@)
544 int CDECL _mbsicmp(const unsigned char* str, const unsigned char* cmp)
546 if(MSVCRT___mb_cur_max > 1)
548 unsigned int strc, cmpc;
549 do {
550 if(!*str)
551 return *cmp ? -1 : 0;
552 if(!*cmp)
553 return 1;
554 strc = _mbctolower(_mbsnextc(str));
555 cmpc = _mbctolower(_mbsnextc(cmp));
556 if(strc != cmpc)
557 return strc < cmpc ? -1 : 1;
558 str +=(strc > 255) ? 2 : 1;
559 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
560 } while(1);
562 return u_strcasecmp(str, cmp); /* ASCII CP */
565 /*********************************************************************
566 * _mbsncmp(MSVCRT.@)
568 int CDECL _mbsncmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
570 if(!len)
571 return 0;
573 if(MSVCRT___mb_cur_max > 1)
575 unsigned int strc, cmpc;
576 while(len--)
578 int inc;
579 if(!*str)
580 return *cmp ? -1 : 0;
581 if(!*cmp)
582 return 1;
583 strc = _mbsnextc(str);
584 cmpc = _mbsnextc(cmp);
585 if(strc != cmpc)
586 return strc < cmpc ? -1 : 1;
587 inc=(strc > 255) ? 2 : 1; /* Equal, use same increment */
588 str += inc;
589 cmp += inc;
591 return 0; /* Matched len chars */
593 return u_strncmp(str, cmp, len); /* ASCII CP */
596 /*********************************************************************
597 * _mbsnbcmp(MSVCRT.@)
599 int CDECL _mbsnbcmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
601 if (!len)
602 return 0;
603 if(MSVCRT___mb_cur_max > 1)
605 unsigned int strc, cmpc;
606 while (len)
608 int clen;
609 if(!*str)
610 return *cmp ? -1 : 0;
611 if(!*cmp)
612 return 1;
613 if (MSVCRT_isleadbyte(*str))
615 strc=(len>=2)?_mbsnextc(str):0;
616 clen=2;
618 else
620 strc=*str;
621 clen=1;
623 if (MSVCRT_isleadbyte(*cmp))
624 cmpc=(len>=2)?_mbsnextc(cmp):0;
625 else
626 cmpc=*str;
627 if(strc != cmpc)
628 return strc < cmpc ? -1 : 1;
629 len -= clen;
630 str += clen;
631 cmp += clen;
633 return 0; /* Matched len chars */
635 return u_strncmp(str,cmp,len);
638 /*********************************************************************
639 * _mbsnicmp(MSVCRT.@)
641 * Compare two multibyte strings case insensitively to 'len' characters.
643 int CDECL _mbsnicmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
645 /* FIXME: No tolower() for mb strings yet */
646 if(MSVCRT___mb_cur_max > 1)
648 unsigned int strc, cmpc;
649 while(len--)
651 if(!*str)
652 return *cmp ? -1 : 0;
653 if(!*cmp)
654 return 1;
655 strc = _mbctolower(_mbsnextc(str));
656 cmpc = _mbctolower(_mbsnextc(cmp));
657 if(strc != cmpc)
658 return strc < cmpc ? -1 : 1;
659 str +=(strc > 255) ? 2 : 1;
660 cmp +=(strc > 255) ? 2 : 1; /* Equal, use same increment */
662 return 0; /* Matched len chars */
664 return u_strncasecmp(str, cmp, len); /* ASCII CP */
667 /*********************************************************************
668 * _mbsnbicmp(MSVCRT.@)
670 int CDECL _mbsnbicmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
672 if (!len)
673 return 0;
674 if(MSVCRT___mb_cur_max > 1)
676 unsigned int strc, cmpc;
677 while (len)
679 int clen;
680 if(!*str)
681 return *cmp ? -1 : 0;
682 if(!*cmp)
683 return 1;
684 if (MSVCRT_isleadbyte(*str))
686 strc=(len>=2)?_mbsnextc(str):0;
687 clen=2;
689 else
691 strc=*str;
692 clen=1;
694 if (MSVCRT_isleadbyte(*cmp))
695 cmpc=(len>=2)?_mbsnextc(cmp):0;
696 else
697 cmpc=*str;
698 strc = _mbctolower(strc);
699 cmpc = _mbctolower(cmpc);
700 if(strc != cmpc)
701 return strc < cmpc ? -1 : 1;
702 len -= clen;
703 str += clen;
704 cmp += clen;
706 return 0; /* Matched len bytes */
708 return u_strncasecmp(str,cmp,len);
711 /*********************************************************************
712 * _mbscat (MSVCRT.@)
714 unsigned char * CDECL _mbscat( unsigned char *dst, const unsigned char *src )
716 strcat( (char *)dst, (const char *)src );
717 return dst;
720 /*********************************************************************
721 * _mbscpy (MSVCRT.@)
723 unsigned char* CDECL _mbscpy( unsigned char *dst, const unsigned char *src )
725 strcpy( (char *)dst, (const char *)src );
726 return dst;
729 /*********************************************************************
730 * _mbsstr (MSVCRT.@)
732 unsigned char * CDECL _mbsstr(const unsigned char *haystack, const unsigned char *needle)
734 return (unsigned char *)strstr( (const char *)haystack, (const char *)needle );
737 /*********************************************************************
738 * _mbschr(MSVCRT.@)
740 * Find a multibyte character in a multibyte string.
742 unsigned char* CDECL _mbschr(const unsigned char* s, unsigned int x)
744 if(MSVCRT___mb_cur_max > 1)
746 unsigned int c;
747 while (1)
749 c = _mbsnextc(s);
750 if (c == x)
751 return (unsigned char*)s;
752 if (!c)
753 return NULL;
754 s += c > 255 ? 2 : 1;
757 return u_strchr(s, x); /* ASCII CP */
760 /*********************************************************************
761 * _mbsrchr(MSVCRT.@)
763 unsigned char* CDECL _mbsrchr(const unsigned char* s, unsigned int x)
765 if(MSVCRT___mb_cur_max > 1)
767 unsigned int c;
768 unsigned char* match=NULL;
769 if(!s)
770 return NULL;
771 while (1) {
772 c = _mbsnextc(s);
773 if (c == x)
774 match=(unsigned char*)s;
775 if (!c)
776 return match;
777 s +=(c > 255) ? 2 : 1;
780 return u_strrchr(s, x);
783 /*********************************************************************
784 * _mbstok(MSVCRT.@)
786 * Find and extract tokens from strings
788 unsigned char* CDECL _mbstok(unsigned char *str, const unsigned char *delim)
790 thread_data_t *data = msvcrt_get_thread_data();
791 unsigned char *ret;
793 if(MSVCRT___mb_cur_max > 1)
795 unsigned int c;
797 if (!str)
798 if (!(str = data->mbstok_next)) return NULL;
800 while ((c = _mbsnextc(str)) && _mbschr(delim, c)) {
801 str += c > 255 ? 2 : 1;
803 if (!*str) return NULL;
804 ret = str++;
805 while ((c = _mbsnextc(str)) && !_mbschr(delim, c)) {
806 str += c > 255 ? 2 : 1;
808 if (*str) {
809 *str++ = 0;
810 if (c > 255) *str++ = 0;
812 data->mbstok_next = str;
813 return ret;
815 return u_strtok(str, delim); /* ASCII CP */
818 /*********************************************************************
819 * mbtowc(MSVCRT.@)
821 int CDECL MSVCRT_mbtowc(MSVCRT_wchar_t *dst, const char* str, MSVCRT_size_t n)
823 /* temp var needed because MultiByteToWideChar wants non NULL destination */
824 MSVCRT_wchar_t tmpdst = '\0';
826 if(n <= 0 || !str)
827 return 0;
828 if(!MultiByteToWideChar(CP_ACP, 0, str, n, &tmpdst, 1))
829 return -1;
830 if(dst)
831 *dst = tmpdst;
832 /* return the number of bytes from src that have been used */
833 if(!*str)
834 return 0;
835 if(n >= 2 && MSVCRT_isleadbyte(*str) && str[1])
836 return 2;
837 return 1;
840 /*********************************************************************
841 * _mbbtombc(MSVCRT.@)
843 unsigned int CDECL _mbbtombc(unsigned int c)
845 if(MSVCRT___mb_cur_max > 1 &&
846 ((c >= 0x20 && c <=0x7e) ||(c >= 0xa1 && c <= 0xdf)))
848 /* FIXME: I can't get this function to return anything
849 * different from what I pass it...
852 return c; /* ASCII CP or no MB char */
855 /*********************************************************************
856 * _mbbtype(MSVCRT.@)
858 int CDECL _mbbtype(unsigned char c, int type)
860 if (type == 1)
862 if ((c >= 0x20 && c <= 0x7e) || (c >= 0xa1 && c <= 0xdf))
863 return _MBC_SINGLE;
864 else if ((c >= 0x40 && c <= 0x7e) || (c >= 0x80 && c <= 0xfc))
865 return _MBC_TRAIL;
866 else
867 return _MBC_ILLEGAL;
869 else
871 if ((c >= 0x20 && c <= 0x7e) || (c >= 0xa1 && c <= 0xdf))
872 return _MBC_SINGLE;
873 else if ((c >= 0x81 && c <= 0x9f) || (c >= 0xe0 && c <= 0xfc))
874 return _MBC_LEAD;
875 else
876 return _MBC_ILLEGAL;
880 /*********************************************************************
881 * _ismbbkana(MSVCRT.@)
883 int CDECL _ismbbkana(unsigned int c)
885 /* FIXME: use lc_ctype when supported, not lc_all */
886 if(MSVCRT___lc_codepage == 932)
888 /* Japanese/Katakana, CP 932 */
889 return (c >= 0xa1 && c <= 0xdf);
891 return 0;
894 /*********************************************************************
895 * _ismbcdigit(MSVCRT.@)
897 int CDECL _ismbcdigit(unsigned int ch)
899 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
900 return (get_char_typeW( wch ) & C1_DIGIT);
903 /*********************************************************************
904 * _ismbcgraph(MSVCRT.@)
906 int CDECL _ismbcgraph(unsigned int ch)
908 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
909 return (get_char_typeW( wch ) & (C1_UPPER | C1_LOWER | C1_DIGIT | C1_PUNCT | C1_ALPHA));
912 /*********************************************************************
913 * _ismbcalpha (MSVCRT.@)
915 int CDECL _ismbcalpha(unsigned int ch)
917 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
918 return (get_char_typeW( wch ) & C1_ALPHA);
921 /*********************************************************************
922 * _ismbclower (MSVCRT.@)
924 int CDECL _ismbclower(unsigned int ch)
926 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
927 return (get_char_typeW( wch ) & C1_UPPER);
930 /*********************************************************************
931 * _ismbcupper (MSVCRT.@)
933 int CDECL _ismbcupper(unsigned int ch)
935 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
936 return (get_char_typeW( wch ) & C1_LOWER);
939 /*********************************************************************
940 * _ismbcsymbol(MSVCRT.@)
942 int CDECL _ismbcsymbol(unsigned int ch)
944 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
945 WORD ctype;
946 if (!GetStringTypeW(CT_CTYPE3, &wch, 1, &ctype))
948 WARN("GetStringTypeW failed on %x\n", ch);
949 return 0;
951 return ((ctype & C3_SYMBOL) != 0);
954 /*********************************************************************
955 * _ismbcalnum (MSVCRT.@)
957 int CDECL _ismbcalnum(unsigned int ch)
959 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
960 return (get_char_typeW( wch ) & (C1_ALPHA | C1_DIGIT));
963 /*********************************************************************
964 * _ismbcspace (MSVCRT.@)
966 int CDECL _ismbcspace(unsigned int ch)
968 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
969 return (get_char_typeW( wch ) & C1_SPACE);
972 /*********************************************************************
973 * _ismbcprint (MSVCRT.@)
975 int CDECL _ismbcprint(unsigned int ch)
977 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
978 return (get_char_typeW( wch ) & (C1_UPPER | C1_LOWER | C1_DIGIT | C1_PUNCT | C1_ALPHA | C1_SPACE));
981 /*********************************************************************
982 * _ismbcpunct(MSVCRT.@)
984 int CDECL _ismbcpunct(unsigned int ch)
986 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
987 return (get_char_typeW( wch ) & C1_PUNCT);
990 /*********************************************************************
991 * _ismbchira(MSVCRT.@)
993 int CDECL _ismbchira(unsigned int c)
995 /* FIXME: use lc_ctype when supported, not lc_all */
996 if(MSVCRT___lc_codepage == 932)
998 /* Japanese/Hiragana, CP 932 */
999 return (c >= 0x829f && c <= 0x82f1);
1001 return 0;
1004 /*********************************************************************
1005 * _ismbckata(MSVCRT.@)
1007 int CDECL _ismbckata(unsigned int c)
1009 /* FIXME: use lc_ctype when supported, not lc_all */
1010 if(MSVCRT___lc_codepage == 932)
1012 if(c < 256)
1013 return _ismbbkana(c);
1014 /* Japanese/Katakana, CP 932 */
1015 return (c >= 0x8340 && c <= 0x8396 && c != 0x837f);
1017 return 0;
1020 /*********************************************************************
1021 * _ismbblead(MSVCRT.@)
1023 int CDECL _ismbblead(unsigned int c)
1025 return (MSVCRT_mbctype[(c&0xff) + 1] & _M1) != 0;
1029 /*********************************************************************
1030 * _ismbbtrail(MSVCRT.@)
1032 int CDECL _ismbbtrail(unsigned int c)
1034 return (MSVCRT_mbctype[(c&0xff) + 1] & _M2) != 0;
1037 /*********************************************************************
1038 * _ismbslead(MSVCRT.@)
1040 int CDECL _ismbslead(const unsigned char* start, const unsigned char* str)
1042 /* Lead bytes can also be trail bytes if caller messed up
1043 * iterating through the string...
1045 if(MSVCRT___mb_cur_max > 1)
1047 while(start < str)
1048 start += MSVCRT_isleadbyte(*str) ? 2 : 1;
1050 if(start == str)
1051 return MSVCRT_isleadbyte(*str);
1053 return 0; /* Must have been a trail, we skipped it */
1056 /*********************************************************************
1057 * _ismbstrail(MSVCRT.@)
1059 int CDECL _ismbstrail(const unsigned char* start, const unsigned char* str)
1061 /* Must not be a lead, and must be preceded by one */
1062 return !_ismbslead(start, str) && MSVCRT_isleadbyte(str[-1]);
1065 /*********************************************************************
1066 * _mbsset(MSVCRT.@)
1068 unsigned char* CDECL _mbsset(unsigned char* str, unsigned int c)
1070 unsigned char* ret = str;
1072 if(MSVCRT___mb_cur_max == 1 || c < 256)
1073 return u__strset(str, c); /* ASCII CP or SB char */
1075 c &= 0xffff; /* Strip high bits */
1077 while(str[0] && str[1])
1079 *str++ = c >> 8;
1080 *str++ = c & 0xff;
1082 if(str[0])
1083 str[0] = '\0'; /* FIXME: OK to shorten? */
1085 return ret;
1088 /*********************************************************************
1089 * _mbsnbset(MSVCRT.@)
1091 unsigned char* CDECL _mbsnbset(unsigned char *str, unsigned int c, MSVCRT_size_t len)
1093 unsigned char *ret = str;
1095 if(!len)
1096 return ret;
1098 if(MSVCRT___mb_cur_max == 1 || c < 256)
1099 return u__strnset(str, c, len); /* ASCII CP or SB char */
1101 c &= 0xffff; /* Strip high bits */
1103 while(str[0] && str[1] && (len > 1))
1105 *str++ = c >> 8;
1106 len--;
1107 *str++ = c & 0xff;
1108 len--;
1110 if(len && str[0]) {
1111 /* as per msdn pad with a blank character */
1112 str[0] = ' ';
1115 return ret;
1118 /*********************************************************************
1119 * _mbsnset(MSVCRT.@)
1121 unsigned char* CDECL _mbsnset(unsigned char* str, unsigned int c, MSVCRT_size_t len)
1123 unsigned char *ret = str;
1125 if(!len)
1126 return ret;
1128 if(MSVCRT___mb_cur_max == 1 || c < 256)
1129 return u__strnset(str, c, len); /* ASCII CP or SB char */
1131 c &= 0xffff; /* Strip high bits */
1133 while(str[0] && str[1] && len--)
1135 *str++ = c >> 8;
1136 *str++ = c & 0xff;
1138 if(len && str[0])
1139 str[0] = '\0'; /* FIXME: OK to shorten? */
1141 return ret;
1144 /*********************************************************************
1145 * _mbsnccnt(MSVCRT.@)
1146 * 'c' is for 'character'.
1148 MSVCRT_size_t CDECL _mbsnccnt(const unsigned char* str, MSVCRT_size_t len)
1150 MSVCRT_size_t ret;
1151 if(MSVCRT___mb_cur_max > 1)
1153 ret=0;
1154 while(*str && len-- > 0)
1156 if(MSVCRT_isleadbyte(*str))
1158 if (!len)
1159 break;
1160 len--;
1161 str++;
1163 str++;
1164 ret++;
1166 return ret;
1168 ret=u_strlen(str);
1169 return min(ret, len); /* ASCII CP */
1172 /*********************************************************************
1173 * _mbsnbcnt(MSVCRT.@)
1174 * 'b' is for byte count.
1176 MSVCRT_size_t CDECL _mbsnbcnt(const unsigned char* str, MSVCRT_size_t len)
1178 MSVCRT_size_t ret;
1179 if(MSVCRT___mb_cur_max > 1)
1181 const unsigned char* xstr = str;
1182 while(*xstr && len-- > 0)
1184 if (MSVCRT_isleadbyte(*xstr++))
1185 xstr++;
1187 return xstr-str;
1189 ret=u_strlen(str);
1190 return min(ret, len); /* ASCII CP */
1193 /*********************************************************************
1194 * _mbsnbcat(MSVCRT.@)
1196 unsigned char* CDECL _mbsnbcat(unsigned char* dst, const unsigned char* src, MSVCRT_size_t len)
1198 if(MSVCRT___mb_cur_max > 1)
1200 unsigned char *res = dst;
1201 while (*dst) {
1202 if (MSVCRT_isleadbyte(*dst++)) {
1203 if (*dst) {
1204 dst++;
1205 } else {
1206 /* as per msdn overwrite the lead byte in front of '\0' */
1207 dst--;
1208 break;
1212 while (*src && len--) *dst++ = *src++;
1213 *dst = '\0';
1214 return res;
1216 return u_strncat(dst, src, len); /* ASCII CP */
1220 /*********************************************************************
1221 * _mbsncat(MSVCRT.@)
1223 unsigned char* CDECL _mbsncat(unsigned char* dst, const unsigned char* src, MSVCRT_size_t len)
1225 if(MSVCRT___mb_cur_max > 1)
1227 unsigned char *res = dst;
1228 while (*dst)
1230 if (MSVCRT_isleadbyte(*dst++))
1231 dst++;
1233 while (*src && len--)
1235 *dst++ = *src;
1236 if(MSVCRT_isleadbyte(*src++))
1237 *dst++ = *src++;
1239 *dst = '\0';
1240 return res;
1242 return u_strncat(dst, src, len); /* ASCII CP */
1246 /*********************************************************************
1247 * _mbslwr(MSVCRT.@)
1249 unsigned char* CDECL _mbslwr(unsigned char* s)
1251 unsigned char *ret = s;
1252 if (!s)
1253 return NULL;
1254 if (MSVCRT___mb_cur_max > 1)
1256 unsigned int c;
1257 while (*s)
1259 c = _mbctolower(_mbsnextc(s));
1260 /* Note that I assume that the size of the character is unchanged */
1261 if (c > 255)
1263 *s++=(c>>8);
1264 c=c & 0xff;
1266 *s++=c;
1269 else for ( ; *s; s++) *s = tolower(*s);
1270 return ret;
1274 /*********************************************************************
1275 * _mbsupr(MSVCRT.@)
1277 unsigned char* CDECL _mbsupr(unsigned char* s)
1279 unsigned char *ret = s;
1280 if (!s)
1281 return NULL;
1282 if (MSVCRT___mb_cur_max > 1)
1284 unsigned int c;
1285 while (*s)
1287 c = _mbctoupper(_mbsnextc(s));
1288 /* Note that I assume that the size of the character is unchanged */
1289 if (c > 255)
1291 *s++=(c>>8);
1292 c=c & 0xff;
1294 *s++=c;
1297 else for ( ; *s; s++) *s = toupper(*s);
1298 return ret;
1302 /*********************************************************************
1303 * _mbsspn (MSVCRT.@)
1305 MSVCRT_size_t CDECL _mbsspn(const unsigned char* string, const unsigned char* set)
1307 const unsigned char *p, *q;
1309 for (p = string; *p; p++)
1311 if (MSVCRT_isleadbyte(*p))
1313 for (q = set; *q; q++)
1315 if (!q[1])
1316 break;
1317 if ((*p == *q) && (p[1] == q[1]))
1318 break;
1319 q++;
1321 if (!q[0] || !q[1]) break;
1323 else
1325 for (q = set; *q; q++)
1326 if (*p == *q)
1327 break;
1328 if (!*q) break;
1331 return p - string;
1334 /*********************************************************************
1335 * _mbsspnp (MSVCRT.@)
1337 const unsigned char* CDECL _mbsspnp(const unsigned char* string, const unsigned char* set)
1339 const unsigned char *p, *q;
1341 for (p = string; *p; p++)
1343 if (MSVCRT_isleadbyte(*p))
1345 for (q = set; *q; q++)
1347 if (!q[1])
1348 break;
1349 if ((*p == *q) && (p[1] == q[1]))
1350 break;
1351 q++;
1353 if (!q[0] || !q[1]) break;
1355 else
1357 for (q = set; *q; q++)
1358 if (*p == *q)
1359 break;
1360 if (!*q) break;
1363 if (*p == '\0')
1364 return NULL;
1365 return p;
1368 /*********************************************************************
1369 * _mbscspn(MSVCRT.@)
1371 MSVCRT_size_t CDECL _mbscspn(const unsigned char* str, const unsigned char* cmp)
1373 if (MSVCRT___mb_cur_max > 1)
1374 FIXME("don't handle double character case\n");
1375 return u_strcspn(str, cmp);
1378 /*********************************************************************
1379 * _mbsrev (MSVCRT.@)
1381 unsigned char* CDECL _mbsrev(unsigned char* str)
1383 int i, len = _mbslen(str);
1384 unsigned char *p, *temp=MSVCRT_malloc(len*2);
1386 if(!temp)
1387 return str;
1389 /* unpack multibyte string to temp buffer */
1390 p=str;
1391 for(i=0; i<len; i++)
1393 if (MSVCRT_isleadbyte(*p))
1395 temp[i*2]=*p++;
1396 temp[i*2+1]=*p++;
1398 else
1400 temp[i*2]=*p++;
1401 temp[i*2+1]=0;
1405 /* repack it in the reverse order */
1406 p=str;
1407 for(i=len-1; i>=0; i--)
1409 if(MSVCRT_isleadbyte(temp[i*2]))
1411 *p++=temp[i*2];
1412 *p++=temp[i*2+1];
1414 else
1416 *p++=temp[i*2];
1420 MSVCRT_free(temp);
1422 return str;
1425 /*********************************************************************
1426 * _mbspbrk (MSVCRT.@)
1428 unsigned char* CDECL _mbspbrk(const unsigned char* str, const unsigned char* accept)
1430 const unsigned char* p;
1432 while(*str)
1434 for(p = accept; *p; p += (MSVCRT_isleadbyte(*p)?2:1) )
1436 if (*p == *str)
1437 if( !MSVCRT_isleadbyte(*p) || ( *(p+1) == *(str+1) ) )
1438 return (unsigned char*)str;
1440 str += (MSVCRT_isleadbyte(*str)?2:1);
1442 return NULL;
1447 * Functions depending on locale codepage
1450 /*********************************************************************
1451 * mblen(MSVCRT.@)
1452 * REMARKS
1453 * Unlike most of the multibyte string functions this function uses
1454 * the locale codepage, not the codepage set by _setmbcp
1456 int CDECL MSVCRT_mblen(const char* str, MSVCRT_size_t size)
1458 if (str && *str && size)
1460 if(MSVCRT___mb_cur_max == 1)
1461 return 1; /* ASCII CP */
1463 return !MSVCRT_isleadbyte(*str) ? 1 : (size>1 ? 2 : -1);
1465 return 0;
1468 /*********************************************************************
1469 * _mbstrlen(MSVCRT.@)
1470 * REMARKS
1471 * Unlike most of the multibyte string functions this function uses
1472 * the locale codepage, not the codepage set by _setmbcp
1474 MSVCRT_size_t CDECL _mbstrlen(const char* str)
1476 if(MSVCRT___mb_cur_max > 1)
1478 MSVCRT_size_t len = 0;
1479 while(*str)
1481 /* FIXME: According to the documentation we are supposed to test for
1482 * multi-byte character validity. Whatever that means
1484 str += MSVCRT_isleadbyte(*str) ? 2 : 1;
1485 len++;
1487 return len;
1489 return strlen(str); /* ASCII CP */