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