/* decode.c - Don Yang (uguu.org) Test encoded data (from encode.c). 11/28/01 */ /*@ -shiftsigned -type @*/ #include #include #define BITS_PER_GROUP 2 #define BASE64_OFFSET '0' static int GetBit(FILE *infile, int *byte, int *bit, int *size); static int GetCode(FILE *infile, int *byte, int *bit, int *size); /******************************************************************** main */ int main(int argc, char **argv) { FILE *infile, *outfile; int i, bit, byte, size; int cstate, csize, code; infile = stdin; outfile = stdout; if( argc > 1 ) { if( (infile = fopen(argv[1], "rb")) == 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]); } } } (void)fseek(infile, 1, SEEK_SET); while( (i = fgetc(infile)) != '\"' ); while( !isdigit(i = fgetc(infile)) ); for(size = i - '0'; isdigit(i = fgetc(infile)); size = size * 10 + i - '0'); (void)fseek(infile, 1, SEEK_SET); bit = byte = 6; for(cstate = csize = 0; csize < size;) { code = GetCode(infile, &byte, &bit, &csize); if( code == 0 ) { (void)fputc('\n', outfile); cstate = 0; } else { for(i = 0; i < code; i++) (void)fputc(cstate == 0 ? ' ' : 'X', outfile); cstate ^= 1; } } (void)fclose(infile); (void)fclose(outfile); return 0; } /* main() */ /****************************************************************** GetBit */ static int GetBit(FILE *infile, int *byte, int *bit, int *size) { int i; if( *bit > 5 ) { if( (i = fgetc(infile)) == '\\' ) i = fgetc(infile); *byte = i - BASE64_OFFSET; *bit = 0; } (*size)++; return (*byte & (1 << (*bit)++)) == 0 ? 0 : 1; } /* GetBit() */ /***************************************************************** GetByte */ static int GetCode(FILE *infile, int *byte, int *bit, int *size) { int i, j, code; for(i = code = 0;; i += BITS_PER_GROUP) { for(j = 0; j < BITS_PER_GROUP; j++) code |= GetBit(infile, byte, bit, size) << (i + j); if( GetBit(infile, byte, bit, size) == 0 ) break; } return code; } /* GetByte() */