mbcs.c 56.7 KB
Newer Older
1 2 3 4 5 6
/*
 * msvcrt.dll mbcs functions
 *
 * Copyright 1999 Alexandre Julliard
 * Copyright 2000 Jon Griffths
 *
7 8 9 10 11 12 13 14 15 16 17 18
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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
20
 *
21
 * FIXME
22
 * Not currently binary compatible with win32. MSVCRT_mbctype must be
23 24
 * populated correctly and the ismb* functions should reference it.
 */
25

26 27
#include <stdio.h>

28
#include "msvcrt.h"
29
#include "wine/unicode.h"
30 31 32
#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
33

34
unsigned char MSVCRT_mbctype[257] = { 0 };
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

/* It seems that the data about valid trail bytes is not available from kernel32
 * so we have to store is here. The format is the same as for lead bytes in CPINFO */
struct cp_extra_info_t
{
    int cp;
    BYTE TrailBytes[MAX_LEADBYTES];
};

static struct cp_extra_info_t g_cpextrainfo[] =
{
    {932, {0x40, 0x7e, 0x80, 0xfc, 0, 0}},
    {936, {0x40, 0xfe, 0, 0}},
    {949, {0x41, 0xfe, 0, 0}},
    {950, {0x40, 0x7e, 0xa1, 0xfe, 0, 0}},
50
    {1361, {0x31, 0x7e, 0x81, 0xfe, 0, 0}},
51
    {20932, {1, 255, 0, 0}},  /* seems to give different results on different systems */
52 53
    {0, {1, 255, 0, 0}}       /* match all with FIXME */
};
54

55 56 57 58 59 60 61 62 63 64 65 66 67
/* Maps cp932 single byte character to multi byte character */
static const unsigned char mbbtombc_932[] = {
  0x40,0x49,0x68,0x94,0x90,0x93,0x95,0x66,0x69,0x6a,0x96,0x7b,0x43,0x7c,0x44,0x5e,
  0x4f,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x46,0x47,0x83,0x81,0x84,0x48,
  0x97,0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,
  0x6f,0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x6d,0x8f,0x6e,0x4f,0x76,
  0x77,0x78,0x79,0x6d,0x8f,0x6e,0x4f,0x51,0x65,0x81,0x82,0x83,0x84,0x85,0x86,0x87,
  0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f,0x90,0x91,0x92,0x93,0x94,0x95,0x50,
       0x42,0x75,0x76,0x41,0x45,0x92,0x40,0x42,0x44,0x46,0x48,0x83,0x85,0x87,0x62,
  0x5b,0x41,0x43,0x45,0x47,0x49,0x4a,0x4c,0x4e,0x50,0x52,0x54,0x56,0x58,0x5a,0x5c,
  0x5e,0x60,0x63,0x65,0x67,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x71,0x74,0x77,0x7a,0x7d,
  0x7e,0x80,0x81,0x82,0x84,0x86,0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8f,0x93,0x4a,0x4b };

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
/* Maps multibyte cp932 punctuation marks to single byte equivalents */
static const unsigned char mbctombb_932_punct[] = {
  0x20,0xa4,0xa1,0x2c,0x2e,0xa5,0x3a,0x3b,0x3f,0x21,0xde,0xdf,0x00,0x00,0x00,0x5e,
  0x7e,0x5f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xb0,0x00,0x00,0x2f,0x00,
  0x00,0x00,0x7c,0x00,0x00,0x60,0x27,0x00,0x22,0x28,0x29,0x00,0x00,0x5b,0x5d,0x7b,
  0x7d,0x00,0x00,0x00,0x00,0xa2,0xa3,0x00,0x00,0x00,0x00,0x2b,0x2d,0x00,0x00,0x00,
  0x00,0x3d,0x00,0x3c,0x3e,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x5c,
  0x24,0x00,0x00,0x25,0x23,0x26,0x2a,0x40};

/* Maps multibyte cp932 hiragana/katakana to single-byte equivalents */
static const unsigned char mbctombb_932_kana[] = {
  0xa7,0xb1,0xa8,0xb2,0xa9,0xb3,0xaa,0xb4,0xab,0xb5,0xb6,0xb6,0xb7,0xb7,0xb8,0xb8,
  0xb9,0xb9,0xba,0xba,0xbb,0xbb,0xbc,0xbc,0xbd,0xbd,0xbe,0xbe,0xbf,0xbf,0xc0,0xc0,
  0xc1,0xc1,0xaf,0xc2,0xc2,0xc3,0xc3,0xc4,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xca,
  0xca,0xcb,0xcb,0xcb,0xcc,0xcc,0xcc,0xcd,0xcd,0xcd,0xce,0xce,0xce,0xcf,0xd0,0xd1,
  0xd2,0xd3,0xac,0xd4,0xad,0xd5,0xae,0xd6,0xd7,0xd8,0xd9,0xda,0xdb,0xdc,0xdc,0xb2,
  0xb4,0xa6,0xdd,0xb3,0xb6,0xb9};

86
static MSVCRT_wchar_t msvcrt_mbc_to_wc(unsigned int ch)
87
{
88
  MSVCRT_wchar_t chW;
89 90 91 92 93 94 95 96 97 98 99
  char mbch[2];
  int n_chars;

  if (ch <= 0xff) {
    mbch[0] = ch;
    n_chars = 1;
  } else {
    mbch[0] = (ch >> 8) & 0xff;
    mbch[1] = ch & 0xff;
    n_chars = 2;
  }
100
  if (!MultiByteToWideChar(get_mbcinfo()->mbcodepage, 0, mbch, n_chars, &chW, 1))
101 102 103 104 105 106 107
  {
    WARN("MultiByteToWideChar failed on %x\n", ch);
    return 0;
  }
  return chW;
}

108
static inline MSVCRT_size_t u_strlen( const unsigned char *str )
Mike McCormack's avatar
Mike McCormack committed
109 110 111 112
{
  return strlen( (const char*) str );
}

113
static inline unsigned char* u_strncat( unsigned char* dst, const unsigned char* src, MSVCRT_size_t len )
Mike McCormack's avatar
Mike McCormack committed
114 115 116 117 118 119 120 121 122 123 124 125 126 127
{
  return (unsigned char*)strncat( (char*)dst, (const char*)src, len);
}

static inline int u_strcmp( const unsigned char *s1, const unsigned char *s2 )
{
  return strcmp( (const char*)s1, (const char*)s2 );
}

static inline int u_strcasecmp( const unsigned char *s1, const unsigned char *s2 )
{
  return strcasecmp( (const char*)s1, (const char*)s2 );
}

128
static inline int u_strncmp( const unsigned char *s1, const unsigned char *s2, MSVCRT_size_t len )
Mike McCormack's avatar
Mike McCormack committed
129 130 131 132
{
  return strncmp( (const char*)s1, (const char*)s2, len );
}

133
static inline int u_strncasecmp( const unsigned char *s1, const unsigned char *s2, MSVCRT_size_t len )
Mike McCormack's avatar
Mike McCormack committed
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
{
  return strncasecmp( (const char*)s1, (const char*)s2, len );
}

static inline unsigned char *u_strchr( const unsigned char *s, unsigned char x )
{
  return (unsigned char*) strchr( (const char*)s, x );
}

static inline unsigned char *u_strrchr( const unsigned char *s, unsigned char x )
{
  return (unsigned char*) strrchr( (const char*)s, x );
}

static inline unsigned char *u__strset( unsigned char *s, unsigned char c )
{
  return (unsigned char*) _strset( (char*)s, c);
}

153
static inline unsigned char *u__strnset( unsigned char *s, unsigned char c, MSVCRT_size_t len )
Mike McCormack's avatar
Mike McCormack committed
154
{
155
  return (unsigned char*) MSVCRT__strnset( (char*)s, c, len );
Mike McCormack's avatar
Mike McCormack committed
156 157
}

158
static inline MSVCRT_size_t u_strcspn( const unsigned char *s, const unsigned char *rej )
Mike McCormack's avatar
Mike McCormack committed
159 160 161 162
{
  return strcspn( (const char *)s, (const char*)rej );
}

163 164 165
/*********************************************************************
 *		__p__mbctype (MSVCRT.@)
 */
166
unsigned char* CDECL __p__mbctype(void)
167
{
168
  return get_mbcinfo()->mbctype;
169 170 171
}

/*********************************************************************
172
 *		__p___mb_cur_max(MSVCRT.@)
173
 */
174
int* CDECL __p___mb_cur_max(void)
175
{
176
  return &get_locinfo()->mb_cur_max;
177 178
}

179 180 181 182 183 184 185 186
/*********************************************************************
 *		___mb_cur_max_func(MSVCRT.@)
 */
int CDECL MSVCRT____mb_cur_max_func(void)
{
  return get_locinfo()->mb_cur_max;
}

187 188 189
/*********************************************************************
 *		___mb_cur_max_l_func  (MSVCR80.@)
 */
190 191
int* CDECL ___mb_cur_max_l_func(MSVCRT__locale_t locale)
{
192 193
  MSVCRT_pthreadlocinfo locinfo;

194
  if(!locale)
195 196 197
    locinfo = get_locinfo();
  else
    locinfo = locale->locinfo;
198

199
  return &locinfo->mb_cur_max;
200 201
}

202
/*********************************************************************
203
 * INTERNAL: _setmbcp_l
204
 */
