Commit 94e5b5c8 by Rudolf

Minor refactoring. Update throwaway size info and make it possible to change

parent d976ef40
Showing with 40 additions and 38 deletions
......@@ -7,36 +7,9 @@
#include "header.h"
#include "print_helper.h"
#define HUFFMAN_START 3
void free_tree(struct tree *parent)
{
if (parent == NULL)
return;
free_tree(parent->left);
free_tree(parent->right);
free(parent);
}
long get_file_size(FILE *file)
{
long pos = ftell(file);
fseek(file, 0, SEEK_END);
long size = ftell(file);
fseek(file, pos, SEEK_SET);
return size;
}
/* free it yourself faget. */
char *read_text(FILE *file, long size)
{
char *buf = malloc(size + 1);
fread(buf, size, 1, file);
buf[size] = '\0';
return buf;
}
/* The first bits that are reserved for throwaways. The throwaways indicates the
* number of bits to throw away at the end when decoding the file. */
#define THROWAWAY_SIZE 3
int insert_throwaways(FILE *file, unsigned char throwaways)
{
......@@ -47,9 +20,9 @@ int insert_throwaways(FILE *file, unsigned char throwaways)
fseek(file, 0, SEEK_SET);
fread(&firstbyte, sizeof(firstbyte), 1, file);
throwaways &= 0x7;
throwaways &= 0xff >> (8 - THROWAWAY_SIZE);
firstbyte |= (throwaways << 5);
firstbyte |= (throwaways << (8 - THROWAWAY_SIZE));
fseek(file, 0, SEEK_SET);
fwrite(&firstbyte, sizeof(firstbyte), 1, file);
......@@ -68,22 +41,51 @@ unsigned char read_throwaways(FILE *file)
fseek(file, 0, SEEK_SET);
fread(&firstbyte, sizeof(firstbyte), 1, file);
firstbyte >>= 5;
firstbyte &= 0x7;
firstbyte >>= 8 - THROWAWAY_SIZE;
firstbyte &= 0xff >> (8 - THROWAWAY_SIZE);
fseek(file, filepos, SEEK_SET);
return firstbyte;
}
int encode_file(FILE *in, FILE *out)
long get_file_size(FILE *file)
{
long pos = ftell(file);
fseek(file, 0, SEEK_END);
long size = ftell(file);
fseek(file, pos, SEEK_SET);
return size;
}
static void free_tree(struct tree *parent)
{
if (parent == NULL)
return;
free_tree(parent->left);
free_tree(parent->right);
free(parent);
}
/* free it yourself faget. */
static char *read_text(FILE *file, long size)
{
char *buf = malloc(size + 1);
fread(buf, size, 1, file);
buf[size] = '\0';
return buf;
}
static int encode_file(FILE *in, FILE *out)
{
struct tree *tree;
struct BIT_BUFFER bitbuf;
bb_init(&bitbuf, out);
/* Make room for throwaways right now. */
bitbuf.pos += HUFFMAN_START;
bitbuf.pos += THROWAWAY_SIZE;
long size = get_file_size(in);
......@@ -106,13 +108,13 @@ int encode_file(FILE *in, FILE *out)
bb_flush(&bitbuf);
}
int decode_file(FILE *in, FILE *out)
static int decode_file(FILE *in, FILE *out)
{
struct tree *tree = malloc(sizeof(*tree));
struct BIT_BUFFER bitbuf;
bb_init(&bitbuf, in);
bitbuf.pos += HUFFMAN_START;
bitbuf.pos += THROWAWAY_SIZE;
fseek(in, 0, SEEK_SET);
decode_header(&bitbuf, tree);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment