]> git.saurik.com Git - apple/securityd.git/blob - src/generate.pl
securityd-30544.tar.gz
[apple/securityd.git] / src / generate.pl
1 #!/usr/bin/perl
2 #
3 #
4 #
5 #use strict;
6
7 my $disclaimer = "Automatically generated - do not edit on penalty of futility!";
8
9
10 # arguments
11 my ($configfile, $out_h, $out_cpp, $types, $hdrpath) = @ARGV;
12
13
14 # open configuration file
15 open(CFG, "$configfile") || die "$configfile: $!";
16
17 # open and load cssmtypes file
18 for my $hdrdir (split (/:/, $hdrpath)) {
19 open(TYPES, "$hdrdir/$types") and last;
20 }
21 TYPES or die "cannot find $types in $hdrpath: $!";
22 $/=undef;
23 my $types_h = <TYPES>;
24 close(TYPES); $/="\n";
25
26 # open output files
27 open(H, ">$out_h") || die "$out_h: $!";
28 open(CPP, ">$out_cpp") || die "$out_cpp: $!";
29
30 # cautionary headings to each file
31 print H <<EOH;
32 //
33 // Flipping bytes for securityd transition.
34 // $disclaimer
35 //
36 EOH
37
38 print CPP <<EOC;
39 //
40 // Flipping bytes for securityd transition.
41 // $disclaimer
42 //
43 EOC
44
45 # process generation instructions
46 while (<CFG>) {
47 chomp;
48 next if /^[ ]*#/;
49 next if /^[ ]*$/;
50
51 my @args = split;
52 $_ = shift @args;
53 my ($cssmName, @aliases) = split /\//;
54
55 print H "void flip($cssmName &obj);\n";
56 for my $alias (@aliases) {
57 print H "inline void flip($alias &obj) { flip(static_cast<$cssmName &>(obj)); }\n";
58 }
59
60 next if ($args[0] eq 'CUSTOM');
61
62 if ($args[0] eq '*') {
63 # extract definition from types file
64 my ($list) = $types_h =~ /{\s+([^}]+)\s+}\s*$cssmName,/;
65 die "cannot find struct definition for $cssmName in $types" unless $list;
66 @args = $list =~ /([A-Za-z0-9_]+);/gm;
67 }
68
69 print CPP "void flip($cssmName &obj)\n{\n";
70 for my $field (@args) {
71 print CPP "\tflip(obj.$field);\n";
72 }
73 print CPP "}\n\n";
74 }