Linux 4.9.243
[linux/fpc-iii.git] / lib / build_OID_registry
blob5d982721736071675be1574cc13ccba8bcbfa52c
1 #!/usr/bin/perl -w
3 # Build a static ASN.1 Object Identified (OID) registry
5 # Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
6 # Written by David Howells (dhowells@redhat.com)
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public Licence
10 # as published by the Free Software Foundation; either version
11 # 2 of the Licence, or (at your option) any later version.
14 use strict;
16 my @names = ();
17 my @oids = ();
19 if ($#ARGV != 1) {
20 print STDERR "Format: ", $0, " <in-h-file> <out-c-file>\n";
21 exit(2);
25 # Open the file to read from
27 open IN_FILE, "<$ARGV[0]" || die;
28 while (<IN_FILE>) {
29 chomp;
30 if (m!\s+OID_([a-zA-z][a-zA-Z0-9_]+),\s+/[*]\s+([012][.0-9]*)\s+[*]/!) {
31 push @names, $1;
32 push @oids, $2;
35 close IN_FILE || die;
38 # Open the files to write into
40 open C_FILE, ">$ARGV[1]" or die;
41 print C_FILE "/*\n";
42 print C_FILE " * Automatically generated by ", $0, ". Do not edit\n";
43 print C_FILE " */\n";
46 # Split the data up into separate lists and also determine the lengths of the
47 # encoded data arrays.
49 my @indices = ();
50 my @lengths = ();
51 my $total_length = 0;
53 for (my $i = 0; $i <= $#names; $i++) {
54 my $name = $names[$i];
55 my $oid = $oids[$i];
57 my @components = split(/[.]/, $oid);
59 # Determine the encoded length of this OID
60 my $size = $#components;
61 for (my $loop = 2; $loop <= $#components; $loop++) {
62 my $c = $components[$loop];
64 # We will base128 encode the number
65 my $tmp = ($c == 0) ? 0 : int(log($c)/log(2));
66 $tmp = int($tmp / 7);
67 $size += $tmp;
69 push @lengths, $size;
70 push @indices, $total_length;
71 $total_length += $size;
75 # Emit the look-up-by-OID index table
77 print C_FILE "\n";
78 if ($total_length <= 255) {
79 print C_FILE "static const unsigned char oid_index[OID__NR + 1] = {\n";
80 } else {
81 print C_FILE "static const unsigned short oid_index[OID__NR + 1] = {\n";
83 for (my $i = 0; $i <= $#names; $i++) {
84 print C_FILE "\t[OID_", $names[$i], "] = ", $indices[$i], ",\n"
86 print C_FILE "\t[OID__NR] = ", $total_length, "\n";
87 print C_FILE "};\n";
90 # Encode the OIDs
92 my @encoded_oids = ();
94 for (my $i = 0; $i <= $#names; $i++) {
95 my @octets = ();
97 my @components = split(/[.]/, $oids[$i]);
99 push @octets, $components[0] * 40 + $components[1];
101 for (my $loop = 2; $loop <= $#components; $loop++) {
102 my $c = $components[$loop];
104 # Base128 encode the number
105 my $tmp = ($c == 0) ? 0 : int(log($c)/log(2));
106 $tmp = int($tmp / 7);
108 for (; $tmp > 0; $tmp--) {
109 push @octets, (($c >> $tmp * 7) & 0x7f) | 0x80;
111 push @octets, $c & 0x7f;
114 push @encoded_oids, \@octets;
118 # Create a hash value for each OID
120 my @hash_values = ();
121 for (my $i = 0; $i <= $#names; $i++) {
122 my @octets = @{$encoded_oids[$i]};
124 my $hash = $#octets;
125 foreach (@octets) {
126 $hash += $_ * 33;
129 $hash = ($hash >> 24) ^ ($hash >> 16) ^ ($hash >> 8) ^ ($hash);
131 push @hash_values, $hash & 0xff;
135 # Emit the OID data
137 print C_FILE "\n";
138 print C_FILE "static const unsigned char oid_data[", $total_length, "] = {\n";
139 for (my $i = 0; $i <= $#names; $i++) {
140 my @octets = @{$encoded_oids[$i]};
141 print C_FILE "\t";
142 print C_FILE $_, ", " foreach (@octets);
143 print C_FILE "\t// ", $names[$i];
144 print C_FILE "\n";
146 print C_FILE "};\n";
149 # Build the search index table (ordered by length then hash then content)
151 my @index_table = ( 0 .. $#names );
153 @index_table = sort {
154 my @octets_a = @{$encoded_oids[$a]};
155 my @octets_b = @{$encoded_oids[$b]};
157 return $hash_values[$a] <=> $hash_values[$b]
158 if ($hash_values[$a] != $hash_values[$b]);
159 return $#octets_a <=> $#octets_b
160 if ($#octets_a != $#octets_b);
161 for (my $i = $#octets_a; $i >= 0; $i--) {
162 return $octets_a[$i] <=> $octets_b[$i]
163 if ($octets_a[$i] != $octets_b[$i]);
165 return 0;
167 } @index_table;
170 # Emit the search index and hash value table
172 print C_FILE "\n";
173 print C_FILE "static const struct {\n";
174 print C_FILE "\tunsigned char hash;\n";
175 if ($#names <= 255) {
176 print C_FILE "\tenum OID oid : 8;\n";
177 } else {
178 print C_FILE "\tenum OID oid : 16;\n";
180 print C_FILE "} oid_search_table[OID__NR] = {\n";
181 for (my $i = 0; $i <= $#names; $i++) {
182 my @octets = @{$encoded_oids[$index_table[$i]]};
183 printf(C_FILE "\t[%3u] = { %3u, OID_%-35s }, // ",
185 $hash_values[$index_table[$i]],
186 $names[$index_table[$i]]);
187 printf C_FILE "%02x", $_ foreach (@octets);
188 print C_FILE "\n";
190 print C_FILE "};\n";
193 # Emit the OID debugging name table
195 #print C_FILE "\n";
196 #print C_FILE "const char *const oid_name_table[OID__NR + 1] = {\n";
198 #for (my $i = 0; $i <= $#names; $i++) {
199 # print C_FILE "\t\"", $names[$i], "\",\n"
201 #print C_FILE "\t\"Unknown-OID\"\n";
202 #print C_FILE "};\n";
205 # Polish off
207 close C_FILE or die;