Refactor test code
This commit is contained in:
parent
1ae8e5f050
commit
669915c0d7
3 changed files with 61 additions and 62 deletions
|
|
@ -76,7 +76,7 @@ typedef struct ISH
|
|||
unsigned last_block : 1;
|
||||
unsigned block_type : 2;
|
||||
};
|
||||
}
|
||||
};
|
||||
} ISH;
|
||||
|
||||
// Dynamic Huffman Code header for DEFLATE
|
||||
|
|
@ -96,26 +96,21 @@ typedef struct zip
|
|||
EOCD* eocd;
|
||||
} zip;
|
||||
|
||||
// Huffman Node and Table
|
||||
typedef struct HN
|
||||
{
|
||||
unsigned char symbol;
|
||||
unsigned char code;
|
||||
unsigned char len;
|
||||
} 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);
|
||||
char* get_encoded_block(zip* in, int n);
|
||||
void parse_zip(char* filename, zip* out);
|
||||
void deflate(zip* in);
|
||||
|
||||
|
||||
short decode_length_token(bitstream* bs, int token);
|
||||
int decode_distance_token(bitstream* bs, int token);
|
||||
|
||||
char* decode_type1_block(
|
||||
bitstream* bs,
|
||||
int uncompressed_size,
|
||||
char* decoded_data);
|
||||
|
||||
static const short length_codes[] = {
|
||||
3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27,
|
||||
31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258};
|
||||
|
|
@ -133,7 +128,4 @@ static const char extra_bits_distance_codes[] = {
|
|||
0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6,
|
||||
6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13};
|
||||
|
||||
short decode_length_token(bitstream* bs, int token);
|
||||
int decode_distance_token(bitstream* bs, int token);
|
||||
|
||||
#endif
|
||||
|
|
@ -2,6 +2,7 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "libmineziper_huffman_tree.h"
|
||||
#include "libmineziper_zip.h"
|
||||
|
||||
void get_eocd(raw* raw, zip* out)
|
||||
|
|
@ -45,12 +46,49 @@ void get_cdh(raw* raw, zip* out)
|
|||
}
|
||||
}
|
||||
|
||||
char* get_encoded_data(zip* in, int n)
|
||||
char* get_encoded_block(zip* in, int n)
|
||||
{
|
||||
return (char*) (in->lfh[n]) + sizeof(LFH) + in->lfh[n]->filename_length +
|
||||
in->lfh[n]->extra_field_length;
|
||||
}
|
||||
|
||||
char* decode_type1_block(bitstream* bs, int uncompressed_size, char* decoded_data)
|
||||
{
|
||||
tree tr = build_default_tree();
|
||||
tree tr_dist = build_default_dist_tree();
|
||||
|
||||
int i = 0, token;
|
||||
while (i < uncompressed_size && (token = next_token(bs, tr)) != END_OF_BLOCK)
|
||||
{
|
||||
if (token < END_OF_BLOCK)
|
||||
{
|
||||
printf("token[%d]: 0x%x\n", i, token);
|
||||
decoded_data[i++] = token;
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
int length = decode_length_token(bs, token);
|
||||
|
||||
if ((token = next_token(bs, tr_dist)) == END_OF_BLOCK)
|
||||
{
|
||||
printf("[ERROR] Got EndOfBlock when decoding distance token\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
int distance = decode_distance_token(bs, token);
|
||||
|
||||
printf("token[%d]: token[-%d] with length %d\n", i, distance, length);
|
||||
|
||||
for (int j = 0; j < length; j++)
|
||||
{
|
||||
decoded_data[i] = decoded_data[i - distance];
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
short decode_length_token(bitstream* bs, int token)
|
||||
{
|
||||
token -= END_OF_BLOCK + 1;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue