From b6d6be58c9b377f48e9d6959c1f9f8a3cf73b21f Mon Sep 17 00:00:00 2001 From: Paul Cruz Date: Mon, 14 Aug 2017 14:05:16 -0700 Subject: [PATCH] created separate function for copying literals during sequence execution --- doc/educational_decoder/zstd_decompress.c | 37 ++++++++++++++--------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/doc/educational_decoder/zstd_decompress.c b/doc/educational_decoder/zstd_decompress.c index f45091b16..5b88e4c97 100644 --- a/doc/educational_decoder/zstd_decompress.c +++ b/doc/educational_decoder/zstd_decompress.c @@ -353,6 +353,9 @@ static void execute_sequences(frame_context_t *const ctx, ostream_t *const out, const sequence_command_t *const sequences, const size_t num_sequences); +static u32 copy_literals(sequence_command_t seq, istream_t *litstream, + ostream_t *const out); + /******* END ZSTD HELPER STRUCTS AND PROTOTYPES *******************************/ size_t ZSTD_decompress(void *const dst, const size_t dst_len, @@ -1256,23 +1259,10 @@ static void execute_sequences(frame_context_t *const ctx, ostream_t *const out, for (size_t i = 0; i < num_sequences; i++) { const sequence_command_t seq = sequences[i]; - { - // If the sequence asks for more literals than are left, the - // sequence must be corrupted - if (seq.literal_length > IO_istream_len(&litstream)) { - CORRUPTION(); - } - - u8 *const write_ptr = IO_write_bytes(out, seq.literal_length); - const u8 *const read_ptr = - IO_read_bytes(&litstream, seq.literal_length); - // Copy literals to output - memcpy(write_ptr, read_ptr, seq.literal_length); - - total_output += seq.literal_length; + const u32 literals_size = copy_literals(seq, &litstream, out); + total_output += literals_size; } - size_t offset; // Offsets are special, we need to handle the repeat offsets @@ -1370,6 +1360,23 @@ static void execute_sequences(frame_context_t *const ctx, ostream_t *const out, ctx->current_total_output = total_output; } + +static u32 copy_literals(const sequence_command_t seq, istream_t *litstream, + ostream_t *const out) { + // If the sequence asks for more literals than are left, the + // sequence must be corrupted + if (seq.literal_length > IO_istream_len(litstream)) { + CORRUPTION(); + } + + u8 *const write_ptr = IO_write_bytes(out, seq.literal_length); + const u8 *const read_ptr = + IO_read_bytes(litstream, seq.literal_length); + // Copy literals to output + memcpy(write_ptr, read_ptr, seq.literal_length); + + return seq.literal_length; +} /******* END SEQUENCE EXECUTION ***********************************************/ /******* OUTPUT SIZE COUNTING *************************************************/