Created
May 15, 2009 08:28
-
-
Save tsupo/112121 to your computer and use it in GitHub Desktop.
encode JIS 7bit kanji strings to 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
/* | |
* encode JIS 7bit kanji strings to MIME strings (ISO-2022JP) | |
*/ | |
#include <stdio.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].key ) { | |
ret = codetbl[i].code; | |
break; | |
} | |
} | |
return ( ret ); | |
} | |
static void | |
pushCode( c, p ) | |
int c; | |
char *p; | |
{ | |
static int cnt = 0, i = 0; | |
static int c1, c2; | |
static char buf[BUFSIZ]; | |
switch ( cnt++ % 3 ) { | |
case 0: | |
c1 = c; | |
break; | |
case 1: | |
c2 = c; | |
if ( p ) { | |
buf[i++] = code( (c1 >> 2) & 0x3f ); | |
buf[i++] = code( (c1 << 4) & 0x3f ); | |
buf[i++] = '='; | |
buf[i++] = '='; | |
} | |
break; | |
case 2: | |
buf[i++] = code( (c1 >> 2) & 0x3f ); | |
buf[i++] = code( ((c1 << 4) | (c2 >> 4)) & 0x3f ); | |
buf[i++] = code( ((c2 << 2) | (c >> 6)) & 0x3f ); | |
buf[i++] = !p ? code( c & 0x3f ) : '='; | |
break; | |
} | |
if ( p ) { | |
buf[i] = NULL; | |
strcpy( p, buf ); | |
cnt = i = 0; | |
} | |
} | |
char * | |
encodeMIME( p ) | |
char *p; | |
{ | |
static char buf[BUFSIZ]; | |
int flag = 0, i = 0; | |
while ( *p ) { | |
if ( !flag ) { | |
if ( *p == '\033' ) { | |
if ( *(p+1) == '$' ) { | |
if ( ( *(p+2) == 'B' ) || ( *(p+2) == '@' ) ) { | |
strcpy( &buf[i], "=?ISO-2022-JP?B?" ); | |
i += 16; | |
flag = 1; | |
pushCode( *p++, NULL ); | |
pushCode( *p++, NULL ); | |
pushCode( *p++, NULL ); | |
continue; | |
} | |
} | |
} | |
buf[i++] = *p++; | |
continue; | |
} | |
if ( *p == '\033' ) { | |
if ( *(p+1) == '(' ) { | |
if ( ( *(p+2) == 'J' ) || | |
( *(p+2) == 'B' ) || | |
( *(p+2) == 'H' ) ) { | |
if ( ( *(p+3) != ' ' ) || ( *(p+4) != '\033' ) ) { | |
pushCode( *p++, NULL ); | |
pushCode( *p++, NULL ); | |
pushCode( *p++, NULL ); | |
pushCode( 0, &buf[i] ); | |
i = strlen( buf ); | |
buf[i++] = '?'; | |
buf[i++] = '='; | |
flag = 0; | |
continue; | |
} | |
} | |
} | |
} | |
pushCode( *p++, NULL ); | |
} | |
buf[i] = NULL; | |
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( encodeMIME( 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( encodeMIME( p ), stdout ); | |
fclose( fp ); | |
} | |
} | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment