Update std/flate's decoder.history_index.

This commit is contained in:
Nigel Tao committed 2017-09-30 11:56:30 +10:00
1 parent 3b57548b0d
commit 04e8a73d10
5 files changed
+148 -30

No files matched your search

+14
View File
@@ -179,6 +179,7 @@ typedef struct {
uint32_t coro_susp_point;
puffs_flate_status v_z;
uint64_t v_n;
uint32_t v_already_full;
} c_decode[1];
struct {
uint32_t coro_susp_point;
@@ -669,6 +670,7 @@ puffs_flate_status puffs_flate_decoder_decode(puffs_flate_decoder* self,
puffs_base_buf1_mark v_m1;
puffs_base_slice_u8 v_written;
uint64_t v_n;
uint32_t v_already_full;
uint8_t* b_wptr_dst = NULL;
uint8_t* b_wend_dst = NULL;
@@ -691,6 +693,7 @@ puffs_flate_status puffs_flate_decoder_decode(puffs_flate_decoder* self,
v_m1 = ((puffs_base_buf1_mark){});
v_written = ((puffs_base_slice_u8){});
v_n = self->private_impl.c_decode[0].v_n;
v_already_full = self->private_impl.c_decode[0].v_already_full;
}
switch (coro_susp_point) {
PUFFS_COROUTINE_SUSPENSION_POINT(0);
@@ -746,6 +749,16 @@ puffs_flate_status puffs_flate_decoder_decode(puffs_flate_decoder* self,
puffs_base_make_slice_u8_from_ints(self->private_impl.f_history,
0, 32768, 32768),
v_written);
self->private_impl.f_history_index =
(((uint32_t)((v_n & 32767))) + 32768);
} else {
v_already_full = 0;
if (self->private_impl.f_history_index >= 32768) {
v_already_full = 32768;
}
self->private_impl.f_history_index =
((self->private_impl.f_history_index & 32767) +
((uint32_t)((v_n & 32767))) + v_already_full);
}
}
}
@@ -760,6 +773,7 @@ suspend:
self->private_impl.c_decode[0].coro_susp_point = coro_susp_point;
self->private_impl.c_decode[0].v_z = v_z;
self->private_impl.c_decode[0].v_n = v_n;
self->private_impl.c_decode[0].v_already_full = v_already_full;
exit:
if (a_dst.buf) {
+1
View File
@@ -179,6 +179,7 @@ typedef struct {
uint32_t coro_susp_point;
puffs_flate_status v_z;
uint64_t v_n;
uint32_t v_already_full;
} c_decode[1];
struct {
uint32_t coro_susp_point;
+30 -6
View File
@@ -94,7 +94,7 @@ pub struct decoder?(
//
// history[history_index & 0x7FFF] is where the next byte of decoded output
// will be written.
history[32 * 1024] u8,
history[0x8000] u8, // 32 KiB.
history_index u32,
// code_lengths is used to pass out-of-band data to init_huff.
@@ -112,18 +112,42 @@ pub func decoder.decode?(dst writer1, src reader1)() {
// modify the state of in.dst, so future mutations (via the slice) can
// change the veracity of any in.dst assertions?
var written[] u8 = in.dst.slice(m0:m0, m1:m1)
if written.length() >= (32 * 1024) {
written = written.suffix(up_to:32 * 1024)
// Append written, the decoded output, to the history ringbuffer.
if written.length() >= 0x8000 {
// If written is longer than the ringbuffer, we can ignore the
// previous value of history_index, as we will overwrite the whole
// ringbuffer.
written = written.suffix(up_to:0x8000)
this.history[:].copy_from(s:written)
this.history_index = 32 * 1024
this.history_index = 0x8000
} else {
// TODO: fill part of the history.
// Otherwise, append written to the history ringbuffer starting at
// the previous history_index (modulo 0x8000).
var n u64 = this.history[this.history_index & 0x7FFF:].copy_from(s:written)
if n < written.length() {
// a_slice.copy_from(s:b_slice) returns the minimum of the two
// slice lengths. If that value is less than b_slice.length(),
// then not all of b_slice was copied.
//
// In terms of the history ringbuffer, that means that we have
// to wrap around and copy the remainder of written over the
// start of the history ringbuffer.
written = written[n:]
n = this.history[:].copy_from(s:written)
// Set history_index (modulo 0x8000) to the length of this
// remainder. The &0x7FFF is redundant, but proves to the
// compiler that the conversion to u32 will not overflow. The
// +0x8000 is to maintain that the history ringbuffer is full
// if and only if history_index >= 0x8000.
this.history_index = ((n & 0x7FFF) as u32) + 0x8000
} else {
// We didn't need to wrap around.
var already_full u32[..0x8000]
if this.history_index >= 0x8000 {
already_full = 0x8000
}
this.history_index = (this.history_index & 0x7FFF) + ((n & 0x7FFF) as u32) + already_full
}
// TODO: update this.history_index.
}
}
return z
+101 -24
View File
@@ -221,6 +221,41 @@ void test_puffs_flate_decode_split_src() {
}
}
bool do_test_puffs_flate_history(int i,
golden_test* gt,
puffs_base_buf1* src,
puffs_base_buf1* got,
puffs_flate_decoder* dec,
uint32_t starting_history_index,
uint64_t limit,
puffs_flate_status want_s) {
src->ri = gt->src_offset0;
src->wi = gt->src_offset1;
got->ri = 0;
got->wi = 0;
puffs_flate_decoder_constructor(dec, PUFFS_VERSION, 0);
puffs_base_writer1 dst_writer = {.buf = got};
puffs_base_reader1 src_reader = {.buf = src};
dec->private_impl.f_history_index = starting_history_index;
dst_writer.limit.ptr_to_len = &limit;
puffs_flate_status got_s =
puffs_flate_decoder_decode(dec, dst_writer, src_reader);
puffs_flate_decoder_destructor(dec);
// TODO: should want_s's SHORT_WRITE be LIMITED_WRITE?
if (got_s != want_s) {
FAIL("i=%d: starting_history_index=0x%04" PRIX32
": decode status: got %" PRIi32 " (%s), want %" PRIi32 " (%s)",
i, starting_history_index, got_s, puffs_flate_status_string(got_s),
want_s, puffs_flate_status_string(want_s));
return false;
}
return true;
}
void test_puffs_flate_history_full() {
proc_funcname = __func__;
@@ -236,38 +271,20 @@ void test_puffs_flate_history_full() {
return;
}
const int full_history_size = 32 * 1024;
const int full_history_size = 0x8000;
int i;
for (i = -2; i <= +2; i++) {
src.ri = gt->src_offset0;
src.wi = gt->src_offset1;
got.ri = 0;
got.wi = 0;
puffs_flate_decoder dec;
puffs_flate_decoder_constructor(&dec, PUFFS_VERSION, 0);
puffs_base_writer1 dst_writer = {.buf = &got};
puffs_base_reader1 src_reader = {.buf = &src};
uint64_t lim = want.wi + i;
dst_writer.limit.ptr_to_len = &lim;
puffs_flate_status got_s =
puffs_flate_decoder_decode(&dec, dst_writer, src_reader);
puffs_flate_decoder_destructor(&dec);
// TODO: should SHORT_WRITE be LIMITED_WRITE?
puffs_flate_status want_s =
i >= 0 ? PUFFS_FLATE_STATUS_OK : PUFFS_FLATE_SUSPENSION_SHORT_WRITE;
if (got_s != want_s) {
FAIL("i=%d: decode status: got %" PRIi32 " (%s), want %" PRIi32 " (%s)",
i, got_s, puffs_flate_status_string(got_s), want_s,
puffs_flate_status_string(want_s));
if (!do_test_puffs_flate_history(
i, gt, &src, &got, &dec, 0, want.wi + i,
i >= 0 ? PUFFS_FLATE_STATUS_OK
: PUFFS_FLATE_SUSPENSION_SHORT_WRITE)) {
return;
}
uint32_t want_history_index = i >= 0 ? 0 : full_history_size;
if (dec.private_impl.f_history_index != want_history_index) {
FAIL("history_index: got %" PRIu32 ", want %" PRIu32,
FAIL("i=%d: history_index: got %" PRIu32 ", want %" PRIu32, i,
dec.private_impl.f_history_index, want_history_index);
return;
}
@@ -295,6 +312,65 @@ void test_puffs_flate_history_full() {
}
}
void test_puffs_flate_history_partial() {
proc_funcname = __func__;
puffs_base_buf1 src = {.ptr = global_src_buffer, .len = BUFFER_SIZE};
puffs_base_buf1 got = {.ptr = global_got_buffer, .len = BUFFER_SIZE};
golden_test* gt = &flate_pi_gt;
if (!read_file(&src, gt->src_filename)) {
return;
}
uint32_t starting_history_indexes[] = {
0x0000, 0x0001, 0x1234, 0x7FFB, 0x7FFC, 0x7FFD, 0x7FFE, 0x7FFF,
0x8000, 0x8001, 0x9234, 0xFFFB, 0xFFFC, 0xFFFD, 0xFFFE, 0xFFFF,
};
int i;
for (i = 0; i < PUFFS_TESTLIB_ARRAY_SIZE(starting_history_indexes); i++) {
uint32_t starting_history_index = starting_history_indexes[i];
// The flate_pi_gt golden test file decodes to the digits of pi.
const char* fragment = "3.14";
const uint32_t fragment_length = 4;
puffs_flate_decoder dec;
if (!do_test_puffs_flate_history(i, gt, &src, &got, &dec,
starting_history_index, fragment_length,
PUFFS_FLATE_SUSPENSION_SHORT_WRITE)) {
return;
}
bool got_full = dec.private_impl.f_history_index >= 0x8000;
uint32_t got_history_index = dec.private_impl.f_history_index & 0x7FFF;
bool want_full = (starting_history_index + fragment_length) >= 0x8000;
uint32_t want_history_index =
(starting_history_index + fragment_length) & 0x7FFF;
if ((got_full != want_full) || (got_history_index != want_history_index)) {
FAIL("i=%d: starting_history_index=0x%04" PRIX32
": history_index: got %d;%04" PRIX32 ", want %d;%04" PRIX32,
i, starting_history_index, (int)(got_full), got_history_index,
(int)(want_full), want_history_index);
return;
}
int j;
for (j = 0; j < fragment_length; j++) {
uint32_t index = (starting_history_index + j) & 0x7FFF;
uint8_t got = dec.private_impl.f_history[index];
uint8_t want = fragment[j];
if (got != want) {
FAIL("i=%d: starting_history_index=0x%04" PRIX32
": j=%d: got 0x%02" PRIX8 ", want 0x%02" PRIX8,
i, starting_history_index, j, got, want);
return;
}
}
}
}
void test_puffs_flate_table_redirect() {
proc_funcname = __func__;
@@ -580,6 +656,7 @@ proc tests[] = {
test_puffs_flate_decode_romeo_fixed, //
test_puffs_flate_decode_split_src, //
test_puffs_flate_history_full, //
test_puffs_flate_history_partial, //
test_puffs_flate_table_redirect, //
test_puffs_zlib_decode_midsummer, //
test_puffs_zlib_decode_pi, //
+2
View File
@@ -11,6 +11,8 @@
#define BUFFER_SIZE (64 * 1024 * 1024)
#define PALETTE_BUFFER_SIZE (4 * 245)
#define PUFFS_TESTLIB_ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
uint8_t global_got_buffer[BUFFER_SIZE];
uint8_t global_want_buffer[BUFFER_SIZE];
uint8_t global_src_buffer[BUFFER_SIZE];