205
int _setmbcp_l(int cp, LCID lcid, MSVCRT_pthreadmbcinfo mbcinfo)
206
{
207 208
  const char format[] = ".%d";

209 210 211 212 213 214 215 216 217 218
  int newcp;
  CPINFO cpi;
  BYTE *bytes;
  WORD chartypes[256];
  char bufA[256];
  WCHAR bufW[256];
  int charcount;
  int ret;
  int i;

219 220 221
  if(!mbcinfo)
      mbcinfo = get_mbcinfo();

222 223 224 225 226 227 228 229 230
  switch (cp)
  {
    case _MB_CP_ANSI:
      newcp = GetACP();
      break;
    case _MB_CP_OEM:
      newcp = GetOEMCP();
      break;
    case _MB_CP_LOCALE:
231
      newcp = get_locinfo()->lc_codepage;
232 233 234
      if(newcp)
          break;
      /* fall through (C locale) */
235 236 237 238 239 240 241 242
    case _MB_CP_SBCS:
      newcp = 20127;   /* ASCII */
      break;
    default:
      newcp = cp;
      break;
  }

243 244
  if(lcid == -1) {
    sprintf(bufA, format, newcp);
245
    mbcinfo->mblcid = MSVCRT_locale_to_LCID(bufA, NULL);
246 247 248 249
  } else {
    mbcinfo->mblcid = lcid;
  }

250 251 252 253 254 255
  if(mbcinfo->mblcid == -1)
  {
    WARN("Can't assign LCID to codepage (%d)\n", mbcinfo->mblcid);
    mbcinfo->mblcid = 0;
  }

256 257 258
  if (!GetCPInfo(newcp, &cpi))
  {
    WARN("Codepage %d not found\n", newcp);
259
    *MSVCRT__errno() = MSVCRT_EINVAL;
260 261 262 263
    return -1;
  }

  /* setup the _mbctype */
264 265
  memset(mbcinfo->mbctype, 0, sizeof(unsigned char[257]));
  memset(mbcinfo->mbcasemap, 0, sizeof(unsigned char[256]));
266 267 268 269 270

  bytes = cpi.LeadByte;
  while (bytes[0] || bytes[1])
  {
    for (i = bytes[0]; i <= bytes[1]; i++)
271
      mbcinfo->mbctype[i + 1] |= _M1;
272 273 274 275 276 277 278
    bytes += 2;
  }

  if (cpi.MaxCharSize > 1)
  {
    /* trail bytes not available through kernel32 but stored in a structure in msvcrt */
    struct cp_extra_info_t *cpextra = g_cpextrainfo;
279

280
    mbcinfo->ismbcodepage = 1;
281 282 283 284 285 286 287 288 289 290 291
    while (TRUE)
    {
      if (cpextra->cp == 0 || cpextra->cp == newcp)
      {
        if (cpextra->cp == 0)
          FIXME("trail bytes data not available for DBCS codepage %d - assuming all bytes\n", newcp);

        bytes = cpextra->TrailBytes;
        while (bytes[0] || bytes[1])
        {
          for (i = bytes[0]; i <= bytes[1]; i++)
292
            mbcinfo->mbctype[i + 1] |= _M2;
293 294 295 296 297 298 299
          bytes += 2;
        }
        break;
      }
      cpextra++;
    }
  }
300
  else
301
    mbcinfo->ismbcodepage = 0;
302 303 304 305 306

  /* we can't use GetStringTypeA directly because we don't have a locale - only a code page
   */
  charcount = 0;
  for (i = 0; i < 256; i++)
307
    if (!(mbcinfo->mbctype[i + 1] & _M1))
308 309 310 311 312 313 314 315
      bufA[charcount++] = i;

  ret = MultiByteToWideChar(newcp, 0, bufA, charcount, bufW, charcount);
  if (ret != charcount)
    ERR("MultiByteToWideChar of chars failed for cp %d, ret=%d (exp %d), error=%d\n", newcp, ret, charcount, GetLastError());

  GetStringTypeW(CT_CTYPE1, bufW, charcount, chartypes);

316
  charcount = 0;
317
  for (i = 0; i < 256; i++)
318
    if (!(mbcinfo->mbctype[i + 1] & _M1))
319
    {
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
      if (chartypes[charcount] & C1_UPPER)
      {
        mbcinfo->mbctype[i + 1] |= _SBUP;
        bufW[charcount] = tolowerW(bufW[charcount]);
      }
      else if (chartypes[charcount] & C1_LOWER)
      {
	mbcinfo->mbctype[i + 1] |= _SBLOW;
        bufW[charcount] = toupperW(bufW[charcount]);
      }
      charcount++;
    }

  ret = WideCharToMultiByte(newcp, 0, bufW, charcount, bufA, charcount, NULL, NULL);
  if (ret != charcount)
    ERR("WideCharToMultiByte failed for cp %d, ret=%d (exp %d), error=%d\n", newcp, ret, charcount, GetLastError());

  charcount = 0;
  for (i = 0; i < 256; i++)
  {
    if(!(mbcinfo->mbctype[i + 1] & _M1))
    {
      if(mbcinfo->mbctype[i] & (C1_UPPER|C1_LOWER))
        mbcinfo->mbcasemap[i] = bufA[charcount];
      charcount++;
345
    }
346
  }
347 348 349 350 351 352 353 354 355

  if (newcp == 932)   /* CP932 only - set _MP and _MS */
  {
    /* On Windows it's possible to calculate the _MP and _MS from CT_CTYPE1
     * and CT_CTYPE3. But as of Wine 0.9.43 we return wrong values what makes
     * it hard. As this is set only for codepage 932 we hardcode it what gives
     * also faster execution.
     */
    for (i = 161; i <= 165; i++)
356
      mbcinfo->mbctype[i + 1] |= _MP;
357
    for (i = 166; i <= 223; i++)
358
      mbcinfo->mbctype[i + 1] |= _MS;
359 360
  }

361
  mbcinfo->mbcodepage = newcp;
362
  if(MSVCRT_locale && mbcinfo == MSVCRT_locale->mbcinfo)
363 364
    memcpy(MSVCRT_mbctype, MSVCRT_locale->mbcinfo->mbctype, sizeof(MSVCRT_mbctype));

365 366 367
  return 0;
}

368 369 370 371 372
/*********************************************************************
 *              _setmbcp (MSVCRT.@)
 */
int CDECL _setmbcp(int cp)
{
373
    return _setmbcp_l(cp, -1, NULL);
374 375
}

376 377 378 379 380
/*********************************************************************
 *		_getmbcp (MSVCRT.@)
 */
int CDECL _getmbcp(void)
{
381
  return get_mbcinfo()->mbcodepage;
382 383
}

384 385 386
/*********************************************************************
 *		_mbsnextc(MSVCRT.@)
 */
387
unsigned int CDECL _mbsnextc(const unsigned char* str)
388
{
389
  if(_ismbblead(*str))
390
    return *str << 8 | str[1];
391
  return *str;
392 393
}

394 395 396
/*********************************************************************
 *		_mbctolower(MSVCRT.@)
 */
397
unsigned int CDECL _mbctolower(unsigned int c)
398
{
399
    if (_ismbblead(c))
400 401 402 403 404 405 406 407 408 409
    {
      FIXME("Handle MBC chars\n");
      return c;
    }
    return tolower(c); /* ASCII CP or SB char */
}

/*********************************************************************
 *		_mbctoupper(MSVCRT.@)
 */
410
unsigned int CDECL _mbctoupper(unsigned int c)
411
{
412
    if (_ismbblead(c))
413 414 415 416 417 418 419
    {
      FIXME("Handle MBC chars\n");
      return c;
    }
    return toupper(c); /* ASCII CP or SB char */
}

420 421 422 423 424 425 426
/*********************************************************************
 *		_mbctombb (MSVCRT.@)
 */
unsigned int CDECL _mbctombb(unsigned int c)
{
    unsigned int value;

427
    if(get_mbcinfo()->mbcodepage == 932)
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
    {
        if(c >= 0x829f && c <= 0x82f1)    /* Hiragana */
            return mbctombb_932_kana[c - 0x829f];
        if(c >= 0x8340 && c <= 0x8396 && c != 0x837f)    /* Katakana */
            return mbctombb_932_kana[c - 0x8340 - (c >= 0x837f ? 1 : 0)];
        if(c >= 0x8140 && c <= 0x8197)    /* Punctuation */
        {
            value = mbctombb_932_punct[c - 0x8140];
            return value ? value : c;
        }
        if((c >= 0x824f && c <= 0x8258) || /* Fullwidth digits */
           (c >= 0x8260 && c <= 0x8279))   /* Fullwidth capitals letters */
            return c - 0x821f;
        if(c >= 0x8281 && c <= 0x829a)     /* Fullwidth small letters */
            return c - 0x8220;
        /* all other cases return c */
    }
    return c;
}

448 449 450 451 452 453 454 455 456 457 458
/*********************************************************************
 *		_mbcjistojms(MSVCRT.@)
 *
 *		Converts a jis character to sjis.
 *		Based on description from
 *		http://www.slayers.ne.jp/~oouchi/code/jistosjis.html
 */
unsigned int CDECL _mbcjistojms(unsigned int c)
{
  /* Conversion takes place only when codepage is 932.
     In all other cases, c is returned unchanged */
459
  if(get_mbcinfo()->mbcodepage == 932)
460 461 462 463 464 465 466 467 468
  {
    if(HIBYTE(c) >= 0x21 && HIBYTE(c) <= 0x7e &&
       LOBYTE(c) >= 0x21 && LOBYTE(c) <= 0x7e)
    {
      if(HIBYTE(c) % 2)
        c += 0x1f;
      else
        c += 0x7d;

469
      if(LOBYTE(c) >= 0x7F)
470 471 472 473 474 475 476 477 478 479 480 481 482 483
        c += 0x1;

      c = (((HIBYTE(c) - 0x21)/2 + 0x81) << 8) | LOBYTE(c);

      if(HIBYTE(c) > 0x9f)
        c += 0x4000;
    }
    else
      return 0; /* Codepage is 932, but c can't be converted */
  }

  return c;
}

