The highest index of the array should be MAXBITS,
hence length = MAXBITS + 1.
Signed-off-by: Lassi Pulkkinen <lassi@pulk.fi>
---
compress/flate/inflate.ha | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/compress/flate/inflate.ha b/compress/flate/inflate.ha
index 2f3a5f4..fd3e9c9 100644
--- a/compress/flate/inflate.ha
+++ b/compress/flate/inflate.ha
@@ -25,7 +25,7 @@ const fixed_dist: huffman = huffman {
};
export type huffman = struct {
- counts: [MAXBITS]u16,
+ counts: [MAXBITS + 1]u16,
symbols: [MAXCODES]u16,
};
@@ -170,7 +170,7 @@ fn copy(d: *decompressor, back: size, n: size) (void | io::error) = {
fn decode(d: *decompressor, h: huffman) (u16 | io::error) = {
let code = 0u32, index = 0u32, first = 0u32;
- for (let i = 1z; i < MAXBITS; i += 1) {
+ for (let i = 1z; i <= MAXBITS; i += 1) {
code |= bits(d, 1)?;
if (code < first + h.counts[i]) {
return h.symbols[index + (code - first)];
@@ -190,7 +190,7 @@ fn construct(h: *huffman, lens: []u16) (void | io::error) = {
};
if (h.counts[0] == len(lens)) return;
- for (let left = 1z, i = 1z; i < MAXBITS; i += 1) {
+ for (let left = 1z, i = 1z; i <= MAXBITS; i += 1) {
left <<= 1;
left -= h.counts[i];
if (left < 0) return wraperror(inflate_err::HUFFMAN);
--
2.39.2
Signed-off-by: Lassi Pulkkinen <lassi@pulk.fi>
---
compress/flate/inflate.ha | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/compress/flate/inflate.ha b/compress/flate/inflate.ha
index fd3e9c9..046ac18 100644
--- a/compress/flate/inflate.ha
+++ b/compress/flate/inflate.ha
@@ -192,8 +192,8 @@ fn construct(h: *huffman, lens: []u16) (void | io::error) = {
for (let left = 1z, i = 1z; i <= MAXBITS; i += 1) {
left <<= 1;
+ if (left < h.counts[i]) return wraperror(inflate_err::HUFFMAN);
left -= h.counts[i];
- if (left < 0) return wraperror(inflate_err::HUFFMAN);
};
let offs: [MAXBITS + 1]u16 = [0...];
--
2.39.2