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
|
// $Id: getcode.cpp,v 1.4 2002/07/01 00:17:02 hio Exp $
#include "Japanese.h"
#include "getcode.h"
#define GC_DISP 0
// ʸ�����������
enum charcode_t
{
cc_unknown,
cc_ascii,
cc_sjis,
cc_eucjp,
cc_jis,
cc_utf8,
cc_utf32,
cc_utf32_be,
cc_utf32_le,
cc_sjis_jsky,
cc_sjis_imode,
cc_sjis_doti,
cc_last,
};
// ʸ��������̾ʸ����(SV*)
#define new_CC_UNKNOWN() newSVpvn("unknown",7)
#define new_CC_ASCII() newSVpvn("ascii", 5)
#define new_CC_SJIS() newSVpvn("sjis", 4)
#define new_CC_JIS() newSVpvn("jis", 3)
#define new_CC_EUCJP() newSVpvn("euc", 3)
#define new_CC_UTF8() newSVpvn("utf8", 4)
#define new_CC_UTF16() newSVpvn("utf16", 5)
#define new_CC_UTF32() newSVpvn("utf32", 5)
#define new_CC_UTF32_BE() newSVpvn("utf32-be",8)
#define new_CC_UTF32_LE() newSVpvn("utf32-le",8)
#define new_CC_SJIS_JSKY() newSVpvn("sjis-jsky",9)
#define new_CC_SJIS_IMODE() newSVpvn("sjis-imode",10)
#define new_CC_SJIS_DOTI() newSVpvn("sjis-doti",9)
//
#define RE_BOM2_BE "\xfe\xff"
#define RE_BOM2_LE "\xff\xfe"
#define RE_BOM4_BE "\x00\x00\xfe\xff"
#define RE_BOM4_LE "\xff\xfe\x00\x00"
#if defined(TEST) || 1
// ʸ�������������ʸ��������̾��
static const char* charcodeToStr(charcode_t code)
{
switch(code)
{
case cc_unknown: return "unknown";
case cc_ascii: return "ascii";
case cc_sjis: return "sjis";
case cc_eucjp: return "eucjp";
case cc_jis: return "jis";
case cc_utf8: return "utf8";
case cc_utf32: return "utf32";
case cc_utf32_be: return "utf32-be";
case cc_utf32_le: return "utf32-le";
case cc_sjis_jsky: return "sjis-jsky";
case cc_sjis_imode: return "sjis-imode";
case cc_sjis_doti: return "sjis-doti";
}
return NULL;
}
#endif
#ifdef TEST
DECL_MAP_MODE(ascii,1) = { "ascii", };
DECL_MAP_MODE(eucjp,5) =
{ "eucjp", "0212:3.1","0212:3.2","c:2.1","kana:2.1",};
DECL_MAP_MODE(sjis,2) = { "sjis","c:2.1", };
DECL_MAP_MODE(jis,10) =
{
"jis","jis#1","jis#2","jis#3","jis#4","jis#5","jis#6",
"jis#7","jis#loop1","jis#loop2",
};
DECL_MAP_MODE(utf8,6) =
{
"utf8",
"u8:6.1","u8:6.2","u8:6.3","u8:6.4","u8:6.5",
};
DECL_MAP_MODE(utf32_be,4) =
{
"utf32-be","utf32-be:4:1","utf32-be:4:2","utf32-be:4:3",
};
DECL_MAP_MODE(utf32_le,4) =
{
"utf32-le","utf32-le:4:1","utf32-le:4:2","utf32-le:4:3",
};
DECL_MAP_MODE(sjis_jsky,5) =
{
"sjis","c:2.1",
"jsky:start:1","jsky:start:2","jsky:code1",
};
DECL_MAP_MODE(sjis_imode,4) =
{
"sjis","c:2.1",
"imode1:1","imode2:1",
};
DECL_MAP_MODE(sjis_doti,7) =
{
"sjis","c:2.1",
"doti1:1", "doti2:1", "doti3:1", "doti4:1", "doti5:1",
};
#endif
// ʸ��������Ƚ����˻��Ѥ��빽¤��
struct CodeCheck
{
charcode_t code;
const unsigned char* base;
const unsigned char* table;
#ifdef TEST
const char** msg;
#endif
};
// ʸ��������Ƚ��ν������
#ifndef TEST
#define GEN_CODE(name) \
{ cc_##name, (const unsigned char*)map_##name, (const unsigned char*)map_##name, }
#else
#define GEN_CODE(name) \
{ cc_##name, (const unsigned char*)map_##name, (const unsigned char*)map_##name, mode_##name, }
#endif
#define cc_tmpl_max 10
const CodeCheck cc_tmpl[] =
{
GEN_CODE(ascii),
GEN_CODE(eucjp),
GEN_CODE(sjis),
GEN_CODE(jis),
GEN_CODE(utf8),
GEN_CODE(utf32_be),
GEN_CODE(utf32_le),
GEN_CODE(sjis_jsky),
GEN_CODE(sjis_imode),
GEN_CODE(sjis_doti),
};
// Ƚ���̤ι�¤��
struct CodeResult
{
charcode_t code;
int begin;
int len;
};
// ʣ�����䤫�飱�Ĥ�����
int choice_one(CodeCheck* check, int cc_max)
{
charcode_t order[cc_tmpl_max] =
{
cc_utf32_be,
cc_utf32_le,
cc_ascii,
cc_jis,
cc_eucjp,
cc_sjis,
cc_sjis_jsky,
cc_sjis_imode,
cc_sjis_doti,
cc_utf8,
};
for( int cc=0; cc<cc_tmpl_max; ++cc )
{
for( int i=0; i<cc_max; ++i )
{
if( check[i].code==order[cc] )
{
return i;
}
}
}
return 0;
}
// getcode�ؿ�
SV* xs_getcode(SV* sv_str)
{
if( sv_str==&PL_sv_undef )
{
return new_SV_UNDEF();
}
unsigned char* src = (unsigned char*)SvPV(sv_str,PL_na);
int len = sv_len(sv_str);
const unsigned char* src_end = src+len;
if( len==0 )
{
return new_CC_UNKNOWN();
}
if( (len%4)==0 && len>=4 &&
( memcmp(src,RE_BOM4_BE,4)==0 || memcmp(src,RE_BOM4_LE,4)==0 ) )
{
return new_CC_UTF32();
}
if( (len%2)==0 && len>=2 &&
( memcmp(src,RE_BOM2_BE,2)==0 || memcmp(src,RE_BOM2_LE,2)==0 ) )
{
return new_CC_UTF16();
}
//fprintf(stderr,"Unicode::Japanese::(xs)getcode[%d]\n",len);
//fprintf(stderr,">>%s<<\n",src);
//bin_dump("in ",src,len);
//asm volatile(".int 3");
//SV_Buf result(len*3/2+4);
CodeCheck check[cc_tmpl_max];
memcpy(check,cc_tmpl,sizeof(cc_tmpl));
int cc_max = cc_tmpl_max;
for( ; src<src_end; ++src )
{
#if TEST && GC_DISP
fprintf(stderr,"[%d] %d (0x%02x)\n",len-(src_end-src),*src,*src);
#endif
// ���ܤĿʤ���
int invalids = 0;
for( int i=0; i<cc_max; ++i )
{
int nxt = check[i].table[*src];
#if TEST && GC_DISP
fprintf(stderr," %s : %d (%s)\n",charcodeToStr(check[i].code),nxt,nxt!=map_invalid?check[i].msg[nxt]:"invalid");
#endif
if( nxt!=map_invalid )
{
check[i].table = check[i].base+nxt*256;
}else
{
++invalids;
check[i].table = NULL;
}
}
if( invalids==0 )
{ // ������³
continue;
}else if( cc_max-invalids>0 )
{ // �ޤ������
int rd = 0;
int wr = 0;
for( ;rd<cc_max; ++rd )
{
if( check[rd].table )
{
if( rd!=wr )
{
check[wr] = check[rd];
}
++wr;
}
}
cc_max = wr;
}else
{ // ���������
return new_CC_UNKNOWN();
break;
}
}
int wr = 0;
for( int i=0; i<cc_max; ++i )
{
if( check[i].table == check[i].base )
{
if( wr!=i )
{
check[wr] = check[i];
}
++wr;
}
}
cc_max = wr;
#if TEST && GC_DISP
fprintf(stderr,"<availables>\n");
for( int i=0; i<cc_max; ++i )
{
fprintf(stderr," %s\n",charcodeToStr(check[i].code));
}
#endif
int index = choice_one(check,cc_max);
#if TEST && GC_DISP
fprintf(stderr,"<choice>\n [%d/0..%d]\n",index,cc_max-1);
fprintf(stderr,"<selected>\n");
fprintf(stderr," %s\n",charcodeToStr(check[index].code));
#endif
switch(check[index].code)
{
case cc_unknown: return new_CC_UNKNOWN();
case cc_ascii: return new_CC_ASCII();
case cc_sjis: return new_CC_SJIS();
case cc_eucjp: return new_CC_EUCJP();
case cc_jis: return new_CC_JIS();
case cc_utf8: return new_CC_UTF8();
//case cc_utf32: return new_CC_UTF32();
case cc_utf32_be: return new_CC_UTF32_BE();
case cc_utf32_le: return new_CC_UTF32_LE();
case cc_sjis_jsky: return new_CC_SJIS_JSKY();
case cc_sjis_imode: return new_CC_SJIS_IMODE();
case cc_sjis_doti: return new_CC_SJIS_DOTI();
default:
#ifdef TEST
return NULL;
#else
return new_CC_UNKNOWN();
#endif
}
}
|