diff --git a/lzw.c b/lzw.c index 5a3ce0d7551af4515588b51657144412213e41a5..3bd991dc12f03295e5594afc174c6d8cc344179a 100644 --- a/lzw.c +++ b/lzw.c @@ -46,7 +46,7 @@ void LZW_clear_table(LZW_context_T *ctx) */ void lzw_table_insert(LZW_context_T *ctx, uint8_t *token, size_t token_len) { - HBytes * next_entry = malloc(sizeof(HBytes)); //XXX handle malloc error + HBytes * next_entry = malloc(sizeof(HBytes)); next_entry->token = token; next_entry->len = token_len; ctx->lzw_code_table[ctx->next] = next_entry; @@ -212,12 +212,12 @@ act_LZW_literal(const HParseResult *p, void *u) //fflush(debug); // DEBUG /* - * Update the dictionary with the new string. Use of system malloc() + * Update the dictionary with the new string. Use of system allocator * 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]; next_entry_size = prev_string->len + 1; - next_entry_token = malloc(sizeof(uint8_t) * next_entry_size); + next_entry_token = calloc(next_entry_size, sizeof(uint8_t)); memcpy(next_entry_token, prev_string->token, prev_string->len); next_entry_token[next_entry_size - 1] = (uint8_t) code; lzw_table_insert(ctx, next_entry_token, next_entry_size); @@ -253,7 +253,7 @@ act_LZW_codeword(const HParseResult *p, void *u) */ code_str = ctx->lzw_code_table[code]; code_token_length = code_str->len; - output_token = malloc(sizeof(uint8_t) * code_token_length); + output_token = calloc(code_token_length, sizeof(uint8_t)); memcpy(output_token, code_str->token, code_token_length); prev_string = ctx->lzw_code_table[ctx->old]; @@ -263,7 +263,7 @@ act_LZW_codeword(const HParseResult *p, void *u) * Update the dictionary */ prefix = output_token[0]; - next_entry_token = malloc(sizeof(uint8_t) * prev_string_length+1); + next_entry_token = calloc(prev_string_length+1, sizeof(uint8_t)); memcpy(next_entry_token, prev_string->token, prev_string_length); next_entry_token[prev_string_length] = prefix; lzw_table_insert(ctx, next_entry_token, prev_string_length+1); @@ -286,7 +286,7 @@ act_LZW_codeword(const HParseResult *p, void *u) * Put together the string for the current code, then insert it into the table. We also copy the token into a separate uint8_t to be returned by the function */ output_length = prev_string_length + 1; - output_token = malloc(sizeof(uint8_t) * output_length); + output_token = calloc(output_length, sizeof(uint8_t)); memcpy(output_token, prev_string->token, prev_string_length); /* * Output is one byte longer than prev_string, and the last byte is the first character of the previous string @@ -296,7 +296,7 @@ act_LZW_codeword(const HParseResult *p, void *u) missing_table_entry = malloc(sizeof(HBytes)); missing_table_entry->len = prev_string_length + 1; - missing_table_entry_token = malloc(sizeof(uint8_t) * missing_table_entry->len); + missing_table_entry_token = calloc(missing_table_entry->len, sizeof(uint8_t)); memcpy(missing_table_entry_token, output_token, missing_table_entry->len); missing_table_entry->token = missing_table_entry_token; ctx->lzw_code_table[code] = missing_table_entry; @@ -305,7 +305,7 @@ act_LZW_codeword(const HParseResult *p, void *u) * Update the dictionary */ new_prefix = output_token[0]; - next_entry_token = malloc(sizeof(uint8_t) * prev_string_length+1); + next_entry_token = calloc(prev_string_length+1, sizeof(uint8_t)); memcpy(next_entry_token, prev_string->token, prev_string_length); next_entry_token[prev_string_length] = new_prefix; lzw_table_insert(ctx, next_entry_token, prev_string_length+1);