build_OID_registry 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. 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. my $c = $components[$loop];
  54. # We will base128 encode the number
  55. my $tmp = ($c == 0) ? 0 : int(log($c)/log(2));
  56. $tmp = int($tmp / 7);
  57. $size += $tmp;
  58. }
  59. push @lengths, $size;
  60. push @indices, $total_length;
  61. $total_length += $size;
  62. }
  63. #
  64. # Emit the look-up-by-OID index table
  65. #
  66. print C_FILE "\n";
  67. if ($total_length <= 255) {
  68. print C_FILE "static const unsigned char oid_index[OID__NR + 1] = {\n";
  69. } else {
  70. print C_FILE "static const unsigned short oid_index[OID__NR + 1] = {\n";
  71. }
  72. for (my $i = 0; $i <= $#names; $i++) {
  73. print C_FILE "\t[OID_", $names[$i], "] = ", $indices[$i], ",\n"
  74. }
  75. print C_FILE "\t[OID__NR] = ", $total_length, "\n";
  76. print C_FILE "};\n";
  77. #
  78. # Encode the OIDs
  79. #
  80. my @encoded_oids = ();
  81. for (my $i = 0; $i <= $#names; $i++) {
  82. my @octets = ();
  83. my @components = split(/[.]/, $oids[$i]);
  84. push @octets, $components[0] * 40 + $components[1];
  85. for (my $loop = 2; $loop <= $#components; $loop++) {
  86. my $c = $components[$loop];
  87. # Base128 encode the number
  88. my $tmp = ($c == 0) ? 0 : int(log($c)/log(2));
  89. $tmp = int($tmp / 7);
  90. for (; $tmp > 0; $tmp--) {
  91. push @octets, (($c >> $tmp * 7) & 0x7f) | 0x80;
  92. }
  93. push @octets, $c & 0x7f;
  94. }
  95. push @encoded_oids, \@octets;
  96. }
  97. #
  98. # Create a hash value for each OID
  99. #
  100. my @hash_values = ();
  101. for (my $i = 0; $i <= $#names; $i++) {
  102. my @octets = @{$encoded_oids[$i]};
  103. my $hash = $#octets;
  104. foreach (@octets) {
  105. $hash += $_ * 33;
  106. }
  107. $hash = ($hash >> 24) ^ ($hash >> 16) ^ ($hash >> 8) ^ ($hash);
  108. push @hash_values, $hash & 0xff;
  109. }
  110. #
  111. # Emit the OID data
  112. #
  113. print C_FILE "\n";
  114. print C_FILE "static const unsigned char oid_data[", $total_length, "] = {\n";
  115. for (my $i = 0; $i <= $#names; $i++) {
  116. my @octets = @{$encoded_oids[$i]};
  117. print C_FILE "\t";
  118. print C_FILE $_, ", " foreach (@octets);
  119. print C_FILE "\t// ", $names[$i];
  120. print C_FILE "\n";
  121. }
  122. print C_FILE "};\n";
  123. #
  124. # Build the search index table (ordered by length then hash then content)
  125. #
  126. my @index_table = ( 0 .. $#names );
  127. @index_table = sort {
  128. my @octets_a = @{$encoded_oids[$a]};
  129. my @octets_b = @{$encoded_oids[$b]};
  130. return $hash_values[$a] <=> $hash_values[$b]
  131. if ($hash_values[$a] != $hash_values[$b]);
  132. return $#octets_a <=> $#octets_b
  133. if ($#octets_a != $#octets_b);
  134. for (my $i = $#octets_a; $i >= 0; $i--) {
  135. return $octets_a[$i] <=> $octets_b[$i]
  136. if ($octets_a[$i] != $octets_b[$i]);
  137. }
  138. return 0;
  139. } @index_table;
  140. #
  141. # Emit the search index and hash value table
  142. #
  143. print C_FILE "\n";
  144. print C_FILE "static const struct {\n";
  145. print C_FILE "\tunsigned char hash;\n";
  146. if ($#names <= 255) {
  147. print C_FILE "\tenum OID oid : 8;\n";
  148. } else {
  149. print C_FILE "\tenum OID oid : 16;\n";
  150. }
  151. print C_FILE "} oid_search_table[OID__NR] = {\n";
  152. for (my $i = 0; $i <= $#names; $i++) {
  153. my @octets = @{$encoded_oids[$index_table[$i]]};
  154. printf(C_FILE "\t[%3u] = { %3u, OID_%-35s }, // ",
  155. $i,
  156. $hash_values[$index_table[$i]],
  157. $names[$index_table[$i]]);
  158. printf C_FILE "%02x", $_ foreach (@octets);
  159. print C_FILE "\n";
  160. }
  161. print C_FILE "};\n";
  162. #
  163. # Emit the OID debugging name table
  164. #
  165. #print C_FILE "\n";
  166. #print C_FILE "const char *const oid_name_table[OID__NR + 1] = {\n";
  167. #
  168. #for (my $i = 0; $i <= $#names; $i++) {
  169. # print C_FILE "\t\"", $names[$i], "\",\n"
  170. #}
  171. #print C_FILE "\t\"Unknown-OID\"\n";
  172. #print C_FILE "};\n";
  173. #
  174. # Polish off
  175. #
  176. close C_FILE or die;