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