2008-08-07 10:53:30 -04:00
|
|
|
#include "transcode_data.h"
|
|
|
|
|
|
|
|
<%
|
|
|
|
map = {}
|
|
|
|
map["1b2842"] = :func_so # designate US-ASCII to G0. "ESC ( B"
|
|
|
|
map["1b284a"] = :func_so # designate JIS X 0201 latin to G0. "ESC ( J"
|
|
|
|
map["1b2440"] = :func_so # designate JIS X 0208 1978 to G0. "ESC $ @"
|
|
|
|
map["1b2442"] = :func_so # designate JIS X 0208 1983 to G0. "ESC $ B"
|
|
|
|
map["{00-0d,10-1a,1c-7f}"] = :func_si
|
|
|
|
|
|
|
|
map_jisx0208_rest = {}
|
|
|
|
map_jisx0208_rest["{21-7e}"] = :func_so
|
2008-08-31 13:35:00 -04:00
|
|
|
|
|
|
|
transcode_generate_node(ActionMap.parse(map), "iso2022jp_to_eucjp")
|
|
|
|
transcode_generate_node(ActionMap.parse(map_jisx0208_rest), "iso2022jp_to_eucjp_jisx0208_rest")
|
|
|
|
|
|
|
|
map_eucjp = {
|
|
|
|
"{0e,0f,1b}" => :undef,
|
|
|
|
"{00-0d,10-1a,1c-7f}" => :func_so,
|
|
|
|
"{a1-fe}{a1-fe}" => :func_so,
|
|
|
|
"8e{a1-fe}" => :undef,
|
|
|
|
"8f{a1-fe}{a1-fe}" => :undef,
|
|
|
|
}
|
|
|
|
|
|
|
|
transcode_generate_node(ActionMap.parse(map_eucjp), "eucjp_to_iso2022jp")
|
2008-08-07 10:53:30 -04:00
|
|
|
%>
|
|
|
|
|
2008-08-31 13:35:00 -04:00
|
|
|
<%= transcode_generated_code %>
|
2008-08-07 10:53:30 -04:00
|
|
|
|
2008-09-03 10:12:06 -04:00
|
|
|
#define G0_ASCII 0
|
|
|
|
#define G0_JISX0208 1
|
|
|
|
|
|
|
|
static int
|
|
|
|
iso2022jp_init(void *statep)
|
|
|
|
{
|
|
|
|
unsigned char *sp = statep;
|
|
|
|
*sp = G0_ASCII;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-08-07 10:53:30 -04:00
|
|
|
static VALUE
|
2008-09-03 10:12:06 -04:00
|
|
|
fun_si_iso2022jp_to_eucjp(void *statep, const unsigned char *s, size_t l)
|
2008-08-07 10:53:30 -04:00
|
|
|
{
|
2008-09-03 10:12:06 -04:00
|
|
|
unsigned char *sp = statep;
|
|
|
|
if (*sp == G0_ASCII)
|
2008-08-07 10:53:30 -04:00
|
|
|
return (VALUE)NOMAP;
|
|
|
|
else if (0x21 <= s[0] && s[0] <= 0x7e)
|
2008-09-01 12:22:49 -04:00
|
|
|
return (VALUE)iso2022jp_to_eucjp_jisx0208_rest;
|
2008-08-07 10:53:30 -04:00
|
|
|
else
|
|
|
|
return (VALUE)INVALID;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2008-09-03 10:12:06 -04:00
|
|
|
fun_so_iso2022jp_to_eucjp(void *statep, const unsigned char *s, size_t l, unsigned char* o)
|
2008-08-07 10:53:30 -04:00
|
|
|
{
|
2008-09-03 10:12:06 -04:00
|
|
|
unsigned char *sp = statep;
|
2008-08-07 10:53:30 -04:00
|
|
|
if (s[0] == 0x1b) {
|
|
|
|
if (s[1] == '(') {
|
|
|
|
switch (s[l-1]) {
|
|
|
|
case 'B':
|
|
|
|
case 'J':
|
2008-09-03 10:12:06 -04:00
|
|
|
*sp = G0_ASCII;
|
2008-08-07 10:53:30 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
switch (s[l-1]) {
|
|
|
|
case '@':
|
|
|
|
case 'B':
|
2008-09-03 10:12:06 -04:00
|
|
|
*sp = G0_JISX0208;
|
2008-08-07 10:53:30 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
o[0] = s[0] | 0x80;
|
|
|
|
o[1] = s[1] | 0x80;
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const rb_transcoder
|
|
|
|
rb_ISO_2022_JP_to_EUC_JP = {
|
2008-09-01 12:22:49 -04:00
|
|
|
"ISO-2022-JP", "EUC-JP", iso2022jp_to_eucjp,
|
2008-09-01 14:18:50 -04:00
|
|
|
TRANSCODE_TABLE_INFO,
|
2008-08-09 02:02:01 -04:00
|
|
|
1, /* input_unit_length */
|
|
|
|
3, /* max_input */
|
|
|
|
3, /* max_output */
|
2008-08-15 19:13:01 -04:00
|
|
|
stateful_decoder, /* stateful_type */
|
2008-09-03 10:12:06 -04:00
|
|
|
1, iso2022jp_init, iso2022jp_init, /* state_size, state_init, state_fini */
|
2008-08-07 10:53:30 -04:00
|
|
|
NULL, fun_si_iso2022jp_to_eucjp, NULL, fun_so_iso2022jp_to_eucjp
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
2008-09-03 10:12:06 -04:00
|
|
|
fun_so_eucjp_to_iso2022jp(void *statep, const unsigned char *s, size_t l, unsigned char *o)
|
2008-08-07 10:53:30 -04:00
|
|
|
{
|
2008-09-03 10:12:06 -04:00
|
|
|
unsigned char *sp = statep;
|
2008-08-07 10:53:30 -04:00
|
|
|
unsigned char *output0 = o;
|
|
|
|
|
2008-09-03 10:12:06 -04:00
|
|
|
if (*sp != (l == 1 ? G0_ASCII : G0_JISX0208)) {
|
2008-08-07 10:53:30 -04:00
|
|
|
if (l == 1) {
|
|
|
|
*o++ = 0x1b;
|
|
|
|
*o++ = '(';
|
|
|
|
*o++ = 'B';
|
2008-09-03 10:12:06 -04:00
|
|
|
*sp = G0_ASCII;
|
2008-08-07 10:53:30 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
*o++ = 0x1b;
|
|
|
|
*o++ = '$';
|
|
|
|
*o++ = 'B';
|
2008-09-03 10:12:06 -04:00
|
|
|
*sp = G0_JISX0208; /* JIS X 0208 1983 */
|
2008-08-07 10:53:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (l == 1) {
|
|
|
|
*o++ = s[0] & 0x7f;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*o++ = s[0] & 0x7f;
|
|
|
|
*o++ = s[1] & 0x7f;
|
|
|
|
}
|
|
|
|
|
|
|
|
return o - output0;
|
|
|
|
}
|
|
|
|
|
2008-08-14 11:56:39 -04:00
|
|
|
static int
|
2008-09-03 10:12:06 -04:00
|
|
|
iso2022jp_reset_sequence_size(void *statep)
|
2008-08-14 11:56:39 -04:00
|
|
|
{
|
2008-09-03 10:12:06 -04:00
|
|
|
unsigned char *sp = statep;
|
|
|
|
if (*sp == G0_JISX0208)
|
2008-08-14 11:56:39 -04:00
|
|
|
return 3;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-08-07 10:53:30 -04:00
|
|
|
static int
|
2008-09-03 10:12:06 -04:00
|
|
|
finish_eucjp_to_iso2022jp(void *statep, unsigned char *o)
|
2008-08-07 10:53:30 -04:00
|
|
|
{
|
2008-09-03 10:12:06 -04:00
|
|
|
unsigned char *sp = statep;
|
2008-08-07 10:53:30 -04:00
|
|
|
unsigned char *output0 = o;
|
|
|
|
|
2008-09-03 10:12:06 -04:00
|
|
|
if (*sp == G0_ASCII)
|
2008-08-07 10:53:30 -04:00
|
|
|
return 0;
|
|
|
|
|
2008-09-03 10:12:06 -04:00
|
|
|
*o++ = 0x1b;
|
|
|
|
*o++ = '(';
|
|
|
|
*o++ = 'B';
|
|
|
|
*sp = G0_ASCII;
|
2008-08-07 10:53:30 -04:00
|
|
|
|
|
|
|
return o - output0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const rb_transcoder
|
|
|
|
rb_EUC_JP_to_ISO_2022_JP = {
|
2008-09-01 12:22:49 -04:00
|
|
|
"EUC-JP", "ISO-2022-JP", eucjp_to_iso2022jp,
|
2008-09-01 14:18:50 -04:00
|
|
|
TRANSCODE_TABLE_INFO,
|
2008-08-09 02:02:01 -04:00
|
|
|
1, /* input_unit_length */
|
|
|
|
3, /* max_input */
|
|
|
|
5, /* max_output */
|
2008-08-15 19:13:01 -04:00
|
|
|
stateful_encoder, /* stateful_type */
|
2008-09-03 10:12:06 -04:00
|
|
|
1, iso2022jp_init, iso2022jp_init, /* state_size, state_init, state_fini */
|
2008-08-11 18:44:23 -04:00
|
|
|
NULL, NULL, NULL, fun_so_eucjp_to_iso2022jp,
|
2008-08-14 11:56:39 -04:00
|
|
|
finish_eucjp_to_iso2022jp,
|
|
|
|
iso2022jp_reset_sequence_size, finish_eucjp_to_iso2022jp
|
2008-08-07 10:53:30 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
Init_iso2022(void)
|
|
|
|
{
|
|
|
|
rb_register_transcoder(&rb_ISO_2022_JP_to_EUC_JP);
|
|
|
|
rb_register_transcoder(&rb_EUC_JP_to_ISO_2022_JP);
|
|
|
|
}
|
|
|
|
|