Something went wrong. Try again.
Reactos
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370//// _ctype.cpp//// Copyright (c) Microsoft Corporation. All rights reserved.//// Functions for converting integers to strings.//#include <corecrt_internal.h>#include <corecrt_internal_securecrt.h>#include <limits.h>#include <stdlib.h>
#pragma warning(disable:__WARNING_NOT_SATISFIED) // 28020 Prefast thinks that 18446744073709551615 < 1.#pragma warning(disable:__WARNING_RANGE_PRECONDITION_VIOLATION) // 26060 Prefast thinks that 18446744073709551615 < 1.
//-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+//// Common Conversion Implementation////-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+namespace{ template <typename T> struct make_signed; template <> struct make_signed<unsigned long> { typedef long type; }; template <> struct make_signed<unsigned __int64> { typedef __int64 type; };}
template <typename UnsignedInteger, typename Character>_Success_(return == 0)static errno_t __cdecl common_xtox( UnsignedInteger const original_value, _Out_writes_z_(buffer_count) Character* const buffer, _When_(is_negative == true, _In_range_(>=, 2)) _In_range_(>=, 1) size_t const buffer_count, unsigned const radix, bool const is_negative ) throw(){// OACR isn't able to track that p stays within the bounds of [buffer, buffer + buffer_count) so manually verified and disabled warning#pragma warning(push)#pragma warning(disable:26014) Character* p = buffer; // pointer to traverse the string size_t length = 0; // current length of string
UnsignedInteger remaining_value = original_value;;
if (is_negative) { *p++ = '-'; ++length;
remaining_value = static_cast<UnsignedInteger>( -static_cast<typename make_signed<UnsignedInteger>::type>(remaining_value) ); }
Character* first_digit = p;
do { unsigned const digit = static_cast<unsigned>(remaining_value % radix); remaining_value /= radix;
// Convert to ASCII and store: if (digit > 9) { *p++ = static_cast<Character>(digit - 10 + 'a'); } else { *p++ = static_cast<Character>(digit + '0'); }
++length; } while (remaining_value > 0 && length < buffer_count);
if (length >= buffer_count) { buffer[0] = '\0'; _VALIDATE_RETURN_ERRCODE(length < buffer_count, ERANGE); }
// We now have the digits of the number in the buffer, but in reverse order. // Reverse the order, but first terminate the string: *p-- = '\0';
do { Character const t = *p; *p = *first_digit; *first_digit = t; --p; ++first_digit; } while (first_digit < p);
return 0;#pragma warning(pop)}
template <typename UnsignedInteger, typename Character>_Success_(return == 0)static errno_t __cdecl common_xtox_s( UnsignedInteger const value, _Out_writes_z_(buffer_count) Character* const buffer, size_t const buffer_count, unsigned const radix, bool const is_negative ) throw(){ _VALIDATE_RETURN_ERRCODE(buffer != nullptr, EINVAL); _VALIDATE_RETURN_ERRCODE(buffer_count > 0, EINVAL); _RESET_STRING(buffer, buffer_count); _VALIDATE_RETURN_ERRCODE(buffer_count > static_cast<size_t>(is_negative ? 2 : 1), ERANGE); _VALIDATE_RETURN_ERRCODE(2 <= radix && radix <= 36, EINVAL);
return common_xtox(value, buffer, buffer_count, radix, is_negative);}
//-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+//// 32-bit Integers => Narrow Strings////-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+extern "C" errno_t __cdecl _itoa_s( int const value, char* const buffer, size_t const buffer_count, int const radix ){ bool const is_negative = radix == 10 && value < 0; return common_xtox_s(static_cast<unsigned long>(value), buffer, buffer_count, radix, is_negative);}
extern "C" errno_t __cdecl _ltoa_s( long const value, char* const buffer, size_t const buffer_count, int const radix ){ bool const is_negative = radix == 10 && value < 0; return common_xtox_s(static_cast<unsigned long>(value), buffer, buffer_count, radix, is_negative);}
extern "C" errno_t __cdecl _ultoa_s( unsigned long const value, char* const buffer, size_t const buffer_count, int const radix ){ return common_xtox_s(value, buffer, buffer_count, radix, false);}
extern "C" char* __cdecl _itoa( int const value, char* const buffer, int const radix ){ bool const is_negative = radix == 10 && value < 0; common_xtox(static_cast<unsigned long>(value), buffer, _CRT_UNBOUNDED_BUFFER_SIZE, radix, is_negative); return buffer;}
extern "C" char* __cdecl _ltoa( long const value, char* const buffer, int const radix ){ bool const is_negative = radix == 10 && value < 0; common_xtox(static_cast<unsigned long>(value), buffer, _CRT_UNBOUNDED_BUFFER_SIZE, radix, is_negative); return buffer;}
extern "C" char* __cdecl _ultoa( unsigned long const value, char* const buffer, int const radix ){ common_xtox(value, buffer, _CRT_UNBOUNDED_BUFFER_SIZE, radix, false); return buffer;}
//-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+//// 64-bit Integers => Narrow Strings////-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+extern "C" errno_t __cdecl _i64toa_s( __int64 const value, char* const buffer, size_t const buffer_count, int const radix ){ bool const is_negative = radix == 10 && value < 0; return common_xtox_s(static_cast<unsigned __int64>(value), buffer, buffer_count, radix, is_negative);}
extern "C" errno_t __cdecl _ui64toa_s( unsigned __int64 const value, char* const buffer, size_t const buffer_count, int const radix ){ return common_xtox_s(value, buffer, buffer_count, radix, false);}
extern "C" char* __cdecl _i64toa( __int64 const value, char* const buffer, int const radix ){ bool const is_negative = radix == 10 && value < 0; common_xtox(static_cast<unsigned __int64>(value), buffer, _CRT_UNBOUNDED_BUFFER_SIZE, radix, is_negative); return buffer;}
extern "C" char* __cdecl _ui64toa( unsigned __int64 const value, char* const buffer, int const radix ){ common_xtox(value, buffer, _CRT_UNBOUNDED_BUFFER_SIZE, radix, false); return buffer;}
//-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+//// 32-bit Integers => Wide Strings////-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+extern "C" errno_t __cdecl _itow_s( int const value, wchar_t* const buffer, size_t const buffer_count, int const radix ){ bool const is_negative = radix == 10 && value < 0; return common_xtox_s(static_cast<unsigned long>(value), buffer, buffer_count, radix, is_negative);}
extern "C" errno_t __cdecl _ltow_s( long const value, wchar_t* const buffer, size_t const buffer_count, int const radix ){ bool const is_negative = radix == 10 && value < 0; return common_xtox_s(static_cast<unsigned long>(value), buffer, buffer_count, radix, is_negative);}
extern "C" errno_t __cdecl _ultow_s( unsigned long const value, wchar_t* const buffer, size_t const buffer_count, int const radix ){ return common_xtox_s(value, buffer, buffer_count, radix, false);}
extern "C" wchar_t* __cdecl _itow( int const value, wchar_t* const buffer, int const radix ){ bool const is_negative = radix == 10 && value < 0; common_xtox(static_cast<unsigned long>(value), buffer, _CRT_UNBOUNDED_BUFFER_SIZE, radix, is_negative); return buffer;}
extern "C" wchar_t* __cdecl _ltow( long const value, wchar_t* const buffer, int const radix ){ bool const is_negative = radix == 10 && value < 0; common_xtox(static_cast<unsigned long>(value), buffer, _CRT_UNBOUNDED_BUFFER_SIZE, radix, is_negative); return buffer;}
extern "C" wchar_t* __cdecl _ultow( unsigned long const value, wchar_t* const buffer, int const radix ){ common_xtox(value, buffer, _CRT_UNBOUNDED_BUFFER_SIZE, radix, false); return buffer;}
//-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+//// 64-bit Integers => Wide Strings////-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+extern "C" errno_t __cdecl _i64tow_s( __int64 const value, wchar_t* const buffer, size_t const buffer_count, int const radix ){ bool const is_negative = radix == 10 && value < 0; return common_xtox_s(static_cast<unsigned __int64>(value), buffer, buffer_count, radix, is_negative);}
extern "C" errno_t __cdecl _ui64tow_s( unsigned __int64 const value, wchar_t* const buffer, size_t const buffer_count, int const radix ){ return common_xtox_s(value, buffer, buffer_count, radix, false);}
extern "C" wchar_t* __cdecl _i64tow( __int64 const value, wchar_t* const buffer, int const radix ){ bool const is_negative = radix == 10 && value < 0; common_xtox(static_cast<unsigned __int64>(value), buffer, _CRT_UNBOUNDED_BUFFER_SIZE, radix, is_negative); return buffer;}
extern "C" wchar_t* __cdecl _ui64tow( unsigned __int64 const value, wchar_t* const buffer, int const radix ){ common_xtox(value, buffer, _CRT_UNBOUNDED_BUFFER_SIZE, radix, false); return buffer;}