split dev_queue
[cor.git] / lib / build_OID_registry
blobd7fc32ea8ac224f0fb6f28c58b196c665e510992
1 #!/usr/bin/perl -w
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)
10 use strict;
12 my @names = ();
13 my @oids = ();
15 if ($#ARGV != 1) {
16 print STDERR "Format: ", $0, " <in-h-file> <out-c-file>\n";
17 exit(2);
21 # Open the file to read from
23 open IN_FILE, "<$ARGV[0]" || die;
24 while (<IN_FILE>) {
25 chomp;
26 if (m!\s+OID_([a-zA-z][a-zA-Z0-9_]+),\s+/[*]\s+([012][.0-9]*)\s+[*]/!) {
27 push @names, $1;
28 push @oids, $2;
31 close IN_FILE || die;
34 # Open the files to write into
36 open C_FILE, ">$ARGV[1]" or die;
37 print C_FILE "/*\n";
38 print C_FILE " * Automatically generated by ", $0, ". Do not edit\n";
39 print C_FILE " */\n";
42 # Split the data up into separate lists and also determine the lengths of the
43 # encoded data arrays.
45 my @indices = ();
46 my @lengths = ();
47 my $total_length = 0;
49 for (my $i = 0; $i <= $#names; $i++) {
50 my $name = $names[$i];
51 my $oid = $oids[$i];
53 my @components = split(/[.]/, $oid);
55 # Determine the encoded length of this OID
56 my $size = $#components;
57 for (my $loop = 2; $loop <= $#components; $loop++) {
58 my $c = $components[$loop];
60 # We will base128 encode the number
61 my $tmp = ($c == 0) ? 0 : int(log($c)/log(2));
62 $tmp = int($tmp / 7);
63 $size += $tmp;
65 push @lengths, $size;
66 push @indices, $total_length;
67 $total_length += $size;
71 # Emit the look-up-by-OID index table
73 print C_FILE "\n";
74 if ($total_length <= 255) {
75 print C_FILE "static const unsigned char oid_index[OID__NR + 1] = {\n";
76 } else {
77 print C_FILE "static const unsigned short oid_index[OID__NR + 1] = {\n";
79 for (my $i = 0; $i <= $#names; $i++) {
80 print C_FILE "\t[OID_", $names[$i], "] = ", $indices[$i], ",\n"
82 print C_FILE "\t[OID__NR] = ", $total_length, "\n";
83 print C_FILE "};\n";
86 # Encode the OIDs
88 my @encoded_oids = ();
90 for (my $i = 0; $i <= $#names; $i++) {
91 my @octets = ();
93 my @components = split(/[.]/, $oids[$i]);
95 push @octets, $components[0] * 40 + $components[1];
97 for (my $loop = 2; $loop <= $#components; $loop++) {
98 my $c = $components[$loop];
100 # Base128 encode the number
101 my $tmp = ($c == 0) ? 0 : int(log($c)/log(2));
102 $tmp = int($tmp / 7);
104 for (; $tmp > 0; $tmp--) {
105 push @octets, (($c >> $tmp * 7) & 0x7f) | 0x80;
107 push @octets, $c & 0x7f;
110 push @encoded_oids, \@octets;
114 # Create a hash value for each OID
116 my @hash_values = ();
117 for (my $i = 0; $i <= $#names; $i++) {
118 my @octets = @{$encoded_oids[$i]};
120 my $hash = $#octets;
121 foreach (@octets) {
122 $hash += $_ * 33;
125 $hash = ($hash >> 24) ^ ($hash >> 16) ^ ($hash >> 8) ^ ($hash);
127 push @hash_values, $hash & 0xff;
131 # Emit the OID data
133 print C_FILE "\n";
134 print C_FILE "static const unsigned char oid_data[", $total_length, "] = {\n";
135 for (my $i = 0; $i <= $#names; $i++) {
136 my @octets = @{$encoded_oids[$i]};
137 print C_FILE "\t";
138 print C_FILE $_, ", " foreach (@octets);
139 print C_FILE "\t// ", $names[$i];
140 print C_FILE "\n";
142 print C_FILE "};\n";
145 # Build the search index table (ordered by length then hash then content)
147 my @index_table = ( 0 .. $#names );
149 @index_table = sort {
150 my @octets_a = @{$encoded_oids[$a]};
151 my @octets_b = @{$encoded_oids[$b]};
153 return $hash_values[$a] <=> $hash_values[$b]
154 if ($hash_values[$a] != $hash_values[$b]);
155 return $#octets_a <=> $#octets_b
156 if ($#octets_a != $#octets_b);
157 for (my $i = $#octets_a; $i >= 0; $i--) {
158 return $octets_a[$i] <=> $octets_b[$i]
159 if ($octets_a[$i] != $octets_b[$i]);
161 return 0;
163 } @index_table;
166 # Emit the search index and hash value table
168 print C_FILE "\n";
169 print C_FILE "static const struct {\n";
170 print C_FILE "\tunsigned char hash;\n";
171 if ($#names <= 255) {
172 print C_FILE "\tenum OID oid : 8;\n";
173 } else {
174 print C_FILE "\tenum OID oid : 16;\n";
176 print C_FILE "} oid_search_table[OID__NR] = {\n";
177 for (my $i = 0; $i <= $#names; $i++) {
178 my @octets = @{$encoded_oids[$index_table[$i]]};
179 printf(C_FILE "\t[%3u] = { %3u, OID_%-35s }, // ",
181 $hash_values[$index_table[$i]],
182 $names[$index_table[$i]]);
183 printf C_FILE "%02x", $_ foreach (@octets);
184 print C_FILE "\n";
186 print C_FILE "};\n";
189 # Emit the OID debugging name table
191 #print C_FILE "\n";
192 #print C_FILE "const char *const oid_name_table[OID__NR + 1] = {\n";
194 #for (my $i = 0; $i <= $#names; $i++) {
195 # print C_FILE "\t\"", $names[$i], "\",\n"
197 #print C_FILE "\t\"Unknown-OID\"\n";
198 #print C_FILE "};\n";
201 # Polish off
203 close C_FILE or die;