mbcs.c 30.9 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
#include "msvcrt.h"
27
#include "wine/unicode.h"
28 29 30
#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
31 32 33 34

unsigned char MSVCRT_mbctype[257];
int MSVCRT___mb_cur_max = 1;

35
static MSVCRT_wchar_t msvcrt_mbc_to_wc(unsigned int ch)
36
{
37
  MSVCRT_wchar_t chW;
38 39 40 41 42 43 44 45 46 47 48
  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;
  }
49
  if (!MultiByteToWideChar(MSVCRT___lc_codepage, 0, mbch, n_chars, &chW, 1))
50 51 52 53 54 55 56
  {
    WARN("MultiByteToWideChar failed on %x\n", ch);
    return 0;
  }
  return chW;
}

Mike McCormack's avatar
Mike McCormack committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
static inline size_t u_strlen( const unsigned char *str )
{
  return strlen( (const char*) str );
}

static inline unsigned char* u_strncat( unsigned char* dst, const unsigned char* src, size_t len )
{
  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 );
}

static inline int u_strncmp( const unsigned char *s1, const unsigned char *s2, size_t len )
{
  return strncmp( (const char*)s1, (const char*)s2, len );
}

static inline int u_strncasecmp( const unsigned char *s1, const unsigned char *s2, size_t len )
{
  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_strtok( unsigned char *s, const unsigned char *delim )
{
  return (unsigned char*) strtok( (char*)s, (const char*)delim );
}

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

static inline unsigned char *u__strnset( unsigned char *s, unsigned char c, size_t len )
{
  return (unsigned char*) _strnset( (char*)s, c, len );
}

static inline size_t u_strcspn( const unsigned char *s, const unsigned char *rej )
{
  return strcspn( (const char *)s, (const char*)rej );
}

117 118 119
/*********************************************************************
 *		__p__mbctype (MSVCRT.@)
 */
120
unsigned char* CDECL __p__mbctype(void)
121 122 123 124 125 126 127
{
  return MSVCRT_mbctype;
}

/*********************************************************************
 *		__p___mb_cur_max(MSVCRT.@)
 */
128
int* CDECL __p___mb_cur_max(void)
129 130 131 132 133 134 135
{
  return &MSVCRT___mb_cur_max;
}

/*********************************************************************
 *		_mbsnextc(MSVCRT.@)
 */
136
unsigned int CDECL _mbsnextc(const unsigned char* str)
137 138 139 140 141 142
{
  if(MSVCRT___mb_cur_max > 1 && MSVCRT_isleadbyte(*str))
    return *str << 8 | str[1];
  return *str; /* ASCII CP or SB char */
}

143 144 145
/*********************************************************************
 *		_mbctolower(MSVCRT.@)
 */
146
unsigned int CDECL _mbctolower(unsigned int c)
147 148 149 150 151 152 153 154 155 156 157 158
{
    if (MSVCRT_isleadbyte(c))
    {
      FIXME("Handle MBC chars\n");
      return c;
    }
    return tolower(c); /* ASCII CP or SB char */
}

/*********************************************************************
 *		_mbctoupper(MSVCRT.@)
 */
159
unsigned int CDECL _mbctoupper(unsigned int c)
160 161 162 163 164 165 166 167 168 169 170 171
{
    if (MSVCRT_isleadbyte(c))
    {
      FIXME("Handle MBC chars\n");
      return c;
    }
    return toupper(c); /* ASCII CP or SB char */
}

/*********************************************************************
 *		_mbsdec(MSVCRT.@)
 */
172
unsigned char* CDECL _mbsdec(const unsigned char* start, const unsigned char* cur)
173 174
{
  if(MSVCRT___mb_cur_max > 1)
175
    return (unsigned char *)(_ismbstrail(start,cur-1) ? cur - 2 : cur -1);
176

177
  return (unsigned char *)cur - 1; /* ASCII CP or SB char */
178 179 180 181 182
}

/*********************************************************************
 *		_mbsinc(MSVCRT.@)
 */
183
unsigned char* CDECL _mbsinc(const unsigned char* str)
184 185 186 187 188 189 190 191 192 193
{
  if(MSVCRT___mb_cur_max > 1 && MSVCRT_isleadbyte(*str))
    return (unsigned char*)str + 2; /* MB char */

  return (unsigned char*)str + 1; /* ASCII CP or SB char */
}

/*********************************************************************
 *		_mbsninc(MSVCRT.@)
 */
194
unsigned char* CDECL _mbsninc(const unsigned char* str, MSVCRT_size_t num)
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
{
  if(!str || num < 1)
    return NULL;
  if(MSVCRT___mb_cur_max > 1)
  {
    while(num--)
      str = _mbsinc(str);
    return (unsigned char*)str;
  }
  return (unsigned char*)str + num; /* ASCII CP */
}

/*********************************************************************
 *		_mbclen(MSVCRT.@)
 */
210
unsigned int CDECL _mbclen(const unsigned char* str)
211 212 213 214 215 216 217
{
  return MSVCRT_isleadbyte(*str) ? 2 : 1;
}

/*********************************************************************
 *		mblen(MSVCRT.@)
 */
218
int CDECL MSVCRT_mblen(const char* str, MSVCRT_size_t size)
219 220 221 222 223 224 225 226 227 228 229 230 231 232
{
  if (str && *str && size)
  {
    if(MSVCRT___mb_cur_max == 1)
      return 1; /* ASCII CP */

    return !MSVCRT_isleadbyte(*str) ? 1 : (size>1 ? 2 : -1);
  }
  return 0;
}

/*********************************************************************
 *		_mbslen(MSVCRT.@)
 */
233
MSVCRT_size_t CDECL _mbslen(const unsigned char* str)
234 235 236 237 238 239 240 241 242 243 244
{
  if(MSVCRT___mb_cur_max > 1)
  {
    MSVCRT_size_t len = 0;
    while(*str)
    {
      str += MSVCRT_isleadbyte(*str) ? 2 : 1;
      len++;
    }
    return len;
  }
Mike McCormack's avatar
Mike McCormack committed
245
  return u_strlen(str); /* ASCII CP */
246 247 248 249 250
}

/*********************************************************************
 *		_mbstrlen(MSVCRT.@)
 */
251
MSVCRT_size_t CDECL _mbstrlen(const char* str)
252 253 254 255 256 257
{
  if(MSVCRT___mb_cur_max > 1)
  {
    MSVCRT_size_t len = 0;
    while(*str)
    {
258
      /* FIXME: According to the documentation we are supposed to test for
259 260 261 262 263 264 265 266 267 268 269 270 271
       * multi-byte character validity. Whatever that means
       */
      str += MSVCRT_isleadbyte(*str) ? 2 : 1;
      len++;
    }
    return len;
  }
  return strlen(str); /* ASCII CP */
}

/*********************************************************************
 *		_mbccpy(MSVCRT.@)
 */
272
void CDECL _mbccpy(unsigned char* dest, const unsigned char* src)
273
{
274
  *dest = *src;
275
  if(MSVCRT___mb_cur_max > 1 && MSVCRT_isleadbyte(*src))
276
    *++dest = *++src; /* MB char */
277 278 279 280 281
}

/*********************************************************************
 *		_mbsncpy(MSVCRT.@)
 */
282
unsigned char* CDECL _mbsncpy(unsigned char* dst, const unsigned char* src, MSVCRT_size_t n)
283
{
284
  unsigned char* ret = dst;
285 286 287 288
  if(!n)
    return dst;
  if(MSVCRT___mb_cur_max > 1)
  {
289
    while (*src && n)
290
    {
291
      n--;
292 293 294 295 296
      *dst++ = *src;
      if (MSVCRT_isleadbyte(*src++))
          *dst++ = *src++;
    }
  }
297 298 299 300 301 302 303 304 305 306
  else
  {
    while (n)
    {
        n--;
        if (!(*dst++ = *src++)) break;
    }
  }
  while (n--) *dst++ = 0;
  return ret;
307 308 309 310 311
}

/*********************************************************************
 *              _mbsnbcpy(MSVCRT.@)
 */
312
unsigned char* CDECL _mbsnbcpy(unsigned char* dst, const unsigned char* src, MSVCRT_size_t n)
313
{
314
  unsigned char* ret = dst;
315 316 317 318
  if(!n)
    return dst;
  if(MSVCRT___mb_cur_max > 1)
  {
319
    while (*src && (n > 1))
320
    {
321
      n--;
322 323 324 325 326 327 328 329 330
      *dst++ = *src;
      if (MSVCRT_isleadbyte(*src++))
      {
        *dst++ = *src++;
        n--;
      }
    }
    if (*src && n && !MSVCRT_isleadbyte(*src))
    {
331
      /* If the last character is a multi-byte character then
332 333 334 335 336 337
       * we cannot copy it since we have only one byte left
       */
      *dst++ = *src;
      n--;
    }
  }
338 339 340 341 342 343 344 345 346 347
  else
  {
    while (n)
    {
        n--;
        if (!(*dst++ = *src++)) break;
    }
  }
  while (n--) *dst++ = 0;
  return ret;
348 349
}

350 351 352
/*********************************************************************
 *		_mbscmp(MSVCRT.@)
 */
353
int CDECL _mbscmp(const unsigned char* str, const unsigned char* cmp)
354 355 356 357 358 359 360 361 362
{
  if(MSVCRT___mb_cur_max > 1)
  {
    unsigned int strc, cmpc;
    do {
      if(!*str)
        return *cmp ? -1 : 0;
      if(!*cmp)
        return 1;
363 364
      strc = _mbsnextc(str);
      cmpc = _mbsnextc(cmp);
365 366 367 368 369 370
      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
371
  return u_strcmp(str, cmp); /* ASCII CP */
372 373
}

374 375 376 377
/*********************************************************************
 *		_mbsicoll(MSVCRT.@)
 * FIXME: handle locales.
 */
378
int CDECL _mbsicoll(const unsigned char* str, const unsigned char* cmp)
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
{
  if(MSVCRT___mb_cur_max > 1)
  {
    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
396
  return u_strcasecmp(str, cmp); /* ASCII CP */
397 398
}

399 400 401 402 403 404 405
/*********************************************************************
 *		_mbscoll(MSVCRT.@)
 * Performs a case-sensitive comparison according to the current code page
 * RETURN
 *   _NLSCMPERROR if error
 * FIXME: handle locales.
 */
406
int CDECL _mbscoll(const unsigned char* str, const unsigned char* cmp)
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
{
  if(MSVCRT___mb_cur_max > 1)
  {
    unsigned int strc, cmpc;
    do {
      if(!*str)
        return *cmp ? -1 : 0;
      if(!*cmp)
        return 1;
      strc = _mbsnextc(str);
      cmpc = _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);
  }
  return u_strcmp(str, cmp); /* ASCII CP */
}

427

428 429 430
/*********************************************************************
 *		_mbsicmp(MSVCRT.@)
 */
431
int CDECL _mbsicmp(const unsigned char* str, const unsigned char* cmp)
432 433
{
  if(MSVCRT___mb_cur_max > 1)
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
  {
    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
449
  return u_strcasecmp(str, cmp); /* ASCII CP */
450 451 452
}

/*********************************************************************
453
 *		_mbsncmp(MSVCRT.@)
454
 */
455
int CDECL _mbsncmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
456 457 458 459 460 461 462 463 464
{
  if(!len)
    return 0;

  if(MSVCRT___mb_cur_max > 1)
  {
    unsigned int strc, cmpc;
    while(len--)
    {
465
      int inc;
466 467 468 469
      if(!*str)
        return *cmp ? -1 : 0;
      if(!*cmp)
        return 1;
470 471
      strc = _mbsnextc(str);
      cmpc = _mbsnextc(cmp);
472 473
      if(strc != cmpc)
        return strc < cmpc ? -1 : 1;
474 475 476
      inc=(strc > 255) ? 2 : 1; /* Equal, use same increment */
      str += inc;
      cmp += inc;
477 478 479
    }
    return 0; /* Matched len chars */
  }
Mike McCormack's avatar
Mike McCormack committed
480
  return u_strncmp(str, cmp, len); /* ASCII CP */
481 482 483
}

/*********************************************************************
484
 *              _mbsnbcmp(MSVCRT.@)
485
 */
486
int CDECL _mbsnbcmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
487
{
488 489
  if (!len)
    return 0;
490
  if(MSVCRT___mb_cur_max > 1)
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
  {
    unsigned int strc, cmpc;
    while (len)
    {
      int clen;
      if(!*str)
        return *cmp ? -1 : 0;
      if(!*cmp)
        return 1;
      if (MSVCRT_isleadbyte(*str))
      {
        strc=(len>=2)?_mbsnextc(str):0;
        clen=2;
      }
      else
      {
        strc=*str;
        clen=1;
      }
      if (MSVCRT_isleadbyte(*cmp))
        cmpc=(len>=2)?_mbsnextc(cmp):0;
      else
        cmpc=*str;
      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
522
  return u_strncmp(str,cmp,len);
523 524 525
}

/*********************************************************************
526 527 528
 *		_mbsnicmp(MSVCRT.@)
 *
 * Compare two multibyte strings case insensitively to 'len' characters.
529
 */
530
int CDECL _mbsnicmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
531
{
532
  /* FIXME: No tolower() for mb strings yet */
533 534
  if(MSVCRT___mb_cur_max > 1)
  {
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
    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 */
550
  }
Mike McCormack's avatar
Mike McCormack committed
551
  return u_strncasecmp(str, cmp, len); /* ASCII CP */
552 553
}

554 555 556
/*********************************************************************
 *              _mbsnbicmp(MSVCRT.@)
 */
557
int CDECL _mbsnbicmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
{
  if (!len)
    return 0;
  if(MSVCRT___mb_cur_max > 1)
  {
    unsigned int strc, cmpc;
    while (len)
    {
      int clen;
      if(!*str)
        return *cmp ? -1 : 0;
      if(!*cmp)
        return 1;
      if (MSVCRT_isleadbyte(*str))
      {
        strc=(len>=2)?_mbsnextc(str):0;
        clen=2;
      }
      else
      {
        strc=*str;
        clen=1;
      }
      if (MSVCRT_isleadbyte(*cmp))
        cmpc=(len>=2)?_mbsnextc(cmp):0;
      else
        cmpc=*str;
      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 */
  }
Mike McCormack's avatar
Mike McCormack committed
595
  return u_strncmp(str,cmp,len);
596 597
}

598 599 600
/*********************************************************************
 *		_mbscat (MSVCRT.@)
 */
601
unsigned char * CDECL _mbscat( unsigned char *dst, const unsigned char *src )
602 603 604 605 606 607 608 609
{
    strcat( (char *)dst, (const char *)src );
    return dst;
}

/*********************************************************************
 *		_mbscpy (MSVCRT.@)
 */
610
unsigned char* CDECL _mbscpy( unsigned char *dst, const unsigned char *src )
611 612 613 614 615 616 617 618
{
    strcpy( (char *)dst, (const char *)src );
    return dst;
}

/*********************************************************************
 *		_mbsstr (MSVCRT.@)
 */
619
unsigned char * CDECL _mbsstr(const unsigned char *haystack, const unsigned char *needle)
620 621 622 623
{
    return (unsigned char *)strstr( (const char *)haystack, (const char *)needle );
}

624
/*********************************************************************
625 626 627
 *		_mbschr(MSVCRT.@)
 *
 * Find a multibyte character in a multibyte string.
628
 */
629
unsigned char* CDECL _mbschr(const unsigned char* s, unsigned int x)
630 631 632
{
  if(MSVCRT___mb_cur_max > 1)
  {
633 634
    unsigned int c;
    while (1)
635
    {
636 637 638 639 640 641
      c = _mbsnextc(s);
      if (c == x)
        return (unsigned char*)s;
      if (!c)
        return NULL;
      s += c > 255 ? 2 : 1;
642 643
    }
  }
Mike McCormack's avatar
Mike McCormack committed
644
  return u_strchr(s, x); /* ASCII CP */
645 646 647 648 649
}

/*********************************************************************
 *		_mbsrchr(MSVCRT.@)
 */
650
unsigned char* CDECL _mbsrchr(const unsigned char* s, unsigned int x)
651
{
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
  if(MSVCRT___mb_cur_max > 1)
  {
    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
667
  return u_strrchr(s, x);
668 669
}

670 671 672 673 674
/*********************************************************************
 *		_mbstok(MSVCRT.@)
 *
 * Find and extract tokens from strings
 */
675
unsigned char* CDECL _mbstok(unsigned char *str, const unsigned char *delim)
676
{
677
    thread_data_t *data = msvcrt_get_thread_data();
Mike McCormack's avatar
Mike McCormack committed
678
    unsigned char *ret;
679 680 681 682 683 684

    if(MSVCRT___mb_cur_max > 1)
    {
	unsigned int c;

	if (!str)
685
    	    if (!(str = data->mbstok_next)) return NULL;
686 687 688 689 690 691 692 693 694 695 696 697 698

	while ((c = _mbsnextc(str)) && _mbschr(delim, c)) {
	    str += c > 255 ? 2 : 1;
	}
	if (!*str) return NULL;
	ret = str++;
	while ((c = _mbsnextc(str)) && !_mbschr(delim, c)) {
	    str += c > 255 ? 2 : 1;
	}
	if (*str) {
	    *str++ = 0;
	    if (c > 255) *str++ = 0;
	}
699
	data->mbstok_next = str;
700 701
	return ret;
    }
Mike McCormack's avatar
Mike McCormack committed
702
    return u_strtok(str, delim); /* ASCII CP */
703 704
}

705 706 707
/*********************************************************************
 *		mbtowc(MSVCRT.@)
 */
708
int CDECL MSVCRT_mbtowc(MSVCRT_wchar_t *dst, const char* str, MSVCRT_size_t n)
709
{
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
    /* temp var needed because MultiByteToWideChar wants non NULL destination */
    MSVCRT_wchar_t tmpdst = '\0';

    if(n <= 0 || !str)
        return 0;
    if(!MultiByteToWideChar(CP_ACP, 0, str, n, &tmpdst, 1))
        return -1;
    if(dst)
        *dst = tmpdst;
    /* return the number of bytes from src that have been used */
    if(!*str)
        return 0;
    if(n >= 2 && MSVCRT_isleadbyte(*str) && str[1])
        return 2;
    return 1;
725 726 727 728 729
}

/*********************************************************************
 *		_mbbtombc(MSVCRT.@)
 */
730
unsigned int CDECL _mbbtombc(unsigned int c)
731 732 733 734 735
{
  if(MSVCRT___mb_cur_max > 1 &&
     ((c >= 0x20 && c <=0x7e) ||(c >= 0xa1 && c <= 0xdf)))
  {
    /* FIXME: I can't get this function to return anything
736
     * different from what I pass it...
737 738 739 740 741 742 743 744
     */
  }
  return c;  /* ASCII CP or no MB char */
}

/*********************************************************************
 *		_ismbbkana(MSVCRT.@)
 */
745
int CDECL _ismbbkana(unsigned int c)
746 747
{
  /* FIXME: use lc_ctype when supported, not lc_all */
748
  if(MSVCRT___lc_codepage == 932)
749 750
  {
    /* Japanese/Katakana, CP 932 */
751
    return (c >= 0xa1 && c <= 0xdf);
752 753 754 755
  }
  return 0;
}

756 757 758
/*********************************************************************
 *              _ismbcdigit(MSVCRT.@)
 */
759
int CDECL _ismbcdigit(unsigned int ch)
760
{
761
    MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
762 763 764 765 766 767
    return (get_char_typeW( wch ) & C1_DIGIT);
}

/*********************************************************************
 *              _ismbcgraph(MSVCRT.@)
 */
768
int CDECL _ismbcgraph(unsigned int ch)
769
{
770
    MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
771 772 773 774 775 776
    return (get_char_typeW( wch ) & (C1_UPPER | C1_LOWER | C1_DIGIT | C1_PUNCT | C1_ALPHA));
}

/*********************************************************************
 *              _ismbcalpha (MSVCRT.@)
 */
777
int CDECL _ismbcalpha(unsigned int ch)
778
{
779
    MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
780 781 782 783 784 785
    return (get_char_typeW( wch ) & C1_ALPHA);
}

/*********************************************************************
 *              _ismbclower (MSVCRT.@)
 */
786
int CDECL _ismbclower(unsigned int ch)
787
{
788
    MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
789 790 791 792 793 794
    return (get_char_typeW( wch ) & C1_UPPER);
}

/*********************************************************************
 *              _ismbcupper (MSVCRT.@)
 */
795
int CDECL _ismbcupper(unsigned int ch)
796
{
797
    MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
798 799 800 801 802 803
    return (get_char_typeW( wch ) & C1_LOWER);
}

/*********************************************************************
 *              _ismbcsymbol(MSVCRT.@)
 */
804
int CDECL _ismbcsymbol(unsigned int ch)
805
{
806
    MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
807 808
    WORD ctype;
    if (!GetStringTypeW(CT_CTYPE3, &wch, 1, &ctype))
809
    {
810 811
        WARN("GetStringTypeW failed on %x\n", ch);
        return 0;
812
    }
813 814 815 816 817 818
    return ((ctype & C3_SYMBOL) != 0);
}

/*********************************************************************
 *              _ismbcalnum (MSVCRT.@)
 */
819
int CDECL _ismbcalnum(unsigned int ch)
820
{
821
    MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
822
    return (get_char_typeW( wch ) & (C1_ALPHA | C1_DIGIT));
823 824 825 826 827
}

/*********************************************************************
 *              _ismbcspace (MSVCRT.@)
 */
828
int CDECL _ismbcspace(unsigned int ch)
829
{
830
    MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
831 832
    return (get_char_typeW( wch ) & C1_SPACE);
}
833

834 835 836
/*********************************************************************
 *              _ismbcprint (MSVCRT.@)
 */
837
int CDECL _ismbcprint(unsigned int ch)
838
{
839
    MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
840 841 842 843 844 845
    return (get_char_typeW( wch ) & (C1_UPPER | C1_LOWER | C1_DIGIT | C1_PUNCT | C1_ALPHA | C1_SPACE));
}

/*********************************************************************
 *              _ismbcpunct(MSVCRT.@)
 */
846
int CDECL _ismbcpunct(unsigned int ch)
847
{
848
    MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
849
    return (get_char_typeW( wch ) & C1_PUNCT);
850 851
}

852 853 854
/*********************************************************************
 *		_ismbchira(MSVCRT.@)
 */
855
int CDECL _ismbchira(unsigned int c)
856 857
{
  /* FIXME: use lc_ctype when supported, not lc_all */
858
  if(MSVCRT___lc_codepage == 932)
859 860
  {
    /* Japanese/Hiragana, CP 932 */
861
    return (c >= 0x829f && c <= 0x82f1);
862 863 864 865 866 867 868
  }
  return 0;
}

/*********************************************************************
 *		_ismbckata(MSVCRT.@)
 */
869
int CDECL _ismbckata(unsigned int c)
870 871
{
  /* FIXME: use lc_ctype when supported, not lc_all */
872
  if(MSVCRT___lc_codepage == 932)
873 874
  {
    if(c < 256)
875
      return _ismbbkana(c);
876
    /* Japanese/Katakana, CP 932 */
877
    return (c >= 0x8340 && c <= 0x8396 && c != 0x837f);
878 879 880 881 882 883 884
  }
  return 0;
}

/*********************************************************************
 *		_ismbblead(MSVCRT.@)
 */
885
int CDECL _ismbblead(unsigned int c)
886 887 888 889 890 891 892 893 894
{
  /* FIXME: should reference MSVCRT_mbctype */
  return MSVCRT___mb_cur_max > 1 && MSVCRT_isleadbyte(c);
}


/*********************************************************************
 *		_ismbbtrail(MSVCRT.@)
 */
895
int CDECL _ismbbtrail(unsigned int c)
896 897
{
  /* FIXME: should reference MSVCRT_mbctype */
898
  return !_ismbblead(c);
899 900 901 902 903
}

/*********************************************************************
 *		_ismbslead(MSVCRT.@)
 */
904
int CDECL _ismbslead(const unsigned char* start, const unsigned char* str)
905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922
{
  /* Lead bytes can also be trail bytes if caller messed up
   * iterating through the string...
   */
  if(MSVCRT___mb_cur_max > 1)
  {
    while(start < str)
      start += MSVCRT_isleadbyte(*str) ? 2 : 1;

    if(start == str)
      return MSVCRT_isleadbyte(*str);
  }
  return 0; /* Must have been a trail, we skipped it */
}

/*********************************************************************
 *		_ismbstrail(MSVCRT.@)
 */
923
int CDECL _ismbstrail(const unsigned char* start, const unsigned char* str)
924
{
925
  /* Must not be a lead, and must be preceded by one */
926
  return !_ismbslead(start, str) && MSVCRT_isleadbyte(str[-1]);
927 928 929 930 931
}

/*********************************************************************
 *		_mbsset(MSVCRT.@)
 */
932
unsigned char* CDECL _mbsset(unsigned char* str, unsigned int c)
933
{
934
  unsigned char* ret = str;
935 936

  if(MSVCRT___mb_cur_max == 1 || c < 256)
Mike McCormack's avatar
Mike McCormack committed
937
    return u__strset(str, c); /* ASCII CP or SB char */
938 939 940 941 942 943 944 945 946 947 948 949 950 951

  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;
}

952 953 954
/*********************************************************************
 *		_mbsnbset(MSVCRT.@)
 */
955
unsigned char* CDECL _mbsnbset(unsigned char *str, unsigned int c, MSVCRT_size_t len)
956 957 958 959 960 961 962
{
    unsigned char *ret = str;

    if(!len)
	return ret;

    if(MSVCRT___mb_cur_max == 1 || c < 256)
Mike McCormack's avatar
Mike McCormack committed
963
	return u__strnset(str, c, len); /* ASCII CP or SB char */
964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981

    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;
}

982 983 984
/*********************************************************************
 *		_mbsnset(MSVCRT.@)
 */
985
unsigned char* CDECL _mbsnset(unsigned char* str, unsigned int c, MSVCRT_size_t len)
986
{
987
  unsigned char *ret = str;
988 989 990 991 992

  if(!len)
    return ret;

  if(MSVCRT___mb_cur_max == 1 || c < 256)
Mike McCormack's avatar
Mike McCormack committed
993
    return u__strnset(str, c, len); /* ASCII CP or SB char */
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009

  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.@)
1010
 * 'c' is for 'character'.
1011
 */
1012
MSVCRT_size_t CDECL _mbsnccnt(const unsigned char* str, MSVCRT_size_t len)
1013
{
1014
  MSVCRT_size_t ret;
1015 1016
  if(MSVCRT___mb_cur_max > 1)
  {
1017
    ret=0;
1018 1019 1020 1021
    while(*str && len-- > 0)
    {
      if(MSVCRT_isleadbyte(*str))
      {
1022 1023
        if (!len)
          break;
1024
        len--;
1025
        str++;
1026 1027
      }
      str++;
1028
      ret++;
1029 1030 1031
    }
    return ret;
  }
Mike McCormack's avatar
Mike McCormack committed
1032
  ret=u_strlen(str);
1033
  return min(ret, len); /* ASCII CP */
1034 1035
}

1036 1037 1038 1039
/*********************************************************************
 *		_mbsnbcnt(MSVCRT.@)
 * 'b' is for byte count.
 */
1040
MSVCRT_size_t CDECL _mbsnbcnt(const unsigned char* str, MSVCRT_size_t len)
1041
{
1042
  MSVCRT_size_t ret;
1043 1044
  if(MSVCRT___mb_cur_max > 1)
  {
1045
    const unsigned char* xstr = str;
1046 1047
    while(*xstr && len-- > 0)
    {
1048
      if (MSVCRT_isleadbyte(*xstr++))
1049 1050 1051 1052
        xstr++;
    }
    return xstr-str;
  }
Mike McCormack's avatar
Mike McCormack committed
1053
  ret=u_strlen(str);
1054
  return min(ret, len); /* ASCII CP */
1055 1056
}

1057 1058 1059
/*********************************************************************
 *		_mbsnbcat(MSVCRT.@)
 */
1060
unsigned char* CDECL _mbsnbcat(unsigned char* dst, const unsigned char* src, MSVCRT_size_t len)
1061 1062 1063
{
    if(MSVCRT___mb_cur_max > 1)
    {
Mike McCormack's avatar
Mike McCormack committed
1064
        unsigned char *res = dst;
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
        while (*dst) {
	    if (MSVCRT_isleadbyte(*dst++)) {
		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
1080
    return u_strncat(dst, src, len); /* ASCII CP */
1081 1082 1083
}


1084 1085 1086
/*********************************************************************
 *		_mbsncat(MSVCRT.@)
 */
1087
unsigned char* CDECL _mbsncat(unsigned char* dst, const unsigned char* src, MSVCRT_size_t len)
1088 1089 1090
{
  if(MSVCRT___mb_cur_max > 1)
  {
Mike McCormack's avatar
Mike McCormack committed
1091
    unsigned char *res = dst;
1092 1093 1094 1095 1096 1097
    while (*dst)
    {
      if (MSVCRT_isleadbyte(*dst++))
        dst++;
    }
    while (*src && len--)
1098
    {
1099 1100 1101
      *dst++ = *src;
      if(MSVCRT_isleadbyte(*src++))
        *dst++ = *src++;
1102
    }
1103
    *dst = '\0';
1104 1105
    return res;
  }
Mike McCormack's avatar
Mike McCormack committed
1106
  return u_strncat(dst, src, len); /* ASCII CP */
1107 1108
}

1109 1110 1111 1112

/*********************************************************************
 *              _mbslwr(MSVCRT.@)
 */
1113
unsigned char* CDECL _mbslwr(unsigned char* s)
1114
{
1115
  unsigned char *ret = s;
1116 1117 1118 1119 1120 1121
  if (!s)
    return NULL;
  if (MSVCRT___mb_cur_max > 1)
  {
    unsigned int c;
    while (*s)
1122
    {
1123 1124 1125 1126 1127 1128 1129 1130
      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;
1131
    }
1132
  }
1133 1134
  else for ( ; *s; s++) *s = tolower(*s);
  return ret;
1135 1136 1137
}


Francois Gouget's avatar
Francois Gouget committed
1138 1139 1140
/*********************************************************************
 *              _mbsupr(MSVCRT.@)
 */
1141
unsigned char* CDECL _mbsupr(unsigned char* s)
Francois Gouget's avatar
Francois Gouget committed
1142
{
1143
  unsigned char *ret = s;
1144 1145 1146 1147 1148 1149
  if (!s)
    return NULL;
  if (MSVCRT___mb_cur_max > 1)
  {
    unsigned int c;
    while (*s)
1150
    {
1151 1152 1153 1154 1155 1156 1157 1158
      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;
1159
    }
1160
  }
1161 1162
  else for ( ; *s; s++) *s = toupper(*s);
  return ret;
1163 1164 1165 1166 1167 1168
}


/*********************************************************************
 *              _mbsspn (MSVCRT.@)
 */
1169
MSVCRT_size_t CDECL _mbsspn(const unsigned char* string, const unsigned char* set)
1170
{
1171
    const unsigned char *p, *q;
1172

1173
    for (p = string; *p; p++)
1174
    {
1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193
        if (MSVCRT_isleadbyte(*p))
        {
            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;
        }
1194
    }
1195
    return p - string;
1196 1197
}

1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
/*********************************************************************
 *              _mbsspnp (MSVCRT.@)
 */
const unsigned char* CDECL _mbsspnp(const unsigned char* string, const unsigned char* set)
{
    const unsigned char *p, *q;

    for (p = string; *p; p++)
    {
        if (MSVCRT_isleadbyte(*p))
        {
            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;
    return p;
}

1232
/*********************************************************************
1233
 *		_mbscspn(MSVCRT.@)
1234
 */
1235
MSVCRT_size_t CDECL _mbscspn(const unsigned char* str, const unsigned char* cmp)
1236
{
1237 1238
  if (MSVCRT___mb_cur_max > 1)
    FIXME("don't handle double character case\n");
Mike McCormack's avatar
Mike McCormack committed
1239
  return u_strcspn(str, cmp);
1240
}
1241 1242 1243 1244

/*********************************************************************
 *              _mbsrev (MSVCRT.@)
 */
1245
unsigned char* CDECL _mbsrev(unsigned char* str)
1246 1247
{
    int i, len = _mbslen(str);
1248
    unsigned char *p, *temp=MSVCRT_malloc(len*2);
1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288

    if(!temp)
        return str;

    /* unpack multibyte string to temp buffer */
    p=str;
    for(i=0; i<len; i++)
    {
        if (MSVCRT_isleadbyte(*p))
        {
            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--)
    {
        if(MSVCRT_isleadbyte(temp[i*2]))
        {
            *p++=temp[i*2];
            *p++=temp[i*2+1];
        }
        else
        {
            *p++=temp[i*2];
        }
    }

    MSVCRT_free(temp);

    return str;
}

1289 1290 1291
/*********************************************************************
 *		_mbspbrk (MSVCRT.@)
 */
1292
unsigned char* CDECL _mbspbrk(const unsigned char* str, const unsigned char* accept)
1293
{
1294
    const unsigned char* p;
1295 1296 1297 1298 1299 1300 1301

    while(*str)
    {
        for(p = accept; *p; p += (MSVCRT_isleadbyte(*p)?2:1) )
        {
            if (*p == *str)
                if( !MSVCRT_isleadbyte(*p) || ( *(p+1) == *(str+1) ) )
1302
                     return (unsigned char*)str;
1303 1304 1305 1306 1307
        }
        str += (MSVCRT_isleadbyte(*str)?2:1);
    }
    return NULL;
}