2 # SPDX-License-Identifier: GPL-2.0-or-later
4 # Build a static ASN.1 Object Identified (OID) registry
6 # Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
7 # Written by David Howells (dhowells@redhat.com)
17 print STDERR
"Format: ", $0, " <in-h-file> <out-c-file>\n";
21 my $abs_srctree = abs_path
($ENV{'srctree'});
24 # Open the file to read from
26 open IN_FILE
, "<$ARGV[0]" || die;
29 if (m!\s+OID_([a-zA-z][a-zA-Z0-9_]+),\s+/[*]\s+([012][.0-9]*)\s+[*]/!) {
37 # Open the files to write into
39 open C_FILE
, ">$ARGV[1]" or die;
42 $scriptname =~ s
#^\Q$abs_srctree/\E##;
43 print C_FILE
" * Automatically generated by ", $scriptname, ". Do not edit\n";
47 # Split the data up into separate lists and also determine the lengths of the
48 # encoded data arrays.
54 for (my $i = 0; $i <= $#names; $i++) {
55 my $name = $names[$i];
58 my @components = split(/[.]/, $oid);
60 # Determine the encoded length of this OID
61 my $size = $#components;
62 for (my $loop = 2; $loop <= $#components; $loop++) {
63 my $c = $components[$loop];
65 # We will base128 encode the number
66 my $tmp = ($c == 0) ?
0 : int(log($c)/log(2));
71 push @indices, $total_length;
72 $total_length += $size;
76 # Emit the look-up-by-OID index table
79 if ($total_length <= 255) {
80 print C_FILE
"static const unsigned char oid_index[OID__NR + 1] = {\n";
82 print C_FILE
"static const unsigned short oid_index[OID__NR + 1] = {\n";
84 for (my $i = 0; $i <= $#names; $i++) {
85 print C_FILE
"\t[OID_", $names[$i], "] = ", $indices[$i], ",\n"
87 print C_FILE
"\t[OID__NR] = ", $total_length, "\n";
93 my @encoded_oids = ();
95 for (my $i = 0; $i <= $#names; $i++) {
98 my @components = split(/[.]/, $oids[$i]);
100 push @octets, $components[0] * 40 + $components[1];
102 for (my $loop = 2; $loop <= $#components; $loop++) {
103 my $c = $components[$loop];
105 # Base128 encode the number
106 my $tmp = ($c == 0) ?
0 : int(log($c)/log(2));
107 $tmp = int($tmp / 7);
109 for (; $tmp > 0; $tmp--) {
110 push @octets, (($c >> $tmp * 7) & 0x7f) | 0x80;
112 push @octets, $c & 0x7f;
115 push @encoded_oids, \
@octets;
119 # Create a hash value for each OID
121 my @hash_values = ();
122 for (my $i = 0; $i <= $#names; $i++) {
123 my @octets = @
{$encoded_oids[$i]};
130 $hash = ($hash >> 24) ^ ($hash >> 16) ^ ($hash >> 8) ^ ($hash);
132 push @hash_values, $hash & 0xff;
139 print C_FILE
"static const unsigned char oid_data[", $total_length, "] = {\n";
140 for (my $i = 0; $i <= $#names; $i++) {
141 my @octets = @
{$encoded_oids[$i]};
143 print C_FILE
$_, ", " foreach (@octets);
144 print C_FILE
"\t// ", $names[$i];
150 # Build the search index table (ordered by length then hash then content)
152 my @index_table = ( 0 .. $#names );
154 @index_table = sort {
155 my @octets_a = @
{$encoded_oids[$a]};
156 my @octets_b = @
{$encoded_oids[$b]};
158 return $hash_values[$a] <=> $hash_values[$b]
159 if ($hash_values[$a] != $hash_values[$b]);
160 return $#octets_a <=> $#octets_b
161 if ($#octets_a != $#octets_b);
162 for (my $i = $#octets_a; $i >= 0; $i--) {
163 return $octets_a[$i] <=> $octets_b[$i]
164 if ($octets_a[$i] != $octets_b[$i]);
171 # Emit the search index and hash value table
174 print C_FILE
"static const struct {\n";
175 print C_FILE
"\tunsigned char hash;\n";
176 if ($#names <= 255) {
177 print C_FILE
"\tenum OID oid : 8;\n";
179 print C_FILE
"\tenum OID oid : 16;\n";
181 print C_FILE
"} oid_search_table[OID__NR] = {\n";
182 for (my $i = 0; $i <= $#names; $i++) {
183 my @octets = @
{$encoded_oids[$index_table[$i]]};
184 printf(C_FILE
"\t[%3u] = { %3u, OID_%-35s }, // ",
186 $hash_values[$index_table[$i]],
187 $names[$index_table[$i]]);
188 printf C_FILE
"%02x", $_ foreach (@octets);
194 # Emit the OID debugging name table
197 #print C_FILE "const char *const oid_name_table[OID__NR + 1] = {\n";
199 #for (my $i = 0; $i <= $#names; $i++) {
200 # print C_FILE "\t\"", $names[$i], "\",\n"
202 #print C_FILE "\t\"Unknown-OID\"\n";
203 #print C_FILE "};\n";