qp2sjis.c

/* */ /* qp2sjis.c */ /* QUOTED-PRINTABLE Decoder */ /* */ /* copyright (C) 2007 thinking-j, All Rights Reserved. */ /* */ #include #include #define false 0 #define true ~false /* HEX -> DEC */ int hex2dec(char c) { if ((c > '/') && (c < ':')) return (int)c - 0x30; else if ((c > '@') && (c < 'G')) return (int)c - 0x41 + 10; else return -1; } /* QUOTED-PRINTABLE decode */ int qp2sjis(char *sjis, char *qp) { int i, j, len; char s; int crflag; len = strlen(qp); i = 0; j= 0; crflag = false; while(i < len) { if ((crflag == true) && (qp[i] == '\n')) { i++; crflag = false; continue; } if (qp[i] == '=') { i++; s = qp[i]; if ((s == '\r') || (s == '\n')) { crflag = true; i++; } else { s = hex2dec(qp[i++]) << 4; s += hex2dec(qp[i++]); sjis[j++] = s; } } else sjis[j++] = qp[i++]; } sjis[j] = '\0'; return j; } main(int argc, char* argv[] ) { char sjis[20]; char qp[40]; if (argc > 1) strcpy(qp, argv[1]); else strcpy(qp, "=83e=83X=83g=0D=0A=\r\n=83e=83X=83g=0D=0A"); qp2sjis(sjis, qp); printf("%s", sjis); }