Check over- and under-subscribed Huffman codes.

This commit is contained in:
Nigel Tao committed 2017-07-28 15:49:05 +10:00
1 parent 3b5e858fea
commit 6b019831ad
3 files changed
+84 -29

No files matched your search

+21 -1
View File
@@ -5,6 +5,8 @@
packageid "flte"
pub error "bad Huffman code (over-subscribed)"
pub error "bad Huffman code (under-subscribed)"
pub error "bad Huffman code length count"
pub error "bad Huffman code length repetition"
pub error "bad Huffman code"
@@ -473,7 +475,25 @@ pri func decoder.init_huff?(which u32[..1], n_codes0 u32[..320], n_codes1 u32[..
// - counts[5] = 2
// - all other counts elements are 0.
// TODO: check for the total code_lengths being too short or too long.
// Check that the Huffman code completely covers all possible input bits.
var remaining u32 = 1 // There is 1 possible 0-bit code.
i = 1
while i <= 15 {
if remaining > (1 << 30) {
return error "internal error: inconsistent Huffman decoder state"
}
// Each iteration doubles the number of possible remaining codes.
remaining <<= 1
if remaining < (this.counts[i] as u32) {
return error "bad Huffman code (over-subscribed)"
}
remaining -= this.counts[i] as u32
i += 1
}
if remaining != 0 {
// TODO: when is a degenerate Huffman table valid?
return error "bad Huffman code (under-subscribed)"
}
var offsets[16] u16[..320]
var n_symbols u32[..320]