ninput: Implement SetPropertyInteractionContext().
[wine.git] / dlls / msvcrt / mbcs.c
blobd00c31af4cfe97900825eb38241a381ad675dd12
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, 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 += 2)
2016 if (!q[1])
2017 break;
2018 if ((*p == *q) && (p[1] == q[1]))
2019 break;
2021 if (!q[0] || !q[1]) break;
2023 else
2025 for (q = set; *q; q++)
2026 if (*p == *q)
2027 break;
2028 if (!*q) break;
2031 return p - string;
2034 /*********************************************************************
2035 * _mbsspnp (MSVCRT.@)
2037 unsigned char* CDECL _mbsspnp(const unsigned char* string, const unsigned char* set)
2039 const unsigned char *p, *q;
2041 for (p = string; *p; p++)
2043 if (_ismbblead(*p))
2045 for (q = set; *q; q += 2)
2047 if (!q[1])
2048 break;
2049 if ((*p == *q) && (p[1] == q[1]))
2050 break;
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;