~ubuntu-branches/ubuntu/maverick/ilohamail/maverick

« back to all changes in this revision

Viewing changes to IlohaMail/lang/jp/jcode.inc

  • Committer: Bazaar Package Importer
  • Author(s): Joerg Jaspert
  • Date: 2004-02-04 13:44:37 UTC
  • Revision ID: james.westby@ubuntu.com-20040204134437-kz8j3ui2qa7oq8z2
Tags: upstream-0.8.12
ImportĀ upstreamĀ versionĀ 0.8.12

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
////////////////////////////////
 
3
//      jcode.inc
 
4
//
 
5
//              Copied entirely from mini_jcode.phps by TOMO <groove@spencernetwork.org>
 
6
//
 
7
////////////////////////////////
 
8
 
 
9
function JcodeConvert(&$str, $from, $to)
 
10
{
 
11
        //0:AUTO DETECT
 
12
        //1:EUC-JP
 
13
        //2:Shift_JIS
 
14
        //3:ISO-2022-JP(JIS)
 
15
 
 
16
        if ($from == 0) $from = AutoDetect($str);
 
17
        if ($from == 1 && $to == 2) return EUCtoSJIS($str);
 
18
        if ($from == 1 && $to == 3) return EUCtoJIS($str);
 
19
        if ($from == 2 && $to == 1) return SJIStoEUC($str);
 
20
        if ($from == 2 && $to == 3) return SJIStoJIS($str);
 
21
        if ($from == 3 && $to == 1) return JIStoEUC($str);
 
22
        if ($from == 3 && $to == 2) return JIStoSJIS($str);
 
23
 
 
24
        return $str;
 
25
}
 
26
 
 
27
function AutoDetect(&$str)
 
28
{
 
29
        //0:US-ASCII
 
30
        //1:EUC-JP
 
31
        //2:Shift_JIS
 
32
        //3:ISO-2022-JP(JIS)
 
33
        //(4:Not available)
 
34
        //5:Unknown
 
35
 
 
36
        if (Jcode_is_JIS($str))   return 3; //JIS
 
37
        if (Jcode_is_ASCII($str)) return 0; //ASCII
 
38
 
 
39
        $b = unpack("C*", $str);
 
40
 
 
41
        if (Jcode_is_EUC($b))     return 1; //EUC
 
42
        if (Jcode_is_SJIS($b))    return 2; //SJIS
 
43
 
 
44
        return 5;
 
45
}
 
46
 
 
47
function HANtoZEN(&$str, $encode)
 
48
{
 
49
        //0:PASS
 
50
        //1:EUC-JP
 
51
        //2:Shift_JIS
 
52
        //3:ISO-2022-JP(JIS)
 
53
 
 
54
        if ($encode == 0) return $str;
 
55
        if ($encode == 1) return HANtoZEN_EUC($str);
 
56
        if ($encode == 2) return HANtoZEN_SJIS($str);
 
57
        if ($encode == 3) return HANtoZEN_JIS($str);
 
58
 
 
59
        return $str;
 
60
}
 
61
 
 
62
function ZENtoHAN(&$str, $encode, $kana=1, $alph=1)
 
63
{
 
64
        //0:PASS
 
65
        //1:EUC-JP
 
66
        //2:Shift_JIS
 
67
        //3:ISO-2022-JP(JIS)
 
68
 
 
69
        if ($encode == 0) return $str;
 
70
        if ($encode == 1) return ZENtoHAN_EUC($str,  $kana, $alph, $kana);
 
71
        if ($encode == 2) return ZENtoHAN_SJIS($str, $kana, $alph, $kana);
 
72
        if ($encode == 3) return ZENtoHAN_JIS($str, $kana, $alph, $kana);
 
73
 
 
74
        return $str;
 
75
}
 
76
 
 
77
 
 
78
function JIStoSJIS(&$str_JIS)
 
79
{
 
80
        $str_SJIS = '';
 
81
        $mode = 0;
 
82
        $b = unpack("C*", $str_JIS);
 
83
        $n = count($b);
 
84
 
 
85
        for ($i = 1; $i <= $n; $i++) {
 
86
                //Check escape sequence
 
87
                if ($b[$i] == 0x1B) {
 
88
                        if (($b[$i+1] == 0x24 && $b[$i+2] == 0x42)
 
89
                                || ($b[$i+1] == 0x24 && $b[$i+2] == 0x40)) {
 
90
                                $mode = 1;
 
91
                        } elseif (($b[$i+1] == 0x28 && $b[$i+2] == 0x49)) {
 
92
                                $mode = 2;
 
93
                        } else {
 
94
                                $mode = 0;
 
95
                        }
 
96
                        $i += 3;
 
97
                }
 
98
 
 
99
                if (!isset($b[$i])) break;
 
100
 
 
101
                //Do convert
 
102
                if ($mode == 1) {
 
103
                        $b1 = $b[$i];
 
104
                        $b2 = $b[$i+1];
 
105
                        if ($b1 & 0x01) {
 
106
                                $b1 >>= 1;
 
107
                                if ($b1 < 0x2F) $b1 += 0x71; else $b1 -= 0x4F;
 
108
                                if ($b2 > 0x5F) $b2 += 0x20; else $b2 += 0x1F;
 
109
                        } else {
 
110
                                $b1 >>= 1;
 
111
                                if ($b1 <= 0x2F) $b1 += 0x70; else $b1 -= 0x50;
 
112
                                $b2 += 0x7E;
 
113
                        }
 
114
                        $str_SJIS .= chr($b1).chr($b2);
 
115
                        $i++;
 
116
                } elseif ($mode == 2) {
 
117
                        $str_SJIS .= chr($b[$i] + 0x80);
 
118
                } else {
 
119
                        $str_SJIS .= chr($b[$i]);
 
120
                }
 
121
        }
 
122
 
 
123
        return $str_SJIS;
 
124
}
 
125
 
 
126
function JIStoEUC(&$str_JIS)
 
127
{
 
128
        $str_EUC = '';
 
129
        $mode = 0;
 
130
        $b = unpack("C*", $str_JIS);
 
131
        $n = count($b);
 
132
 
 
133
        for ($i = 1; $i <= $n; $i++) {
 
134
                //Check escape sequence
 
135
                if ($b[$i] == 0x1B) {
 
136
                        if (($b[$i+1] == 0x24 && $b[$i+2] == 0x42)
 
137
                                || ($b[$i+1] == 0x24 && $b[$i+2] == 0x40)) {
 
138
                                $mode = 1;
 
139
                        } elseif (($b[$i+1] == 0x28 && $b[$i+2] == 0x49)) {
 
140
                                $mode = 2;
 
141
                        } else {
 
142
                                $mode = 0;
 
143
                        }
 
144
                        $i += 3;
 
145
                }
 
146
 
 
147
                if (!isset($b[$i])) break;
 
148
 
 
149
                //Do convert
 
150
                if ($mode == 1) {
 
151
                        $str_EUC .= chr($b[$i] + 0x80).chr($b[$i+1] + 0x80);
 
152
                        $i++;
 
153
                } elseif ($mode == 2) {
 
154
                        $str_EUC .= chr(0x8E).chr($b[$i] + 0x80);
 
155
                } else {
 
156
                        $str_EUC .= chr($b[$i]);
 
157
                }
 
158
        }
 
159
 
 
160
        return $str_EUC;
 
161
}
 
162
 
 
163
function SJIStoJIS(&$str_SJIS)
 
164
{
 
165
        $str_JIS = '';
 
166
        $mode = 0;
 
167
        $b = unpack("C*", $str_SJIS);
 
168
        $n = count($b);
 
169
        //Escape sequence
 
170
        $ESC = array(chr(0x1B).chr(0x28).chr(0x42),
 
171
                     chr(0x1B).chr(0x24).chr(0x42),
 
172
                     chr(0x1B).chr(0x28).chr(0x49));
 
173
 
 
174
        for ($i = 1; $i <= $n; $i++) {
 
175
                $b1 = $b[$i];
 
176
                if (0xA1 <= $b1 && $b1 <= 0xDF) {
 
177
                        if ($mode != 2) {
 
178
                                $mode = 2;
 
179
                                $str_JIS .= $ESC[$mode];
 
180
                        }
 
181
                        $str_JIS .= chr($b1 - 0x80);
 
182
                } elseif ($b1 >= 0x80) {
 
183
                        if ($mode != 1) {
 
184
                                $mode = 1;
 
185
                                $str_JIS .= $ESC[$mode];
 
186
                        }
 
187
                        $b2 = $b[$i+1];
 
188
                        $b1 <<= 1;
 
189
                        if ($b2 < 0x9F) {
 
190
                                if ($b1 < 0x13F) $b1 -= 0xE1; else $b1 -= 0x61;
 
191
                                if ($b2 > 0x7E)  $b2 -= 0x20; else $b2 -= 0x1F;
 
192
                        } else {
 
193
                                if ($b1 < 0x13F) $b1 -= 0xE0; else $b1 -= 0x60;
 
194
                                $b2 -= 0x7E;
 
195
                        }
 
196
                        $str_JIS .= chr($b1).chr($b2);
 
197
                        $i++;
 
198
                } else {
 
199
                        if ($mode != 0) {
 
200
                                $mode = 0;
 
201
                                $str_JIS .= $ESC[$mode];
 
202
                        }
 
203
                        $str_JIS .= chr($b1);
 
204
                }
 
205
        }
 
206
        if ($mode != 0) $str_JIS .= $ESC[0];
 
207
 
 
208
        return $str_JIS;
 
209
}
 
210
 
 
211
function SJIStoEUC(&$str_SJIS)
 
212
{
 
213
        $b = unpack("C*", $str_SJIS);
 
214
        $n = count($b);
 
215
        $str_EUC = '';
 
216
 
 
217
        for ($i = 1; $i <= $n; $i++) {
 
218
                $b1 = $b[$i];
 
219
                if (0xA1 <= $b1 && $b1 <= 0xDF) {
 
220
                        $str_EUC .= chr(0x8E).chr($b1);
 
221
                } elseif ($b1 >= 0x81) {
 
222
                        $b2 = $b[$i+1];
 
223
                        $b1 <<= 1;
 
224
                        if ($b2 < 0x9F) {
 
225
                                if ($b1 < 0x13F) $b1 -= 0x61; else $b1 -= 0xE1;
 
226
                                if ($b2 > 0x7E)  $b2 += 0x60; else $b2 += 0x61;
 
227
                        } else {
 
228
                                if ($b1 < 0x13F) $b1 -= 0x60; else $b1 -= 0xE0;
 
229
                                $b2 += 0x02;
 
230
                        }
 
231
                        $str_EUC .= chr($b1).chr($b2);
 
232
                        $i++;
 
233
                } else {
 
234
                        $str_EUC .= chr($b1);
 
235
                }
 
236
        }
 
237
 
 
238
        return $str_EUC;
 
239
}
 
