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
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt
);
39 /*********************************************************************
43 char* CDECL
_strdup(const char* str
)
47 char * ret
= malloc(strlen(str
)+1);
48 if (ret
) strcpy( ret
, str
);
54 /*********************************************************************
55 * _strlwr_s_l (MSVCRT.@)
57 int CDECL
_strlwr_s_l(char *str
, size_t len
, _locale_t locale
)
59 pthreadlocinfo locinfo
;
82 locinfo
= get_locinfo();
84 locinfo
= locale
->locinfo
;
86 if(!locinfo
->lc_handle
[LC_CTYPE
])
90 if (*str
>= 'A' && *str
<= 'Z')
99 *str
= _tolower_l((unsigned char)*str
, locale
);
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
);
124 /*********************************************************************
127 char* CDECL
_strlwr(char *str
)
129 _strlwr_s_l(str
, -1, NULL
);
133 /*********************************************************************
134 * _strupr_s_l (MSVCRT.@)
136 int CDECL
_strupr_s_l(char *str
, size_t len
, _locale_t locale
)
138 pthreadlocinfo locinfo
;
161 locinfo
= get_locinfo();
163 locinfo
= locale
->locinfo
;
165 if(!locinfo
->lc_handle
[LC_CTYPE
])
169 if (*str
>= 'a' && *str
<= 'z')
178 *str
= _toupper_l((unsigned char)*str
, locale
);
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
);
203 /*********************************************************************
206 char* CDECL
_strupr(char *str
)
208 _strupr_s_l(str
, -1, NULL
);
212 /*********************************************************************
213 * _strnset_s (MSVCRT.@)
215 int CDECL
_strnset_s(char *str
, size_t size
, int c
, size_t count
)
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;
228 if(!str
[i
]) return 0;
231 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
236 /*********************************************************************
237 * _strnset (MSVCRT.@)
239 char* CDECL
_strnset(char* str
, int value
, size_t len
)
242 while (*str
&& len
--)
247 /*********************************************************************
250 char* CDECL
_strrev(char* str
)
256 for (p1
= str
, p2
= str
+ strlen(str
) - 1; p2
> p1
; ++p1
, --p2
)
266 /*********************************************************************
269 char* CDECL
_strset(char* str
, int value
)
278 /*********************************************************************
281 char * CDECL
strtok( char *str
, const char *delim
)
283 thread_data_t
*data
= msvcrt_get_thread_data();
287 if (!(str
= data
->strtok_next
)) return NULL
;
289 while (*str
&& strchr( delim
, *str
)) str
++;
292 data
->strtok_next
= str
;
296 while (*str
&& !strchr( delim
, *str
)) str
++;
297 if (*str
) *str
++ = 0;
298 data
->strtok_next
= str
;
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
;
314 while(*str
&& strchr(delim
, *str
))
323 while(**ctx
&& !strchr(delim
, **ctx
))
331 /*********************************************************************
334 void CDECL
_swab(char* src
, char* dst
, int len
)
338 len
= (unsigned)len
>> 1;
350 static struct fpnum
fpnum(int sign
, int exp
, ULONGLONG m
, enum fpmod mod
)
361 int fpnum_double(struct fpnum
*fp
, double *d
)
365 if (fp
->mod
== FP_VAL_INFINITY
)
367 *d
= fp
->sign
* INFINITY
;
371 if (fp
->mod
== FP_VAL_NAN
)
375 bits
&= ~((ULONGLONG
)1 << (MANT_BITS
+ EXP_BITS
- 1));
376 *d
= *(double*)&bits
;
380 TRACE("%c %#I64x *2^%d (round %d)\n", fp
->sign
== -1 ? '-' : '+',
381 fp
->m
, fp
->exp
, fp
->mod
);
388 /* make sure that we don't overflow modifying exponent */
389 if (fp
->exp
> 1<<EXP_BITS
)
391 *d
= fp
->sign
* INFINITY
;
394 if (fp
->exp
< -(1<<EXP_BITS
))
399 fp
->exp
+= MANT_BITS
- 1;
401 /* normalize mantissa */
402 while(fp
->m
< (ULONGLONG
)1 << (MANT_BITS
-1))
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
;
418 fp
->exp
+= (1 << (EXP_BITS
-1)) - 1;
420 /* handle subnormals */
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
;
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
;
438 if (fp
->mod
== FP_ROUND_UP
|| (fp
->mod
== FP_ROUND_EVEN
&& fp
->m
& 1))
442 /* handle subnormal that falls into regular range due to rounding */
443 if (fp
->m
== (ULONGLONG
)1 << (MANT_BITS
- 1))
447 else if (fp
->m
>= (ULONGLONG
)1 << MANT_BITS
)
454 if (fp
->exp
>= (1<<EXP_BITS
)-1)
456 *d
= fp
->sign
* INFINITY
;
459 if (!fp
->m
|| fp
->exp
< 0)
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
;
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
)
482 d
->x80
[1] = 0x80000000;
483 d
->x80
[2] = (1 << LDBL_EXP_BITS
) - 1;
485 d
->x80
[2] |= 1 << LDBL_EXP_BITS
;
489 if (fp
->mod
== FP_VAL_NAN
)
493 d
->x80
[2] = (1 << LDBL_EXP_BITS
) - 1;
495 d
->x80
[2] |= 1 << LDBL_EXP_BITS
;
499 TRACE("%c %#I64x *2^%d (round %d)\n", fp
->sign
== -1 ? '-' : '+',
500 fp
->m
, fp
->exp
, fp
->mod
);
507 d
->x80
[2] |= 1 << LDBL_EXP_BITS
;
511 /* make sure that we don't overflow modifying exponent */
512 if (fp
->exp
> 1<<LDBL_EXP_BITS
)
515 d
->x80
[1] = 0x80000000;
516 d
->x80
[2] = (1 << LDBL_EXP_BITS
) - 1;
518 d
->x80
[2] |= 1 << LDBL_EXP_BITS
;
521 if (fp
->exp
< -(1<<LDBL_EXP_BITS
))
527 d
->x80
[2] |= 1 << LDBL_EXP_BITS
;
530 fp
->exp
+= LDBL_MANT_BITS
- 1;
532 /* normalize mantissa */
533 while(fp
->m
< (ULONGLONG
)1 << (LDBL_MANT_BITS
-1))
538 fp
->exp
+= (1 << (LDBL_EXP_BITS
-1)) - 1;
540 /* handle subnormals */
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
;
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
;
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);
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)
577 d
->x80
[1] = 0x80000000;
578 d
->x80
[2] = (1 << LDBL_EXP_BITS
) - 1;
580 d
->x80
[2] |= 1 << LDBL_EXP_BITS
;
583 if (!fp
->m
|| fp
->exp
< 0)
589 d
->x80
[2] |= 1 << LDBL_EXP_BITS
;
594 d
->x80
[1] = fp
->m
>> 32;
597 d
->x80
[2] |= 1 << LDBL_EXP_BITS
;
601 #if _MSVCR_VER >= 140
603 static inline int hex2int(char c
)
605 if (c
>= '0' && c
<= '9')
607 else if (c
>= 'a' && c
<= 'f')
609 else if (c
>= 'A' && c
<= 'F')
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
;
624 while(m
< UI64_MAX
/16)
627 if (val
== -1) break;
636 if (val
== -1) break;
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
)
653 else if (!found_digit
)
655 if(nch
!=WEOF
) unget(ctx
);
657 return fpnum(0, 0, 0, 0);
660 while(m
<= UI64_MAX
/16)
663 if (val
== -1) break;
673 if (val
== -1) break;
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
;
686 if (nch
!= WEOF
) unget(ctx
);
687 if (found_dp
) unget(ctx
);
689 return fpnum(0, 0, 0, 0);
692 if(nch
=='p' || nch
=='P') {
693 BOOL found_sign
= FALSE
;
701 } else if(nch
== '+') {
705 if(nch
>='0' && nch
<='9') {
706 while(nch
>='0' && nch
<='9') {
707 if(e
>INT_MAX
/10 || e
*10>INT_MAX
-nch
+'0')
713 if((nch
!=WEOF
) && (nch
< '0' || nch
> '9')) unget(ctx
);
716 if(e
<0 && exp
<INT_MIN
-e
) exp
= INT_MIN
;
717 else if(e
>0 && exp
>INT_MAX
-e
) exp
= INT_MAX
;
720 if(nch
!= WEOF
) unget(ctx
);
721 if(found_sign
) unget(ctx
);
726 return fpnum(sign
, exp
, m
, round
);
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)];
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
;
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
;
765 } else if(nch
== '+') {
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]))
775 while(str_match
&& nch
!= WEOF
&&
776 (nch
== str_match
[matched
] || nch
== _toupper(str_match
[matched
]))) {
782 if(matched
>= 8) keep
= 8;
783 else if(matched
>= 3) keep
= 3;
784 if(nch
!= WEOF
) unget(ctx
);
785 for (; matched
> keep
; matched
--) {
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
) {
797 return fpnum(0, 0, 0, 0);
803 if(nch
== 'x' || nch
== 'X')
804 return fpnum_parse16(get
, unget
, ctx
, sign
, locinfo
);
816 while(nch
>='0' && nch
<='9') {
818 if(limb_digits
== LIMB_DIGITS
) {
819 if(bnum_idx(b
, b
->b
-1) == bnum_idx(b
, b
->e
)) break;
822 b
->data
[bnum_idx(b
, b
->b
)] = 0;
827 b
->data
[bnum_idx(b
, b
->b
)] = b
->data
[bnum_idx(b
, b
->b
)] * 10 + nch
- '0';
832 while(nch
>='0' && nch
<='9') {
833 if(nch
!= '0') b
->data
[bnum_idx(b
, b
->b
)] |= 1;
838 if(nch
== *locinfo
->lconv
->decimal_point
) {
843 /* skip leading '0' */
844 if(nch
=='0' && !limb_digits
&& !b
->b
) {
852 while(nch
>='0' && nch
<='9') {
854 if(limb_digits
== LIMB_DIGITS
) {
855 if(bnum_idx(b
, b
->b
-1) == bnum_idx(b
, b
->e
)) break;
858 b
->data
[bnum_idx(b
, b
->b
)] = 0;
863 b
->data
[bnum_idx(b
, b
->b
)] = b
->data
[bnum_idx(b
, b
->b
)] * 10 + nch
- '0';
867 while(nch
>='0' && nch
<='9') {
868 if(nch
!= '0') b
->data
[bnum_idx(b
, b
->b
)] |= 1;
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') {
887 } else if(nch
== '+') {
894 if(nch
>='0' && nch
<='9') {
895 while(nch
>='0' && nch
<='9') {
896 if(e
>INT_MAX
/10 || e
*10>INT_MAX
-nch
+'0')
902 if(nch
!= WEOF
) unget(ctx
);
905 if(e
<0 && dp
<INT_MIN
-e
) dp
= INT_MIN
;
906 else if(e
>0 && dp
>INT_MAX
-e
) dp
= INT_MAX
;
909 if(nch
!= WEOF
) unget(ctx
);
910 if(found_sign
) unget(ctx
);
913 } else if(nch
!= WEOF
) {
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 */
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
;
947 while(dp
<= 2*LIMB_DIGITS
) {
948 if(bnum_lshift(b
, 29)) dp
+= LIMB_DIGITS
;
951 /* Make sure most significant mantissa bit will be set */
952 while(b
->data
[bnum_idx(b
, b
->e
-1)] <= 9) {
956 while(!bnum_to_mant(b
, &m
)) {
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
)
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
);
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
;
1002 static void strtod_str_unget(void *ctx
)
1004 const char **p
= ctx
;
1008 static inline double strtod_helper(const char *str
, char **end
, _locale_t locale
, int *perr
)
1010 pthreadlocinfo locinfo
;
1011 const char *beg
, *p
;
1016 if (perr
) *perr
= 0;
1021 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) {
1022 if (end
) *end
= NULL
;
1027 locinfo
= get_locinfo();
1029 locinfo
= locale
->locinfo
;
1032 while(_isspace_l((unsigned char)*p
, locale
))
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
;
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 /*********************************************************************
1056 double CDECL
strtod( const char *str
, char **end
)
1058 return _strtod_l( str
, end
, NULL
);
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
)) {
1071 if (!f
|| !isfinite(f
))
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 /*********************************************************************
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
)
1111 d
= strtod_helper(str
, NULL
, locale
, &err
);
1115 if((d
!=0 || err
) && value
->f
>-FLT_MIN
&& value
->f
<FLT_MIN
)
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
)
1135 value
->x
= strtod_helper(str
, NULL
, locale
, &err
);
1138 if((value
->x
!=0 || err
) && value
->x
>-DBL_MIN
&& value
->x
<DBL_MIN
)
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
;
1159 locinfo
= get_locinfo();
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
;
1184 locinfo
= get_locinfo();
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
;
1210 locinfo
= get_locinfo();
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
;
1237 locinfo
= get_locinfo();
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
)
1263 for(i
=0; i
<len
; i
++)
1264 if((dst
[i
] = src
[i
]) == '\0') break;
1266 while (i
< len
) dst
[i
++] = 0;
1271 /******************************************************************
1272 * strncpy_s (MSVCRT.@)
1274 int __cdecl
strncpy_s( char *dst
, size_t elem
, const char *src
, size_t count
)
1277 BOOL truncate
= (count
== _TRUNCATE
);
1279 TRACE("(%p %Iu %s %Iu)\n", dst
, elem
, debugstr_a(src
), count
);
1283 if (dst
&& elem
) *dst
= 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
))
1295 while (elem
&& count
&& *src
)
1301 if (!elem
&& truncate
)
1315 /*********************************************************************
1318 char* CDECL
strcpy(char *dst
, const char *src
)
1321 while ((*dst
++ = *src
++));
1325 /*********************************************************************
1326 * strcpy_s (MSVCRT.@)
1328 int CDECL
strcpy_s( char* dst
, size_t elem
, const char* src
)
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
))
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
);
1348 /*********************************************************************
1349 * strcat_s (MSVCRT.@)
1351 int CDECL
strcat_s( char* dst
, size_t elem
, const char* src
)
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
))
1362 for(i
= 0; i
< elem
; i
++)
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
);
1378 /*********************************************************************
1381 char* __cdecl
strcat( char *dst
, const char *src
)
1385 while ((*d
++ = *src
++));
1389 /*********************************************************************
1390 * strncat_s (MSVCRT.@)
1392 int CDECL
strncat_s( char* dst
, size_t elem
, const char* src
, size_t count
)
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
))
1406 for (i
= 0; i
< elem
; i
++) if (!dst
[i
]) break;
1410 MSVCRT_INVALID_PMT("dst[elem] is not NULL terminated\n", EINVAL
);
1415 for (j
= 0; (j
+ i
) < elem
; j
++)
1417 if(count
== _TRUNCATE
&& j
+ i
== elem
- 1)
1422 if(j
== count
|| (dst
[j
+ i
] = src
[j
]) == '\0')
1429 MSVCRT_INVALID_PMT("dst[elem] is too small", ERANGE
);
1434 /*********************************************************************
1435 * strncat (MSVCRT.@)
1437 char* __cdecl
strncat(char *dst
, const char *src
, size_t len
)
1441 for ( ; len
&& *src
; d
++, src
++, len
--) *d
= *src
;
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
;
1455 if(!MSVCRT_CHECK_PMT(src
)) return INT_MAX
;
1456 if(!MSVCRT_CHECK_PMT(dest
|| !len
)) return INT_MAX
;
1459 FIXME("len > INT_MAX not supported\n");
1464 locinfo
= get_locinfo();
1466 locinfo
= locale
->locinfo
;
1468 if(!locinfo
->lc_handle
[LC_COLLATE
]) {
1469 strncpy(dest
, src
, len
);
1473 ret
= LCMapStringA(locinfo
->lc_handle
[LC_COLLATE
],
1474 LCMAP_SORTKEY
, src
, -1, NULL
, 0);
1476 if(len
) dest
[0] = 0;
1480 if(!len
) 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
;
1511 if (flags
) FIXME("flags not supported: %x\n", flags
);
1514 locinfo
= get_locinfo();
1516 locinfo
= locale
->locinfo
;
1519 while (_isspace_l((unsigned char)*p
, locale
))
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);
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
)
1546 switch(__STRINGTOLD_L( value
, &endptr
, str
, 0, locale
))
1548 case 1: return _UNDERFLOW
;
1549 case 2: return _OVERFLOW
;
1554 /********************************************************************
1555 * _atoldbl (MSVCRT.@)
1557 int CDECL
_atoldbl(_LDOUBLE
*value
, char *str
)
1559 return _atoldbl_l( (MSVCRT__LDOUBLE
*)value
, str
, NULL
);
1562 /*********************************************************************
1565 size_t __cdecl
strlen(const char *str
)
1567 const char *s
= str
;
1572 /******************************************************************
1573 * strnlen (MSVCRT.@)
1575 size_t CDECL
strnlen(const char *s
, size_t maxlen
)
1579 for(i
=0; i
<maxlen
; 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
;
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
++;
1608 } else if(*nptr
== '+')
1611 if((base
==0 || base
==16) && *nptr
=='0' && _tolower_l(*(nptr
+1), locale
)=='x') {
1624 char cur
= _tolower_l(*nptr
, locale
);
1627 if(cur
>='0' && cur
<='9') {
1632 if(cur
<'a' || cur
>='a'+base
-10)
1643 if(!negative
&& (ret
>I64_MAX
/base
|| ret
*base
>I64_MAX
-v
)) {
1646 } else if(negative
&& (ret
<I64_MIN
/base
|| ret
*base
<I64_MIN
-v
)) {
1654 *endptr
= (char*)(got_digit
? nptr
: p
);
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
);
1677 } else if(ret
< INT_MIN
) {
1684 /*********************************************************************
1688 int __cdecl
atoi(const char *str
)
1696 while(_isspace_l((unsigned char)*str
, NULL
)) str
++;
1700 }else if(*str
== '-') {
1705 while(*str
>='0' && *str
<='9') {
1706 ret
= ret
*10+*str
-'0';
1710 return minus
? -ret
: ret
;
1713 int CDECL
atoi(const char *str
)
1715 return _atoi_l(str
, NULL
);
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
) {
1745 } else if(ret
< LONG_MIN
) {
1752 /******************************************************************
1755 __msvcrt_long CDECL
atol(const char *str
)
1760 return _atol_l(str
, NULL
);
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
) {
1795 } else if(ret
< LONG_MIN
) {
1803 /******************************************************************
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
) {
1821 }else if(ret
< -(__int64
)ULONG_MAX
) {
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
++;
1860 } else if(*nptr
== '+')
1863 if((base
==0 || base
==16) && *nptr
=='0' && _tolower_l(*(nptr
+1), locale
)=='x') {
1876 char cur
= _tolower_l(*nptr
, locale
);
1879 if(cur
>='0' && cur
<='9') {
1884 if(cur
<'a' || cur
>='a'+base
-10)
1892 if(ret
>UI64_MAX
/base
|| ret
*base
>UI64_MAX
-v
) {
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
)
1918 char buffer
[33], *pos
;
1921 if (value
< 0 && radix
== 10)
1928 is_negative
= FALSE
;
1937 digit
= val
% radix
;
1941 *--pos
= '0' + digit
;
1943 *--pos
= 'a' + digit
- 10;
1950 len
= buffer
+ 33 - pos
;
1956 /* Copy the temporary buffer backwards up to the available number of
1957 * characters. Don't copy the negative sign if present. */
1965 for (pos
= buffer
+ 31, i
= 0; i
< size
; i
++)
1969 MSVCRT_INVALID_PMT("str[size] is too small", ERANGE
);
1973 memcpy(str
, pos
, len
);
1977 static int ltow_helper(__msvcrt_long value
, wchar_t *str
, size_t size
, int radix
)
1982 wchar_t buffer
[33], *pos
;
1985 if (value
< 0 && radix
== 10)
1992 is_negative
= FALSE
;
2001 digit
= val
% radix
;
2005 *--pos
= '0' + digit
;
2007 *--pos
= 'a' + digit
- 10;
2014 len
= buffer
+ 33 - pos
;
2020 /* Copy the temporary buffer backwards up to the available number of
2021 * characters. Don't copy the negative sign if present. */
2029 for (pos
= buffer
+ 31, i
= 0; i
< size
; i
++)
2033 MSVCRT_INVALID_PMT("str[size] is too small", ERANGE
);
2037 memcpy(str
, pos
, len
* sizeof(wchar_t));
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))
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))
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 /*********************************************************************
2084 char* CDECL
_itoa(int value
, char *str
, int radix
)
2086 return ltoa_helper(value
, str
, SIZE_MAX
, radix
) ? NULL
: str
;
2089 /*********************************************************************
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 /*********************************************************************
2108 wchar_t* CDECL
_itow(int value
, wchar_t *str
, int radix
)
2110 return ltow_helper(value
, str
, SIZE_MAX
, radix
) ? NULL
: str
;
2113 /*********************************************************************
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 /*********************************************************************
2124 char* CDECL
_ultoa(__msvcrt_ulong value
, char *str
, int radix
)
2126 char buffer
[33], *pos
;
2132 int digit
= value
% radix
;
2136 *--pos
= '0' + digit
;
2138 *--pos
= 'a' + digit
- 10;
2139 } while (value
!= 0);
2141 memcpy(str
, pos
, buffer
+ 33 - pos
);
2145 /*********************************************************************
2146 * _ui64toa (MSVCRT.@)
2148 char* CDECL
_ui64toa(unsigned __int64 value
, char *str
, int radix
)
2150 char buffer
[65], *pos
;
2156 int digit
= value
% radix
;
2160 *--pos
= '0' + digit
;
2162 *--pos
= 'a' + digit
- 10;
2163 } while (value
!= 0);
2165 memcpy(str
, pos
, buffer
+ 65 - pos
);
2169 /*********************************************************************
2172 wchar_t* CDECL
_ultow(__msvcrt_ulong value
, wchar_t *str
, int radix
)
2174 wchar_t buffer
[33], *pos
;
2180 int digit
= value
% radix
;
2184 *--pos
= '0' + digit
;
2186 *--pos
= 'a' + digit
- 10;
2187 } while (value
!= 0);
2189 memcpy(str
, pos
, (buffer
+ 33 - pos
) * sizeof(wchar_t));
2193 /*********************************************************************
2194 * _ui64tow (MSVCRT.@)
2196 wchar_t* CDECL
_ui64tow(unsigned __int64 value
, wchar_t *str
, int radix
)
2198 wchar_t buffer
[65], *pos
;
2204 int digit
= value
% radix
;
2208 *--pos
= '0' + digit
;
2210 *--pos
= 'a' + digit
- 10;
2211 } while (value
!= 0);
2213 memcpy(str
, pos
, (buffer
+ 65 - pos
) * sizeof(wchar_t));
2217 /*********************************************************************
2218 * _i64toa (MSVCRT.@)
2220 char* CDECL
_i64toa(__int64 value
, char *str
, int radix
)
2222 unsigned __int64 val
;
2224 char buffer
[65], *pos
;
2226 if (value
< 0 && radix
== 10)
2233 is_negative
= FALSE
;
2242 int digit
= val
% radix
;
2246 *--pos
= '0' + digit
;
2248 *--pos
= 'a' + digit
- 10;
2255 memcpy(str
, pos
, buffer
+ 65 - pos
);
2259 /*********************************************************************
2260 * _i64tow (MSVCRT.@)
2262 wchar_t* CDECL
_i64tow(__int64 value
, wchar_t *str
, int radix
)
2264 unsigned __int64 val
;
2266 wchar_t buffer
[65], *pos
;
2268 if (value
< 0 && radix
== 10)
2275 is_negative
= FALSE
;
2284 int digit
= val
% radix
;
2288 *--pos
= '0' + digit
;
2290 *--pos
= 'a' + digit
- 10;
2297 memcpy(str
, pos
, (buffer
+ 65 - pos
) * sizeof(wchar_t));
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
;
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))
2322 digit
= value
%radix
;
2328 *--pos
= 'a'+digit
-10;
2331 if(buffer
-pos
+65 > size
) {
2332 MSVCRT_INVALID_PMT("str[size] is too small", EINVAL
);
2336 memcpy(str
, pos
, buffer
-pos
+65);
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
;
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))
2361 digit
= value
% radix
;
2362 value
= value
/ radix
;
2364 *--pos
= '0' + digit
;
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
);
2374 memcpy(str
, pos
, (buffer
-pos
+65)*sizeof(wchar_t));
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
;
2387 if (!str
|| !size
|| radix
< 2 || radix
> 36)
2401 digit
= value
% radix
;
2405 *--pos
= '0' + digit
;
2407 *--pos
= 'a' + digit
- 10;
2411 len
= buffer
+ 33 - pos
;
2417 /* Copy the temporary buffer backwards up to the available number of
2420 for (pos
= buffer
+ 31, i
= 0; i
< size
; i
++)
2428 memcpy(str
, pos
, len
);
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
;
2441 if (!str
|| !size
|| radix
< 2 || radix
> 36)
2455 digit
= value
% radix
;
2459 *--pos
= '0' + digit
;
2461 *--pos
= 'a' + digit
- 10;
2465 len
= buffer
+ 33 - pos
;
2471 /* Copy the temporary buffer backwards up to the available number of
2474 for (pos
= buffer
+ 31, i
= 0; i
< size
; i
++)
2482 memcpy(str
, pos
, len
* sizeof(wchar_t));
2486 /*********************************************************************
2487 * _i64toa_s (MSVCRT.@)
2489 int CDECL
_i64toa_s(__int64 value
, char *str
, size_t size
, int radix
)
2491 unsigned __int64 val
;
2494 char buffer
[65], *pos
;
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))
2505 if (value
< 0 && radix
== 10)
2512 is_negative
= FALSE
;
2521 digit
= val
% radix
;
2525 *--pos
= '0' + digit
;
2527 *--pos
= 'a' + digit
- 10;
2534 len
= buffer
+ 65 - pos
;
2540 /* Copy the temporary buffer backwards up to the available number of
2541 * characters. Don't copy the negative sign if present. */
2549 for (pos
= buffer
+ 63, i
= 0; i
< size
; i
++)
2553 MSVCRT_INVALID_PMT("str[size] is too small", ERANGE
);
2557 memcpy(str
, pos
, len
);
2561 /*********************************************************************
2562 * _i64tow_s (MSVCRT.@)
2564 int CDECL
_i64tow_s(__int64 value
, wchar_t *str
, size_t size
, int radix
)
2566 unsigned __int64 val
;
2569 wchar_t buffer
[65], *pos
;
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))
2580 if (value
< 0 && radix
== 10)
2587 is_negative
= FALSE
;
2596 digit
= val
% radix
;
2600 *--pos
= '0' + digit
;
2602 *--pos
= 'a' + digit
- 10;
2609 len
= buffer
+ 65 - pos
;
2615 /* Copy the temporary buffer backwards up to the available number of
2616 * characters. Don't copy the negative sign if present. */
2624 for (pos
= buffer
+ 63, i
= 0; i
< size
; i
++)
2628 MSVCRT_INVALID_PMT("str[size] is too small", ERANGE
);
2632 memcpy(str
, pos
, len
* sizeof(wchar_t));
2636 #define I10_OUTPUT_MAX_PREC 21
2637 /* Internal structure used by $I10_OUTPUT */
2638 struct _I10_OUTPUT_DATA
{
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
2653 * 0 if given double is NaN or INF
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
)
2665 char buf
[I10_OUTPUT_MAX_PREC
+9]; /* 9 = strlen("0.e+0000") + '\0' */
2668 if ((ld80
.x80
[2] & 0x7fff) == 0x7fff)
2670 if (ld80
.x80
[0] == 0 && ld80
.x80
[1] == 0x80000000)
2671 strcpy( data
->str
, "1#INF" );
2673 strcpy( data
->str
, (ld80
.x80
[1] & 0x40000000) ? "1#QNAN" : "1#SNAN" );
2675 data
->sign
= (ld80
.x80
[2] & 0x8000) ? '-' : ' ';
2676 data
->len
= strlen(data
->str
);
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
);
2694 int exp
= 1 + floor(log10(d
));
2702 if(prec
+1 > I10_OUTPUT_MAX_PREC
)
2703 prec
= I10_OUTPUT_MAX_PREC
-1;
2709 sprintf(format
, "%%.%dle", prec
);
2710 sprintf(buf
, format
, d
);
2713 data
->pos
= atoi(buf
+prec
+3);
2717 for(p
= buf
+prec
+1; p
>buf
+1 && *p
=='0'; p
--);
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);
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
++)
2737 return *p1
> *p2
? 1 : -1;
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);
2754 return memcmp_bytes(p1
, p2
, sizeof(uint64_t));
2761 return memcmp_bytes(p1
, p2
, remainder
);
2764 /*********************************************************************
2767 int __cdecl
memcmp(const void *ptr1
, const void *ptr2
, size_t n
)
2769 const unsigned char *p1
= ptr1
, *p2
= ptr2
;
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
)))
2785 return memcmp_blocks(p1
, p2
, n
);
2788 #if defined(__i386__) || defined(__x86_64__)
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")
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")
2839 void * __cdecl
sse2_memmove(void *dst
, const void *src
, size_t n
);
2840 __ASM_GLOBAL_FUNC( sse2_memmove
,
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"
2847 "cmp $4, " LEN_REG
"\n\t" /* 4-bytes align */
2849 "mov " DEST_REG
", " TMP_REG
"\n\t"
2850 "shr $1, " TMP_REG
"\n\t"
2853 "dec " LEN_REG
"\n\t"
2854 "inc " TMP_REG
"\n\t"
2856 "shr $1, " TMP_REG
"\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"
2864 "shr $1, " TMP_REG
"\n\t"
2867 "sub $4, " LEN_REG
"\n\t"
2868 "inc " TMP_REG
"\n\t"
2870 "shr $1, " TMP_REG
"\n\t"
2874 "sub $8, " LEN_REG
"\n\t"
2876 "cmp $64, " LEN_REG
"\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"
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"
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"
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"
2917 "shr $1, " TMP_REG
"\n\t"
2921 "copy_fwd3:\n\t" /* copy last 3 bytes */
2922 "shr $1, " LEN_REG
"\n\t"
2926 "shr $1, " LEN_REG
"\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 */
2937 "mov " DEST_REG
", " TMP_REG
"\n\t"
2938 "shr $1, " TMP_REG
"\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"
2946 "shr $1, " TMP_REG
"\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"
2956 "shr $1, " TMP_REG
"\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"
2964 "shr $1, " TMP_REG
"\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"
2974 "cmp $64, " LEN_REG
"\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"
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"
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"
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"
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"
3018 "shr $1, " TMP_REG
"\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"
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"
3034 "shr $1, " LEN_REG
"\n\t"
3036 "movw -2(" SRC_REG
"), %ax\n\t"
3037 "movw %ax, -2(" DEST_REG
")\n\t"
3044 /*********************************************************************
3045 * memmove (MSVCRT.@)
3047 #ifdef WORDS_BIGENDIAN
3048 # define MERGE(w1, sh1, w2, sh2) ((w1 << sh1) | (w2 >> sh2))
3050 # define MERGE(w1, sh1, w2, sh2) ((w1 >> sh1) | (w2 << sh2))
3052 void * __cdecl
memmove(void *dst
, const void *src
, size_t n
)
3055 return sse2_memmove(dst
, src
, n
);
3057 unsigned char *d
= dst
;
3058 const unsigned char *s
= src
;
3063 return sse2_memmove(dst
, src
, n
);
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));
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
;
3092 s
+= sizeof(size_t);
3094 *(size_t*)d
= MERGE(x
, sh1
, y
, sh2
);
3095 d
+= sizeof(size_t);
3097 s
+= sizeof(size_t);
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));
3106 while (n
--) *d
++ = *s
++;
3114 for (; (size_t)d
% sizeof(size_t) && n
; n
--) *--d
= *--s
;
3116 sh1
= 8 * ((size_t)s
% sizeof(size_t));
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
;
3136 s
-= sizeof(size_t);
3138 d
-= sizeof(size_t);
3139 *(size_t*)d
= MERGE(y
, sh1
, x
, sh2
);
3141 s
-= sizeof(size_t);
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));
3150 while (n
--) *--d
= *--s
;
3157 /*********************************************************************
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
;
3177 static inline void memset_aligned_32(unsigned char *d
, uint64_t v
, size_t n
)
3179 unsigned char *end
= d
+ n
;
3182 *(uint64_t *)(d
+ 0) = v
;
3183 *(uint64_t *)(d
+ 8) = v
;
3184 *(uint64_t *)(d
+ 16) = v
;
3185 *(uint64_t *)(d
+ 24) = v
;
3190 /*********************************************************************
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);
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
);
3222 *(unaligned_ui64
*)d
= v
;
3223 *(unaligned_ui64
*)(d
+ n
- 8) = v
;
3228 *(unaligned_ui32
*)d
= v
;
3229 *(unaligned_ui32
*)(d
+ n
- 4) = v
;
3234 *(unaligned_ui16
*)d
= v
;
3235 *(unaligned_ui16
*)(d
+ n
- 2) = v
;
3246 /*********************************************************************
3249 char* __cdecl
strchr(const char *str
, int c
)
3253 if (*str
== (char)c
) return (char*)str
;
3258 /*********************************************************************
3259 * strrchr (MSVCRT.@)
3261 char* __cdecl
strrchr(const char *str
, int c
)
3264 do { if (*str
== (char)c
) ret
= (char*)str
; } while (*str
++);
3268 /*********************************************************************
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
;
3279 /*********************************************************************
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;
3290 /*********************************************************************
3291 * strncmp (MSVCRT.@)
3293 int __cdecl
strncmp(const char *str1
, const char *str2
, size_t len
)
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;
3303 return (unsigned char)*str1
- (unsigned char)*str2
;
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
;
3316 if(s1
==NULL
|| s2
==NULL
)
3317 return _NLSCMPERROR
;
3323 locinfo
= get_locinfo();
3325 locinfo
= locale
->locinfo
;
3327 if(!locinfo
->lc_handle
[LC_CTYPE
])
3330 if ((c1
= *s1
++) >= 'A' && c1
<= 'Z')
3332 if ((c2
= *s2
++) >= 'A' && c2
<= 'Z')
3334 }while(--count
&& c1
&& c1
==c2
);
3340 c1
= _tolower_l((unsigned char)*s1
++, locale
);
3341 c2
= _tolower_l((unsigned char)*s2
++, locale
);
3342 }while(--count
&& c1
&& 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 /*********************************************************************
3374 char* __cdecl
strstr(const char *haystack
, const char *needle
)
3376 size_t i
, j
, len
, needle_len
, lps_len
;
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
;
3388 if (needle
[i
] == needle
[len
]) lps
[i
++] = ++len
;
3389 else if (len
) len
= lps
[len
-1];
3396 while (j
< lps_len
&& haystack
[i
] && haystack
[i
] == needle
[j
])
3402 if (j
== needle_len
) return (char*)haystack
+ i
- j
;
3405 if (j
== ARRAY_SIZE(lps
) && !strncmp(haystack
+ i
, needle
+ j
, needle_len
- j
))
3406 return (char*)haystack
+ i
- j
;
3409 else if (haystack
[i
]) i
++;
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
;
3422 #if _MSVCR_VER == 0 || _MSVCR_VER >= 80
3426 MSVCRT_INVALID_PMT(NULL
, EINVAL
);
3427 return len
? _NLSCMPERROR
: 0;
3433 if ((ret
= _tolower_l(*s1
, locale
) - _tolower_l(*s2
, locale
)))
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
)
3457 memset(rejects
, 0, sizeof(rejects
));
3462 rejects
[(unsigned char)*p
] = TRUE
;
3467 while(*p
&& !rejects
[(unsigned char)*p
]) p
++;
3471 /*********************************************************************
3474 size_t __cdecl
strspn(const char *str
, const char *accept
)
3477 for (ptr
= str
; *ptr
; ptr
++) if (!strchr( accept
, *ptr
)) break;
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
;
3490 /*********************************************************************
3491 * __strncnt (MSVCRT.@)
3493 size_t __cdecl
__strncnt(const char *str
, size_t size
)
3497 #if _MSVCR_VER >= 140
3498 while (*str
++ && size
--)
3500 while (size
-- && *str
++)
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
;