Weekly spelling fixes.
[wine/dcerpc.git] / libs / unicode / cpmap.pl
blob17d26c9bfcc18fd6b14889dd9aceb68fc365eacd
1 #!/usr/bin/perl
3 # Generate code page .c files from ftp.unicode.org descriptions
5 # Copyright 2000 Alexandre Julliard
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2.1 of the License, or (at your option) any later version.
12 # This library is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # Lesser General Public License for more details.
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this library; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 # base directory for ftp.unicode.org files
23 $BASEDIR = "ftp.unicode.org/Public/";
24 $MAPPREFIX = $BASEDIR . "MAPPINGS/";
26 # UnicodeData file
27 $UNICODEDATA = $BASEDIR . "UNIDATA/UnicodeData.txt";
29 # Sort keys file
30 $SORTKEYS = "www.unicode.org/reports/tr10/allkeys.txt";
32 # Defaults mapping
33 $DEFAULTS = "./defaults";
35 # Default char for undefined mappings
36 $DEF_CHAR = ord '?';
38 @allfiles =
40 [ 37, "VENDORS/MICSFT/EBCDIC/CP037.TXT", "IBM EBCDIC US Canada" ],
41 [ 42, "VENDORS/ADOBE/symbol.txt", "Symbol" ],
42 [ 424, "VENDORS/MISC/CP424.TXT", "IBM EBCDIC Hebrew" ],
43 [ 437, "VENDORS/MICSFT/PC/CP437.TXT", "OEM United States" ],
44 [ 500, "VENDORS/MICSFT/EBCDIC/CP500.TXT", "IBM EBCDIC International" ],
45 [ 737, "VENDORS/MICSFT/PC/CP737.TXT", "OEM Greek 437G" ],
46 [ 775, "VENDORS/MICSFT/PC/CP775.TXT", "OEM Baltic" ],
47 [ 850, "VENDORS/MICSFT/PC/CP850.TXT", "OEM Multilingual Latin 1" ],
48 [ 852, "VENDORS/MICSFT/PC/CP852.TXT", "OEM Slovak Latin 2" ],
49 [ 855, "VENDORS/MICSFT/PC/CP855.TXT", "OEM Cyrillic" ],
50 [ 856, "VENDORS/MISC/CP856.TXT", "Hebrew PC" ],
51 [ 857, "VENDORS/MICSFT/PC/CP857.TXT", "OEM Turkish" ],
52 [ 860, "VENDORS/MICSFT/PC/CP860.TXT", "OEM Portuguese" ],
53 [ 861, "VENDORS/MICSFT/PC/CP861.TXT", "OEM Icelandic" ],
54 [ 862, "VENDORS/MICSFT/PC/CP862.TXT", "OEM Hebrew" ],
55 [ 863, "VENDORS/MICSFT/PC/CP863.TXT", "OEM Canadian French" ],
56 [ 864, "VENDORS/MICSFT/PC/CP864.TXT", "OEM Arabic" ],
57 [ 865, "VENDORS/MICSFT/PC/CP865.TXT", "OEM Nordic" ],
58 [ 866, "VENDORS/MICSFT/PC/CP866.TXT", "OEM Russian" ],
59 [ 869, "VENDORS/MICSFT/PC/CP869.TXT", "OEM Greek" ],
60 [ 874, "VENDORS/MICSFT/PC/CP874.TXT", "ANSI/OEM Thai" ],
61 [ 875, "VENDORS/MICSFT/EBCDIC/CP875.TXT", "IBM EBCDIC Greek" ],
62 [ 878, "VENDORS/MISC/KOI8-R.TXT", "Russian KOI8" ],
63 [ 932, "VENDORS/MICSFT/WINDOWS/CP932.TXT", "ANSI/OEM Japanese Shift-JIS" ],
64 [ 936, "VENDORS/MICSFT/WINDOWS/CP936.TXT", "ANSI/OEM Simplified Chinese GBK" ],
65 [ 949, "VENDORS/MICSFT/WINDOWS/CP949.TXT", "ANSI/OEM Korean Unified Hangul" ],
66 [ 950, "VENDORS/MICSFT/WINDOWS/CP950.TXT", "ANSI/OEM Traditional Chinese Big5" ],
67 [ 1006, "VENDORS/MISC/CP1006.TXT", "IBM Arabic" ],
68 [ 1026, "VENDORS/MICSFT/EBCDIC/CP1026.TXT", "IBM EBCDIC Latin 5 Turkish" ],
69 [ 1250, "VENDORS/MICSFT/WINDOWS/CP1250.TXT", "ANSI Eastern Europe" ],
70 [ 1251, "VENDORS/MICSFT/WINDOWS/CP1251.TXT", "ANSI Cyrillic" ],
71 [ 1252, "VENDORS/MICSFT/WINDOWS/CP1252.TXT", "ANSI Latin 1" ],
72 [ 1253, "VENDORS/MICSFT/WINDOWS/CP1253.TXT", "ANSI Greek" ],
73 [ 1254, "VENDORS/MICSFT/WINDOWS/CP1254.TXT", "ANSI Turkish" ],
74 [ 1255, "VENDORS/MICSFT/WINDOWS/CP1255.TXT", "ANSI Hebrew" ],
75 [ 1256, "VENDORS/MICSFT/WINDOWS/CP1256.TXT", "ANSI Arabic" ],
76 [ 1257, "VENDORS/MICSFT/WINDOWS/CP1257.TXT", "ANSI Baltic" ],
77 [ 1258, "VENDORS/MICSFT/WINDOWS/CP1258.TXT", "ANSI/OEM Viet Nam" ],
78 [ 10000, "VENDORS/MICSFT/MAC/ROMAN.TXT", "Mac Roman" ],
79 [ 10006, "VENDORS/MICSFT/MAC/GREEK.TXT", "Mac Greek" ],
80 [ 10007, "VENDORS/MICSFT/MAC/CYRILLIC.TXT", "Mac Cyrillic" ],
81 [ 10029, "VENDORS/MICSFT/MAC/LATIN2.TXT", "Mac Latin 2" ],
82 [ 10079, "VENDORS/MICSFT/MAC/ICELAND.TXT", "Mac Icelandic" ],
83 [ 10081, "VENDORS/MICSFT/MAC/TURKISH.TXT", "Mac Turkish" ],
84 [ 20866, "VENDORS/MISC/KOI8-R.TXT", "Russian KOI8" ],
85 [ 20932, "OBSOLETE/EASTASIA/JIS/JIS0208.TXT", "EUC-JP" ],
86 [ 28591, "ISO8859/8859-1.TXT", "ISO 8859-1 Latin 1" ],
87 [ 28592, "ISO8859/8859-2.TXT", "ISO 8859-2 Latin 2 (East European)" ],
88 [ 28593, "ISO8859/8859-3.TXT", "ISO 8859-3 Latin 3 (South European)" ],
89 [ 28594, "ISO8859/8859-4.TXT", "ISO 8859-4 Latin 4 (Baltic old)" ],
90 [ 28595, "ISO8859/8859-5.TXT", "ISO 8859-5 Cyrillic" ],
91 [ 28596, "ISO8859/8859-6.TXT", "ISO 8859-6 Arabic" ],
92 [ 28597, "ISO8859/8859-7.TXT", "ISO 8859-7 Greek" ],
93 [ 28598, "ISO8859/8859-8.TXT", "ISO 8859-8 Hebrew" ],
94 [ 28599, "ISO8859/8859-9.TXT", "ISO 8859-9 Latin 5 (Turkish)" ],
95 [ 28600, "ISO8859/8859-10.TXT", "ISO 8859-10 Latin 6 (Nordic)" ],
96 [ 28603, "ISO8859/8859-13.TXT", "ISO 8859-13 Latin 7 (Baltic)" ],
97 [ 28604, "ISO8859/8859-14.TXT", "ISO 8859-14 Latin 8 (Celtic)" ],
98 [ 28605, "ISO8859/8859-15.TXT", "ISO 8859-15 Latin 9 (Euro)" ],
99 [ 28606, "ISO8859/8859-16.TXT", "ISO 8859-16 Latin 10 (Balkan)" ]
103 %ctype =
105 "upper" => 0x0001,
106 "lower" => 0x0002,
107 "digit" => 0x0004,
108 "space" => 0x0008,
109 "punct" => 0x0010,
110 "cntrl" => 0x0020,
111 "blank" => 0x0040,
112 "xdigit" => 0x0080,
113 "alpha" => 0x0100
116 %categories =
118 "Lu" => $ctype{"alpha"}|$ctype{"upper"}, # Letter, Uppercase
119 "Ll" => $ctype{"alpha"}|$ctype{"lower"}, # Letter, Lowercase
120 "Lt" => $ctype{"alpha"}, # Letter, Titlecase
121 "Mn" => $ctype{"punct"}, # Mark, Non-Spacing
122 "Mc" => $ctype{"punct"}, # Mark, Spacing Combining
123 "Me" => $ctype{"punct"}, # Mark, Enclosing
124 "Nd" => $ctype{"digit"}, # Number, Decimal Digit
125 "Nl" => $ctype{"punct"}, # Number, Letter
126 "No" => $ctype{"punct"}, # Number, Other
127 "Zs" => $ctype{"space"}, # Separator, Space
128 "Zl" => 0, # Separator, Line
129 "Zp" => 0, # Separator, Paragraph
130 "Cc" => $ctype{"cntrl"}, # Other, Control
131 "Cf" => 0, # Other, Format
132 "Cs" => 0, # Other, Surrogate
133 "Co" => 0, # Other, Private Use
134 "Cn" => 0, # Other, Not Assigned
135 "Lm" => $ctype{"punct"}, # Letter, Modifier
136 "Lo" => $ctype{"alpha"}, # Letter, Other
137 "Pc" => $ctype{"punct"}, # Punctuation, Connector
138 "Pd" => $ctype{"punct"}, # Punctuation, Dash
139 "Ps" => $ctype{"punct"}, # Punctuation, Open
140 "Pe" => $ctype{"punct"}, # Punctuation, Close
141 "Pi" => $ctype{"punct"}, # Punctuation, Initial quote
142 "Pf" => $ctype{"punct"}, # Punctuation, Final quote
143 "Po" => $ctype{"punct"}, # Punctuation, Other
144 "Sm" => $ctype{"punct"}, # Symbol, Math
145 "Sc" => $ctype{"punct"}, # Symbol, Currency
146 "Sk" => $ctype{"punct"}, # Symbol, Modifier
147 "So" => $ctype{"punct"} # Symbol, Other
150 # a few characters need additional categories that cannot be determined automatically
151 %special_categories =
153 "xdigit" => [ ord('0')..ord('9'),ord('A')..ord('F'),ord('a')..ord('f'),
154 0xff10..0xff19, 0xff21..0xff26, 0xff41..0xff46 ],
155 "space" => [ 0x09..0x0d, 0xfeff ],
156 "blank" => [ 0x09, 0x20, 0xa0, 0xfeff ]
159 %directions =
161 "L" => 1, # Left-to-Right
162 "LRE" => 11, # Left-to-Right Embedding
163 "LRO" => 11, # Left-to-Right Override
164 "R" => 2, # Right-to-Left
165 "AL" => 2, # Right-to-Left Arabic
166 "RLE" => 11, # Right-to-Left Embedding
167 "RLO" => 11, # Right-to-Left Override
168 "PDF" => 11, # Pop Directional Format
169 "EN" => 3, # European Number
170 "ES" => 4, # European Number Separator
171 "ET" => 5, # European Number Terminator
172 "AN" => 6, # Arabic Number
173 "CS" => 7, # Common Number Separator
174 "NSM" => 0, # Non-Spacing Mark
175 "BN" => 0, # Boundary Neutral
176 "B" => 8, # Paragraph Separator
177 "S" => 9, # Segment Separator
178 "WS" => 10, # Whitespace
179 "ON" => 11 # Other Neutrals
183 ################################################################
184 # main routine
186 READ_DEFAULTS();
187 my @sortkeys = READ_SORTKEYS_FILE();
188 DUMP_CASE_MAPPINGS();
189 DUMP_SORTKEYS(@sortkeys);
190 DUMP_COMPOSE_TABLES();
191 DUMP_CTYPE_TABLES();
193 foreach $file (@allfiles) { HANDLE_FILE( @$file ); }
195 OUTPUT_CPTABLE();
197 exit(0);
200 ################################################################
201 # read in the defaults file
202 sub READ_DEFAULTS
204 @unicode_defaults = ();
205 @unicode_aliases = ();
206 @tolower_table = ();
207 @toupper_table = ();
208 @digitmap_table = ();
209 @compatmap_table = ();
210 @category_table = ();
211 @direction_table = ();
212 @decomp_table = ();
213 @compose_table = ();
215 # first setup a few default mappings
217 open DEFAULTS or die "Cannot open $DEFAULTS";
218 print "Loading $DEFAULTS\n";
219 while (<DEFAULTS>)
221 next if /^\#/; # skip comments
222 next if /^$/; # skip empty lines
223 if (/^(([0-9a-fA-F]+)(,[0-9a-fA-F]+)*)\s+([0-9a-fA-F]+|'.'|none)\s+(\#.*)?/)
225 my @src = map hex, split /,/,$1;
226 my $dst = $4;
227 my $comment = $5;
228 if ($#src > 0) { push @unicode_aliases, \@src; }
229 next if ($dst eq "none");
230 $dst = ($dst =~ /\'.\'/) ? ord substr($dst,1,1) : hex $dst;
231 foreach $src (@src)
233 die "Duplicate value" if defined($unicode_defaults[$src]);
234 $unicode_defaults[$src] = $dst;
236 next;
238 die "Unrecognized line $_\n";
241 # now build mappings from the decomposition field of the Unicode database
243 open UNICODEDATA or die "Cannot open $UNICODEDATA";
244 print "Loading $UNICODEDATA\n";
245 while (<UNICODEDATA>)
247 # Decode the fields ...
248 ($code, $name, $cat, $comb, $bidi,
249 $decomp, $dec, $dig, $num, $mirror,
250 $oldname, $comment, $upper, $lower, $title) = split /;/;
252 my $src = hex $code;
254 die "unknown category $cat" unless defined $categories{$cat};
255 die "unknown directionality $bidi" unless defined $directions{$bidi};
257 $uniname[$src] = $name;
258 $category_table[$src] = $categories{$cat};
259 $direction_table[$src] = $directions{$bidi};
261 if ($lower ne "")
263 $tolower_table[$src] = hex $lower;
264 $category_table[$src] |= $ctype{"upper"}|$ctype{"alpha"};
266 if ($upper ne "")
268 $toupper_table[$src] = hex $upper;
269 $category_table[$src] |= $ctype{"lower"}|$ctype{"alpha"};
271 if ($dec ne "")
273 $category_table[$src] |= $ctype{"digit"};
275 if ($dig ne "")
277 $digitmap_table[$src] = ord $dig;
280 # copy the category and direction for everything between First/Last pairs
281 if ($name =~ /, First>/) { $start = $src; }
282 if ($name =~ /, Last>/)
284 while ($start < $src)
286 $category_table[$start] = $category_table[$src];
287 $direction_table[$start] = $direction_table[$src];
288 $start++;
292 next if $decomp eq ""; # no decomposition, skip it
294 if ($decomp =~ /^<([a-zA-Z]+)>\s+([0-9a-fA-F]+)$/)
296 # decomposition of the form "<foo> 1234" -> use char if type is known
297 if (($src >= 0xf900 && $src < 0xfb00) || ($src >= 0xfe30 && $src < 0xfffd))
299 # Single char decomposition in the compatibility range
300 $compatmap_table[$src] = hex $2;
302 next unless ($1 eq "font" ||
303 $1 eq "noBreak" ||
304 $1 eq "circle" ||
305 $1 eq "super" ||
306 $1 eq "sub" ||
307 $1 eq "wide" ||
308 $1 eq "narrow" ||
309 $1 eq "compat" ||
310 $1 eq "small");
311 $dst = hex $2;
313 elsif ($decomp =~ /^<compat>\s+0020\s+([0-9a-fA-F]+)/)
315 # decomposition "<compat> 0020 1234" -> combining accent
316 $dst = hex $1;
318 elsif ($decomp =~ /^([0-9a-fA-F]+)/)
320 # decomposition contains only char values without prefix -> use first char
321 $dst = hex $1;
322 $category_table[$src] |= $category_table[$dst];
323 # store decomposition if it contains two chars
324 if ($decomp =~ /^([0-9a-fA-F]+)\s+([0-9a-fA-F]+)$/)
326 $decomp_table[$src] = [ hex $1, hex $2 ];
327 push @compose_table, [ hex $1, hex $2, $src ];
329 elsif ($decomp =~ /^(<[a-z]+>\s)*([0-9a-fA-F]+)$/ &&
330 (($src >= 0xf900 && $src < 0xfb00) || ($src >= 0xfe30 && $src < 0xfffd)))
332 # Single char decomposition in the compatibility range
333 $compatmap_table[$src] = hex $2;
336 else
338 next;
341 next if defined($unicode_defaults[$src]); # may have been set in the defaults file
343 # check for loops
344 for ($i = $dst; ; $i = $unicode_defaults[$i])
346 die sprintf("loop detected for %04x -> %04x",$src,$dst) if $i == $src;
347 last unless defined($unicode_defaults[$i]);
349 $unicode_defaults[$src] = $dst;
352 # patch the category of some special characters
354 foreach $cat (keys %special_categories)
356 my $flag = $ctype{$cat};
357 foreach $i (@{$special_categories{$cat}}) { $category_table[$i] |= $flag; }
362 ################################################################
363 # parse the input file
364 sub READ_FILE
366 my $name = shift;
367 open INPUT,$name or die "Cannot open $name";
368 @cp2uni = ();
369 @lead_bytes = ();
370 @uni2cp = ();
372 while (<INPUT>)
374 next if /^\#/; # skip comments
375 next if /^$/; # skip empty lines
376 next if /\x1a/; # skip ^Z
377 next if (/^0x([0-9a-fA-F]+)\s+\#UNDEFINED/); # undefined char
379 if (/^0x([0-9a-fA-F]+)\s+\#DBCS LEAD BYTE/)
381 $cp = hex $1;
382 push @lead_bytes,$cp;
383 $cp2uni[$cp] = 0;
384 next;
386 if (/^0x([0-9a-fA-F]+)\s+0x([0-9a-fA-F]+)\s+(\#.*)?/)
388 $cp = hex $1;
389 $uni = hex $2;
390 $cp2uni[$cp] = $uni unless defined($cp2uni[$cp]);
391 $uni2cp[$uni] = $cp unless defined($uni2cp[$uni]);
392 next;
394 die "$name: Unrecognized line $_\n";
399 ################################################################
400 # parse the symbol.txt file, since its syntax is different from the other ones
401 sub READ_SYMBOL_FILE
403 my $name = shift;
404 open INPUT,$name or die "Cannot open $name";
405 @cp2uni = ();
406 @lead_bytes = ();
407 @uni2cp = ();
409 while (<INPUT>)
411 next if /^\#/; # skip comments
412 next if /^$/; # skip empty lines
413 next if /\x1a/; # skip ^Z
414 if (/^([0-9a-fA-F]+)\s+([0-9a-fA-F]+)\s+(\#.*)?/)
416 $uni = hex $1;
417 $cp = hex $2;
418 $cp2uni[$cp] = $uni unless defined($cp2uni[$cp]);
419 $uni2cp[$uni] = $cp unless defined($uni2cp[$uni]);
420 next;
422 die "$name: Unrecognized line $_\n";
427 ################################################################
428 # build EUC-JP table from the JIS 0208 file
429 # FIXME: for proper EUC-JP we should probably read JIS 0212 too
430 # but this would require 3-byte DBCS characters
431 sub READ_JIS0208_FILE
433 my $name = shift;
434 @cp2uni = ();
435 @lead_bytes = ();
436 @uni2cp = ();
438 # ASCII chars
439 for ($i = 0x00; $i <= 0x7f; $i++)
441 $cp2uni[$i] = $i;
442 $uni2cp[$i] = $i;
445 # JIS X 0201 right plane
446 for ($i = 0xa1; $i <= 0xdf; $i++)
448 $cp2uni[0x8e00 + $i] = 0xfec0 + $i;
449 $uni2cp[0xfec0 + $i] = 0x8e00 + $i;
452 # lead bytes
453 foreach $i (0x8e, 0x8f, 0xa1 .. 0xfe)
455 push @lead_bytes,$i;
456 $cp2uni[$i] = 0;
459 # undefined chars
460 foreach $i (0x80 .. 0x8d, 0x90 .. 0xa0, 0xff)
462 $cp2uni[$i] = $DEF_CHAR;
465 # Shift-JIS compatibility
466 $uni2cp[0x00a5] = 0x5c;
467 $uni2cp[0x203e] = 0x7e;
469 # Fix backslash conversion
470 $cp2uni[0xa1c0] = 0xff3c;
471 $uni2cp[0xff3c] = 0xa1c0;
473 open INPUT, "$name" or die "Cannot open $name";
474 while (<INPUT>)
476 next if /^\#/; # skip comments
477 next if /^$/; # skip empty lines
478 next if /\x1a/; # skip ^Z
479 if (/^0x[0-9a-fA-F]+\s+0x([0-9a-fA-F]+)\s+0x([0-9a-fA-F]+)\s+(\#.*)?/)
481 $cp = 0x8080 + hex $1;
482 $uni = hex $2;
483 $cp2uni[$cp] = $uni unless defined($cp2uni[$cp]);
484 $uni2cp[$uni] = $cp unless defined($uni2cp[$uni]);
485 next;
487 die "$name: Unrecognized line $_\n";
492 ################################################################
493 # build the sort keys table
494 sub READ_SORTKEYS_FILE
496 my @sortkeys = ();
497 for (my $i = 0; $i < 65536; $i++) { $sortkeys[$i] = [ -1, 0, 0, 0, 0 ] };
499 open INPUT, "$SORTKEYS" or die "Cannot open $SORTKEYS";
500 print "Loading $SORTKEYS\n";
501 while (<INPUT>)
503 next if /^\#/; # skip comments
504 next if /^$/; # skip empty lines
505 next if /\x1a/; # skip ^Z
506 next if /^\@version/; # skip @version header
507 if (/^([0-9a-fA-F]+)\s+;\s+\[([*.])([0-9a-fA-F]{4})\.([0-9a-fA-F]{4})\.([0-9a-fA-F]{4})\.([0-9a-fA-F]+)\]/)
509 my ($uni,$variable) = (hex $1, $2);
510 next if $uni > 65535;
511 $sortkeys[$uni] = [ $uni, hex $3, hex $4, hex $5, hex $6 ];
512 next;
514 if (/^([0-9a-fA-F]+\s+)+;\s+\[[*.]([0-9a-fA-F]{4})\.([0-9a-fA-F]{4})\.([0-9a-fA-F]{4})\.([0-9a-fA-F]+)\]/)
516 # multiple character sequence, ignored for now
517 next;
519 die "$SORTKEYS: Unrecognized line $_\n";
521 close INPUT;
523 # compress the keys to 32 bit:
524 # key 1 to 16 bits, key 2 to 8 bits, key 3 to 4 bits, key 4 to 1 bit
526 @sortkeys = sort { ${$a}[1] <=> ${$b}[1] or
527 ${$a}[2] <=> ${$b}[2] or
528 ${$a}[3] <=> ${$b}[3] or
529 ${$a}[4] <=> ${$b}[4] or
530 $a cmp $b; } @sortkeys;
532 my ($n2, $n3) = (1, 1);
533 my @keys = (-1, -1, -1, -1, -1 );
534 my @flatkeys = ();
536 for (my $i = 0; $i < 65536; $i++)
538 my @current = @{$sortkeys[$i]};
539 next if $current[0] == -1;
540 if ($current[1] == $keys[1])
542 if ($current[2] == $keys[2])
544 if ($current[3] == $keys[3])
546 # nothing
548 else
550 $keys[3] = $current[3];
551 $n3++;
552 die if ($n3 >= 16);
555 else
557 $keys[2] = $current[2];
558 $keys[3] = $current[3];
559 $n2++;
560 $n3 = 1;
561 die if ($n2 >= 256);
564 else
566 $keys[1] = $current[1];
567 $keys[2] = $current[2];
568 $keys[3] = $current[3];
569 $n2 = 1;
570 $n3 = 1;
573 if ($current[2]) { $current[2] = $n2; }
574 if ($current[3]) { $current[3] = $n3; }
575 if ($current[4]) { $current[4] = 1; }
577 $flatkeys[$current[0]] = ($current[1] << 16) | ($current[2] << 8) | ($current[3] << 4) | $current[4];
579 return @flatkeys;
583 ################################################################
584 # build the sort keys table
585 sub DUMP_SORTKEYS
587 my @keys = @_;
589 # count the number of 256-key ranges that contain something
591 my @offsets = ();
592 my $ranges = 2;
593 for (my $i = 0; $i < 256; $i++) { $offsets[$i] = 256; }
594 for (my $i = 0; $i < 65536; $i++)
596 next unless defined $keys[$i];
597 $offsets[$i >> 8] = $ranges * 256;
598 $ranges++;
599 $i |= 255;
602 # output the range offsets
604 open OUTPUT,">collation.c" or die "Cannot create collation.c";
605 printf "Building collation.c\n";
606 printf OUTPUT "/* Unicode collation element table */\n";
607 printf OUTPUT "/* generated from %s */\n", $SORTKEYS;
608 printf OUTPUT "/* DO NOT EDIT!! */\n\n";
610 printf OUTPUT "const unsigned int collation_table[%d] =\n{\n", $ranges*256;
611 printf OUTPUT " /* index */\n";
612 printf OUTPUT "%s,\n", DUMP_ARRAY( "0x%08x", 0, @offsets );
614 # output the default values
616 printf OUTPUT " /* defaults */\n";
617 printf OUTPUT "%s", DUMP_ARRAY( "0x%08x", 0, (-1) x 256 );
619 # output all the key ranges
621 for (my $i = 0; $i < 256; $i++)
623 next if $offsets[$i] == 256;
624 printf OUTPUT ",\n /* 0x%02x00 .. 0x%02xff */\n", $i, $i;
625 printf OUTPUT "%s", DUMP_ARRAY( "0x%08x", -1, @keys[($i<<8) .. ($i<<8)+255] );
627 printf OUTPUT "\n};\n";
628 close OUTPUT;
632 ################################################################
633 # add default mappings once the file had been read
634 sub ADD_DEFAULT_MAPPINGS
636 # Apply aliases
638 foreach $alias (@unicode_aliases)
640 my $target = undef;
641 foreach $src (@$alias)
643 if (defined($uni2cp[$src]))
645 $target = $uni2cp[$src];
646 last;
649 next unless defined($target);
651 # At least one char of the alias set is defined, set the others to the same value
652 foreach $src (@$alias)
654 $uni2cp[$src] = $target unless defined($uni2cp[$src]);
658 # For every src -> target mapping in the defaults table,
659 # make uni2cp[src] = uni2cp[target] if uni2cp[target] is defined
661 for ($src = 0; $src < 65536; $src++)
663 next if defined($uni2cp[$src]); # source has a definition already
664 next unless defined($unicode_defaults[$src]); # no default for this char
665 my $target = $unicode_defaults[$src];
667 # do a recursive mapping until we find a target char that is defined
668 while (!defined($uni2cp[$target]) &&
669 defined($unicode_defaults[$target])) { $target = $unicode_defaults[$target]; }
671 if (defined($uni2cp[$target])) { $uni2cp[$src] = $uni2cp[$target]; }
674 # Add an identity mapping for all undefined chars
676 for ($i = 0; $i < 256; $i++)
678 next if defined($cp2uni[$i]);
679 next if defined($uni2cp[$i]);
680 $cp2uni[$i] = $uni2cp[$i] = $i;
684 ################################################################
685 # dump an array of integers
686 sub DUMP_ARRAY
688 my ($format,$default,@array) = @_;
689 my $i, $ret = " ";
690 for ($i = 0; $i < $#array; $i++)
692 $ret .= sprintf($format, defined $array[$i] ? $array[$i] : $default);
693 $ret .= (($i % 8) != 7) ? ", " : ",\n ";
695 $ret .= sprintf($format, defined $array[$i] ? $array[$i] : $default);
696 return $ret;
699 ################################################################
700 # dump an SBCS mapping table
701 sub DUMP_SBCS_TABLE
703 my ($codepage, $name) = @_;
704 my $i;
706 # output the ascii->unicode table
708 printf OUTPUT "static const WCHAR cp2uni[256] =\n";
709 printf OUTPUT "{\n%s\n};\n\n", DUMP_ARRAY( "0x%04x", $DEF_CHAR, @cp2uni[0 .. 255] );
711 # count the number of unicode->ascii subtables that contain something
713 my @filled = ();
714 my $subtables = 1;
715 for ($i = 0; $i < 65536; $i++)
717 next unless defined $uni2cp[$i];
718 $filled[$i >> 8] = 1;
719 $subtables++;
720 $i |= 255;
723 # output all the subtables into a single array
725 printf OUTPUT "static const unsigned char uni2cp_low[%d] =\n{\n", $subtables*256;
726 for ($i = 0; $i < 256; $i++)
728 next unless $filled[$i];
729 printf OUTPUT " /* 0x%02x00 .. 0x%02xff */\n", $i, $i;
730 printf OUTPUT "%s,\n", DUMP_ARRAY( "0x%02x", $DEF_CHAR, @uni2cp[($i<<8) .. ($i<<8)+255] );
732 printf OUTPUT " /* defaults */\n";
733 printf OUTPUT "%s\n};\n\n", DUMP_ARRAY( "0x%02x", 0, ($DEF_CHAR) x 256 );
735 # output a table of the offsets of the subtables in the previous array
737 my $pos = 0;
738 my @offsets = ();
739 for ($i = 0; $i < 256; $i++)
741 if ($filled[$i]) { push @offsets, $pos; $pos += 256; }
742 else { push @offsets, ($subtables-1) * 256; }
744 printf OUTPUT "static const unsigned short uni2cp_high[256] =\n";
745 printf OUTPUT "{\n%s\n};\n\n", DUMP_ARRAY( "0x%04x", 0, @offsets );
747 # output the code page descriptor
749 printf OUTPUT "const struct sbcs_table cptable_%03d =\n{\n", $codepage;
750 printf OUTPUT " { %d, 1, 0x%04x, 0x%04x, \"%s\" },\n",
751 $codepage, $DEF_CHAR, $DEF_CHAR, $name;
752 printf OUTPUT " cp2uni,\n";
753 printf OUTPUT " uni2cp_low,\n";
754 printf OUTPUT " uni2cp_high\n};\n";
758 ################################################################
759 # dump a DBCS mapping table
760 sub DUMP_DBCS_TABLE
762 my ($codepage, $name) = @_;
763 my $i, $x, $y;
765 # build a list of lead bytes that are actually used
767 my @lblist = ();
768 LBLOOP: for ($y = 0; $y <= $#lead_bytes; $y++)
770 my $base = $lead_bytes[$y] << 8;
771 for ($x = 0; $x < 256; $x++)
773 if (defined $cp2uni[$base+$x])
775 push @lblist,$lead_bytes[$y];
776 next LBLOOP;
780 my $unused = ($#lead_bytes > $#lblist);
782 # output the ascii->unicode table for the single byte chars
784 printf OUTPUT "static const WCHAR cp2uni[%d] =\n", 256 * ($#lblist + 2 + $unused);
785 printf OUTPUT "{\n%s,\n", DUMP_ARRAY( "0x%04x", $DEF_CHAR, @cp2uni[0 .. 255] );
787 # output the default table for unused lead bytes
789 if ($unused)
791 printf OUTPUT " /* unused lead bytes */\n";
792 printf OUTPUT "%s,\n", DUMP_ARRAY( "0x%04x", 0, ($DEF_CHAR) x 256 );
795 # output the ascii->unicode table for each DBCS lead byte
797 for ($y = 0; $y <= $#lblist; $y++)
799 my $base = $lblist[$y] << 8;
800 printf OUTPUT " /* lead byte %02x */\n", $lblist[$y];
801 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", $DEF_CHAR, @cp2uni[$base .. $base+255] );
802 printf OUTPUT ($y < $#lblist) ? ",\n" : "\n};\n\n";
805 # output the lead byte subtables offsets
807 my @offsets = ();
808 for ($x = 0; $x < 256; $x++) { $offsets[$x] = 0; }
809 for ($x = 0; $x <= $#lblist; $x++) { $offsets[$lblist[$x]] = $x + 1; }
810 if ($unused)
812 # increment all lead bytes offset to take into account the unused table
813 for ($x = 0; $x <= $#lead_bytes; $x++) { $offsets[$lead_bytes[$x]]++; }
815 printf OUTPUT "static const unsigned char cp2uni_leadbytes[256] =\n";
816 printf OUTPUT "{\n%s\n};\n\n", DUMP_ARRAY( "0x%02x", 0, @offsets );
818 # count the number of unicode->ascii subtables that contain something
820 my @filled = ();
821 my $subtables = 1;
822 for ($i = 0; $i < 65536; $i++)
824 next unless defined $uni2cp[$i];
825 $filled[$i >> 8] = 1;
826 $subtables++;
827 $i |= 255;
830 # output all the subtables into a single array
832 printf OUTPUT "static const unsigned short uni2cp_low[%d] =\n{\n", $subtables*256;
833 for ($y = 0; $y < 256; $y++)
835 next unless $filled[$y];
836 printf OUTPUT " /* 0x%02x00 .. 0x%02xff */\n", $y, $y;
837 printf OUTPUT "%s,\n", DUMP_ARRAY( "0x%04x", $DEF_CHAR, @uni2cp[($y<<8) .. ($y<<8)+255] );
839 printf OUTPUT " /* defaults */\n";
840 printf OUTPUT "%s\n};\n\n", DUMP_ARRAY( "0x%04x", 0, ($DEF_CHAR) x 256 );
842 # output a table of the offsets of the subtables in the previous array
844 my $pos = 0;
845 my @offsets = ();
846 for ($y = 0; $y < 256; $y++)
848 if ($filled[$y]) { push @offsets, $pos; $pos += 256; }
849 else { push @offsets, ($subtables-1) * 256; }
851 printf OUTPUT "static const unsigned short uni2cp_high[256] =\n";
852 printf OUTPUT "{\n%s\n};\n\n", DUMP_ARRAY( "0x%04x", 0, @offsets );
854 # output the code page descriptor
856 printf OUTPUT "const struct dbcs_table cptable_%03d =\n{\n", $codepage;
857 printf OUTPUT " { %d, 2, 0x%04x, 0x%04x, \"%s\" },\n",
858 $codepage, $DEF_CHAR, $DEF_CHAR, $name;
859 printf OUTPUT " cp2uni,\n";
860 printf OUTPUT " cp2uni_leadbytes,\n";
861 printf OUTPUT " uni2cp_low,\n";
862 printf OUTPUT " uni2cp_high,\n";
863 DUMP_LB_RANGES();
864 printf OUTPUT "};\n";
868 ################################################################
869 # dump the list of defined lead byte ranges
870 sub DUMP_LB_RANGES
872 my @list = ();
873 my $i = 0;
874 foreach $i (@lead_bytes) { $list[$i] = 1; }
875 my $on = 0;
876 printf OUTPUT " { ";
877 for ($i = 0; $i < 256; $i++)
879 if ($on)
881 if (!defined $list[$i]) { printf OUTPUT "0x%02x, ", $i-1; $on = 0; }
883 else
885 if ($list[$i]) { printf OUTPUT "0x%02x, ", $i; $on = 1; }
888 if ($on) { printf OUTPUT "0xff, "; }
889 printf OUTPUT "0x00, 0x00 }\n";
893 ################################################################
894 # dump the case mapping tables
895 sub DUMP_CASE_MAPPINGS
897 open OUTPUT,">casemap.c" or die "Cannot create casemap.c";
898 printf "Building casemap.c\n";
899 printf OUTPUT "/* Unicode case mappings */\n";
900 printf OUTPUT "/* Automatically generated; DO NOT EDIT!! */\n\n";
901 printf OUTPUT "#include \"wine/unicode.h\"\n\n";
903 DUMP_CASE_TABLE( "wine_casemap_lower", @tolower_table );
904 DUMP_CASE_TABLE( "wine_casemap_upper", @toupper_table );
905 DUMP_CASE_TABLE( "wine_digitmap", @digitmap_table );
906 DUMP_CASE_TABLE( "wine_compatmap", @compatmap_table );
907 close OUTPUT;
911 ################################################################
912 # dump a case mapping table
913 sub DUMP_CASE_TABLE
915 my ($name,@table) = @_;
917 # count the number of sub tables that contain something
918 # also compute the low and upper populated bounds
920 my @lowerbounds = ( 0, 0 );
921 my @upperbounds = ( 0, 255 );
922 my $index = 0;
923 my @filled = ();
924 for ($i = 0; $i < 65536; $i++)
926 next unless defined $table[$i];
927 if (!defined $filled[$i >> 8])
929 $lowerbounds[$index] = $i & 0xff;
930 $upperbounds[$index] = 0xff - $lowerbounds[$index];
931 $filled[$i >> 8] = $index * 256 + 512;
932 $index++;
934 else
936 $upperbounds[$index-1] = 0xff - ($i & 0xff);
938 $table[$i] = ($table[$i] - $i) & 0xffff;
941 # Collapse blocks upwards if possible
942 my $removed = 0;
943 $index = 0;
944 for ($i = 0; $i < 256; $i++)
946 next unless defined $filled[$i];
947 if ($upperbounds[$index - 1] > $lowerbounds[$index])
949 $removed = $removed + $lowerbounds[$index];
951 else
953 $removed = $removed + $upperbounds[$index - 1];
954 $lowerbounds[$index] = $upperbounds[$index - 1];
956 $filled[$i] = $filled[$i] - $removed;
957 $index++;
960 # dump the table
962 printf OUTPUT "const WCHAR %s[%d] =\n", $name, $index * 256 + 512 - $removed;
963 printf OUTPUT "{\n /* index */\n";
964 printf OUTPUT "%s,\n", DUMP_ARRAY( "0x%04x", 256, @filled );
965 printf OUTPUT " /* defaults */\n";
966 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, (0) x 256 );
967 $index = 0;
968 for ($i = 0; $i < 256; $i++)
970 next unless $filled[$i];
971 printf OUTPUT ",\n /* 0x%02x%02x .. 0x%02xff */\n", $i, $lowerbounds[$index], $i;
972 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0,
973 @table[($i<<8) + $lowerbounds[$index] .. ($i<<8)+255] );
974 $index++;
976 printf OUTPUT "\n};\n";
980 ################################################################
981 # dump the ctype tables
982 sub DUMP_CTYPE_TABLES
984 open OUTPUT,">wctype.c" or die "Cannot create wctype.c";
985 printf "Building wctype.c\n";
986 printf OUTPUT "/* Unicode ctype tables */\n";
987 printf OUTPUT "/* Automatically generated; DO NOT EDIT!! */\n\n";
988 printf OUTPUT "#include \"wine/unicode.h\"\n\n";
990 my $i;
991 my @array = (0) x 256;
993 # add the direction in the high 4 bits of the category
994 for ($i = 0; $i < 65536; $i++)
996 $category_table[$i] |= $direction_table[$i] << 12;
999 # try to merge table rows
1000 for ($row = 0; $row < 256; $row++)
1002 my $rowtxt = sprintf "%04x" x 256, @category_table[($row<<8)..($row<<8)+255];
1003 if (defined($sequences{$rowtxt}))
1005 # reuse an existing row
1006 $array[$row] = $sequences{$rowtxt};
1008 else
1010 # create a new row
1011 $sequences{$rowtxt} = $array[$row] = $#array + 1;
1012 push @array, @category_table[($row<<8)..($row<<8)+255];
1016 printf OUTPUT "const unsigned short wine_wctype_table[%d] =\n{\n", $#array+1;
1017 printf OUTPUT " /* offsets */\n%s,\n", DUMP_ARRAY( "0x%04x", 0, @array[0..255] );
1018 printf OUTPUT " /* values */\n%s\n};\n", DUMP_ARRAY( "0x%04x", 0, @array[256..$#array] );
1020 close OUTPUT;
1024 ################################################################
1025 # dump the char composition tables
1026 sub DUMP_COMPOSE_TABLES
1028 open OUTPUT,">compose.c" or die "Cannot create compose.c";
1029 printf "Building compose.c\n";
1030 printf OUTPUT "/* Unicode char composition */\n";
1031 printf OUTPUT "/* Automatically generated; DO NOT EDIT!! */\n\n";
1032 printf OUTPUT "#include \"wine/unicode.h\"\n\n";
1034 ######### composition table
1036 my @filled = ();
1037 foreach $i (@compose_table)
1039 my @comp = @$i;
1040 push @{$filled[$comp[1]]}, [ $comp[0], $comp[2] ];
1043 # count how many different second chars we have
1045 for ($i = $count = 0; $i < 65536; $i++)
1047 next unless defined $filled[$i];
1048 $count++;
1051 # build the table of second chars and offsets
1053 my $pos = $count + 1;
1054 for ($i = 0; $i < 65536; $i++)
1056 next unless defined $filled[$i];
1057 push @table, $i, $pos;
1058 $pos += @{$filled[$i]};
1060 # terminator with last position
1061 push @table, 0, $pos;
1062 printf OUTPUT "const WCHAR unicode_compose_table[0x%x] =\n{\n", 2*$pos;
1063 printf OUTPUT " /* second chars + offsets */\n%s", DUMP_ARRAY( "0x%04x", 0, @table );
1065 # build the table of first chars and mappings
1067 for ($i = 0; $i < 65536; $i++)
1069 next unless defined $filled[$i];
1070 my @table = ();
1071 my @list = sort { $a->[0] <=> $b->[0] } @{$filled[$i]};
1072 for ($j = 0; $j <= $#list; $j++)
1074 push @table, $list[$j][0], $list[$j][1];
1076 printf OUTPUT ",\n /* 0x%04x */\n%s", $i, DUMP_ARRAY( "0x%04x", 0, @table );
1078 printf OUTPUT "\n};\n\nconst unsigned int unicode_compose_table_size = %d;\n\n", $count;
1080 ######### decomposition table
1082 # first determine all the 16-char subsets that contain something
1084 my @filled = (0) x 4096;
1085 my $pos = 16*2; # for the null subset
1086 for ($i = 0; $i < 65536; $i++)
1088 next unless defined $decomp_table[$i];
1089 $filled[$i >> 4] = $pos;
1090 $pos += 16*2;
1091 $i |= 15;
1093 my $total = $pos;
1095 # now count the 256-char subsets that contain something
1097 my @filled_idx = (256) x 256;
1098 $pos = 256 + 16;
1099 for ($i = 0; $i < 4096; $i++)
1101 next unless $filled[$i];
1102 $filled_idx[$i >> 4] = $pos;
1103 $pos += 16;
1104 $i |= 15;
1106 my $null_offset = $pos; # null mapping
1107 $total += $pos;
1109 # add the index offsets to the subsets positions
1111 for ($i = 0; $i < 4096; $i++)
1113 next unless $filled[$i];
1114 $filled[$i] += $null_offset;
1117 # dump the main index
1119 printf OUTPUT "const WCHAR unicode_decompose_table[%d] =\n", $total;
1120 printf OUTPUT "{\n /* index */\n";
1121 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, @filled_idx );
1122 printf OUTPUT ",\n /* null sub-index */\n%s", DUMP_ARRAY( "0x%04x", 0, ($null_offset) x 16 );
1124 # dump the second-level indexes
1126 for ($i = 0; $i < 256; $i++)
1128 next unless ($filled_idx[$i] > 256);
1129 my @table = @filled[($i<<4)..($i<<4)+15];
1130 for ($j = 0; $j < 16; $j++) { $table[$j] ||= $null_offset; }
1131 printf OUTPUT ",\n /* sub-index %02x */\n", $i;
1132 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, @table );
1135 # dump the 16-char subsets
1137 printf OUTPUT ",\n /* null mapping */\n";
1138 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, (0) x 32 );
1140 for ($i = 0; $i < 4096; $i++)
1142 next unless $filled[$i];
1143 my @table = (0) x 32;
1144 for ($j = 0; $j < 16; $j++)
1146 if (defined $decomp_table[($i<<4) + $j])
1148 $table[2 * $j] = ${$decomp_table[($i << 4) + $j]}[0];
1149 $table[2 * $j + 1] = ${$decomp_table[($i << 4) + $j]}[1];
1152 printf OUTPUT ",\n /* 0x%03x0 .. 0x%03xf */\n", $i, $i;
1153 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, @table );
1156 printf OUTPUT "\n};\n";
1157 close OUTPUT;
1161 ################################################################
1162 # read an input file and generate the corresponding .c file
1163 sub HANDLE_FILE
1165 my ($codepage,$filename,$comment) = @_;
1167 # symbol codepage file is special
1168 if ($codepage == 42) { READ_SYMBOL_FILE($MAPPREFIX . $filename); }
1169 elsif ($codepage == 20932) { READ_JIS0208_FILE($MAPPREFIX . $filename); }
1170 else { READ_FILE($MAPPREFIX . $filename); }
1172 # hack: 0x00a5 must map to backslash in Shift-JIS
1173 if ($codepage == 932) { $uni2cp[0x00a5] = 0x5c; }
1175 ADD_DEFAULT_MAPPINGS();
1177 my $output = sprintf "c_%03d.c", $codepage;
1178 open OUTPUT,">$output" or die "Cannot create $output";
1180 printf "Building %s from %s (%s)\n", $output, $filename, $comment;
1182 # dump all tables
1184 printf OUTPUT "/* code page %03d (%s) */\n", $codepage, $comment;
1185 printf OUTPUT "/* generated from %s */\n", $MAPPREFIX . $filename;
1186 printf OUTPUT "/* DO NOT EDIT!! */\n\n";
1187 printf OUTPUT "#include \"wine/unicode.h\"\n\n";
1189 if ($#lead_bytes == -1) { DUMP_SBCS_TABLE( $codepage, $comment ); }
1190 else { DUMP_DBCS_TABLE( $codepage, $comment ); }
1191 close OUTPUT;
1195 ################################################################
1196 # output the list of codepage tables into the cptable.c file
1197 sub OUTPUT_CPTABLE
1199 @tables_decl = ();
1201 foreach $file (@allfiles)
1203 my ($codepage,$filename,$comment) = @$file;
1204 push @tables_decl, sprintf("extern union cptable cptable_%03d;\n",$codepage);
1207 push @tables_decl, sprintf("\nstatic const union cptable * const cptables[%d] =\n{\n",$#allfiles+1);
1208 foreach $file (@allfiles)
1210 my ($codepage,$filename,$comment) = @$file;
1211 push @tables_decl, sprintf(" &cptable_%03d,\n", $codepage);
1213 push @tables_decl, "};";
1214 REPLACE_IN_FILE( "cptable.c", @tables_decl );
1217 ################################################################
1218 # replace the contents of a file between ### cpmap ### marks
1220 sub REPLACE_IN_FILE
1222 my $name = shift;
1223 my @data = @_;
1224 my @lines = ();
1225 open(FILE,$name) or die "Can't open $name";
1226 while (<FILE>)
1228 push @lines, $_;
1229 last if /\#\#\# cpmap begin \#\#\#/;
1231 push @lines, @data;
1232 while (<FILE>)
1234 if (/\#\#\# cpmap end \#\#\#/) { push @lines, "\n", $_; last; }
1236 push @lines, <FILE>;
1237 open(FILE,">$name") or die "Can't modify $name";
1238 print FILE @lines;
1239 close(FILE);