240
 
 
241
function EUCtoJIS(&$str_EUC)
 
242
{
 
243
        $str_JIS = '';
 
244
        $mode = 0;
 
245
        $b = unpack("C*", $str_EUC);
 
246
        $n = count($b);
 
247
        //Escape sequence
 
248
        $ESC = array(chr(0x1B).chr(0x28).chr(0x42),
 
249
                     chr(0x1B).chr(0x24).chr(0x42),
 
250
                     chr(0x1B).chr(0x28).chr(0x49));
 
251
 
 
252
        for ($i = 1; $i <= $n; $i++) {
 
253
                $b1 = $b[$i];
 
254
                if ($b1 == 0x8E) {
 
255
                        if ($mode != 2) {
 
256
                                $mode = 2;
 
257
                                $str_JIS .= $ESC[$mode];
 
258
                        }
 
259
                        $str_JIS .= chr($b[$i+1] - 0x80);
 
260
                        $i++;
 
261
                } elseif ($b1 > 0x8E) {
 
262
                        if ($mode != 1) {
 
263
                                $mode = 1;
 
264
                                $str_JIS .= $ESC[$mode];
 
265
                        }
 
266
                        $str_JIS .= chr($b1 - 0x80).chr($b[$i+1] - 0x80);
 
267
                        $i++;
 
268
                } else {
 
269
                        if ($mode != 0) {
 
270
                                $mode = 0;
 
271
                                $str_JIS .= $ESC[$mode];
 
272
                        }
 
273
                        $str_JIS .= chr($b1);
 
274
                }
 
275
        }
 
276
        if ($mode != 0) $str_JIS .= $ESC[0];
 
277
 
 
278
        return $str_JIS;
 
279
}
 
280
 
 
281
function EUCtoSJIS(&$str_EUC)
 
282
{
 
283
        $str_SJIS = '';
 
284
        $b = unpack("C*", $str_EUC);
 
285
        $n = count($b);
 
286
 
 
287
        for ($i = 1; $i <= $n; $i++) {
 
288
                $b1 = $b[$i];
 
289
                if ($b1 == 0x8E) {
 
290
                        $str_SJIS .= chr($b[$i+1]);
 
291
                        $i++;
 
292
                } elseif ($b1 >= 0x8E) {
 
293
                        $b2 = $b[$i+1];
 
294
                        if ($b1 & 0x01) {
 
295
                                $b1 >>= 1;
 
296
                                if ($b1 < 0x6F) $b1 += 0x31; else $b1 += 0x71;
 
297
                                if ($b2 > 0xDF) $b2 -= 0x60; else $b2 -= 0x61;
 
298
                        } else {
 
299
                                $b1 >>= 1;
 
300
                                if ($b1 <= 0x6F) $b1 += 0x30; else $b1 += 0x70;
 
301
                                $b2 -= 0x02;
 
302
                        }
 
303
                        $str_SJIS .= chr($b1).chr($b2);
 
304
                        $i++;
 
305
                } else {
 
306
                        $str_SJIS .= chr($b1);
 
307
                }
 
308
        }
 
309
 
 
310
        return $str_SJIS;
 
311
}
 
312
 
 
313
function HANtoZEN_EUC(&$str_HAN)
 
314
{
 
315
        $table_han2zen_euc = array(0xA1A3,0xA1D6,0xA1D7,0xA1A2,0xA1A6,0xA5F2,
 
316
        0xA5A1,0xA5A3,0xA5A5,0xA5A7,0xA5A9,0xA5E3,0xA5E5,0xA5E7,0xA5C3,0xA1BC,
 
317
        0xA5A2,0xA5A4,0xA5A6,0xA5A8,0xA5AA,0xA5AB,0xA5AD,0xA5AF,0xA5B1,0xA5B3,
 
318
        0xA5B5,0xA5B7,0xA5B9,0xA5BB,0xA5BD,0xA5BF,0xA5C1,0xA5C4,0xA5C6,0xA5C8,
 
319
        0xA5CA,0xA5CB,0xA5CC,0xA5CD,0xA5CE,0xA5CF,0xA5D2,0xA5D5,0xA5D8,0xA5DB,
 
320
        0xA5DE,0xA5DF,0xA5E0,0xA5E1,0xA5E2,0xA5E4,0xA5E6,0xA5E8,0xA5E9,0xA5EA,
 
321
        0xA5EB,0xA5EC,0xA5ED,0xA5EF,0xA5F3,0xA1AB,0xA1AC);
 
322
 
 
323
        $str_ZEN = '';
 
324
        $b = unpack("C*", $str_HAN);
 
325
        $n = count($b);
 
326
 
 
327
        for ($i = 1; $i <= $n; $i++) {
 
328
                $b1 = $b[$i];
 
329
                if ($b1 == 0x8E) {
 
330
                        $b2 = $b[$i+1] - 0xA1;
 
331
                        $c1 = ($table_han2zen_euc[$b2] & 0xFF00) >> 8;
 
332
                        $c2 =  $table_han2zen_euc[$b2] & 0x00FF;
 
333
                        $str_ZEN .= chr($c1).chr($c2);
 
334
                        $i++;
 
335
                } elseif ($b1 >= 0xA1) {
 
336
                        $str_ZEN .= chr($b1).chr($b[$i+1]);
 
337
                        $i++;
 
338
                } else {
 
339
                        $str_ZEN .= chr($b1);
 
340
                }
 
341
        }
 
342
 
 
343
        return $str_ZEN;
 
344
}
 
