Clean up extension confirmation prompts and make them consistent between Views and...
[chromium-blink-merge.git] / components / packed_ct_ev_whitelist / bit_stream_reader.cc
blob94ef00904bbd9ae79758a6f24552022be90184bb
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 {
12 namespace internal {
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) {
20 if (BitsLeft() == 0)
21 return false;
23 *out = 0;
24 while ((BitsLeft() > 0) && ReadBit())
25 ++(*out);
27 return true;
30 bool BitStreamReader::ReadBits(uint8_t num_bits, uint64_t* out) {
31 DCHECK_LE(num_bits, 64);
33 if (BitsLeft() < num_bits)
34 return false;
36 *out = 0;
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));
45 return true;
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;
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;
66 return res;
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