certdata.perl (3968B)
1 #!perl -w 2 # 3 # This Source Code Form is subject to the terms of the Mozilla Public 4 # License, v. 2.0. If a copy of the MPL was not distributed with this 5 # file, You can obtain one at http://mozilla.org/MPL/2.0/. 6 use strict; 7 8 my %constants; 9 my $count = 0; 10 my $o; 11 my @objects = (); 12 my @objsize; 13 14 $constants{CK_TRUE} = "static const CK_BBOOL ck_true = CK_TRUE;\n"; 15 $constants{CK_FALSE} = "static const CK_BBOOL ck_false = CK_FALSE;\n"; 16 17 if( scalar @ARGV == 0 ) { 18 print STDERR "Usage: $0 <input-file> [output-file]\n"; 19 exit 1; 20 } 21 22 open(STDIN, '<', $ARGV[0]) 23 or die "Could not open input file '$ARGV[0]' $!"; 24 if( scalar @ARGV > 1 ) { 25 open(STDOUT, '>', $ARGV[1]) 26 or die "Could not open output file '$ARGV[1]' $!"; 27 } 28 29 while(<>) { 30 my @fields = (); 31 my $size; 32 33 s/^((?:[^"#]+|"[^"]*")*)(\s*#.*$)/$1/; 34 next if (/^\s*$/); 35 36 # This was taken from the perl faq #4. 37 my $text = $_; 38 push(@fields, $+) while $text =~ m{ 39 "([^\"\\]*(?:\\.[^\"\\]*)*)"\s? # groups the phrase inside the quotes 40 | ([^\s]+)\s? 41 | \s 42 }gx; 43 push(@fields, undef) if substr($text,-1,1) eq '\s'; 44 45 if( $fields[0] =~ /BEGINDATA/ ) { 46 next; 47 } 48 49 if( $fields[1] =~ /MULTILINE/ ) { 50 $fields[2] = ""; 51 while(<>) { 52 last if /END/; 53 chomp; 54 $fields[2] .= "\"$_\"\n"; 55 } 56 } 57 58 if( $fields[1] =~ /UTF8/ ) { 59 if( $fields[2] =~ /^"/ ) { 60 ; 61 } else { 62 $fields[2] = "\"" . $fields[2] . "\""; 63 } 64 65 my $scratch = eval($fields[2]); 66 67 $size = length($scratch) + 1; # null terminate 68 } 69 70 if( $fields[1] =~ /OCTAL/ ) { 71 if( $fields[2] =~ /^"/ ) { 72 ; 73 } else { 74 $fields[2] = "\"" . $fields[2] . "\""; 75 } 76 77 my $scratch = $fields[2]; 78 $size = $scratch =~ tr/\\//; 79 # no null termination 80 } 81 82 if( $fields[1] =~ /^CK_/ ) { 83 my $lcv = $fields[2]; 84 $lcv =~ tr/A-Z/a-z/; 85 if( !defined($constants{$fields[2]}) ) { 86 $constants{$fields[2]} = "static const $fields[1] $lcv = $fields[2];\n"; 87 } 88 89 $size = "sizeof($fields[1])"; 90 $fields[2] = "&$lcv"; 91 } 92 93 if( $fields[0] =~ /CKA_CLASS/ ) { 94 $count++; 95 $objsize[$count] = 0; 96 } 97 98 @{$objects[$count][$objsize[$count]++]} = ( "$fields[0]", $fields[2], "$size" ); 99 100 # print "$fields[0] | $fields[1] | $size | $fields[2]\n"; 101 } 102 103 doprint(); 104 105 sub dudump { 106 my $i; 107 for( $i = 1; $i <= $count; $i++ ) { 108 print "\n"; 109 $o = $objects[$i]; 110 my @ob = @{$o}; 111 my $l; 112 my $j; 113 for( $j = 0; $j < @ob; $j++ ) { 114 $l = $ob[$j]; 115 my @a = @{$l}; 116 print "$a[0] ! $a[1] ! $a[2]\n"; 117 } 118 } 119 120 } 121 122 sub doprint { 123 my $i; 124 125 print <<EOD 126 /* THIS IS A GENERATED FILE */ 127 /* This Source Code Form is subject to the terms of the Mozilla Public 128 * License, v. 2.0. If a copy of the MPL was not distributed with this 129 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 130 131 #ifndef BUILTINS_H 132 #include "builtins.h" 133 #endif /* BUILTINS_H */ 134 135 EOD 136 ; 137 138 foreach $b (sort values(%constants)) { 139 print $b; 140 } 141 142 for( $i = 1; $i <= $count; $i++ ) { 143 print "static const CK_ATTRIBUTE_TYPE nss_builtins_types_$i [] = {\n"; 144 $o = $objects[$i]; 145 my @ob = @{$o}; 146 my $j; 147 for( $j = 0; $j < @ob; $j++ ) { 148 my $l = $ob[$j]; 149 my @a = @{$l}; 150 print " $a[0]"; 151 if( $j+1 != @ob ) { 152 print ", "; 153 } 154 } 155 print "\n};\n"; 156 } 157 158 for( $i = 1; $i <= $count; $i++ ) { 159 print "static const NSSItem nss_builtins_items_$i [] = {\n"; 160 $o = $objects[$i]; 161 my @ob = @{$o}; 162 my $j; 163 for( $j = 0; $j < @ob; $j++ ) { 164 my $l = $ob[$j]; 165 my @a = @{$l}; 166 print " { (void *)$a[1], (PRUint32)$a[2] }"; 167 if( $j+1 != @ob ) { 168 print ",\n"; 169 } else { 170 print "\n"; 171 } 172 } 173 print "};\n"; 174 } 175 176 print "\nbuiltinsInternalObject\n"; 177 print "nss_builtins_data[] = {\n"; 178 179 for( $i = 1; $i <= $count; $i++ ) { 180 print " { $objsize[$i], nss_builtins_types_$i, nss_builtins_items_$i, {NULL} }"; 181 if( $i == $count ) { 182 print "\n"; 183 } else { 184 print ",\n"; 185 } 186 } 187 188 print "};\n"; 189 190 print "const PRUint32\n"; 191 print "nss_builtins_nObjects = $count;\n"; 192 }