Browse Source

Have bugged wocky

master
stew3254 2 years ago
parent
commit
1cd8c0af36
  1. 3
      jabber.c
  2. 18
      tree.c
  3. 3
      tree.h
  4. 108
      wocky.c

3
jabber.c

@ -26,7 +26,6 @@ void jabber(char *buff, int buff_len, FILE *f) {
n->zero = newn(++t->size);
itob(n->index, len, f);
fputc('0', f);
fflush(f);
}
else {
n = n->zero;
@ -57,8 +56,6 @@ void jabber(char *buff, int buff_len, FILE *f) {
//Print last index
if (!isleaf(n))
itob(n->index, len, f);
//print_tree(t->root);
//printf("\n");
}

18
tree.c

@ -1,18 +0,0 @@
#include "stdio.h"
#include "tree.h"
void print_tree(node * v) {
printf("%d -> (", v -> index);
if (v -> zero != NULL) {
print_tree(v -> zero);
} else {
printf("NULL");
}
printf(" | ");
if (v -> one != NULL) {
print_tree(v -> one);
} else {
printf("NULL");
}
printf(")");
}

3
tree.h

@ -15,9 +15,6 @@ typedef struct Tree {
int size;
} tree;
void print_tree(node * v);
//void tprint(tree *t);
static inline bool isleaf(node *n) {return (n->zero == NULL && n->one == NULL);}
static inline bool tisempty(tree *t) {return (t->root->zero == NULL && t->root->one == NULL);}
static inline int tsize(tree *t) {return t->size;}

108
wocky.c

@ -0,0 +1,108 @@
#include "assert.h"
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "unistd.h"
//Print to binary
int btoi(char* b, int len) {
int n = 0;
for (int i = len; i < len; ++i) {
n += (b[i] - '0') << (len-i);
}
return n;
}
//LZ77 decompression (No Error checking)
void wocky(char *buff, int buff_len, FILE *f) {
int table_max = 32, table_len = 1;
char **table = (char**) calloc(table_max, sizeof(char*));
table[0] = "";
int buff_index = 0, len = 0, bound = 1;
while(buff_index < buff_len) {
char *bin_int = (char*) calloc(len+1, sizeof(char));
int bin_index = 0;
for (int i = 0; i < len; ++i) {
bin_int[i] = buff[buff_index+i];
}
//Get binary index
bin_index = btoi(bin_int, len);
char *s = NULL;
if (table[bin_index] != NULL) {
s = (char*) calloc(strlen(table[bin_index])+2, sizeof(char));
//Copy contents to s
strcat(s, table[bin_index]);
}
else {
s = (char*) calloc(2, sizeof(char));
}
buff_index += len;
//Make sure to account for end to file edge case
if (buff_index < buff_len)
s[strlen(s)] = buff[buff_index++];
//Make sure table isn't full
if (table_len == table_max) {
table_max <<= 1;
table = (char**) realloc(table, table_max*sizeof(char*));
memset(table+table_len, 0, table_len);
}
//Increase upper bound
if (table_len == bound) {
bound <<= 1;
++len;
}
table[table_len++] = s;
fprintf(f, "%s", s);
fflush(f);
free(bin_int);
}
}
int main(int argc, char **argv) {
assert(argc == 3);
struct stat sbuf = {};
if (stat(argv[1], &sbuf) != 0) {
fprintf(stderr, "Error finding file\n");
return -1;
}
//Assume we can read the file
char *inbuff = (char*) malloc((sbuf.st_size+1) * sizeof(char));
char *outbuff = (char*) malloc((sbuf.st_size+1) * sizeof(char));
int out_len = 0;
FILE *f = fopen(argv[1], "r");
if (f == NULL) {
fprintf(stderr, "Error opening input file\n");
return -1;
}
if (fread(inbuff, sizeof(char), sbuf.st_size, f) != sbuf.st_size) {
fclose(f);
fprintf(stderr, "Error reading file\n");
return -1;
}
//Close file when done reading
fclose(f);
f = fopen(argv[2], "w");
if (f == NULL) {
fprintf(stderr, "Error opening output file\n");
return -1;
}
//Write contents of jabber back out
wocky(inbuff, sbuf.st_size, stdout);
fclose(f);
return 0;
}
Loading…
Cancel
Save