d3d10/effect: Use case-insensitive comparison in GetMemberTypeBySemantic().
[wine.git] / dlls / msvcrt / string.c
blob4d09405094dc9a87bdb265239b157847c53b4283
1 /*
2 * MSVCRT string functions
4 * Copyright 1996,1998 Marcus Meissner
5 * Copyright 1996 Jukka Iivonen
6 * Copyright 1997,2000 Uwe Bonnes
7 * Copyright 2000 Jon Griffiths
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include <assert.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <math.h>
28 #include <limits.h>
29 #include <locale.h>
30 #include <float.h>
31 #include "msvcrt.h"
32 #include "bnum.h"
33 #include "winnls.h"
34 #include "wine/asm.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
39 /*********************************************************************
40 * _mbsdup (MSVCRT.@)
41 * _strdup (MSVCRT.@)
43 char* CDECL _strdup(const char* str)
45 if(str)
47 char * ret = malloc(strlen(str)+1);
48 if (ret) strcpy( ret, str );
49 return ret;
51 else return 0;
54 /*********************************************************************
55 * _strlwr_s_l (MSVCRT.@)
57 int CDECL _strlwr_s_l(char *str, size_t len, _locale_t locale)
59 pthreadlocinfo locinfo;
60 char *ptr = str;
62 if (!str || !len)
64 *_errno() = EINVAL;
65 return EINVAL;
68 while (len && *ptr)
70 len--;
71 ptr++;
74 if (!len)
76 str[0] = '\0';
77 *_errno() = EINVAL;
78 return EINVAL;
81 if(!locale)
82 locinfo = get_locinfo();
83 else
84 locinfo = locale->locinfo;
86 if(!locinfo->lc_handle[LC_CTYPE])
88 while (*str)
90 if (*str >= 'A' && *str <= 'Z')
91 *str -= 'A' - 'a';
92 str++;
95 else
97 while (*str)
99 *str = _tolower_l((unsigned char)*str, locale);
100 str++;
104 return 0;
107 /*********************************************************************
108 * _strlwr_s (MSVCRT.@)
110 int CDECL _strlwr_s(char *str, size_t len)
112 return _strlwr_s_l(str, len, NULL);
115 /*********************************************************************
116 * _strlwr_l (MSVCRT.@)
118 char* CDECL _strlwr_l(char *str, _locale_t locale)
120 _strlwr_s_l(str, -1, locale);
121 return str;
124 /*********************************************************************
125 * _strlwr (MSVCRT.@)
127 char* CDECL _strlwr(char *str)
129 _strlwr_s_l(str, -1, NULL);
130 return str;
133 /*********************************************************************
134 * _strupr_s_l (MSVCRT.@)
136 int CDECL _strupr_s_l(char *str, size_t len, _locale_t locale)
138 pthreadlocinfo locinfo;
139 char *ptr = str;
141 if (!str || !len)
143 *_errno() = EINVAL;
144 return EINVAL;
147 while (len && *ptr)
149 len--;
150 ptr++;
153 if (!len)
155 str[0] = '\0';
156 *_errno() = EINVAL;
157 return EINVAL;
160 if(!locale)
161 locinfo = get_locinfo();
162 else
163 locinfo = locale->locinfo;
165 if(!locinfo->lc_handle[LC_CTYPE])
167 while (*str)
169 if (*str >= 'a' && *str <= 'z')
170 *str -= 'a' - 'A';
171 str++;
174 else
176 while (*str)
178 *str = _toupper_l((unsigned char)*str, locale);
179 str++;
183 return 0;
186 /*********************************************************************
187 * _strupr_s (MSVCRT.@)
189 int CDECL _strupr_s(char *str, size_t len)
191 return _strupr_s_l(str, len, NULL);
194 /*********************************************************************
195 * _strupr_l (MSVCRT.@)
197 char* CDECL _strupr_l(char *str, _locale_t locale)
199 _strupr_s_l(str, -1, locale);
200 return str;
203 /*********************************************************************
204 * _strupr (MSVCRT.@)
206 char* CDECL _strupr(char *str)
208 _strupr_s_l(str, -1, NULL);
209 return str;
212 /*********************************************************************
213 * _strnset_s (MSVCRT.@)
215 int CDECL _strnset_s(char *str, size_t size, int c, size_t count)
217 size_t i;
219 if(!str && !size && !count) return 0;
220 if(!MSVCRT_CHECK_PMT(str != NULL)) return EINVAL;
221 if(!MSVCRT_CHECK_PMT(size > 0)) return EINVAL;
223 for(i=0; i<size-1 && i<count; i++) {
224 if(!str[i]) return 0;
225 str[i] = c;
227 for(; i<size; i++)
228 if(!str[i]) return 0;
230 str[0] = 0;
231 _invalid_parameter(NULL, NULL, NULL, 0, 0);
232 *_errno() = EINVAL;
233 return EINVAL;
236 /*********************************************************************
237 * _strnset (MSVCRT.@)
239 char* CDECL _strnset(char* str, int value, size_t len)
241 if (len > 0 && str)
242 while (*str && len--)
243 *str++ = value;
244 return str;
247 /*********************************************************************
248 * _strrev (MSVCRT.@)
250 char* CDECL _strrev(char* str)
252 char * p1;
253 char * p2;
255 if (str && *str)
256 for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
258 *p1 ^= *p2;
259 *p2 ^= *p1;
260 *p1 ^= *p2;
263 return str;
266 /*********************************************************************
267 * _strset (MSVCRT.@)
269 char* CDECL _strset(char* str, int value)
271 char *ptr = str;
272 while (*ptr)
273 *ptr++ = value;
275 return str;
278 /*********************************************************************
279 * strtok (MSVCRT.@)
281 char * CDECL strtok( char *str, const char *delim )
283 thread_data_t *data = msvcrt_get_thread_data();
284 char *ret;
286 if (!str)
287 if (!(str = data->strtok_next)) return NULL;
289 while (*str && strchr( delim, *str )) str++;
290 if (!*str)
292 data->strtok_next = str;
293 return NULL;
295 ret = str++;
296 while (*str && !strchr( delim, *str )) str++;
297 if (*str) *str++ = 0;
298 data->strtok_next = str;
299 return ret;
302 /*********************************************************************
303 * strtok_s (MSVCRT.@)
305 char * CDECL strtok_s(char *str, const char *delim, char **ctx)
307 if (!MSVCRT_CHECK_PMT(delim != NULL)) return NULL;
308 if (!MSVCRT_CHECK_PMT(ctx != NULL)) return NULL;
309 if (!MSVCRT_CHECK_PMT(str != NULL || *ctx != NULL)) return NULL;
311 if(!str)
312 str = *ctx;
314 while(*str && strchr(delim, *str))
315 str++;
316 if(!*str)
318 *ctx = str;
319 return NULL;
322 *ctx = str+1;
323 while(**ctx && !strchr(delim, **ctx))
324 (*ctx)++;
325 if(**ctx)
326 *(*ctx)++ = 0;
328 return str;
331 /*********************************************************************
332 * _swab (MSVCRT.@)
334 void CDECL _swab(char* src, char* dst, int len)
336 if (len > 1)
338 len = (unsigned)len >> 1;
340 while (len--) {
341 char s0 = src[0];
342 char s1 = src[1];
343 *dst++ = s1;
344 *dst++ = s0;
345 src = src + 2;
350 static struct fpnum fpnum(int sign, int exp, ULONGLONG m, enum fpmod mod)
352 struct fpnum ret;
354 ret.sign = sign;
355 ret.exp = exp;
356 ret.m = m;
357 ret.mod = mod;
358 return ret;
361 int fpnum_double(struct fpnum *fp, double *d)
363 ULONGLONG bits = 0;
365 if (fp->mod == FP_VAL_INFINITY)
367 *d = fp->sign * INFINITY;
368 return 0;
371 if (fp->mod == FP_VAL_NAN)
373 bits = ~0;
374 if (fp->sign == 1)
375 bits &= ~((ULONGLONG)1 << (MANT_BITS + EXP_BITS - 1));
376 *d = *(double*)&bits;
377 return 0;
380 TRACE("%c %s *2^%d (round %d)\n", fp->sign == -1 ? '-' : '+',
381 wine_dbgstr_longlong(fp->m), fp->exp, fp->mod);
382 if (!fp->m)
384 *d = fp->sign * 0.0;
385 return 0;
388 /* make sure that we don't overflow modifying exponent */
389 if (fp->exp > 1<<EXP_BITS)
391 *d = fp->sign * INFINITY;
392 return ERANGE;
394 if (fp->exp < -(1<<EXP_BITS))
396 *d = fp->sign * 0.0;
397 return ERANGE;
399 fp->exp += MANT_BITS - 1;
401 /* normalize mantissa */
402 while(fp->m < (ULONGLONG)1 << (MANT_BITS-1))
404 fp->m <<= 1;
405 fp->exp--;
407 while(fp->m >= (ULONGLONG)1 << MANT_BITS)
409 if (fp->m & 1 || fp->mod != FP_ROUND_ZERO)
411 if (!(fp->m & 1)) fp->mod = FP_ROUND_DOWN;
412 else if(fp->mod == FP_ROUND_ZERO) fp->mod = FP_ROUND_EVEN;
413 else fp->mod = FP_ROUND_UP;
415 fp->m >>= 1;
416 fp->exp++;
418 fp->exp += (1 << (EXP_BITS-1)) - 1;
420 /* handle subnormals */
421 if (fp->exp <= 0)
423 if (fp->m & 1 && fp->mod == FP_ROUND_ZERO) fp->mod = FP_ROUND_EVEN;
424 else if (fp->m & 1) fp->mod = FP_ROUND_UP;
425 else if (fp->mod != FP_ROUND_ZERO) fp->mod = FP_ROUND_DOWN;
426 fp->m >>= 1;
428 while(fp->m && fp->exp<0)
430 if (fp->m & 1 && fp->mod == FP_ROUND_ZERO) fp->mod = FP_ROUND_EVEN;
431 else if (fp->m & 1) fp->mod = FP_ROUND_UP;
432 else if (fp->mod != FP_ROUND_ZERO) fp->mod = FP_ROUND_DOWN;
433 fp->m >>= 1;
434 fp->exp++;
437 /* round mantissa */
438 if (fp->mod == FP_ROUND_UP || (fp->mod == FP_ROUND_EVEN && fp->m & 1))
440 fp->m++;
442 /* handle subnormal that falls into regular range due to rounding */
443 if (fp->m == (ULONGLONG)1 << (MANT_BITS - 1))
445 fp->exp++;
447 else if (fp->m >= (ULONGLONG)1 << MANT_BITS)
449 fp->exp++;
450 fp->m >>= 1;
454 if (fp->exp >= (1<<EXP_BITS)-1)
456 *d = fp->sign * INFINITY;
457 return ERANGE;
459 if (!fp->m || fp->exp < 0)
461 *d = fp->sign * 0.0;
462 return ERANGE;
465 if (fp->sign == -1)
466 bits |= (ULONGLONG)1 << (MANT_BITS + EXP_BITS - 1);
467 bits |= (ULONGLONG)fp->exp << (MANT_BITS - 1);
468 bits |= fp->m & (((ULONGLONG)1 << (MANT_BITS - 1)) - 1);
470 TRACE("returning %s\n", wine_dbgstr_longlong(bits));
471 *d = *(double*)&bits;
472 return 0;
475 #define LDBL_EXP_BITS 15
476 #define LDBL_MANT_BITS 64
477 int fpnum_ldouble(struct fpnum *fp, MSVCRT__LDOUBLE *d)
479 if (fp->mod == FP_VAL_INFINITY)
481 d->x80[0] = 0;
482 d->x80[1] = 0x80000000;
483 d->x80[2] = (1 << LDBL_EXP_BITS) - 1;
484 if (fp->sign == -1)
485 d->x80[2] |= 1 << LDBL_EXP_BITS;
486 return 0;
489 if (fp->mod == FP_VAL_NAN)
491 d->x80[0] = ~0;
492 d->x80[1] = ~0;
493 d->x80[2] = (1 << LDBL_EXP_BITS) - 1;
494 if (fp->sign == -1)
495 d->x80[2] |= 1 << LDBL_EXP_BITS;
496 return 0;
499 TRACE("%c %s *2^%d (round %d)\n", fp->sign == -1 ? '-' : '+',
500 wine_dbgstr_longlong(fp->m), fp->exp, fp->mod);
501 if (!fp->m)
503 d->x80[0] = 0;
504 d->x80[1] = 0;
505 d->x80[2] = 0;
506 if (fp->sign == -1)
507 d->x80[2] |= 1 << LDBL_EXP_BITS;
508 return 0;
511 /* make sure that we don't overflow modifying exponent */
512 if (fp->exp > 1<<LDBL_EXP_BITS)
514 d->x80[0] = 0;
515 d->x80[1] = 0x80000000;
516 d->x80[2] = (1 << LDBL_EXP_BITS) - 1;
517 if (fp->sign == -1)
518 d->x80[2] |= 1 << LDBL_EXP_BITS;
519 return ERANGE;
521 if (fp->exp < -(1<<LDBL_EXP_BITS))
523 d->x80[0] = 0;
524 d->x80[1] = 0;
525 d->x80[2] = 0;
526 if (fp->sign == -1)
527 d->x80[2] |= 1 << LDBL_EXP_BITS;
528 return ERANGE;
530 fp->exp += LDBL_MANT_BITS - 1;
532 /* normalize mantissa */
533 while(fp->m < (ULONGLONG)1 << (LDBL_MANT_BITS-1))
535 fp->m <<= 1;
536 fp->exp--;
538 fp->exp += (1 << (LDBL_EXP_BITS-1)) - 1;
540 /* handle subnormals */
541 if (fp->exp <= 0)
543 if (fp->m & 1 && fp->mod == FP_ROUND_ZERO) fp->mod = FP_ROUND_EVEN;
544 else if (fp->m & 1) fp->mod = FP_ROUND_UP;
545 else if (fp->mod != FP_ROUND_ZERO) fp->mod = FP_ROUND_DOWN;
546 fp->m >>= 1;
548 while(fp->m && fp->exp<0)
550 if (fp->m & 1 && fp->mod == FP_ROUND_ZERO) fp->mod = FP_ROUND_EVEN;
551 else if (fp->m & 1) fp->mod = FP_ROUND_UP;
552 else if (fp->mod != FP_ROUND_ZERO) fp->mod = FP_ROUND_DOWN;
553 fp->m >>= 1;
554 fp->exp++;
557 /* round mantissa */
558 if (fp->mod == FP_ROUND_UP || (fp->mod == FP_ROUND_EVEN && fp->m & 1))
560 if (fp->m == UI64_MAX)
562 fp->m = (ULONGLONG)1 << (LDBL_MANT_BITS - 1);
563 fp->exp++;
565 else
567 fp->m++;
569 /* handle subnormal that falls into regular range due to rounding */
570 if ((fp->m ^ (fp->m - 1)) & ((ULONGLONG)1 << (LDBL_MANT_BITS - 1))) fp->exp++;
574 if (fp->exp >= (1<<LDBL_EXP_BITS)-1)
576 d->x80[0] = 0;
577 d->x80[1] = 0x80000000;
578 d->x80[2] = (1 << LDBL_EXP_BITS) - 1;
579 if (fp->sign == -1)
580 d->x80[2] |= 1 << LDBL_EXP_BITS;
581 return ERANGE;
583 if (!fp->m || fp->exp < 0)
585 d->x80[0] = 0;
586 d->x80[1] = 0;
587 d->x80[2] = 0;
588 if (fp->sign == -1)
589 d->x80[2] |= 1 << LDBL_EXP_BITS;
590 return ERANGE;
593 d->x80[0] = fp->m;
594 d->x80[1] = fp->m >> 32;
595 d->x80[2] = fp->exp;
596 if (fp->sign == -1)
597 d->x80[2] |= 1 << LDBL_EXP_BITS;
598 return 0;
601 #if _MSVCR_VER >= 140
603 static inline int hex2int(char c)
605 if (c >= '0' && c <= '9')
606 return c - '0';
607 else if (c >= 'a' && c <= 'f')
608 return c - 'a' + 10;
609 else if (c >= 'A' && c <= 'F')
610 return c - 'A' + 10;
611 return -1;
614 static struct fpnum fpnum_parse16(wchar_t get(void *ctx), void unget(void *ctx),
615 void *ctx, int sign, pthreadlocinfo locinfo)
617 BOOL found_digit = FALSE, found_dp = FALSE;
618 enum fpmod round = FP_ROUND_ZERO;
619 wchar_t nch;
620 ULONGLONG m = 0;
621 int val, exp = 0;
623 nch = get(ctx);
624 while(m < UI64_MAX/16)
626 val = hex2int(nch);
627 if (val == -1) break;
628 found_digit = TRUE;
629 nch = get(ctx);
631 m = m*16 + val;
633 while(1)
635 val = hex2int(nch);
636 if (val == -1) break;
637 nch = get(ctx);
638 exp += 4;
640 if (val || round != FP_ROUND_ZERO)
642 if (val < 8) round = FP_ROUND_DOWN;
643 else if (val == 8 && round == FP_ROUND_ZERO) round = FP_ROUND_EVEN;
644 else round = FP_ROUND_UP;
648 if(nch == *locinfo->lconv->decimal_point)
650 found_dp = TRUE;
651 nch = get(ctx);
653 else if (!found_digit)
655 if(nch!=WEOF) unget(ctx);
656 unget(ctx);
657 return fpnum(0, 0, 0, 0);
660 while(m <= UI64_MAX/16)
662 val = hex2int(nch);
663 if (val == -1) break;
664 found_digit = TRUE;
665 nch = get(ctx);
667 m = m*16 + val;
668 exp -= 4;
670 while(1)
672 val = hex2int(nch);
673 if (val == -1) break;
674 nch = get(ctx);
676 if (val || round != FP_ROUND_ZERO)
678 if (val < 8) round = FP_ROUND_DOWN;
679 else if (val == 8 && round == FP_ROUND_ZERO) round = FP_ROUND_EVEN;
680 else round = FP_ROUND_UP;
684 if (!found_digit)
686 if (nch != WEOF) unget(ctx);
687 if (found_dp) unget(ctx);
688 unget(ctx);
689 return fpnum(0, 0, 0, 0);
692 if(nch=='p' || nch=='P') {
693 BOOL found_sign = FALSE;
694 int e=0, s=1;
696 nch = get(ctx);
697 if(nch == '-') {
698 found_sign = TRUE;
699 s = -1;
700 nch = get(ctx);
701 } else if(nch == '+') {
702 found_sign = TRUE;
703 nch = get(ctx);
705 if(nch>='0' && nch<='9') {
706 while(nch>='0' && nch<='9') {
707 if(e>INT_MAX/10 || e*10>INT_MAX-nch+'0')
708 e = INT_MAX;
709 else
710 e = e*10+nch-'0';
711 nch = get(ctx);
713 if((nch!=WEOF) && (nch < '0' || nch > '9')) unget(ctx);
714 e *= s;
716 if(e<0 && exp<INT_MIN-e) exp = INT_MIN;
717 else if(e>0 && exp>INT_MAX-e) exp = INT_MAX;
718 else exp += e;
719 } else {
720 if(nch != WEOF) unget(ctx);
721 if(found_sign) unget(ctx);
722 unget(ctx);
726 return fpnum(sign, exp, m, round);
728 #endif
730 /* Converts first 3 limbs to ULONGLONG */
731 /* Return FALSE on overflow */
732 static inline BOOL bnum_to_mant(struct bnum *b, ULONGLONG *m)
734 if(UI64_MAX / LIMB_MAX / LIMB_MAX < b->data[bnum_idx(b, b->e-1)]) return FALSE;
735 *m = (ULONGLONG)b->data[bnum_idx(b, b->e-1)] * LIMB_MAX * LIMB_MAX;
736 if(b->b == b->e-1) return TRUE;
737 if(UI64_MAX - *m < (ULONGLONG)b->data[bnum_idx(b, b->e-2)] * LIMB_MAX) return FALSE;
738 *m += (ULONGLONG)b->data[bnum_idx(b, b->e-2)] * LIMB_MAX;
739 if(b->b == b->e-2) return TRUE;
740 if(UI64_MAX - *m < b->data[bnum_idx(b, b->e-3)]) return FALSE;
741 *m += b->data[bnum_idx(b, b->e-3)];
742 return TRUE;
745 static struct fpnum fpnum_parse_bnum(wchar_t (*get)(void *ctx), void (*unget)(void *ctx),
746 void *ctx, pthreadlocinfo locinfo, BOOL ldouble, struct bnum *b)
748 #if _MSVCR_VER >= 140
749 const wchar_t _infinity[] = L"infinity";
750 const wchar_t _nan[] = L"nan";
751 const wchar_t *str_match = NULL;
752 int matched=0;
753 #endif
754 BOOL found_digit = FALSE, found_dp = FALSE, found_sign = FALSE;
755 int e2 = 0, dp=0, sign=1, off, limb_digits = 0, i;
756 enum fpmod round = FP_ROUND_ZERO;
757 wchar_t nch;
758 ULONGLONG m;
760 nch = get(ctx);
761 if(nch == '-') {
762 found_sign = TRUE;
763 sign = -1;
764 nch = get(ctx);
765 } else if(nch == '+') {
766 found_sign = TRUE;
767 nch = get(ctx);
770 #if _MSVCR_VER >= 140
771 if(nch == _infinity[0] || nch == _toupper(_infinity[0]))
772 str_match = _infinity;
773 if(nch == _nan[0] || nch == _toupper(_nan[0]))
774 str_match = _nan;
775 while(str_match && nch != WEOF &&
776 (nch == str_match[matched] || nch == _toupper(str_match[matched]))) {
777 nch = get(ctx);
778 matched++;
780 if(str_match) {
781 int keep = 0;
782 if(matched >= 8) keep = 8;
783 else if(matched >= 3) keep = 3;
784 if(nch != WEOF) unget(ctx);
785 for (; matched > keep; matched--) {
786 unget(ctx);
788 if(keep) {
789 if (str_match == _infinity)
790 return fpnum(sign, 0, 0, FP_VAL_INFINITY);
791 if (str_match == _nan)
792 return fpnum(sign, 0, 0, FP_VAL_NAN);
793 } else if(found_sign) {
794 unget(ctx);
797 return fpnum(0, 0, 0, 0);
800 if(nch == '0') {
801 found_digit = TRUE;
802 nch = get(ctx);
803 if(nch == 'x' || nch == 'X')
804 return fpnum_parse16(get, unget, ctx, sign, locinfo);
806 #endif
808 while(nch == '0') {
809 found_digit = TRUE;
810 nch = get(ctx);
813 b->b = 0;
814 b->e = 1;
815 b->data[0] = 0;
816 while(nch>='0' && nch<='9') {
817 found_digit = TRUE;
818 if(limb_digits == LIMB_DIGITS) {
819 if(bnum_idx(b, b->b-1) == bnum_idx(b, b->e)) break;
820 else {
821 b->b--;
822 b->data[bnum_idx(b, b->b)] = 0;
823 limb_digits = 0;
827 b->data[bnum_idx(b, b->b)] = b->data[bnum_idx(b, b->b)] * 10 + nch - '0';
828 limb_digits++;
829 nch = get(ctx);
830 dp++;
832 while(nch>='0' && nch<='9') {
833 if(nch != '0') b->data[bnum_idx(b, b->b)] |= 1;
834 nch = get(ctx);
835 dp++;
838 if(nch == *locinfo->lconv->decimal_point) {
839 found_dp = TRUE;
840 nch = get(ctx);
843 /* skip leading '0' */
844 if(nch=='0' && !limb_digits && !b->b) {
845 found_digit = TRUE;
846 while(nch == '0') {
847 nch = get(ctx);
848 dp--;
852 while(nch>='0' && nch<='9') {
853 found_digit = TRUE;
854 if(limb_digits == LIMB_DIGITS) {
855 if(bnum_idx(b, b->b-1) == bnum_idx(b, b->e)) break;
856 else {
857 b->b--;
858 b->data[bnum_idx(b, b->b)] = 0;
859 limb_digits = 0;
863 b->data[bnum_idx(b, b->b)] = b->data[bnum_idx(b, b->b)] * 10 + nch - '0';
864 limb_digits++;
865 nch = get(ctx);
867 while(nch>='0' && nch<='9') {
868 if(nch != '0') b->data[bnum_idx(b, b->b)] |= 1;
869 nch = get(ctx);
872 if(!found_digit) {
873 if(nch != WEOF) unget(ctx);
874 if(found_dp) unget(ctx);
875 if(found_sign) unget(ctx);
876 return fpnum(0, 0, 0, 0);
879 if(nch=='e' || nch=='E' || nch=='d' || nch=='D') {
880 int e=0, s=1;
882 nch = get(ctx);
883 if(nch == '-') {
884 found_sign = TRUE;
885 s = -1;
886 nch = get(ctx);
887 } else if(nch == '+') {
888 found_sign = TRUE;
889 nch = get(ctx);
890 } else {
891 found_sign = FALSE;
894 if(nch>='0' && nch<='9') {
895 while(nch>='0' && nch<='9') {
896 if(e>INT_MAX/10 || e*10>INT_MAX-nch+'0')
897 e = INT_MAX;
898 else
899 e = e*10+nch-'0';
900 nch = get(ctx);
902 if(nch != WEOF) unget(ctx);
903 e *= s;
905 if(e<0 && dp<INT_MIN-e) dp = INT_MIN;
906 else if(e>0 && dp>INT_MAX-e) dp = INT_MAX;
907 else dp += e;
908 } else {
909 if(nch != WEOF) unget(ctx);
910 if(found_sign) unget(ctx);
911 unget(ctx);
913 } else if(nch != WEOF) {
914 unget(ctx);
917 if(!b->data[bnum_idx(b, b->e-1)])
918 return fpnum(sign, 0, 0, 0);
920 /* Fill last limb with 0 if needed */
921 if(b->b+1 != b->e) {
922 for(; limb_digits != LIMB_DIGITS; limb_digits++)
923 b->data[bnum_idx(b, b->b)] *= 10;
925 for(; bnum_idx(b, b->b) < bnum_idx(b, b->e); b->b++) {
926 if(b->data[bnum_idx(b, b->b)]) break;
929 /* move decimal point to limb boundary */
930 if(limb_digits==dp && b->b==b->e-1)
931 return fpnum(sign, 0, b->data[bnum_idx(b, b->e-1)], FP_ROUND_ZERO);
932 off = (dp - limb_digits) % LIMB_DIGITS;
933 if(off < 0) off += LIMB_DIGITS;
934 if(off) bnum_mult(b, p10s[off]);
936 if(dp-1 > (ldouble ? DBL80_MAX_10_EXP : DBL_MAX_10_EXP))
937 return fpnum(sign, INT_MAX, 1, FP_ROUND_ZERO);
938 /* Count part of exponent stored in denormalized mantissa. */
939 /* Increase exponent range to handle subnormals. */
940 if(dp-1 < (ldouble ? DBL80_MIN_10_EXP : DBL_MIN_10_EXP-DBL_DIG-18))
941 return fpnum(sign, INT_MIN, 1, FP_ROUND_ZERO);
943 while(dp > 3*LIMB_DIGITS) {
944 if(bnum_rshift(b, 9)) dp -= LIMB_DIGITS;
945 e2 += 9;
947 while(dp <= 2*LIMB_DIGITS) {
948 if(bnum_lshift(b, 29)) dp += LIMB_DIGITS;
949 e2 -= 29;
951 /* Make sure most significant mantissa bit will be set */
952 while(b->data[bnum_idx(b, b->e-1)] <= 9) {
953 bnum_lshift(b, 1);
954 e2--;
956 while(!bnum_to_mant(b, &m)) {
957 bnum_rshift(b, 1);
958 e2++;
961 if(b->e-4 >= b->b && b->data[bnum_idx(b, b->e-4)]) {
962 if(b->data[bnum_idx(b, b->e-4)] > LIMB_MAX/2) round = FP_ROUND_UP;
963 else if(b->data[bnum_idx(b, b->e-4)] == LIMB_MAX/2) round = FP_ROUND_EVEN;
964 else round = FP_ROUND_DOWN;
966 if(round == FP_ROUND_ZERO || round == FP_ROUND_EVEN) {
967 for(i=b->e-5; i>=b->b; i--) {
968 if(!b->data[bnum_idx(b, b->b)]) continue;
969 if(round == FP_ROUND_EVEN) round = FP_ROUND_UP;
970 else round = FP_ROUND_DOWN;
974 return fpnum(sign, e2, m, round);
977 struct fpnum fpnum_parse(wchar_t (*get)(void *ctx), void (*unget)(void *ctx),
978 void *ctx, pthreadlocinfo locinfo, BOOL ldouble)
980 if(!ldouble) {
981 BYTE bnum_data[FIELD_OFFSET(struct bnum, data[BNUM_PREC64])];
982 struct bnum *b = (struct bnum*)bnum_data;
984 b->size = BNUM_PREC64;
985 return fpnum_parse_bnum(get, unget, ctx, locinfo, ldouble, b);
986 } else {
987 BYTE bnum_data[FIELD_OFFSET(struct bnum, data[BNUM_PREC80])];
988 struct bnum *b = (struct bnum*)bnum_data;
990 b->size = BNUM_PREC80;
991 return fpnum_parse_bnum(get, unget, ctx, locinfo, ldouble, b);
995 static wchar_t strtod_str_get(void *ctx)
997 const char **p = ctx;
998 if (!**p) return WEOF;
999 return *(*p)++;
1002 static void strtod_str_unget(void *ctx)
1004 const char **p = ctx;
1005 (*p)--;
1008 static inline double strtod_helper(const char *str, char **end, _locale_t locale, int *perr)
1010 pthreadlocinfo locinfo;
1011 const char *beg, *p;
1012 struct fpnum fp;
1013 double ret;
1014 int err;
1016 if (perr) *perr = 0;
1017 #if _MSVCR_VER == 0
1018 else *_errno() = 0;
1019 #endif
1021 if (!MSVCRT_CHECK_PMT(str != NULL)) {
1022 if (end) *end = NULL;
1023 return 0;
1026 if (!locale)
1027 locinfo = get_locinfo();
1028 else
1029 locinfo = locale->locinfo;
1031 p = str;
1032 while(_isspace_l((unsigned char)*p, locale))
1033 p++;
1034 beg = p;
1036 fp = fpnum_parse(strtod_str_get, strtod_str_unget, &p, locinfo, FALSE);
1037 if (end) *end = (p == beg ? (char*)str : (char*)p);
1039 err = fpnum_double(&fp, &ret);
1040 if (perr) *perr = err;
1041 else if(err) *_errno() = err;
1042 return ret;
1045 /*********************************************************************
1046 * _strtod_l (MSVCRT.@)
1048 double CDECL _strtod_l(const char *str, char **end, _locale_t locale)
1050 return strtod_helper(str, end, locale, NULL);
1053 /*********************************************************************
1054 * strtod (MSVCRT.@)
1056 double CDECL strtod( const char *str, char **end )
1058 return _strtod_l( str, end, NULL );
1061 #if _MSVCR_VER>=120
1063 /*********************************************************************
1064 * strtof_l (MSVCR120.@)
1066 float CDECL _strtof_l( const char *str, char **end, _locale_t locale )
1068 double ret = _strtod_l(str, end, locale);
1069 if (ret && isfinite(ret)) {
1070 float f = ret;
1071 if (!f || !isfinite(f))
1072 *_errno() = ERANGE;
1074 return ret;
1077 /*********************************************************************
1078 * strtof (MSVCR120.@)
1080 float CDECL strtof( const char *str, char **end )
1082 return _strtof_l(str, end, NULL);
1085 #endif /* _MSVCR_VER>=120 */
1087 /*********************************************************************
1088 * atof (MSVCRT.@)
1090 double CDECL atof( const char *str )
1092 return _strtod_l(str, NULL, NULL);
1095 /*********************************************************************
1096 * _atof_l (MSVCRT.@)
1098 double CDECL _atof_l( const char *str, _locale_t locale)
1100 return _strtod_l(str, NULL, locale);
1103 /*********************************************************************
1104 * _atoflt_l (MSVCRT.@)
1106 int CDECL _atoflt_l(_CRT_FLOAT *value, char *str, _locale_t locale)
1108 double d;
1109 int err;
1111 d = strtod_helper(str, NULL, locale, &err);
1112 value->f = d;
1113 if(isinf(value->f))
1114 return _OVERFLOW;
1115 if((d!=0 || err) && value->f>-FLT_MIN && value->f<FLT_MIN)
1116 return _UNDERFLOW;
1117 return 0;
1120 /*********************************************************************
1121 * _atoflt (MSVCR100.@)
1123 int CDECL _atoflt(_CRT_FLOAT *value, char *str)
1125 return _atoflt_l(value, str, NULL);
1128 /*********************************************************************
1129 * _atodbl_l (MSVCRT.@)
1131 int CDECL _atodbl_l(_CRT_DOUBLE *value, char *str, _locale_t locale)
1133 int err;
1135 value->x = strtod_helper(str, NULL, locale, &err);
1136 if(isinf(value->x))
1137 return _OVERFLOW;
1138 if((value->x!=0 || err) && value->x>-DBL_MIN && value->x<DBL_MIN)
1139 return _UNDERFLOW;
1140 return 0;
1143 /*********************************************************************
1144 * _atodbl (MSVCRT.@)
1146 int CDECL _atodbl(_CRT_DOUBLE *value, char *str)
1148 return _atodbl_l(value, str, NULL);
1151 /*********************************************************************
1152 * _strcoll_l (MSVCRT.@)
1154 int CDECL _strcoll_l( const char* str1, const char* str2, _locale_t locale )
1156 pthreadlocinfo locinfo;
1158 if(!locale)
1159 locinfo = get_locinfo();
1160 else
1161 locinfo = locale->locinfo;
1163 if(!locinfo->lc_handle[LC_COLLATE])
1164 return strcmp(str1, str2);
1165 return CompareStringA(locinfo->lc_handle[LC_COLLATE], 0, str1, -1, str2, -1)-CSTR_EQUAL;
1168 /*********************************************************************
1169 * strcoll (MSVCRT.@)
1171 int CDECL strcoll( const char* str1, const char* str2 )
1173 return _strcoll_l(str1, str2, NULL);
1176 /*********************************************************************
1177 * _stricoll_l (MSVCRT.@)
1179 int CDECL _stricoll_l( const char* str1, const char* str2, _locale_t locale )
1181 pthreadlocinfo locinfo;
1183 if(!locale)
1184 locinfo = get_locinfo();
1185 else
1186 locinfo = locale->locinfo;
1188 if(!locinfo->lc_handle[LC_COLLATE])
1189 return _stricmp(str1, str2);
1190 return CompareStringA(locinfo->lc_handle[LC_COLLATE], NORM_IGNORECASE,
1191 str1, -1, str2, -1)-CSTR_EQUAL;
1194 /*********************************************************************
1195 * _stricoll (MSVCRT.@)
1197 int CDECL _stricoll( const char* str1, const char* str2 )
1199 return _stricoll_l(str1, str2, NULL);
1202 /*********************************************************************
1203 * _strncoll_l (MSVCRT.@)
1205 int CDECL _strncoll_l( const char* str1, const char* str2, size_t count, _locale_t locale )
1207 pthreadlocinfo locinfo;
1209 if(!locale)
1210 locinfo = get_locinfo();
1211 else
1212 locinfo = locale->locinfo;
1214 if(!locinfo->lc_handle[LC_COLLATE])
1215 return strncmp(str1, str2, count);
1216 return CompareStringA(locinfo->lc_handle[LC_COLLATE], 0,
1217 str1, strnlen(str1, count),
1218 str2, strnlen(str2, count))-CSTR_EQUAL;
1221 /*********************************************************************
1222 * _strncoll (MSVCRT.@)
1224 int CDECL _strncoll( const char* str1, const char* str2, size_t count )
1226 return _strncoll_l(str1, str2, count, NULL);
1229 /*********************************************************************
1230 * _strnicoll_l (MSVCRT.@)
1232 int CDECL _strnicoll_l( const char* str1, const char* str2, size_t count, _locale_t locale )
1234 pthreadlocinfo locinfo;
1236 if(!locale)
1237 locinfo = get_locinfo();
1238 else
1239 locinfo = locale->locinfo;
1241 if(!locinfo->lc_handle[LC_COLLATE])
1242 return _strnicmp(str1, str2, count);
1243 return CompareStringA(locinfo->lc_handle[LC_COLLATE], NORM_IGNORECASE,
1244 str1, strnlen(str1, count),
1245 str2, strnlen(str2, count))-CSTR_EQUAL;
1248 /*********************************************************************
1249 * _strnicoll (MSVCRT.@)
1251 int CDECL _strnicoll( const char* str1, const char* str2, size_t count )
1253 return _strnicoll_l(str1, str2, count, NULL);
1256 /*********************************************************************
1257 * strncpy (MSVCRT.@)
1259 char* __cdecl strncpy(char *dst, const char *src, size_t len)
1261 size_t i;
1263 for(i=0; i<len; i++)
1264 if((dst[i] = src[i]) == '\0') break;
1266 while (i < len) dst[i++] = 0;
1268 return dst;
1271 /*********************************************************************
1272 * strcpy (MSVCRT.@)
1274 char* CDECL strcpy(char *dst, const char *src)
1276 char *ret = dst;
1277 while ((*dst++ = *src++));
1278 return ret;
1281 /*********************************************************************
1282 * strcpy_s (MSVCRT.@)
1284 int CDECL strcpy_s( char* dst, size_t elem, const char* src )
1286 size_t i;
1287 if(!elem) return EINVAL;
1288 if(!dst) return EINVAL;
1289 if(!src)
1291 dst[0] = '\0';
1292 return EINVAL;
1295 for(i = 0; i < elem; i++)
1297 if((dst[i] = src[i]) == '\0') return 0;
1299 dst[0] = '\0';
1300 return ERANGE;
1303 /*********************************************************************
1304 * strcat_s (MSVCRT.@)
1306 int CDECL strcat_s( char* dst, size_t elem, const char* src )
1308 size_t i, j;
1309 if(!dst) return EINVAL;
1310 if(elem == 0) return EINVAL;
1311 if(!src)
1313 dst[0] = '\0';
1314 return EINVAL;
1317 for(i = 0; i < elem; i++)
1319 if(dst[i] == '\0')
1321 for(j = 0; (j + i) < elem; j++)
1323 if((dst[j + i] = src[j]) == '\0') return 0;
1327 /* Set the first element to 0, not the first element after the skipped part */
1328 dst[0] = '\0';
1329 return ERANGE;
1332 /*********************************************************************
1333 * strcat (MSVCRT.@)
1335 char* __cdecl strcat( char *dst, const char *src )
1337 char *d = dst;
1338 while (*d) d++;
1339 while ((*d++ = *src++));
1340 return dst;
1343 /*********************************************************************
1344 * strncat_s (MSVCRT.@)
1346 int CDECL strncat_s( char* dst, size_t elem, const char* src, size_t count )
1348 size_t i, j;
1350 if (!MSVCRT_CHECK_PMT(dst != 0)) return EINVAL;
1351 if (!MSVCRT_CHECK_PMT(elem != 0)) return EINVAL;
1352 if (!MSVCRT_CHECK_PMT(src != 0))
1354 dst[0] = '\0';
1355 return EINVAL;
1358 for(i = 0; i < elem; i++)
1360 if(dst[i] == '\0')
1362 for(j = 0; (j + i) < elem; j++)
1364 if(count == _TRUNCATE && j + i == elem - 1)
1366 dst[j + i] = '\0';
1367 return STRUNCATE;
1369 if(j == count || (dst[j + i] = src[j]) == '\0')
1371 dst[j + i] = '\0';
1372 return 0;
1377 /* Set the first element to 0, not the first element after the skipped part */
1378 dst[0] = '\0';
1379 return ERANGE;
1382 /*********************************************************************
1383 * strncat (MSVCRT.@)
1385 char* __cdecl strncat(char *dst, const char *src, size_t len)
1387 char *d = dst;
1388 while (*d) d++;
1389 for ( ; len && *src; d++, src++, len--) *d = *src;
1390 *d = 0;
1391 return dst;
1394 /*********************************************************************
1395 * _strxfrm_l (MSVCRT.@)
1397 size_t CDECL _strxfrm_l( char *dest, const char *src,
1398 size_t len, _locale_t locale )
1400 pthreadlocinfo locinfo;
1401 int ret;
1403 if(!MSVCRT_CHECK_PMT(src)) return INT_MAX;
1404 if(!MSVCRT_CHECK_PMT(dest || !len)) return INT_MAX;
1406 if(len > INT_MAX) {
1407 FIXME("len > INT_MAX not supported\n");
1408 len = INT_MAX;
1411 if(!locale)
1412 locinfo = get_locinfo();
1413 else
1414 locinfo = locale->locinfo;
1416 if(!locinfo->lc_handle[LC_COLLATE]) {
1417 strncpy(dest, src, len);
1418 return strlen(src);
1421 ret = LCMapStringA(locinfo->lc_handle[LC_COLLATE],
1422 LCMAP_SORTKEY, src, -1, NULL, 0);
1423 if(!ret) {
1424 if(len) dest[0] = 0;
1425 *_errno() = EILSEQ;
1426 return INT_MAX;
1428 if(!len) return ret-1;
1430 if(ret > len) {
1431 dest[0] = 0;
1432 *_errno() = ERANGE;
1433 return ret-1;
1436 return LCMapStringA(locinfo->lc_handle[LC_COLLATE],
1437 LCMAP_SORTKEY, src, -1, dest, len) - 1;
1440 /*********************************************************************
1441 * strxfrm (MSVCRT.@)
1443 size_t CDECL strxfrm( char *dest, const char *src, size_t len )
1445 return _strxfrm_l(dest, src, len, NULL);
1448 /********************************************************************
1449 * __STRINGTOLD_L (MSVCR80.@)
1451 int CDECL __STRINGTOLD_L( MSVCRT__LDOUBLE *value, char **endptr,
1452 const char *str, int flags, _locale_t locale )
1454 pthreadlocinfo locinfo;
1455 const char *beg, *p;
1456 int err, ret = 0;
1457 struct fpnum fp;
1459 if (flags) FIXME("flags not supported: %x\n", flags);
1461 if (!locale)
1462 locinfo = get_locinfo();
1463 else
1464 locinfo = locale->locinfo;
1466 p = str;
1467 while (_isspace_l((unsigned char)*p, locale))
1468 p++;
1469 beg = p;
1471 fp = fpnum_parse(strtod_str_get, strtod_str_unget, &p, locinfo, TRUE);
1472 if (endptr) *endptr = (p == beg ? (char*)str : (char*)p);
1473 if (p == beg) ret = 4;
1475 err = fpnum_ldouble(&fp, value);
1476 if (err) ret = (value->x80[2] & 0x7fff ? 2 : 1);
1477 return ret;
1480 /********************************************************************
1481 * __STRINGTOLD (MSVCRT.@)
1483 int CDECL __STRINGTOLD( MSVCRT__LDOUBLE *value, char **endptr, const char *str, int flags )
1485 return __STRINGTOLD_L( value, endptr, str, flags, NULL );
1488 /********************************************************************
1489 * _atoldbl_l (MSVCRT.@)
1491 int CDECL _atoldbl_l( MSVCRT__LDOUBLE *value, char *str, _locale_t locale )
1493 char *endptr;
1494 switch(__STRINGTOLD_L( value, &endptr, str, 0, locale ))
1496 case 1: return _UNDERFLOW;
1497 case 2: return _OVERFLOW;
1498 default: return 0;
1502 /********************************************************************
1503 * _atoldbl (MSVCRT.@)
1505 int CDECL _atoldbl(_LDOUBLE *value, char *str)
1507 return _atoldbl_l( (MSVCRT__LDOUBLE*)value, str, NULL );
1510 /*********************************************************************
1511 * strlen (MSVCRT.@)
1513 size_t __cdecl strlen(const char *str)
1515 const char *s = str;
1516 while (*s) s++;
1517 return s - str;
1520 /******************************************************************
1521 * strnlen (MSVCRT.@)
1523 size_t CDECL strnlen(const char *s, size_t maxlen)
1525 size_t i;
1527 for(i=0; i<maxlen; i++)
1528 if(!s[i]) break;
1530 return i;
1533 /*********************************************************************
1534 * _strtoi64_l (MSVCRT.@)
1536 * FIXME: locale parameter is ignored
1538 __int64 CDECL _strtoi64_l(const char *nptr, char **endptr, int base, _locale_t locale)
1540 const char *p = nptr;
1541 BOOL negative = FALSE;
1542 BOOL got_digit = FALSE;
1543 __int64 ret = 0;
1545 TRACE("(%s %p %d %p)\n", debugstr_a(nptr), endptr, base, locale);
1547 if (!MSVCRT_CHECK_PMT(nptr != NULL)) return 0;
1548 if (!MSVCRT_CHECK_PMT(base == 0 || base >= 2)) return 0;
1549 if (!MSVCRT_CHECK_PMT(base <= 36)) return 0;
1551 while(_isspace_l((unsigned char)*nptr, locale)) nptr++;
1553 if(*nptr == '-') {
1554 negative = TRUE;
1555 nptr++;
1556 } else if(*nptr == '+')
1557 nptr++;
1559 if((base==0 || base==16) && *nptr=='0' && _tolower_l(*(nptr+1), locale)=='x') {
1560 base = 16;
1561 nptr += 2;
1564 if(base == 0) {
1565 if(*nptr=='0')
1566 base = 8;
1567 else
1568 base = 10;
1571 while(*nptr) {
1572 char cur = _tolower_l(*nptr, locale);
1573 int v;
1575 if(cur>='0' && cur<='9') {
1576 if(cur >= '0'+base)
1577 break;
1578 v = cur-'0';
1579 } else {
1580 if(cur<'a' || cur>='a'+base-10)
1581 break;
1582 v = cur-'a'+10;
1584 got_digit = TRUE;
1586 if(negative)
1587 v = -v;
1589 nptr++;
1591 if(!negative && (ret>I64_MAX/base || ret*base>I64_MAX-v)) {
1592 ret = I64_MAX;
1593 *_errno() = ERANGE;
1594 } else if(negative && (ret<I64_MIN/base || ret*base<I64_MIN-v)) {
1595 ret = I64_MIN;
1596 *_errno() = ERANGE;
1597 } else
1598 ret = ret*base + v;
1601 if(endptr)
1602 *endptr = (char*)(got_digit ? nptr : p);
1604 return ret;
1607 /*********************************************************************
1608 * _strtoi64 (MSVCRT.@)
1610 __int64 CDECL _strtoi64(const char *nptr, char **endptr, int base)
1612 return _strtoi64_l(nptr, endptr, base, NULL);
1615 /*********************************************************************
1616 * _atoi_l (MSVCRT.@)
1618 int __cdecl _atoi_l(const char *str, _locale_t locale)
1620 __int64 ret = _strtoi64_l(str, NULL, 10, locale);
1622 if(ret > INT_MAX) {
1623 ret = INT_MAX;
1624 *_errno() = ERANGE;
1625 } else if(ret < INT_MIN) {
1626 ret = INT_MIN;
1627 *_errno() = ERANGE;
1629 return ret;
1632 /*********************************************************************
1633 * atoi (MSVCRT.@)
1635 #if _MSVCR_VER == 0
1636 int __cdecl atoi(const char *str)
1638 BOOL minus = FALSE;
1639 int ret = 0;
1641 if(!str)
1642 return 0;
1644 while(_isspace_l((unsigned char)*str, NULL)) str++;
1646 if(*str == '+') {
1647 str++;
1648 }else if(*str == '-') {
1649 minus = TRUE;
1650 str++;
1653 while(*str>='0' && *str<='9') {
1654 ret = ret*10+*str-'0';
1655 str++;
1658 return minus ? -ret : ret;
1660 #else
1661 int CDECL atoi(const char *str)
1663 return _atoi_l(str, NULL);
1665 #endif
1667 /******************************************************************
1668 * _atoi64_l (MSVCRT.@)
1670 __int64 CDECL _atoi64_l(const char *str, _locale_t locale)
1672 return _strtoi64_l(str, NULL, 10, locale);
1675 /******************************************************************
1676 * _atoi64 (MSVCRT.@)
1678 __int64 CDECL _atoi64(const char *str)
1680 return _strtoi64_l(str, NULL, 10, NULL);
1683 /******************************************************************
1684 * _atol_l (MSVCRT.@)
1686 __msvcrt_long CDECL _atol_l(const char *str, _locale_t locale)
1688 __int64 ret = _strtoi64_l(str, NULL, 10, locale);
1690 if(ret > LONG_MAX) {
1691 ret = LONG_MAX;
1692 *_errno() = ERANGE;
1693 } else if(ret < LONG_MIN) {
1694 ret = LONG_MIN;
1695 *_errno() = ERANGE;
1697 return ret;
1700 /******************************************************************
1701 * atol (MSVCRT.@)
1703 __msvcrt_long CDECL atol(const char *str)
1705 #if _MSVCR_VER == 0
1706 return atoi(str);
1707 #else
1708 return _atol_l(str, NULL);
1709 #endif
1712 #if _MSVCR_VER>=120
1714 /******************************************************************
1715 * _atoll_l (MSVCR120.@)
1717 __int64 CDECL _atoll_l(const char* str, _locale_t locale)
1719 return _strtoi64_l(str, NULL, 10, locale);
1722 /******************************************************************
1723 * atoll (MSVCR120.@)
1725 __int64 CDECL atoll(const char* str)
1727 return _atoll_l(str, NULL);
1730 #endif /* _MSVCR_VER>=120 */
1732 /******************************************************************
1733 * _strtol_l (MSVCRT.@)
1735 __msvcrt_long CDECL _strtol_l(const char* nptr,
1736 char** end, int base, _locale_t locale)
1738 __int64 ret = _strtoi64_l(nptr, end, base, locale);
1740 if(ret > LONG_MAX) {
1741 ret = LONG_MAX;
1742 *_errno() = ERANGE;
1743 } else if(ret < LONG_MIN) {
1744 ret = LONG_MIN;
1745 *_errno() = ERANGE;
1748 return ret;
1751 /******************************************************************
1752 * strtol (MSVCRT.@)
1754 __msvcrt_long CDECL strtol(const char* nptr, char** end, int base)
1756 return _strtol_l(nptr, end, base, NULL);
1759 /******************************************************************
1760 * _strtoul_l (MSVCRT.@)
1762 __msvcrt_ulong CDECL _strtoul_l(const char* nptr, char** end, int base, _locale_t locale)
1764 __int64 ret = _strtoi64_l(nptr, end, base, locale);
1766 if(ret > ULONG_MAX) {
1767 ret = ULONG_MAX;
1768 *_errno() = ERANGE;
1769 }else if(ret < -(__int64)ULONG_MAX) {
1770 ret = 1;
1771 *_errno() = ERANGE;
1774 return ret;
1777 /******************************************************************
1778 * strtoul (MSVCRT.@)
1780 __msvcrt_ulong CDECL strtoul(const char* nptr, char** end, int base)
1782 return _strtoul_l(nptr, end, base, NULL);
1785 /*********************************************************************
1786 * _strtoui64_l (MSVCRT.@)
1788 * FIXME: locale parameter is ignored
1790 unsigned __int64 CDECL _strtoui64_l(const char *nptr, char **endptr, int base, _locale_t locale)
1792 const char *p = nptr;
1793 BOOL negative = FALSE;
1794 BOOL got_digit = FALSE;
1795 unsigned __int64 ret = 0;
1797 TRACE("(%s %p %d %p)\n", debugstr_a(nptr), endptr, base, locale);
1799 if (!MSVCRT_CHECK_PMT(nptr != NULL)) return 0;
1800 if (!MSVCRT_CHECK_PMT(base == 0 || base >= 2)) return 0;
1801 if (!MSVCRT_CHECK_PMT(base <= 36)) return 0;
1803 while(_isspace_l((unsigned char)*nptr, locale)) nptr++;
1805 if(*nptr == '-') {
1806 negative = TRUE;
1807 nptr++;
1808 } else if(*nptr == '+')
1809 nptr++;
1811 if((base==0 || base==16) && *nptr=='0' && _tolower_l(*(nptr+1), locale)=='x') {
1812 base = 16;
1813 nptr += 2;
1816 if(base == 0) {
1817 if(*nptr=='0')
1818 base = 8;
1819 else
1820 base = 10;
1823 while(*nptr) {
1824 char cur = _tolower_l(*nptr, locale);
1825 int v;
1827 if(cur>='0' && cur<='9') {
1828 if(cur >= '0'+base)
1829 break;
1830 v = *nptr-'0';
1831 } else {
1832 if(cur<'a' || cur>='a'+base-10)
1833 break;
1834 v = cur-'a'+10;
1836 got_digit = TRUE;
1838 nptr++;
1840 if(ret>UI64_MAX/base || ret*base>UI64_MAX-v) {
1841 ret = UI64_MAX;
1842 *_errno() = ERANGE;
1843 } else
1844 ret = ret*base + v;
1847 if(endptr)
1848 *endptr = (char*)(got_digit ? nptr : p);
1850 return negative ? -ret : ret;
1853 /*********************************************************************
1854 * _strtoui64 (MSVCRT.@)
1856 unsigned __int64 CDECL _strtoui64(const char *nptr, char **endptr, int base)
1858 return _strtoui64_l(nptr, endptr, base, NULL);
1861 static int ltoa_helper(__msvcrt_long value, char *str, size_t size, int radix)
1863 __msvcrt_ulong val;
1864 unsigned int digit;
1865 BOOL is_negative;
1866 char buffer[33], *pos;
1867 size_t len;
1869 if (value < 0 && radix == 10)
1871 is_negative = TRUE;
1872 val = -value;
1874 else
1876 is_negative = FALSE;
1877 val = value;
1880 pos = buffer + 32;
1881 *pos = '\0';
1885 digit = val % radix;
1886 val /= radix;
1888 if (digit < 10)
1889 *--pos = '0' + digit;
1890 else
1891 *--pos = 'a' + digit - 10;
1893 while (val != 0);
1895 if (is_negative)
1896 *--pos = '-';
1898 len = buffer + 33 - pos;
1899 if (len > size)
1901 size_t i;
1902 char *p = str;
1904 /* Copy the temporary buffer backwards up to the available number of
1905 * characters. Don't copy the negative sign if present. */
1907 if (is_negative)
1909 p++;
1910 size--;
1913 for (pos = buffer + 31, i = 0; i < size; i++)
1914 *p++ = *pos--;
1916 str[0] = '\0';
1917 MSVCRT_INVALID_PMT("str[size] is too small", ERANGE);
1918 return ERANGE;
1921 memcpy(str, pos, len);
1922 return 0;
1925 /*********************************************************************
1926 * _ltoa_s (MSVCRT.@)
1928 int CDECL _ltoa_s(__msvcrt_long value, char *str, size_t size, int radix)
1930 if (!MSVCRT_CHECK_PMT(str != NULL)) return EINVAL;
1931 if (!MSVCRT_CHECK_PMT(size > 0)) return EINVAL;
1932 if (!MSVCRT_CHECK_PMT(radix >= 2 && radix <= 36))
1934 str[0] = '\0';
1935 return EINVAL;
1938 return ltoa_helper(value, str, size, radix);
1941 /*********************************************************************
1942 * _ltow_s (MSVCRT.@)
1944 int CDECL _ltow_s(__msvcrt_long value, wchar_t *str, size_t size, int radix)
1946 __msvcrt_ulong val;
1947 unsigned int digit;
1948 BOOL is_negative;
1949 wchar_t buffer[33], *pos;
1950 size_t len;
1952 if (!MSVCRT_CHECK_PMT(str != NULL)) return EINVAL;
1953 if (!MSVCRT_CHECK_PMT(size > 0)) return EINVAL;
1954 if (!MSVCRT_CHECK_PMT(radix >= 2 && radix <= 36))
1956 str[0] = '\0';
1957 return EINVAL;
1960 if (value < 0 && radix == 10)
1962 is_negative = TRUE;
1963 val = -value;
1965 else
1967 is_negative = FALSE;
1968 val = value;
1971 pos = buffer + 32;
1972 *pos = '\0';
1976 digit = val % radix;
1977 val /= radix;
1979 if (digit < 10)
1980 *--pos = '0' + digit;
1981 else
1982 *--pos = 'a' + digit - 10;
1984 while (val != 0);
1986 if (is_negative)
1987 *--pos = '-';
1989 len = buffer + 33 - pos;
1990 if (len > size)
1992 size_t i;
1993 wchar_t *p = str;
1995 /* Copy the temporary buffer backwards up to the available number of
1996 * characters. Don't copy the negative sign if present. */
1998 if (is_negative)
2000 p++;
2001 size--;
2004 for (pos = buffer + 31, i = 0; i < size; i++)
2005 *p++ = *pos--;
2007 str[0] = '\0';
2008 MSVCRT_INVALID_PMT("str[size] is too small", ERANGE);
2009 return ERANGE;
2012 memcpy(str, pos, len * sizeof(wchar_t));
2013 return 0;
2016 /*********************************************************************
2017 * _itoa_s (MSVCRT.@)
2019 int CDECL _itoa_s(int value, char *str, size_t size, int radix)
2021 return _ltoa_s(value, str, size, radix);
2024 /*********************************************************************
2025 * _itoa (MSVCRT.@)
2027 char* CDECL _itoa(int value, char *str, int radix)
2029 return ltoa_helper(value, str, SIZE_MAX, radix) ? NULL : str;
2032 /*********************************************************************
2033 * _itow_s (MSVCRT.@)
2035 int CDECL _itow_s(int value, wchar_t *str, size_t size, int radix)
2037 return _ltow_s(value, str, size, radix);
2040 /*********************************************************************
2041 * _ui64toa_s (MSVCRT.@)
2043 int CDECL _ui64toa_s(unsigned __int64 value, char *str,
2044 size_t size, int radix)
2046 char buffer[65], *pos;
2047 int digit;
2049 if (!MSVCRT_CHECK_PMT(str != NULL)) return EINVAL;
2050 if (!MSVCRT_CHECK_PMT(size > 0)) return EINVAL;
2051 if (!MSVCRT_CHECK_PMT(radix >= 2 && radix <= 36))
2053 str[0] = '\0';
2054 return EINVAL;
2057 pos = buffer+64;
2058 *pos = '\0';
2060 do {
2061 digit = value%radix;
2062 value /= radix;
2064 if(digit < 10)
2065 *--pos = '0'+digit;
2066 else
2067 *--pos = 'a'+digit-10;
2068 }while(value != 0);
2070 if(buffer-pos+65 > size) {
2071 MSVCRT_INVALID_PMT("str[size] is too small", EINVAL);
2072 return EINVAL;
2075 memcpy(str, pos, buffer-pos+65);
2076 return 0;
2079 /*********************************************************************
2080 * _ui64tow_s (MSVCRT.@)
2082 int CDECL _ui64tow_s( unsigned __int64 value, wchar_t *str,
2083 size_t size, int radix )
2085 wchar_t buffer[65], *pos;
2086 int digit;
2088 if (!MSVCRT_CHECK_PMT(str != NULL)) return EINVAL;
2089 if (!MSVCRT_CHECK_PMT(size > 0)) return EINVAL;
2090 if (!MSVCRT_CHECK_PMT(radix >= 2 && radix <= 36))
2092 str[0] = '\0';
2093 return EINVAL;
2096 pos = &buffer[64];
2097 *pos = '\0';
2099 do {
2100 digit = value % radix;
2101 value = value / radix;
2102 if (digit < 10)
2103 *--pos = '0' + digit;
2104 else
2105 *--pos = 'a' + digit - 10;
2106 } while (value != 0);
2108 if(buffer-pos+65 > size) {
2109 MSVCRT_INVALID_PMT("str[size] is too small", EINVAL);
2110 return EINVAL;
2113 memcpy(str, pos, (buffer-pos+65)*sizeof(wchar_t));
2114 return 0;
2117 /*********************************************************************
2118 * _ultoa_s (MSVCRT.@)
2120 int CDECL _ultoa_s(__msvcrt_ulong value, char *str, size_t size, int radix)
2122 __msvcrt_ulong digit;
2123 char buffer[33], *pos;
2124 size_t len;
2126 if (!str || !size || radix < 2 || radix > 36)
2128 if (str && size)
2129 str[0] = '\0';
2131 *_errno() = EINVAL;
2132 return EINVAL;
2135 pos = buffer + 32;
2136 *pos = '\0';
2140 digit = value % radix;
2141 value /= radix;
2143 if (digit < 10)
2144 *--pos = '0' + digit;
2145 else
2146 *--pos = 'a' + digit - 10;
2148 while (value != 0);
2150 len = buffer + 33 - pos;
2151 if (len > size)
2153 size_t i;
2154 char *p = str;
2156 /* Copy the temporary buffer backwards up to the available number of
2157 * characters. */
2159 for (pos = buffer + 31, i = 0; i < size; i++)
2160 *p++ = *pos--;
2162 str[0] = '\0';
2163 *_errno() = ERANGE;
2164 return ERANGE;
2167 memcpy(str, pos, len);
2168 return 0;
2171 /*********************************************************************
2172 * _ultow_s (MSVCRT.@)
2174 int CDECL _ultow_s(__msvcrt_ulong value, wchar_t *str, size_t size, int radix)
2176 __msvcrt_ulong digit;
2177 WCHAR buffer[33], *pos;
2178 size_t len;
2180 if (!str || !size || radix < 2 || radix > 36)
2182 if (str && size)
2183 str[0] = '\0';
2185 *_errno() = EINVAL;
2186 return EINVAL;
2189 pos = buffer + 32;
2190 *pos = '\0';
2194 digit = value % radix;
2195 value /= radix;
2197 if (digit < 10)
2198 *--pos = '0' + digit;
2199 else
2200 *--pos = 'a' + digit - 10;
2202 while (value != 0);
2204 len = buffer + 33 - pos;
2205 if (len > size)
2207 size_t i;
2208 WCHAR *p = str;
2210 /* Copy the temporary buffer backwards up to the available number of
2211 * characters. */
2213 for (pos = buffer + 31, i = 0; i < size; i++)
2214 *p++ = *pos--;
2216 str[0] = '\0';
2217 *_errno() = ERANGE;
2218 return ERANGE;
2221 memcpy(str, pos, len * sizeof(wchar_t));
2222 return 0;
2225 /*********************************************************************
2226 * _i64toa_s (MSVCRT.@)
2228 int CDECL _i64toa_s(__int64 value, char *str, size_t size, int radix)
2230 unsigned __int64 val;
2231 unsigned int digit;
2232 BOOL is_negative;
2233 char buffer[65], *pos;
2234 size_t len;
2236 if (!MSVCRT_CHECK_PMT(str != NULL)) return EINVAL;
2237 if (!MSVCRT_CHECK_PMT(size > 0)) return EINVAL;
2238 if (!MSVCRT_CHECK_PMT(radix >= 2 && radix <= 36))
2240 str[0] = '\0';
2241 return EINVAL;
2244 if (value < 0 && radix == 10)
2246 is_negative = TRUE;
2247 val = -value;
2249 else
2251 is_negative = FALSE;
2252 val = value;
2255 pos = buffer + 64;
2256 *pos = '\0';
2260 digit = val % radix;
2261 val /= radix;
2263 if (digit < 10)
2264 *--pos = '0' + digit;
2265 else
2266 *--pos = 'a' + digit - 10;
2268 while (val != 0);
2270 if (is_negative)
2271 *--pos = '-';
2273 len = buffer + 65 - pos;
2274 if (len > size)
2276 size_t i;
2277 char *p = str;
2279 /* Copy the temporary buffer backwards up to the available number of
2280 * characters. Don't copy the negative sign if present. */
2282 if (is_negative)
2284 p++;
2285 size--;
2288 for (pos = buffer + 63, i = 0; i < size; i++)
2289 *p++ = *pos--;
2291 str[0] = '\0';
2292 MSVCRT_INVALID_PMT("str[size] is too small", ERANGE);
2293 return ERANGE;
2296 memcpy(str, pos, len);
2297 return 0;
2300 /*********************************************************************
2301 * _i64tow_s (MSVCRT.@)
2303 int CDECL _i64tow_s(__int64 value, wchar_t *str, size_t size, int radix)
2305 unsigned __int64 val;
2306 unsigned int digit;
2307 BOOL is_negative;
2308 wchar_t buffer[65], *pos;
2309 size_t len;
2311 if (!MSVCRT_CHECK_PMT(str != NULL)) return EINVAL;
2312 if (!MSVCRT_CHECK_PMT(size > 0)) return EINVAL;
2313 if (!MSVCRT_CHECK_PMT(radix >= 2 && radix <= 36))
2315 str[0] = '\0';
2316 return EINVAL;
2319 if (value < 0 && radix == 10)
2321 is_negative = TRUE;
2322 val = -value;
2324 else
2326 is_negative = FALSE;
2327 val = value;
2330 pos = buffer + 64;
2331 *pos = '\0';
2335 digit = val % radix;
2336 val /= radix;
2338 if (digit < 10)
2339 *--pos = '0' + digit;
2340 else
2341 *--pos = 'a' + digit - 10;
2343 while (val != 0);
2345 if (is_negative)
2346 *--pos = '-';
2348 len = buffer + 65 - pos;
2349 if (len > size)
2351 size_t i;
2352 wchar_t *p = str;
2354 /* Copy the temporary buffer backwards up to the available number of
2355 * characters. Don't copy the negative sign if present. */
2357 if (is_negative)
2359 p++;
2360 size--;
2363 for (pos = buffer + 63, i = 0; i < size; i++)
2364 *p++ = *pos--;
2366 str[0] = '\0';
2367 MSVCRT_INVALID_PMT("str[size] is too small", ERANGE);
2368 return ERANGE;
2371 memcpy(str, pos, len * sizeof(wchar_t));
2372 return 0;
2375 #define I10_OUTPUT_MAX_PREC 21
2376 /* Internal structure used by $I10_OUTPUT */
2377 struct _I10_OUTPUT_DATA {
2378 short pos;
2379 char sign;
2380 BYTE len;
2381 char str[I10_OUTPUT_MAX_PREC+1]; /* add space for '\0' */
2384 /*********************************************************************
2385 * $I10_OUTPUT (MSVCRT.@)
2386 * ld80 - long double (Intel 80 bit FP in 12 bytes) to be printed to data
2387 * prec - precision of part, we're interested in
2388 * flag - 0 for first prec digits, 1 for fractional part
2389 * data - data to be populated
2391 * return value
2392 * 0 if given double is NaN or INF
2393 * 1 otherwise
2395 * FIXME
2396 * Native sets last byte of data->str to '0' or '9', I don't know what
2397 * it means. Current implementation sets it always to '0'.
2399 int CDECL I10_OUTPUT(MSVCRT__LDOUBLE ld80, int prec, int flag, struct _I10_OUTPUT_DATA *data)
2401 struct fpnum num;
2402 double d;
2403 char format[8];
2404 char buf[I10_OUTPUT_MAX_PREC+9]; /* 9 = strlen("0.e+0000") + '\0' */
2405 char *p;
2407 if ((ld80.x80[2] & 0x7fff) == 0x7fff)
2409 if (ld80.x80[0] == 0 && ld80.x80[1] == 0x80000000)
2410 strcpy( data->str, "1#INF" );
2411 else
2412 strcpy( data->str, (ld80.x80[1] & 0x40000000) ? "1#QNAN" : "1#SNAN" );
2413 data->pos = 1;
2414 data->sign = (ld80.x80[2] & 0x8000) ? '-' : ' ';
2415 data->len = strlen(data->str);
2416 return 0;
2419 num.sign = (ld80.x80[2] & 0x8000) ? -1 : 1;
2420 num.exp = (ld80.x80[2] & 0x7fff) - 0x3fff - 63;
2421 num.m = ld80.x80[0] | ((ULONGLONG)ld80.x80[1] << 32);
2422 num.mod = FP_ROUND_EVEN;
2423 fpnum_double( &num, &d );
2424 TRACE("(%lf %d %x %p)\n", d, prec, flag, data);
2426 if(d<0) {
2427 data->sign = '-';
2428 d = -d;
2429 } else
2430 data->sign = ' ';
2432 if(flag&1) {
2433 int exp = 1 + floor(log10(d));
2435 prec += exp;
2436 if(exp < 0)
2437 prec--;
2439 prec--;
2441 if(prec+1 > I10_OUTPUT_MAX_PREC)
2442 prec = I10_OUTPUT_MAX_PREC-1;
2443 else if(prec < 0) {
2444 d = 0.0;
2445 prec = 0;
2448 sprintf(format, "%%.%dle", prec);
2449 sprintf(buf, format, d);
2451 buf[1] = buf[0];
2452 data->pos = atoi(buf+prec+3);
2453 if(buf[1] != '0')
2454 data->pos++;
2456 for(p = buf+prec+1; p>buf+1 && *p=='0'; p--);
2457 data->len = p-buf;
2459 memcpy(data->str, buf+1, data->len);
2460 data->str[data->len] = '\0';
2462 if(buf[1]!='0' && prec-data->len+1>0)
2463 memcpy(data->str+data->len+1, buf+data->len+1, prec-data->len+1);
2465 return 1;
2467 #undef I10_OUTPUT_MAX_PREC
2469 /*********************************************************************
2470 * memcmp (MSVCRT.@)
2472 int __cdecl memcmp(const void *ptr1, const void *ptr2, size_t n)
2474 const unsigned char *p1, *p2;
2476 for (p1 = ptr1, p2 = ptr2; n; n--, p1++, p2++)
2478 if (*p1 < *p2) return -1;
2479 if (*p1 > *p2) return 1;
2481 return 0;
2484 #if defined(__i386__) || defined(__x86_64__)
2486 #ifdef __i386__
2488 #define DEST_REG "%edi"
2489 #define SRC_REG "%esi"
2490 #define LEN_REG "%ecx"
2491 #define TMP_REG "%edx"
2493 #define MEMMOVE_INIT \
2494 "pushl " SRC_REG "\n\t" \
2495 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
2496 "pushl " DEST_REG "\n\t" \
2497 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
2498 "movl 12(%esp), " DEST_REG "\n\t" \
2499 "movl 16(%esp), " SRC_REG "\n\t" \
2500 "movl 20(%esp), " LEN_REG "\n\t"
2502 #define MEMMOVE_CLEANUP \
2503 "movl 12(%esp), %eax\n\t" \
2504 "popl " DEST_REG "\n\t" \
2505 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t") \
2506 "popl " SRC_REG "\n\t" \
2507 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
2509 #else
2511 #define DEST_REG "%rdi"
2512 #define SRC_REG "%rsi"
2513 #define LEN_REG "%r8"
2514 #define TMP_REG "%r9"
2516 #define MEMMOVE_INIT \
2517 "pushq " SRC_REG "\n\t" \
2518 __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t") \
2519 "pushq " DEST_REG "\n\t" \
2520 __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t") \
2521 "movq %rcx, " DEST_REG "\n\t" \
2522 "movq %rdx, " SRC_REG "\n\t"
2524 #define MEMMOVE_CLEANUP \
2525 "movq %rcx, %rax\n\t" \
2526 "popq " DEST_REG "\n\t" \
2527 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t") \
2528 "popq " SRC_REG "\n\t" \
2529 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
2530 #endif
2532 void * __cdecl sse2_memmove(void *dst, const void *src, size_t n);
2533 __ASM_GLOBAL_FUNC( sse2_memmove,
2534 MEMMOVE_INIT
2535 "mov " DEST_REG ", " TMP_REG "\n\t" /* check copying direction */
2536 "sub " SRC_REG ", " TMP_REG "\n\t"
2537 "cmp " LEN_REG ", " TMP_REG "\n\t"
2538 "jb copy_bwd\n\t"
2539 /* copy forwards */
2540 "cmp $4, " LEN_REG "\n\t" /* 4-bytes align */
2541 "jb copy_fwd3\n\t"
2542 "mov " DEST_REG ", " TMP_REG "\n\t"
2543 "shr $1, " TMP_REG "\n\t"
2544 "jnc 1f\n\t"
2545 "movsb\n\t"
2546 "dec " LEN_REG "\n\t"
2547 "inc " TMP_REG "\n\t"
2548 "1:\n\t"
2549 "shr $1, " TMP_REG "\n\t"
2550 "jnc 1f\n\t"
2551 "movsw\n\t"
2552 "sub $2, " LEN_REG "\n\t"
2553 "inc " TMP_REG "\n\t"
2554 "1:\n\t" /* 16-bytes align */
2555 "cmp $16, " LEN_REG "\n\t"
2556 "jb copy_fwd15\n\t"
2557 "shr $1, " TMP_REG "\n\t"
2558 "jnc 1f\n\t"
2559 "movsl\n\t"
2560 "sub $4, " LEN_REG "\n\t"
2561 "inc " TMP_REG "\n\t"
2562 "1:\n\t"
2563 "shr $1, " TMP_REG "\n\t"
2564 "jnc 1f\n\t"
2565 "movsl\n\t"
2566 "movsl\n\t"
2567 "sub $8, " LEN_REG "\n\t"
2568 "1:\n\t"
2569 "cmp $64, " LEN_REG "\n\t"
2570 "jb copy_fwd63\n\t"
2571 "1:\n\t" /* copy 64-bytes blocks in loop, dest 16-bytes aligned */
2572 "movdqu 0x00(" SRC_REG "), %xmm0\n\t"
2573 "movdqu 0x10(" SRC_REG "), %xmm1\n\t"
2574 "movdqu 0x20(" SRC_REG "), %xmm2\n\t"
2575 "movdqu 0x30(" SRC_REG "), %xmm3\n\t"
2576 "movdqa %xmm0, 0x00(" DEST_REG ")\n\t"
2577 "movdqa %xmm1, 0x10(" DEST_REG ")\n\t"
2578 "movdqa %xmm2, 0x20(" DEST_REG ")\n\t"
2579 "movdqa %xmm3, 0x30(" DEST_REG ")\n\t"
2580 "add $64, " SRC_REG "\n\t"
2581 "add $64, " DEST_REG "\n\t"
2582 "sub $64, " LEN_REG "\n\t"
2583 "cmp $64, " LEN_REG "\n\t"
2584 "jae 1b\n\t"
2585 "copy_fwd63:\n\t" /* copy last 63 bytes, dest 16-bytes aligned */
2586 "mov " LEN_REG ", " TMP_REG "\n\t"
2587 "and $15, " LEN_REG "\n\t"
2588 "shr $5, " TMP_REG "\n\t"
2589 "jnc 1f\n\t"
2590 "movdqu 0(" SRC_REG "), %xmm0\n\t"
2591 "movdqa %xmm0, 0(" DEST_REG ")\n\t"
2592 "add $16, " SRC_REG "\n\t"
2593 "add $16, " DEST_REG "\n\t"
2594 "1:\n\t"
2595 "shr $1, " TMP_REG "\n\t"
2596 "jnc copy_fwd15\n\t"
2597 "movdqu 0x00(" SRC_REG "), %xmm0\n\t"
2598 "movdqu 0x10(" SRC_REG "), %xmm1\n\t"
2599 "movdqa %xmm0, 0x00(" DEST_REG ")\n\t"
2600 "movdqa %xmm1, 0x10(" DEST_REG ")\n\t"
2601 "add $32, " SRC_REG "\n\t"
2602 "add $32, " DEST_REG "\n\t"
2603 "copy_fwd15:\n\t" /* copy last 15 bytes, dest 4-bytes aligned */
2604 "mov " LEN_REG ", " TMP_REG "\n\t"
2605 "and $3, " LEN_REG "\n\t"
2606 "shr $3, " TMP_REG "\n\t"
2607 "jnc 1f\n\t"
2608 "movsl\n\t"
2609 "1:\n\t"
2610 "shr $1, " TMP_REG "\n\t"
2611 "jnc copy_fwd3\n\t"
2612 "movsl\n\t"
2613 "movsl\n\t"
2614 "copy_fwd3:\n\t" /* copy last 3 bytes */
2615 "shr $1, " LEN_REG "\n\t"
2616 "jnc 1f\n\t"
2617 "movsb\n\t"
2618 "1:\n\t"
2619 "shr $1, " LEN_REG "\n\t"
2620 "jnc 1f\n\t"
2621 "movsw\n\t"
2622 "1:\n\t"
2623 MEMMOVE_CLEANUP
2624 "ret\n\t"
2625 "copy_bwd:\n\t"
2626 "lea (" DEST_REG ", " LEN_REG "), " DEST_REG "\n\t"
2627 "lea (" SRC_REG ", " LEN_REG "), " SRC_REG "\n\t"
2628 "cmp $4, " LEN_REG "\n\t" /* 4-bytes align */
2629 "jb copy_bwd3\n\t"
2630 "mov " DEST_REG ", " TMP_REG "\n\t"
2631 "shr $1, " TMP_REG "\n\t"
2632 "jnc 1f\n\t"
2633 "dec " SRC_REG "\n\t"
2634 "dec " DEST_REG "\n\t"
2635 "movb (" SRC_REG "), %al\n\t"
2636 "movb %al, (" DEST_REG ")\n\t"
2637 "dec " LEN_REG "\n\t"
2638 "1:\n\t"
2639 "shr $1, " TMP_REG "\n\t"
2640 "jnc 1f\n\t"
2641 "sub $2, " SRC_REG "\n\t"
2642 "sub $2, " DEST_REG "\n\t"
2643 "movw (" SRC_REG "), %ax\n\t"
2644 "movw %ax, (" DEST_REG ")\n\t"
2645 "sub $2, " LEN_REG "\n\t"
2646 "1:\n\t" /* 16-bytes align */
2647 "cmp $16, " LEN_REG "\n\t"
2648 "jb copy_bwd15\n\t"
2649 "shr $1, " TMP_REG "\n\t"
2650 "jnc 1f\n\t"
2651 "sub $4, " SRC_REG "\n\t"
2652 "sub $4, " DEST_REG "\n\t"
2653 "movl (" SRC_REG "), %eax\n\t"
2654 "movl %eax, (" DEST_REG ")\n\t"
2655 "sub $4, " LEN_REG "\n\t"
2656 "1:\n\t"
2657 "shr $1, " TMP_REG "\n\t"
2658 "jnc 1f\n\t"
2659 "sub $8, " SRC_REG "\n\t"
2660 "sub $8, " DEST_REG "\n\t"
2661 "movl 4(" SRC_REG "), %eax\n\t"
2662 "movl %eax, 4(" DEST_REG ")\n\t"
2663 "movl (" SRC_REG "), %eax\n\t"
2664 "movl %eax, (" DEST_REG ")\n\t"
2665 "sub $8, " LEN_REG "\n\t"
2666 "1:\n\t"
2667 "cmp $64, " LEN_REG "\n\t"
2668 "jb copy_bwd63\n\t"
2669 "1:\n\t" /* copy 64-bytes blocks in loop, dest 16-bytes aligned */
2670 "sub $64, " SRC_REG "\n\t"
2671 "sub $64, " DEST_REG "\n\t"
2672 "movdqu 0x00(" SRC_REG "), %xmm0\n\t"
2673 "movdqu 0x10(" SRC_REG "), %xmm1\n\t"
2674 "movdqu 0x20(" SRC_REG "), %xmm2\n\t"
2675 "movdqu 0x30(" SRC_REG "), %xmm3\n\t"
2676 "movdqa %xmm0, 0x00(" DEST_REG ")\n\t"
2677 "movdqa %xmm1, 0x10(" DEST_REG ")\n\t"
2678 "movdqa %xmm2, 0x20(" DEST_REG ")\n\t"
2679 "movdqa %xmm3, 0x30(" DEST_REG ")\n\t"
2680 "sub $64, " LEN_REG "\n\t"
2681 "cmp $64, " LEN_REG "\n\t"
2682 "jae 1b\n\t"
2683 "copy_bwd63:\n\t" /* copy last 63 bytes, dest 16-bytes aligned */
2684 "mov " LEN_REG ", " TMP_REG "\n\t"
2685 "and $15, " LEN_REG "\n\t"
2686 "shr $5, " TMP_REG "\n\t"
2687 "jnc 1f\n\t"
2688 "sub $16, " SRC_REG "\n\t"
2689 "sub $16, " DEST_REG "\n\t"
2690 "movdqu (" SRC_REG "), %xmm0\n\t"
2691 "movdqa %xmm0, (" DEST_REG ")\n\t"
2692 "1:\n\t"
2693 "shr $1, " TMP_REG "\n\t"
2694 "jnc copy_bwd15\n\t"
2695 "sub $32, " SRC_REG "\n\t"
2696 "sub $32, " DEST_REG "\n\t"
2697 "movdqu 0x00(" SRC_REG "), %xmm0\n\t"
2698 "movdqu 0x10(" SRC_REG "), %xmm1\n\t"
2699 "movdqa %xmm0, 0x00(" DEST_REG ")\n\t"
2700 "movdqa %xmm1, 0x10(" DEST_REG ")\n\t"
2701 "copy_bwd15:\n\t" /* copy last 15 bytes, dest 4-bytes aligned */
2702 "mov " LEN_REG ", " TMP_REG "\n\t"
2703 "and $3, " LEN_REG "\n\t"
2704 "shr $3, " TMP_REG "\n\t"
2705 "jnc 1f\n\t"
2706 "sub $4, " SRC_REG "\n\t"
2707 "sub $4, " DEST_REG "\n\t"
2708 "movl (" SRC_REG "), %eax\n\t"
2709 "movl %eax, (" DEST_REG ")\n\t"
2710 "1:\n\t"
2711 "shr $1, " TMP_REG "\n\t"
2712 "jnc copy_bwd3\n\t"
2713 "sub $8, " SRC_REG "\n\t"
2714 "sub $8, " DEST_REG "\n\t"
2715 "movl 4(" SRC_REG "), %eax\n\t"
2716 "movl %eax, 4(" DEST_REG ")\n\t"
2717 "movl (" SRC_REG "), %eax\n\t"
2718 "movl %eax, (" DEST_REG ")\n\t"
2719 "copy_bwd3:\n\t" /* copy last 3 bytes */
2720 "shr $1, " LEN_REG "\n\t"
2721 "jnc 1f\n\t"
2722 "dec " SRC_REG "\n\t"
2723 "dec " DEST_REG "\n\t"
2724 "movb (" SRC_REG "), %al\n\t"
2725 "movb %al, (" DEST_REG ")\n\t"
2726 "1:\n\t"
2727 "shr $1, " LEN_REG "\n\t"
2728 "jnc 1f\n\t"
2729 "movw -2(" SRC_REG "), %ax\n\t"
2730 "movw %ax, -2(" DEST_REG ")\n\t"
2731 "1:\n\t"
2732 MEMMOVE_CLEANUP
2733 "ret" )
2735 #endif
2737 /*********************************************************************
2738 * memmove (MSVCRT.@)
2740 #ifdef WORDS_BIGENDIAN
2741 # define MERGE(w1, sh1, w2, sh2) ((w1 << sh1) | (w2 >> sh2))
2742 #else
2743 # define MERGE(w1, sh1, w2, sh2) ((w1 >> sh1) | (w2 << sh2))
2744 #endif
2745 void * __cdecl memmove(void *dst, const void *src, size_t n)
2747 #ifdef __x86_64__
2748 return sse2_memmove(dst, src, n);
2749 #else
2750 unsigned char *d = dst;
2751 const unsigned char *s = src;
2752 int sh1;
2754 #ifdef __i386__
2755 if (sse2_supported)
2756 return sse2_memmove(dst, src, n);
2757 #endif
2759 if (!n) return dst;
2761 if ((size_t)dst - (size_t)src >= n)
2763 for (; (size_t)d % sizeof(size_t) && n; n--) *d++ = *s++;
2765 sh1 = 8 * ((size_t)s % sizeof(size_t));
2766 if (!sh1)
2768 while (n >= sizeof(size_t))
2770 *(size_t*)d = *(size_t*)s;
2771 s += sizeof(size_t);
2772 d += sizeof(size_t);
2773 n -= sizeof(size_t);
2776 else if (n >= 2 * sizeof(size_t))
2778 int sh2 = 8 * sizeof(size_t) - sh1;
2779 size_t x, y;
2781 s -= sh1 / 8;
2782 x = *(size_t*)s;
2785 s += sizeof(size_t);
2786 y = *(size_t*)s;
2787 *(size_t*)d = MERGE(x, sh1, y, sh2);
2788 d += sizeof(size_t);
2790 s += sizeof(size_t);
2791 x = *(size_t*)s;
2792 *(size_t*)d = MERGE(y, sh1, x, sh2);
2793 d += sizeof(size_t);
2795 n -= 2 * sizeof(size_t);
2796 } while (n >= 2 * sizeof(size_t));
2797 s += sh1 / 8;
2799 while (n--) *d++ = *s++;
2800 return dst;
2802 else
2804 d += n;
2805 s += n;
2807 for (; (size_t)d % sizeof(size_t) && n; n--) *--d = *--s;
2809 sh1 = 8 * ((size_t)s % sizeof(size_t));
2810 if (!sh1)
2812 while (n >= sizeof(size_t))
2814 s -= sizeof(size_t);
2815 d -= sizeof(size_t);
2816 *(size_t*)d = *(size_t*)s;
2817 n -= sizeof(size_t);
2820 else if (n >= 2 * sizeof(size_t))
2822 int sh2 = 8 * sizeof(size_t) - sh1;
2823 size_t x, y;
2825 s -= sh1 / 8;
2826 x = *(size_t*)s;
2829 s -= sizeof(size_t);
2830 y = *(size_t*)s;
2831 d -= sizeof(size_t);
2832 *(size_t*)d = MERGE(y, sh1, x, sh2);
2834 s -= sizeof(size_t);
2835 x = *(size_t*)s;
2836 d -= sizeof(size_t);
2837 *(size_t*)d = MERGE(x, sh1, y, sh2);
2839 n -= 2 * sizeof(size_t);
2840 } while (n >= 2 * sizeof(size_t));
2841 s += sh1 / 8;
2843 while (n--) *--d = *--s;
2845 return dst;
2846 #endif
2848 #undef MERGE
2850 /*********************************************************************
2851 * memcpy (MSVCRT.@)
2853 void * __cdecl memcpy(void *dst, const void *src, size_t n)
2855 return memmove(dst, src, n);
2858 /*********************************************************************
2859 * memset (MSVCRT.@)
2861 void* __cdecl memset(void *dst, int c, size_t n)
2863 volatile unsigned char *d = dst; /* avoid gcc optimizations */
2864 while (n--) *d++ = c;
2865 return dst;
2868 /*********************************************************************
2869 * strchr (MSVCRT.@)
2871 char* __cdecl strchr(const char *str, int c)
2875 if (*str == (char)c) return (char*)str;
2876 } while (*str++);
2877 return NULL;
2880 /*********************************************************************
2881 * strrchr (MSVCRT.@)
2883 char* __cdecl strrchr(const char *str, int c)
2885 char *ret = NULL;
2886 do { if (*str == (char)c) ret = (char*)str; } while (*str++);
2887 return ret;
2890 /*********************************************************************
2891 * memchr (MSVCRT.@)
2893 void* __cdecl memchr(const void *ptr, int c, size_t n)
2895 const unsigned char *p = ptr;
2897 for (p = ptr; n; n--, p++) if (*p == (unsigned char)c) return (void *)(ULONG_PTR)p;
2898 return NULL;
2901 /*********************************************************************
2902 * strcmp (MSVCRT.@)
2904 int __cdecl strcmp(const char *str1, const char *str2)
2906 while (*str1 && *str1 == *str2) { str1++; str2++; }
2907 if ((unsigned char)*str1 > (unsigned char)*str2) return 1;
2908 if ((unsigned char)*str1 < (unsigned char)*str2) return -1;
2909 return 0;
2912 /*********************************************************************
2913 * strncmp (MSVCRT.@)
2915 int __cdecl strncmp(const char *str1, const char *str2, size_t len)
2917 if (!len) return 0;
2918 while (--len && *str1 && *str1 == *str2) { str1++; str2++; }
2919 return (unsigned char)*str1 - (unsigned char)*str2;
2922 /*********************************************************************
2923 * _strnicmp_l (MSVCRT.@)
2925 int __cdecl _strnicmp_l(const char *s1, const char *s2,
2926 size_t count, _locale_t locale)
2928 pthreadlocinfo locinfo;
2929 int c1, c2;
2931 if(s1==NULL || s2==NULL)
2932 return _NLSCMPERROR;
2934 if(!count)
2935 return 0;
2937 if(!locale)
2938 locinfo = get_locinfo();
2939 else
2940 locinfo = locale->locinfo;
2942 if(!locinfo->lc_handle[LC_CTYPE])
2944 do {
2945 if ((c1 = *s1++) >= 'A' && c1 <= 'Z')
2946 c1 -= 'A' - 'a';
2947 if ((c2 = *s2++) >= 'A' && c2 <= 'Z')
2948 c2 -= 'A' - 'a';
2949 }while(--count && c1 && c1==c2);
2951 return c1-c2;
2954 do {
2955 c1 = _tolower_l((unsigned char)*s1++, locale);
2956 c2 = _tolower_l((unsigned char)*s2++, locale);
2957 }while(--count && c1 && c1==c2);
2959 return c1-c2;
2962 /*********************************************************************
2963 * _stricmp_l (MSVCRT.@)
2965 int __cdecl _stricmp_l(const char *s1, const char *s2, _locale_t locale)
2967 return _strnicmp_l(s1, s2, -1, locale);
2970 /*********************************************************************
2971 * _strnicmp (MSVCRT.@)
2973 int __cdecl _strnicmp(const char *s1, const char *s2, size_t count)
2975 return _strnicmp_l(s1, s2, count, NULL);
2978 /*********************************************************************
2979 * _stricmp (MSVCRT.@)
2981 int __cdecl _stricmp(const char *s1, const char *s2)
2983 return _strnicmp_l(s1, s2, -1, NULL);
2986 /*********************************************************************
2987 * strstr (MSVCRT.@)
2989 char* __cdecl strstr(const char *haystack, const char *needle)
2991 size_t i, j, len, needle_len, lps_len;
2992 BYTE lps[256];
2994 needle_len = strlen(needle);
2995 if (!needle_len) return (char*)haystack;
2996 lps_len = needle_len > ARRAY_SIZE(lps) ? ARRAY_SIZE(lps) : needle_len;
2998 lps[0] = 0;
2999 len = 0;
3000 i = 1;
3001 while (i < lps_len)
3003 if (needle[i] == needle[len]) lps[i++] = ++len;
3004 else if (len) len = lps[len-1];
3005 else lps[i++] = 0;
3008 i = j = 0;
3009 while (haystack[i])
3011 while (j < lps_len && haystack[i] && haystack[i] == needle[j])
3013 i++;
3014 j++;
3017 if (j == needle_len) return (char*)haystack + i - j;
3018 else if (j)
3020 if (j == ARRAY_SIZE(lps) && !strncmp(haystack + i, needle + j, needle_len - j))
3021 return (char*)haystack + i - j;
3022 j = lps[j-1];
3024 else if (haystack[i]) i++;
3026 return NULL;
3029 /*********************************************************************
3030 * _memicmp_l (MSVCRT.@)
3032 int __cdecl _memicmp_l(const void *v1, const void *v2, size_t len, _locale_t locale)
3034 const char *s1 = v1, *s2 = v2;
3035 int ret = 0;
3037 #if _MSVCR_VER == 0 || _MSVCR_VER >= 80
3038 if (!s1 || !s2)
3040 if (len)
3041 MSVCRT_INVALID_PMT(NULL, EINVAL);
3042 return len ? _NLSCMPERROR : 0;
3044 #endif
3046 while (len--)
3048 if ((ret = _tolower_l(*s1, locale) - _tolower_l(*s2, locale)))
3049 break;
3050 s1++;
3051 s2++;
3053 return ret;
3056 /*********************************************************************
3057 * _memicmp (MSVCRT.@)
3059 int __cdecl _memicmp(const void *s1, const void *s2, size_t len)
3061 return _memicmp_l(s1, s2, len, NULL);
3064 /*********************************************************************
3065 * strcspn (MSVCRT.@)
3067 size_t __cdecl strcspn(const char *str, const char *reject)
3069 BOOL rejects[256];
3070 const char *p;
3072 memset(rejects, 0, sizeof(rejects));
3074 p = reject;
3075 while(*p)
3077 rejects[(unsigned char)*p] = TRUE;
3078 p++;
3081 p = str;
3082 while(*p && !rejects[(unsigned char)*p]) p++;
3083 return p - str;
3086 /*********************************************************************
3087 * strpbrk (MSVCRT.@)
3089 char* __cdecl strpbrk(const char *str, const char *accept)
3091 for (; *str; str++) if (strchr( accept, *str )) return (char*)str;
3092 return NULL;
3095 /*********************************************************************
3096 * __strncnt (MSVCRT.@)
3098 size_t __cdecl __strncnt(const char *str, size_t size)
3100 size_t ret = 0;
3102 #if _MSVCR_VER >= 140
3103 while (*str++ && size--)
3104 #else
3105 while (size-- && *str++)
3106 #endif
3108 ret++;
3111 return ret;
3115 #ifdef _CRTDLL
3116 /*********************************************************************
3117 * _strdec (CRTDLL.@)
3119 char * CDECL _strdec(const char *str1, const char *str2)
3121 return (char *)(str2 - 1);
3124 /*********************************************************************
3125 * _strinc (CRTDLL.@)
3127 char * CDECL _strinc(const char *str)
3129 return (char *)(str + 1);
3132 /*********************************************************************
3133 * _strnextc (CRTDLL.@)
3135 unsigned int CDECL _strnextc(const char *str)
3137 return (unsigned char)str[0];
3140 /*********************************************************************
3141 * _strninc (CRTDLL.@)
3143 char * CDECL _strninc(const char *str, size_t len)
3145 return (char *)(str + len);
3148 /*********************************************************************
3149 * _strspnp (CRTDLL.@)
3151 char * CDECL _strspnp( const char *str1, const char *str2)
3153 str1 += strspn( str1, str2 );
3154 return *str1 ? (char*)str1 : NULL;
3156 #endif