advapi32/tests: Replace an '#if 0' with an 'if (0)'.
[wine.git] / libs / wine / cpmap.pl
blob0a9e7d13d50a61cfba0af9f4cf0710edf54155b2
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 [ 424, "VENDORS/MISC/CP424.TXT", "IBM EBCDIC Hebrew" ],
42 [ 437, "VENDORS/MICSFT/PC/CP437.TXT", "OEM United States" ],
43 [ 500, "VENDORS/MICSFT/EBCDIC/CP500.TXT", "IBM EBCDIC International" ],
44 [ 737, "VENDORS/MICSFT/PC/CP737.TXT", "OEM Greek 437G" ],
45 [ 775, "VENDORS/MICSFT/PC/CP775.TXT", "OEM Baltic" ],
46 [ 850, "VENDORS/MICSFT/PC/CP850.TXT", "OEM Multilingual Latin 1" ],
47 [ 852, "VENDORS/MICSFT/PC/CP852.TXT", "OEM Slovak Latin 2" ],
48 [ 855, "VENDORS/MICSFT/PC/CP855.TXT", "OEM Cyrillic" ],
49 [ 856, "VENDORS/MISC/CP856.TXT", "Hebrew PC" ],
50 [ 857, "VENDORS/MICSFT/PC/CP857.TXT", "OEM Turkish" ],
51 [ 860, "VENDORS/MICSFT/PC/CP860.TXT", "OEM Portuguese" ],
52 [ 861, "VENDORS/MICSFT/PC/CP861.TXT", "OEM Icelandic" ],
53 [ 862, "VENDORS/MICSFT/PC/CP862.TXT", "OEM Hebrew" ],
54 [ 863, "VENDORS/MICSFT/PC/CP863.TXT", "OEM Canadian French" ],
55 [ 864, "VENDORS/MICSFT/PC/CP864.TXT", "OEM Arabic" ],
56 [ 865, "VENDORS/MICSFT/PC/CP865.TXT", "OEM Nordic" ],
57 [ 866, "VENDORS/MICSFT/PC/CP866.TXT", "OEM Russian" ],
58 [ 869, "VENDORS/MICSFT/PC/CP869.TXT", "OEM Greek" ],
59 [ 874, "VENDORS/MICSFT/PC/CP874.TXT", "ANSI/OEM Thai" ],
60 [ 875, "VENDORS/MICSFT/EBCDIC/CP875.TXT", "IBM EBCDIC Greek" ],
61 [ 878, "VENDORS/MISC/KOI8-R.TXT", "Russian KOI8" ],
62 [ 932, "VENDORS/MICSFT/WINDOWS/CP932.TXT", "ANSI/OEM Japanese Shift-JIS" ],
63 [ 936, "VENDORS/MICSFT/WINDOWS/CP936.TXT", "ANSI/OEM Simplified Chinese GBK" ],
64 [ 949, "VENDORS/MICSFT/WINDOWS/CP949.TXT", "ANSI/OEM Korean Unified Hangul" ],
65 [ 950, "VENDORS/MICSFT/WINDOWS/CP950.TXT", "ANSI/OEM Traditional Chinese Big5" ],
66 [ 1006, "VENDORS/MISC/CP1006.TXT", "IBM Arabic" ],
67 [ 1026, "VENDORS/MICSFT/EBCDIC/CP1026.TXT", "IBM EBCDIC Latin 5 Turkish" ],
68 [ 1250, "VENDORS/MICSFT/WINDOWS/CP1250.TXT", "ANSI Eastern Europe" ],
69 [ 1251, "VENDORS/MICSFT/WINDOWS/CP1251.TXT", "ANSI Cyrillic" ],
70 [ 1252, "VENDORS/MICSFT/WINDOWS/CP1252.TXT", "ANSI Latin 1" ],
71 [ 1253, "VENDORS/MICSFT/WINDOWS/CP1253.TXT", "ANSI Greek" ],
72 [ 1254, "VENDORS/MICSFT/WINDOWS/CP1254.TXT", "ANSI Turkish" ],
73 [ 1255, "VENDORS/MICSFT/WINDOWS/CP1255.TXT", "ANSI Hebrew" ],
74 [ 1256, "VENDORS/MICSFT/WINDOWS/CP1256.TXT", "ANSI Arabic" ],
75 [ 1257, "VENDORS/MICSFT/WINDOWS/CP1257.TXT", "ANSI Baltic" ],
76 [ 1258, "VENDORS/MICSFT/WINDOWS/CP1258.TXT", "ANSI/OEM Viet Nam" ],
77 [ 10000, "VENDORS/MICSFT/MAC/ROMAN.TXT", "Mac Roman" ],
78 [ 10006, "VENDORS/MICSFT/MAC/GREEK.TXT", "Mac Greek" ],
79 [ 10007, "VENDORS/MICSFT/MAC/CYRILLIC.TXT", "Mac Cyrillic" ],
80 [ 10029, "VENDORS/MICSFT/MAC/LATIN2.TXT", "Mac Latin 2" ],
81 [ 10079, "VENDORS/MICSFT/MAC/ICELAND.TXT", "Mac Icelandic" ],
82 [ 10081, "VENDORS/MICSFT/MAC/TURKISH.TXT", "Mac Turkish" ],
83 [ 20127, "VENDORS/MISC/US-ASCII-QUOTES.TXT", "US-ASCII (7bit)" ],
84 [ 20866, "VENDORS/MISC/KOI8-R.TXT", "Russian KOI8" ],
85 [ 20932, "OBSOLETE/EASTASIA/JIS/JIS0208.TXT", "EUC-JP" ],
86 [ 21866, "VENDORS/MISC/KOI8-U.TXT", "Ukrainian KOI8" ],
87 [ 28591, "ISO8859/8859-1.TXT", "ISO 8859-1 Latin 1" ],
88 [ 28592, "ISO8859/8859-2.TXT", "ISO 8859-2 Latin 2 (East European)" ],
89 [ 28593, "ISO8859/8859-3.TXT", "ISO 8859-3 Latin 3 (South European)" ],
90 [ 28594, "ISO8859/8859-4.TXT", "ISO 8859-4 Latin 4 (Baltic old)" ],
91 [ 28595, "ISO8859/8859-5.TXT", "ISO 8859-5 Cyrillic" ],
92 [ 28596, "ISO8859/8859-6.TXT", "ISO 8859-6 Arabic" ],
93 [ 28597, "ISO8859/8859-7.TXT", "ISO 8859-7 Greek" ],
94 [ 28598, "ISO8859/8859-8.TXT", "ISO 8859-8 Hebrew" ],
95 [ 28599, "ISO8859/8859-9.TXT", "ISO 8859-9 Latin 5 (Turkish)" ],
96 [ 28600, "ISO8859/8859-10.TXT", "ISO 8859-10 Latin 6 (Nordic)" ],
97 [ 28603, "ISO8859/8859-13.TXT", "ISO 8859-13 Latin 7 (Baltic)" ],
98 [ 28604, "ISO8859/8859-14.TXT", "ISO 8859-14 Latin 8 (Celtic)" ],
99 [ 28605, "ISO8859/8859-15.TXT", "ISO 8859-15 Latin 9 (Euro)" ],
100 [ 28606, "ISO8859/8859-16.TXT", "ISO 8859-16 Latin 10 (Balkan)" ]
104 %ctype =
106 "upper" => 0x0001,
107 "lower" => 0x0002,
108 "digit" => 0x0004,
109 "space" => 0x0008,
110 "punct" => 0x0010,
111 "cntrl" => 0x0020,
112 "blank" => 0x0040,
113 "xdigit" => 0x0080,
114 "alpha" => 0x0100
117 %categories =
119 "Lu" => $ctype{"alpha"}|$ctype{"upper"}, # Letter, Uppercase
120 "Ll" => $ctype{"alpha"}|$ctype{"lower"}, # Letter, Lowercase
121 "Lt" => $ctype{"alpha"}, # Letter, Titlecase
122 "Mn" => $ctype{"punct"}, # Mark, Non-Spacing
123 "Mc" => $ctype{"punct"}, # Mark, Spacing Combining
124 "Me" => $ctype{"punct"}, # Mark, Enclosing
125 "Nd" => $ctype{"digit"}, # Number, Decimal Digit
126 "Nl" => $ctype{"punct"}, # Number, Letter
127 "No" => $ctype{"punct"}, # Number, Other
128 "Zs" => $ctype{"space"}, # Separator, Space
129 "Zl" => $ctype{"space"}, # Separator, Line
130 "Zp" => $ctype{"space"}, # Separator, Paragraph
131 "Cc" => $ctype{"cntrl"}, # Other, Control
132 "Cf" => 0, # Other, Format
133 "Cs" => 0, # Other, Surrogate
134 "Co" => 0, # Other, Private Use
135 "Cn" => 0, # Other, Not Assigned
136 "Lm" => $ctype{"punct"}, # Letter, Modifier
137 "Lo" => $ctype{"alpha"}, # Letter, Other
138 "Pc" => $ctype{"punct"}, # Punctuation, Connector
139 "Pd" => $ctype{"punct"}, # Punctuation, Dash
140 "Ps" => $ctype{"punct"}, # Punctuation, Open
141 "Pe" => $ctype{"punct"}, # Punctuation, Close
142 "Pi" => $ctype{"punct"}, # Punctuation, Initial quote
143 "Pf" => $ctype{"punct"}, # Punctuation, Final quote
144 "Po" => $ctype{"punct"}, # Punctuation, Other
145 "Sm" => $ctype{"punct"}, # Symbol, Math
146 "Sc" => $ctype{"punct"}, # Symbol, Currency
147 "Sk" => $ctype{"punct"}, # Symbol, Modifier
148 "So" => $ctype{"punct"} # Symbol, Other
151 # a few characters need additional categories that cannot be determined automatically
152 %special_categories =
154 "xdigit" => [ ord('0')..ord('9'),ord('A')..ord('F'),ord('a')..ord('f'),
155 0xff10..0xff19, 0xff21..0xff26, 0xff41..0xff46 ],
156 "space" => [ 0x09..0x0d, 0x85 ],
157 "blank" => [ 0x09, 0x20, 0xa0, 0x3000, 0xfeff ],
158 "cntrl" => [ 0x070f, 0x180b, 0x180c, 0x180d, 0x180e, 0x200c, 0x200d,
159 0x200e, 0x200f, 0x202a, 0x202b, 0x202c, 0x202d, 0x202e,
160 0x206a, 0x206b, 0x206c, 0x206d, 0x206e, 0x206f, 0xfeff,
161 0xfff9, 0xfffa, 0xfffb ]
164 %directions =
166 "L" => 1, # Left-to-Right
167 "LRE" => 11, # Left-to-Right Embedding
168 "LRO" => 11, # Left-to-Right Override
169 "R" => 2, # Right-to-Left
170 "AL" => 2, # Right-to-Left Arabic
171 "RLE" => 11, # Right-to-Left Embedding
172 "RLO" => 11, # Right-to-Left Override
173 "PDF" => 11, # Pop Directional Format
174 "EN" => 3, # European Number
175 "ES" => 4, # European Number Separator
176 "ET" => 5, # European Number Terminator
177 "AN" => 6, # Arabic Number
178 "CS" => 7, # Common Number Separator
179 "NSM" => 0, # Non-Spacing Mark
180 "BN" => 0, # Boundary Neutral
181 "B" => 8, # Paragraph Separator
182 "S" => 9, # Segment Separator
183 "WS" => 10, # Whitespace
184 "ON" => 11 # Other Neutrals
188 ################################################################
189 # main routine
191 READ_DEFAULTS();
192 my @sortkeys = READ_SORTKEYS_FILE();
193 DUMP_CASE_MAPPINGS();
194 DUMP_SORTKEYS(@sortkeys);
195 DUMP_COMPOSE_TABLES();
196 DUMP_CTYPE_TABLES();
198 foreach $file (@allfiles) { HANDLE_FILE( @$file ); }
200 OUTPUT_CPTABLE();
202 exit(0);
205 ################################################################
206 # read in the defaults file
207 sub READ_DEFAULTS
209 @unicode_defaults = ();
210 @unicode_aliases = ();
211 @tolower_table = ();
212 @toupper_table = ();
213 @digitmap_table = ();
214 @compatmap_table = ();
215 @category_table = ();
216 @direction_table = ();
217 @decomp_table = ();
218 @compose_table = ();
220 # first setup a few default mappings
222 open DEFAULTS or die "Cannot open $DEFAULTS";
223 print "Loading $DEFAULTS\n";
224 while (<DEFAULTS>)
226 next if /^\#/; # skip comments
227 next if /^$/; # skip empty lines
228 if (/^(([0-9a-fA-F]+)(,[0-9a-fA-F]+)*)\s+([0-9a-fA-F]+|'.'|none)\s+(\#.*)?/)
230 my @src = map hex, split /,/,$1;
231 my $dst = $4;
232 my $comment = $5;
233 if ($#src > 0) { push @unicode_aliases, \@src; }
234 next if ($dst eq "none");
235 $dst = ($dst =~ /\'.\'/) ? ord substr($dst,1,1) : hex $dst;
236 foreach $src (@src)
238 die "Duplicate value" if defined($unicode_defaults[$src]);
239 $unicode_defaults[$src] = $dst;
241 next;
243 die "Unrecognized line $_\n";
246 # now build mappings from the decomposition field of the Unicode database
248 open UNICODEDATA or die "Cannot open $UNICODEDATA";
249 print "Loading $UNICODEDATA\n";
250 while (<UNICODEDATA>)
252 # Decode the fields ...
253 ($code, $name, $cat, $comb, $bidi,
254 $decomp, $dec, $dig, $num, $mirror,
255 $oldname, $comment, $upper, $lower, $title) = split /;/;
257 my $src = hex $code;
259 die "unknown category $cat" unless defined $categories{$cat};
260 die "unknown directionality $bidi" unless defined $directions{$bidi};
262 $uniname[$src] = $name;
263 $category_table[$src] = $categories{$cat};
264 $direction_table[$src] = $directions{$bidi};
266 if ($lower ne "")
268 $tolower_table[$src] = hex $lower;
269 $category_table[$src] |= $ctype{"upper"}|$ctype{"alpha"};
271 if ($upper ne "")
273 $toupper_table[$src] = hex $upper;
274 $category_table[$src] |= $ctype{"lower"}|$ctype{"alpha"};
276 if ($dec ne "")
278 $category_table[$src] |= $ctype{"digit"};
280 if ($dig ne "")
282 $digitmap_table[$src] = ord $dig;
285 # copy the category and direction for everything between First/Last pairs
286 if ($name =~ /, First>/) { $start = $src; }
287 if ($name =~ /, Last>/)
289 while ($start < $src)
291 $category_table[$start] = $category_table[$src];
292 $direction_table[$start] = $direction_table[$src];
293 $start++;
297 next if $decomp eq ""; # no decomposition, skip it
299 if ($decomp =~ /^<([a-zA-Z]+)>\s+([0-9a-fA-F]+)$/)
301 # decomposition of the form "<foo> 1234" -> use char if type is known
302 if (($src >= 0xf900 && $src < 0xfb00) || ($src >= 0xfe30 && $src < 0xfffd))
304 # Single char decomposition in the compatibility range
305 $compatmap_table[$src] = hex $2;
307 next unless ($1 eq "font" ||
308 $1 eq "noBreak" ||
309 $1 eq "circle" ||
310 $1 eq "super" ||
311 $1 eq "sub" ||
312 $1 eq "wide" ||
313 $1 eq "narrow" ||
314 $1 eq "compat" ||
315 $1 eq "small");
316 $dst = hex $2;
318 elsif ($decomp =~ /^<compat>\s+0020\s+([0-9a-fA-F]+)/)
320 # decomposition "<compat> 0020 1234" -> combining accent
321 $dst = hex $1;
323 elsif ($decomp =~ /^([0-9a-fA-F]+)/)
325 # decomposition contains only char values without prefix -> use first char
326 $dst = hex $1;
327 $category_table[$src] |= $category_table[$dst];
328 # store decomposition if it contains two chars
329 if ($decomp =~ /^([0-9a-fA-F]+)\s+([0-9a-fA-F]+)$/)
331 $decomp_table[$src] = [ hex $1, hex $2 ];
332 push @compose_table, [ hex $1, hex $2, $src ];
334 elsif ($decomp =~ /^(<[a-z]+>\s)*([0-9a-fA-F]+)$/ &&
335 (($src >= 0xf900 && $src < 0xfb00) || ($src >= 0xfe30 && $src < 0xfffd)))
337 # Single char decomposition in the compatibility range
338 $compatmap_table[$src] = hex $2;
341 else
343 next;
346 next if defined($unicode_defaults[$src]); # may have been set in the defaults file
348 # check for loops
349 for ($i = $dst; ; $i = $unicode_defaults[$i])
351 die sprintf("loop detected for %04x -> %04x",$src,$dst) if $i == $src;
352 last unless defined($unicode_defaults[$i]);
354 $unicode_defaults[$src] = $dst;
357 # patch the category of some special characters
359 foreach $cat (keys %special_categories)
361 my $flag = $ctype{$cat};
362 foreach $i (@{$special_categories{$cat}}) { $category_table[$i] |= $flag; }
367 ################################################################
368 # parse the input file
369 sub READ_FILE
371 my $name = shift;
372 open INPUT,$name or die "Cannot open $name";
373 @cp2uni = ();
374 @lead_bytes = ();
375 @uni2cp = ();
377 while (<INPUT>)
379 next if /^\#/; # skip comments
380 next if /^$/; # skip empty lines
381 next if /\x1a/; # skip ^Z
382 next if (/^0x([0-9a-fA-F]+)\s+\#UNDEFINED/); # undefined char
384 if (/^0x([0-9a-fA-F]+)\s+\#DBCS LEAD BYTE/)
386 $cp = hex $1;
387 push @lead_bytes,$cp;
388 $cp2uni[$cp] = 0;
389 next;
391 if (/^0x([0-9a-fA-F]+)\s+0x([0-9a-fA-F]+)\s+(\#.*)?/)
393 $cp = hex $1;
394 $uni = hex $2;
395 $cp2uni[$cp] = $uni unless defined($cp2uni[$cp]);
396 $uni2cp[$uni] = $cp unless defined($uni2cp[$uni]);
397 next;
399 die "$name: Unrecognized line $_\n";
404 ################################################################
405 # build EUC-JP table from the JIS 0208 file
406 # FIXME: for proper EUC-JP we should probably read JIS 0212 too
407 # but this would require 3-byte DBCS characters
408 sub READ_JIS0208_FILE
410 my $name = shift;
411 @cp2uni = ();
412 @lead_bytes = ();
413 @uni2cp = ();
415 # ASCII chars
416 for ($i = 0x00; $i <= 0x7f; $i++)
418 $cp2uni[$i] = $i;
419 $uni2cp[$i] = $i;
422 # JIS X 0201 right plane
423 for ($i = 0xa1; $i <= 0xdf; $i++)
425 $cp2uni[0x8e00 + $i] = 0xfec0 + $i;
426 $uni2cp[0xfec0 + $i] = 0x8e00 + $i;
429 # lead bytes
430 foreach $i (0x8e, 0x8f, 0xa1 .. 0xfe)
432 push @lead_bytes,$i;
433 $cp2uni[$i] = 0;
436 # undefined chars
437 foreach $i (0x80 .. 0x8d, 0x90 .. 0xa0, 0xff)
439 $cp2uni[$i] = $DEF_CHAR;
442 # Shift-JIS compatibility
443 $uni2cp[0x00a5] = 0x5c;
444 $uni2cp[0x203e] = 0x7e;
446 # Fix backslash conversion
447 $cp2uni[0xa1c0] = 0xff3c;
448 $uni2cp[0xff3c] = 0xa1c0;
450 open INPUT, "$name" or die "Cannot open $name";
451 while (<INPUT>)
453 next if /^\#/; # skip comments
454 next if /^$/; # skip empty lines
455 next if /\x1a/; # skip ^Z
456 if (/^0x[0-9a-fA-F]+\s+0x([0-9a-fA-F]+)\s+0x([0-9a-fA-F]+)\s+(\#.*)?/)
458 $cp = 0x8080 + hex $1;
459 $uni = hex $2;
460 $cp2uni[$cp] = $uni unless defined($cp2uni[$cp]);
461 $uni2cp[$uni] = $cp unless defined($uni2cp[$uni]);
462 next;
464 die "$name: Unrecognized line $_\n";
469 ################################################################
470 # build the sort keys table
471 sub READ_SORTKEYS_FILE
473 my @sortkeys = ();
474 for (my $i = 0; $i < 65536; $i++) { $sortkeys[$i] = [ -1, 0, 0, 0, 0 ] };
476 open INPUT, "$SORTKEYS" or die "Cannot open $SORTKEYS";
477 print "Loading $SORTKEYS\n";
478 while (<INPUT>)
480 next if /^\#/; # skip comments
481 next if /^$/; # skip empty lines
482 next if /\x1a/; # skip ^Z
483 next if /^\@version/; # skip @version header
484 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]+)\]/)
486 my ($uni,$variable) = (hex $1, $2);
487 next if $uni > 65535;
488 $sortkeys[$uni] = [ $uni, hex $3, hex $4, hex $5, hex $6 ];
489 next;
491 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]+)\]/)
493 # multiple character sequence, ignored for now
494 next;
496 die "$SORTKEYS: Unrecognized line $_\n";
498 close INPUT;
500 # compress the keys to 32 bit:
501 # key 1 to 16 bits, key 2 to 8 bits, key 3 to 4 bits, key 4 to 1 bit
503 @sortkeys = sort { ${$a}[1] <=> ${$b}[1] or
504 ${$a}[2] <=> ${$b}[2] or
505 ${$a}[3] <=> ${$b}[3] or
506 ${$a}[4] <=> ${$b}[4] or
507 $a cmp $b; } @sortkeys;
509 my ($n2, $n3) = (1, 1);
510 my @keys = (-1, -1, -1, -1, -1 );
511 my @flatkeys = ();
513 for (my $i = 0; $i < 65536; $i++)
515 my @current = @{$sortkeys[$i]};
516 next if $current[0] == -1;
517 if ($current[1] == $keys[1])
519 if ($current[2] == $keys[2])
521 if ($current[3] == $keys[3])
523 # nothing
525 else
527 $keys[3] = $current[3];
528 $n3++;
529 die if ($n3 >= 16);
532 else
534 $keys[2] = $current[2];
535 $keys[3] = $current[3];
536 $n2++;
537 $n3 = 1;
538 die if ($n2 >= 256);
541 else
543 $keys[1] = $current[1];
544 $keys[2] = $current[2];
545 $keys[3] = $current[3];
546 $n2 = 1;
547 $n3 = 1;
550 if ($current[2]) { $current[2] = $n2; }
551 if ($current[3]) { $current[3] = $n3; }
552 if ($current[4]) { $current[4] = 1; }
554 $flatkeys[$current[0]] = ($current[1] << 16) | ($current[2] << 8) | ($current[3] << 4) | $current[4];
556 return @flatkeys;
560 ################################################################
561 # build the sort keys table
562 sub DUMP_SORTKEYS
564 my @keys = @_;
566 # count the number of 256-key ranges that contain something
568 my @offsets = ();
569 my $ranges = 2;
570 for (my $i = 0; $i < 256; $i++) { $offsets[$i] = 256; }
571 for (my $i = 0; $i < 65536; $i++)
573 next unless defined $keys[$i];
574 $offsets[$i >> 8] = $ranges * 256;
575 $ranges++;
576 $i |= 255;
579 # output the range offsets
581 open OUTPUT,">collation.c.new" or die "Cannot create collation.c";
582 printf "Building collation.c\n";
583 printf OUTPUT "/* Unicode collation element table */\n";
584 printf OUTPUT "/* generated from %s */\n", $SORTKEYS;
585 printf OUTPUT "/* DO NOT EDIT!! */\n\n";
587 printf OUTPUT "const unsigned int collation_table[%d] =\n{\n", $ranges*256;
588 printf OUTPUT " /* index */\n";
589 printf OUTPUT "%s,\n", DUMP_ARRAY( "0x%08x", 0, @offsets );
591 # output the default values
593 printf OUTPUT " /* defaults */\n";
594 printf OUTPUT "%s", DUMP_ARRAY( "0x%08x", 0, (-1) x 256 );
596 # output all the key ranges
598 for (my $i = 0; $i < 256; $i++)
600 next if $offsets[$i] == 256;
601 printf OUTPUT ",\n /* 0x%02x00 .. 0x%02xff */\n", $i, $i;
602 printf OUTPUT "%s", DUMP_ARRAY( "0x%08x", -1, @keys[($i<<8) .. ($i<<8)+255] );
604 printf OUTPUT "\n};\n";
605 close OUTPUT;
606 save_file("collation.c");
610 ################################################################
611 # add default mappings once the file had been read
612 sub ADD_DEFAULT_MAPPINGS
614 # Apply aliases
616 foreach $alias (@unicode_aliases)
618 my $target = undef;
619 foreach $src (@$alias)
621 if (defined($uni2cp[$src]))
623 $target = $uni2cp[$src];
624 last;
627 next unless defined($target);
629 # At least one char of the alias set is defined, set the others to the same value
630 foreach $src (@$alias)
632 $uni2cp[$src] = $target unless defined($uni2cp[$src]);
636 # For every src -> target mapping in the defaults table,
637 # make uni2cp[src] = uni2cp[target] if uni2cp[target] is defined
639 for ($src = 0; $src < 65536; $src++)
641 next if defined($uni2cp[$src]); # source has a definition already
642 next unless defined($unicode_defaults[$src]); # no default for this char
643 my $target = $unicode_defaults[$src];
645 # do a recursive mapping until we find a target char that is defined
646 while (!defined($uni2cp[$target]) &&
647 defined($unicode_defaults[$target])) { $target = $unicode_defaults[$target]; }
649 if (defined($uni2cp[$target])) { $uni2cp[$src] = $uni2cp[$target]; }
652 # Add an identity mapping for all undefined chars
654 for ($i = 0; $i < 256; $i++)
656 next if defined($cp2uni[$i]);
657 next if defined($uni2cp[$i]);
658 $cp2uni[$i] = $uni2cp[$i] = $i;
662 ################################################################
663 # dump an array of integers
664 sub DUMP_ARRAY
666 my ($format,$default,@array) = @_;
667 my $i, $ret = " ";
668 for ($i = 0; $i < $#array; $i++)
670 $ret .= sprintf($format, defined $array[$i] ? $array[$i] : $default);
671 $ret .= (($i % 8) != 7) ? ", " : ",\n ";
673 $ret .= sprintf($format, defined $array[$i] ? $array[$i] : $default);
674 return $ret;
677 ################################################################
678 # dump an SBCS mapping table
679 sub DUMP_SBCS_TABLE
681 my ($codepage, $name) = @_;
682 my $i;
684 # output the ascii->unicode table
686 printf OUTPUT "static const WCHAR cp2uni[256] =\n";
687 printf OUTPUT "{\n%s\n};\n\n", DUMP_ARRAY( "0x%04x", $DEF_CHAR, @cp2uni[0 .. 255] );
689 # count the number of unicode->ascii subtables that contain something
691 my @filled = ();
692 my $subtables = 1;
693 for ($i = 0; $i < 65536; $i++)
695 next unless defined $uni2cp[$i];
696 $filled[$i >> 8] = 1;
697 $subtables++;
698 $i |= 255;
701 # output all the subtables into a single array
703 printf OUTPUT "static const unsigned char uni2cp_low[%d] =\n{\n", $subtables*256;
704 for ($i = 0; $i < 256; $i++)
706 next unless $filled[$i];
707 printf OUTPUT " /* 0x%02x00 .. 0x%02xff */\n", $i, $i;
708 printf OUTPUT "%s,\n", DUMP_ARRAY( "0x%02x", $DEF_CHAR, @uni2cp[($i<<8) .. ($i<<8)+255] );
710 printf OUTPUT " /* defaults */\n";
711 printf OUTPUT "%s\n};\n\n", DUMP_ARRAY( "0x%02x", 0, ($DEF_CHAR) x 256 );
713 # output a table of the offsets of the subtables in the previous array
715 my $pos = 0;
716 my @offsets = ();
717 for ($i = 0; $i < 256; $i++)
719 if ($filled[$i]) { push @offsets, $pos; $pos += 256; }
720 else { push @offsets, ($subtables-1) * 256; }
722 printf OUTPUT "static const unsigned short uni2cp_high[256] =\n";
723 printf OUTPUT "{\n%s\n};\n\n", DUMP_ARRAY( "0x%04x", 0, @offsets );
725 # output the code page descriptor
727 printf OUTPUT "const struct sbcs_table cptable_%03d =\n{\n", $codepage;
728 printf OUTPUT " { %d, 1, 0x%04x, 0x%04x, \"%s\" },\n",
729 $codepage, $DEF_CHAR, $DEF_CHAR, $name;
730 printf OUTPUT " cp2uni,\n";
731 printf OUTPUT " uni2cp_low,\n";
732 printf OUTPUT " uni2cp_high\n};\n";
736 ################################################################
737 # dump a DBCS mapping table
738 sub DUMP_DBCS_TABLE
740 my ($codepage, $name) = @_;
741 my $i, $x, $y;
743 # build a list of lead bytes that are actually used
745 my @lblist = ();
746 LBLOOP: for ($y = 0; $y <= $#lead_bytes; $y++)
748 my $base = $lead_bytes[$y] << 8;
749 for ($x = 0; $x < 256; $x++)
751 if (defined $cp2uni[$base+$x])
753 push @lblist,$lead_bytes[$y];
754 next LBLOOP;
758 my $unused = ($#lead_bytes > $#lblist);
760 # output the ascii->unicode table for the single byte chars
762 printf OUTPUT "static const WCHAR cp2uni[%d] =\n", 256 * ($#lblist + 2 + $unused);
763 printf OUTPUT "{\n%s,\n", DUMP_ARRAY( "0x%04x", $DEF_CHAR, @cp2uni[0 .. 255] );
765 # output the default table for unused lead bytes
767 if ($unused)
769 printf OUTPUT " /* unused lead bytes */\n";
770 printf OUTPUT "%s,\n", DUMP_ARRAY( "0x%04x", 0, ($DEF_CHAR) x 256 );
773 # output the ascii->unicode table for each DBCS lead byte
775 for ($y = 0; $y <= $#lblist; $y++)
777 my $base = $lblist[$y] << 8;
778 printf OUTPUT " /* lead byte %02x */\n", $lblist[$y];
779 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", $DEF_CHAR, @cp2uni[$base .. $base+255] );
780 printf OUTPUT ($y < $#lblist) ? ",\n" : "\n};\n\n";
783 # output the lead byte subtables offsets
785 my @offsets = ();
786 for ($x = 0; $x < 256; $x++) { $offsets[$x] = 0; }
787 for ($x = 0; $x <= $#lblist; $x++) { $offsets[$lblist[$x]] = $x + 1; }
788 if ($unused)
790 # increment all lead bytes offset to take into account the unused table
791 for ($x = 0; $x <= $#lead_bytes; $x++) { $offsets[$lead_bytes[$x]]++; }
793 printf OUTPUT "static const unsigned char cp2uni_leadbytes[256] =\n";
794 printf OUTPUT "{\n%s\n};\n\n", DUMP_ARRAY( "0x%02x", 0, @offsets );
796 # count the number of unicode->ascii subtables that contain something
798 my @filled = ();
799 my $subtables = 1;
800 for ($i = 0; $i < 65536; $i++)
802 next unless defined $uni2cp[$i];
803 $filled[$i >> 8] = 1;
804 $subtables++;
805 $i |= 255;
808 # output all the subtables into a single array
810 printf OUTPUT "static const unsigned short uni2cp_low[%d] =\n{\n", $subtables*256;
811 for ($y = 0; $y < 256; $y++)
813 next unless $filled[$y];
814 printf OUTPUT " /* 0x%02x00 .. 0x%02xff */\n", $y, $y;
815 printf OUTPUT "%s,\n", DUMP_ARRAY( "0x%04x", $DEF_CHAR, @uni2cp[($y<<8) .. ($y<<8)+255] );
817 printf OUTPUT " /* defaults */\n";
818 printf OUTPUT "%s\n};\n\n", DUMP_ARRAY( "0x%04x", 0, ($DEF_CHAR) x 256 );
820 # output a table of the offsets of the subtables in the previous array
822 my $pos = 0;
823 my @offsets = ();
824 for ($y = 0; $y < 256; $y++)
826 if ($filled[$y]) { push @offsets, $pos; $pos += 256; }
827 else { push @offsets, ($subtables-1) * 256; }
829 printf OUTPUT "static const unsigned short uni2cp_high[256] =\n";
830 printf OUTPUT "{\n%s\n};\n\n", DUMP_ARRAY( "0x%04x", 0, @offsets );
832 # output the code page descriptor
834 printf OUTPUT "const struct dbcs_table cptable_%03d =\n{\n", $codepage;
835 printf OUTPUT " { %d, 2, 0x%04x, 0x%04x, \"%s\" },\n",
836 $codepage, $DEF_CHAR, $DEF_CHAR, $name;
837 printf OUTPUT " cp2uni,\n";
838 printf OUTPUT " cp2uni_leadbytes,\n";
839 printf OUTPUT " uni2cp_low,\n";
840 printf OUTPUT " uni2cp_high,\n";
841 DUMP_LB_RANGES();
842 printf OUTPUT "};\n";
846 ################################################################
847 # dump the list of defined lead byte ranges
848 sub DUMP_LB_RANGES
850 my @list = ();
851 my $i = 0;
852 foreach $i (@lead_bytes) { $list[$i] = 1; }
853 my $on = 0;
854 printf OUTPUT " { ";
855 for ($i = 0; $i < 256; $i++)
857 if ($on)
859 if (!defined $list[$i]) { printf OUTPUT "0x%02x, ", $i-1; $on = 0; }
861 else
863 if ($list[$i]) { printf OUTPUT "0x%02x, ", $i; $on = 1; }
866 if ($on) { printf OUTPUT "0xff, "; }
867 printf OUTPUT "0x00, 0x00 }\n";
871 ################################################################
872 # dump the case mapping tables
873 sub DUMP_CASE_MAPPINGS
875 open OUTPUT,">casemap.c.new" or die "Cannot create casemap.c";
876 printf "Building casemap.c\n";
877 printf OUTPUT "/* Unicode case mappings */\n";
878 printf OUTPUT "/* Automatically generated; DO NOT EDIT!! */\n\n";
879 printf OUTPUT "#include \"wine/unicode.h\"\n\n";
881 DUMP_CASE_TABLE( "wine_casemap_lower", @tolower_table );
882 DUMP_CASE_TABLE( "wine_casemap_upper", @toupper_table );
883 DUMP_CASE_TABLE( "wine_digitmap", @digitmap_table );
884 DUMP_CASE_TABLE( "wine_compatmap", @compatmap_table );
885 close OUTPUT;
886 save_file("casemap.c");
890 ################################################################
891 # dump a case mapping table
892 sub DUMP_CASE_TABLE
894 my ($name,@table) = @_;
896 # count the number of sub tables that contain something
897 # also compute the low and upper populated bounds
899 my @lowerbounds = ( 0, 0 );
900 my @upperbounds = ( 0, 255 );
901 my $index = 0;
902 my @filled = ();
903 for ($i = 0; $i < 65536; $i++)
905 next unless defined $table[$i];
906 if (!defined $filled[$i >> 8])
908 $lowerbounds[$index] = $i & 0xff;
909 $upperbounds[$index] = 0xff - $lowerbounds[$index];
910 $filled[$i >> 8] = $index * 256 + 512;
911 $index++;
913 else
915 $upperbounds[$index-1] = 0xff - ($i & 0xff);
917 $table[$i] = ($table[$i] - $i) & 0xffff;
920 # Collapse blocks upwards if possible
921 my $removed = 0;
922 $index = 0;
923 for ($i = 0; $i < 256; $i++)
925 next unless defined $filled[$i];
926 if ($upperbounds[$index - 1] > $lowerbounds[$index])
928 $removed = $removed + $lowerbounds[$index];
930 else
932 $removed = $removed + $upperbounds[$index - 1];
933 $lowerbounds[$index] = $upperbounds[$index - 1];
935 $filled[$i] = $filled[$i] - $removed;
936 $index++;
939 # dump the table
941 printf OUTPUT "const WCHAR %s[%d] =\n", $name, $index * 256 + 512 - $removed;
942 printf OUTPUT "{\n /* index */\n";
943 printf OUTPUT "%s,\n", DUMP_ARRAY( "0x%04x", 256, @filled );
944 printf OUTPUT " /* defaults */\n";
945 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, (0) x 256 );
946 $index = 0;
947 for ($i = 0; $i < 256; $i++)
949 next unless $filled[$i];
950 printf OUTPUT ",\n /* 0x%02x%02x .. 0x%02xff */\n", $i, $lowerbounds[$index], $i;
951 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0,
952 @table[($i<<8) + $lowerbounds[$index] .. ($i<<8)+255] );
953 $index++;
955 printf OUTPUT "\n};\n";
959 ################################################################
960 # dump the ctype tables
961 sub DUMP_CTYPE_TABLES
963 open OUTPUT,">wctype.c.new" or die "Cannot create wctype.c";
964 printf "Building wctype.c\n";
965 printf OUTPUT "/* Unicode ctype tables */\n";
966 printf OUTPUT "/* Automatically generated; DO NOT EDIT!! */\n\n";
967 printf OUTPUT "#include \"wine/unicode.h\"\n\n";
969 my $i;
970 my @array = (0) x 256;
972 # add the direction in the high 4 bits of the category
973 for ($i = 0; $i < 65536; $i++)
975 $category_table[$i] |= $direction_table[$i] << 12;
978 # try to merge table rows
979 for ($row = 0; $row < 256; $row++)
981 my $rowtxt = sprintf "%04x" x 256, @category_table[($row<<8)..($row<<8)+255];
982 if (defined($sequences{$rowtxt}))
984 # reuse an existing row
985 $array[$row] = $sequences{$rowtxt};
987 else
989 # create a new row
990 $sequences{$rowtxt} = $array[$row] = $#array + 1;
991 push @array, @category_table[($row<<8)..($row<<8)+255];
995 printf OUTPUT "const unsigned short wine_wctype_table[%d] =\n{\n", $#array+1;
996 printf OUTPUT " /* offsets */\n%s,\n", DUMP_ARRAY( "0x%04x", 0, @array[0..255] );
997 printf OUTPUT " /* values */\n%s\n};\n", DUMP_ARRAY( "0x%04x", 0, @array[256..$#array] );
999 close OUTPUT;
1000 save_file("wctype.c");
1004 ################################################################
1005 # dump the char composition tables
1006 sub DUMP_COMPOSE_TABLES
1008 open OUTPUT,">compose.c.new" or die "Cannot create compose.c";
1009 printf "Building compose.c\n";
1010 printf OUTPUT "/* Unicode char composition */\n";
1011 printf OUTPUT "/* Automatically generated; DO NOT EDIT!! */\n\n";
1012 printf OUTPUT "#include \"wine/unicode.h\"\n\n";
1014 ######### composition table
1016 my @filled = ();
1017 foreach $i (@compose_table)
1019 my @comp = @$i;
1020 push @{$filled[$comp[1]]}, [ $comp[0], $comp[2] ];
1023 # count how many different second chars we have
1025 for ($i = $count = 0; $i < 65536; $i++)
1027 next unless defined $filled[$i];
1028 $count++;
1031 # build the table of second chars and offsets
1033 my $pos = $count + 1;
1034 for ($i = 0; $i < 65536; $i++)
1036 next unless defined $filled[$i];
1037 push @table, $i, $pos;
1038 $pos += @{$filled[$i]};
1040 # terminator with last position
1041 push @table, 0, $pos;
1042 printf OUTPUT "const WCHAR unicode_compose_table[0x%x] =\n{\n", 2*$pos;
1043 printf OUTPUT " /* second chars + offsets */\n%s", DUMP_ARRAY( "0x%04x", 0, @table );
1045 # build the table of first chars and mappings
1047 for ($i = 0; $i < 65536; $i++)
1049 next unless defined $filled[$i];
1050 my @table = ();
1051 my @list = sort { $a->[0] <=> $b->[0] } @{$filled[$i]};
1052 for ($j = 0; $j <= $#list; $j++)
1054 push @table, $list[$j][0], $list[$j][1];
1056 printf OUTPUT ",\n /* 0x%04x */\n%s", $i, DUMP_ARRAY( "0x%04x", 0, @table );
1058 printf OUTPUT "\n};\n\nconst unsigned int unicode_compose_table_size = %d;\n\n", $count;
1060 ######### decomposition table
1062 # first determine all the 16-char subsets that contain something
1064 my @filled = (0) x 4096;
1065 my $pos = 16*2; # for the null subset
1066 for ($i = 0; $i < 65536; $i++)
1068 next unless defined $decomp_table[$i];
1069 $filled[$i >> 4] = $pos;
1070 $pos += 16*2;
1071 $i |= 15;
1073 my $total = $pos;
1075 # now count the 256-char subsets that contain something
1077 my @filled_idx = (256) x 256;
1078 $pos = 256 + 16;
1079 for ($i = 0; $i < 4096; $i++)
1081 next unless $filled[$i];
1082 $filled_idx[$i >> 4] = $pos;
1083 $pos += 16;
1084 $i |= 15;
1086 my $null_offset = $pos; # null mapping
1087 $total += $pos;
1089 # add the index offsets to the subsets positions
1091 for ($i = 0; $i < 4096; $i++)
1093 next unless $filled[$i];
1094 $filled[$i] += $null_offset;
1097 # dump the main index
1099 printf OUTPUT "const WCHAR unicode_decompose_table[%d] =\n", $total;
1100 printf OUTPUT "{\n /* index */\n";
1101 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, @filled_idx );
1102 printf OUTPUT ",\n /* null sub-index */\n%s", DUMP_ARRAY( "0x%04x", 0, ($null_offset) x 16 );
1104 # dump the second-level indexes
1106 for ($i = 0; $i < 256; $i++)
1108 next unless ($filled_idx[$i] > 256);
1109 my @table = @filled[($i<<4)..($i<<4)+15];
1110 for ($j = 0; $j < 16; $j++) { $table[$j] ||= $null_offset; }
1111 printf OUTPUT ",\n /* sub-index %02x */\n", $i;
1112 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, @table );
1115 # dump the 16-char subsets
1117 printf OUTPUT ",\n /* null mapping */\n";
1118 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, (0) x 32 );
1120 for ($i = 0; $i < 4096; $i++)
1122 next unless $filled[$i];
1123 my @table = (0) x 32;
1124 for ($j = 0; $j < 16; $j++)
1126 if (defined $decomp_table[($i<<4) + $j])
1128 $table[2 * $j] = ${$decomp_table[($i << 4) + $j]}[0];
1129 $table[2 * $j + 1] = ${$decomp_table[($i << 4) + $j]}[1];
1132 printf OUTPUT ",\n /* 0x%03x0 .. 0x%03xf */\n", $i, $i;
1133 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, @table );
1136 printf OUTPUT "\n};\n";
1137 close OUTPUT;
1138 save_file("compose.c");
1142 ################################################################
1143 # read an input file and generate the corresponding .c file
1144 sub HANDLE_FILE
1146 my ($codepage,$filename,$comment) = @_;
1148 # symbol codepage file is special
1149 if ($codepage == 20932) { READ_JIS0208_FILE($MAPPREFIX . $filename); }
1150 else { READ_FILE($MAPPREFIX . $filename); }
1152 # hack: 0x00a5 must map to backslash in Shift-JIS
1153 if ($codepage == 932) { $uni2cp[0x00a5] = 0x5c; }
1155 ADD_DEFAULT_MAPPINGS();
1157 my $output = sprintf "c_%03d.c", $codepage;
1158 open OUTPUT,">$output.new" or die "Cannot create $output";
1160 printf "Building %s from %s (%s)\n", $output, $filename, $comment;
1162 # dump all tables
1164 printf OUTPUT "/* code page %03d (%s) */\n", $codepage, $comment;
1165 printf OUTPUT "/* generated from %s */\n", $MAPPREFIX . $filename;
1166 printf OUTPUT "/* DO NOT EDIT!! */\n\n";
1167 printf OUTPUT "#include \"wine/unicode.h\"\n\n";
1169 if ($#lead_bytes == -1) { DUMP_SBCS_TABLE( $codepage, $comment ); }
1170 else { DUMP_DBCS_TABLE( $codepage, $comment ); }
1171 close OUTPUT;
1172 save_file($output);
1176 ################################################################
1177 # save a file if modified
1178 sub save_file($)
1180 my $file = shift;
1181 if (!system "cmp $file $file.new >/dev/null")
1183 unlink "$file.new";
1185 else
1187 rename "$file.new", "$file";
1192 ################################################################
1193 # output the list of codepage tables into the cptable.c file
1194 sub OUTPUT_CPTABLE
1196 @tables_decl = ();
1198 foreach $file (@allfiles)
1200 my ($codepage,$filename,$comment) = @$file;
1201 push @tables_decl, sprintf("extern union cptable cptable_%03d;\n",$codepage);
1204 push @tables_decl, sprintf("\nstatic const union cptable * const cptables[%d] =\n{\n",$#allfiles+1);
1205 foreach $file (@allfiles)
1207 my ($codepage,$filename,$comment) = @$file;
1208 push @tables_decl, sprintf(" &cptable_%03d,\n", $codepage);
1210 push @tables_decl, "};";
1211 REPLACE_IN_FILE( "cptable.c", @tables_decl );
1214 ################################################################
1215 # replace the contents of a file between ### cpmap ### marks
1217 sub REPLACE_IN_FILE
1219 my $name = shift;
1220 my @data = @_;
1221 my @lines = ();
1222 open(FILE,$name) or die "Can't open $name";
1223 while (<FILE>)
1225 push @lines, $_;
1226 last if /\#\#\# cpmap begin \#\#\#/;
1228 push @lines, @data;
1229 while (<FILE>)
1231 if (/\#\#\# cpmap end \#\#\#/) { push @lines, "\n", $_; last; }
1233 push @lines, <FILE>;
1234 open(FILE,">$name.new") or die "Can't modify $name";
1235 print FILE @lines;
1236 close(FILE);
1237 save_file($name);