1 // Copyright 2015 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 <stddef.h>
6 #include <stdint.h>
7 
8 #include <string>
9 #include <vector>
10 
11 #include "base/strings/string_number_conversions.h"
12 
13 // Entry point for LibFuzzer.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)14 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
15   base::StringPiece string_piece_input(reinterpret_cast<const char*>(data),
16                                        size);
17   std::string string_input(reinterpret_cast<const char*>(data), size);
18 
19   int out_int;
20   base::StringToInt(string_piece_input, &out_int);
21   unsigned out_uint;
22   base::StringToUint(string_piece_input, &out_uint);
23   int64_t out_int64;
24   base::StringToInt64(string_piece_input, &out_int64);
25   uint64_t out_uint64;
26   base::StringToUint64(string_piece_input, &out_uint64);
27   size_t out_size;
28   base::StringToSizeT(string_piece_input, &out_size);
29 
30   // Test for StringPiece16 if size is even.
31   if (size % 2 == 0) {
32     base::StringPiece16 string_piece_input16(
33         reinterpret_cast<const base::char16*>(data), size / 2);
34 
35     base::StringToInt(string_piece_input16, &out_int);
36     base::StringToUint(string_piece_input16, &out_uint);
37     base::StringToInt64(string_piece_input16, &out_int64);
38     base::StringToUint64(string_piece_input16, &out_uint64);
39     base::StringToSizeT(string_piece_input16, &out_size);
40   }
41 
42   double out_double;
43   base::StringToDouble(string_input, &out_double);
44 
45   base::HexStringToInt(string_piece_input, &out_int);
46   base::HexStringToUInt(string_piece_input, &out_uint);
47   base::HexStringToInt64(string_piece_input, &out_int64);
48   base::HexStringToUInt64(string_piece_input, &out_uint64);
49   std::vector<uint8_t> out_bytes;
50   base::HexStringToBytes(string_piece_input, &out_bytes);
51 
52   base::HexEncode(data, size);
53 
54   // Convert the numbers back to strings.
55   base::NumberToString(out_int);
56   base::NumberToString16(out_int);
57   base::NumberToString(out_uint);
58   base::NumberToString16(out_uint);
59   base::NumberToString(out_int64);
60   base::NumberToString16(out_int64);
61   base::NumberToString(out_uint64);
62   base::NumberToString16(out_uint64);
63   base::NumberToString(out_double);
64   base::NumberToString16(out_double);
65 
66   return 0;
67 }
68