From beca1d73a89a2ca8abb0d52e8448b9d9328b25ce Mon Sep 17 00:00:00 2001 From: Eric Pouech Date: Mon, 15 Nov 2010 22:00:01 +0100 Subject: [PATCH] msvcrt: Implemented _i64to[aw]_s. --- dlls/msvcr100/msvcr100.spec | 4 +- dlls/msvcr80/msvcr80.spec | 4 +- dlls/msvcr90/msvcr90.spec | 4 +- dlls/msvcrt/msvcrt.spec | 4 +- dlls/msvcrt/string.c | 156 ++++++++++++++++++++++++++++++++++++ 5 files changed, 164 insertions(+), 8 deletions(-) diff --git a/dlls/msvcr100/msvcr100.spec b/dlls/msvcr100/msvcr100.spec index 4d26904c12e..28243de2b86 100644 --- a/dlls/msvcr100/msvcr100.spec +++ b/dlls/msvcr100/msvcr100.spec @@ -731,9 +731,9 @@ @ cdecl _hypot(double double) msvcrt._hypot @ cdecl _hypotf(float float) msvcrt._hypotf @ cdecl _i64toa(int64 ptr long) msvcrt._i64toa -@ stub _i64toa_s +@ cdecl _i64toa_s(int64 ptr long long) msvcrt._i64toa_s @ cdecl _i64tow(int64 ptr long) msvcrt._i64tow -@ stub _i64tow_s +@ cdecl _i64tow_s(int64 ptr long long) msvcrt._i64tow_s @ stub _initptd @ cdecl _initterm(ptr ptr) msvcrt._initterm @ cdecl _initterm_e(ptr ptr) msvcr90._initterm_e diff --git a/dlls/msvcr80/msvcr80.spec b/dlls/msvcr80/msvcr80.spec index a65a89dd61e..9206b938d1d 100644 --- a/dlls/msvcr80/msvcr80.spec +++ b/dlls/msvcr80/msvcr80.spec @@ -578,9 +578,9 @@ @ cdecl _hypot(double double) msvcrt._hypot @ cdecl _hypotf(float float) msvcrt._hypotf @ cdecl _i64toa(int64 ptr long) msvcrt._i64toa -@ stub _i64toa_s +@ cdecl _i64toa_s(int64 ptr long long) msvcrt._i64toa_s @ cdecl _i64tow(int64 ptr long) msvcrt._i64tow -@ stub _i64tow_s +@ cdecl _i64tow_s(int64 ptr long long) msvcrt._i64tow_s @ stub _initptd @ cdecl _initterm(ptr ptr) msvcrt._initterm @ cdecl _initterm_e(ptr ptr) msvcr90._initterm_e diff --git a/dlls/msvcr90/msvcr90.spec b/dlls/msvcr90/msvcr90.spec index 64332f57b0d..c58637d20b6 100644 --- a/dlls/msvcr90/msvcr90.spec +++ b/dlls/msvcr90/msvcr90.spec @@ -566,9 +566,9 @@ @ cdecl _hypot(double double) msvcrt._hypot @ cdecl _hypotf(float float) msvcrt._hypotf @ cdecl _i64toa(int64 ptr long) msvcrt._i64toa -@ stub _i64toa_s +@ cdecl _i64toa_s(int64 ptr long long) msvcrt._i64toa_s @ cdecl _i64tow(int64 ptr long) msvcrt._i64tow -@ stub _i64tow_s +@ cdecl _i64tow_s(int64 ptr long long) msvcrt._i64tow_s @ stub _initptd @ cdecl _initterm(ptr ptr) msvcrt._initterm @ cdecl _initterm_e(ptr ptr) diff --git a/dlls/msvcrt/msvcrt.spec b/dlls/msvcrt/msvcrt.spec index 00ba5ea88b1..c73a132f33f 100644 --- a/dlls/msvcrt/msvcrt.spec +++ b/dlls/msvcrt/msvcrt.spec @@ -515,9 +515,9 @@ @ cdecl _hypot(double double) @ cdecl _hypotf(float float) @ cdecl _i64toa(int64 ptr long) ntdll._i64toa -# stub _i64toa_s +@ cdecl _i64toa_s(int64 ptr long long) _i64toa_s @ cdecl _i64tow(int64 ptr long) ntdll._i64tow -# stub _i64tow_s +@ cdecl _i64tow_s(int64 ptr long long) _i64tow_s @ cdecl _initterm(ptr ptr) # stub _initterm_e @ stub _inp #(long) -i386 diff --git a/dlls/msvcrt/string.c b/dlls/msvcrt/string.c index d807c143061..11ad5a0c2d7 100644 --- a/dlls/msvcrt/string.c +++ b/dlls/msvcrt/string.c @@ -972,6 +972,162 @@ int CDECL _ultoa_s(MSVCRT_ulong value, char *str, MSVCRT_size_t size, int radix) return 0; } +/********************************************************************* + * _i64toa_s (MSVCRT.@) + */ +int CDECL _i64toa_s(__int64 value, char *str, MSVCRT_size_t size, int radix) +{ + unsigned __int64 val; + unsigned int digit; + int is_negative; + char buffer[65], *pos; + size_t len; + + if (!MSVCRT_CHECK_PMT(str != NULL) || !MSVCRT_CHECK_PMT(size > 0) || + !MSVCRT_CHECK_PMT(radix >= 2) || !MSVCRT_CHECK_PMT(radix <= 36)) + { + if (str && size) + str[0] = '\0'; + + *MSVCRT__errno() = MSVCRT_EINVAL; + return MSVCRT_EINVAL; + } + + if (value < 0 && radix == 10) + { + is_negative = 1; + val = -value; + } + else + { + is_negative = 0; + val = value; + } + + pos = buffer + 64; + *pos = '\0'; + + do + { + digit = val % radix; + val /= radix; + + if (digit < 10) + *--pos = '0' + digit; + else + *--pos = 'a' + digit - 10; + } + while (val != 0); + + if (is_negative) + *--pos = '-'; + + len = buffer + 65 - pos; + if (len > size) + { + size_t i; + char *p = str; + + /* Copy the temporary buffer backwards up to the available number of + * characters. Don't copy the negative sign if present. */ + + if (is_negative) + { + p++; + size--; + } + + for (pos = buffer + 63, i = 0; i < size; i++) + *p++ = *pos--; + + str[0] = '\0'; + MSVCRT_INVALID_PMT("str[size] is too small"); + *MSVCRT__errno() = MSVCRT_ERANGE; + return MSVCRT_ERANGE; + } + + memcpy(str, pos, len); + return 0; +} + +/********************************************************************* + * _i64tow_s (MSVCRT.@) + */ +int CDECL _i64tow_s(__int64 value, MSVCRT_wchar_t *str, MSVCRT_size_t size, int radix) +{ + unsigned __int64 val; + unsigned int digit; + int is_negative; + MSVCRT_wchar_t buffer[65], *pos; + size_t len; + + if (!MSVCRT_CHECK_PMT(str != NULL) || !MSVCRT_CHECK_PMT(size > 0) || + !MSVCRT_CHECK_PMT(radix >= 2) || !MSVCRT_CHECK_PMT(radix <= 36)) + { + if (str && size) + str[0] = '\0'; + + *MSVCRT__errno() = MSVCRT_EINVAL; + return MSVCRT_EINVAL; + } + + if (value < 0 && radix == 10) + { + is_negative = 1; + val = -value; + } + else + { + is_negative = 0; + val = value; + } + + pos = buffer + 64; + *pos = '\0'; + + do + { + digit = val % radix; + val /= radix; + + if (digit < 10) + *--pos = '0' + digit; + else + *--pos = 'a' + digit - 10; + } + while (val != 0); + + if (is_negative) + *--pos = '-'; + + len = buffer + 65 - pos; + if (len > size) + { + size_t i; + MSVCRT_wchar_t *p = str; + + /* Copy the temporary buffer backwards up to the available number of + * characters. Don't copy the negative sign if present. */ + + if (is_negative) + { + p++; + size--; + } + + for (pos = buffer + 63, i = 0; i < size; i++) + *p++ = *pos--; + + MSVCRT_INVALID_PMT("str[size] is too small"); + str[0] = '\0'; + *MSVCRT__errno() = MSVCRT_ERANGE; + return MSVCRT_ERANGE; + } + + memcpy(str, pos, len * sizeof(MSVCRT_wchar_t)); + return 0; +} + #define I10_OUTPUT_MAX_PREC 21 /* Internal structure used by $I10_OUTPUT */ struct _I10_OUTPUT_DATA {