1 // Copyright 2019 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 <limits>
9 
10 #include "base/check_op.h"
11 #include "base/stl_util.h"
12 #include "components/base32/base32.h"
13 
GetBase32EncodePolicyFromUint8(uint8_t value)14 base32::Base32EncodePolicy GetBase32EncodePolicyFromUint8(uint8_t value) {
15   // Dummy switch to detect changes to the enum definition.
16   switch (base32::Base32EncodePolicy()) {
17     case base32::Base32EncodePolicy::INCLUDE_PADDING:
18     case base32::Base32EncodePolicy::OMIT_PADDING:
19       break;
20   }
21 
22   return (value % 2) == 0 ? base32::Base32EncodePolicy::INCLUDE_PADDING
23                           : base32::Base32EncodePolicy::OMIT_PADDING;
24 }
25 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)26 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
27   if (size < 2 || size > std::numeric_limits<size_t>::max() / 5)
28     return 0;
29 
30   const base32::Base32EncodePolicy encode_policy =
31       GetBase32EncodePolicyFromUint8(data[0]);
32   const base::StringPiece string_piece_input(
33       reinterpret_cast<const char*>(data + 1), size - 1);
34   std::string encoded_string =
35       base32::Base32Encode(string_piece_input, encode_policy);
36   std::string decoded_string = base32::Base32Decode(encoded_string);
37 
38   CHECK_EQ(string_piece_input, decoded_string);
39   return 0;
40 }
41