| 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 #include "net/base/fuzzed_data_provider.h" | 5 #include "net/base/fuzzed_data_provider.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <limits> | 8 #include <limits> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 | 11 |
| 12 namespace net { | 12 namespace net { |
| 13 | 13 |
| 14 FuzzedDataProvider::FuzzedDataProvider(const uint8_t* data, size_t size) | 14 FuzzedDataProvider::FuzzedDataProvider(const uint8_t* data, size_t size) |
| 15 : remaining_data_(reinterpret_cast<const char*>(data), size) {} | 15 : remaining_data_(reinterpret_cast<const char*>(data), size) {} |
| 16 | 16 |
| 17 FuzzedDataProvider::~FuzzedDataProvider() {} | 17 FuzzedDataProvider::~FuzzedDataProvider() {} |
| 18 | 18 |
| 19 base::StringPiece FuzzedDataProvider::ConsumeBytes(size_t length) { | 19 base::StringPiece FuzzedDataProvider::ConsumeBytes(size_t length) { |
| 20 length = std::min(length, remaining_data_.length()); | 20 length = std::min(length, remaining_data_.length()); |
| 21 base::StringPiece result(remaining_data_.data(), length); | 21 base::StringPiece result(remaining_data_.data(), length); |
| 22 remaining_data_ = remaining_data_.substr(length); | 22 remaining_data_ = remaining_data_.substr(length); |
| 23 return result; | 23 return result; |
| 24 } | 24 } |
| 25 | 25 |
| 26 base::StringPiece FuzzedDataProvider::ConsumeRemainingBytes() { |
| 27 return ConsumeBytes(remaining_data_.length()); |
| 28 } |
| 29 |
| 26 uint32_t FuzzedDataProvider::ConsumeValueInRange(uint32_t min, uint32_t max) { | 30 uint32_t FuzzedDataProvider::ConsumeValueInRange(uint32_t min, uint32_t max) { |
| 27 CHECK_LE(min, max); | 31 CHECK_LE(min, max); |
| 28 | 32 |
| 29 uint32_t range = max - min; | 33 uint32_t range = max - min; |
| 30 uint32_t offset = 0; | 34 uint32_t offset = 0; |
| 31 uint32_t result = 0; | 35 uint32_t result = 0; |
| 32 | 36 |
| 33 while ((range >> offset) > 0 && !remaining_data_.empty()) { | 37 while ((range >> offset) > 0 && !remaining_data_.empty()) { |
| 34 // Pull bytes off the end of the seed data. Experimentally, this seems to | 38 // Pull bytes off the end of the seed data. Experimentally, this seems to |
| 35 // allow the fuzzer to more easily explore the input space. This makes | 39 // allow the fuzzer to more easily explore the input space. This makes |
| (...skipping 22 matching lines...) Expand all Loading... |
| 58 return ConsumeValueInRange(0, std::numeric_limits<uint32_t>::max()); | 62 return ConsumeValueInRange(0, std::numeric_limits<uint32_t>::max()); |
| 59 return ConsumeValueInRange(0, (1 << num_bits) - 1); | 63 return ConsumeValueInRange(0, (1 << num_bits) - 1); |
| 60 } | 64 } |
| 61 | 65 |
| 62 bool FuzzedDataProvider::ConsumeBool() { | 66 bool FuzzedDataProvider::ConsumeBool() { |
| 63 // Double negation so this returns false once there's no more data. | 67 // Double negation so this returns false once there's no more data. |
| 64 return !!ConsumeBits(1); | 68 return !!ConsumeBits(1); |
| 65 } | 69 } |
| 66 | 70 |
| 67 } // namespace net | 71 } // namespace net |
| OLD | NEW |