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 /*********************************************************************
1274 char* CDECL
strcpy(char *dst
, const char *src
)
1277 while ((*dst
++ = *src
++));
1281 /*********************************************************************
1282 * strcpy_s (MSVCRT.@)
1284 int CDECL
strcpy_s( char* dst
, size_t elem
, const char* src
)
1287 if(!elem
) return EINVAL
;
1288 if(!dst
) return EINVAL
;
1295 for(i
= 0; i
< elem
; i
++)
1297 if((dst
[i
] = src
[i
]) == '\0') return 0;
1303 /*********************************************************************
1304 * strcat_s (MSVCRT.@)
1306 int CDECL
strcat_s( char* dst
, size_t elem
, const char* src
)
1309 if(!dst
) return EINVAL
;
1310 if(elem
== 0) return EINVAL
;
1317 for(i
= 0; i
< elem
; i
++)
1321 for(j
= 0; (j
+ i
) < elem
; j
++)
1323 if((dst
[j
+ i
] = src
[j
]) == '\0') return 0;
1327 /* Set the first element to 0, not the first element after the skipped part */
1332 /*********************************************************************
1335 char* __cdecl
strcat( char *dst
, const char *src
)
1339 while ((*d
++ = *src
++));
1343 /*********************************************************************
1344 * strncat_s (MSVCRT.@)
1346 int CDECL
strncat_s( char* dst
, size_t elem
, const char* src
, size_t count
)
1350 if (!MSVCRT_CHECK_PMT(dst
!= 0)) return EINVAL
;
1351 if (!MSVCRT_CHECK_PMT(elem
!= 0)) return EINVAL
;
1352 if (!MSVCRT_CHECK_PMT(src
!= 0))
1358 for(i
= 0; i
< elem
; i
++)
1362 for(j
= 0; (j
+ i
) < elem
; j
++)
1364 if(count
== _TRUNCATE
&& j
+ i
== elem
- 1)
1369 if(j
== count
|| (dst
[j
+ i
] = src
[j
]) == '\0')
1377 /* Set the first element to 0, not the first element after the skipped part */
1382 /*********************************************************************
1383 * strncat (MSVCRT.@)
1385 char* __cdecl
strncat(char *dst
, const char *src
, size_t len
)
1389 for ( ; len
&& *src
; d
++, src
++, len
--) *d
= *src
;
1394 /*********************************************************************
1395 * _strxfrm_l (MSVCRT.@)
1397 size_t CDECL
_strxfrm_l( char *dest
, const char *src
,
1398 size_t len
, _locale_t locale
)
1400 pthreadlocinfo locinfo
;
1403 if(!MSVCRT_CHECK_PMT(src
)) return INT_MAX
;
1404 if(!MSVCRT_CHECK_PMT(dest
|| !len
)) return INT_MAX
;
1407 FIXME("len > INT_MAX not supported\n");
1412 locinfo
= get_locinfo();
1414 locinfo
= locale
->locinfo
;
1416 if(!locinfo
->lc_handle
[LC_COLLATE
]) {
1417 strncpy(dest
, src
, len
);
1421 ret
= LCMapStringA(locinfo
->lc_handle
[LC_COLLATE
],
1422 LCMAP_SORTKEY
, src
, -1, NULL
, 0);
1424 if(len
) dest
[0] = 0;
1428 if(!len
) return ret
-1;
1436 return LCMapStringA(locinfo
->lc_handle
[LC_COLLATE
],
1437 LCMAP_SORTKEY
, src
, -1, dest
, len
) - 1;
1440 /*********************************************************************
1441 * strxfrm (MSVCRT.@)
1443 size_t CDECL
strxfrm( char *dest
, const char *src
, size_t len
)
1445 return _strxfrm_l(dest
, src
, len
, NULL
);
1448 /********************************************************************
1449 * __STRINGTOLD_L (MSVCR80.@)
1451 int CDECL
__STRINGTOLD_L( MSVCRT__LDOUBLE
*value
, char **endptr
,
1452 const char *str
, int flags
, _locale_t locale
)
1454 pthreadlocinfo locinfo
;
1455 const char *beg
, *p
;
1459 if (flags
) FIXME("flags not supported: %x\n", flags
);
1462 locinfo
= get_locinfo();
1464 locinfo
= locale
->locinfo
;
1467 while (_isspace_l((unsigned char)*p
, locale
))
1471 fp
= fpnum_parse(strtod_str_get
, strtod_str_unget
, &p
, locinfo
, TRUE
);
1472 if (endptr
) *endptr
= (p
== beg
? (char*)str
: (char*)p
);
1473 if (p
== beg
) ret
= 4;
1475 err
= fpnum_ldouble(&fp
, value
);
1476 if (err
) ret
= (value
->x80
[2] & 0x7fff ? 2 : 1);
1480 /********************************************************************
1481 * __STRINGTOLD (MSVCRT.@)
1483 int CDECL
__STRINGTOLD( MSVCRT__LDOUBLE
*value
, char **endptr
, const char *str
, int flags
)
1485 return __STRINGTOLD_L( value
, endptr
, str
, flags
, NULL
);
1488 /********************************************************************
1489 * _atoldbl_l (MSVCRT.@)
1491 int CDECL
_atoldbl_l( MSVCRT__LDOUBLE
*value
, char *str
, _locale_t locale
)
1494 switch(__STRINGTOLD_L( value
, &endptr
, str
, 0, locale
))
1496 case 1: return _UNDERFLOW
;
1497 case 2: return _OVERFLOW
;
1502 /********************************************************************
1503 * _atoldbl (MSVCRT.@)
1505 int CDECL
_atoldbl(_LDOUBLE
*value
, char *str
)
1507 return _atoldbl_l( (MSVCRT__LDOUBLE
*)value
, str
, NULL
);
1510 /*********************************************************************
1513 size_t __cdecl
strlen(const char *str
)
1515 const char *s
= str
;
1520 /******************************************************************
1521 * strnlen (MSVCRT.@)
1523 size_t CDECL
strnlen(const char *s
, size_t maxlen
)
1527 for(i
=0; i
<maxlen
; i
++)
1533 /*********************************************************************
1534 * _strtoi64_l (MSVCRT.@)
1536 * FIXME: locale parameter is ignored
1538 __int64 CDECL
_strtoi64_l(const char *nptr
, char **endptr
, int base
, _locale_t locale
)
1540 const char *p
= nptr
;
1541 BOOL negative
= FALSE
;
1542 BOOL got_digit
= FALSE
;
1545 TRACE("(%s %p %d %p)\n", debugstr_a(nptr
), endptr
, base
, locale
);
1547 if (!MSVCRT_CHECK_PMT(nptr
!= NULL
)) return 0;
1548 if (!MSVCRT_CHECK_PMT(base
== 0 || base
>= 2)) return 0;
1549 if (!MSVCRT_CHECK_PMT(base
<= 36)) return 0;
1551 while(_isspace_l((unsigned char)*nptr
, locale
)) nptr
++;
1556 } else if(*nptr
== '+')
1559 if((base
==0 || base
==16) && *nptr
=='0' && _tolower_l(*(nptr
+1), locale
)=='x') {
1572 char cur
= _tolower_l(*nptr
, locale
);
1575 if(cur
>='0' && cur
<='9') {
1580 if(cur
<'a' || cur
>='a'+base
-10)
1591 if(!negative
&& (ret
>I64_MAX
/base
|| ret
*base
>I64_MAX
-v
)) {
1594 } else if(negative
&& (ret
<I64_MIN
/base
|| ret
*base
<I64_MIN
-v
)) {
1602 *endptr
= (char*)(got_digit
? nptr
: p
);
1607 /*********************************************************************
1608 * _strtoi64 (MSVCRT.@)
1610 __int64 CDECL
_strtoi64(const char *nptr
, char **endptr
, int base
)
1612 return _strtoi64_l(nptr
, endptr
, base
, NULL
);
1615 /*********************************************************************
1616 * _atoi_l (MSVCRT.@)
1618 int __cdecl
_atoi_l(const char *str
, _locale_t locale
)
1620 __int64 ret
= _strtoi64_l(str
, NULL
, 10, locale
);
1625 } else if(ret
< INT_MIN
) {
1632 /*********************************************************************
1636 int __cdecl
atoi(const char *str
)
1644 while(_isspace_l((unsigned char)*str
, NULL
)) str
++;
1648 }else if(*str
== '-') {
1653 while(*str
>='0' && *str
<='9') {
1654 ret
= ret
*10+*str
-'0';
1658 return minus
? -ret
: ret
;
1661 int CDECL
atoi(const char *str
)
1663 return _atoi_l(str
, NULL
);
1667 /******************************************************************
1668 * _atoi64_l (MSVCRT.@)
1670 __int64 CDECL
_atoi64_l(const char *str
, _locale_t locale
)
1672 return _strtoi64_l(str
, NULL
, 10, locale
);
1675 /******************************************************************
1676 * _atoi64 (MSVCRT.@)
1678 __int64 CDECL
_atoi64(const char *str
)
1680 return _strtoi64_l(str
, NULL
, 10, NULL
);
1683 /******************************************************************
1684 * _atol_l (MSVCRT.@)
1686 __msvcrt_long CDECL
_atol_l(const char *str
, _locale_t locale
)
1688 __int64 ret
= _strtoi64_l(str
, NULL
, 10, locale
);
1690 if(ret
> LONG_MAX
) {
1693 } else if(ret
< LONG_MIN
) {
1700 /******************************************************************
1703 __msvcrt_long CDECL
atol(const char *str
)
1708 return _atol_l(str
, NULL
);
1714 /******************************************************************
1715 * _atoll_l (MSVCR120.@)
1717 __int64 CDECL
_atoll_l(const char* str
, _locale_t locale
)
1719 return _strtoi64_l(str
, NULL
, 10, locale
);
1722 /******************************************************************
1723 * atoll (MSVCR120.@)
1725 __int64 CDECL
atoll(const char* str
)
1727 return _atoll_l(str
, NULL
);
1730 #endif /* _MSVCR_VER>=120 */
1732 /******************************************************************
1733 * _strtol_l (MSVCRT.@)
1735 __msvcrt_long CDECL
_strtol_l(const char* nptr
,
1736 char** end
, int base
, _locale_t locale
)
1738 __int64 ret
= _strtoi64_l(nptr
, end
, base
, locale
);
1740 if(ret
> LONG_MAX
) {
1743 } else if(ret
< LONG_MIN
) {
1751 /******************************************************************
1754 __msvcrt_long CDECL
strtol(const char* nptr
, char** end
, int base
)
1756 return _strtol_l(nptr
, end
, base
, NULL
);
1759 /******************************************************************
1760 * _strtoul_l (MSVCRT.@)
1762 __msvcrt_ulong CDECL
_strtoul_l(const char* nptr
, char** end
, int base
, _locale_t locale
)
1764 __int64 ret
= _strtoi64_l(nptr
, end
, base
, locale
);
1766 if(ret
> ULONG_MAX
) {
1769 }else if(ret
< -(__int64
)ULONG_MAX
) {
1777 /******************************************************************
1778 * strtoul (MSVCRT.@)
1780 __msvcrt_ulong CDECL
strtoul(const char* nptr
, char** end
, int base
)
1782 return _strtoul_l(nptr
, end
, base
, NULL
);
1785 /*********************************************************************
1786 * _strtoui64_l (MSVCRT.@)
1788 * FIXME: locale parameter is ignored
1790 unsigned __int64 CDECL
_strtoui64_l(const char *nptr
, char **endptr
, int base
, _locale_t locale
)
1792 const char *p
= nptr
;
1793 BOOL negative
= FALSE
;
1794 BOOL got_digit
= FALSE
;
1795 unsigned __int64 ret
= 0;
1797 TRACE("(%s %p %d %p)\n", debugstr_a(nptr
), endptr
, base
, locale
);
1799 if (!MSVCRT_CHECK_PMT(nptr
!= NULL
)) return 0;
1800 if (!MSVCRT_CHECK_PMT(base
== 0 || base
>= 2)) return 0;
1801 if (!MSVCRT_CHECK_PMT(base
<= 36)) return 0;
1803 while(_isspace_l((unsigned char)*nptr
, locale
)) nptr
++;
1808 } else if(*nptr
== '+')
1811 if((base
==0 || base
==16) && *nptr
=='0' && _tolower_l(*(nptr
+1), locale
)=='x') {
1824 char cur
= _tolower_l(*nptr
, locale
);
1827 if(cur
>='0' && cur
<='9') {
1832 if(cur
<'a' || cur
>='a'+base
-10)
1840 if(ret
>UI64_MAX
/base
|| ret
*base
>UI64_MAX
-v
) {
1848 *endptr
= (char*)(got_digit
? nptr
: p
);
1850 return negative
? -ret
: ret
;
1853 /*********************************************************************
1854 * _strtoui64 (MSVCRT.@)
1856 unsigned __int64 CDECL
_strtoui64(const char *nptr
, char **endptr
, int base
)
1858 return _strtoui64_l(nptr
, endptr
, base
, NULL
);
1861 static int ltoa_helper(__msvcrt_long value
, char *str
, size_t size
, int radix
)
1866 char buffer
[33], *pos
;
1869 if (value
< 0 && radix
== 10)
1876 is_negative
= FALSE
;
1885 digit
= val
% radix
;
1889 *--pos
= '0' + digit
;
1891 *--pos
= 'a' + digit
- 10;
1898 len
= buffer
+ 33 - pos
;
1904 /* Copy the temporary buffer backwards up to the available number of
1905 * characters. Don't copy the negative sign if present. */
1913 for (pos
= buffer
+ 31, i
= 0; i
< size
; i
++)
1917 MSVCRT_INVALID_PMT("str[size] is too small", ERANGE
);
1921 memcpy(str
, pos
, len
);
1925 static int ltow_helper(__msvcrt_long value
, wchar_t *str
, size_t size
, int radix
)
1930 wchar_t buffer
[33], *pos
;
1933 if (value
< 0 && radix
== 10)
1940 is_negative
= FALSE
;
1949 digit
= val
% radix
;
1953 *--pos
= '0' + digit
;
1955 *--pos
= 'a' + digit
- 10;
1962 len
= buffer
+ 33 - pos
;
1968 /* Copy the temporary buffer backwards up to the available number of
1969 * characters. Don't copy the negative sign if present. */
1977 for (pos
= buffer
+ 31, i
= 0; i
< size
; i
++)
1981 MSVCRT_INVALID_PMT("str[size] is too small", ERANGE
);
1985 memcpy(str
, pos
, len
* sizeof(wchar_t));
1989 /*********************************************************************
1990 * _ltoa_s (MSVCRT.@)
1992 int CDECL
_ltoa_s(__msvcrt_long value
, char *str
, size_t size
, int radix
)
1994 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) return EINVAL
;
1995 if (!MSVCRT_CHECK_PMT(size
> 0)) return EINVAL
;
1996 if (!MSVCRT_CHECK_PMT(radix
>= 2 && radix
<= 36))
2002 return ltoa_helper(value
, str
, size
, radix
);
2005 /*********************************************************************
2006 * _ltow_s (MSVCRT.@)
2008 int CDECL
_ltow_s(__msvcrt_long value
, wchar_t *str
, size_t size
, int radix
)
2010 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) return EINVAL
;
2011 if (!MSVCRT_CHECK_PMT(size
> 0)) return EINVAL
;
2012 if (!MSVCRT_CHECK_PMT(radix
>= 2 && radix
<= 36))
2018 return ltow_helper(value
, str
, size
, radix
);
2021 /*********************************************************************
2022 * _itoa_s (MSVCRT.@)
2024 int CDECL
_itoa_s(int value
, char *str
, size_t size
, int radix
)
2026 return _ltoa_s(value
, str
, size
, radix
);
2029 /*********************************************************************
2032 char* CDECL
_itoa(int value
, char *str
, int radix
)
2034 return ltoa_helper(value
, str
, SIZE_MAX
, radix
) ? NULL
: str
;
2037 /*********************************************************************
2040 char* CDECL
_ltoa(__msvcrt_long value
, char *str
, int radix
)
2042 return ltoa_helper(value
, str
, SIZE_MAX
, radix
) ? NULL
: str
;
2045 /*********************************************************************
2046 * _itow_s (MSVCRT.@)
2048 int CDECL
_itow_s(int value
, wchar_t *str
, size_t size
, int radix
)
2050 return _ltow_s(value
, str
, size
, radix
);
2053 /*********************************************************************
2056 wchar_t* CDECL
_itow(int value
, wchar_t *str
, int radix
)
2058 return ltow_helper(value
, str
, SIZE_MAX
, radix
) ? NULL
: str
;
2061 /*********************************************************************
2064 wchar_t* CDECL
_ltow(__msvcrt_long value
, wchar_t *str
, int radix
)
2066 return ltow_helper(value
, str
, SIZE_MAX
, radix
) ? NULL
: str
;
2069 /*********************************************************************
2072 char* CDECL
_ultoa(__msvcrt_ulong value
, char *str
, int radix
)
2074 char buffer
[33], *pos
;
2080 int digit
= value
% radix
;
2084 *--pos
= '0' + digit
;
2086 *--pos
= 'a' + digit
- 10;
2087 } while (value
!= 0);
2089 memcpy(str
, pos
, buffer
+ 33 - pos
);
2093 /*********************************************************************
2094 * _ui64toa (MSVCRT.@)
2096 char* CDECL
_ui64toa(unsigned __int64 value
, char *str
, int radix
)
2098 char buffer
[65], *pos
;
2104 int digit
= value
% radix
;
2108 *--pos
= '0' + digit
;
2110 *--pos
= 'a' + digit
- 10;
2111 } while (value
!= 0);
2113 memcpy(str
, pos
, buffer
+ 65 - pos
);
2117 /*********************************************************************
2120 wchar_t* CDECL
_ultow(__msvcrt_ulong value
, wchar_t *str
, int radix
)
2122 wchar_t buffer
[33], *pos
;
2128 int digit
= value
% radix
;
2132 *--pos
= '0' + digit
;
2134 *--pos
= 'a' + digit
- 10;
2135 } while (value
!= 0);
2137 memcpy(str
, pos
, (buffer
+ 33 - pos
) * sizeof(wchar_t));
2141 /*********************************************************************
2142 * _ui64tow (MSVCRT.@)
2144 wchar_t* CDECL
_ui64tow(unsigned __int64 value
, wchar_t *str
, int radix
)
2146 wchar_t buffer
[65], *pos
;
2152 int digit
= value
% radix
;
2156 *--pos
= '0' + digit
;
2158 *--pos
= 'a' + digit
- 10;
2159 } while (value
!= 0);
2161 memcpy(str
, pos
, (buffer
+ 65 - pos
) * sizeof(wchar_t));
2165 /*********************************************************************
2166 * _i64toa (MSVCRT.@)
2168 char* CDECL
_i64toa(__int64 value
, char *str
, int radix
)
2170 unsigned __int64 val
;
2172 char buffer
[65], *pos
;
2174 if (value
< 0 && radix
== 10)
2181 is_negative
= FALSE
;
2190 int digit
= val
% radix
;
2194 *--pos
= '0' + digit
;
2196 *--pos
= 'a' + digit
- 10;
2203 memcpy(str
, pos
, buffer
+ 65 - pos
);
2207 /*********************************************************************
2208 * _i64tow (MSVCRT.@)
2210 wchar_t* CDECL
_i64tow(__int64 value
, wchar_t *str
, int radix
)
2212 unsigned __int64 val
;
2214 wchar_t buffer
[65], *pos
;
2216 if (value
< 0 && radix
== 10)
2223 is_negative
= FALSE
;
2232 int digit
= val
% radix
;
2236 *--pos
= '0' + digit
;
2238 *--pos
= 'a' + digit
- 10;
2245 memcpy(str
, pos
, (buffer
+ 65 - pos
) * sizeof(wchar_t));
2249 /*********************************************************************
2250 * _ui64toa_s (MSVCRT.@)
2252 int CDECL
_ui64toa_s(unsigned __int64 value
, char *str
,
2253 size_t size
, int radix
)
2255 char buffer
[65], *pos
;
2258 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) return EINVAL
;
2259 if (!MSVCRT_CHECK_PMT(size
> 0)) return EINVAL
;
2260 if (!MSVCRT_CHECK_PMT(radix
>= 2 && radix
<= 36))
2270 digit
= value
%radix
;
2276 *--pos
= 'a'+digit
-10;
2279 if(buffer
-pos
+65 > size
) {
2280 MSVCRT_INVALID_PMT("str[size] is too small", EINVAL
);
2284 memcpy(str
, pos
, buffer
-pos
+65);
2288 /*********************************************************************
2289 * _ui64tow_s (MSVCRT.@)
2291 int CDECL
_ui64tow_s( unsigned __int64 value
, wchar_t *str
,
2292 size_t size
, int radix
)
2294 wchar_t buffer
[65], *pos
;
2297 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) return EINVAL
;
2298 if (!MSVCRT_CHECK_PMT(size
> 0)) return EINVAL
;
2299 if (!MSVCRT_CHECK_PMT(radix
>= 2 && radix
<= 36))
2309 digit
= value
% radix
;
2310 value
= value
/ radix
;
2312 *--pos
= '0' + digit
;
2314 *--pos
= 'a' + digit
- 10;
2315 } while (value
!= 0);
2317 if(buffer
-pos
+65 > size
) {
2318 MSVCRT_INVALID_PMT("str[size] is too small", EINVAL
);
2322 memcpy(str
, pos
, (buffer
-pos
+65)*sizeof(wchar_t));
2326 /*********************************************************************
2327 * _ultoa_s (MSVCRT.@)
2329 int CDECL
_ultoa_s(__msvcrt_ulong value
, char *str
, size_t size
, int radix
)
2331 __msvcrt_ulong digit
;
2332 char buffer
[33], *pos
;
2335 if (!str
|| !size
|| radix
< 2 || radix
> 36)
2349 digit
= value
% radix
;
2353 *--pos
= '0' + digit
;
2355 *--pos
= 'a' + digit
- 10;
2359 len
= buffer
+ 33 - pos
;
2365 /* Copy the temporary buffer backwards up to the available number of
2368 for (pos
= buffer
+ 31, i
= 0; i
< size
; i
++)
2376 memcpy(str
, pos
, len
);
2380 /*********************************************************************
2381 * _ultow_s (MSVCRT.@)
2383 int CDECL
_ultow_s(__msvcrt_ulong value
, wchar_t *str
, size_t size
, int radix
)
2385 __msvcrt_ulong digit
;
2386 WCHAR buffer
[33], *pos
;
2389 if (!str
|| !size
|| radix
< 2 || radix
> 36)
2403 digit
= value
% radix
;
2407 *--pos
= '0' + digit
;
2409 *--pos
= 'a' + digit
- 10;
2413 len
= buffer
+ 33 - pos
;
2419 /* Copy the temporary buffer backwards up to the available number of
2422 for (pos
= buffer
+ 31, i
= 0; i
< size
; i
++)
2430 memcpy(str
, pos
, len
* sizeof(wchar_t));
2434 /*********************************************************************
2435 * _i64toa_s (MSVCRT.@)
2437 int CDECL
_i64toa_s(__int64 value
, char *str
, size_t size
, int radix
)
2439 unsigned __int64 val
;
2442 char buffer
[65], *pos
;
2445 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) return EINVAL
;
2446 if (!MSVCRT_CHECK_PMT(size
> 0)) return EINVAL
;
2447 if (!MSVCRT_CHECK_PMT(radix
>= 2 && radix
<= 36))
2453 if (value
< 0 && radix
== 10)
2460 is_negative
= FALSE
;
2469 digit
= val
% radix
;
2473 *--pos
= '0' + digit
;
2475 *--pos
= 'a' + digit
- 10;
2482 len
= buffer
+ 65 - pos
;
2488 /* Copy the temporary buffer backwards up to the available number of
2489 * characters. Don't copy the negative sign if present. */
2497 for (pos
= buffer
+ 63, i
= 0; i
< size
; i
++)
2501 MSVCRT_INVALID_PMT("str[size] is too small", ERANGE
);
2505 memcpy(str
, pos
, len
);
2509 /*********************************************************************
2510 * _i64tow_s (MSVCRT.@)
2512 int CDECL
_i64tow_s(__int64 value
, wchar_t *str
, size_t size
, int radix
)
2514 unsigned __int64 val
;
2517 wchar_t buffer
[65], *pos
;
2520 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) return EINVAL
;
2521 if (!MSVCRT_CHECK_PMT(size
> 0)) return EINVAL
;
2522 if (!MSVCRT_CHECK_PMT(radix
>= 2 && radix
<= 36))
2528 if (value
< 0 && radix
== 10)
2535 is_negative
= FALSE
;
2544 digit
= val
% radix
;
2548 *--pos
= '0' + digit
;
2550 *--pos
= 'a' + digit
- 10;
2557 len
= buffer
+ 65 - pos
;
2563 /* Copy the temporary buffer backwards up to the available number of
2564 * characters. Don't copy the negative sign if present. */
2572 for (pos
= buffer
+ 63, i
= 0; i
< size
; i
++)
2576 MSVCRT_INVALID_PMT("str[size] is too small", ERANGE
);
2580 memcpy(str
, pos
, len
* sizeof(wchar_t));
2584 #define I10_OUTPUT_MAX_PREC 21
2585 /* Internal structure used by $I10_OUTPUT */
2586 struct _I10_OUTPUT_DATA
{
2590 char str
[I10_OUTPUT_MAX_PREC
+1]; /* add space for '\0' */
2593 /*********************************************************************
2594 * $I10_OUTPUT (MSVCRT.@)
2595 * ld80 - long double (Intel 80 bit FP in 12 bytes) to be printed to data
2596 * prec - precision of part, we're interested in
2597 * flag - 0 for first prec digits, 1 for fractional part
2598 * data - data to be populated
2601 * 0 if given double is NaN or INF
2605 * Native sets last byte of data->str to '0' or '9', I don't know what
2606 * it means. Current implementation sets it always to '0'.
2608 int CDECL
I10_OUTPUT(MSVCRT__LDOUBLE ld80
, int prec
, int flag
, struct _I10_OUTPUT_DATA
*data
)
2613 char buf
[I10_OUTPUT_MAX_PREC
+9]; /* 9 = strlen("0.e+0000") + '\0' */
2616 if ((ld80
.x80
[2] & 0x7fff) == 0x7fff)
2618 if (ld80
.x80
[0] == 0 && ld80
.x80
[1] == 0x80000000)
2619 strcpy( data
->str
, "1#INF" );
2621 strcpy( data
->str
, (ld80
.x80
[1] & 0x40000000) ? "1#QNAN" : "1#SNAN" );
2623 data
->sign
= (ld80
.x80
[2] & 0x8000) ? '-' : ' ';
2624 data
->len
= strlen(data
->str
);
2628 num
.sign
= (ld80
.x80
[2] & 0x8000) ? -1 : 1;
2629 num
.exp
= (ld80
.x80
[2] & 0x7fff) - 0x3fff - 63;
2630 num
.m
= ld80
.x80
[0] | ((ULONGLONG
)ld80
.x80
[1] << 32);
2631 num
.mod
= FP_ROUND_EVEN
;
2632 fpnum_double( &num
, &d
);
2633 TRACE("(%lf %d %x %p)\n", d
, prec
, flag
, data
);
2642 int exp
= 1 + floor(log10(d
));
2650 if(prec
+1 > I10_OUTPUT_MAX_PREC
)
2651 prec
= I10_OUTPUT_MAX_PREC
-1;
2657 sprintf(format
, "%%.%dle", prec
);
2658 sprintf(buf
, format
, d
);
2661 data
->pos
= atoi(buf
+prec
+3);
2665 for(p
= buf
+prec
+1; p
>buf
+1 && *p
=='0'; p
--);
2668 memcpy(data
->str
, buf
+1, data
->len
);
2669 data
->str
[data
->len
] = '\0';
2671 if(buf
[1]!='0' && prec
-data
->len
+1>0)
2672 memcpy(data
->str
+data
->len
+1, buf
+data
->len
+1, prec
-data
->len
+1);
2676 #undef I10_OUTPUT_MAX_PREC
2678 /*********************************************************************
2681 int __cdecl
memcmp(const void *ptr1
, const void *ptr2
, size_t n
)
2683 const unsigned char *p1
, *p2
;
2685 for (p1
= ptr1
, p2
= ptr2
; n
; n
--, p1
++, p2
++)
2687 if (*p1
< *p2
) return -1;
2688 if (*p1
> *p2
) return 1;
2693 #if defined(__i386__) || defined(__x86_64__)
2697 #define DEST_REG "%edi"
2698 #define SRC_REG "%esi"
2699 #define LEN_REG "%ecx"
2700 #define TMP_REG "%edx"
2702 #define MEMMOVE_INIT \
2703 "pushl " SRC_REG "\n\t" \
2704 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
2705 "pushl " DEST_REG "\n\t" \
2706 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
2707 "movl 12(%esp), " DEST_REG "\n\t" \
2708 "movl 16(%esp), " SRC_REG "\n\t" \
2709 "movl 20(%esp), " LEN_REG "\n\t"
2711 #define MEMMOVE_CLEANUP \
2712 "movl 12(%esp), %eax\n\t" \
2713 "popl " DEST_REG "\n\t" \
2714 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t") \
2715 "popl " SRC_REG "\n\t" \
2716 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
2720 #define DEST_REG "%rdi"
2721 #define SRC_REG "%rsi"
2722 #define LEN_REG "%r8"
2723 #define TMP_REG "%r9"
2725 #define MEMMOVE_INIT \
2726 "pushq " SRC_REG "\n\t" \
2727 __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t") \
2728 "pushq " DEST_REG "\n\t" \
2729 __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t") \
2730 "movq %rcx, " DEST_REG "\n\t" \
2731 "movq %rdx, " SRC_REG "\n\t"
2733 #define MEMMOVE_CLEANUP \
2734 "movq %rcx, %rax\n\t" \
2735 "popq " DEST_REG "\n\t" \
2736 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t") \
2737 "popq " SRC_REG "\n\t" \
2738 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
2741 void * __cdecl
sse2_memmove(void *dst
, const void *src
, size_t n
);
2742 __ASM_GLOBAL_FUNC( sse2_memmove
,
2744 "mov " DEST_REG
", " TMP_REG
"\n\t" /* check copying direction */
2745 "sub " SRC_REG
", " TMP_REG
"\n\t"
2746 "cmp " LEN_REG
", " TMP_REG
"\n\t"
2749 "cmp $4, " LEN_REG
"\n\t" /* 4-bytes align */
2751 "mov " DEST_REG
", " TMP_REG
"\n\t"
2752 "shr $1, " TMP_REG
"\n\t"
2755 "dec " LEN_REG
"\n\t"
2756 "inc " TMP_REG
"\n\t"
2758 "shr $1, " TMP_REG
"\n\t"
2761 "sub $2, " LEN_REG
"\n\t"
2762 "inc " TMP_REG
"\n\t"
2763 "1:\n\t" /* 16-bytes align */
2764 "cmp $16, " LEN_REG
"\n\t"
2766 "shr $1, " TMP_REG
"\n\t"
2769 "sub $4, " LEN_REG
"\n\t"
2770 "inc " TMP_REG
"\n\t"
2772 "shr $1, " TMP_REG
"\n\t"
2776 "sub $8, " LEN_REG
"\n\t"
2778 "cmp $64, " LEN_REG
"\n\t"
2780 "1:\n\t" /* copy 64-bytes blocks in loop, dest 16-bytes aligned */
2781 "movdqu 0x00(" SRC_REG
"), %xmm0\n\t"
2782 "movdqu 0x10(" SRC_REG
"), %xmm1\n\t"
2783 "movdqu 0x20(" SRC_REG
"), %xmm2\n\t"
2784 "movdqu 0x30(" SRC_REG
"), %xmm3\n\t"
2785 "movdqa %xmm0, 0x00(" DEST_REG
")\n\t"
2786 "movdqa %xmm1, 0x10(" DEST_REG
")\n\t"
2787 "movdqa %xmm2, 0x20(" DEST_REG
")\n\t"
2788 "movdqa %xmm3, 0x30(" DEST_REG
")\n\t"
2789 "add $64, " SRC_REG
"\n\t"
2790 "add $64, " DEST_REG
"\n\t"
2791 "sub $64, " LEN_REG
"\n\t"
2792 "cmp $64, " LEN_REG
"\n\t"
2794 "copy_fwd63:\n\t" /* copy last 63 bytes, dest 16-bytes aligned */
2795 "mov " LEN_REG
", " TMP_REG
"\n\t"
2796 "and $15, " LEN_REG
"\n\t"
2797 "shr $5, " TMP_REG
"\n\t"
2799 "movdqu 0(" SRC_REG
"), %xmm0\n\t"
2800 "movdqa %xmm0, 0(" DEST_REG
")\n\t"
2801 "add $16, " SRC_REG
"\n\t"
2802 "add $16, " DEST_REG
"\n\t"
2804 "shr $1, " TMP_REG
"\n\t"
2805 "jnc copy_fwd15\n\t"
2806 "movdqu 0x00(" SRC_REG
"), %xmm0\n\t"
2807 "movdqu 0x10(" SRC_REG
"), %xmm1\n\t"
2808 "movdqa %xmm0, 0x00(" DEST_REG
")\n\t"
2809 "movdqa %xmm1, 0x10(" DEST_REG
")\n\t"
2810 "add $32, " SRC_REG
"\n\t"
2811 "add $32, " DEST_REG
"\n\t"
2812 "copy_fwd15:\n\t" /* copy last 15 bytes, dest 4-bytes aligned */
2813 "mov " LEN_REG
", " TMP_REG
"\n\t"
2814 "and $3, " LEN_REG
"\n\t"
2815 "shr $3, " TMP_REG
"\n\t"
2819 "shr $1, " TMP_REG
"\n\t"
2823 "copy_fwd3:\n\t" /* copy last 3 bytes */
2824 "shr $1, " LEN_REG
"\n\t"
2828 "shr $1, " LEN_REG
"\n\t"
2835 "lea (" DEST_REG
", " LEN_REG
"), " DEST_REG
"\n\t"
2836 "lea (" SRC_REG
", " LEN_REG
"), " SRC_REG
"\n\t"
2837 "cmp $4, " LEN_REG
"\n\t" /* 4-bytes align */
2839 "mov " DEST_REG
", " TMP_REG
"\n\t"
2840 "shr $1, " TMP_REG
"\n\t"
2842 "dec " SRC_REG
"\n\t"
2843 "dec " DEST_REG
"\n\t"
2844 "movb (" SRC_REG
"), %al\n\t"
2845 "movb %al, (" DEST_REG
")\n\t"
2846 "dec " LEN_REG
"\n\t"
2848 "shr $1, " TMP_REG
"\n\t"
2850 "sub $2, " SRC_REG
"\n\t"
2851 "sub $2, " DEST_REG
"\n\t"
2852 "movw (" SRC_REG
"), %ax\n\t"
2853 "movw %ax, (" DEST_REG
")\n\t"
2854 "sub $2, " LEN_REG
"\n\t"
2855 "1:\n\t" /* 16-bytes align */
2856 "cmp $16, " LEN_REG
"\n\t"
2858 "shr $1, " TMP_REG
"\n\t"
2860 "sub $4, " SRC_REG
"\n\t"
2861 "sub $4, " DEST_REG
"\n\t"
2862 "movl (" SRC_REG
"), %eax\n\t"
2863 "movl %eax, (" DEST_REG
")\n\t"
2864 "sub $4, " LEN_REG
"\n\t"
2866 "shr $1, " TMP_REG
"\n\t"
2868 "sub $8, " SRC_REG
"\n\t"
2869 "sub $8, " DEST_REG
"\n\t"
2870 "movl 4(" SRC_REG
"), %eax\n\t"
2871 "movl %eax, 4(" DEST_REG
")\n\t"
2872 "movl (" SRC_REG
"), %eax\n\t"
2873 "movl %eax, (" DEST_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 "sub $64, " SRC_REG
"\n\t"
2880 "sub $64, " DEST_REG
"\n\t"
2881 "movdqu 0x00(" SRC_REG
"), %xmm0\n\t"
2882 "movdqu 0x10(" SRC_REG
"), %xmm1\n\t"
2883 "movdqu 0x20(" SRC_REG
"), %xmm2\n\t"
2884 "movdqu 0x30(" SRC_REG
"), %xmm3\n\t"
2885 "movdqa %xmm0, 0x00(" DEST_REG
")\n\t"
2886 "movdqa %xmm1, 0x10(" DEST_REG
")\n\t"
2887 "movdqa %xmm2, 0x20(" DEST_REG
")\n\t"
2888 "movdqa %xmm3, 0x30(" DEST_REG
")\n\t"
2889 "sub $64, " LEN_REG
"\n\t"
2890 "cmp $64, " LEN_REG
"\n\t"
2892 "copy_bwd63:\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 "sub $16, " SRC_REG
"\n\t"
2898 "sub $16, " DEST_REG
"\n\t"
2899 "movdqu (" SRC_REG
"), %xmm0\n\t"
2900 "movdqa %xmm0, (" DEST_REG
")\n\t"
2902 "shr $1, " TMP_REG
"\n\t"
2903 "jnc copy_bwd15\n\t"
2904 "sub $32, " SRC_REG
"\n\t"
2905 "sub $32, " DEST_REG
"\n\t"
2906 "movdqu 0x00(" SRC_REG
"), %xmm0\n\t"
2907 "movdqu 0x10(" SRC_REG
"), %xmm1\n\t"
2908 "movdqa %xmm0, 0x00(" DEST_REG
")\n\t"
2909 "movdqa %xmm1, 0x10(" DEST_REG
")\n\t"
2910 "copy_bwd15:\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"
2915 "sub $4, " SRC_REG
"\n\t"
2916 "sub $4, " DEST_REG
"\n\t"
2917 "movl (" SRC_REG
"), %eax\n\t"
2918 "movl %eax, (" DEST_REG
")\n\t"
2920 "shr $1, " TMP_REG
"\n\t"
2922 "sub $8, " SRC_REG
"\n\t"
2923 "sub $8, " DEST_REG
"\n\t"
2924 "movl 4(" SRC_REG
"), %eax\n\t"
2925 "movl %eax, 4(" DEST_REG
")\n\t"
2926 "movl (" SRC_REG
"), %eax\n\t"
2927 "movl %eax, (" DEST_REG
")\n\t"
2928 "copy_bwd3:\n\t" /* copy last 3 bytes */
2929 "shr $1, " LEN_REG
"\n\t"
2931 "dec " SRC_REG
"\n\t"
2932 "dec " DEST_REG
"\n\t"
2933 "movb (" SRC_REG
"), %al\n\t"
2934 "movb %al, (" DEST_REG
")\n\t"
2936 "shr $1, " LEN_REG
"\n\t"
2938 "movw -2(" SRC_REG
"), %ax\n\t"
2939 "movw %ax, -2(" DEST_REG
")\n\t"
2946 /*********************************************************************
2947 * memmove (MSVCRT.@)
2949 #ifdef WORDS_BIGENDIAN
2950 # define MERGE(w1, sh1, w2, sh2) ((w1 << sh1) | (w2 >> sh2))
2952 # define MERGE(w1, sh1, w2, sh2) ((w1 >> sh1) | (w2 << sh2))
2954 void * __cdecl
memmove(void *dst
, const void *src
, size_t n
)
2957 return sse2_memmove(dst
, src
, n
);
2959 unsigned char *d
= dst
;
2960 const unsigned char *s
= src
;
2965 return sse2_memmove(dst
, src
, n
);
2970 if ((size_t)dst
- (size_t)src
>= n
)
2972 for (; (size_t)d
% sizeof(size_t) && n
; n
--) *d
++ = *s
++;
2974 sh1
= 8 * ((size_t)s
% sizeof(size_t));
2977 while (n
>= sizeof(size_t))
2979 *(size_t*)d
= *(size_t*)s
;
2980 s
+= sizeof(size_t);
2981 d
+= sizeof(size_t);
2982 n
-= sizeof(size_t);
2985 else if (n
>= 2 * sizeof(size_t))
2987 int sh2
= 8 * sizeof(size_t) - sh1
;
2994 s
+= sizeof(size_t);
2996 *(size_t*)d
= MERGE(x
, sh1
, y
, sh2
);
2997 d
+= sizeof(size_t);
2999 s
+= sizeof(size_t);
3001 *(size_t*)d
= MERGE(y
, sh1
, x
, sh2
);
3002 d
+= sizeof(size_t);
3004 n
-= 2 * sizeof(size_t);
3005 } while (n
>= 2 * sizeof(size_t));
3008 while (n
--) *d
++ = *s
++;
3016 for (; (size_t)d
% sizeof(size_t) && n
; n
--) *--d
= *--s
;
3018 sh1
= 8 * ((size_t)s
% sizeof(size_t));
3021 while (n
>= sizeof(size_t))
3023 s
-= sizeof(size_t);
3024 d
-= sizeof(size_t);
3025 *(size_t*)d
= *(size_t*)s
;
3026 n
-= sizeof(size_t);
3029 else if (n
>= 2 * sizeof(size_t))
3031 int sh2
= 8 * sizeof(size_t) - sh1
;
3038 s
-= sizeof(size_t);
3040 d
-= sizeof(size_t);
3041 *(size_t*)d
= MERGE(y
, sh1
, x
, sh2
);
3043 s
-= sizeof(size_t);
3045 d
-= sizeof(size_t);
3046 *(size_t*)d
= MERGE(x
, sh1
, y
, sh2
);
3048 n
-= 2 * sizeof(size_t);
3049 } while (n
>= 2 * sizeof(size_t));
3052 while (n
--) *--d
= *--s
;
3059 /*********************************************************************
3062 void * __cdecl
memcpy(void *dst
, const void *src
, size_t n
)
3064 return memmove(dst
, src
, n
);
3067 /*********************************************************************
3068 * _memccpy (MSVCRT.@)
3070 void * __cdecl
_memccpy(void *dst
, const void *src
, int c
, size_t n
)
3072 unsigned char *d
= dst
;
3073 const unsigned char *s
= src
;
3074 while (n
--) if ((*d
++ = *s
++) == (unsigned char)c
) return d
;
3079 static inline void memset_aligned_32(unsigned char *d
, uint64_t v
, size_t n
)
3081 unsigned char *end
= d
+ n
;
3084 *(uint64_t *)(d
+ 0) = v
;
3085 *(uint64_t *)(d
+ 8) = v
;
3086 *(uint64_t *)(d
+ 16) = v
;
3087 *(uint64_t *)(d
+ 24) = v
;
3092 /*********************************************************************
3095 void *__cdecl
memset(void *dst
, int c
, size_t n
)
3097 typedef uint64_t DECLSPEC_ALIGN(1) unaligned_ui64
;
3098 typedef uint32_t DECLSPEC_ALIGN(1) unaligned_ui32
;
3099 typedef uint16_t DECLSPEC_ALIGN(1) unaligned_ui16
;
3101 uint64_t v
= 0x101010101010101ull
* (unsigned char)c
;
3102 unsigned char *d
= (unsigned char *)dst
;
3103 size_t a
= 0x20 - ((uintptr_t)d
& 0x1f);
3107 *(unaligned_ui64
*)(d
+ 0) = v
;
3108 *(unaligned_ui64
*)(d
+ 8) = v
;
3109 *(unaligned_ui64
*)(d
+ n
- 16) = v
;
3110 *(unaligned_ui64
*)(d
+ n
- 8) = v
;
3111 if (n
<= 32) return dst
;
3112 *(unaligned_ui64
*)(d
+ 16) = v
;
3113 *(unaligned_ui64
*)(d
+ 24) = v
;
3114 *(unaligned_ui64
*)(d
+ n
- 32) = v
;
3115 *(unaligned_ui64
*)(d
+ n
- 24) = v
;
3116 if (n
<= 64) return dst
;
3118 n
= (n
- a
) & ~0x1f;
3119 memset_aligned_32(d
+ a
, v
, n
);
3124 *(unaligned_ui64
*)d
= v
;
3125 *(unaligned_ui64
*)(d
+ n
- 8) = v
;
3130 *(unaligned_ui32
*)d
= v
;
3131 *(unaligned_ui32
*)(d
+ n
- 4) = v
;
3136 *(unaligned_ui16
*)d
= v
;
3137 *(unaligned_ui16
*)(d
+ n
- 2) = v
;
3148 /*********************************************************************
3151 char* __cdecl
strchr(const char *str
, int c
)
3155 if (*str
== (char)c
) return (char*)str
;
3160 /*********************************************************************
3161 * strrchr (MSVCRT.@)
3163 char* __cdecl
strrchr(const char *str
, int c
)
3166 do { if (*str
== (char)c
) ret
= (char*)str
; } while (*str
++);
3170 /*********************************************************************
3173 void* __cdecl
memchr(const void *ptr
, int c
, size_t n
)
3175 const unsigned char *p
= ptr
;
3177 for (p
= ptr
; n
; n
--, p
++) if (*p
== (unsigned char)c
) return (void *)(ULONG_PTR
)p
;
3181 /*********************************************************************
3184 int __cdecl
strcmp(const char *str1
, const char *str2
)
3186 while (*str1
&& *str1
== *str2
) { str1
++; str2
++; }
3187 if ((unsigned char)*str1
> (unsigned char)*str2
) return 1;
3188 if ((unsigned char)*str1
< (unsigned char)*str2
) return -1;
3192 /*********************************************************************
3193 * strncmp (MSVCRT.@)
3195 int __cdecl
strncmp(const char *str1
, const char *str2
, size_t len
)
3198 while (--len
&& *str1
&& *str1
== *str2
) { str1
++; str2
++; }
3199 return (unsigned char)*str1
- (unsigned char)*str2
;
3202 /*********************************************************************
3203 * _strnicmp_l (MSVCRT.@)
3205 int __cdecl
_strnicmp_l(const char *s1
, const char *s2
,
3206 size_t count
, _locale_t locale
)
3208 pthreadlocinfo locinfo
;
3211 if(s1
==NULL
|| s2
==NULL
)
3212 return _NLSCMPERROR
;
3218 locinfo
= get_locinfo();
3220 locinfo
= locale
->locinfo
;
3222 if(!locinfo
->lc_handle
[LC_CTYPE
])
3225 if ((c1
= *s1
++) >= 'A' && c1
<= 'Z')
3227 if ((c2
= *s2
++) >= 'A' && c2
<= 'Z')
3229 }while(--count
&& c1
&& c1
==c2
);
3235 c1
= _tolower_l((unsigned char)*s1
++, locale
);
3236 c2
= _tolower_l((unsigned char)*s2
++, locale
);
3237 }while(--count
&& c1
&& c1
==c2
);
3242 /*********************************************************************
3243 * _stricmp_l (MSVCRT.@)
3245 int __cdecl
_stricmp_l(const char *s1
, const char *s2
, _locale_t locale
)
3247 return _strnicmp_l(s1
, s2
, -1, locale
);
3250 /*********************************************************************
3251 * _strnicmp (MSVCRT.@)
3253 int __cdecl
_strnicmp(const char *s1
, const char *s2
, size_t count
)
3255 return _strnicmp_l(s1
, s2
, count
, NULL
);
3258 /*********************************************************************
3259 * _stricmp (MSVCRT.@)
3261 int __cdecl
_stricmp(const char *s1
, const char *s2
)
3263 return _strnicmp_l(s1
, s2
, -1, NULL
);
3266 /*********************************************************************
3269 char* __cdecl
strstr(const char *haystack
, const char *needle
)
3271 size_t i
, j
, len
, needle_len
, lps_len
;
3274 needle_len
= strlen(needle
);
3275 if (!needle_len
) return (char*)haystack
;
3276 lps_len
= needle_len
> ARRAY_SIZE(lps
) ? ARRAY_SIZE(lps
) : needle_len
;
3283 if (needle
[i
] == needle
[len
]) lps
[i
++] = ++len
;
3284 else if (len
) len
= lps
[len
-1];
3291 while (j
< lps_len
&& haystack
[i
] && haystack
[i
] == needle
[j
])
3297 if (j
== needle_len
) return (char*)haystack
+ i
- j
;
3300 if (j
== ARRAY_SIZE(lps
) && !strncmp(haystack
+ i
, needle
+ j
, needle_len
- j
))
3301 return (char*)haystack
+ i
- j
;
3304 else if (haystack
[i
]) i
++;
3309 /*********************************************************************
3310 * _memicmp_l (MSVCRT.@)
3312 int __cdecl
_memicmp_l(const void *v1
, const void *v2
, size_t len
, _locale_t locale
)
3314 const char *s1
= v1
, *s2
= v2
;
3317 #if _MSVCR_VER == 0 || _MSVCR_VER >= 80
3321 MSVCRT_INVALID_PMT(NULL
, EINVAL
);
3322 return len
? _NLSCMPERROR
: 0;
3328 if ((ret
= _tolower_l(*s1
, locale
) - _tolower_l(*s2
, locale
)))
3336 /*********************************************************************
3337 * _memicmp (MSVCRT.@)
3339 int __cdecl
_memicmp(const void *s1
, const void *s2
, size_t len
)
3341 return _memicmp_l(s1
, s2
, len
, NULL
);
3344 /*********************************************************************
3345 * strcspn (MSVCRT.@)
3347 size_t __cdecl
strcspn(const char *str
, const char *reject
)
3352 memset(rejects
, 0, sizeof(rejects
));
3357 rejects
[(unsigned char)*p
] = TRUE
;
3362 while(*p
&& !rejects
[(unsigned char)*p
]) p
++;
3366 /*********************************************************************
3369 size_t __cdecl
strspn(const char *str
, const char *accept
)
3372 for (ptr
= str
; *ptr
; ptr
++) if (!strchr( accept
, *ptr
)) break;
3376 /*********************************************************************
3377 * strpbrk (MSVCRT.@)
3379 char* __cdecl
strpbrk(const char *str
, const char *accept
)
3381 for (; *str
; str
++) if (strchr( accept
, *str
)) return (char*)str
;
3385 /*********************************************************************
3386 * __strncnt (MSVCRT.@)
3388 size_t __cdecl
__strncnt(const char *str
, size_t size
)
3392 #if _MSVCR_VER >= 140
3393 while (*str
++ && size
--)
3395 while (size
-- && *str
++)
3406 /*********************************************************************
3407 * _strdec (CRTDLL.@)
3409 char * CDECL
_strdec(const char *str1
, const char *str2
)
3411 return (char *)(str2
- 1);
3414 /*********************************************************************
3415 * _strinc (CRTDLL.@)
3417 char * CDECL
_strinc(const char *str
)
3419 return (char *)(str
+ 1);
3422 /*********************************************************************
3423 * _strnextc (CRTDLL.@)
3425 unsigned int CDECL
_strnextc(const char *str
)
3427 return (unsigned char)str
[0];
3430 /*********************************************************************
3431 * _strninc (CRTDLL.@)
3433 char * CDECL
_strninc(const char *str
, size_t len
)
3435 return (char *)(str
+ len
);
3438 /*********************************************************************
3439 * _strspnp (CRTDLL.@)
3441 char * CDECL
_strspnp( const char *str1
, const char *str2
)
3443 str1
+= strspn( str1
, str2
);
3444 return *str1
? (char*)str1
: NULL
;