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