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 #include "components/packed_ct_ev_whitelist/bit_stream_reader.h" | |
6 | |
7 #include "base/big_endian.h" | |
8 #include "base/logging.h" | |
9 #include "base/numerics/safe_conversions.h" | |
10 | |
11 namespace packed_ct_ev_whitelist { | |
12 namespace internal { | |
13 | |
14 BitStreamReader::BitStreamReader(const base::StringPiece& source) | |
15 : source_(source), current_byte_(0), current_bit_(7) { | |
16 DCHECK_LT(source_.length(), UINT32_MAX); | |
17 } | |
18 | |
19 bool BitStreamReader::ReadUnaryEncoding(uint64_t* out) { | |
20 if (BitsLeft() == 0) | |
21 return false; | |
22 | |
23 *out = 0; | |
24 while ((BitsLeft() > 0) && ReadBit()) | |
25 ++(*out); | |
26 | |
27 return true; | |
28 } | |
29 | |
30 bool BitStreamReader::ReadBits(uint8_t num_bits, uint64_t* out) { | |
31 DCHECK_LE(num_bits, 64); | |
32 | |
33 if (BitsLeft() < num_bits) | |
34 return false; | |
35 | |
36 *out = 0; | |
37 | |
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)); | |
44 | |
45 return true; | |
46 } | |
47 | |
48 uint64_t BitStreamReader::BitsLeft() const { | |
49 if (current_byte_ == source_.length()) | |
50 return 0; | |
51 DCHECK_GT(source_.length(), current_byte_); | |
52 return (source_.length() - (current_byte_ + 1)) * 8 + current_bit_ + 1; | |
53 } | |
54 | |
55 uint8_t BitStreamReader::ReadBit() { | |
56 DCHECK_GT(BitsLeft(), 0u); | |
57 DCHECK(current_bit_ < 8 && current_bit_ >= 0); | |
58 uint8_t res = | |
59 (source_.data()[current_byte_] & (1 << current_bit_)) >> current_bit_; | |
60 current_bit_--; | |
61 if (current_bit_ < 0) { | |
62 current_byte_++; | |
63 current_bit_ = 7; | |
64 } | |
65 | |
66 return res; | |
67 } | |
68 | |
69 uint8_t BitStreamReader::ReadByte() { | |
70 DCHECK_GT(BitsLeft(), 7u); | |
71 DCHECK_EQ(current_bit_, 7); | |
72 | |
73 return source_.data()[current_byte_++]; | |
74 | |
75 } | |
76 | |
77 } // namespace internal | |
78 } // namespace packed_ct_ev_whitelist | |
OLD | NEW |