| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 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 #ifndef NET_TOOLS_DOMAIN_SECURITY_PRELOAD_GENERATOR_BIT_WRITER_H_ |
| 6 #define NET_TOOLS_DOMAIN_SECURITY_PRELOAD_GENERATOR_BIT_WRITER_H_ |
| 7 |
| 8 #include <stdint.h> |
| 9 |
| 10 #include <vector> |
| 11 |
| 12 namespace net { |
| 13 |
| 14 namespace transport_security_state { |
| 15 |
| 16 // BitWriter acts as a buffer to which bits can be written. The bits are stored |
| 17 // as bytes in a vector. BitWriter will buffer bits until it contains 8 bits at |
| 18 // which point they will be appended to the vector automatically. |
| 19 class BitWriter { |
| 20 public: |
| 21 BitWriter(); |
| 22 ~BitWriter(); |
| 23 |
| 24 // Appends |bit| to the end of the buffer. |
| 25 void WriteBit(uint8_t bit); |
| 26 |
| 27 // Appends the |number_of_bits| least-significant bits of |bits| to the end of |
| 28 // the buffer. |
| 29 void WriteBits(uint32_t bits, uint8_t number_of_bits); |
| 30 |
| 31 // Appends the buffered bits in |current_byte_| to the |bytes_| vector. When |
| 32 // there are less than 8 bits in the buffer, the empty bits will be filled |
| 33 // with zero's. |
| 34 void Flush(); |
| 35 uint32_t position() const { return position_; } |
| 36 |
| 37 // Returns a reference to |bytes_|. Make sure to call Flush() first so that |
| 38 // the buffered bits are written to |bytes_| as well. |
| 39 const std::vector<uint8_t>& bytes() const { return bytes_; } |
| 40 |
| 41 private: |
| 42 // Returns the minimum number of bits needed to represent |input|. |
| 43 uint8_t BitLength(uint32_t input) const; |
| 44 |
| 45 // Buffers bits until they fill a whole byte. |
| 46 uint8_t current_byte_ = 0; |
| 47 |
| 48 // The number of bits currently in |current_byte_|. |
| 49 uint8_t used_ = 0; |
| 50 |
| 51 // Total number of bits written to this BitWriter. |
| 52 uint32_t position_ = 0; |
| 53 |
| 54 std::vector<uint8_t> bytes_; |
| 55 }; |
| 56 |
| 57 } // namespace transport_security_state |
| 58 |
| 59 } // namespace net |
| 60 |
| 61 #endif // NET_TOOLS_DOMAIN_SECURITY_PRELOAD_GENERATOR_BIT_WRITER_H_ |
| OLD | NEW |