From 781cb3047f6783e53f69795729ea59f4a28ebc0e Mon Sep 17 00:00:00 2001 From: Christos Choutouridis Date: Sun, 15 Feb 2026 21:19:59 +0200 Subject: [PATCH] FIX: decode_huff off-by-one bug --- source/level_3/material/huff_utils.py | 3 +++ source/material/huff_utils.py | 7 +++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/source/level_3/material/huff_utils.py b/source/level_3/material/huff_utils.py index 9f996ee..8cdd279 100644 --- a/source/level_3/material/huff_utils.py +++ b/source/level_3/material/huff_utils.py @@ -381,11 +381,14 @@ def decode_huff(huff_sec, huff_LUT): while b: N += 1 b = huff_sec[streamIndex + N] + # Skip the N leading '1' bits AND the terminating '0' delimiter. + # The encoder writes: '1'*N + '0' + streamIndex += N +1 N4 = N + 4 escape_word = huff_sec[streamIndex:streamIndex + N4] escape_value = 2 ** N4 + int("".join(map(str, escape_word)), 2) nTupleDec[idx] = escape_value + # We already consumed the delimiter above; now consume only N4 bits. streamIndex += N4 # Apply signs again nTupleDec[escIndex] *= nTupleSign[escIndex] diff --git a/source/material/huff_utils.py b/source/material/huff_utils.py index a8c7fe7..8cdd279 100644 --- a/source/material/huff_utils.py +++ b/source/material/huff_utils.py @@ -381,12 +381,15 @@ def decode_huff(huff_sec, huff_LUT): while b: N += 1 b = huff_sec[streamIndex + N] - streamIndex += N + # Skip the N leading '1' bits AND the terminating '0' delimiter. + # The encoder writes: '1'*N + '0' + + streamIndex += N +1 N4 = N + 4 escape_word = huff_sec[streamIndex:streamIndex + N4] escape_value = 2 ** N4 + int("".join(map(str, escape_word)), 2) nTupleDec[idx] = escape_value - streamIndex += N4 + 1 + # We already consumed the delimiter above; now consume only N4 bits. + streamIndex += N4 # Apply signs again nTupleDec[escIndex] *= nTupleSign[escIndex]