~sircmpwn/hare-compress

40f898d679ccf0f931578b21a022982a3d83957a — Lassi Pulkkinen 1 year, 3 months ago 4a86362
compress::deflate: Fix off-by-one size of counts array

The highest index of the array should be MAXBITS,
hence length = MAXBITS + 1.

Signed-off-by: Lassi Pulkkinen <lassi@pulk.fi>
1 files changed, 3 insertions(+), 3 deletions(-)

M compress/flate/inflate.ha
M compress/flate/inflate.ha => compress/flate/inflate.ha +3 -3
@@ 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);