| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "net/base/fuzzed_data_provider.h" |
| 6 |
| 7 #include <algorithm> |
| 8 #include <string> |
| 9 |
| 10 #include "base/logging.h" |
| 11 |
| 12 namespace net { |
| 13 |
| 14 FuzzedDataProvider::FuzzedDataProvider(const uint8_t* data, size_t size) |
| 15 : remaining_data_(reinterpret_cast<const char*>(data), size), |
| 16 unused_bits_(0), |
| 17 num_unused_bits_(0) {} |
| 18 |
| 19 FuzzedDataProvider::~FuzzedDataProvider() {} |
| 20 |
| 21 size_t FuzzedDataProvider::ConsumeBytes(char* dest, size_t bytes) { |
| 22 size_t bytes_to_write = std::min(bytes, remaining_data_.length()); |
| 23 memcpy(dest, remaining_data_.data(), bytes_to_write); |
| 24 remaining_data_ = remaining_data_.substr(bytes_to_write); |
| 25 return bytes_to_write; |
| 26 } |
| 27 |
| 28 uint32_t FuzzedDataProvider::ConsumeBits(size_t num_bits) { |
| 29 CHECK_NE(0u, num_bits); |
| 30 uint32_t out = 0; |
| 31 while (num_bits > 0) { |
| 32 if (num_unused_bits_ == 0) { |
| 33 unused_bits_ = 0; |
| 34 if (remaining_data_.length() > 0) { |
| 35 unused_bits_ = remaining_data_.data()[remaining_data_.length() - 1]; |
| 36 remaining_data_ = |
| 37 remaining_data_.substr(0, remaining_data_.length() - 1); |
| 38 } |
| 39 num_unused_bits_ = 8; |
| 40 } |
| 41 size_t bits_to_consume = |
| 42 num_bits <= num_unused_bits_ ? num_bits : num_unused_bits_; |
| 43 uint32_t new_bits = unused_bits_ & ((1 << bits_to_consume) - 1); |
| 44 out = (out << bits_to_consume) + new_bits; |
| 45 unused_bits_ >>= bits_to_consume; |
| 46 num_unused_bits_ -= bits_to_consume; |
| 47 num_bits -= bits_to_consume; |
| 48 } |
| 49 |
| 50 return out; |
| 51 } |
| 52 |
| 53 } // namespace net |
| OLD | NEW |