Browse Source

Make jabber

master
stew3254 2 years ago
parent
commit
7cb7e9b69e
  1. 3
      .gitignore
  2. 8
      atob.c
  3. 128
      function.c
  4. 84
      jabber.c
  5. 60
      tree.c
  6. 19
      tree.h

3
.gitignore

@ -4,3 +4,6 @@ jabber
wocky
Session.vim
test*
in.txt
binary.txt
jabber.txt

8
atob.c

@ -8,12 +8,12 @@
//Print binary
void printb(FILE *f, const char *s, const int len) {
char *out = (char*) malloc(9*sizeof(char));
out[8] = '\0';
char *out = (char*) malloc((8*sizeof(char)+1)*sizeof(char));
out[sizeof(char)] = '\0';
for (int i = 0; i < len; ++i) {
char c;
for (int j = 7; j > -1; --j) {
out[7-j] = '0' + ((s[i] >> j) & 1);
for (int j = sizeof(char)-1; j > -1; --j) {
out[sizeof(char)-1-j] = '0' + ((s[i] >> j) & 1);
}
fprintf(f, "%s", out);
}

128
function.c

@ -0,0 +1,128 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void jabber ( FILE *ifp, FILE *ofp );
int main( int argc, char *argv[] ) {
FILE *ifp, *ofp;
if (argc < 3) {
fprintf(stderr, "Not enough arguments\n");
exit(1);
}
if (!(ifp = fopen(argv[1],"r"))) {
fprintf(stderr,"Cannot open file %s\n",argv[1]);
exit(1);
}
if (!(ofp = fopen(argv[2],"w"))) {
fprintf(stderr,"Cannot open file %s\n",argv[2]);
exit(1);
}
jabber(ifp, ofp);
return 0;
}
typedef struct node_t {
int index;
// Children
struct node_t * zero;
struct node_t * one;
} node;
node * new_node( int index ) {
node * n = malloc(sizeof(node));
n -> index = index;
n -> zero = NULL;
n -> one = NULL;
return n;
}
void print_tree(node * v);
void jabber( FILE *ifp, FILE *ofp ) {
// Read the entire file into a buffer
fseek(ifp, 0L, SEEK_END);
long sz = ftell(ifp);
rewind(ifp);
char * buffer = malloc(sz);
fread(buffer, 1, sz, ifp);
// Start the tree
node * root = new_node(0);
// Start a cursor
node * cursor = root;
int i;
int index = 0;
int bits = 0;
for (i = 0; i < sz; ++i) {
char c = buffer[i];
fprintf(stderr, "%d | %d\n", index, bits);
if (c == '0' && cursor -> zero == NULL) {
cursor -> zero = new_node(++index);
cursor = root;
if ((1 << bits) & index) {
++bits;
}
int j;
for (j = bits; j >= 0; --j) {
fputc('0' + ((index >> j) & 1), ofp);
//fprintf(stderr, "%c", '0' + (index >> j) & 1);
}
fputc(buffer[++i], ofp);
} else if (c == '1' && cursor -> one == NULL) {
cursor -> one = new_node(++index);
cursor = root;
if ((1 << bits) & index) {
++bits;
}
int j;
for (j = 8; j >= 0; --j) {
fputc('0' + ((index >> j) & 1), ofp);
//fprintf(stderr, "%c", '0' + (index >> j) & 1);
}
fputc(buffer[++i], ofp);
} else if (c == '0') {
cursor = cursor -> zero;
} else if (c == '1') {
cursor = cursor -> one;
} else {
fprintf(stderr, "Invalid character: %c\n", c);
exit(1);
}
}
print_tree(root);
printf("\n");
}
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(")");
}

84
jabber.c

@ -1,23 +1,68 @@
#include "assert.h"
#include "stdlib.h"
#include "stdbool.h"
#include "string.h"
#include "stdio.h"
#include "string.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "unistd.h"
#include "tree.h"
//Define node on tree
typedef struct Node {
struct Node *zero;
struct Node *one;
int index;
} node;
//Print to binary
static inline void itob(int i, int len, FILE *f) {
for (int j = len-1; j > -1; --j) {
fputc('0' + ((i >> j) & 1), f);
}
}
//LZ77 compression
void jabber(char *buff, int buff_len, FILE *f) {
tree *t = newt();
node *n = t->root;
int len = 0, bound = 1;
for (int i = 0; i < buff_len; ++i) {
if (buff[i] == '0') {
if (n->zero == NULL) {
n->zero = newn(++t->size);
itob(n->index, len, f);
fputc('0', f);
fflush(f);
}
else {
n = n->zero;
continue;
}
}
else {
if (n->one == NULL) {
n->one = newn(++t->size);
itob(n->index, len, f);
fputc('1', f);
fflush(f);
}
else {
n = n->one;
continue;
}
}
//Reset node after printing
n = t->root;
//Check if upper bound needs to be increased
if (t->size == bound) {
++len;
bound <<= 1;
}
}
//Print last index
if (!isleaf(n))
itob(n->index, len, f);
//print_tree(t->root);
//printf("\n");
}
//Define tree
typedef struct Tree {
node *root;
int size;
} tree;
int main() {
int main(int argc, char **argv) {
assert(argc == 3);
struct stat sbuf = {};
if (stat(argv[1], &sbuf) != 0) {
@ -25,7 +70,9 @@ int main() {
return -1;
}
//Assume we can read the file
char *buff = (char*) malloc(sbuf.st_size * sizeof(char));
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) {
@ -33,7 +80,7 @@ int main() {
return -1;
}
if (fread(buff, sizeof(char), sbuf.st_size, f) != sbuf.st_size) {
if (fread(inbuff, sizeof(char), sbuf.st_size, f) != sbuf.st_size) {
fclose(f);
fprintf(stderr, "Error reading file\n");
return -1;
@ -48,8 +95,9 @@ int main() {
return -1;
}
tree *t = newt();
//Write contents of jabber back out
jabber(inbuff, sbuf.st_size, f);
close();
fclose(f);
return 0;
}

60
tree.c

@ -1,52 +1,18 @@
#include "stdlib.h"
#include "stdbool.h"
#include "string.h"
#include "stdio.h"
#include "tree.h"
static inline int isleaf(node *n) {return (n->zero == NULL && n->one == NULL);}
node *newn(int index) {
node *n = (node*) malloc(sizeof(node));
n->zero = NULL;
n->one = NULL;
n->index = index;
}
int tadd(tree *t, char *tag) {
node *current = t->root;
int i;
for (i = 0; i < strlen(tag); ++i) {
if (isleaf(current)) {
++t->size;
if (current == t->root) {
if (tag[i] == '0')
t->root->zero = newn(t->size);
else
t->root->one = newn(t->size);
return t->size;
}
else {
if (tag[i] == '0')
current->zero = newn(t->size);
else
current->one = newn(t->size);
}
}
else if (tag[i] == '0') {
current = current->zero;
}
else {
current = current->one;
}
}
if (isleaf(current)) {
if (tag[i] == '0')
current->zero = newn(t->size);
else
current->one = newn(t->size);
return i;
void print_tree(node * v) {
printf("%d -> (", v -> index);
if (v -> zero != NULL) {
print_tree(v -> zero);
} else {
printf("NULL");
}
else {
return -1;
printf(" | ");
if (v -> one != NULL) {
print_tree(v -> one);
} else {
printf("NULL");
}
printf(")");
}

19
tree.h

@ -1,3 +1,7 @@
#ifndef TREE_H
#define TREE_H
#include "stdlib.h"
#include "stdbool.h"
typedef struct Node {
@ -11,13 +15,26 @@ typedef struct Tree {
int size;
} tree;
node *newn(int index);
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;}
static inline node *newn(int index) {
node *n = (node*) malloc(sizeof(node));
n->zero = NULL;
n->one = NULL;
n->index = index;
return n;
}
static inline tree *newt() {
tree *t = (tree*) malloc(sizeof(tree));
t->root = newn(0);
t->size = 0;
return t;
}
#endif
Loading…
Cancel
Save