-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathobjects.pl
225 lines (210 loc) · 5.66 KB
/
objects.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#! /usr/bin/env perl
#
# Script to generate module `deimos.openssl.object_mac`
#
# Upstream uses a script to generate `<openssl/object_mac.h>`.
# Instead of porting the header, we can simply modify this script
# to output D code. This script was first copied from openssl-3.0.3 tag,
# then modified. If it needs updating, check git history for required changes.
#
# Since the object definitions are just values, versioning for old versions
# is not required. Hence one can just update the definitions on a new release
# using the following commands (assuming CWD is the root of this repository):
# ````
# export OSSL_COPYRIGHT_YEAR=2022 # Or later
# perl -I $PTOR/util/perl/ scripts/objects.pl \
# $PTOR/crypto/objects.txt $PTOR/crypto/obj_mac.num \
# > source/deimos/openssl/obj_mac.d
# ```
# Where $PTOR is the 'path to OpenSSL repository'.
#
# Copyright 2000-2021 The OpenSSL Project Authors. All Rights Reserved.
#
# Licensed under the Apache License 2.0 (the "License"). You may not use
# this file except in compliance with the License. You can obtain a copy
# in the file LICENSE in the source distribution or at
# https://www.openssl.org/source/license.html
use Getopt::Std;
use FindBin;
use lib "$FindBin::Bin/../../util/perl";
use OpenSSL::copyright;
our($opt_n);
getopts('n');
# The year the output file is generated.
my $YEAR = OpenSSL::copyright::latest(($0, $ARGV[1], $ARGV[0]));
open (NUMIN,"$ARGV[1]") || die "Can't open number file $ARGV[1]";
$max_nid=0;
$o=0;
while(<NUMIN>)
{
s|\R$||;
$o++;
s/#.*$//;
next if /^\s*$/;
$_ = 'X'.$_;
($Cname,$mynum) = split;
$Cname =~ s/^X//;
if (defined($nidn{$mynum}))
{ die "$ARGV[1]:$o:There's already an object with NID ",$mynum," on line ",$order{$mynum},"\n"; }
if (defined($nid{$Cname}))
{ die "$ARGV[1]:$o:There's already an object with name ",$Cname," on line ",$order{$nid{$Cname}},"\n"; }
$nid{$Cname} = $mynum;
$nidn{$mynum} = $Cname;
$order{$mynum} = $o;
$max_nid = $mynum if $mynum > $max_nid;
}
close NUMIN;
open (IN,"$ARGV[0]") || die "Can't open input file $ARGV[0]";
$Cname="";
$o=0;
while (<IN>)
{
s|\R$||;
$o++;
if (/^!module\s+(.*)$/)
{
$module = $1."-";
$module =~ s/\./_/g;
$module =~ s/-/_/g;
}
if (/^!global$/)
{ $module = ""; }
if (/^!Cname\s+(.*)$/)
{ $Cname = $1; }
if (/^!Alias\s+(.+?)\s+(.*)$/)
{
$Cname = $module.$1;
$myoid = $2;
$myoid = &process_oid($myoid);
$Cname =~ s/-/_/g;
$ordern{$o} = $Cname;
$order{$Cname} = $o;
$obj{$Cname} = $myoid;
$_ = "";
$Cname = "";
}
s/!.*$//;
s/#.*$//;
next if /^\s*$/;
($myoid,$mysn,$myln) = split ':';
$mysn =~ s/^\s*//;
$mysn =~ s/\s*$//;
$myln =~ s/^\s*//;
$myln =~ s/\s*$//;
$myoid =~ s/^\s*//;
$myoid =~ s/\s*$//;
if ($myoid ne "")
{
$myoid = &process_oid($myoid);
}
if ($Cname eq "" && ($myln =~ /^[_A-Za-z][\w.-]*$/ ))
{
$Cname = $myln;
$Cname =~ s/\./_/g;
$Cname =~ s/-/_/g;
if ($Cname ne "" && defined($ln{$module.$Cname}))
{ die "objects.txt:$o:There's already an object with long name ",$ln{$module.$Cname}," on line ",$order{$module.$Cname},"\n"; }
}
if ($Cname eq "")
{
$Cname = $mysn;
$Cname =~ s/-/_/g;
if ($Cname ne "" && defined($sn{$module.$Cname}))
{ die "objects.txt:$o:There's already an object with short name ",$sn{$module.$Cname}," on line ",$order{$module.$Cname},"\n"; }
}
if ($Cname eq "")
{
$Cname = $myln;
$Cname =~ s/-/_/g;
$Cname =~ s/\./_/g;
$Cname =~ s/ /_/g;
if ($Cname ne "" && defined($ln{$module.$Cname}))
{ die "objects.txt:$o:There's already an object with long name ",$ln{$module.$Cname}," on line ",$order{$module.$Cname},"\n"; }
}
$Cname =~ s/\./_/g;
$Cname =~ s/-/_/g;
$Cname = $module.$Cname;
$ordern{$o} = $Cname;
$order{$Cname} = $o;
$sn{$Cname} = $mysn;
$ln{$Cname} = $myln;
$obj{$Cname} = $myoid;
if (!defined($nid{$Cname}))
{
$max_nid++;
$nid{$Cname} = $max_nid;
$nidn{$max_nid} = $Cname;
print STDERR "Added OID $Cname\n";
}
$Cname="";
}
close IN;
if ( $opt_n ) {
foreach (sort { $a <=> $b } keys %nidn)
{
print $nidn{$_},"\t\t",$_,"\n";
}
exit;
}
print <<"EOF";
/**
* Bindings for `<openssl/obj_mac.h>`
*
* WARNING: do not edit!
* Generated by `scripts/objects.pl`, copied and modified from the upstream
* script `crypto/objects/objects.pl`.
*
* Copyright 2000-$YEAR The OpenSSL Project Authors. All Rights Reserved.
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/
module deimos.openssl.obj_mac;
enum SN_undef = "UNDEF";
enum LN_undef = "undefined";
enum NID_undef = 0;
enum OBJ_undef = "0L";
EOF
sub expand
{
my $string = shift;
1 while $string =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e;
return $string;
}
foreach (sort { $a <=> $b } keys %ordern)
{
$Cname=$ordern{$_};
print "\n";
print expand("enum SN_$Cname = \"$sn{$Cname}\";\n") if $sn{$Cname} ne "";
print expand("enum LN_$Cname = \"$ln{$Cname}\";\n") if $ln{$Cname} ne "";
print expand("enum NID_$Cname = $nid{$Cname};\n") if $nid{$Cname} ne "";
print expand("enum OBJ_$Cname = \"$obj{$Cname}\";\n") if $obj{$Cname} ne "";
}
sub process_oid
{
local($oid)=@_;
local(@a,$oid_pref);
@a = split(/\s+/,$myoid);
$pref_oid = "";
$pref_sep = "";
if (!($a[0] =~ /^[0-9]+$/))
{
$a[0] =~ s/-/_/g;
if (!defined($obj{$a[0]}))
{ die "$ARGV[0]:$o:Undefined identifier ",$a[0],"\n"; }
$pref_oid = "OBJ_" . $a[0];
$pref_sep = ",";
shift @a;
}
$oids = join('L,',@a) . "L";
if ($oids ne "L")
{
$oids = $pref_oid . $pref_sep . $oids;
}
else
{
$oids = $pref_oid;
}
return($oids);
}