345
 
 
346
function HANtoZEN_SJIS(&$str_HAN)
 
347
{
 
348
        $table_han2zen_sjis = array(0x8142,0x8175,0x8176,0x8141,0x8145,0x8392,
 
349
        0x8340,0x8342,0x8344,0x8346,0x8348,0x8383,0x8385,0x8387,0x8362,0x815B,
 
350
        0x8341,0x8343,0x8345,0x8347,0x8349,0x834A,0x834C,0x834E,0x8350,0x8352,
 
351
        0x8354,0x8356,0x8358,0x835A,0x835C,0x835E,0x8360,0x8363,0x8365,0x8367,
 
352
        0x8369,0x836A,0x836B,0x836C,0x836D,0x836E,0x8371,0x8374,0x8377,0x837A,
 
353
        0x837D,0x837E,0x8380,0x8381,0x8382,0x8384,0x8386,0x8388,0x8389,0x838A,
 
354
        0x838B,0x838C,0x838D,0x838F,0x8393,0x814A,0x814B);
 
355
 
 
356
        $str_ZEN = '';
 
357
        $b = unpack("C*", $str_HAN);
 
358
        $n = count($b);
 
359
 
 
360
        for ($i = 1; $i <= $n; $i++) {
 
361
                $b1 = $b[$i];
 
362
                if (0xA1 <= $b1 && $b1 <= 0xDF) {
 
363
                        $b1 -= 0xA1;
 
364
                        $c1 = ($table_han2zen_sjis[$b1] & 0xFF00) >> 8;
 
365
                        $c2 =  $table_han2zen_sjis[$b1] & 0x00FF;
 
366
                        $str_ZEN .= chr($c1).chr($c2);
 
367
                } elseif ($b1 >= 0x80) {
 
368
                        $str_ZEN .= chr($b1).chr($b[$i+1]);
 
369
                        $i++;
 
370
                } else {
 
371
                        $str_ZEN .= chr($b1);
 
372
                }
 
373
        }
 
374
 
 
375
        return $str_ZEN;
 
376
}
 
377
 
 
378
function HANtoZEN_JIS(&$str_HAN)
 
379
{
 
380
        $table_han2zen_jis = array(0x2123,0x2156,0x2157,0x2122,0x2126,0x2572,
 
381
        0x2521,0x2523,0x2525,0x2527,0x2529,0x2563,0x2565,0x2567,0x2543,0x213C,
 
382
        0x2522,0x2524,0x2526,0x2528,0x252A,0x252B,0x252D,0x252F,0x2531,0x2533,
 
383
        0x2535,0x2537,0x2539,0x253B,0x253D,0x253F,0x2541,0x2544,0x2546,0x2548,
 
384
        0x254A,0x254B,0x254C,0x254D,0x254E,0x254F,0x2552,0x2555,0x2558,0x255B,
 
385
        0x255E,0x255F,0x2560,0x2561,0x2562,0x2564,0x2566,0x2568,0x2569,0x256A,
 
386
        0x256B,0x256C,0x256D,0x256F,0x2573,0x212B,0x212C);
 
387
 
 
388
        $str_ZEN = '';
 
389
        $b = unpack("C*", $str_HAN);
 
390
        $n = count($b);
 
391
        $mode = 0;
 
392
        $new_mode = 0;
 
393
        $esc = FALSE;
 
394
        $ESC = array(chr(0x1B).chr(0x28).chr(0x42),
 
395
                     chr(0x1B).chr(0x24).chr(0x42),
 
396
                     chr(0x1B).chr(0x28).chr(0x49));
 
397
 
 
398
        for ($i = 1; $i <= $n; $i++) {
 
399
 
 
400
                if ($b[$i] == 0x1B) {
 
401
                        if (($b[$i+1] == 0x24 && $b[$i+2] == 0x42)
 
402
                                || ($b[$i+1] == 0x24 && $b[$i+2] == 0x40)) {
 
403
                                $mode = 1; //Zenkaku
 
404
                        } elseif ($b[$i+1] == 0x28 && $b[$i+2] == 0x49) {
 
405
                                $mode = 2; //Hankaku
 
406
                        } else {
 
407
                                $mode = 0; //ASCII
 
408
                        }
 
409
                        $i += 3;
 
410
                }
 
411
 
 
412
                if (!isset($b[$i])) break;
 
413
 
 
414
                if ($mode == 2) {
 
415
                        if ($new_mode != 1) $esc = TRUE;
 
416
                        $new_mode = 1;
 
417
                        $c  = $b[$i] - 0x21;
 
418
                        $c1 = ($table_han2zen_jis[$c] & 0xFF00) >> 8;
 
419
                        $c2 =  $table_han2zen_jis[$c] & 0x00FF;
 
420
                        $str = chr($c1).chr($c2);
 
421
                } else {
 
422
                        if ($new_mode != $mode) $esc = TRUE;
 
423
                        $new_mode = $mode;
 
424
                        $str = chr($b[$i]);
 
425
                }
 
426
 
 
427
                if ($esc) {  //add escape sequence
 
428
                        $str_ZEN .= $ESC[$new_mode];
 
429
                        $esc = FALSE;
 
430
                }
 
431
                $str_ZEN .= $str;
 
432
        }
 
433
 
 
434
        if ($new_mode != 0) $str_ZEN .= $ESC[0];
 
435
 
 
436
        return $str_ZEN;
 
437
}
 
