xref: /openbmc/linux/lib/build_OID_registry (revision 2ae1beb3)
1#!/usr/bin/perl -w
2# SPDX-License-Identifier: GPL-2.0-or-later
3#
4# Build a static ASN.1 Object Identified (OID) registry
5#
6# Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
7# Written by David Howells (dhowells@redhat.com)
8#
9
10use strict;
11use Cwd qw(abs_path);
12
13my @names = ();
14my @oids = ();
15
16if ($#ARGV != 1) {
17    print STDERR "Format: ", $0, " <in-h-file> <out-c-file>\n";
18    exit(2);
19}
20
21my $abs_srctree = abs_path($ENV{'srctree'});
22
23#
24# Open the file to read from
25#
26open IN_FILE, "<$ARGV[0]" || die;
27while (<IN_FILE>) {
28    chomp;
29    if (m!\s+OID_([a-zA-z][a-zA-Z0-9_]+),\s+/[*]\s+([012][.0-9]*)\s+[*]/!) {
30	push @names, $1;
31	push @oids, $2;
32    }
33}
34close IN_FILE || die;
35
36#
37# Open the files to write into
38#
39open C_FILE, ">$ARGV[1]" or die;
40print C_FILE "/*\n";
41print C_FILE " * Automatically generated by ", $0 =~ s#^\Q$abs_srctree/\E##r, ".  Do not edit\n";
42print C_FILE " */\n";
43
44#
45# Split the data up into separate lists and also determine the lengths of the
46# encoded data arrays.
47#
48my @indices = ();
49my @lengths = ();
50my $total_length = 0;
51
52for (my $i = 0; $i <= $#names; $i++) {
53    my $name = $names[$i];
54    my $oid = $oids[$i];
55
56    my @components = split(/[.]/, $oid);
57
58    # Determine the encoded length of this OID
59    my $size = $#components;
60    for (my $loop = 2; $loop <= $#components; $loop++) {
61	my $c = $components[$loop];
62
63	# We will base128 encode the number
64	my $tmp = ($c == 0) ? 0 : int(log($c)/log(2));
65	$tmp = int($tmp / 7);
66	$size += $tmp;
67    }
68    push @lengths, $size;
69    push @indices, $total_length;
70    $total_length += $size;
71}
72
73#
74# Emit the look-up-by-OID index table
75#
76print C_FILE "\n";
77if ($total_length <= 255) {
78    print C_FILE "static const unsigned char oid_index[OID__NR + 1] = {\n";
79} else {
80    print C_FILE "static const unsigned short oid_index[OID__NR + 1] = {\n";
81}
82for (my $i = 0; $i <= $#names; $i++) {
83    print C_FILE "\t[OID_", $names[$i], "] = ", $indices[$i], ",\n"
84}
85print C_FILE "\t[OID__NR] = ", $total_length, "\n";
86print C_FILE "};\n";
87
88#
89# Encode the OIDs
90#
91my @encoded_oids = ();
92
93for (my $i = 0; $i <= $#names; $i++) {
94    my @octets = ();
95
96    my @components = split(/[.]/, $oids[$i]);
97
98    push @octets, $components[0] * 40 + $components[1];
99
100    for (my $loop = 2; $loop <= $#components; $loop++) {
101	my $c = $components[$loop];
102
103	# Base128 encode the number
104	my $tmp = ($c == 0) ? 0 : int(log($c)/log(2));
105	$tmp = int($tmp / 7);
106
107	for (; $tmp > 0; $tmp--) {
108	    push @octets, (($c >> $tmp * 7) & 0x7f) | 0x80;
109	}
110	push @octets, $c & 0x7f;
111    }
112
113    push @encoded_oids, \@octets;
114}
115
116#
117# Create a hash value for each OID
118#
119my @hash_values = ();
120for (my $i = 0; $i <= $#names; $i++) {
121    my @octets = @{$encoded_oids[$i]};
122
123    my $hash = $#octets;
124    foreach (@octets) {
125	$hash += $_ * 33;
126    }
127
128    $hash = ($hash >> 24) ^ ($hash >> 16) ^ ($hash >> 8) ^ ($hash);
129
130    push @hash_values, $hash & 0xff;
131}
132
133#
134# Emit the OID data
135#
136print C_FILE "\n";
137print C_FILE "static const unsigned char oid_data[", $total_length, "] = {\n";
138for (my $i = 0; $i <= $#names; $i++) {
139    my @octets = @{$encoded_oids[$i]};
140    print C_FILE "\t";
141    print C_FILE $_, ", " foreach (@octets);
142    print C_FILE "\t// ", $names[$i];
143    print C_FILE "\n";
144}
145print C_FILE "};\n";
146
147#
148# Build the search index table (ordered by length then hash then content)
149#
150my @index_table = ( 0 .. $#names );
151
152@index_table = sort {
153    my @octets_a = @{$encoded_oids[$a]};
154    my @octets_b = @{$encoded_oids[$b]};
155
156    return $hash_values[$a] <=> $hash_values[$b]
157	if ($hash_values[$a] != $hash_values[$b]);
158    return $#octets_a <=> $#octets_b
159	if ($#octets_a != $#octets_b);
160    for (my $i = $#octets_a; $i >= 0; $i--) {
161	return $octets_a[$i] <=> $octets_b[$i]
162	    if ($octets_a[$i] != $octets_b[$i]);
163    }
164    return 0;
165
166} @index_table;
167
168#
169# Emit the search index and hash value table
170#
171print C_FILE "\n";
172print C_FILE "static const struct {\n";
173print C_FILE "\tunsigned char hash;\n";
174if ($#names <= 255) {
175    print C_FILE "\tenum OID oid : 8;\n";
176} else {
177    print C_FILE "\tenum OID oid : 16;\n";
178}
179print C_FILE "} oid_search_table[OID__NR] = {\n";
180for (my $i = 0; $i <= $#names; $i++) {
181    my @octets = @{$encoded_oids[$index_table[$i]]};
182    printf(C_FILE "\t[%3u] = { %3u, OID_%-35s }, // ",
183	   $i,
184	   $hash_values[$index_table[$i]],
185	   $names[$index_table[$i]]);
186    printf C_FILE "%02x", $_ foreach (@octets);
187    print C_FILE "\n";
188}
189print C_FILE "};\n";
190
191#
192# Emit the OID debugging name table
193#
194#print C_FILE "\n";
195#print C_FILE "const char *const oid_name_table[OID__NR + 1] = {\n";
196#
197#for (my $i = 0; $i <= $#names; $i++) {
198#    print C_FILE "\t\"", $names[$i], "\",\n"
199#}
200#print C_FILE "\t\"Unknown-OID\"\n";
201#print C_FILE "};\n";
202
203#
204# Polish off
205#
206close C_FILE or die;
207