Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | #!perl |
michael@0 | 2 | # |
michael@0 | 3 | # This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | # License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | # file, You can obtain one at http://mozilla.org/MPL/2.0/. |
michael@0 | 6 | |
michael@0 | 7 | $copyright = '/* THIS IS A GENERATED FILE */ |
michael@0 | 8 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 9 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 10 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 11 | '; |
michael@0 | 12 | |
michael@0 | 13 | $count = -1; |
michael@0 | 14 | $i = 0; |
michael@0 | 15 | |
michael@0 | 16 | open(INPUT, "<$ARGV[0]") || die "Can't open $ARGV[0]: $!"; |
michael@0 | 17 | |
michael@0 | 18 | while(<INPUT>) { |
michael@0 | 19 | s/^((?:[^"#]+|"[^"]*")*)(\s*#.*$)/$1/; |
michael@0 | 20 | next if (/^\s*$/); |
michael@0 | 21 | |
michael@0 | 22 | # print; |
michael@0 | 23 | |
michael@0 | 24 | /^([\S]+)\s+([^"][\S]*|"[^"]*")/; |
michael@0 | 25 | $name = $1; |
michael@0 | 26 | $value = $2; |
michael@0 | 27 | |
michael@0 | 28 | if( ($name =~ "FUNCTION") && !($name =~ "CK_FUNCTION") ) { |
michael@0 | 29 | $count++; |
michael@0 | 30 | $x[$count]{name} = $value; |
michael@0 | 31 | $i = 0; |
michael@0 | 32 | } else { |
michael@0 | 33 | if( $count < 0 ) { |
michael@0 | 34 | $value =~ s/"//g; |
michael@0 | 35 | $g{$name} = $value; |
michael@0 | 36 | } else { |
michael@0 | 37 | $x[$count]{args}[$i]{type} = $name; |
michael@0 | 38 | $x[$count]{args}[$i]{name} = $value; |
michael@0 | 39 | $i++; |
michael@0 | 40 | $x[$count]{nargs} = $i; # rewritten each time, oh well |
michael@0 | 41 | } |
michael@0 | 42 | } |
michael@0 | 43 | } |
michael@0 | 44 | |
michael@0 | 45 | close INPUT; |
michael@0 | 46 | |
michael@0 | 47 | # dodump(); |
michael@0 | 48 | doprint(); |
michael@0 | 49 | |
michael@0 | 50 | sub dodump { |
michael@0 | 51 | for( $j = 0; $j <= $count; $j++ ) { |
michael@0 | 52 | print "CK_RV CK_ENTRY $x[$j]{name}\n"; |
michael@0 | 53 | for( $i = 0; $i < $x[$j]{nargs}; $i++ ) { |
michael@0 | 54 | print " $x[$j]{args}[$i]{type} $x[$j]{args}[$i]{name}"; |
michael@0 | 55 | if( $i == ($x[$j]{nargs} - 1) ) { |
michael@0 | 56 | print "\n"; |
michael@0 | 57 | } else { |
michael@0 | 58 | print ",\n"; |
michael@0 | 59 | } |
michael@0 | 60 | } |
michael@0 | 61 | } |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | sub doprint { |
michael@0 | 65 | open(PROTOTYPE, ">nssckg.h") || die "Can't open nssckg.h: $!"; |
michael@0 | 66 | open(TYPEDEF, ">nssckft.h") || die "Can't open nssckft.h: $!"; |
michael@0 | 67 | open(EPV, ">nssckepv.h") || die "Can't open nssckepv.h: $!"; |
michael@0 | 68 | open(API, ">nssck.api") || die "Can't open nssck.api: $!"; |
michael@0 | 69 | |
michael@0 | 70 | select PROTOTYPE; |
michael@0 | 71 | |
michael@0 | 72 | print $copyright; |
michael@0 | 73 | print <<EOD |
michael@0 | 74 | #ifndef NSSCKG_H |
michael@0 | 75 | #define NSSCKG_H |
michael@0 | 76 | |
michael@0 | 77 | /* |
michael@0 | 78 | * nssckg.h |
michael@0 | 79 | * |
michael@0 | 80 | * This automatically-generated header file prototypes the Cryptoki |
michael@0 | 81 | * functions specified by PKCS#11. |
michael@0 | 82 | */ |
michael@0 | 83 | |
michael@0 | 84 | #ifndef NSSCKT_H |
michael@0 | 85 | #include "nssckt.h" |
michael@0 | 86 | #endif /* NSSCKT_H */ |
michael@0 | 87 | |
michael@0 | 88 | EOD |
michael@0 | 89 | ; |
michael@0 | 90 | |
michael@0 | 91 | for( $j = 0; $j <= $count; $j++ ) { |
michael@0 | 92 | print "CK_RV CK_ENTRY $x[$j]{name}\n"; |
michael@0 | 93 | print "(\n"; |
michael@0 | 94 | for( $i = 0; $i < $x[$j]{nargs}; $i++ ) { |
michael@0 | 95 | print " $x[$j]{args}[$i]{type} $x[$j]{args}[$i]{name}"; |
michael@0 | 96 | if( $i == ($x[$j]{nargs} - 1) ) { |
michael@0 | 97 | print "\n"; |
michael@0 | 98 | } else { |
michael@0 | 99 | print ",\n"; |
michael@0 | 100 | } |
michael@0 | 101 | } |
michael@0 | 102 | print ");\n\n"; |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | print <<EOD |
michael@0 | 106 | #endif /* NSSCKG_H */ |
michael@0 | 107 | EOD |
michael@0 | 108 | ; |
michael@0 | 109 | |
michael@0 | 110 | select TYPEDEF; |
michael@0 | 111 | |
michael@0 | 112 | print $copyright; |
michael@0 | 113 | print <<EOD |
michael@0 | 114 | #ifndef NSSCKFT_H |
michael@0 | 115 | #define NSSCKFT_H |
michael@0 | 116 | |
michael@0 | 117 | /* |
michael@0 | 118 | * nssckft.h |
michael@0 | 119 | * |
michael@0 | 120 | * The automatically-generated header file declares a typedef |
michael@0 | 121 | * each of the Cryptoki functions specified by PKCS#11. |
michael@0 | 122 | */ |
michael@0 | 123 | |
michael@0 | 124 | #ifndef NSSCKT_H |
michael@0 | 125 | #include "nssckt.h" |
michael@0 | 126 | #endif /* NSSCKT_H */ |
michael@0 | 127 | |
michael@0 | 128 | EOD |
michael@0 | 129 | ; |
michael@0 | 130 | |
michael@0 | 131 | for( $j = 0; $j <= $count; $j++ ) { |
michael@0 | 132 | # print "typedef CK_RV (CK_ENTRY *CK_$x[$j]{name})(\n"; |
michael@0 | 133 | print "typedef CK_CALLBACK_FUNCTION(CK_RV, CK_$x[$j]{name})(\n"; |
michael@0 | 134 | for( $i = 0; $i < $x[$j]{nargs}; $i++ ) { |
michael@0 | 135 | print " $x[$j]{args}[$i]{type} $x[$j]{args}[$i]{name}"; |
michael@0 | 136 | if( $i == ($x[$j]{nargs} - 1) ) { |
michael@0 | 137 | print "\n"; |
michael@0 | 138 | } else { |
michael@0 | 139 | print ",\n"; |
michael@0 | 140 | } |
michael@0 | 141 | } |
michael@0 | 142 | print ");\n\n"; |
michael@0 | 143 | } |
michael@0 | 144 | |
michael@0 | 145 | print <<EOD |
michael@0 | 146 | #endif /* NSSCKFT_H */ |
michael@0 | 147 | EOD |
michael@0 | 148 | ; |
michael@0 | 149 | |
michael@0 | 150 | select EPV; |
michael@0 | 151 | |
michael@0 | 152 | print $copyright; |
michael@0 | 153 | print <<EOD |
michael@0 | 154 | #ifndef NSSCKEPV_H |
michael@0 | 155 | #define NSSCKEPV_H |
michael@0 | 156 | |
michael@0 | 157 | /* |
michael@0 | 158 | * nssckepv.h |
michael@0 | 159 | * |
michael@0 | 160 | * This automatically-generated header file defines the type |
michael@0 | 161 | * CK_FUNCTION_LIST specified by PKCS#11. |
michael@0 | 162 | */ |
michael@0 | 163 | |
michael@0 | 164 | #ifndef NSSCKT_H |
michael@0 | 165 | #include "nssckt.h" |
michael@0 | 166 | #endif /* NSSCKT_H */ |
michael@0 | 167 | |
michael@0 | 168 | #ifndef NSSCKFT_H |
michael@0 | 169 | #include "nssckft.h" |
michael@0 | 170 | #endif /* NSSCKFT_H */ |
michael@0 | 171 | |
michael@0 | 172 | #include "nssckp.h" |
michael@0 | 173 | |
michael@0 | 174 | struct CK_FUNCTION_LIST { |
michael@0 | 175 | CK_VERSION version; |
michael@0 | 176 | EOD |
michael@0 | 177 | ; |
michael@0 | 178 | |
michael@0 | 179 | for( $j = 0; $j <= $count; $j++ ) { |
michael@0 | 180 | print " CK_$x[$j]{name} $x[$j]{name};\n"; |
michael@0 | 181 | } |
michael@0 | 182 | |
michael@0 | 183 | print <<EOD |
michael@0 | 184 | }; |
michael@0 | 185 | |
michael@0 | 186 | #include "nsscku.h" |
michael@0 | 187 | |
michael@0 | 188 | #endif /* NSSCKEPV_H */ |
michael@0 | 189 | EOD |
michael@0 | 190 | ; |
michael@0 | 191 | |
michael@0 | 192 | select API; |
michael@0 | 193 | |
michael@0 | 194 | print $copyright; |
michael@0 | 195 | print <<EOD |
michael@0 | 196 | |
michael@0 | 197 | /* |
michael@0 | 198 | * nssck.api |
michael@0 | 199 | * |
michael@0 | 200 | * This automatically-generated file is used to generate a set of |
michael@0 | 201 | * Cryptoki entry points within the object space of a Module using |
michael@0 | 202 | * the NSS Cryptoki Framework. |
michael@0 | 203 | * |
michael@0 | 204 | * The Module should have a .c file with the following: |
michael@0 | 205 | * |
michael@0 | 206 | * #define MODULE_NAME name |
michael@0 | 207 | * #define INSTANCE_NAME instance |
michael@0 | 208 | * #include "nssck.api" |
michael@0 | 209 | * |
michael@0 | 210 | * where "name" is some module-specific name that can be used to |
michael@0 | 211 | * disambiguate various modules. This included file will then |
michael@0 | 212 | * define the actual Cryptoki routines which pass through to the |
michael@0 | 213 | * Framework calls. All routines, except C_GetFunctionList, will |
michael@0 | 214 | * be prefixed with the name; C_GetFunctionList will be generated |
michael@0 | 215 | * to return an entry-point vector with these routines. The |
michael@0 | 216 | * instance specified should be the basic instance of NSSCKMDInstance. |
michael@0 | 217 | * |
michael@0 | 218 | * If, prior to including nssck.api, the .c file also specifies |
michael@0 | 219 | * |
michael@0 | 220 | * #define DECLARE_STRICT_CRYTPOKI_NAMES |
michael@0 | 221 | * |
michael@0 | 222 | * Then a set of "stub" routines not prefixed with the name will |
michael@0 | 223 | * be included. This would allow the combined module and framework |
michael@0 | 224 | * to be used in applications which are hard-coded to use the |
michael@0 | 225 | * PKCS#11 names (instead of going through the EPV). Please note |
michael@0 | 226 | * that such applications should be careful resolving symbols when |
michael@0 | 227 | * more than one PKCS#11 module is loaded. |
michael@0 | 228 | */ |
michael@0 | 229 | |
michael@0 | 230 | #ifndef MODULE_NAME |
michael@0 | 231 | #error "Error: MODULE_NAME must be defined." |
michael@0 | 232 | #endif /* MODULE_NAME */ |
michael@0 | 233 | |
michael@0 | 234 | #ifndef INSTANCE_NAME |
michael@0 | 235 | #error "Error: INSTANCE_NAME must be defined." |
michael@0 | 236 | #endif /* INSTANCE_NAME */ |
michael@0 | 237 | |
michael@0 | 238 | #ifndef NSSCKT_H |
michael@0 | 239 | #include "nssckt.h" |
michael@0 | 240 | #endif /* NSSCKT_H */ |
michael@0 | 241 | |
michael@0 | 242 | #ifndef NSSCKFWT_H |
michael@0 | 243 | #include "nssckfwt.h" |
michael@0 | 244 | #endif /* NSSCKFWT_H */ |
michael@0 | 245 | |
michael@0 | 246 | #ifndef NSSCKFWC_H |
michael@0 | 247 | #include "nssckfwc.h" |
michael@0 | 248 | #endif /* NSSCKFWC_H */ |
michael@0 | 249 | |
michael@0 | 250 | #ifndef NSSCKEPV_H |
michael@0 | 251 | #include "nssckepv.h" |
michael@0 | 252 | #endif /* NSSCKEPV_H */ |
michael@0 | 253 | |
michael@0 | 254 | #define ADJOIN(x,y) x##y |
michael@0 | 255 | |
michael@0 | 256 | #define __ADJOIN(x,y) ADJOIN(x,y) |
michael@0 | 257 | |
michael@0 | 258 | /* |
michael@0 | 259 | * The anchor. This object is used to store an "anchor" pointer in |
michael@0 | 260 | * the Module's object space, so the wrapper functions can relate |
michael@0 | 261 | * back to this instance. |
michael@0 | 262 | */ |
michael@0 | 263 | |
michael@0 | 264 | static NSSCKFWInstance *fwInstance = (NSSCKFWInstance *)0; |
michael@0 | 265 | |
michael@0 | 266 | static CK_RV CK_ENTRY |
michael@0 | 267 | __ADJOIN(MODULE_NAME,C_Initialize) |
michael@0 | 268 | ( |
michael@0 | 269 | CK_VOID_PTR pInitArgs |
michael@0 | 270 | ) |
michael@0 | 271 | { |
michael@0 | 272 | return NSSCKFWC_Initialize(&fwInstance, INSTANCE_NAME, pInitArgs); |
michael@0 | 273 | } |
michael@0 | 274 | |
michael@0 | 275 | #ifdef DECLARE_STRICT_CRYPTOKI_NAMES |
michael@0 | 276 | CK_RV CK_ENTRY |
michael@0 | 277 | C_Initialize |
michael@0 | 278 | ( |
michael@0 | 279 | CK_VOID_PTR pInitArgs |
michael@0 | 280 | ) |
michael@0 | 281 | { |
michael@0 | 282 | return __ADJOIN(MODULE_NAME,C_Initialize)(pInitArgs); |
michael@0 | 283 | } |
michael@0 | 284 | #endif /* DECLARE_STRICT_CRYPTOKI_NAMES */ |
michael@0 | 285 | |
michael@0 | 286 | static CK_RV CK_ENTRY |
michael@0 | 287 | __ADJOIN(MODULE_NAME,C_Finalize) |
michael@0 | 288 | ( |
michael@0 | 289 | CK_VOID_PTR pReserved |
michael@0 | 290 | ) |
michael@0 | 291 | { |
michael@0 | 292 | return NSSCKFWC_Finalize(&fwInstance); |
michael@0 | 293 | } |
michael@0 | 294 | |
michael@0 | 295 | #ifdef DECLARE_STRICT_CRYPTOKI_NAMES |
michael@0 | 296 | CK_RV CK_ENTRY |
michael@0 | 297 | C_Finalize |
michael@0 | 298 | ( |
michael@0 | 299 | CK_VOID_PTR pReserved |
michael@0 | 300 | ) |
michael@0 | 301 | { |
michael@0 | 302 | return __ADJOIN(MODULE_NAME,C_Finalize)(pReserved); |
michael@0 | 303 | } |
michael@0 | 304 | #endif /* DECLARE_STRICT_CRYPTOKI_NAMES */ |
michael@0 | 305 | |
michael@0 | 306 | static CK_RV CK_ENTRY |
michael@0 | 307 | __ADJOIN(MODULE_NAME,C_GetInfo) |
michael@0 | 308 | ( |
michael@0 | 309 | CK_INFO_PTR pInfo |
michael@0 | 310 | ) |
michael@0 | 311 | { |
michael@0 | 312 | return NSSCKFWC_GetInfo(fwInstance, pInfo); |
michael@0 | 313 | } |
michael@0 | 314 | |
michael@0 | 315 | #ifdef DECLARE_STRICT_CRYPTOKI_NAMES |
michael@0 | 316 | CK_RV CK_ENTRY |
michael@0 | 317 | C_GetInfo |
michael@0 | 318 | ( |
michael@0 | 319 | CK_INFO_PTR pInfo |
michael@0 | 320 | ) |
michael@0 | 321 | { |
michael@0 | 322 | return __ADJOIN(MODULE_NAME,C_GetInfo)(pInfo); |
michael@0 | 323 | } |
michael@0 | 324 | #endif /* DECLARE_STRICT_CRYPTOKI_NAMES */ |
michael@0 | 325 | |
michael@0 | 326 | /* |
michael@0 | 327 | * C_GetFunctionList is defined at the end. |
michael@0 | 328 | */ |
michael@0 | 329 | |
michael@0 | 330 | EOD |
michael@0 | 331 | ; |
michael@0 | 332 | |
michael@0 | 333 | for( $j = 4; $j <= $count; $j++ ) { |
michael@0 | 334 | print "static CK_RV CK_ENTRY\n"; |
michael@0 | 335 | print "__ADJOIN(MODULE_NAME,$x[$j]{name})\n"; |
michael@0 | 336 | print "(\n"; |
michael@0 | 337 | for( $i = 0; $i < $x[$j]{nargs}; $i++ ) { |
michael@0 | 338 | print " $x[$j]{args}[$i]{type} $x[$j]{args}[$i]{name}"; |
michael@0 | 339 | if( $i == ($x[$j]{nargs} - 1) ) { |
michael@0 | 340 | print "\n"; |
michael@0 | 341 | } else { |
michael@0 | 342 | print ",\n"; |
michael@0 | 343 | } |
michael@0 | 344 | } |
michael@0 | 345 | print ")\n"; |
michael@0 | 346 | print "{\n"; |
michael@0 | 347 | print " return NSSCKFW$x[$j]{name}(fwInstance, "; |
michael@0 | 348 | for( $i = 0; $i < $x[$j]{nargs}; $i++ ) { |
michael@0 | 349 | print "$x[$j]{args}[$i]{name}"; |
michael@0 | 350 | if( $i == ($x[$j]{nargs} - 1) ) { |
michael@0 | 351 | print ");\n"; |
michael@0 | 352 | } else { |
michael@0 | 353 | print ", "; |
michael@0 | 354 | } |
michael@0 | 355 | } |
michael@0 | 356 | print "}\n\n"; |
michael@0 | 357 | |
michael@0 | 358 | print "#ifdef DECLARE_STRICT_CRYPTOKI_NAMES\n"; |
michael@0 | 359 | print "CK_RV CK_ENTRY\n"; |
michael@0 | 360 | print "$x[$j]{name}\n"; |
michael@0 | 361 | print "(\n"; |
michael@0 | 362 | for( $i = 0; $i < $x[$j]{nargs}; $i++ ) { |
michael@0 | 363 | print " $x[$j]{args}[$i]{type} $x[$j]{args}[$i]{name}"; |
michael@0 | 364 | if( $i == ($x[$j]{nargs} - 1) ) { |
michael@0 | 365 | print "\n"; |
michael@0 | 366 | } else { |
michael@0 | 367 | print ",\n"; |
michael@0 | 368 | } |
michael@0 | 369 | } |
michael@0 | 370 | print ")\n"; |
michael@0 | 371 | print "{\n"; |
michael@0 | 372 | print " return __ADJOIN(MODULE_NAME,$x[$j]{name})("; |
michael@0 | 373 | for( $i = 0; $i < $x[$j]{nargs}; $i++ ) { |
michael@0 | 374 | print "$x[$j]{args}[$i]{name}"; |
michael@0 | 375 | if( $i == ($x[$j]{nargs} - 1) ) { |
michael@0 | 376 | print ");\n"; |
michael@0 | 377 | } else { |
michael@0 | 378 | print ", "; |
michael@0 | 379 | } |
michael@0 | 380 | } |
michael@0 | 381 | print "}\n"; |
michael@0 | 382 | print "#endif /* DECLARE_STRICT_CRYPTOKI_NAMES */\n\n"; |
michael@0 | 383 | } |
michael@0 | 384 | |
michael@0 | 385 | print <<EOD |
michael@0 | 386 | static CK_RV CK_ENTRY |
michael@0 | 387 | __ADJOIN(MODULE_NAME,C_GetFunctionList) |
michael@0 | 388 | ( |
michael@0 | 389 | CK_FUNCTION_LIST_PTR_PTR ppFunctionList |
michael@0 | 390 | ); |
michael@0 | 391 | |
michael@0 | 392 | static CK_FUNCTION_LIST FunctionList = { |
michael@0 | 393 | { 2, 1 }, |
michael@0 | 394 | EOD |
michael@0 | 395 | ; |
michael@0 | 396 | |
michael@0 | 397 | for( $j = 0; $j <= $count; $j++ ) { |
michael@0 | 398 | print "__ADJOIN(MODULE_NAME,$x[$j]{name})"; |
michael@0 | 399 | if( $j < $count ) { |
michael@0 | 400 | print ",\n"; |
michael@0 | 401 | } else { |
michael@0 | 402 | print "\n};\n\n"; |
michael@0 | 403 | } |
michael@0 | 404 | } |
michael@0 | 405 | |
michael@0 | 406 | print <<EOD |
michael@0 | 407 | static CK_RV CK_ENTRY |
michael@0 | 408 | __ADJOIN(MODULE_NAME,C_GetFunctionList) |
michael@0 | 409 | ( |
michael@0 | 410 | CK_FUNCTION_LIST_PTR_PTR ppFunctionList |
michael@0 | 411 | ) |
michael@0 | 412 | { |
michael@0 | 413 | *ppFunctionList = &FunctionList; |
michael@0 | 414 | return CKR_OK; |
michael@0 | 415 | } |
michael@0 | 416 | |
michael@0 | 417 | /* This one is always present */ |
michael@0 | 418 | CK_RV CK_ENTRY |
michael@0 | 419 | C_GetFunctionList |
michael@0 | 420 | ( |
michael@0 | 421 | CK_FUNCTION_LIST_PTR_PTR ppFunctionList |
michael@0 | 422 | ) |
michael@0 | 423 | { |
michael@0 | 424 | return __ADJOIN(MODULE_NAME,C_GetFunctionList)(ppFunctionList); |
michael@0 | 425 | } |
michael@0 | 426 | |
michael@0 | 427 | #undef __ADJOIN |
michael@0 | 428 | |
michael@0 | 429 | EOD |
michael@0 | 430 | ; |
michael@0 | 431 | |
michael@0 | 432 | select STDOUT; |
michael@0 | 433 | |
michael@0 | 434 | } |