summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Drake <tlsa@netsurf-browser.org>2021-03-31 19:40:35 +0100
committerMichael Drake <tlsa@netsurf-browser.org>2021-04-05 14:51:52 +0100
commit55c00227e1ad7cd7e03cc377fdaf052d57a56a06 (patch)
tree60c02bf9953c05de4b8986ae60e47c9569c0269c
parent86786f4cc6800fa6c6282f8a87e2defacebeb94d (diff)
downloadlibnsgif-55c00227e1ad7cd7e03cc377fdaf052d57a56a06.tar.gz
libnsgif-55c00227e1ad7cd7e03cc377fdaf052d57a56a06.tar.bz2
lzw: Split out dictionary augmentation.
-rw-r--r--src/lzw.c35
1 files changed, 21 insertions, 14 deletions
diff --git a/src/lzw.c b/src/lzw.c
index a5097ab..1d145b4 100644
--- a/src/lzw.c
+++ b/src/lzw.c
@@ -303,6 +303,24 @@ lzw_result lzw_decode_init(
return lzw__clear_codes(ctx, stack_pos_out);
}
+/**
+ * Create new dictionary entry.
+ *
+ * \param[in] ctx LZW reading context, updated.
+ * \param[in] code Last value code for new dictionary entry.
+ */
+static inline void lzw__dictionary_add_entry(
+ struct lzw_ctx *ctx,
+ uint32_t code)
+{
+ struct lzw_dictionary_entry *entry = &ctx->table[ctx->current_entry];
+
+ entry->last_value = code;
+ entry->first_value = ctx->previous_code_first;
+ entry->previous_entry = ctx->previous_code;
+
+ ctx->current_entry++;
+}
/* Exported function, documented in lzw.h */
lzw_result lzw_decode(struct lzw_ctx *ctx,
@@ -310,7 +328,6 @@ lzw_result lzw_decode(struct lzw_ctx *ctx,
{
lzw_result res;
uint32_t code_new;
- uint8_t last_value;
uint8_t *stack_pos = ctx->stack_base;
uint32_t clear_code = ctx->clear_code;
uint32_t current_entry = ctx->current_entry;
@@ -334,22 +351,12 @@ lzw_result lzw_decode(struct lzw_ctx *ctx,
} else if (code_new > current_entry) {
/* Code is invalid */
return LZW_BAD_CODE;
-
- } else if (code_new < current_entry) {
- /* Code is in table */
- last_value = table[code_new].first_value;
- } else {
- /* Code not in table */
- last_value = ctx->previous_code_first;
}
- /* Add to the dictionary, only if there's space */
if (current_entry < LZW_DICTIONARY_ENTRY_MAX) {
- struct lzw_dictionary_entry *entry = table + current_entry;
- entry->last_value = last_value;
- entry->first_value = ctx->previous_code_first;
- entry->previous_entry = ctx->previous_code;
- ctx->current_entry++;
+ lzw__dictionary_add_entry(ctx, (code_new < current_entry) ?
+ table[code_new].first_value :
+ ctx->previous_code_first);
}
/* Ensure code size is increased, if needed. */