OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/string_number_conversions.h" | 5 #include "base/string_number_conversions.h" |
6 | 6 |
7 #include <ctype.h> | 7 #include <ctype.h> |
8 #include <errno.h> | 8 #include <errno.h> |
9 #include <stdlib.h> | 9 #include <stdlib.h> |
10 #include <wctype.h> | 10 #include <wctype.h> |
(...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
295 template<typename ITERATOR> | 295 template<typename ITERATOR> |
296 class BaseHexIteratorRangeToIntTraits | 296 class BaseHexIteratorRangeToIntTraits |
297 : public BaseIteratorRangeToNumberTraits<ITERATOR, int, 16> { | 297 : public BaseIteratorRangeToNumberTraits<ITERATOR, int, 16> { |
298 public: | 298 public: |
299 // Allow parsing of 0xFFFFFFFF, which is technically an overflow | 299 // Allow parsing of 0xFFFFFFFF, which is technically an overflow |
300 static unsigned int max() { | 300 static unsigned int max() { |
301 return std::numeric_limits<unsigned int>::max(); | 301 return std::numeric_limits<unsigned int>::max(); |
302 } | 302 } |
303 }; | 303 }; |
304 | 304 |
| 305 template<typename ITERATOR> |
| 306 class BaseHexIteratorRangeToInt64Traits |
| 307 : public BaseIteratorRangeToNumberTraits<ITERATOR, int64, 16> { |
| 308 public: |
| 309 // Allow parsing of 0xFFFFFFFFFFFFFFFF, which is technically an overflow |
| 310 static uint64 max() { |
| 311 return std::numeric_limits<uint64>::max(); |
| 312 } |
| 313 }; |
| 314 |
305 typedef BaseHexIteratorRangeToIntTraits<StringPiece::const_iterator> | 315 typedef BaseHexIteratorRangeToIntTraits<StringPiece::const_iterator> |
306 HexIteratorRangeToIntTraits; | 316 HexIteratorRangeToIntTraits; |
307 | 317 |
| 318 typedef BaseHexIteratorRangeToInt64Traits<StringPiece::const_iterator> |
| 319 HexIteratorRangeToInt64Traits; |
| 320 |
308 template<typename STR> | 321 template<typename STR> |
309 bool HexStringToBytesT(const STR& input, std::vector<uint8>* output) { | 322 bool HexStringToBytesT(const STR& input, std::vector<uint8>* output) { |
310 DCHECK_EQ(output->size(), 0u); | 323 DCHECK_EQ(output->size(), 0u); |
311 size_t count = input.size(); | 324 size_t count = input.size(); |
312 if (count == 0 || (count % 2) != 0) | 325 if (count == 0 || (count % 2) != 0) |
313 return false; | 326 return false; |
314 for (uintptr_t i = 0; i < count / 2; ++i) { | 327 for (uintptr_t i = 0; i < count / 2; ++i) { |
315 uint8 msb = 0; // most significant 4 bits | 328 uint8 msb = 0; // most significant 4 bits |
316 uint8 lsb = 0; // least significant 4 bits | 329 uint8 lsb = 0; // least significant 4 bits |
317 if (!CharToDigit<16>(input[i * 2], &msb) || | 330 if (!CharToDigit<16>(input[i * 2], &msb) || |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
474 ret[(i * 2) + 1] = kHexChars[b & 0xf]; | 487 ret[(i * 2) + 1] = kHexChars[b & 0xf]; |
475 } | 488 } |
476 return ret; | 489 return ret; |
477 } | 490 } |
478 | 491 |
479 bool HexStringToInt(const StringPiece& input, int* output) { | 492 bool HexStringToInt(const StringPiece& input, int* output) { |
480 return IteratorRangeToNumber<HexIteratorRangeToIntTraits>::Invoke( | 493 return IteratorRangeToNumber<HexIteratorRangeToIntTraits>::Invoke( |
481 input.begin(), input.end(), output); | 494 input.begin(), input.end(), output); |
482 } | 495 } |
483 | 496 |
| 497 bool HexStringToInt64(const StringPiece& input, int64* output) { |
| 498 return IteratorRangeToNumber<HexIteratorRangeToInt64Traits>::Invoke( |
| 499 input.begin(), input.end(), output); |
| 500 } |
| 501 |
484 bool HexStringToBytes(const std::string& input, std::vector<uint8>* output) { | 502 bool HexStringToBytes(const std::string& input, std::vector<uint8>* output) { |
485 return HexStringToBytesT(input, output); | 503 return HexStringToBytesT(input, output); |
486 } | 504 } |
487 | 505 |
488 } // namespace base | 506 } // namespace base |
OLD | NEW |