Move randomness to allocated buffer
[openssl.git] / crypto / objects / objxref.pl
1 #! /usr/bin/env perl
2 # Copyright 1998-2016 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
9
10 use strict;
11
12 my %xref_tbl;
13 my %oid_tbl;
14
15 my ($mac_file, $xref_file) = @ARGV;
16
17 open(IN, $mac_file) || die "Can't open $mac_file, $!\n";
18
19 # Read in OID nid values for a lookup table.
20
21 while (<IN>)
22         {
23         s|\R$||;                # Better chomp
24         my ($name, $num) = /^(\S+)\s+(\S+)$/;
25         $oid_tbl{$name} = $num;
26         }
27 close IN;
28
29 open(IN, $xref_file) || die "Can't open $xref_file, $!\n";
30
31 my $ln = 1;
32
33 while (<IN>)
34         {
35         s|\R$||;                # Better chomp
36         s/#.*$//;
37         next if (/^\S*$/);
38         my ($xr, $p1, $p2) = /^(\S+)\s+(\S+)\s+(\S+)/;
39         check_oid($xr);
40         check_oid($p1);
41         check_oid($p2);
42         $xref_tbl{$xr} = [$p1, $p2, $ln];
43         }
44
45 my @xrkeys = keys %xref_tbl;
46
47 my @srt1 = sort { $oid_tbl{$a} <=> $oid_tbl{$b}} @xrkeys;
48
49 my $i;
50 for($i = 0; $i <= $#srt1; $i++)
51         {
52         $xref_tbl{$srt1[$i]}[2] = $i;
53         }
54
55 my @srt2 = sort
56         {
57         my$ap1 = $oid_tbl{$xref_tbl{$a}[0]};
58         my$bp1 = $oid_tbl{$xref_tbl{$b}[0]};
59         return $ap1 - $bp1 if ($ap1 != $bp1);
60         my$ap2 = $oid_tbl{$xref_tbl{$a}[1]};
61         my$bp2 = $oid_tbl{$xref_tbl{$b}[1]};
62
63         return $ap2 - $bp2;
64         } @xrkeys;
65
66 my $pname = $0;
67 $pname =~ s|.*/||;
68
69 print <<EOF;
70 /*
71  * WARNING: do not edit!
72  * Generated by $pname
73  *
74  * Copyright 1998-2016 The OpenSSL Project Authors. All Rights Reserved.
75  *
76  * Licensed under the OpenSSL license (the "License").  You may not use
77  * this file except in compliance with the License.  You can obtain a copy
78  * in the file LICENSE in the source distribution or at
79  * https://www.openssl.org/source/license.html
80  */
81
82
83 typedef struct {
84     int sign_id;
85     int hash_id;
86     int pkey_id;
87 } nid_triple;
88
89 DEFINE_STACK_OF(nid_triple)
90
91 static const nid_triple sigoid_srt[] = {
92 EOF
93
94 foreach (@srt1)
95         {
96         my $xr = $_;
97         my ($p1, $p2) = @{$xref_tbl{$_}};
98         my $o1 = "    {NID_$xr, NID_$p1,";
99         my $o2 = "NID_$p2},";
100         if (length("$o1 $o2") < 78)
101                 {
102                 print "$o1 $o2\n";
103                 }
104         else
105                 {
106                 print "$o1\n     $o2\n";
107                 }
108         }
109
110 print "};";
111 print <<EOF;
112
113
114 static const nid_triple *const sigoid_srt_xref[] = {
115 EOF
116
117 foreach (@srt2)
118         {
119         my ($p1, $p2, $x) = @{$xref_tbl{$_}};
120         # If digest or signature algorithm is "undef" then the algorithm
121         # needs special handling and is excluded from the cross reference table.
122         next if $p1 eq "undef" || $p2 eq "undef";
123         print "    \&sigoid_srt\[$x\],\n";
124         }
125
126 print "};\n";
127
128 sub check_oid
129         {
130         my ($chk) = @_;
131         if (!exists $oid_tbl{$chk})
132                 {
133                 die "Can't find \"$chk\"\n";
134                 }
135         }