Use C-style comments in the brotli decoder.

This commit is contained in:
Zoltan Szabadka
2013-12-16 14:45:57 +01:00
parent 931479d735
commit 30625ba238
13 changed files with 491 additions and 476 deletions
+47 -45
View File
@@ -1,18 +1,19 @@
// Copyright 2013 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Utilities for building and looking up Huffman trees.
/* Copyright 2013 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Utilities for building and looking up Huffman trees.
*/
#include <assert.h>
#include <stdlib.h>
@@ -28,7 +29,7 @@ extern "C" {
#define MAX_ALLOWED_CODE_LENGTH 15
static void TreeNodeInit(HuffmanTreeNode* const node) {
node->children_ = -1; // means: 'unassigned so far'
node->children_ = -1; /* means: 'unassigned so far' */
}
static int NodeIsEmpty(const HuffmanTreeNode* const node) {
@@ -52,15 +53,15 @@ static void AssignChildren(HuffmanTree* const tree,
static int TreeInit(HuffmanTree* const tree, int num_leaves) {
assert(tree != NULL);
if (num_leaves == 0) return 0;
// We allocate maximum possible nodes in the tree at once.
// Note that a Huffman tree is a full binary tree; and in a full binary tree
// with L leaves, the total number of nodes N = 2 * L - 1.
/* We allocate maximum possible nodes in the tree at once. */
/* Note that a Huffman tree is a full binary tree; and in a full binary */
/* tree with L leaves, the total number of nodes N = 2 * L - 1. */
tree->max_nodes_ = 2 * num_leaves - 1;
assert(tree->max_nodes_ < (1 << 16)); // limit for the lut_jump_ table
assert(tree->max_nodes_ < (1 << 16)); /* limit for the lut_jump_ table */
tree->root_ = (HuffmanTreeNode*)BrotliSafeMalloc((uint64_t)tree->max_nodes_,
sizeof(*tree->root_));
if (tree->root_ == NULL) return 0;
TreeNodeInit(tree->root_); // Initialize root.
TreeNodeInit(tree->root_); /* Initialize root. */
tree->num_nodes_ = 1;
memset(tree->lut_bits_, 255, sizeof(tree->lut_bits_));
memset(tree->lut_jump_, 0, sizeof(tree->lut_jump_));
@@ -76,9 +77,9 @@ void BrotliHuffmanTreeRelease(HuffmanTree* const tree) {
}
}
// Utility: converts Huffman code lengths to corresponding Huffman codes.
// 'huff_codes' should be pre-allocated.
// Returns false in case of error (memory allocation, invalid codes).
/* Utility: converts Huffman code lengths to corresponding Huffman codes. */
/* 'huff_codes' should be pre-allocated. */
/* Returns false in case of error (memory allocation, invalid codes). */
static int HuffmanCodeLengthsToCodes(const uint8_t* const code_lengths,
int code_lengths_size,
int* const huff_codes) {
@@ -93,7 +94,7 @@ static int HuffmanCodeLengthsToCodes(const uint8_t* const code_lengths,
assert(code_lengths_size > 0);
assert(huff_codes != NULL);
// Calculate max code length.
/* Calculate max code length. */
for (symbol = 0; symbol < code_lengths_size; ++symbol) {
if (code_lengths[symbol] > max_code_length) {
max_code_length = code_lengths[symbol];
@@ -101,23 +102,24 @@ static int HuffmanCodeLengthsToCodes(const uint8_t* const code_lengths,
}
if (max_code_length > MAX_ALLOWED_CODE_LENGTH) return 0;
// Calculate code length histogram.
/* Calculate code length histogram. */
for (symbol = 0; symbol < code_lengths_size; ++symbol) {
++code_length_hist[code_lengths[symbol]];
}
code_length_hist[0] = 0;
// Calculate the initial values of 'next_codes' for each code length.
// next_codes[code_len] denotes the code to be assigned to the next symbol
// of code length 'code_len'.
/* Calculate the initial values of 'next_codes' for each code length. */
/* next_codes[code_len] denotes the code to be assigned to the next symbol */
/* of code length 'code_len'. */
curr_code = 0;
next_codes[0] = -1; // Unused, as code length = 0 implies code doesn't exist.
next_codes[0] = -1; /* Unused, as code length = 0 implies */
/* code doesn't exist. */
for (code_len = 1; code_len <= max_code_length; ++code_len) {
curr_code = (curr_code + code_length_hist[code_len - 1]) << 1;
next_codes[code_len] = curr_code;
}
// Get symbols.
/* Get symbols. */
for (symbol = 0; symbol < code_lengths_size; ++symbol) {
if (code_lengths[symbol] > 0) {
huff_codes[symbol] = next_codes[code_lengths[symbol]]++;
@@ -169,10 +171,10 @@ static int TreeAddSymbol(HuffmanTree* const tree,
return 0;
}
if (NodeIsEmpty(node)) {
if (IsFull(tree)) return 0; // error: too many symbols.
if (IsFull(tree)) return 0; /* error: too many symbols. */
AssignChildren(tree, node);
} else if (!HuffmanTreeNodeIsNotLeaf(node)) {
return 0; // leaf is already occupied.
return 0; /* leaf is already occupied. */
}
node += node->children_ + ((code >> code_length) & 1);
if (--step == 0) {
@@ -180,11 +182,11 @@ static int TreeAddSymbol(HuffmanTree* const tree,
}
}
if (NodeIsEmpty(node)) {
node->children_ = 0; // turn newly created node into a leaf.
node->children_ = 0; /* turn newly created node into a leaf. */
} else if (HuffmanTreeNodeIsNotLeaf(node)) {
return 0; // trying to assign a symbol to already used code.
return 0; /* trying to assign a symbol to already used code. */
}
node->symbol_ = symbol; // Add symbol in this node.
node->symbol_ = symbol; /* Add symbol in this node. */
return 1;
}
@@ -198,30 +200,30 @@ int BrotliHuffmanTreeBuildImplicit(HuffmanTree* const tree,
assert(tree != NULL);
assert(code_lengths != NULL);
// Find out number of symbols and the root symbol.
/* Find out number of symbols and the root symbol. */
for (symbol = 0; symbol < code_lengths_size; ++symbol) {
if (code_lengths[symbol] > 0) {
// Note: code length = 0 indicates non-existent symbol.
/* Note: code length = 0 indicates non-existent symbol. */
++num_symbols;
root_symbol = symbol;
}
}
// Initialize the tree. Will fail for num_symbols = 0
/* Initialize the tree. Will fail for num_symbols = 0 */
if (!TreeInit(tree, num_symbols)) return 0;
// Build tree.
if (num_symbols == 1) { // Trivial case.
/* Build tree. */
if (num_symbols == 1) { /* Trivial case. */
const int max_symbol = code_lengths_size;
if (root_symbol < 0 || root_symbol >= max_symbol) {
BrotliHuffmanTreeRelease(tree);
return 0;
}
return TreeAddSymbol(tree, root_symbol, 0, 0);
} else { // Normal case.
} else { /* Normal case. */
int ok = 0;
// Get Huffman codes from the code lengths.
/* Get Huffman codes from the code lengths. */
int* const codes =
(int*)BrotliSafeMalloc((uint64_t)code_lengths_size, sizeof(*codes));
if (codes == NULL) goto End;
@@ -230,7 +232,7 @@ int BrotliHuffmanTreeBuildImplicit(HuffmanTree* const tree,
goto End;
}
// Add symbols one-by-one.
/* Add symbols one-by-one. */
for (symbol = 0; symbol < code_lengths_size; ++symbol) {
if (code_lengths[symbol] > 0) {
if (!TreeAddSymbol(tree, symbol, codes[symbol], code_lengths[symbol])) {
@@ -248,5 +250,5 @@ int BrotliHuffmanTreeBuildImplicit(HuffmanTree* const tree,
}
#if defined(__cplusplus) || defined(c_plusplus)
} // extern "C"
} /* extern "C" */
#endif