Chromium Code Reviews| 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 CHROME_BROWSER_NET_BIT_STREAM_READER_H_ | |
| 6 #define CHROME_BROWSER_NET_BIT_STREAM_READER_H_ | |
| 7 | |
| 8 #include <stdint.h> | |
| 9 | |
| 10 #include "base/strings/string_piece.h" | |
| 11 | |
| 12 namespace internal { | |
| 13 | |
| 14 // A class for reading individual bits from a buffer of bytes they are packed | |
| 15 // into. Bits are read MSB-first from the stream. | |
| 16 // It is limited to 64-bit reads and is inefficient as a design choice - Since | |
| 17 // it is used infrequently to unpack the Golomb-coded EV certificate hashes | |
| 18 // whitelist in a blocking thread. | |
|
Ryan Sleevi
2014/10/20 19:18:24
This comment needs rewording. You shouldn't discus
Eran Messeri
2014/10/21 14:59:59
Done.
| |
| 19 class BitStreamReader { | |
| 20 public: | |
| 21 explicit BitStreamReader(const base::StringPiece& source); | |
| 22 | |
| 23 // Reads unary-encoded number into |out|. Returns true if | |
| 24 // there was at least one bit to read, false otherwise. | |
| 25 bool ReadUnaryEncoding(uint64_t* out); | |
| 26 | |
| 27 // Reads |num_bits| (up to 64) into |out|. |out| is filled from the MSB to the | |
| 28 // LSB. If |num_bits| is less than 64, the most significant |64 - num_bits| | |
| 29 // bits are unused and left as zeros. Returns true if the stream had the | |
| 30 // requested |num_bits|, false otherwise. | |
| 31 bool ReadBits(uint8_t num_bits, uint64_t* out); | |
| 32 | |
| 33 // Returns the number of bits left in the stream. | |
| 34 uint64_t BitsLeft() const; | |
| 35 | |
| 36 private: | |
| 37 // Reads a single bit. Within a byte, the bits are read from the MSB to the | |
| 38 // LSB. | |
| 39 uint8_t ReadBit(); | |
| 40 | |
| 41 const base::StringPiece source_; | |
| 42 | |
| 43 // Index of the byte currently being read from. | |
| 44 uint64_t current_byte_; | |
| 45 | |
| 46 // Index of the last bit read within |current_byte_|. Since bits are read | |
| 47 // from the MSB to the LSB, this value is initialized to 7 and decremented | |
| 48 // after each read. | |
| 49 int8 current_bit_; | |
| 50 }; | |
|
Ryan Sleevi
2014/10/20 19:18:25
DISALLOW_COPY_AND_ASSIGN
Eran Messeri
2014/10/21 14:59:59
Done.
| |
| 51 | |
| 52 } // namespace internal | |
| 53 | |
| 54 #endif // CHROME_BROWSER_NET_BIT_STREAM_READER_H_ | |
| OLD | NEW |