Print LFH info in the cdh test
This commit is contained in:
parent
e18152f1d8
commit
3449b9edbf
3 changed files with 54 additions and 41 deletions
|
|
@ -7,6 +7,7 @@
|
||||||
#define EOCD_SIG "PK\05\06"
|
#define EOCD_SIG "PK\05\06"
|
||||||
#define LFH_SIG "PK\03\04"
|
#define LFH_SIG "PK\03\04"
|
||||||
#define CDH_SIG "PK\01\02"
|
#define CDH_SIG "PK\01\02"
|
||||||
|
#define DEFLATE 8
|
||||||
|
|
||||||
#define NUM_OF_CODE 26 * 2 + 10
|
#define NUM_OF_CODE 26 * 2 + 10
|
||||||
const char* SYMBOLS =
|
const char* SYMBOLS =
|
||||||
|
|
@ -15,6 +16,7 @@ const char* SYMBOLS =
|
||||||
typedef struct raw
|
typedef struct raw
|
||||||
{
|
{
|
||||||
char* buf;
|
char* buf;
|
||||||
|
char* stream;
|
||||||
int size;
|
int size;
|
||||||
} raw;
|
} raw;
|
||||||
|
|
||||||
|
|
@ -31,7 +33,6 @@ typedef struct LFH
|
||||||
int uncompressed_size;
|
int uncompressed_size;
|
||||||
short filename_length;
|
short filename_length;
|
||||||
short extra_field_length;
|
short extra_field_length;
|
||||||
char* filename;
|
|
||||||
} LFH;
|
} LFH;
|
||||||
|
|
||||||
typedef struct CDH
|
typedef struct CDH
|
||||||
|
|
@ -64,11 +65,27 @@ typedef struct EOCD
|
||||||
int off_cdh;
|
int off_cdh;
|
||||||
} EOCD;
|
} 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
|
typedef struct zip
|
||||||
{
|
{
|
||||||
// compression type
|
// compression type
|
||||||
char* cd;
|
char* cd;
|
||||||
CDH** cdh;
|
CDH** cdh;
|
||||||
|
LFH** lfh;
|
||||||
EOCD* eocd;
|
EOCD* eocd;
|
||||||
} zip;
|
} zip;
|
||||||
|
|
||||||
|
|
@ -78,11 +95,18 @@ typedef struct HN
|
||||||
unsigned char symbol;
|
unsigned char symbol;
|
||||||
unsigned char code;
|
unsigned char code;
|
||||||
unsigned char len;
|
unsigned char len;
|
||||||
} HN, *HT;
|
} HN;
|
||||||
|
|
||||||
|
typedef struct HT
|
||||||
|
{
|
||||||
|
unsigned char size;
|
||||||
|
HN* nodes;
|
||||||
|
} HT;
|
||||||
|
|
||||||
void get_eocd(raw* raw, zip* out);
|
void get_eocd(raw* raw, zip* out);
|
||||||
void get_cdh(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 parse_zip(char* filename, zip* out);
|
||||||
void decode_huffman_tree(char* encoded, HT* out);
|
void deflate(zip* in);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
@ -16,6 +16,7 @@ void get_eocd(raw* raw, zip* out)
|
||||||
{
|
{
|
||||||
out->eocd = (EOCD*) se;
|
out->eocd = (EOCD*) se;
|
||||||
out->cdh = (CDH**) malloc(out->eocd->number_of_entries * sizeof(CDH*));
|
out->cdh = (CDH**) malloc(out->eocd->number_of_entries * sizeof(CDH*));
|
||||||
|
out->lfh = (LFH**) malloc(out->eocd->number_of_entries * sizeof(LFH*));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -37,48 +38,16 @@ void get_cdh(raw* raw, zip* out)
|
||||||
for (int i = 0; i < out->eocd->number_of_entries; i++)
|
for (int i = 0; i < out->eocd->number_of_entries; i++)
|
||||||
{
|
{
|
||||||
out->cdh[i] = cdh;
|
out->cdh[i] = cdh;
|
||||||
|
out->lfh[i] = (LFH*) (raw->buf + cdh->off_lfh);
|
||||||
|
|
||||||
cdh = (CDH*) (((char*) cdh) + sizeof(CDH) + cdh->filename_length +
|
cdh = (CDH*) (((char*) cdh) + sizeof(CDH) + cdh->filename_length +
|
||||||
cdh->extra_field_length + cdh->file_comment_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;
|
return (char*) (in->lfh[n]) + sizeof(LFH) + in->lfh[n]->filename_length +
|
||||||
if (size == 0)
|
in->lfh[n]->extra_field_length;
|
||||||
{
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -49,5 +49,25 @@ int main(int argc, char** argv)
|
||||||
printf("\n}\n\n");
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue