| 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/trie/trie_bit_buffer.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 |
| 9 namespace net { |
| 10 |
| 11 TrieBitBuffer::TrieBitBuffer() : current_byte_(0), used_(0) {} |
| 12 |
| 13 TrieBitBuffer::~TrieBitBuffer() {} |
| 14 |
| 15 void TrieBitBuffer::WriteBit(uint8_t bit) { |
| 16 current_byte_ |= bit << (7 - used_); |
| 17 used_++; |
| 18 |
| 19 if (used_ == 8) { |
| 20 Close(); |
| 21 } |
| 22 } |
| 23 |
| 24 void TrieBitBuffer::WriteBits(uint32_t bits, uint8_t number_of_bits) { |
| 25 CHECK(number_of_bits <= 32); |
| 26 for (uint8_t i = 1; i <= number_of_bits; i++) { |
| 27 uint8_t bit = 1 & (bits >> (number_of_bits - i)); |
| 28 WriteBit(bit); |
| 29 } |
| 30 } |
| 31 |
| 32 void TrieBitBuffer::WritePosition(int position, int* last_position) { |
| 33 if (*last_position != -1) { |
| 34 int delta = position - *last_position; |
| 35 CHECK(delta > 0) << "delta position is not positive."; |
| 36 |
| 37 uint8_t number_of_bits = BitLength(delta); |
| 38 CHECK(number_of_bits <= 7 + 15) << "positive position delta too large."; |
| 39 |
| 40 if (number_of_bits <= 7) { |
| 41 WriteBits(0, 1); |
| 42 WriteBits(delta, 7); |
| 43 } else { |
| 44 WriteBits(1, 1); |
| 45 WriteBits(number_of_bits - 8, 4); |
| 46 WriteBits(delta, number_of_bits); |
| 47 } |
| 48 |
| 49 *last_position = position; |
| 50 return; |
| 51 } |
| 52 |
| 53 if (used_ != 0) { |
| 54 Close(); |
| 55 } |
| 56 |
| 57 AppendPositionElement(position); |
| 58 |
| 59 *last_position = position; |
| 60 } |
| 61 |
| 62 uint8_t TrieBitBuffer::BitLength(int input) const { |
| 63 uint8_t number_of_bits = 0; |
| 64 while (input != 0) { |
| 65 number_of_bits++; |
| 66 input >>= 1; |
| 67 } |
| 68 return number_of_bits; |
| 69 } |
| 70 |
| 71 void TrieBitBuffer::WriteChar(uint8_t byte, |
| 72 const HuffmanRepresentationTable& table, |
| 73 HuffmanFrequencyTracker* tracker) { |
| 74 HuffmanRepresentationTable::const_iterator item; |
| 75 item = table.find(byte); |
| 76 CHECK(item != table.end()); |
| 77 if (tracker) { |
| 78 tracker->RecordUsage(byte); |
| 79 } |
| 80 WriteBits(item->second.bits, item->second.number_of_bits); |
| 81 } |
| 82 |
| 83 void TrieBitBuffer::AppendBitsElement(uint8_t bits, uint8_t number_of_bits) { |
| 84 BitsOrPosition element; |
| 85 element.bits = current_byte_; |
| 86 element.number_of_bits = used_; |
| 87 elements_.push_back(element); |
| 88 } |
| 89 |
| 90 void TrieBitBuffer::AppendPositionElement(int position) { |
| 91 BitsOrPosition element; |
| 92 element.position = position; |
| 93 element.number_of_bits = 0; |
| 94 elements_.push_back(element); |
| 95 } |
| 96 |
| 97 uint32_t TrieBitBuffer::WriteToBitWriter(BitWriter& writer) { |
| 98 Close(); |
| 99 |
| 100 uint32_t old_position = writer.position(); |
| 101 for (auto const& element : elements_) { |
| 102 if (element.number_of_bits) { |
| 103 writer.WriteBits(element.bits >> (8 - element.number_of_bits), |
| 104 element.number_of_bits); |
| 105 } else { |
| 106 uint32_t current = old_position; |
| 107 uint32_t target = element.position; |
| 108 CHECK(target < current) << "Reference is not backwards"; |
| 109 uint32_t delta = current - target; |
| 110 uint8_t delta_number_of_bits = BitLength(delta); |
| 111 CHECK(delta_number_of_bits < 32) << "Delta to large"; |
| 112 writer.WriteBits(delta_number_of_bits, 5); |
| 113 writer.WriteBits(delta, delta_number_of_bits); |
| 114 } |
| 115 } |
| 116 return old_position; |
| 117 } |
| 118 |
| 119 void TrieBitBuffer::Close() { |
| 120 if (used_) { |
| 121 AppendBitsElement(current_byte_, used_); |
| 122 |
| 123 used_ = 0; |
| 124 current_byte_ = 0; |
| 125 } |
| 126 } |
| 127 |
| 128 } // namespace net |
| OLD | NEW |