objects.pl 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #! /usr/bin/env perl
  2. # Copyright 2000-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the OpenSSL license (the "License"). You may not use
  5. # this file except in compliance with the License. You can obtain a copy
  6. # in the file LICENSE in the source distribution or at
  7. # https://www.openssl.org/source/license.html
  8. use Getopt::Std;
  9. our($opt_n);
  10. getopts('n');
  11. # Output year depends on the year of the script and the input file.
  12. my $YEAR = [localtime([stat($0)]->[9])]->[5] + 1900;
  13. my $iYEAR = [localtime([stat($ARGV[0])]->[9])]->[5] + 1900;
  14. $YEAR = $iYEAR if $iYEAR > $YEAR;
  15. $iYEAR = [localtime([stat($ARGV[1])]->[9])]->[5] + 1900;
  16. $YEAR = $iYEAR if $iYEAR > $YEAR;
  17. open (NUMIN,"$ARGV[1]") || die "Can't open number file $ARGV[1]";
  18. $max_nid=0;
  19. $o=0;
  20. while(<NUMIN>)
  21. {
  22. s|\R$||;
  23. $o++;
  24. s/#.*$//;
  25. next if /^\s*$/;
  26. $_ = 'X'.$_;
  27. ($Cname,$mynum) = split;
  28. $Cname =~ s/^X//;
  29. if (defined($nidn{$mynum}))
  30. { die "$ARGV[1]:$o:There's already an object with NID ",$mynum," on line ",$order{$mynum},"\n"; }
  31. if (defined($nid{$Cname}))
  32. { die "$ARGV[1]:$o:There's already an object with name ",$Cname," on line ",$order{$nid{$Cname}},"\n"; }
  33. $nid{$Cname} = $mynum;
  34. $nidn{$mynum} = $Cname;
  35. $order{$mynum} = $o;
  36. $max_nid = $mynum if $mynum > $max_nid;
  37. }
  38. close NUMIN;
  39. open (IN,"$ARGV[0]") || die "Can't open input file $ARGV[0]";
  40. $Cname="";
  41. $o=0;
  42. while (<IN>)
  43. {
  44. s|\R$||;
  45. $o++;
  46. if (/^!module\s+(.*)$/)
  47. {
  48. $module = $1."-";
  49. $module =~ s/\./_/g;
  50. $module =~ s/-/_/g;
  51. }
  52. if (/^!global$/)
  53. { $module = ""; }
  54. if (/^!Cname\s+(.*)$/)
  55. { $Cname = $1; }
  56. if (/^!Alias\s+(.+?)\s+(.*)$/)
  57. {
  58. $Cname = $module.$1;
  59. $myoid = $2;
  60. $myoid = &process_oid($myoid);
  61. $Cname =~ s/-/_/g;
  62. $ordern{$o} = $Cname;
  63. $order{$Cname} = $o;
  64. $obj{$Cname} = $myoid;
  65. $_ = "";
  66. $Cname = "";
  67. }
  68. s/!.*$//;
  69. s/#.*$//;
  70. next if /^\s*$/;
  71. ($myoid,$mysn,$myln) = split ':';
  72. $mysn =~ s/^\s*//;
  73. $mysn =~ s/\s*$//;
  74. $myln =~ s/^\s*//;
  75. $myln =~ s/\s*$//;
  76. $myoid =~ s/^\s*//;
  77. $myoid =~ s/\s*$//;
  78. if ($myoid ne "")
  79. {
  80. $myoid = &process_oid($myoid);
  81. }
  82. if ($Cname eq "" && ($myln =~ /^[_A-Za-z][\w.-]*$/ ))
  83. {
  84. $Cname = $myln;
  85. $Cname =~ s/\./_/g;
  86. $Cname =~ s/-/_/g;
  87. if ($Cname ne "" && defined($ln{$module.$Cname}))
  88. { die "objects.txt:$o:There's already an object with long name ",$ln{$module.$Cname}," on line ",$order{$module.$Cname},"\n"; }
  89. }
  90. if ($Cname eq "")
  91. {
  92. $Cname = $mysn;
  93. $Cname =~ s/-/_/g;
  94. if ($Cname ne "" && defined($sn{$module.$Cname}))
  95. { die "objects.txt:$o:There's already an object with short name ",$sn{$module.$Cname}," on line ",$order{$module.$Cname},"\n"; }
  96. }
  97. if ($Cname eq "")
  98. {
  99. $Cname = $myln;
  100. $Cname =~ s/-/_/g;
  101. $Cname =~ s/\./_/g;
  102. $Cname =~ s/ /_/g;
  103. if ($Cname ne "" && defined($ln{$module.$Cname}))
  104. { die "objects.txt:$o:There's already an object with long name ",$ln{$module.$Cname}," on line ",$order{$module.$Cname},"\n"; }
  105. }
  106. $Cname =~ s/\./_/g;
  107. $Cname =~ s/-/_/g;
  108. $Cname = $module.$Cname;
  109. $ordern{$o} = $Cname;
  110. $order{$Cname} = $o;
  111. $sn{$Cname} = $mysn;
  112. $ln{$Cname} = $myln;
  113. $obj{$Cname} = $myoid;
  114. if (!defined($nid{$Cname}))
  115. {
  116. $max_nid++;
  117. $nid{$Cname} = $max_nid;
  118. $nidn{$max_nid} = $Cname;
  119. print STDERR "Added OID $Cname\n";
  120. }
  121. $Cname="";
  122. }
  123. close IN;
  124. if ( $opt_n ) {
  125. foreach (sort { $a <=> $b } keys %nidn)
  126. {
  127. print $nidn{$_},"\t\t",$_,"\n";
  128. }
  129. exit;
  130. }
  131. print <<"EOF";
  132. /*
  133. * WARNING: do not edit!
  134. * Generated by crypto/objects/objects.pl
  135. *
  136. * Copyright 2000-$YEAR The OpenSSL Project Authors. All Rights Reserved.
  137. * Licensed under the OpenSSL license (the "License"). You may not use
  138. * this file except in compliance with the License. You can obtain a copy
  139. * in the file LICENSE in the source distribution or at
  140. * https://www.openssl.org/source/license.html
  141. */
  142. #define SN_undef "UNDEF"
  143. #define LN_undef "undefined"
  144. #define NID_undef 0
  145. #define OBJ_undef 0L
  146. EOF
  147. sub expand
  148. {
  149. my $string = shift;
  150. 1 while $string =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e;
  151. return $string;
  152. }
  153. foreach (sort { $a <=> $b } keys %ordern)
  154. {
  155. $Cname=$ordern{$_};
  156. print "\n";
  157. print expand("#define SN_$Cname\t\t\"$sn{$Cname}\"\n") if $sn{$Cname} ne "";
  158. print expand("#define LN_$Cname\t\t\"$ln{$Cname}\"\n") if $ln{$Cname} ne "";
  159. print expand("#define NID_$Cname\t\t$nid{$Cname}\n") if $nid{$Cname} ne "";
  160. print expand("#define OBJ_$Cname\t\t$obj{$Cname}\n") if $obj{$Cname} ne "";
  161. }
  162. sub process_oid
  163. {
  164. local($oid)=@_;
  165. local(@a,$oid_pref);
  166. @a = split(/\s+/,$myoid);
  167. $pref_oid = "";
  168. $pref_sep = "";
  169. if (!($a[0] =~ /^[0-9]+$/))
  170. {
  171. $a[0] =~ s/-/_/g;
  172. if (!defined($obj{$a[0]}))
  173. { die "$ARGV[0]:$o:Undefined identifier ",$a[0],"\n"; }
  174. $pref_oid = "OBJ_" . $a[0];
  175. $pref_sep = ",";
  176. shift @a;
  177. }
  178. $oids = join('L,',@a) . "L";
  179. if ($oids ne "L")
  180. {
  181. $oids = $pref_oid . $pref_sep . $oids;
  182. }
  183. else
  184. {
  185. $oids = $pref_oid;
  186. }
  187. return($oids);
  188. }