build_OID_registry 5.0 KB

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