This source file includes following definitions.
- SymbolLengthAndIdCompare
- SymbolIdCompare
- symbol_id
- symbol_id
- Initialize
- BuildEncodeTable
- BuildDecodeTables
- AddDecodeTable
- SetEntry
- IsInitialized
- EncodeString
- DecodeString
#include "net/spdy/hpack_huffman_table.h"
#include <algorithm>
#include <cmath>
#include "base/logging.h"
#include "net/spdy/hpack_input_stream.h"
#include "net/spdy/hpack_output_stream.h"
namespace net {
using base::StringPiece;
using std::string;
namespace {
const uint8 kDecodeTableRootBits = 9;
const uint8 kDecodeTableBranchBits = 6;
bool SymbolLengthAndIdCompare(const HpackHuffmanSymbol& a,
const HpackHuffmanSymbol& b) {
if (a.length == b.length) {
return a.id < b.id;
}
return a.length < b.length;
}
bool SymbolIdCompare(const HpackHuffmanSymbol& a,
const HpackHuffmanSymbol& b) {
return a.id < b.id;
}
}
HpackHuffmanTable::DecodeEntry::DecodeEntry()
: next_table_index(0), length(0), symbol_id(0) {
}
HpackHuffmanTable::DecodeEntry::DecodeEntry(uint8 next_table_index,
uint8 length,
uint16 symbol_id)
: next_table_index(next_table_index), length(length), symbol_id(symbol_id) {
}
size_t HpackHuffmanTable::DecodeTable::size() const {
return size_t(1) << indexed_length;
}
HpackHuffmanTable::HpackHuffmanTable() {}
HpackHuffmanTable::~HpackHuffmanTable() {}
bool HpackHuffmanTable::Initialize(const HpackHuffmanSymbol* input_symbols,
size_t symbol_count) {
CHECK(!IsInitialized());
std::vector<Symbol> symbols(symbol_count);
for (size_t i = 0; i != symbol_count; i++) {
if (i != input_symbols[i].id) {
failed_symbol_id_ = i;
return false;
}
symbols[i] = input_symbols[i];
}
std::sort(symbols.begin(), symbols.end(), SymbolLengthAndIdCompare);
if (symbols[0].code != 0) {
failed_symbol_id_ = 0;
return false;
}
for (size_t i = 1; i != symbols.size(); i++) {
unsigned code_shift = 32 - symbols[i-1].length;
uint32 code = symbols[i-1].code + (1 << code_shift);
if (code != symbols[i].code) {
failed_symbol_id_ = symbols[i].id;
return false;
}
if (code < symbols[i-1].code) {
failed_symbol_id_ = symbols[i].id;
return false;
}
}
if (symbols.back().length < 8) {
return false;
}
pad_bits_ = static_cast<uint8>(symbols.back().code >> 24);
BuildDecodeTables(symbols);
std::sort(symbols.begin(), symbols.end(), SymbolIdCompare);
BuildEncodeTable(symbols);
return true;
}
void HpackHuffmanTable::BuildEncodeTable(const std::vector<Symbol>& symbols) {
for (size_t i = 0; i != symbols.size(); i++) {
const Symbol& symbol = symbols[i];
CHECK_EQ(i, symbol.id);
code_by_id_.push_back(symbol.code);
length_by_id_.push_back(symbol.length);
}
}
void HpackHuffmanTable::BuildDecodeTables(const std::vector<Symbol>& symbols) {
AddDecodeTable(0, kDecodeTableRootBits);
for (std::vector<Symbol>::const_reverse_iterator it = symbols.rbegin();
it != symbols.rend(); ++it) {
uint8 table_index = 0;
while (true) {
const DecodeTable table = decode_tables_[table_index];
uint32 index = (it->code << table.prefix_length);
index = index >> (32 - table.indexed_length);
CHECK_LT(index, table.size());
DecodeEntry entry = Entry(table, index);
uint8 total_indexed = table.prefix_length + table.indexed_length;
if (total_indexed >= it->length) {
entry.length = it->length;
entry.symbol_id = it->id;
entry.next_table_index = table_index;
SetEntry(table, index, entry);
break;
}
if (entry.length == 0) {
CHECK_EQ(entry.next_table_index, 0);
entry.length = it->length;
entry.next_table_index = AddDecodeTable(
total_indexed,
std::min<uint8>(kDecodeTableBranchBits,
entry.length - total_indexed));
SetEntry(table, index, entry);
}
CHECK_NE(entry.next_table_index, table_index);
table_index = entry.next_table_index;
}
}
for (size_t i = 0; i != decode_tables_.size(); i++) {
const DecodeTable& table = decode_tables_[i];
uint8 total_indexed = table.prefix_length + table.indexed_length;
size_t j = 0;
while (j != table.size()) {
const DecodeEntry& entry = Entry(table, j);
if (entry.length != 0 && entry.length < total_indexed) {
size_t fill_count = 1 << (total_indexed - entry.length);
CHECK_LE(j + fill_count, table.size());
for (size_t k = 1; k != fill_count; k++) {
CHECK_EQ(Entry(table, j + k).length, 0);
SetEntry(table, j + k, entry);
}
j += fill_count;
} else {
j++;
}
}
}
}
uint8 HpackHuffmanTable::AddDecodeTable(uint8 prefix, uint8 indexed) {
CHECK_LT(decode_tables_.size(), 255u);
{
DecodeTable table;
table.prefix_length = prefix;
table.indexed_length = indexed;
table.entries_offset = decode_entries_.size();
decode_tables_.push_back(table);
}
decode_entries_.resize(decode_entries_.size() + (size_t(1) << indexed));
return static_cast<uint8>(decode_tables_.size() - 1);
}
const HpackHuffmanTable::DecodeEntry& HpackHuffmanTable::Entry(
const DecodeTable& table,
uint32 index) const {
DCHECK_LT(index, table.size());
DCHECK_LT(table.entries_offset + index, decode_entries_.size());
return decode_entries_[table.entries_offset + index];
}
void HpackHuffmanTable::SetEntry(const DecodeTable& table,
uint32 index,
const DecodeEntry& entry) {
CHECK_LT(index, table.size());
CHECK_LT(table.entries_offset + index, decode_entries_.size());
decode_entries_[table.entries_offset + index] = entry;
}
bool HpackHuffmanTable::IsInitialized() const {
return !code_by_id_.empty();
}
void HpackHuffmanTable::EncodeString(StringPiece in,
HpackOutputStream* out) const {
size_t bit_remnant = 0;
for (size_t i = 0; i != in.size(); i++) {
uint16 symbol_id = static_cast<uint8>(in[i]);
CHECK_GT(code_by_id_.size(), symbol_id);
unsigned length = length_by_id_[symbol_id];
uint32 code = code_by_id_[symbol_id] >> (32 - length);
bit_remnant = (bit_remnant + length) % 8;
if (length > 24) {
out->AppendBits(static_cast<uint8>(code >> 24), length - 24);
length = 24;
}
if (length > 16) {
out->AppendBits(static_cast<uint8>(code >> 16), length - 16);
length = 16;
}
if (length > 8) {
out->AppendBits(static_cast<uint8>(code >> 8), length - 8);
length = 8;
}
out->AppendBits(static_cast<uint8>(code), length);
}
if (bit_remnant != 0) {
out->AppendBits(pad_bits_ >> bit_remnant, 8 - bit_remnant);
}
}
bool HpackHuffmanTable::DecodeString(HpackInputStream* in,
size_t out_capacity,
string* out) const {
const int kDecodeIterations = static_cast<int>(
std::ceil((32.f - kDecodeTableRootBits) / kDecodeTableBranchBits));
out->clear();
uint32 bits = 0;
size_t bits_available = 0;
bool peeked_success = in->PeekBits(&bits_available, &bits);
while (true) {
const DecodeTable* table = &decode_tables_[0];
uint32 index = bits >> (32 - kDecodeTableRootBits);
for (int i = 0; i != kDecodeIterations; i++) {
DCHECK_LT(index, table->size());
DCHECK_LT(Entry(*table, index).next_table_index, decode_tables_.size());
table = &decode_tables_[Entry(*table, index).next_table_index];
index = (bits << table->prefix_length) >> (32 - table->indexed_length);
}
const DecodeEntry& entry = Entry(*table, index);
if (entry.length > bits_available) {
if (!peeked_success) {
in->ConsumeByteRemainder();
return !in->HasMoreData();
}
} else if (entry.length == 0) {
return false;
} else {
if (out->size() == out_capacity) {
return false;
}
if (entry.symbol_id < 256) {
out->push_back(static_cast<char>(entry.symbol_id));
}
in->ConsumeBits(entry.length);
bits = bits << entry.length;
bits_available -= entry.length;
}
peeked_success = in->PeekBits(&bits_available, &bits);
}
NOTREACHED();
return false;
}
}