Skip to content

Instantly share code, notes, and snippets.

@andrewrcollins
Created January 4, 2012 18:11
Show Gist options
  • Save andrewrcollins/1561270 to your computer and use it in GitHub Desktop.
Save andrewrcollins/1561270 to your computer and use it in GitHub Desktop.
#TJHSST ~ Huffman encoding/decoding and Hamming codes
/* ARC File Viewer */
#include <stdio.h>
/* The header portion of an ARChive file is as follows. */
typedef struct {
char headerflag; /* This flag is always 0x1a. */
char compresstype; /* In range 1..9 identifies the compression type. */
char filename[13]; /* ASCIIZ format for the filename. */
long int cfilesize; /* The compressed file's size. */
unsigned day : 5; /* Day of creation. */
unsigned month : 4; /* Month */
unsigned year : 7; /* Year */
unsigned second : 5; /* Seconds */
unsigned minute : 6; /* Minutes */
unsigned hour : 5; /* Hours */
int crc; /* CRC (Cyclic Redundancy Check) for the file. */
long int rfilesize; /* The uncompressed file's size. */
} ARCheader;
/* These are the corresponding strings to compression methods used, and
identified by the compresstype field in ARCheader. */
char compression[][10] =
{ "Stored","Stored","Packed",
"Squeezed","Crunched","Crunched",
"Crunched","Crunched","Squashed",
"Unknown" };
/* The following function reads in the next header in the file pointed to
by ARCfile. Returns zero on success. -1 means end of ARCfile, 1
means error in ARCfile. */
int readheader(ARCfile,header)
FILE *ARCfile;
ARCheader *header;
{
if(!fread(header,sizeof(ARCheader),1,ARCfile))
return -1;
if(fseek(ARCfile,header->cfilesize,SEEK_CUR))
return 1;
return 0;
}
/* Displays the appropriate information in a formatted matter. Returns
nonzero on termination, unless header is NULL. */
int displayheader(header)
ARCheader *header;
{
if(header==NULL)
return 0;
printf("%-16s%6ld%10s%9ld%6.1f%% %02d-%02d-%02d %02d:%02d:%02d\n",
strupr(header->filename),header->rfilesize,
compression[header->compresstype-1],header->cfilesize,
(1-(float)header->cfilesize/(float)header->rfilesize)*100.0,
header->month,header->day,header->year+80,
header->hour,header->minute,header->second*2);
return 1;
}
/* This program will read the ARCfile specified by a command line
parameter, and display the various ARCfiles stored within it. */
main(argc,argv)
int argc;
char *argv[];
{
FILE *ARCfile;
ARCheader header;
/* These variables will tally the various quantities in the ARCfile. */
int ARCfiles=0;
long int ARClength=0,REALlength=0;
float Ratio=0.0;
if(argc<2)
fputs("Filename Not Specified",stderr),exit(1);
if((ARCfile=fopen(argv[1],"rb"))==NULL)
fputs("File Not Found",stderr),exit(1);
printf("Searching: %s\n\n",strupr(argv[1]));
puts("Filename Length Method Size Ratio Date Time");
puts("------------ ------ -------- ------ ----- ---- ----");
while(!readheader(ARCfile,&header)) {
ARCfiles++;
REALlength+=header.rfilesize;
ARClength+=header.cfilesize;
Ratio+=100.0-((float)header.cfilesize/(float)header.rfilesize)*100.0;
displayheader(&header);
}
Ratio/=ARCfiles;
puts("------------ ------ ------ -----");
printf("%-12d %6ld %6ld %4.1f%%\n\n",
ARCfiles,REALlength,ARClength,Ratio);
fclose(ARCfile);
}
/*
Computer Architecture
Huffman Data Compression and Extraction Program
Andy Collins
Mr. Hyatt First Period
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* some defines, make BufferSize smaller for faster disk drives/computers */
#define BufferSize 2048
/* defines are awesome, you can make a macro function which is spiffy
these two return the low and high nibbles of a byte respectively */
#define LowNibble(x) (x&0x0f)
#define HighNibble(x) ((x>>4)&0x0f)
/* some defines for checking emptiness, used in making tree */
#define Nil (-1)
/* this header goes at the beginning of each compressed file so that
this program can be used for any type of file, TXT, COM, EXE, etc.
a tree formed all in one array, no messy memory allocation, deallocation
also used as the digit counter and digit holder, the index fields
aren't used then, but are used when making the tree. */
struct treetype {
signed digit : 6; /* holds Nil or the digit a four bit number */
signed left : 6;
signed right : 6;
signed parent : 6;
unsigned bitcnt : 4; /* holds number of relevant bits in bits */
unsigned bits : 12; /* holds bit pattern of Huffman code */
long int count;
};
/* it has left,right, and parent indices instead of pointers, same thing,
it will be the header to go at the front of every compressed file */
int compare(struct treetype *,struct treetype *);
/* a function prototype for when I quick-sort the digits array */
main(argc,argv)
int argc;
char *argv[];
{
struct treetype tree[32];
char filename[12];
/* enter <filename> to compress */
if(argc!=2) {
printf("you forgot file name\n");
/* an exitcode of 0 means no error, anything else is some sort of error */
exit(1);
}
strcpy(filename,argv[1]);
/* proceed to decode the file */
decode(filename,tree);
}
/* this function is only used in the debugging part of the program, it
will be deleted later on, when I know the tree works */
int showtree(tree)
struct treetype tree[32];
{
int cnt;
/* print out the tree so far */
for(cnt=0;cnt<32;cnt++) {
if(!(cnt%16))
printf("index digit left right parent\n");
printf("%3d ",cnt);
/* print out the information, with some formatting, Branch and such. */
if(tree[cnt].digit==Nil)
printf("Branch %4d %5d",tree[cnt].left,tree[cnt].right);
else
printf("%6X NIL NIL",tree[cnt].digit);
if(tree[cnt].parent==Nil)
printf(" NIL\n");
else
printf(" %6d\n",tree[cnt].parent);
}
}
/* function takes two ints and prints out the first bitcnt bits of bits */
int printb(bits,bitcnt)
int bits,bitcnt;
{
for(;bitcnt>0;bitcnt--) {
/* print out the 1s and 0s in the bit pattern */
printf("%c",(bits&1)?'1':'0');
/* shift it right so as to get at the next bit */
bits>>=1;
}
printf("\n");
}
/* the workhorse part of the code, does the decoding of the file
after a tree has been read in from the file with which contains
all the information needed to uncompress the file. */
int decode(name,tree)
char *name;
struct treetype tree[32];
{
FILE *infile,*outfile;
char *buffer;
int bit,traverse=31; /* traversing variables for the decoding process */
if((infile=fopen(name,"rb"))==NULL) { /* try to open input and output */
printf("couldn't open the input file\n"); /* files for decoding */
exit(1);
}
*strchr(name,'.')=0; /* remove the extension from the input filename */
strcat(name,".EXT"); /* and append the .EXT extension instead */
if((outfile=fopen(name,"wb"))==NULL) {
printf("couldn't open the output file\n");
exit(1);
}
fgetc(infile); /* read in the END-OF-FILE character, but don't do
anything with it.*/
fread(tree,sizeof(struct treetype),31,infile); /* then the header part */
showtree(tree);
do {
traverse=30;
do {
bit=readbit(infile);
if(bit!=Nil) {
if(bit)
traverse=tree[traverse].left;
else
traverse=tree[traverse].right;
}
} while(tree[traverse].digit==Nil&&bit!=Nil);
if(bit!=Nil)
writenibble(outfile,tree[traverse].digit);
} while(!feof(infile));
fclose(infile);
fclose(outfile);
}
int readbit(infile)
FILE *infile;
{
static char inchar=0,incnt=0;
int tmp;
if(incnt==0) {
if(feof(infile))
return Nil;
inchar=fgetc(infile);
incnt=8;
}
incnt--;
tmp=(inchar&0x1);
inchar>>=1;
return tmp>0;
}
/* this function writes individual bits to a file, a static variable
is used to store the current bit pattern that is added to until
the number of bits to be written is 8 */
int writenibble(outfile,nibble)
FILE *outfile;
char nibble;
{
static char outchar=0,outcnt=0;
/* these variables are declared once on the first call and not disposed
until the program terminates */
outchar|=nibble;
switch(outcnt) {
case 0 :
outchar<<=4;
outcnt++;
break;
case 1 :
fputc(outchar,outfile);
outchar=outcnt=0;
break;
}
}
/*
Computer Architecture
Huffman Data Compression and Extraction Program
Andy Collins
Mr. Hyatt First Period
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mem.h>
/* all the include files known to person-kind
mem.h contains the memory mangling functions, I use one of the functions
to do a quick array clearing. the rest are for printf, etc. */
/* some defines, make BufferSize smaller for faster disk drives/computers */
#define BufferSize 2048
/* defines are awesome, you can make a macro function which is spiffy
these two return the low and high nibbles of a byte respectively */
#define LowNibble(x) (x&0x0f)
#define HighNibble(x) ((x>>4)&0x0f)
/* some defines for checking emptiness, used in making tree */
#define Nil (-1)
/* this header goes at the beginning of each compressed file so that
this program can be used for any type of file, TXT, COM, EXE, etc.
a tree formed all in one array, no messy memory allocation, deallocation
also used as the digit counter and digit holder, the index fields
aren't used then, but are used when making the tree. */
struct treetype {
signed digit : 6; /* holds Nil or the digit a four bit number */
signed left : 6;
signed right : 6;
signed parent : 6;
unsigned bitcnt : 4; /* holds number of relevant bits in bits */
unsigned bits : 12; /* holds bit pattern of Huffman code */
long int count;
};
/* it has left,right, and parent indices instead of pointers, same thing,
it will be the header to go at the front of every compressed file */
int compare(struct treetype *,struct treetype *);
/* a function prototype for when I quick-sort the digits array */
main(argc,argv)
int argc;
char *argv[];
{
struct treetype tree[32],digits[16];
char filename[12];
/* enter <filename> to compress */
if(argc!=2) {
printf("you forgot file name\n");
/* an exitcode of 0 means no error, anything else is some sort of error */
exit(1);
}
strcpy(filename,argv[1]);
/* clear out the fileheader and the tree and the digit count stuff */
memset(tree,Nil,sizeof(tree));
memset(digits,Nil,sizeof(digits));
/* read in the file and count the hex digits */
cntdigits(digits,filename);
/* now we have the hex digits counted, make a tree! */
maketree(digits,tree);
/* now we got that darn tree created, show it to me! */
showtree(tree);
/* tree is shown, now for the bit patterns */
setcodes(tree);
/* proceed to encode the file */
encode(filename,tree);
}
int cntdigits(digits,name)
struct treetype digits[16];
char *name;
{
/* infile is the file variable, buffer is for fread and the ints are for
counting and holding counts */
FILE *infile;
char *buffer;
int incnt;
unsigned long int outcnt=0;
/* check for error in opening the input file */
if((infile=fopen(name,"rb"))==NULL) {
printf("couldn't open file\n");
exit(1);
}
/* set the fields in digits that need to be set to values other than -1 */
for(incnt=0;incnt<16;incnt++) {
digits[incnt].digit=incnt;
digits[incnt].count=0;
}
/* allocate space for the filebuffer used in fread */
buffer=(char *)malloc(BufferSize);
/* keep reading until physical end of file reached, some binary files use
the EOF marker used in text files, this program deals with only binary
files, carriage returns are taken as a carriage return/line feed pair
unlike in text file processing */
while(!feof(infile)) {
/* fread returns the number of items actually read, in this case chars */
outcnt+=(incnt=fread(buffer,sizeof(char),BufferSize,infile));
/* now after reading a whole bunch in, process them all and count'em up */
for(;incnt>=0;incnt--) {
digits[LowNibble(buffer[incnt])].count++;
digits[HighNibble(buffer[incnt])].count++;
}
}
/* buffer isn't really needed anymore, neither is the infile */
free(buffer);
fclose(infile);
/* return the size of the file, just because. */
return outcnt;
}
/* take the information in digits[] and generate an appropriate tree
structure that will be written into the file at encoding time */
int maketree(digits,tree)
struct treetype digits[16],tree[32];
{
int treecnt=0;
while(treecnt<31) {
/* quick sort the array, qsort is a standard misc. function */
qsort(digits,16,sizeof(struct treetype),compare);
/* now the digits with the least number of occurrences are at
lower addresses of digits */
if(digits[0].parent==Nil) { /* does this digit have a parent node */
if(digits[1].parent==Nil) { /* does the next one have a parent node */
tree[treecnt].parent=treecnt+2; /* if not then we must add them */
tree[treecnt].digit=digits[0].digit; /* to the array as leaf nodes */
tree[treecnt].count=digits[0].count;
tree[treecnt].left=Nil;
tree[treecnt].right=Nil;
treecnt++;
tree[treecnt].parent=treecnt+1; /* this is the second leaf */
tree[treecnt].digit=digits[1].digit;
tree[treecnt].count=digits[1].count;
tree[treecnt].left=Nil;
tree[treecnt].right=Nil;
treecnt++;
tree[treecnt].count=digits[1].count+digits[0].count;
tree[treecnt].left=treecnt-2; /* and then make their parent node */
tree[treecnt].right=treecnt-1;
digits[1].parent=treecnt; /* set the digit in the second position
so that it will be recognized as not
being a leaf node, but a branch */
treecnt++;
}
else {
tree[treecnt].parent=treecnt+1;
tree[treecnt].digit=digits[0].digit; /* the first was a leaf, */
tree[treecnt].count=digits[0].count; /* but the second one was */
tree[treecnt].left=Nil; /* a branch node, so we */
tree[treecnt].right=Nil; /* don't need another */
treecnt++; /* leaf node added, just */
tree[treecnt].left=treecnt-1; /* a new branch node which */
tree[treecnt].right=digits[1].parent; /* 'points' to the leaf node */
tree[digits[1].parent].parent=treecnt; /* and the branch node. */
digits[1].parent=treecnt;
treecnt++;
}
}
else {
if(digits[1].parent==Nil) { /* now we have the first one */
tree[treecnt].parent=treecnt+1; /* being a branch and the */
tree[treecnt].digit=digits[1].digit; /* second one as a leaf node */
tree[treecnt].count=digits[1].count; /* see the above explanation */
tree[treecnt].left=Nil; /* only switched around */
tree[treecnt].right=Nil;
treecnt++;
tree[treecnt].left=treecnt-1;
tree[treecnt].right=digits[0].parent;
tree[digits[0].parent].parent=treecnt;
digits[1].parent=treecnt;
treecnt++;
}
else {
tree[treecnt].left=digits[0].parent; /* now both of the nodes */
tree[digits[0].parent].parent=treecnt; /* are branches, just add */
tree[treecnt].right=digits[1].parent; /* a branch node that */
tree[digits[1].parent].parent=treecnt; /* 'points' to both the */
digits[1].parent=treecnt; /* left and right branches */
treecnt++;
}
}
digits[1].count+=digits[0].count; /* this has to be done by each */
digits[0].count=Nil; /* section of code above */
/* add the total of occurences of each digit and then set the count
on the first one to Nil so that it goes to the back of the list,
see the compare() function below for details */
}
}
/* this function is only used in the debugging part of the program, it
will be deleted later on, when I know the tree works */
int showtree(tree)
struct treetype tree[32];
{
int cnt;
/* print out the tree so far */
for(cnt=0;cnt<32;cnt++) {
if(!(cnt%16))
printf("index digit left right parent\n");
printf("%3d ",cnt);
/* print out the information, with some formatting, Branch and such. */
if(tree[cnt].digit==Nil)
printf("Branch %4d %5d",tree[cnt].left,tree[cnt].right);
else
printf("%6X NIL NIL",tree[cnt].digit);
if(tree[cnt].parent==Nil)
printf(" NIL\n");
else
printf(" %6d\n",tree[cnt].parent);
}
}
/* takes the generated tree and creates the bit patterns for
each hex digit in the file */
int setcodes(tree)
struct treetype tree[32];
{
int cnt,cnt2,cnt3;
for(cnt=0;cnt<16;cnt++) {
/* make a header for the compressed file, it will have sixteen entries */
for(cnt2=0;cnt2<32;cnt2++)
/* find the coressponding digit in the tree made in maketree and then
set up its huffman code from the tree. left = 1 */
if(tree[cnt2].digit==cnt)
break;
/* if you've got it no sense in continuing the search. */
tree[cnt2].bitcnt=0;
tree[cnt2].bits=0;
cnt3=cnt2;
/* clear out the header */
while(tree[cnt3].parent!=Nil) {
/* shift the old stuff so we can add new bits to it. */
tree[cnt2].bits<<=1;
tree[cnt2].bits|=(tree[tree[cnt3].parent].left==cnt3);
/* check for leftness and then or this truth with bits */
tree[cnt2].bitcnt++;
/* one more bit in bits */
cnt3=tree[cnt3].parent;
/* traverse the tree upwards, adding bits at each step until you reach
the top. then you can relax, just until the next digit. */
}
/* now, print out the little buggers. */
printf(" %2X ",cnt);
printb(tree[cnt2].bits,tree[cnt2].bitcnt);
}
}
/* function takes two ints and prints out the first bitcnt bits of bits */
int printb(bits,bitcnt)
int bits,bitcnt;
{
for(;bitcnt>0;bitcnt--) {
/* print out the 1s and 0s in the bit pattern */
printf("%c",(bits&1)?'1':'0');
/* shift it right so as to get at the next bit */
bits>>=1;
}
printf("\n");
}
/* the workhorse part of the code, does the encoding of the file
after a tree has been constructed and appropriate Huffman coding
scheme has been created from the tree. */
int encode(name,tree)
char *name;
struct treetype tree[32];
{
FILE *infile,*outfile;
char *buffer;
int cnt,cnt2; /* counting variables for the encoding process */
int indices[16]; /* will contain a table of 'addresses' into the tree */
if((infile=fopen(name,"rb"))==NULL) { /* try to open input and output */
printf("couldn't open the input file\n"); /* files for encoding */
exit(1);
}
*strchr(name,'.')=0; /* remove the extension from the input filename */
strcat(name,".HUF"); /* and append the .HUF extension instead */
if((outfile=fopen(name,"wb"))==NULL) {
printf("couldn't open the output file\n");
exit(1);
}
fputc(26,outfile); /* put an END-OF-FILE character in the file first */
/* this is so that a person can't use the TYPE command to view the file */
fwrite(tree,sizeof(struct treetype),31,outfile); /* then the header part */
buffer=(char *)malloc(BufferSize); /* allocate space fot a buffer */
for(cnt=0;cnt<32;cnt++)
if(tree[cnt].digit!=Nil)
indices[tree[cnt].digit]=cnt;
/* set up the table of indices into the tree structure so it can be a
accessed as though it were an array of bitcount, bits type structure */
while(!feof(infile)) {
cnt=fread(buffer,sizeof(char),BufferSize,infile);
for(cnt2=0;cnt2<cnt;cnt2++) {
/* write out the encoded bits of the low order nibble and then */
writebits(outfile,tree[indices[LowNibble(buffer[cnt2])]].bits,
tree[indices[LowNibble(buffer[cnt2])]].bitcnt);
/* write out the encoded bits of high order nibble */
writebits(outfile,tree[indices[HighNibble(buffer[cnt2])]].bits,
tree[indices[HighNibble(buffer[cnt2])]].bitcnt);
}
}
/* this clears out the static variables in writebits() to the file */
writebits(outfile,0,-1);
/* close down all files and free the memory */
free(buffer);
fclose(infile);
fclose(outfile);
}
/* this function writes individual bits to a file, a static variable
is used to store the current bit pattern that is added to until
the number of bits to be written is 8 */
int writebits(outfile,bits,bitcnt)
FILE *outfile;
int bits,bitcnt;
{
static char outchar=0,outcnt=0;
/* these variables are declared once on the first call and not disposed
until the program terminates */
if(bitcnt!=(-1)) /* if it is then flush the current bits to the file */
for(;bitcnt>0;bitcnt--) { /* add bits to outchar until we have */
outchar<<=1; /* enough to write to the file */
outchar|=bits&1;
bits>>=1;
outcnt++;
if(outcnt>=7) { /* if so we write them to the file and */
fputc(outchar,outfile); /* start all over again */
outcnt=outchar=0;
}
}
else {
fputc(outchar,outfile); /* flush everything to the file */
outcnt=outchar=0;
}
}
/* the function that qsort calls to compare to array elements when sorting */
int compare(digit1,digit2)
struct treetype *digit1,*digit2;
{
/* if count is -1 I want it at the end of the array, it won't be needed
again after it has been used once. */
if(digit1->count==Nil&&digit2->count!=Nil)
/* digit1 is greater than digit2, it's not, but I want it to be */
return 1;
if(digit1->count!=Nil&&digit2->count==Nil)
/* digit2 is greater than digit1, again it's not, but I want it to be */
return -1;
/* if both digit1 and digit2 don't have -1 counts than just compare them
in the easiest manner, if the return is <0 than digit2 > digit1, etc. */
return digit1->count-digit2->count;
}
/*---------------------------*\
| Computer Architecture |
| Program for Extra Credit |
| Hamming Code |
| Andy Collins |
| Mr. Hyatt First Period |
\*---------------------------*/
#include <stdio.h>
#include <stdlib.h>
/*
#define ENCODE This program will now encode files.
*/
#define DecodeBufSize 1024
/* this is the input buffer size, must be some 'nice' block size */
#define EncodeBufSize (DecodeBufSize*3)/2
/* the output buffer really contains one and a half more bytes */
#define UP (float)3/(float)2
#define DOWN (float)2/(float)3
struct trinibble {
char l,m,h;
};
main(argc,argv)
int argc;
char *argv[];
{
FILE *infile,*outfile;
register int cnt,numread,numwrite;
char *inbuf,*outbuf;
if(argc!=3)
printf("you forgot a file name, two required\n"),exit(1);
if((infile=fopen(argv[1],"rb"))==NULL)
printf("couldn't open input file\n"),exit(1);
if((outfile=fopen(argv[2],"wb"))==NULL)
printf("couldn't open output file\n"),exit(1);
#ifdef ENCODE
inbuf=(char *)malloc(DecodeBufSize);
outbuf=(char *)malloc(EncodeBufSize);
#else
inbuf=(char *)malloc(EncodeBufSize);
outbuf=(char *)malloc(DecodeBufSize);
#endif
while(!feof(infile)) {
#ifdef ENCODE
numread=fread(inbuf,sizeof(char),DecodeBufSize,infile);
for(cnt=0;cnt<numread;cnt++)
encodehamming(inbuf[cnt],outbuf);
numwrite=fwrite(outbuf,sizeof(char),round((float)numread*UP),outfile);
if(numwrite!=round((float)numread*UP))
break;
#else
numread=fread(inbuf,sizeof(char),EncodeBufSize,infile);
for(cnt=0;cnt<=round(numread*DOWN);cnt++)
decodehamming(inbuf,&outbuf[cnt]);
numwrite=fwrite(outbuf,sizeof(char),round((float)numread*DOWN),outfile);
if(numwrite!=round((float)numread*DOWN))
break;
#endif
}
free(outbuf);
free(inbuf);
fclose(outfile);
fclose(infile);
}
#ifdef ENCODE
int encodehamming(in,out)
char in,out[EncodeBufSize];
{
static int outcnt=0,first=1;
struct trinibble nibbles;
int parity[4];
parity[0]=((in&1||0)+(in&2||0)+(in&8||0)+(in&16||0)+(in&64||0))&1;
parity[1]=((in&1||0)+(in&4||0)+(in&8||0)+(in&32||0)+(in&64||0))&1;
parity[2]=((in&2||0)+(in&4||0)+(in&8||0)+(in&128||0))&1;
parity[3]=((in&16||0)+(in&32||0)+(in&64||0)+(in&128||0))&1;
nibbles.l=(in&1)<<2;
nibbles.l|=parity[0];
nibbles.l|=parity[1]<<1;
nibbles.l|=parity[2]<<3;
nibbles.m=(in&14)>>1;
nibbles.m|=parity[3]<<3;
nibbles.h=in>>4;
if(first) {
nibbles.l<<=4;
nibbles.l&=0xf0;
out[outcnt]=nibbles.l;
out[outcnt]|=nibbles.m;
++outcnt;
nibbles.h<<=4;
out[outcnt]=nibbles.h;
first=0;
}
else {
out[outcnt]|=nibbles.l;
++outcnt;
nibbles.m<<=4;
out[outcnt]=nibbles.m;
out[outcnt]|=nibbles.h;
++outcnt;
first=1;
}
outcnt%=EncodeBufSize;
}
#else
int decodehamming(in,out)
char in[DecodeBufSize],*out;
{
static int incnt=0,first=1;
struct trinibble nibbles;
if(first) {
nibbles.l=in[incnt]>>4;
nibbles.m=in[incnt]&0x0f;
in[incnt]=0;
++incnt;
nibbles.h=in[incnt]>>4;
first=0;
}
else {
nibbles.l=in[incnt]&0x0f;
in[incnt]=0;
++incnt;
nibbles.m=in[incnt]>>4;
nibbles.h=in[incnt]&0x0f;
in[incnt]=0;
++incnt;
first=1;
}
nibbles.l>>=2;
nibbles.l&=0x01;
nibbles.m<<=1;
nibbles.m&=0x0e;
nibbles.h<<=4;
nibbles.h&=0xf0;
(*out)=nibbles.l;
(*out)|=nibbles.m;
(*out)|=nibbles.h;
incnt%=DecodeBufSize;
}
#endif
int round(real)
float real;
{
if((real-(int)real)>0)
return (int)real+1;
else
return (int)real;
}
@andrewrcollins
Copy link
Author

Set of C programs found on old 5.25 floppy disks from when I was a sizable nerd at Thomas Jefferson High School for Science and Technology in the computer systems lab between 1988 and 1992.

http://en.wikipedia.org/wiki/Huffman_coding
http://en.wikipedia.org/wiki/Hamming_code

It is work for my Computer Architecture class with Mr. Hyatt.

http://www.tjhsst.edu/~dhyatt/

Anyone can do whatever they'd like to with them--if anything.

I remain a massively huge nerd.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment