mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-21 08:47:28 +01:00
Check over- and under-subscribed Huffman codes.
This commit is contained in:
1 parent
3b5e858fea
commit
6b019831ad
3 files changed
+84
-29
No files matched your search
@@ -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]
|
||||
|
||||
Reference in new issue
Block a user