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