484 485 486 487 488 489 490 491 492 493 494
/*********************************************************************
 *		_mbcjmstojis(MSVCRT.@)
 *
 *		Converts a sjis character to jis.
 */
unsigned int CDECL _mbcjmstojis(unsigned int c)
{
  /* Conversion takes place only when codepage is 932.
     In all other cases, c is returned unchanged */
  if(get_mbcinfo()->mbcodepage == 932)
  {
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
    if(_ismbclegal(c) && HIBYTE(c) < 0xf0)
    {
      if(HIBYTE(c) >= 0xe0)
        c -= 0x4000;

      c = (((HIBYTE(c) - 0x81)*2 + 0x21) << 8) | LOBYTE(c);

      if(LOBYTE(c) > 0x7f)
        c -= 0x1;

      if(LOBYTE(c) > 0x9d)
        c += 0x83;
      else
        c -= 0x1f;
    }
    else
      return 0; /* Codepage is 932, but c can't be converted */
512 513 514 515 516
  }

  return c;
}

517 518 519
/*********************************************************************
 *		_mbsdec(MSVCRT.@)
 */
520
unsigned char* CDECL _mbsdec(const unsigned char* start, const unsigned char* cur)
521
{
522
  if(get_mbcinfo()->ismbcodepage)
523
    return (unsigned char *)(_ismbstrail(start,cur-1) ? cur - 2 : cur -1);
524

525
  return (unsigned char *)cur - 1; /* ASCII CP or SB char */
526 527
}

528 529 530 531 532 533 534 535
/*********************************************************************
 *		_mbclen(MSVCRT.@)
 */
unsigned int CDECL _mbclen(const unsigned char* str)
{
  return _ismbblead(*str) ? 2 : 1;
}

536 537 538
/*********************************************************************
 *		_mbsinc(MSVCRT.@)
 */
539
unsigned char* CDECL _mbsinc(const unsigned char* str)
540
{
541
  return (unsigned char *)(str + _mbclen(str));
542 543 544 545 546
}

/*********************************************************************
 *		_mbsninc(MSVCRT.@)
 */
547
unsigned char* CDECL _mbsninc(const unsigned char* str, MSVCRT_size_t num)
548
{
549
  if(!str)
550
    return NULL;
551 552

  while (num > 0 && *str)
553
  {
554 555 556 557 558 559 560 561
    if (_ismbblead(*str))
    {
      if (!*(str+1))
         break;
      str++;
    }
    str++;
    num--;
562
  }
563 564

  return (unsigned char*)str;
565 566 567 568 569
}

/*********************************************************************
 *		_mbslen(MSVCRT.@)
 */
570
MSVCRT_size_t CDECL _mbslen(const unsigned char* str)
571
{
572 573
  MSVCRT_size_t len = 0;
  while(*str)
574
  {
575
    if (_ismbblead(*str))
576
    {
577 578 579
      str++;
      if (!*str)  /* count only full chars */
        break;
580
    }
581 582
    str++;
    len++;
583
  }
584
  return len;
585 586 587 588 589
}

/*********************************************************************
 *		_mbccpy(MSVCRT.@)
 */
590
void CDECL _mbccpy(unsigned char* dest, const unsigned char* src)
591
{
592
  *dest = *src;
593
  if(_ismbblead(*src))
594
    *++dest = *++src; /* MB char */
595 596 597 598
}

/*********************************************************************
 *		_mbsncpy(MSVCRT.@)
599 600 601
 * REMARKS
 *  The parameter n is the number or characters to copy, not the size of
 *  the buffer. Use _mbsnbcpy for a function analogical to strncpy
602
 */
603
unsigned char* CDECL _mbsncpy(unsigned char* dst, const unsigned char* src, MSVCRT_size_t n)
604
{
605
  unsigned char* ret = dst;
606 607
  if(!n)
    return dst;
608
  if (get_mbcinfo()->ismbcodepage)
609
  {
610
    while (*src && n)
611
    {
612
      n--;
613 614 615 616 617 618 619 620 621 622 623 624 625
      if (_ismbblead(*src))
      {
        if (!*(src+1))
        {
            *dst++ = 0;
            *dst++ = 0;
            break;
        }

        *dst++ = *src++;
      }

      *dst++ = *src++;
626 627
    }
  }
628 629 630 631 632 633 634 635 636 637
  else
  {
    while (n)
    {
        n--;
        if (!(*dst++ = *src++)) break;
    }
  }
  while (n--) *dst++ = 0;
  return ret;
638 639
}

640
/*********************************************************************
641
 *              _mbsnbcpy_s_l(MSVCRT.@)
642 643 644 645
 * REMARKS
 * Unlike _mbsnbcpy this function does not pad the rest of the dest
 * string with 0
 */
646 647
int CDECL _mbsnbcpy_s_l(unsigned char* dst, MSVCRT_size_t size,
        const unsigned char* src, MSVCRT_size_t n, MSVCRT__locale_t locale)
648 649 650 651
{
    MSVCRT_size_t pos = 0;

    if(!dst || size == 0)
652
        return MSVCRT_EINVAL;
653 654 655
    if(!src)
    {
        dst[0] = '\0';
656
        return MSVCRT_EINVAL;
657 658 659 660
    }
    if(!n)
        return 0;

661
    if((locale ? locale->mbcinfo : get_mbcinfo())->ismbcodepage)
662 663 664 665 666 667 668
    {
        int is_lead = 0;
        while (*src && n)
        {
            if(pos == size)
            {
                dst[0] = '\0';
669
                return MSVCRT_ERANGE;
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686
            }
            is_lead = (!is_lead && _ismbblead(*src));
            n--;
            dst[pos++] = *src++;
        }

        if (is_lead) /* if string ends with a lead, remove it */
            dst[pos - 1] = 0;
    }
    else
    {
        while (n)
        {
            n--;
            if(pos == size)
            {
                dst[0] = '\0';
687
                return MSVCRT_ERANGE;
688 689 690 691 692 693 694 695 696 697 698 699
            }

            if(!(*src)) break;
            dst[pos++] = *src++;
        }
    }

    if(pos < size)
        dst[pos] = '\0';
    else
    {
        dst[0] = '\0';
700
        return MSVCRT_ERANGE;
701 702 703 704 705
    }

    return 0;
}

706 707 708 709 710 711 712 713
/*********************************************************************
 *              _mbsnbcpy_s(MSVCRT.@)
 */
int CDECL _mbsnbcpy_s(unsigned char* dst, MSVCRT_size_t size, const unsigned char* src, MSVCRT_size_t n)
{
    return _mbsnbcpy_s_l(dst, size, src, n, NULL);
}

714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
/*********************************************************************
 *              _mbscpy_s_l(MSVCRT.@)
 */
int CDECL _mbscpy_s_l(unsigned char *dst, MSVCRT_size_t size,
        const unsigned char *src, MSVCRT__locale_t locale)
{
    return _mbsnbcpy_s_l(dst, size, src, -1, locale);
}

/*********************************************************************
 *              _mbscpy_s(MSVCRT.@)
 */
int CDECL _mbscpy_s(unsigned char *dst, MSVCRT_size_t size, const unsigned char *src)
{
    return _mbscpy_s_l(dst, size, src, NULL);
}

731 732
/*********************************************************************
 *              _mbsnbcpy(MSVCRT.@)
733 734 735
 * REMARKS
 *  Like strncpy this function doesn't enforce the string to be
 *  NUL-terminated
736
 */
737
unsigned char* CDECL _mbsnbcpy(unsigned char* dst, const unsigned char* src, MSVCRT_size_t n)
738
{
739
  unsigned char* ret = dst;
740 741
  if(!n)
    return dst;
742
  if(get_mbcinfo()->ismbcodepage)
743
  {
744 745
    int is_lead = 0;
    while (*src && n)
746
    {
747
      is_lead = (!is_lead && _ismbblead(*src));
748
      n--;
749
      *dst++ = *src++;
750
    }
751 752 753

    if (is_lead) /* if string ends with a lead, remove it */
	*(dst - 1) = 0;
754
  }
755 756 757 758 759 760 761 762 763 764
  else
  {
    while (n)
    {
        n--;
        if (!(*dst++ = *src++)) break;
    }
  }
  while (n--) *dst++ = 0;
  return ret;
765 766
}

767 768 769
/*********************************************************************
 *		_mbscmp(MSVCRT.@)
 */
770
int CDECL _mbscmp(const unsigned char* str, const unsigned char* cmp)
771
{
772
  if(get_mbcinfo()->ismbcodepage)
773 774 775 776 777 778 779
  {
    unsigned int strc, cmpc;
    do {
      if(!*str)
        return *cmp ? -1 : 0;
      if(!*cmp)
        return 1;
780 781
      strc = _mbsnextc(str);
      cmpc = _mbsnextc(cmp);
782 783 784 785 786 787
      if(strc != cmpc)
        return strc < cmpc ? -1 : 1;
      str +=(strc > 255) ? 2 : 1;
      cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
    } while(1);
  }
Mike McCormack's avatar
Mike McCormack committed
788
  return u_strcmp(str, cmp); /* ASCII CP */
789 790
}

791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823
/*********************************************************************
 *              _mbsnbicoll_l(MSVCRT.@)
 */
int CDECL _mbsnbicoll_l(const unsigned char *str1, const unsigned char *str2, MSVCRT_size_t len, MSVCRT__locale_t locale)
{
    MSVCRT_pthreadmbcinfo mbcinfo;

    if(!locale)
        mbcinfo = get_mbcinfo();
    else
        mbcinfo = locale->mbcinfo;

    if(!mbcinfo->ismbcodepage)
        return MSVCRT__strnicoll_l((const char*)str1, (const char*)str2, len, locale);
    return CompareStringA(mbcinfo->mblcid, NORM_IGNORECASE, (const char*)str1, len, (const char*)str2, len)-CSTR_EQUAL;
}

