objects.pl 4.7 KB

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