| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 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 COMPONENTS_PACKED_CT_EV_WHITELIST_BIT_STREAM_READER_H_ | |
| 6 #define COMPONENTS_PACKED_CT_EV_WHITELIST_BIT_STREAM_READER_H_ | |
| 7 | |
| 8 #include <stddef.h> | |
| 9 #include <stdint.h> | |
| 10 | |
| 11 #include "base/macros.h" | |
| 12 #include "base/strings/string_piece.h" | |
| 13 | |
| 14 namespace packed_ct_ev_whitelist { | |
| 15 namespace internal { | |
| 16 | |
| 17 // A class for reading individual bits from a packed buffer. Bits are read | |
| 18 // MSB-first from the stream. | |
| 19 // It is limited to 64-bit reads, 4GB streams and is inefficient as a design | |
| 20 // choice. This class should not be used frequently. | |
| 21 // | |
| 22 // It is meant for data that is is packed across bytes, necessitating the need | |
| 23 // to read a variable number of bits across a byte boundary. | |
| 24 class BitStreamReader { | |
| 25 public: | |
| 26 explicit BitStreamReader(const base::StringPiece& source); | |
| 27 | |
| 28 // Reads unary-encoded number into |out|. Returns true if | |
| 29 // there was at least one bit to read, false otherwise. | |
| 30 bool ReadUnaryEncoding(uint64_t* out); | |
| 31 | |
| 32 // Reads |num_bits| (up to 64) into |out|. |out| is filled from the MSB to the | |
| 33 // LSB. If |num_bits| is less than 64, the most significant |64 - num_bits| | |
| 34 // bits are unused and left as zeros. Returns true if the stream had the | |
| 35 // requested |num_bits|, false otherwise. | |
| 36 bool ReadBits(uint8_t num_bits, uint64_t* out); | |
| 37 | |
| 38 // Returns the number of bits left in the stream. | |
| 39 uint64_t BitsLeft() const; | |
| 40 | |
| 41 private: | |
| 42 // Reads a single bit. Within a byte, the bits are read from the MSB to the | |
| 43 // LSB. | |
| 44 uint8_t ReadBit(); | |
| 45 | |
| 46 // Reads a single byte. | |
| 47 // Precondition: The stream must be byte-aligned (current_bit_ == 7) before | |
| 48 // calling this function. | |
| 49 uint8_t ReadByte(); | |
| 50 | |
| 51 const base::StringPiece source_; | |
| 52 | |
| 53 // Index of the byte currently being read from. | |
| 54 size_t current_byte_; | |
| 55 | |
| 56 // Index of the last bit read within |current_byte_|. Since bits are read | |
| 57 // from the MSB to the LSB, this value is initialized to 7 and decremented | |
| 58 // after each read. | |
| 59 int8_t current_bit_; | |
| 60 | |
| 61 DISALLOW_COPY_AND_ASSIGN(BitStreamReader); | |
| 62 }; | |
| 63 | |
| 64 } // namespace internal | |
| 65 } // namespace packed_ct_ev_whitelist | |
| 66 | |
| 67 #endif // COMPONENTS_PACKED_CT_EV_WHITELIST_BIT_STREAM_READER_H_ | |
| OLD | NEW |