/*********************************************************************
 *              _mbsicoll_l(MSVCRT.@)
 */
int CDECL _mbsicoll_l(const unsigned char *str1, const unsigned char *str2, MSVCRT__locale_t locale)
{
    return _mbsnbicoll_l(str1, str2, -1, locale);
}

/*********************************************************************
 *              _mbsnbicoll(MSVCRT.@)
 */
int CDECL _mbsnbicoll(const unsigned char *str1, const unsigned char *str2, MSVCRT_size_t len)
{
    return _mbsnbicoll_l(str1, str2, len, NULL);
}

824 825 826
/*********************************************************************
 *		_mbsicoll(MSVCRT.@)
 */
827
int CDECL _mbsicoll(const unsigned char* str, const unsigned char* cmp)
828
{
829
    return _mbsnbicoll_l(str, cmp, -1, NULL);
830 831
}

832 833 834 835 836 837 838 839 840 841 842 843 844
/*********************************************************************
 *              _mbsnbcoll_l(MSVCRT.@)
 */
int CDECL _mbsnbcoll_l(const unsigned char *str1, const unsigned char *str2, MSVCRT_size_t len, MSVCRT__locale_t locale)
{
    MSVCRT_pthreadmbcinfo mbcinfo;

    if(!locale)
        mbcinfo = get_mbcinfo();
    else
        mbcinfo = locale->mbcinfo;

    if(!mbcinfo->ismbcodepage)
845
        return MSVCRT__strncoll_l((const char*)str1, (const char*)str2, len, locale);
846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
    return CompareStringA(mbcinfo->mblcid, 0, (const char*)str1, len, (const char*)str2, len)-CSTR_EQUAL;
}

/*********************************************************************
 *              _mbscoll_l(MSVCRT.@)
 */
int CDECL _mbscoll_l(const unsigned char *str1, const unsigned char *str2, MSVCRT__locale_t locale)
{
    return _mbsnbcoll_l(str1, str2, -1, locale);
}

/*********************************************************************
 *              _mbsnbcoll(MSVCRT.@)
 */
int CDECL _mbsnbcoll(const unsigned char *str1, const unsigned char *str2, MSVCRT_size_t len)
{
    return _mbsnbcoll_l(str1, str2, len, NULL);
}

865 866 867
/*********************************************************************
 *		_mbscoll(MSVCRT.@)
 */
868
int CDECL _mbscoll(const unsigned char* str, const unsigned char* cmp)
869
{
870
    return _mbsnbcoll_l(str, cmp, -1, NULL);
871 872
}

873 874 875
/*********************************************************************
 *		_mbsicmp(MSVCRT.@)
 */
876
int CDECL _mbsicmp(const unsigned char* str, const unsigned char* cmp)
877
{
878
  if(get_mbcinfo()->ismbcodepage)
879 880 881 882 883 884 885 886 887 888 889 890 891 892 893
  {
    unsigned int strc, cmpc;
    do {
      if(!*str)
        return *cmp ? -1 : 0;
      if(!*cmp)
        return 1;
      strc = _mbctolower(_mbsnextc(str));
      cmpc = _mbctolower(_mbsnextc(cmp));
      if(strc != cmpc)
        return strc < cmpc ? -1 : 1;
      str +=(strc > 255) ? 2 : 1;
      cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
    } while(1);
  }
Mike McCormack's avatar
Mike McCormack committed
894
  return u_strcasecmp(str, cmp); /* ASCII CP */
895 896 897
}

/*********************************************************************
898
 *		_mbsncmp(MSVCRT.@)
899
 */
900
int CDECL _mbsncmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
901 902 903 904
{
  if(!len)
    return 0;

905
  if(get_mbcinfo()->ismbcodepage)
906 907 908 909
  {
    unsigned int strc, cmpc;
    while(len--)
    {
910
      int inc;
911 912 913 914
      if(!*str)
        return *cmp ? -1 : 0;
      if(!*cmp)
        return 1;
915 916
      strc = _mbsnextc(str);
      cmpc = _mbsnextc(cmp);
917 918
      if(strc != cmpc)
        return strc < cmpc ? -1 : 1;
919 920 921
      inc=(strc > 255) ? 2 : 1; /* Equal, use same increment */
      str += inc;
      cmp += inc;
922 923 924
    }
    return 0; /* Matched len chars */
  }
Mike McCormack's avatar
Mike McCormack committed
925
  return u_strncmp(str, cmp, len); /* ASCII CP */
926 927 928
}

/*********************************************************************
929
 *              _mbsnbcmp(MSVCRT.@)
930
 */
931
int CDECL _mbsnbcmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
932
{
933 934
  if (!len)
    return 0;
935
  if(get_mbcinfo()->ismbcodepage)
936 937 938 939 940 941 942 943 944
  {
    unsigned int strc, cmpc;
    while (len)
    {
      int clen;
      if(!*str)
        return *cmp ? -1 : 0;
      if(!*cmp)
        return 1;
945
      if (_ismbblead(*str))
946 947 948 949 950 951 952 953 954
      {
        strc=(len>=2)?_mbsnextc(str):0;
        clen=2;
      }
      else
      {
        strc=*str;
        clen=1;
      }
955
      if (_ismbblead(*cmp))
956 957
        cmpc=(len>=2)?_mbsnextc(cmp):0;
      else
958
        cmpc=*cmp;
959 960 961 962 963 964 965 966
      if(strc != cmpc)
        return strc < cmpc ? -1 : 1;
      len -= clen;
      str += clen;
      cmp += clen;
    }
    return 0; /* Matched len chars */
  }
Mike McCormack's avatar
Mike McCormack committed
967
  return u_strncmp(str,cmp,len);
968 969 970
}

/*********************************************************************
971 972 973
 *		_mbsnicmp(MSVCRT.@)
 *
 * Compare two multibyte strings case insensitively to 'len' characters.
974
 */
975
int CDECL _mbsnicmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
976
{
977
  /* FIXME: No tolower() for mb strings yet */
978
  if(get_mbcinfo()->ismbcodepage)
979
  {
980 981 982 983 984 985 986 987 988 989 990 991 992 993 994
    unsigned int strc, cmpc;
    while(len--)
    {
      if(!*str)
        return *cmp ? -1 : 0;
      if(!*cmp)
        return 1;
      strc = _mbctolower(_mbsnextc(str));
      cmpc = _mbctolower(_mbsnextc(cmp));
      if(strc != cmpc)
        return strc < cmpc ? -1 : 1;
      str +=(strc > 255) ? 2 : 1;
      cmp +=(strc > 255) ? 2 : 1; /* Equal, use same increment */
    }
    return 0; /* Matched len chars */
995
  }
Mike McCormack's avatar
Mike McCormack committed
996
  return u_strncasecmp(str, cmp, len); /* ASCII CP */
997 998
}

999 1000 1001
/*********************************************************************
 *              _mbsnbicmp(MSVCRT.@)
 */
1002
int CDECL _mbsnbicmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
1003 1004 1005
{
  if (!len)
    return 0;
1006
  if(get_mbcinfo()->ismbcodepage)
1007 1008 1009 1010 1011 1012 1013 1014 1015
  {
    unsigned int strc, cmpc;
    while (len)
    {
      int clen;
      if(!*str)
        return *cmp ? -1 : 0;
      if(!*cmp)
        return 1;
1016
      if (_ismbblead(*str))
1017 1018 1019 1020 1021 1022 1023 1024 1025
      {
        strc=(len>=2)?_mbsnextc(str):0;
        clen=2;
      }
      else
      {
        strc=*str;
        clen=1;
      }
1026
      if (_ismbblead(*cmp))
1027 1028
        cmpc=(len>=2)?_mbsnextc(cmp):0;
      else
1029
        cmpc=*cmp;
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
      strc = _mbctolower(strc);
      cmpc = _mbctolower(cmpc);
      if(strc != cmpc)
        return strc < cmpc ? -1 : 1;
      len -= clen;
      str += clen;
      cmp += clen;
    }
    return 0; /* Matched len bytes */
  }
1040
  return u_strncasecmp(str,cmp,len);
1041 1042
}

1043 1044 1045
/*********************************************************************
 *		_mbscat (MSVCRT.@)
 */
1046
unsigned char * CDECL _mbscat( unsigned char *dst, const unsigned char *src )
1047 1048 1049 1050 1051 1052 1053 1054
{
    strcat( (char *)dst, (const char *)src );
    return dst;
}

/*********************************************************************
 *		_mbscpy (MSVCRT.@)
 */
1055
unsigned char* CDECL _mbscpy( unsigned char *dst, const unsigned char *src )
1056 1057 1058 1059 1060 1061 1062 1063
{
    strcpy( (char *)dst, (const char *)src );
    return dst;
}

/*********************************************************************
 *		_mbsstr (MSVCRT.@)
 */
1064
unsigned char * CDECL _mbsstr(const unsigned char *haystack, const unsigned char *needle)
1065 1066 1067 1068
{
    return (unsigned char *)strstr( (const char *)haystack, (const char *)needle );
}

1069
/*********************************************************************
1070 1071 1072
 *		_mbschr(MSVCRT.@)
 *
 * Find a multibyte character in a multibyte string.
1073
 */
1074
unsigned char* CDECL _mbschr(const unsigned char* s, unsigned int x)
1075
{
1076
  if(get_mbcinfo()->ismbcodepage)
1077
  {
1078 1079
    unsigned int c;
    while (1)
1080
    {
1081 1082 1083 1084 1085 1086
      c = _mbsnextc(s);
      if (c == x)
        return (unsigned char*)s;
      if (!c)
        return NULL;
      s += c > 255 ? 2 : 1;
1087 1088
    }
  }
Mike McCormack's avatar
Mike McCormack committed
1089
  return u_strchr(s, x); /* ASCII CP */
1090 1091 1092 1093 1094
}

