diff --git a/libmineziper/include/libmineziper_zip.h b/libmineziper/include/libmineziper_zip.h index 428402c..9fee34f 100644 --- a/libmineziper/include/libmineziper_zip.h +++ b/libmineziper/include/libmineziper_zip.h @@ -7,6 +7,7 @@ #define EOCD_SIG "PK\05\06" #define LFH_SIG "PK\03\04" #define CDH_SIG "PK\01\02" +#define DEFLATE 8 #define NUM_OF_CODE 26 * 2 + 10 const char* SYMBOLS = @@ -15,6 +16,7 @@ const char* SYMBOLS = typedef struct raw { char* buf; + char* stream; int size; } raw; @@ -31,7 +33,6 @@ typedef struct LFH int uncompressed_size; short filename_length; short extra_field_length; - char* filename; } LFH; typedef struct CDH @@ -64,11 +65,27 @@ typedef struct EOCD int off_cdh; } EOCD; +// Input stream header for DEFLATE +typedef struct ISH +{ + unsigned last_block : 1; + unsigned block_type : 2; +} ISH; + +// Dynamic Huffman Code header for DEFLATE +typedef struct DHCH +{ + unsigned literal_codes : 5; + unsigned dist_codes : 5; + unsigned bit_length_code : 4; +} DHCH; + typedef struct zip { // compression type char* cd; CDH** cdh; + LFH** lfh; EOCD* eocd; } zip; @@ -78,11 +95,18 @@ typedef struct HN unsigned char symbol; unsigned char code; unsigned char len; -} HN, *HT; +} HN; + +typedef struct HT +{ + unsigned char size; + HN* nodes; +} HT; void get_eocd(raw* raw, zip* out); void get_cdh(raw* raw, zip* out); +char* get_encoded_data(zip* in, int n); void parse_zip(char* filename, zip* out); -void decode_huffman_tree(char* encoded, HT* out); +void deflate(zip* in); #endif \ No newline at end of file diff --git a/libmineziper/src/libmineziper_zip.c b/libmineziper/src/libmineziper_zip.c index e70598b..9cd212d 100644 --- a/libmineziper/src/libmineziper_zip.c +++ b/libmineziper/src/libmineziper_zip.c @@ -16,6 +16,7 @@ void get_eocd(raw* raw, zip* out) { out->eocd = (EOCD*) se; out->cdh = (CDH**) malloc(out->eocd->number_of_entries * sizeof(CDH*)); + out->lfh = (LFH**) malloc(out->eocd->number_of_entries * sizeof(LFH*)); break; } @@ -37,48 +38,16 @@ void get_cdh(raw* raw, zip* out) for (int i = 0; i < out->eocd->number_of_entries; i++) { out->cdh[i] = cdh; + out->lfh[i] = (LFH*) (raw->buf + cdh->off_lfh); + cdh = (CDH*) (((char*) cdh) + sizeof(CDH) + cdh->filename_length + cdh->extra_field_length + cdh->file_comment_length); } } -void decode_huffman_tree(char* encoded, HT* out) +char* get_encoded_data(zip* in, int n) { - 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; + return (char*) (in->lfh[n]) + sizeof(LFH) + in->lfh[n]->filename_length + + in->lfh[n]->extra_field_length; } + diff --git a/tests/test_get_cdh.c b/tests/test_get_cdh.c index cb6d87e..18560d0 100644 --- a/tests/test_get_cdh.c +++ b/tests/test_get_cdh.c @@ -49,5 +49,25 @@ int main(int argc, char** argv) printf("\n}\n\n"); } + printf("\n--------------------------------------------\n"); + + for (int i = 0; i < zip.eocd->number_of_entries; i++) + { + printf( + "lfh %d = {\n sig: 0x%x\n comp size: 0x%x\n uncomp size: 0x%x\n " + "filename: ", + i, + zip.lfh[i]->sig, + zip.lfh[i]->compressed_size, + zip.lfh[i]->uncompressed_size); + + for (int j = 0; j < zip.lfh[i]->filename_length; j++) + { + printf("%c", ((char*) zip.lfh[i])[sizeof(LFH) + j]); + } + + printf("\n}\n\n"); + } + return 0; } \ No newline at end of file