2 // implements canonical Huffman
10 { uint max,lim; // maximum symbol (0..max), same excluding zero freqs
11 uint depth; // max symbol length
13 { uint *spos; // symbol positions after sorting by decr freq (enc)
14 uint *symb; // symbols sorted by freq (dec)
16 uint *num; // first pos of each length (dec), number of each length (enc)
17 uint *fst; // first code (numeric) of each length (dec)
18 ulong total; // total length to achieve, in bits
21 // Creates Huffman encoder given symbols 0..lim with frequencies
22 // freq[i], ready for compression
24 THuff createHuff (uint *freq, uint lim);
26 // Encodes symb using H, over stream[ptr...lim] (ptr and lim are
27 // bit positions of stream). Returns the new ptr.
29 ulong encodeHuff (THuff H, uint symb, uint *stream, ulong ptr);
31 // Decodes *symb using H, over stream[ptr...lim] (ptr and lim are
32 // bit positions of stream). Returns the new ptr.
34 ulong decodeHuff (THuff H, uint *symb, uint *stream, ulong ptr);
38 void saveHuff (THuff H, FILE *f);
40 // Size of H written on file
42 uint sizeHuff (THuff H);
46 void freeHuff (THuff H);
48 // Loads H from file f, prepared for encoding or decoding depending
51 THuff loadHuff (FILE *f, int enc);