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_
10 #include "base/strings/string_piece.h"
12 namespace packed_ct_ev_whitelist
{
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
{
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;
40 // Reads a single bit. Within a byte, the bits are read from the MSB to the
44 const base::StringPiece source_
;
46 // Index of the byte currently being read from.
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
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_