build_OID_registry 4.8 KB

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