advapi32/tests: Use the available ARRAY_SIZE() macro.
[wine.git] / dlls / msvcrt / mbcs.c
blob7ca1906dbe72e9b0ea6e07425c5d60f8bcbac060
1 /*
2 * msvcrt.dll mbcs functions
4 * Copyright 1999 Alexandre Julliard
5 * Copyright 2000 Jon Griffths
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 * FIXME
22 * Not currently binary compatible with win32. MSVCRT_mbctype must be
23 * populated correctly and the ismb* functions should reference it.
26 #include <stdio.h>
27 #include <limits.h>
29 #include "msvcrt.h"
30 #include "wine/unicode.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
35 unsigned char MSVCRT_mbctype[257] = { 0 };
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 cp932 single byte character to multi byte character */
57 static const unsigned char mbbtombc_932[] = {
58 0x40,0x49,0x68,0x94,0x90,0x93,0x95,0x66,0x69,0x6a,0x96,0x7b,0x43,0x7c,0x44,0x5e,
59 0x4f,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x46,0x47,0x83,0x81,0x84,0x48,
60 0x97,0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,
61 0x6f,0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x6d,0x8f,0x6e,0x4f,0x76,
62 0x77,0x78,0x79,0x6d,0x8f,0x6e,0x4f,0x51,0x65,0x81,0x82,0x83,0x84,0x85,0x86,0x87,
63 0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f,0x90,0x91,0x92,0x93,0x94,0x95,0x50,
64 0x42,0x75,0x76,0x41,0x45,0x92,0x40,0x42,0x44,0x46,0x48,0x83,0x85,0x87,0x62,
65 0x5b,0x41,0x43,0x45,0x47,0x49,0x4a,0x4c,0x4e,0x50,0x52,0x54,0x56,0x58,0x5a,0x5c,
66 0x5e,0x60,0x63,0x65,0x67,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x71,0x74,0x77,0x7a,0x7d,
67 0x7e,0x80,0x81,0x82,0x84,0x86,0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8f,0x93,0x4a,0x4b };
69 /* Maps multibyte cp932 punctuation marks to single byte equivalents */
70 static const unsigned char mbctombb_932_punct[] = {
71 0x20,0xa4,0xa1,0x2c,0x2e,0xa5,0x3a,0x3b,0x3f,0x21,0xde,0xdf,0x00,0x00,0x00,0x5e,
72 0x7e,0x5f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xb0,0x00,0x00,0x2f,0x00,
73 0x00,0x00,0x7c,0x00,0x00,0x60,0x27,0x00,0x22,0x28,0x29,0x00,0x00,0x5b,0x5d,0x7b,
74 0x7d,0x00,0x00,0x00,0x00,0xa2,0xa3,0x00,0x00,0x00,0x00,0x2b,0x2d,0x00,0x00,0x00,
75 0x00,0x3d,0x00,0x3c,0x3e,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x5c,
76 0x24,0x00,0x00,0x25,0x23,0x26,0x2a,0x40};
78 /* Maps multibyte cp932 hiragana/katakana to single-byte equivalents */
79 static const unsigned char mbctombb_932_kana[] = {
80 0xa7,0xb1,0xa8,0xb2,0xa9,0xb3,0xaa,0xb4,0xab,0xb5,0xb6,0xb6,0xb7,0xb7,0xb8,0xb8,
81 0xb9,0xb9,0xba,0xba,0xbb,0xbb,0xbc,0xbc,0xbd,0xbd,0xbe,0xbe,0xbf,0xbf,0xc0,0xc0,
82 0xc1,0xc1,0xaf,0xc2,0xc2,0xc3,0xc3,0xc4,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xca,
83 0xca,0xcb,0xcb,0xcb,0xcc,0xcc,0xcc,0xcd,0xcd,0xcd,0xce,0xce,0xce,0xcf,0xd0,0xd1,
84 0xd2,0xd3,0xac,0xd4,0xad,0xd5,0xae,0xd6,0xd7,0xd8,0xd9,0xda,0xdb,0xdc,0xdc,0xb2,
85 0xb4,0xa6,0xdd,0xb3,0xb6,0xb9};
87 static MSVCRT_wchar_t msvcrt_mbc_to_wc(unsigned int ch)
89 MSVCRT_wchar_t chW;
90 char mbch[2];
91 int n_chars;
93 if (ch <= 0xff) {
94 mbch[0] = ch;
95 n_chars = 1;
96 } else {
97 mbch[0] = (ch >> 8) & 0xff;
98 mbch[1] = ch & 0xff;
99 n_chars = 2;
101 if (!MultiByteToWideChar(get_mbcinfo()->mbcodepage, 0, mbch, n_chars, &chW, 1))
103 WARN("MultiByteToWideChar failed on %x\n", ch);
104 return 0;
106 return chW;
109 static inline MSVCRT_size_t u_strlen( const unsigned char *str )
111 return strlen( (const char*) str );
114 static inline unsigned char* u_strncat( unsigned char* dst, const unsigned char* src, MSVCRT_size_t len )
116 return (unsigned char*)strncat( (char*)dst, (const char*)src, len);
119 static inline int u_strcmp( const unsigned char *s1, const unsigned char *s2 )
121 return strcmp( (const char*)s1, (const char*)s2 );
124 static inline int u_strcasecmp( const unsigned char *s1, const unsigned char *s2 )
126 return strcasecmp( (const char*)s1, (const char*)s2 );
129 static inline int u_strncmp( const unsigned char *s1, const unsigned char *s2, MSVCRT_size_t len )
131 return strncmp( (const char*)s1, (const char*)s2, len );
134 static inline int u_strncasecmp( const unsigned char *s1, const unsigned char *s2, MSVCRT_size_t len )
136 return strncasecmp( (const char*)s1, (const char*)s2, len );
139 static inline unsigned char *u_strchr( const unsigned char *s, unsigned char x )
141 return (unsigned char*) strchr( (const char*)s, x );
144 static inline unsigned char *u_strrchr( const unsigned char *s, unsigned char x )
146 return (unsigned char*) strrchr( (const char*)s, x );
149 static inline unsigned char *u__strset( unsigned char *s, unsigned char c )
151 return (unsigned char*) _strset( (char*)s, c);
154 static inline unsigned char *u__strnset( unsigned char *s, unsigned char c, MSVCRT_size_t len )
156 return (unsigned char*) MSVCRT__strnset( (char*)s, c, len );
159 static inline MSVCRT_size_t u_strcspn( const unsigned char *s, const unsigned char *rej )
161 return strcspn( (const char *)s, (const char*)rej );
164 /*********************************************************************
165 * __p__mbctype (MSVCRT.@)
167 unsigned char* CDECL __p__mbctype(void)
169 return get_mbcinfo()->mbctype;
172 /*********************************************************************
173 * __p___mb_cur_max(MSVCRT.@)
175 int* CDECL __p___mb_cur_max(void)
177 return &get_locinfo()->mb_cur_max;
180 /*********************************************************************
181 * ___mb_cur_max_func(MSVCRT.@)
183 int CDECL MSVCRT____mb_cur_max_func(void)
185 return get_locinfo()->mb_cur_max;
188 #if _MSVCR_VER>=80
189 /*********************************************************************
190 * ___mb_cur_max_l_func (MSVCR80.@)
192 int* CDECL ___mb_cur_max_l_func(MSVCRT__locale_t locale)
194 MSVCRT_pthreadlocinfo locinfo;
196 if(!locale)
197 locinfo = get_locinfo();
198 else
199 locinfo = locale->locinfo;
201 return &locinfo->mb_cur_max;
203 #endif
205 /*********************************************************************
206 * INTERNAL: _setmbcp_l
208 int _setmbcp_l(int cp, LCID lcid, MSVCRT_pthreadmbcinfo mbcinfo)
210 const char format[] = ".%d";
212 int newcp;
213 CPINFO cpi;
214 BYTE *bytes;
215 WORD chartypes[256];
216 char bufA[256];
217 WCHAR bufW[256];
218 int charcount;
219 int ret;
220 int i;
222 if(!mbcinfo)
223 mbcinfo = get_mbcinfo();
225 switch (cp)
227 case _MB_CP_ANSI:
228 newcp = GetACP();
229 break;
230 case _MB_CP_OEM:
231 newcp = GetOEMCP();
232 break;
233 case _MB_CP_LOCALE:
234 newcp = get_locinfo()->lc_codepage;
235 if(newcp)
236 break;
237 /* fall through (C locale) */
238 case _MB_CP_SBCS:
239 newcp = 20127; /* ASCII */
240 break;
241 default:
242 newcp = cp;
243 break;
246 if(lcid == -1) {
247 sprintf(bufA, format, newcp);
248 mbcinfo->mblcid = MSVCRT_locale_to_LCID(bufA, NULL);
249 } else {
250 mbcinfo->mblcid = lcid;
253 if(mbcinfo->mblcid == -1)
255 WARN("Can't assign LCID to codepage (%d)\n", mbcinfo->mblcid);
256 mbcinfo->mblcid = 0;
259 if (!GetCPInfo(newcp, &cpi))
261 WARN("Codepage %d not found\n", newcp);
262 *MSVCRT__errno() = MSVCRT_EINVAL;
263 return -1;
266 /* setup the _mbctype */
267 memset(mbcinfo->mbctype, 0, sizeof(unsigned char[257]));
268 memset(mbcinfo->mbcasemap, 0, sizeof(unsigned char[256]));
270 bytes = cpi.LeadByte;
271 while (bytes[0] || bytes[1])
273 for (i = bytes[0]; i <= bytes[1]; i++)
274 mbcinfo->mbctype[i + 1] |= _M1;
275 bytes += 2;
278 if (cpi.MaxCharSize > 1)
280 /* trail bytes not available through kernel32 but stored in a structure in msvcrt */
281 struct cp_extra_info_t *cpextra = g_cpextrainfo;
283 mbcinfo->ismbcodepage = 1;
284 while (TRUE)
286 if (cpextra->cp == 0 || cpextra->cp == newcp)
288 if (cpextra->cp == 0)
289 FIXME("trail bytes data not available for DBCS codepage %d - assuming all bytes\n", newcp);
291 bytes = cpextra->TrailBytes;
292 while (bytes[0] || bytes[1])
294 for (i = bytes[0]; i <= bytes[1]; i++)
295 mbcinfo->mbctype[i + 1] |= _M2;
296 bytes += 2;
298 break;
300 cpextra++;
303 else
304 mbcinfo->ismbcodepage = 0;
306 /* we can't use GetStringTypeA directly because we don't have a locale - only a code page
308 charcount = 0;
309 for (i = 0; i < 256; i++)
310 if (!(mbcinfo->mbctype[i + 1] & _M1))
311 bufA[charcount++] = i;
313 ret = MultiByteToWideChar(newcp, 0, bufA, charcount, bufW, charcount);
314 if (ret != charcount)
315 ERR("MultiByteToWideChar of chars failed for cp %d, ret=%d (exp %d), error=%d\n", newcp, ret, charcount, GetLastError());
317 GetStringTypeW(CT_CTYPE1, bufW, charcount, chartypes);
319 charcount = 0;
320 for (i = 0; i < 256; i++)
321 if (!(mbcinfo->mbctype[i + 1] & _M1))
323 if (chartypes[charcount] & C1_UPPER)
325 mbcinfo->mbctype[i + 1] |= _SBUP;
326 bufW[charcount] = tolowerW(bufW[charcount]);
328 else if (chartypes[charcount] & C1_LOWER)
330 mbcinfo->mbctype[i + 1] |= _SBLOW;
331 bufW[charcount] = toupperW(bufW[charcount]);
333 charcount++;
336 ret = WideCharToMultiByte(newcp, 0, bufW, charcount, bufA, charcount, NULL, NULL);
337 if (ret != charcount)
338 ERR("WideCharToMultiByte failed for cp %d, ret=%d (exp %d), error=%d\n", newcp, ret, charcount, GetLastError());
340 charcount = 0;
341 for (i = 0; i < 256; i++)
343 if(!(mbcinfo->mbctype[i + 1] & _M1))
345 if(mbcinfo->mbctype[i] & (C1_UPPER|C1_LOWER))
346 mbcinfo->mbcasemap[i] = bufA[charcount];
347 charcount++;
351 if (newcp == 932) /* CP932 only - set _MP and _MS */
353 /* On Windows it's possible to calculate the _MP and _MS from CT_CTYPE1
354 * and CT_CTYPE3. But as of Wine 0.9.43 we return wrong values what makes
355 * it hard. As this is set only for codepage 932 we hardcode it what gives
356 * also faster execution.
358 for (i = 161; i <= 165; i++)
359 mbcinfo->mbctype[i + 1] |= _MP;
360 for (i = 166; i <= 223; i++)
361 mbcinfo->mbctype[i + 1] |= _MS;
364 mbcinfo->mbcodepage = newcp;
365 if(MSVCRT_locale && mbcinfo == MSVCRT_locale->mbcinfo)
366 memcpy(MSVCRT_mbctype, MSVCRT_locale->mbcinfo->mbctype, sizeof(MSVCRT_mbctype));
368 return 0;
371 /*********************************************************************
372 * _setmbcp (MSVCRT.@)
374 int CDECL _setmbcp(int cp)
376 return _setmbcp_l(cp, -1, NULL);
379 /*********************************************************************
380 * _getmbcp (MSVCRT.@)
382 int CDECL _getmbcp(void)
384 return get_mbcinfo()->mbcodepage;
387 /*********************************************************************
388 * _mbsnextc(MSVCRT.@)
390 unsigned int CDECL _mbsnextc(const unsigned char* str)
392 if(_ismbblead(*str))
393 return *str << 8 | str[1];
394 return *str;
397 /*********************************************************************
398 * _mbctolower(MSVCRT.@)
400 unsigned int CDECL _mbctolower(unsigned int c)
402 if (_ismbblead(c))
404 FIXME("Handle MBC chars\n");
405 return c;
407 return tolower(c); /* ASCII CP or SB char */
410 /*********************************************************************
411 * _mbctoupper(MSVCRT.@)
413 unsigned int CDECL _mbctoupper(unsigned int c)
415 if (_ismbblead(c))
417 FIXME("Handle MBC chars\n");
418 return c;
420 return toupper(c); /* ASCII CP or SB char */
423 /*********************************************************************
424 * _mbctombb (MSVCRT.@)
426 unsigned int CDECL _mbctombb(unsigned int c)
428 unsigned int value;
430 if(get_mbcinfo()->mbcodepage == 932)
432 if(c >= 0x829f && c <= 0x82f1) /* Hiragana */
433 return mbctombb_932_kana[c - 0x829f];
434 if(c >= 0x8340 && c <= 0x8396 && c != 0x837f) /* Katakana */
435 return mbctombb_932_kana[c - 0x8340 - (c >= 0x837f ? 1 : 0)];
436 if(c >= 0x8140 && c <= 0x8197) /* Punctuation */
438 value = mbctombb_932_punct[c - 0x8140];
439 return value ? value : c;
441 if((c >= 0x824f && c <= 0x8258) || /* Fullwidth digits */
442 (c >= 0x8260 && c <= 0x8279)) /* Fullwidth capitals letters */
443 return c - 0x821f;
444 if(c >= 0x8281 && c <= 0x829a) /* Fullwidth small letters */
445 return c - 0x8220;
446 /* all other cases return c */
448 return c;
451 /*********************************************************************
452 * _mbcjistojms(MSVCRT.@)
454 * Converts a jis character to sjis.
455 * Based on description from
456 * http://www.slayers.ne.jp/~oouchi/code/jistosjis.html
458 unsigned int CDECL _mbcjistojms(unsigned int c)
460 /* Conversion takes place only when codepage is 932.
461 In all other cases, c is returned unchanged */
462 if(get_mbcinfo()->mbcodepage == 932)
464 if(HIBYTE(c) >= 0x21 && HIBYTE(c) <= 0x7e &&
465 LOBYTE(c) >= 0x21 && LOBYTE(c) <= 0x7e)
467 if(HIBYTE(c) % 2)
468 c += 0x1f;
469 else
470 c += 0x7d;
472 if(LOBYTE(c) >= 0x7F)
473 c += 0x1;
475 c = (((HIBYTE(c) - 0x21)/2 + 0x81) << 8) | LOBYTE(c);
477 if(HIBYTE(c) > 0x9f)
478 c += 0x4000;
480 else
481 return 0; /* Codepage is 932, but c can't be converted */
484 return c;
487 /*********************************************************************
488 * _mbcjmstojis(MSVCRT.@)
490 * Converts a sjis character to jis.
492 unsigned int CDECL _mbcjmstojis(unsigned int c)
494 /* Conversion takes place only when codepage is 932.
495 In all other cases, c is returned unchanged */
496 if(get_mbcinfo()->mbcodepage == 932)
498 if(_ismbclegal(c) && HIBYTE(c) < 0xf0)
500 if(HIBYTE(c) >= 0xe0)
501 c -= 0x4000;
503 c = (((HIBYTE(c) - 0x81)*2 + 0x21) << 8) | LOBYTE(c);
505 if(LOBYTE(c) > 0x7f)
506 c -= 0x1;
508 if(LOBYTE(c) > 0x9d)
509 c += 0x83;
510 else
511 c -= 0x1f;
513 else
514 return 0; /* Codepage is 932, but c can't be converted */
517 return c;
520 /*********************************************************************
521 * _mbsdec(MSVCRT.@)
523 unsigned char* CDECL _mbsdec(const unsigned char* start, const unsigned char* cur)
525 if(start >= cur)
526 return NULL;
528 if(get_mbcinfo()->ismbcodepage)
529 return (unsigned char *)(_ismbstrail(start,cur-1) ? cur - 2 : cur -1);
531 return (unsigned char *)cur - 1; /* ASCII CP or SB char */
534 /*********************************************************************
535 * _mbclen(MSVCRT.@)
537 unsigned int CDECL _mbclen(const unsigned char* str)
539 return _ismbblead(*str) ? 2 : 1;
542 /*********************************************************************
543 * _mbsinc(MSVCRT.@)
545 unsigned char* CDECL _mbsinc(const unsigned char* str)
547 return (unsigned char *)(str + _mbclen(str));
550 /*********************************************************************
551 * _mbsninc(MSVCRT.@)
553 unsigned char* CDECL _mbsninc(const unsigned char* str, MSVCRT_size_t num)
555 if(!str)
556 return NULL;
558 while (num > 0 && *str)
560 if (_ismbblead(*str))
562 if (!*(str+1))
563 break;
564 str++;
566 str++;
567 num--;
570 return (unsigned char*)str;
573 /*********************************************************************
574 * _mbsnlen_l(MSVCRT.@)
576 MSVCRT_size_t CDECL _mbsnlen_l(const unsigned char *str,
577 MSVCRT_size_t maxsize, MSVCRT__locale_t locale)
579 MSVCRT_pthreadmbcinfo mbcinfo;
580 MSVCRT_size_t i = 0, len = 0;
582 if(!locale)
583 mbcinfo = get_mbcinfo();
584 else
585 mbcinfo = locale->mbcinfo;
587 if(!mbcinfo->ismbcodepage)
588 return MSVCRT_strnlen((const char*)str, maxsize);
590 while(i<maxsize && str[i])
592 if (_ismbblead_l(str[i], locale))
594 i++;
595 if (!str[i]) /* count only full chars */
596 break;
598 i++;
599 len++;
601 return i < maxsize ? len : maxsize;
604 /*********************************************************************
605 * _mbslen(MSVCRT.@)
607 MSVCRT_size_t CDECL _mbslen(const unsigned char* str)
609 return _mbsnlen_l(str, -1, NULL);
612 /*********************************************************************
613 * _mbslen_l(MSVCRT.@)
615 MSVCRT_size_t CDECL _mbslen_l(const unsigned char* str, MSVCRT__locale_t locale)
617 return _mbsnlen_l(str, -1, locale);
620 /*********************************************************************
621 * _mbsnlen(MSVCRT.@)
623 MSVCRT_size_t CDECL _mbsnlen(const unsigned char* str, MSVCRT_size_t maxsize)
625 return _mbsnlen_l(str, maxsize, NULL);
628 /*********************************************************************
629 * _mbccpy_s_l(MSVCRT.@)
631 int CDECL _mbccpy_s_l(unsigned char* dest, MSVCRT_size_t maxsize,
632 int *copied, const unsigned char* src, MSVCRT__locale_t locale)
634 if(copied) *copied = 0;
635 if(!MSVCRT_CHECK_PMT(dest != NULL && maxsize >= 1)) return MSVCRT_EINVAL;
636 dest[0] = 0;
637 if(!MSVCRT_CHECK_PMT(src != NULL)) return MSVCRT_EINVAL;
639 if(_ismbblead_l(*src, locale)) {
640 if(!src[1]) {
641 if(copied) *copied = 1;
642 *MSVCRT__errno() = MSVCRT_EILSEQ;
643 return MSVCRT_EILSEQ;
646 if(maxsize < 2) {
647 MSVCRT_INVALID_PMT("dst buffer is too small", MSVCRT_ERANGE);
648 return MSVCRT_ERANGE;
651 *dest++ = *src++;
652 *dest = *src;
653 if(copied) *copied = 2;
654 }else {
655 *dest = *src;
656 if(copied) *copied = 1;
659 return 0;
662 /*********************************************************************
663 * _mbccpy(MSVCRT.@)
665 void CDECL _mbccpy(unsigned char* dest, const unsigned char* src)
667 _mbccpy_s_l(dest, 2, NULL, src, NULL);
670 /*********************************************************************
671 * _mbccpy_l(MSVCRT.@)
673 void CDECL _mbccpy_l(unsigned char* dest, const unsigned char* src,
674 MSVCRT__locale_t locale)
676 _mbccpy_s_l(dest, 2, NULL, src, locale);
679 /*********************************************************************
680 * _mbccpy_s(MSVCRT.@)
682 int CDECL _mbccpy_s(unsigned char* dest, MSVCRT_size_t maxsize,
683 int *copied, const unsigned char* src)
685 return _mbccpy_s_l(dest, maxsize, copied, src, NULL);
688 /*********************************************************************
689 * _mbsncpy(MSVCRT.@)
690 * REMARKS
691 * The parameter n is the number or characters to copy, not the size of
692 * the buffer. Use _mbsnbcpy for a function analogical to strncpy
694 unsigned char* CDECL _mbsncpy(unsigned char* dst, const unsigned char* src, MSVCRT_size_t n)
696 unsigned char* ret = dst;
697 if(!n)
698 return dst;
699 if (get_mbcinfo()->ismbcodepage)
701 while (*src && n)
703 n--;
704 if (_ismbblead(*src))
706 if (!*(src+1))
708 *dst++ = 0;
709 *dst++ = 0;
710 break;
713 *dst++ = *src++;
716 *dst++ = *src++;
719 else
721 while (n)
723 n--;
724 if (!(*dst++ = *src++)) break;
727 while (n--) *dst++ = 0;
728 return ret;
731 /*********************************************************************
732 * _mbsnbcpy_s_l(MSVCRT.@)
733 * REMARKS
734 * Unlike _mbsnbcpy this function does not pad the rest of the dest
735 * string with 0
737 int CDECL _mbsnbcpy_s_l(unsigned char* dst, MSVCRT_size_t size,
738 const unsigned char* src, MSVCRT_size_t n, MSVCRT__locale_t locale)
740 MSVCRT_size_t pos = 0;
742 if(!dst || size == 0)
743 return MSVCRT_EINVAL;
744 if(!src)
746 dst[0] = '\0';
747 return MSVCRT_EINVAL;
749 if(!n)
750 return 0;
752 if((locale ? locale->mbcinfo : get_mbcinfo())->ismbcodepage)
754 BOOL is_lead = FALSE;
755 while (*src && n)
757 if(pos == size)
759 dst[0] = '\0';
760 return MSVCRT_ERANGE;
762 is_lead = (!is_lead && _ismbblead(*src));
763 n--;
764 dst[pos++] = *src++;
767 if (is_lead) /* if string ends with a lead, remove it */
768 dst[pos - 1] = 0;
770 else
772 while (n)
774 n--;
775 if(pos == size)
777 dst[0] = '\0';
778 return MSVCRT_ERANGE;
781 if(!(*src)) break;
782 dst[pos++] = *src++;
786 if(pos < size)
787 dst[pos] = '\0';
788 else
790 dst[0] = '\0';
791 return MSVCRT_ERANGE;
794 return 0;
797 /*********************************************************************
798 * _mbsnbcpy_s(MSVCRT.@)
800 int CDECL _mbsnbcpy_s(unsigned char* dst, MSVCRT_size_t size, const unsigned char* src, MSVCRT_size_t n)
802 return _mbsnbcpy_s_l(dst, size, src, n, NULL);
805 /*********************************************************************
806 * _mbscpy_s_l(MSVCRT.@)
808 int CDECL _mbscpy_s_l(unsigned char *dst, MSVCRT_size_t size,
809 const unsigned char *src, MSVCRT__locale_t locale)
811 return _mbsnbcpy_s_l(dst, size, src, -1, locale);
814 /*********************************************************************
815 * _mbscpy_s(MSVCRT.@)
817 int CDECL _mbscpy_s(unsigned char *dst, MSVCRT_size_t size, const unsigned char *src)
819 return _mbscpy_s_l(dst, size, src, NULL);
822 /*********************************************************************
823 * _mbsnbcpy(MSVCRT.@)
824 * REMARKS
825 * Like strncpy this function doesn't enforce the string to be
826 * NUL-terminated
828 unsigned char* CDECL _mbsnbcpy(unsigned char* dst, const unsigned char* src, MSVCRT_size_t n)
830 unsigned char* ret = dst;
831 if(!n)
832 return dst;
833 if(get_mbcinfo()->ismbcodepage)
835 BOOL is_lead = FALSE;
836 while (*src && n)
838 is_lead = (!is_lead && _ismbblead(*src));
839 n--;
840 *dst++ = *src++;
843 if (is_lead) /* if string ends with a lead, remove it */
844 *(dst - 1) = 0;
846 else
848 while (n)
850 n--;
851 if (!(*dst++ = *src++)) break;
854 while (n--) *dst++ = 0;
855 return ret;
858 /*********************************************************************
859 * _mbscmp(MSVCRT.@)
861 int CDECL _mbscmp(const unsigned char* str, const unsigned char* cmp)
863 if (!str || !cmp) return INT_MAX;
865 if(get_mbcinfo()->ismbcodepage)
867 unsigned int strc, cmpc;
868 do {
869 if(!*str)
870 return *cmp ? -1 : 0;
871 if(!*cmp)
872 return 1;
873 strc = _mbsnextc(str);
874 cmpc = _mbsnextc(cmp);
875 if(strc != cmpc)
876 return strc < cmpc ? -1 : 1;
877 str +=(strc > 255) ? 2 : 1;
878 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
879 } while(1);
881 return u_strcmp(str, cmp); /* ASCII CP */
884 /*********************************************************************
885 * _mbsnbicoll_l(MSVCRT.@)
887 int CDECL _mbsnbicoll_l(const unsigned char *str1, const unsigned char *str2, MSVCRT_size_t len, MSVCRT__locale_t locale)
889 MSVCRT_pthreadmbcinfo mbcinfo;
891 if(!locale)
892 mbcinfo = get_mbcinfo();
893 else
894 mbcinfo = locale->mbcinfo;
896 if(!mbcinfo->ismbcodepage)
897 return MSVCRT__strnicoll_l((const char*)str1, (const char*)str2, len, locale);
898 return CompareStringA(mbcinfo->mblcid, NORM_IGNORECASE, (const char*)str1, len, (const char*)str2, len)-CSTR_EQUAL;
901 /*********************************************************************
902 * _mbsicoll_l(MSVCRT.@)
904 int CDECL _mbsicoll_l(const unsigned char *str1, const unsigned char *str2, MSVCRT__locale_t locale)
906 return _mbsnbicoll_l(str1, str2, -1, locale);
909 /*********************************************************************
910 * _mbsnbicoll(MSVCRT.@)
912 int CDECL _mbsnbicoll(const unsigned char *str1, const unsigned char *str2, MSVCRT_size_t len)
914 return _mbsnbicoll_l(str1, str2, len, NULL);
917 /*********************************************************************
918 * _mbsicoll(MSVCRT.@)
920 int CDECL _mbsicoll(const unsigned char* str, const unsigned char* cmp)
922 #if _MSVCR_VER>=60 && _MSVCR_VER<=71
923 return CompareStringA(get_mbcinfo()->mblcid, NORM_IGNORECASE,
924 (const char*)str, -1, (const char*)cmp, -1)-CSTR_EQUAL;
925 #else
926 return _mbsnbicoll_l(str, cmp, -1, NULL);
927 #endif
930 /*********************************************************************
931 * _mbsnbcoll_l(MSVCRT.@)
933 int CDECL _mbsnbcoll_l(const unsigned char *str1, const unsigned char *str2, MSVCRT_size_t len, MSVCRT__locale_t locale)
935 MSVCRT_pthreadmbcinfo mbcinfo;
937 if(!locale)
938 mbcinfo = get_mbcinfo();
939 else
940 mbcinfo = locale->mbcinfo;
942 if(!mbcinfo->ismbcodepage)
943 return MSVCRT__strncoll_l((const char*)str1, (const char*)str2, len, locale);
944 return CompareStringA(mbcinfo->mblcid, 0, (const char*)str1, len, (const char*)str2, len)-CSTR_EQUAL;
947 /*********************************************************************
948 * _mbscoll_l(MSVCRT.@)
950 int CDECL _mbscoll_l(const unsigned char *str1, const unsigned char *str2, MSVCRT__locale_t locale)
952 return _mbsnbcoll_l(str1, str2, -1, locale);
955 /*********************************************************************
956 * _mbsnbcoll(MSVCRT.@)
958 int CDECL _mbsnbcoll(const unsigned char *str1, const unsigned char *str2, MSVCRT_size_t len)
960 return _mbsnbcoll_l(str1, str2, len, NULL);
963 /*********************************************************************
964 * _mbscoll(MSVCRT.@)
966 int CDECL _mbscoll(const unsigned char* str, const unsigned char* cmp)
968 #if _MSVCR_VER>=60 && _MSVCR_VER<=71
969 return CompareStringA(get_mbcinfo()->mblcid, 0,
970 (const char*)str, -1, (const char*)cmp, -1)-CSTR_EQUAL;
971 #else
972 return _mbsnbcoll_l(str, cmp, -1, NULL);
973 #endif
976 /*********************************************************************
977 * _mbsicmp(MSVCRT.@)
979 int CDECL _mbsicmp(const unsigned char* str, const unsigned char* cmp)
981 if(get_mbcinfo()->ismbcodepage)
983 unsigned int strc, cmpc;
984 do {
985 if(!*str)
986 return *cmp ? -1 : 0;
987 if(!*cmp)
988 return 1;
989 strc = _mbctolower(_mbsnextc(str));
990 cmpc = _mbctolower(_mbsnextc(cmp));
991 if(strc != cmpc)
992 return strc < cmpc ? -1 : 1;
993 str +=(strc > 255) ? 2 : 1;
994 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
995 } while(1);
997 return u_strcasecmp(str, cmp); /* ASCII CP */
1000 /*********************************************************************
1001 * _mbsncmp(MSVCRT.@)
1003 int CDECL _mbsncmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
1005 if(!len)
1006 return 0;
1008 if(get_mbcinfo()->ismbcodepage)
1010 unsigned int strc, cmpc;
1011 while(len--)
1013 int inc;
1014 if(!*str)
1015 return *cmp ? -1 : 0;
1016 if(!*cmp)
1017 return 1;
1018 strc = _mbsnextc(str);
1019 cmpc = _mbsnextc(cmp);
1020 if(strc != cmpc)
1021 return strc < cmpc ? -1 : 1;
1022 inc=(strc > 255) ? 2 : 1; /* Equal, use same increment */
1023 str += inc;
1024 cmp += inc;
1026 return 0; /* Matched len chars */
1028 return u_strncmp(str, cmp, len); /* ASCII CP */
1031 /*********************************************************************
1032 * _mbsnbcmp(MSVCRT.@)
1034 int CDECL _mbsnbcmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
1036 if (!len)
1037 return 0;
1038 if(get_mbcinfo()->ismbcodepage)
1040 unsigned int strc, cmpc;
1041 while (len)
1043 int clen;
1044 if(!*str)
1045 return *cmp ? -1 : 0;
1046 if(!*cmp)
1047 return 1;
1048 if (_ismbblead(*str))
1050 strc=(len>=2)?_mbsnextc(str):0;
1051 clen=2;
1053 else
1055 strc=*str;
1056 clen=1;
1058 if (_ismbblead(*cmp))
1059 cmpc=(len>=2)?_mbsnextc(cmp):0;
1060 else
1061 cmpc=*cmp;
1062 if(strc != cmpc)
1063 return strc < cmpc ? -1 : 1;
1064 len -= clen;
1065 str += clen;
1066 cmp += clen;
1068 return 0; /* Matched len chars */
1070 return u_strncmp(str,cmp,len);
1073 /*********************************************************************
1074 * _mbsnicmp(MSVCRT.@)
1076 * Compare two multibyte strings case insensitively to 'len' characters.
1078 int CDECL _mbsnicmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
1080 /* FIXME: No tolower() for mb strings yet */
1081 if(get_mbcinfo()->ismbcodepage)
1083 unsigned int strc, cmpc;
1084 while(len--)
1086 if(!*str)
1087 return *cmp ? -1 : 0;
1088 if(!*cmp)
1089 return 1;
1090 strc = _mbctolower(_mbsnextc(str));
1091 cmpc = _mbctolower(_mbsnextc(cmp));
1092 if(strc != cmpc)
1093 return strc < cmpc ? -1 : 1;
1094 str +=(strc > 255) ? 2 : 1;
1095 cmp +=(strc > 255) ? 2 : 1; /* Equal, use same increment */
1097 return 0; /* Matched len chars */
1099 return u_strncasecmp(str, cmp, len); /* ASCII CP */
1102 /*********************************************************************
1103 * _mbsnbicmp(MSVCRT.@)
1105 int CDECL _mbsnbicmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
1107 if (!len)
1108 return 0;
1109 if(get_mbcinfo()->ismbcodepage)
1111 unsigned int strc, cmpc;
1112 while (len)
1114 int clen;
1115 if(!*str)
1116 return *cmp ? -1 : 0;
1117 if(!*cmp)
1118 return 1;
1119 if (_ismbblead(*str))
1121 strc=(len>=2)?_mbsnextc(str):0;
1122 clen=2;
1124 else
1126 strc=*str;
1127 clen=1;
1129 if (_ismbblead(*cmp))
1130 cmpc=(len>=2)?_mbsnextc(cmp):0;
1131 else
1132 cmpc=*cmp;
1133 strc = _mbctolower(strc);
1134 cmpc = _mbctolower(cmpc);
1135 if(strc != cmpc)
1136 return strc < cmpc ? -1 : 1;
1137 len -= clen;
1138 str += clen;
1139 cmp += clen;
1141 return 0; /* Matched len bytes */
1143 return u_strncasecmp(str,cmp,len);
1146 /*********************************************************************
1147 * _mbscat (MSVCRT.@)
1149 unsigned char * CDECL _mbscat( unsigned char *dst, const unsigned char *src )
1151 strcat( (char *)dst, (const char *)src );
1152 return dst;
1155 /*********************************************************************
1156 * _mbscat_s_l (MSVCRT.@)
1158 int CDECL _mbscat_s_l( unsigned char *dst, MSVCRT_size_t size,
1159 const unsigned char *src, MSVCRT__locale_t locale )
1161 MSVCRT_size_t i, j;
1162 int ret = 0;
1164 if(!MSVCRT_CHECK_PMT(dst != NULL)) return MSVCRT_EINVAL;
1165 if(!MSVCRT_CHECK_PMT(src != NULL)) return MSVCRT_EINVAL;
1167 for(i=0; i<size; i++)
1168 if(!dst[i]) break;
1169 if(i == size) {
1170 MSVCRT_INVALID_PMT("dst is not NULL-terminated", MSVCRT_EINVAL);
1171 if(size) dst[0] = 0;
1172 return MSVCRT_EINVAL;
1175 if(i && _ismbblead_l(dst[i-1], locale)) {
1176 ret = MSVCRT_EILSEQ;
1177 i--;
1180 for(j=0; src[j] && i+j<size; j++)
1181 dst[i+j] = src[j];
1182 if(i+j == size) {
1183 MSVCRT_INVALID_PMT("dst buffer is too small", MSVCRT_ERANGE);
1184 dst[0] = 0;
1185 return MSVCRT_ERANGE;
1188 if(j && _ismbblead_l(src[j-1], locale)) {
1189 ret = MSVCRT_EILSEQ;
1190 j--;
1193 dst[i+j] = 0;
1194 return ret;
1197 /*********************************************************************
1198 * _mbscat_s (MSVCRT.@)
1200 int CDECL _mbscat_s( unsigned char *dst, MSVCRT_size_t size, const unsigned char *src )
1202 return _mbscat_s_l(dst, size, src, NULL);
1205 /*********************************************************************
1206 * _mbscpy (MSVCRT.@)
1208 unsigned char* CDECL _mbscpy( unsigned char *dst, const unsigned char *src )
1210 strcpy( (char *)dst, (const char *)src );
1211 return dst;
1214 /*********************************************************************
1215 * _mbsstr (MSVCRT.@)
1217 unsigned char * CDECL _mbsstr(const unsigned char *haystack, const unsigned char *needle)
1219 return (unsigned char *)strstr( (const char *)haystack, (const char *)needle );
1222 /*********************************************************************
1223 * _mbschr(MSVCRT.@)
1225 * Find a multibyte character in a multibyte string.
1227 unsigned char* CDECL _mbschr(const unsigned char* s, unsigned int x)
1229 if(get_mbcinfo()->ismbcodepage)
1231 unsigned int c;
1232 while (1)
1234 c = _mbsnextc(s);
1235 if (c == x)
1236 return (unsigned char*)s;
1237 if (!c)
1238 return NULL;
1239 s += c > 255 ? 2 : 1;
1242 return u_strchr(s, x); /* ASCII CP */
1245 /*********************************************************************
1246 * _mbsrchr(MSVCRT.@)
1248 unsigned char* CDECL _mbsrchr(const unsigned char* s, unsigned int x)
1250 if(get_mbcinfo()->ismbcodepage)
1252 unsigned int c;
1253 unsigned char* match=NULL;
1254 if(!s)
1255 return NULL;
1256 while (1) {
1257 c = _mbsnextc(s);
1258 if (c == x)
1259 match=(unsigned char*)s;
1260 if (!c)
1261 return match;
1262 s +=(c > 255) ? 2 : 1;
1265 return u_strrchr(s, x);
1268 /*********************************************************************
1269 * _mbstok_s_l(MSVCRT.@)
1271 unsigned char* CDECL _mbstok_s_l(unsigned char *str, const unsigned char *delim,
1272 unsigned char **ctx, MSVCRT__locale_t locale)
1274 MSVCRT_pthreadmbcinfo mbcinfo;
1275 unsigned int c;
1277 if(!MSVCRT_CHECK_PMT(delim != NULL)) return NULL;
1278 if(!MSVCRT_CHECK_PMT(ctx != NULL)) return NULL;
1279 if(!MSVCRT_CHECK_PMT(str || *ctx)) return NULL;
1281 if(locale)
1282 mbcinfo = locale->mbcinfo;
1283 else
1284 mbcinfo = get_mbcinfo();
1286 if(!mbcinfo->ismbcodepage)
1287 return (unsigned char*)MSVCRT_strtok_s((char*)str, (const char*)delim, (char**)ctx);
1289 if(!str)
1290 str = *ctx;
1292 while((c=_mbsnextc(str)) && _mbschr(delim, c))
1293 str += c>255 ? 2 : 1;
1294 if(!*str)
1296 *ctx = str;
1297 return NULL;
1300 *ctx = str + (c>255 ? 2 : 1);
1301 while((c=_mbsnextc(*ctx)) && !_mbschr(delim, c))
1302 *ctx += c>255 ? 2 : 1;
1303 if (**ctx) {
1304 *(*ctx)++ = 0;
1305 if(c > 255)
1306 *(*ctx)++ = 0;
1309 return str;
1313 /*********************************************************************
1314 * _mbstok_s(MSVCRT.@)
1316 unsigned char* CDECL _mbstok_s(unsigned char *str,
1317 const unsigned char *delim, unsigned char **ctx)
1319 return _mbstok_s_l(str, delim, ctx, NULL);
1322 /*********************************************************************
1323 * _mbstok_l(MSVCRT.@)
1325 unsigned char* CDECL _mbstok_l(unsigned char *str,
1326 const unsigned char *delim, MSVCRT__locale_t locale)
1328 return _mbstok_s_l(str, delim, &msvcrt_get_thread_data()->mbstok_next, locale);
1331 /*********************************************************************
1332 * _mbstok(MSVCRT.@)
1334 unsigned char* CDECL _mbstok(unsigned char *str, const unsigned char *delim)
1336 thread_data_t *data = msvcrt_get_thread_data();
1338 #if _MSVCR_VER == 0
1339 if(!str && !data->mbstok_next)
1340 return NULL;
1341 #endif
1343 return _mbstok_s_l(str, delim, &data->mbstok_next, NULL);
1346 /*********************************************************************
1347 * _mbbtombc(MSVCRT.@)
1349 unsigned int CDECL _mbbtombc(unsigned int c)
1351 if(get_mbcinfo()->mbcodepage == 932)
1353 if(c >= 0x20 && c <= 0x7e) {
1354 if((c >= 0x41 && c <= 0x5a) || (c >= 0x61 && c <= 0x7a) || (c >= 0x30 && c <= 0x39))
1355 return mbbtombc_932[c - 0x20] | 0x8200;
1356 else
1357 return mbbtombc_932[c - 0x20] | 0x8100;
1359 else if(c >= 0xa1 && c <= 0xdf) {
1360 if(c >= 0xa6 && c <= 0xdd && c != 0xb0)
1361 return mbbtombc_932[c - 0xa1 + 0x5f] | 0x8300;
1362 else
1363 return mbbtombc_932[c - 0xa1 + 0x5f] | 0x8100;
1366 return c; /* not Japanese or no MB char */
1369 /*********************************************************************
1370 * _mbbtype(MSVCRT.@)
1372 int CDECL _mbbtype(unsigned char c, int type)
1374 if (type == 1)
1376 if ((c >= 0x20 && c <= 0x7e) || (c >= 0xa1 && c <= 0xdf))
1377 return _MBC_SINGLE;
1378 else if ((c >= 0x40 && c <= 0x7e) || (c >= 0x80 && c <= 0xfc))
1379 return _MBC_TRAIL;
1380 else
1381 return _MBC_ILLEGAL;
1383 else
1385 if ((c >= 0x20 && c <= 0x7e) || (c >= 0xa1 && c <= 0xdf))
1386 return _MBC_SINGLE;
1387 else if ((c >= 0x81 && c <= 0x9f) || (c >= 0xe0 && c <= 0xfc))
1388 return _MBC_LEAD;
1389 else
1390 return _MBC_ILLEGAL;
1394 /*********************************************************************
1395 * _ismbbkana(MSVCRT.@)
1397 int CDECL _ismbbkana(unsigned int c)
1399 /* FIXME: use lc_ctype when supported, not lc_all */
1400 if(get_mbcinfo()->mbcodepage == 932)
1402 /* Japanese/Katakana, CP 932 */
1403 return (c >= 0xa1 && c <= 0xdf);
1405 return 0;
1408 /*********************************************************************
1409 * _ismbcdigit(MSVCRT.@)
1411 int CDECL _ismbcdigit(unsigned int ch)
1413 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1414 return (get_char_typeW( wch ) & C1_DIGIT);
1417 /*********************************************************************
1418 * _ismbcgraph(MSVCRT.@)
1420 int CDECL _ismbcgraph(unsigned int ch)
1422 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1423 return (get_char_typeW( wch ) & (C1_UPPER | C1_LOWER | C1_DIGIT | C1_PUNCT | C1_ALPHA));
1426 /*********************************************************************
1427 * _ismbcalpha (MSVCRT.@)
1429 int CDECL _ismbcalpha(unsigned int ch)
1431 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1432 return (get_char_typeW( wch ) & C1_ALPHA);
1435 /*********************************************************************
1436 * _ismbclower (MSVCRT.@)
1438 int CDECL _ismbclower(unsigned int ch)
1440 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1441 return (get_char_typeW( wch ) & C1_UPPER);
1444 /*********************************************************************
1445 * _ismbcupper (MSVCRT.@)
1447 int CDECL _ismbcupper(unsigned int ch)
1449 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1450 return (get_char_typeW( wch ) & C1_LOWER);
1453 /*********************************************************************
1454 * _ismbcsymbol(MSVCRT.@)
1456 int CDECL _ismbcsymbol(unsigned int ch)
1458 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1459 WORD ctype;
1460 if (!GetStringTypeW(CT_CTYPE3, &wch, 1, &ctype))
1462 WARN("GetStringTypeW failed on %x\n", ch);
1463 return 0;
1465 return ((ctype & C3_SYMBOL) != 0);
1468 /*********************************************************************
1469 * _ismbcalnum (MSVCRT.@)
1471 int CDECL _ismbcalnum(unsigned int ch)
1473 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1474 return (get_char_typeW( wch ) & (C1_ALPHA | C1_DIGIT));
1477 /*********************************************************************
1478 * _ismbcspace (MSVCRT.@)
1480 int CDECL _ismbcspace(unsigned int ch)
1482 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1483 return (get_char_typeW( wch ) & C1_SPACE);
1486 /*********************************************************************
1487 * _ismbcprint (MSVCRT.@)
1489 int CDECL _ismbcprint(unsigned int ch)
1491 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1492 return (get_char_typeW( wch ) & (C1_UPPER | C1_LOWER | C1_DIGIT | C1_PUNCT | C1_ALPHA | C1_SPACE));
1495 /*********************************************************************
1496 * _ismbcpunct(MSVCRT.@)
1498 int CDECL _ismbcpunct(unsigned int ch)
1500 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1501 return (get_char_typeW( wch ) & C1_PUNCT);
1504 /*********************************************************************
1505 * _ismbchira(MSVCRT.@)
1507 int CDECL _ismbchira(unsigned int c)
1509 if(get_mbcinfo()->mbcodepage == 932)
1511 /* Japanese/Hiragana, CP 932 */
1512 return (c >= 0x829f && c <= 0x82f1);
1514 return 0;
1517 /*********************************************************************
1518 * _ismbckata(MSVCRT.@)
1520 int CDECL _ismbckata(unsigned int c)
1522 if(get_mbcinfo()->mbcodepage == 932)
1524 /* Japanese/Katakana, CP 932 */
1525 return (c >= 0x8340 && c <= 0x8396 && c != 0x837f);
1527 return 0;
1531 /*********************************************************************
1532 * _ismbblead_l(MSVCRT.@)
1534 int CDECL _ismbblead_l(unsigned int c, MSVCRT__locale_t locale)
1536 MSVCRT_pthreadmbcinfo mbcinfo;
1538 if(!locale)
1539 mbcinfo = get_mbcinfo();
1540 else
1541 mbcinfo = locale->mbcinfo;
1543 return (mbcinfo->mbctype[(c&0xff) + 1] & _M1) != 0;
1546 /*********************************************************************
1547 * _ismbblead(MSVCRT.@)
1549 int CDECL _ismbblead(unsigned int c)
1551 return _ismbblead_l(c, NULL);
1555 /*********************************************************************
1556 * _ismbbtrail(MSVCRT.@)
1558 int CDECL _ismbbtrail(unsigned int c)
1560 return (get_mbcinfo()->mbctype[(c&0xff) + 1] & _M2) != 0;
1563 /*********************************************************************
1564 * _ismbclegal(MSVCRT.@)
1566 int CDECL _ismbclegal(unsigned int c)
1568 return _ismbblead(HIBYTE(c)) && _ismbbtrail(LOBYTE(c));
1571 /*********************************************************************
1572 * _ismbslead(MSVCRT.@)
1574 int CDECL _ismbslead(const unsigned char* start, const unsigned char* str)
1576 int lead = 0;
1578 if(!get_mbcinfo()->ismbcodepage)
1579 return 0;
1581 /* Lead bytes can also be trail bytes so we need to analyse the string
1583 while (start <= str)
1585 if (!*start)
1586 return 0;
1587 lead = !lead && _ismbblead(*start);
1588 start++;
1591 return lead ? -1 : 0;
1594 /*********************************************************************
1595 * _ismbstrail(MSVCRT.@)
1597 int CDECL _ismbstrail(const unsigned char* start, const unsigned char* str)
1599 /* Note: this function doesn't check _ismbbtrail */
1600 if ((str > start) && _ismbslead(start, str-1))
1601 return -1;
1602 else
1603 return 0;
1606 /*********************************************************************
1607 * _mbsbtype (MSVCRT.@)
1609 int CDECL _mbsbtype(const unsigned char *str, MSVCRT_size_t count)
1611 int lead = 0;
1612 const unsigned char *end = str + count;
1614 /* Lead bytes can also be trail bytes so we need to analyse the string.
1615 * Also we must return _MBC_ILLEGAL for chars past the end of the string
1617 while (str < end) /* Note: we skip the last byte - will check after the loop */
1619 if (!*str)
1620 return _MBC_ILLEGAL;
1621 lead = get_mbcinfo()->ismbcodepage && !lead && _ismbblead(*str);
1622 str++;
1625 if (lead)
1626 if (_ismbbtrail(*str))
1627 return _MBC_TRAIL;
1628 else
1629 return _MBC_ILLEGAL;
1630 else
1631 if (_ismbblead(*str))
1632 return _MBC_LEAD;
1633 else
1634 return _MBC_SINGLE;
1637 /*********************************************************************
1638 * _mbsset(MSVCRT.@)
1640 unsigned char* CDECL _mbsset(unsigned char* str, unsigned int c)
1642 unsigned char* ret = str;
1644 if(!get_mbcinfo()->ismbcodepage || c < 256)
1645 return u__strset(str, c); /* ASCII CP or SB char */
1647 c &= 0xffff; /* Strip high bits */
1649 while(str[0] && str[1])
1651 *str++ = c >> 8;
1652 *str++ = c & 0xff;
1654 if(str[0])
1655 str[0] = '\0'; /* FIXME: OK to shorten? */
1657 return ret;
1660 /*********************************************************************
1661 * _mbsnbset(MSVCRT.@)
1663 unsigned char* CDECL _mbsnbset(unsigned char *str, unsigned int c, MSVCRT_size_t len)
1665 unsigned char *ret = str;
1667 if(!len)
1668 return ret;
1670 if(!get_mbcinfo()->ismbcodepage || c < 256)
1671 return u__strnset(str, c, len); /* ASCII CP or SB char */
1673 c &= 0xffff; /* Strip high bits */
1675 while(str[0] && str[1] && (len > 1))
1677 *str++ = c >> 8;
1678 len--;
1679 *str++ = c & 0xff;
1680 len--;
1682 if(len && str[0]) {
1683 /* as per msdn pad with a blank character */
1684 str[0] = ' ';
1687 return ret;
1690 /*********************************************************************
1691 * _mbsnset(MSVCRT.@)
1693 unsigned char* CDECL _mbsnset(unsigned char* str, unsigned int c, MSVCRT_size_t len)
1695 unsigned char *ret = str;
1697 if(!len)
1698 return ret;
1700 if(!get_mbcinfo()->ismbcodepage || c < 256)
1701 return u__strnset(str, c, len); /* ASCII CP or SB char */
1703 c &= 0xffff; /* Strip high bits */
1705 while(str[0] && str[1] && len--)
1707 *str++ = c >> 8;
1708 *str++ = c & 0xff;
1710 if(len && str[0])
1711 str[0] = '\0'; /* FIXME: OK to shorten? */
1713 return ret;
1716 /*********************************************************************
1717 * _mbsnccnt(MSVCRT.@)
1718 * 'c' is for 'character'.
1720 MSVCRT_size_t CDECL _mbsnccnt(const unsigned char* str, MSVCRT_size_t len)
1722 MSVCRT_size_t ret;
1723 if(get_mbcinfo()->ismbcodepage)
1725 ret=0;
1726 while(*str && len-- > 0)
1728 if(_ismbblead(*str))
1730 if (!len)
1731 break;
1732 len--;
1733 str++;
1735 str++;
1736 ret++;
1738 return ret;
1740 ret=u_strlen(str);
1741 return min(ret, len); /* ASCII CP */
1744 /*********************************************************************
1745 * _mbsnbcnt(MSVCRT.@)
1746 * 'b' is for byte count.
1748 MSVCRT_size_t CDECL _mbsnbcnt(const unsigned char* str, MSVCRT_size_t len)
1750 MSVCRT_size_t ret;
1751 if(get_mbcinfo()->ismbcodepage)
1753 const unsigned char* xstr = str;
1754 while(*xstr && len-- > 0)
1756 if (_ismbblead(*xstr++))
1757 xstr++;
1759 return xstr-str;
1761 ret=u_strlen(str);
1762 return min(ret, len); /* ASCII CP */
1765 /*********************************************************************
1766 * _mbsnbcat(MSVCRT.@)
1768 unsigned char* CDECL _mbsnbcat(unsigned char* dst, const unsigned char* src, MSVCRT_size_t len)
1770 if(get_mbcinfo()->ismbcodepage)
1772 unsigned char *res = dst;
1773 while (*dst) {
1774 if (_ismbblead(*dst++)) {
1775 if (*dst) {
1776 dst++;
1777 } else {
1778 /* as per msdn overwrite the lead byte in front of '\0' */
1779 dst--;
1780 break;
1784 while (*src && len--) *dst++ = *src++;
1785 *dst = '\0';
1786 return res;
1788 return u_strncat(dst, src, len); /* ASCII CP */
1791 int CDECL _mbsnbcat_s(unsigned char *dst, MSVCRT_size_t size, const unsigned char *src, MSVCRT_size_t len)
1793 unsigned char *ptr = dst;
1794 MSVCRT_size_t i;
1796 if (!dst && !size && !src && !len)
1797 return 0;
1799 if (!dst || !size || !src)
1801 if (dst && size)
1802 *dst = '\0';
1804 *MSVCRT__errno() = MSVCRT_EINVAL;
1805 return MSVCRT_EINVAL;
1808 /* Find the null terminator of the destination buffer. */
1809 while (size && *ptr)
1810 size--, ptr++;
1812 if (!size)
1814 *dst = '\0';
1815 *MSVCRT__errno() = MSVCRT_EINVAL;
1816 return MSVCRT_EINVAL;
1819 /* If necessary, check that the character preceding the null terminator is
1820 * a lead byte and move the pointer back by one for later overwrite. */
1821 if (ptr != dst && get_mbcinfo()->ismbcodepage && _ismbblead(*(ptr - 1)))
1822 size++, ptr--;
1824 for (i = 0; *src && i < len; i++)
1826 *ptr++ = *src++;
1827 size--;
1829 if (!size)
1831 *dst = '\0';
1832 *MSVCRT__errno() = MSVCRT_ERANGE;
1833 return MSVCRT_ERANGE;
1837 *ptr = '\0';
1838 return 0;
1841 /*********************************************************************
1842 * _mbsncat(MSVCRT.@)
1844 unsigned char* CDECL _mbsncat(unsigned char* dst, const unsigned char* src, MSVCRT_size_t len)
1846 if(get_mbcinfo()->ismbcodepage)
1848 unsigned char *res = dst;
1849 while (*dst)
1851 if (_ismbblead(*dst++))
1852 dst++;
1854 while (*src && len--)
1856 *dst++ = *src;
1857 if(_ismbblead(*src++))
1858 *dst++ = *src++;
1860 *dst = '\0';
1861 return res;
1863 return u_strncat(dst, src, len); /* ASCII CP */
1867 /*********************************************************************
1868 * _mbslwr(MSVCRT.@)
1870 unsigned char* CDECL _mbslwr(unsigned char* s)
1872 unsigned char *ret = s;
1873 if (!s)
1874 return NULL;
1875 if (get_mbcinfo()->ismbcodepage)
1877 unsigned int c;
1878 while (*s)
1880 c = _mbctolower(_mbsnextc(s));
1881 /* Note that I assume that the size of the character is unchanged */
1882 if (c > 255)
1884 *s++=(c>>8);
1885 c=c & 0xff;
1887 *s++=c;
1890 else for ( ; *s; s++) *s = tolower(*s);
1891 return ret;
1895 /*********************************************************************
1896 * _mbslwr_s(MSVCRT.@)
1898 int CDECL _mbslwr_s(unsigned char* s, MSVCRT_size_t len)
1900 if (!s && !len)
1902 return 0;
1904 else if (!s || !len)
1906 *MSVCRT__errno() = MSVCRT_EINVAL;
1907 return MSVCRT_EINVAL;
1909 if (get_mbcinfo()->ismbcodepage)
1911 unsigned int c;
1912 for ( ; *s && len > 0; len--)
1914 c = _mbctolower(_mbsnextc(s));
1915 /* Note that I assume that the size of the character is unchanged */
1916 if (c > 255)
1918 *s++=(c>>8);
1919 c=c & 0xff;
1921 *s++=c;
1924 else for ( ; *s && len > 0; s++, len--) *s = tolower(*s);
1925 if (*s)
1927 *s = '\0';
1928 *MSVCRT__errno() = MSVCRT_EINVAL;
1929 return MSVCRT_EINVAL;
1931 return 0;
1935 /*********************************************************************
1936 * _mbsupr(MSVCRT.@)
1938 unsigned char* CDECL _mbsupr(unsigned char* s)
1940 unsigned char *ret = s;
1941 if (!s)
1942 return NULL;
1943 if (get_mbcinfo()->ismbcodepage)
1945 unsigned int c;
1946 while (*s)
1948 c = _mbctoupper(_mbsnextc(s));
1949 /* Note that I assume that the size of the character is unchanged */
1950 if (c > 255)
1952 *s++=(c>>8);
1953 c=c & 0xff;
1955 *s++=c;
1958 else for ( ; *s; s++) *s = toupper(*s);
1959 return ret;
1963 /*********************************************************************
1964 * _mbsupr_s(MSVCRT.@)
1966 int CDECL _mbsupr_s(unsigned char* s, MSVCRT_size_t len)
1968 if (!s && !len)
1970 return 0;
1972 else if (!s || !len)
1974 *MSVCRT__errno() = MSVCRT_EINVAL;
1975 return MSVCRT_EINVAL;
1977 if (get_mbcinfo()->ismbcodepage)
1979 unsigned int c;
1980 for ( ; *s && len > 0; len--)
1982 c = _mbctoupper(_mbsnextc(s));
1983 /* Note that I assume that the size of the character is unchanged */
1984 if (c > 255)
1986 *s++=(c>>8);
1987 c=c & 0xff;
1989 *s++=c;
1992 else for ( ; *s && len > 0; s++, len--) *s = toupper(*s);
1993 if (*s)
1995 *s = '\0';
1996 *MSVCRT__errno() = MSVCRT_EINVAL;
1997 return MSVCRT_EINVAL;
1999 return 0;
2003 /*********************************************************************
2004 * _mbsspn (MSVCRT.@)
2006 MSVCRT_size_t CDECL _mbsspn(const unsigned char* string, const unsigned char* set)
2008 const unsigned char *p, *q;
2010 for (p = string; *p; p++)
2012 if (_ismbblead(*p))
2014 for (q = set; *q; q++)
2016 if (!q[1])
2017 break;
2018 if ((*p == *q) && (p[1] == q[1]))
2019 break;
2020 q++;
2022 if (!q[0] || !q[1]) break;
2024 else
2026 for (q = set; *q; q++)
2027 if (*p == *q)
2028 break;
2029 if (!*q) break;
2032 return p - string;
2035 /*********************************************************************
2036 * _mbsspnp (MSVCRT.@)
2038 unsigned char* CDECL _mbsspnp(const unsigned char* string, const unsigned char* set)
2040 const unsigned char *p, *q;
2042 for (p = string; *p; p++)
2044 if (_ismbblead(*p))
2046 for (q = set; *q; q++)
2048 if (!q[1])
2049 break;
2050 if ((*p == *q) && (p[1] == q[1]))
2051 break;
2052 q++;
2054 if (!q[0] || !q[1]) break;
2056 else
2058 for (q = set; *q; q++)
2059 if (*p == *q)
2060 break;
2061 if (!*q) break;
2064 if (*p == '\0')
2065 return NULL;
2066 return (unsigned char *)p;
2069 /*********************************************************************
2070 * _mbscspn(MSVCRT.@)
2072 MSVCRT_size_t CDECL _mbscspn(const unsigned char* str, const unsigned char* cmp)
2074 if (get_mbcinfo()->ismbcodepage)
2075 FIXME("don't handle double character case\n");
2076 return u_strcspn(str, cmp);
2079 /*********************************************************************
2080 * _mbsrev (MSVCRT.@)
2082 unsigned char* CDECL _mbsrev(unsigned char* str)
2084 int i, len = _mbslen(str);
2085 unsigned char *p, *temp=MSVCRT_malloc(len*2);
2087 if(!temp)
2088 return str;
2090 /* unpack multibyte string to temp buffer */
2091 p=str;
2092 for(i=0; i<len; i++)
2094 if (_ismbblead(*p))
2096 temp[i*2]=*p++;
2097 temp[i*2+1]=*p++;
2099 else
2101 temp[i*2]=*p++;
2102 temp[i*2+1]=0;
2106 /* repack it in the reverse order */
2107 p=str;
2108 for(i=len-1; i>=0; i--)
2110 if(_ismbblead(temp[i*2]))
2112 *p++=temp[i*2];
2113 *p++=temp[i*2+1];
2115 else
2117 *p++=temp[i*2];
2121 MSVCRT_free(temp);
2123 return str;
2126 /*********************************************************************
2127 * _mbspbrk (MSVCRT.@)
2129 unsigned char* CDECL _mbspbrk(const unsigned char* str, const unsigned char* accept)
2131 const unsigned char* p;
2133 while(*str)
2135 for(p = accept; *p; p += (_ismbblead(*p)?2:1) )
2137 if (*p == *str)
2138 if( !_ismbblead(*p) || ( *(p+1) == *(str+1) ) )
2139 return (unsigned char*)str;
2141 str += (_ismbblead(*str)?2:1);
2143 return NULL;
2148 * Functions depending on locale codepage
2151 /*********************************************************************
2152 * mblen(MSVCRT.@)
2153 * REMARKS
2154 * Unlike most of the multibyte string functions this function uses
2155 * the locale codepage, not the codepage set by _setmbcp
2157 int CDECL MSVCRT_mblen(const char* str, MSVCRT_size_t size)
2159 if (str && *str && size)
2161 if(get_locinfo()->mb_cur_max == 1)
2162 return 1; /* ASCII CP */
2164 return !MSVCRT_isleadbyte((unsigned char)*str) ? 1 : (size>1 ? 2 : -1);
2166 return 0;
2169 /*********************************************************************
2170 * mbrlen(MSVCRT.@)
2172 MSVCRT_size_t CDECL MSVCRT_mbrlen(const char *str, MSVCRT_size_t len, MSVCRT_mbstate_t *state)
2174 MSVCRT_mbstate_t s = (state ? *state : 0);
2175 MSVCRT_size_t ret;
2177 if(!len || !str || !*str)
2178 return 0;
2180 if(get_locinfo()->mb_cur_max == 1) {
2181 return 1;
2182 }else if(!s && MSVCRT_isleadbyte((unsigned char)*str)) {
2183 if(len == 1) {
2184 s = (unsigned char)*str;
2185 ret = -2;
2186 }else {
2187 ret = 2;
2189 }else if(!s) {
2190 ret = 1;
2191 }else {
2192 s = 0;
2193 ret = 2;
2196 if(state)
2197 *state = s;
2198 return ret;
2201 /*********************************************************************
2202 * _mbstrlen_l(MSVCRT.@)
2204 MSVCRT_size_t CDECL _mbstrlen_l(const char* str, MSVCRT__locale_t locale)
2206 MSVCRT_pthreadlocinfo locinfo;
2208 if(!locale)
2209 locinfo = get_locinfo();
2210 else
2211 locinfo = locale->locinfo;
2213 if(locinfo->mb_cur_max > 1) {
2214 MSVCRT_size_t len;
2215 len = MultiByteToWideChar(locinfo->lc_codepage, MB_ERR_INVALID_CHARS,
2216 str, -1, NULL, 0);
2217 if (!len) {
2218 *MSVCRT__errno() = MSVCRT_EILSEQ;
2219 return -1;
2221 return len - 1;
2224 return strlen(str);
2227 /*********************************************************************
2228 * _mbstrlen(MSVCRT.@)
2230 MSVCRT_size_t CDECL _mbstrlen(const char* str)
2232 return _mbstrlen_l(str, NULL);
2235 /*********************************************************************
2236 * _mbtowc_l(MSVCRT.@)
2238 int CDECL MSVCRT_mbtowc_l(MSVCRT_wchar_t *dst, const char* str, MSVCRT_size_t n, MSVCRT__locale_t locale)
2240 MSVCRT_pthreadlocinfo locinfo;
2241 MSVCRT_wchar_t tmpdst = '\0';
2243 if(!locale)
2244 locinfo = get_locinfo();
2245 else
2246 locinfo = locale->locinfo;
2248 if(n <= 0 || !str)
2249 return 0;
2251 if(!*str) {
2252 if(dst) *dst = 0;
2253 return 0;
2256 if(!locinfo->lc_codepage) {
2257 if(dst) *dst = (unsigned char)*str;
2258 return 1;
2260 if(n>=2 && MSVCRT__isleadbyte_l((unsigned char)*str, locale)) {
2261 if(!MultiByteToWideChar(locinfo->lc_codepage, 0, str, 2, &tmpdst, 1))
2262 return -1;
2263 if(dst) *dst = tmpdst;
2264 return 2;
2266 if(!MultiByteToWideChar(locinfo->lc_codepage, 0, str, 1, &tmpdst, 1))
2267 return -1;
2268 if(dst) *dst = tmpdst;
2269 return 1;
2272 /*********************************************************************
2273 * mbtowc(MSVCRT.@)
2275 int CDECL MSVCRT_mbtowc(MSVCRT_wchar_t *dst, const char* str, MSVCRT_size_t n)
2277 return MSVCRT_mbtowc_l(dst, str, n, NULL);
2280 /*********************************************************************
2281 * mbrtowc(MSVCRT.@)
2283 MSVCRT_size_t CDECL MSVCRT_mbrtowc(MSVCRT_wchar_t *dst, const char *str,
2284 MSVCRT_size_t n, MSVCRT_mbstate_t *state)
2286 MSVCRT_pthreadlocinfo locinfo = get_locinfo();
2287 MSVCRT_mbstate_t s = (state ? *state : 0);
2288 char tmpstr[2];
2289 int len = 0;
2291 if(dst)
2292 *dst = 0;
2294 if(!n || !str || !*str)
2295 return 0;
2297 if(locinfo->mb_cur_max == 1) {
2298 tmpstr[len++] = *str;
2299 }else if(!s && MSVCRT_isleadbyte((unsigned char)*str)) {
2300 if(n == 1) {
2301 s = (unsigned char)*str;
2302 len = -2;
2303 }else {
2304 tmpstr[0] = str[0];
2305 tmpstr[1] = str[1];
2306 len = 2;
2308 }else if(!s) {
2309 tmpstr[len++] = *str;
2310 }else {
2311 tmpstr[0] = s;
2312 tmpstr[1] = *str;
2313 len = 2;
2314 s = 0;
2317 if(len > 0) {
2318 if(!MultiByteToWideChar(locinfo->lc_codepage, 0, tmpstr, len, dst, dst ? 1 : 0))
2319 len = -1;
2322 if(state)
2323 *state = s;
2324 return len;
2327 /*********************************************************************
2328 * _mbstowcs_l(MSVCRT.@)
2330 MSVCRT_size_t CDECL MSVCRT__mbstowcs_l(MSVCRT_wchar_t *wcstr, const char *mbstr,
2331 MSVCRT_size_t count, MSVCRT__locale_t locale)
2333 MSVCRT_pthreadlocinfo locinfo;
2334 MSVCRT_size_t i, size;
2336 if(!mbstr) {
2337 *MSVCRT__errno() = MSVCRT_EINVAL;
2338 return -1;
2341 if(!locale)
2342 locinfo = get_locinfo();
2343 else
2344 locinfo = locale->locinfo;
2346 if(!locinfo->lc_codepage) {
2347 if(!wcstr)
2348 return strlen(mbstr);
2350 for(i=0; i<count; i++) {
2351 wcstr[i] = (unsigned char)mbstr[i];
2352 if(!wcstr[i]) break;
2354 return i;
2357 /* Ignore count parameter */
2358 if(!wcstr)
2359 return MultiByteToWideChar(locinfo->lc_codepage, 0, mbstr, -1, NULL, 0)-1;
2361 for(i=0, size=0; i<count; i++) {
2362 if(mbstr[size] == '\0')
2363 break;
2365 size += (MSVCRT__isleadbyte_l((unsigned char)mbstr[size], locale) ? 2 : 1);
2368 if(size) {
2369 size = MultiByteToWideChar(locinfo->lc_codepage, 0,
2370 mbstr, size, wcstr, count);
2371 if(!size) {
2372 if(count) wcstr[0] = '\0';
2373 *MSVCRT__errno() = MSVCRT_EILSEQ;
2374 return -1;
2378 if(size<count && wcstr)
2379 wcstr[size] = '\0';
2381 return size;
2384 /*********************************************************************
2385 * mbstowcs(MSVCRT.@)
2387 MSVCRT_size_t CDECL MSVCRT_mbstowcs(MSVCRT_wchar_t *wcstr,
2388 const char *mbstr, MSVCRT_size_t count)
2390 return MSVCRT__mbstowcs_l(wcstr, mbstr, count, NULL);
2393 /*********************************************************************
2394 * _mbstowcs_s_l(MSVCRT.@)
2396 int CDECL MSVCRT__mbstowcs_s_l(MSVCRT_size_t *ret, MSVCRT_wchar_t *wcstr,
2397 MSVCRT_size_t size, const char *mbstr,
2398 MSVCRT_size_t count, MSVCRT__locale_t locale)
2400 MSVCRT_size_t conv;
2402 if(!wcstr && !size) {
2403 conv = MSVCRT__mbstowcs_l(NULL, mbstr, 0, locale);
2404 if(ret)
2405 *ret = conv+1;
2406 return 0;
2409 if (!MSVCRT_CHECK_PMT(wcstr != NULL)) return MSVCRT_EINVAL;
2410 if (!MSVCRT_CHECK_PMT(mbstr != NULL)) {
2411 if(size) wcstr[0] = '\0';
2412 return MSVCRT_EINVAL;
2415 if(count==MSVCRT__TRUNCATE || size<count)
2416 conv = size;
2417 else
2418 conv = count;
2420 conv = MSVCRT__mbstowcs_l(wcstr, mbstr, conv, locale);
2421 if(conv<size)
2422 wcstr[conv++] = '\0';
2423 else if(conv==size && (count==MSVCRT__TRUNCATE || wcstr[conv-1]=='\0'))
2424 wcstr[conv-1] = '\0';
2425 else {
2426 MSVCRT_INVALID_PMT("wcstr[size] is too small", MSVCRT_ERANGE);
2427 if(size)
2428 wcstr[0] = '\0';
2429 return MSVCRT_ERANGE;
2432 if(ret)
2433 *ret = conv;
2434 return 0;
2437 /*********************************************************************
2438 * mbstowcs_s(MSVCRT.@)
2440 int CDECL MSVCRT__mbstowcs_s(MSVCRT_size_t *ret, MSVCRT_wchar_t *wcstr,
2441 MSVCRT_size_t size, const char *mbstr, MSVCRT_size_t count)
2443 return MSVCRT__mbstowcs_s_l(ret, wcstr, size, mbstr, count, NULL);
2446 /*********************************************************************
2447 * mbsrtowcs(MSVCRT.@)
2449 MSVCRT_size_t CDECL MSVCRT_mbsrtowcs(MSVCRT_wchar_t *wcstr,
2450 const char **pmbstr, MSVCRT_size_t count, MSVCRT_mbstate_t *state)
2452 MSVCRT_mbstate_t s = (state ? *state : 0);
2453 MSVCRT_wchar_t tmpdst;
2454 MSVCRT_size_t ret = 0;
2455 const char *p;
2457 if(!MSVCRT_CHECK_PMT(pmbstr != NULL))
2458 return -1;
2460 p = *pmbstr;
2461 while(!wcstr || count>ret) {
2462 int ch_len = MSVCRT_mbrtowc(&tmpdst, p, 2, &s);
2463 if(wcstr)
2464 wcstr[ret] = tmpdst;
2466 if(ch_len < 0) {
2467 return -1;
2468 }else if(ch_len == 0) {
2469 if(wcstr) *pmbstr = NULL;
2470 return ret;
2473 p += ch_len;
2474 ret++;
2477 if(wcstr) *pmbstr = p;
2478 return ret;
2481 /*********************************************************************
2482 * mbsrtowcs_s(MSVCRT.@)
2484 int CDECL MSVCRT_mbsrtowcs_s(MSVCRT_size_t *ret, MSVCRT_wchar_t *wcstr, MSVCRT_size_t len,
2485 const char **mbstr, MSVCRT_size_t count, MSVCRT_mbstate_t *state)
2487 MSVCRT_size_t tmp;
2489 if(!ret) ret = &tmp;
2490 if(!MSVCRT_CHECK_PMT(!!wcstr == !!len)) {
2491 *ret = -1;
2492 return MSVCRT_EINVAL;
2495 *ret = MSVCRT_mbsrtowcs(wcstr, mbstr, count>len ? len : count, state);
2496 if(*ret == -1) {
2497 if(wcstr) *wcstr = 0;
2498 return *MSVCRT__errno();
2500 (*ret)++;
2501 if(*ret > len) {
2502 /* no place for terminating '\0' */
2503 if(wcstr) *wcstr = 0;
2504 return 0;
2506 if(wcstr) wcstr[(*ret)-1] = 0;
2507 return 0;
2510 /*********************************************************************
2511 * _mbctohira (MSVCRT.@)
2513 * Converts a sjis katakana character to hiragana.
2515 unsigned int CDECL _mbctohira(unsigned int c)
2517 if(_ismbckata(c) && c <= 0x8393)
2518 return (c - 0x8340 - (c >= 0x837f ? 1 : 0)) + 0x829f;
2519 return c;
2522 /*********************************************************************
2523 * _mbctokata (MSVCRT.@)
2525 * Converts a sjis hiragana character to katakana.
2527 unsigned int CDECL _mbctokata(unsigned int c)
2529 if(_ismbchira(c))
2530 return (c - 0x829f) + 0x8340 + (c >= 0x82de ? 1 : 0);
2531 return c;
2535 /*********************************************************************
2536 * _ismbcl0 (MSVCRT.@)
2538 int CDECL _ismbcl0(unsigned int c)
2540 if(get_mbcinfo()->mbcodepage == 932)
2542 /* JIS non-Kanji */
2543 return (c >= 0x8140 && c <= 0x889e);
2545 return 0;