From 480e0310db818ba673ac6c749c20e87dd366e1ef Mon Sep 17 00:00:00 2001 From: Eric Biggers Date: Sat, 30 Jan 2016 21:34:30 -0600 Subject: [PATCH] decompress: fix handling of Huffman codes with one used symbol The previous code did not take into account that 'lens' alias 'decode_table'. --- src/deflate_decompress.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/deflate_decompress.c b/src/deflate_decompress.c index 93fc852..8138474 100644 --- a/src/deflate_decompress.c +++ b/src/deflate_decompress.c @@ -533,6 +533,19 @@ build_decode_table(u32 decode_table[], for (sym = 0; sym < num_syms; sym++) len_counts[lens[sym]]++; + /* Sort the symbols primarily by increasing codeword length and + * secondarily by increasing symbol value. */ + + /* Initialize 'offsets' so that offsets[len] is the number of codewords + * shorter than 'len' bits, including length 0. */ + offsets[0] = 0; + for (len = 0; len < max_codeword_len; len++) + offsets[len + 1] = offsets[len] + len_counts[len]; + + /* Use the 'offsets' array to sort the symbols. */ + for (sym = 0; sym < num_syms; sym++) + sorted_syms[offsets[lens[sym]]++] = sym; + /* It is already guaranteed that all lengths are <= max_codeword_len, * but it cannot be assumed they form a complete prefix code. A * codeword of length n should require a proportion of the codespace @@ -577,19 +590,6 @@ build_decode_table(u32 decode_table[], return false; } - /* Sort the symbols primarily by increasing codeword length and - * secondarily by increasing symbol value. */ - - /* Initialize 'offsets' so that offsets[len] is the number of codewords - * shorter than 'len' bits, including length 0. */ - offsets[0] = 0; - for (len = 0; len < max_codeword_len; len++) - offsets[len + 1] = offsets[len] + len_counts[len]; - - /* Use the 'offsets' array to sort the symbols. */ - for (sym = 0; sym < num_syms; sym++) - sorted_syms[offsets[lens[sym]]++] = sym; - /* Generate the decode table entries. Since we process codewords from * shortest to longest, the main portion of the decode table is filled * first; then the subtables are filled. Note that it's already been