getcode.c

filter: Statementstmt 66.6% 70 / 105 Branchbran 70.9% 44 / 62 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 68.2% 114 / 167 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« memmap_unix.c ↑ Summary memmap_common.c »
1  
2 /* $Id: getcode.c 5234 2008-01-16 09:45:32Z hio $ */
3  
4 #include "Japanese.h"
5 #include "getcode.h"
6  
7 #ifndef dAX
8 /* 5.6.x? */
9 #define dAX I32 ax = MARK - PL_stack_base + 1
10 #endif
11  
12 #define PERL_PATCHLEVEL_H_IMPLICIT
13 #include "patchlevel.h"
14 #if PATCHLEVEL == 4
15 /* 5.004 */
16 #define PL_stack_base Perl_stack_base
17 extern SV ** PL_stack_base;
18 #endif
19  
20 #define GC_DISP 0
21  
22 #ifndef __cplusplus
23 #undef bool
24 #undef true
25 #undef false
26 typedef enum bool { false, true, } bool;
27 #endif
28  
29 /* ʸ»ú¥³¡¼¥ÉÄê¿ô */
30 enum charcode_t
31 {
32 cc_unknown,
33 cc_ascii,
34 cc_sjis,
35 cc_eucjp,
36 cc_jis_au,
37 cc_jis_jsky,
38 cc_jis,
39 cc_utf8,
40 cc_utf16,
41 cc_utf32,
42 cc_utf32_be,
43 cc_utf32_le,
44 cc_sjis_jsky,
45 cc_sjis_imode,
46 cc_sjis_doti,
47 cc_sjis_au,
48 cc_last,
49 };
50 typedef enum charcode_t charcode_t;
51  
52 /* ʸ»ú¥³¡¼¥É̾ʸ»úÎó(SV*) */
53 #define new_CC_UNKNOWN() newSVpvn("unknown", 7)
54 #define new_CC_ASCII() newSVpvn("ascii", 5)
55 #define new_CC_SJIS() newSVpvn("sjis", 4)
56 #define new_CC_JIS_AU() newSVpvn("jis-au", 6)
57 #define new_CC_JIS_JSKY() newSVpvn("jis-jsky",8)
58 #define new_CC_JIS() newSVpvn("jis", 3)
59 #define new_CC_EUCJP() newSVpvn("euc", 3)
60 #define new_CC_UTF8() newSVpvn("utf8", 4)
61 #define new_CC_UTF16() newSVpvn("utf16", 5)
62 #define new_CC_UTF32() newSVpvn("utf32", 5)
63 #define new_CC_UTF32_BE() newSVpvn("utf32-be",8)
64 #define new_CC_UTF32_LE() newSVpvn("utf32-le",8)
65 #define new_CC_SJIS_JSKY() newSVpvn("sjis-jsky",9)
66 #define new_CC_SJIS_IMODE() newSVpvn("sjis-imode",10)
67 #define new_CC_SJIS_DOTI() newSVpvn("sjis-doti",9)
68 #define new_CC_SJIS_AU() newSVpvn("sjis-au",7)
69  
70 /* */
71 #define RE_BOM2_BE "\xfe\xff"
72 #define RE_BOM2_LE "\xff\xfe"
73 #define RE_BOM4_BE "\x00\x00\xfe\xff"
74 #define RE_BOM4_LE "\xff\xfe\x00\x00"
75  
76 #if defined(TEST) && GC_DISP
77 /* ʸ»ú¥³¡¼¥ÉÄê¿ô¤òʸ»ú¥³¡¼¥É̾¤Ë. */
78 static const char* charcodeToStr(charcode_t code)
79 {
80 switch(code)
81 {
82 case cc_unknown: return "unknown";
83 case cc_ascii: return "ascii";
84 case cc_sjis: return "sjis";
85 case cc_eucjp: return "eucjp";
86 case cc_jis_au: return "jis-au";
87 case cc_jis_jsky: return "jis-jsky";
88 case cc_jis: return "jis";
89 case cc_utf8: return "utf8";
90 case cc_utf32: return "utf32";
91 case cc_utf32_be: return "utf32-be";
92 case cc_utf32_le: return "utf32-le";
93 case cc_sjis_jsky: return "sjis-jsky";
94 case cc_sjis_imode: return "sjis-imode";
95 case cc_sjis_doti: return "sjis-doti";
96 case cc_sjis_au: return "sjis-au";
97 default: return NULL;
98 }
99 }
100 #endif
101 #ifdef TEST
102 DECL_MAP_MODE(ascii,1) = { "ascii", };
103 DECL_MAP_MODE(eucjp,5) =
104 { "eucjp", "0212:3.1","0212:3.2","c:2.1","kana:2.1",};
105 DECL_MAP_MODE(sjis,2) = { "sjis","c:2.1", };
106 DECL_MAP_MODE(jis,11) =
107 {
108 "jis","jis#1","jis#2","jis#3","jis#4","jis#5","jis#6",
109 "jis#7","jis#loop1","jis#loop2","jis#kana",
110 };
111 DECL_MAP_MODE(jis_au,12) =
112 {
113 "jis","jis#1","jis#2","jis#3","jis#4","jis#5","jis#6",
114 "jis#7","jis#loop1","jis#loop2","jis#kana","jis#au",
115 };
116 DECL_MAP_MODE(jis_jsky,13) =
117 {
118 "jis","jis#1","jis#2","jis#3","jis#4","jis#5","jis#6",
119 "jis#7","jis#loop1","jis#loop2","jis#kana","jis#j2","jis#jend",
120 };
121 DECL_MAP_MODE(utf8,6) =
122 {
123 "utf8",
124 "u8:6.1","u8:6.2","u8:6.3","u8:6.4","u8:6.5",
125 };
126 DECL_MAP_MODE(utf32_be,4) =
127 {
128 "utf32-be","utf32-be:4:1","utf32-be:4:2","utf32-be:4:3",
129 };
130 DECL_MAP_MODE(utf32_le,4) =
131 {
132 "utf32-le","utf32-le:4:1","utf32-le:4:2","utf32-le:4:3",
133 };
134 DECL_MAP_MODE(sjis_jsky,5) =
135 {
136 "sjis","c:2.1",
137 "jsky:start:1","jsky:start:2","jsky:code1",
138 };
139 DECL_MAP_MODE(sjis_imode,4) =
140 {
141 "sjis","c:2.1",
142 "imode1:1","imode2:1",
143 };
144 DECL_MAP_MODE(sjis_doti,7) =
145 {
146 "sjis","c:2.1",
147 "doti1:1", "doti2:1", "doti3:1", "doti4:1", "doti5:1",
148 };
149 DECL_MAP_MODE(sjis_au,3) =
150 {
151 "sjis","c:2.1",
152 "au:1",
153 };
154 #endif
155  
156 /* ʸ»ú¥³¡¼¥ÉȽÄê»þ¤Ë»ÈÍѤ¹¤ë¹½Â¤ÂÎ. */
157 struct CodeCheck
158 {
159 charcode_t code;
160 const unsigned char* base;
161 const unsigned char* table;
162 #ifdef TEST
163 const char** msg;
164 #endif
165 };
166 typedef struct CodeCheck CodeCheck;
167  
168 /* ʸ»ú¥³¡¼¥ÉȽÄê¤Î½é´ü¾õÂÖ. */
169 #ifndef TEST
170 #define GEN_CODE(name) \
171 { cc_##name, (const unsigned char*)map_##name, (const unsigned char*)map_##name, }
172 #else
173 #define GEN_CODE(name) \
174 { cc_##name, (const unsigned char*)map_##name, (const unsigned char*)map_##name, mode_##name, }
175 #endif
176 #define cc_tmpl_max 13
177 const CodeCheck cc_tmpl[cc_tmpl_max] =
178 {
179 GEN_CODE(utf32_be),
180 GEN_CODE(utf32_le),
181 GEN_CODE(ascii),
182 GEN_CODE(jis),
183 GEN_CODE(jis_au),
184 GEN_CODE(jis_jsky),
185 GEN_CODE(eucjp),
186 GEN_CODE(sjis),
187 GEN_CODE(sjis_jsky),
188 GEN_CODE(sjis_imode),
189 GEN_CODE(sjis_au),
190 GEN_CODE(sjis_doti),
191 GEN_CODE(utf8),
192 };
193  
194 /* ȽÄê·ë²Ì¤Î¹½Â¤ÂÎ. */
195 struct CodeResult
196 {
197 charcode_t code;
198 int begin;
199 int len;
200 };
201 typedef struct CodeResult CodeResult;
202  
203 14 static bool _is_acceptable_state(const CodeCheck* check)
204 {
205 /* special cases. */
206 14 ▶ if( check->table==map_jis_jsky[11] )
Branch: 1 branch
BranchTrueFalseTotal
if( check->table==map_jis_jsky[11] ) 0 14 14
207 { /* jis-jsky, jis#j2 */
208 0 return true;
209 }
210 14 ▶ if( check->table==map_sjis_jsky[4] )
Branch: 1 branch
BranchTrueFalseTotal
if( check->table==map_sjis_jsky[4] ) 0 14 14
211 { /* sjis-jsky, sjis#j2 */
212 0 return true;
213 }
214 14 return false;
215 }
216  
217 98 static int getcode_list(SV* sv_str, CodeCheck* check)
218 {
219 unsigned char* src;
220 int len;
221 const unsigned char* src_end;
222 int cc_max;
223
224 98 ▶ if( sv_str==&PL_sv_undef )
Branch: 1 branch
BranchTrueFalseTotal
if( sv_str==&PL_sv_undef ) 0 98 98
225 {
226 0 return 0;
227 }
228
229 98 src = (unsigned char*)SvPV(sv_str,PL_na);
230 98 len = sv_len(sv_str);
231 98 src_end = src+len;
232
233 /* empty string */
234 /* (jp:) ¶õʸ»úÎó¤Ï unknown */
235 98 ▶ if( len==0 )
Branch: 1 branch
BranchTrueFalseTotal
if( len==0 ) 0 98 98
236 {
237 0 return 0;
238 }
239
240 /* BOM of UTF32 */
241 98 ▶ if( (len%4)==0 && len>=4 &&
Branch: 2 branches
BranchTrueFalseTotal
if( (len%4)==0 && len>=4 && 12 86 98
if( (len%4)==0 && len>=4 && 12 0 12
242 12 ▶ ( memcmp(src,RE_BOM4_BE,4)==0 || memcmp(src,RE_BOM4_LE,4)==0 ) )
Branch: 2 branches
BranchTrueFalseTotal
( memcmp(src,RE_BOM4_BE,4)==0 || memcmp(src,RE_BOM4_LE,4)==0 ) ) 11 1 12
( memcmp(src,RE_BOM4_BE,4)==0 || memcmp(src,RE_BOM4_LE,4)==0 ) ) 1 10 11
243 {
244 2 check[0].code = cc_utf32;
245 2 return 1;
246 }
247
248 /* BOM of UTF16 */
249 96 ▶ if( (len%2)==0 && len>=2 &&
Branch: 2 branches
BranchTrueFalseTotal
if( (len%2)==0 && len>=2 && 16 80 96
if( (len%2)==0 && len>=2 && 16 0 16
250 16 ▶ ( memcmp(src,RE_BOM2_BE,2)==0 || memcmp(src,RE_BOM2_LE,2)==0 ) )
Branch: 2 branches
BranchTrueFalseTotal
( memcmp(src,RE_BOM2_BE,2)==0 || memcmp(src,RE_BOM2_LE,2)==0 ) ) 15 1 16
( memcmp(src,RE_BOM2_BE,2)==0 || memcmp(src,RE_BOM2_LE,2)==0 ) ) 1 14 15
251 {
252 2 check[0].code = cc_utf16;
253 2 return 1;
254 }
255  
256 /* fprintf(stderr,"Unicode::Japanese::(xs)getcode[%d]\n",len); */
257 /* fprintf(stderr,">>%s<<\n",src); */
258 /* bin_dump("in ",src,len); */
259  
260 94 memcpy(check,cc_tmpl,sizeof(cc_tmpl));
261 94 cc_max = cc_tmpl_max;
262  
263 573 ▶ for( ; src<src_end; ++src )
Branch: 1 branch
BranchTrueFalseTotal
for( ; src<src_end; ++src ) 480 93 573
264 {
265 int invalids;
266 int i;
267 #if TEST && GC_DISP
268 fprintf(stderr,"[%d] '%c' 0x%02x (%d)\n",len-(src_end-src),(0x20<=*src&&*src<=0x7f?*src:'.'),*src,*src);
269 #endif
270 /* Á«°Ü¤ò£±¤Ä¿Ê¤á¤ë¡Á */
271 480 invalids = 0;
272 3487 ▶ for( i=0; i<cc_max; ++i )
Branch: 1 branch
BranchTrueFalseTotal
for( i=0; i<cc_max; ++i ) 3007 480 3487
273 {
274 3007 int nxt = check[i].table[*src];
275 #if TEST && GC_DISP
276 fprintf(stderr," %s : %d (%s)\n",charcodeToStr(check[i].code),nxt,nxt!=map_invalid?check[i].msg[nxt]:"invalid");
277 #endif
278 3007 ▶ if( nxt!=map_invalid )
Branch: 1 branch
BranchTrueFalseTotal
if( nxt!=map_invalid ) 1932 1075 3007
279 {
280 1932 check[i].table = check[i].base+nxt*256;
281 }else
282 {
283 1075 ++invalids;
284 1075 check[i].table = NULL;
285 }
286 }
287 480 ▶ if( invalids==0 )
Branch: 1 branch
BranchTrueFalseTotal
if( invalids==0 ) 213 267 480
288 { /* Á´Éô·Ñ³ */
289 213 continue;
290 267 ▶ }else if( cc_max-invalids>0 )
Branch: 1 branch
BranchTrueFalseTotal
}else if( cc_max-invalids>0 ) 266 1 267
291 { /* ¤Þ¤À¤¢¤ê¡Á */
292 266 int rd = 0;
293 266 int wr = 0;
294 2456 ▶ for( ;rd<cc_max; ++rd )
Branch: 1 branch
BranchTrueFalseTotal
for( ;rd<cc_max; ++rd ) 2190 266 2456
295 {
296 2190 ▶ if( check[rd].table )
Branch: 1 branch
BranchTrueFalseTotal
if( check[rd].table ) 1120 1070 2190
297 {
298 1120 ▶ if( rd!=wr )
Branch: 1 branch
BranchTrueFalseTotal
if( rd!=wr ) 1113 7 1120
299 {
300 1113 check[wr] = check[rd];
301 }
302 1120 ++wr;
303 }
304 }
305 266 cc_max = wr;
306 }else
307 { /* Á´Éô¤À¤á¡Á */
308 1 return 0;
309 }
310 }
311  
312 /* check if we have stopped at a valid (final?) state */
313 {
314 93 int wr = 0;
315 int i;
316 240 ▶ for( i=0; i<cc_max; ++i )
Branch: 1 branch
BranchTrueFalseTotal
for( i=0; i<cc_max; ++i ) 147 93 240
317 {
318 147 ▶ if( check[i].table == check[i].base || _is_acceptable_state(&check[i]) )
Branch: 2 branches
BranchTrueFalseTotal
if( check[i].table == check[i].base || _is_acceptable_state(&check[i]) ) 14 133 147
if( check[i].table == check[i].base || _is_acceptable_state(&check[i]) ) 0 14 14
319 {
320 133 ▶ if( wr!=i )
Branch: 1 branch
BranchTrueFalseTotal
if( wr!=i ) 18 115 133
321 {
322 18 check[wr] = check[i];
323 }
324 133 ++wr;
325 }
326 }
327 93 cc_max = wr;
328 }
329  
330 #if TEST && GC_DISP
331 fprintf(stderr,"<availables>\n");
332 {
333 int i;
334 for( i=0; i<cc_max; ++i )
335 {
336 fprintf(stderr," %s\n",charcodeToStr(check[i].code));
337 }
338 }
339 #endif
340
341 93 return cc_max;
342 }
343  
344 /* getcode´Ø¿ô */
345 98 SV* xs_getcode(SV* sv_str)
346 {
347 int matches;
348 CodeCheck check[cc_tmpl_max];
349
350 98 ▶ if( sv_str==&PL_sv_undef )
Branch: 1 branch
BranchTrueFalseTotal
if( sv_str==&PL_sv_undef ) 0 98 98
351 {
352 0 return new_SV_UNDEF();
353 }
354 98 matches = getcode_list(sv_str, check);
355 98 ▶ if( matches>0 )
Branch: 1 branch
BranchTrueFalseTotal
if( matches>0 ) 97 1 98
356 {
357 97 int index = 0;
358 #if TEST && GC_DISP
359 fprintf(stderr,"<selected>\n");
360 fprintf(stderr," %d of 0..%d\n",index,matches-1);
361 fprintf(stderr," %s\n",charcodeToStr(check[index].code));
362 #endif
363 97 switch(check[index].code)
364 {
365 0 case cc_unknown: return new_CC_UNKNOWN();
366 1 case cc_ascii: return new_CC_ASCII();
367 2 case cc_sjis: return new_CC_SJIS();
368 1 case cc_eucjp: return new_CC_EUCJP();
369 2 case cc_jis: return new_CC_JIS();
370 1 case cc_jis_au: return new_CC_JIS_AU();
371 1 case cc_jis_jsky: return new_CC_JIS_JSKY();
372 1 case cc_utf8: return new_CC_UTF8();
373 2 case cc_utf16: return new_CC_UTF16();
374 2 case cc_utf32: return new_CC_UTF32();
375 1 case cc_utf32_be: return new_CC_UTF32_BE();
376 1 case cc_utf32_le: return new_CC_UTF32_LE();
377 2 case cc_sjis_jsky: return new_CC_SJIS_JSKY();
378 77 case cc_sjis_imode: return new_CC_SJIS_IMODE();
379 1 case cc_sjis_doti: return new_CC_SJIS_DOTI();
380 2 case cc_sjis_au: return new_CC_SJIS_AU();
381
382 0 default:
383 #ifdef TEST
384 return NULL;
385 #else
386 0 return new_CC_UNKNOWN();
387 #endif
388 }
389 }else
390 {
391 1 return new_CC_UNKNOWN();
392 }
393 }
394  
395 /* getcode_list´Ø¿ô */
396 0 int xs_getcode_list(SV* sv_str)
397 {
398 int matches;
399 CodeCheck check[cc_tmpl_max];
400 int i;
401 0 dSP; dMARK; dAX; /* XSARGS; - items */
402
403 0 ▶ if( sv_str==&PL_sv_undef )
Branch: 1 branch
BranchTrueFalseTotal
if( sv_str==&PL_sv_undef ) 0 0 0
404 {
405 0 return 0;
406 }
407 0 matches = getcode_list(sv_str, check);
408 0 ▶ if( matches<=0 )
Branch: 1 branch
BranchTrueFalseTotal
if( matches<=0 ) 0 0 0
409 {
410 0 return 0;
411 }
412 0 ▶ EXTEND(SP, matches);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, matches); 0 0 0
EXTEND(SP, matches); 0 0 0
413 0 ▶ for( i=0; i<matches; ++i )
Branch: 1 branch
BranchTrueFalseTotal
for( i=0; i<matches; ++i ) 0 0 0
414 {
415 0 switch(check[i].code)
416 {
417 0 case cc_unknown: ST(i) = sv_2mortal( new_CC_UNKNOWN() ); break;
418 0 case cc_ascii: ST(i) = sv_2mortal( new_CC_ASCII() ); break;
419 0 case cc_sjis: ST(i) = sv_2mortal( new_CC_SJIS() ); break;
420 0 case cc_eucjp: ST(i) = sv_2mortal( new_CC_EUCJP() ); break;
421 0 case cc_jis: ST(i) = sv_2mortal( new_CC_JIS() ); break;
422 0 case cc_jis_au: ST(i) = sv_2mortal( new_CC_JIS_AU() ); break;
423 0 case cc_jis_jsky: ST(i) = sv_2mortal( new_CC_JIS_JSKY() ); break;
424 0 case cc_utf8: ST(i) = sv_2mortal( new_CC_UTF8() ); break;
425 0 case cc_utf16: ST(i) = sv_2mortal( new_CC_UTF16() ); break;
426 0 case cc_utf32: ST(i) = sv_2mortal( new_CC_UTF32() ); break;
427 0 case cc_utf32_be: ST(i) = sv_2mortal( new_CC_UTF32_BE() ); break;
428 0 case cc_utf32_le: ST(i) = sv_2mortal( new_CC_UTF32_LE() ); break;
429 0 case cc_sjis_jsky: ST(i) = sv_2mortal( new_CC_SJIS_JSKY() ); break;
430 0 case cc_sjis_imode: ST(i) = sv_2mortal( new_CC_SJIS_IMODE() ); break;
431 0 case cc_sjis_doti: ST(i) = sv_2mortal( new_CC_SJIS_DOTI() ); break;
432 0 default: ST(i) = sv_2mortal( new_CC_UNKNOWN() ); break;
433 }
434 }
435 0 return matches;
436 }
437  
438 /* ----------------------------------------------------------------------------
439 * End of File.
440 * ------------------------------------------------------------------------- */
« memmap_unix.c ↑ Summary memmap_common.c »