The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/lib/build_OID_registry

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: e74c9d33d7042f327d6d487001999e32


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.