Roll libvpx 861f35:1fff3e
[chromium-blink-merge.git] / components / packed_ct_ev_whitelist / bit_stream_reader.h
blob45fa5f5a39d7463ad8a1f5855833240b6a2893db
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.
5 #ifndef COMPONENTS_PACKED_CT_EV_WHITELIST_BIT_STREAM_READER_H_
6 #define COMPONENTS_PACKED_CT_EV_WHITELIST_BIT_STREAM_READER_H_
8 #include <stdint.h>
10 #include "base/strings/string_piece.h"
12 namespace packed_ct_ev_whitelist {
13 namespace internal {
15 // A class for reading individual bits from a packed buffer. Bits are read
16 // MSB-first from the stream.
17 // It is limited to 64-bit reads, 4GB streams and is inefficient as a design
18 // choice. This class should not be used frequently.
20 // It is meant for data that is is packed across bytes, necessitating the need
21 // to read a variable number of bits across a byte boundary.
22 class BitStreamReader {
23 public:
24 explicit BitStreamReader(const base::StringPiece& source);
26 // Reads unary-encoded number into |out|. Returns true if
27 // there was at least one bit to read, false otherwise.
28 bool ReadUnaryEncoding(uint64_t* out);
30 // Reads |num_bits| (up to 64) into |out|. |out| is filled from the MSB to the
31 // LSB. If |num_bits| is less than 64, the most significant |64 - num_bits|
32 // bits are unused and left as zeros. Returns true if the stream had the
33 // requested |num_bits|, false otherwise.
34 bool ReadBits(uint8_t num_bits, uint64_t* out);
36 // Returns the number of bits left in the stream.
37 uint64_t BitsLeft() const;
39 private:
40 // Reads a single bit. Within a byte, the bits are read from the MSB to the
41 // LSB.
42 uint8_t ReadBit();
44 const base::StringPiece source_;
46 // Index of the byte currently being read from.
47 size_t current_byte_;
49 // Index of the last bit read within |current_byte_|. Since bits are read
50 // from the MSB to the LSB, this value is initialized to 7 and decremented
51 // after each read.
52 int8 current_bit_;
54 DISALLOW_COPY_AND_ASSIGN(BitStreamReader);
57 } // namespace internal
58 } // namespace packed_ct_ev_whitelist
60 #endif // COMPONENTS_PACKED_CT_EV_WHITELIST_BIT_STREAM_READER_H_