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