winecoreaudio: Remove GetAudioSessionWrapper.
[wine.git] / dlls / msvcrt / string.c
blobd58a21406b1788eb38c5568bfb125577f877a40b
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 %#I64x *2^%d (round %d)\n", fp->sign == -1 ? '-' : '+',
381 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 %#I64x\n", 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 %#I64x *2^%d (round %d)\n", fp->sign == -1 ? '-' : '+',
500 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 * strncpy_s (MSVCRT.@)
1274 int __cdecl strncpy_s( char *dst, size_t elem, const char *src, size_t count )
1276 char *p = dst;
1277 BOOL truncate = (count == _TRUNCATE);
1279 TRACE("(%p %Iu %s %Iu)\n", dst, elem, debugstr_a(src), count);
1281 if (!count)
1283 if (dst && elem) *dst = 0;
1284 return 0;
1287 if (!MSVCRT_CHECK_PMT(dst != NULL)) return EINVAL;
1288 if (!MSVCRT_CHECK_PMT(elem != 0)) return EINVAL;
1289 if (!MSVCRT_CHECK_PMT(src != NULL))
1291 *dst = 0;
1292 return EINVAL;
1295 while (elem && count && *src)
1297 *p++ = *src++;
1298 elem--;
1299 count--;
1301 if (!elem && truncate)
1303 *(p-1) = 0;
1304 return STRUNCATE;
1306 else if (!elem)
1308 *dst = 0;
1309 return ERANGE;
1311 *p = 0;
1312 return 0;
1315 /*********************************************************************
1316 * strcpy (MSVCRT.@)
1318 char* CDECL strcpy(char *dst, const char *src)
1320 char *ret = dst;
1321 while ((*dst++ = *src++));
1322 return ret;
1325 /*********************************************************************
1326 * strcpy_s (MSVCRT.@)
1328 int CDECL strcpy_s( char* dst, size_t elem, const char* src )
1330 size_t i;
1331 if (!MSVCRT_CHECK_PMT(dst != 0)) return EINVAL;
1332 if (!MSVCRT_CHECK_PMT(elem != 0)) return EINVAL;
1333 if (!MSVCRT_CHECK_PMT(src != NULL))
1335 dst[0] = '\0';
1336 return EINVAL;
1339 for(i = 0; i < elem; i++)
1341 if((dst[i] = src[i]) == '\0') return 0;
1343 MSVCRT_INVALID_PMT("dst[elem] is too small", ERANGE);
1344 dst[0] = '\0';
1345 return ERANGE;
1348 /*********************************************************************
1349 * strcat_s (MSVCRT.@)
1351 int CDECL strcat_s( char* dst, size_t elem, const char* src )
1353 size_t i, j;
1354 if (!MSVCRT_CHECK_PMT(dst != 0)) return EINVAL;
1355 if (!MSVCRT_CHECK_PMT(elem != 0)) return EINVAL;
1356 if (!MSVCRT_CHECK_PMT(src != NULL))
1358 dst[0] = '\0';
1359 return EINVAL;
1362 for(i = 0; i < elem; i++)
1364 if(dst[i] == '\0')
1366 for(j = 0; (j + i) < elem; j++)
1368 if((dst[j + i] = src[j]) == '\0') return 0;
1372 /* Set the first element to 0, not the first element after the skipped part */
1373 MSVCRT_INVALID_PMT("dst[elem] is too small", ERANGE);
1374 dst[0] = '\0';
1375 return ERANGE;
1378 /*********************************************************************
1379 * strcat (MSVCRT.@)
1381 char* __cdecl strcat( char *dst, const char *src )
1383 char *d = dst;
1384 while (*d) d++;
1385 while ((*d++ = *src++));
1386 return dst;
1389 /*********************************************************************
1390 * strncat_s (MSVCRT.@)
1392 int CDECL strncat_s( char* dst, size_t elem, const char* src, size_t count )
1394 size_t i, j;
1396 if (!MSVCRT_CHECK_PMT(dst != 0)) return EINVAL;
1397 if (!MSVCRT_CHECK_PMT(elem != 0)) return EINVAL;
1398 if (count == 0) return 0;
1400 if (!MSVCRT_CHECK_PMT(src != NULL))
1402 *dst = 0;
1403 return EINVAL;
1406 for (i = 0; i < elem; i++) if (!dst[i]) break;
1408 if (i == elem)
1410 MSVCRT_INVALID_PMT("dst[elem] is not NULL terminated\n", EINVAL);
1411 *dst = 0;
1412 return EINVAL;
1415 for (j = 0; (j + i) < elem; j++)
1417 if(count == _TRUNCATE && j + i == elem - 1)
1419 dst[j + i] = '\0';
1420 return STRUNCATE;
1422 if(j == count || (dst[j + i] = src[j]) == '\0')
1424 dst[j + i] = '\0';
1425 return 0;
1429 MSVCRT_INVALID_PMT("dst[elem] is too small", ERANGE);
1430 dst[0] = '\0';
1431 return ERANGE;
1434 /*********************************************************************
1435 * strncat (MSVCRT.@)
1437 char* __cdecl strncat(char *dst, const char *src, size_t len)
1439 char *d = dst;
1440 while (*d) d++;
1441 for ( ; len && *src; d++, src++, len--) *d = *src;
1442 *d = 0;
1443 return dst;
1446 /*********************************************************************
1447 * _strxfrm_l (MSVCRT.@)
1449 size_t CDECL _strxfrm_l( char *dest, const char *src,
1450 size_t len, _locale_t locale )
1452 pthreadlocinfo locinfo;
1453 int ret;
1455 if(!MSVCRT_CHECK_PMT(src)) return INT_MAX;
1456 if(!MSVCRT_CHECK_PMT(dest || !len)) return INT_MAX;
1458 if(len > INT_MAX) {
1459 FIXME("len > INT_MAX not supported\n");
1460 len = INT_MAX;
1463 if(!locale)
1464 locinfo = get_locinfo();
1465 else
1466 locinfo = locale->locinfo;
1468 if(!locinfo->lc_handle[LC_COLLATE]) {
1469 strncpy(dest, src, len);
1470 return strlen(src);
1473 ret = LCMapStringA(locinfo->lc_handle[LC_COLLATE],
1474 LCMAP_SORTKEY, src, -1, NULL, 0);
1475 if(!ret) {
1476 if(len) dest[0] = 0;
1477 *_errno() = EILSEQ;
1478 return INT_MAX;
1480 if(!len) return ret-1;
1482 if(ret > len) {
1483 dest[0] = 0;
1484 *_errno() = ERANGE;
1485 return ret-1;
1488 return LCMapStringA(locinfo->lc_handle[LC_COLLATE],
1489 LCMAP_SORTKEY, src, -1, dest, len) - 1;
1492 /*********************************************************************
1493 * strxfrm (MSVCRT.@)
1495 size_t CDECL strxfrm( char *dest, const char *src, size_t len )
1497 return _strxfrm_l(dest, src, len, NULL);
1500 /********************************************************************
1501 * __STRINGTOLD_L (MSVCR80.@)
1503 int CDECL __STRINGTOLD_L( MSVCRT__LDOUBLE *value, char **endptr,
1504 const char *str, int flags, _locale_t locale )
1506 pthreadlocinfo locinfo;
1507 const char *beg, *p;
1508 int err, ret = 0;
1509 struct fpnum fp;
1511 if (flags) FIXME("flags not supported: %x\n", flags);
1513 if (!locale)
1514 locinfo = get_locinfo();
1515 else
1516 locinfo = locale->locinfo;
1518 p = str;
1519 while (_isspace_l((unsigned char)*p, locale))
1520 p++;
1521 beg = p;
1523 fp = fpnum_parse(strtod_str_get, strtod_str_unget, &p, locinfo, TRUE);
1524 if (endptr) *endptr = (p == beg ? (char*)str : (char*)p);
1525 if (p == beg) ret = 4;
1527 err = fpnum_ldouble(&fp, value);
1528 if (err) ret = (value->x80[2] & 0x7fff ? 2 : 1);
1529 return ret;
1532 /********************************************************************
1533 * __STRINGTOLD (MSVCRT.@)
1535 int CDECL __STRINGTOLD( MSVCRT__LDOUBLE *value, char **endptr, const char *str, int flags )
1537 return __STRINGTOLD_L( value, endptr, str, flags, NULL );
1540 /********************************************************************
1541 * _atoldbl_l (MSVCRT.@)
1543 int CDECL _atoldbl_l( MSVCRT__LDOUBLE *value, char *str, _locale_t locale )
1545 char *endptr;
1546 switch(__STRINGTOLD_L( value, &endptr, str, 0, locale ))
1548 case 1: return _UNDERFLOW;
1549 case 2: return _OVERFLOW;
1550 default: return 0;
1554 /********************************************************************
1555 * _atoldbl (MSVCRT.@)
1557 int CDECL _atoldbl(_LDOUBLE *value, char *str)
1559 return _atoldbl_l( (MSVCRT__LDOUBLE*)value, str, NULL );
1562 /*********************************************************************
1563 * strlen (MSVCRT.@)
1565 size_t __cdecl strlen(const char *str)
1567 const char *s = str;
1568 while (*s) s++;
1569 return s - str;
1572 /******************************************************************
1573 * strnlen (MSVCRT.@)
1575 size_t CDECL strnlen(const char *s, size_t maxlen)
1577 size_t i;
1579 for(i=0; i<maxlen; i++)
1580 if(!s[i]) break;
1582 return i;
1585 /*********************************************************************
1586 * _strtoi64_l (MSVCRT.@)
1588 * FIXME: locale parameter is ignored
1590 __int64 CDECL _strtoi64_l(const char *nptr, char **endptr, int base, _locale_t locale)
1592 const char *p = nptr;
1593 BOOL negative = FALSE;
1594 BOOL got_digit = FALSE;
1595 __int64 ret = 0;
1597 TRACE("(%s %p %d %p)\n", debugstr_a(nptr), endptr, base, locale);
1599 if (!MSVCRT_CHECK_PMT(nptr != NULL)) return 0;
1600 if (!MSVCRT_CHECK_PMT(base == 0 || base >= 2)) return 0;
1601 if (!MSVCRT_CHECK_PMT(base <= 36)) return 0;
1603 while(_isspace_l((unsigned char)*nptr, locale)) nptr++;
1605 if(*nptr == '-') {
1606 negative = TRUE;
1607 nptr++;
1608 } else if(*nptr == '+')
1609 nptr++;
1611 if((base==0 || base==16) && *nptr=='0' && _tolower_l(*(nptr+1), locale)=='x') {
1612 base = 16;
1613 nptr += 2;
1616 if(base == 0) {
1617 if(*nptr=='0')
1618 base = 8;
1619 else
1620 base = 10;
1623 while(*nptr) {
1624 char cur = _tolower_l(*nptr, locale);
1625 int v;
1627 if(cur>='0' && cur<='9') {
1628 if(cur >= '0'+base)
1629 break;
1630 v = cur-'0';
1631 } else {
1632 if(cur<'a' || cur>='a'+base-10)
1633 break;
1634 v = cur-'a'+10;
1636 got_digit = TRUE;
1638 if(negative)
1639 v = -v;
1641 nptr++;
1643 if(!negative && (ret>I64_MAX/base || ret*base>I64_MAX-v)) {
1644 ret = I64_MAX;
1645 *_errno() = ERANGE;
1646 } else if(negative && (ret<I64_MIN/base || ret*base<I64_MIN-v)) {
1647 ret = I64_MIN;
1648 *_errno() = ERANGE;
1649 } else
1650 ret = ret*base + v;
1653 if(endptr)
1654 *endptr = (char*)(got_digit ? nptr : p);
1656 return ret;
1659 /*********************************************************************
1660 * _strtoi64 (MSVCRT.@)
1662 __int64 CDECL _strtoi64(const char *nptr, char **endptr, int base)
1664 return _strtoi64_l(nptr, endptr, base, NULL);
1667 /*********************************************************************
1668 * _atoi_l (MSVCRT.@)
1670 int __cdecl _atoi_l(const char *str, _locale_t locale)
1672 __int64 ret = _strtoi64_l(str, NULL, 10, locale);
1674 if(ret > INT_MAX) {
1675 ret = INT_MAX;
1676 *_errno() = ERANGE;
1677 } else if(ret < INT_MIN) {
1678 ret = INT_MIN;
1679 *_errno() = ERANGE;
1681 return ret;
1684 /*********************************************************************
1685 * atoi (MSVCRT.@)
1687 #if _MSVCR_VER == 0
1688 int __cdecl atoi(const char *str)
1690 BOOL minus = FALSE;
1691 int ret = 0;
1693 if(!str)
1694 return 0;
1696 while(_isspace_l((unsigned char)*str, NULL)) str++;
1698 if(*str == '+') {
1699 str++;
1700 }else if(*str == '-') {
1701 minus = TRUE;
1702 str++;
1705 while(*str>='0' && *str<='9') {
1706 ret = ret*10+*str-'0';
1707 str++;
1710 return minus ? -ret : ret;
1712 #else
1713 int CDECL atoi(const char *str)
1715 return _atoi_l(str, NULL);
1717 #endif
1719 /******************************************************************
1720 * _atoi64_l (MSVCRT.@)
1722 __int64 CDECL _atoi64_l(const char *str, _locale_t locale)
1724 return _strtoi64_l(str, NULL, 10, locale);
1727 /******************************************************************
1728 * _atoi64 (MSVCRT.@)
1730 __int64 CDECL _atoi64(const char *str)
1732 return _strtoi64_l(str, NULL, 10, NULL);
1735 /******************************************************************
1736 * _atol_l (MSVCRT.@)
1738 __msvcrt_long CDECL _atol_l(const char *str, _locale_t locale)
1740 __int64 ret = _strtoi64_l(str, NULL, 10, locale);
1742 if(ret > LONG_MAX) {
1743 ret = LONG_MAX;
1744 *_errno() = ERANGE;
1745 } else if(ret < LONG_MIN) {
1746 ret = LONG_MIN;
1747 *_errno() = ERANGE;
1749 return ret;
1752 /******************************************************************
1753 * atol (MSVCRT.@)
1755 __msvcrt_long CDECL atol(const char *str)
1757 #if _MSVCR_VER == 0
1758 return atoi(str);
1759 #else
1760 return _atol_l(str, NULL);
1761 #endif
1764 #if _MSVCR_VER>=120
1766 /******************************************************************
1767 * _atoll_l (MSVCR120.@)
1769 __int64 CDECL _atoll_l(const char* str, _locale_t locale)
1771 return _strtoi64_l(str, NULL, 10, locale);
1774 /******************************************************************
1775 * atoll (MSVCR120.@)
1777 __int64 CDECL atoll(const char* str)
1779 return _atoll_l(str, NULL);
1782 #endif /* _MSVCR_VER>=120 */
1784 /******************************************************************
1785 * _strtol_l (MSVCRT.@)
1787 __msvcrt_long CDECL _strtol_l(const char* nptr,
1788 char** end, int base, _locale_t locale)
1790 __int64 ret = _strtoi64_l(nptr, end, base, locale);
1792 if(ret > LONG_MAX) {
1793 ret = LONG_MAX;
1794 *_errno() = ERANGE;
1795 } else if(ret < LONG_MIN) {
1796 ret = LONG_MIN;
1797 *_errno() = ERANGE;
1800 return ret;
1803 /******************************************************************
1804 * strtol (MSVCRT.@)
1806 __msvcrt_long CDECL strtol(const char* nptr, char** end, int base)
1808 return _strtol_l(nptr, end, base, NULL);
1811 /******************************************************************
1812 * _strtoul_l (MSVCRT.@)
1814 __msvcrt_ulong CDECL _strtoul_l(const char* nptr, char** end, int base, _locale_t locale)
1816 __int64 ret = _strtoi64_l(nptr, end, base, locale);
1818 if(ret > ULONG_MAX) {
1819 ret = ULONG_MAX;
1820 *_errno() = ERANGE;
1821 }else if(ret < -(__int64)ULONG_MAX) {
1822 ret = 1;
1823 *_errno() = ERANGE;
1826 return ret;
1829 /******************************************************************
1830 * strtoul (MSVCRT.@)
1832 __msvcrt_ulong CDECL strtoul(const char* nptr, char** end, int base)
1834 return _strtoul_l(nptr, end, base, NULL);
1837 /*********************************************************************
1838 * _strtoui64_l (MSVCRT.@)
1840 * FIXME: locale parameter is ignored
1842 unsigned __int64 CDECL _strtoui64_l(const char *nptr, char **endptr, int base, _locale_t locale)
1844 const char *p = nptr;
1845 BOOL negative = FALSE;
1846 BOOL got_digit = FALSE;
1847 unsigned __int64 ret = 0;
1849 TRACE("(%s %p %d %p)\n", debugstr_a(nptr), endptr, base, locale);
1851 if (!MSVCRT_CHECK_PMT(nptr != NULL)) return 0;
1852 if (!MSVCRT_CHECK_PMT(base == 0 || base >= 2)) return 0;
1853 if (!MSVCRT_CHECK_PMT(base <= 36)) return 0;
1855 while(_isspace_l((unsigned char)*nptr, locale)) nptr++;
1857 if(*nptr == '-') {
1858 negative = TRUE;
1859 nptr++;
1860 } else if(*nptr == '+')
1861 nptr++;
1863 if((base==0 || base==16) && *nptr=='0' && _tolower_l(*(nptr+1), locale)=='x') {
1864 base = 16;
1865 nptr += 2;
1868 if(base == 0) {
1869 if(*nptr=='0')
1870 base = 8;
1871 else
1872 base = 10;
1875 while(*nptr) {
1876 char cur = _tolower_l(*nptr, locale);
1877 int v;
1879 if(cur>='0' && cur<='9') {
1880 if(cur >= '0'+base)
1881 break;
1882 v = *nptr-'0';
1883 } else {
1884 if(cur<'a' || cur>='a'+base-10)
1885 break;
1886 v = cur-'a'+10;
1888 got_digit = TRUE;
1890 nptr++;
1892 if(ret>UI64_MAX/base || ret*base>UI64_MAX-v) {
1893 ret = UI64_MAX;
1894 *_errno() = ERANGE;
1895 } else
1896 ret = ret*base + v;
1899 if(endptr)
1900 *endptr = (char*)(got_digit ? nptr : p);
1902 return negative ? -ret : ret;
1905 /*********************************************************************
1906 * _strtoui64 (MSVCRT.@)
1908 unsigned __int64 CDECL _strtoui64(const char *nptr, char **endptr, int base)
1910 return _strtoui64_l(nptr, endptr, base, NULL);
1913 static int ltoa_helper(__msvcrt_long value, char *str, size_t size, int radix)
1915 __msvcrt_ulong val;
1916 unsigned int digit;
1917 BOOL is_negative;
1918 char buffer[33], *pos;
1919 size_t len;
1921 if (value < 0 && radix == 10)
1923 is_negative = TRUE;
1924 val = -value;
1926 else
1928 is_negative = FALSE;
1929 val = value;
1932 pos = buffer + 32;
1933 *pos = '\0';
1937 digit = val % radix;
1938 val /= radix;
1940 if (digit < 10)
1941 *--pos = '0' + digit;
1942 else
1943 *--pos = 'a' + digit - 10;
1945 while (val != 0);
1947 if (is_negative)
1948 *--pos = '-';
1950 len = buffer + 33 - pos;
1951 if (len > size)
1953 size_t i;
1954 char *p = str;
1956 /* Copy the temporary buffer backwards up to the available number of
1957 * characters. Don't copy the negative sign if present. */
1959 if (is_negative)
1961 p++;
1962 size--;
1965 for (pos = buffer + 31, i = 0; i < size; i++)
1966 *p++ = *pos--;
1968 str[0] = '\0';
1969 MSVCRT_INVALID_PMT("str[size] is too small", ERANGE);
1970 return ERANGE;
1973 memcpy(str, pos, len);
1974 return 0;
1977 static int ltow_helper(__msvcrt_long value, wchar_t *str, size_t size, int radix)
1979 __msvcrt_ulong val;
1980 unsigned int digit;
1981 BOOL is_negative;
1982 wchar_t buffer[33], *pos;
1983 size_t len;
1985 if (value < 0 && radix == 10)
1987 is_negative = TRUE;
1988 val = -value;
1990 else
1992 is_negative = FALSE;
1993 val = value;
1996 pos = buffer + 32;
1997 *pos = '\0';
2001 digit = val % radix;
2002 val /= radix;
2004 if (digit < 10)
2005 *--pos = '0' + digit;
2006 else
2007 *--pos = 'a' + digit - 10;
2009 while (val != 0);
2011 if (is_negative)
2012 *--pos = '-';
2014 len = buffer + 33 - pos;
2015 if (len > size)
2017 size_t i;
2018 wchar_t *p = str;
2020 /* Copy the temporary buffer backwards up to the available number of
2021 * characters. Don't copy the negative sign if present. */
2023 if (is_negative)
2025 p++;
2026 size--;
2029 for (pos = buffer + 31, i = 0; i < size; i++)
2030 *p++ = *pos--;
2032 str[0] = '\0';
2033 MSVCRT_INVALID_PMT("str[size] is too small", ERANGE);
2034 return ERANGE;
2037 memcpy(str, pos, len * sizeof(wchar_t));
2038 return 0;
2041 /*********************************************************************
2042 * _ltoa_s (MSVCRT.@)
2044 int CDECL _ltoa_s(__msvcrt_long value, char *str, size_t size, int radix)
2046 if (!MSVCRT_CHECK_PMT(str != NULL)) return EINVAL;
2047 if (!MSVCRT_CHECK_PMT(size > 0)) return EINVAL;
2048 if (!MSVCRT_CHECK_PMT(radix >= 2 && radix <= 36))
2050 str[0] = '\0';
2051 return EINVAL;
2054 return ltoa_helper(value, str, size, radix);
2057 /*********************************************************************
2058 * _ltow_s (MSVCRT.@)
2060 int CDECL _ltow_s(__msvcrt_long value, wchar_t *str, size_t size, int radix)
2062 if (!MSVCRT_CHECK_PMT(str != NULL)) return EINVAL;
2063 if (!MSVCRT_CHECK_PMT(size > 0)) return EINVAL;
2064 if (!MSVCRT_CHECK_PMT(radix >= 2 && radix <= 36))
2066 str[0] = '\0';
2067 return EINVAL;
2070 return ltow_helper(value, str, size, radix);
2073 /*********************************************************************
2074 * _itoa_s (MSVCRT.@)
2076 int CDECL _itoa_s(int value, char *str, size_t size, int radix)
2078 return _ltoa_s(value, str, size, radix);
2081 /*********************************************************************
2082 * _itoa (MSVCRT.@)
2084 char* CDECL _itoa(int value, char *str, int radix)
2086 return ltoa_helper(value, str, SIZE_MAX, radix) ? NULL : str;
2089 /*********************************************************************
2090 * _ltoa (MSVCRT.@)
2092 char* CDECL _ltoa(__msvcrt_long value, char *str, int radix)
2094 return ltoa_helper(value, str, SIZE_MAX, radix) ? NULL : str;
2097 /*********************************************************************
2098 * _itow_s (MSVCRT.@)
2100 int CDECL _itow_s(int value, wchar_t *str, size_t size, int radix)
2102 return _ltow_s(value, str, size, radix);
2105 /*********************************************************************
2106 * _itow (MSVCRT.@)
2108 wchar_t* CDECL _itow(int value, wchar_t *str, int radix)
2110 return ltow_helper(value, str, SIZE_MAX, radix) ? NULL : str;
2113 /*********************************************************************
2114 * _ltow (MSVCRT.@)
2116 wchar_t* CDECL _ltow(__msvcrt_long value, wchar_t *str, int radix)
2118 return ltow_helper(value, str, SIZE_MAX, radix) ? NULL : str;
2121 /*********************************************************************
2122 * _ultoa (MSVCRT.@)
2124 char* CDECL _ultoa(__msvcrt_ulong value, char *str, int radix)
2126 char buffer[33], *pos;
2128 pos = &buffer[32];
2129 *pos = '\0';
2131 do {
2132 int digit = value % radix;
2133 value /= radix;
2135 if (digit < 10)
2136 *--pos = '0' + digit;
2137 else
2138 *--pos = 'a' + digit - 10;
2139 } while (value != 0);
2141 memcpy(str, pos, buffer + 33 - pos);
2142 return str;
2145 /*********************************************************************
2146 * _ui64toa (MSVCRT.@)
2148 char* CDECL _ui64toa(unsigned __int64 value, char *str, int radix)
2150 char buffer[65], *pos;
2152 pos = &buffer[64];
2153 *pos = '\0';
2155 do {
2156 int digit = value % radix;
2157 value /= radix;
2159 if (digit < 10)
2160 *--pos = '0' + digit;
2161 else
2162 *--pos = 'a' + digit - 10;
2163 } while (value != 0);
2165 memcpy(str, pos, buffer + 65 - pos);
2166 return str;
2169 /*********************************************************************
2170 * _ultow (MSVCRT.@)
2172 wchar_t* CDECL _ultow(__msvcrt_ulong value, wchar_t *str, int radix)
2174 wchar_t buffer[33], *pos;
2176 pos = &buffer[32];
2177 *pos = '\0';
2179 do {
2180 int digit = value % radix;
2181 value /= radix;
2183 if (digit < 10)
2184 *--pos = '0' + digit;
2185 else
2186 *--pos = 'a' + digit - 10;
2187 } while (value != 0);
2189 memcpy(str, pos, (buffer + 33 - pos) * sizeof(wchar_t));
2190 return str;
2193 /*********************************************************************
2194 * _ui64tow (MSVCRT.@)
2196 wchar_t* CDECL _ui64tow(unsigned __int64 value, wchar_t *str, int radix)
2198 wchar_t buffer[65], *pos;
2200 pos = &buffer[64];
2201 *pos = '\0';
2203 do {
2204 int digit = value % radix;
2205 value /= radix;
2207 if (digit < 10)
2208 *--pos = '0' + digit;
2209 else
2210 *--pos = 'a' + digit - 10;
2211 } while (value != 0);
2213 memcpy(str, pos, (buffer + 65 - pos) * sizeof(wchar_t));
2214 return str;
2217 /*********************************************************************
2218 * _i64toa (MSVCRT.@)
2220 char* CDECL _i64toa(__int64 value, char *str, int radix)
2222 unsigned __int64 val;
2223 BOOL is_negative;
2224 char buffer[65], *pos;
2226 if (value < 0 && radix == 10)
2228 is_negative = TRUE;
2229 val = -value;
2231 else
2233 is_negative = FALSE;
2234 val = value;
2237 pos = buffer + 64;
2238 *pos = '\0';
2242 int digit = val % radix;
2243 val /= radix;
2245 if (digit < 10)
2246 *--pos = '0' + digit;
2247 else
2248 *--pos = 'a' + digit - 10;
2250 while (val != 0);
2252 if (is_negative)
2253 *--pos = '-';
2255 memcpy(str, pos, buffer + 65 - pos);
2256 return str;
2259 /*********************************************************************
2260 * _i64tow (MSVCRT.@)
2262 wchar_t* CDECL _i64tow(__int64 value, wchar_t *str, int radix)
2264 unsigned __int64 val;
2265 BOOL is_negative;
2266 wchar_t buffer[65], *pos;
2268 if (value < 0 && radix == 10)
2270 is_negative = TRUE;
2271 val = -value;
2273 else
2275 is_negative = FALSE;
2276 val = value;
2279 pos = buffer + 64;
2280 *pos = '\0';
2284 int digit = val % radix;
2285 val /= radix;
2287 if (digit < 10)
2288 *--pos = '0' + digit;
2289 else
2290 *--pos = 'a' + digit - 10;
2292 while (val != 0);
2294 if (is_negative)
2295 *--pos = '-';
2297 memcpy(str, pos, (buffer + 65 - pos) * sizeof(wchar_t));
2298 return str;
2301 /*********************************************************************
2302 * _ui64toa_s (MSVCRT.@)
2304 int CDECL _ui64toa_s(unsigned __int64 value, char *str,
2305 size_t size, int radix)
2307 char buffer[65], *pos;
2308 int digit;
2310 if (!MSVCRT_CHECK_PMT(str != NULL)) return EINVAL;
2311 if (!MSVCRT_CHECK_PMT(size > 0)) return EINVAL;
2312 if (!MSVCRT_CHECK_PMT(radix >= 2 && radix <= 36))
2314 str[0] = '\0';
2315 return EINVAL;
2318 pos = buffer+64;
2319 *pos = '\0';
2321 do {
2322 digit = value%radix;
2323 value /= radix;
2325 if(digit < 10)
2326 *--pos = '0'+digit;
2327 else
2328 *--pos = 'a'+digit-10;
2329 }while(value != 0);
2331 if(buffer-pos+65 > size) {
2332 MSVCRT_INVALID_PMT("str[size] is too small", EINVAL);
2333 return EINVAL;
2336 memcpy(str, pos, buffer-pos+65);
2337 return 0;
2340 /*********************************************************************
2341 * _ui64tow_s (MSVCRT.@)
2343 int CDECL _ui64tow_s( unsigned __int64 value, wchar_t *str,
2344 size_t size, int radix )
2346 wchar_t buffer[65], *pos;
2347 int digit;
2349 if (!MSVCRT_CHECK_PMT(str != NULL)) return EINVAL;
2350 if (!MSVCRT_CHECK_PMT(size > 0)) return EINVAL;
2351 if (!MSVCRT_CHECK_PMT(radix >= 2 && radix <= 36))
2353 str[0] = '\0';
2354 return EINVAL;
2357 pos = &buffer[64];
2358 *pos = '\0';
2360 do {
2361 digit = value % radix;
2362 value = value / radix;
2363 if (digit < 10)
2364 *--pos = '0' + digit;
2365 else
2366 *--pos = 'a' + digit - 10;
2367 } while (value != 0);
2369 if(buffer-pos+65 > size) {
2370 MSVCRT_INVALID_PMT("str[size] is too small", EINVAL);
2371 return EINVAL;
2374 memcpy(str, pos, (buffer-pos+65)*sizeof(wchar_t));
2375 return 0;
2378 /*********************************************************************
2379 * _ultoa_s (MSVCRT.@)
2381 int CDECL _ultoa_s(__msvcrt_ulong value, char *str, size_t size, int radix)
2383 __msvcrt_ulong digit;
2384 char buffer[33], *pos;
2385 size_t len;
2387 if (!str || !size || radix < 2 || radix > 36)
2389 if (str && size)
2390 str[0] = '\0';
2392 *_errno() = EINVAL;
2393 return EINVAL;
2396 pos = buffer + 32;
2397 *pos = '\0';
2401 digit = value % radix;
2402 value /= radix;
2404 if (digit < 10)
2405 *--pos = '0' + digit;
2406 else
2407 *--pos = 'a' + digit - 10;
2409 while (value != 0);
2411 len = buffer + 33 - pos;
2412 if (len > size)
2414 size_t i;
2415 char *p = str;
2417 /* Copy the temporary buffer backwards up to the available number of
2418 * characters. */
2420 for (pos = buffer + 31, i = 0; i < size; i++)
2421 *p++ = *pos--;
2423 str[0] = '\0';
2424 *_errno() = ERANGE;
2425 return ERANGE;
2428 memcpy(str, pos, len);
2429 return 0;
2432 /*********************************************************************
2433 * _ultow_s (MSVCRT.@)
2435 int CDECL _ultow_s(__msvcrt_ulong value, wchar_t *str, size_t size, int radix)
2437 __msvcrt_ulong digit;
2438 WCHAR buffer[33], *pos;
2439 size_t len;
2441 if (!str || !size || radix < 2 || radix > 36)
2443 if (str && size)
2444 str[0] = '\0';
2446 *_errno() = EINVAL;
2447 return EINVAL;
2450 pos = buffer + 32;
2451 *pos = '\0';
2455 digit = value % radix;
2456 value /= radix;
2458 if (digit < 10)
2459 *--pos = '0' + digit;
2460 else
2461 *--pos = 'a' + digit - 10;
2463 while (value != 0);
2465 len = buffer + 33 - pos;
2466 if (len > size)
2468 size_t i;
2469 WCHAR *p = str;
2471 /* Copy the temporary buffer backwards up to the available number of
2472 * characters. */
2474 for (pos = buffer + 31, i = 0; i < size; i++)
2475 *p++ = *pos--;
2477 str[0] = '\0';
2478 *_errno() = ERANGE;
2479 return ERANGE;
2482 memcpy(str, pos, len * sizeof(wchar_t));
2483 return 0;
2486 /*********************************************************************
2487 * _i64toa_s (MSVCRT.@)
2489 int CDECL _i64toa_s(__int64 value, char *str, size_t size, int radix)
2491 unsigned __int64 val;
2492 unsigned int digit;
2493 BOOL is_negative;
2494 char buffer[65], *pos;
2495 size_t len;
2497 if (!MSVCRT_CHECK_PMT(str != NULL)) return EINVAL;
2498 if (!MSVCRT_CHECK_PMT(size > 0)) return EINVAL;
2499 if (!MSVCRT_CHECK_PMT(radix >= 2 && radix <= 36))
2501 str[0] = '\0';
2502 return EINVAL;
2505 if (value < 0 && radix == 10)
2507 is_negative = TRUE;
2508 val = -value;
2510 else
2512 is_negative = FALSE;
2513 val = value;
2516 pos = buffer + 64;
2517 *pos = '\0';
2521 digit = val % radix;
2522 val /= radix;
2524 if (digit < 10)
2525 *--pos = '0' + digit;
2526 else
2527 *--pos = 'a' + digit - 10;
2529 while (val != 0);
2531 if (is_negative)
2532 *--pos = '-';
2534 len = buffer + 65 - pos;
2535 if (len > size)
2537 size_t i;
2538 char *p = str;
2540 /* Copy the temporary buffer backwards up to the available number of
2541 * characters. Don't copy the negative sign if present. */
2543 if (is_negative)
2545 p++;
2546 size--;
2549 for (pos = buffer + 63, i = 0; i < size; i++)
2550 *p++ = *pos--;
2552 str[0] = '\0';
2553 MSVCRT_INVALID_PMT("str[size] is too small", ERANGE);
2554 return ERANGE;
2557 memcpy(str, pos, len);
2558 return 0;
2561 /*********************************************************************
2562 * _i64tow_s (MSVCRT.@)
2564 int CDECL _i64tow_s(__int64 value, wchar_t *str, size_t size, int radix)
2566 unsigned __int64 val;
2567 unsigned int digit;
2568 BOOL is_negative;
2569 wchar_t buffer[65], *pos;
2570 size_t len;
2572 if (!MSVCRT_CHECK_PMT(str != NULL)) return EINVAL;
2573 if (!MSVCRT_CHECK_PMT(size > 0)) return EINVAL;
2574 if (!MSVCRT_CHECK_PMT(radix >= 2 && radix <= 36))
2576 str[0] = '\0';
2577 return EINVAL;
2580 if (value < 0 && radix == 10)
2582 is_negative = TRUE;
2583 val = -value;
2585 else
2587 is_negative = FALSE;
2588 val = value;
2591 pos = buffer + 64;
2592 *pos = '\0';
2596 digit = val % radix;
2597 val /= radix;
2599 if (digit < 10)
2600 *--pos = '0' + digit;
2601 else
2602 *--pos = 'a' + digit - 10;
2604 while (val != 0);
2606 if (is_negative)
2607 *--pos = '-';
2609 len = buffer + 65 - pos;
2610 if (len > size)
2612 size_t i;
2613 wchar_t *p = str;
2615 /* Copy the temporary buffer backwards up to the available number of
2616 * characters. Don't copy the negative sign if present. */
2618 if (is_negative)
2620 p++;
2621 size--;
2624 for (pos = buffer + 63, i = 0; i < size; i++)
2625 *p++ = *pos--;
2627 str[0] = '\0';
2628 MSVCRT_INVALID_PMT("str[size] is too small", ERANGE);
2629 return ERANGE;
2632 memcpy(str, pos, len * sizeof(wchar_t));
2633 return 0;
2636 #define I10_OUTPUT_MAX_PREC 21
2637 /* Internal structure used by $I10_OUTPUT */
2638 struct _I10_OUTPUT_DATA {
2639 short pos;
2640 char sign;
2641 BYTE len;
2642 char str[I10_OUTPUT_MAX_PREC+1]; /* add space for '\0' */
2645 /*********************************************************************
2646 * $I10_OUTPUT (MSVCRT.@)
2647 * ld80 - long double (Intel 80 bit FP in 12 bytes) to be printed to data
2648 * prec - precision of part, we're interested in
2649 * flag - 0 for first prec digits, 1 for fractional part
2650 * data - data to be populated
2652 * return value
2653 * 0 if given double is NaN or INF
2654 * 1 otherwise
2656 * FIXME
2657 * Native sets last byte of data->str to '0' or '9', I don't know what
2658 * it means. Current implementation sets it always to '0'.
2660 int CDECL I10_OUTPUT(MSVCRT__LDOUBLE ld80, int prec, int flag, struct _I10_OUTPUT_DATA *data)
2662 struct fpnum num;
2663 double d;
2664 char format[8];
2665 char buf[I10_OUTPUT_MAX_PREC+9]; /* 9 = strlen("0.e+0000") + '\0' */
2666 char *p;
2668 if ((ld80.x80[2] & 0x7fff) == 0x7fff)
2670 if (ld80.x80[0] == 0 && ld80.x80[1] == 0x80000000)
2671 strcpy( data->str, "1#INF" );
2672 else
2673 strcpy( data->str, (ld80.x80[1] & 0x40000000) ? "1#QNAN" : "1#SNAN" );
2674 data->pos = 1;
2675 data->sign = (ld80.x80[2] & 0x8000) ? '-' : ' ';
2676 data->len = strlen(data->str);
2677 return 0;
2680 num.sign = (ld80.x80[2] & 0x8000) ? -1 : 1;
2681 num.exp = (ld80.x80[2] & 0x7fff) - 0x3fff - 63;
2682 num.m = ld80.x80[0] | ((ULONGLONG)ld80.x80[1] << 32);
2683 num.mod = FP_ROUND_EVEN;
2684 fpnum_double( &num, &d );
2685 TRACE("(%lf %d %x %p)\n", d, prec, flag, data);
2687 if(d<0) {
2688 data->sign = '-';
2689 d = -d;
2690 } else
2691 data->sign = ' ';
2693 if(flag&1) {
2694 int exp = 1 + floor(log10(d));
2696 prec += exp;
2697 if(exp < 0)
2698 prec--;
2700 prec--;
2702 if(prec+1 > I10_OUTPUT_MAX_PREC)
2703 prec = I10_OUTPUT_MAX_PREC-1;
2704 else if(prec < 0) {
2705 d = 0.0;
2706 prec = 0;
2709 sprintf(format, "%%.%dle", prec);
2710 sprintf(buf, format, d);
2712 buf[1] = buf[0];
2713 data->pos = atoi(buf+prec+3);
2714 if(buf[1] != '0')
2715 data->pos++;
2717 for(p = buf+prec+1; p>buf+1 && *p=='0'; p--);
2718 data->len = p-buf;
2720 memcpy(data->str, buf+1, data->len);
2721 data->str[data->len] = '\0';
2723 if(buf[1]!='0' && prec-data->len+1>0)
2724 memcpy(data->str+data->len+1, buf+data->len+1, prec-data->len+1);
2726 return 1;
2728 #undef I10_OUTPUT_MAX_PREC
2730 static inline int memcmp_bytes(const void *ptr1, const void *ptr2, size_t n)
2732 const unsigned char *p1, *p2;
2734 for (p1 = ptr1, p2 = ptr2; n; n--, p1++, p2++)
2736 if (*p1 != *p2)
2737 return *p1 > *p2 ? 1 : -1;
2739 return 0;
2742 static inline int memcmp_blocks(const void *ptr1, const void *ptr2, size_t size)
2744 typedef uint64_t DECLSPEC_ALIGN(1) unaligned_ui64;
2746 const uint64_t *p1 = ptr1;
2747 const unaligned_ui64 *p2 = ptr2;
2748 size_t remainder = size & (sizeof(uint64_t) - 1);
2749 size_t block_count = size / sizeof(uint64_t);
2751 while (block_count)
2753 if (*p1 != *p2)
2754 return memcmp_bytes(p1, p2, sizeof(uint64_t));
2756 p1++;
2757 p2++;
2758 block_count--;
2761 return memcmp_bytes(p1, p2, remainder);
2764 /*********************************************************************
2765 * memcmp (MSVCRT.@)
2767 int __cdecl memcmp(const void *ptr1, const void *ptr2, size_t n)
2769 const unsigned char *p1 = ptr1, *p2 = ptr2;
2770 size_t align;
2771 int result;
2773 if (n < sizeof(uint64_t))
2774 return memcmp_bytes(p1, p2, n);
2776 align = -(size_t)p1 & (sizeof(uint64_t) - 1);
2778 if ((result = memcmp_bytes(p1, p2, align)))
2779 return result;
2781 p1 += align;
2782 p2 += align;
2783 n -= align;
2785 return memcmp_blocks(p1, p2, n);
2788 #if defined(__i386__) || defined(__x86_64__)
2790 #ifdef __i386__
2792 #define DEST_REG "%edi"
2793 #define SRC_REG "%esi"
2794 #define LEN_REG "%ecx"
2795 #define TMP_REG "%edx"
2797 #define MEMMOVE_INIT \
2798 "pushl " SRC_REG "\n\t" \
2799 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
2800 "pushl " DEST_REG "\n\t" \
2801 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
2802 "movl 12(%esp), " DEST_REG "\n\t" \
2803 "movl 16(%esp), " SRC_REG "\n\t" \
2804 "movl 20(%esp), " LEN_REG "\n\t"
2806 #define MEMMOVE_CLEANUP \
2807 "movl 12(%esp), %eax\n\t" \
2808 "popl " DEST_REG "\n\t" \
2809 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t") \
2810 "popl " SRC_REG "\n\t" \
2811 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
2813 #else
2815 #define DEST_REG "%rdi"
2816 #define SRC_REG "%rsi"
2817 #define LEN_REG "%r8"
2818 #define TMP_REG "%r9"
2820 #define MEMMOVE_INIT \
2821 "pushq " SRC_REG "\n\t" \
2822 __ASM_SEH(".seh_pushreg " SRC_REG "\n\t") \
2823 __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t") \
2824 "pushq " DEST_REG "\n\t" \
2825 __ASM_SEH(".seh_pushreg " DEST_REG "\n\t") \
2826 __ASM_SEH(".seh_endprologue\n\t") \
2827 __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t") \
2828 "movq %rcx, " DEST_REG "\n\t" \
2829 "movq %rdx, " SRC_REG "\n\t"
2831 #define MEMMOVE_CLEANUP \
2832 "movq %rcx, %rax\n\t" \
2833 "popq " DEST_REG "\n\t" \
2834 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t") \
2835 "popq " SRC_REG "\n\t" \
2836 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
2837 #endif
2839 void * __cdecl sse2_memmove(void *dst, const void *src, size_t n);
2840 __ASM_GLOBAL_FUNC( sse2_memmove,
2841 MEMMOVE_INIT
2842 "mov " DEST_REG ", " TMP_REG "\n\t" /* check copying direction */
2843 "sub " SRC_REG ", " TMP_REG "\n\t"
2844 "cmp " LEN_REG ", " TMP_REG "\n\t"
2845 "jb copy_bwd\n\t"
2846 /* copy forwards */
2847 "cmp $4, " LEN_REG "\n\t" /* 4-bytes align */
2848 "jb copy_fwd3\n\t"
2849 "mov " DEST_REG ", " TMP_REG "\n\t"
2850 "shr $1, " TMP_REG "\n\t"
2851 "jnc 1f\n\t"
2852 "movsb\n\t"
2853 "dec " LEN_REG "\n\t"
2854 "inc " TMP_REG "\n\t"
2855 "1:\n\t"
2856 "shr $1, " TMP_REG "\n\t"
2857 "jnc 1f\n\t"
2858 "movsw\n\t"
2859 "sub $2, " LEN_REG "\n\t"
2860 "inc " TMP_REG "\n\t"
2861 "1:\n\t" /* 16-bytes align */
2862 "cmp $16, " LEN_REG "\n\t"
2863 "jb copy_fwd15\n\t"
2864 "shr $1, " TMP_REG "\n\t"
2865 "jnc 1f\n\t"
2866 "movsl\n\t"
2867 "sub $4, " LEN_REG "\n\t"
2868 "inc " TMP_REG "\n\t"
2869 "1:\n\t"
2870 "shr $1, " TMP_REG "\n\t"
2871 "jnc 1f\n\t"
2872 "movsl\n\t"
2873 "movsl\n\t"
2874 "sub $8, " LEN_REG "\n\t"
2875 "1:\n\t"
2876 "cmp $64, " LEN_REG "\n\t"
2877 "jb copy_fwd63\n\t"
2878 "1:\n\t" /* copy 64-bytes blocks in loop, dest 16-bytes aligned */
2879 "movdqu 0x00(" SRC_REG "), %xmm0\n\t"
2880 "movdqu 0x10(" SRC_REG "), %xmm1\n\t"
2881 "movdqu 0x20(" SRC_REG "), %xmm2\n\t"
2882 "movdqu 0x30(" SRC_REG "), %xmm3\n\t"
2883 "movdqa %xmm0, 0x00(" DEST_REG ")\n\t"
2884 "movdqa %xmm1, 0x10(" DEST_REG ")\n\t"
2885 "movdqa %xmm2, 0x20(" DEST_REG ")\n\t"
2886 "movdqa %xmm3, 0x30(" DEST_REG ")\n\t"
2887 "add $64, " SRC_REG "\n\t"
2888 "add $64, " DEST_REG "\n\t"
2889 "sub $64, " LEN_REG "\n\t"
2890 "cmp $64, " LEN_REG "\n\t"
2891 "jae 1b\n\t"
2892 "copy_fwd63:\n\t" /* copy last 63 bytes, dest 16-bytes aligned */
2893 "mov " LEN_REG ", " TMP_REG "\n\t"
2894 "and $15, " LEN_REG "\n\t"
2895 "shr $5, " TMP_REG "\n\t"
2896 "jnc 1f\n\t"
2897 "movdqu 0(" SRC_REG "), %xmm0\n\t"
2898 "movdqa %xmm0, 0(" DEST_REG ")\n\t"
2899 "add $16, " SRC_REG "\n\t"
2900 "add $16, " DEST_REG "\n\t"
2901 "1:\n\t"
2902 "shr $1, " TMP_REG "\n\t"
2903 "jnc copy_fwd15\n\t"
2904 "movdqu 0x00(" SRC_REG "), %xmm0\n\t"
2905 "movdqu 0x10(" SRC_REG "), %xmm1\n\t"
2906 "movdqa %xmm0, 0x00(" DEST_REG ")\n\t"
2907 "movdqa %xmm1, 0x10(" DEST_REG ")\n\t"
2908 "add $32, " SRC_REG "\n\t"
2909 "add $32, " DEST_REG "\n\t"
2910 "copy_fwd15:\n\t" /* copy last 15 bytes, dest 4-bytes aligned */
2911 "mov " LEN_REG ", " TMP_REG "\n\t"
2912 "and $3, " LEN_REG "\n\t"
2913 "shr $3, " TMP_REG "\n\t"
2914 "jnc 1f\n\t"
2915 "movsl\n\t"
2916 "1:\n\t"
2917 "shr $1, " TMP_REG "\n\t"
2918 "jnc copy_fwd3\n\t"
2919 "movsl\n\t"
2920 "movsl\n\t"
2921 "copy_fwd3:\n\t" /* copy last 3 bytes */
2922 "shr $1, " LEN_REG "\n\t"
2923 "jnc 1f\n\t"
2924 "movsb\n\t"
2925 "1:\n\t"
2926 "shr $1, " LEN_REG "\n\t"
2927 "jnc 1f\n\t"
2928 "movsw\n\t"
2929 "1:\n\t"
2930 MEMMOVE_CLEANUP
2931 "ret\n\t"
2932 "copy_bwd:\n\t"
2933 "lea (" DEST_REG ", " LEN_REG "), " DEST_REG "\n\t"
2934 "lea (" SRC_REG ", " LEN_REG "), " SRC_REG "\n\t"
2935 "cmp $4, " LEN_REG "\n\t" /* 4-bytes align */
2936 "jb copy_bwd3\n\t"
2937 "mov " DEST_REG ", " TMP_REG "\n\t"
2938 "shr $1, " TMP_REG "\n\t"
2939 "jnc 1f\n\t"
2940 "dec " SRC_REG "\n\t"
2941 "dec " DEST_REG "\n\t"
2942 "movb (" SRC_REG "), %al\n\t"
2943 "movb %al, (" DEST_REG ")\n\t"
2944 "dec " LEN_REG "\n\t"
2945 "1:\n\t"
2946 "shr $1, " TMP_REG "\n\t"
2947 "jnc 1f\n\t"
2948 "sub $2, " SRC_REG "\n\t"
2949 "sub $2, " DEST_REG "\n\t"
2950 "movw (" SRC_REG "), %ax\n\t"
2951 "movw %ax, (" DEST_REG ")\n\t"
2952 "sub $2, " LEN_REG "\n\t"
2953 "1:\n\t" /* 16-bytes align */
2954 "cmp $16, " LEN_REG "\n\t"
2955 "jb copy_bwd15\n\t"
2956 "shr $1, " TMP_REG "\n\t"
2957 "jnc 1f\n\t"
2958 "sub $4, " SRC_REG "\n\t"
2959 "sub $4, " DEST_REG "\n\t"
2960 "movl (" SRC_REG "), %eax\n\t"
2961 "movl %eax, (" DEST_REG ")\n\t"
2962 "sub $4, " LEN_REG "\n\t"
2963 "1:\n\t"
2964 "shr $1, " TMP_REG "\n\t"
2965 "jnc 1f\n\t"
2966 "sub $8, " SRC_REG "\n\t"
2967 "sub $8, " DEST_REG "\n\t"
2968 "movl 4(" SRC_REG "), %eax\n\t"
2969 "movl %eax, 4(" DEST_REG ")\n\t"
2970 "movl (" SRC_REG "), %eax\n\t"
2971 "movl %eax, (" DEST_REG ")\n\t"
2972 "sub $8, " LEN_REG "\n\t"
2973 "1:\n\t"
2974 "cmp $64, " LEN_REG "\n\t"
2975 "jb copy_bwd63\n\t"
2976 "1:\n\t" /* copy 64-bytes blocks in loop, dest 16-bytes aligned */
2977 "sub $64, " SRC_REG "\n\t"
2978 "sub $64, " DEST_REG "\n\t"
2979 "movdqu 0x00(" SRC_REG "), %xmm0\n\t"
2980 "movdqu 0x10(" SRC_REG "), %xmm1\n\t"
2981 "movdqu 0x20(" SRC_REG "), %xmm2\n\t"
2982 "movdqu 0x30(" SRC_REG "), %xmm3\n\t"
2983 "movdqa %xmm0, 0x00(" DEST_REG ")\n\t"
2984 "movdqa %xmm1, 0x10(" DEST_REG ")\n\t"
2985 "movdqa %xmm2, 0x20(" DEST_REG ")\n\t"
2986 "movdqa %xmm3, 0x30(" DEST_REG ")\n\t"
2987 "sub $64, " LEN_REG "\n\t"
2988 "cmp $64, " LEN_REG "\n\t"
2989 "jae 1b\n\t"
2990 "copy_bwd63:\n\t" /* copy last 63 bytes, dest 16-bytes aligned */
2991 "mov " LEN_REG ", " TMP_REG "\n\t"
2992 "and $15, " LEN_REG "\n\t"
2993 "shr $5, " TMP_REG "\n\t"
2994 "jnc 1f\n\t"
2995 "sub $16, " SRC_REG "\n\t"
2996 "sub $16, " DEST_REG "\n\t"
2997 "movdqu (" SRC_REG "), %xmm0\n\t"
2998 "movdqa %xmm0, (" DEST_REG ")\n\t"
2999 "1:\n\t"
3000 "shr $1, " TMP_REG "\n\t"
3001 "jnc copy_bwd15\n\t"
3002 "sub $32, " SRC_REG "\n\t"
3003 "sub $32, " DEST_REG "\n\t"
3004 "movdqu 0x00(" SRC_REG "), %xmm0\n\t"
3005 "movdqu 0x10(" SRC_REG "), %xmm1\n\t"
3006 "movdqa %xmm0, 0x00(" DEST_REG ")\n\t"
3007 "movdqa %xmm1, 0x10(" DEST_REG ")\n\t"
3008 "copy_bwd15:\n\t" /* copy last 15 bytes, dest 4-bytes aligned */
3009 "mov " LEN_REG ", " TMP_REG "\n\t"
3010 "and $3, " LEN_REG "\n\t"
3011 "shr $3, " TMP_REG "\n\t"
3012 "jnc 1f\n\t"
3013 "sub $4, " SRC_REG "\n\t"
3014 "sub $4, " DEST_REG "\n\t"
3015 "movl (" SRC_REG "), %eax\n\t"
3016 "movl %eax, (" DEST_REG ")\n\t"
3017 "1:\n\t"
3018 "shr $1, " TMP_REG "\n\t"
3019 "jnc copy_bwd3\n\t"
3020 "sub $8, " SRC_REG "\n\t"
3021 "sub $8, " DEST_REG "\n\t"
3022 "movl 4(" SRC_REG "), %eax\n\t"
3023 "movl %eax, 4(" DEST_REG ")\n\t"
3024 "movl (" SRC_REG "), %eax\n\t"
3025 "movl %eax, (" DEST_REG ")\n\t"
3026 "copy_bwd3:\n\t" /* copy last 3 bytes */
3027 "shr $1, " LEN_REG "\n\t"
3028 "jnc 1f\n\t"
3029 "dec " SRC_REG "\n\t"
3030 "dec " DEST_REG "\n\t"
3031 "movb (" SRC_REG "), %al\n\t"
3032 "movb %al, (" DEST_REG ")\n\t"
3033 "1:\n\t"
3034 "shr $1, " LEN_REG "\n\t"
3035 "jnc 1f\n\t"
3036 "movw -2(" SRC_REG "), %ax\n\t"
3037 "movw %ax, -2(" DEST_REG ")\n\t"
3038 "1:\n\t"
3039 MEMMOVE_CLEANUP
3040 "ret" )
3042 #endif
3044 /*********************************************************************
3045 * memmove (MSVCRT.@)
3047 #ifdef WORDS_BIGENDIAN
3048 # define MERGE(w1, sh1, w2, sh2) ((w1 << sh1) | (w2 >> sh2))
3049 #else
3050 # define MERGE(w1, sh1, w2, sh2) ((w1 >> sh1) | (w2 << sh2))
3051 #endif
3052 void * __cdecl memmove(void *dst, const void *src, size_t n)
3054 #ifdef __x86_64__
3055 return sse2_memmove(dst, src, n);
3056 #else
3057 unsigned char *d = dst;
3058 const unsigned char *s = src;
3059 int sh1;
3061 #ifdef __i386__
3062 if (sse2_supported)
3063 return sse2_memmove(dst, src, n);
3064 #endif
3066 if (!n) return dst;
3068 if ((size_t)dst - (size_t)src >= n)
3070 for (; (size_t)d % sizeof(size_t) && n; n--) *d++ = *s++;
3072 sh1 = 8 * ((size_t)s % sizeof(size_t));
3073 if (!sh1)
3075 while (n >= sizeof(size_t))
3077 *(size_t*)d = *(size_t*)s;
3078 s += sizeof(size_t);
3079 d += sizeof(size_t);
3080 n -= sizeof(size_t);
3083 else if (n >= 2 * sizeof(size_t))
3085 int sh2 = 8 * sizeof(size_t) - sh1;
3086 size_t x, y;
3088 s -= sh1 / 8;
3089 x = *(size_t*)s;
3092 s += sizeof(size_t);
3093 y = *(size_t*)s;
3094 *(size_t*)d = MERGE(x, sh1, y, sh2);
3095 d += sizeof(size_t);
3097 s += sizeof(size_t);
3098 x = *(size_t*)s;
3099 *(size_t*)d = MERGE(y, sh1, x, sh2);
3100 d += sizeof(size_t);
3102 n -= 2 * sizeof(size_t);
3103 } while (n >= 2 * sizeof(size_t));
3104 s += sh1 / 8;
3106 while (n--) *d++ = *s++;
3107 return dst;
3109 else
3111 d += n;
3112 s += n;
3114 for (; (size_t)d % sizeof(size_t) && n; n--) *--d = *--s;
3116 sh1 = 8 * ((size_t)s % sizeof(size_t));
3117 if (!sh1)
3119 while (n >= sizeof(size_t))
3121 s -= sizeof(size_t);
3122 d -= sizeof(size_t);
3123 *(size_t*)d = *(size_t*)s;
3124 n -= sizeof(size_t);
3127 else if (n >= 2 * sizeof(size_t))
3129 int sh2 = 8 * sizeof(size_t) - sh1;
3130 size_t x, y;
3132 s -= sh1 / 8;
3133 x = *(size_t*)s;
3136 s -= sizeof(size_t);
3137 y = *(size_t*)s;
3138 d -= sizeof(size_t);
3139 *(size_t*)d = MERGE(y, sh1, x, sh2);
3141 s -= sizeof(size_t);
3142 x = *(size_t*)s;
3143 d -= sizeof(size_t);
3144 *(size_t*)d = MERGE(x, sh1, y, sh2);
3146 n -= 2 * sizeof(size_t);
3147 } while (n >= 2 * sizeof(size_t));
3148 s += sh1 / 8;
3150 while (n--) *--d = *--s;
3152 return dst;
3153 #endif
3155 #undef MERGE
3157 /*********************************************************************
3158 * memcpy (MSVCRT.@)
3160 void * __cdecl memcpy(void *dst, const void *src, size_t n)
3162 return memmove(dst, src, n);
3165 /*********************************************************************
3166 * _memccpy (MSVCRT.@)
3168 void * __cdecl _memccpy(void *dst, const void *src, int c, size_t n)
3170 unsigned char *d = dst;
3171 const unsigned char *s = src;
3172 while (n--) if ((*d++ = *s++) == (unsigned char)c) return d;
3173 return NULL;
3177 static inline void memset_aligned_32(unsigned char *d, uint64_t v, size_t n)
3179 unsigned char *end = d + n;
3180 while (d < end)
3182 *(uint64_t *)(d + 0) = v;
3183 *(uint64_t *)(d + 8) = v;
3184 *(uint64_t *)(d + 16) = v;
3185 *(uint64_t *)(d + 24) = v;
3186 d += 32;
3190 /*********************************************************************
3191 * memset (MSVCRT.@)
3193 void *__cdecl memset(void *dst, int c, size_t n)
3195 typedef uint64_t DECLSPEC_ALIGN(1) unaligned_ui64;
3196 typedef uint32_t DECLSPEC_ALIGN(1) unaligned_ui32;
3197 typedef uint16_t DECLSPEC_ALIGN(1) unaligned_ui16;
3199 uint64_t v = 0x101010101010101ull * (unsigned char)c;
3200 unsigned char *d = (unsigned char *)dst;
3201 size_t a = 0x20 - ((uintptr_t)d & 0x1f);
3203 if (n >= 16)
3205 *(unaligned_ui64 *)(d + 0) = v;
3206 *(unaligned_ui64 *)(d + 8) = v;
3207 *(unaligned_ui64 *)(d + n - 16) = v;
3208 *(unaligned_ui64 *)(d + n - 8) = v;
3209 if (n <= 32) return dst;
3210 *(unaligned_ui64 *)(d + 16) = v;
3211 *(unaligned_ui64 *)(d + 24) = v;
3212 *(unaligned_ui64 *)(d + n - 32) = v;
3213 *(unaligned_ui64 *)(d + n - 24) = v;
3214 if (n <= 64) return dst;
3216 n = (n - a) & ~0x1f;
3217 memset_aligned_32(d + a, v, n);
3218 return dst;
3220 if (n >= 8)
3222 *(unaligned_ui64 *)d = v;
3223 *(unaligned_ui64 *)(d + n - 8) = v;
3224 return dst;
3226 if (n >= 4)
3228 *(unaligned_ui32 *)d = v;
3229 *(unaligned_ui32 *)(d + n - 4) = v;
3230 return dst;
3232 if (n >= 2)
3234 *(unaligned_ui16 *)d = v;
3235 *(unaligned_ui16 *)(d + n - 2) = v;
3236 return dst;
3238 if (n >= 1)
3240 *(uint8_t *)d = v;
3241 return dst;
3243 return dst;
3246 /*********************************************************************
3247 * strchr (MSVCRT.@)
3249 char* __cdecl strchr(const char *str, int c)
3253 if (*str == (char)c) return (char*)str;
3254 } while (*str++);
3255 return NULL;
3258 /*********************************************************************
3259 * strrchr (MSVCRT.@)
3261 char* __cdecl strrchr(const char *str, int c)
3263 char *ret = NULL;
3264 do { if (*str == (char)c) ret = (char*)str; } while (*str++);
3265 return ret;
3268 /*********************************************************************
3269 * memchr (MSVCRT.@)
3271 void* __cdecl memchr(const void *ptr, int c, size_t n)
3273 const unsigned char *p = ptr;
3275 for (p = ptr; n; n--, p++) if (*p == (unsigned char)c) return (void *)(ULONG_PTR)p;
3276 return NULL;
3279 /*********************************************************************
3280 * strcmp (MSVCRT.@)
3282 int __cdecl strcmp(const char *str1, const char *str2)
3284 while (*str1 && *str1 == *str2) { str1++; str2++; }
3285 if ((unsigned char)*str1 > (unsigned char)*str2) return 1;
3286 if ((unsigned char)*str1 < (unsigned char)*str2) return -1;
3287 return 0;
3290 /*********************************************************************
3291 * strncmp (MSVCRT.@)
3293 int __cdecl strncmp(const char *str1, const char *str2, size_t len)
3295 if (!len) return 0;
3296 while (--len && *str1 && *str1 == *str2) { str1++; str2++; }
3298 #if defined(_WIN64) || defined(_UCRT) || _MSVCR_VER == 70 || _MSVCR_VER == 71 || _MSVCR_VER >= 110
3299 if ((unsigned char)*str1 > (unsigned char)*str2) return 1;
3300 if ((unsigned char)*str1 < (unsigned char)*str2) return -1;
3301 return 0;
3302 #else
3303 return (unsigned char)*str1 - (unsigned char)*str2;
3304 #endif
3307 /*********************************************************************
3308 * _strnicmp_l (MSVCRT.@)
3310 int __cdecl _strnicmp_l(const char *s1, const char *s2,
3311 size_t count, _locale_t locale)
3313 pthreadlocinfo locinfo;
3314 int c1, c2;
3316 if(s1==NULL || s2==NULL)
3317 return _NLSCMPERROR;
3319 if(!count)
3320 return 0;
3322 if(!locale)
3323 locinfo = get_locinfo();
3324 else
3325 locinfo = locale->locinfo;
3327 if(!locinfo->lc_handle[LC_CTYPE])
3329 do {
3330 if ((c1 = *s1++) >= 'A' && c1 <= 'Z')
3331 c1 -= 'A' - 'a';
3332 if ((c2 = *s2++) >= 'A' && c2 <= 'Z')
3333 c2 -= 'A' - 'a';
3334 }while(--count && c1 && c1==c2);
3336 return c1-c2;
3339 do {
3340 c1 = _tolower_l((unsigned char)*s1++, locale);
3341 c2 = _tolower_l((unsigned char)*s2++, locale);
3342 }while(--count && c1 && c1==c2);
3344 return c1-c2;
3347 /*********************************************************************
3348 * _stricmp_l (MSVCRT.@)
3350 int __cdecl _stricmp_l(const char *s1, const char *s2, _locale_t locale)
3352 return _strnicmp_l(s1, s2, -1, locale);
3355 /*********************************************************************
3356 * _strnicmp (MSVCRT.@)
3358 int __cdecl _strnicmp(const char *s1, const char *s2, size_t count)
3360 return _strnicmp_l(s1, s2, count, NULL);
3363 /*********************************************************************
3364 * _stricmp (MSVCRT.@)
3366 int __cdecl _stricmp(const char *s1, const char *s2)
3368 return _strnicmp_l(s1, s2, -1, NULL);
3371 /*********************************************************************
3372 * strstr (MSVCRT.@)
3374 char* __cdecl strstr(const char *haystack, const char *needle)
3376 size_t i, j, len, needle_len, lps_len;
3377 BYTE lps[256];
3379 needle_len = strlen(needle);
3380 if (!needle_len) return (char*)haystack;
3381 lps_len = needle_len > ARRAY_SIZE(lps) ? ARRAY_SIZE(lps) : needle_len;
3383 lps[0] = 0;
3384 len = 0;
3385 i = 1;
3386 while (i < lps_len)
3388 if (needle[i] == needle[len]) lps[i++] = ++len;
3389 else if (len) len = lps[len-1];
3390 else lps[i++] = 0;
3393 i = j = 0;
3394 while (haystack[i])
3396 while (j < lps_len && haystack[i] && haystack[i] == needle[j])
3398 i++;
3399 j++;
3402 if (j == needle_len) return (char*)haystack + i - j;
3403 else if (j)
3405 if (j == ARRAY_SIZE(lps) && !strncmp(haystack + i, needle + j, needle_len - j))
3406 return (char*)haystack + i - j;
3407 j = lps[j-1];
3409 else if (haystack[i]) i++;
3411 return NULL;
3414 /*********************************************************************
3415 * _memicmp_l (MSVCRT.@)
3417 int __cdecl _memicmp_l(const void *v1, const void *v2, size_t len, _locale_t locale)
3419 const char *s1 = v1, *s2 = v2;
3420 int ret = 0;
3422 #if _MSVCR_VER == 0 || _MSVCR_VER >= 80
3423 if (!s1 || !s2)
3425 if (len)
3426 MSVCRT_INVALID_PMT(NULL, EINVAL);
3427 return len ? _NLSCMPERROR : 0;
3429 #endif
3431 while (len--)
3433 if ((ret = _tolower_l(*s1, locale) - _tolower_l(*s2, locale)))
3434 break;
3435 s1++;
3436 s2++;
3438 return ret;
3441 /*********************************************************************
3442 * _memicmp (MSVCRT.@)
3444 int __cdecl _memicmp(const void *s1, const void *s2, size_t len)
3446 return _memicmp_l(s1, s2, len, NULL);
3449 /*********************************************************************
3450 * strcspn (MSVCRT.@)
3452 size_t __cdecl strcspn(const char *str, const char *reject)
3454 BOOL rejects[256];
3455 const char *p;
3457 memset(rejects, 0, sizeof(rejects));
3459 p = reject;
3460 while(*p)
3462 rejects[(unsigned char)*p] = TRUE;
3463 p++;
3466 p = str;
3467 while(*p && !rejects[(unsigned char)*p]) p++;
3468 return p - str;
3471 /*********************************************************************
3472 * strspn (MSVCRT.@)
3474 size_t __cdecl strspn(const char *str, const char *accept)
3476 const char *ptr;
3477 for (ptr = str; *ptr; ptr++) if (!strchr( accept, *ptr )) break;
3478 return ptr - str;
3481 /*********************************************************************
3482 * strpbrk (MSVCRT.@)
3484 char* __cdecl strpbrk(const char *str, const char *accept)
3486 for (; *str; str++) if (strchr( accept, *str )) return (char*)str;
3487 return NULL;
3490 /*********************************************************************
3491 * __strncnt (MSVCRT.@)
3493 size_t __cdecl __strncnt(const char *str, size_t size)
3495 size_t ret = 0;
3497 #if _MSVCR_VER >= 140
3498 while (*str++ && size--)
3499 #else
3500 while (size-- && *str++)
3501 #endif
3503 ret++;
3506 return ret;
3510 #ifdef _CRTDLL
3511 /*********************************************************************
3512 * _strdec (CRTDLL.@)
3514 char * CDECL _strdec(const char *str1, const char *str2)
3516 return (char *)(str2 - 1);
3519 /*********************************************************************
3520 * _strinc (CRTDLL.@)
3522 char * CDECL _strinc(const char *str)
3524 return (char *)(str + 1);
3527 /*********************************************************************
3528 * _strnextc (CRTDLL.@)
3530 unsigned int CDECL _strnextc(const char *str)
3532 return (unsigned char)str[0];
3535 /*********************************************************************
3536 * _strninc (CRTDLL.@)
3538 char * CDECL _strninc(const char *str, size_t len)
3540 return (char *)(str + len);
3543 /*********************************************************************
3544 * _strspnp (CRTDLL.@)
3546 char * CDECL _strspnp( const char *str1, const char *str2)
3548 str1 += strspn( str1, str2 );
3549 return *str1 ? (char*)str1 : NULL;
3551 #endif