| 6 |
| 7 | #include <ctype.h> |
| 8 | #include <errno.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <wctype.h> |
| 11 |
| 12 | #include <limits> |
| 13 | #include <type_traits> |
| 14 |
| 15 | #include "base/logging.h" |
| 16 | #include "base/no_destructor.h" |
| 17 | #include "base/numerics/safe_math.h" |
| 18 | #include "base/strings/string_util.h" |
| 19 | #include "base/strings/utf_string_conversions.h" |
| 20 | #include "base/third_party/double_conversion/double-conversion/double-conversion.h" |
| 21 |
| 22 | namespace base { |
| 23 |
| 24 | namespace { |
| 25 |
| 26 | template <typename STR, typename INT> |
| 27 | struct IntToStringT { |
| 28 | static STR IntToString(INT value) { |
| 29 | // log10(2) ~= 0.3 bytes needed per bit or per byte log10(2**8) ~= 2.4. |
| 30 | // So round up to allocate 3 output characters per byte, plus 1 for '-'. |
| 31 | const size_t kOutputBufSize = |
| 32 | 3 * sizeof(INT) + std::numeric_limits<INT>::is_signed; |
| 33 |
| 34 | // Create the string in a temporary buffer, write it back to front, and |
| 35 | // then return the substr of what we ended up using. |
| 36 | using CHR = typename STR::value_type; |
| 37 | CHR outbuf[kOutputBufSize]; |
| 38 |
| 39 | // The ValueOrDie call below can never fail, because UnsignedAbs is valid |
| 40 | // for all valid inputs. |
| 41 | typename std::make_unsigned<INT>::type res = |
| 42 | CheckedNumeric<INT>(value).UnsignedAbs().ValueOrDie(); |
| 43 |
| 44 | CHR* end = outbuf + kOutputBufSize; |
| 45 | CHR* i = end; |
| 46 | do { |
| 47 | --i; |
| 48 | DCHECK(i != outbuf); |
| 49 | *i = static_cast<CHR>((res % 10) + '0'); |
| 50 | res /= 10; |
| 51 | } while (res != 0); |
| 52 | if (IsValueNegative(value)) { |
| 53 | --i; |
| 54 | DCHECK(i != outbuf); |
| 55 | *i = static_cast<CHR>('-'); |
| 56 | } |
| 57 | return STR(i, end); |
| 58 | } |
| 59 | }; |
| 60 |
| 61 | // Utility to convert a character to a digit in a given base |
| 62 | template<typename CHAR, int BASE, bool BASE_LTE_10> class BaseCharToDigit { |
| 63 | }; |
| 64 |
| 65 | // Faster specialization for bases <= 10 |
| 66 | template<typename CHAR, int BASE> class BaseCharToDigit<CHAR, BASE, true> { |
| 67 | public: |
| 68 | static bool Convert(CHAR c, uint8_t* digit) { |
| 69 | if (c >= '0' && c < '0' + BASE) { |
| 70 | *digit = static_cast<uint8_t>(c - '0'); |
| 71 | return true; |
| 72 | } |
| 73 | return false; |
| 74 | } |
| 75 | }; |
| 76 |
| 77 | // Specialization for bases where 10 < base <= 36 |
| 78 | template<typename CHAR, int BASE> class BaseCharToDigit<CHAR, BASE, false> { |
| 79 | public: |
| 80 | static bool Convert(CHAR c, uint8_t* digit) { |
| 81 | if (c >= '0' && c <= '9') { |
| 82 | *digit = c - '0'; |
| 83 | } else if (c >= 'a' && c < 'a' + BASE - 10) { |
| 84 | *digit = c - 'a' + 10; |
| 85 | } else if (c >= 'A' && c < 'A' + BASE - 10) { |
| 86 | *digit = c - 'A' + 10; |
| 87 | } else { |
| 88 | return false; |
| 89 | } |
| 90 | return true; |
| 91 | } |
| 92 | }; |
| 93 |
| 94 | template <int BASE, typename CHAR> |
| 95 | bool CharToDigit(CHAR c, uint8_t* digit) { |
| 96 | return BaseCharToDigit<CHAR, BASE, BASE <= 10>::Convert(c, digit); |
| 97 | } |
| 98 |
| 99 | // There is an IsUnicodeWhitespace for wchars defined in string_util.h, but it |
| 100 | // is locale independent, whereas the functions we are replacing were |
| 101 | // locale-dependent. TBD what is desired, but for the moment let's not |
| 102 | // introduce a change in behaviour. |
| 103 | template<typename CHAR> class WhitespaceHelper { |
| 104 | }; |
| 105 |
| 106 | template<> class WhitespaceHelper<char> { |
| 107 | public: |
| 108 | static bool Invoke(char c) { |
| 109 | return 0 != isspace(static_cast<unsigned char>(c)); |
| 110 | } |
| 111 | }; |
| 112 |
| 113 | template<> class WhitespaceHelper<char16> { |
| 114 | public: |
| 115 | static bool Invoke(char16 c) { |
| 116 | return 0 != iswspace(c); |
| 117 | } |
| 118 | }; |
| 119 |
| 120 | template<typename CHAR> bool LocalIsWhitespace(CHAR c) { |
| 121 | return WhitespaceHelper<CHAR>::Invoke(c); |
| 122 | } |
| 123 |
| 124 | // IteratorRangeToNumberTraits should provide: |
| 125 | // - a typedef for iterator_type, the iterator type used as input. |
| 126 | // - a typedef for value_type, the target numeric type. |
| 127 | // - static functions min, max (returning the minimum and maximum permitted |
| 128 | // values) |
| 129 | // - constant kBase, the base in which to interpret the input |
| 130 | template<typename IteratorRangeToNumberTraits> |
| 131 | class IteratorRangeToNumber { |
| 132 | public: |
| 133 | typedef IteratorRangeToNumberTraits traits; |
| 134 | typedef typename traits::iterator_type const_iterator; |
| 135 | typedef typename traits::value_type value_type; |
| 136 |
| 137 | // Generalized iterator-range-to-number conversion. |
| 138 | // |
| 139 | static bool Invoke(const_iterator begin, |
| 140 | const_iterator end, |
| 141 | value_type* output) { |
| 142 | bool valid = true; |
| 143 |
| 144 | while (begin != end && LocalIsWhitespace(*begin)) { |
| 145 | valid = false; |
| 146 | ++begin; |
| 147 | } |
| 148 |
| 149 | if (begin != end && *begin == '-') { |
| 150 | if (!std::numeric_limits<value_type>::is_signed) { |
| 151 | *output = 0; |
| 152 | valid = false; |
| 153 | } else if (!Negative::Invoke(begin + 1, end, output)) { |
| 154 | valid = false; |
| 155 | } |
| 156 | } else { |
| 157 | if (begin != end && *begin == '+') { |
| 158 | ++begin; |
| 159 | } |
| 160 | if (!Positive::Invoke(begin, end, output)) { |
| 161 | valid = false; |
| 162 | } |
| 163 | } |
| 164 |
| 165 | return valid; |
| 166 | } |
| 167 |
| 168 | private: |
| 169 | // Sign provides: |
| 170 | // - a static function, CheckBounds, that determines whether the next digit |
| 171 | // causes an overflow/underflow |
| 172 | // - a static function, Increment, that appends the next digit appropriately |
| 173 | // according to the sign of the number being parsed. |
| 174 | template<typename Sign> |
| 175 | class Base { |
| 176 | public: |
| 177 | static bool Invoke(const_iterator begin, const_iterator end, |
| 178 | typename traits::value_type* output) { |
| 179 | *output = 0; |
| 180 |
| 181 | if (begin == end) { |
| 182 | return false; |
| 183 | } |
| 184 |
| 185 | // Note: no performance difference was found when using template |
| 186 | // specialization to remove this check in bases other than 16 |
| 187 | if (traits::kBase == 16 && end - begin > 2 && *begin == '0' && |
| 188 | (*(begin + 1) == 'x' || *(begin + 1) == 'X')) { |
| 189 | begin += 2; |
| 190 | } |
| 191 |
| 192 | for (const_iterator current = begin; current != end; ++current) { |
| 193 | uint8_t new_digit = 0; |
| 194 |
| 195 | if (!CharToDigit<traits::kBase>(*current, &new_digit)) { |
| 196 | return false; |
| 197 | } |
| 198 |
| 199 | if (current != begin) { |
| 200 | if (!Sign::CheckBounds(output, new_digit)) { |
| 201 | return false; |
| 202 | } |
| 203 | *output *= traits::kBase; |
| 204 | } |
| 205 |
| 206 | Sign::Increment(new_digit, output); |
| 207 | } |
| 208 | return true; |
| 209 | } |
| 210 | }; |
| 211 |
| 212 | class Positive : public Base<Positive> { |
| 213 | public: |
| 214 | static bool CheckBounds(value_type* output, uint8_t new_digit) { |
| 215 | if (*output > static_cast<value_type>(traits::max() / traits::kBase) || |
| 216 | (*output == static_cast<value_type>(traits::max() / traits::kBase) && |
| 217 | new_digit > traits::max() % traits::kBase)) { |
| 218 | *output = traits::max(); |
| 219 | return false; |
| 220 | } |
| 221 | return true; |
| 222 | } |
| 223 | static void Increment(uint8_t increment, value_type* output) { |
| 224 | *output += increment; |
| 225 | } |
| 226 | }; |
| 227 |
| 228 | class Negative : public Base<Negative> { |
| 229 | public: |
| 230 | static bool CheckBounds(value_type* output, uint8_t new_digit) { |
| 231 | if (*output < traits::min() / traits::kBase || |
| 232 | (*output == traits::min() / traits::kBase && |
| 233 | new_digit > 0 - traits::min() % traits::kBase)) { |
| 234 | *output = traits::min(); |
| 235 | return false; |
| 236 | } |
| 237 | return true; |
| 238 | } |
| 239 | static void Increment(uint8_t increment, value_type* output) { |
| 240 | *output -= increment; |
| 241 | } |
| 242 | }; |
| 243 | }; |
| 244 |
| 245 | template<typename ITERATOR, typename VALUE, int BASE> |
| 246 | class BaseIteratorRangeToNumberTraits { |
| 247 | public: |
| 248 | typedef ITERATOR iterator_type; |
| 249 | typedef VALUE value_type; |
| 250 | static value_type min() { |
| 251 | return std::numeric_limits<value_type>::min(); |
| 252 | } |
| 253 | static value_type max() { |
| 254 | return std::numeric_limits<value_type>::max(); |
| 255 | } |
| 256 | static const int kBase = BASE; |
| 257 | }; |
| 258 |
| 259 | template<typename ITERATOR> |
| 260 | class BaseHexIteratorRangeToIntTraits |
| 261 | : public BaseIteratorRangeToNumberTraits<ITERATOR, int, 16> { |
| 262 | }; |
| 263 |
| 264 | template <typename ITERATOR> |
| 265 | class BaseHexIteratorRangeToUIntTraits |
| 266 | : public BaseIteratorRangeToNumberTraits<ITERATOR, uint32_t, 16> {}; |
| 267 |
| 268 | template <typename ITERATOR> |
| 269 | class BaseHexIteratorRangeToInt64Traits |
| 270 | : public BaseIteratorRangeToNumberTraits<ITERATOR, int64_t, 16> {}; |
| 271 |
| 272 | template <typename ITERATOR> |
| 273 | class BaseHexIteratorRangeToUInt64Traits |
| 274 | : public BaseIteratorRangeToNumberTraits<ITERATOR, uint64_t, 16> {}; |
| 275 |
| 276 | typedef BaseHexIteratorRangeToIntTraits<StringPiece::const_iterator> |
| 277 | HexIteratorRangeToIntTraits; |
| 278 |
| 279 | typedef BaseHexIteratorRangeToUIntTraits<StringPiece::const_iterator> |
| 280 | HexIteratorRangeToUIntTraits; |
| 281 |
| 282 | typedef BaseHexIteratorRangeToInt64Traits<StringPiece::const_iterator> |
| 283 | HexIteratorRangeToInt64Traits; |
| 284 |
| 285 | typedef BaseHexIteratorRangeToUInt64Traits<StringPiece::const_iterator> |
| 286 | HexIteratorRangeToUInt64Traits; |
| 287 |
| 288 | template <typename VALUE, int BASE> |
| 289 | class StringPieceToNumberTraits |
| 290 | : public BaseIteratorRangeToNumberTraits<StringPiece::const_iterator, |
| 291 | VALUE, |
| 292 | BASE> { |
| 293 | }; |
| 294 |
| 295 | template <typename VALUE> |
| 296 | bool StringToIntImpl(StringPiece input, VALUE* output) { |
| 297 | return IteratorRangeToNumber<StringPieceToNumberTraits<VALUE, 10> >::Invoke( |
| 298 | input.begin(), input.end(), output); |
| 299 | } |
| 300 |
| 301 | template <typename VALUE, int BASE> |
| 302 | class StringPiece16ToNumberTraits |
| 303 | : public BaseIteratorRangeToNumberTraits<StringPiece16::const_iterator, |
| 304 | VALUE, |
| 305 | BASE> { |
| 306 | }; |
| 307 |
| 308 | template <typename VALUE> |
| 309 | bool String16ToIntImpl(StringPiece16 input, VALUE* output) { |
| 310 | return IteratorRangeToNumber<StringPiece16ToNumberTraits<VALUE, 10> >::Invoke( |
| 311 | input.begin(), input.end(), output); |
| 312 | } |
| 313 |
| 314 | } // namespace |
| 315 |
| 316 | std::string NumberToString(int value) { |
| 317 | return IntToStringT<std::string, int>::IntToString(value); |
| 318 | } |
| 319 |
| 320 | string16 NumberToString16(int value) { |
| 321 | return IntToStringT<string16, int>::IntToString(value); |
| 322 | } |
| 323 |
| 324 | std::string NumberToString(unsigned value) { |
| 325 | return IntToStringT<std::string, unsigned>::IntToString(value); |
| 326 | } |
| 327 |
| 328 | string16 NumberToString16(unsigned value) { |
| 329 | return IntToStringT<string16, unsigned>::IntToString(value); |
| 330 | } |
| 331 |
| 332 | std::string NumberToString(long value) { |
| 333 | return IntToStringT<std::string, long>::IntToString(value); |
| 334 | } |
| 335 |
| 336 | string16 NumberToString16(long value) { |
| 337 | return IntToStringT<string16, long>::IntToString(value); |
| 338 | } |
| 339 |
| 340 | std::string NumberToString(unsigned long value) { |
| 341 | return IntToStringT<std::string, unsigned long>::IntToString(value); |
| 342 | } |
| 343 |
| 344 | string16 NumberToString16(unsigned long value) { |
| 345 | return IntToStringT<string16, unsigned long>::IntToString(value); |
| 346 | } |
| 347 |
| 348 | std::string NumberToString(long long value) { |
| 349 | return IntToStringT<std::string, long long>::IntToString(value); |
| 350 | } |
| 351 |
| 352 | string16 NumberToString16(long long value) { |
| 353 | return IntToStringT<string16, long long>::IntToString(value); |
| 354 | } |
| 355 |
| 356 | std::string NumberToString(unsigned long long value) { |
| 357 | return IntToStringT<std::string, unsigned long long>::IntToString(value); |
| 358 | } |
| 359 |
| 360 | string16 NumberToString16(unsigned long long value) { |
| 361 | return IntToStringT<string16, unsigned long long>::IntToString(value); |
| 362 | } |
| 363 |
| 364 | static const double_conversion::DoubleToStringConverter* |
| 365 | GetDoubleToStringConverter() { |
| 366 | static NoDestructor<double_conversion::DoubleToStringConverter> converter( |
| 367 | double_conversion::DoubleToStringConverter::EMIT_POSITIVE_EXPONENT_SIGN, |
| 368 | nullptr, nullptr, 'e', -6, 12, 0, 0); |
| 369 | return converter.get(); |
| 370 | } |
| 371 |
| 372 | std::string NumberToString(double value) { |
| 373 | char buffer[32]; |
| 374 | double_conversion::StringBuilder builder(buffer, sizeof(buffer)); |
| 375 | GetDoubleToStringConverter()->ToShortest(value, &builder); |
| 376 | return std::string(buffer, builder.position()); |
| 377 | } |
| 378 |
| 379 | base::string16 NumberToString16(double value) { |
| 380 | char buffer[32]; |
| 381 | double_conversion::StringBuilder builder(buffer, sizeof(buffer)); |
| 382 | GetDoubleToStringConverter()->ToShortest(value, &builder); |
| 383 |
| 384 | // The number will be ASCII. This creates the string using the "input |
| 385 | // iterator" variant which promotes from 8-bit to 16-bit via "=". |
| 386 | return base::string16(&buffer[0], &buffer[builder.position()]); |
| 387 | } |
| 388 |
| 389 | bool StringToInt(StringPiece input, int* output) { |
| 390 | return StringToIntImpl(input, output); |
| 391 | } |
| 392 |
| 393 | bool StringToInt(StringPiece16 input, int* output) { |
| 394 | return String16ToIntImpl(input, output); |
| 395 | } |
| 396 |
| 397 | bool StringToUint(StringPiece input, unsigned* output) { |
| 398 | return StringToIntImpl(input, output); |
| 399 | } |
| 400 |
| 401 | bool StringToUint(StringPiece16 input, unsigned* output) { |
| 402 | return String16ToIntImpl(input, output); |
| 403 | } |
| 404 |
| 405 | bool StringToInt64(StringPiece input, int64_t* output) { |
| 406 | return StringToIntImpl(input, output); |
| 407 | } |
| 408 |
| 409 | bool StringToInt64(StringPiece16 input, int64_t* output) { |
| 410 | return String16ToIntImpl(input, output); |
| 411 | } |
| 412 |
| 413 | bool StringToUint64(StringPiece input, uint64_t* output) { |
| 414 | return StringToIntImpl(input, output); |
| 415 | } |
| 416 |
| 417 | bool StringToUint64(StringPiece16 input, uint64_t* output) { |
| 418 | return String16ToIntImpl(input, output); |
| 419 | } |
| 420 |
| 421 | bool StringToSizeT(StringPiece input, size_t* output) { |
| 422 | return StringToIntImpl(input, output); |
| 423 | } |
| 424 |
| 425 | bool StringToSizeT(StringPiece16 input, size_t* output) { |
| 426 | return String16ToIntImpl(input, output); |
| 427 | } |
| 428 |
| 429 | template <typename STRING, typename CHAR> |
| 430 | bool StringToDoubleImpl(STRING input, const CHAR* data, double* output) { |
| 431 | static NoDestructor<double_conversion::StringToDoubleConverter> converter( |
| 432 | double_conversion::StringToDoubleConverter::ALLOW_LEADING_SPACES | |
| 433 | double_conversion::StringToDoubleConverter::ALLOW_TRAILING_JUNK, |
| 434 | 0.0, 0, nullptr, nullptr); |
| 435 |
| 436 | int processed_characters_count; |
| 437 | *output = converter->StringToDouble(data, input.size(), |
| 438 | &processed_characters_count); |
| 439 |
| 440 | // Cases to return false: |
| 441 | // - If the input string is empty, there was nothing to parse. |
| 442 | // - If the value saturated to HUGE_VAL. |
| 443 | // - If the entire string was not processed, there are either characters |
| 444 | // remaining in the string after a parsed number, or the string does not |
| 445 | // begin with a parseable number. |
| 446 | // - If the first character is a space, there was leading whitespace |
| 447 | return !input.empty() && *output != HUGE_VAL && *output != -HUGE_VAL && |
| 448 | static_cast<size_t>(processed_characters_count) == input.size() && |
| 449 | !IsUnicodeWhitespace(input[0]); |
| 450 | } |
| 451 |
| 452 | bool StringToDouble(StringPiece input, double* output) { |
| 453 | return StringToDoubleImpl(input, input.data(), output); |
| 454 | } |
| 455 |
| 456 | bool StringToDouble(StringPiece16 input, double* output) { |
| 457 | return StringToDoubleImpl( |
| 458 | input, reinterpret_cast<const uint16_t*>(input.data()), output); |
| 459 | } |
| 460 |
| 461 | std::string HexEncode(const void* bytes, size_t size) { |
| 462 | static const char kHexChars[] = "0123456789ABCDEF"; |
| 463 |
| 464 | // Each input byte creates two output hex characters. |
| 465 | std::string ret(size * 2, '\0'); |
| 466 |
| 467 | for (size_t i = 0; i < size; ++i) { |
| 468 | char b = reinterpret_cast<const char*>(bytes)[i]; |
| 469 | ret[(i * 2)] = kHexChars[(b >> 4) & 0xf]; |
| 470 | ret[(i * 2) + 1] = kHexChars[b & 0xf]; |
| 471 | } |
| 472 | return ret; |
| 473 | } |
| 474 |
| 475 | std::string HexEncode(base::span<const uint8_t> bytes) { |
| 476 | return HexEncode(bytes.data(), bytes.size()); |
| 477 | } |
| 478 |
| 479 | bool HexStringToInt(StringPiece input, int* output) { |
| 480 | return IteratorRangeToNumber<HexIteratorRangeToIntTraits>::Invoke( |
| 481 | input.begin(), input.end(), output); |
| 482 | } |
| 483 |
| 484 | bool HexStringToUInt(StringPiece input, uint32_t* output) { |
| 485 | return IteratorRangeToNumber<HexIteratorRangeToUIntTraits>::Invoke( |
| 486 | input.begin(), input.end(), output); |
| 487 | } |
| 488 |
| 489 | bool HexStringToInt64(StringPiece input, int64_t* output) { |
| 490 | return IteratorRangeToNumber<HexIteratorRangeToInt64Traits>::Invoke( |
| 491 | input.begin(), input.end(), output); |
| 492 | } |
| 493 |
| 494 | bool HexStringToUInt64(StringPiece input, uint64_t* output) { |
| 495 | return IteratorRangeToNumber<HexIteratorRangeToUInt64Traits>::Invoke( |
| 496 | input.begin(), input.end(), output); |
| 497 | } |
| 498 |
| 499 | template <typename Container> |
| 500 | static bool HexStringToByteContainer(StringPiece input, Container* output) { |
| 501 | DCHECK_EQ(output->size(), 0u); |
| 502 | size_t count = input.size(); |
| 503 | if (count == 0 || (count % 2) != 0) |
| 504 | return false; |
| 505 | for (uintptr_t i = 0; i < count / 2; ++i) { |
| 506 | uint8_t msb = 0; // most significant 4 bits |
| 507 | uint8_t lsb = 0; // least significant 4 bits |
| 508 | if (!CharToDigit<16>(input[i * 2], &msb) || |
| 509 | !CharToDigit<16>(input[i * 2 + 1], &lsb)) { |
| 510 | return false; |
| 511 | } |
| 512 | output->push_back((msb << 4) | lsb); |
| 513 | } |
| 514 | return true; |
| 515 | } |
| 516 |
| 517 | bool HexStringToBytes(StringPiece input, std::vector<uint8_t>* output) { |
| 518 | return HexStringToByteContainer(input, output); |
| 519 | } |
| 520 |
| 521 | bool HexStringToString(StringPiece input, std::string* output) { |
| 522 | return HexStringToByteContainer(input, output); |
| 523 | } |
| 524 |
| 525 | bool HexStringToSpan(StringPiece input, base::span<uint8_t> output) { |
| 526 | size_t count = input.size(); |
| 527 | if (count == 0 || (count % 2) != 0) |
| 528 | return false; |
| 529 |
| 530 | if (count / 2 != output.size()) |
| 531 | return false; |
| 532 |
| 533 | for (uintptr_t i = 0; i < count / 2; ++i) { |
| 534 | uint8_t msb = 0; // most significant 4 bits |
| 535 | uint8_t lsb = 0; // least significant 4 bits |
| 536 | if (!CharToDigit<16>(input[i * 2], &msb) || |
| 537 | !CharToDigit<16>(input[i * 2 + 1], &lsb)) { |
| 538 | return false; |
| 539 | } |
| 540 | output[i] = (msb << 4) | lsb; |
| 541 | } |
| 542 | return true; |
| 543 | } |
| 544 |
| 545 | } // namespace base |
| 546 |