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
3 // found in the LICENSE file.
4 
5 #include "base/strings/string_number_conversions.h"
6 
7 #include <ctype.h>
8 #include <errno.h>
9 #include <stdlib.h>
10 #include <wctype.h>
11 
12 #include <limits>
13 
14 #include "base/logging.h"
15 #include "base/numerics/safe_conversions.h"
16 #include "base/numerics/safe_math.h"
17 
18 namespace base {
19 
20 namespace {
21 
22 template <typename STR, typename INT>
23 struct IntToStringT {
IntToStringbase::__anonea98a4890111::IntToStringT24   static STR IntToString(INT value) {
25     // log10(2) ~= 0.3 bytes needed per bit or per byte log10(2**8) ~= 2.4.
26     // So round up to allocate 3 output characters per byte, plus 1 for '-'.
27     const size_t kOutputBufSize =
28         3 * sizeof(INT) + std::numeric_limits<INT>::is_signed;
29 
30     // Create the string in a temporary buffer, write it back to front, and
31     // then return the substr of what we ended up using.
32     using CHR = typename STR::value_type;
33     CHR outbuf[kOutputBufSize];
34 
35     // The ValueOrDie call below can never fail, because UnsignedAbs is valid
36     // for all valid inputs.
37     auto res = CheckedNumeric<INT>(value).UnsignedAbs().ValueOrDie();
38 
39     CHR* end = outbuf + kOutputBufSize;
40     CHR* i = end;
41     do {
42       --i;
43       DCHECK(i != outbuf);
44       *i = static_cast<CHR>((res % 10) + '0');
45       res /= 10;
46     } while (res != 0);
47     if (IsValueNegative(value)) {
48       --i;
49       DCHECK(i != outbuf);
50       *i = static_cast<CHR>('-');
51     }
52     return STR(i, end);
53   }
54 };
55 
56 // Utility to convert a character to a digit in a given base
57 template<typename CHAR, int BASE, bool BASE_LTE_10> class BaseCharToDigit {
58 };
59 
60 // Faster specialization for bases <= 10
61 template<typename CHAR, int BASE> class BaseCharToDigit<CHAR, BASE, true> {
62  public:
Convert(CHAR c,uint8_t * digit)63   static bool Convert(CHAR c, uint8_t* digit) {
64     if (c >= '0' && c < '0' + BASE) {
65       *digit = static_cast<uint8_t>(c - '0');
66       return true;
67     }
68     return false;
69   }
70 };
71 
72 // Specialization for bases where 10 < base <= 36
73 template<typename CHAR, int BASE> class BaseCharToDigit<CHAR, BASE, false> {
74  public:
Convert(CHAR c,uint8_t * digit)75   static bool Convert(CHAR c, uint8_t* digit) {
76     if (c >= '0' && c <= '9') {
77       *digit = c - '0';
78     } else if (c >= 'a' && c < 'a' + BASE - 10) {
79       *digit = c - 'a' + 10;
80     } else if (c >= 'A' && c < 'A' + BASE - 10) {
81       *digit = c - 'A' + 10;
82     } else {
83       return false;
84     }
85     return true;
86   }
87 };
88 
89 template <int BASE, typename CHAR>
CharToDigit(CHAR c,uint8_t * digit)90 bool CharToDigit(CHAR c, uint8_t* digit) {
91   return BaseCharToDigit<CHAR, BASE, BASE <= 10>::Convert(c, digit);
92 }
93 
94 // There is an IsUnicodeWhitespace for wchars defined in string_util.h, but it
95 // is locale independent, whereas the functions we are replacing were
96 // locale-dependent. TBD what is desired, but for the moment let's not
97 // introduce a change in behaviour.
98 template<typename CHAR> class WhitespaceHelper {
99 };
100 
101 template<> class WhitespaceHelper<char> {
102  public:
Invoke(char c)103   static bool Invoke(char c) {
104     return 0 != isspace(static_cast<unsigned char>(c));
105   }
106 };
107 
LocalIsWhitespace(CHAR c)108 template<typename CHAR> bool LocalIsWhitespace(CHAR c) {
109   return WhitespaceHelper<CHAR>::Invoke(c);
110 }
111 
112 // IteratorRangeToNumberTraits should provide:
113 //  - a typedef for iterator_type, the iterator type used as input.
114 //  - a typedef for value_type, the target numeric type.
115 //  - static functions min, max (returning the minimum and maximum permitted
116 //    values)
117 //  - constant kBase, the base in which to interpret the input
118 template<typename IteratorRangeToNumberTraits>
119 class IteratorRangeToNumber {
120  public:
121   typedef IteratorRangeToNumberTraits traits;
122   typedef typename traits::iterator_type const_iterator;
123   typedef typename traits::value_type value_type;
124 
125   // Generalized iterator-range-to-number conversion.
126   //
Invoke(const_iterator begin,const_iterator end,value_type * output)127   static bool Invoke(const_iterator begin,
128                      const_iterator end,
129                      value_type* output) {
130     bool valid = true;
131 
132     while (begin != end && LocalIsWhitespace(*begin)) {
133       valid = false;
134       ++begin;
135     }
136 
137     if (begin != end && *begin == '-') {
138       if (!std::numeric_limits<value_type>::is_signed) {
139         valid = false;
140       } else if (!Negative::Invoke(begin + 1, end, output)) {
141         valid = false;
142       }
143     } else {
144       if (begin != end && *begin == '+') {
145         ++begin;
146       }
147       if (!Positive::Invoke(begin, end, output)) {
148         valid = false;
149       }
150     }
151 
152     return valid;
153   }
154 
155  private:
156   // Sign provides:
157   //  - a static function, CheckBounds, that determines whether the next digit
158   //    causes an overflow/underflow
159   //  - a static function, Increment, that appends the next digit appropriately
160   //    according to the sign of the number being parsed.
161   template<typename Sign>
162   class Base {
163    public:
Invoke(const_iterator begin,const_iterator end,typename traits::value_type * output)164     static bool Invoke(const_iterator begin, const_iterator end,
165                        typename traits::value_type* output) {
166       *output = 0;
167 
168       if (begin == end) {
169         return false;
170       }
171 
172       // Note: no performance difference was found when using template
173       // specialization to remove this check in bases other than 16
174       if (traits::kBase == 16 && end - begin > 2 && *begin == '0' &&
175           (*(begin + 1) == 'x' || *(begin + 1) == 'X')) {
176         begin += 2;
177       }
178 
179       for (const_iterator current = begin; current != end; ++current) {
180         uint8_t new_digit = 0;
181 
182         if (!CharToDigit<traits::kBase>(*current, &new_digit)) {
183           return false;
184         }
185 
186         if (current != begin) {
187           if (!Sign::CheckBounds(output, new_digit)) {
188             return false;
189           }
190           *output *= traits::kBase;
191         }
192 
193         Sign::Increment(new_digit, output);
194       }
195       return true;
196     }
197   };
198 
199   class Positive : public Base<Positive> {
200    public:
CheckBounds(value_type * output,uint8_t new_digit)201     static bool CheckBounds(value_type* output, uint8_t new_digit) {
202       if (*output > static_cast<value_type>(traits::max() / traits::kBase) ||
203           (*output == static_cast<value_type>(traits::max() / traits::kBase) &&
204            new_digit > traits::max() % traits::kBase)) {
205         *output = traits::max();
206         return false;
207       }
208       return true;
209     }
Increment(uint8_t increment,value_type * output)210     static void Increment(uint8_t increment, value_type* output) {
211       *output += increment;
212     }
213   };
214 
215   class Negative : public Base<Negative> {
216    public:
CheckBounds(value_type * output,uint8_t new_digit)217     static bool CheckBounds(value_type* output, uint8_t new_digit) {
218       if (*output < traits::min() / traits::kBase ||
219           (*output == traits::min() / traits::kBase &&
220            new_digit > 0 - traits::min() % traits::kBase)) {
221         *output = traits::min();
222         return false;
223       }
224       return true;
225     }
Increment(uint8_t increment,value_type * output)226     static void Increment(uint8_t increment, value_type* output) {
227       *output -= increment;
228     }
229   };
230 };
231 
232 template<typename ITERATOR, typename VALUE, int BASE>
233 class BaseIteratorRangeToNumberTraits {
234  public:
235   typedef ITERATOR iterator_type;
236   typedef VALUE value_type;
min()237   static value_type min() {
238     return std::numeric_limits<value_type>::min();
239   }
max()240   static value_type max() {
241     return std::numeric_limits<value_type>::max();
242   }
243   static const int kBase = BASE;
244 };
245 
246 template<typename ITERATOR>
247 class BaseHexIteratorRangeToIntTraits
248     : public BaseIteratorRangeToNumberTraits<ITERATOR, int, 16> {
249 };
250 
251 template <typename ITERATOR>
252 class BaseHexIteratorRangeToUIntTraits
253     : public BaseIteratorRangeToNumberTraits<ITERATOR, uint32_t, 16> {};
254 
255 template <typename ITERATOR>
256 class BaseHexIteratorRangeToInt64Traits
257     : public BaseIteratorRangeToNumberTraits<ITERATOR, int64_t, 16> {};
258 
259 template <typename ITERATOR>
260 class BaseHexIteratorRangeToUInt64Traits
261     : public BaseIteratorRangeToNumberTraits<ITERATOR, uint64_t, 16> {};
262 
263 typedef BaseHexIteratorRangeToIntTraits<StringPiece::const_iterator>
264     HexIteratorRangeToIntTraits;
265 
266 typedef BaseHexIteratorRangeToUIntTraits<StringPiece::const_iterator>
267     HexIteratorRangeToUIntTraits;
268 
269 typedef BaseHexIteratorRangeToInt64Traits<StringPiece::const_iterator>
270     HexIteratorRangeToInt64Traits;
271 
272 typedef BaseHexIteratorRangeToUInt64Traits<StringPiece::const_iterator>
273     HexIteratorRangeToUInt64Traits;
274 
275 template <typename STR>
HexStringToBytesT(const STR & input,std::vector<uint8_t> * output)276 bool HexStringToBytesT(const STR& input, std::vector<uint8_t>* output) {
277   DCHECK_EQ(output->size(), 0u);
278   size_t count = input.size();
279   if (count == 0 || (count % 2) != 0)
280     return false;
281   for (uintptr_t i = 0; i < count / 2; ++i) {
282     uint8_t msb = 0;  // most significant 4 bits
283     uint8_t lsb = 0;  // least significant 4 bits
284     if (!CharToDigit<16>(input[i * 2], &msb) ||
285         !CharToDigit<16>(input[i * 2 + 1], &lsb))
286       return false;
287     output->push_back((msb << 4) | lsb);
288   }
289   return true;
290 }
291 
292 template <typename VALUE, int BASE>
293 class StringPieceToNumberTraits
294     : public BaseIteratorRangeToNumberTraits<StringPiece::const_iterator,
295                                              VALUE,
296                                              BASE> {
297 };
298 
299 template <typename VALUE>
StringToIntImpl(const StringPiece & input,VALUE * output)300 bool StringToIntImpl(const StringPiece& input, VALUE* output) {
301   return IteratorRangeToNumber<StringPieceToNumberTraits<VALUE, 10> >::Invoke(
302       input.begin(), input.end(), output);
303 }
304 
305 }  // namespace
306 
IntToString(int value)307 std::string IntToString(int value) {
308   return IntToStringT<std::string, int>::IntToString(value);
309 }
310 
UintToString(unsigned int value)311 std::string UintToString(unsigned int value) {
312   return IntToStringT<std::string, unsigned int>::IntToString(value);
313 }
314 
Int64ToString(int64_t value)315 std::string Int64ToString(int64_t value) {
316   return IntToStringT<std::string, int64_t>::IntToString(value);
317 }
318 
Uint64ToString(uint64_t value)319 std::string Uint64ToString(uint64_t value) {
320   return IntToStringT<std::string, uint64_t>::IntToString(value);
321 }
322 
SizeTToString(size_t value)323 std::string SizeTToString(size_t value) {
324   return IntToStringT<std::string, size_t>::IntToString(value);
325 }
326 
DoubleToString(double value)327 std::string DoubleToString(double value) {
328   auto ret = std::to_string(value);
329   // If this returned an integer, don't do anything.
330   if (ret.find('.') == std::string::npos) {
331     return ret;
332   }
333   // Otherwise, it has an annoying tendency to leave trailing zeros.
334   size_t len = ret.size();
335   while (len >= 2 && ret[len - 1] == '0' && ret[len - 2] != '.') {
336     --len;
337   }
338   ret.erase(len);
339   return ret;
340 }
341 
StringToInt(const StringPiece & input,int * output)342 bool StringToInt(const StringPiece& input, int* output) {
343   return StringToIntImpl(input, output);
344 }
345 
StringToUint(const StringPiece & input,unsigned * output)346 bool StringToUint(const StringPiece& input, unsigned* output) {
347   return StringToIntImpl(input, output);
348 }
349 
StringToInt64(const StringPiece & input,int64_t * output)350 bool StringToInt64(const StringPiece& input, int64_t* output) {
351   return StringToIntImpl(input, output);
352 }
353 
StringToUint64(const StringPiece & input,uint64_t * output)354 bool StringToUint64(const StringPiece& input, uint64_t* output) {
355   return StringToIntImpl(input, output);
356 }
357 
StringToSizeT(const StringPiece & input,size_t * output)358 bool StringToSizeT(const StringPiece& input, size_t* output) {
359   return StringToIntImpl(input, output);
360 }
361 
StringToDouble(const std::string & input,double * output)362 bool StringToDouble(const std::string& input, double* output) {
363   char* endptr = nullptr;
364   *output = strtod(input.c_str(), &endptr);
365 
366   // Cases to return false:
367   //  - If the input string is empty, there was nothing to parse.
368   //  - If endptr does not point to the end of the string, there are either
369   //    characters remaining in the string after a parsed number, or the string
370   //    does not begin with a parseable number.  endptr is compared to the
371   //    expected end given the string's stated length to correctly catch cases
372   //    where the string contains embedded NUL characters.
373   //  - If the first character is a space, there was leading whitespace
374   return !input.empty() &&
375          input.c_str() + input.length() == endptr &&
376          !isspace(input[0]) &&
377          *output != std::numeric_limits<double>::infinity() &&
378          *output != -std::numeric_limits<double>::infinity();
379 }
380 
381 // Note: if you need to add String16ToDouble, first ask yourself if it's
382 // really necessary. If it is, probably the best implementation here is to
383 // convert to 8-bit and then use the 8-bit version.
384 
385 // Note: if you need to add an iterator range version of StringToDouble, first
386 // ask yourself if it's really necessary. If it is, probably the best
387 // implementation here is to instantiate a string and use the string version.
388 
HexEncode(const void * bytes,size_t size)389 std::string HexEncode(const void* bytes, size_t size) {
390   static const char kHexChars[] = "0123456789ABCDEF";
391 
392   // Each input byte creates two output hex characters.
393   std::string ret(size * 2, '\0');
394 
395   for (size_t i = 0; i < size; ++i) {
396     char b = reinterpret_cast<const char*>(bytes)[i];
397     ret[(i * 2)] = kHexChars[(b >> 4) & 0xf];
398     ret[(i * 2) + 1] = kHexChars[b & 0xf];
399   }
400   return ret;
401 }
402 
HexStringToInt(const StringPiece & input,int * output)403 bool HexStringToInt(const StringPiece& input, int* output) {
404   return IteratorRangeToNumber<HexIteratorRangeToIntTraits>::Invoke(
405     input.begin(), input.end(), output);
406 }
407 
HexStringToUInt(const StringPiece & input,uint32_t * output)408 bool HexStringToUInt(const StringPiece& input, uint32_t* output) {
409   return IteratorRangeToNumber<HexIteratorRangeToUIntTraits>::Invoke(
410       input.begin(), input.end(), output);
411 }
412 
HexStringToInt64(const StringPiece & input,int64_t * output)413 bool HexStringToInt64(const StringPiece& input, int64_t* output) {
414   return IteratorRangeToNumber<HexIteratorRangeToInt64Traits>::Invoke(
415     input.begin(), input.end(), output);
416 }
417 
HexStringToUInt64(const StringPiece & input,uint64_t * output)418 bool HexStringToUInt64(const StringPiece& input, uint64_t* output) {
419   return IteratorRangeToNumber<HexIteratorRangeToUInt64Traits>::Invoke(
420       input.begin(), input.end(), output);
421 }
422 
HexStringToBytes(const std::string & input,std::vector<uint8_t> * output)423 bool HexStringToBytes(const std::string& input, std::vector<uint8_t>* output) {
424   return HexStringToBytesT(input, output);
425 }
426 
427 }  // namespace base
428