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
|
#
# Copyright (c) 2001-2017, PostgreSQL Global Development Group
#
# src/backend/utils/mb/Unicode/convutils.pm
use strict;
#######################################################################
# convert UCS-4 to UTF-8
#
sub ucs2utf
{
my ($ucs) = @_;
my $utf;
if ($ucs <= 0x007f)
{
$utf = $ucs;
}
elsif ($ucs > 0x007f && $ucs <= 0x07ff)
{
$utf = (($ucs & 0x003f) | 0x80) | ((($ucs >> 6) | 0xc0) << 8);
}
elsif ($ucs > 0x07ff && $ucs <= 0xffff)
{
$utf =
((($ucs >> 12) | 0xe0) << 16) |
(((($ucs & 0x0fc0) >> 6) | 0x80) << 8) | (($ucs & 0x003f) | 0x80);
}
else
{
$utf =
((($ucs >> 18) | 0xf0) << 24) |
(((($ucs & 0x3ffff) >> 12) | 0x80) << 16) |
(((($ucs & 0x0fc0) >> 6) | 0x80) << 8) | (($ucs & 0x003f) | 0x80);
}
return ($utf);
}
#######################################################################
# read_source - common routine to read source file
#
# fname ; input file name
sub read_source
{
my ($fname) = @_;
my @r;
open(my $in, '<', $fname) || die("cannot open $fname");
while (<$in>)
{
next if (/^#/);
chop;
next if (/^$/); # Ignore empty lines
next if (/^0x([0-9A-F]+)\s+(#.*)$/);
# Skip the first column for JIS0208.TXT
if (!/^0x([0-9A-Fa-f]+)\s+0x([0-9A-Fa-f]+)\s+(?:0x([0-9A-Fa-f]+)\s+)?(#.*)$/)
{
print STDERR "READ ERROR at line $. in $fname: $_\n";
exit;
}
my $out = {f => $fname, l => $.,
code => hex($1),
ucs => hex($2),
comment => $4,
direction => "both"
};
# Ignore pure ASCII mappings. PostgreSQL character conversion code
# never even passes these to the conversion code.
next if ($out->{code} < 0x80 || $out->{ucs} < 0x80);
push(@r, $out);
}
close($in);
return \@r;
}
##################################################################
# print_tables : output mapping tables
#
# Arguments:
# charset - string name of the character set.
# table - mapping table (see format below)
# verbose - if 1, output comment on each line,
# if 2, also output source file name and number
#
#
#
# Mapping table format:
#
# Mapping table is a list of hashes. Each hash has the following fields:
# direction - Direction: 'both', 'from_unicode' or 'to_unicode'
# ucs - Unicode code point
# ucs_second - Second Unicode code point, if this is a "combined" character.
# code - Byte sequence in the "other" character set, as an integer
# comment - Text representation of the character
# f - Source filename
# l - Line number in source file
#
#
sub print_tables
{
my ($charset, $table, $verbose) = @_;
# Build an array with only the to-UTF8 direction mappings
my @to_unicode;
my @to_unicode_combined;
my @from_unicode;
my @from_unicode_combined;
foreach my $i (@$table)
{
if (defined $i->{ucs_second})
{
my $entry = {utf8 => ucs2utf($i->{ucs}),
utf8_second => ucs2utf($i->{ucs_second}),
code => $i->{code},
comment => $i->{comment},
f => $i->{f}, l => $i->{l}};
if ($i->{direction} eq "both" || $i->{direction} eq "to_unicode")
{
push @to_unicode_combined, $entry;
}
if ($i->{direction} eq "both" || $i->{direction} eq "from_unicode")
{
push @from_unicode_combined, $entry;
}
}
else
{
my $entry = {utf8 => ucs2utf($i->{ucs}),
code => $i->{code},
comment => $i->{comment},
f => $i->{f}, l => $i->{l}};
if ($i->{direction} eq "both" || $i->{direction} eq "to_unicode")
{
push @to_unicode, $entry;
}
if ($i->{direction} eq "both" || $i->{direction} eq "from_unicode")
{
push @from_unicode, $entry;
}
}
}
print_to_utf8_map($charset, \@to_unicode, $verbose);
print_to_utf8_combined_map($charset, \@to_unicode_combined, $verbose) if (scalar @to_unicode_combined > 0);
print_from_utf8_map($charset, \@from_unicode, $verbose);
print_from_utf8_combined_map($charset, \@from_unicode_combined, $verbose) if (scalar @from_unicode_combined > 0);
}
sub print_from_utf8_map
{
my ($charset, $table, $verbose) = @_;
my $last_comment = "";
my $fname = lc("utf8_to_${charset}.map");
print "- Writing UTF8=>${charset} conversion table: $fname\n";
open(my $out, '>', $fname) || die "cannot open output file : $fname\n";
printf($out "/* src/backend/utils/mb/Unicode/$fname */\n\n".
"static const pg_utf_to_local ULmap${charset}[ %d ] = {",
scalar(@$table));
my $first = 1;
foreach my $i (sort {$$a{utf8} <=> $$b{utf8}} @$table)
{
print($out ",") if (!$first);
$first = 0;
print($out "\t/* $last_comment */") if ($verbose);
printf($out "\n {0x%04x, 0x%04x}", $$i{utf8}, $$i{code});
if ($verbose >= 2)
{
$last_comment = "$$i{f}:$$i{l} $$i{comment}";
}
else
{
$last_comment = $$i{comment};
}
}
print($out "\t/* $last_comment */") if ($verbose);
print $out "\n};\n";
close($out);
}
sub print_from_utf8_combined_map
{
my ($charset, $table, $verbose) = @_;
my $last_comment = "";
my $fname = lc("utf8_to_${charset}_combined.map");
print "- Writing UTF8=>${charset} conversion table: $fname\n";
open(my $out, '>', $fname) || die "cannot open output file : $fname\n";
printf($out "/* src/backend/utils/mb/Unicode/$fname */\n\n".
"static const pg_utf_to_local_combined ULmap${charset}_combined[ %d ] = {",
scalar(@$table));
my $first = 1;
foreach my $i (sort {$$a{utf8} <=> $$b{utf8}} @$table)
{
print($out ",") if (!$first);
$first = 0;
print($out "\t/* $last_comment */") if ($verbose);
printf($out "\n {0x%08x, 0x%08x, 0x%04x}", $$i{utf8}, $$i{utf8_second}, $$i{code});
$last_comment = "$$i{comment}";
}
print($out "\t/* $last_comment */") if ($verbose);
print $out "\n};\n";
close($out);
}
sub print_to_utf8_map
{
my ($charset, $table, $verbose) = @_;
my $last_comment = "";
my $fname = lc("${charset}_to_utf8.map");
print "- Writing ${charset}=>UTF8 conversion table: $fname\n";
open(my $out, '>', $fname) || die "cannot open output file : $fname\n";
printf($out "/* src/backend/utils/mb/Unicode/${fname} */\n\n".
"static const pg_local_to_utf LUmap${charset}[ %d ] = {",
scalar(@$table));
my $first = 1;
foreach my $i (sort {$$a{code} <=> $$b{code}} @$table)
{
print($out ",") if (!$first);
$first = 0;
print($out "\t/* $last_comment */") if ($verbose);
printf($out "\n {0x%04x, 0x%x}", $$i{code}, $$i{utf8});
if ($verbose >= 2)
{
$last_comment = "$$i{f}:$$i{l} $$i{comment}";
}
else
{
$last_comment = $$i{comment};
}
}
print($out "\t/* $last_comment */") if ($verbose);
print $out "\n};\n";
close($out);
}
sub print_to_utf8_combined_map
{
my ($charset, $table, $verbose) = @_;
my $last_comment = "";
my $fname = lc("${charset}_to_utf8_combined.map");
print "- Writing ${charset}=>UTF8 conversion table: $fname\n";
open(my $out, '>', $fname) || die "cannot open output file : $fname\n";
printf($out "/* src/backend/utils/mb/Unicode/${fname} */\n\n".
"static const pg_local_to_utf_combined LUmap${charset}_combined[ %d ] = {",
scalar(@$table));
my $first = 1;
foreach my $i (sort {$$a{code} <=> $$b{code}} @$table)
{
print($out ",") if (!$first);
$first = 0;
print($out "\t/* $last_comment */") if ($verbose);
printf($out "\n {0x%04x, 0x%08x, 0x%08x}", $$i{code}, $$i{utf8}, $$i{utf8_second});
$last_comment = "$$i{comment}";
}
print($out "\t/* $last_comment */") if ($verbose);
print $out "\n};\n";
close($out);
}
1;
|