Decode canonical huffman tree

This commit is contained in:
atxr 2024-02-13 11:27:24 +01:00
parent 3636d3fa0d
commit 2d34e143d7
2 changed files with 55 additions and 1 deletions

View file

@ -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

View file

@ -41,3 +41,44 @@ void get_cdh(raw* raw, zip* out)
cdh->extra_field_length + cdh->file_comment_length);
}
}
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;
}