diff --git a/lzw.c b/lzw.c index ebe3ea0dc88355834516c53d6a7772f3e4a2a389..c80eefdf210eebdc1d1a4cadb6f932ff54f518cf 100644 --- a/lzw.c +++ b/lzw.c @@ -64,18 +64,19 @@ LZW_context_T * context; HParsedToken* act_LZW_firstcode(const HParseResult *p, void *u) { - HBytes * next_entry; + /*HBytes * next_entry; size_t next_entry_size; - uint8_t * next_entry_token; + uint8_t * next_entry_token;*/ LZW_context_T * ctx = (LZW_context_T *) u; uint64_t code = H_CAST_UINT(p->ast); uint8_t *output = malloc(sizeof(uint8_t)); *output = (uint8_t) code; - fprintf(debug, "firstcode code: %lu, next: %u\n", p->ast->uint, ctx->next); // DEBUG - fflush(debug); // DEBUG + //fprintf(debug, "firstcode code: %lu, next: %u\n", p->ast->uint, ctx->next); // 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? */ - next_entry_size = 2; + /*next_entry_size = 2; next_entry_token = malloc(sizeof(uint8_t) * next_entry_size); memcpy(next_entry_token, output, 1); next_entry_token[next_entry_size - 1] = *output; @@ -85,6 +86,7 @@ act_LZW_firstcode(const HParseResult *p, void *u) assert(ctx->next == 258); // DEBUG ctx->lzw_code_table[ctx->next] = next_entry; ctx->next++; + */ ctx->old = code; return H_MAKE_BYTES(output, 1); @@ -95,8 +97,9 @@ act_LZW_clear(const HParseResult *p, void *u) { LZW_context_T * ctx = (LZW_context_T *) u; LZW_clear_table(ctx); - fprintf(debug, "clear code: %lu, next: %u\n", p->ast->uint, ctx->next); // DEBUG - fflush(debug); // DEBUG + //fprintf(debug, "clear code: %lu, next: %u\n", p->ast->uint, ctx->next); // 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 return H_MAKE_BYTES(NULL, 0); } @@ -108,84 +111,84 @@ bool validate_LZW_9bitcodeword(HParseResult *p, void *u) { LZW_context_T * ctx = (LZW_context_T *) u; - fprintf(debug, "9 bit code: %lu, next: %u\n", p->ast->uint, ctx->next); // DEBUG - fflush(debug); // DEBUG - if (ctx->next < 512) // DEBUG - assert(H_CAST_UINT(p->ast) < ctx->next); // DEBUG - return (ctx->next < 512); + //fprintf(debug, "9 bit code: %lu, next: %u\n", p->ast->uint, ctx->next); // DEBUG + //fflush(debug); // DEBUG + if (ctx->next < 511) // DEBUG + assert(H_CAST_UINT(p->ast) <= ctx->next); // DEBUG + return (ctx->next < 511); // XXX: parameterize codeword boundaries via EarlyChange } bool validate_LZW_10bitcodeword(HParseResult *p, void *u) { LZW_context_T * ctx = (LZW_context_T *) u; - fprintf(debug, "10 bit code: %lu, next: %u\n", p->ast->uint, ctx->next); // DEBUG - fflush(debug); // DEBUG - if (ctx->next >= 512 && ctx->next < 1024) // DEBUG - assert(H_CAST_UINT(p->ast) < ctx->next); // DEBUG - return (ctx->next >= 512 && ctx->next < 1024); + //fprintf(debug, "10 bit code: %lu, next: %u\n", p->ast->uint, ctx->next); // DEBUG + //fflush(debug); // DEBUG + if (ctx->next >= 511 && ctx->next < 1023) // DEBUG + assert(H_CAST_UINT(p->ast) <= ctx->next); // DEBUG + return (ctx->next >= 511 && ctx->next < 1023); } bool validate_LZW_11bitcodeword(HParseResult *p, void *u) { LZW_context_T * ctx = (LZW_context_T *) u; - fprintf(debug, "11 bit code: %lu, next: %u\n", p->ast->uint, ctx->next); // DEBUG - fflush(debug); // DEBUG - if (ctx->next >= 1024 && ctx->next < 2048) // DEBUG - assert(H_CAST_UINT(p->ast) < ctx->next); // DEBUG - return (ctx->next >= 1024 && ctx->next < 2048); + //fprintf(debug, "11 bit code: %lu, next: %u\n", p->ast->uint, ctx->next); // DEBUG + //fflush(debug); // DEBUG + if (ctx->next >= 1023 && ctx->next < 2047) // DEBUG + assert(H_CAST_UINT(p->ast) <= ctx->next); // DEBUG + return (ctx->next >= 1023 && ctx->next < 2047); } bool validate_LZW_12bitcodeword(HParseResult *p, void *u) { LZW_context_T * ctx = (LZW_context_T *) u; - fprintf(debug, "12 bit code: %lu, next: %u\n", p->ast->uint, ctx->next); // DEBUG - fflush(debug); // DEBUG - if (ctx->next >= 2048 && ctx->next < 4096) // DEBUG - assert(H_CAST_UINT(p->ast) < ctx->next); // DEBUG - return (ctx->next >= 2048 && ctx->next < 4096); + //fprintf(debug, "12 bit code: %lu, next: %u\n", p->ast->uint, ctx->next); // DEBUG + //fflush(debug); // DEBUG + if (ctx->next >= 2047 && ctx->next < 4095) // DEBUG + assert(H_CAST_UINT(p->ast) <= ctx->next); // DEBUG + return (ctx->next >= 2047 && ctx->next < 4095); } bool validate_LZW_9bitlitspec(HParseResult *p, void *u) { LZW_context_T * ctx = (LZW_context_T *) u; - fprintf(debug, "9 bit lit: %lu, next: %u\n", p->ast->uint, ctx->next); // DEBUG - fflush(debug); // DEBUG + //fprintf(debug, "9 bit lit: %lu, next: %u\n", p->ast->uint, ctx->next); // DEBUG + //fflush(debug); // DEBUG uint64_t code = H_CAST_UINT(p->ast); - return (ctx->next < 512 && code < 258); + return (ctx->next < 511 && code < 258); } bool validate_LZW_10bitlitspec(HParseResult *p, void *u) { LZW_context_T * ctx = (LZW_context_T *) u; - fprintf(debug, "10 bit lit: %lu, next: %u\n", p->ast->uint, ctx->next); // DEBUG - fflush(debug); // DEBUG + //fprintf(debug, "10 bit lit: %lu, next: %u\n", p->ast->uint, ctx->next); // DEBUG + //fflush(debug); // DEBUG 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 validate_LZW_11bitlitspec(HParseResult *p, void *u) { LZW_context_T * ctx = (LZW_context_T *) u; - fprintf(debug, "11 bit lit: %lu, next: %u\n", p->ast->uint, ctx->next); // DEBUG - fflush(debug); // DEBUG + //fprintf(debug, "11 bit lit: %lu, next: %u\n", p->ast->uint, ctx->next); // DEBUG + //fflush(debug); // DEBUG 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 validate_LZW_12bitlitspec(HParseResult *p, void *u) { LZW_context_T * ctx = (LZW_context_T *) u; - fprintf(debug, "12 bit lit: %lu, next: %u\n", p->ast->uint, ctx->next); // DEBUG - fflush(debug); // DEBUG + //fprintf(debug, "12 bit lit: %lu, next: %u\n", p->ast->uint, ctx->next); // DEBUG + //fflush(debug); // DEBUG 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 @@ -223,8 +226,9 @@ act_LZW_literal(const HParseResult *p, void *u) */ uint8_t *output = malloc(sizeof(uint8_t)); *output = (uint8_t) code; - fprintf(debug, "lit: %lu, next: %u\n", code, ctx->next); // DEBUG - fflush(debug); // DEBUG + //fprintf(debug, "lit: %lu, next: %u\n", code, ctx->next); // DEBUG + //fprintf(debug, "%lu ", code); // DEBUG + //fflush(debug); // DEBUG prev_string = ctx->lzw_code_table[ctx->old]; //XXX: insert_table(ctx, uint8_t*) function next_entry_size = prev_string->len + 1; @@ -253,8 +257,9 @@ act_LZW_codeword(const HParseResult *p, void *u) size_t prev_string_length; LZW_context_T * ctx = (LZW_context_T *) u; - fprintf(debug, "code: %lu, next: %u\n", code, ctx->next); // DEBUG - fflush(debug); // DEBUG + //fprintf(debug, "code: %lu, next: %u\n", code, ctx->next); // DEBUG + //fprintf(debug, "%lu ", code); // DEBUG + //fflush(debug); // DEBUG if(ctx->lzw_code_table[code] != NULL) // code is in the table @@ -365,6 +370,10 @@ act_LZW_body(const HParseResult *p, void *u) 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); } @@ -381,6 +390,7 @@ act_LZW_data(const HParseResult *p, void *u) */ //HCountedArray * seq = H_CAST_SEQ(p->ast); + //LZW_context_T *ctx = (LZW_context_T*) u; // DEBUG size_t total_buffer_size = 0; uint8_t * buffer; HBytes first = H_FIELD_BYTES(1); @@ -392,6 +402,15 @@ act_LZW_data(const HParseResult *p, void *u) memcpy(buffer, first.token, first.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); } @@ -441,9 +460,9 @@ void init_LZW_parser() 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); - fclose(debug); // DEBUG + //fclose(debug); // DEBUG return res; }