]>
git.saurik.com Git - apple/security.git/blob - Security/libsecurity_cssm/lib/generator.pl
   3 # Copyright (c) 2001-2004,2011-2012,2014 Apple Inc. All Rights Reserved. 
   5 # @APPLE_LICENSE_HEADER_START@ 
   7 # This file contains Original Code and/or Modifications of Original Code 
   8 # as defined in and that are subject to the Apple Public Source License 
   9 # Version 2.0 (the 'License'). You may not use this file except in 
  10 # compliance with the License. Please obtain a copy of the License at 
  11 # http://www.opensource.apple.com/apsl/ and read it before using this 
  14 # The Original Code and all software distributed under the License are 
  15 # distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 
  16 # EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 
  17 # INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 
  18 # FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 
  19 # Please see the License for the specific language governing rights and 
  20 # limitations under the License. 
  22 # @APPLE_LICENSE_HEADER_END@ 
  24 # generator.pl - derive various and sundry C++ code from the CDSA header files 
  27 #       perl generator.pl input-directory config-file output-directory 
  29 # Perry The Cynic, Fall 1999. 
  32 %SPI_H=("AC" => "cssmaci.h", "CSP" => "cssmcspi.h", "DL" => "cssmdli.h", 
  33         "CL" => "cssmcli.h", "TP"  => "cssmtpi.h"); 
  34 @OIDS_H=("oidscert.h", "oidscrl.h"); 
  35 @OIDS_H2=("oidsattr.h", "oidsalg.h"); 
  37 $SOURCEDIR=$ARGV[0];                    # directory with inputs 
  38 $APICFG=$ARGV[1];                               # configuration file 
  39 $TARGETDIR=$ARGV[2];                    # directory for outputs 
  42 $TRANSITION="$TARGETDIR/transition.gen"; # C++ code for transition layer 
  43 $TABLES="$TARGETDIR/funcnames.gen";             # function name tables 
  44 $REPORT="$TARGETDIR/generator.rpt";             # report file 
  45 $EXPORTS="$TARGETDIR/cssmexports.gen";  # Exports file 
  47 $tabs = "\t\t\t";       # argument indentation (noncritical) 
  48 $warning = "This file was automatically generated. Do not edit on penalty of futility!"; 
  52 # Parse API headers and extract function names and argument lists 
  54 # Jim Muprhy I added [^;]* to the end of the %formals variable to 
  55 # allow for deprecation macros. 
  57 $/=undef;       # big gulp mode 
  59   open(API_H
, "$SOURCEDIR/$_") or die "Cannot open $SOURCEDIR/$_: $^E"; 
  60   $_ = <API_H
