refactor, libc made optional
This commit is contained in:
+13
-17
@@ -5,17 +5,11 @@
|
||||
|
||||
#include "lzw.h"
|
||||
#include "bits.h"
|
||||
#include "std.h"
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#ifndef COM_DEF_NOSTD
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
#else
|
||||
#include "std.h"
|
||||
#endif
|
||||
|
||||
/* References: */
|
||||
/* https://www.w3.org/Graphics/GIF/spec-gif89a.txt */
|
||||
/* https://www.daubnet.com/en/file-format-gif */
|
||||
@@ -50,8 +44,9 @@ struct com_lzw_table com_lzw_infer_table(const char *datain, uint32_t sizein) {
|
||||
if (!code_found) {
|
||||
if (table_size >= table_cap) {
|
||||
/* TODO: Ref to prev table gets missed, memory leak scenario */
|
||||
if (!(table = realloc(table, sizeof(struct com_lzw_table_entry) *
|
||||
(table_cap + COM_LZW_TABLE_CAP_GROW))))
|
||||
if (!(table = com_std_realloc(
|
||||
table, sizeof(struct com_lzw_table_entry) *
|
||||
(table_cap + COM_LZW_TABLE_CAP_GROW))))
|
||||
goto ERR_ALLOC_INIT_TABLE;
|
||||
|
||||
table_cap += COM_LZW_TABLE_CAP_GROW;
|
||||
@@ -73,14 +68,14 @@ struct com_lzw_table com_lzw_infer_table(const char *datain, uint32_t sizein) {
|
||||
|
||||
ERR_ALLOC_INIT_TABLE:
|
||||
if (table_cap > 0)
|
||||
free(table);
|
||||
com_std_free(table);
|
||||
return (struct com_lzw_table){0};
|
||||
}
|
||||
|
||||
void com_lzw_free_table(struct com_lzw_table *table) {
|
||||
if (!table->table)
|
||||
return;
|
||||
free(table->table);
|
||||
com_std_free(table->table);
|
||||
table->size = 0;
|
||||
table->cap = 0;
|
||||
table->init_size = 0;
|
||||
@@ -89,14 +84,14 @@ void com_lzw_free_table(struct com_lzw_table *table) {
|
||||
/* For GIFs, color should already be collapsed to indices at this point */
|
||||
bool com_lzw_compress(const struct com_lzw_table *table, const char *datain,
|
||||
uint32_t sizein, char **dataout, uint32_t *sizeout) {
|
||||
assert(datain && dataout && sizeout && sizein > 0);
|
||||
assert(*dataout == NULL && *sizeout == 0);
|
||||
com_std_assert(datain && dataout && sizeout && sizein > 0);
|
||||
com_std_assert(*dataout == NULL && *sizeout == 0);
|
||||
|
||||
/* TODO: Make sure the table is cleared */
|
||||
|
||||
/* Encode step */
|
||||
uint8_t codesize = com_bits_needed(table->init_size) + 1;
|
||||
assert(codesize < COM_TABLE_BIT_WIDTH_LIMIT);
|
||||
com_std_assert(codesize < COM_TABLE_BIT_WIDTH_LIMIT);
|
||||
|
||||
uint32_t cur_table_idx =
|
||||
0; /* Head table in which vector we should be looking into */
|
||||
@@ -131,12 +126,13 @@ bool com_lzw_compress(const struct com_lzw_table *table, const char *datain,
|
||||
// }
|
||||
|
||||
/* Emit output, drop the string */
|
||||
assert(cur_table_idx < (1 << codesize));
|
||||
assert(cur_table_idx < table->size);
|
||||
com_std_assert(cur_table_idx < (1 << codesize));
|
||||
com_std_assert(cur_table_idx < table->size);
|
||||
|
||||
if (output_size == output_cap && output_bitshift + codesize > 8) {
|
||||
/* TODO: catch alloc failure */
|
||||
output = realloc(output, output_cap + COM_LZW_OUTPUT_CAP_GROWTH);
|
||||
output =
|
||||
com_std_realloc(output, output_cap + COM_LZW_OUTPUT_CAP_GROWTH);
|
||||
output_cap += COM_LZW_OUTPUT_CAP_GROWTH;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user