Refactor test code

This commit is contained in:
atxr 2024-02-20 15:38:03 +01:00
parent 1ae8e5f050
commit 669915c0d7
3 changed files with 61 additions and 62 deletions

View file

@ -76,7 +76,7 @@ typedef struct ISH
unsigned last_block : 1; unsigned last_block : 1;
unsigned block_type : 2; unsigned block_type : 2;
}; };
} };
} ISH; } ISH;
// Dynamic Huffman Code header for DEFLATE // Dynamic Huffman Code header for DEFLATE
@ -96,26 +96,21 @@ typedef struct zip
EOCD* eocd; EOCD* eocd;
} zip; } 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_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); char* get_encoded_block(zip* in, int n);
void parse_zip(char* filename, zip* out); void parse_zip(char* filename, zip* out);
void deflate(zip* in); 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[] = { static const short length_codes[] = {
3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 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}; 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, 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}; 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 #endif

View file

@ -2,6 +2,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "libmineziper_huffman_tree.h"
#include "libmineziper_zip.h" #include "libmineziper_zip.h"
void get_eocd(raw* raw, zip* out) 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 + return (char*) (in->lfh[n]) + sizeof(LFH) + in->lfh[n]->filename_length +
in->lfh[n]->extra_field_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) short decode_length_token(bitstream* bs, int token)
{ {
token -= END_OF_BLOCK + 1; token -= END_OF_BLOCK + 1;

View file

@ -1,4 +1,5 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <strings.h> #include <strings.h>
#include "libmineziper.h" #include "libmineziper.h"
@ -34,7 +35,7 @@ void main()
{ {
printf("BLOC %d:\n", k); printf("BLOC %d:\n", k);
unsigned int real_size = zip.lfh[k]->uncompressed_size; unsigned int uncompressed_size = zip.lfh[k]->uncompressed_size;
if (zip.lfh[k]->compressed_size == 0) if (zip.lfh[k]->compressed_size == 0)
{ {
@ -44,7 +45,7 @@ void main()
{ {
if (zip.cdh[k]->compression_method == DEFLATE) if (zip.cdh[k]->compression_method == DEFLATE)
{ {
char* data = get_encoded_data(&zip, k); char* data = get_encoded_block(&zip, k);
bitstream bs = init_bitstream(data, 0 /*TODO WWEUWIEUWIEUWI*/, 0); bitstream bs = init_bitstream(data, 0 /*TODO WWEUWIEUWIEUWI*/, 0);
@ -56,49 +57,15 @@ void main()
if (deflate_header.block_type == 1) if (deflate_header.block_type == 1)
{ {
tree tr = build_default_tree(); char* decoded_data = malloc(uncompressed_size);
tree tr_dist = build_default_dist_tree();
char* decoded_data = malloc(real_size); decode_type1_block(&bs, uncompressed_size, decoded_data);
int i = 0;
int token;
while (i < real_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++;
}
}
}
FILE* tmp_file = fopen("/tmp/test.txt", "w"); FILE* tmp_file = fopen("/tmp/test.txt", "w");
fwrite(decoded_data, 1, i, tmp_file); fwrite(decoded_data, 1, uncompressed_size, tmp_file);
fclose(tmp_file); fclose(tmp_file);
free(decoded_data);
} }
else if (deflate_header.block_type == 2) else if (deflate_header.block_type == 2)
@ -117,6 +84,7 @@ void main()
} }
} }
/*
void handle_type2(char* data) void handle_type2(char* data)
{ {
DHCH* dynamic_huffman_code_header = data; DHCH* dynamic_huffman_code_header = data;
@ -145,4 +113,5 @@ void handle_type2(char* data)
{ {
printf("For length %d: %d\n", i, code_length[i].len); printf("For length %d: %d\n", i, code_length[i].len);
} }
} }
*/