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

Unified Diff: net/tools/domain_security_preload_generator/bit_writer.h

Issue 2551153003: Add static domain security state generator tool. (Closed)
Patch Set: fix base64 issue and accidental replace. Created 4 years 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/tools/domain_security_preload_generator/bit_writer.h
diff --git a/net/tools/domain_security_preload_generator/bit_writer.h b/net/tools/domain_security_preload_generator/bit_writer.h
new file mode 100644
index 0000000000000000000000000000000000000000..55fee37a72fce7d1f57091bac2833f1422fcff7a
--- /dev/null
+++ b/net/tools/domain_security_preload_generator/bit_writer.h
@@ -0,0 +1,56 @@
+// Copyright (c) 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.
+
+#ifndef NET_TOOLS_DOMAIN_SECURITY_PRELOAD_GENERATOR_BIT_WRITER_H_
+#define NET_TOOLS_DOMAIN_SECURITY_PRELOAD_GENERATOR_BIT_WRITER_H_
+
+#include <stdint.h>
+
+#include <vector>
+
+namespace net {
+
+// BitWriter acts as a buffer to which bits can be written. The bits are stored
+// as bytes in a vector. BitWriter will buffer bytes. When the buffer contains
+// 8 bits they will be appended to the vector automaticly.
agl 2016/12/06 18:51:34 typo.
martijnc 2016/12/07 22:37:53 Fixed.
+class BitWriter {
+ public:
+ BitWriter();
+ ~BitWriter();
+
+ // Append |bit| to the end of the buffer.
+ void WriteBit(uint8_t bit);
+
+ // Append the last |number_of_bits| of |bits| to the end of the buffer.
agl 2016/12/06 18:51:34 does "last" mean "least-significant"?
martijnc 2016/12/07 22:37:53 Yes, clarified in the comment.
+ void WriteBits(uint32_t bits, uint8_t number_of_bits);
+
+ // Appends the buffered bits in |current_byte_| to the |bytes_| vector. When
+ // there are less than 8 bits in the buffer, the empty bits will be filled
+ // with zero's.
+ void Close();
agl 2016/12/06 18:51:34 I think Close would be better named Flush, since i
martijnc 2016/12/07 22:37:53 Renamed.
+ uint32_t position() const { return position_; }
+
+ // Returns a reference to the |bytes_| buffer of this writer. Make sure to
+ // call Close() so that the buffered bits are written to |bytes_| as well.
+ const std::vector<uint8_t>& bytes() const { return bytes_; }
+
+ private:
+ // Returns the minimum number of bits needed to represent |input|.
+ uint8_t BitLength(uint32_t input) const;
+
+ // Buffers bits until it can fill a byte.
+ uint8_t current_byte_;
+
+ // The number of bits currently in |current_byte_|.
+ uint8_t used_;
agl 2016/12/06 18:51:34 I think we're allowed to say " = 0" in class decla
martijnc 2016/12/07 22:37:53 Done.
+
+ // Number of bits in written to this BitWriter.
+ int32_t position_;
agl 2016/12/06 18:51:34 uint32_t (or size_t) to match the return type of |
martijnc 2016/12/07 22:37:53 Done.
+
+ std::vector<uint8_t> bytes_;
+};
+
+} // namespace net
+
+#endif // NET_TOOLS_DOMAIN_SECURITY_PRELOAD_GENERATOR_BIT_WRITER_H_

Powered by Google App Engine
This is Rietveld 408576698