6 BEGIN { unshift @INC, '.' }
8 require './regen/regen_lib.pl';
9 require './regen/charset_translations.pl';
11 # Generates the EBCDIC translation tables that were formerly hard-coded into
14 my $out_fh = open_new('ebcdic_tables.h', '>',
15 {style => '*', by => $0, });
17 sub get_column_headers ($$;$) {
18 my ($row_hdr_len, $field_width, $dfa_columns) = @_;
20 my $final_column_format;
23 if (defined $dfa_columns) {
24 $num_columns = $dfa_columns;
26 # Trailing blank to correspond with commas in the rows below
27 $format = "%${field_width}d ";
29 else { # Is a regular table
32 # Use blanks to separate the fields
33 $format = " " x ( $field_width
35 $format .= "_%X "; # Again, trailing blank over the commas below
38 my $header = "/*" . " " x ($row_hdr_len - length "/*");
40 # All but the final column
41 $header .= sprintf($format, $_) for 0 .. $num_columns - 2;
43 # Get rid of trailing blank, so that the final column takes up one less
44 # space so that the "*/" doesn't extend past the commas in the rows below
46 $header .= sprintf $format, $num_columns - 1;
48 # Again, remove trailing blank
51 return $header . "*/\n";
54 sub output_table_start($$$;$) {
55 my ($out_fh, $TYPE, $name, $size) = @_;
57 $size = "" unless defined $size;
59 # Anything locale related will be written on
60 my $const = ($name !~ /locale/i) ? 'CONST' : "";
62 my $declaration = "EXT$const $TYPE $name\[$size\]";
71 sub output_table_end($) {
72 print $out_fh "};\n# endif\n\n";
75 sub output_table ($$;$) {
76 my $table_ref = shift;
79 my $size = @$table_ref;
81 # 0 => print in decimal
82 # 1 => print in hex (translates code point to code point)
83 # >= 2 => is a dfa table, like https://bjoern.hoehrmann.de/utf-8/decoder/dfa/
84 # The number is how many columns in the part after the code point
87 # code point tables in hex areasier to debug, but don't fit into 80
89 my $type = shift // 1;
91 my $print_in_hex = $type == 1;
92 my $is_dfa = ($type >= 2) ? $type : 0;
93 my $columns_after_256 = 16;
95 die "Requres 256 entries in table $name, got @$table_ref"
96 if ! $is_dfa && @$table_ref != 256;
98 die "Requres 256 entries in table $name, got @$table_ref"
99 if @$table_ref != 256;
102 $columns_after_256 = $is_dfa;
104 print $out_fh <<'EOF';
106 /* The table below is adapted from
107 * https://bjoern.hoehrmann.de/utf-8/decoder/dfa/
108 * See copyright notice at the beginning of this file.
114 # Highest number in the table
116 $max_entry = map { $_ > $max_entry ? $_ : $max_entry } @$table_ref;
118 # We assume that every table has at least one two digit entry, and none
119 # are more than three digit.
120 my $field_width = ($print_in_hex)
122 : (($max_entry) > 99 ? 3 : 2);
125 my $node_number_field_width;
126 my $node_value_field_width;
128 # dfa tables have a special header for the rows in the transitions part of
129 # the table. It is longer than the regular one.
131 my $max_node_number = ($max_entry - 256) / $columns_after_256 - 1;
132 $node_number_field_width = ($max_node_number > 9) ? 2 : 1;
133 $node_value_field_width = ($max_node_number * $columns_after_256 > 99)
135 # The header starts with this template, and adds in the number of
136 # digits needed to represent the maximum node number and its value
137 $row_hdr_length = length("/*N=*/")
138 + $node_number_field_width
139 + $node_value_field_width;
142 $row_hdr_length = length "/*_X*/"; # Template for what the header
146 # The table may not be representable in 8 bits.
148 $TYPE = 'U16' if grep { $_ > 255 } @$table_ref;
150 output_table_start $out_fh, $TYPE, $name, $size;
152 # First the headers for the columns
153 print $out_fh get_column_headers($row_hdr_length, $field_width);
156 my $count = @$table_ref;
159 # Print each element individually, arranged in rows of columns
160 for my $i (0 .. $count - 1) {
162 # Node number for here is -1 until get into the dfa state transitions
163 my $node = ($i < 256) ? -1 : ($i - 256) / $columns_after_256;
165 # Print row header at beginning of each row
168 printf $out_fh "/*N%-*d=%*d*/", $node_number_field_width, $node,
169 $node_value_field_width, $i - 256;
171 else { # Otherwise is regular row; print its number
172 printf $out_fh "/*%X_", $i / 16;
174 # These rows in a dfa table require extra space so columns
175 # will align vertically (because the Ndd=ddd requires extra
178 print $out_fh " " x ( $node_number_field_width
179 + $node_value_field_width);
186 printf $out_fh "0x%02X", $table_ref->[$i];
189 printf $out_fh "%${field_width}d", $table_ref->[$i];
192 print $out_fh ",", if $i < $count -1; # No comma on final entry
194 # Add \n if at end of row, which is 16 columns until we get to the
196 if ( ($node < 0 && $i % 16 == 15)
197 || ($node >= 0 && ($i -256) % $columns_after_256
198 == $columns_after_256 - 1))
208 # Print column footer
209 print $out_fh get_column_headers($row_hdr_length, $field_width,
210 ($is_dfa) ? $columns_after_256 : undef);
212 output_table_end($out_fh);
215 print $out_fh <<'END';
217 #ifndef PERL_EBCDIC_TABLES_H_ /* Guard against nested #includes */
218 #define PERL_EBCDIC_TABLES_H_ 1
220 /* This file contains definitions for various tables used in EBCDIC handling.
221 * More info is in utfebcdic.h
223 * Some of the tables are adapted from
224 * https://bjoern.hoehrmann.de/utf-8/decoder/dfa/
225 * which requires this copyright notice:
227 Copyright (c) 2008-2009 Bjoern Hoehrmann <bjoern@hoehrmann.de>
229 Permission is hereby granted, free of charge, to any person obtaining a copy of
230 this software and associated documentation files (the "Software"), to deal in
231 the Software without restriction, including without limitation the rights to
232 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
233 of the Software, and to permit persons to whom the Software is furnished to do
234 so, subject to the following conditions:
236 The above copyright notice and this permission notice shall be included in all
237 copies or substantial portions of the Software.
239 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
240 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
241 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
242 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
243 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
244 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
250 my @charsets = get_supported_code_pages();
251 shift @charsets; # ASCII is the 0th, and we don't deal with that here.
252 foreach my $charset (@charsets) {
253 # we process the whole array several times, make a copy
254 my @a2e = @{get_a2n($charset)};
257 print $out_fh "\n" . get_conditional_compile_line_start($charset);
260 print $out_fh "/* Index is ASCII platform code point; value is $charset equivalent */\n";
261 output_table(\@a2e, "PL_a2e");
263 { # Construct the inverse
264 for my $i (0 .. 255) {
267 print $out_fh "/* Index is $charset code point; value is ASCII platform equivalent */\n";
268 output_table(\@e2a, "PL_e2a");
271 my @i82utf = @{get_I8_2_utf($charset)};
273 /* (Confusingly named) Index is $charset I8 byte; value is
274 * $charset UTF-EBCDIC equivalent */
276 output_table(\@i82utf, "PL_utf2e");
278 { #Construct the inverse
280 for my $i (0 .. 255) {
281 $utf2i8[$i82utf[$i]] = $i;
284 /* (Confusingly named) Index is $charset UTF-EBCDIC byte; value is
285 * $charset I8 equivalent */
287 output_table(\@utf2i8, "PL_e2utf");
293 # These are invariants or continuation bytes.
294 for my $i (0 .. 0xBF) {
295 $utf8skip[$i82utf[$i]] = 1;
298 # These are start bytes; The skip is the number of consecutive highest
299 # order 1-bits (up to 7)
300 for my $i (0xC0 .. 255) {
302 if ($i == 0b11111111) {
304 $count = $CHARSET_TRANSLATIONS::UTF_EBCDIC_MAXBYTES;
306 elsif (($i & 0b11111110) == 0b11111110) {
309 elsif (($i & 0b11111100) == 0b11111100) {
312 elsif (($i & 0b11111000) == 0b11111000) {
315 elsif (($i & 0b11110000) == 0b11110000) {
318 elsif (($i & 0b11100000) == 0b11100000) {
321 elsif (($i & 0b11000000) == 0b11000000) {
325 die "Something wrong for UTF8SKIP calculation for $i";
327 $utf8skip[$i82utf[$i]] = $count;
331 /* Index is $charset UTF-EBCDIC byte; value is UTF8SKIP for start bytes
332 * (including for overlongs); 1 for continuation. Adapted from the shadow
333 * flags table in tr16. The entries marked 9 in tr16 are continuation bytes
334 * and are marked as length 1 here so that we can recover. */
336 output_table(\@utf8skip, "PL_utf8skip", 0); # The 0 means don't print
340 use feature 'unicode_strings';
344 for my $i (0 .. 255) {
345 $lc[$a2e[$i]] = $a2e[ord lc chr $i];
348 "/* Index is $charset code point; value is its lowercase equivalent */\n";
349 output_table(\@lc, "PL_latin1_lc");
354 for my $i (0 .. 255) {
356 if (length $uc > 1 || ord $uc > 255) {
357 $uc = "\N{LATIN SMALL LETTER Y WITH DIAERESIS}";
359 $uc[$a2e[$i]] = $a2e[ord $uc];
362 /* Index is $charset code point; value is its uppercase equivalent.
363 * The 'mod' in the name means that codepoints whose uppercase is above 255 or
364 * longer than 1 character map to LATIN SMALL LETTER Y WITH DIARESIS */
366 output_table(\@uc, "PL_mod_latin1_uc");
371 for my $i (0 .. 255) { # Initialise to identity map
372 $ascii_fold[$i] = $i;
375 # Overwrite the entries that aren't identity
376 for my $chr ('A' .. 'Z') {
377 $ascii_fold[$a2e[ord $chr]] = $a2e[ord lc $chr];
379 for my $chr ('a' .. 'z') {
380 $ascii_fold[$a2e[ord $chr]] = $a2e[ord uc $chr];
383 /* Index is $charset code point; For A-Z, value is a-z; for a-z, value
384 * is A-Z; all other code points map to themselves */
386 output_table(\@ascii_fold, "PL_fold");
388 # This table is also the correct folding for the default C locale
389 output_table(\@ascii_fold, "PL_fold_locale");
394 for my $i (0 .. 255) {
398 # lc and uc adequately proxy for fold-case pairs in this 0-255
401 $uc = $char if length $uc > 1 || ord $uc > 255;
403 $latin1_fold[$a2e[$i]] = $a2e[ord $lc];
405 elsif ($uc ne $char) {
406 $latin1_fold[$a2e[$i]] = $a2e[ord $uc];
409 $latin1_fold[$a2e[$i]] = $a2e[$i];
413 /* Index is $charset code point; value is its other fold-pair equivalent
414 * (A => a; a => A, etc) in the 0-255 range. If no such equivalent, value is
415 * the code point itself */
417 output_table(\@latin1_fold, "PL_fold_latin1");
421 # This generates the dfa table for perl extended UTF-8, which accepts
422 # surrogates, non-characters, and accepts start bytes up through FE
423 # (start byte FF has to be handled outside this dfa). The class numbers
424 # for start bytes are constrained so that they can be used as a shift
425 # count for masking off the leading one bits
429 # A0-A1 7 Not legal immediately after start bytes F0 F8 FC
431 # A2-A3 8 Not legal immediately after start bytes F0 F8 FC
432 # A4-A7 9 Not legal immediately after start bytes F0 F8
433 # A8-AF 10 Not legal immediately after start bytes F0
448 # Here's the I8 for the code points before which overlongs occur:
449 # U+4000: \xF0\xB0\xA0\xA0
450 # U+40000: \xF8\xA8\xA0\xA0\xA0
451 # U+400000: \xFC\xA4\xA0\xA0\xA0\xA0
452 # U+4000000: \xFE\xA2\xA0\xA0\xA0\xA0\xA0
454 # The first part of the table maps bytes to character classes to reduce
455 # the size of the transition table and create bitmasks.
457 # The second part is a transition table that maps a combination of a
458 # state of the automaton and a character class to a new state. The
459 # numbering of the original nodes is retained, but some have been split
460 # so that there are new nodes. They mean:
461 # N0 The initial state, and final accepting one.
462 # N1 One continuation byte (A0-BF) left. This is transitioned to
463 # immediately when the start byte indicates a two-byte sequence
464 # N2 Two continuation bytes left.
465 # N3 Three continuation bytes left.
466 # N4 Four continuation bytes left.
467 # N5 Five continuation bytes left.
468 # N6 Start byte is F0. Continuation bytes A[0-F] are illegal
469 # (overlong); the other continuations transition to N2
470 # N7 Start byte is F8. Continuation bytes A[0-7] are illegal
471 # (overlong); the other continuations transition to N3
472 # N8 Start byte is FC. Continuation bytes A[0-3] are illegal
473 # (overlong); the other continuations transition to N4
474 # N9 Start byte is FE. Continuation bytes A[01] are illegal
475 # (overlong); the other continuations transition to N5
476 # 1 Reject. All transitions not mentioned above (except the single
477 # byte ones (as they are always legal) are to this state.
479 my $NUM_CLASSES = 16;
481 my $N1 = $N0 + $NUM_CLASSES;
482 my $N2 = $N1 + $NUM_CLASSES;
483 my $N3 = $N2 + $NUM_CLASSES;
484 my $N4 = $N3 + $NUM_CLASSES;
485 my $N5 = $N4 + $NUM_CLASSES;
486 my $N6 = $N5 + $NUM_CLASSES;
487 my $N7 = $N6 + $NUM_CLASSES;
488 my $N8 = $N7 + $NUM_CLASSES;
489 my $N9 = $N8 + $NUM_CLASSES;
490 my $N10 = $N9 + $NUM_CLASSES;
492 my @perl_extended_utf8_dfa;
494 # 0 1 2 3 4 5 6 7 8 9 A B C D E F
495 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, # 00-0F
496 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, # 10-1F
497 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, # 20-2F
498 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, # 30-3F
499 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, # 40-4F
500 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, # 50-5F
501 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, # 60-6F
502 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, # 70-7F
503 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, # 80-8F
504 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, # 90-9F
505 7, 7, 8, 8, 9, 9, 9, 9,10,10,10,10,10,10,10,10, # A0-AF
506 11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11, # B0-BF
507 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, # C0-CF
508 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, # D0-DF
509 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, # E0-EF
510 12, 4, 4, 4, 4, 4, 4, 4,13, 5, 5, 5,14, 6,15, 1, # F0-FF
512 $perl_extended_utf8_dfa[$i82utf[$_]] = $i8[$_] for (0 .. 255);
513 push @perl_extended_utf8_dfa, (
515 # 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
516 0, 1,$N1,$N2,$N3,$N4,$N5, 1, 1, 1, 1, 1,$N6,$N7,$N8,$N9, # N0
517 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, # N1
518 1, 1, 1, 1, 1, 1, 1,$N1,$N1,$N1,$N1,$N1, 1, 1, 1, 1, # N2
519 1, 1, 1, 1, 1, 1, 1,$N2,$N2,$N2,$N2,$N2, 1, 1, 1, 1, # N3
520 1, 1, 1, 1, 1, 1, 1,$N3,$N3,$N3,$N3,$N3, 1, 1, 1, 1, # N4
521 1, 1, 1, 1, 1, 1, 1,$N4,$N4,$N4,$N4,$N4, 1, 1, 1, 1, # N5
523 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,$N2, 1, 1, 1, 1, # N6
524 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,$N3,$N3, 1, 1, 1, 1, # N7
525 1, 1, 1, 1, 1, 1, 1, 1, 1,$N4,$N4,$N4, 1, 1, 1, 1, # N8
526 1, 1, 1, 1, 1, 1, 1, 1,$N5,$N5,$N5,$N5, 1, 1, 1, 1, # N9
528 output_table(\@perl_extended_utf8_dfa, "PL_extended_utf8_dfa_tab",
533 # This generates the dfa table for strict UTF-8, which rejects
534 # surrogates, non-characters, and above Unicode.
537 # 00-9F 0 Always legal at start
538 # A0 10 Not legal immediately after start bytes F0 F8
539 # A1 11 Not legal immediately after start bytes F0 F8,
540 # A2-A7 12 Not legal immediately after start bytes F0 F8 F9
541 # A8,AA,AC 13 Not legal immediately after start bytes F0 F9
542 # A9,AB,AD 14 Not legal immediately after start byte F0
543 # AE 15 Not legal immediately after start byte F0
544 # AF 16 Not legal immediately after start bytes F0
545 # B[0248AC] 17 Not legal immediately after start byte F9
546 # B[1359D] 18 Not legal immediately after start byte F9
547 # B6 19 Not legal immediately after start byte F9
548 # B7 20 Not legal immediately after start byte F9
549 # BE 21 Not legal immediately after start byte F9
550 # BF 22 Not legal immediately after start byte F9
551 # C0-C4 1 (reject, all are overlong)
552 # C5-DF 2 Accepts any legal continuation
553 # E0 1 (reject, all are overlong)
554 # E1-EF 3 Accepts any legal continuation
555 # F0 8 (has overlongs)
556 # F1 6 (has surrogates, non-chars)
557 # F2,F4,F6 4 Accepts any legal continuation
558 # F3,F5,F7 5 (has non-chars)
559 # F8 9 (has overlongs, non-chars)
560 # F9 7 (has non-chars, non-Unicode)
561 # FA-FF 1 (reject, all are non-Unicode)
563 # Here's the I8 for enough code points so that you can figure out what's
566 # U+D800: \xF1\xB6\xA0\xA0
567 # U+DFFF: \xF1\xB7\xBF\xBF
568 # U+FDD0: \xF1\xBF\xAE\xB0
569 # U+FDEF: \xF1\xBF\xAF\xAF
570 # U+FFFE: \xF1\xBF\xBF\xBE
571 # U+1FFFE: \xF3\xBF\xBF\xBE
572 # U+2FFFE: \xF5\xBF\xBF\xBE
573 # U+3FFFE: \xF7\xBF\xBF\xBE
574 # U+4FFFE: \xF8\xA9\xBF\xBF\xBE
575 # U+5FFFE: \xF8\xAB\xBF\xBF\xBE
576 # U+6FFFE: \xF8\xAD\xBF\xBF\xBE
577 # U+7FFFE: \xF8\xAF\xBF\xBF\xBE
578 # U+8FFFE: \xF8\xB1\xBF\xBF\xBE
579 # U+9FFFE: \xF8\xB3\xBF\xBF\xBE
580 # U+AFFFE: \xF8\xB5\xBF\xBF\xBE
581 # U+BFFFE: \xF8\xB7\xBF\xBF\xBE
582 # U+CFFFE: \xF8\xB9\xBF\xBF\xBE
583 # U+DFFFE: \xF8\xBB\xBF\xBF\xBE
584 # U+EFFFE: \xF8\xBD\xBF\xBF\xBE
585 # U+FFFFE: \xF8\xBF\xBF\xBF\xBE
586 # U+10FFFE: \xF9\xA1\xBF\xBF\xBE
588 # The first part of the table maps bytes to character classes to reduce
589 # the size of the transition table and create bitmasks.
591 # The second part is a transition table that maps a combination of a
592 # state of the automaton and a character class to a new state. The
593 # numbering of the original nodes is retained, but some have been split
594 # so that there are new nodes. They mean:
595 # N0 The initial state, and final accepting one.
596 # N1 One continuation byte (A0-BF) left. This is transitioned to
597 # immediately when the start byte indicates a two-byte sequence
598 # N2 Two continuation bytes left.
599 # N3 Three continuation bytes left.
600 # N4 Start byte is F0. Continuation bytes A[0-F] are illegal
601 # (overlong); the other continuations transition to N2
602 # N5 Start byte is F1. Continuation bytes B6 and B7 are illegal
603 # (surrogates); BF transitions to N9; the other continuations to
605 # N6 Start byte is F[357]. Continuation byte BF transitions to N12;
606 # other continuations to N2
607 # N7 Start byte is F8. Continuation bytes A[0-7] are illegal
608 # (overlong); continuations A[9BDF] and B[13579BDF] transition to
609 # N14; the other continuations to N3
610 # N8 Start byte is F9. Continuation byte A0 transitions to N3; A1
611 # to N14; the other continuation bytes are illegal.
612 # N9 Initial sequence is F1 BF. Continuation byte AE transitions to
613 # state N10; AF to N11; BF to N13; the other continuations to N1.
614 # N10 Initial sequence is F1 BF AE. Continuation bytes B0-BF are
615 # illegal (non-chars); the other continuations are legal
616 # N11 Initial sequence is F1 BF AF. Continuation bytes A0-AF are
617 # illegal (non-chars); the other continuations are legal
618 # N12 Initial sequence is F[357] BF. Continuation bytes BF
619 # transitions to N13; the other continuations to N1
620 # N13 Initial sequence is F[1357] BF BF or F8 x y BF (where x and y
621 # are something that can lead to a non-char. Continuation bytes
622 # BE and BF are illegal (non-chars); the other continuations are
624 # N14 Initial sequence is F8 A[9BDF]; or F8 B[13579BDF]; or F9 A1.
625 # Continuation byte BF transitions to N15; the other
626 # continuations to N2
627 # N15 Initial sequence is F8 A[9BDF] BF; or F8 B[13579BDF] BF; or
628 # F9 A1 BF. Continuation byte BF transitions to N16; the other
629 # continuations to N2
630 # N16 Initial sequence is F8 A[9BDF] BF BF; or F8 B[13579BDF] BF BF;
631 # or F9 A1 BF BF. Continuation bytes BE and BF are illegal
632 # (non-chars); the other continuations are legal
633 # 1 Reject. All transitions not mentioned above (except the single
634 # byte ones (as they are always legal) are to this state.
636 my $NUM_CLASSES = 23;
638 my $N1 = $N0 + $NUM_CLASSES;
639 my $N2 = $N1 + $NUM_CLASSES;
640 my $N3 = $N2 + $NUM_CLASSES;
641 my $N4 = $N3 + $NUM_CLASSES;
642 my $N5 = $N4 + $NUM_CLASSES;
643 my $N6 = $N5 + $NUM_CLASSES;
644 my $N7 = $N6 + $NUM_CLASSES;
645 my $N8 = $N7 + $NUM_CLASSES;
646 my $N9 = $N8 + $NUM_CLASSES;
647 my $N10 = $N9 + $NUM_CLASSES;
648 my $N11 = $N10 + $NUM_CLASSES;
649 my $N12 = $N11 + $NUM_CLASSES;
650 my $N13 = $N12 + $NUM_CLASSES;
651 my $N14 = $N13 + $NUM_CLASSES;
652 my $N15 = $N14 + $NUM_CLASSES;
656 # 0 1 2 3 4 5 6 7 8 9 A B C D E F
657 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, # 00-0F
658 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, # 10-1F
659 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, # 20-2F
660 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, # 30-3F
661 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, # 40-4F
662 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, # 50-5F
663 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, # 60-6F
664 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, # 70-7F
665 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, # 80-8F
666 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, # 90-9F
667 10,11,12,12,12,12,12,12,13,14,13,14,13,14,15,16, # A0-AF
668 17,18,17,18,17,18,19,20,17,18,17,18,17,18,21,22, # B0-BF
669 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, # C0-CF
670 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, # D0-DF
671 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, # E0-EF
672 8, 6, 4, 5, 4, 5, 4, 5, 9, 7, 1, 1, 1, 1, 1, 1, # F0-FF
674 $strict_utf8_dfa[$i82utf[$_]] = $i8[$_] for (0 .. 255);
675 push @strict_utf8_dfa, (
677 # 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
678 0,1,$N1,$N2,$N3,$N6,$N5,$N8,$N4,$N7, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, # N0
679 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, # N1
680 1,1, 1, 1, 1, 1, 1, 1, 1, 1, $N1, $N1, $N1, $N1, $N1, $N1, $N1, $N1, $N1, $N1, $N1, $N1, $N1, # N2
681 1,1, 1, 1, 1, 1, 1, 1, 1, 1, $N2, $N2, $N2, $N2, $N2, $N2, $N2, $N2, $N2, $N2, $N2, $N2, $N2, # N3
683 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, $N2, $N2, $N2, $N2, $N2, $N2, # N4
684 1,1, 1, 1, 1, 1, 1, 1, 1, 1, $N2, $N2, $N2, $N2, $N2, $N2, $N2, $N2, $N2, 1, 1, $N2, $N9, # N5
685 1,1, 1, 1, 1, 1, 1, 1, 1, 1, $N2, $N2, $N2, $N2, $N2, $N2, $N2, $N2, $N2, $N2, $N2, $N2,$N12, # N6
686 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, $N3,$N14, $N3,$N14, $N3,$N14, $N3,$N14, $N3,$N14, # N7
687 1,1, 1, 1, 1, 1, 1, 1, 1, 1, $N3,$N14, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, # N8
688 1,1, 1, 1, 1, 1, 1, 1, 1, 1, $N1, $N1, $N1, $N1, $N1,$N10,$N11, $N1, $N1, $N1, $N1, $N1,$N13, # N9
689 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, # N10
690 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, # N11
691 1,1, 1, 1, 1, 1, 1, 1, 1, 1, $N1, $N1, $N1, $N1, $N1, $N1, $N1, $N1, $N1, $N1, $N1, $N1,$N13, # N12
692 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, # N13
693 1,1, 1, 1, 1, 1, 1, 1, 1, 1, $N2, $N2, $N2, $N2, $N2, $N2, $N2, $N2, $N2, $N2, $N2, $N2,$N15, # N14
694 1,1, 1, 1, 1, 1, 1, 1, 1, 1, $N1, $N1, $N1, $N1, $N1, $N1, $N1, $N1, $N1, $N1, $N1, $N1,$N13, # N15
696 output_table(\@strict_utf8_dfa, "PL_strict_utf8_dfa_tab", $NUM_CLASSES);
700 # This generates the dfa table for C9 strict UTF-8, which rejects
701 # surrogates and above Unicode, but allows non-characters,.
704 # 00-9F 0 Always legal at start
705 # A0-A1 9 Not legal immediately after start bytes F0 F8
706 # A2-A7 10 Not legal immediately after start bytes F0 F8 F9
707 # A8-AF 11 Not legal immediately after start bytes F0 F9
708 # B0-B5,B8-BF 12 Not legal immediately after start byte F9
710 # C0-C4 1 (reject, all are overlong)
711 # C5-DF 2 Accepts any legal continuation
712 # E0 1 (reject, all are overlong)
713 # E1-EF 3 Accepts any legal continuation
714 # F0 6 (has overlongs)
715 # F1 5 (has surrogates)
716 # F2-F7 4 Accepts any legal continuation
717 # F8 8 (has overlongs)
718 # F9 7 (has non-Unicode)
719 # FA-FF 1 (reject, all are non-Unicode)
721 # The first part of the table maps bytes to character classes to reduce
722 # the size of the transition table and create bitmasks.
724 # The second part is a transition table that maps a combination of a
725 # state of the automaton and a character class to a new state. The
726 # numbering of the original nodes is retained, but some have been split
727 # so that there are new nodes. They mean:
728 # N0 The initial state, and final accepting one.
729 # N1 One continuation byte (A0-BF) left. This is transitioned to
730 # immediately when the start byte indicates a two-byte sequence
731 # N2 Two continuation bytes left.
732 # N3 Three continuation bytes left.
733 # N4 Start byte is F0. Continuation bytes A[0-F] are illegal
734 # (overlong); the other continuations transition to N2
735 # N5 Start byte is F1. B6 and B7 are illegal (surrogates); the
736 # other continuations transition to N2
737 # N6 Start byte is F8. Continuation bytes A[0-7] are illegal
738 # (overlong); the other continuations transition to N3
739 # N7 Start byte is F9. Continuation bytes A0 and A1 transition to
740 # N3; the other continuation bytes are illegal (non-Unicode)
741 # 1 Reject. All transitions not mentioned above (except the single
742 # byte ones (as they are always legal) are to this state.
744 my $NUM_CLASSES = 14;
746 my $N1 = $N0 + $NUM_CLASSES;
747 my $N2 = $N1 + $NUM_CLASSES;
748 my $N3 = $N2 + $NUM_CLASSES;
749 my $N4 = $N3 + $NUM_CLASSES;
750 my $N5 = $N4 + $NUM_CLASSES;
751 my $N6 = $N5 + $NUM_CLASSES;
752 my $N7 = $N6 + $NUM_CLASSES;
756 # 0 1 2 3 4 5 6 7 8 9 A B C D E F
757 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, # 00-0F
758 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, # 10-1F
759 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, # 20-2F
760 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, # 30-3F
761 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, # 40-4F
762 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, # 50-5F
763 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, # 60-6F
764 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, # 70-7F
765 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, # 80-8F
766 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, # 90-9F
767 9, 9,10,10,10,10,10,10,11,11,11,11,11,11,11,11, # A0-AF
768 12,12,12,12,12,12,13,13,12,12,12,12,12,12,12,12, # B0-BF
769 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, # C0-CF
770 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, # D0-DF
771 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, # E0-EF
772 6, 5, 4, 4, 4, 4, 4, 4, 8, 7, 1, 1, 1, 1, 1, 1, # F0-FF
774 $C9_utf8_dfa[$i82utf[$_]] = $i8[$_] for (0 .. 255);
777 # 0 1 2 3 4 5 6 7 8 9 10 11 12 13
778 0,1,$N1,$N2,$N3,$N5,$N4,$N7,$N6, 1, 1, 1, 1, 1, # N0
779 1,1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, # N1
780 1,1, 1, 1, 1, 1, 1, 1, 1,$N1, $N1, $N1, $N1, $N1, # N2
781 1,1, 1, 1, 1, 1, 1, 1, 1,$N2, $N2, $N2, $N2, $N2, # N3
783 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, $N2, $N2, # N4
784 1,1, 1, 1, 1, 1, 1, 1, 1,$N2, $N2, $N2, $N2, 1, # N5
785 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, $N3, $N3, $N3, # N6
786 1,1, 1, 1, 1, 1, 1, 1, 1,$N3, 1, 1, 1, 1, # N7
788 output_table(\@C9_utf8_dfa, "PL_c9_utf8_dfa_tab", $NUM_CLASSES);
791 print $out_fh get_conditional_compile_line_end();
794 print $out_fh "\n#endif /* PERL_EBCDIC_TABLES_H_ */\n";
796 read_only_bottom_close_and_rename($out_fh);