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
|
#!/opt/bin/perl
# the generated tables mostly have NOT been checked so far!
use v5.8.2;
use utf8;
use Encode;
use Encode::JP;
use Encode::CN;
use Encode::KR;
use Encode::HanExtra;
use Encode::JIS2K;
my $gen;
$TO_UNICODE = 0; # also generate to_unicode tables
sub linear {
my ($a, $l, $h, $b) = @_;
for ($l .. $h) {
return unless defined $a->[$_] && $a->[$_] == $_ + ($b - $l);
}
1;
}
sub wrap {
my $res = " ";
$res .= "$1\n " while $_[0] =~ /\G(.{90}\S*?)\s+/gc;
$res .= "$1" if $_[0] =~ /\G(.*)$/;
$res;
}
my $last_tab_full; # hack
sub gentab {
my ($enc, $l, $h, $f, $rep) = @_;
$last_tab_full = 0;
" = {\n"
. (wrap join ", ",
map +(sprintf "0x$f",
defined $enc->[$_] ? $enc->[$_] : $last_tab_full++ * 0 + $rep
), $l..$h)
. "\n};\n";
}
sub gen {
my ($enc, $base, $ch) = @_;
my ($fun, $tab);
my (@t1, @t2);
for (255, 159, 127, 126, 125) {
if (linear $enc, 0, $_, 0) {
undef $enc->[$_] for 0..$_;
$fun .= sprintf "if (· <= 0x%04x) return ·;\n", $_;
}
}
for (126, 127, 128) {
if (linear $enc, $_, 159, $_) {
undef $enc->[$_] for $_..159;
$fun .= sprintf "if (0x%04x <= · && · <= 0x%04x) return ·;\n", $_, 159;
}
}
for (126, 127) {
next unless defined $enc->[$_];
$fun .= sprintf "if (· == 0x%04x) return 0x%04x;\n", $_, delete $enc->[$_];
}
my @map;
my @map2;
for (0 .. $#$enc) {
if (defined $enc->[$_]) {
$map[$_] = $enc->[$_];
$map2[$_ >> 8]++;
}
}
for my $p (0..255) {
if ($map2[$p]) {
my $b = $p << 8;
my ($l, $h);
for my $i (0..255) { $l = $i, last if defined $map[$b + $i]; }
for my $i (0..255) { $h = 255 - $i, last if defined $map[$b + 255 - $i]; }
if ($map2[$p] <= 5) {
for ($l .. $h) {
next unless defined $enc->[$b + $_];
$fun .= sprintf "if (· == 0x%04x) return 0x%04x;\n", $b + $_, $enc->[$b + $_];
}
} elsif (linear $enc, $b + $l, $b + $h, $enc->[$b + $l]) {
my $dif = $enc->[$b + $l] - ($b + $l);
$dif = $dif < 0 ? sprintf "- 0x%04x", -$dif : sprintf "+ 0x%04x", $dif;
$fun .= sprintf "if (· >= 0x%04x && 0x%04x >= ·) return · %s;\n", $b + $l, $b + $h, $dif;
} elsif ($map2[$p] <= 5) { # defunct
$fun .= "switch (·)\n {\n";
for ($l .. $h) {
next unless defined $enc->[$b + $_];
$fun .= sprintf " case 0x%04x: return 0x%04x;\n", $b + $_, $enc->[$b + $_];
}
$fun .= " }\n";
} else {
my ($i, $t, $f) = (0, "uint8_t", "%02x");
for ($l .. $h) {
if ($enc->[$b + $_] > 255) {
($i, $t, $f) = (1, "uint16_t", "%04x");
last;
}
}
$i ? push @t2, [$p, $l, $h, $t, $f]
: push @t1, [$b, $l, $h, $t, $f];
}
}
}
if (@t2 <= 3) {
push @t1, @t2;
@t2 = ();
}
for (@t1) {
my ($b, $l, $h, $t, $f) = @$_;
my $rep = $b + $l == 0 ? 128 : 0;
$tab .= "static const $t $base\_$b\[]" . gentab $enc, $b + $l, $b + $h, $f;
$fun .= sprintf "if (0x%04x <= · && · <= 0x%04x)\n"
. " return %s$base\_$b\[· - 0x%04x];\n",
$b + $l, $b + $h,
($last_tab_full ? sprintf "$base\_$b\[· - 0x%04x] == $rep ? NOCHAR : ", $b + $l : ""),
$b + $l;
}
if (@t2) {
my ($min, $max) = (255, 0);
my ($l, $h) = (255, 0);
for (@t2) {
my ($p, $L, $H, $t, $f) = @$_;
$max = $p if $p > $max;
$min = $p if $p < $min;
$l = $L if $L < $l;
$h = $H if $H > $h;
}
$fun .= "uint8_t l = ·;\n";
$fun .= "uint16_t h = · >> 8;\n";
if ($h - $l < 200) {
my $d = $h - $l + 1;
my @enc;
for $p ($min .. $max) {
for $i ($l .. $h) {
$enc[($p - $min) * $d + $i - $l] = $enc->[$p * 256 + $i];
}
}
$tab .= "static const uint16_t $base\_m[]" . gentab \@enc, 0, $#enc, "%04x";
if ($last_tab_full) {
$fun .= sprintf "if (0x%02x <= h && h <= 0x%02x\n"
. " && 0x%02x <= l && l <= 0x%02x)\n"
. " return $base\_m\[h * 0x%02x + l - 0x%04x]\n"
. " ? $base\_m\[h * 0x%02x + l - 0x%04x]\n"
. " : NOCHAR;\n",
$min, $max, $l, $h,
($d, $min * $d + $l) x 2;
} else {
$fun .= sprintf "if (0x%02x <= h && h <= 0x%02x\n"
. " && 0x%02x <= l && l <= 0x%02x)\n"
. " return $base\_m\[h * 0x%02x + l - 0x%04x];\n",
$min, $max, $l, $h,
$d,
$min * $d + $l;
}
} else {
my @tab = (0) x ($max - $min);
for (@t2) {
my ($p, undef, undef, $t, $f) = @$_;
$tab .= "static const $t $base\_$p\[]" . gentab $enc, $p * 256 + $l, $p * 256 + $h, $f, 0;
$tab[$p - $min] = "$base\_$p";
}
$tab .= "const uint16_t *$base\_i[] = {\n"
. (wrap join ", ", @tab)
. "\n};\n\n";
$fun .= sprintf "if (0x%02x <= h && h <= 0x%02x\n"
. " && 0x%02x <= l && l <= 0x%02x\n"
. " && $base\_i[h - 0x%02x])\n"
. " return $base\_i\[h - 0x%02x][l - 0x%02x]\n"
. " ? $base\_i\[h - 0x%02x][l - 0x%02x]\n"
. " : NOCHAR;\n",
$min, $max, $l, $h,
$min, ($min, $l) x 2;
}
}
$fun .= "return NOCHAR;\n";
$fun =~ s/·/$ch/g;
($tab, $fun);
}
while (<DATA>) {
my ($group, $base, $cs, $type) = split /\s+/;
my @enc1;
my @enc2;
for (0 .. 65535) {
my $enc = encode $cs, (chr $_), Encode::FB_QUIET;
if (length $enc) {
my $code = hex unpack "H*", $enc;
$enc1[$_] = $code;
$enc2[$code] = $_ unless defined $enc2[$code];
}
}
my ($tab1, $fun1);
my ($tab2, $fun2);
($tab1, $fun1) = gen \@enc1, "$base\_f", "unicode";
($tab2, $fun2) = gen \@enc2, "$base\_t", "enc" if $TO_UNICODE;
$fun1 =~ s/^/ /gm;
$fun2 =~ s/^/ /gm;
print "$base\n";
open OUT, ">table/$base.h" or die;
print OUT <<EOF;
//
// AUTOMATICALLLY GENERATED by gentables
//
#ifdef ENCODING_$group
$tab1$tab2
static uint32_t cs_$base\_from_unicode (unicode_t unicode) {
$fun1}
EOF
if ($TO_UNICODE) {
print OUT <<EOF
#if ENCODING_TO_UNICODE
static unicode_t cs_$base\_to_unicode (uint32_t enc) {
$fun2}
#endif
EOF
}
print OUT <<EOF;
#else
#define cs_$base\_from_unicode cs_unknown_from_unicode
#define cs_$base\_to_unicode cs_unknown_to_unicode
#endif
EOF
close OUT;
}
__DATA__
VN viscii viscii
KR ksc5601_1987_0 ksc5601-raw
ZH gb2312_1980_0 gb2312-raw
ZH gbk_0 gbk
ZH_EXT cns11643_1992_1 cns11643-1
ZH_EXT cns11643_1992_2 cns11643-2
ZH_EXT cns11643_1992_3 cns11643-3
ZH_EXT cns11643_1992_4 cns11643-4
ZH_EXT cns11643_1992_5 cns11643-5
ZH_EXT cns11643_1992_6 cns11643-6
ZH_EXT cns11643_1992_7 cns11643-7
ZH_EXT cns11643_1992_f cns11643-f
ZH big5 big5
ZH_EXT big5_ext big5ext
ZH_EXT big5_plus big5plus
EU koi8_r koi8-r
EU koi8_u koi8-u
DEFAULT iso8859_1 iso-8859-1
EU iso8859_2 iso-8859-2
EU iso8859_3 iso-8859-3
EU iso8859_4 iso-8859-4
EU iso8859_5 iso-8859-5
EU iso8859_6 iso-8859-6
EU iso8859_7 iso-8859-7
EU iso8859_8 iso-8859-8
EU iso8859_9 iso-8859-9
EU iso8859_10 iso-8859-10
EU iso8859_11 iso-8859-11
EU iso8859_13 iso-8859-13
EU iso8859_14 iso-8859-14
DEFAULT iso8859_15 iso-8859-15
EU iso8859_16 iso-8859-16
JP jis0201_1976_0 jis0201-raw
JP jis0208_1990_0 jis0208-raw
JP jis0212_1990_0 jis0212-raw
JP_EXT jis0213_1 jis0213-1-raw
JP_EXT jis0213_2 jis0213-2-raw
|