* This file is part of wimlib, a library for working with WIM files.
*
* wimlib is free software; you can redistribute it and/or modify it under the
- * terms of the GNU Lesser General Public License as published by the Free
- * Software Foundation; either version 2.1 of the License, or (at your option)
+ * terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 3 of the License, or (at your option)
* any later version.
*
* wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
- * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
+ * A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
- * You should have received a copy of the GNU Lesser General Public License
+ * You should have received a copy of the GNU General Public License
* along with wimlib; if not, see http://www.gnu.org/licenses/.
*/
static inline void flush_bits(struct output_bitstream *ostream)
{
- *(u16*)ostream->bit_output = to_le16(ostream->bitbuf);
+ *(u16*)ostream->bit_output = cpu_to_le16(ostream->bitbuf);
ostream->bit_output = ostream->next_bit_output;
ostream->next_bit_output = ostream->output;
ostream->output += 2;
/* Writes @num_bits bits, given by the @num_bits least significant bits of
* @bits, to the output @ostream. */
-int bitstream_put_bits(struct output_bitstream *ostream, output_bitbuf_t bits,
+int bitstream_put_bits(struct output_bitstream *ostream, output_bitbuf_t bits,
uint num_bits)
{
uint rem_bits;
ostream->free_bits -= num_bits;
} else {
- if (ostream->num_bytes_remaining + (ostream->output -
+ if (ostream->num_bytes_remaining + (ostream->output -
ostream->bit_output) < 2)
return 1;
/* Flushes any remaining bits in the output buffer to the output byte stream. */
int flush_output_bitstream(struct output_bitstream *ostream)
{
- if (ostream->num_bytes_remaining + (ostream->output -
+ if (ostream->num_bytes_remaining + (ostream->output -
ostream->bit_output) < 2)
return 1;
if (ostream->free_bits != 16) {
/* Initializes an output bit buffer to write its output to the memory location
* pointer to by @data. */
-void init_output_bitstream(struct output_bitstream *ostream, void *data,
+void init_output_bitstream(struct output_bitstream *ostream, void *data,
uint num_bytes)
{
+ wimlib_assert(num_bytes >= 4);
+
ostream->bitbuf = 0;
ostream->free_bits = 16;
ostream->bit_output = (u8*)data;
}
}
-/* Creates a canonical Huffman code from an array of symbol frequencies.
+/* Creates a canonical Huffman code from an array of symbol frequencies.
*
* The algorithm used is similar to the well-known algorithm that builds a
* Huffman tree using a minheap. In that algorithm, the leaf nodes are
* codewords for each symbol will be written.
*
* @codewords: An array of @num_syms short integers into which the
- * codewords for each symbol will be written. The first
- * lens[i] bits of codewords[i] will contain the codeword
+ * codewords for each symbol will be written. The first
+ * lens[i] bits of codewords[i] will contain the codeword
* for symbol i.
*/
-void make_canonical_huffman_code(uint num_syms, uint max_codeword_len,
- const u32 freq_tab[], u8 lens[],
+void make_canonical_huffman_code(uint num_syms, uint max_codeword_len,
+ const u32 freq_tab[], u8 lens[],
u16 codewords[])
{
/* We require at least 2 possible symbols in the alphabet to produce a
* most num_used_symbols - 1 intermediate nodes when creating a Huffman
* code. This is because if there were at least num_used_symbols nodes,
* the code would be suboptimal because there would be at least one
- * unnecessary intermediate node.
+ * unnecessary intermediate node.
*
* The worst case (greatest number of intermediate nodes) would be if
* all the intermediate nodes were chained together. This results in
/* Pointer to the leaf node of lowest frequency that hasn't already been
* added as the child of some intermediate note. */
- HuffmanLeafNode *cur_leaf = &leaves[0];
+ HuffmanLeafNode *cur_leaf;
/* Pointer past the end of the array of leaves. */
HuffmanLeafNode *end_leaf = &leaves[num_used_symbols];
/* Pointer to the intermediate node of lowest frequency. */
- HuffmanNode *cur_inode = &inodes[0];
+ HuffmanNode *cur_inode;
/* Pointer to the next unallocated intermediate node. */
- HuffmanNode *next_inode = &inodes[0];
+ HuffmanNode *next_inode;
/* Only jump back to here if the maximum length of the codewords allowed
* by the LZX format (16 bits) is exceeded. */
while (1) {
/* Lowest frequency node. */
- HuffmanNode *f1 = NULL;
+ HuffmanNode *f1 = NULL;
/* Second lowest frequency node. */
HuffmanNode *f2 = NULL;
* the remaining leaves or from the intermediate nodes.
* */
- if (cur_leaf != end_leaf && (cur_inode == next_inode ||
+ if (cur_leaf != end_leaf && (cur_inode == next_inode ||
cur_leaf->freq <= cur_inode->freq)) {
f1 = (HuffmanNode*)cur_leaf++;
} else if (cur_inode != next_inode) {
f1 = cur_inode++;
}
- if (cur_leaf != end_leaf && (cur_inode == next_inode ||
+ if (cur_leaf != end_leaf && (cur_inode == next_inode ||
cur_leaf->freq <= cur_inode->freq)) {
f2 = (HuffmanNode*)cur_leaf++;
} else if (cur_inode != next_inode) {
if (leaves[i].freq > 1)
leaves[i].freq >>= 1;
goto try_building_tree_again;
- }
+ }
next_inode++;
}