/*----------------------------------------------------*/ /* qpdecode.c -- QuotedPrintable-Decoding Filter */ /*----------------------------------------------------*/ /* Usage: */ /* qpdecode [infile] > */ /* infile: filename of the file to be decoded */ /* (if omitted, input from stdin) */ /* result(stdout) should be redirected to */ /*----------------------------------------------------*/ #include #include #include int main(int argc, char *argv[]) { int c, b; FILE *fp; # ifdef O_BINARY setmode(1,O_BINARY); # endif if (argc == 1) { fp = stdin; # ifdef O_BINARY setmode(0,O_BINARY); # endif } else if ((fp=fopen(argv[1],"rb")) == NULL) { fprintf(stderr, "file %s not found\n", argv[1]); exit(1); } while ((c=getc(fp)) != EOF) { if (c == '=') { if ((b=getc(fp)) == EOF) { printf("=\n"); break; } if (b == 0x0D) { if ((c=getc(fp)) == EOF) { printf("=[CR]\n"); break; } if (c != 0x0A) { ungetc(c, fp); printf("=[CR]"); } } else if (b == 0x0A) { } else if ((0x30 <= b && b <= 0x39) || (0x41 <= b && b <= 0x46) || (0x61 <= b && b <= 0x66)) { if ((c=getc(fp)) == EOF) { printf("=%c\n", b); break; } if ((0x30 <= c && c <= 0x39) || (0x41 <= c && c <= 0x46) || (0x61 <= c && c <= 0x66)) { if (b < 0x41) b = b-0x30; else b = ((b < 0x61)? b-0x37: b-0x57); if (c < 0x41) c = c-0x30; else c = ((c < 0x61)? c-0x37: c-0x57); c = b*16 + c; putchar(c); } else { ungetc(c, fp); printf("=%c", b); } } else { ungetc(c, fp); putchar('='); } } else { putchar(c); } } fclose(fp); fclose(stdout); return(0); }