Avi Drissman | 8ba1bad | 2022-09-13 19:22:36 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors |
Daniel Hosseinian | b9bc53e | 2019-10-22 02:21:53 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Peter Kasting | ccea0983 | 2025-01-27 18:38:22 | [diff] [blame] | 5 | #include "components/base32/base32.h" |
| 6 | |
Daniel Hosseinian | b9bc53e | 2019-10-22 02:21:53 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | #include <stdint.h> |
| 9 | |
Peter Kasting | ccea0983 | 2025-01-27 18:38:22 | [diff] [blame] | 10 | #include <algorithm> |
Daniel Hosseinian | b9bc53e | 2019-10-22 02:21:53 | [diff] [blame] | 11 | #include <limits> |
Tom Sepez | 46f69d89 | 2023-11-10 23:39:10 | [diff] [blame] | 12 | #include <string> |
| 13 | #include <vector> |
Daniel Hosseinian | b9bc53e | 2019-10-22 02:21:53 | [diff] [blame] | 14 | |
Tom Sepez | 46f69d89 | 2023-11-10 23:39:10 | [diff] [blame] | 15 | #include "base/check.h" |
| 16 | #include "base/containers/span.h" |
Daniel Hosseinian | b9bc53e | 2019-10-22 02:21:53 | [diff] [blame] | 17 | |
| 18 | base32::Base32EncodePolicy GetBase32EncodePolicyFromUint8(uint8_t value) { |
| 19 | // Dummy switch to detect changes to the enum definition. |
| 20 | switch (base32::Base32EncodePolicy()) { |
| 21 | case base32::Base32EncodePolicy::INCLUDE_PADDING: |
| 22 | case base32::Base32EncodePolicy::OMIT_PADDING: |
| 23 | break; |
| 24 | } |
| 25 | |
| 26 | return (value % 2) == 0 ? base32::Base32EncodePolicy::INCLUDE_PADDING |
| 27 | : base32::Base32EncodePolicy::OMIT_PADDING; |
| 28 | } |
| 29 | |
| 30 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
| 31 | if (size < 2 || size > std::numeric_limits<size_t>::max() / 5) |
| 32 | return 0; |
| 33 | |
| 34 | const base32::Base32EncodePolicy encode_policy = |
| 35 | GetBase32EncodePolicyFromUint8(data[0]); |
Elly | a88dc72 | 2024-09-04 22:32:29 | [diff] [blame] | 36 | // SAFETY: libfuzzer guarantees that there are size bytes available at data. |
| 37 | UNSAFE_BUFFERS( |
| 38 | const base::span<const uint8_t> input_bytes(data + 1, size - 1)); |
Tom Sepez | 46f69d89 | 2023-11-10 23:39:10 | [diff] [blame] | 39 | std::string encoded_string = base32::Base32Encode(input_bytes, encode_policy); |
| 40 | std::vector<uint8_t> decoded_bytes = base32::Base32Decode(encoded_string); |
Peter Kasting | ccea0983 | 2025-01-27 18:38:22 | [diff] [blame] | 41 | CHECK(std::ranges::equal(input_bytes, decoded_bytes)); |
Daniel Hosseinian | b9bc53e | 2019-10-22 02:21:53 | [diff] [blame] | 42 | return 0; |
| 43 | } |