1#! /usr/bin/env perl
2# Copyright 2000-2019 The OpenSSL Project Authors. All Rights Reserved.
3#
4# Licensed under the OpenSSL license (the "License").  You may not use
5# this file except in compliance with the License.  You can obtain a copy
6# in the file LICENSE in the source distribution or at
7# https://www.openssl.org/source/license.html
8
9use Getopt::Std;
10
11our($opt_n);
12getopts('n');
13
14# Output year depends on the year of the script and the input file.
15my $YEAR = [localtime([stat($0)]->[9])]->[5] + 1900;
16my $iYEAR = [localtime([stat($ARGV[0])]->[9])]->[5] + 1900;
17$YEAR = $iYEAR if $iYEAR > $YEAR;
18$iYEAR = [localtime([stat($ARGV[1])]->[9])]->[5] + 1900;
19$YEAR = $iYEAR if $iYEAR > $YEAR;
20
21open (NUMIN,"$ARGV[1]") || die "Can't open number file $ARGV[1]";
22$max_nid=0;
23$o=0;
24while(<NUMIN>)
25	{
26	s|\R$||;
27	$o++;
28	s/#.*$//;
29	next if /^\s*$/;
30	$_ = 'X'.$_;
31	($Cname,$mynum) = split;
32	$Cname =~ s/^X//;
33	if (defined($nidn{$mynum}))
34		{ die "$ARGV[1]:$o:There's already an object with NID ",$mynum," on line ",$order{$mynum},"\n"; }
35	if (defined($nid{$Cname}))
36		{ die "$ARGV[1]:$o:There's already an object with name ",$Cname," on line ",$order{$nid{$Cname}},"\n"; }
37	$nid{$Cname} = $mynum;
38	$nidn{$mynum} = $Cname;
39	$order{$mynum} = $o;
40	$max_nid = $mynum if $mynum > $max_nid;
41	}
42close NUMIN;
43
44open (IN,"$ARGV[0]") || die "Can't open input file $ARGV[0]";
45$Cname="";
46$o=0;
47while (<IN>)
48	{
49	s|\R$||;
50	$o++;
51        if (/^!module\s+(.*)$/)
52		{
53		$module = $1."-";
54		$module =~ s/\./_/g;
55		$module =~ s/-/_/g;
56		}
57        if (/^!global$/)
58		{ $module = ""; }
59	if (/^!Cname\s+(.*)$/)
60		{ $Cname = $1; }
61	if (/^!Alias\s+(.+?)\s+(.*)$/)
62		{
63		$Cname = $module.$1;
64		$myoid = $2;
65		$myoid = &process_oid($myoid);
66		$Cname =~ s/-/_/g;
67		$ordern{$o} = $Cname;
68		$order{$Cname} = $o;
69		$obj{$Cname} = $myoid;
70		$_ = "";
71		$Cname = "";
72		}
73	s/!.*$//;
74	s/#.*$//;
75	next if /^\s*$/;
76	($myoid,$mysn,$myln) = split ':';
77	$mysn =~ s/^\s*//;
78	$mysn =~ s/\s*$//;
79	$myln =~ s/^\s*//;
80	$myln =~ s/\s*$//;
81	$myoid =~ s/^\s*//;
82	$myoid =~ s/\s*$//;
83	if ($myoid ne "")
84		{
85		$myoid = &process_oid($myoid);
86		}
87
88	if ($Cname eq "" && ($myln =~ /^[_A-Za-z][\w.-]*$/ ))
89		{
90		$Cname = $myln;
91		$Cname =~ s/\./_/g;
92		$Cname =~ s/-/_/g;
93		if ($Cname ne "" && defined($ln{$module.$Cname}))
94			{ die "objects.txt:$o:There's already an object with long name ",$ln{$module.$Cname}," on line ",$order{$module.$Cname},"\n"; }
95		}
96	if ($Cname eq "")
97		{
98		$Cname = $mysn;
99		$Cname =~ s/-/_/g;
100		if ($Cname ne "" && defined($sn{$module.$Cname}))
101			{ die "objects.txt:$o:There's already an object with short name ",$sn{$module.$Cname}," on line ",$order{$module.$Cname},"\n"; }
102		}
103	if ($Cname eq "")
104		{
105		$Cname = $myln;
106		$Cname =~ s/-/_/g;
107		$Cname =~ s/\./_/g;
108		$Cname =~ s/ /_/g;
109		if ($Cname ne "" && defined($ln{$module.$Cname}))
110			{ die "objects.txt:$o:There's already an object with long name ",$ln{$module.$Cname}," on line ",$order{$module.$Cname},"\n"; }
111		}
112	$Cname =~ s/\./_/g;
113	$Cname =~ s/-/_/g;
114	$Cname = $module.$Cname;
115	$ordern{$o} = $Cname;
116	$order{$Cname} = $o;
117	$sn{$Cname} = $mysn;
118	$ln{$Cname} = $myln;
119	$obj{$Cname} = $myoid;
120	if (!defined($nid{$Cname}))
121		{
122		$max_nid++;
123		$nid{$Cname} = $max_nid;
124		$nidn{$max_nid} = $Cname;
125print STDERR "Added OID $Cname\n";
126		}
127	$Cname="";
128	}
129close IN;
130
131if ( $opt_n ) {
132    foreach (sort { $a <=> $b } keys %nidn)
133            {
134            print $nidn{$_},"\t\t",$_,"\n";
135            }
136    exit;
137}
138
139print <<"EOF";
140/*
141 * WARNING: do not edit!
142 * Generated by crypto/objects/objects.pl
143 *
144 * Copyright 2000-$YEAR The OpenSSL Project Authors. All Rights Reserved.
145 * Licensed under the OpenSSL license (the "License").  You may not use
146 * this file except in compliance with the License.  You can obtain a copy
147 * in the file LICENSE in the source distribution or at
148 * https://www.openssl.org/source/license.html
149 */
150
151#define SN_undef                        "UNDEF"
152#define LN_undef                        "undefined"
153#define NID_undef                       0
154#define OBJ_undef                       0L
155EOF
156
157sub expand
158	{
159	my $string = shift;
160
161	1 while $string =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e;
162
163	return $string;
164	}
165
166foreach (sort { $a <=> $b } keys %ordern)
167	{
168	$Cname=$ordern{$_};
169	print "\n";
170	print expand("#define SN_$Cname\t\t\"$sn{$Cname}\"\n") if $sn{$Cname} ne "";
171	print expand("#define LN_$Cname\t\t\"$ln{$Cname}\"\n") if $ln{$Cname} ne "";
172	print expand("#define NID_$Cname\t\t$nid{$Cname}\n") if $nid{$Cname} ne "";
173	print expand("#define OBJ_$Cname\t\t$obj{$Cname}\n") if $obj{$Cname} ne "";
174	}
175
176sub process_oid
177	{
178	local($oid)=@_;
179	local(@a,$oid_pref);
180
181	@a = split(/\s+/,$myoid);
182	$pref_oid = "";
183	$pref_sep = "";
184	if (!($a[0] =~ /^[0-9]+$/))
185		{
186		$a[0] =~ s/-/_/g;
187		if (!defined($obj{$a[0]}))
188			{ die "$ARGV[0]:$o:Undefined identifier ",$a[0],"\n"; }
189		$pref_oid = "OBJ_" . $a[0];
190		$pref_sep = ",";
191		shift @a;
192		}
193	$oids = join('L,',@a) . "L";
194	if ($oids ne "L")
195		{
196		$oids = $pref_oid . $pref_sep . $oids;
197		}
198	else
199		{
200		$oids = $pref_oid;
201		}
202	return($oids);
203	}
204