/*********************************************************************
 *		_mbsrchr(MSVCRT.@)
 */
1095
unsigned char* CDECL _mbsrchr(const unsigned char* s, unsigned int x)
1096
{
1097
  if(get_mbcinfo()->ismbcodepage)
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
  {
    unsigned int c;
    unsigned char* match=NULL;
    if(!s)
      return NULL;
    while (1) {
      c = _mbsnextc(s);
      if (c == x)
        match=(unsigned char*)s;
      if (!c)
        return match;
      s +=(c > 255) ? 2 : 1;
    }
  }
Mike McCormack's avatar
Mike McCormack committed
1112
  return u_strrchr(s, x);
1113 1114
}

1115
/*********************************************************************
1116
 *              _mbstok_s_l(MSVCRT.@)
1117
 */
1118 1119
unsigned char* CDECL _mbstok_s_l(unsigned char *str, const unsigned char *delim,
        unsigned char **ctx, MSVCRT__locale_t locale)
1120
{
1121 1122
    MSVCRT_pthreadmbcinfo mbcinfo;
    unsigned int c;
1123

1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150
    if(!MSVCRT_CHECK_PMT(delim != NULL)) return NULL;
    if(!MSVCRT_CHECK_PMT(ctx != NULL)) return NULL;
    if(!MSVCRT_CHECK_PMT(str || *ctx)) return NULL;

    if(locale)
        mbcinfo = locale->mbcinfo;
    else
        mbcinfo = get_mbcinfo();

    if(!mbcinfo->ismbcodepage)
        return (unsigned char*)MSVCRT_strtok_s((char*)str, (const char*)delim, (char**)ctx);

    if(!str)
        str = *ctx;

    while((c=_mbsnextc(str)) && _mbschr(delim, c))
        str += c>255 ? 2 : 1;
    if(!*str)
        return NULL;

    *ctx = str + (c>255 ? 2 : 1);
    while((c=_mbsnextc(*ctx)) && !_mbschr(delim, c))
        *ctx += c>255 ? 2 : 1;
    if (**ctx) {
        *(*ctx)++ = 0;
        if(c > 255)
            *(*ctx)++ = 0;
1151
    }
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179

    return str;
}


/*********************************************************************
 *              _mbstok_s(MSVCRT.@)
 */
unsigned char* CDECL _mbstok_s(unsigned char *str,
        const unsigned char *delim, unsigned char **ctx)
{
    return _mbstok_s_l(str, delim, ctx, NULL);
}

/*********************************************************************
 *              _mbstok_l(MSVCRT.@)
 */
unsigned char* CDECL _mbstok_l(unsigned char *str,
        const unsigned char *delim, MSVCRT__locale_t locale)
{
    return _mbstok_s_l(str, delim, &msvcrt_get_thread_data()->mbstok_next, locale);
}

/*********************************************************************
 *		_mbstok(MSVCRT.@)
 */
unsigned char* CDECL _mbstok(unsigned char *str, const unsigned char *delim)
{
1180 1181
    thread_data_t *data = msvcrt_get_thread_data();

1182
#if _MSVCR_VER == 0
1183 1184
    if(!str && !data->mbstok_next)
        return NULL;
1185
#endif
1186 1187

    return _mbstok_s_l(str, delim, &data->mbstok_next, NULL);
1188 1189
}

1190 1191 1192
/*********************************************************************
 *		_mbbtombc(MSVCRT.@)
 */
1193
unsigned int CDECL _mbbtombc(unsigned int c)
1194
{
1195
  if(get_mbcinfo()->mbcodepage == 932)
1196
  {
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
    if(c >= 0x20 && c <= 0x7e) {
      if((c >= 0x41 && c <= 0x5a) || (c >= 0x61 && c <= 0x7a) || (c >= 0x30 && c <= 0x39))
        return mbbtombc_932[c - 0x20] | 0x8200;
      else
        return mbbtombc_932[c - 0x20] | 0x8100;
    }
    else if(c >= 0xa1 && c <= 0xdf) {
      if(c >= 0xa6 && c <= 0xdd && c != 0xb0)
        return mbbtombc_932[c - 0xa1 + 0x5f] | 0x8300;
      else
        return mbbtombc_932[c - 0xa1 + 0x5f] | 0x8100;
    }
1209
  }
1210
  return c;  /* not Japanese or no MB char */
1211 1212
}

1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237
/*********************************************************************
 *		_mbbtype(MSVCRT.@)
 */
int CDECL _mbbtype(unsigned char c, int type)
{
    if (type == 1)
    {
        if ((c >= 0x20 && c <= 0x7e) || (c >= 0xa1 && c <= 0xdf))
            return _MBC_SINGLE;
        else if ((c >= 0x40 && c <= 0x7e) || (c >= 0x80 && c <= 0xfc))
            return _MBC_TRAIL;
        else
            return _MBC_ILLEGAL;
    }
    else
    {
        if ((c >= 0x20 && c <= 0x7e) || (c >= 0xa1 && c <= 0xdf))
            return _MBC_SINGLE;
        else if ((c >= 0x81 && c <= 0x9f) || (c >= 0xe0 && c <= 0xfc))
            return _MBC_LEAD;
        else
            return _MBC_ILLEGAL;
    }
}

1238 1239 1240
/*********************************************************************
 *		_ismbbkana(MSVCRT.@)
 */
1241
int CDECL _ismbbkana(unsigned int c)
1242 1243
{
  /* FIXME: use lc_ctype when supported, not lc_all */
1244
  if(get_mbcinfo()->mbcodepage == 932)
1245 1246
  {
    /* Japanese/Katakana, CP 932 */
1247
    return (c >= 0xa1 && c <= 0xdf);
1248 1249 1250 1251
  }
  return 0;
}

1252 1253 1254
/*********************************************************************
 *              _ismbcdigit(MSVCRT.@)
 */
1255
int CDECL _ismbcdigit(unsigned int ch)
1256
{
1257
    MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1258 1259 1260 1261 1262 1263
    return (get_char_typeW( wch ) & C1_DIGIT);
}

/*********************************************************************
 *              _ismbcgraph(MSVCRT.@)
 */
1264
int CDECL _ismbcgraph(unsigned int ch)
1265
{
1266
    MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1267 1268 1269 1270 1271 1272
    return (get_char_typeW( wch ) & (C1_UPPER | C1_LOWER | C1_DIGIT | C1_PUNCT | C1_ALPHA));
}

/*********************************************************************
 *              _ismbcalpha (MSVCRT.@)
 */
1273
int CDECL _ismbcalpha(unsigned int ch)
1274
{
1275
    MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1276 1277 1278 1279 1280 1281
    return (get_char_typeW( wch ) & C1_ALPHA);
}

/*********************************************************************
 *              _ismbclower (MSVCRT.@)
 */
1282
int CDECL _ismbclower(unsigned int ch)
1283
{
1284
    MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1285 1286 1287 1288 1289 1290
    return (get_char_typeW( wch ) & C1_UPPER);
}

/*********************************************************************
 *              _ismbcupper (MSVCRT.@)
 */
1291
int CDECL _ismbcupper(unsigned int ch)
1292
{
1293
    MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1294 1295 1296 1297 1298 1299
    return (get_char_typeW( wch ) & C1_LOWER);
}

/*********************************************************************
 *              _ismbcsymbol(MSVCRT.@)
 */
1300
int CDECL _ismbcsymbol(unsigned int ch)
1301
{
1302
    MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1303 1304
    WORD ctype;
    if (!GetStringTypeW(CT_CTYPE3, &wch, 1, &ctype))
1305
    {
1306 1307
        WARN("GetStringTypeW failed on %x\n", ch);
        return 0;
1308
    }
1309 1310 1311 1312 1313 1314
    return ((ctype & C3_SYMBOL) != 0);
}

/*********************************************************************
 *              _ismbcalnum (MSVCRT.@)
 */
1315
int CDECL _ismbcalnum(unsigned int ch)
1316
{
1317
    MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1318
    return (get_char_typeW( wch ) & (C1_ALPHA | C1_DIGIT));
1319 1320 1321 1322 1323
}

/*********************************************************************
 *              _ismbcspace (MSVCRT.@)
 */
1324
int CDECL _ismbcspace(unsigned int ch)
1325
{
1326
    MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1327 1328
    return (get_char_typeW( wch ) & C1_SPACE);
}
1329

1330 1331 1332
/*********************************************************************
 *              _ismbcprint (MSVCRT.@)
 */
1333
int CDECL _ismbcprint(unsigned int ch)
1334
{
1335
    MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1336 1337 1338 1339 1340 1341
    return (get_char_typeW( wch ) & (C1_UPPER | C1_LOWER | C1_DIGIT | C1_PUNCT | C1_ALPHA | C1_SPACE));
}

/*********************************************************************
 *              _ismbcpunct(MSVCRT.@)
 */
1342
int CDECL _ismbcpunct(unsigned int ch)
1343
{
1344
    MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
1345
    return (get_char_typeW( wch ) & C1_PUNCT);
1346 1347
}

1348 1349 1350
/*********************************************************************
 *		_ismbchira(MSVCRT.@)
 */
1351
int CDECL _ismbchira(unsigned int c)
1352
{
1353
  if(get_mbcinfo()->mbcodepage == 932)
1354 1355
  {
    /* Japanese/Hiragana, CP 932 */
1356
    return (c >= 0x829f && c <= 0x82f1);
1357 1358 1359 1360 1361 1362 1363
  }
  return 0;
}

/*********************************************************************
 *		_ismbckata(MSVCRT.@)
 */
