Chromium Code Reviews| Index: net/base/fuzzed_data_provider.cc |
| diff --git a/net/base/fuzzed_data_provider.cc b/net/base/fuzzed_data_provider.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..efc450f3f2b66f0255848f4a9608a4f131fe9209 |
| --- /dev/null |
| +++ b/net/base/fuzzed_data_provider.cc |
| @@ -0,0 +1,53 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "net/base/fuzzed_data_provider.h" |
| + |
| +#include <algorithm> |
| +#include <string> |
| + |
| +#include "base/logging.h" |
| + |
| +namespace net { |
| + |
| +FuzzedDataProvider::FuzzedDataProvider(const uint8_t* data, size_t size) |
| + : remaining_data_(reinterpret_cast<const char*>(data), size), |
| + unused_bits_(0), |
| + num_unused_bits_(0) {} |
| + |
| +FuzzedDataProvider::~FuzzedDataProvider() {} |
| + |
| +size_t FuzzedDataProvider::ConsumeBytes(char* dest, size_t bytes) { |
| + size_t bytes_to_write = std::min(bytes, remaining_data_.length()); |
| + memcpy(dest, remaining_data_.data(), bytes_to_write); |
| + remaining_data_ = remaining_data_.substr(bytes_to_write); |
| + return bytes_to_write; |
| +} |
| + |
| +uint32_t FuzzedDataProvider::ConsumeBits(size_t num_bits) { |
| + CHECK_NE(0u, num_bits); |
| + uint32_t out = 0; |
| + while (num_bits > 0) { |
| + if (num_unused_bits_ == 0) { |
| + unused_bits_ = 0; |
| + if (remaining_data_.length() > 0) { |
|
eroman
2016/04/22 22:07:10
nit: !x.empty()
mmenke
2016/04/27 19:53:24
Done.
|
| + unused_bits_ = remaining_data_.data()[remaining_data_.length() - 1]; |
|
eroman
2016/04/22 22:07:10
Why does ConsumeBits() and ConsumeBytes() pull fro
mmenke
2016/04/25 15:09:18
For consistency. Remember that the fuzzer takes p
eroman
2016/04/25 16:07:12
OK, if you have run tests and believe this to be a
mmenke
2016/04/27 19:53:24
I have now run tests. I ran each method twice wit
|
| + remaining_data_ = |
| + remaining_data_.substr(0, remaining_data_.length() - 1); |
| + } |
| + num_unused_bits_ = 8; |
| + } |
| + size_t bits_to_consume = |
| + num_bits <= num_unused_bits_ ? num_bits : num_unused_bits_; |
|
eroman
2016/04/22 22:07:10
Or how about:
bits_to_consume = std::min(num_bits,
mmenke
2016/04/27 19:53:24
Now unused is a hard-coded 8, and "std::min(num_bi
|
| + uint32_t new_bits = unused_bits_ & ((1 << bits_to_consume) - 1); |
| + out = (out << bits_to_consume) + new_bits; |
|
eroman
2016/04/22 22:07:10
Use | instead of +. It will be equivalent in this
mmenke
2016/04/27 19:53:24
Done.
|
| + unused_bits_ >>= bits_to_consume; |
| + num_unused_bits_ -= bits_to_consume; |
| + num_bits -= bits_to_consume; |
| + } |
| + |
| + return out; |
| +} |
| + |
| +} // namespace net |