build_OID_registry 4.5 KB

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