>;         # glglgl... aaaaah 
  61   %formals = /CSSM_RETURN CSSMAPI\s*([A-Za-z_]+)\s+\(([^)]*)\)[^;]*;/gs; 
  62   while (($name, $args) = each %formals) { 
  63     $args =~ s/^.*[ *]([A-Za-z_]+,?)$/$tabs$1/gm;       # remove type declarators 
  64     $args =~ s/^$tabs//o;       # chop intial tabs              # so now we have... 
  65     $actuals{$name} = $args;                                            # ...an actual argument list 
  72 # Slurp SPI headers into memory for future use 
  74 $/=undef;       # slurp files 
  75 while (($key, $file) = each %SPI_H) { 
  76   open(SPI_H
, "$SOURCEDIR/$file") or die "Cannot open $SOURCEDIR/$file: $^E"; 
  83 # Open and read the configuration file 
  85 $/=undef;       # gulp yet again 
  86 open(APICFG
, $APICFG) or die "Cannot open $APICFG: $^E"; 
  89 %config = /^\s*(\w+)\s+(.*)$/gm; 
  93 # Now we will generate the API transition layer. 
  94 # The idea here is that for each function in the API, we try to 
  95 # figure out what type of plugin it belongs to, and then look up 
  96 # its evil twin in that type's header. If that works, we generate 
  97 # a function for the transition, taking care of various oddities 
  98 # and endities in the process. 
 100 open(OUT
, ">$TRANSITION") or die "Cannot write $TRANSITION: $^E"; 
 102 open(REPORT
, ">$REPORT") or die "Cannot write $REPORT: $^E"; 
 107   print REPORT 
"$name $reason\n"; 
 114 for $name (sort keys %formals) { 
 115   $config = $config{$name}; 
 116   do { ignored 
"has custom implementation"; next; } if $config =~ /custom/; 
 118   ($barename) = $name =~ /^CSSM.*_([A-Za-z]+$)/; 
 119   die "Can't fathom SPI name for $name" unless $barename; 
 120   $actuals = $actuals{$name}; 
 122   # key off the type code in the first argument: CSSM_type_HANDLE 
 123   ($type, $handle) = $formals{$name} =~ /CSSM_([A-Z_]+)_HANDLE\s+([A-Za-z0-9_]+)/; 
 124   $type = "CSP" if $type eq "CC";       # CSP methods may have CC (context) handles 
 125   $type = "DL" if $type eq "DL_DB";     # DL methods may have DL_DB handles 
 126   $type = "KR" if $type eq "KRSP";      # KR methods have KRSP handles 
 127   $Type = $type . "Attachment"; 
 128   do { ignored 
"has no module type"; next; } unless defined $SPI_H{$type}; 
 130   # $prefix will hold code to be generated before the actual call 
 133   # match the SPI; take care of the Privilege variants of some calls 
 134   ($args) = $spi{$type} =~ /CSSM_RETURN \(CSSM${type}I \*$barename\)\s+\(([^)]*)\)[^;]*;/s 
 135     or $barename =~ s/P$// &&   # second chance for FooBarP() API functions 
 136       (($args) = $spi{$type} =~ /CSSM_RETURN \(CSSM${type}I \*$barename\)\s+\(([^)]*)\)[^;]*;/s) 
 137     or do { ignored 
"not in $SPI_H{$type}"; next; }; 
 139   # take care of CSP calls taking context handles 
 141   $type eq "CSP" && $actuals =~ /CCHandle/ && do { 
 142     $actuals =~ s/CCHandle/context.CSPHandle, CCHandle/; 
 143     $args =~ /CSSM_CONTEXT/ && 
 144       $actuals =~ s/CCHandle/CCHandle, &context/; 
 148   # add the default privilege argument to non-P functions taking privileges 
 149   $args =~ /CSSM_PRIVILEGE/ && ! ($name =~ /P$/) &&                     # add privilege argument (last) 
 150     ($actuals .= ",\n${tabs}attachment.module.cssm.getPrivilege()"); 
 152   # finally translate DLDBHandles into their DL component 
 153   $handle =~ s/DLDBHandle/DLDBHandle.DLHandle/; 
 158 $name ($formals{$name}) 
 161   if ($handletype eq "CC") { 
 163   HandleContext &context = enterContext($handle); 
 164   CSPAttachment &attachment = context.attachment;"; 
 167   $Type &attachment = enterAttachment<$Type>($handle);"; 
 170   TransitLock _(attachment); 
 171   ${prefix}return attachment.downcalls.$barename($actuals); 
 182 # Now peruse the SPI headers for a list of function names 
 183 # and build in-memory translation tables for runtime. 
 185 open(OUT
, ">$TABLES") or die "Cannot write $TABLES: $^E"; 
 189 // Standard plugin name tables 
 193 while (($name, $_) = each %spi) { 
 194   print "extern const char *const ${name}NameTable[] = {"; 
 195   s/^.*struct cssm_spi.*{(.*)} CSSM_SPI.*$/$1/s 
 196     or die "bad format in $SPI_H{$name}"; 
 197   s/CSSM_RETURN \(CSSM[A-Z]*I \*([A-Za-z_]+)\)\s+\([^)]+\)[^;]*;/\t"$1",/g; 
 205 # Finally, generate linker export file to avoid leaking internal symbols 
 207 open(OUT
, ">$EXPORTS") or die "Cannot write $EXPORTS: $^E"; 
 210 # entry point names (functions) 
 211 for $name (keys %formals) { 
 215 # OID-related data symbols 
 217 foreach $_ (@OIDS_H) { 
 218   open(OIDS_H
, "$SOURCEDIR/$_") or die "Cannot open $SOURCEDIR/$_: $^E"; 
 219   $_ = <OIDS_H
>;                # glglgl... aaaaah 
 220   s/\/\*.*\*\///gm;     # remove comments 
 222   foreach $name (/\s+(CSSMOID_[A-Za-z0-9_]+)/gs) { 
 228 foreach $_ (@OIDS_H2) { 
 229     open(OIDS_H2
, "$SOURCEDIR/../../libsecurity_asn1/lib/$_") or die "Cannot open $SOURCEDIR/$_: $^E"; 
 230     $_ = <OIDS_H2
>;             # glglgl... aaaaah 
 231     s/\/\*.*\*\///gm;   # remove comments 
 233     foreach $name (/\s+(CSSMOID_[A-Za-z0-9_]+)/gs) { 
 239 foreach $name (keys %symbols) { 
 249 print "$written API functions generated; $ignored ignored (see $REPORT)\n";