438
 
 
439
function ZENtoHAN_EUC(&$str_ZEN, $kana = 1, $alph = 1, $sym = 1)
 
440
{
 
441
        $kana_euc = array(
 
442
        0x00A7,0x00B1,0x00A8,0x00B2,0x00A9,0x00B3,0x00AA,0x00B4,0x00AB,0x00B5,
 
443
        0x00B6,0xB6DE,0x00B7,0xB7DE,0x00B8,0xB8DE,0x00B9,0xB9DE,0x00BA,0xBADE,
 
444
        0x00BB,0xBBDE,0x00BC,0xBCDE,0x00BD,0xBDDE,0x00BE,0xBEDE,0x00BF,0xBFDE,
 
445
        0x00C0,0xC0DE,0x00C1,0xC1DE,0x00AF,0x00C2,0xC2DE,0x00C3,0xC3DE,0x00C4,
 
446
        0xC4DE,0x00C5,0x00C6,0x00C7,0x00C8,0x00C9,0x00CA,0xCADE,0xCADF,0x00CB,
 
447
        0xCBDE,0xCBDF,0x00CC,0xCCDE,0xCCDF,0x00CD,0xCDDE,0xCDDF,0x00CE,0xCEDE,
 
448
        0xCEDF,0x00CF,0x00D0,0x00D1,0x00D2,0x00D3,0x00AC,0x00D4,0x00AD,0x00D5,
 
449
        0x00AE,0x00D6,0x00D7,0x00D8,0x00D9,0x00DA,0x00DB,0x0000,0x00DC,0x0000,
 
450
        0x0000,0x00A6,0x00DD,0xB3DE,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
 
451
        0x0000,0x0000,0x0000,0x0000,0x0000
 
452
        );
 
453
 
 
454
        $sym_euc = array(
 
455
        0x0020,0x8EA4,0x8EA1,0x0000,0x0000,0x8EA5,0x0000,0x0000,0x0000,0x0000,
 
456
        0x8EDE,0x8EDF,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
 
457
        0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x8EB0,0x0000,0x0000,
 
458
        0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
 
459
        0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
 
460
        0x0000,0x0000,0x0000,0x8EA2,0x8EA3,0x0000,0x0000,0x0000,0x0000,0x0000,
 
461
        0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
 
462
        0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
 
463
        0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
 
464
        0x0000,0x0000,0x0000,0x0000,0x0000
 
465
        );
 
466
 
 
467
        $str_HAN = '';
 
468
        $b = unpack("C*", $str_ZEN);
 
469
        $n = count($b);
 
470
 
 
471
        for ($i = 1; $i <= $n; $i++) {
 
472
                $b1 = $b[$i];
 
473
                if ($b1 >= 0x80) {
 
474
                        if ($kana == 1 && $b1 == 0xA5) { // Katakana
 
475
                                $c  = $b[$i+1] - 0xA1;
 
476
                                $c1 = ($kana_euc[$c] & 0xFF00) >> 8;
 
477
                                $c2 =  $kana_euc[$c] & 0x00FF;
 
478
                                if ($c1 == 0x00) {
 
479
                                        if ($c2 == 0x00) {
 
480
                                                $str_HAN .= chr($b1).chr($b[$i+1]);
 
481
                                        } else {
 
482
                                                $str_HAN .= chr(0x8E).chr($c2);
 
483
                                        }
 
484
                                } else {
 
485
                                        $str_HAN .= chr(0x8E).chr($c1).chr(0x8E).chr($c2);
 
486
                                }
 
487
                        } elseif ($sym == 1 && $b1 == 0xA1) { // Symbol
 
488
                                $c = $b[$i+1] - 0xA1;
 
489
                                $c1 = ($sym_euc[$c] & 0xFF00) >> 8;
 
490
                                $c2 =  $sym_euc[$c] & 0x00FF;
 
491
                                if ($c1 == 0x00) {
 
492
                                        if ($c2 == 0x00) {
 
493
                                                $str_HAN .= chr($b1).chr($b[$i+1]);
 
494
                                        } else {
 
495
                                                $str_HAN .= chr($c2);
 
496
                                        }
 
497
                                } else {
 
498
                                        $str_HAN .= chr($c1).chr($c2);
 
499
                                }
 
500
                        } elseif ( $alph == 1 && $b1 == 0xA3 ) { // Alphabet & Number
 
501
                                $str_HAN .= chr($b[$i+1] - 0x80);
 
502
                        } else { // Rest of Zenkaku
 
503
                                $str_HAN .= chr($b1).chr($b[$i+1]);
 
504
                        }
 
505
                        $i++;
 
506
                } else {  // ASCII
 
507
                        $str_HAN .= chr($b1);
 
508
                }
 
509
        }
 
510
 
 
511
        return $str_HAN;
 
512
}
 
