OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/packed_ct_ev_whitelist/bit_stream_reader.h" | 5 #include "components/packed_ct_ev_whitelist/bit_stream_reader.h" |
6 | 6 |
7 #include "base/big_endian.h" | 7 #include "base/big_endian.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/numerics/safe_conversions.h" | 9 #include "base/numerics/safe_conversions.h" |
10 | 10 |
(...skipping 16 matching lines...) Expand all Loading... |
27 return true; | 27 return true; |
28 } | 28 } |
29 | 29 |
30 bool BitStreamReader::ReadBits(uint8_t num_bits, uint64_t* out) { | 30 bool BitStreamReader::ReadBits(uint8_t num_bits, uint64_t* out) { |
31 DCHECK_LE(num_bits, 64); | 31 DCHECK_LE(num_bits, 64); |
32 | 32 |
33 if (BitsLeft() < num_bits) | 33 if (BitsLeft() < num_bits) |
34 return false; | 34 return false; |
35 | 35 |
36 *out = 0; | 36 *out = 0; |
37 for (uint8_t i = 0; i < num_bits; ++i) | 37 |
38 (*out) |= (static_cast<uint64_t>(ReadBit()) << (num_bits - (i + 1))); | 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)); |
39 | 44 |
40 return true; | 45 return true; |
41 } | 46 } |
42 | 47 |
43 uint64_t BitStreamReader::BitsLeft() const { | 48 uint64_t BitStreamReader::BitsLeft() const { |
44 if (current_byte_ == source_.length()) | 49 if (current_byte_ == source_.length()) |
45 return 0; | 50 return 0; |
46 DCHECK_GT(source_.length(), current_byte_); | 51 DCHECK_GT(source_.length(), current_byte_); |
47 return (source_.length() - (current_byte_ + 1)) * 8 + current_bit_ + 1; | 52 return (source_.length() - (current_byte_ + 1)) * 8 + current_bit_ + 1; |
48 } | 53 } |
49 | 54 |
50 uint8_t BitStreamReader::ReadBit() { | 55 uint8_t BitStreamReader::ReadBit() { |
51 DCHECK_GT(BitsLeft(), 0u); | 56 DCHECK_GT(BitsLeft(), 0u); |
52 DCHECK(current_bit_ < 8 && current_bit_ >= 0); | 57 DCHECK(current_bit_ < 8 && current_bit_ >= 0); |
53 uint8_t res = | 58 uint8_t res = |
54 (source_.data()[current_byte_] & (1 << current_bit_)) >> current_bit_; | 59 (source_.data()[current_byte_] & (1 << current_bit_)) >> current_bit_; |
55 current_bit_--; | 60 current_bit_--; |
56 if (current_bit_ < 0) { | 61 if (current_bit_ < 0) { |
57 current_byte_++; | 62 current_byte_++; |
58 current_bit_ = 7; | 63 current_bit_ = 7; |
59 } | 64 } |
60 | 65 |
61 return res; | 66 return res; |
62 } | 67 } |
63 | 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 |
64 } // namespace internal | 77 } // namespace internal |
65 } // namespace packed_ct_ev_whitelist | 78 } // namespace packed_ct_ev_whitelist |
OLD | NEW |