mbfilter_cp51932.c [plain text]
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "mbfilter.h"
#include "mbfilter_cp51932.h"
#include "unicode_table_cp932_ext.h"
#include "unicode_table_jis.h"
#include "cp932_table.h"
static int mbfl_filt_ident_cp51932(int c, mbfl_identify_filter *filter);
static const unsigned char mblen_table_eucjp[] = {
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1
};
static const char *mbfl_encoding_cp51932_aliases[] = {"cp51932", NULL};
const struct mbfl_identify_vtbl vtbl_identify_cp51932 = {
mbfl_no_encoding_cp51932,
mbfl_filt_ident_common_ctor,
mbfl_filt_ident_common_dtor,
mbfl_filt_ident_cp51932
};
const mbfl_encoding mbfl_encoding_cp51932 = {
mbfl_no_encoding_cp51932,
"CP51932",
"CP51932",
(const char *(*)[])&mbfl_encoding_cp51932_aliases,
mblen_table_eucjp,
MBFL_ENCTYPE_MBCS
};
const struct mbfl_convert_vtbl vtbl_cp51932_wchar = {
mbfl_no_encoding_cp51932,
mbfl_no_encoding_wchar,
mbfl_filt_conv_common_ctor,
mbfl_filt_conv_common_dtor,
mbfl_filt_conv_cp51932_wchar,
mbfl_filt_conv_common_flush
};
const struct mbfl_convert_vtbl vtbl_wchar_cp51932 = {
mbfl_no_encoding_wchar,
mbfl_no_encoding_cp51932,
mbfl_filt_conv_common_ctor,
mbfl_filt_conv_common_dtor,
mbfl_filt_conv_wchar_cp51932,
mbfl_filt_conv_common_flush
};
#define CK(statement) do { if ((statement) < 0) return (-1); } while (0)
#define sjistoidx(c1, c2) \
(((c1) > 0x9f) \
? (((c1) - 0xc1) * 188 + (c2) - (((c2) > 0x7e) ? 0x41 : 0x40)) \
: (((c1) - 0x81) * 188 + (c2) - (((c2) > 0x7e) ? 0x41 : 0x40)))
#define idxtoeuc1(c) (((c) / 94) + 0xa1)
#define idxtoeuc2(c) (((c) % 94) + 0xa1)
int
mbfl_filt_conv_cp51932_wchar(int c, mbfl_convert_filter *filter)
{
int c1, s, w;
switch (filter->status) {
case 0:
if (c >= 0 && c < 0x80) {
CK((*filter->output_function)(c, filter->data));
} else if (c > 0xa0 && c < 0xff) {
filter->status = 1;
filter->cache = c;
} else if (c == 0x8e) {
filter->status = 2;
} else {
w = c & MBFL_WCSGROUP_MASK;
w |= MBFL_WCSGROUP_THROUGH;
CK((*filter->output_function)(w, filter->data));
}
break;
case 1:
filter->status = 0;
c1 = filter->cache;
if (c > 0xa0 && c < 0xff) {
w = 0;
s = (c1 - 0xa1)*94 + c - 0xa1;
if (s <= 137) {
if (s == 31) {
w = 0xff3c;
} else if (s == 32) {
w = 0xff5e;
} else if (s == 33) {
w = 0x2225;
} else if (s == 60) {
w = 0xff0d;
} else if (s == 80) {
w = 0xffe0;
} else if (s == 81) {
w = 0xffe1;
} else if (s == 137) {
w = 0xffe2;
}
}
if (w == 0) {
if (s >= cp932ext1_ucs_table_min && s < cp932ext1_ucs_table_max) {
w = cp932ext1_ucs_table[s - cp932ext1_ucs_table_min];
} else if (s >= 0 && s < jisx0208_ucs_table_size) {
w = jisx0208_ucs_table[s];
} else if (s >= cp932ext2_ucs_table_min && s < cp932ext2_ucs_table_max) {
w = cp932ext2_ucs_table[s - cp932ext2_ucs_table_min];
}
}
if (w <= 0) {
w = ((c1 & 0x7f) << 8) | (c & 0x7f);
w &= MBFL_WCSPLANE_MASK;
w |= MBFL_WCSPLANE_WINCP932;
}
CK((*filter->output_function)(w, filter->data));
} else if ((c >= 0 && c < 0x21) || c == 0x7f) {
CK((*filter->output_function)(c, filter->data));
} else {
w = (c1 << 8) | c;
w &= MBFL_WCSGROUP_MASK;
w |= MBFL_WCSGROUP_THROUGH;
CK((*filter->output_function)(w, filter->data));
}
break;
case 2:
filter->status = 0;
if (c > 0xa0 && c < 0xe0) {
w = 0xfec0 + c;
CK((*filter->output_function)(w, filter->data));
} else if ((c >= 0 && c < 0x21) || c == 0x7f) {
CK((*filter->output_function)(c, filter->data));
} else {
w = 0x8e00 | c;
w &= MBFL_WCSGROUP_MASK;
w |= MBFL_WCSGROUP_THROUGH;
CK((*filter->output_function)(w, filter->data));
}
break;
default:
filter->status = 0;
break;
}
return c;
}
int
mbfl_filt_conv_wchar_cp51932(int c, mbfl_convert_filter *filter)
{
int c1, c2, s1;
s1 = 0;
if (c >= ucs_a1_jis_table_min && c < ucs_a1_jis_table_max) {
s1 = ucs_a1_jis_table[c - ucs_a1_jis_table_min];
} else if (c >= ucs_a2_jis_table_min && c < ucs_a2_jis_table_max) {
s1 = ucs_a2_jis_table[c - ucs_a2_jis_table_min];
} else if (c >= ucs_i_jis_table_min && c < ucs_i_jis_table_max) {
s1 = ucs_i_jis_table[c - ucs_i_jis_table_min];
} else if (c >= ucs_r_jis_table_min && c < ucs_r_jis_table_max) {
s1 = ucs_r_jis_table[c - ucs_r_jis_table_min];
}
if (s1 >= 0x8080) s1 = -1;
if (s1 <= 0) {
c1 = c & ~MBFL_WCSPLANE_MASK;
if (c1 == MBFL_WCSPLANE_WINCP932) {
s1 = c & MBFL_WCSPLANE_MASK;
if (s1 >= ((85 + 0x20) << 8)) {
s1 = -1;
}
} else if (c1 == MBFL_WCSPLANE_JIS0208) {
s1 = c & MBFL_WCSPLANE_MASK;
if ((s1 >= ((85 + 0x20) << 8) &&
s1 <= ((88 + 0x20) << 8)) ||
(s1 >= ((93 + 0x20) << 8) &&
s1 <= ((94 + 0x20) << 8))) {
s1 = -1;
}
} else if (c == 0xa5) {
s1 = 0x005c;
} else if (c == 0x203e) {
s1 = 0x007e;
} else if (c == 0xff3c) {
s1 = 0x2140;
} else if (c == 0xff5e) {
s1 = 0x2141;
} else if (c == 0x2225) {
s1 = 0x2142;
} else if (c == 0xff0d) {
s1 = 0x215d;
} else if (c == 0xffe0) {
s1 = 0x2171;
} else if (c == 0xffe1) {
s1 = 0x2172;
} else if (c == 0xffe2) {
s1 = 0x224c;
} else {
s1 = -1;
c1 = 0;
c2 = cp932ext1_ucs_table_max - cp932ext1_ucs_table_min;
while (c1 < c2) {
if (c == cp932ext1_ucs_table[c1]) {
s1 = ((c1/94 + 0x2d) << 8) + (c1%94 + 0x21);
break;
}
c1++;
}
if (s1 < 0) {
c1 = 0;
c2 = cp932ext2_ucs_table_max - cp932ext2_ucs_table_min;
while (c1 < c2) {
if (c == cp932ext2_ucs_table[c1]) {
s1 = ((c1/94 + 0x79) << 8) +(c1%94 + 0x21);
break;
}
c1++;
}
}
}
if (c == 0) {
s1 = 0;
} else if (s1 <= 0) {
s1 = -1;
}
}
if (s1 >= 0) {
if (s1 < 0x80) {
CK((*filter->output_function)(s1, filter->data));
} else if (s1 < 0x100) {
CK((*filter->output_function)(0x8e, filter->data));
CK((*filter->output_function)(s1, filter->data));
} else if (s1 < 0x8080) {
CK((*filter->output_function)(((s1 >> 8) & 0xff) | 0x80, filter->data));
CK((*filter->output_function)((s1 & 0xff) | 0x80, filter->data));
} else {
if (filter->illegal_mode != MBFL_OUTPUTFILTER_ILLEGAL_MODE_NONE) {
CK(mbfl_filt_conv_illegal_output(c, filter));
}
}
} else {
if (filter->illegal_mode != MBFL_OUTPUTFILTER_ILLEGAL_MODE_NONE) {
CK(mbfl_filt_conv_illegal_output(c, filter));
}
}
return c;
}
static int mbfl_filt_ident_cp51932(int c, mbfl_identify_filter *filter)
{
switch (filter->status) {
case 0:
if (c >= 0 && c < 0x80) {
;
} else if (c > 0xa0 && c < 0xff) {
filter->status = 1;
} else if (c == 0x8e) {
filter->status = 2;
} else {
filter->flag = 1;
}
break;
case 1:
if (c < 0xa1 || c > 0xfe) {
filter->flag = 1;
}
filter->status = 0;
break;
case 2:
if (c < 0xa1 || c > 0xdf) {
filter->flag = 1;
}
filter->status = 0;
break;
default:
filter->status = 0;
break;
}
return c;
}