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 packed buffer. Bits are read | |
15 // MSB-first from the stream. | |
16 // It is limited to 64-bit reads, 4GB streams and is inefficient as a design | |
17 // choice. This class should not be used frequently. | |
18 // | |
19 // It is meant for data that is is packed across bytes, necessitating the need | |
20 // to read a variable number of bits across a byte boundary. | |
21 class BitStreamReader { | |
22 public: | |
23 explicit BitStreamReader(const base::StringPiece& source); | |
24 | |
25 // Reads unary-encoded number into |out|. Returns true if | |
26 // there was at least one bit to read, false otherwise. | |
27 bool ReadUnaryEncoding(uint64_t* out); | |
28 | |
29 // Reads |num_bits| (up to 64) into |out|. |out| is filled from the MSB to the | |
30 // LSB. If |num_bits| is less than 64, the most significant |64 - num_bits| | |
31 // bits are unused and left as zeros. Returns true if the stream had the | |
32 // requested |num_bits|, false otherwise. | |
33 bool ReadBits(uint8_t num_bits, uint64_t* out); | |
34 | |
35 // Returns the number of bits left in the stream. | |
36 uint64_t BitsLeft() const; | |
37 | |
38 private: | |
39 // Reads a single bit. Within a byte, the bits are read from the MSB to the | |
40 // LSB. | |
41 uint8_t ReadBit(); | |
42 | |
43 const base::StringPiece source_; | |
44 | |
45 // Index of the byte currently being read from. | |
46 size_t current_byte_; | |
47 | |
48 // Index of the last bit read within |current_byte_|. Since bits are read | |
49 // from the MSB to the LSB, this value is initialized to 7 and decremented | |
50 // after each read. | |
51 int8 current_bit_; | |
52 | |
53 DISALLOW_COPY_AND_ASSIGN(BitStreamReader); | |
54 }; | |
55 | |
56 } // namespace internal | |
57 | |
58 #endif // CHROME_BROWSER_NET_BIT_STREAM_READER_H_ | |
OLD | NEW |