1364
int CDECL _ismbckata(unsigned int c)
1365
{
1366
  if(get_mbcinfo()->mbcodepage == 932)
1367 1368
  {
    /* Japanese/Katakana, CP 932 */
1369
    return (c >= 0x8340 && c <= 0x8396 && c != 0x837f);
1370 1371 1372 1373 1374 1375 1376
  }
  return 0;
}

/*********************************************************************
 *		_ismbblead(MSVCRT.@)
 */
1377
int CDECL _ismbblead(unsigned int c)
1378
{
1379
  return (get_mbcinfo()->mbctype[(c&0xff) + 1] & _M1) != 0;
1380 1381 1382 1383 1384 1385
}


/*********************************************************************
 *		_ismbbtrail(MSVCRT.@)
 */
1386
int CDECL _ismbbtrail(unsigned int c)
1387
{
1388
  return (get_mbcinfo()->mbctype[(c&0xff) + 1] & _M2) != 0;
1389 1390
}

1391 1392 1393 1394 1395 1396 1397 1398
/*********************************************************************
 *              _ismbclegal(MSVCRT.@)
 */
int CDECL _ismbclegal(unsigned int c)
{
    return _ismbblead(HIBYTE(c)) && _ismbbtrail(LOBYTE(c));
}

1399 1400 1401
/*********************************************************************
 *		_ismbslead(MSVCRT.@)
 */
1402
int CDECL _ismbslead(const unsigned char* start, const unsigned char* str)
1403
{
1404 1405
  int lead = 0;

1406
  if(!get_mbcinfo()->ismbcodepage)
1407 1408
    return 0;

1409
  /* Lead bytes can also be trail bytes so we need to analyse the string
1410
   */
1411
  while (start <= str)
1412
  {
1413 1414 1415 1416
    if (!*start)
      return 0;
    lead = !lead && _ismbblead(*start);
    start++;
1417
  }
1418 1419

  return lead ? -1 : 0;
1420 1421 1422 1423 1424
}

/*********************************************************************
 *		_ismbstrail(MSVCRT.@)
 */
1425
int CDECL _ismbstrail(const unsigned char* start, const unsigned char* str)
1426
{
1427 1428 1429 1430 1431
  /* Note: this function doesn't check _ismbbtrail */
  if ((str > start) && _ismbslead(start, str-1))
    return -1;
  else
    return 0;
1432 1433
}

1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448
/*********************************************************************
 *		_mbsbtype (MSVCRT.@)
 */
int CDECL _mbsbtype(const unsigned char *str, MSVCRT_size_t count)
{
  int lead = 0;
  const unsigned char *end = str + count;

  /* Lead bytes can also be trail bytes so we need to analyse the string.
   * Also we must return _MBC_ILLEGAL for chars past the end of the string
   */
  while (str < end) /* Note: we skip the last byte - will check after the loop */
  {
    if (!*str)
      return _MBC_ILLEGAL;
1449
    lead = get_mbcinfo()->ismbcodepage && !lead && _ismbblead(*str);
1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464
    str++;
  }

  if (lead)
    if (_ismbbtrail(*str))
      return _MBC_TRAIL;
    else
      return _MBC_ILLEGAL;
  else
    if (_ismbblead(*str))
      return _MBC_LEAD;
    else
      return _MBC_SINGLE;
}

1465 1466 1467
/*********************************************************************
 *		_mbsset(MSVCRT.@)
 */
1468
unsigned char* CDECL _mbsset(unsigned char* str, unsigned int c)
1469
{
1470
  unsigned char* ret = str;
1471

1472
  if(!get_mbcinfo()->ismbcodepage || c < 256)
Mike McCormack's avatar
Mike McCormack committed
1473
    return u__strset(str, c); /* ASCII CP or SB char */
1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487

  c &= 0xffff; /* Strip high bits */

  while(str[0] && str[1])
  {
    *str++ = c >> 8;
    *str++ = c & 0xff;
  }
  if(str[0])
    str[0] = '\0'; /* FIXME: OK to shorten? */

  return ret;
}

1488 1489 1490
/*********************************************************************
 *		_mbsnbset(MSVCRT.@)
 */
1491
unsigned char* CDECL _mbsnbset(unsigned char *str, unsigned int c, MSVCRT_size_t len)
1492 1493 1494 1495 1496 1497
{
    unsigned char *ret = str;

    if(!len)
	return ret;

1498
    if(!get_mbcinfo()->ismbcodepage || c < 256)
Mike McCormack's avatar
Mike McCormack committed
1499
	return u__strnset(str, c, len); /* ASCII CP or SB char */
1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517

    c &= 0xffff; /* Strip high bits */

    while(str[0] && str[1] && (len > 1))
    {
	*str++ = c >> 8;
	len--;
	*str++ = c & 0xff;
	len--;
    }
    if(len && str[0]) {
	/* as per msdn pad with a blank character */
	str[0] = ' ';
    }

    return ret;
}

1518 1519 1520
/*********************************************************************
 *		_mbsnset(MSVCRT.@)
 */
1521
unsigned char* CDECL _mbsnset(unsigned char* str, unsigned int c, MSVCRT_size_t len)
1522
{
1523
  unsigned char *ret = str;
1524 1525 1526 1527

  if(!len)
    return ret;

1528
  if(!get_mbcinfo()->ismbcodepage || c < 256)
Mike McCormack's avatar
Mike McCormack committed
1529
    return u__strnset(str, c, len); /* ASCII CP or SB char */
1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545

  c &= 0xffff; /* Strip high bits */

  while(str[0] && str[1] && len--)
  {
    *str++ = c >> 8;
    *str++ = c & 0xff;
  }
  if(len && str[0])
    str[0] = '\0'; /* FIXME: OK to shorten? */

  return ret;
}

/*********************************************************************
 *		_mbsnccnt(MSVCRT.@)
1546
 * 'c' is for 'character'.
1547
 */
1548
MSVCRT_size_t CDECL _mbsnccnt(const unsigned char* str, MSVCRT_size_t len)
1549
{
1550
  MSVCRT_size_t ret;
1551
  if(get_mbcinfo()->ismbcodepage)
1552
  {
1553
    ret=0;
1554 1555
    while(*str && len-- > 0)
    {
1556
      if(_ismbblead(*str))
1557
      {
1558 1559
        if (!len)
          break;
1560
        len--;
1561
        str++;
1562 1563
      }
      str++;
1564
      ret++;
1565 1566 1567
    }
    return ret;
  }
Mike McCormack's avatar
Mike McCormack committed
1568
  ret=u_strlen(str);
1569
  return min(ret, len); /* ASCII CP */
1570 1571
}

1572 1573 1574 1575
/*********************************************************************
 *		_mbsnbcnt(MSVCRT.@)
 * 'b' is for byte count.
 */
1576
MSVCRT_size_t CDECL _mbsnbcnt(const unsigned char* str, MSVCRT_size_t len)
1577
{
1578
  MSVCRT_size_t ret;
1579
  if(get_mbcinfo()->ismbcodepage)
1580
  {
1581
    const unsigned char* xstr = str;
1582 1583
    while(*xstr && len-- > 0)
    {
1584
      if (_ismbblead(*xstr++))
1585 1586 1587 1588
        xstr++;
    }
    return xstr-str;
  }
Mike McCormack's avatar
Mike McCormack committed
1589
  ret=u_strlen(str);
1590
  return min(ret, len); /* ASCII CP */
1591 1592
}

1593 1594 1595
/*********************************************************************
 *		_mbsnbcat(MSVCRT.@)
 */
1596
unsigned char* CDECL _mbsnbcat(unsigned char* dst, const unsigned char* src, MSVCRT_size_t len)
1597
{
1598
    if(get_mbcinfo()->ismbcodepage)
1599
    {
Mike McCormack's avatar
Mike McCormack committed
1600
        unsigned char *res = dst;
1601
        while (*dst) {
1602
	    if (_ismbblead(*dst++)) {
1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615
		if (*dst) {
		    dst++;
		} else {
		    /* as per msdn overwrite the lead byte in front of '\0' */
		    dst--;
		    break;
		}
	    }
	}
        while (*src && len--) *dst++ = *src++;
        *dst = '\0';
        return res;
    }
Mike McCormack's avatar
Mike McCormack committed
1616
    return u_strncat(dst, src, len); /* ASCII CP */
1617 1618
}

1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648
int CDECL _mbsnbcat_s(unsigned char *dst, MSVCRT_size_t size, const unsigned char *src, MSVCRT_size_t len)
{
    unsigned char *ptr = dst;
    MSVCRT_size_t i;

    if (!dst && !size && !src && !len)
        return 0;

    if (!dst || !size || !src)
    {
        if (dst && size)
            *dst = '\0';

        *MSVCRT__errno() = MSVCRT_EINVAL;
        return MSVCRT_EINVAL;
    }

    /* Find the null terminator of the destination buffer. */
    while (size && *ptr)
        size--, ptr++;

    if (!size)
    {
        *dst = '\0';
        *MSVCRT__errno() = MSVCRT_EINVAL;
        return MSVCRT_EINVAL;
    }

    /* If necessary, check that the character preceding the null terminator is
     * a lead byte and move the pointer back by one for later overwrite. */
1649
    if (ptr != dst && get_mbcinfo()->ismbcodepage && _ismbblead(*(ptr - 1)))
1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667
        size++, ptr--;

    for (i = 0; *src && i < len; i++)
    {
        *ptr++ = *src++;
        size--;

        if (!size)
        {
            *dst = '\0';
            *MSVCRT__errno() = MSVCRT_ERANGE;
            return MSVCRT_ERANGE;
        }
    }

    *ptr = '\0';
    return 0;
}
1668

1669 1670 1671
/*********************************************************************
 *		_mbsncat(MSVCRT.@)
 */
