| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2016 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 "net/tools/domain_security_preload_generator/bit_writer.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 |
| 9 namespace net { |
| 10 |
| 11 BitWriter::BitWriter() : current_byte_(0), used_(0), position_(0) {} |
| 12 |
| 13 BitWriter::~BitWriter() {} |
| 14 |
| 15 void BitWriter::WriteBits(uint32_t bits, uint8_t number_of_bits) { |
| 16 CHECK(number_of_bits <= 32); |
| 17 for (uint8_t i = 1; i <= number_of_bits; i++) { |
| 18 uint8_t bit = 1 & (bits >> (number_of_bits - i)); |
| 19 WriteBit(bit); |
| 20 } |
| 21 } |
| 22 |
| 23 void BitWriter::WriteBit(uint8_t bit) { |
| 24 current_byte_ |= bit << (7 - used_); |
| 25 used_++; |
| 26 position_++; |
| 27 |
| 28 if (used_ == 8) { |
| 29 Close(); |
| 30 } |
| 31 } |
| 32 |
| 33 void BitWriter::Close() { |
| 34 bytes_.push_back(current_byte_); |
| 35 |
| 36 used_ = 0; |
| 37 current_byte_ = 0; |
| 38 } |
| 39 |
| 40 uint8_t BitWriter::BitLength(uint32_t input) const { |
| 41 uint8_t number_of_bits = 0; |
| 42 while (input != 0) { |
| 43 number_of_bits++; |
| 44 input >>= 1; |
| 45 } |
| 46 return number_of_bits; |
| 47 } |
| 48 |
| 49 } // namespace net |
| OLD | NEW |