objects.pl 4.9 KB

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