Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(409)

Unified Diff: net/base/fuzzed_data_provider.cc

Issue 1917503002: URLRequest fuzzer. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@fuzz
Patch Set: Remove tab Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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) {
+ unused_bits_ = remaining_data_.data()[remaining_data_.length() - 1];
+ 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_;
+ uint32_t new_bits = unused_bits_ & ((1 << bits_to_consume) - 1);
+ out = (out << bits_to_consume) + new_bits;
+ unused_bits_ >>= bits_to_consume;
+ num_unused_bits_ -= bits_to_consume;
+ num_bits -= bits_to_consume;
+ }
+
+ return out;
+}
+
+} // namespace net

Powered by Google App Engine
This is Rietveld 408576698