1672
unsigned char* CDECL _mbsncat(unsigned char* dst, const unsigned char* src, MSVCRT_size_t len)
1673
{
1674
  if(get_mbcinfo()->ismbcodepage)
1675
  {
Mike McCormack's avatar
Mike McCormack committed
1676
    unsigned char *res = dst;
1677 1678
    while (*dst)
    {
1679
      if (_ismbblead(*dst++))
1680 1681 1682
        dst++;
    }
    while (*src && len--)
1683
    {
1684
      *dst++ = *src;
1685
      if(_ismbblead(*src++))
1686
        *dst++ = *src++;
1687
    }
1688
    *dst = '\0';
1689 1690
    return res;
  }
Mike McCormack's avatar
Mike McCormack committed
1691
  return u_strncat(dst, src, len); /* ASCII CP */
1692 1693
}

1694 1695 1696 1697

/*********************************************************************
 *              _mbslwr(MSVCRT.@)
 */
1698
unsigned char* CDECL _mbslwr(unsigned char* s)
1699
{
1700
  unsigned char *ret = s;
1701 1702
  if (!s)
    return NULL;
1703
  if (get_mbcinfo()->ismbcodepage)
1704 1705 1706
  {
    unsigned int c;
    while (*s)
1707
    {
1708 1709 1710 1711 1712 1713 1714 1715
      c = _mbctolower(_mbsnextc(s));
      /* Note that I assume that the size of the character is unchanged */
      if (c > 255)
      {
          *s++=(c>>8);
          c=c & 0xff;
      }
      *s++=c;
1716
    }
1717
  }
1718 1719
  else for ( ; *s; s++) *s = tolower(*s);
  return ret;
1720 1721 1722
}


1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736
/*********************************************************************
 *              _mbslwr_s(MSVCRT.@)
 */
int CDECL _mbslwr_s(unsigned char* s, MSVCRT_size_t len)
{
  if (!s && !len)
  {
    return 0;
  }
  else if (!s || !len)
  {
    *MSVCRT__errno() = MSVCRT_EINVAL;
    return MSVCRT_EINVAL;
  }
1737
  if (get_mbcinfo()->ismbcodepage)
1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762
  {
    unsigned int c;
    for ( ; *s && len > 0; len--)
    {
      c = _mbctolower(_mbsnextc(s));
      /* Note that I assume that the size of the character is unchanged */
      if (c > 255)
      {
          *s++=(c>>8);
          c=c & 0xff;
      }
      *s++=c;
    }
  }
  else for ( ; *s && len > 0; s++, len--) *s = tolower(*s);
  if (*s)
  {
    *s = '\0';
    *MSVCRT__errno() = MSVCRT_EINVAL;
    return MSVCRT_EINVAL;
  }
  return 0;
}


Francois Gouget's avatar
Francois Gouget committed
1763 1764 1765
/*********************************************************************
 *              _mbsupr(MSVCRT.@)
 */
1766
unsigned char* CDECL _mbsupr(unsigned char* s)
Francois Gouget's avatar
Francois Gouget committed
1767
{
1768
  unsigned char *ret = s;
1769 1770
  if (!s)
    return NULL;
1771
  if (get_mbcinfo()->ismbcodepage)
1772 1773 1774
  {
    unsigned int c;
    while (*s)
1775
    {
1776 1777 1778 1779 1780 1781 1782 1783
      c = _mbctoupper(_mbsnextc(s));
      /* Note that I assume that the size of the character is unchanged */
      if (c > 255)
      {
          *s++=(c>>8);
          c=c & 0xff;
      }
      *s++=c;
1784
    }
1785
  }
1786 1787
  else for ( ; *s; s++) *s = toupper(*s);
  return ret;
1788 1789 1790
}


1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804
/*********************************************************************
 *              _mbsupr_s(MSVCRT.@)
 */
int CDECL _mbsupr_s(unsigned char* s, MSVCRT_size_t len)
{
  if (!s && !len)
  {
    return 0;
  }
  else if (!s || !len)
  {
    *MSVCRT__errno() = MSVCRT_EINVAL;
    return MSVCRT_EINVAL;
  }
1805
  if (get_mbcinfo()->ismbcodepage)
1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830
  {
    unsigned int c;
    for ( ; *s && len > 0; len--)
    {
      c = _mbctoupper(_mbsnextc(s));
      /* Note that I assume that the size of the character is unchanged */
      if (c > 255)
      {
          *s++=(c>>8);
          c=c & 0xff;
      }
      *s++=c;
    }
  }
  else for ( ; *s && len > 0; s++, len--) *s = toupper(*s);
  if (*s)
  {
    *s = '\0';
    *MSVCRT__errno() = MSVCRT_EINVAL;
    return MSVCRT_EINVAL;
  }
  return 0;
}


1831 1832 1833
/*********************************************************************
 *              _mbsspn (MSVCRT.@)
 */
1834
MSVCRT_size_t CDECL _mbsspn(const unsigned char* string, const unsigned char* set)
1835
{
1836
    const unsigned char *p, *q;
1837

1838
    for (p = string; *p; p++)
1839
    {
1840
        if (_ismbblead(*p))
1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858
        {
            for (q = set; *q; q++)
            {
                if (!q[1])
                    break;
                if ((*p == *q) &&  (p[1] == q[1]))
                    break;
                q++;
            }
            if (!q[0] || !q[1]) break;
        }
        else
        {
            for (q = set; *q; q++)
                if (*p == *q)
                    break;
            if (!*q) break;
        }
1859
    }
1860
    return p - string;
1861 1862
}

1863 1864 1865
/*********************************************************************
 *              _mbsspnp (MSVCRT.@)
 */
1866
unsigned char* CDECL _mbsspnp(const unsigned char* string, const unsigned char* set)
1867 1868 1869 1870 1871
{
    const unsigned char *p, *q;

    for (p = string; *p; p++)
    {
1872
        if (_ismbblead(*p))
1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893
        {
            for (q = set; *q; q++)
            {
                if (!q[1])
                    break;
                if ((*p == *q) &&  (p[1] == q[1]))
                    break;
                q++;
            }
            if (!q[0] || !q[1]) break;
        }
        else
        {
            for (q = set; *q; q++)
                if (*p == *q)
                    break;
            if (!*q) break;
        }
    }
    if (*p == '\0')
        return NULL;
1894
    return (unsigned char *)p;
1895 1896
}

1897
/*********************************************************************
1898
 *		_mbscspn(MSVCRT.@)
1899
 */
1900
MSVCRT_size_t CDECL _mbscspn(const unsigned char* str, const unsigned char* cmp)
1901
{
1902
  if (get_mbcinfo()->ismbcodepage)
1903
    FIXME("don't handle double character case\n");
Mike McCormack's avatar
Mike McCormack committed
1904
  return u_strcspn(str, cmp);
1905
}
1906 1907 1908 1909

/*********************************************************************
 *              _mbsrev (MSVCRT.@)
 */
1910
unsigned char* CDECL _mbsrev(unsigned char* str)
1911 1912
{
    int i, len = _mbslen(str);
1913
    unsigned char *p, *temp=MSVCRT_malloc(len*2);
1914 1915 1916 1917 1918 1919 1920 1921

    if(!temp)
        return str;

    /* unpack multibyte string to temp buffer */
    p=str;
    for(i=0; i<len; i++)
    {
1922
        if (_ismbblead(*p))
1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937
        {
            temp[i*2]=*p++;
            temp[i*2+1]=*p++;
        }
        else
        {
            temp[i*2]=*p++;
            temp[i*2+1]=0;
        }
    }

    /* repack it in the reverse order */
    p=str;
    for(i=len-1; i>=0; i--)
    {
1938
        if(_ismbblead(temp[i*2]))
1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953
        {
            *p++=temp[i*2];
            *p++=temp[i*2+1];
        }
        else
        {
            *p++=temp[i*2];
        }
    }

    MSVCRT_free(temp);

    return str;
}

1954 1955 1956
/*********************************************************************
 *		_mbspbrk (MSVCRT.@)
 */
1957
unsigned char* CDECL _mbspbrk(const unsigned char* str, const unsigned char* accept)
1958
{
1959
    const unsigned char* p;
1960 1961 1962

    while(*str)
    {
1963
        for(p = accept; *p; p += (_ismbblead(*p)?2:1) )
1964 1965
        {
            if (*p == *str)
1966
                if( !_ismbblead(*p) || ( *(p+1) == *(str+1) ) )
1967
                     return (unsigned char*)str;
1968
        }
1969
        str += (_ismbblead(*str)?2:1);
1970 1971 1972
    }
    return NULL;
}
1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988


/*
 * Functions depending on locale codepage
 */

/*********************************************************************
 *		mblen(MSVCRT.@)
 * REMARKS
 *  Unlike most of the multibyte string functions this function uses
 *  the locale codepage, not the codepage set by _setmbcp
 */
int CDECL MSVCRT_mblen(const char* str, MSVCRT_size_t size)
{
  if (str && *str && size)
  {
1989
    if(get_locinfo()->mb_cur_max == 1)
1990 1991
      return 1; /* ASCII CP */

1992
    return !MSVCRT_isleadbyte((unsigned char)*str) ? 1 : (size>1 ? 2 : -1);
1993 1994 1995 1996
  }
  return 0;
}

1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028
/*********************************************************************
 *              mbrlen(MSVCRT.@)
 */
MSVCRT_size_t CDECL MSVCRT_mbrlen(const char *str, MSVCRT_size_t len, MSVCRT_mbstate_t *state)
{
    MSVCRT_mbstate_t s = (state ? *state : 0);
    MSVCRT_size_t ret;

    if(!len || !str || !*str)
        return 0;

    if(get_locinfo()->mb_cur_max == 1) {
        return 1;
    }else if(!s && MSVCRT_isleadbyte((unsigned char)*str)) {
        if(len == 1) {
            s = (unsigned char)*str;
            ret = -2;
        }else {
            ret = 2;
        }
    }else if(!s) {
        ret = 1;
    }else {
        s = 0;
        ret = 2;
    }

    if(state)
        *state = s;
    return ret;
}

