From 8ba367a140890de914ea7b147dfc143304715a44 Mon Sep 17 00:00:00 2001 From: Pompolic <pompolic@special-circumstanc.es> Date: Fri, 24 Sep 2021 21:02:21 +0200 Subject: [PATCH] (WIP) clean up some compiler errors TODO: use HCountedArray or HBytes instead of uint8_t* --- lzw.c | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/lzw.c b/lzw.c index f1a4fcb..8dbb279 100644 --- a/lzw.c +++ b/lzw.c @@ -2,6 +2,11 @@ // XXX lookup is O(1) like this, but maybe memory use will be bad // XXX unify lzw_context_t and lzwspec #include <hammer/hammer.h> +#include <hammer/glue.h> +// malloc, free +#include <stdlib.h> +// strlen +#include <string.h> typedef struct LZW_context_S { @@ -9,7 +14,7 @@ typedef struct LZW_context_S * Table for storing sequences represented by an LZW code * 0-255, and 256 are special, representing literals, and the reset code. We could explicitly pre-fill them, but it's probably not necessary. */ - const char * lzw_code_table[4096]; + uint8_t * lzw_code_table[4096]; /* * Holds the next expected LZW code. We also use this for telling LZW_9bitcodeword, LZW_10bitcodeword, etc. apart. Parses fail if "next" is larger than what can be represented on that many bits. @@ -30,7 +35,7 @@ void LZW_clear_table(LZW_context_T *ctx) */ for(int i = 258; i < ctx->next; ++i) { - const char * sequence = ctx->lzw_code_table[i]; + uint8_t * sequence = ctx->lzw_code_table[i]; if(sequence != NULL) { free(sequence); @@ -49,7 +54,7 @@ act_LZW_firstcode(const HParseResult *p, void *u) LZW_context_T * ctx = (LZW_context_T *) u; uint64_t code = H_CAST_UINT(p->ast); ctx->old = code; - return H_MAKE_BYTES(code, 1); + return H_MAKE_UINT(code); // XXX ideally, there would be a homogenous collection of bytes to merge at the top level } HParsedToken* @@ -95,7 +100,6 @@ validate_LZW_12bitcodeword(const HParseResult *p, void *u) bool validate_LZW_literal(const HParseResult *p, void *u) { - LZW_context_T * ctx = (LZW_context_T *) u; uint64_t code = H_CAST_UINT(p->ast); return (code < 256); } @@ -104,7 +108,7 @@ HParsedToken* act_LZW_literal(const HParseResult *p, void *u) { uint64_t code = H_CAST_UINT(p->ast); - return H_MAKE_BYTES(code, 1); + return H_MAKE_UINT(code); } // TODO: maybe a continuation can be used to remember the previous code @@ -112,16 +116,16 @@ act_LZW_literal(const HParseResult *p, void *u) HParsedToken* act_LZW_codeword(const HParseResult *p, void *u) { - char * prev_string; - char * output; - char * next_entry; + uint8_t * prev_string; + uint8_t * output; + uint8_t * next_entry; uint64_t code = H_CAST_UINT(p->ast); LZW_context_T * ctx = (LZW_context_T *) u; if(ctx->lzw_code_table[code] != NULL) // code is in the table { - char prefix; + uint8_t prefix; size_t prev_string_length; /* @@ -131,13 +135,13 @@ act_LZW_codeword(const HParseResult *p, void *u) output = ctx->lzw_code_table[code]; prefix = output[0]; - prev_string = ctx->lzw_code_table[old]; + prev_string = ctx->lzw_code_table[ctx->old]; prev_string_length = strlen(prev_string); /* * Update the dictionary */ - next_entry = malloc(sizeof(char) * prev_string_length+2); + next_entry = malloc(sizeof(uint8_t) * prev_string_length+2); strncpy(next_entry, prev_string, prev_string_length); next_entry[prev_string_length] = prefix; next_entry[prev_string_length+1] = '\0'; @@ -150,18 +154,18 @@ act_LZW_codeword(const HParseResult *p, void *u) } else // code is not in the table { - char prefix; - char new_prefix; + uint8_t prefix; + uint8_t new_prefix; size_t prev_string_length; - prev_string = ctx->lzw_code_table[old]; + prev_string = ctx->lzw_code_table[ctx->old]; prev_string_length = strlen(prev_string); prefix = prev_string[0]; /* * Put together the string for the current code, then insert it into the table. This will also be the output. */ - output = malloc(sizeof(char) * prev_string_length+2); // +1 for the NULL byte, and +1 for the character we're going to append + output = malloc(sizeof(uint8_t) * prev_string_length+2); // +1 for the NULL byte, and +1 for the character we're going to append strncpy(output, prev_string, prev_string_length); output[prev_string_length] = prefix; output[prev_string_length+1] = '\0'; @@ -171,7 +175,7 @@ act_LZW_codeword(const HParseResult *p, void *u) * Update the dictionary */ new_prefix = output[0]; - next_entry = malloc(sizeof(char) * prev_string_length+2); + next_entry = malloc(sizeof(uint8_t) * prev_string_length+2); strncpy(next_entry, prev_string, prev_string_length); next_entry[prev_string_length] = new_prefix; next_entry[prev_string_length+1] = '\0'; @@ -193,7 +197,7 @@ void init_lzw_parser() H_VRULE(LZW_10bitcodeword, h_bits(10, false)); H_VRULE(LZW_11bitcodeword, h_bits(11, false)); H_VRULE(LZW_12bitcodeword, h_bits(12, false)); - H_ARULE(LZW_codeword, h_nothing_p()); + H_ARULE(LZW_codeword, h_choice(LZW_9bitcodeword, LZW_10bitcodeword, LZW_11bitcodeword, LZW_12bitcodeword, NULL)); H_RULE(LZW_data, h_sequence(LZW_clear, h_many1(h_choice(LZW_literal, LZW_clear, LZW_codeword, NULL)), LZW_eod, NULL)); } -- GitLab