Skip to content
Snippets Groups Projects
Commit 8e7c8676 authored by pompolic's avatar pompolic
Browse files

Use lzw_table_insert to add new codes to the table

parent 6e6c5177
No related branches found
No related tags found
No related merge requests found
......@@ -41,11 +41,12 @@ void LZW_clear_table(LZW_context_T *ctx)
}
/*
* Creates a HBytes from an array of bytes and its length. The HBytes will keep the token pointer, to be freed later in lzw_clear_table or init_lzw_context.
* Creates a HBytes from an array of bytes and its length, and inserts it into the lzw dictionary in ctx.
* Also increments ctx->next. The HBytes will keep the token pointer, to be freed later in lzw_clear_table or init_lzw_context.
*/
void lzw_table_insert(LZW_context_T *ctx, uint8_t *token, size_t token_len)
{
HBytes * next_entry = malloc(sizeof(HBytes));
HBytes * next_entry = malloc(sizeof(HBytes)); //XXX handle malloc error
next_entry->token = token;
next_entry->len = token_len;
ctx->lzw_code_table[ctx->next] = next_entry;
......@@ -196,7 +197,6 @@ validate_LZW_literal(HParseResult *p, void *u)
HParsedToken*
act_LZW_literal(const HParseResult *p, void *u)
{
HBytes * next_entry;
size_t next_entry_size;
uint8_t * next_entry_token;
HBytes * prev_string;
......@@ -215,16 +215,12 @@ act_LZW_literal(const HParseResult *p, void *u)
* Update the dictionary with the new string. Use of system malloc()
* here and in act_LZW_codeword is intentional, as LZW_clear_table/init_LZW_context free these
*/
prev_string = ctx->lzw_code_table[ctx->old]; //XXX: insert_table(ctx, uint8_t*) function
prev_string = ctx->lzw_code_table[ctx->old];
next_entry_size = prev_string->len + 1;
next_entry_token = malloc(sizeof(uint8_t) * next_entry_size);
memcpy(next_entry_token, prev_string->token, prev_string->len);
next_entry_token[next_entry_size - 1] = (uint8_t) code;
next_entry = malloc(sizeof(HBytes));
next_entry->len = next_entry_size;
next_entry->token = next_entry_token;
ctx->lzw_code_table[ctx->next] = next_entry;
ctx->next++;
lzw_table_insert(ctx, next_entry_token, next_entry_size);
ctx->old = code;
return H_MAKE_BYTES(output, 1);
......@@ -234,7 +230,6 @@ HParsedToken*
act_LZW_codeword(const HParseResult *p, void *u)
{
HBytes * prev_string;
HBytes * next_entry;
uint64_t code = H_CAST_UINT(p->ast);
uint8_t prefix;
uint8_t * output_token;
......@@ -269,16 +264,11 @@ act_LZW_codeword(const HParseResult *p, void *u)
*/
// XXX: handle malloc errors
prefix = output_token[0];
next_entry = malloc(sizeof(HBytes));
next_entry_token = malloc(sizeof(uint8_t) * prev_string_length+1);
next_entry->len = prev_string_length+1;
memcpy(next_entry_token, prev_string->token, prev_string_length);
next_entry_token[prev_string_length] = prefix;
next_entry->token = next_entry_token;
ctx->lzw_code_table[ctx->next] = next_entry;
lzw_table_insert(ctx, next_entry_token, prev_string_length+1);
ctx->old = code;
ctx->next += 1;
return H_MAKE_BYTES(output_token, code_token_length);
}
......@@ -316,16 +306,11 @@ act_LZW_codeword(const HParseResult *p, void *u)
* Update the dictionary
*/
new_prefix = output_token[0];
next_entry = malloc(sizeof(HBytes));
next_entry_token = malloc(sizeof(uint8_t) * prev_string_length+1);
next_entry->len = prev_string_length+1;
memcpy(next_entry_token, prev_string->token, prev_string_length);
next_entry_token[prev_string_length] = new_prefix;
next_entry->token = next_entry_token;
ctx->lzw_code_table[ctx->next] = next_entry;
lzw_table_insert(ctx, next_entry_token, prev_string_length+1);
ctx->old = code;
ctx->next += 1;
return H_MAKE_BYTES(output_token, output_length);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment