From 03afb5602acd88341232774c0ccd844c25e6e4d5 Mon Sep 17 00:00:00 2001 From: kartofen Date: Wed, 22 Mar 2023 22:58:12 +0200 Subject: more progress --- build.sh | 3 +++ encode.c | 18 +++++++++++------- text2.txt | 2 ++ 3 files changed, 16 insertions(+), 7 deletions(-) create mode 100644 text2.txt diff --git a/build.sh b/build.sh index 94427a6..e35b44a 100755 --- a/build.sh +++ b/build.sh @@ -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 diff --git a/encode.c b/encode.c index 5a03f92..552f201 100644 --- a/encode.c +++ b/encode.c @@ -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 -- cgit v1.2.3