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