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 #include "components/packed_ct_ev_whitelist/bit_stream_reader.h"
7 #include "base/big_endian.h"
8 #include "base/logging.h"
9 #include "base/numerics/safe_conversions.h"
11 namespace packed_ct_ev_whitelist
{
14 BitStreamReader::BitStreamReader(const base::StringPiece
& source
)
15 : source_(source
), current_byte_(0), current_bit_(7) {
16 DCHECK_LT(source_
.length(), UINT32_MAX
);
19 bool BitStreamReader::ReadUnaryEncoding(uint64_t* out
) {
24 while ((BitsLeft() > 0) && ReadBit())
30 bool BitStreamReader::ReadBits(uint8_t num_bits
, uint64_t* out
) {
31 DCHECK_LE(num_bits
, 64);
33 if (BitsLeft() < num_bits
)
38 for (; num_bits
&& (current_bit_
!= 7); --num_bits
)
39 (*out
) |= (static_cast<uint64_t>(ReadBit()) << (num_bits
- 1));
40 for (; num_bits
/ 8; num_bits
-= 8)
41 (*out
) |= (static_cast<uint64_t>(ReadByte()) << (num_bits
- 8));
42 for (; num_bits
; --num_bits
)
43 (*out
) |= (static_cast<uint64_t>(ReadBit()) << (num_bits
- 1));
48 uint64_t BitStreamReader::BitsLeft() const {
49 if (current_byte_
== source_
.length())
51 DCHECK_GT(source_
.length(), current_byte_
);
52 return (source_
.length() - (current_byte_
+ 1)) * 8 + current_bit_
+ 1;
55 uint8_t BitStreamReader::ReadBit() {
56 DCHECK_GT(BitsLeft(), 0u);
57 DCHECK(current_bit_
< 8 && current_bit_
>= 0);
59 (source_
.data()[current_byte_
] & (1 << current_bit_
)) >> current_bit_
;
61 if (current_bit_
< 0) {
69 uint8_t BitStreamReader::ReadByte() {
70 DCHECK_GT(BitsLeft(), 7u);
71 DCHECK_EQ(current_bit_
, 7);
73 return source_
.data()[current_byte_
++];
77 } // namespace internal
78 } // namespace packed_ct_ev_whitelist