513
 
 
514
function ZENtoHAN_SJIS(&$str_ZEN, $kana = 1, $alph = 1, $sym = 1)
 
515
{
 
516
        $kana_sjis = array(
 
517
        0x00A7,0x00B1,0x00A8,0x00B2,0x00A9,0x00B3,0x00AA,0x00B4,0x00AB,0x00B5,
 
518
        0x00B6,0xB6DE,0x00B7,0xB7DE,0x00B8,0xB8DE,0x00B9,0xB9DE,0x00BA,0xBADE,
 
519
        0x00BB,0xBBDE,0x00BC,0xBCDE,0x00BD,0xBDDE,0x00BE,0xBEDE,0x00BF,0xBFDE,
 
520
        0x00C0,0xC0DE,0x00C1,0xC1DE,0x00AF,0x00C2,0xC2DE,0x00C3,0xC3DE,0x00C4,
 
521
        0xC4DE,0x00C5,0x00C6,0x00C7,0x00C8,0x00C9,0x00CA,0xCADE,0xCADF,0x00CB,
 
522
        0xCBDE,0xCBDF,0x00CC,0xCCDE,0xCCDF,0x00CD,0xCDDE,0xCDDF,0x00CE,0xCEDE,
 
523
        0xCEDF,0x00CF,0x00D0,0x0000,0x00D1,0x00D2,0x00D3,0x00AC,0x00D4,0x00AD,
 
524
        0x00D5,0x00AE,0x00D6,0x00D7,0x00D8,0x00D9,0x00DA,0x00DB,0x0000,0x00DC,
 
525
        0x0000,0x0000,0x00A6,0x00DD,0xB3DE,0x0000,0x0000,0x0000,0x0000,0x0000,
 
526
        0x0000,0x0000,0x0000,0x0000,0x0000,0x0000
 
527
        );
 
528
 
 
529
        $sym_sjis = array(
 
530
        0x20,0xA4,0xA1,0x00,0x00,0xA5,0x00,0x00,0x00,0x00,0xDE,0xDF,0x00,0x00,
 
531
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xB0,
 
532
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
 
533
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xA2,0xA3,0x00,
 
534
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
 
535
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
 
536
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
 
537
        );
 
538
 
 
539
        $str_HAN = '';
 
540
        $b = unpack("C*", $str_ZEN);
 
541
        $n = count($b);
 
542
 
 
543
        for ($i = 1; $i <= $n; $i++) {
 
544
                $b1 = $b[$i];
 
545
                if ($b1 >= 0x80) {
 
546
                        $b2 = $b[$i+1];
 
547
                        if ($kana == 1 && $b1 == 0x83  // Katakana
 
548
                                && (0x3F < $b2 && $b2 < 0x9F)) {
 
549
                                $c = $b2 - 0x40;
 
550
                                $c1 = ($kana_sjis[$c] & 0xFF00) >> 8;
 
551
                                $c2 =  $kana_sjis[$c] & 0x00FF;
 
552
                                if ($c1 == 0x00) {
 
553
                                        if ($c2 == 0x00) {
 
554
                                                $str_HAN .= chr($b1).chr($b2);
 
555
                                        } else {
 
556
                                                $str_HAN .= chr($c2);
 
557
                                        }
 
558
                                } else {
 
559
                                        $str_HAN .= chr($c1).chr($c2);
 
560
                                }
 
561
                        } elseif ($sym == 1 && $b1 == 0x81  // Symbol
 
562
                                && (0x3F < $b2 && $b2 < 0x9F)) {
 
563
                                $c1 = $sym_sjis[ $b2 - 0x40 ];
 
564
                                if ($c1 == 0x00) {
 
565
                                        $str_HAN .= chr($b1).chr($b2);
 
566
                                } else {
 
567
                                        $str_HAN .= chr($c1);
 
568
                                }
 
569
                        } elseif ($alph == 1 && $b1 == 0x82  // Alphabet & Number
 
570
                                && (0x3F < $b2 && $b2 < 0x9F)) {
 
571
                                if ($b[$i+1] < 0x80) {
 
572
                                        $str_HAN .= chr($b2 - 0x1F);
 
573
                                } else {
 
574
                                        $str_HAN .= chr($b2 - 0x20);
 
575
                                }
 
576
                        } else { // Rest of Zenkaku
 
577
                                $str_HAN .= chr($b1).chr($b2);
 
578
                        }
 
579
                        $i++;
 
580
                } else {  // ASCII
 
581
                        $str_HAN .= chr($b1);
 
582
                }
 
583
        }
 
584
 
 
585
        return $str_HAN;
 
586
}
 
