ntdll: Allow NtQueryVolumeInformationFile to make async volume information queries.
[wine/zf.git] / dlls / msvcrt / mbcs.c
blob03af50d8b0128cee5cd2212970e87cd998824a0f
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>
28 #include <mbctype.h>
29 #include <mbstring.h>
31 #include "msvcrt.h"
32 #include "mtdll.h"
33 #include "winnls.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
38 unsigned char MSVCRT_mbctype[257] = { 0 };
40 /* It seems that the data about valid trail bytes is not available from kernel32
41 * so we have to store is here. The format is the same as for lead bytes in CPINFO */
42 struct cp_extra_info_t
44 int cp;
45 BYTE TrailBytes[MAX_LEADBYTES];
48 static struct cp_extra_info_t g_cpextrainfo[] =
50 {932, {0x40, 0x7e, 0x80, 0xfc, 0, 0}},
51 {936, {0x40, 0xfe, 0, 0}},
52 {949, {0x41, 0xfe, 0, 0}},
53 {950, {0x40, 0x7e, 0xa1, 0xfe, 0, 0}},
54 {1361, {0x31, 0x7e, 0x81, 0xfe, 0, 0}},
55 {20932, {1, 255, 0, 0}}, /* seems to give different results on different systems */
56 {0, {1, 255, 0, 0}} /* match all with FIXME */
59 /* Maps cp932 single byte character to multi byte character */
60 static const unsigned char mbbtombc_932[] = {
61 0x40,0x49,0x68,0x94,0x90,0x93,0x95,0x66,0x69,0x6a,0x96,0x7b,0x43,0x7c,0x44,0x5e,
62 0x4f,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x46,0x47,0x83,0x81,0x84,0x48,
63 0x97,0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,
64 0x6f,0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x6d,0x8f,0x6e,0x4f,0x76,
65 0x77,0x78,0x79,0x6d,0x8f,0x6e,0x4f,0x51,0x65,0x81,0x82,0x83,0x84,0x85,0x86,0x87,
66 0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f,0x90,0x91,0x92,0x93,0x94,0x95,0x50,
67 0x42,0x75,0x76,0x41,0x45,0x92,0x40,0x42,0x44,0x46,0x48,0x83,0x85,0x87,0x62,
68 0x5b,0x41,0x43,0x45,0x47,0x49,0x4a,0x4c,0x4e,0x50,0x52,0x54,0x56,0x58,0x5a,0x5c,
69 0x5e,0x60,0x63,0x65,0x67,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x71,0x74,0x77,0x7a,0x7d,
70 0x7e,0x80,0x81,0x82,0x84,0x86,0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8f,0x93,0x4a,0x4b };
72 /* Maps multibyte cp932 punctuation marks to single byte equivalents */
73 static const unsigned char mbctombb_932_punct[] = {
74 0x20,0xa4,0xa1,0x2c,0x2e,0xa5,0x3a,0x3b,0x3f,0x21,0xde,0xdf,0x00,0x00,0x00,0x5e,
75 0x7e,0x5f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xb0,0x00,0x00,0x2f,0x00,
76 0x00,0x00,0x7c,0x00,0x00,0x60,0x27,0x00,0x22,0x28,0x29,0x00,0x00,0x5b,0x5d,0x7b,
77 0x7d,0x00,0x00,0x00,0x00,0xa2,0xa3,0x00,0x00,0x00,0x00,0x2b,0x2d,0x00,0x00,0x00,
78 0x00,0x3d,0x00,0x3c,0x3e,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x5c,
79 0x24,0x00,0x00,0x25,0x23,0x26,0x2a,0x40};
81 /* Maps multibyte cp932 hiragana/katakana to single-byte equivalents */
82 static const unsigned char mbctombb_932_kana[] = {
83 0xa7,0xb1,0xa8,0xb2,0xa9,0xb3,0xaa,0xb4,0xab,0xb5,0xb6,0xb6,0xb7,0xb7,0xb8,0xb8,
84 0xb9,0xb9,0xba,0xba,0xbb,0xbb,0xbc,0xbc,0xbd,0xbd,0xbe,0xbe,0xbf,0xbf,0xc0,0xc0,
85 0xc1,0xc1,0xaf,0xc2,0xc2,0xc3,0xc3,0xc4,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xca,
86 0xca,0xcb,0xcb,0xcb,0xcc,0xcc,0xcc,0xcd,0xcd,0xcd,0xce,0xce,0xce,0xcf,0xd0,0xd1,
87 0xd2,0xd3,0xac,0xd4,0xad,0xd5,0xae,0xd6,0xd7,0xd8,0xd9,0xda,0xdb,0xdc,0xdc,0xb2,
88 0xb4,0xa6,0xdd,0xb3,0xb6,0xb9};
90 static wchar_t msvcrt_mbc_to_wc_l(unsigned int ch, _locale_t locale)
92 pthreadmbcinfo mbcinfo;
93 wchar_t chW;
94 char mbch[2];
95 int n_chars;
97 if(locale)
98 mbcinfo = locale->mbcinfo;
99 else
100 mbcinfo = get_mbcinfo();
102 if (ch <= 0xff) {
103 mbch[0] = ch;
104 n_chars = 1;
105 } else {
106 mbch[0] = (ch >> 8) & 0xff;
107 mbch[1] = ch & 0xff;
108 n_chars = 2;
110 if (!MultiByteToWideChar(mbcinfo->mbcodepage, 0, mbch, n_chars, &chW, 1))
112 WARN("MultiByteToWideChar failed on %x\n", ch);
113 return 0;
115 return chW;
118 static wchar_t msvcrt_mbc_to_wc(unsigned int ch)
120 return msvcrt_mbc_to_wc_l(ch, NULL);
123 static inline size_t u_strlen( const unsigned char *str )
125 return strlen( (const char*) str );
128 static inline unsigned char* u_strncat( unsigned char* dst, const unsigned char* src, size_t len )
130 return (unsigned char*)strncat( (char*)dst, (const char*)src, len);
133 static inline int u_strcmp( const unsigned char *s1, const unsigned char *s2 )
135 return strcmp( (const char*)s1, (const char*)s2 );
138 static inline int u_strcasecmp( const unsigned char *s1, const unsigned char *s2 )
140 return _stricmp( (const char*)s1, (const char*)s2 );
143 static inline int u_strncmp( const unsigned char *s1, const unsigned char *s2, size_t len )
145 return strncmp( (const char*)s1, (const char*)s2, len );
148 static inline int u_strncasecmp( const unsigned char *s1, const unsigned char *s2, size_t len )
150 return _strnicmp( (const char*)s1, (const char*)s2, len );
153 static inline unsigned char *u_strchr( const unsigned char *s, unsigned char x )
155 return (unsigned char*) strchr( (const char*)s, x );
158 static inline unsigned char *u_strrchr( const unsigned char *s, unsigned char x )
160 return (unsigned char*) strrchr( (const char*)s, x );
163 static inline unsigned char *u__strset( unsigned char *s, unsigned char c )
165 return (unsigned char*) _strset( (char*)s, c);
168 static inline unsigned char *u__strnset( unsigned char *s, unsigned char c, size_t len )
170 return (unsigned char*) _strnset( (char*)s, c, len );
173 /*********************************************************************
174 * __p__mbctype (MSVCRT.@)
176 unsigned char* CDECL __p__mbctype(void)
178 return get_mbcinfo()->mbctype;
181 /*********************************************************************
182 * __p___mb_cur_max(MSVCRT.@)
184 int* CDECL __p___mb_cur_max(void)
186 return &get_locinfo()->mb_cur_max;
189 /*********************************************************************
190 * ___mb_cur_max_func(MSVCRT.@)
192 int CDECL ___mb_cur_max_func(void)
194 return get_locinfo()->mb_cur_max;
197 #if _MSVCR_VER>=80
198 /*********************************************************************
199 * ___mb_cur_max_l_func (MSVCR80.@)
201 int CDECL ___mb_cur_max_l_func(_locale_t locale)
203 pthreadlocinfo locinfo;
205 if(!locale)
206 locinfo = get_locinfo();
207 else
208 locinfo = locale->locinfo;
210 return locinfo->mb_cur_max;
212 #endif
214 threadmbcinfo* create_mbcinfo(int cp, LCID lcid, threadmbcinfo *old_mbcinfo)
216 threadmbcinfo *mbcinfo;
217 int newcp;
218 CPINFO cpi;
219 BYTE *bytes;
220 WORD chartypes[256];
221 char bufA[256];
222 WCHAR bufW[256], lowW[256], upW[256];
223 int charcount;
224 int ret;
225 int i;
227 if(old_mbcinfo && cp==old_mbcinfo->mbcodepage
228 && (lcid==-1 || lcid==old_mbcinfo->mblcid)) {
229 InterlockedIncrement(&old_mbcinfo->refcount);
230 return old_mbcinfo;
233 mbcinfo = malloc(sizeof(threadmbcinfo));
234 if(!mbcinfo)
235 return NULL;
236 mbcinfo->refcount = 1;
238 switch (cp)
240 case _MB_CP_ANSI:
241 newcp = GetACP();
242 break;
243 case _MB_CP_OEM:
244 newcp = GetOEMCP();
245 break;
246 case _MB_CP_LOCALE:
247 newcp = get_locinfo()->lc_codepage;
248 if(newcp)
249 break;
250 /* fall through (C locale) */
251 case _MB_CP_SBCS:
252 newcp = 20127; /* ASCII */
253 break;
254 default:
255 newcp = cp;
256 break;
259 if(lcid == -1) {
260 sprintf(bufA, ".%d", newcp);
261 mbcinfo->mblcid = locale_to_LCID(bufA, NULL, NULL);
262 } else {
263 mbcinfo->mblcid = lcid;
266 if(mbcinfo->mblcid == -1)
268 WARN("Can't assign LCID to codepage (%d)\n", mbcinfo->mblcid);
269 mbcinfo->mblcid = 0;
272 if (!GetCPInfo(newcp, &cpi))
274 WARN("Codepage %d not found\n", newcp);
275 free(mbcinfo);
276 return NULL;
279 /* setup the _mbctype */
280 memset(mbcinfo->mbctype, 0, sizeof(unsigned char[257]));
281 memset(mbcinfo->mbcasemap, 0, sizeof(unsigned char[256]));
283 bytes = cpi.LeadByte;
284 while (bytes[0] || bytes[1])
286 for (i = bytes[0]; i <= bytes[1]; i++)
287 mbcinfo->mbctype[i + 1] |= _M1;
288 bytes += 2;
291 if (cpi.MaxCharSize > 1)
293 /* trail bytes not available through kernel32 but stored in a structure in msvcrt */
294 struct cp_extra_info_t *cpextra = g_cpextrainfo;
296 mbcinfo->ismbcodepage = 1;
297 while (TRUE)
299 if (cpextra->cp == 0 || cpextra->cp == newcp)
301 if (cpextra->cp == 0)
302 FIXME("trail bytes data not available for DBCS codepage %d - assuming all bytes\n", newcp);
304 bytes = cpextra->TrailBytes;
305 while (bytes[0] || bytes[1])
307 for (i = bytes[0]; i <= bytes[1]; i++)
308 mbcinfo->mbctype[i + 1] |= _M2;
309 bytes += 2;
311 break;
313 cpextra++;
316 else
317 mbcinfo->ismbcodepage = 0;
319 /* we can't use GetStringTypeA directly because we don't have a locale - only a code page
321 charcount = 0;
322 for (i = 0; i < 256; i++)
323 if (!(mbcinfo->mbctype[i + 1] & _M1))
324 bufA[charcount++] = i;
326 ret = MultiByteToWideChar(newcp, 0, bufA, charcount, bufW, charcount);
327 if (ret != charcount)
328 ERR("MultiByteToWideChar of chars failed for cp %d, ret=%d (exp %d), error=%d\n", newcp, ret, charcount, GetLastError());
330 GetStringTypeW(CT_CTYPE1, bufW, charcount, chartypes);
331 LCMapStringW(lcid, LCMAP_LOWERCASE, bufW, charcount, lowW, charcount);
332 LCMapStringW(lcid, LCMAP_UPPERCASE, bufW, charcount, upW, charcount);
334 charcount = 0;
335 for (i = 0; i < 256; i++)
336 if (!(mbcinfo->mbctype[i + 1] & _M1))
338 if (chartypes[charcount] & C1_UPPER)
340 mbcinfo->mbctype[i + 1] |= _SBUP;
341 bufW[charcount] = lowW[charcount];
343 else if (chartypes[charcount] & C1_LOWER)
345 mbcinfo->mbctype[i + 1] |= _SBLOW;
346 bufW[charcount] = upW[charcount];
348 charcount++;
351 ret = WideCharToMultiByte(newcp, 0, bufW, charcount, bufA, charcount, NULL, NULL);
352 if (ret != charcount)
353 ERR("WideCharToMultiByte failed for cp %d, ret=%d (exp %d), error=%d\n", newcp, ret, charcount, GetLastError());
355 charcount = 0;
356 for (i = 0; i < 256; i++)
358 if(!(mbcinfo->mbctype[i + 1] & _M1))
360 if(mbcinfo->mbctype[i] & (C1_UPPER|C1_LOWER))
361 mbcinfo->mbcasemap[i] = bufA[charcount];
362 charcount++;
366 if (newcp == 932) /* CP932 only - set _MP and _MS */
368 /* On Windows it's possible to calculate the _MP and _MS from CT_CTYPE1
369 * and CT_CTYPE3. But as of Wine 0.9.43 we return wrong values what makes
370 * it hard. As this is set only for codepage 932 we hardcode it what gives
371 * also faster execution.
373 for (i = 161; i <= 165; i++)
374 mbcinfo->mbctype[i + 1] |= _MP;
375 for (i = 166; i <= 223; i++)
376 mbcinfo->mbctype[i + 1] |= _MS;
379 mbcinfo->mbcodepage = newcp;
380 return mbcinfo;
383 /*********************************************************************
384 * _setmbcp (MSVCRT.@)
386 int CDECL _setmbcp(int cp)
388 thread_data_t *data = msvcrt_get_thread_data();
389 threadmbcinfo *mbcinfo;
391 mbcinfo = create_mbcinfo(cp, -1, get_mbcinfo());
392 if(!mbcinfo)
394 *_errno() = EINVAL;
395 return -1;
398 if(data->locale_flags & LOCALE_THREAD)
400 if(data->locale_flags & LOCALE_FREE)
401 free_mbcinfo(data->mbcinfo);
402 data->mbcinfo = mbcinfo;
404 else
406 _lock(_MB_CP_LOCK);
407 free_mbcinfo(MSVCRT_locale->mbcinfo);
408 MSVCRT_locale->mbcinfo = mbcinfo;
409 memcpy(MSVCRT_mbctype, MSVCRT_locale->mbcinfo->mbctype, sizeof(MSVCRT_mbctype));
410 _unlock(_MB_CP_LOCK);
412 return 0;
415 /*********************************************************************
416 * _getmbcp (MSVCRT.@)
418 int CDECL _getmbcp(void)
420 return get_mbcinfo()->mbcodepage;
423 /*********************************************************************
424 * _mbsnextc_l(MSVCRT.@)
426 unsigned int CDECL _mbsnextc_l(const unsigned char* str, _locale_t locale)
428 if(_ismbblead_l(*str, locale))
429 return *str << 8 | str[1];
430 return *str;
433 /*********************************************************************
434 * _mbsnextc(MSVCRT.@)
436 unsigned int CDECL _mbsnextc(const unsigned char* str)
438 return _mbsnextc_l(str, NULL);
441 /*********************************************************************
442 * _mbctolower(MSVCRT.@)
444 unsigned int CDECL _mbctolower(unsigned int c)
446 if (_ismbblead(c))
448 FIXME("Handle MBC chars\n");
449 return c;
451 return _tolower_l(c, NULL); /* ASCII CP or SB char */
454 /*********************************************************************
455 * _mbctoupper(MSVCRT.@)
457 unsigned int CDECL _mbctoupper(unsigned int c)
459 if (_ismbblead(c))
461 FIXME("Handle MBC chars\n");
462 return c;
464 return _toupper_l(c, NULL); /* ASCII CP or SB char */
467 /*********************************************************************
468 * _mbctombb (MSVCRT.@)
470 unsigned int CDECL _mbctombb(unsigned int c)
472 unsigned int value;
474 if(get_mbcinfo()->mbcodepage == 932)
476 if(c >= 0x829f && c <= 0x82f1) /* Hiragana */
477 return mbctombb_932_kana[c - 0x829f];
478 if(c >= 0x8340 && c <= 0x8396 && c != 0x837f) /* Katakana */
479 return mbctombb_932_kana[c - 0x8340 - (c >= 0x837f ? 1 : 0)];
480 if(c >= 0x8140 && c <= 0x8197) /* Punctuation */
482 value = mbctombb_932_punct[c - 0x8140];
483 return value ? value : c;
485 if((c >= 0x824f && c <= 0x8258) || /* Fullwidth digits */
486 (c >= 0x8260 && c <= 0x8279)) /* Fullwidth capitals letters */
487 return c - 0x821f;
488 if(c >= 0x8281 && c <= 0x829a) /* Fullwidth small letters */
489 return c - 0x8220;
490 /* all other cases return c */
492 return c;
495 /*********************************************************************
496 * _mbcjistojms(MSVCRT.@)
498 * Converts a jis character to sjis.
499 * Based on description from
500 * http://www.slayers.ne.jp/~oouchi/code/jistosjis.html
502 unsigned int CDECL _mbcjistojms(unsigned int c)
504 /* Conversion takes place only when codepage is 932.
505 In all other cases, c is returned unchanged */
506 if(get_mbcinfo()->mbcodepage == 932)
508 if(HIBYTE(c) >= 0x21 && HIBYTE(c) <= 0x7e &&
509 LOBYTE(c) >= 0x21 && LOBYTE(c) <= 0x7e)
511 if(HIBYTE(c) % 2)
512 c += 0x1f;
513 else
514 c += 0x7d;
516 if(LOBYTE(c) >= 0x7F)
517 c += 0x1;
519 c = (((HIBYTE(c) - 0x21)/2 + 0x81) << 8) | LOBYTE(c);
521 if(HIBYTE(c) > 0x9f)
522 c += 0x4000;
524 else
525 return 0; /* Codepage is 932, but c can't be converted */
528 return c;
531 /*********************************************************************
532 * _mbcjmstojis(MSVCRT.@)
534 * Converts a sjis character to jis.
536 unsigned int CDECL _mbcjmstojis(unsigned int c)
538 /* Conversion takes place only when codepage is 932.
539 In all other cases, c is returned unchanged */
540 if(get_mbcinfo()->mbcodepage == 932)
542 if(_ismbclegal(c) && HIBYTE(c) < 0xf0)
544 if(HIBYTE(c) >= 0xe0)
545 c -= 0x4000;
547 c = (((HIBYTE(c) - 0x81)*2 + 0x21) << 8) | LOBYTE(c);
549 if(LOBYTE(c) > 0x7f)
550 c -= 0x1;
552 if(LOBYTE(c) > 0x9d)
553 c += 0x83;
554 else
555 c -= 0x1f;
557 else
558 return 0; /* Codepage is 932, but c can't be converted */
561 return c;
564 /*********************************************************************
565 * _mbsdec(MSVCRT.@)
567 unsigned char* CDECL _mbsdec(const unsigned char* start, const unsigned char* cur)
569 if(start >= cur)
570 return NULL;
572 if(get_mbcinfo()->ismbcodepage)
573 return (unsigned char *)(_ismbstrail(start,cur-1) ? cur - 2 : cur -1);
575 return (unsigned char *)cur - 1; /* ASCII CP or SB char */
578 /*********************************************************************
579 * _mbclen(MSVCRT.@)
581 size_t CDECL _mbclen(const unsigned char* str)
583 return _ismbblead(*str) ? 2 : 1;
586 /*********************************************************************
587 * _mbsinc(MSVCRT.@)
589 unsigned char* CDECL _mbsinc(const unsigned char* str)
591 return (unsigned char *)(str + _mbclen(str));
594 /*********************************************************************
595 * _mbsninc(MSVCRT.@)
597 unsigned char* CDECL _mbsninc(const unsigned char* str, size_t num)
599 if(!str)
600 return NULL;
602 while (num > 0 && *str)
604 if (_ismbblead(*str))
606 if (!*(str+1))
607 break;
608 str++;
610 str++;
611 num--;
614 return (unsigned char*)str;
617 /*********************************************************************
618 * _mbsnlen_l(MSVCRT.@)
620 size_t CDECL _mbsnlen_l(const unsigned char *str,
621 size_t maxsize, _locale_t locale)
623 pthreadmbcinfo mbcinfo;
624 size_t i = 0, len = 0;
626 if(!locale)
627 mbcinfo = get_mbcinfo();
628 else
629 mbcinfo = locale->mbcinfo;
631 if(!mbcinfo->ismbcodepage)
632 return strnlen((const char*)str, maxsize);
634 while(i<maxsize && str[i])
636 if (_ismbblead_l(str[i], locale))
638 i++;
639 if (!str[i]) /* count only full chars */
640 break;
642 i++;
643 len++;
645 return i < maxsize ? len : maxsize;
648 /*********************************************************************
649 * _mbslen(MSVCRT.@)
651 size_t CDECL _mbslen(const unsigned char* str)
653 return _mbsnlen_l(str, -1, NULL);
656 /*********************************************************************
657 * _mbslen_l(MSVCRT.@)
659 size_t CDECL _mbslen_l(const unsigned char* str, _locale_t locale)
661 return _mbsnlen_l(str, -1, locale);
664 /*********************************************************************
665 * _mbsnlen(MSVCRT.@)
667 size_t CDECL _mbsnlen(const unsigned char* str, size_t maxsize)
669 return _mbsnlen_l(str, maxsize, NULL);
672 /*********************************************************************
673 * _mbccpy_s_l(MSVCRT.@)
675 int CDECL _mbccpy_s_l(unsigned char* dest, size_t maxsize,
676 int *copied, const unsigned char* src, _locale_t locale)
678 if(copied) *copied = 0;
679 if(!MSVCRT_CHECK_PMT(dest != NULL && maxsize >= 1)) return EINVAL;
680 dest[0] = 0;
681 if(!MSVCRT_CHECK_PMT(src != NULL)) return EINVAL;
683 if(_ismbblead_l(*src, locale)) {
684 if(!src[1]) {
685 if(copied) *copied = 1;
686 *_errno() = EILSEQ;
687 return EILSEQ;
690 if(maxsize < 2) {
691 MSVCRT_INVALID_PMT("dst buffer is too small", ERANGE);
692 return ERANGE;
695 *dest++ = *src++;
696 *dest = *src;
697 if(copied) *copied = 2;
698 }else {
699 *dest = *src;
700 if(copied) *copied = 1;
703 return 0;
706 /*********************************************************************
707 * _mbccpy(MSVCRT.@)
709 void CDECL _mbccpy(unsigned char* dest, const unsigned char* src)
711 _mbccpy_s_l(dest, 2, NULL, src, NULL);
714 /*********************************************************************
715 * _mbccpy_l(MSVCRT.@)
717 void CDECL _mbccpy_l(unsigned char* dest, const unsigned char* src,
718 _locale_t locale)
720 _mbccpy_s_l(dest, 2, NULL, src, locale);
723 /*********************************************************************
724 * _mbccpy_s(MSVCRT.@)
726 int CDECL _mbccpy_s(unsigned char* dest, size_t maxsize,
727 int *copied, const unsigned char* src)
729 return _mbccpy_s_l(dest, maxsize, copied, src, NULL);
732 /*********************************************************************
733 * _mbsncpy(MSVCRT.@)
734 * REMARKS
735 * The parameter n is the number or characters to copy, not the size of
736 * the buffer. Use _mbsnbcpy for a function analogical to strncpy
738 unsigned char* CDECL _mbsncpy(unsigned char* dst, const unsigned char* src, size_t n)
740 unsigned char* ret = dst;
741 if(!n)
742 return dst;
743 if (get_mbcinfo()->ismbcodepage)
745 while (*src && n)
747 n--;
748 if (_ismbblead(*src))
750 if (!*(src+1))
752 *dst++ = 0;
753 *dst++ = 0;
754 break;
757 *dst++ = *src++;
760 *dst++ = *src++;
763 else
765 while (n)
767 n--;
768 if (!(*dst++ = *src++)) break;
771 while (n--) *dst++ = 0;
772 return ret;
775 /*********************************************************************
776 * _mbsnbcpy_s_l(MSVCRT.@)
777 * REMARKS
778 * Unlike _mbsnbcpy this function does not pad the rest of the dest
779 * string with 0
781 int CDECL _mbsnbcpy_s_l(unsigned char* dst, size_t size,
782 const unsigned char* src, size_t n, _locale_t locale)
784 size_t pos = 0;
786 if(!dst || size == 0)
787 return EINVAL;
788 if(!src)
790 dst[0] = '\0';
791 return EINVAL;
793 if(!n)
794 return 0;
796 if((locale ? locale->mbcinfo : get_mbcinfo())->ismbcodepage)
798 BOOL is_lead = FALSE;
799 while (*src && n)
801 if(pos == size)
803 dst[0] = '\0';
804 return ERANGE;
806 is_lead = (!is_lead && _ismbblead(*src));
807 n--;
808 dst[pos++] = *src++;
811 if (is_lead) /* if string ends with a lead, remove it */
812 dst[pos - 1] = 0;
814 else
816 while (n)
818 n--;
819 if(pos == size)
821 dst[0] = '\0';
822 return ERANGE;
825 if(!(*src)) break;
826 dst[pos++] = *src++;
830 if(pos < size)
831 dst[pos] = '\0';
832 else
834 dst[0] = '\0';
835 return ERANGE;
838 return 0;
841 /*********************************************************************
842 * _mbsnbcpy_s(MSVCRT.@)
844 int CDECL _mbsnbcpy_s(unsigned char* dst, size_t size, const unsigned char* src, size_t n)
846 return _mbsnbcpy_s_l(dst, size, src, n, NULL);
849 /*********************************************************************
850 * _mbscpy_s_l(MSVCRT.@)
852 int CDECL _mbscpy_s_l(unsigned char *dst, size_t size,
853 const unsigned char *src, _locale_t locale)
855 return _mbsnbcpy_s_l(dst, size, src, -1, locale);
858 /*********************************************************************
859 * _mbscpy_s(MSVCRT.@)
861 int CDECL _mbscpy_s(unsigned char *dst, size_t size, const unsigned char *src)
863 return _mbscpy_s_l(dst, size, src, NULL);
866 /*********************************************************************
867 * _mbsnbcpy(MSVCRT.@)
868 * REMARKS
869 * Like strncpy this function doesn't enforce the string to be
870 * NUL-terminated
872 unsigned char* CDECL _mbsnbcpy(unsigned char* dst, const unsigned char* src, size_t n)
874 unsigned char* ret = dst;
875 if(!n)
876 return dst;
877 if(get_mbcinfo()->ismbcodepage)
879 BOOL is_lead = FALSE;
880 while (*src && n)
882 is_lead = (!is_lead && _ismbblead(*src));
883 n--;
884 *dst++ = *src++;
887 if (is_lead) /* if string ends with a lead, remove it */
888 *(dst - 1) = 0;
890 else
892 while (n)
894 n--;
895 if (!(*dst++ = *src++)) break;
898 while (n--) *dst++ = 0;
899 return ret;
902 /*********************************************************************
903 * _mbscmp_l(MSVCRT.@)
905 int CDECL _mbscmp_l(const unsigned char* str, const unsigned char* cmp, _locale_t locale)
907 pthreadmbcinfo mbcinfo;
909 if (!str || !cmp) return INT_MAX;
911 mbcinfo = locale ? locale->mbcinfo : get_mbcinfo();
913 if(mbcinfo->ismbcodepage)
915 unsigned int strc, cmpc;
916 do {
917 if(!*str)
918 return *cmp ? -1 : 0;
919 if(!*cmp)
920 return 1;
921 strc = _mbsnextc_l(str, locale);
922 cmpc = _mbsnextc_l(cmp, locale);
923 if(strc != cmpc)
924 return strc < cmpc ? -1 : 1;
925 str +=(strc > 255) ? 2 : 1;
926 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
927 } while(1);
929 return u_strcmp(str, cmp); /* ASCII CP */
932 /*********************************************************************
933 * _mbscmp(MSVCRT.@)
935 int CDECL _mbscmp(const unsigned char* str, const unsigned char* cmp)
937 return _mbscmp_l(str, cmp, NULL);
940 /*********************************************************************
941 * _mbsnbicoll_l(MSVCRT.@)
943 int CDECL _mbsnbicoll_l(const unsigned char *str1, const unsigned char *str2, size_t len, _locale_t locale)
945 pthreadmbcinfo mbcinfo;
947 if(!locale)
948 mbcinfo = get_mbcinfo();
949 else
950 mbcinfo = locale->mbcinfo;
952 if(!mbcinfo->ismbcodepage)
953 return _strnicoll_l((const char*)str1, (const char*)str2, len, locale);
954 return CompareStringA(mbcinfo->mblcid, NORM_IGNORECASE, (const char*)str1, len, (const char*)str2, len)-CSTR_EQUAL;
957 /*********************************************************************
958 * _mbsicoll_l(MSVCRT.@)
960 int CDECL _mbsicoll_l(const unsigned char *str1, const unsigned char *str2, _locale_t locale)
962 return _mbsnbicoll_l(str1, str2, -1, locale);
965 /*********************************************************************
966 * _mbsnbicoll(MSVCRT.@)
968 int CDECL _mbsnbicoll(const unsigned char *str1, const unsigned char *str2, size_t len)
970 return _mbsnbicoll_l(str1, str2, len, NULL);
973 /*********************************************************************
974 * _mbsicoll(MSVCRT.@)
976 int CDECL _mbsicoll(const unsigned char* str, const unsigned char* cmp)
978 #if _MSVCR_VER>=60 && _MSVCR_VER<=71
979 return CompareStringA(get_mbcinfo()->mblcid, NORM_IGNORECASE,
980 (const char*)str, -1, (const char*)cmp, -1)-CSTR_EQUAL;
981 #else
982 return _mbsnbicoll_l(str, cmp, -1, NULL);
983 #endif
986 /*********************************************************************
987 * _mbsnbcoll_l(MSVCRT.@)
989 int CDECL _mbsnbcoll_l(const unsigned char *str1, const unsigned char *str2, size_t len, _locale_t locale)
991 pthreadmbcinfo mbcinfo;
993 if(!locale)
994 mbcinfo = get_mbcinfo();
995 else
996 mbcinfo = locale->mbcinfo;
998 if(!mbcinfo->ismbcodepage)
999 return _strncoll_l((const char*)str1, (const char*)str2, len, locale);
1000 return CompareStringA(mbcinfo->mblcid, 0, (const char*)str1, len, (const char*)str2, len)-CSTR_EQUAL;
1003 /*********************************************************************
1004 * _mbscoll_l(MSVCRT.@)
1006 int CDECL _mbscoll_l(const unsigned char *str1, const unsigned char *str2, _locale_t locale)
1008 return _mbsnbcoll_l(str1, str2, -1, locale);
1011 /*********************************************************************
1012 * _mbsnbcoll(MSVCRT.@)
1014 int CDECL _mbsnbcoll(const unsigned char *str1, const unsigned char *str2, size_t len)
1016 return _mbsnbcoll_l(str1, str2, len, NULL);
1019 /*********************************************************************
1020 * _mbscoll(MSVCRT.@)
1022 int CDECL _mbscoll(const unsigned char* str, const unsigned char* cmp)
1024 #if _MSVCR_VER>=60 && _MSVCR_VER<=71
1025 return CompareStringA(get_mbcinfo()->mblcid, 0,
1026 (const char*)str, -1, (const char*)cmp, -1)-CSTR_EQUAL;
1027 #else
1028 return _mbsnbcoll_l(str, cmp, -1, NULL);
1029 #endif
1032 /*********************************************************************
1033 * _mbsicmp(MSVCRT.@)
1035 int CDECL _mbsicmp(const unsigned char* str, const unsigned char* cmp)
1037 if(get_mbcinfo()->ismbcodepage)
1039 unsigned int strc, cmpc;
1040 do {
1041 if(!*str)
1042 return *cmp ? -1 : 0;
1043 if(!*cmp)
1044 return 1;
1045 strc = _mbctolower(_mbsnextc(str));
1046 cmpc = _mbctolower(_mbsnextc(cmp));
1047 if(strc != cmpc)
1048 return strc < cmpc ? -1 : 1;
1049 str +=(strc > 255) ? 2 : 1;
1050 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
1051 } while(1);
1053 return u_strcasecmp(str, cmp); /* ASCII CP */
1056 /*********************************************************************
1057 * _mbsncmp(MSVCRT.@)
1059 int CDECL _mbsncmp(const unsigned char* str, const unsigned char* cmp, size_t len)
1061 if(!len)
1062 return 0;
1064 if(get_mbcinfo()->ismbcodepage)
1066 unsigned int strc, cmpc;
1067 while(len--)
1069 int inc;
1070 if(!*str)
1071 return *cmp ? -1 : 0;
1072 if(!*cmp)
1073 return 1;
1074 strc = _mbsnextc(str);
1075 cmpc = _mbsnextc(cmp);
1076 if(strc != cmpc)
1077 return strc < cmpc ? -1 : 1;
1078 inc=(strc > 255) ? 2 : 1; /* Equal, use same increment */
1079 str += inc;
1080 cmp += inc;
1082 return 0; /* Matched len chars */
1084 return u_strncmp(str, cmp, len); /* ASCII CP */
1087 /*********************************************************************
1088 * _mbsnbcmp(MSVCRT.@)
1090 int CDECL _mbsnbcmp(const unsigned char* str, const unsigned char* cmp, size_t len)
1092 if (!len)
1093 return 0;
1094 if(get_mbcinfo()->ismbcodepage)
1096 unsigned int strc, cmpc;
1097 while (len)
1099 int clen;
1100 if(!*str)
1101 return *cmp ? -1 : 0;
1102 if(!*cmp)
1103 return 1;
1104 if (_ismbblead(*str))
1106 strc=(len>=2)?_mbsnextc(str):0;
1107 clen=2;
1109 else
1111 strc=*str;
1112 clen=1;
1114 if (_ismbblead(*cmp))
1115 cmpc=(len>=2)?_mbsnextc(cmp):0;
1116 else
1117 cmpc=*cmp;
1118 if(strc != cmpc)
1119 return strc < cmpc ? -1 : 1;
1120 len -= clen;
1121 str += clen;
1122 cmp += clen;
1124 return 0; /* Matched len chars */
1126 return u_strncmp(str,cmp,len);
1129 /*********************************************************************
1130 * _mbsnicmp(MSVCRT.@)
1132 * Compare two multibyte strings case insensitively to 'len' characters.
1134 int CDECL _mbsnicmp(const unsigned char* str, const unsigned char* cmp, size_t len)
1136 /* FIXME: No tolower() for mb strings yet */
1137 if(get_mbcinfo()->ismbcodepage)
1139 unsigned int strc, cmpc;
1140 while(len--)
1142 if(!*str)
1143 return *cmp ? -1 : 0;
1144 if(!*cmp)
1145 return 1;
1146 strc = _mbctolower(_mbsnextc(str));
1147 cmpc = _mbctolower(_mbsnextc(cmp));
1148 if(strc != cmpc)
1149 return strc < cmpc ? -1 : 1;
1150 str +=(strc > 255) ? 2 : 1;
1151 cmp +=(strc > 255) ? 2 : 1; /* Equal, use same increment */
1153 return 0; /* Matched len chars */
1155 return u_strncasecmp(str, cmp, len); /* ASCII CP */
1158 /*********************************************************************
1159 * _mbsnbicmp(MSVCRT.@)
1161 int CDECL _mbsnbicmp(const unsigned char* str, const unsigned char* cmp, size_t len)
1163 if (!len)
1164 return 0;
1165 if(get_mbcinfo()->ismbcodepage)
1167 unsigned int strc, cmpc;
1168 while (len)
1170 int clen;
1171 if(!*str)
1172 return *cmp ? -1 : 0;
1173 if(!*cmp)
1174 return 1;
1175 if (_ismbblead(*str))
1177 strc=(len>=2)?_mbsnextc(str):0;
1178 clen=2;
1180 else
1182 strc=*str;
1183 clen=1;
1185 if (_ismbblead(*cmp))
1186 cmpc=(len>=2)?_mbsnextc(cmp):0;
1187 else
1188 cmpc=*cmp;
1189 strc = _mbctolower(strc);
1190 cmpc = _mbctolower(cmpc);
1191 if(strc != cmpc)
1192 return strc < cmpc ? -1 : 1;
1193 len -= clen;
1194 str += clen;
1195 cmp += clen;
1197 return 0; /* Matched len bytes */
1199 return u_strncasecmp(str,cmp,len);
1202 /*********************************************************************
1203 * _mbscat (MSVCRT.@)
1205 unsigned char * CDECL _mbscat( unsigned char *dst, const unsigned char *src )
1207 strcat( (char *)dst, (const char *)src );
1208 return dst;
1211 /*********************************************************************
1212 * _mbscat_s_l (MSVCRT.@)
1214 int CDECL _mbscat_s_l( unsigned char *dst, size_t size,
1215 const unsigned char *src, _locale_t locale )
1217 size_t i, j;
1218 int ret = 0;
1220 if(!MSVCRT_CHECK_PMT(dst != NULL)) return EINVAL;
1221 if(!MSVCRT_CHECK_PMT(src != NULL)) return EINVAL;
1223 for(i=0; i<size; i++)
1224 if(!dst[i]) break;
1225 if(i == size) {
1226 MSVCRT_INVALID_PMT("dst is not NULL-terminated", EINVAL);
1227 if(size) dst[0] = 0;
1228 return EINVAL;
1231 if(i && _ismbblead_l(dst[i-1], locale)) {
1232 ret = EILSEQ;
1233 i--;
1236 for(j=0; src[j] && i+j<size; j++)
1237 dst[i+j] = src[j];
1238 if(i+j == size) {
1239 MSVCRT_INVALID_PMT("dst buffer is too small", ERANGE);
1240 dst[0] = 0;
1241 return ERANGE;
1244 if(j && _ismbblead_l(src[j-1], locale)) {
1245 ret = EILSEQ;
1246 j--;
1249 dst[i+j] = 0;
1250 return ret;
1253 /*********************************************************************
1254 * _mbscat_s (MSVCRT.@)
1256 int CDECL _mbscat_s( unsigned char *dst, size_t size, const unsigned char *src )
1258 return _mbscat_s_l(dst, size, src, NULL);
1261 /*********************************************************************
1262 * _mbscpy (MSVCRT.@)
1264 unsigned char* CDECL _mbscpy( unsigned char *dst, const unsigned char *src )
1266 strcpy( (char *)dst, (const char *)src );
1267 return dst;
1270 /*********************************************************************
1271 * _mbsstr (MSVCRT.@)
1273 unsigned char * CDECL _mbsstr(const unsigned char *haystack, const unsigned char *needle)
1275 return (unsigned char *)strstr( (const char *)haystack, (const char *)needle );
1278 /*********************************************************************
1279 * _mbschr(MSVCRT.@)
1281 * Find a multibyte character in a multibyte string.
1283 unsigned char* CDECL _mbschr(const unsigned char* s, unsigned int x)
1285 if(get_mbcinfo()->ismbcodepage)
1287 unsigned int c;
1288 while (1)
1290 c = _mbsnextc(s);
1291 if (c == x)
1292 return (unsigned char*)s;
1293 if (!c)
1294 return NULL;
1295 s += c > 255 ? 2 : 1;
1298 return u_strchr(s, x); /* ASCII CP */
1301 /*********************************************************************
1302 * _mbsrchr(MSVCRT.@)
1304 unsigned char* CDECL _mbsrchr(const unsigned char* s, unsigned int x)
1306 if(get_mbcinfo()->ismbcodepage)
1308 unsigned int c;
1309 unsigned char* match=NULL;
1310 if(!s)
1311 return NULL;
1312 while (1) {
1313 c = _mbsnextc(s);
1314 if (c == x)
1315 match=(unsigned char*)s;
1316 if (!c)
1317 return match;
1318 s +=(c > 255) ? 2 : 1;
1321 return u_strrchr(s, x);
1324 /*********************************************************************
1325 * _mbstok_s_l(MSVCRT.@)
1327 unsigned char* CDECL _mbstok_s_l(unsigned char *str, const unsigned char *delim,
1328 unsigned char **ctx, _locale_t locale)
1330 pthreadmbcinfo mbcinfo;
1331 unsigned int c;
1333 if(!MSVCRT_CHECK_PMT(delim != NULL)) return NULL;
1334 if(!MSVCRT_CHECK_PMT(ctx != NULL)) return NULL;
1335 if(!MSVCRT_CHECK_PMT(str || *ctx)) return NULL;
1337 if(locale)
1338 mbcinfo = locale->mbcinfo;
1339 else
1340 mbcinfo = get_mbcinfo();
1342 if(!mbcinfo->ismbcodepage)
1343 return (unsigned char*)strtok_s((char*)str, (const char*)delim, (char**)ctx);
1345 if(!str)
1346 str = *ctx;
1348 while((c=_mbsnextc(str)) && _mbschr(delim, c))
1349 str += c>255 ? 2 : 1;
1350 if(!*str)
1352 *ctx = str;
1353 return NULL;
1356 *ctx = str + (c>255 ? 2 : 1);
1357 while((c=_mbsnextc(*ctx)) && !_mbschr(delim, c))
1358 *ctx += c>255 ? 2 : 1;
1359 if (**ctx) {
1360 *(*ctx)++ = 0;
1361 if(c > 255)
1362 *(*ctx)++ = 0;
1365 return str;
1369 /*********************************************************************
1370 * _mbstok_s(MSVCRT.@)
1372 unsigned char* CDECL _mbstok_s(unsigned char *str,
1373 const unsigned char *delim, unsigned char **ctx)
1375 return _mbstok_s_l(str, delim, ctx, NULL);
1378 /*********************************************************************
1379 * _mbstok_l(MSVCRT.@)
1381 unsigned char* CDECL _mbstok_l(unsigned char *str,
1382 const unsigned char *delim, _locale_t locale)
1384 return _mbstok_s_l(str, delim, &msvcrt_get_thread_data()->mbstok_next, locale);
1387 /*********************************************************************
1388 * _mbstok(MSVCRT.@)
1390 unsigned char* CDECL _mbstok(unsigned char *str, const unsigned char *delim)
1392 thread_data_t *data = msvcrt_get_thread_data();
1394 #if _MSVCR_VER == 0
1395 if(!str && !data->mbstok_next)
1396 return NULL;
1397 #endif
1399 return _mbstok_s_l(str, delim, &data->mbstok_next, NULL);
1402 /*********************************************************************
1403 * _mbbtombc(MSVCRT.@)
1405 unsigned int CDECL _mbbtombc(unsigned int c)
1407 if(get_mbcinfo()->mbcodepage == 932)
1409 if(c >= 0x20 && c <= 0x7e) {
1410 if((c >= 0x41 && c <= 0x5a) || (c >= 0x61 && c <= 0x7a) || (c >= 0x30 && c <= 0x39))
1411 return mbbtombc_932[c - 0x20] | 0x8200;
1412 else
1413 return mbbtombc_932[c - 0x20] | 0x8100;
1415 else if(c >= 0xa1 && c <= 0xdf) {
1416 if(c >= 0xa6 && c <= 0xdd && c != 0xb0)
1417 return mbbtombc_932[c - 0xa1 + 0x5f] | 0x8300;
1418 else
1419 return mbbtombc_932[c - 0xa1 + 0x5f] | 0x8100;
1422 return c; /* not Japanese or no MB char */
1425 /*********************************************************************
1426 * _ismbbkana_l(MSVCRT.@)
1428 int CDECL _ismbbkana_l(unsigned int c, _locale_t locale)
1430 pthreadmbcinfo mbcinfo;
1432 if(locale)
1433 mbcinfo = locale->mbcinfo;
1434 else
1435 mbcinfo = get_mbcinfo();
1437 if(mbcinfo->mbcodepage == 932)
1439 /* Japanese/Katakana, CP 932 */
1440 return (c >= 0xa1 && c <= 0xdf);
1442 return 0;
1445 /*********************************************************************
1446 * _ismbbkana(MSVCRT.@)
1448 int CDECL _ismbbkana(unsigned int c)
1450 return _ismbbkana_l( c, NULL );
1453 /*********************************************************************
1454 * _ismbcdigit_l(MSVCRT.@)
1456 int CDECL _ismbcdigit_l(unsigned int ch, _locale_t locale)
1458 return _iswdigit_l( msvcrt_mbc_to_wc_l(ch, locale), locale );
1461 /*********************************************************************
1462 * _ismbcdigit(MSVCRT.@)
1464 int CDECL _ismbcdigit(unsigned int ch)
1466 return _ismbcdigit_l( ch, NULL );
1469 /*********************************************************************
1470 * _ismbcgraph_l(MSVCRT.@)
1472 int CDECL _ismbcgraph_l(unsigned int ch, _locale_t locale)
1474 return _iswgraph_l( msvcrt_mbc_to_wc_l(ch, locale), locale );
1477 /*********************************************************************
1478 * _ismbcgraph(MSVCRT.@)
1480 int CDECL _ismbcgraph(unsigned int ch)
1482 return _ismbcgraph_l( ch, NULL );
1485 /*********************************************************************
1486 * _ismbcalpha_l (MSVCRT.@)
1488 int CDECL _ismbcalpha_l(unsigned int ch, _locale_t locale)
1490 return _iswalpha_l( msvcrt_mbc_to_wc_l(ch, locale), locale );
1493 /*********************************************************************
1494 * _ismbcalpha (MSVCRT.@)
1496 int CDECL _ismbcalpha(unsigned int ch)
1498 return _ismbcalpha_l( ch, NULL );
1501 /*********************************************************************
1502 * _ismbclower_l (MSVCRT.@)
1504 int CDECL _ismbclower_l(unsigned int ch, _locale_t locale)
1506 return _iswlower_l( msvcrt_mbc_to_wc_l(ch, locale), locale );
1509 /*********************************************************************
1510 * _ismbclower (MSVCRT.@)
1512 int CDECL _ismbclower(unsigned int ch)
1514 return _ismbclower_l( ch, NULL );
1517 /*********************************************************************
1518 * _ismbcupper_l (MSVCRT.@)
1520 int CDECL _ismbcupper_l(unsigned int ch, _locale_t locale)
1522 return _iswupper_l( msvcrt_mbc_to_wc_l(ch, locale), locale );
1525 /*********************************************************************
1526 * _ismbcupper (MSVCRT.@)
1528 int CDECL _ismbcupper(unsigned int ch)
1530 return _ismbcupper_l( ch, NULL );
1533 /*********************************************************************
1534 * _ismbcsymbol(MSVCRT.@)
1536 int CDECL _ismbcsymbol(unsigned int ch)
1538 wchar_t wch = msvcrt_mbc_to_wc( ch );
1539 WORD ctype;
1540 if (!GetStringTypeW(CT_CTYPE3, &wch, 1, &ctype))
1542 WARN("GetStringTypeW failed on %x\n", ch);
1543 return 0;
1545 return ((ctype & C3_SYMBOL) != 0);
1548 /*********************************************************************
1549 * _ismbcalnum_l (MSVCRT.@)
1551 int CDECL _ismbcalnum_l(unsigned int ch, _locale_t locale)
1553 return _iswalnum_l( msvcrt_mbc_to_wc_l(ch, locale), locale );
1556 /*********************************************************************
1557 * _ismbcalnum (MSVCRT.@)
1559 int CDECL _ismbcalnum(unsigned int ch)
1561 return _ismbcalnum_l( ch, NULL );
1564 /*********************************************************************
1565 * _ismbcspace_l (MSVCRT.@)
1567 int CDECL _ismbcspace_l(unsigned int ch, _locale_t locale)
1569 return _iswspace_l( msvcrt_mbc_to_wc_l(ch, locale), locale );
1572 /*********************************************************************
1573 * _ismbcspace (MSVCRT.@)
1575 int CDECL _ismbcspace(unsigned int ch)
1577 return _ismbcspace_l( ch, NULL );
1580 /*********************************************************************
1581 * _ismbcprint_l (MSVCRT.@)
1583 int CDECL _ismbcprint_l(unsigned int ch, _locale_t locale)
1585 return _iswprint_l( msvcrt_mbc_to_wc_l(ch, locale), locale );
1588 /*********************************************************************
1589 * _ismbcprint (MSVCRT.@)
1591 int CDECL _ismbcprint(unsigned int ch)
1593 return _ismbcprint_l( ch, NULL );
1596 /*********************************************************************
1597 * _ismbcpunct_l (MSVCRT.@)
1599 int CDECL _ismbcpunct_l(unsigned int ch, _locale_t locale)
1601 return _iswpunct_l( msvcrt_mbc_to_wc_l(ch, locale), locale );
1604 /*********************************************************************
1605 * _ismbcpunct(MSVCRT.@)
1607 int CDECL _ismbcpunct(unsigned int ch)
1609 return _ismbcpunct_l( ch, NULL );
1612 /*********************************************************************
1613 * _ismbchira(MSVCRT.@)
1615 int CDECL _ismbchira(unsigned int c)
1617 if(get_mbcinfo()->mbcodepage == 932)
1619 /* Japanese/Hiragana, CP 932 */
1620 return (c >= 0x829f && c <= 0x82f1);
1622 return 0;
1625 /*********************************************************************
1626 * _ismbckata(MSVCRT.@)
1628 int CDECL _ismbckata(unsigned int c)
1630 if(get_mbcinfo()->mbcodepage == 932)
1632 /* Japanese/Katakana, CP 932 */
1633 return (c >= 0x8340 && c <= 0x8396 && c != 0x837f);
1635 return 0;
1639 /*********************************************************************
1640 * _ismbblead_l(MSVCRT.@)
1642 int CDECL _ismbblead_l(unsigned int c, _locale_t locale)
1644 pthreadmbcinfo mbcinfo;
1646 if(!locale)
1647 mbcinfo = get_mbcinfo();
1648 else
1649 mbcinfo = locale->mbcinfo;
1651 return (mbcinfo->mbctype[(c&0xff) + 1] & _M1) != 0;
1654 /*********************************************************************
1655 * _ismbblead(MSVCRT.@)
1657 int CDECL _ismbblead(unsigned int c)
1659 return _ismbblead_l(c, NULL);
1662 /*********************************************************************
1663 * _ismbbtrail_l(MSVCRT.@)
1665 int CDECL _ismbbtrail_l(unsigned int c, _locale_t locale)
1667 pthreadmbcinfo mbcinfo;
1669 if(!locale)
1670 mbcinfo = get_mbcinfo();
1671 else
1672 mbcinfo = locale->mbcinfo;
1674 return (mbcinfo->mbctype[(c&0xff) + 1] & _M2) != 0;
1677 /*********************************************************************
1678 * _ismbbtrail(MSVCRT.@)
1680 int CDECL _ismbbtrail(unsigned int c)
1682 return _ismbbtrail_l(c, NULL);
1685 /*********************************************************************
1686 * _ismbclegal_l(MSVCRT.@)
1688 int CDECL _ismbclegal_l(unsigned int c, _locale_t locale)
1690 return _ismbblead_l(HIBYTE(c), locale) && _ismbbtrail_l(LOBYTE(c), locale);
1693 /*********************************************************************
1694 * _ismbclegal(MSVCRT.@)
1696 int CDECL _ismbclegal(unsigned int c)
1698 return _ismbclegal_l(c, NULL);
1701 /*********************************************************************
1702 * _ismbslead(MSVCRT.@)
1704 int CDECL _ismbslead(const unsigned char* start, const unsigned char* str)
1706 int lead = 0;
1708 if(!get_mbcinfo()->ismbcodepage)
1709 return 0;
1711 /* Lead bytes can also be trail bytes so we need to analyse the string
1713 while (start <= str)
1715 if (!*start)
1716 return 0;
1717 lead = !lead && _ismbblead(*start);
1718 start++;
1721 return lead ? -1 : 0;
1724 /*********************************************************************
1725 * _ismbstrail(MSVCRT.@)
1727 int CDECL _ismbstrail(const unsigned char* start, const unsigned char* str)
1729 /* Note: this function doesn't check _ismbbtrail */
1730 if ((str > start) && _ismbslead(start, str-1))
1731 return -1;
1732 else
1733 return 0;
1736 /*********************************************************************
1737 * _mbbtype_l(MSVCRT.@)
1739 int CDECL _mbbtype_l(unsigned char c, int type, _locale_t locale)
1741 if (type == 1)
1742 return _ismbbtrail_l(c, locale) ? _MBC_TRAIL : _MBC_ILLEGAL;
1743 else
1744 return _ismbblead_l(c, locale) ? _MBC_LEAD
1745 : _isprint_l(c, locale) ? _MBC_SINGLE : _MBC_ILLEGAL;
1748 /*********************************************************************
1749 * _mbbtype(MSVCRT.@)
1751 int CDECL _mbbtype(unsigned char c, int type)
1753 return _mbbtype_l(c, type, NULL);
1756 /*********************************************************************
1757 * _mbsbtype (MSVCRT.@)
1759 int CDECL _mbsbtype(const unsigned char *str, size_t count)
1761 int lead = 0;
1762 const unsigned char *end = str + count;
1764 /* Lead bytes can also be trail bytes so we need to analyse the string.
1765 * Also we must return _MBC_ILLEGAL for chars past the end of the string
1767 while (str < end) /* Note: we skip the last byte - will check after the loop */
1769 if (!*str)
1770 return _MBC_ILLEGAL;
1771 lead = get_mbcinfo()->ismbcodepage && !lead && _ismbblead(*str);
1772 str++;
1775 if (lead)
1776 if (_ismbbtrail(*str))
1777 return _MBC_TRAIL;
1778 else
1779 return _MBC_ILLEGAL;
1780 else
1781 if (_ismbblead(*str))
1782 return _MBC_LEAD;
1783 else
1784 return _MBC_SINGLE;
1787 /*********************************************************************
1788 * _mbsset(MSVCRT.@)
1790 unsigned char* CDECL _mbsset(unsigned char* str, unsigned int c)
1792 unsigned char* ret = str;
1794 if(!get_mbcinfo()->ismbcodepage || c < 256)
1795 return u__strset(str, c); /* ASCII CP or SB char */
1797 c &= 0xffff; /* Strip high bits */
1799 while(str[0] && str[1])
1801 *str++ = c >> 8;
1802 *str++ = c & 0xff;
1804 if(str[0])
1805 str[0] = '\0'; /* FIXME: OK to shorten? */
1807 return ret;
1810 /*********************************************************************
1811 * _mbsnbset(MSVCRT.@)
1813 unsigned char* CDECL _mbsnbset(unsigned char *str, unsigned int c, size_t len)
1815 unsigned char *ret = str;
1817 if(!len)
1818 return ret;
1820 if(!get_mbcinfo()->ismbcodepage || c < 256)
1821 return u__strnset(str, c, len); /* ASCII CP or SB char */
1823 c &= 0xffff; /* Strip high bits */
1825 while(str[0] && str[1] && (len > 1))
1827 *str++ = c >> 8;
1828 len--;
1829 *str++ = c & 0xff;
1830 len--;
1832 if(len && str[0]) {
1833 /* as per msdn pad with a blank character */
1834 str[0] = ' ';
1837 return ret;
1840 /*********************************************************************
1841 * _mbsnset(MSVCRT.@)
1843 unsigned char* CDECL _mbsnset(unsigned char* str, unsigned int c, size_t len)
1845 unsigned char *ret = str;
1847 if(!len)
1848 return ret;
1850 if(!get_mbcinfo()->ismbcodepage || c < 256)
1851 return u__strnset(str, c, len); /* ASCII CP or SB char */
1853 c &= 0xffff; /* Strip high bits */
1855 while(str[0] && str[1] && len--)
1857 *str++ = c >> 8;
1858 *str++ = c & 0xff;
1860 if(len && str[0])
1861 str[0] = '\0'; /* FIXME: OK to shorten? */
1863 return ret;
1866 /*********************************************************************
1867 * _mbsnccnt(MSVCRT.@)
1868 * 'c' is for 'character'.
1870 size_t CDECL _mbsnccnt(const unsigned char* str, size_t len)
1872 size_t ret;
1873 if(get_mbcinfo()->ismbcodepage)
1875 ret=0;
1876 while(*str && len-- > 0)
1878 if(_ismbblead(*str))
1880 if (!len)
1881 break;
1882 len--;
1883 str++;
1885 str++;
1886 ret++;
1888 return ret;
1890 ret=u_strlen(str);
1891 return min(ret, len); /* ASCII CP */
1894 /*********************************************************************
1895 * _mbsnbcnt(MSVCRT.@)
1896 * 'b' is for byte count.
1898 size_t CDECL _mbsnbcnt(const unsigned char* str, size_t len)
1900 size_t ret;
1901 if(get_mbcinfo()->ismbcodepage)
1903 const unsigned char* xstr = str;
1904 while(*xstr && len-- > 0)
1906 if (_ismbblead(*xstr++))
1907 xstr++;
1909 return xstr-str;
1911 ret=u_strlen(str);
1912 return min(ret, len); /* ASCII CP */
1915 /*********************************************************************
1916 * _mbsnbcat(MSVCRT.@)
1918 unsigned char* CDECL _mbsnbcat(unsigned char* dst, const unsigned char* src, size_t len)
1920 if(get_mbcinfo()->ismbcodepage)
1922 unsigned char *res = dst;
1923 while (*dst) {
1924 if (_ismbblead(*dst++)) {
1925 if (*dst) {
1926 dst++;
1927 } else {
1928 /* as per msdn overwrite the lead byte in front of '\0' */
1929 dst--;
1930 break;
1934 while (*src && len--) *dst++ = *src++;
1935 *dst = '\0';
1936 return res;
1938 return u_strncat(dst, src, len); /* ASCII CP */
1941 int CDECL _mbsnbcat_s(unsigned char *dst, size_t size, const unsigned char *src, size_t len)
1943 unsigned char *ptr = dst;
1944 size_t i;
1946 if (!dst && !size && !src && !len)
1947 return 0;
1949 if (!dst || !size || !src)
1951 if (dst && size)
1952 *dst = '\0';
1954 *_errno() = EINVAL;
1955 return EINVAL;
1958 /* Find the null terminator of the destination buffer. */
1959 while (size && *ptr)
1960 size--, ptr++;
1962 if (!size)
1964 *dst = '\0';
1965 *_errno() = EINVAL;
1966 return EINVAL;
1969 /* If necessary, check that the character preceding the null terminator is
1970 * a lead byte and move the pointer back by one for later overwrite. */
1971 if (ptr != dst && get_mbcinfo()->ismbcodepage && _ismbblead(*(ptr - 1)))
1972 size++, ptr--;
1974 for (i = 0; *src && i < len; i++)
1976 *ptr++ = *src++;
1977 size--;
1979 if (!size)
1981 *dst = '\0';
1982 *_errno() = ERANGE;
1983 return ERANGE;
1987 *ptr = '\0';
1988 return 0;
1991 /*********************************************************************
1992 * _mbsncat(MSVCRT.@)
1994 unsigned char* CDECL _mbsncat(unsigned char* dst, const unsigned char* src, size_t len)
1996 if(get_mbcinfo()->ismbcodepage)
1998 unsigned char *res = dst;
1999 while (*dst)
2001 if (_ismbblead(*dst++))
2002 dst++;
2004 while (*src && len--)
2006 *dst++ = *src;
2007 if(_ismbblead(*src++))
2008 *dst++ = *src++;
2010 *dst = '\0';
2011 return res;
2013 return u_strncat(dst, src, len); /* ASCII CP */
2017 /*********************************************************************
2018 * _mbslwr(MSVCRT.@)
2020 unsigned char* CDECL _mbslwr(unsigned char* s)
2022 unsigned char *ret = s;
2023 if (!s)
2024 return NULL;
2025 if (get_mbcinfo()->ismbcodepage)
2027 unsigned int c;
2028 while (*s)
2030 c = _mbctolower(_mbsnextc(s));
2031 /* Note that I assume that the size of the character is unchanged */
2032 if (c > 255)
2034 *s++=(c>>8);
2035 c=c & 0xff;
2037 *s++=c;
2040 else for ( ; *s; s++) *s = _tolower_l(*s, NULL);
2041 return ret;
2045 /*********************************************************************
2046 * _mbslwr_s(MSVCRT.@)
2048 int CDECL _mbslwr_s(unsigned char* s, size_t len)
2050 if (!s && !len)
2052 return 0;
2054 else if (!s || !len)
2056 *_errno() = EINVAL;
2057 return EINVAL;
2059 if (get_mbcinfo()->ismbcodepage)
2061 unsigned int c;
2062 for ( ; *s && len > 0; len--)
2064 c = _mbctolower(_mbsnextc(s));
2065 /* Note that I assume that the size of the character is unchanged */
2066 if (c > 255)
2068 *s++=(c>>8);
2069 c=c & 0xff;
2071 *s++=c;
2074 else for ( ; *s && len > 0; s++, len--) *s = _tolower_l(*s, NULL);
2075 if (*s)
2077 *s = '\0';
2078 *_errno() = EINVAL;
2079 return EINVAL;
2081 return 0;
2085 /*********************************************************************
2086 * _mbsupr(MSVCRT.@)
2088 unsigned char* CDECL _mbsupr(unsigned char* s)
2090 unsigned char *ret = s;
2091 if (!s)
2092 return NULL;
2093 if (get_mbcinfo()->ismbcodepage)
2095 unsigned int c;
2096 while (*s)
2098 c = _mbctoupper(_mbsnextc(s));
2099 /* Note that I assume that the size of the character is unchanged */
2100 if (c > 255)
2102 *s++=(c>>8);
2103 c=c & 0xff;
2105 *s++=c;
2108 else for ( ; *s; s++) *s = _toupper_l(*s, NULL);
2109 return ret;
2113 /*********************************************************************
2114 * _mbsupr_s(MSVCRT.@)
2116 int CDECL _mbsupr_s(unsigned char* s, size_t len)
2118 if (!s && !len)
2120 return 0;
2122 else if (!s || !len)
2124 *_errno() = EINVAL;
2125 return EINVAL;
2127 if (get_mbcinfo()->ismbcodepage)
2129 unsigned int c;
2130 for ( ; *s && len > 0; len--)
2132 c = _mbctoupper(_mbsnextc(s));
2133 /* Note that I assume that the size of the character is unchanged */
2134 if (c > 255)
2136 *s++=(c>>8);
2137 c=c & 0xff;
2139 *s++=c;
2142 else for ( ; *s && len > 0; s++, len--) *s = _toupper_l(*s, NULL);
2143 if (*s)
2145 *s = '\0';
2146 *_errno() = EINVAL;
2147 return EINVAL;
2149 return 0;
2152 /*********************************************************************
2153 * _mbsspn_l (MSVCRT.@)
2155 size_t CDECL _mbsspn_l(const unsigned char* string,
2156 const unsigned char* set, _locale_t locale)
2158 const unsigned char *p, *q;
2160 for (p = string; *p; p++)
2162 for (q = set; *q; q++)
2164 if (_ismbblead_l(*q, locale))
2166 /* duplicate a bug in native implementation */
2167 if (!q[1]) break;
2169 if (p[0] == q[0] && p[1] == q[1])
2171 p++;
2172 break;
2174 q++;
2176 else
2178 if (p[0] == q[0]) break;
2181 if (!*q) break;
2183 return p - string;
2186 /*********************************************************************
2187 * _mbsspn (MSVCRT.@)
2189 size_t CDECL _mbsspn(const unsigned char* string, const unsigned char* set)
2191 return _mbsspn_l(string, set, NULL);
2194 /*********************************************************************
2195 * _mbsspnp (MSVCRT.@)
2197 unsigned char* CDECL _mbsspnp(const unsigned char* string, const unsigned char* set)
2199 string += _mbsspn( string, set );
2200 return *string ? (unsigned char*)string : NULL;
2203 /*********************************************************************
2204 * _mbscspn_l (MSVCRT.@)
2206 size_t CDECL _mbscspn_l(const unsigned char* str,
2207 const unsigned char* cmp, _locale_t locale)
2209 const unsigned char *p, *q;
2211 for (p = str; *p; p++)
2213 for (q = cmp; *q; q++)
2215 if (_ismbblead_l(*q, locale))
2217 /* duplicate a bug in native implementation */
2218 if (!q[1]) return 0;
2220 if (p[0] == q[0] && p[1] == q[1])
2221 return p - str;
2222 q++;
2224 else if (p[0] == q[0])
2225 return p - str;
2228 return p - str;
2231 /*********************************************************************
2232 * _mbscspn (MSVCRT.@)
2234 size_t CDECL _mbscspn(const unsigned char* str, const unsigned char* cmp)
2236 return _mbscspn_l(str, cmp, NULL);
2239 /*********************************************************************
2240 * _mbsrev (MSVCRT.@)
2242 unsigned char* CDECL _mbsrev(unsigned char* str)
2244 int i, len = _mbslen(str);
2245 unsigned char *p, *temp=malloc(len*2);
2247 if(!temp)
2248 return str;
2250 /* unpack multibyte string to temp buffer */
2251 p=str;
2252 for(i=0; i<len; i++)
2254 if (_ismbblead(*p))
2256 temp[i*2]=*p++;
2257 temp[i*2+1]=*p++;
2259 else
2261 temp[i*2]=*p++;
2262 temp[i*2+1]=0;
2266 /* repack it in the reverse order */
2267 p=str;
2268 for(i=len-1; i>=0; i--)
2270 if(_ismbblead(temp[i*2]))
2272 *p++=temp[i*2];
2273 *p++=temp[i*2+1];
2275 else
2277 *p++=temp[i*2];
2281 free(temp);
2283 return str;
2286 /*********************************************************************
2287 * _mbspbrk (MSVCRT.@)
2289 unsigned char* CDECL _mbspbrk(const unsigned char* str, const unsigned char* accept)
2291 const unsigned char* p;
2293 while(*str)
2295 for(p = accept; *p; p += (_ismbblead(*p)?2:1) )
2297 if (*p == *str)
2298 if( !_ismbblead(*p) || ( *(p+1) == *(str+1) ) )
2299 return (unsigned char*)str;
2301 str += (_ismbblead(*str)?2:1);
2303 return NULL;
2308 * Functions depending on locale codepage
2311 /*********************************************************************
2312 * mblen(MSVCRT.@)
2313 * REMARKS
2314 * Unlike most of the multibyte string functions this function uses
2315 * the locale codepage, not the codepage set by _setmbcp
2317 int CDECL mblen(const char* str, size_t size)
2319 if (str && *str && size)
2321 if(get_locinfo()->mb_cur_max == 1)
2322 return 1; /* ASCII CP */
2324 return !isleadbyte((unsigned char)*str) ? 1 : (size>1 ? 2 : -1);
2326 return 0;
2329 /*********************************************************************
2330 * mbrlen(MSVCRT.@)
2332 size_t CDECL mbrlen(const char *str, size_t len, mbstate_t *state)
2334 mbstate_t s = (state ? *state : 0);
2335 size_t ret;
2337 if(!len || !str || !*str)
2338 return 0;
2340 if(get_locinfo()->mb_cur_max == 1) {
2341 return 1;
2342 }else if(!s && isleadbyte((unsigned char)*str)) {
2343 if(len == 1) {
2344 s = (unsigned char)*str;
2345 ret = -2;
2346 }else {
2347 ret = 2;
2349 }else if(!s) {
2350 ret = 1;
2351 }else {
2352 s = 0;
2353 ret = 2;
2356 if(state)
2357 *state = s;
2358 return ret;
2361 /*********************************************************************
2362 * _mbstrlen_l(MSVCRT.@)
2364 size_t CDECL _mbstrlen_l(const char* str, _locale_t locale)
2366 pthreadlocinfo locinfo;
2368 if(!locale)
2369 locinfo = get_locinfo();
2370 else
2371 locinfo = locale->locinfo;
2373 if(locinfo->mb_cur_max > 1) {
2374 size_t len;
2375 len = MultiByteToWideChar(locinfo->lc_codepage, MB_ERR_INVALID_CHARS,
2376 str, -1, NULL, 0);
2377 if (!len) {
2378 *_errno() = EILSEQ;
2379 return -1;
2381 return len - 1;
2384 return strlen(str);
2387 /*********************************************************************
2388 * _mbstrlen(MSVCRT.@)
2390 size_t CDECL _mbstrlen(const char* str)
2392 return _mbstrlen_l(str, NULL);
2395 /*********************************************************************
2396 * _mbtowc_l(MSVCRT.@)
2398 int CDECL _mbtowc_l(wchar_t *dst, const char* str, size_t n, _locale_t locale)
2400 pthreadlocinfo locinfo;
2401 wchar_t tmpdst;
2403 if(!locale)
2404 locinfo = get_locinfo();
2405 else
2406 locinfo = locale->locinfo;
2408 if(n <= 0 || !str)
2409 return 0;
2411 if(!*str) {
2412 if(dst) *dst = 0;
2413 return 0;
2416 if(!locinfo->lc_codepage) {
2417 if(dst) *dst = (unsigned char)*str;
2418 return 1;
2420 if(n>=2 && _isleadbyte_l((unsigned char)*str, locale)) {
2421 if(!MultiByteToWideChar(locinfo->lc_codepage, 0, str, 2, &tmpdst, 1))
2422 return -1;
2423 if(dst) *dst = tmpdst;
2424 return 2;
2426 if(!MultiByteToWideChar(locinfo->lc_codepage, 0, str, 1, &tmpdst, 1))
2427 return -1;
2428 if(dst) *dst = tmpdst;
2429 return 1;
2432 /*********************************************************************
2433 * mbtowc(MSVCRT.@)
2435 int CDECL mbtowc(wchar_t *dst, const char* str, size_t n)
2437 return _mbtowc_l(dst, str, n, NULL);
2440 /*********************************************************************
2441 * btowc(MSVCRT.@)
2443 wint_t CDECL btowc(int c)
2445 unsigned char letter = c;
2446 wchar_t ret;
2448 if(c == EOF)
2449 return WEOF;
2450 if(!get_locinfo()->lc_codepage)
2451 return c & 255;
2452 if(!MultiByteToWideChar(get_locinfo()->lc_codepage,
2453 MB_ERR_INVALID_CHARS, (LPCSTR)&letter, 1, &ret, 1))
2454 return WEOF;
2456 return ret;
2459 /*********************************************************************
2460 * mbrtowc(MSVCRT.@)
2462 size_t CDECL mbrtowc(wchar_t *dst, const char *str,
2463 size_t n, mbstate_t *state)
2465 pthreadlocinfo locinfo = get_locinfo();
2466 mbstate_t s = (state ? *state : 0);
2467 char tmpstr[2];
2468 int len = 0;
2470 if(dst)
2471 *dst = 0;
2473 if(!n || !str || !*str)
2474 return 0;
2476 if(locinfo->mb_cur_max == 1) {
2477 tmpstr[len++] = *str;
2478 }else if(!s && isleadbyte((unsigned char)*str)) {
2479 if(n == 1) {
2480 s = (unsigned char)*str;
2481 len = -2;
2482 }else {
2483 tmpstr[0] = str[0];
2484 tmpstr[1] = str[1];
2485 len = 2;
2487 }else if(!s) {
2488 tmpstr[len++] = *str;
2489 }else {
2490 tmpstr[0] = s;
2491 tmpstr[1] = *str;
2492 len = 2;
2493 s = 0;
2496 if(len > 0) {
2497 if(!MultiByteToWideChar(locinfo->lc_codepage, 0, tmpstr, len, dst, dst ? 1 : 0))
2498 len = -1;
2501 if(state)
2502 *state = s;
2503 return len;
2506 /*********************************************************************
2507 * _mbstowcs_l(MSVCRT.@)
2509 size_t CDECL _mbstowcs_l(wchar_t *wcstr, const char *mbstr,
2510 size_t count, _locale_t locale)
2512 pthreadlocinfo locinfo;
2513 size_t i, size;
2515 if(!mbstr) {
2516 *_errno() = EINVAL;
2517 return -1;
2520 if(!locale)
2521 locinfo = get_locinfo();
2522 else
2523 locinfo = locale->locinfo;
2525 if(!locinfo->lc_codepage) {
2526 if(!wcstr)
2527 return strlen(mbstr);
2529 for(i=0; i<count; i++) {
2530 wcstr[i] = (unsigned char)mbstr[i];
2531 if(!wcstr[i]) break;
2533 return i;
2536 /* Ignore count parameter */
2537 if(!wcstr) {
2538 size = MultiByteToWideChar(locinfo->lc_codepage,
2539 MB_ERR_INVALID_CHARS, mbstr, -1, NULL, 0);
2540 if(!size) {
2541 *_errno() = EILSEQ;
2542 return -1;
2544 return size - 1;
2547 for(i=0, size=0; i<count; i++) {
2548 if(mbstr[size] == '\0')
2549 break;
2551 size += (_isleadbyte_l((unsigned char)mbstr[size], locale) ? 2 : 1);
2554 if(size) {
2555 size = MultiByteToWideChar(locinfo->lc_codepage,
2556 MB_ERR_INVALID_CHARS, mbstr, size, wcstr, count);
2557 if(!size) {
2558 if(count) wcstr[0] = '\0';
2559 *_errno() = EILSEQ;
2560 return -1;
2564 if(size<count)
2565 wcstr[size] = '\0';
2567 return size;
2570 /*********************************************************************
2571 * mbstowcs(MSVCRT.@)
2573 size_t CDECL mbstowcs(wchar_t *wcstr,
2574 const char *mbstr, size_t count)
2576 return _mbstowcs_l(wcstr, mbstr, count, NULL);
2579 /*********************************************************************
2580 * _mbstowcs_s_l(MSVCRT.@)
2582 int CDECL _mbstowcs_s_l(size_t *ret, wchar_t *wcstr,
2583 size_t size, const char *mbstr, size_t count, _locale_t locale)
2585 size_t conv;
2586 int err = 0;
2588 if(!wcstr && !size) {
2589 conv = _mbstowcs_l(NULL, mbstr, 0, locale);
2590 if(ret)
2591 *ret = conv+1;
2592 return 0;
2595 if (!MSVCRT_CHECK_PMT(wcstr != NULL)) return EINVAL;
2596 if (!MSVCRT_CHECK_PMT(mbstr != NULL)) {
2597 if(size) wcstr[0] = '\0';
2598 return EINVAL;
2601 if(count==_TRUNCATE || size<count)
2602 conv = size;
2603 else
2604 conv = count;
2606 conv = _mbstowcs_l(wcstr, mbstr, conv, locale);
2607 if(conv<size)
2608 wcstr[conv++] = '\0';
2609 else if(conv==size && count==_TRUNCATE && wcstr[conv-1]!='\0') {
2610 wcstr[conv-1] = '\0';
2611 err = STRUNCATE;
2612 }else if(conv==size && wcstr[conv-1]!='\0') {
2613 MSVCRT_INVALID_PMT("wcstr[size] is too small", ERANGE);
2614 if(size)
2615 wcstr[0] = '\0';
2616 return ERANGE;
2619 if(ret)
2620 *ret = conv;
2621 return err;
2624 /*********************************************************************
2625 * mbstowcs_s(MSVCRT.@)
2627 int CDECL _mbstowcs_s(size_t *ret, wchar_t *wcstr,
2628 size_t size, const char *mbstr, size_t count)
2630 return _mbstowcs_s_l(ret, wcstr, size, mbstr, count, NULL);
2633 /*********************************************************************
2634 * mbsrtowcs(MSVCRT.@)
2636 size_t CDECL mbsrtowcs(wchar_t *wcstr,
2637 const char **pmbstr, size_t count, mbstate_t *state)
2639 mbstate_t s = (state ? *state : 0);
2640 wchar_t tmpdst;
2641 size_t ret = 0;
2642 const char *p;
2644 if(!MSVCRT_CHECK_PMT(pmbstr != NULL))
2645 return -1;
2647 p = *pmbstr;
2648 while(!wcstr || count>ret) {
2649 int ch_len = mbrtowc(&tmpdst, p, 2, &s);
2650 if(wcstr)
2651 wcstr[ret] = tmpdst;
2653 if(ch_len < 0) {
2654 return -1;
2655 }else if(ch_len == 0) {
2656 if(wcstr) *pmbstr = NULL;
2657 return ret;
2660 p += ch_len;
2661 ret++;
2664 if(wcstr) *pmbstr = p;
2665 return ret;
2668 /*********************************************************************
2669 * mbsrtowcs_s(MSVCRT.@)
2671 int CDECL mbsrtowcs_s(size_t *ret, wchar_t *wcstr, size_t len,
2672 const char **mbstr, size_t count, mbstate_t *state)
2674 size_t tmp;
2676 if(!ret) ret = &tmp;
2677 if(!MSVCRT_CHECK_PMT(!!wcstr == !!len)) {
2678 *ret = -1;
2679 return EINVAL;
2682 *ret = mbsrtowcs(wcstr, mbstr, count>len ? len : count, state);
2683 if(*ret == -1) {
2684 if(wcstr) *wcstr = 0;
2685 return *_errno();
2687 (*ret)++;
2688 if(*ret > len) {
2689 /* no place for terminating '\0' */
2690 if(wcstr) *wcstr = 0;
2691 return 0;
2693 if(wcstr) wcstr[(*ret)-1] = 0;
2694 return 0;
2697 /*********************************************************************
2698 * _mbctohira (MSVCRT.@)
2700 * Converts a sjis katakana character to hiragana.
2702 unsigned int CDECL _mbctohira(unsigned int c)
2704 if(_ismbckata(c) && c <= 0x8393)
2705 return (c - 0x8340 - (c >= 0x837f ? 1 : 0)) + 0x829f;
2706 return c;
2709 /*********************************************************************
2710 * _mbctokata (MSVCRT.@)
2712 * Converts a sjis hiragana character to katakana.
2714 unsigned int CDECL _mbctokata(unsigned int c)
2716 if(_ismbchira(c))
2717 return (c - 0x829f) + 0x8340 + (c >= 0x82de ? 1 : 0);
2718 return c;
2721 /*********************************************************************
2722 * _ismbcl0_l (MSVCRT.@)
2724 int CDECL _ismbcl0_l(unsigned int c, _locale_t locale)
2726 pthreadmbcinfo mbcinfo;
2728 if(!locale)
2729 mbcinfo = get_mbcinfo();
2730 else
2731 mbcinfo = locale->mbcinfo;
2733 if(mbcinfo->mbcodepage == 932)
2735 /* JIS non-Kanji */
2736 return _ismbclegal_l(c, locale) && c >= 0x8140 && c <= 0x889e;
2739 return 0;
2742 /*********************************************************************
2743 * _ismbcl0 (MSVCRT.@)
2745 int CDECL _ismbcl0(unsigned int c)
2747 return _ismbcl0_l(c, NULL);
2750 /*********************************************************************
2751 * _ismbcl1_l (MSVCRT.@)
2753 int CDECL _ismbcl1_l(unsigned int c, _locale_t locale)
2755 pthreadmbcinfo mbcinfo;
2757 if(!locale)
2758 mbcinfo = get_mbcinfo();
2759 else
2760 mbcinfo = locale->mbcinfo;
2762 if(mbcinfo->mbcodepage == 932)
2764 /* JIS level-1 */
2765 return _ismbclegal_l(c, locale) && c >= 0x889f && c <= 0x9872;
2768 return 0;
2771 /*********************************************************************
2772 * _ismbcl1 (MSVCRT.@)
2774 int CDECL _ismbcl1(unsigned int c)
2776 return _ismbcl1_l(c, NULL);
2779 /*********************************************************************
2780 * _ismbcl2_l (MSVCRT.@)
2782 int CDECL _ismbcl2_l(unsigned int c, _locale_t locale)
2784 pthreadmbcinfo mbcinfo;
2786 if(!locale)
2787 mbcinfo = get_mbcinfo();
2788 else
2789 mbcinfo = locale->mbcinfo;
2791 if(mbcinfo->mbcodepage == 932)
2793 /* JIS level-2 */
2794 return _ismbclegal_l(c, locale) && c >= 0x989f && c <= 0xeaa4;
2797 return 0;
2800 /*********************************************************************
2801 * _ismbcl2 (MSVCRT.@)
2803 int CDECL _ismbcl2(unsigned int c)
2805 return _ismbcl2_l(c, NULL);