lzw: Simplify new code handling.

The tiny overhead of an extra time through the output loop
is worth the simpler code.
This commit is contained in:
Michael Drake 2021-03-31 19:04:02 +01:00 committed by Michael Drake
parent 21c565d5d6
commit f66e20e017

View File

@ -4,6 +4,7 @@
* http://www.opensource.org/licenses/mit-license.php * http://www.opensource.org/licenses/mit-license.php
* *
* Copyright 2017 Michael Drake <michael.drake@codethink.co.uk> * Copyright 2017 Michael Drake <michael.drake@codethink.co.uk>
* Copyright 2021 Michael Drake <tlsa@netsurf-browser.org>
*/ */
#include <assert.h> #include <assert.h>
@ -307,7 +308,6 @@ lzw_result lzw_decode(struct lzw_ctx *ctx,
{ {
lzw_result res; lzw_result res;
uint32_t code_new; uint32_t code_new;
uint32_t code_out;
uint8_t last_value; uint8_t last_value;
uint8_t *stack_pos = ctx->stack_base; uint8_t *stack_pos = ctx->stack_base;
uint32_t clear_code = ctx->clear_code; uint32_t clear_code = ctx->clear_code;
@ -335,12 +335,9 @@ lzw_result lzw_decode(struct lzw_ctx *ctx,
} else if (code_new < current_entry) { } else if (code_new < current_entry) {
/* Code is in table */ /* Code is in table */
code_out = code_new;
last_value = table[code_new].first_value; last_value = table[code_new].first_value;
} else { } else {
/* Code not in table */ /* Code not in table */
*stack_pos++ = ctx->previous_code_first;
code_out = ctx->previous_code;
last_value = ctx->previous_code_first; last_value = ctx->previous_code_first;
} }
@ -366,15 +363,13 @@ lzw_result lzw_decode(struct lzw_ctx *ctx,
ctx->previous_code_first = table[code_new].first_value; ctx->previous_code_first = table[code_new].first_value;
ctx->previous_code = code_new; ctx->previous_code = code_new;
/* Put rest of data for this code on output stack. /* Put data for this code on output stack. */
* Note, in the case of "code not in table", the last entry of the while (code_new > clear_code) {
* current code has already been placed on the stack above. */ struct lzw_dictionary_entry *entry = table + code_new;
while (code_out > clear_code) {
struct lzw_dictionary_entry *entry = table + code_out;
*stack_pos++ = entry->last_value; *stack_pos++ = entry->last_value;
code_out = entry->previous_entry; code_new = entry->previous_entry;
} }
*stack_pos++ = table[code_out].last_value; *stack_pos++ = table[code_new].last_value;
*stack_pos_out = stack_pos; *stack_pos_out = stack_pos;
return LZW_OK; return LZW_OK;