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

Bugfix: adding to the dictionary at firstcode causes off by one error

TODO: Parameterize constraints on ctx->next in validations to accomodate EarlyChange
parent be7dc08f
No related branches found
No related tags found
No related merge requests found
...@@ -64,18 +64,19 @@ LZW_context_T * context; ...@@ -64,18 +64,19 @@ LZW_context_T * context;
HParsedToken* HParsedToken*
act_LZW_firstcode(const HParseResult *p, void *u) act_LZW_firstcode(const HParseResult *p, void *u)
{ {
HBytes * next_entry; /*HBytes * next_entry;
size_t next_entry_size; size_t next_entry_size;
uint8_t * next_entry_token; uint8_t * next_entry_token;*/
LZW_context_T * ctx = (LZW_context_T *) u; LZW_context_T * ctx = (LZW_context_T *) u;
uint64_t code = H_CAST_UINT(p->ast); uint64_t code = H_CAST_UINT(p->ast);
uint8_t *output = malloc(sizeof(uint8_t)); uint8_t *output = malloc(sizeof(uint8_t));
*output = (uint8_t) code; *output = (uint8_t) code;
fprintf(debug, "firstcode code: %lu, next: %u\n", p->ast->uint, ctx->next); // DEBUG //fprintf(debug, "firstcode code: %lu, next: %u\n", p->ast->uint, ctx->next); // DEBUG
fflush(debug); // DEBUG //fprintf(debug, "%lu ", p->ast->uint); // DEBUG
//fflush(debug); // DEBUG
/* PDF's version of the decoding algorithm seems to add the first character into the encoding table already. Maybe? */ /* PDF's version of the decoding algorithm seems to add the first character into the encoding table already. Maybe? */
next_entry_size = 2; /*next_entry_size = 2;
next_entry_token = malloc(sizeof(uint8_t) * next_entry_size); next_entry_token = malloc(sizeof(uint8_t) * next_entry_size);
memcpy(next_entry_token, output, 1); memcpy(next_entry_token, output, 1);
next_entry_token[next_entry_size - 1] = *output; next_entry_token[next_entry_size - 1] = *output;
...@@ -85,6 +86,7 @@ act_LZW_firstcode(const HParseResult *p, void *u) ...@@ -85,6 +86,7 @@ act_LZW_firstcode(const HParseResult *p, void *u)
assert(ctx->next == 258); // DEBUG assert(ctx->next == 258); // DEBUG
ctx->lzw_code_table[ctx->next] = next_entry; ctx->lzw_code_table[ctx->next] = next_entry;
ctx->next++; ctx->next++;
*/
ctx->old = code; ctx->old = code;
return H_MAKE_BYTES(output, 1); return H_MAKE_BYTES(output, 1);
...@@ -95,8 +97,9 @@ act_LZW_clear(const HParseResult *p, void *u) ...@@ -95,8 +97,9 @@ act_LZW_clear(const HParseResult *p, void *u)
{ {
LZW_context_T * ctx = (LZW_context_T *) u; LZW_context_T * ctx = (LZW_context_T *) u;
LZW_clear_table(ctx); LZW_clear_table(ctx);
fprintf(debug, "clear code: %lu, next: %u\n", p->ast->uint, ctx->next); // DEBUG //fprintf(debug, "clear code: %lu, next: %u\n", p->ast->uint, ctx->next); // DEBUG
fflush(debug); // DEBUG //fprintf(debug, "%lu ", p->ast->uint); // DEBUG
//fflush(debug); // DEBUG
ctx->next = 258; // Caution: moving this before the call to LZW_clear_table() will cause a memory leak ctx->next = 258; // Caution: moving this before the call to LZW_clear_table() will cause a memory leak
return H_MAKE_BYTES(NULL, 0); return H_MAKE_BYTES(NULL, 0);
} }
...@@ -108,84 +111,84 @@ bool ...@@ -108,84 +111,84 @@ bool
validate_LZW_9bitcodeword(HParseResult *p, void *u) validate_LZW_9bitcodeword(HParseResult *p, void *u)
{ {
LZW_context_T * ctx = (LZW_context_T *) u; LZW_context_T * ctx = (LZW_context_T *) u;
fprintf(debug, "9 bit code: %lu, next: %u\n", p->ast->uint, ctx->next); // DEBUG //fprintf(debug, "9 bit code: %lu, next: %u\n", p->ast->uint, ctx->next); // DEBUG
fflush(debug); // DEBUG //fflush(debug); // DEBUG
if (ctx->next < 512) // DEBUG if (ctx->next < 511) // DEBUG
assert(H_CAST_UINT(p->ast) < ctx->next); // DEBUG assert(H_CAST_UINT(p->ast) <= ctx->next); // DEBUG
return (ctx->next < 512); return (ctx->next < 511); // XXX: parameterize codeword boundaries via EarlyChange
} }
bool bool
validate_LZW_10bitcodeword(HParseResult *p, void *u) validate_LZW_10bitcodeword(HParseResult *p, void *u)
{ {
LZW_context_T * ctx = (LZW_context_T *) u; LZW_context_T * ctx = (LZW_context_T *) u;
fprintf(debug, "10 bit code: %lu, next: %u\n", p->ast->uint, ctx->next); // DEBUG //fprintf(debug, "10 bit code: %lu, next: %u\n", p->ast->uint, ctx->next); // DEBUG
fflush(debug); // DEBUG //fflush(debug); // DEBUG
if (ctx->next >= 512 && ctx->next < 1024) // DEBUG if (ctx->next >= 511 && ctx->next < 1023) // DEBUG
assert(H_CAST_UINT(p->ast) < ctx->next); // DEBUG assert(H_CAST_UINT(p->ast) <= ctx->next); // DEBUG
return (ctx->next >= 512 && ctx->next < 1024); return (ctx->next >= 511 && ctx->next < 1023);
} }
bool bool
validate_LZW_11bitcodeword(HParseResult *p, void *u) validate_LZW_11bitcodeword(HParseResult *p, void *u)
{ {
LZW_context_T * ctx = (LZW_context_T *) u; LZW_context_T * ctx = (LZW_context_T *) u;
fprintf(debug, "11 bit code: %lu, next: %u\n", p->ast->uint, ctx->next); // DEBUG //fprintf(debug, "11 bit code: %lu, next: %u\n", p->ast->uint, ctx->next); // DEBUG
fflush(debug); // DEBUG //fflush(debug); // DEBUG
if (ctx->next >= 1024 && ctx->next < 2048) // DEBUG if (ctx->next >= 1023 && ctx->next < 2047) // DEBUG
assert(H_CAST_UINT(p->ast) < ctx->next); // DEBUG assert(H_CAST_UINT(p->ast) <= ctx->next); // DEBUG
return (ctx->next >= 1024 && ctx->next < 2048); return (ctx->next >= 1023 && ctx->next < 2047);
} }
bool bool
validate_LZW_12bitcodeword(HParseResult *p, void *u) validate_LZW_12bitcodeword(HParseResult *p, void *u)
{ {
LZW_context_T * ctx = (LZW_context_T *) u; LZW_context_T * ctx = (LZW_context_T *) u;
fprintf(debug, "12 bit code: %lu, next: %u\n", p->ast->uint, ctx->next); // DEBUG //fprintf(debug, "12 bit code: %lu, next: %u\n", p->ast->uint, ctx->next); // DEBUG
fflush(debug); // DEBUG //fflush(debug); // DEBUG
if (ctx->next >= 2048 && ctx->next < 4096) // DEBUG if (ctx->next >= 2047 && ctx->next < 4095) // DEBUG
assert(H_CAST_UINT(p->ast) < ctx->next); // DEBUG assert(H_CAST_UINT(p->ast) <= ctx->next); // DEBUG
return (ctx->next >= 2048 && ctx->next < 4096); return (ctx->next >= 2047 && ctx->next < 4095);
} }
bool bool
validate_LZW_9bitlitspec(HParseResult *p, void *u) validate_LZW_9bitlitspec(HParseResult *p, void *u)
{ {
LZW_context_T * ctx = (LZW_context_T *) u; LZW_context_T * ctx = (LZW_context_T *) u;
fprintf(debug, "9 bit lit: %lu, next: %u\n", p->ast->uint, ctx->next); // DEBUG //fprintf(debug, "9 bit lit: %lu, next: %u\n", p->ast->uint, ctx->next); // DEBUG
fflush(debug); // DEBUG //fflush(debug); // DEBUG
uint64_t code = H_CAST_UINT(p->ast); uint64_t code = H_CAST_UINT(p->ast);
return (ctx->next < 512 && code < 258); return (ctx->next < 511 && code < 258);
} }
bool bool
validate_LZW_10bitlitspec(HParseResult *p, void *u) validate_LZW_10bitlitspec(HParseResult *p, void *u)
{ {
LZW_context_T * ctx = (LZW_context_T *) u; LZW_context_T * ctx = (LZW_context_T *) u;
fprintf(debug, "10 bit lit: %lu, next: %u\n", p->ast->uint, ctx->next); // DEBUG //fprintf(debug, "10 bit lit: %lu, next: %u\n", p->ast->uint, ctx->next); // DEBUG
fflush(debug); // DEBUG //fflush(debug); // DEBUG
uint64_t code = H_CAST_UINT(p->ast); uint64_t code = H_CAST_UINT(p->ast);
return (ctx->next >= 512 && ctx->next < 1024 && code < 258); return (ctx->next >= 511 && ctx->next < 1023 && code < 258);
} }
bool bool
validate_LZW_11bitlitspec(HParseResult *p, void *u) validate_LZW_11bitlitspec(HParseResult *p, void *u)
{ {
LZW_context_T * ctx = (LZW_context_T *) u; LZW_context_T * ctx = (LZW_context_T *) u;
fprintf(debug, "11 bit lit: %lu, next: %u\n", p->ast->uint, ctx->next); // DEBUG //fprintf(debug, "11 bit lit: %lu, next: %u\n", p->ast->uint, ctx->next); // DEBUG
fflush(debug); // DEBUG //fflush(debug); // DEBUG
uint64_t code = H_CAST_UINT(p->ast); uint64_t code = H_CAST_UINT(p->ast);
return (ctx->next >= 1024 && ctx->next < 2048 && code < 258); return (ctx->next >= 1023 && ctx->next < 2047 && code < 258);
} }
bool bool
validate_LZW_12bitlitspec(HParseResult *p, void *u) validate_LZW_12bitlitspec(HParseResult *p, void *u)
{ {
LZW_context_T * ctx = (LZW_context_T *) u; LZW_context_T * ctx = (LZW_context_T *) u;
fprintf(debug, "12 bit lit: %lu, next: %u\n", p->ast->uint, ctx->next); // DEBUG //fprintf(debug, "12 bit lit: %lu, next: %u\n", p->ast->uint, ctx->next); // DEBUG
fflush(debug); // DEBUG //fflush(debug); // DEBUG
uint64_t code = H_CAST_UINT(p->ast); uint64_t code = H_CAST_UINT(p->ast);
return (ctx->next >= 2048 && ctx->next < 4096 && code < 258); return (ctx->next >= 2047 && ctx->next < 4095 && code < 258);
} }
bool bool
...@@ -223,8 +226,9 @@ act_LZW_literal(const HParseResult *p, void *u) ...@@ -223,8 +226,9 @@ act_LZW_literal(const HParseResult *p, void *u)
*/ */
uint8_t *output = malloc(sizeof(uint8_t)); uint8_t *output = malloc(sizeof(uint8_t));
*output = (uint8_t) code; *output = (uint8_t) code;
fprintf(debug, "lit: %lu, next: %u\n", code, ctx->next); // DEBUG //fprintf(debug, "lit: %lu, next: %u\n", code, ctx->next); // DEBUG
fflush(debug); // DEBUG //fprintf(debug, "%lu ", code); // DEBUG
//fflush(debug); // DEBUG
prev_string = ctx->lzw_code_table[ctx->old]; //XXX: insert_table(ctx, uint8_t*) function prev_string = ctx->lzw_code_table[ctx->old]; //XXX: insert_table(ctx, uint8_t*) function
next_entry_size = prev_string->len + 1; next_entry_size = prev_string->len + 1;
...@@ -253,8 +257,9 @@ act_LZW_codeword(const HParseResult *p, void *u) ...@@ -253,8 +257,9 @@ act_LZW_codeword(const HParseResult *p, void *u)
size_t prev_string_length; size_t prev_string_length;
LZW_context_T * ctx = (LZW_context_T *) u; LZW_context_T * ctx = (LZW_context_T *) u;
fprintf(debug, "code: %lu, next: %u\n", code, ctx->next); // DEBUG //fprintf(debug, "code: %lu, next: %u\n", code, ctx->next); // DEBUG
fflush(debug); // DEBUG //fprintf(debug, "%lu ", code); // DEBUG
//fflush(debug); // DEBUG
if(ctx->lzw_code_table[code] != NULL) // code is in the table if(ctx->lzw_code_table[code] != NULL) // code is in the table
...@@ -365,6 +370,10 @@ act_LZW_body(const HParseResult *p, void *u) ...@@ -365,6 +370,10 @@ act_LZW_body(const HParseResult *p, void *u)
index += len; index += len;
} }
//fprintf(debug, "\n\n"); // DEBUG
//fwrite(buffer, 1, total_buffer_size, debug); // DEBUG
//fflush(debug); // DEBUG
return H_MAKE_BYTES(buffer, total_buffer_size); return H_MAKE_BYTES(buffer, total_buffer_size);
} }
...@@ -381,6 +390,7 @@ act_LZW_data(const HParseResult *p, void *u) ...@@ -381,6 +390,7 @@ act_LZW_data(const HParseResult *p, void *u)
*/ */
//HCountedArray * seq = H_CAST_SEQ(p->ast); //HCountedArray * seq = H_CAST_SEQ(p->ast);
//LZW_context_T *ctx = (LZW_context_T*) u; // DEBUG
size_t total_buffer_size = 0; size_t total_buffer_size = 0;
uint8_t * buffer; uint8_t * buffer;
HBytes first = H_FIELD_BYTES(1); HBytes first = H_FIELD_BYTES(1);
...@@ -392,6 +402,15 @@ act_LZW_data(const HParseResult *p, void *u) ...@@ -392,6 +402,15 @@ act_LZW_data(const HParseResult *p, void *u)
memcpy(buffer, first.token, first.len); memcpy(buffer, first.token, first.len);
memcpy(buffer+first.len, rest.token, rest.len); memcpy(buffer+first.len, rest.token, rest.len);
//fprintf(debug, "\n\n"); // DEBUG
/*for(int i = 258; i < ctx->next; ++i) // DEBUG
{
fprintf(debug, "i: %u, str: ", i);
fwrite(ctx->lzw_code_table[i]->token, ctx->lzw_code_table[i]->len, 1, debug);
fprintf(debug, "\n");
}
fflush(debug); // DEBUG */
return H_MAKE_BYTES(buffer, total_buffer_size); return H_MAKE_BYTES(buffer, total_buffer_size);
} }
...@@ -441,9 +460,9 @@ void init_LZW_parser() ...@@ -441,9 +460,9 @@ void init_LZW_parser()
HParseResult* parse_LZW_data(const uint8_t* input, size_t length) HParseResult* parse_LZW_data(const uint8_t* input, size_t length)
{ {
debug = fopen("lzw_debug.txt", "w"); // DEBUG //debug = fopen("lzw_debug.txt", "a"); // DEBUG
HParseResult *res = h_parse(p_lzwdata, input, length); HParseResult *res = h_parse(p_lzwdata, input, length);
fclose(debug); // DEBUG //fclose(debug); // DEBUG
return res; return res;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment