diff options
-rwxr-xr-x | build.sh | 3 | ||||
-rw-r--r-- | encode.c | 18 | ||||
-rw-r--r-- | text2.txt | 2 |
3 files changed, 16 insertions, 7 deletions
@@ -4,3 +4,6 @@ set -xe gcc -o encode encode.c -Wall -Wextra -pedantic -g gcc -o decode decode.c -Wall -Wextra -pedantic -g + +time ./encode < text2.txt > text2.huff +time ./decode < text2.huff @@ -36,6 +36,8 @@ size_t write_tree_shape(node_t *root); size_t write_encoded(node_t *root); void encode(); +size_t MMAP_SIZE_CAP = 1024; // 1 kibibyte + FILE *in; FILE *out; @@ -48,7 +50,7 @@ node_t *encoding_table[256] = {0}; void encode() { // write tree // - wb_flush(1); // flush amount of bits (written in gen_tree()) + wb_flush(1); // flush amount of leafs in tree (written in gen_tree()) wb_flush(write_tree_data(root)); wb_flush(write_tree_shape(root)/8 + 1); @@ -61,13 +63,15 @@ void encode() // encode // char ch; while(fread(&ch, sizeof(char), 1, in) > 0) { - if(encoding_table[ch] == NULL) { - fprintf(stderr, "Character %c not found encoding table\n", ch); - exit(1); - } + // fprintf(stderr, "%c\n", ch); write_encoded(encoding_table[ch]); } - wb_flush(write_encoded(NULL)/8+1); + // fprintf(stderr, "%d\n", write_encoded(NULL)/8); + wb_flush((write_encoded(NULL) + 7)/8); + + // write amout of extra bits + write_buf[0] = write_encoded(NULL) % 8; + wb_flush(1); } int main(void) @@ -145,7 +149,7 @@ size_t write_encoded(node_t *root) static size_t bits = 0; if(root == NULL || root->parent == NULL) return bits; - write_buf[bits/8] |= root->direction << (bits % 8); + write_buf[bits/8] |= root->direction << (7 - (bits % 8)); bits++; if(bits/8 == WRITE_BUF_CAP) { diff --git a/text2.txt b/text2.txt new file mode 100644 index 0000000..428960f --- /dev/null +++ b/text2.txt @@ -0,0 +1,2 @@ +One Two Tree Four Five Eight +fff |