riched20: Use cell ptrs in the rtf parsing code.
[wine/zf.git] / dlls / msvcrt / mbcs.c
blob13fffc5734ae4acc87f7b73fcde82974ca13dae1
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 "winnls.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_l(unsigned int ch, MSVCRT__locale_t locale)
89 MSVCRT_pthreadmbcinfo mbcinfo;
90 MSVCRT_wchar_t chW;
91 char mbch[2];
92 int n_chars;
94 if(locale)
95 mbcinfo = locale->mbcinfo;
96 else
97 mbcinfo = get_mbcinfo();
99 if (ch <= 0xff) {
100 mbch[0] = ch;
101 n_chars = 1;
102 } else {
103 mbch[0] = (ch >> 8) & 0xff;
104 mbch[1] = ch & 0xff;
105 n_chars = 2;
107 if (!MultiByteToWideChar(mbcinfo->mbcodepage, 0, mbch, n_chars, &chW, 1))
109 WARN("MultiByteToWideChar failed on %x\n", ch);
110 return 0;
112 return chW;
115 static MSVCRT_wchar_t msvcrt_mbc_to_wc(unsigned int ch)
117 return msvcrt_mbc_to_wc_l(ch, NULL);
120 static inline MSVCRT_size_t u_strlen( const unsigned char *str )
122 return strlen( (const char*) str );
125 static inline unsigned char* u_strncat( unsigned char* dst, const unsigned char* src, MSVCRT_size_t len )
127 return (unsigned char*)strncat( (char*)dst, (const char*)src, len);
130 static inline int u_strcmp( const unsigned char *s1, const unsigned char *s2 )
132 return MSVCRT_strcmp( (const char*)s1, (const char*)s2 );
135 static inline int u_strcasecmp( const unsigned char *s1, const unsigned char *s2 )
137 return MSVCRT__stricmp( (const char*)s1, (const char*)s2 );
140 static inline int u_strncmp( const unsigned char *s1, const unsigned char *s2, MSVCRT_size_t len )
142 return MSVCRT_strncmp( (const char*)s1, (const char*)s2, len );
145 static inline int u_strncasecmp( const unsigned char *s1, const unsigned char *s2, MSVCRT_size_t len )
147 return MSVCRT__strnicmp( (const char*)s1, (const char*)s2, len );
150 static inline unsigned char *u_strchr( const unsigned char *s, unsigned char x )
152 return (unsigned char*) strchr( (const char*)s, x );
155 static inline unsigned char *u_strrchr( const unsigned char *s, unsigned char x )
157 return (unsigned char*) strrchr( (const char*)s, x );
160 static inline unsigned char *u__strset( unsigned char *s, unsigned char c )
162 return (unsigned char*) _strset( (char*)s, c);
165 static inline unsigned char *u__strnset( unsigned char *s, unsigned char c, MSVCRT_size_t len )
167 return (unsigned char*) MSVCRT__strnset( (char*)s, c, len );
170 /*********************************************************************
171 * __p__mbctype (MSVCRT.@)
173 unsigned char* CDECL __p__mbctype(void)
175 return get_mbcinfo()->mbctype;
178 /*********************************************************************
179 * __p___mb_cur_max(MSVCRT.@)
181 int* CDECL __p___mb_cur_max(void)
183 return &get_locinfo()->mb_cur_max;
186 /*********************************************************************
187 * ___mb_cur_max_func(MSVCRT.@)
189 int CDECL MSVCRT____mb_cur_max_func(void)
191 return get_locinfo()->mb_cur_max;
194 #if _MSVCR_VER>=80
195 /*********************************************************************
196 * ___mb_cur_max_l_func (MSVCR80.@)
198 int CDECL ___mb_cur_max_l_func(MSVCRT__locale_t locale)
200 MSVCRT_pthreadlocinfo locinfo;
202 if(!locale)
203 locinfo = get_locinfo();
204 else
205 locinfo = locale->locinfo;
207 return locinfo->mb_cur_max;
209 #endif
211 /*********************************************************************
212 * INTERNAL: _setmbcp_l
214 int _setmbcp_l(int cp, LCID lcid, MSVCRT_pthreadmbcinfo mbcinfo)
216 const char format[] = ".%d";
218 int newcp;
219 CPINFO cpi;
220 BYTE *bytes;
221 WORD chartypes[256];
222 char bufA[256];
223 WCHAR bufW[256], lowW[256], upW[256];
224 int charcount;
225 int ret;
226 int i;
228 if(!mbcinfo)
229 mbcinfo = get_mbcinfo();
231 switch (cp)
233 case _MB_CP_ANSI:
234 newcp = GetACP();
235 break;
236 case _MB_CP_OEM:
237 newcp = GetOEMCP();
238 break;
239 case _MB_CP_LOCALE:
240 newcp = get_locinfo()->lc_codepage;
241 if(newcp)
242 break;
243 /* fall through (C locale) */
244 case _MB_CP_SBCS:
245 newcp = 20127; /* ASCII */
246 break;
247 default:
248 newcp = cp;
249 break;
252 if(lcid == -1) {
253 MSVCRT_sprintf(bufA, format, newcp);
254 mbcinfo->mblcid = MSVCRT_locale_to_LCID(bufA, NULL, NULL);
255 } else {
256 mbcinfo->mblcid = lcid;
259 if(mbcinfo->mblcid == -1)
261 WARN("Can't assign LCID to codepage (%d)\n", mbcinfo->mblcid);
262 mbcinfo->mblcid = 0;
265 if (!GetCPInfo(newcp, &cpi))
267 WARN("Codepage %d not found\n", newcp);
268 *MSVCRT__errno() = MSVCRT_EINVAL;
269 return -1;
272 /* setup the _mbctype */
273 memset(mbcinfo->mbctype, 0, sizeof(unsigned char[257]));
274 memset(mbcinfo->mbcasemap, 0, sizeof(unsigned char[256]));
276 bytes = cpi.LeadByte;
277 while (bytes[0] || bytes[1])
279 for (i = bytes[0]; i <= bytes[1]; i++)
280 mbcinfo->mbctype[i + 1] |= _M1;
281 bytes += 2;
284 if (cpi.MaxCharSize > 1)
286 /* trail bytes not available through kernel32 but stored in a structure in msvcrt */
287 struct cp_extra_info_t *cpextra = g_cpextrainfo;
289 mbcinfo->ismbcodepage = 1;
290 while (TRUE)
292 if (cpextra->cp == 0 || cpextra->cp == newcp)
294 if (cpextra->cp == 0)
295 FIXME("trail bytes data not available for DBCS codepage %d - assuming all bytes\n", newcp);
297 bytes = cpextra->TrailBytes;
298 while (bytes[0] || bytes[1])
300 for (i = bytes[0]; i <= bytes[1]; i++)
301 mbcinfo->mbctype[i + 1] |= _M2;
302 bytes += 2;
304 break;
306 cpextra++;
309 else
310 mbcinfo->ismbcodepage = 0;
312 /* we can't use GetStringTypeA directly because we don't have a locale - only a code page
314 charcount = 0;
315 for (i = 0; i < 256; i++)
316 if (!(mbcinfo->mbctype[i + 1] & _M1))
317 bufA[charcount++] = i;
319 ret = MultiByteToWideChar(newcp, 0, bufA, charcount, bufW, charcount);
320 if (ret != charcount)
321 ERR("MultiByteToWideChar of chars failed for cp %d, ret=%d (exp %d), error=%d\n", newcp, ret, charcount, GetLastError());
323 GetStringTypeW(CT_CTYPE1, bufW, charcount, chartypes);
324 LCMapStringW(lcid, LCMAP_LOWERCASE, bufW, charcount, lowW, charcount);
325 LCMapStringW(lcid, LCMAP_UPPERCASE, bufW, charcount, upW, charcount);
327 charcount = 0;
328 for (i = 0; i < 256; i++)
329 if (!(mbcinfo->mbctype[i + 1] & _M1))
331 if (chartypes[charcount] & C1_UPPER)
333 mbcinfo->mbctype[i + 1] |= _SBUP;
334 bufW[charcount] = lowW[charcount];
336 else if (chartypes[charcount] & C1_LOWER)
338 mbcinfo->mbctype[i + 1] |= _SBLOW;
339 bufW[charcount] = upW[charcount];
341 charcount++;
344 ret = WideCharToMultiByte(newcp, 0, bufW, charcount, bufA, charcount, NULL, NULL);
345 if (ret != charcount)
346 ERR("WideCharToMultiByte failed for cp %d, ret=%d (exp %d), error=%d\n", newcp, ret, charcount, GetLastError());
348 charcount = 0;
349 for (i = 0; i < 256; i++)
351 if(!(mbcinfo->mbctype[i + 1] & _M1))
353 if(mbcinfo->mbctype[i] & (C1_UPPER|C1_LOWER))
354 mbcinfo->mbcasemap[i] = bufA[charcount];
355 charcount++;
359 if (newcp == 932) /* CP932 only - set _MP and _MS */
361 /* On Windows it's possible to calculate the _MP and _MS from CT_CTYPE1
362 * and CT_CTYPE3. But as of Wine 0.9.43 we return wrong values what makes
363 * it hard. As this is set only for codepage 932 we hardcode it what gives
364 * also faster execution.
366 for (i = 161; i <= 165; i++)
367 mbcinfo->mbctype[i + 1] |= _MP;
368 for (i = 166; i <= 223; i++)
369 mbcinfo->mbctype[i + 1] |= _MS;
372 mbcinfo->mbcodepage = newcp;
373 if(MSVCRT_locale && mbcinfo == MSVCRT_locale->mbcinfo)
374 memcpy(MSVCRT_mbctype, MSVCRT_locale->mbcinfo->mbctype, sizeof(MSVCRT_mbctype));
376 return 0;
379 /*********************************************************************
380 * _setmbcp (MSVCRT.@)
382 int CDECL _setmbcp(int cp)
384 return _setmbcp_l(cp, -1, NULL);
387 /*********************************************************************
388 * _getmbcp (MSVCRT.@)
390 int CDECL _getmbcp(void)
392 return get_mbcinfo()->mbcodepage;
395 /*********************************************************************
396 * _mbsnextc_l(MSVCRT.@)
398 unsigned int CDECL _mbsnextc_l(const unsigned char* str, MSVCRT__locale_t locale)
400 if(_ismbblead_l(*str, locale))
401 return *str << 8 | str[1];
402 return *str;
405 /*********************************************************************
406 * _mbsnextc(MSVCRT.@)
408 unsigned int CDECL _mbsnextc(const unsigned char* str)
410 return _mbsnextc_l(str, NULL);
413 /*********************************************************************
414 * _mbctolower(MSVCRT.@)
416 unsigned int CDECL _mbctolower(unsigned int c)
418 if (_ismbblead(c))
420 FIXME("Handle MBC chars\n");
421 return c;
423 return MSVCRT__tolower_l(c, NULL); /* ASCII CP or SB char */
426 /*********************************************************************
427 * _mbctoupper(MSVCRT.@)
429 unsigned int CDECL _mbctoupper(unsigned int c)
431 if (_ismbblead(c))
433 FIXME("Handle MBC chars\n");
434 return c;
436 return MSVCRT__toupper_l(c, NULL); /* ASCII CP or SB char */
439 /*********************************************************************
440 * _mbctombb (MSVCRT.@)
442 unsigned int CDECL _mbctombb(unsigned int c)
444 unsigned int value;
446 if(get_mbcinfo()->mbcodepage == 932)
448 if(c >= 0x829f && c <= 0x82f1) /* Hiragana */
449 return mbctombb_932_kana[c - 0x829f];
450 if(c >= 0x8340 && c <= 0x8396 && c != 0x837f) /* Katakana */
451 return mbctombb_932_kana[c - 0x8340 - (c >= 0x837f ? 1 : 0)];
452 if(c >= 0x8140 && c <= 0x8197) /* Punctuation */
454 value = mbctombb_932_punct[c - 0x8140];
455 return value ? value : c;
457 if((c >= 0x824f && c <= 0x8258) || /* Fullwidth digits */
458 (c >= 0x8260 && c <= 0x8279)) /* Fullwidth capitals letters */
459 return c - 0x821f;
460 if(c >= 0x8281 && c <= 0x829a) /* Fullwidth small letters */
461 return c - 0x8220;
462 /* all other cases return c */
464 return c;
467 /*********************************************************************
468 * _mbcjistojms(MSVCRT.@)
470 * Converts a jis character to sjis.
471 * Based on description from
472 * http://www.slayers.ne.jp/~oouchi/code/jistosjis.html
474 unsigned int CDECL _mbcjistojms(unsigned int c)
476 /* Conversion takes place only when codepage is 932.
477 In all other cases, c is returned unchanged */
478 if(get_mbcinfo()->mbcodepage == 932)
480 if(HIBYTE(c) >= 0x21 && HIBYTE(c) <= 0x7e &&
481 LOBYTE(c) >= 0x21 && LOBYTE(c) <= 0x7e)
483 if(HIBYTE(c) % 2)
484 c += 0x1f;
485 else
486 c += 0x7d;
488 if(LOBYTE(c) >= 0x7F)
489 c += 0x1;
491 c = (((HIBYTE(c) - 0x21)/2 + 0x81) << 8) | LOBYTE(c);
493 if(HIBYTE(c) > 0x9f)
494 c += 0x4000;
496 else
497 return 0; /* Codepage is 932, but c can't be converted */
500 return c;
503 /*********************************************************************
504 * _mbcjmstojis(MSVCRT.@)
506 * Converts a sjis character to jis.
508 unsigned int CDECL _mbcjmstojis(unsigned int c)
510 /* Conversion takes place only when codepage is 932.
511 In all other cases, c is returned unchanged */
512 if(get_mbcinfo()->mbcodepage == 932)
514 if(_ismbclegal(c) && HIBYTE(c) < 0xf0)
516 if(HIBYTE(c) >= 0xe0)
517 c -= 0x4000;
519 c = (((HIBYTE(c) - 0x81)*2 + 0x21) << 8) | LOBYTE(c);
521 if(LOBYTE(c) > 0x7f)
522 c -= 0x1;
524 if(LOBYTE(c) > 0x9d)
525 c += 0x83;
526 else
527 c -= 0x1f;
529 else
530 return 0; /* Codepage is 932, but c can't be converted */
533 return c;
536 /*********************************************************************
537 * _mbsdec(MSVCRT.@)
539 unsigned char* CDECL _mbsdec(const unsigned char* start, const unsigned char* cur)
541 if(start >= cur)
542 return NULL;
544 if(get_mbcinfo()->ismbcodepage)
545 return (unsigned char *)(_ismbstrail(start,cur-1) ? cur - 2 : cur -1);
547 return (unsigned char *)cur - 1; /* ASCII CP or SB char */
550 /*********************************************************************
551 * _mbclen(MSVCRT.@)
553 unsigned int CDECL _mbclen(const unsigned char* str)
555 return _ismbblead(*str) ? 2 : 1;
558 /*********************************************************************
559 * _mbsinc(MSVCRT.@)
561 unsigned char* CDECL _mbsinc(const unsigned char* str)
563 return (unsigned char *)(str + _mbclen(str));
566 /*********************************************************************
567 * _mbsninc(MSVCRT.@)
569 unsigned char* CDECL _mbsninc(const unsigned char* str, MSVCRT_size_t num)
571 if(!str)
572 return NULL;
574 while (num > 0 && *str)
576 if (_ismbblead(*str))
578 if (!*(str+1))
579 break;
580 str++;
582 str++;
583 num--;
586 return (unsigned char*)str;
589 /*********************************************************************
590 * _mbsnlen_l(MSVCRT.@)
592 MSVCRT_size_t CDECL _mbsnlen_l(const unsigned char *str,
593 MSVCRT_size_t maxsize, MSVCRT__locale_t locale)
595 MSVCRT_pthreadmbcinfo mbcinfo;
596 MSVCRT_size_t i = 0, len = 0;
598 if(!locale)
599 mbcinfo = get_mbcinfo();
600 else
601 mbcinfo = locale->mbcinfo;
603 if(!mbcinfo->ismbcodepage)
604 return MSVCRT_strnlen((const char*)str, maxsize);
606 while(i<maxsize && str[i])
608 if (_ismbblead_l(str[i], locale))
610 i++;
611 if (!str[i]) /* count only full chars */
612 break;
614 i++;
615 len++;
617 return i < maxsize ? len : maxsize;
620 /*********************************************************************
621 * _mbslen(MSVCRT.@)
623 MSVCRT_size_t CDECL _mbslen(const unsigned char* str)
625 return _mbsnlen_l(str, -1, NULL);
628 /*********************************************************************
629 * _mbslen_l(MSVCRT.@)
631 MSVCRT_size_t CDECL _mbslen_l(const unsigned char* str, MSVCRT__locale_t locale)
633 return _mbsnlen_l(str, -1, locale);
636 /*********************************************************************
637 * _mbsnlen(MSVCRT.@)
639 MSVCRT_size_t CDECL _mbsnlen(const unsigned char* str, MSVCRT_size_t maxsize)
641 return _mbsnlen_l(str, maxsize, NULL);
644 /*********************************************************************
645 * _mbccpy_s_l(MSVCRT.@)
647 int CDECL _mbccpy_s_l(unsigned char* dest, MSVCRT_size_t maxsize,
648 int *copied, const unsigned char* src, MSVCRT__locale_t locale)
650 if(copied) *copied = 0;
651 if(!MSVCRT_CHECK_PMT(dest != NULL && maxsize >= 1)) return MSVCRT_EINVAL;
652 dest[0] = 0;
653 if(!MSVCRT_CHECK_PMT(src != NULL)) return MSVCRT_EINVAL;
655 if(_ismbblead_l(*src, locale)) {
656 if(!src[1]) {
657 if(copied) *copied = 1;
658 *MSVCRT__errno() = MSVCRT_EILSEQ;
659 return MSVCRT_EILSEQ;
662 if(maxsize < 2) {
663 MSVCRT_INVALID_PMT("dst buffer is too small", MSVCRT_ERANGE);
664 return MSVCRT_ERANGE;
667 *dest++ = *src++;
668 *dest = *src;
669 if(copied) *copied = 2;
670 }else {
671 *dest = *src;
672 if(copied) *copied = 1;
675 return 0;
678 /*********************************************************************
679 * _mbccpy(MSVCRT.@)
681 void CDECL _mbccpy(unsigned char* dest, const unsigned char* src)
683 _mbccpy_s_l(dest, 2, NULL, src, NULL);
686 /*********************************************************************
687 * _mbccpy_l(MSVCRT.@)
689 void CDECL _mbccpy_l(unsigned char* dest, const unsigned char* src,
690 MSVCRT__locale_t locale)
692 _mbccpy_s_l(dest, 2, NULL, src, locale);
695 /*********************************************************************
696 * _mbccpy_s(MSVCRT.@)
698 int CDECL _mbccpy_s(unsigned char* dest, MSVCRT_size_t maxsize,
699 int *copied, const unsigned char* src)
701 return _mbccpy_s_l(dest, maxsize, copied, src, NULL);
704 /*********************************************************************
705 * _mbsncpy(MSVCRT.@)
706 * REMARKS
707 * The parameter n is the number or characters to copy, not the size of
708 * the buffer. Use _mbsnbcpy for a function analogical to strncpy
710 unsigned char* CDECL _mbsncpy(unsigned char* dst, const unsigned char* src, MSVCRT_size_t n)
712 unsigned char* ret = dst;
713 if(!n)
714 return dst;
715 if (get_mbcinfo()->ismbcodepage)
717 while (*src && n)
719 n--;
720 if (_ismbblead(*src))
722 if (!*(src+1))
724 *dst++ = 0;
725 *dst++ = 0;
726 break;
729 *dst++ = *src++;
732 *dst++ = *src++;
735 else
737 while (n)
739 n--;
740 if (!(*dst++ = *src++)) break;
743 while (n--) *dst++ = 0;
744 return ret;
747 /*********************************************************************
748 * _mbsnbcpy_s_l(MSVCRT.@)
749 * REMARKS
750 * Unlike _mbsnbcpy this function does not pad the rest of the dest
751 * string with 0
753 int CDECL _mbsnbcpy_s_l(unsigned char* dst, MSVCRT_size_t size,
754 const unsigned char* src, MSVCRT_size_t n, MSVCRT__locale_t locale)
756 MSVCRT_size_t pos = 0;
758 if(!dst || size == 0)
759 return MSVCRT_EINVAL;
760 if(!src)
762 dst[0] = '\0';
763 return MSVCRT_EINVAL;
765 if(!n)
766 return 0;
768 if((locale ? locale->mbcinfo : get_mbcinfo())->ismbcodepage)
770 BOOL is_lead = FALSE;
771 while (*src && n)
773 if(pos == size)
775 dst[0] = '\0';
776 return MSVCRT_ERANGE;
778 is_lead = (!is_lead && _ismbblead(*src));
779 n--;
780 dst[pos++] = *src++;
783 if (is_lead) /* if string ends with a lead, remove it */
784 dst[pos - 1] = 0;
786 else
788 while (n)
790 n--;
791 if(pos == size)
793 dst[0] = '\0';
794 return MSVCRT_ERANGE;
797 if(!(*src)) break;
798 dst[pos++] = *src++;
802 if(pos < size)
803 dst[pos] = '\0';
804 else
806 dst[0] = '\0';
807 return MSVCRT_ERANGE;
810 return 0;
813 /*********************************************************************
814 * _mbsnbcpy_s(MSVCRT.@)
816 int CDECL _mbsnbcpy_s(unsigned char* dst, MSVCRT_size_t size, const unsigned char* src, MSVCRT_size_t n)
818 return _mbsnbcpy_s_l(dst, size, src, n, NULL);
821 /*********************************************************************
822 * _mbscpy_s_l(MSVCRT.@)
824 int CDECL _mbscpy_s_l(unsigned char *dst, MSVCRT_size_t size,
825 const unsigned char *src, MSVCRT__locale_t locale)
827 return _mbsnbcpy_s_l(dst, size, src, -1, locale);
830 /*********************************************************************
831 * _mbscpy_s(MSVCRT.@)
833 int CDECL _mbscpy_s(unsigned char *dst, MSVCRT_size_t size, const unsigned char *src)
835 return _mbscpy_s_l(dst, size, src, NULL);
838 /*********************************************************************
839 * _mbsnbcpy(MSVCRT.@)
840 * REMARKS
841 * Like strncpy this function doesn't enforce the string to be
842 * NUL-terminated
844 unsigned char* CDECL _mbsnbcpy(unsigned char* dst, const unsigned char* src, MSVCRT_size_t n)
846 unsigned char* ret = dst;
847 if(!n)
848 return dst;
849 if(get_mbcinfo()->ismbcodepage)
851 BOOL is_lead = FALSE;
852 while (*src && n)
854 is_lead = (!is_lead && _ismbblead(*src));
855 n--;
856 *dst++ = *src++;
859 if (is_lead) /* if string ends with a lead, remove it */
860 *(dst - 1) = 0;
862 else
864 while (n)
866 n--;
867 if (!(*dst++ = *src++)) break;
870 while (n--) *dst++ = 0;
871 return ret;
874 /*********************************************************************
875 * _mbscmp_l(MSVCRT.@)
877 int CDECL _mbscmp_l(const unsigned char* str, const unsigned char* cmp, MSVCRT__locale_t locale)
879 MSVCRT_pthreadmbcinfo mbcinfo;
881 if (!str || !cmp) return INT_MAX;
883 mbcinfo = locale ? locale->mbcinfo : get_mbcinfo();
885 if(mbcinfo->ismbcodepage)
887 unsigned int strc, cmpc;
888 do {
889 if(!*str)
890 return *cmp ? -1 : 0;
891 if(!*cmp)
892 return 1;
893 strc = _mbsnextc_l(str, locale);
894 cmpc = _mbsnextc_l(cmp, locale);
895 if(strc != cmpc)
896 return strc < cmpc ? -1 : 1;
897 str +=(strc > 255) ? 2 : 1;
898 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
899 } while(1);
901 return u_strcmp(str, cmp); /* ASCII CP */
904 /*********************************************************************
905 * _mbscmp(MSVCRT.@)
907 int CDECL _mbscmp(const unsigned char* str, const unsigned char* cmp, MSVCRT__locale_t locale)
909 return _mbscmp_l(str, cmp, NULL);
912 /*********************************************************************
913 * _mbsnbicoll_l(MSVCRT.@)
915 int CDECL _mbsnbicoll_l(const unsigned char *str1, const unsigned char *str2, MSVCRT_size_t len, MSVCRT__locale_t locale)
917 MSVCRT_pthreadmbcinfo mbcinfo;
919 if(!locale)
920 mbcinfo = get_mbcinfo();
921 else
922 mbcinfo = locale->mbcinfo;
924 if(!mbcinfo->ismbcodepage)
925 return MSVCRT__strnicoll_l((const char*)str1, (const char*)str2, len, locale);
926 return CompareStringA(mbcinfo->mblcid, NORM_IGNORECASE, (const char*)str1, len, (const char*)str2, len)-CSTR_EQUAL;
929 /*********************************************************************
930 * _mbsicoll_l(MSVCRT.@)
932 int CDECL _mbsicoll_l(const unsigned char *str1, const unsigned char *str2, MSVCRT__locale_t locale)
934 return _mbsnbicoll_l(str1, str2, -1, locale);
937 /*********************************************************************
938 * _mbsnbicoll(MSVCRT.@)
940 int CDECL _mbsnbicoll(const unsigned char *str1, const unsigned char *str2, MSVCRT_size_t len)
942 return _mbsnbicoll_l(str1, str2, len, NULL);
945 /*********************************************************************
946 * _mbsicoll(MSVCRT.@)
948 int CDECL _mbsicoll(const unsigned char* str, const unsigned char* cmp)
950 #if _MSVCR_VER>=60 && _MSVCR_VER<=71
951 return CompareStringA(get_mbcinfo()->mblcid, NORM_IGNORECASE,
952 (const char*)str, -1, (const char*)cmp, -1)-CSTR_EQUAL;
953 #else
954 return _mbsnbicoll_l(str, cmp, -1, NULL);
955 #endif
958 /*********************************************************************
959 * _mbsnbcoll_l(MSVCRT.@)
961 int CDECL _mbsnbcoll_l(const unsigned char *str1, const unsigned char *str2, MSVCRT_size_t len, MSVCRT__locale_t locale)
963 MSVCRT_pthreadmbcinfo mbcinfo;
965 if(!locale)
966 mbcinfo = get_mbcinfo();
967 else
968 mbcinfo = locale->mbcinfo;
970 if(!mbcinfo->ismbcodepage)
971 return MSVCRT__strncoll_l((const char*)str1, (const char*)str2, len, locale);
972 return CompareStringA(mbcinfo->mblcid, 0, (const char*)str1, len, (const char*)str2, len)-CSTR_EQUAL;
975 /*********************************************************************
976 * _mbscoll_l(MSVCRT.@)
978 int CDECL _mbscoll_l(const unsigned char *str1, const unsigned char *str2, MSVCRT__locale_t locale)
980 return _mbsnbcoll_l(str1, str2, -1, locale);
983 /*********************************************************************
984 * _mbsnbcoll(MSVCRT.@)
986 int CDECL _mbsnbcoll(const unsigned char *str1, const unsigned char *str2, MSVCRT_size_t len)
988 return _mbsnbcoll_l(str1, str2, len, NULL);
991 /*********************************************************************
992 * _mbscoll(MSVCRT.@)
994 int CDECL _mbscoll(const unsigned char* str, const unsigned char* cmp)
996 #if _MSVCR_VER>=60 && _MSVCR_VER<=71
997 return CompareStringA(get_mbcinfo()->mblcid, 0,
998 (const char*)str, -1, (const char*)cmp, -1)-CSTR_EQUAL;
999 #else
1000 return _mbsnbcoll_l(str, cmp, -1, NULL);
1001 #endif
1004 /*********************************************************************
1005 * _mbsicmp(MSVCRT.@)
1007 int CDECL _mbsicmp(const unsigned char* str, const unsigned char* cmp)
1009 if(get_mbcinfo()->ismbcodepage)
1011 unsigned int strc, cmpc;
1012 do {
1013 if(!*str)
1014 return *cmp ? -1 : 0;
1015 if(!*cmp)
1016 return 1;
1017 strc = _mbctolower(_mbsnextc(str));
1018 cmpc = _mbctolower(_mbsnextc(cmp));
1019 if(strc != cmpc)
1020 return strc < cmpc ? -1 : 1;
1021 str +=(strc > 255) ? 2 : 1;
1022 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
1023 } while(1);
1025 return u_strcasecmp(str, cmp); /* ASCII CP */
1028 /*********************************************************************
1029 * _mbsncmp(MSVCRT.@)
1031 int CDECL _mbsncmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
1033 if(!len)
1034 return 0;
1036 if(get_mbcinfo()->ismbcodepage)
1038 unsigned int strc, cmpc;
1039 while(len--)
1041 int inc;
1042 if(!*str)
1043 return *cmp ? -1 : 0;
1044 if(!*cmp)
1045 return 1;
1046 strc = _mbsnextc(str);
1047 cmpc = _mbsnextc(cmp);
1048 if(strc != cmpc)
1049 return strc < cmpc ? -1 : 1;
1050 inc=(strc > 255) ? 2 : 1; /* Equal, use same increment */
1051 str += inc;
1052 cmp += inc;
1054 return 0; /* Matched len chars */
1056 return u_strncmp(str, cmp, len); /* ASCII CP */
1059 /*********************************************************************
1060 * _mbsnbcmp(MSVCRT.@)
1062 int CDECL _mbsnbcmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
1064 if (!len)
1065 return 0;
1066 if(get_mbcinfo()->ismbcodepage)
1068 unsigned int strc, cmpc;
1069 while (len)
1071 int clen;
1072 if(!*str)
1073 return *cmp ? -1 : 0;
1074 if(!*cmp)
1075 return 1;
1076 if (_ismbblead(*str))
1078 strc=(len>=2)?_mbsnextc(str):0;
1079 clen=2;
1081 else
1083 strc=*str;
1084 clen=1;
1086 if (_ismbblead(*cmp))
1087 cmpc=(len>=2)?_mbsnextc(cmp):0;
1088 else
1089 cmpc=*cmp;
1090 if(strc != cmpc)
1091 return strc < cmpc ? -1 : 1;
1092 len -= clen;
1093 str += clen;
1094 cmp += clen;
1096 return 0; /* Matched len chars */
1098 return u_strncmp(str,cmp,len);
1101 /*********************************************************************
1102 * _mbsnicmp(MSVCRT.@)
1104 * Compare two multibyte strings case insensitively to 'len' characters.
1106 int CDECL _mbsnicmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
1108 /* FIXME: No tolower() for mb strings yet */
1109 if(get_mbcinfo()->ismbcodepage)
1111 unsigned int strc, cmpc;
1112 while(len--)
1114 if(!*str)
1115 return *cmp ? -1 : 0;
1116 if(!*cmp)
1117 return 1;
1118 strc = _mbctolower(_mbsnextc(str));
1119 cmpc = _mbctolower(_mbsnextc(cmp));
1120 if(strc != cmpc)
1121 return strc < cmpc ? -1 : 1;
1122 str +=(strc > 255) ? 2 : 1;
1123 cmp +=(strc > 255) ? 2 : 1; /* Equal, use same increment */
1125 return 0; /* Matched len chars */
1127 return u_strncasecmp(str, cmp, len); /* ASCII CP */
1130 /*********************************************************************
1131 * _mbsnbicmp(MSVCRT.@)
1133 int CDECL _mbsnbicmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
1135 if (!len)
1136 return 0;
1137 if(get_mbcinfo()->ismbcodepage)
1139 unsigned int strc, cmpc;
1140 while (len)
1142 int clen;
1143 if(!*str)
1144 return *cmp ? -1 : 0;
1145 if(!*cmp)
1146 return 1;
1147 if (_ismbblead(*str))
1149 strc=(len>=2)?_mbsnextc(str):0;
1150 clen=2;
1152 else
1154 strc=*str;
1155 clen=1;
1157 if (_ismbblead(*cmp))
1158 cmpc=(len>=2)?_mbsnextc(cmp):0;
1159 else
1160 cmpc=*cmp;
1161 strc = _mbctolower(strc);
1162 cmpc = _mbctolower(cmpc);
1163 if(strc != cmpc)
1164 return strc < cmpc ? -1 : 1;
1165 len -= clen;
1166 str += clen;
1167 cmp += clen;
1169 return 0; /* Matched len bytes */
1171 return u_strncasecmp(str,cmp,len);
1174 /*********************************************************************
1175 * _mbscat (MSVCRT.@)
1177 unsigned char * CDECL _mbscat( unsigned char *dst, const unsigned char *src )
1179 strcat( (char *)dst, (const char *)src );
1180 return dst;
1183 /*********************************************************************
1184 * _mbscat_s_l (MSVCRT.@)
1186 int CDECL _mbscat_s_l( unsigned char *dst, MSVCRT_size_t size,
1187 const unsigned char *src, MSVCRT__locale_t locale )
1189 MSVCRT_size_t i, j;
1190 int ret = 0;
1192 if(!MSVCRT_CHECK_PMT(dst != NULL)) return MSVCRT_EINVAL;
1193 if(!MSVCRT_CHECK_PMT(src != NULL)) return MSVCRT_EINVAL;
1195 for(i=0; i<size; i++)
1196 if(!dst[i]) break;
1197 if(i == size) {
1198 MSVCRT_INVALID_PMT("dst is not NULL-terminated", MSVCRT_EINVAL);
1199 if(size) dst[0] = 0;
1200 return MSVCRT_EINVAL;
1203 if(i && _ismbblead_l(dst[i-1], locale)) {
1204 ret = MSVCRT_EILSEQ;
1205 i--;
1208 for(j=0; src[j] && i+j<size; j++)
1209 dst[i+j] = src[j];
1210 if(i+j == size) {
1211 MSVCRT_INVALID_PMT("dst buffer is too small", MSVCRT_ERANGE);
1212 dst[0] = 0;
1213 return MSVCRT_ERANGE;
1216 if(j && _ismbblead_l(src[j-1], locale)) {
1217 ret = MSVCRT_EILSEQ;
1218 j--;
1221 dst[i+j] = 0;
1222 return ret;
1225 /*********************************************************************
1226 * _mbscat_s (MSVCRT.@)
1228 int CDECL _mbscat_s( unsigned char *dst, MSVCRT_size_t size, const unsigned char *src )
1230 return _mbscat_s_l(dst, size, src, NULL);
1233 /*********************************************************************
1234 * _mbscpy (MSVCRT.@)
1236 unsigned char* CDECL _mbscpy( unsigned char *dst, const unsigned char *src )
1238 strcpy( (char *)dst, (const char *)src );
1239 return dst;
1242 /*********************************************************************
1243 * _mbsstr (MSVCRT.@)
1245 unsigned char * CDECL _mbsstr(const unsigned char *haystack, const unsigned char *needle)
1247 return (unsigned char *)MSVCRT_strstr( (const char *)haystack, (const char *)needle );
1250 /*********************************************************************
1251 * _mbschr(MSVCRT.@)
1253 * Find a multibyte character in a multibyte string.
1255 unsigned char* CDECL _mbschr(const unsigned char* s, unsigned int x)
1257 if(get_mbcinfo()->ismbcodepage)
1259 unsigned int c;
1260 while (1)
1262 c = _mbsnextc(s);
1263 if (c == x)
1264 return (unsigned char*)s;
1265 if (!c)
1266 return NULL;
1267 s += c > 255 ? 2 : 1;
1270 return u_strchr(s, x); /* ASCII CP */
1273 /*********************************************************************
1274 * _mbsrchr(MSVCRT.@)
1276 unsigned char* CDECL _mbsrchr(const unsigned char* s, unsigned int x)
1278 if(get_mbcinfo()->ismbcodepage)
1280 unsigned int c;
1281 unsigned char* match=NULL;
1282 if(!s)
1283 return NULL;
1284 while (1) {
1285 c = _mbsnextc(s);
1286 if (c == x)
1287 match=(unsigned char*)s;
1288 if (!c)
1289 return match;
1290 s +=(c > 255) ? 2 : 1;
1293 return u_strrchr(s, x);
1296 /*********************************************************************
1297 * _mbstok_s_l(MSVCRT.@)
1299 unsigned char* CDECL _mbstok_s_l(unsigned char *str, const unsigned char *delim,
1300 unsigned char **ctx, MSVCRT__locale_t locale)
1302 MSVCRT_pthreadmbcinfo mbcinfo;
1303 unsigned int c;
1305 if(!MSVCRT_CHECK_PMT(delim != NULL)) return NULL;
1306 if(!MSVCRT_CHECK_PMT(ctx != NULL)) return NULL;
1307 if(!MSVCRT_CHECK_PMT(str || *ctx)) return NULL;
1309 if(locale)
1310 mbcinfo = locale->mbcinfo;
1311 else
1312 mbcinfo = get_mbcinfo();
1314 if(!mbcinfo->ismbcodepage)
1315 return (unsigned char*)MSVCRT_strtok_s((char*)str, (const char*)delim, (char**)ctx);
1317 if(!str)
1318 str = *ctx;
1320 while((c=_mbsnextc(str)) && _mbschr(delim, c))
1321 str += c>255 ? 2 : 1;
1322 if(!*str)
1324 *ctx = str;
1325 return NULL;
1328 *ctx = str + (c>255 ? 2 : 1);
1329 while((c=_mbsnextc(*ctx)) && !_mbschr(delim, c))
1330 *ctx += c>255 ? 2 : 1;
1331 if (**ctx) {
1332 *(*ctx)++ = 0;
1333 if(c > 255)
1334 *(*ctx)++ = 0;
1337 return str;
1341 /*********************************************************************
1342 * _mbstok_s(MSVCRT.@)
1344 unsigned char* CDECL _mbstok_s(unsigned char *str,
1345 const unsigned char *delim, unsigned char **ctx)
1347 return _mbstok_s_l(str, delim, ctx, NULL);
1350 /*********************************************************************
1351 * _mbstok_l(MSVCRT.@)
1353 unsigned char* CDECL _mbstok_l(unsigned char *str,
1354 const unsigned char *delim, MSVCRT__locale_t locale)
1356 return _mbstok_s_l(str, delim, &msvcrt_get_thread_data()->mbstok_next, locale);
1359 /*********************************************************************
1360 * _mbstok(MSVCRT.@)
1362 unsigned char* CDECL _mbstok(unsigned char *str, const unsigned char *delim)
1364 thread_data_t *data = msvcrt_get_thread_data();
1366 #if _MSVCR_VER == 0
1367 if(!str && !data->mbstok_next)
1368 return NULL;
1369 #endif
1371 return _mbstok_s_l(str, delim, &data->mbstok_next, NULL);
1374 /*********************************************************************
1375 * _mbbtombc(MSVCRT.@)
1377 unsigned int CDECL _mbbtombc(unsigned int c)
1379 if(get_mbcinfo()->mbcodepage == 932)
1381 if(c >= 0x20 && c <= 0x7e) {
1382 if((c >= 0x41 && c <= 0x5a) || (c >= 0x61 && c <= 0x7a) || (c >= 0x30 && c <= 0x39))
1383 return mbbtombc_932[c - 0x20] | 0x8200;
1384 else
1385 return mbbtombc_932[c - 0x20] | 0x8100;
1387 else if(c >= 0xa1 && c <= 0xdf) {
1388 if(c >= 0xa6 && c <= 0xdd && c != 0xb0)
1389 return mbbtombc_932[c - 0xa1 + 0x5f] | 0x8300;
1390 else
1391 return mbbtombc_932[c - 0xa1 + 0x5f] | 0x8100;
1394 return c; /* not Japanese or no MB char */
1397 /*********************************************************************
1398 * _ismbbkana_l(MSVCRT.@)
1400 int CDECL _ismbbkana_l(unsigned int c, MSVCRT__locale_t locale)
1402 MSVCRT_pthreadmbcinfo mbcinfo;
1404 if(locale)
1405 mbcinfo = locale->mbcinfo;
1406 else
1407 mbcinfo = get_mbcinfo();
1409 if(mbcinfo->mbcodepage == 932)
1411 /* Japanese/Katakana, CP 932 */
1412 return (c >= 0xa1 && c <= 0xdf);
1414 return 0;
1417 /*********************************************************************
1418 * _ismbbkana(MSVCRT.@)
1420 int CDECL _ismbbkana(unsigned int c)
1422 return _ismbbkana_l( c, NULL );
1425 /*********************************************************************
1426 * _ismbcdigit_l(MSVCRT.@)
1428 int CDECL _ismbcdigit_l(unsigned int ch, MSVCRT__locale_t locale)
1430 return MSVCRT__iswdigit_l( msvcrt_mbc_to_wc_l(ch, locale), locale );
1433 /*********************************************************************
1434 * _ismbcdigit(MSVCRT.@)
1436 int CDECL _ismbcdigit(unsigned int ch)
1438 return _ismbcdigit_l( ch, NULL );
1441 /*********************************************************************
1442 * _ismbcgraph_l(MSVCRT.@)
1444 int CDECL _ismbcgraph_l(unsigned int ch, MSVCRT__locale_t locale)
1446 return MSVCRT__iswgraph_l( msvcrt_mbc_to_wc_l(ch, locale), locale );
1449 /*********************************************************************
1450 * _ismbcgraph(MSVCRT.@)
1452 int CDECL _ismbcgraph(unsigned int ch)
1454 return _ismbcgraph_l( ch, NULL );
1457 /*********************************************************************
1458 * _ismbcalpha_l (MSVCRT.@)
1460 int CDECL _ismbcalpha_l(unsigned int ch, MSVCRT__locale_t locale)
1462 return MSVCRT__iswalpha_l( msvcrt_mbc_to_wc_l(ch, locale), locale );
1465 /*********************************************************************
1466 * _ismbcalpha (MSVCRT.@)
1468 int CDECL _ismbcalpha(unsigned int ch)
1470 return _ismbcalpha_l( ch, NULL );
1473 /*********************************************************************
1474 * _ismbclower_l (MSVCRT.@)
1476 int CDECL _ismbclower_l(unsigned int ch, MSVCRT__locale_t locale)
1478 return MSVCRT__iswlower_l( msvcrt_mbc_to_wc_l(ch, locale), locale );
1481 /*********************************************************************
1482 * _ismbclower (MSVCRT.@)
1484 int CDECL _ismbclower(unsigned int ch)
1486 return _ismbclower_l( ch, NULL );
1489 /*********************************************************************
1490 * _ismbcupper_l (MSVCRT.@)
1492 int CDECL _ismbcupper_l(unsigned int ch, MSVCRT__locale_t locale)
1494 return MSVCRT__iswupper_l( msvcrt_mbc_to_wc_l(ch, locale), locale );
1497 /*********************************************************************
1498 * _ismbcupper (MSVCRT.@)
1500 int CDECL _ismbcupper(unsigned int ch)
1502 return _ismbcupper_l( ch, NULL );
1505 /*********************************************************************
1506 * _ismbcsymbol(MSVCRT.@)
1508 int CDECL _ismbcsymbol(unsigned int ch)
1510 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1511 WORD ctype;
1512 if (!GetStringTypeW(CT_CTYPE3, &wch, 1, &ctype))
1514 WARN("GetStringTypeW failed on %x\n", ch);
1515 return 0;
1517 return ((ctype & C3_SYMBOL) != 0);
1520 /*********************************************************************
1521 * _ismbcalnum_l (MSVCRT.@)
1523 int CDECL _ismbcalnum_l(unsigned int ch, MSVCRT__locale_t locale)
1525 return MSVCRT__iswalnum_l( msvcrt_mbc_to_wc_l(ch, locale), locale );
1528 /*********************************************************************
1529 * _ismbcalnum (MSVCRT.@)
1531 int CDECL _ismbcalnum(unsigned int ch)
1533 return _ismbcalnum_l( ch, NULL );
1536 /*********************************************************************
1537 * _ismbcspace_l (MSVCRT.@)
1539 int CDECL _ismbcspace_l(unsigned int ch, MSVCRT__locale_t locale)
1541 return MSVCRT__iswspace_l( msvcrt_mbc_to_wc_l(ch, locale), locale );
1544 /*********************************************************************
1545 * _ismbcspace (MSVCRT.@)
1547 int CDECL _ismbcspace(unsigned int ch)
1549 return _ismbcspace_l( ch, NULL );
1552 /*********************************************************************
1553 * _ismbcprint_l (MSVCRT.@)
1555 int CDECL _ismbcprint_l(unsigned int ch, MSVCRT__locale_t locale)
1557 return MSVCRT__iswprint_l( msvcrt_mbc_to_wc_l(ch, locale), locale );
1560 /*********************************************************************
1561 * _ismbcprint (MSVCRT.@)
1563 int CDECL _ismbcprint(unsigned int ch)
1565 return _ismbcprint_l( ch, NULL );
1568 /*********************************************************************
1569 * _ismbcpunct_l (MSVCRT.@)
1571 int CDECL _ismbcpunct_l(unsigned int ch, MSVCRT__locale_t locale)
1573 return MSVCRT__iswpunct_l( msvcrt_mbc_to_wc_l(ch, locale), locale );
1576 /*********************************************************************
1577 * _ismbcpunct(MSVCRT.@)
1579 int CDECL _ismbcpunct(unsigned int ch)
1581 return _ismbcpunct_l( ch, NULL );
1584 /*********************************************************************
1585 * _ismbchira(MSVCRT.@)
1587 int CDECL _ismbchira(unsigned int c)
1589 if(get_mbcinfo()->mbcodepage == 932)
1591 /* Japanese/Hiragana, CP 932 */
1592 return (c >= 0x829f && c <= 0x82f1);
1594 return 0;
1597 /*********************************************************************
1598 * _ismbckata(MSVCRT.@)
1600 int CDECL _ismbckata(unsigned int c)
1602 if(get_mbcinfo()->mbcodepage == 932)
1604 /* Japanese/Katakana, CP 932 */
1605 return (c >= 0x8340 && c <= 0x8396 && c != 0x837f);
1607 return 0;
1611 /*********************************************************************
1612 * _ismbblead_l(MSVCRT.@)
1614 int CDECL _ismbblead_l(unsigned int c, MSVCRT__locale_t locale)
1616 MSVCRT_pthreadmbcinfo mbcinfo;
1618 if(!locale)
1619 mbcinfo = get_mbcinfo();
1620 else
1621 mbcinfo = locale->mbcinfo;
1623 return (mbcinfo->mbctype[(c&0xff) + 1] & _M1) != 0;
1626 /*********************************************************************
1627 * _ismbblead(MSVCRT.@)
1629 int CDECL _ismbblead(unsigned int c)
1631 return _ismbblead_l(c, NULL);
1634 /*********************************************************************
1635 * _ismbbtrail_l(MSVCRT.@)
1637 int CDECL _ismbbtrail_l(unsigned int c, MSVCRT__locale_t locale)
1639 MSVCRT_pthreadmbcinfo mbcinfo;
1641 if(!locale)
1642 mbcinfo = get_mbcinfo();
1643 else
1644 mbcinfo = locale->mbcinfo;
1646 return (mbcinfo->mbctype[(c&0xff) + 1] & _M2) != 0;
1649 /*********************************************************************
1650 * _ismbbtrail(MSVCRT.@)
1652 int CDECL _ismbbtrail(unsigned int c)
1654 return _ismbbtrail_l(c, NULL);
1657 /*********************************************************************
1658 * _ismbclegal_l(MSVCRT.@)
1660 int CDECL _ismbclegal_l(unsigned int c, MSVCRT__locale_t locale)
1662 return _ismbblead_l(HIBYTE(c), locale) && _ismbbtrail_l(LOBYTE(c), locale);
1665 /*********************************************************************
1666 * _ismbclegal(MSVCRT.@)
1668 int CDECL _ismbclegal(unsigned int c)
1670 return _ismbclegal_l(c, NULL);
1673 /*********************************************************************
1674 * _ismbslead(MSVCRT.@)
1676 int CDECL _ismbslead(const unsigned char* start, const unsigned char* str)
1678 int lead = 0;
1680 if(!get_mbcinfo()->ismbcodepage)
1681 return 0;
1683 /* Lead bytes can also be trail bytes so we need to analyse the string
1685 while (start <= str)
1687 if (!*start)
1688 return 0;
1689 lead = !lead && _ismbblead(*start);
1690 start++;
1693 return lead ? -1 : 0;
1696 /*********************************************************************
1697 * _ismbstrail(MSVCRT.@)
1699 int CDECL _ismbstrail(const unsigned char* start, const unsigned char* str)
1701 /* Note: this function doesn't check _ismbbtrail */
1702 if ((str > start) && _ismbslead(start, str-1))
1703 return -1;
1704 else
1705 return 0;
1708 /*********************************************************************
1709 * _mbbtype_l(MSVCRT.@)
1711 int CDECL _mbbtype_l(unsigned char c, int type, MSVCRT__locale_t locale)
1713 if (type == 1)
1714 return _ismbbtrail_l(c, locale) ? _MBC_TRAIL : _MBC_ILLEGAL;
1715 else
1716 return _ismbblead_l(c, locale) ? _MBC_LEAD
1717 : MSVCRT__isprint_l(c, locale) ? _MBC_SINGLE : _MBC_ILLEGAL;
1720 /*********************************************************************
1721 * _mbbtype(MSVCRT.@)
1723 int CDECL _mbbtype(unsigned char c, int type)
1725 return _mbbtype_l(c, type, NULL);
1728 /*********************************************************************
1729 * _mbsbtype (MSVCRT.@)
1731 int CDECL _mbsbtype(const unsigned char *str, MSVCRT_size_t count)
1733 int lead = 0;
1734 const unsigned char *end = str + count;
1736 /* Lead bytes can also be trail bytes so we need to analyse the string.
1737 * Also we must return _MBC_ILLEGAL for chars past the end of the string
1739 while (str < end) /* Note: we skip the last byte - will check after the loop */
1741 if (!*str)
1742 return _MBC_ILLEGAL;
1743 lead = get_mbcinfo()->ismbcodepage && !lead && _ismbblead(*str);
1744 str++;
1747 if (lead)
1748 if (_ismbbtrail(*str))
1749 return _MBC_TRAIL;
1750 else
1751 return _MBC_ILLEGAL;
1752 else
1753 if (_ismbblead(*str))
1754 return _MBC_LEAD;
1755 else
1756 return _MBC_SINGLE;
1759 /*********************************************************************
1760 * _mbsset(MSVCRT.@)
1762 unsigned char* CDECL _mbsset(unsigned char* str, unsigned int c)
1764 unsigned char* ret = str;
1766 if(!get_mbcinfo()->ismbcodepage || c < 256)
1767 return u__strset(str, c); /* ASCII CP or SB char */
1769 c &= 0xffff; /* Strip high bits */
1771 while(str[0] && str[1])
1773 *str++ = c >> 8;
1774 *str++ = c & 0xff;
1776 if(str[0])
1777 str[0] = '\0'; /* FIXME: OK to shorten? */
1779 return ret;
1782 /*********************************************************************
1783 * _mbsnbset(MSVCRT.@)
1785 unsigned char* CDECL _mbsnbset(unsigned char *str, unsigned int c, MSVCRT_size_t len)
1787 unsigned char *ret = str;
1789 if(!len)
1790 return ret;
1792 if(!get_mbcinfo()->ismbcodepage || c < 256)
1793 return u__strnset(str, c, len); /* ASCII CP or SB char */
1795 c &= 0xffff; /* Strip high bits */
1797 while(str[0] && str[1] && (len > 1))
1799 *str++ = c >> 8;
1800 len--;
1801 *str++ = c & 0xff;
1802 len--;
1804 if(len && str[0]) {
1805 /* as per msdn pad with a blank character */
1806 str[0] = ' ';
1809 return ret;
1812 /*********************************************************************
1813 * _mbsnset(MSVCRT.@)
1815 unsigned char* CDECL _mbsnset(unsigned char* str, unsigned int c, MSVCRT_size_t len)
1817 unsigned char *ret = str;
1819 if(!len)
1820 return ret;
1822 if(!get_mbcinfo()->ismbcodepage || c < 256)
1823 return u__strnset(str, c, len); /* ASCII CP or SB char */
1825 c &= 0xffff; /* Strip high bits */
1827 while(str[0] && str[1] && len--)
1829 *str++ = c >> 8;
1830 *str++ = c & 0xff;
1832 if(len && str[0])
1833 str[0] = '\0'; /* FIXME: OK to shorten? */
1835 return ret;
1838 /*********************************************************************
1839 * _mbsnccnt(MSVCRT.@)
1840 * 'c' is for 'character'.
1842 MSVCRT_size_t CDECL _mbsnccnt(const unsigned char* str, MSVCRT_size_t len)
1844 MSVCRT_size_t ret;
1845 if(get_mbcinfo()->ismbcodepage)
1847 ret=0;
1848 while(*str && len-- > 0)
1850 if(_ismbblead(*str))
1852 if (!len)
1853 break;
1854 len--;
1855 str++;
1857 str++;
1858 ret++;
1860 return ret;
1862 ret=u_strlen(str);
1863 return min(ret, len); /* ASCII CP */
1866 /*********************************************************************
1867 * _mbsnbcnt(MSVCRT.@)
1868 * 'b' is for byte count.
1870 MSVCRT_size_t CDECL _mbsnbcnt(const unsigned char* str, MSVCRT_size_t len)
1872 MSVCRT_size_t ret;
1873 if(get_mbcinfo()->ismbcodepage)
1875 const unsigned char* xstr = str;
1876 while(*xstr && len-- > 0)
1878 if (_ismbblead(*xstr++))
1879 xstr++;
1881 return xstr-str;
1883 ret=u_strlen(str);
1884 return min(ret, len); /* ASCII CP */
1887 /*********************************************************************
1888 * _mbsnbcat(MSVCRT.@)
1890 unsigned char* CDECL _mbsnbcat(unsigned char* dst, const unsigned char* src, MSVCRT_size_t len)
1892 if(get_mbcinfo()->ismbcodepage)
1894 unsigned char *res = dst;
1895 while (*dst) {
1896 if (_ismbblead(*dst++)) {
1897 if (*dst) {
1898 dst++;
1899 } else {
1900 /* as per msdn overwrite the lead byte in front of '\0' */
1901 dst--;
1902 break;
1906 while (*src && len--) *dst++ = *src++;
1907 *dst = '\0';
1908 return res;
1910 return u_strncat(dst, src, len); /* ASCII CP */
1913 int CDECL _mbsnbcat_s(unsigned char *dst, MSVCRT_size_t size, const unsigned char *src, MSVCRT_size_t len)
1915 unsigned char *ptr = dst;
1916 MSVCRT_size_t i;
1918 if (!dst && !size && !src && !len)
1919 return 0;
1921 if (!dst || !size || !src)
1923 if (dst && size)
1924 *dst = '\0';
1926 *MSVCRT__errno() = MSVCRT_EINVAL;
1927 return MSVCRT_EINVAL;
1930 /* Find the null terminator of the destination buffer. */
1931 while (size && *ptr)
1932 size--, ptr++;
1934 if (!size)
1936 *dst = '\0';
1937 *MSVCRT__errno() = MSVCRT_EINVAL;
1938 return MSVCRT_EINVAL;
1941 /* If necessary, check that the character preceding the null terminator is
1942 * a lead byte and move the pointer back by one for later overwrite. */
1943 if (ptr != dst && get_mbcinfo()->ismbcodepage && _ismbblead(*(ptr - 1)))
1944 size++, ptr--;
1946 for (i = 0; *src && i < len; i++)
1948 *ptr++ = *src++;
1949 size--;
1951 if (!size)
1953 *dst = '\0';
1954 *MSVCRT__errno() = MSVCRT_ERANGE;
1955 return MSVCRT_ERANGE;
1959 *ptr = '\0';
1960 return 0;
1963 /*********************************************************************
1964 * _mbsncat(MSVCRT.@)
1966 unsigned char* CDECL _mbsncat(unsigned char* dst, const unsigned char* src, MSVCRT_size_t len)
1968 if(get_mbcinfo()->ismbcodepage)
1970 unsigned char *res = dst;
1971 while (*dst)
1973 if (_ismbblead(*dst++))
1974 dst++;
1976 while (*src && len--)
1978 *dst++ = *src;
1979 if(_ismbblead(*src++))
1980 *dst++ = *src++;
1982 *dst = '\0';
1983 return res;
1985 return u_strncat(dst, src, len); /* ASCII CP */
1989 /*********************************************************************
1990 * _mbslwr(MSVCRT.@)
1992 unsigned char* CDECL _mbslwr(unsigned char* s)
1994 unsigned char *ret = s;
1995 if (!s)
1996 return NULL;
1997 if (get_mbcinfo()->ismbcodepage)
1999 unsigned int c;
2000 while (*s)
2002 c = _mbctolower(_mbsnextc(s));
2003 /* Note that I assume that the size of the character is unchanged */
2004 if (c > 255)
2006 *s++=(c>>8);
2007 c=c & 0xff;
2009 *s++=c;
2012 else for ( ; *s; s++) *s = MSVCRT__tolower_l(*s, NULL);
2013 return ret;
2017 /*********************************************************************
2018 * _mbslwr_s(MSVCRT.@)
2020 int CDECL _mbslwr_s(unsigned char* s, MSVCRT_size_t len)
2022 if (!s && !len)
2024 return 0;
2026 else if (!s || !len)
2028 *MSVCRT__errno() = MSVCRT_EINVAL;
2029 return MSVCRT_EINVAL;
2031 if (get_mbcinfo()->ismbcodepage)
2033 unsigned int c;
2034 for ( ; *s && len > 0; len--)
2036 c = _mbctolower(_mbsnextc(s));
2037 /* Note that I assume that the size of the character is unchanged */
2038 if (c > 255)
2040 *s++=(c>>8);
2041 c=c & 0xff;
2043 *s++=c;
2046 else for ( ; *s && len > 0; s++, len--) *s = MSVCRT__tolower_l(*s, NULL);
2047 if (*s)
2049 *s = '\0';
2050 *MSVCRT__errno() = MSVCRT_EINVAL;
2051 return MSVCRT_EINVAL;
2053 return 0;
2057 /*********************************************************************
2058 * _mbsupr(MSVCRT.@)
2060 unsigned char* CDECL _mbsupr(unsigned char* s)
2062 unsigned char *ret = s;
2063 if (!s)
2064 return NULL;
2065 if (get_mbcinfo()->ismbcodepage)
2067 unsigned int c;
2068 while (*s)
2070 c = _mbctoupper(_mbsnextc(s));
2071 /* Note that I assume that the size of the character is unchanged */
2072 if (c > 255)
2074 *s++=(c>>8);
2075 c=c & 0xff;
2077 *s++=c;
2080 else for ( ; *s; s++) *s = MSVCRT__toupper_l(*s, NULL);
2081 return ret;
2085 /*********************************************************************
2086 * _mbsupr_s(MSVCRT.@)
2088 int CDECL _mbsupr_s(unsigned char* s, MSVCRT_size_t len)
2090 if (!s && !len)
2092 return 0;
2094 else if (!s || !len)
2096 *MSVCRT__errno() = MSVCRT_EINVAL;
2097 return MSVCRT_EINVAL;
2099 if (get_mbcinfo()->ismbcodepage)
2101 unsigned int c;
2102 for ( ; *s && len > 0; len--)
2104 c = _mbctoupper(_mbsnextc(s));
2105 /* Note that I assume that the size of the character is unchanged */
2106 if (c > 255)
2108 *s++=(c>>8);
2109 c=c & 0xff;
2111 *s++=c;
2114 else for ( ; *s && len > 0; s++, len--) *s = MSVCRT__toupper_l(*s, NULL);
2115 if (*s)
2117 *s = '\0';
2118 *MSVCRT__errno() = MSVCRT_EINVAL;
2119 return MSVCRT_EINVAL;
2121 return 0;
2124 /*********************************************************************
2125 * _mbsspn_l (MSVCRT.@)
2127 MSVCRT_size_t CDECL _mbsspn_l(const unsigned char* string,
2128 const unsigned char* set, MSVCRT__locale_t locale)
2130 const unsigned char *p, *q;
2132 for (p = string; *p; p++)
2134 for (q = set; *q; q++)
2136 if (_ismbblead_l(*q, locale))
2138 /* duplicate a bug in native implementation */
2139 if (!q[1]) break;
2141 if (p[0] == q[0] && p[1] == q[1])
2143 p++;
2144 break;
2146 q++;
2148 else
2150 if (p[0] == q[0]) break;
2153 if (!*q) break;
2155 return p - string;
2158 /*********************************************************************
2159 * _mbsspn (MSVCRT.@)
2161 MSVCRT_size_t CDECL _mbsspn(const unsigned char* string, const unsigned char* set)
2163 return _mbsspn_l(string, set, NULL);
2166 /*********************************************************************
2167 * _mbsspnp (MSVCRT.@)
2169 unsigned char* CDECL _mbsspnp(const unsigned char* string, const unsigned char* set)
2171 string += _mbsspn( string, set );
2172 return *string ? (unsigned char*)string : NULL;
2175 /*********************************************************************
2176 * _mbscspn_l (MSVCRT.@)
2178 MSVCRT_size_t CDECL _mbscspn_l(const unsigned char* str,
2179 const unsigned char* cmp, MSVCRT__locale_t locale)
2181 const unsigned char *p, *q;
2183 for (p = str; *p; p++)
2185 for (q = cmp; *q; q++)
2187 if (_ismbblead_l(*q, locale))
2189 /* duplicate a bug in native implementation */
2190 if (!q[1]) return 0;
2192 if (p[0] == q[0] && p[1] == q[1])
2193 return p - str;
2194 q++;
2196 else if (p[0] == q[0])
2197 return p - str;
2200 return p - str;
2203 /*********************************************************************
2204 * _mbscspn (MSVCRT.@)
2206 MSVCRT_size_t CDECL _mbscspn(const unsigned char* str, const unsigned char* cmp)
2208 return _mbscspn_l(str, cmp, NULL);
2211 /*********************************************************************
2212 * _mbsrev (MSVCRT.@)
2214 unsigned char* CDECL _mbsrev(unsigned char* str)
2216 int i, len = _mbslen(str);
2217 unsigned char *p, *temp=MSVCRT_malloc(len*2);
2219 if(!temp)
2220 return str;
2222 /* unpack multibyte string to temp buffer */
2223 p=str;
2224 for(i=0; i<len; i++)
2226 if (_ismbblead(*p))
2228 temp[i*2]=*p++;
2229 temp[i*2+1]=*p++;
2231 else
2233 temp[i*2]=*p++;
2234 temp[i*2+1]=0;
2238 /* repack it in the reverse order */
2239 p=str;
2240 for(i=len-1; i>=0; i--)
2242 if(_ismbblead(temp[i*2]))
2244 *p++=temp[i*2];
2245 *p++=temp[i*2+1];
2247 else
2249 *p++=temp[i*2];
2253 MSVCRT_free(temp);
2255 return str;
2258 /*********************************************************************
2259 * _mbspbrk (MSVCRT.@)
2261 unsigned char* CDECL _mbspbrk(const unsigned char* str, const unsigned char* accept)
2263 const unsigned char* p;
2265 while(*str)
2267 for(p = accept; *p; p += (_ismbblead(*p)?2:1) )
2269 if (*p == *str)
2270 if( !_ismbblead(*p) || ( *(p+1) == *(str+1) ) )
2271 return (unsigned char*)str;
2273 str += (_ismbblead(*str)?2:1);
2275 return NULL;
2280 * Functions depending on locale codepage
2283 /*********************************************************************
2284 * mblen(MSVCRT.@)
2285 * REMARKS
2286 * Unlike most of the multibyte string functions this function uses
2287 * the locale codepage, not the codepage set by _setmbcp
2289 int CDECL MSVCRT_mblen(const char* str, MSVCRT_size_t size)
2291 if (str && *str && size)
2293 if(get_locinfo()->mb_cur_max == 1)
2294 return 1; /* ASCII CP */
2296 return !MSVCRT_isleadbyte((unsigned char)*str) ? 1 : (size>1 ? 2 : -1);
2298 return 0;
2301 /*********************************************************************
2302 * mbrlen(MSVCRT.@)
2304 MSVCRT_size_t CDECL MSVCRT_mbrlen(const char *str, MSVCRT_size_t len, MSVCRT_mbstate_t *state)
2306 MSVCRT_mbstate_t s = (state ? *state : 0);
2307 MSVCRT_size_t ret;
2309 if(!len || !str || !*str)
2310 return 0;
2312 if(get_locinfo()->mb_cur_max == 1) {
2313 return 1;
2314 }else if(!s && MSVCRT_isleadbyte((unsigned char)*str)) {
2315 if(len == 1) {
2316 s = (unsigned char)*str;
2317 ret = -2;
2318 }else {
2319 ret = 2;
2321 }else if(!s) {
2322 ret = 1;
2323 }else {
2324 s = 0;
2325 ret = 2;
2328 if(state)
2329 *state = s;
2330 return ret;
2333 /*********************************************************************
2334 * _mbstrlen_l(MSVCRT.@)
2336 MSVCRT_size_t CDECL _mbstrlen_l(const char* str, MSVCRT__locale_t locale)
2338 MSVCRT_pthreadlocinfo locinfo;
2340 if(!locale)
2341 locinfo = get_locinfo();
2342 else
2343 locinfo = locale->locinfo;
2345 if(locinfo->mb_cur_max > 1) {
2346 MSVCRT_size_t len;
2347 len = MultiByteToWideChar(locinfo->lc_codepage, MB_ERR_INVALID_CHARS,
2348 str, -1, NULL, 0);
2349 if (!len) {
2350 *MSVCRT__errno() = MSVCRT_EILSEQ;
2351 return -1;
2353 return len - 1;
2356 return strlen(str);
2359 /*********************************************************************
2360 * _mbstrlen(MSVCRT.@)
2362 MSVCRT_size_t CDECL _mbstrlen(const char* str)
2364 return _mbstrlen_l(str, NULL);
2367 /*********************************************************************
2368 * _mbtowc_l(MSVCRT.@)
2370 int CDECL MSVCRT_mbtowc_l(MSVCRT_wchar_t *dst, const char* str, MSVCRT_size_t n, MSVCRT__locale_t locale)
2372 MSVCRT_pthreadlocinfo locinfo;
2373 MSVCRT_wchar_t tmpdst = '\0';
2375 if(!locale)
2376 locinfo = get_locinfo();
2377 else
2378 locinfo = locale->locinfo;
2380 if(n <= 0 || !str)
2381 return 0;
2383 if(!*str) {
2384 if(dst) *dst = 0;
2385 return 0;
2388 if(!locinfo->lc_codepage) {
2389 if(dst) *dst = (unsigned char)*str;
2390 return 1;
2392 if(n>=2 && MSVCRT__isleadbyte_l((unsigned char)*str, locale)) {
2393 if(!MultiByteToWideChar(locinfo->lc_codepage, 0, str, 2, &tmpdst, 1))
2394 return -1;
2395 if(dst) *dst = tmpdst;
2396 return 2;
2398 if(!MultiByteToWideChar(locinfo->lc_codepage, 0, str, 1, &tmpdst, 1))
2399 return -1;
2400 if(dst) *dst = tmpdst;
2401 return 1;
2404 /*********************************************************************
2405 * mbtowc(MSVCRT.@)
2407 int CDECL MSVCRT_mbtowc(MSVCRT_wchar_t *dst, const char* str, MSVCRT_size_t n)
2409 return MSVCRT_mbtowc_l(dst, str, n, NULL);
2412 /*********************************************************************
2413 * mbrtowc(MSVCRT.@)
2415 MSVCRT_size_t CDECL MSVCRT_mbrtowc(MSVCRT_wchar_t *dst, const char *str,
2416 MSVCRT_size_t n, MSVCRT_mbstate_t *state)
2418 MSVCRT_pthreadlocinfo locinfo = get_locinfo();
2419 MSVCRT_mbstate_t s = (state ? *state : 0);
2420 char tmpstr[2];
2421 int len = 0;
2423 if(dst)
2424 *dst = 0;
2426 if(!n || !str || !*str)
2427 return 0;
2429 if(locinfo->mb_cur_max == 1) {
2430 tmpstr[len++] = *str;
2431 }else if(!s && MSVCRT_isleadbyte((unsigned char)*str)) {
2432 if(n == 1) {
2433 s = (unsigned char)*str;
2434 len = -2;
2435 }else {
2436 tmpstr[0] = str[0];
2437 tmpstr[1] = str[1];
2438 len = 2;
2440 }else if(!s) {
2441 tmpstr[len++] = *str;
2442 }else {
2443 tmpstr[0] = s;
2444 tmpstr[1] = *str;
2445 len = 2;
2446 s = 0;
2449 if(len > 0) {
2450 if(!MultiByteToWideChar(locinfo->lc_codepage, 0, tmpstr, len, dst, dst ? 1 : 0))
2451 len = -1;
2454 if(state)
2455 *state = s;
2456 return len;
2459 /*********************************************************************
2460 * _mbstowcs_l(MSVCRT.@)
2462 MSVCRT_size_t CDECL MSVCRT__mbstowcs_l(MSVCRT_wchar_t *wcstr, const char *mbstr,
2463 MSVCRT_size_t count, MSVCRT__locale_t locale)
2465 MSVCRT_pthreadlocinfo locinfo;
2466 MSVCRT_size_t i, size;
2468 if(!mbstr) {
2469 *MSVCRT__errno() = MSVCRT_EINVAL;
2470 return -1;
2473 if(!locale)
2474 locinfo = get_locinfo();
2475 else
2476 locinfo = locale->locinfo;
2478 if(!locinfo->lc_codepage) {
2479 if(!wcstr)
2480 return strlen(mbstr);
2482 for(i=0; i<count; i++) {
2483 wcstr[i] = (unsigned char)mbstr[i];
2484 if(!wcstr[i]) break;
2486 return i;
2489 /* Ignore count parameter */
2490 if(!wcstr) {
2491 size = MultiByteToWideChar(locinfo->lc_codepage,
2492 MB_ERR_INVALID_CHARS, mbstr, -1, NULL, 0);
2493 if(!size) {
2494 *MSVCRT__errno() = MSVCRT_EILSEQ;
2495 return -1;
2497 return size - 1;
2500 for(i=0, size=0; i<count; i++) {
2501 if(mbstr[size] == '\0')
2502 break;
2504 size += (MSVCRT__isleadbyte_l((unsigned char)mbstr[size], locale) ? 2 : 1);
2507 if(size) {
2508 size = MultiByteToWideChar(locinfo->lc_codepage,
2509 MB_ERR_INVALID_CHARS, mbstr, size, wcstr, count);
2510 if(!size) {
2511 if(count) wcstr[0] = '\0';
2512 *MSVCRT__errno() = MSVCRT_EILSEQ;
2513 return -1;
2517 if(size<count)
2518 wcstr[size] = '\0';
2520 return size;
2523 /*********************************************************************
2524 * mbstowcs(MSVCRT.@)
2526 MSVCRT_size_t CDECL MSVCRT_mbstowcs(MSVCRT_wchar_t *wcstr,
2527 const char *mbstr, MSVCRT_size_t count)
2529 return MSVCRT__mbstowcs_l(wcstr, mbstr, count, NULL);
2532 /*********************************************************************
2533 * _mbstowcs_s_l(MSVCRT.@)
2535 int CDECL MSVCRT__mbstowcs_s_l(MSVCRT_size_t *ret, MSVCRT_wchar_t *wcstr,
2536 MSVCRT_size_t size, const char *mbstr,
2537 MSVCRT_size_t count, MSVCRT__locale_t locale)
2539 MSVCRT_size_t conv;
2540 int err = 0;
2542 if(!wcstr && !size) {
2543 conv = MSVCRT__mbstowcs_l(NULL, mbstr, 0, locale);
2544 if(ret)
2545 *ret = conv+1;
2546 return 0;
2549 if (!MSVCRT_CHECK_PMT(wcstr != NULL)) return MSVCRT_EINVAL;
2550 if (!MSVCRT_CHECK_PMT(mbstr != NULL)) {
2551 if(size) wcstr[0] = '\0';
2552 return MSVCRT_EINVAL;
2555 if(count==MSVCRT__TRUNCATE || size<count)
2556 conv = size;
2557 else
2558 conv = count;
2560 conv = MSVCRT__mbstowcs_l(wcstr, mbstr, conv, locale);
2561 if(conv<size)
2562 wcstr[conv++] = '\0';
2563 else if(conv==size && count==MSVCRT__TRUNCATE && wcstr[conv-1]!='\0') {
2564 wcstr[conv-1] = '\0';
2565 err = MSVCRT_STRUNCATE;
2566 }else if(conv==size && wcstr[conv-1]!='\0') {
2567 MSVCRT_INVALID_PMT("wcstr[size] is too small", MSVCRT_ERANGE);
2568 if(size)
2569 wcstr[0] = '\0';
2570 return MSVCRT_ERANGE;
2573 if(ret)
2574 *ret = conv;
2575 return err;
2578 /*********************************************************************
2579 * mbstowcs_s(MSVCRT.@)
2581 int CDECL MSVCRT__mbstowcs_s(MSVCRT_size_t *ret, MSVCRT_wchar_t *wcstr,
2582 MSVCRT_size_t size, const char *mbstr, MSVCRT_size_t count)
2584 return MSVCRT__mbstowcs_s_l(ret, wcstr, size, mbstr, count, NULL);
2587 /*********************************************************************
2588 * mbsrtowcs(MSVCRT.@)
2590 MSVCRT_size_t CDECL MSVCRT_mbsrtowcs(MSVCRT_wchar_t *wcstr,
2591 const char **pmbstr, MSVCRT_size_t count, MSVCRT_mbstate_t *state)
2593 MSVCRT_mbstate_t s = (state ? *state : 0);
2594 MSVCRT_wchar_t tmpdst;
2595 MSVCRT_size_t ret = 0;
2596 const char *p;
2598 if(!MSVCRT_CHECK_PMT(pmbstr != NULL))
2599 return -1;
2601 p = *pmbstr;
2602 while(!wcstr || count>ret) {
2603 int ch_len = MSVCRT_mbrtowc(&tmpdst, p, 2, &s);
2604 if(wcstr)
2605 wcstr[ret] = tmpdst;
2607 if(ch_len < 0) {
2608 return -1;
2609 }else if(ch_len == 0) {
2610 if(wcstr) *pmbstr = NULL;
2611 return ret;
2614 p += ch_len;
2615 ret++;
2618 if(wcstr) *pmbstr = p;
2619 return ret;
2622 /*********************************************************************
2623 * mbsrtowcs_s(MSVCRT.@)
2625 int CDECL MSVCRT_mbsrtowcs_s(MSVCRT_size_t *ret, MSVCRT_wchar_t *wcstr, MSVCRT_size_t len,
2626 const char **mbstr, MSVCRT_size_t count, MSVCRT_mbstate_t *state)
2628 MSVCRT_size_t tmp;
2630 if(!ret) ret = &tmp;
2631 if(!MSVCRT_CHECK_PMT(!!wcstr == !!len)) {
2632 *ret = -1;
2633 return MSVCRT_EINVAL;
2636 *ret = MSVCRT_mbsrtowcs(wcstr, mbstr, count>len ? len : count, state);
2637 if(*ret == -1) {
2638 if(wcstr) *wcstr = 0;
2639 return *MSVCRT__errno();
2641 (*ret)++;
2642 if(*ret > len) {
2643 /* no place for terminating '\0' */
2644 if(wcstr) *wcstr = 0;
2645 return 0;
2647 if(wcstr) wcstr[(*ret)-1] = 0;
2648 return 0;
2651 /*********************************************************************
2652 * _mbctohira (MSVCRT.@)
2654 * Converts a sjis katakana character to hiragana.
2656 unsigned int CDECL _mbctohira(unsigned int c)
2658 if(_ismbckata(c) && c <= 0x8393)
2659 return (c - 0x8340 - (c >= 0x837f ? 1 : 0)) + 0x829f;
2660 return c;
2663 /*********************************************************************
2664 * _mbctokata (MSVCRT.@)
2666 * Converts a sjis hiragana character to katakana.
2668 unsigned int CDECL _mbctokata(unsigned int c)
2670 if(_ismbchira(c))
2671 return (c - 0x829f) + 0x8340 + (c >= 0x82de ? 1 : 0);
2672 return c;
2675 /*********************************************************************
2676 * _ismbcl0_l (MSVCRT.@)
2678 int CDECL _ismbcl0_l(unsigned int c, MSVCRT__locale_t locale)
2680 MSVCRT_pthreadmbcinfo mbcinfo;
2682 if(!locale)
2683 mbcinfo = get_mbcinfo();
2684 else
2685 mbcinfo = locale->mbcinfo;
2687 if(mbcinfo->mbcodepage == 932)
2689 /* JIS non-Kanji */
2690 return _ismbclegal_l(c, locale) && c >= 0x8140 && c <= 0x889e;
2693 return 0;
2696 /*********************************************************************
2697 * _ismbcl0 (MSVCRT.@)
2699 int CDECL _ismbcl0(unsigned int c)
2701 return _ismbcl0_l(c, NULL);
2704 /*********************************************************************
2705 * _ismbcl1_l (MSVCRT.@)
2707 int CDECL _ismbcl1_l(unsigned int c, MSVCRT__locale_t locale)
2709 MSVCRT_pthreadmbcinfo mbcinfo;
2711 if(!locale)
2712 mbcinfo = get_mbcinfo();
2713 else
2714 mbcinfo = locale->mbcinfo;
2716 if(mbcinfo->mbcodepage == 932)
2718 /* JIS level-1 */
2719 return _ismbclegal_l(c, locale) && c >= 0x889f && c <= 0x9872;
2722 return 0;
2725 /*********************************************************************
2726 * _ismbcl1 (MSVCRT.@)
2728 int CDECL _ismbcl1(unsigned int c)
2730 return _ismbcl1_l(c, NULL);
2733 /*********************************************************************
2734 * _ismbcl2_l (MSVCRT.@)
2736 int CDECL _ismbcl2_l(unsigned int c, MSVCRT__locale_t locale)
2738 MSVCRT_pthreadmbcinfo mbcinfo;
2740 if(!locale)
2741 mbcinfo = get_mbcinfo();
2742 else
2743 mbcinfo = locale->mbcinfo;
2745 if(mbcinfo->mbcodepage == 932)
2747 /* JIS level-2 */
2748 return _ismbclegal_l(c, locale) && c >= 0x989f && c <= 0xeaa4;
2751 return 0;
2754 /*********************************************************************
2755 * _ismbcl2 (MSVCRT.@)
2757 int CDECL _ismbcl2(unsigned int c)
2759 return _ismbcl2_l(c, NULL);