587
 
 
588
function ZENtoHAN_JIS(&$str_ZEN, $kana = 1, $alph = 1, $sym = 1)
 
589
{
 
590
        $kana_jis = array(
 
591
        0x0027,0x0031,0x0028,0x0032,0x0029,0x0033,0x002A,0x0034,0x002B,0x0035,
 
592
        0x0036,0x365E,0x0037,0x375E,0x0038,0x385E,0x0039,0x395E,0x003A,0x3A5E,
 
593
        0x003B,0x3B5E,0x003C,0x3C5E,0x003D,0x3D5E,0x003E,0x3E5E,0x003F,0x3F5E,
 
594
        0x0040,0x405E,0x0041,0x415E,0x002F,0x0042,0x425E,0x0043,0x435E,0x0044,
 
595
        0x445E,0x0045,0x0046,0x0047,0x0048,0x0049,0x004A,0x4A5E,0x4A5F,0x004B,
 
596
        0x4B5E,0x4B5F,0x004C,0x4C5E,0x4C5F,0x004D,0x4D5E,0x4D5F,0x004E,0x4E5E,
 
597
        0x4E5F,0x004F,0x0050,0x0051,0x0052,0x0053,0x002C,0x0054,0x002D,0x0055,
 
598
        0x002E,0x0056,0x0057,0x0058,0x0059,0x005A,0x005B,0x0000,0x005C,0x0000,
 
599
        0x0000,0x0026,0x005D,0x335E,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
 
600
        0x0000,0x0000,0x0000,0x0000,0x0000
 
601
        );
 
602
 
 
603
        $sym_jis = array(
 
604
        0x0020,0xFF24,0xFF21,0x0000,0x0000,0xFF25,0x0000,0x0000,0x0000,0x0000,
 
605
        0xFF5E,0xFF5F,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
 
606
        0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0xFF30,0x0000,0x0000,
 
607
        0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
 
608
        0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
 
609
        0x0000,0x0000,0x0000,0xFF22,0xFF23,0x0000,0x0000,0x0000,0x0000,0x0000,
 
610
        0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
 
611
        0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
 
612
        0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
 
613
        0x0000,0x0000,0x0000,0x0000,0x0000
 
614
        );
 
615
 
 
616
        $str_HAN = '';
 
617
        $b = unpack("C*", $str_ZEN);
 
618
        $n = count($b);
 
619
        $mode = 0;
 
620
        $new_mode = 0;
 
621
        $esc = FALSE;
 
622
        $ESC = array(chr(0x1B).chr(0x28).chr(0x42),
 
623
                     chr(0x1B).chr(0x24).chr(0x42),
 
624
                     chr(0x1B).chr(0x28).chr(0x49));
 
625
 
 
626
        for ($i = 1; $i <= $n; $i++) {
 
627
                if ($b[$i] == 0x1B) {
 
628
                        if (($b[$i+1] == 0x24 && $b[$i+2] == 0x42)
 
629
                                || ($b[$i+1] == 0x24 && $b[$i+2] == 0x40)) {
 
630
                                $mode = 1;
 
631
                        } elseif ($b[$i+1] == 0x28 && $b[$i+2] == 0x49) {
 
632
                                $mode = 2;
 
633
                        } else {
 
634
                                $mode = 0;
 
635
                        }
 
636
                        $i += 3;
 
637
                }
 
638
 
 
639
                if (!isset($b[$i])) break;
 
640
 
 
641
                $b1 = $b[$i];
 
642
                if ($mode == 1) { //Zenkaku
 
643
                        if ($alph == 1 && $b1 == 0x23) {  //Alphabet & Number
 
644
                                if ($new_mode != 0) $esc = TRUE;
 
645
                                $new_mode = 0;
 
646
                                $str = chr($b[$i+1]);
 
647
                        } elseif ($sym == 1 && $b1 == 0x21) {  //Symbol
 
648
                                $c = $b[$i+1] - 0x21;
 
649
                                $c1 = ($sym_jis[$c] & 0xFF00) >> 8;
 
650
                                $c2 =  $sym_jis[$c] & 0x00FF;
 
651
                                if ($c1 == 0x00) {
 
652
                                        if ($c2 == 0x00) {
 
653
                                                if ($new_mode != 1) $esc = TRUE;
 
654
                                                $new_mode = 1;
 
655
                                                $str = chr($b1).chr($b[$i+1]);
 
656
                                        } else {
 
657
                                                if ($new_mode != 0) $esc = true;
 
658
                                                $new_mode = 0;
 
659
                                                $str = chr($c2);
 
660
                                        }
 
661
                                } else {
 
662
                                        if ($new_mode != 2) $esc = TRUE;
 
663
                                        $new_mode = 2;
 
664
                                        $str = chr($c2);
 
665
                                }
 
666
                        } elseif ($kana == 1 && $b1 == 0x25) {  //Katakana
 
667
                                $c  = $b[$i+1] - 0x21;
 
668
                                $c1 = ($kana_jis[$c] & 0xFF00) >> 8;
 
669
                                $c2 =  $kana_jis[$c] & 0x00FF;
 
670
                                if ($c1 == 0x00) {
 
671
                                        if ($c2 == 0x00) {
 
672
                                                if ($new_mode != 1) $esc = TRUE;
 
673
                                                $new_mode = 1;
 
674
                                                $str = chr($b1).chr($b[$i+1]);
 
675
                                        } else {
 
676
                                                if ($new_mode != 2) $esc = TRUE;
 
677
                                                $new_mode = 2;
 
678
                                                $str = chr($c2);
 
679
                                        }
 
680
                                } else {
 
681
                                        if ($new_mode != 2) $esc = TRUE;
 
682
                                        $new_mode = 2;
 
683
                                        $str = chr($c1).chr($c2);
 
684
                                }
 
685
                        } else {
 
686
                                if ($new_mode != 1) $esc = TRUE;
 
687
                                $new_mode = 1;
 
688
                                $str = chr($b1).chr($b[$i+1]);
 
689
                        }
 
690
                        $i++;
 
691
                } elseif ($mode == 2) {
 
692
                        if ($new_mode != 2) $esc = TRUE;
 
693
                        $new_mode = 2;
 
694
                        $str = chr($b1);
 
695
                } else {
 
696
                        if ($new_mode != 0) $esc = TRUE;
 
697
                        $new_mode = 0;
 
698
                        $str = chr($b1);
 
699
                }
 
700
 
 
701
                if ($esc) {  //add escape sequense
 
702
                        $str_HAN .= $ESC[$new_mode];
 
703
                        $esc = FALSE;
 
704
                }
 
705
                $str_HAN .= $str;
 
706
        }
 
707
 
 
708
        if ($new_mode != 0) $str_HAN .= $ESC[0];
 
709
 
 
710
        return $str_HAN;
 
711
}
 
