Created
May 15, 2009 08:28
-
-
Save tsupo/112120 to your computer and use it in GitHub Desktop.
decode for MIME strings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* decode for MIME strings (ISO-2022JP only) | |
*/ | |
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
static struct codeTable { | |
char code; | |
char key; | |
} codetbl[] = { | |
{ 'A', 0 }, { 'B', 1 }, { 'C', 2 }, { 'D', 3 }, | |
{ 'E', 4 }, { 'F', 5 }, { 'G', 6 }, { 'H', 7 }, | |
{ 'I', 8 }, { 'J', 9 }, { 'K', 10 }, { 'L', 11 }, | |
{ 'M', 12 }, { 'N', 13 }, { 'O', 14 }, { 'P', 15 }, | |
{ 'Q', 16 }, { 'R', 17 }, { 'S', 18 }, { 'T', 19 }, | |
{ 'U', 20 }, { 'V', 21 }, { 'W', 22 }, { 'X', 23 }, | |
{ 'Y', 24 }, { 'Z', 25 }, { 'a', 26 }, { 'b', 27 }, | |
{ 'c', 28 }, { 'd', 29 }, { 'e', 30 }, { 'f', 31 }, | |
{ 'g', 32 }, { 'h', 33 }, { 'i', 34 }, { 'j', 35 }, | |
{ 'k', 36 }, { 'l', 37 }, { 'm', 38 }, { 'n', 39 }, | |
{ 'o', 40 }, { 'p', 41 }, { 'q', 42 }, { 'r', 43 }, | |
{ 's', 44 }, { 't', 45 }, { 'u', 46 }, { 'v', 47 }, | |
{ 'w', 48 }, { 'x', 49 }, { 'y', 50 }, { 'z', 51 }, | |
{ '0', 52 }, { '1', 53 }, { '2', 54 }, { '3', 55 }, | |
{ '4', 56 }, { '5', 57 }, { '6', 58 }, { '7', 59 }, | |
{ '8', 60 }, { '9', 61 }, { '+', 62 }, { '/', 63 } | |
}; | |
static int | |
code( c ) | |
int c; | |
{ | |
int ret = -1, i; | |
for ( i = 0; i < 64; i++ ) { | |
if ( c == codetbl[i].code ) { | |
ret = codetbl[i].key; | |
break; | |
} | |
} | |
return ( ret ); | |
} | |
char * | |
decodeMIME( p ) | |
char *p; | |
{ | |
static char buf[BUFSIZ]; | |
int i = 0, flag = 0; | |
int c1, c2, c3, c4; | |
while ( *p ) { | |
if ( !flag ) { | |
if ( !strncmp( p, "=?ISO-2022-JP?B?", 16 ) || | |
!strncmp( p, "=?iso-2022-jp?B?", 16 ) ) { | |
char *q, *strchr(); | |
if ( ( q = strchr( p + 16, '?' ) ) != NULL ) { | |
if ( *(q + 1) == '=' ) { | |
flag = 1; | |
p += 16; | |
continue; | |
} | |
} | |
} | |
buf[i++] = *p++; | |
continue; | |
} | |
if ( !strncmp( p, "?=", 2 ) ) { | |
while ( buf[i-1] == '\0' ) | |
i--; | |
flag = 0; | |
p += 2; | |
continue; | |
} | |
c1 = code( *p++ ); | |
c2 = code( *p++ ); | |
c3 = code( *p++ ); | |
c4 = code( *p++ ); | |
if ( c1 >= 0 ) { | |
if ( c2 >= 0 ) { | |
buf[i++] = (c1 << 2) | (c2 >> 4); | |
if ( c3 >= 0 ) { | |
buf[i++] = (c2 << 4) | (c3 >> 2); | |
buf[i++] = (c4 >= 0) ? (c3 << 6) | c4 : (c3 << 6); | |
} | |
else | |
buf[i++] = c2 << 4; | |
} | |
else | |
buf[i++] = c1 << 2; | |
} | |
} | |
buf[i] = '\0'; | |
return ( buf ); | |
} | |
#ifdef TEST | |
void | |
main( argc, argv ) | |
int argc; | |
char *argv[]; | |
{ | |
char *p, buf[BUFSIZ]; | |
FILE *fp; | |
int i; | |
if ( argc <= 1 ) { | |
while ( ( p = fgets( buf, BUFSIZ - 1, stdin ) ) != NULL ) | |
fputs( decodeMIME( p ), stdout ); | |
} | |
else { | |
for ( i = 1; i < argc; i++ ) { | |
if ( ( fp = fopen( argv[i], "r" ) ) != NULL ) { | |
while ( ( p = fgets( buf, BUFSIZ - 1, fp ) ) != NULL ) | |
fputs( decodeMIME( p ), stdout ); | |
fclose( fp ); | |
} | |
} | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment