wineandroid: Check that Android supports the format in IsFormatSupported.
[wine.git] / dlls / msvcrt / mbcs.c
blob6e46b84335bade7c7a2468ab677783f7a3d238cb
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 /*********************************************************************
189 * ___mb_cur_max_l_func (MSVCR80.@)
191 int* CDECL ___mb_cur_max_l_func(MSVCRT__locale_t locale)
193 MSVCRT_pthreadlocinfo locinfo;
195 if(!locale)
196 locinfo = get_locinfo();
197 else
198 locinfo = locale->locinfo;
200 return &locinfo->mb_cur_max;
203 /*********************************************************************
204 * INTERNAL: _setmbcp_l
206 int _setmbcp_l(int cp, LCID lcid, MSVCRT_pthreadmbcinfo mbcinfo)
208 const char format[] = ".%d";
210 int newcp;
211 CPINFO cpi;
212 BYTE *bytes;
213 WORD chartypes[256];
214 char bufA[256];
215 WCHAR bufW[256];
216 int charcount;
217 int ret;
218 int i;
220 if(!mbcinfo)
221 mbcinfo = get_mbcinfo();
223 switch (cp)
225 case _MB_CP_ANSI:
226 newcp = GetACP();
227 break;
228 case _MB_CP_OEM:
229 newcp = GetOEMCP();
230 break;
231 case _MB_CP_LOCALE:
232 newcp = get_locinfo()->lc_codepage;
233 if(newcp)
234 break;
235 /* fall through (C locale) */
236 case _MB_CP_SBCS:
237 newcp = 20127; /* ASCII */
238 break;
239 default:
240 newcp = cp;
241 break;
244 if(lcid == -1) {
245 sprintf(bufA, format, newcp);
246 mbcinfo->mblcid = MSVCRT_locale_to_LCID(bufA, NULL);
247 } else {
248 mbcinfo->mblcid = lcid;
251 if(mbcinfo->mblcid == -1)
253 WARN("Can't assign LCID to codepage (%d)\n", mbcinfo->mblcid);
254 mbcinfo->mblcid = 0;
257 if (!GetCPInfo(newcp, &cpi))
259 WARN("Codepage %d not found\n", newcp);
260 *MSVCRT__errno() = MSVCRT_EINVAL;
261 return -1;
264 /* setup the _mbctype */
265 memset(mbcinfo->mbctype, 0, sizeof(unsigned char[257]));
266 memset(mbcinfo->mbcasemap, 0, sizeof(unsigned char[256]));
268 bytes = cpi.LeadByte;
269 while (bytes[0] || bytes[1])
271 for (i = bytes[0]; i <= bytes[1]; i++)
272 mbcinfo->mbctype[i + 1] |= _M1;
273 bytes += 2;
276 if (cpi.MaxCharSize > 1)
278 /* trail bytes not available through kernel32 but stored in a structure in msvcrt */
279 struct cp_extra_info_t *cpextra = g_cpextrainfo;
281 mbcinfo->ismbcodepage = 1;
282 while (TRUE)
284 if (cpextra->cp == 0 || cpextra->cp == newcp)
286 if (cpextra->cp == 0)
287 FIXME("trail bytes data not available for DBCS codepage %d - assuming all bytes\n", newcp);
289 bytes = cpextra->TrailBytes;
290 while (bytes[0] || bytes[1])
292 for (i = bytes[0]; i <= bytes[1]; i++)
293 mbcinfo->mbctype[i + 1] |= _M2;
294 bytes += 2;
296 break;
298 cpextra++;
301 else
302 mbcinfo->ismbcodepage = 0;
304 /* we can't use GetStringTypeA directly because we don't have a locale - only a code page
306 charcount = 0;
307 for (i = 0; i < 256; i++)
308 if (!(mbcinfo->mbctype[i + 1] & _M1))
309 bufA[charcount++] = i;
311 ret = MultiByteToWideChar(newcp, 0, bufA, charcount, bufW, charcount);
312 if (ret != charcount)
313 ERR("MultiByteToWideChar of chars failed for cp %d, ret=%d (exp %d), error=%d\n", newcp, ret, charcount, GetLastError());
315 GetStringTypeW(CT_CTYPE1, bufW, charcount, chartypes);
317 charcount = 0;
318 for (i = 0; i < 256; i++)
319 if (!(mbcinfo->mbctype[i + 1] & _M1))
321 if (chartypes[charcount] & C1_UPPER)
323 mbcinfo->mbctype[i + 1] |= _SBUP;
324 bufW[charcount] = tolowerW(bufW[charcount]);
326 else if (chartypes[charcount] & C1_LOWER)
328 mbcinfo->mbctype[i + 1] |= _SBLOW;
329 bufW[charcount] = toupperW(bufW[charcount]);
331 charcount++;
334 ret = WideCharToMultiByte(newcp, 0, bufW, charcount, bufA, charcount, NULL, NULL);
335 if (ret != charcount)
336 ERR("WideCharToMultiByte failed for cp %d, ret=%d (exp %d), error=%d\n", newcp, ret, charcount, GetLastError());
338 charcount = 0;
339 for (i = 0; i < 256; i++)
341 if(!(mbcinfo->mbctype[i + 1] & _M1))
343 if(mbcinfo->mbctype[i] & (C1_UPPER|C1_LOWER))
344 mbcinfo->mbcasemap[i] = bufA[charcount];
345 charcount++;
349 if (newcp == 932) /* CP932 only - set _MP and _MS */
351 /* On Windows it's possible to calculate the _MP and _MS from CT_CTYPE1
352 * and CT_CTYPE3. But as of Wine 0.9.43 we return wrong values what makes
353 * it hard. As this is set only for codepage 932 we hardcode it what gives
354 * also faster execution.
356 for (i = 161; i <= 165; i++)
357 mbcinfo->mbctype[i + 1] |= _MP;
358 for (i = 166; i <= 223; i++)
359 mbcinfo->mbctype[i + 1] |= _MS;
362 mbcinfo->mbcodepage = newcp;
363 if(MSVCRT_locale && mbcinfo == MSVCRT_locale->mbcinfo)
364 memcpy(MSVCRT_mbctype, MSVCRT_locale->mbcinfo->mbctype, sizeof(MSVCRT_mbctype));
366 return 0;
369 /*********************************************************************
370 * _setmbcp (MSVCRT.@)
372 int CDECL _setmbcp(int cp)
374 return _setmbcp_l(cp, -1, NULL);
377 /*********************************************************************
378 * _getmbcp (MSVCRT.@)
380 int CDECL _getmbcp(void)
382 return get_mbcinfo()->mbcodepage;
385 /*********************************************************************
386 * _mbsnextc(MSVCRT.@)
388 unsigned int CDECL _mbsnextc(const unsigned char* str)
390 if(_ismbblead(*str))
391 return *str << 8 | str[1];
392 return *str;
395 /*********************************************************************
396 * _mbctolower(MSVCRT.@)
398 unsigned int CDECL _mbctolower(unsigned int c)
400 if (_ismbblead(c))
402 FIXME("Handle MBC chars\n");
403 return c;
405 return tolower(c); /* ASCII CP or SB char */
408 /*********************************************************************
409 * _mbctoupper(MSVCRT.@)
411 unsigned int CDECL _mbctoupper(unsigned int c)
413 if (_ismbblead(c))
415 FIXME("Handle MBC chars\n");
416 return c;
418 return toupper(c); /* ASCII CP or SB char */
421 /*********************************************************************
422 * _mbctombb (MSVCRT.@)
424 unsigned int CDECL _mbctombb(unsigned int c)
426 unsigned int value;
428 if(get_mbcinfo()->mbcodepage == 932)
430 if(c >= 0x829f && c <= 0x82f1) /* Hiragana */
431 return mbctombb_932_kana[c - 0x829f];
432 if(c >= 0x8340 && c <= 0x8396 && c != 0x837f) /* Katakana */
433 return mbctombb_932_kana[c - 0x8340 - (c >= 0x837f ? 1 : 0)];
434 if(c >= 0x8140 && c <= 0x8197) /* Punctuation */
436 value = mbctombb_932_punct[c - 0x8140];
437 return value ? value : c;
439 if((c >= 0x824f && c <= 0x8258) || /* Fullwidth digits */
440 (c >= 0x8260 && c <= 0x8279)) /* Fullwidth capitals letters */
441 return c - 0x821f;
442 if(c >= 0x8281 && c <= 0x829a) /* Fullwidth small letters */
443 return c - 0x8220;
444 /* all other cases return c */
446 return c;
449 /*********************************************************************
450 * _mbcjistojms(MSVCRT.@)
452 * Converts a jis character to sjis.
453 * Based on description from
454 * http://www.slayers.ne.jp/~oouchi/code/jistosjis.html
456 unsigned int CDECL _mbcjistojms(unsigned int c)
458 /* Conversion takes place only when codepage is 932.
459 In all other cases, c is returned unchanged */
460 if(get_mbcinfo()->mbcodepage == 932)
462 if(HIBYTE(c) >= 0x21 && HIBYTE(c) <= 0x7e &&
463 LOBYTE(c) >= 0x21 && LOBYTE(c) <= 0x7e)
465 if(HIBYTE(c) % 2)
466 c += 0x1f;
467 else
468 c += 0x7d;
470 if(LOBYTE(c) >= 0x7F)
471 c += 0x1;
473 c = (((HIBYTE(c) - 0x21)/2 + 0x81) << 8) | LOBYTE(c);
475 if(HIBYTE(c) > 0x9f)
476 c += 0x4000;
478 else
479 return 0; /* Codepage is 932, but c can't be converted */
482 return c;
485 /*********************************************************************
486 * _mbcjmstojis(MSVCRT.@)
488 * Converts a sjis character to jis.
490 unsigned int CDECL _mbcjmstojis(unsigned int c)
492 /* Conversion takes place only when codepage is 932.
493 In all other cases, c is returned unchanged */
494 if(get_mbcinfo()->mbcodepage == 932)
496 if(_ismbclegal(c) && HIBYTE(c) < 0xf0)
498 if(HIBYTE(c) >= 0xe0)
499 c -= 0x4000;
501 c = (((HIBYTE(c) - 0x81)*2 + 0x21) << 8) | LOBYTE(c);
503 if(LOBYTE(c) > 0x7f)
504 c -= 0x1;
506 if(LOBYTE(c) > 0x9d)
507 c += 0x83;
508 else
509 c -= 0x1f;
511 else
512 return 0; /* Codepage is 932, but c can't be converted */
515 return c;
518 /*********************************************************************
519 * _mbsdec(MSVCRT.@)
521 unsigned char* CDECL _mbsdec(const unsigned char* start, const unsigned char* cur)
523 if(start >= cur)
524 return NULL;
526 if(get_mbcinfo()->ismbcodepage)
527 return (unsigned char *)(_ismbstrail(start,cur-1) ? cur - 2 : cur -1);
529 return (unsigned char *)cur - 1; /* ASCII CP or SB char */
532 /*********************************************************************
533 * _mbclen(MSVCRT.@)
535 unsigned int CDECL _mbclen(const unsigned char* str)
537 return _ismbblead(*str) ? 2 : 1;
540 /*********************************************************************
541 * _mbsinc(MSVCRT.@)
543 unsigned char* CDECL _mbsinc(const unsigned char* str)
545 return (unsigned char *)(str + _mbclen(str));
548 /*********************************************************************
549 * _mbsninc(MSVCRT.@)
551 unsigned char* CDECL _mbsninc(const unsigned char* str, MSVCRT_size_t num)
553 if(!str)
554 return NULL;
556 while (num > 0 && *str)
558 if (_ismbblead(*str))
560 if (!*(str+1))
561 break;
562 str++;
564 str++;
565 num--;
568 return (unsigned char*)str;
571 /*********************************************************************
572 * _mbsnlen_l(MSVCRT.@)
574 MSVCRT_size_t CDECL _mbsnlen_l(const unsigned char *str,
575 MSVCRT_size_t maxsize, MSVCRT__locale_t locale)
577 MSVCRT_pthreadmbcinfo mbcinfo;
578 MSVCRT_size_t i = 0, len = 0;
580 if(!locale)
581 mbcinfo = get_mbcinfo();
582 else
583 mbcinfo = locale->mbcinfo;
585 if(!mbcinfo->ismbcodepage)
586 return MSVCRT_strnlen((const char*)str, maxsize);
588 while(i<maxsize && str[i])
590 if (_ismbblead_l(str[i], locale))
592 i++;
593 if (!str[i]) /* count only full chars */
594 break;
596 i++;
597 len++;
599 return i < maxsize ? len : maxsize;
602 /*********************************************************************
603 * _mbslen(MSVCRT.@)
605 MSVCRT_size_t CDECL _mbslen(const unsigned char* str)
607 return _mbsnlen_l(str, -1, NULL);
610 /*********************************************************************
611 * _mbslen_l(MSVCRT.@)
613 MSVCRT_size_t CDECL _mbslen_l(const unsigned char* str, MSVCRT__locale_t locale)
615 return _mbsnlen_l(str, -1, locale);
618 /*********************************************************************
619 * _mbsnlen(MSVCRT.@)
621 MSVCRT_size_t CDECL _mbsnlen(const unsigned char* str, MSVCRT_size_t maxsize)
623 return _mbsnlen_l(str, maxsize, NULL);
626 /*********************************************************************
627 * _mbccpy_s_l(MSVCRT.@)
629 int CDECL _mbccpy_s_l(unsigned char* dest, MSVCRT_size_t maxsize,
630 int *copied, const unsigned char* src, MSVCRT__locale_t locale)
632 if(copied) *copied = 0;
633 if(!MSVCRT_CHECK_PMT(dest != NULL && maxsize >= 1)) return MSVCRT_EINVAL;
634 dest[0] = 0;
635 if(!MSVCRT_CHECK_PMT(src != NULL)) return MSVCRT_EINVAL;
637 if(_ismbblead_l(*src, locale)) {
638 if(!src[1]) {
639 if(copied) *copied = 1;
640 *MSVCRT__errno() = MSVCRT_EILSEQ;
641 return MSVCRT_EILSEQ;
644 if(maxsize < 2) {
645 MSVCRT_INVALID_PMT("dst buffer is too small", MSVCRT_ERANGE);
646 return MSVCRT_ERANGE;
649 *dest++ = *src++;
650 *dest = *src;
651 if(copied) *copied = 2;
652 }else {
653 *dest = *src;
654 if(copied) *copied = 1;
657 return 0;
660 /*********************************************************************
661 * _mbccpy(MSVCRT.@)
663 void CDECL _mbccpy(unsigned char* dest, const unsigned char* src)
665 _mbccpy_s_l(dest, 2, NULL, src, NULL);
668 /*********************************************************************
669 * _mbccpy_l(MSVCRT.@)
671 void CDECL _mbccpy_l(unsigned char* dest, const unsigned char* src,
672 MSVCRT__locale_t locale)
674 _mbccpy_s_l(dest, 2, NULL, src, locale);
677 /*********************************************************************
678 * _mbccpy_s(MSVCRT.@)
680 int CDECL _mbccpy_s(unsigned char* dest, MSVCRT_size_t maxsize,
681 int *copied, const unsigned char* src)
683 return _mbccpy_s_l(dest, maxsize, copied, src, NULL);
686 /*********************************************************************
687 * _mbsncpy(MSVCRT.@)
688 * REMARKS
689 * The parameter n is the number or characters to copy, not the size of
690 * the buffer. Use _mbsnbcpy for a function analogical to strncpy
692 unsigned char* CDECL _mbsncpy(unsigned char* dst, const unsigned char* src, MSVCRT_size_t n)
694 unsigned char* ret = dst;
695 if(!n)
696 return dst;
697 if (get_mbcinfo()->ismbcodepage)
699 while (*src && n)
701 n--;
702 if (_ismbblead(*src))
704 if (!*(src+1))
706 *dst++ = 0;
707 *dst++ = 0;
708 break;
711 *dst++ = *src++;
714 *dst++ = *src++;
717 else
719 while (n)
721 n--;
722 if (!(*dst++ = *src++)) break;
725 while (n--) *dst++ = 0;
726 return ret;
729 /*********************************************************************
730 * _mbsnbcpy_s_l(MSVCRT.@)
731 * REMARKS
732 * Unlike _mbsnbcpy this function does not pad the rest of the dest
733 * string with 0
735 int CDECL _mbsnbcpy_s_l(unsigned char* dst, MSVCRT_size_t size,
736 const unsigned char* src, MSVCRT_size_t n, MSVCRT__locale_t locale)
738 MSVCRT_size_t pos = 0;
740 if(!dst || size == 0)
741 return MSVCRT_EINVAL;
742 if(!src)
744 dst[0] = '\0';
745 return MSVCRT_EINVAL;
747 if(!n)
748 return 0;
750 if((locale ? locale->mbcinfo : get_mbcinfo())->ismbcodepage)
752 BOOL is_lead = FALSE;
753 while (*src && n)
755 if(pos == size)
757 dst[0] = '\0';
758 return MSVCRT_ERANGE;
760 is_lead = (!is_lead && _ismbblead(*src));
761 n--;
762 dst[pos++] = *src++;
765 if (is_lead) /* if string ends with a lead, remove it */
766 dst[pos - 1] = 0;
768 else
770 while (n)
772 n--;
773 if(pos == size)
775 dst[0] = '\0';
776 return MSVCRT_ERANGE;
779 if(!(*src)) break;
780 dst[pos++] = *src++;
784 if(pos < size)
785 dst[pos] = '\0';
786 else
788 dst[0] = '\0';
789 return MSVCRT_ERANGE;
792 return 0;
795 /*********************************************************************
796 * _mbsnbcpy_s(MSVCRT.@)
798 int CDECL _mbsnbcpy_s(unsigned char* dst, MSVCRT_size_t size, const unsigned char* src, MSVCRT_size_t n)
800 return _mbsnbcpy_s_l(dst, size, src, n, NULL);
803 /*********************************************************************
804 * _mbscpy_s_l(MSVCRT.@)
806 int CDECL _mbscpy_s_l(unsigned char *dst, MSVCRT_size_t size,
807 const unsigned char *src, MSVCRT__locale_t locale)
809 return _mbsnbcpy_s_l(dst, size, src, -1, locale);
812 /*********************************************************************
813 * _mbscpy_s(MSVCRT.@)
815 int CDECL _mbscpy_s(unsigned char *dst, MSVCRT_size_t size, const unsigned char *src)
817 return _mbscpy_s_l(dst, size, src, NULL);
820 /*********************************************************************
821 * _mbsnbcpy(MSVCRT.@)
822 * REMARKS
823 * Like strncpy this function doesn't enforce the string to be
824 * NUL-terminated
826 unsigned char* CDECL _mbsnbcpy(unsigned char* dst, const unsigned char* src, MSVCRT_size_t n)
828 unsigned char* ret = dst;
829 if(!n)
830 return dst;
831 if(get_mbcinfo()->ismbcodepage)
833 BOOL is_lead = FALSE;
834 while (*src && n)
836 is_lead = (!is_lead && _ismbblead(*src));
837 n--;
838 *dst++ = *src++;
841 if (is_lead) /* if string ends with a lead, remove it */
842 *(dst - 1) = 0;
844 else
846 while (n)
848 n--;
849 if (!(*dst++ = *src++)) break;
852 while (n--) *dst++ = 0;
853 return ret;
856 /*********************************************************************
857 * _mbscmp(MSVCRT.@)
859 int CDECL _mbscmp(const unsigned char* str, const unsigned char* cmp)
861 if (!str || !cmp) return INT_MAX;
863 if(get_mbcinfo()->ismbcodepage)
865 unsigned int strc, cmpc;
866 do {
867 if(!*str)
868 return *cmp ? -1 : 0;
869 if(!*cmp)
870 return 1;
871 strc = _mbsnextc(str);
872 cmpc = _mbsnextc(cmp);
873 if(strc != cmpc)
874 return strc < cmpc ? -1 : 1;
875 str +=(strc > 255) ? 2 : 1;
876 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
877 } while(1);
879 return u_strcmp(str, cmp); /* ASCII CP */
882 /*********************************************************************
883 * _mbsnbicoll_l(MSVCRT.@)
885 int CDECL _mbsnbicoll_l(const unsigned char *str1, const unsigned char *str2, MSVCRT_size_t len, MSVCRT__locale_t locale)
887 MSVCRT_pthreadmbcinfo mbcinfo;
889 if(!locale)
890 mbcinfo = get_mbcinfo();
891 else
892 mbcinfo = locale->mbcinfo;
894 if(!mbcinfo->ismbcodepage)
895 return MSVCRT__strnicoll_l((const char*)str1, (const char*)str2, len, locale);
896 return CompareStringA(mbcinfo->mblcid, NORM_IGNORECASE, (const char*)str1, len, (const char*)str2, len)-CSTR_EQUAL;
899 /*********************************************************************
900 * _mbsicoll_l(MSVCRT.@)
902 int CDECL _mbsicoll_l(const unsigned char *str1, const unsigned char *str2, MSVCRT__locale_t locale)
904 return _mbsnbicoll_l(str1, str2, -1, locale);
907 /*********************************************************************
908 * _mbsnbicoll(MSVCRT.@)
910 int CDECL _mbsnbicoll(const unsigned char *str1, const unsigned char *str2, MSVCRT_size_t len)
912 return _mbsnbicoll_l(str1, str2, len, NULL);
915 /*********************************************************************
916 * _mbsicoll(MSVCRT.@)
918 int CDECL _mbsicoll(const unsigned char* str, const unsigned char* cmp)
920 #if _MSVCR_VER>=60 && _MSVCR_VER<=71
921 return CompareStringA(get_mbcinfo()->mblcid, NORM_IGNORECASE,
922 (const char*)str, -1, (const char*)cmp, -1)-CSTR_EQUAL;
923 #else
924 return _mbsnbicoll_l(str, cmp, -1, NULL);
925 #endif
928 /*********************************************************************
929 * _mbsnbcoll_l(MSVCRT.@)
931 int CDECL _mbsnbcoll_l(const unsigned char *str1, const unsigned char *str2, MSVCRT_size_t len, MSVCRT__locale_t locale)
933 MSVCRT_pthreadmbcinfo mbcinfo;
935 if(!locale)
936 mbcinfo = get_mbcinfo();
937 else
938 mbcinfo = locale->mbcinfo;
940 if(!mbcinfo->ismbcodepage)
941 return MSVCRT__strncoll_l((const char*)str1, (const char*)str2, len, locale);
942 return CompareStringA(mbcinfo->mblcid, 0, (const char*)str1, len, (const char*)str2, len)-CSTR_EQUAL;
945 /*********************************************************************
946 * _mbscoll_l(MSVCRT.@)
948 int CDECL _mbscoll_l(const unsigned char *str1, const unsigned char *str2, MSVCRT__locale_t locale)
950 return _mbsnbcoll_l(str1, str2, -1, locale);
953 /*********************************************************************
954 * _mbsnbcoll(MSVCRT.@)
956 int CDECL _mbsnbcoll(const unsigned char *str1, const unsigned char *str2, MSVCRT_size_t len)
958 return _mbsnbcoll_l(str1, str2, len, NULL);
961 /*********************************************************************
962 * _mbscoll(MSVCRT.@)
964 int CDECL _mbscoll(const unsigned char* str, const unsigned char* cmp)
966 #if _MSVCR_VER>=60 && _MSVCR_VER<=71
967 return CompareStringA(get_mbcinfo()->mblcid, 0,
968 (const char*)str, -1, (const char*)cmp, -1)-CSTR_EQUAL;
969 #else
970 return _mbsnbcoll_l(str, cmp, -1, NULL);
971 #endif
974 /*********************************************************************
975 * _mbsicmp(MSVCRT.@)
977 int CDECL _mbsicmp(const unsigned char* str, const unsigned char* cmp)
979 if(get_mbcinfo()->ismbcodepage)
981 unsigned int strc, cmpc;
982 do {
983 if(!*str)
984 return *cmp ? -1 : 0;
985 if(!*cmp)
986 return 1;
987 strc = _mbctolower(_mbsnextc(str));
988 cmpc = _mbctolower(_mbsnextc(cmp));
989 if(strc != cmpc)
990 return strc < cmpc ? -1 : 1;
991 str +=(strc > 255) ? 2 : 1;
992 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
993 } while(1);
995 return u_strcasecmp(str, cmp); /* ASCII CP */
998 /*********************************************************************
999 * _mbsncmp(MSVCRT.@)
1001 int CDECL _mbsncmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
1003 if(!len)
1004 return 0;
1006 if(get_mbcinfo()->ismbcodepage)
1008 unsigned int strc, cmpc;
1009 while(len--)
1011 int inc;
1012 if(!*str)
1013 return *cmp ? -1 : 0;
1014 if(!*cmp)
1015 return 1;
1016 strc = _mbsnextc(str);
1017 cmpc = _mbsnextc(cmp);
1018 if(strc != cmpc)
1019 return strc < cmpc ? -1 : 1;
1020 inc=(strc > 255) ? 2 : 1; /* Equal, use same increment */
1021 str += inc;
1022 cmp += inc;
1024 return 0; /* Matched len chars */
1026 return u_strncmp(str, cmp, len); /* ASCII CP */
1029 /*********************************************************************
1030 * _mbsnbcmp(MSVCRT.@)
1032 int CDECL _mbsnbcmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
1034 if (!len)
1035 return 0;
1036 if(get_mbcinfo()->ismbcodepage)
1038 unsigned int strc, cmpc;
1039 while (len)
1041 int clen;
1042 if(!*str)
1043 return *cmp ? -1 : 0;
1044 if(!*cmp)
1045 return 1;
1046 if (_ismbblead(*str))
1048 strc=(len>=2)?_mbsnextc(str):0;
1049 clen=2;
1051 else
1053 strc=*str;
1054 clen=1;
1056 if (_ismbblead(*cmp))
1057 cmpc=(len>=2)?_mbsnextc(cmp):0;
1058 else
1059 cmpc=*cmp;
1060 if(strc != cmpc)
1061 return strc < cmpc ? -1 : 1;
1062 len -= clen;
1063 str += clen;
1064 cmp += clen;
1066 return 0; /* Matched len chars */
1068 return u_strncmp(str,cmp,len);
1071 /*********************************************************************
1072 * _mbsnicmp(MSVCRT.@)
1074 * Compare two multibyte strings case insensitively to 'len' characters.
1076 int CDECL _mbsnicmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
1078 /* FIXME: No tolower() for mb strings yet */
1079 if(get_mbcinfo()->ismbcodepage)
1081 unsigned int strc, cmpc;
1082 while(len--)
1084 if(!*str)
1085 return *cmp ? -1 : 0;
1086 if(!*cmp)
1087 return 1;
1088 strc = _mbctolower(_mbsnextc(str));
1089 cmpc = _mbctolower(_mbsnextc(cmp));
1090 if(strc != cmpc)
1091 return strc < cmpc ? -1 : 1;
1092 str +=(strc > 255) ? 2 : 1;
1093 cmp +=(strc > 255) ? 2 : 1; /* Equal, use same increment */
1095 return 0; /* Matched len chars */
1097 return u_strncasecmp(str, cmp, len); /* ASCII CP */
1100 /*********************************************************************
1101 * _mbsnbicmp(MSVCRT.@)
1103 int CDECL _mbsnbicmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
1105 if (!len)
1106 return 0;
1107 if(get_mbcinfo()->ismbcodepage)
1109 unsigned int strc, cmpc;
1110 while (len)
1112 int clen;
1113 if(!*str)
1114 return *cmp ? -1 : 0;
1115 if(!*cmp)
1116 return 1;
1117 if (_ismbblead(*str))
1119 strc=(len>=2)?_mbsnextc(str):0;
1120 clen=2;
1122 else
1124 strc=*str;
1125 clen=1;
1127 if (_ismbblead(*cmp))
1128 cmpc=(len>=2)?_mbsnextc(cmp):0;
1129 else
1130 cmpc=*cmp;
1131 strc = _mbctolower(strc);
1132 cmpc = _mbctolower(cmpc);
1133 if(strc != cmpc)
1134 return strc < cmpc ? -1 : 1;
1135 len -= clen;
1136 str += clen;
1137 cmp += clen;
1139 return 0; /* Matched len bytes */
1141 return u_strncasecmp(str,cmp,len);
1144 /*********************************************************************
1145 * _mbscat (MSVCRT.@)
1147 unsigned char * CDECL _mbscat( unsigned char *dst, const unsigned char *src )
1149 strcat( (char *)dst, (const char *)src );
1150 return dst;
1153 /*********************************************************************
1154 * _mbscat_s_l (MSVCRT.@)
1156 int CDECL _mbscat_s_l( unsigned char *dst, MSVCRT_size_t size,
1157 const unsigned char *src, MSVCRT__locale_t locale )
1159 MSVCRT_size_t i, j;
1160 int ret = 0;
1162 if(!MSVCRT_CHECK_PMT(dst != NULL)) return MSVCRT_EINVAL;
1163 if(!MSVCRT_CHECK_PMT(src != NULL)) return MSVCRT_EINVAL;
1165 for(i=0; i<size; i++)
1166 if(!dst[i]) break;
1167 if(i == size) {
1168 MSVCRT_INVALID_PMT("dst is not NULL-terminated", MSVCRT_EINVAL);
1169 if(size) dst[0] = 0;
1170 return MSVCRT_EINVAL;
1173 if(i && _ismbblead_l(dst[i-1], locale)) {
1174 ret = MSVCRT_EILSEQ;
1175 i--;
1178 for(j=0; src[j] && i+j<size; j++)
1179 dst[i+j] = src[j];
1180 if(i+j == size) {
1181 MSVCRT_INVALID_PMT("dst buffer is too small", MSVCRT_ERANGE);
1182 dst[0] = 0;
1183 return MSVCRT_ERANGE;
1186 if(j && _ismbblead_l(src[j-1], locale)) {
1187 ret = MSVCRT_EILSEQ;
1188 j--;
1191 dst[i+j] = 0;
1192 return ret;
1195 /*********************************************************************
1196 * _mbscat_s (MSVCRT.@)
1198 int CDECL _mbscat_s( unsigned char *dst, MSVCRT_size_t size, const unsigned char *src )
1200 return _mbscat_s_l(dst, size, src, NULL);
1203 /*********************************************************************
1204 * _mbscpy (MSVCRT.@)
1206 unsigned char* CDECL _mbscpy( unsigned char *dst, const unsigned char *src )
1208 strcpy( (char *)dst, (const char *)src );
1209 return dst;
1212 /*********************************************************************
1213 * _mbsstr (MSVCRT.@)
1215 unsigned char * CDECL _mbsstr(const unsigned char *haystack, const unsigned char *needle)
1217 return (unsigned char *)strstr( (const char *)haystack, (const char *)needle );
1220 /*********************************************************************
1221 * _mbschr(MSVCRT.@)
1223 * Find a multibyte character in a multibyte string.
1225 unsigned char* CDECL _mbschr(const unsigned char* s, unsigned int x)
1227 if(get_mbcinfo()->ismbcodepage)
1229 unsigned int c;
1230 while (1)
1232 c = _mbsnextc(s);
1233 if (c == x)
1234 return (unsigned char*)s;
1235 if (!c)
1236 return NULL;
1237 s += c > 255 ? 2 : 1;
1240 return u_strchr(s, x); /* ASCII CP */
1243 /*********************************************************************
1244 * _mbsrchr(MSVCRT.@)
1246 unsigned char* CDECL _mbsrchr(const unsigned char* s, unsigned int x)
1248 if(get_mbcinfo()->ismbcodepage)
1250 unsigned int c;
1251 unsigned char* match=NULL;
1252 if(!s)
1253 return NULL;
1254 while (1) {
1255 c = _mbsnextc(s);
1256 if (c == x)
1257 match=(unsigned char*)s;
1258 if (!c)
1259 return match;
1260 s +=(c > 255) ? 2 : 1;
1263 return u_strrchr(s, x);
1266 /*********************************************************************
1267 * _mbstok_s_l(MSVCRT.@)
1269 unsigned char* CDECL _mbstok_s_l(unsigned char *str, const unsigned char *delim,
1270 unsigned char **ctx, MSVCRT__locale_t locale)
1272 MSVCRT_pthreadmbcinfo mbcinfo;
1273 unsigned int c;
1275 if(!MSVCRT_CHECK_PMT(delim != NULL)) return NULL;
1276 if(!MSVCRT_CHECK_PMT(ctx != NULL)) return NULL;
1277 if(!MSVCRT_CHECK_PMT(str || *ctx)) return NULL;
1279 if(locale)
1280 mbcinfo = locale->mbcinfo;
1281 else
1282 mbcinfo = get_mbcinfo();
1284 if(!mbcinfo->ismbcodepage)
1285 return (unsigned char*)MSVCRT_strtok_s((char*)str, (const char*)delim, (char**)ctx);
1287 if(!str)
1288 str = *ctx;
1290 while((c=_mbsnextc(str)) && _mbschr(delim, c))
1291 str += c>255 ? 2 : 1;
1292 if(!*str)
1294 *ctx = str;
1295 return NULL;
1298 *ctx = str + (c>255 ? 2 : 1);
1299 while((c=_mbsnextc(*ctx)) && !_mbschr(delim, c))
1300 *ctx += c>255 ? 2 : 1;
1301 if (**ctx) {
1302 *(*ctx)++ = 0;
1303 if(c > 255)
1304 *(*ctx)++ = 0;
1307 return str;
1311 /*********************************************************************
1312 * _mbstok_s(MSVCRT.@)
1314 unsigned char* CDECL _mbstok_s(unsigned char *str,
1315 const unsigned char *delim, unsigned char **ctx)
1317 return _mbstok_s_l(str, delim, ctx, NULL);
1320 /*********************************************************************
1321 * _mbstok_l(MSVCRT.@)
1323 unsigned char* CDECL _mbstok_l(unsigned char *str,
1324 const unsigned char *delim, MSVCRT__locale_t locale)
1326 return _mbstok_s_l(str, delim, &msvcrt_get_thread_data()->mbstok_next, locale);
1329 /*********************************************************************
1330 * _mbstok(MSVCRT.@)
1332 unsigned char* CDECL _mbstok(unsigned char *str, const unsigned char *delim)
1334 thread_data_t *data = msvcrt_get_thread_data();
1336 #if _MSVCR_VER == 0
1337 if(!str && !data->mbstok_next)
1338 return NULL;
1339 #endif
1341 return _mbstok_s_l(str, delim, &data->mbstok_next, NULL);
1344 /*********************************************************************
1345 * _mbbtombc(MSVCRT.@)
1347 unsigned int CDECL _mbbtombc(unsigned int c)
1349 if(get_mbcinfo()->mbcodepage == 932)
1351 if(c >= 0x20 && c <= 0x7e) {
1352 if((c >= 0x41 && c <= 0x5a) || (c >= 0x61 && c <= 0x7a) || (c >= 0x30 && c <= 0x39))
1353 return mbbtombc_932[c - 0x20] | 0x8200;
1354 else
1355 return mbbtombc_932[c - 0x20] | 0x8100;
1357 else if(c >= 0xa1 && c <= 0xdf) {
1358 if(c >= 0xa6 && c <= 0xdd && c != 0xb0)
1359 return mbbtombc_932[c - 0xa1 + 0x5f] | 0x8300;
1360 else
1361 return mbbtombc_932[c - 0xa1 + 0x5f] | 0x8100;
1364 return c; /* not Japanese or no MB char */
1367 /*********************************************************************
1368 * _mbbtype(MSVCRT.@)
1370 int CDECL _mbbtype(unsigned char c, int type)
1372 if (type == 1)
1374 if ((c >= 0x20 && c <= 0x7e) || (c >= 0xa1 && c <= 0xdf))
1375 return _MBC_SINGLE;
1376 else if ((c >= 0x40 && c <= 0x7e) || (c >= 0x80 && c <= 0xfc))
1377 return _MBC_TRAIL;
1378 else
1379 return _MBC_ILLEGAL;
1381 else
1383 if ((c >= 0x20 && c <= 0x7e) || (c >= 0xa1 && c <= 0xdf))
1384 return _MBC_SINGLE;
1385 else if ((c >= 0x81 && c <= 0x9f) || (c >= 0xe0 && c <= 0xfc))
1386 return _MBC_LEAD;
1387 else
1388 return _MBC_ILLEGAL;
1392 /*********************************************************************
1393 * _ismbbkana(MSVCRT.@)
1395 int CDECL _ismbbkana(unsigned int c)
1397 /* FIXME: use lc_ctype when supported, not lc_all */
1398 if(get_mbcinfo()->mbcodepage == 932)
1400 /* Japanese/Katakana, CP 932 */
1401 return (c >= 0xa1 && c <= 0xdf);
1403 return 0;
1406 /*********************************************************************
1407 * _ismbcdigit(MSVCRT.@)
1409 int CDECL _ismbcdigit(unsigned int ch)
1411 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1412 return (get_char_typeW( wch ) & C1_DIGIT);
1415 /*********************************************************************
1416 * _ismbcgraph(MSVCRT.@)
1418 int CDECL _ismbcgraph(unsigned int ch)
1420 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1421 return (get_char_typeW( wch ) & (C1_UPPER | C1_LOWER | C1_DIGIT | C1_PUNCT | C1_ALPHA));
1424 /*********************************************************************
1425 * _ismbcalpha (MSVCRT.@)
1427 int CDECL _ismbcalpha(unsigned int ch)
1429 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1430 return (get_char_typeW( wch ) & C1_ALPHA);
1433 /*********************************************************************
1434 * _ismbclower (MSVCRT.@)
1436 int CDECL _ismbclower(unsigned int ch)
1438 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1439 return (get_char_typeW( wch ) & C1_UPPER);
1442 /*********************************************************************
1443 * _ismbcupper (MSVCRT.@)
1445 int CDECL _ismbcupper(unsigned int ch)
1447 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1448 return (get_char_typeW( wch ) & C1_LOWER);
1451 /*********************************************************************
1452 * _ismbcsymbol(MSVCRT.@)
1454 int CDECL _ismbcsymbol(unsigned int ch)
1456 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1457 WORD ctype;
1458 if (!GetStringTypeW(CT_CTYPE3, &wch, 1, &ctype))
1460 WARN("GetStringTypeW failed on %x\n", ch);
1461 return 0;
1463 return ((ctype & C3_SYMBOL) != 0);
1466 /*********************************************************************
1467 * _ismbcalnum (MSVCRT.@)
1469 int CDECL _ismbcalnum(unsigned int ch)
1471 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1472 return (get_char_typeW( wch ) & (C1_ALPHA | C1_DIGIT));
1475 /*********************************************************************
1476 * _ismbcspace (MSVCRT.@)
1478 int CDECL _ismbcspace(unsigned int ch)
1480 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1481 return (get_char_typeW( wch ) & C1_SPACE);
1484 /*********************************************************************
1485 * _ismbcprint (MSVCRT.@)
1487 int CDECL _ismbcprint(unsigned int ch)
1489 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1490 return (get_char_typeW( wch ) & (C1_UPPER | C1_LOWER | C1_DIGIT | C1_PUNCT | C1_ALPHA | C1_SPACE));
1493 /*********************************************************************
1494 * _ismbcpunct(MSVCRT.@)
1496 int CDECL _ismbcpunct(unsigned int ch)
1498 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1499 return (get_char_typeW( wch ) & C1_PUNCT);
1502 /*********************************************************************
1503 * _ismbchira(MSVCRT.@)
1505 int CDECL _ismbchira(unsigned int c)
1507 if(get_mbcinfo()->mbcodepage == 932)
1509 /* Japanese/Hiragana, CP 932 */
1510 return (c >= 0x829f && c <= 0x82f1);
1512 return 0;
1515 /*********************************************************************
1516 * _ismbckata(MSVCRT.@)
1518 int CDECL _ismbckata(unsigned int c)
1520 if(get_mbcinfo()->mbcodepage == 932)
1522 /* Japanese/Katakana, CP 932 */
1523 return (c >= 0x8340 && c <= 0x8396 && c != 0x837f);
1525 return 0;
1529 /*********************************************************************
1530 * _ismbblead_l(MSVCRT.@)
1532 int CDECL _ismbblead_l(unsigned int c, MSVCRT__locale_t locale)
1534 MSVCRT_pthreadmbcinfo mbcinfo;
1536 if(!locale)
1537 mbcinfo = get_mbcinfo();
1538 else
1539 mbcinfo = locale->mbcinfo;
1541 return (mbcinfo->mbctype[(c&0xff) + 1] & _M1) != 0;
1544 /*********************************************************************
1545 * _ismbblead(MSVCRT.@)
1547 int CDECL _ismbblead(unsigned int c)
1549 return _ismbblead_l(c, NULL);
1553 /*********************************************************************
1554 * _ismbbtrail(MSVCRT.@)
1556 int CDECL _ismbbtrail(unsigned int c)
1558 return (get_mbcinfo()->mbctype[(c&0xff) + 1] & _M2) != 0;
1561 /*********************************************************************
1562 * _ismbclegal(MSVCRT.@)
1564 int CDECL _ismbclegal(unsigned int c)
1566 return _ismbblead(HIBYTE(c)) && _ismbbtrail(LOBYTE(c));
1569 /*********************************************************************
1570 * _ismbslead(MSVCRT.@)
1572 int CDECL _ismbslead(const unsigned char* start, const unsigned char* str)
1574 int lead = 0;
1576 if(!get_mbcinfo()->ismbcodepage)
1577 return 0;
1579 /* Lead bytes can also be trail bytes so we need to analyse the string
1581 while (start <= str)
1583 if (!*start)
1584 return 0;
1585 lead = !lead && _ismbblead(*start);
1586 start++;
1589 return lead ? -1 : 0;
1592 /*********************************************************************
1593 * _ismbstrail(MSVCRT.@)
1595 int CDECL _ismbstrail(const unsigned char* start, const unsigned char* str)
1597 /* Note: this function doesn't check _ismbbtrail */
1598 if ((str > start) && _ismbslead(start, str-1))
1599 return -1;
1600 else
1601 return 0;
1604 /*********************************************************************
1605 * _mbsbtype (MSVCRT.@)
1607 int CDECL _mbsbtype(const unsigned char *str, MSVCRT_size_t count)
1609 int lead = 0;
1610 const unsigned char *end = str + count;
1612 /* Lead bytes can also be trail bytes so we need to analyse the string.
1613 * Also we must return _MBC_ILLEGAL for chars past the end of the string
1615 while (str < end) /* Note: we skip the last byte - will check after the loop */
1617 if (!*str)
1618 return _MBC_ILLEGAL;
1619 lead = get_mbcinfo()->ismbcodepage && !lead && _ismbblead(*str);
1620 str++;
1623 if (lead)
1624 if (_ismbbtrail(*str))
1625 return _MBC_TRAIL;
1626 else
1627 return _MBC_ILLEGAL;
1628 else
1629 if (_ismbblead(*str))
1630 return _MBC_LEAD;
1631 else
1632 return _MBC_SINGLE;
1635 /*********************************************************************
1636 * _mbsset(MSVCRT.@)
1638 unsigned char* CDECL _mbsset(unsigned char* str, unsigned int c)
1640 unsigned char* ret = str;
1642 if(!get_mbcinfo()->ismbcodepage || c < 256)
1643 return u__strset(str, c); /* ASCII CP or SB char */
1645 c &= 0xffff; /* Strip high bits */
1647 while(str[0] && str[1])
1649 *str++ = c >> 8;
1650 *str++ = c & 0xff;
1652 if(str[0])
1653 str[0] = '\0'; /* FIXME: OK to shorten? */
1655 return ret;
1658 /*********************************************************************
1659 * _mbsnbset(MSVCRT.@)
1661 unsigned char* CDECL _mbsnbset(unsigned char *str, unsigned int c, MSVCRT_size_t len)
1663 unsigned char *ret = str;
1665 if(!len)
1666 return ret;
1668 if(!get_mbcinfo()->ismbcodepage || c < 256)
1669 return u__strnset(str, c, len); /* ASCII CP or SB char */
1671 c &= 0xffff; /* Strip high bits */
1673 while(str[0] && str[1] && (len > 1))
1675 *str++ = c >> 8;
1676 len--;
1677 *str++ = c & 0xff;
1678 len--;
1680 if(len && str[0]) {
1681 /* as per msdn pad with a blank character */
1682 str[0] = ' ';
1685 return ret;
1688 /*********************************************************************
1689 * _mbsnset(MSVCRT.@)
1691 unsigned char* CDECL _mbsnset(unsigned char* str, unsigned int c, MSVCRT_size_t len)
1693 unsigned char *ret = str;
1695 if(!len)
1696 return ret;
1698 if(!get_mbcinfo()->ismbcodepage || c < 256)
1699 return u__strnset(str, c, len); /* ASCII CP or SB char */
1701 c &= 0xffff; /* Strip high bits */
1703 while(str[0] && str[1] && len--)
1705 *str++ = c >> 8;
1706 *str++ = c & 0xff;
1708 if(len && str[0])
1709 str[0] = '\0'; /* FIXME: OK to shorten? */
1711 return ret;
1714 /*********************************************************************
1715 * _mbsnccnt(MSVCRT.@)
1716 * 'c' is for 'character'.
1718 MSVCRT_size_t CDECL _mbsnccnt(const unsigned char* str, MSVCRT_size_t len)
1720 MSVCRT_size_t ret;
1721 if(get_mbcinfo()->ismbcodepage)
1723 ret=0;
1724 while(*str && len-- > 0)
1726 if(_ismbblead(*str))
1728 if (!len)
1729 break;
1730 len--;
1731 str++;
1733 str++;
1734 ret++;
1736 return ret;
1738 ret=u_strlen(str);
1739 return min(ret, len); /* ASCII CP */
1742 /*********************************************************************
1743 * _mbsnbcnt(MSVCRT.@)
1744 * 'b' is for byte count.
1746 MSVCRT_size_t CDECL _mbsnbcnt(const unsigned char* str, MSVCRT_size_t len)
1748 MSVCRT_size_t ret;
1749 if(get_mbcinfo()->ismbcodepage)
1751 const unsigned char* xstr = str;
1752 while(*xstr && len-- > 0)
1754 if (_ismbblead(*xstr++))
1755 xstr++;
1757 return xstr-str;
1759 ret=u_strlen(str);
1760 return min(ret, len); /* ASCII CP */
1763 /*********************************************************************
1764 * _mbsnbcat(MSVCRT.@)
1766 unsigned char* CDECL _mbsnbcat(unsigned char* dst, const unsigned char* src, MSVCRT_size_t len)
1768 if(get_mbcinfo()->ismbcodepage)
1770 unsigned char *res = dst;
1771 while (*dst) {
1772 if (_ismbblead(*dst++)) {
1773 if (*dst) {
1774 dst++;
1775 } else {
1776 /* as per msdn overwrite the lead byte in front of '\0' */
1777 dst--;
1778 break;
1782 while (*src && len--) *dst++ = *src++;
1783 *dst = '\0';
1784 return res;
1786 return u_strncat(dst, src, len); /* ASCII CP */
1789 int CDECL _mbsnbcat_s(unsigned char *dst, MSVCRT_size_t size, const unsigned char *src, MSVCRT_size_t len)
1791 unsigned char *ptr = dst;
1792 MSVCRT_size_t i;
1794 if (!dst && !size && !src && !len)
1795 return 0;
1797 if (!dst || !size || !src)
1799 if (dst && size)
1800 *dst = '\0';
1802 *MSVCRT__errno() = MSVCRT_EINVAL;
1803 return MSVCRT_EINVAL;
1806 /* Find the null terminator of the destination buffer. */
1807 while (size && *ptr)
1808 size--, ptr++;
1810 if (!size)
1812 *dst = '\0';
1813 *MSVCRT__errno() = MSVCRT_EINVAL;
1814 return MSVCRT_EINVAL;
1817 /* If necessary, check that the character preceding the null terminator is
1818 * a lead byte and move the pointer back by one for later overwrite. */
1819 if (ptr != dst && get_mbcinfo()->ismbcodepage && _ismbblead(*(ptr - 1)))
1820 size++, ptr--;
1822 for (i = 0; *src && i < len; i++)
1824 *ptr++ = *src++;
1825 size--;
1827 if (!size)
1829 *dst = '\0';
1830 *MSVCRT__errno() = MSVCRT_ERANGE;
1831 return MSVCRT_ERANGE;
1835 *ptr = '\0';
1836 return 0;
1839 /*********************************************************************
1840 * _mbsncat(MSVCRT.@)
1842 unsigned char* CDECL _mbsncat(unsigned char* dst, const unsigned char* src, MSVCRT_size_t len)
1844 if(get_mbcinfo()->ismbcodepage)
1846 unsigned char *res = dst;
1847 while (*dst)
1849 if (_ismbblead(*dst++))
1850 dst++;
1852 while (*src && len--)
1854 *dst++ = *src;
1855 if(_ismbblead(*src++))
1856 *dst++ = *src++;
1858 *dst = '\0';
1859 return res;
1861 return u_strncat(dst, src, len); /* ASCII CP */
1865 /*********************************************************************
1866 * _mbslwr(MSVCRT.@)
1868 unsigned char* CDECL _mbslwr(unsigned char* s)
1870 unsigned char *ret = s;
1871 if (!s)
1872 return NULL;
1873 if (get_mbcinfo()->ismbcodepage)
1875 unsigned int c;
1876 while (*s)
1878 c = _mbctolower(_mbsnextc(s));
1879 /* Note that I assume that the size of the character is unchanged */
1880 if (c > 255)
1882 *s++=(c>>8);
1883 c=c & 0xff;
1885 *s++=c;
1888 else for ( ; *s; s++) *s = tolower(*s);
1889 return ret;
1893 /*********************************************************************
1894 * _mbslwr_s(MSVCRT.@)
1896 int CDECL _mbslwr_s(unsigned char* s, MSVCRT_size_t len)
1898 if (!s && !len)
1900 return 0;
1902 else if (!s || !len)
1904 *MSVCRT__errno() = MSVCRT_EINVAL;
1905 return MSVCRT_EINVAL;
1907 if (get_mbcinfo()->ismbcodepage)
1909 unsigned int c;
1910 for ( ; *s && len > 0; len--)
1912 c = _mbctolower(_mbsnextc(s));
1913 /* Note that I assume that the size of the character is unchanged */
1914 if (c > 255)
1916 *s++=(c>>8);
1917 c=c & 0xff;
1919 *s++=c;
1922 else for ( ; *s && len > 0; s++, len--) *s = tolower(*s);
1923 if (*s)
1925 *s = '\0';
1926 *MSVCRT__errno() = MSVCRT_EINVAL;
1927 return MSVCRT_EINVAL;
1929 return 0;
1933 /*********************************************************************
1934 * _mbsupr(MSVCRT.@)
1936 unsigned char* CDECL _mbsupr(unsigned char* s)
1938 unsigned char *ret = s;
1939 if (!s)
1940 return NULL;
1941 if (get_mbcinfo()->ismbcodepage)
1943 unsigned int c;
1944 while (*s)
1946 c = _mbctoupper(_mbsnextc(s));
1947 /* Note that I assume that the size of the character is unchanged */
1948 if (c > 255)
1950 *s++=(c>>8);
1951 c=c & 0xff;
1953 *s++=c;
1956 else for ( ; *s; s++) *s = toupper(*s);
1957 return ret;
1961 /*********************************************************************
1962 * _mbsupr_s(MSVCRT.@)
1964 int CDECL _mbsupr_s(unsigned char* s, MSVCRT_size_t len)
1966 if (!s && !len)
1968 return 0;
1970 else if (!s || !len)
1972 *MSVCRT__errno() = MSVCRT_EINVAL;
1973 return MSVCRT_EINVAL;
1975 if (get_mbcinfo()->ismbcodepage)
1977 unsigned int c;
1978 for ( ; *s && len > 0; len--)
1980 c = _mbctoupper(_mbsnextc(s));
1981 /* Note that I assume that the size of the character is unchanged */
1982 if (c > 255)
1984 *s++=(c>>8);
1985 c=c & 0xff;
1987 *s++=c;
1990 else for ( ; *s && len > 0; s++, len--) *s = toupper(*s);
1991 if (*s)
1993 *s = '\0';
1994 *MSVCRT__errno() = MSVCRT_EINVAL;
1995 return MSVCRT_EINVAL;
1997 return 0;
2001 /*********************************************************************
2002 * _mbsspn (MSVCRT.@)
2004 MSVCRT_size_t CDECL _mbsspn(const unsigned char* string, const unsigned char* set)
2006 const unsigned char *p, *q;
2008 for (p = string; *p; p++)
2010 if (_ismbblead(*p))
2012 for (q = set; *q; q++)
2014 if (!q[1])
2015 break;
2016 if ((*p == *q) && (p[1] == q[1]))
2017 break;
2018 q++;
2020 if (!q[0] || !q[1]) break;
2022 else
2024 for (q = set; *q; q++)
2025 if (*p == *q)
2026 break;
2027 if (!*q) break;
2030 return p - string;
2033 /*********************************************************************
2034 * _mbsspnp (MSVCRT.@)
2036 unsigned char* CDECL _mbsspnp(const unsigned char* string, const unsigned char* set)
2038 const unsigned char *p, *q;
2040 for (p = string; *p; p++)
2042 if (_ismbblead(*p))
2044 for (q = set; *q; q++)
2046 if (!q[1])
2047 break;
2048 if ((*p == *q) && (p[1] == q[1]))
2049 break;
2050 q++;
2052 if (!q[0] || !q[1]) break;
2054 else
2056 for (q = set; *q; q++)
2057 if (*p == *q)
2058 break;
2059 if (!*q) break;
2062 if (*p == '\0')
2063 return NULL;
2064 return (unsigned char *)p;
2067 /*********************************************************************
2068 * _mbscspn(MSVCRT.@)
2070 MSVCRT_size_t CDECL _mbscspn(const unsigned char* str, const unsigned char* cmp)
2072 if (get_mbcinfo()->ismbcodepage)
2073 FIXME("don't handle double character case\n");
2074 return u_strcspn(str, cmp);
2077 /*********************************************************************
2078 * _mbsrev (MSVCRT.@)
2080 unsigned char* CDECL _mbsrev(unsigned char* str)
2082 int i, len = _mbslen(str);
2083 unsigned char *p, *temp=MSVCRT_malloc(len*2);
2085 if(!temp)
2086 return str;
2088 /* unpack multibyte string to temp buffer */
2089 p=str;
2090 for(i=0; i<len; i++)
2092 if (_ismbblead(*p))
2094 temp[i*2]=*p++;
2095 temp[i*2+1]=*p++;
2097 else
2099 temp[i*2]=*p++;
2100 temp[i*2+1]=0;
2104 /* repack it in the reverse order */
2105 p=str;
2106 for(i=len-1; i>=0; i--)
2108 if(_ismbblead(temp[i*2]))
2110 *p++=temp[i*2];
2111 *p++=temp[i*2+1];
2113 else
2115 *p++=temp[i*2];
2119 MSVCRT_free(temp);
2121 return str;
2124 /*********************************************************************
2125 * _mbspbrk (MSVCRT.@)
2127 unsigned char* CDECL _mbspbrk(const unsigned char* str, const unsigned char* accept)
2129 const unsigned char* p;
2131 while(*str)
2133 for(p = accept; *p; p += (_ismbblead(*p)?2:1) )
2135 if (*p == *str)
2136 if( !_ismbblead(*p) || ( *(p+1) == *(str+1) ) )
2137 return (unsigned char*)str;
2139 str += (_ismbblead(*str)?2:1);
2141 return NULL;
2146 * Functions depending on locale codepage
2149 /*********************************************************************
2150 * mblen(MSVCRT.@)
2151 * REMARKS
2152 * Unlike most of the multibyte string functions this function uses
2153 * the locale codepage, not the codepage set by _setmbcp
2155 int CDECL MSVCRT_mblen(const char* str, MSVCRT_size_t size)
2157 if (str && *str && size)
2159 if(get_locinfo()->mb_cur_max == 1)
2160 return 1; /* ASCII CP */
2162 return !MSVCRT_isleadbyte((unsigned char)*str) ? 1 : (size>1 ? 2 : -1);
2164 return 0;
2167 /*********************************************************************
2168 * mbrlen(MSVCRT.@)
2170 MSVCRT_size_t CDECL MSVCRT_mbrlen(const char *str, MSVCRT_size_t len, MSVCRT_mbstate_t *state)
2172 MSVCRT_mbstate_t s = (state ? *state : 0);
2173 MSVCRT_size_t ret;
2175 if(!len || !str || !*str)
2176 return 0;
2178 if(get_locinfo()->mb_cur_max == 1) {
2179 return 1;
2180 }else if(!s && MSVCRT_isleadbyte((unsigned char)*str)) {
2181 if(len == 1) {
2182 s = (unsigned char)*str;
2183 ret = -2;
2184 }else {
2185 ret = 2;
2187 }else if(!s) {
2188 ret = 1;
2189 }else {
2190 s = 0;
2191 ret = 2;
2194 if(state)
2195 *state = s;
2196 return ret;
2199 /*********************************************************************
2200 * _mbstrlen_l(MSVCRT.@)
2202 MSVCRT_size_t CDECL _mbstrlen_l(const char* str, MSVCRT__locale_t locale)
2204 MSVCRT_pthreadlocinfo locinfo;
2206 if(!locale)
2207 locinfo = get_locinfo();
2208 else
2209 locinfo = locale->locinfo;
2211 if(locinfo->mb_cur_max > 1) {
2212 MSVCRT_size_t len;
2213 len = MultiByteToWideChar(locinfo->lc_codepage, MB_ERR_INVALID_CHARS,
2214 str, -1, NULL, 0);
2215 if (!len) {
2216 *MSVCRT__errno() = MSVCRT_EILSEQ;
2217 return -1;
2219 return len - 1;
2222 return strlen(str);
2225 /*********************************************************************
2226 * _mbstrlen(MSVCRT.@)
2228 MSVCRT_size_t CDECL _mbstrlen(const char* str)
2230 return _mbstrlen_l(str, NULL);
2233 /*********************************************************************
2234 * _mbtowc_l(MSVCRT.@)
2236 int CDECL MSVCRT_mbtowc_l(MSVCRT_wchar_t *dst, const char* str, MSVCRT_size_t n, MSVCRT__locale_t locale)
2238 MSVCRT_pthreadlocinfo locinfo;
2239 MSVCRT_wchar_t tmpdst = '\0';
2241 if(!locale)
2242 locinfo = get_locinfo();
2243 else
2244 locinfo = locale->locinfo;
2246 if(n <= 0 || !str)
2247 return 0;
2249 if(!*str) {
2250 if(dst) *dst = 0;
2251 return 0;
2254 if(!locinfo->lc_codepage) {
2255 if(dst) *dst = (unsigned char)*str;
2256 return 1;
2258 if(n>=2 && MSVCRT__isleadbyte_l((unsigned char)*str, locale)) {
2259 if(!MultiByteToWideChar(locinfo->lc_codepage, 0, str, 2, &tmpdst, 1))
2260 return -1;
2261 if(dst) *dst = tmpdst;
2262 return 2;
2264 if(!MultiByteToWideChar(locinfo->lc_codepage, 0, str, 1, &tmpdst, 1))
2265 return -1;
2266 if(dst) *dst = tmpdst;
2267 return 1;
2270 /*********************************************************************
2271 * mbtowc(MSVCRT.@)
2273 int CDECL MSVCRT_mbtowc(MSVCRT_wchar_t *dst, const char* str, MSVCRT_size_t n)
2275 return MSVCRT_mbtowc_l(dst, str, n, NULL);
2278 /*********************************************************************
2279 * mbrtowc(MSVCRT.@)
2281 MSVCRT_size_t CDECL MSVCRT_mbrtowc(MSVCRT_wchar_t *dst, const char *str,
2282 MSVCRT_size_t n, MSVCRT_mbstate_t *state)
2284 MSVCRT_pthreadlocinfo locinfo = get_locinfo();
2285 MSVCRT_mbstate_t s = (state ? *state : 0);
2286 char tmpstr[2];
2287 int len = 0;
2289 if(dst)
2290 *dst = 0;
2292 if(!n || !str || !*str)
2293 return 0;
2295 if(locinfo->mb_cur_max == 1) {
2296 tmpstr[len++] = *str;
2297 }else if(!s && MSVCRT_isleadbyte((unsigned char)*str)) {
2298 if(n == 1) {
2299 s = (unsigned char)*str;
2300 len = -2;
2301 }else {
2302 tmpstr[0] = str[0];
2303 tmpstr[1] = str[1];
2304 len = 2;
2306 }else if(!s) {
2307 tmpstr[len++] = *str;
2308 }else {
2309 tmpstr[0] = s;
2310 tmpstr[1] = *str;
2311 len = 2;
2312 s = 0;
2315 if(len > 0) {
2316 if(!MultiByteToWideChar(locinfo->lc_codepage, 0, tmpstr, len, dst, dst ? 1 : 0))
2317 len = -1;
2320 if(state)
2321 *state = s;
2322 return len;
2325 /*********************************************************************
2326 * _mbstowcs_l(MSVCRT.@)
2328 MSVCRT_size_t CDECL MSVCRT__mbstowcs_l(MSVCRT_wchar_t *wcstr, const char *mbstr,
2329 MSVCRT_size_t count, MSVCRT__locale_t locale)
2331 MSVCRT_pthreadlocinfo locinfo;
2332 MSVCRT_size_t i, size;
2334 if(!mbstr) {
2335 *MSVCRT__errno() = MSVCRT_EINVAL;
2336 return -1;
2339 if(!locale)
2340 locinfo = get_locinfo();
2341 else
2342 locinfo = locale->locinfo;
2344 if(!locinfo->lc_codepage) {
2345 if(!wcstr)
2346 return strlen(mbstr);
2348 for(i=0; i<count; i++) {
2349 wcstr[i] = (unsigned char)mbstr[i];
2350 if(!wcstr[i]) break;
2352 return i;
2355 /* Ignore count parameter */
2356 if(!wcstr)
2357 return MultiByteToWideChar(locinfo->lc_codepage, 0, mbstr, -1, NULL, 0)-1;
2359 for(i=0, size=0; i<count; i++) {
2360 if(mbstr[size] == '\0')
2361 break;
2363 size += (MSVCRT__isleadbyte_l((unsigned char)mbstr[size], locale) ? 2 : 1);
2366 if(size) {
2367 size = MultiByteToWideChar(locinfo->lc_codepage, 0,
2368 mbstr, size, wcstr, count);
2369 if(!size) {
2370 if(count) wcstr[0] = '\0';
2371 *MSVCRT__errno() = MSVCRT_EILSEQ;
2372 return -1;
2376 if(size<count && wcstr)
2377 wcstr[size] = '\0';
2379 return size;
2382 /*********************************************************************
2383 * mbstowcs(MSVCRT.@)
2385 MSVCRT_size_t CDECL MSVCRT_mbstowcs(MSVCRT_wchar_t *wcstr,
2386 const char *mbstr, MSVCRT_size_t count)
2388 return MSVCRT__mbstowcs_l(wcstr, mbstr, count, NULL);
2391 /*********************************************************************
2392 * _mbstowcs_s_l(MSVCRT.@)
2394 int CDECL MSVCRT__mbstowcs_s_l(MSVCRT_size_t *ret, MSVCRT_wchar_t *wcstr,
2395 MSVCRT_size_t size, const char *mbstr,
2396 MSVCRT_size_t count, MSVCRT__locale_t locale)
2398 MSVCRT_size_t conv;
2400 if(!wcstr && !size) {
2401 conv = MSVCRT__mbstowcs_l(NULL, mbstr, 0, locale);
2402 if(ret)
2403 *ret = conv+1;
2404 return 0;
2407 if (!MSVCRT_CHECK_PMT(wcstr != NULL)) return MSVCRT_EINVAL;
2408 if (!MSVCRT_CHECK_PMT(mbstr != NULL)) {
2409 if(size) wcstr[0] = '\0';
2410 return MSVCRT_EINVAL;
2413 if(count==MSVCRT__TRUNCATE || size<count)
2414 conv = size;
2415 else
2416 conv = count;
2418 conv = MSVCRT__mbstowcs_l(wcstr, mbstr, conv, locale);
2419 if(conv<size)
2420 wcstr[conv++] = '\0';
2421 else if(conv==size && (count==MSVCRT__TRUNCATE || wcstr[conv-1]=='\0'))
2422 wcstr[conv-1] = '\0';
2423 else {
2424 MSVCRT_INVALID_PMT("wcstr[size] is too small", MSVCRT_ERANGE);
2425 if(size)
2426 wcstr[0] = '\0';
2427 return MSVCRT_ERANGE;
2430 if(ret)
2431 *ret = conv;
2432 return 0;
2435 /*********************************************************************
2436 * mbstowcs_s(MSVCRT.@)
2438 int CDECL MSVCRT__mbstowcs_s(MSVCRT_size_t *ret, MSVCRT_wchar_t *wcstr,
2439 MSVCRT_size_t size, const char *mbstr, MSVCRT_size_t count)
2441 return MSVCRT__mbstowcs_s_l(ret, wcstr, size, mbstr, count, NULL);
2444 /*********************************************************************
2445 * mbsrtowcs(MSVCRT.@)
2447 MSVCRT_size_t CDECL MSVCRT_mbsrtowcs(MSVCRT_wchar_t *wcstr,
2448 const char **pmbstr, MSVCRT_size_t count, MSVCRT_mbstate_t *state)
2450 MSVCRT_mbstate_t s = (state ? *state : 0);
2451 MSVCRT_wchar_t tmpdst;
2452 MSVCRT_size_t ret = 0;
2453 const char *p;
2455 if(!MSVCRT_CHECK_PMT(pmbstr != NULL))
2456 return -1;
2458 p = *pmbstr;
2459 while(!wcstr || count>ret) {
2460 int ch_len = MSVCRT_mbrtowc(&tmpdst, p, 2, &s);
2461 if(wcstr)
2462 wcstr[ret] = tmpdst;
2464 if(ch_len < 0) {
2465 return -1;
2466 }else if(ch_len == 0) {
2467 if(wcstr) *pmbstr = NULL;
2468 return ret;
2471 p += ch_len;
2472 ret++;
2475 if(wcstr) *pmbstr = p;
2476 return ret;
2479 /*********************************************************************
2480 * mbsrtowcs_s(MSVCRT.@)
2482 int CDECL MSVCRT_mbsrtowcs_s(MSVCRT_size_t *ret, MSVCRT_wchar_t *wcstr, MSVCRT_size_t len,
2483 const char **mbstr, MSVCRT_size_t count, MSVCRT_mbstate_t *state)
2485 MSVCRT_size_t tmp;
2487 if(!ret) ret = &tmp;
2488 if(!MSVCRT_CHECK_PMT(!!wcstr == !!len)) {
2489 *ret = -1;
2490 return MSVCRT_EINVAL;
2493 *ret = MSVCRT_mbsrtowcs(wcstr, mbstr, count>len ? len : count, state);
2494 if(*ret == -1) {
2495 if(wcstr) *wcstr = 0;
2496 return *MSVCRT__errno();
2498 (*ret)++;
2499 if(*ret > len) {
2500 /* no place for terminating '\0' */
2501 if(wcstr) *wcstr = 0;
2502 return 0;
2504 if(wcstr) wcstr[(*ret)-1] = 0;
2505 return 0;
2508 /*********************************************************************
2509 * _mbctohira (MSVCRT.@)
2511 * Converts a sjis katakana character to hiragana.
2513 unsigned int CDECL _mbctohira(unsigned int c)
2515 if(_ismbckata(c) && c <= 0x8393)
2516 return (c - 0x8340 - (c >= 0x837f ? 1 : 0)) + 0x829f;
2517 return c;
2520 /*********************************************************************
2521 * _mbctokata (MSVCRT.@)
2523 * Converts a sjis hiragana character to katakana.
2525 unsigned int CDECL _mbctokata(unsigned int c)
2527 if(_ismbchira(c))
2528 return (c - 0x829f) + 0x8340 + (c >= 0x82de ? 1 : 0);
2529 return c;
2533 /*********************************************************************
2534 * _ismbcl0 (MSVCRT.@)
2536 int CDECL _ismbcl0(unsigned int c)
2538 if(get_mbcinfo()->mbcodepage == 932)
2540 /* JIS non-Kanji */
2541 return (c >= 0x8140 && c <= 0x889e);
2543 return 0;