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 NET_BASE_FUZZED_DATA_PROVIDER_H |
6 #define NET_BASE_FUZZED_DATA_PROVIDER_H | 6 #define NET_BASE_FUZZED_DATA_PROVIDER_H |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
(...skipping 15 matching lines...) Expand all Loading... | |
26 // |num_bytes| of data remain, returns a shorter StringPiece containing all | 26 // |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 | 27 // of the data that's left. The data pointed at by the returned StringPiece |
28 // must not be used after the FuzzedDataProvider is destroyed. | 28 // must not be used after the FuzzedDataProvider is destroyed. |
29 base::StringPiece ConsumeBytes(size_t num_bytes); | 29 base::StringPiece ConsumeBytes(size_t num_bytes); |
30 | 30 |
31 // Returns a StringPiece containing all remaining bytes of the input data. | 31 // 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 | 32 // The data pointed at by the returned StringPiece must not be used after the |
33 // FuzzedDataProvider is destroyed. | 33 // FuzzedDataProvider is destroyed. |
34 base::StringPiece ConsumeRemainingBytes(); | 34 base::StringPiece ConsumeRemainingBytes(); |
35 | 35 |
36 // Returns an unsigned number in the range [min, max] by consuming bytes from | 36 // Returns a number in the range [min, max] by consuming bytes from the input |
37 // the input data. The value might not be uniformly distributed in the given | 37 // data. The value might not be uniformly distributed in the given range. If |
38 // range. If there's no input data left, always returns |min|. |min| must be | 38 // there's no input data left, always returns |min|. |min| must be less than |
39 // less than or equal to |max|. | 39 // or equal to |max|. |
40 uint32_t ConsumeValueInRange(uint32_t min, uint32_t max); | 40 uint32_t ConsumeUint32InRange(uint32_t min, uint32_t max); |
41 int ConsumeInt32InRange(int min, int max); | |
41 | 42 |
42 // Returns a bool, or false when no data remains. | 43 // Returns a bool, or false when no data remains. |
43 bool ConsumeBool(); | 44 bool ConsumeBool(); |
44 | 45 |
45 // Returns a uint8_t from the input or 0 if nothing remains. This is | 46 // Returns a uint8_t from the input or 0 if nothing remains. This is |
46 // equivalent to ConsumeValueInRange(0, 0xFF). | 47 // equivalent to ConsumeUint32InRange(0, 0xFF). |
47 uint8_t ConsumeUint8(); | 48 uint8_t ConsumeUint8(); |
48 | 49 |
49 // Returns a uint16_t from the input. If fewer than 2 bytes of data remain | 50 // Returns a uint16_t from the input. If fewer than 2 bytes of data remain |
50 // will fill the most significant bytes with 0. This is equivalent to | 51 // will fill the most significant bytes with 0. This is equivalent to |
51 // ConsumeValueInRange(0, 0xFFFF). | 52 // ConsumeUint32InRange(0, 0xFFFF). |
52 uint16_t ConsumeUint16(); | 53 uint16_t ConsumeUint16(); |
53 | 54 |
55 // Returns a value from |array|, consuming as many bytes as needed to do so. | |
56 // |array| must be a fixed-size array. Equivalent to | |
57 // array[ConsumeUint32InRange(sizeof(array)-1)]; | |
58 template <typename Type, int size> | |
eroman
2016/06/01 01:47:21
nit: size_t -- Although I can't imagine it making
mmenke
2016/06/01 21:21:51
Done.
| |
59 Type PickArrayEntry(Type (&array)[size]) { | |
mmenke
2016/05/19 19:09:46
Open to other names here. "ConsumeArrayEntry" or
eroman
2016/06/01 01:47:21
Yep this is what I was thinking.
Current name is
mmenke
2016/06/01 21:21:51
I like the name much better... Actually, though,
| |
60 return array[ConsumeUint32InRange(0, size - 1)]; | |
61 } | |
62 | |
54 // Reports the remaining bytes available for fuzzed input. | 63 // Reports the remaining bytes available for fuzzed input. |
55 size_t remaining_bytes() { return remaining_data_.length(); } | 64 size_t remaining_bytes() { return remaining_data_.length(); } |
56 | 65 |
57 private: | 66 private: |
58 base::StringPiece remaining_data_; | 67 base::StringPiece remaining_data_; |
59 | 68 |
60 DISALLOW_COPY_AND_ASSIGN(FuzzedDataProvider); | 69 DISALLOW_COPY_AND_ASSIGN(FuzzedDataProvider); |
61 }; | 70 }; |
62 | 71 |
63 } // namespace net | 72 } // namespace net |
64 | 73 |
65 #endif // NET_BASE_FUZZED_DATA_PROVIDER_H | 74 #endif // NET_BASE_FUZZED_DATA_PROVIDER_H |
OLD | NEW |