1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
|
/* $Id: jis.c 5403 2008-02-01 05:06:12Z hio $ */
#include "Japanese.h"
#include "sjis.h"
#include <string.h>
#define S2J_DISP 0
#define J2S_DISP 0
#if S2J_DISP
#define ECHO_S2J(arg) fprintf arg
#define ON_S2J(cmd) cmd
#else
#define ECHO_S2J(arg)
#define ON_S2J(cmd)
#endif
#if J2S_DISP
#define ECHO_J2S(arg) fprintf arg
#define ON_J2S(cmd) cmd
#else
#define ECHO_J2S(arg)
#define ON_J2S(cmd)
#endif
/*
# JIS C 6226-1979 \e$@
# JIS X 0208-1983 \e$B
# JIS X 0208-1990 \e&@\e$B
# JIS X 0212-1990 \e$(D
*/
#define JIS_C6226_1979 ((const unsigned char*)"\x1b$@")
#define JIS_X0208_1983 ((const unsigned char*)"\x1b$B")
#define JIS_X0208_1990 ((const unsigned char*)"\x1b&@\x1b$B")
#define JIS_X0212_1990 ((const unsigned char*)"\x1b$(D")
#define JIS_ASC ((const unsigned char*)"\x1b(B")
#define JIS_ROMAN ((const unsigned char*)"\x1b(J")
#define JIS_KANA ((const unsigned char*)"\x1b(I")
#define JIS_C6226_1979_LEN 3
#define JIS_X0208_1983_LEN 3
#define JIS_X0208_1990_LEN 6
#define JIS_X0212_1990_LEN 4
#define JIS_ASC_LEN 3
#define JIS_ROMAN_LEN 3
#define JIS_KANA_LEN 3
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/* sjis=>jisѴ */
EXTERN_C
SV*
xs_sjis_jis(SV* sv_str)
{
unsigned char* src;
STRLEN len;
SV_Buf result;
int esc_asc;
const unsigned char* src_end;
if( sv_str==&PL_sv_undef )
{
return newSVsv(&PL_sv_undef);
}
if( SvGMAGICAL(sv_str) )
{
mg_get(sv_str);
}
if( !SvOK(sv_str) )
{
return newSVsv(&PL_sv_undef);
}
src = (unsigned char*)SvPV(sv_str, len);
ECHO_S2J((stderr,"Unicode::Japanese::(xs)sjis_jis, len:%d\n",len));
ON_S2J(bin_dump("in ",src,len));
SV_Buf_init(&result,len+8);
esc_asc = 1;
src_end = src+len;
while( src<src_end )
{
ECHO_S2J((stderr, "switch: %02x : %d\n", *src, chk_sjis[*src]));
switch(chk_sjis[*src])
{
case CHK_SJIS_THROUGH:
{ /* SJIS:THROUGH => JIS:ASCII */
const unsigned char* begin;
if( !esc_asc )
{
SV_Buf_append_mem(&result,JIS_ASC,JIS_ASC_LEN);
esc_asc = 1;
}
#if TEST && S2J_DISP
fprintf(stderr," (throuh) %c[%02x]",*src,*src);
fflush(stderr);
#endif
begin = src;
while( ++src<src_end && chk_sjis[*src]==CHK_SJIS_THROUGH )
{
#if TEST && S2J_DISP
fprintf(stderr," %c[%02x]",*src,*src);
fflush(stderr);
#endif
}
#if TEST && S2J_DISP
fprintf(stderr,"\n");
fflush(stderr);
#endif
SV_Buf_append_mem(&result,begin,src-begin);
break;
}
case CHK_SJIS_C:
{
SV_Buf_append_mem(&result,JIS_X0208_1983,JIS_X0208_1983_LEN);
esc_asc = 0;
ECHO_S2J((stderr," (sjis:c)"));
do
{
union {
UJ_UINT16 u16_val;
UJ_UINT8 u8_val[2];
} tmp;
ECHO_S2J((stderr, "%c%c[%02x.%02x]",src[0],src[1],src[0],src[1]));
if( src[1]<0x40 || 0xfc<src[1] || src[1]==0x7f )
{
ECHO_S2J((stderr, "*"));
SV_Buf_append_mem(&result,UNDEF_JIS,UNDEF_JIS_LEN);
++src;
break;
}
if( 0x9f <= src[1] )
{
tmp.u8_val[0] = src[0]*2 - (src[0]>=0xe0 ? 0xe0 : 0x60);
tmp.u8_val[1] = src[1] + 2;
}else
{
tmp.u8_val[0] = src[0]*2 - (src[0]>=0xe0 ? 0xe1 : 0x61);
tmp.u8_val[1] = src[1] + 0x60 + (src[1] < 0x7f);
}
tmp.u8_val[0] &= 0x7f;
tmp.u8_val[1] &= 0x7f;
SV_Buf_append_ch2(&result, tmp.u16_val);
src += 2;
}while( src<src_end && chk_sjis[*src]==CHK_SJIS_C );
ECHO_S2J((stderr,"\n"));
break;
}
case CHK_SJIS_KANA:
{ /* SJIS:KANA => JIS:KANA */
SV_Buf_append_mem(&result,JIS_KANA,JIS_KANA_LEN);
esc_asc = 0;
#if TEST && S2J_DISP
fprintf(stderr," (sjis:kana)");
fflush(stderr);
#endif
do
{
#if TEST && S2J_DISP
fprintf(stderr," %02x",*src);
fflush(stderr);
#endif
SV_Buf_append_ch(&result,*src&0x7f);
}while( ++src<src_end && chk_sjis[*src]==CHK_SJIS_KANA );
#if TEST && S2J_DISP
fprintf(stderr,"\n");
#endif
break;
}
default:
{
#ifdef TEST
fprintf(stderr,"xs_sjis_eucjp, unknown check-code[%02x] on char-code[%05x]\n",chk_sjis[*src],*src);
#endif
SV_Buf_append_ch(&result,*src);
++src;
}
} /*switch */
} /*while */
if( !esc_asc )
{
SV_Buf_append_mem(&result,JIS_ASC,JIS_ASC_LEN);
}
/* bin_dump("out",SV_Buf_getBegin(&result),SV_Buf_getLength(&result)); */
SV_Buf_setLength(&result);
return SV_Buf_getSv(&result);
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/* jis=>sjisѴ */
EXTERN_C
SV*
xs_jis_sjis(SV* sv_str)
{
unsigned char* src;
STRLEN len;
SV_Buf result;
const unsigned char* src_end;
if( sv_str==&PL_sv_undef )
{
return newSVsv(&PL_sv_undef);
}
if( SvGMAGICAL(sv_str) )
{
mg_get(sv_str);
}
if( !SvOK(sv_str) )
{
return newSVsv(&PL_sv_undef);
}
src = (unsigned char*)SvPV(sv_str, len);
ECHO_J2S((stderr,"Unicode::Japanese::(xs)jis_sjis, len:%d\n",len));
ON_J2S(bin_dump("in ",src,len));
SV_Buf_init(&result,len);
src_end = src+len;
if( len!=0 && *src!='\x1b' )
{
const unsigned char* begin = src;
while( ++src<src_end && *src!='\x1b')
{
}
SV_Buf_append_mem(&result,begin,src-begin);
}
while( src<src_end )
{
ECHO_J2S((stderr," len: %d\n",src_end-src));
if( src_end-src>=JIS_ASC_LEN && memcmp(src,JIS_ASC,JIS_ASC_LEN)==0 )
{ /* <<jis.asc>> */
const unsigned char* begin;
/*fprintf(stderr," <jis.asc>\n"); */
src += JIS_ASC_LEN;
begin = src;
while( src<src_end && *src!='\x1b')
{
++src;
}
if( src!=begin )
{
SV_Buf_append_mem(&result,begin,src-begin);
}
}else if( src_end-src>=JIS_ROMAN_LEN && memcmp(src,JIS_ROMAN,JIS_ROMAN_LEN)==0 )
{ /* <<jis.roman>> */
const unsigned char* begin;
/*fprintf(stderr," <jis.roman>\n"); */
src += JIS_ROMAN_LEN;
begin = src;
while( src<src_end && *src!='\x1b')
{
++src;
}
if( src!=begin )
{
SV_Buf_append_mem(&result,begin,src-begin);
}
}else if( src_end-src>=JIS_KANA_LEN && memcmp(src,JIS_KANA,JIS_KANA_LEN)==0 )
{ /* <<jis.kana>> */
/*fprintf(stderr," <jis.kana>\n"); */
src += JIS_KANA_LEN;
while( src<src_end && *src!='\x1b')
{
SV_Buf_append_ch(&result,*src|0x80);
++src;
}
}else if( (src_end-src>=JIS_X0208_1983_LEN && memcmp(src,JIS_X0208_1983,JIS_X0208_1983_LEN)==0)
|| (src_end-src>=JIS_X0208_1990_LEN && memcmp(src,JIS_X0208_1990,JIS_X0208_1990_LEN)==0)
|| (src_end-src>=JIS_C6226_1979_LEN && memcmp(src,JIS_C6226_1979,JIS_C6226_1979_LEN)==0)
)
{ /* <<jis.0208/0212>> */
ECHO_J2S((stderr," <jis.c>");fflush(stderr));
src += src[1]!='&' ? 3 : 6;
while( src<src_end )
{
union {
UJ_UINT16 u16_val;
UJ_UINT8 u8_val[2];
} tmp;
if( *src=='\x1b' ) break;
ECHO_J2S((stderr," %02x",src[0]);fflush(stderr));
if( *src>=0x21 && *src<0x7e )
{}else
{
ECHO_J2S((stderr,"+");fflush(stderr));
break;
}
if( src+1==src_end || src[1]=='\x1b' )
{
ECHO_J2S((stderr,"*");fflush(stderr));
break;
}
ECHO_J2S((stderr," %02x",src[0]);fflush(stderr));
tmp.u8_val[0] = src[0] | 0x80;
tmp.u8_val[1] = src[1] | 0x80;
if( src[0]%2 )
{
tmp.u8_val[0] = (tmp.u8_val[0]>>1) + (tmp.u8_val[0] < 0xdf ? 0x31 : 0x71);
tmp.u8_val[1] = tmp.u8_val[1] - ( 0x60 + (tmp.u8_val[1] < 0xe0) );
}else
{
tmp.u8_val[0] = (tmp.u8_val[0]>>1) + (tmp.u8_val[0] < 0xdf ? 0x30 : 0x70);
tmp.u8_val[1] = tmp.u8_val[1] - 2;
}
SV_Buf_append_ch2(&result, tmp.u16_val);
src += 2;
}
ECHO_J2S((stderr,"\n"));
}else if( src_end-src>=JIS_X0212_1990_LEN && memcmp(src,JIS_X0212_1990,JIS_X0212_1990_LEN)==0 )
{ /* <<jis.0212>> */
const unsigned char* begin;
int i;
ECHO_J2S((stderr," <jis.0212>");fflush(stderr));
src += JIS_X0212_1990_LEN;
begin = src;
while( src<src_end && *src!='\x1b')
{
++src;
}
for( i=0; i<(src-begin)/2; ++i )
{
SV_Buf_append_mem(&result,UNDEF_SJIS,UNDEF_SJIS_LEN);
}
}else if( src[0]!='\x1b') /* !='\e' */
{ /* <<no escape>> */
const unsigned char* begin;
ECHO_J2S((stderr," <no.escape>");fflush(stderr));
begin = src;
while( src<src_end && *src!='\x1b')
{
++src;
}
if( src!=begin )
{
SV_Buf_append_mem(&result,begin,src-begin);
}
}else
{ /* <<unknown escape>> */
ECHO_J2S((stderr," <escape.unknown>");fflush(stderr));
SV_Buf_append_ch(&result,*src);
++src;
}
} /*while */
ON_J2S(bin_dump("out",SV_Buf_getBegin(&result), SV_Buf_getLength(&result)));
SV_Buf_setLength(&result);
return SV_Buf_getSv(&result);
}
|