Code:
#include <stdio.h>
struct bitree
{
int freq;
char symbol;
struct bitree *left;
struct bitree *right;
}bintree;
int count=0;
int count2=0;
int binary_array[8][8];
int letter_array[8];
int byte_pos=0;
int binary_nums[20];
void insert(int freq,char letter,struct bitree **node);
void tree_search(char letter,struct bitree **node);
void binary2_print(char letter);
void reverse_tree(int num_right,int num_right_const,struct bitree **node,int one,int twice);
void binary_print(char letter);
int main()
{
struct bitree **root;
struct bitree **root2;
unsigned long file_len;
FILE *SOURCE;
FILE *enc_w;
int a,i,b;
int ammount[256];
int freq_ammount;
int got_one=0;
int byte=0;
int total=0;
int mask;
int right=0;
int left=0;
int letter_pos=0;
int mask2;
char *buffer;
char freq[256];
char holder;
char chars[256];
char buffc;
char data[4096];
for(i=0;i<20;i++)
binary_nums[i]=0;
root=(struct bintree *)malloc(sizeof(bintree));
root2=(struct bintree *)malloc(sizeof(bintree));
if((SOURCE=fopen("source.c","r"))==NULL)
{
perror("fopen");
exit(1);
}
if((enc_w=fopen("source.enc","w+b"))==NULL)
{
perror("fopen source.enc");
exit(1);
}
for(i=0;i<256;i++)
{
ammount[i]=0;
chars[i]=0;
}
//add chars and freqs to the pos of the letter
a=0;
i=0;
while((buffc=fgetc(SOURCE))!=EOF)
{
ammount[buffc]++;
chars[buffc]=buffc;
data[i]=buffc;
i++;
}
//order freqs and symbols to make in order table
for(a=0;a<256;a++)
for(i=0;i<256;i++)
if(ammount[a]<ammount[i])
{
holder=ammount[a];
ammount[a]=ammount[i];
ammount[i]=holder;
holder=chars[a];
chars[a]=chars[i];
chars[i]=holder;
}
//take out beg 0's
a=0;
for(i=0;i<256;i++)
if(ammount[i]!=0)
{
ammount[a]=ammount[i];
chars[a]=chars[i];
a++;
}
freq_ammount=a;
for(i=0;i<freq_ammount;i++)
printf("char:%c = ammount:%i\n",chars[i],ammount[i]);
//add zero for symbol, 1 for compressed
for(i=0;i<7;i++)
total+=ammount[i];
for(i=freq_ammount-1;i>freq_ammount-8;i--)
{
insert(0,0,&(*root));
insert(ammount[i],chars[i],&(*root));
count2++;
count=0;
}
for(i=freq_ammount-1;i>freq_ammount-8;i--)
fprintf(enc_w,"%c",chars[i]);
fputc('*',enc_w);
a=0;
count=0;
int first_zero=0;
for(i=0;data[i]!=NULL;i++)
{
printf("%c\n",data[i]);
for(count=0,a=freq_ammount-1;a>freq_ammount-8;a--,count++)
{
if(data[i]==chars[a])
{
for(b=0;b<9;b++)
{
if(b==0)
byte_pos++;
if(byte_pos>=8)
{
binary2_print(byte);
fprintf(enc_w,"%c",byte);
byte_pos=0;
byte=0;
}
mask=0x80>>byte_pos;
got_one=1;
byte_pos++;
if(binary_array[b][count]==1)
byte|=mask;
else
break;
got_one++;
} //end for
} //end if data[i]==chars[a]
}
if(got_one==0)
{
binary_print(data[i]);
for(a=0;a<8;a++)
{
if(a==0)
{
byte|=0x80>>byte_pos;
byte_pos++;
}
mask=0x80>>byte_pos;
if(letter_array[a]==1)
{
byte|=mask;
byte_pos++;
}
else
byte_pos++;
if(byte_pos>=8)
{
binary2_print(byte);
fprintf(enc_w,"%c",byte);
byte_pos=0;
byte=0;
}
}
}
got_one=0;
}
fseek(enc_w,0,SEEK_END);
file_len=ftell(enc_w);
fseek(enc_w,0,SEEK_SET);
buffer=(char *)malloc(file_len+1);
fread(buffer,file_len,1,enc_w);
printf("%s\n",buffer);
i=0;
b=0;
int c;
count=0;
count2=0;
for(i=0;buffer[i]!='*';i++)
{
insert(0,0,&(*root2));
insert(1,buffer[i],&(*root2));
}
a=i;
b=0;
left=0;
byte_pos=0;
for(i=8;i<file_len;i++)
{
//binary2_print(buffer[i]));
byte=0;
byte_pos=0;
for(;;)
{
mask2=0x80>>byte_pos;
if(letter_pos==0)
{
if(buffer[i]&mask2)
{
byte_pos++;
letter_pos=0;
for(b=0;b<8;b++)
{
if(byte_pos>=8)
{
byte_pos=0;
i++;
}
mask=0x80>>b;
mask2=0x80>>byte_pos;
if(buffer[i]&mask2)
{
byte|=mask;
}
byte_pos++;
}
printf("%c",byte);
}
else
{
letter_pos++;
byte_pos++;
}
}
else
{
if(byte_pos>=8)
{
byte_pos=0;
break;
}
mask=0x80>>byte_pos;
if(buffer[i]&mask)
{
mask=0x80>>byte_pos;
right++;
letter_pos++;
byte_pos++;
}
else
{
byte_pos++;
reverse_tree(right+1,right,&(*root2),1,0);
right=0;
letter_pos=0;
//send num rights to func that has for loop going ammount of rights then print char
}
}
}
}
return(0);
}
void binary_print(char letter)
{
int mask=0x80;
int i;
for(i=0;i<8;i++)
{
if(letter&mask)
letter_array[i]=1;
else
letter_array[i]=0;
mask>>=1;
}
}
void binary2_print(char letter)
{
int mask=0x80;
int i;
for(i=0;i<8;i++)
{
if(letter&mask)
printf("%i",1);
else
printf("%i",0);
mask>>=1;
}
printf("\n");
}
void insert(int freq,char letter,struct bitree **node)
{
if((*node)==NULL)
{
(*node)=(struct bitree *)malloc(sizeof(node));
(*node)->symbol=letter;
(*node)->freq=freq;
(*node)->left=NULL;
(*node)->right=NULL;
}
else if(freq!=0)
if((*node)->left!=NULL)
insert(freq,letter,&(*node)->right);
else
{ binary_nums[count2]++;
insert(freq,letter,&(*node)->left);
}
else if(freq==0)
{
binary_array[count++][count2]=1;
binary_nums[count2]++;
insert(0,0,&(*node)->right);
}
}
void reverse_tree(int num_right,int num_right_const,struct bitree **node,int one,int twice)
{
int i;
if(one==1 && num_right>0)
{
reverse_tree(num_right-1,num_right_const,&(*node)->right,1,twice+1);
if(twice==num_right_const)
{
reverse_tree(0,num_right_const,&(*node)->left,0,twice);
}
}
if(twice==num_right_const)
printf("%c",(*node)->symbol);
}
much thanks,