1 #! /usr/local/bin/perl -w
2 # Id: generate_nameprep_data.pl,v 1.1 2003/06/04 00:27:54 marka Exp
4 # Copyright (c) 2001 Japan Network Information Center. All rights reserved.
6 # By using this file, you agree to the terms and conditions set forth bellow.
8 # LICENSE TERMS AND CONDITIONS
10 # The following License Terms and Conditions apply, unless a different
11 # license is obtained from Japan Network Information Center ("JPNIC"),
12 # a Japanese association, Kokusai-Kougyou-Kanda Bldg 6F, 2-3-4 Uchi-Kanda,
13 # Chiyoda-ku, Tokyo 101-0047, Japan.
15 # 1. Use, Modification and Redistribution (including distribution of any
16 # modified or derived work) in source and/or binary forms is permitted
17 # under this License Terms and Conditions.
19 # 2. Redistribution of source code must retain the copyright notices as they
20 # appear in each source code file, this License Terms and Conditions.
22 # 3. Redistribution in binary form must reproduce the Copyright Notice,
23 # this License Terms and Conditions, in the documentation and/or other
24 # materials provided with the distribution. For the purposes of binary
25 # distribution the "Copyright Notice" refers to the following language:
26 # "Copyright (c) 2000-2002 Japan Network Information Center. All rights reserved."
28 # 4. The name of JPNIC may not be used to endorse or promote products
29 # derived from this Software without specific prior written approval of
32 # 5. Disclaimer/Limitation of Liability: THIS SOFTWARE IS PROVIDED BY JPNIC
33 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
35 # PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JPNIC BE LIABLE
36 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
37 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
38 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
39 # BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
40 # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
41 # OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
42 # ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
45 use v5
.6
.0; # for pack('U')
53 (my $myid = 'Id: generate_nameprep_data.pl,v 1.1 2003/06/04 00:27:54 marka Exp $') =~ s/\$([^\$]+)\$/\$-$1-\/;
55 my @map_bits = (9, 7, 5);
56 my @proh_bits = (7, 7, 7);
57 my @unas_bits = (7, 7, 7);
58 my @bidi_bits = (9, 7, 5);
60 my @bidi_types = ('OTHERS', 'R_AL', 'L');
65 GetOptions
('dir=s', \
$dir) or die usage
();
70 bits_definition
("MAP", @map_bits);
71 bits_definition
("PROH", @proh_bits);
72 bits_definition
("UNAS", @unas_bits);
73 bits_definition
("BIDI", @bidi_bits);
75 generate_data
($_) foreach @ARGV;
78 die "Usage: $0 [-dir dir] version..\n";
83 generate_mapdata
($version, "$dir/nameprep.$version.map");
84 generate_prohibiteddata
($version, "$dir/nameprep.$version.prohibited");
85 generate_unassigneddata
($version, "$dir/nameprep.$version.unassigned");
86 generate_bididata
($version, "$dir/nameprep.$version.bidi");
90 # Generate mapping data.
92 sub generate_mapdata
{
96 my $map = SparseMap
::Int
->new(BITS
=> [@map_bits],
100 open FILE
, $file or die "cannot open $file: $!\n";
102 my $mapbuf = "\0"; # dummy
105 if ($. == 1 and /^%\s*SAME-AS\s+(\S+)/) {
107 if (grep {$_ eq $same_as} @versions > 0) {
108 generate_map_ref
($version, $same_as);
116 register_map
($map, \
$mapbuf, \
%maphash, $_);
119 generate_map
($version, $map, \
$mapbuf);
123 # Generate prohibited character data.
125 sub generate_prohibiteddata
{
129 my $proh = SparseMap
::Bit
->new(BITS
=> [@proh_bits],
131 open FILE
, $file or die "cannot open $file: $!\n";
133 if ($. == 1 and /^%\s*SAME-AS\s+(\S+)/) {
135 if (grep {$_ eq $same_as} @versions > 0) {
136 generate_prohibited_ref
($version, $same_as);
144 register_prohibited
($proh, $_);
147 generate_prohibited
($version, $proh);
151 # Generate unassigned codepoint data.
153 sub generate_unassigneddata
{
157 my $unas = SparseMap
::Bit
->new(BITS
=> [@unas_bits],
159 open FILE
, $file or die "cannot open $file: $!\n";
161 if ($. == 1 and /^%\s*SAME-AS\s+(\S+)/) {
163 if (grep {$_ eq $same_as} @versions > 0) {
164 generate_unassigned_ref
($version, $same_as);
172 register_unassigned
($unas, $_);
175 generate_unassigned
($version, $unas);
179 # Generate data of bidi "R" or "AL" characters.
181 sub generate_bididata
{
185 my $bidi = SparseMap
::Int
->new(BITS
=> [@bidi_bits],
187 open FILE
, $file or die "cannot open $file: $!\n";
191 if ($. == 1 and /^%\s*SAME-AS\s+(\S+)/) {
193 if (grep {$_ eq $same_as} @versions > 0) {
194 generate_unassigned_ref
($version, $same_as);
200 if (/^%\s*BIDI_TYPE\s+(\S+)$/) {
202 for ($i = 0; $i < @bidi_types; $i++) {
203 if ($1 eq $bidi_types[$i]) {
208 die "unrecognized line: $_" if ($i >= @bidi_types);
213 register_bidi
($bidi, $type, $_);
217 generate_bidi
($version, $bidi);
225 * Do not edit this file!
226 * This file is generated from NAMEPREP specification.
232 sub bits_definition
{
237 foreach my $n (@bits) {
238 print "#define ${name}_BITS_$i\t$n\n";
245 my ($map, $bufref, $hashref, $line) = @_;
247 my ($from, $to) = split /;/, $line;
248 my @fcode = map {hex($_)} split ' ', $from;
249 my @tcode = map {hex($_)} split ' ', $to;
251 my $ucs4 = pack('V*', @tcode);
255 if (exists $hashref->{$ucs4}) {
256 $offset = $hashref->{$ucs4};
258 $offset = length $$bufref;
259 $$bufref .= pack('C', length($ucs4)) . $ucs4;
260 $hashref->{$ucs4} = $offset;
263 die "unrecognized line: $line" if @fcode != 1;
264 $map->add($fcode[0], $offset);
268 my ($version, $map, $bufref) = @_;
272 print $map->cprog(NAME
=> "nameprep_${version}_map");
273 print "\nstatic const unsigned char nameprep_${version}_map_data[] = \{\n";
274 print_uchararray
($$bufref);
278 sub generate_map_ref
{
279 my ($version, $refversion) = @_;
281 #define nameprep_${version}_map_imap nameprep_${refversion}_map_imap
282 #define nameprep_${version}_map_table nameprep_${refversion}_map_table
283 #define nameprep_${version}_map_data nameprep_${refversion}_map_data
288 sub print_uchararray
{
289 my @chars = unpack 'C*', $_[0];
291 foreach my $v (@chars) {
293 print "\n" if $i != 0;
302 sub register_prohibited
{
304 register_bitmap
($proh, @_);
307 sub register_unassigned
{
309 register_bitmap
($unas, @_);
315 register_intmap
($bidi, $type, @_);
318 sub generate_prohibited
{
319 my ($version, $proh) = @_;
320 generate_bitmap
($proh, "nameprep_${version}_prohibited");
324 sub generate_prohibited_ref
{
325 my ($version, $refversion) = @_;
327 #define nameprep_${version}_prohibited_imap nameprep_${refversion}_prohibited_imap
328 #define nameprep_${version}_prohibited_bitmap nameprep_${refversion}_prohibited_bitmap
333 sub generate_unassigned
{
334 my ($version, $unas) = @_;
335 generate_bitmap
($unas, "nameprep_${version}_unassigned");
339 sub generate_unassigned_ref
{
340 my ($version, $refversion) = @_;
342 #define nameprep_${version}_unassigned_imap nameprep_${refversion}_unassigned_imap
343 #define nameprep_${version}_unassigned_bitmap nameprep_${refversion}_unassigned_bitmap
349 my ($version, $bidi) = @_;
353 print $bidi->cprog(NAME
=> "nameprep_${version}_bidi");
355 print "static const unsigned char nameprep_${version}_bidi_data[] = \{\n";
357 foreach my $type (@bidi_types) {
358 printf "\tidn_biditype_%s, \n", lc($type);
363 sub generate_bidi_ref
{
364 my ($version, $refversion) = @_;
366 #define nameprep_${version}_bidi_imap nameprep_${refversion}_bidi_imap
367 #define nameprep_${version}_bidi_table nameprep_${refversion}_bidi_table
372 sub register_bitmap
{
376 /^([0-9A-Fa-f]+)(?:-([0-9A-Fa-f]+))?/ or die "unrecognized line: $line";
378 my $end = defined($2) ?
hex($2) : undef;
380 $map->add($start .. $end);
386 sub register_intmap
{
391 /^([0-9A-Fa-f]+)(?:-([0-9A-Fa-f]+))?/ or die "unrecognized line: $line";
393 my $end = defined($2) ?
hex($2) : $start;
394 for (my $i = $start; $i <= $end; $i++) {
395 $map->add($i, $value);
399 sub generate_bitmap
{
404 print $map->cprog(NAME
=> $name);