2029 2030 2031 2032 2033
/*********************************************************************
 *		_mbstrlen_l(MSVCRT.@)
 */
MSVCRT_size_t CDECL _mbstrlen_l(const char* str, MSVCRT__locale_t locale)
{
2034 2035
    MSVCRT_pthreadlocinfo locinfo;

2036
    if(!locale)
2037 2038 2039
        locinfo = get_locinfo();
    else
        locinfo = locale->locinfo;
2040

2041
    if(locinfo->mb_cur_max > 1) {
2042 2043 2044 2045 2046 2047
        MSVCRT_size_t len;
        len = MultiByteToWideChar(locinfo->lc_codepage, MB_ERR_INVALID_CHARS,
                                  str, -1, NULL, 0);
        if (!len) {
            *MSVCRT__errno() = MSVCRT_EILSEQ;
            return -1;
2048
        }
2049
        return len - 1;
2050 2051 2052 2053 2054
    }

    return strlen(str);
}

2055 2056 2057 2058 2059
/*********************************************************************
 *		_mbstrlen(MSVCRT.@)
 */
MSVCRT_size_t CDECL _mbstrlen(const char* str)
{
2060
    return _mbstrlen_l(str, NULL);
2061
}
2062

2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077
/*********************************************************************
 *		_mbtowc_l(MSVCRT.@)
 */
int CDECL MSVCRT_mbtowc_l(MSVCRT_wchar_t *dst, const char* str, MSVCRT_size_t n, MSVCRT__locale_t locale)
{
    MSVCRT_pthreadlocinfo locinfo;
    MSVCRT_wchar_t tmpdst = '\0';

    if(!locale)
        locinfo = get_locinfo();
    else
        locinfo = locale->locinfo;

    if(n <= 0 || !str)
        return 0;
2078 2079 2080
    if(!locinfo->lc_codepage)
        tmpdst = (unsigned char)*str;
    else if(!MultiByteToWideChar(locinfo->lc_codepage, 0, str, n, &tmpdst, 1))
2081 2082 2083 2084 2085 2086
        return -1;
    if(dst)
        *dst = tmpdst;
    /* return the number of bytes from src that have been used */
    if(!*str)
        return 0;
2087
    if(n >= 2 && MSVCRT__isleadbyte_l((unsigned char)*str, locale) && str[1])
2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099
        return 2;
    return 1;
}

/*********************************************************************
 *              mbtowc(MSVCRT.@)
 */
int CDECL MSVCRT_mbtowc(MSVCRT_wchar_t *dst, const char* str, MSVCRT_size_t n)
{
    return MSVCRT_mbtowc_l(dst, str, n, NULL);
}

2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146
/*********************************************************************
 *              mbrtowc(MSVCRT.@)
 */
MSVCRT_size_t CDECL MSVCRT_mbrtowc(MSVCRT_wchar_t *dst, const char *str,
        MSVCRT_size_t n, MSVCRT_mbstate_t *state)
{
    MSVCRT_pthreadlocinfo locinfo = get_locinfo();
    MSVCRT_mbstate_t s = (state ? *state : 0);
    char tmpstr[2];
    int len = 0;

    if(dst)
        *dst = 0;

    if(!n || !str || !*str)
        return 0;

    if(locinfo->mb_cur_max == 1) {
        tmpstr[len++] = *str;
    }else if(!s && MSVCRT_isleadbyte((unsigned char)*str)) {
        if(n == 1) {
            s = (unsigned char)*str;
            len = -2;
        }else {
            tmpstr[0] = str[0];
            tmpstr[1] = str[1];
            len = 2;
        }
    }else if(!s) {
        tmpstr[len++] = *str;
    }else {
        tmpstr[0] = s;
        tmpstr[1] = *str;
        len = 2;
        s = 0;
    }

    if(len > 0) {
        if(!MultiByteToWideChar(locinfo->lc_codepage, 0, tmpstr, len, dst, dst ? 1 : 0))
            len = -1;
    }

    if(state)
        *state = s;
    return len;
}

2147 2148 2149 2150 2151 2152
/*********************************************************************
 *		_mbstowcs_l(MSVCRT.@)
 */
MSVCRT_size_t CDECL MSVCRT__mbstowcs_l(MSVCRT_wchar_t *wcstr, const char *mbstr,
        MSVCRT_size_t count, MSVCRT__locale_t locale)
{
2153
    MSVCRT_pthreadlocinfo locinfo;
2154
    MSVCRT_size_t i, size;
2155

2156 2157 2158 2159 2160
    if(!mbstr) {
        *MSVCRT__errno() = MSVCRT_EINVAL;
        return -1;
    }

2161
    if(!locale)
2162 2163 2164
        locinfo = get_locinfo();
    else
        locinfo = locale->locinfo;
2165

2166 2167
    /* Ignore count parameter */
    if(!wcstr)
2168
        return MultiByteToWideChar(locinfo->lc_codepage, 0, mbstr, -1, NULL, 0)-1;
2169

2170 2171 2172 2173
    for(i=0, size=0; i<count; i++) {
        if(mbstr[size] == '\0')
            break;

2174
        size += (MSVCRT__isleadbyte_l((unsigned char)mbstr[size], locale) ? 2 : 1);
2175 2176
    }

2177
    size = MultiByteToWideChar(locinfo->lc_codepage, 0,
2178
            mbstr, size, wcstr, count);
2179

2180 2181
    if(size<count && wcstr)
        wcstr[size] = '\0';
2182

2183
    return size;
2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206
}

/*********************************************************************
 *		mbstowcs(MSVCRT.@)
 */
MSVCRT_size_t CDECL MSVCRT_mbstowcs(MSVCRT_wchar_t *wcstr,
        const char *mbstr, MSVCRT_size_t count)
{
    return MSVCRT__mbstowcs_l(wcstr, mbstr, count, NULL);
}

/*********************************************************************
 *              _mbstowcs_s_l(MSVCRT.@)
 */
int CDECL MSVCRT__mbstowcs_s_l(MSVCRT_size_t *ret, MSVCRT_wchar_t *wcstr,
        MSVCRT_size_t size, const char *mbstr,
        MSVCRT_size_t count, MSVCRT__locale_t locale)
{
    MSVCRT_size_t conv;

    if(!wcstr && !size) {
        conv = MSVCRT__mbstowcs_l(NULL, mbstr, 0, locale);
        if(ret)
2207
            *ret = conv+1;
2208 2209 2210
        return 0;
    }

2211 2212 2213
    if (!MSVCRT_CHECK_PMT(wcstr != NULL)) return MSVCRT_EINVAL;
    if (!MSVCRT_CHECK_PMT(mbstr != NULL)) {
        if(size) wcstr[0] = '\0';
2214 2215 2216
        return MSVCRT_EINVAL;
    }

2217
    if(count==MSVCRT__TRUNCATE || size<count)
2218 2219 2220 2221 2222 2223 2224
        conv = size;
    else
        conv = count;

    conv = MSVCRT__mbstowcs_l(wcstr, mbstr, conv, locale);
    if(conv<size)
        wcstr[conv++] = '\0';
2225
    else if(conv==size && (count==MSVCRT__TRUNCATE || wcstr[conv-1]=='\0'))
2226 2227
        wcstr[conv-1] = '\0';
    else {
2228
        MSVCRT_INVALID_PMT("wcstr[size] is too small", MSVCRT_ERANGE);
2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246
        if(size)
            wcstr[0] = '\0';
        return MSVCRT_ERANGE;
    }

    if(ret)
        *ret = conv;
    return 0;
}

/*********************************************************************
 *              mbstowcs_s(MSVCRT.@)
 */
int CDECL MSVCRT__mbstowcs_s(MSVCRT_size_t *ret, MSVCRT_wchar_t *wcstr,
        MSVCRT_size_t size, const char *mbstr, MSVCRT_size_t count)
{
    return MSVCRT__mbstowcs_s_l(ret, wcstr, size, mbstr, count, NULL);
}
2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278

/*********************************************************************
 *              mbsrtowcs(MSVCRT.@)
 */
MSVCRT_size_t CDECL MSVCRT_mbsrtowcs(MSVCRT_wchar_t *wcstr,
        const char **pmbstr, MSVCRT_size_t count, MSVCRT_mbstate_t *state)
{
    MSVCRT_mbstate_t s = (state ? *state : 0);
    MSVCRT_wchar_t tmpdst;
    MSVCRT_size_t ret = 0;

    if(!MSVCRT_CHECK_PMT(pmbstr != NULL))
        return -1;

    while(!wcstr || count>ret) {
        int ch_len = MSVCRT_mbrtowc(&tmpdst, *pmbstr, 2, &s);
        if(wcstr)
            wcstr[ret] = tmpdst;

        if(ch_len < 0) {
            return -1;
        }else if(ch_len == 0) {
            *pmbstr = NULL;
            return ret;
        }

        *pmbstr += ch_len;
        ret++;
    }

    return ret;
}
2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290

/*********************************************************************
 *		_mbctohira (MSVCRT.@)
 *
 *              Converts a sjis katakana character to hiragana.
 */
unsigned int CDECL _mbctohira(unsigned int c)
{
    if(_ismbckata(c) && c <= 0x8393)
        return (c - 0x8340 - (c >= 0x837f ? 1 : 0)) + 0x829f;
    return c;
}
2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302

/*********************************************************************
 *		_mbctokata (MSVCRT.@)
 *
 *              Converts a sjis hiragana character to katakana.
 */
unsigned int CDECL _mbctokata(unsigned int c)
{
    if(_ismbchira(c))
        return (c - 0x829f) + 0x8340 + (c >= 0x82de ? 1 : 0);
    return c;
}