5 # Utilities for various character set issues. Currently handles ASCII and
6 # EBCDIC only. It is trivial to add support for new EBCDIC code pages (unless
7 # they have identical variant character signatures as existing ones, and there
8 # aren't other glitches that arise): just add a mapping table to
9 # %ebcdic_translations and regen everything that uses this.
11 my %ebcdic_translations = (
12 # Keys are code page name; values are arrays that map ASCII ordinals to
13 # the code page's ordinals
16 [ 0x00, 0x01, 0x02, 0x03, 0x37, 0x2D, 0x2E, 0x2F, 0x16, 0x05, 0x15, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
17 0x10, 0x11, 0x12, 0x13, 0x3C, 0x3D, 0x32, 0x26, 0x18, 0x19, 0x3F, 0x27, 0x1C, 0x1D, 0x1E, 0x1F,
18 0x40, 0x5A, 0x7F, 0x7B, 0x5B, 0x6C, 0x50, 0x7D, 0x4D, 0x5D, 0x5C, 0x4E, 0x6B, 0x60, 0x4B, 0x61,
19 0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0x7A, 0x5E, 0x4C, 0x7E, 0x6E, 0x6F,
20 0x7C, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6,
21 0xD7, 0xD8, 0xD9, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xAD, 0xE0, 0xBD, 0x5F, 0x6D,
22 0x79, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
23 0x97, 0x98, 0x99, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xC0, 0x4F, 0xD0, 0xA1, 0x07,
24 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x06, 0x17, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x09, 0x0A, 0x1B,
25 0x30, 0x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x9A, 0x8A, 0x5F, 0xCA, 0xAF, 0xBC,
67 0x90, 0x8F, 0xEA, 0xFA, 0xBE, 0xA0, 0xB6, 0xB3, 0x9D, 0xDA, 0x9B, 0x8B, 0xB7, 0xB8, 0xB9, 0xAB,
68 0x64, 0x65, 0x62, 0x66, 0x63, 0x67, 0x9E, 0x68, 0x74, 0x71, 0x72, 0x73, 0x78, 0x75, 0x76, 0x77,
69 0xAC, 0x69, 0xED, 0xEE, 0xEB, 0xEF, 0xEC, 0xBF, 0x80, 0xFD, 0xFE, 0xFB, 0xFC, 0xAD, 0xAE, 0x59,
70 0x44, 0x45, 0x42, 0x46, 0x43, 0x47, 0x9C, 0x48, 0x54, 0x51, 0x52, 0x53, 0x58, 0x55, 0x56, 0x57,
71 0x8C, 0x49, 0xCD, 0xCE, 0xCB, 0xCF, 0xCC, 0xE1, 0x70, 0xDD, 0xDE, 0xDB, 0xDC, 0x8D, 0x8E, 0xDF
75 my $ascii_key = 'ASCII/Latin1';
77 my %I8_TO_NATIVE_UTF8; # Maps I8 UTF to final UTF-EBCDIC
78 # See http://www.unicode.org/reports/tr16/
80 sub get_supported_code_pages() {
81 # Returns an ordered array of the currently supported code pages,
82 # including ASCII as the 0th element, 1047 as the 1th, and the others
83 # sorted lexically by code page name.
85 # Create an ASCII table.
86 unless (exists $ebcdic_translations{$ascii_key}) {
87 for my $i (0 .. 255) {
88 $ebcdic_translations{$ascii_key}->[$i] = $i;
102 } keys %ebcdic_translations;
106 # Returns the mapping array for ASCII to code page for the code page named
107 # by the input parameter.
111 if (! exists $ebcdic_translations{$charset}) {
112 die "Unknown character set '$charset'";
115 return @{$ebcdic_translations{$charset}};
118 sub get_I8_2_utf($) {
119 # Returns the mapping array for I8 to code page UTF-EBCDIC for the code
120 # page named by the input parameter. This is Table 2 of TR16 customized
121 # for the code page. See utfebcdic.h for why, contrary to TR16, it has to
122 # be code-page-specific.
126 die "I8 not a valid concept for ASCII" if $charset eq $ascii_key;
127 die "'$charset' unknown" unless exists $ebcdic_translations{$charset};
129 # Generate the table if not already present
130 if (! exists $I8_TO_NATIVE_UTF8{$charset}) {
132 # The code points not used for invariants. Initialized to everything,
133 # then entries are removed as we go along.
135 for my $i (0 .. 255) {
139 # These are the invariants. The output has them mapped to the
140 # original EBCDIC code point.
141 for my $i (0 .. 0x9F) {
143 my $ebcdic_value = $ebcdic_translations{$charset}[$i];
144 #printf "$charset: using %02x which is %02x ascii, %s\n", $ebcdic_value, $i, charnames::viacode($i);
145 $I8_TO_NATIVE_UTF8{$charset}[$i] = $ebcdic_value;
146 if (! defined delete $unused_cps{$ebcdic_value}) {
147 die "Two code points map to $ebcdic_value; one is $i";
151 # Put the unused code points in order
152 my @unused_cps = sort { $a <=> $b } keys %unused_cps;
154 # Fill in the rest of the map with these ordered code points, as TR16
156 for my $i (0xA0 .. 255) {
157 $I8_TO_NATIVE_UTF8{$charset}[$i] = shift @unused_cps;
158 #printf "$charset: filling in %02x which is %02x ascii, %s\n", $I8_TO_NATIVE_UTF8{$charset}[$i], $i, charnames::viacode($i);
162 die "Left-over code points";
166 return @{$I8_TO_NATIVE_UTF8{$charset}};
171 my $charset; # We use these to do some error checking that the #if and
172 # #endif are matched.
175 sub get_conditional_compile_line_start($;$) {
176 # Returns the '#if' line to put into C code to compile for the code
177 # page given by the first parameter. The second parameter, if
178 # present, is the indentation level, like '# if ...'
180 if (defined $charset || defined $indent) {
181 die "Missing call to get_conditional_compile_line_end()"
185 my $indent_level = shift // 0;
187 die "This is designed to run only on an ASCII platform" unless ord "A" == 65;
189 if ($indent_level == 0) {
193 $indent = " " x (($indent_level * 4) - 1);
196 die "Unknown character set '$charset'" unless exists $ebcdic_translations{$charset};
203 # We use all the typical variant characters to construct the #if,
204 # so that it is unlikely that a different code page will match
206 for my $char (qw/A \\\ [ ] { } ^ ~ ! # | $ @ `/) {
208 my $ascii_ord = ord $char;
209 my $first_time = $return eq "";
211 $compare = $ebcdic_translations{$charset}[$ascii_ord];
212 $return .= " && " unless $first_time;
213 $return .= "'$char' == $compare";
214 $return .= " /* $charset */" if $first_time;
215 last if $charset eq $ascii_key;
217 $return .= " \\\n " if $first_time || $count % 5 == 0;
221 return "#${indent}if $return\n";
224 sub get_conditional_compile_line_end () {
225 # Returns the #endif for the currently open #if
227 my $return = "#${indent}endif\t/* $charset */\n";
234 sub _UTF_START_MASK($) {
237 return ((($len) >= 6) ? 0x01 : (0x1F >> (($len)-2)));
240 sub _UTF_START_MARK($) {
242 return (0xFF & (0xFE << (7-(shift))));
245 sub cp_2_utfbytes($$) {
246 # Returns a string consisting of the UTF-EBCDIC for the code page given by
247 # the first parameter and the code point within it given by the 2nd, using
248 # the UTF-MOD algorithm published in TR16. (If the "code page" is ASCII,
249 # straight UTF-8 is returned.)
251 my ($ucp, $charset) = @_;
253 if ($charset eq $ascii_key) {
259 elsif (exists $ebcdic_translations{$charset}) {
262 return chr $ebcdic_translations{$charset}[$ucp];
265 my @I8_2_utf = get_I8_2_utf($charset);
267 my $len = $ucp < 0xA0 ? 1 :
271 $ucp < 0x400000 ? 5 :
272 $ucp < 0x4000000 ? 6 : 7;
275 for (1 .. $len - 1) {
276 unshift @str, chr $I8_2_utf[($ucp & 0x1f) | 0xA0];
280 unshift @str, chr $I8_2_utf[($ucp & _UTF_START_MASK($len)) | _UTF_START_MARK($len)];
282 return join "", @str;
285 die "Unknown character set '$charset'";