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