obj_dat.pl 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. #!/usr/local/bin/perl
  2. # fixes bug in floating point emulation on sparc64 when
  3. # this script produces off-by-one output on sparc64
  4. use integer;
  5. sub obj_cmp
  6. {
  7. local(@a,@b,$_,$r);
  8. $A=$obj_len{$obj{$nid{$a}}};
  9. $B=$obj_len{$obj{$nid{$b}}};
  10. $r=($A-$B);
  11. return($r) if $r != 0;
  12. $A=$obj_der{$obj{$nid{$a}}};
  13. $B=$obj_der{$obj{$nid{$b}}};
  14. return($A cmp $B);
  15. }
  16. sub expand_obj
  17. {
  18. local(*v)=@_;
  19. local($k,$d);
  20. local($i);
  21. do {
  22. $i=0;
  23. foreach $k (keys %v)
  24. {
  25. if (($v{$k} =~ s/(OBJ_[^,]+),/$v{$1},/))
  26. { $i++; }
  27. }
  28. } while($i);
  29. foreach $k (keys %v)
  30. {
  31. @a=split(/,/,$v{$k});
  32. $objn{$k}=$#a+1;
  33. }
  34. return(%objn);
  35. }
  36. open (IN,"$ARGV[0]") || die "Can't open input file $ARGV[0]";
  37. open (OUT,">$ARGV[1]") || die "Can't open output file $ARGV[1]";
  38. while (<IN>)
  39. {
  40. next unless /^\#define\s+(\S+)\s+(.*)$/;
  41. $v=$1;
  42. $d=$2;
  43. $d =~ s/^\"//;
  44. $d =~ s/\"$//;
  45. if ($v =~ /^SN_(.*)$/)
  46. {
  47. if(defined $snames{$d})
  48. {
  49. print "WARNING: Duplicate short name \"$d\"\n";
  50. }
  51. else
  52. { $snames{$d} = "X"; }
  53. $sn{$1}=$d;
  54. }
  55. elsif ($v =~ /^LN_(.*)$/)
  56. {
  57. if(defined $lnames{$d})
  58. {
  59. print "WARNING: Duplicate long name \"$d\"\n";
  60. }
  61. else
  62. { $lnames{$d} = "X"; }
  63. $ln{$1}=$d;
  64. }
  65. elsif ($v =~ /^NID_(.*)$/)
  66. { $nid{$d}=$1; }
  67. elsif ($v =~ /^OBJ_(.*)$/)
  68. {
  69. $obj{$1}=$v;
  70. $objd{$v}=$d;
  71. }
  72. }
  73. close IN;
  74. %ob=&expand_obj(*objd);
  75. @a=sort { $a <=> $b } keys %nid;
  76. $n=$a[$#a]+1;
  77. @lvalues=();
  78. $lvalues=0;
  79. for ($i=0; $i<$n; $i++)
  80. {
  81. if (!defined($nid{$i}))
  82. {
  83. push(@out,"{NULL,NULL,NID_undef,0,NULL,0},\n");
  84. }
  85. else
  86. {
  87. $sn=defined($sn{$nid{$i}})?"$sn{$nid{$i}}":"NULL";
  88. $ln=defined($ln{$nid{$i}})?"$ln{$nid{$i}}":"NULL";
  89. if ($sn eq "NULL") {
  90. $sn=$ln;
  91. $sn{$nid{$i}} = $ln;
  92. }
  93. if ($ln eq "NULL") {
  94. $ln=$sn;
  95. $ln{$nid{$i}} = $sn;
  96. }
  97. $out ="{";
  98. $out.="\"$sn\"";
  99. $out.=","."\"$ln\"";
  100. $out.=",NID_$nid{$i},";
  101. if (defined($obj{$nid{$i}}))
  102. {
  103. $v=$objd{$obj{$nid{$i}}};
  104. $v =~ s/L//g;
  105. $v =~ s/,/ /g;
  106. $r=&der_it($v);
  107. $z="";
  108. $length=0;
  109. foreach (unpack("C*",$r))
  110. {
  111. $z.=sprintf("0x%02X,",$_);
  112. $length++;
  113. }
  114. $obj_der{$obj{$nid{$i}}}=$z;
  115. $obj_len{$obj{$nid{$i}}}=$length;
  116. push(@lvalues,sprintf("%-45s/* [%3d] %s */\n",
  117. $z,$lvalues,$obj{$nid{$i}}));
  118. $out.="$length,&(lvalues[$lvalues]),0";
  119. $lvalues+=$length;
  120. }
  121. else
  122. {
  123. $out.="0,NULL,0";
  124. }
  125. $out.="},\n";
  126. push(@out,$out);
  127. }
  128. }
  129. @a=grep(defined($sn{$nid{$_}}),0 .. $n);
  130. foreach (sort { $sn{$nid{$a}} cmp $sn{$nid{$b}} } @a)
  131. {
  132. push(@sn,sprintf("%2d,\t/* \"$sn{$nid{$_}}\" */\n",$_));
  133. }
  134. @a=grep(defined($ln{$nid{$_}}),0 .. $n);
  135. foreach (sort { $ln{$nid{$a}} cmp $ln{$nid{$b}} } @a)
  136. {
  137. push(@ln,sprintf("%2d,\t/* \"$ln{$nid{$_}}\" */\n",$_));
  138. }
  139. @a=grep(defined($obj{$nid{$_}}),0 .. $n);
  140. foreach (sort obj_cmp @a)
  141. {
  142. $m=$obj{$nid{$_}};
  143. $v=$objd{$m};
  144. $v =~ s/L//g;
  145. $v =~ s/,/ /g;
  146. push(@ob,sprintf("%2d,\t/* %-32s %s */\n",$_,$m,$v));
  147. }
  148. print OUT <<'EOF';
  149. /* crypto/objects/obj_dat.h */
  150. /* THIS FILE IS GENERATED FROM objects.h by obj_dat.pl via the
  151. * following command:
  152. * perl obj_dat.pl obj_mac.h obj_dat.h
  153. */
  154. /* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
  155. * All rights reserved.
  156. *
  157. * This package is an SSL implementation written
  158. * by Eric Young (eay@cryptsoft.com).
  159. * The implementation was written so as to conform with Netscapes SSL.
  160. *
  161. * This library is free for commercial and non-commercial use as long as
  162. * the following conditions are aheared to. The following conditions
  163. * apply to all code found in this distribution, be it the RC4, RSA,
  164. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  165. * included with this distribution is covered by the same copyright terms
  166. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  167. *
  168. * Copyright remains Eric Young's, and as such any Copyright notices in
  169. * the code are not to be removed.
  170. * If this package is used in a product, Eric Young should be given attribution
  171. * as the author of the parts of the library used.
  172. * This can be in the form of a textual message at program startup or
  173. * in documentation (online or textual) provided with the package.
  174. *
  175. * Redistribution and use in source and binary forms, with or without
  176. * modification, are permitted provided that the following conditions
  177. * are met:
  178. * 1. Redistributions of source code must retain the copyright
  179. * notice, this list of conditions and the following disclaimer.
  180. * 2. Redistributions in binary form must reproduce the above copyright
  181. * notice, this list of conditions and the following disclaimer in the
  182. * documentation and/or other materials provided with the distribution.
  183. * 3. All advertising materials mentioning features or use of this software
  184. * must display the following acknowledgement:
  185. * "This product includes cryptographic software written by
  186. * Eric Young (eay@cryptsoft.com)"
  187. * The word 'cryptographic' can be left out if the rouines from the library
  188. * being used are not cryptographic related :-).
  189. * 4. If you include any Windows specific code (or a derivative thereof) from
  190. * the apps directory (application code) you must include an acknowledgement:
  191. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  192. *
  193. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  194. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  195. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  196. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  197. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  198. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  199. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  200. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  201. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  202. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  203. * SUCH DAMAGE.
  204. *
  205. * The licence and distribution terms for any publically available version or
  206. * derivative of this code cannot be changed. i.e. this code cannot simply be
  207. * copied and put under another distribution licence
  208. * [including the GNU Public Licence.]
  209. */
  210. EOF
  211. printf OUT "#define NUM_NID %d\n",$n;
  212. printf OUT "#define NUM_SN %d\n",$#sn+1;
  213. printf OUT "#define NUM_LN %d\n",$#ln+1;
  214. printf OUT "#define NUM_OBJ %d\n\n",$#ob+1;
  215. printf OUT "static const unsigned char lvalues[%d]={\n",$lvalues+1;
  216. print OUT @lvalues;
  217. print OUT "};\n\n";
  218. printf OUT "static const ASN1_OBJECT nid_objs[NUM_NID]={\n";
  219. foreach (@out)
  220. {
  221. if (length($_) > 75)
  222. {
  223. $out="";
  224. foreach (split(/,/))
  225. {
  226. $t=$out.$_.",";
  227. if (length($t) > 70)
  228. {
  229. print OUT "$out\n";
  230. $t="\t$_,";
  231. }
  232. $out=$t;
  233. }
  234. chop $out;
  235. print OUT "$out";
  236. }
  237. else
  238. { print OUT $_; }
  239. }
  240. print OUT "};\n\n";
  241. printf OUT "static const unsigned int sn_objs[NUM_SN]={\n";
  242. print OUT @sn;
  243. print OUT "};\n\n";
  244. printf OUT "static const unsigned int ln_objs[NUM_LN]={\n";
  245. print OUT @ln;
  246. print OUT "};\n\n";
  247. printf OUT "static const unsigned int obj_objs[NUM_OBJ]={\n";
  248. print OUT @ob;
  249. print OUT "};\n\n";
  250. close OUT;
  251. sub der_it
  252. {
  253. local($v)=@_;
  254. local(@a,$i,$ret,@r);
  255. @a=split(/\s+/,$v);
  256. $ret.=pack("C*",$a[0]*40+$a[1]);
  257. shift @a;
  258. shift @a;
  259. foreach (@a)
  260. {
  261. @r=();
  262. $t=0;
  263. while ($_ >= 128)
  264. {
  265. $x=$_%128;
  266. $_/=128;
  267. push(@r,((($t++)?0x80:0)|$x));
  268. }
  269. push(@r,((($t++)?0x80:0)|$_));
  270. $ret.=pack("C*",reverse(@r));
  271. }
  272. return($ret);
  273. }