/* decode.c - Don Yang (uguu.org) 12/01/01 */ /*@ -shiftsigned -type @*/ #include #include #include static char Line[256]; static int Pos, BitCount; static int GetBit(FILE *infile); /******************************************************************** main */ int main(int argc, char **argv) { static char dict[11] = ",.`':\"\nx %X"; static char output[4096]; FILE *infile, *outfile; int i, j, offset, length, ref; infile = stdin; outfile = stdout; if( argc > 1 ) { if( (infile = fopen(argv[1], "rt")) == NULL ) return printf("Can not open %s\n", argv[1]); if( argc > 2 ) { if( (outfile = fopen(argv[2], "wt+")) == NULL ) { (void)fclose(infile); return printf("Can not create %s\n", argv[2]); } } } Line[Pos = BitCount = 0] = '\0'; offset = 0; while( (j = GetBit(infile)) != -1 ) { for(i = length = 0; i < 5; i++) length = (length << 1) | GetBit(infile); if( j == 0 ) { for(i = ref = 0; i < 5; i++) ref = (ref << 1) | GetBit(infile); for(i = 0; i < length; i++) { output[offset] = output[offset - ref]; offset++; } } else { for(i = 0; i < length; i++) { if( GetBit(infile) != 0 ) { if( GetBit(infile) != 0 ) { for(j = ref = 0; j < 3; j++) ref = (ref << 1) | GetBit(infile); output[offset++] = dict[ref]; } else { output[offset++] = GetBit(infile) != 0 ? 'X' : '%'; } } else { output[offset++] = ' '; } } } } output[offset] = '\0'; (void)fputs(output, outfile); fprintf(stderr, "%d bits\n", BitCount); (void)fclose(infile); (void)fclose(outfile); return 0; } /* main() */ /****************************************************************** GetBit */ static int GetBit(FILE *infile) { while( Line[Pos] != '0' && Line[Pos] != '1' ) { if( Line[Pos] == '\0' || Line[Pos] == '\n' ) { do { if( fgets(Line, 256, infile) == NULL ) return -1; } while( strchr(Line, ':') != NULL ); Pos = 0; continue; } Pos++; } BitCount++; printf("%c", Line[Pos]); return Line[Pos++] - '0'; } /* GetBit() */