diff --git a/libmineziper/include/libmineziper_zip.h b/libmineziper/include/libmineziper_zip.h index 223420e..428402c 100644 --- a/libmineziper/include/libmineziper_zip.h +++ b/libmineziper/include/libmineziper_zip.h @@ -8,6 +8,10 @@ #define LFH_SIG "PK\03\04" #define CDH_SIG "PK\01\02" +#define NUM_OF_CODE 26 * 2 + 10 +const char* SYMBOLS = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; + typedef struct raw { char* buf; @@ -68,8 +72,17 @@ typedef struct zip EOCD* eocd; } zip; +// Huffman Node and Table +typedef struct HN +{ + unsigned char symbol; + unsigned char code; + unsigned char len; +} HN, *HT; + void get_eocd(raw* raw, zip* out); void get_cdh(raw* raw, zip* out); void parse_zip(char* filename, zip* out); +void decode_huffman_tree(char* encoded, HT* out); #endif \ No newline at end of file diff --git a/libmineziper/src/libmineziper_zip.c b/libmineziper/src/libmineziper_zip.c index 8e864bd..e70598b 100644 --- a/libmineziper/src/libmineziper_zip.c +++ b/libmineziper/src/libmineziper_zip.c @@ -40,4 +40,45 @@ void get_cdh(raw* raw, zip* out) cdh = (CDH*) (((char*) cdh) + sizeof(CDH) + cdh->filename_length + cdh->extra_field_length + cdh->file_comment_length); } -} \ No newline at end of file +} + +void decode_huffman_tree(char* encoded, HT* out) +{ + unsigned char size = *encoded; + if (size == 0) + { + printf("ERROR WTF"); + // TODO + return; + } + + if (size > NUM_OF_CODE) + { + printf("To many symbols"); + // TODO + return; + } + + HT tree = malloc(sizeof(HN) * size); + encoded++; + + char code = 0; + char len_diff = 0; + + for (int i = 0; i < size; i++) + { + tree[i].symbol = SYMBOLS[i]; + tree[i].code = code; + tree[i].len = *encoded; + + if (i + 1 < size) + { + // TODO WHAT IS LENDIFF IS BIG? + len_diff = encoded[1] - encoded[0]; + code = (code + 1) << len_diff; + encoded++; + } + } + + *out = tree; +}