| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 #ifndef NET_BASE_FUZZED_DATA_PROVIDER_H | 5 #ifndef BASE_FUZZED_DATA_PROVIDER_H_ |
| 6 #define NET_BASE_FUZZED_DATA_PROVIDER_H | 6 #define BASE_FUZZED_DATA_PROVIDER_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include "base/base_export.h" |
| 10 #include "base/macros.h" | 11 #include "base/macros.h" |
| 11 #include "base/strings/string_piece.h" | 12 #include "base/strings/string_piece.h" |
| 12 | 13 |
| 13 namespace net { | 14 namespace base { |
| 14 | 15 |
| 15 // Utility class to break up fuzzer input for multiple consumers. Whenever run | 16 // Utility class to break up fuzzer input for multiple consumers. Whenever run |
| 16 // on the same input, provides the same output, as long as its methods are | 17 // on the same input, provides the same output, as long as its methods are |
| 17 // called in the same order, with the same arguments. | 18 // called in the same order, with the same arguments. |
| 18 class FuzzedDataProvider { | 19 class BASE_EXPORT FuzzedDataProvider { |
| 19 public: | 20 public: |
| 20 // |data| is an array of length |size| that the FuzzedDataProvider wraps to | 21 // |data| is an array of length |size| that the FuzzedDataProvider wraps to |
| 21 // provide more granular access. |data| must outlive the FuzzedDataProvider. | 22 // provide more granular access. |data| must outlive the FuzzedDataProvider. |
| 22 FuzzedDataProvider(const uint8_t* data, size_t size); | 23 FuzzedDataProvider(const uint8_t* data, size_t size); |
| 23 ~FuzzedDataProvider(); | 24 ~FuzzedDataProvider(); |
| 24 | 25 |
| 25 // Returns a StringPiece containing |num_bytes| of input data. If fewer than | 26 // Returns a StringPiece containing |num_bytes| of input data. If fewer than |
| 26 // |num_bytes| of data remain, returns a shorter StringPiece containing all | 27 // |num_bytes| of data remain, returns a shorter StringPiece containing all |
| 27 // of the data that's left. The data pointed at by the returned StringPiece | 28 // of the data that's left. The data pointed at by the returned StringPiece |
| 28 // must not be used after the FuzzedDataProvider is destroyed. | 29 // must not be used after the FuzzedDataProvider is destroyed. |
| 29 base::StringPiece ConsumeBytes(size_t num_bytes); | 30 StringPiece ConsumeBytes(size_t num_bytes); |
| 30 | 31 |
| 31 // Returns a StringPiece containing all remaining bytes of the input data. | 32 // Returns a StringPiece containing all remaining bytes of the input data. |
| 32 // The data pointed at by the returned StringPiece must not be used after the | 33 // The data pointed at by the returned StringPiece must not be used after the |
| 33 // FuzzedDataProvider is destroyed. | 34 // FuzzedDataProvider is destroyed. |
| 34 base::StringPiece ConsumeRemainingBytes(); | 35 StringPiece ConsumeRemainingBytes(); |
| 35 | 36 |
| 36 // Returns a number in the range [min, max] by consuming bytes from the input | 37 // Returns a number in the range [min, max] by consuming bytes from the input |
| 37 // data. The value might not be uniformly distributed in the given range. If | 38 // data. The value might not be uniformly distributed in the given range. If |
| 38 // there's no input data left, always returns |min|. |min| must be less than | 39 // there's no input data left, always returns |min|. |min| must be less than |
| 39 // or equal to |max|. | 40 // or equal to |max|. |
| 40 uint32_t ConsumeUint32InRange(uint32_t min, uint32_t max); | 41 uint32_t ConsumeUint32InRange(uint32_t min, uint32_t max); |
| 41 int ConsumeInt32InRange(int min, int max); | 42 int ConsumeInt32InRange(int min, int max); |
| 42 | 43 |
| 43 // Returns a bool, or false when no data remains. | 44 // Returns a bool, or false when no data remains. |
| 44 bool ConsumeBool(); | 45 bool ConsumeBool(); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 57 // array[ConsumeUint32InRange(sizeof(array)-1)]; | 58 // array[ConsumeUint32InRange(sizeof(array)-1)]; |
| 58 template <typename Type, size_t size> | 59 template <typename Type, size_t size> |
| 59 Type PickValueInArray(Type (&array)[size]) { | 60 Type PickValueInArray(Type (&array)[size]) { |
| 60 return array[ConsumeUint32InRange(0, size - 1)]; | 61 return array[ConsumeUint32InRange(0, size - 1)]; |
| 61 } | 62 } |
| 62 | 63 |
| 63 // Reports the remaining bytes available for fuzzed input. | 64 // Reports the remaining bytes available for fuzzed input. |
| 64 size_t remaining_bytes() { return remaining_data_.length(); } | 65 size_t remaining_bytes() { return remaining_data_.length(); } |
| 65 | 66 |
| 66 private: | 67 private: |
| 67 base::StringPiece remaining_data_; | 68 StringPiece remaining_data_; |
| 68 | 69 |
| 69 DISALLOW_COPY_AND_ASSIGN(FuzzedDataProvider); | 70 DISALLOW_COPY_AND_ASSIGN(FuzzedDataProvider); |
| 70 }; | 71 }; |
| 71 | 72 |
| 72 } // namespace net | 73 } // namespace base |
| 73 | 74 |
| 74 #endif // NET_BASE_FUZZED_DATA_PROVIDER_H | 75 #endif // BASE_FUZZED_DATA_PROVIDER_H_ |
| OLD | NEW |