Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle all input/output functionality #14

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ add_executable(
pqueue.c
huffman_tree.c
key_table.c
io.c
)

add_executable(
Expand All @@ -28,6 +29,12 @@ add_executable(
pqueue.c
key_table.c
)
add_executable(
io_test
io_test.c
io.c
key_table.c
)


enable_testing()
Expand All @@ -47,7 +54,12 @@ set_tests_properties(pqueue_test PROPERTIES WILL_FAIL FALSE)
add_test(
htree_test
htree_test
)
set_tests_properties(htree_test PROPERTIES WILL_FAIL FALSE)

add_test(
io_test
io_test
)
set_tests_properties(htree_test PROPERTIES WILL_FAIL FALSE)

Expand Down
9 changes: 4 additions & 5 deletions hzip.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "frequency.h"
#include "pqueue.h"
#include "huffman_tree.h"
#include "io.h"

int huffman_compress(unsigned char* in_buffer, unsigned char* out_buffer, int size) {

Expand All @@ -28,14 +29,12 @@ int huffman_compress(unsigned char* in_buffer, unsigned char* out_buffer, int si

// Traverse tree and fill in key table
struct KeyTable key_table = convert_tree_to_keys(tree);
int bits_written = write_key_table(out_buffer, key_table);
bits_written += write_compressed_buffer(out_buffer + bits_written / 8, key_table, in_buffer, size);

destroy_tree(tree);
destroy_key_table(key_table);
destroy_pqueue(queue);
destroy_frequency_table(table);

//TODO remove this and write compressed buffer into output
memcpy(out_buffer, in_buffer, size);

return size;
return bits_written / 8;
}
49 changes: 49 additions & 0 deletions io.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include "io.h"
#include "key_table.h"
#include <stdio.h>
#include <string.h>

int write_key_table(unsigned char* buffer, struct KeyTable table) {
// TODO optimize this
memcpy(buffer, table.table, 0x100 * sizeof(struct KeyTableEntry));
return (0x100 * sizeof(struct KeyTableEntry)) * 8;
}

int read_key_table(unsigned char* buffer, struct KeyTable* table) {
// TODO optimize this
memcpy(table->table, buffer, 0x100 * sizeof(struct KeyTableEntry));
return (0x100 * sizeof(struct KeyTableEntry)) * 8;
}

int write_compressed_buffer(unsigned char* buffer, struct KeyTable table, unsigned char* input, int size) {
unsigned int buffer_pos = 0;
unsigned int input_pos = 0;
unsigned char current_chunk = 0;
unsigned int current_chunk_filled = 0;
while (input_pos < size) {
unsigned int encoded_length = lookup_bit_length(table, input[input_pos]);
unsigned int encoded_value = lookup_table_value(table, input[input_pos]);
for (unsigned int current_value_pos = 0; current_value_pos < encoded_length; current_value_pos++) {
current_chunk<<=1;
current_chunk |= (encoded_value >> (encoded_length - current_value_pos - 1)) & 1;
current_chunk_filled++;
if (current_chunk_filled == 8) {
buffer[buffer_pos] = current_chunk;
current_chunk = 0;
current_chunk_filled = 0;
buffer_pos++;
}
}
input_pos++;
}
if (current_chunk_filled > 0) {
buffer[buffer_pos] = current_chunk;
buffer_pos++;
}
return (buffer_pos * 8) + current_chunk_filled;
}

int read_compressed_buffer(unsigned char* buffer, struct KeyTable table, unsigned char* output, int size) {
printf("Not implemented");
return 0;
}
11 changes: 11 additions & 0 deletions io.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef IO_H
#define IO_H

#include "key_table.h"

int write_key_table(unsigned char* buffer, struct KeyTable table); // Returns # of bits written
int read_key_table(unsigned char* buffer, struct KeyTable* table); // Returns # of bits read
int write_compressed_buffer(unsigned char* buffer, struct KeyTable table, unsigned char* input, int size); // returns # of bits written
int read_compressed_buffer(unsigned char* buffer, struct KeyTable table, unsigned char* input, int size); // returns # of bits read

#endif
6 changes: 6 additions & 0 deletions io_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <assert.h>
#include "io.h"

int main(int argc, char** argv) {
assert("Not implemented" == 0);
}
2 changes: 1 addition & 1 deletion key_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ struct KeyTableEntry {
};

struct KeyTable {
struct KetTableEntry* table;
struct KeyTableEntry* table;
};

struct KeyTable create_key_table();
Expand Down
2 changes: 1 addition & 1 deletion main.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ int main(int argc, char** argv) {
}
fclose(input_file);

unsigned char* compressed_buffer = calloc(file_size * sizeof(char), 1);
unsigned char* compressed_buffer = calloc(file_size * sizeof(char), 1); // TODO should be creating the buffer in huffman_compress rather than here to get the size right

int hzip_result = huffman_compress(file_buffer, compressed_buffer, file_size);

Expand Down