/* huffdec.c - Don Yang (uguu.org) 09/13/01 */ #include #include"hufftree.h" static void HuffmanDecode(FILE *infile, FILE *outfile, int size); /******************************************************************** main */ int main(int argc, char **argv) { FILE *infile, *outfile; int size; if( argc < 3 ) return printf("%s \n", *argv); if( (infile = fopen(argv[1], "rb")) == NULL ) return printf("Can not open %s\n", argv[1]); if( (outfile = fopen(argv[2], "wb+")) == NULL ) { fclose(infile); return printf("Can not create %s\n", argv[2]); } fread(&size, sizeof(int), 1, infile); HuffmanDecode(infile, outfile, size); fclose(infile); fclose(outfile); return 0; } /* main() */ /*********************************************************** HuffmanDecode */ static void HuffmanDecode(FILE *infile, FILE *outfile, int size) { int bit, byte, i, x; bit = 8; for(i = x = byte = 0; i < size;) { if( bit > 7 ) { if( (byte = fgetc(infile)) == EOF ) break; bit = 0; } if( (byte & (1 << bit)) == 0 ) { if( Left[x] <= 0 ) { fputc(-Left[x], outfile); i++; x = 0; } else { x = Left[x]; } } else { if( Right[x] <= 0 ) { fputc(-Right[x], outfile); i++; x = 0; } else { x = Right[x]; } } bit++; } } /* HuffmanDecode() */