#ifndef COM_LZW_H #define COM_LZW_H #include #include /* Alphabet table for use in LZW algorithms */ struct com_lzw_table { /* table_size indexed entry is reserved for clear code */ /* table_size+1 indexed entry is reserved for terminator */ struct com_lzw_table_entry *table; uint32_t init_size; /* Alphabet size, table clear is done over it */ uint32_t size; uint32_t cap; bool continuous; /* If true, first order search is simple indexing, where codepoint = index, up to table_init_size */ }; struct com_lzw_table_entry { uint16_t child_chain; /* Start of linked list inside table, or 0 if none */ uint16_t next_child; /* Index of next child, or 0 if none */ char code; /* Single byte, children bytes are appended to it */ }; /* Returns table that is optimized for continuous range of codepoints */ /* Useful for binary compression, such as .GIF and .SAM formats */ struct com_lzw_table com_lzw_continous_table(uint8_t codepoints); /* Returns table with alphabet inferred from the incoming data */ /* Useful for alphanumeric compression, where not all codepoints are in use */ /* TODO: Sort table by frequency? */ struct com_lzw_table com_lzw_infer_table(const char *datain, uint32_t sizein); /* Each bit corresponds to a byte value, position-wise */ struct com_lzw_encoded_alphabet { uint64_t b0; uint64_t b1; uint64_t b2; uint64_t b3; }; struct com_lzw_encoded_alphabet com_lzw_encode_alphabet(const struct com_lzw_table *table); struct com_lzw_table com_lzw_decode_table(struct com_lzw_encoded_alphabet alphabet); /* Return table to its original form */ void com_lzw_clear_table(struct com_lzw_table *table); void com_lzw_free_table(struct com_lzw_table *table); bool com_lzw_compress(const struct com_lzw_table *table, const char *datain, uint32_t sizein, char **dataout, uint32_t *sizeout); #endif