From 325c36314a9616746e074de13800b37230e4f388 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sun, 5 Nov 2017 14:13:27 +1100 Subject: [PATCH] Prepare decode_huffman_fast to rewind in.src. --- gen/c/std/flate.c | 3 +++ std/flate/decode_huffman_fast.puffs | 18 +++++++++++++++++- std/flate/decode_huffman_slow.puffs | 2 ++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/gen/c/std/flate.c b/gen/c/std/flate.c index a416b3a0..72edc4cd 100644 --- a/gen/c/std/flate.c +++ b/gen/c/std/flate.c @@ -2328,6 +2328,9 @@ static puffs_flate__status puffs_flate__flate_decoder__decode_huffman_fast( label_6_break:; } label_0_break:; + while (v_n_bits >= 8) { + v_n_bits -= 8; + } self->private_impl.f_bits = v_bits; self->private_impl.f_n_bits = v_n_bits; if (self->private_impl.f_n_bits >= 8) { diff --git a/std/flate/decode_huffman_fast.puffs b/std/flate/decode_huffman_fast.puffs index 6da2ba4b..5d6b4303 100644 --- a/std/flate/decode_huffman_fast.puffs +++ b/std/flate/decode_huffman_fast.puffs @@ -291,7 +291,23 @@ pri func flate_decoder.decode_huffman_fast?(dst writer1, src reader1)() { } } - // TODO: unread some in.src bytes if n_bits >= 8? + // Ensure n_bits < 8 by rewindng in.src, if we loaded too many of its bytes + // into the bits variable. + // + // Note that we can unconditionally call unread_u8 (without resulting in an + // "invalid I/O operation" error code) only because this whole function can + // never suspend, as all of its I/O operations were checked beforehand for + // sufficient buffer space. Otherwise, resuming from the suspension could + // mean that the (possibly different) in.src is no longer rewindable, even + // if conceptually, this function was responsible for reading the bytes we + // want to rewind. + // + // TODO: have the compiler enforce (via some explicit syntax) that this + // function is ProvenNotToSuspend? + while n_bits >= 8 { + n_bits -= 8 + // TODO: in.src.unread_u8?() + } this.bits = bits this.n_bits = n_bits diff --git a/std/flate/decode_huffman_slow.puffs b/std/flate/decode_huffman_slow.puffs index 98395707..5c4927e4 100644 --- a/std/flate/decode_huffman_slow.puffs +++ b/std/flate/decode_huffman_slow.puffs @@ -252,6 +252,8 @@ pri func flate_decoder.decode_huffman_slow?(dst writer1, src reader1)() { } } + // TODO: "assert n_bits < 8"? + this.bits = bits this.n_bits = n_bits