Make objxref.pl output in correct format
[openssl.git] / crypto / objects / objxref.pl
1 #!/usr/local/bin/perl
2
3 use strict;
4
5 my %xref_tbl;
6 my %oid_tbl;
7
8 my ($mac_file, $xref_file) = @ARGV;
9
10 open(IN, $mac_file) || die "Can't open $mac_file";
11
12 # Read in OID nid values for a lookup table.
13
14 while (<IN>)
15         {
16         chomp;
17         my ($name, $num) = /^(\S+)\s+(\S+)$/;
18         $oid_tbl{$name} = $num;
19         }
20 close IN;
21
22 open(IN, $xref_file) || die "Can't open $xref_file";
23
24 my $ln = 1;
25
26 while (<IN>)
27         {
28         chomp;
29         s/#.*$//;
30         next if (/^\S*$/);
31         my ($xr, $p1, $p2) = /^(\S+)\s+(\S+)\s+(\S+)/;
32         check_oid($xr);
33         check_oid($p1);
34         check_oid($p2);
35         $xref_tbl{$xr} = [$p1, $p2, $ln];
36         }
37
38 my @xrkeys = keys %xref_tbl;
39
40 my @srt1 = sort { $oid_tbl{$a} <=> $oid_tbl{$b}} @xrkeys;
41
42 my $i;
43 for($i = 0; $i <= $#srt1; $i++)
44         {
45         $xref_tbl{$srt1[$i]}[2] = $i;
46         }
47
48 my @srt2 = sort
49         {
50         my$ap1 = $oid_tbl{$xref_tbl{$a}[0]};
51         my$bp1 = $oid_tbl{$xref_tbl{$b}[0]};
52         return $ap1 - $bp1 if ($ap1 != $bp1);
53         my$ap2 = $oid_tbl{$xref_tbl{$a}[1]};
54         my$bp2 = $oid_tbl{$xref_tbl{$b}[1]};
55
56         return $ap2 - $bp2;
57         } @xrkeys;
58
59 my $pname = $0;
60
61 $pname =~ s|^.[^/]/||;
62
63 print <<EOF;
64 /* AUTOGENERATED BY $pname, DO NOT EDIT */
65
66 typedef struct {
67     int sign_id;
68     int hash_id;
69     int pkey_id;
70 } nid_triple;
71
72 static const nid_triple sigoid_srt[] = {
73 EOF
74
75 foreach (@srt1)
76         {
77         my $xr = $_;
78         my ($p1, $p2) = @{$xref_tbl{$_}};
79         my $o1 = "    {NID_$xr, NID_$p1,";
80         my $o2 = "NID_$p2},";
81         if (length("$o1 $o2") < 78)
82                 {
83                 print "$o1 $o2\n";
84                 }
85         else
86                 {
87                 print "$o1\n     $o2\n";
88                 }
89         }
90
91 print "};";
92 print <<EOF;
93
94
95 static const nid_triple *const sigoid_srt_xref[] = {
96 EOF
97
98 foreach (@srt2)
99         {
100         my ($p1, $p2, $x) = @{$xref_tbl{$_}};
101         # If digest or signature algorithm is "undef" then the algorithm
102         # needs special handling and is excluded from the cross reference table.
103         next if $p1 eq "undef" || $p2 eq "undef";
104         print "    \&sigoid_srt\[$x\],\n";
105         }
106
107 print "};\n";
108
109 sub check_oid
110         {
111         my ($chk) = @_;
112         if (!exists $oid_tbl{$chk})
113                 {
114                 die "Not Found \"$chk\"\n";
115                 }
116         }