]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
created separate function for copying literals during sequence execution
authorPaul Cruz <paulcruz74@fb.com>
Mon, 14 Aug 2017 21:05:16 +0000 (14:05 -0700)
committerPaul Cruz <paulcruz74@fb.com>
Mon, 14 Aug 2017 21:05:16 +0000 (14:05 -0700)
doc/educational_decoder/zstd_decompress.c

index f45091b16735fa32801af88a8176a012c5756db0..5b88e4c975e69ba0138283ae83d7048136cb7968 100644 (file)
@@ -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 *************************************************/