712
 
 
713
/*************************************************************************/
 
714
 
 
715
function Jcode_is_EUC(&$b)
 
716
{
 
717
        $n = count($b);
 
718
        for ($i = 1; $i <= $n; $i++) {
 
719
                $b1 = $b[$i];
 
720
                if ($b1 == 0x8E) {
 
721
                        if (!isset($b[$i+1])) {
 
722
                                return FALSE;
 
723
                        }
 
724
                        $b2 = $b[$i+1] - 0x80;
 
725
                        if ((0x20 > $b2) || ($b2 > 0x7F)) {
 
726
                                return FALSE;
 
727
                        }
 
728
                        $i++;
 
729
                } elseif ($b1 > 0x8E) {
 
730
                        $b1 -= 0x80;
 
731
                        if ((0x20 > $b1) || ($b1 > 0x7F)) {
 
732
                                return FALSE;
 
733
                        }
 
734
                        if (!isset($b[$i+1])) {
 
735
                                return FALSE;
 
736
                        }
 
737
                        $b2 = $b[$i+1] - 0x80;
 
738
                        if ((0x20 > $b2) || ($b2 > 0x7F)) {
 
739
                                return FALSE;
 
740
                        }
 
741
                        $i++;
 
742
                } elseif ($b1 <= 0x7F){
 
743
                        continue;
 
744
                } else {
 
745
                        return FALSE;
 
746
                }
 
747
        }
 
748
        return TRUE;
 
749
}
 
750
 
 
751
function Jcode_is_SJIS(&$b)
 
752
{
 
753
        $n = count($b);
 
754
        for ($i = 1; $i <= $n; $i++) {
 
755
                $b1 = $b[$i];
 
756
                if (0xA1 <= $b1 && $b1 <= 0xDF) {
 
757
                        $b1 -= 0x80;
 
758
                        if ((0x20 > $b1) || ($b1 > 0x7F)) {
 
759
                                return FALSE;
 
760
                        }
 
761
                } elseif ($b1 >= 0x80) {
 
762
                        $b2 = $b[$i+1];
 
763
                        $b1 <<= 1;
 
764
                        if ($b2 < 0x9F) {
 
765
                                if ($b1 < 0x13F) $b1 -= 0xE1; else $b1 -= 0x61;
 
766
                                if ($b2 > 0x7E)  $b2 -= 0x20; else $b2 -= 0x1F;
 
767
                        } else {
 
768
                                if ($b1 < 0x13F) $b1 -= 0xE0; else $b1 -= 0x60;
 
769
                                $b2 -= 0x7E;
 
770
                        }
 
771
                        if (((0x20 > $b1) || ($b1 > 0x7F))
 
772
                                || ((0x20 > $b2) || ($b2 > 0x7F))) {
 
773
                                return FALSE;
 
774
                        }
 
775
                        $i++;
 
776
                } elseif ($b1 <= 0x7F){
 
777
                        continue;
 
778
                } else {
 
779
                        return FALSE;
 
780
                }
 
781
        }
 
782
 
 
783
        return TRUE;
 
784
}
 
785
 
 
786
function Jcode_is_JIS(&$str)
 
787
{
 
788
        return ereg("\x1B", $str);
 
789
}
 
790
 
 
791
function Jcode_is_ASCII(&$str)
 
792
{
 
793
        return !ereg("[\x80-\xFF]", $str);
 
794
}
 
795
?>
 
 
b'\\ No newline at end of file'