Wed, 31 Dec 2014 07:16:47 +0100
Revert simplistic fix pending revisit of Mozilla integration attempt.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | |
michael@0 | 6 | #include <stdio.h> |
michael@0 | 7 | #include <string.h> |
michael@0 | 8 | |
michael@0 | 9 | #if defined(WIN32) |
michael@0 | 10 | #undef __STDC__ |
michael@0 | 11 | #include "fcntl.h" |
michael@0 | 12 | #include "io.h" |
michael@0 | 13 | #include <fcntl.h> |
michael@0 | 14 | #else |
michael@0 | 15 | #include <unistd.h> |
michael@0 | 16 | #include <fcntl.h> |
michael@0 | 17 | #endif |
michael@0 | 18 | |
michael@0 | 19 | #include "secutil.h" |
michael@0 | 20 | |
michael@0 | 21 | |
michael@0 | 22 | #include "nspr.h" |
michael@0 | 23 | #include "prtypes.h" |
michael@0 | 24 | #include "prtime.h" |
michael@0 | 25 | #include "prlong.h" |
michael@0 | 26 | #include "prinrval.h" |
michael@0 | 27 | #include "prenv.h" |
michael@0 | 28 | |
michael@0 | 29 | #include "pkcs11.h" |
michael@0 | 30 | |
michael@0 | 31 | #include "pk11table.h" |
michael@0 | 32 | |
michael@0 | 33 | #ifndef O_BINARY |
michael@0 | 34 | #define O_BINARY 0 |
michael@0 | 35 | #endif |
michael@0 | 36 | |
michael@0 | 37 | CK_ULONG systemFlags; |
michael@0 | 38 | #define FLAG_NEGATE 0x80000000 |
michael@0 | 39 | #define FLAG_Verify 0x00000001 |
michael@0 | 40 | #define FLAG_VerifyFile 0x00000002 |
michael@0 | 41 | #define CKR_QUIT 0x80000000 |
michael@0 | 42 | |
michael@0 | 43 | int ArgSize(ArgType type); |
michael@0 | 44 | const char *constLookup(const char *bp, CK_ULONG *value, ConstType *type); |
michael@0 | 45 | |
michael@0 | 46 | int |
michael@0 | 47 | isNum(char c) |
michael@0 | 48 | { |
michael@0 | 49 | return (c >= '0' && c <= '9'); |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | int |
michael@0 | 53 | isConst(const char *c) |
michael@0 | 54 | { |
michael@0 | 55 | CK_ULONG value; |
michael@0 | 56 | ConstType type; |
michael@0 | 57 | |
michael@0 | 58 | constLookup(c, &value, &type); |
michael@0 | 59 | return type != ConstNone; |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | /* |
michael@0 | 63 | * see if the variable is really a 'size' function. This |
michael@0 | 64 | * function may modify var if it is a size function. |
michael@0 | 65 | */ |
michael@0 | 66 | char * |
michael@0 | 67 | isSize(char *var, int *isArray) |
michael@0 | 68 | { |
michael@0 | 69 | char *ptr = NULL; |
michael@0 | 70 | char *end; |
michael@0 | 71 | int array = 0; |
michael@0 | 72 | |
michael@0 | 73 | if (PL_strncasecmp(var,"sizeof(",/*)*/ 7) == 0) { |
michael@0 | 74 | ptr = var + 7; |
michael@0 | 75 | } else if (PL_strncasecmp(var,"size(",/*)*/ 5) == 0) { |
michael@0 | 76 | ptr = var + 5; |
michael@0 | 77 | } else if (PL_strncasecmp(var,"sizeofarray(",/*)*/ 12) == 0) { |
michael@0 | 78 | ptr = var + 12; |
michael@0 | 79 | array = 1; |
michael@0 | 80 | } else if (PL_strncasecmp(var,"sizea(",/*)*/ 6) == 0) { |
michael@0 | 81 | ptr = var + 6; |
michael@0 | 82 | array = 1; |
michael@0 | 83 | } else { |
michael@0 | 84 | return NULL; |
michael@0 | 85 | } |
michael@0 | 86 | end = strchr(ptr,/*(*/ ')') ; |
michael@0 | 87 | if (end == NULL) { |
michael@0 | 88 | return NULL; |
michael@0 | 89 | } |
michael@0 | 90 | if (isArray) *isArray = array; |
michael@0 | 91 | *end = 0; |
michael@0 | 92 | return ptr; |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | void |
michael@0 | 96 | printConst(CK_ULONG value, ConstType type, int newLine) |
michael@0 | 97 | { |
michael@0 | 98 | int i; |
michael@0 | 99 | |
michael@0 | 100 | for (i=0; i < constCount; i++) { |
michael@0 | 101 | if (consts[i].type == type && consts[i].value == value) { |
michael@0 | 102 | printf("%s",consts[i].name); |
michael@0 | 103 | break; |
michael@0 | 104 | } |
michael@0 | 105 | if (type == ConstNone && consts[i].value == value) { |
michael@0 | 106 | printf("%s",consts[i].name); |
michael@0 | 107 | break; |
michael@0 | 108 | } |
michael@0 | 109 | } |
michael@0 | 110 | if (i == constCount) { |
michael@0 | 111 | if ((type == ConstAvailableSizes) || (type == ConstCurrentSize)) { |
michael@0 | 112 | printf("%lu",value); |
michael@0 | 113 | } else { |
michael@0 | 114 | printf("Unknown %s (%lu:0x%lx)",constTypeString[type],value,value); |
michael@0 | 115 | } |
michael@0 | 116 | } |
michael@0 | 117 | if (newLine) { |
michael@0 | 118 | printf("\n"); |
michael@0 | 119 | } |
michael@0 | 120 | } |
michael@0 | 121 | |
michael@0 | 122 | ConstType |
michael@0 | 123 | getConstFromAttribute(CK_ATTRIBUTE_TYPE type) |
michael@0 | 124 | { |
michael@0 | 125 | int i; |
michael@0 | 126 | |
michael@0 | 127 | for (i=0; i < constCount; i++) { |
michael@0 | 128 | if (consts[i].type == ConstAttribute && consts[i].value == type) { |
michael@0 | 129 | return consts[i].attrType; |
michael@0 | 130 | } |
michael@0 | 131 | } |
michael@0 | 132 | return ConstNone; |
michael@0 | 133 | } |
michael@0 | 134 | |
michael@0 | 135 | void |
michael@0 | 136 | printChars(const char *name, CK_ULONG size) |
michael@0 | 137 | { |
michael@0 | 138 | CK_ULONG i; |
michael@0 | 139 | for (i=0; i < size; i++) { |
michael@0 | 140 | if (name[i] == 0) { |
michael@0 | 141 | break; |
michael@0 | 142 | } |
michael@0 | 143 | printf("%c",name[i]); |
michael@0 | 144 | } |
michael@0 | 145 | printf("\n"); |
michael@0 | 146 | } |
michael@0 | 147 | |
michael@0 | 148 | #define DUMP_LEN 16 |
michael@0 | 149 | void printDump(const unsigned char *buf, int size) |
michael@0 | 150 | { |
michael@0 | 151 | int i,j; |
michael@0 | 152 | |
michael@0 | 153 | for(i=0; i < size; i+= DUMP_LEN) { |
michael@0 | 154 | printf(" "); |
michael@0 | 155 | for (j=0; j< DUMP_LEN; j++) { |
michael@0 | 156 | if (i+j < size) { |
michael@0 | 157 | printf("%02x ",buf[i+j]); |
michael@0 | 158 | } else { |
michael@0 | 159 | printf(" "); |
michael@0 | 160 | } |
michael@0 | 161 | } |
michael@0 | 162 | for (j=0; j< DUMP_LEN; j++) { |
michael@0 | 163 | if (i+j < size) { |
michael@0 | 164 | if (buf[i+j] < ' ' || buf[i+j] >= 0x7f) { |
michael@0 | 165 | printf("."); |
michael@0 | 166 | } else { |
michael@0 | 167 | printf("%c",buf[i+j]); |
michael@0 | 168 | } |
michael@0 | 169 | } else { |
michael@0 | 170 | printf(" "); |
michael@0 | 171 | } |
michael@0 | 172 | } |
michael@0 | 173 | printf("\n"); |
michael@0 | 174 | } |
michael@0 | 175 | } |
michael@0 | 176 | |
michael@0 | 177 | /* |
michael@0 | 178 | * free an argument structure |
michael@0 | 179 | */ |
michael@0 | 180 | void |
michael@0 | 181 | argFreeData(Value *arg) |
michael@0 | 182 | { |
michael@0 | 183 | if (arg->data && ((arg->type & ArgStatic) == 0)) { |
michael@0 | 184 | if ((arg->type & ArgMask) == ArgAttribute) { |
michael@0 | 185 | int i; |
michael@0 | 186 | CK_ATTRIBUTE *template = (CK_ATTRIBUTE *)arg->data; |
michael@0 | 187 | |
michael@0 | 188 | for (i=0; i < arg->arraySize; i++) { |
michael@0 | 189 | free(template[i].pValue); |
michael@0 | 190 | } |
michael@0 | 191 | } |
michael@0 | 192 | if ((arg->type & ArgMask) == ArgInitializeArgs) { |
michael@0 | 193 | CK_C_INITIALIZE_ARGS *init = (CK_C_INITIALIZE_ARGS *)arg->data; |
michael@0 | 194 | if (init->LibraryParameters) { |
michael@0 | 195 | free(init->LibraryParameters); |
michael@0 | 196 | } |
michael@0 | 197 | } |
michael@0 | 198 | free(arg->data); |
michael@0 | 199 | } |
michael@0 | 200 | arg->type &= ~ArgStatic; |
michael@0 | 201 | arg->data = NULL; |
michael@0 | 202 | } |
michael@0 | 203 | |
michael@0 | 204 | void |
michael@0 | 205 | argFree(Value *arg) |
michael@0 | 206 | { |
michael@0 | 207 | if (arg == NULL) return; |
michael@0 | 208 | |
michael@0 | 209 | arg->reference--; |
michael@0 | 210 | if (arg->reference == 0) { |
michael@0 | 211 | if (arg->type & ArgFile) { |
michael@0 | 212 | free(arg->filename); |
michael@0 | 213 | } |
michael@0 | 214 | argFreeData(arg); |
michael@0 | 215 | free (arg); |
michael@0 | 216 | } |
michael@0 | 217 | } |
michael@0 | 218 | |
michael@0 | 219 | /* |
michael@0 | 220 | * free and argument list |
michael@0 | 221 | */ |
michael@0 | 222 | void |
michael@0 | 223 | parseFree(Value **ap) |
michael@0 | 224 | { |
michael@0 | 225 | int i; |
michael@0 | 226 | for (i=0 ; i < MAX_ARGS; i++) { |
michael@0 | 227 | argFree(ap[i]); |
michael@0 | 228 | } |
michael@0 | 229 | } |
michael@0 | 230 | |
michael@0 | 231 | /* |
michael@0 | 232 | * getEnd: how for to the end of this argmument list? |
michael@0 | 233 | */ |
michael@0 | 234 | int |
michael@0 | 235 | getEnd(const char *bp) |
michael@0 | 236 | { |
michael@0 | 237 | int count = 0; |
michael@0 | 238 | |
michael@0 | 239 | while (*bp) { |
michael@0 | 240 | if (*bp == ' ' || *bp == '\t' || *bp == '\n') return count; |
michael@0 | 241 | count++; |
michael@0 | 242 | bp++; |
michael@0 | 243 | } |
michael@0 | 244 | return (count); |
michael@0 | 245 | } |
michael@0 | 246 | |
michael@0 | 247 | |
michael@0 | 248 | /* |
michael@0 | 249 | * strip: return the first none white space character |
michael@0 | 250 | */ |
michael@0 | 251 | const char * |
michael@0 | 252 | strip(const char *bp) |
michael@0 | 253 | { |
michael@0 | 254 | while (*bp && (*bp == ' ' || *bp == '\t' || *bp == '\n')) bp++; |
michael@0 | 255 | return bp; |
michael@0 | 256 | } |
michael@0 | 257 | |
michael@0 | 258 | /* |
michael@0 | 259 | * read in the next argument into dp ... don't overflow |
michael@0 | 260 | */ |
michael@0 | 261 | const char * |
michael@0 | 262 | readChars(const char *bp, char *dp, int max ) |
michael@0 | 263 | { |
michael@0 | 264 | int count = 1; |
michael@0 | 265 | while (*bp) { |
michael@0 | 266 | if (*bp == ' ' || *bp == '\t' || *bp == '\n' ) { |
michael@0 | 267 | *dp = 0; |
michael@0 | 268 | return bp; |
michael@0 | 269 | } |
michael@0 | 270 | *dp++ = *bp++; |
michael@0 | 271 | if (++count == max) break; |
michael@0 | 272 | } |
michael@0 | 273 | while (*bp && (*bp != ' ' && *bp != '\t' && *bp != '\n')) bp++; |
michael@0 | 274 | *dp = 0; |
michael@0 | 275 | return (bp); |
michael@0 | 276 | } |
michael@0 | 277 | |
michael@0 | 278 | Value * varLookup(const char *bp, char *vname, int max, int *error); |
michael@0 | 279 | |
michael@0 | 280 | CK_ULONG |
michael@0 | 281 | getValue(const char *v, int *error) |
michael@0 | 282 | { |
michael@0 | 283 | Value * varVal = NULL; |
michael@0 | 284 | CK_ULONG retVal = 0; |
michael@0 | 285 | ConstType type; |
michael@0 | 286 | char tvar[512]; |
michael@0 | 287 | |
michael@0 | 288 | *error = 0; |
michael@0 | 289 | |
michael@0 | 290 | varVal = varLookup( v, tvar, sizeof(tvar), error); |
michael@0 | 291 | |
michael@0 | 292 | if (varVal) { |
michael@0 | 293 | if ((varVal->type & ArgMask) == ArgULong) { |
michael@0 | 294 | retVal = *(CK_ULONG *)varVal->data; |
michael@0 | 295 | } else { |
michael@0 | 296 | fprintf(stderr,"%s: is not a ulong\n", v); |
michael@0 | 297 | *error = 1; |
michael@0 | 298 | } |
michael@0 | 299 | argFree(varVal); |
michael@0 | 300 | return retVal; |
michael@0 | 301 | } |
michael@0 | 302 | constLookup(v, &retVal, &type); |
michael@0 | 303 | return retVal; |
michael@0 | 304 | } |
michael@0 | 305 | |
michael@0 | 306 | Value * |
michael@0 | 307 | NewValue(ArgType type, CK_ULONG arraySize) |
michael@0 | 308 | { |
michael@0 | 309 | Value *value; |
michael@0 | 310 | |
michael@0 | 311 | value = (Value *)malloc(sizeof(Value)); |
michael@0 | 312 | if (!value) return NULL; |
michael@0 | 313 | value->size = ArgSize(type)*arraySize; |
michael@0 | 314 | value->type = type; |
michael@0 | 315 | value->filename = NULL; |
michael@0 | 316 | value->constType = ConstNone; |
michael@0 | 317 | value->data = (void *)malloc(value->size); |
michael@0 | 318 | if (!value->data) { |
michael@0 | 319 | free(value); |
michael@0 | 320 | return NULL; |
michael@0 | 321 | } |
michael@0 | 322 | value->reference = 1; |
michael@0 | 323 | value->arraySize = (type == ArgChar) ? 1: arraySize; |
michael@0 | 324 | |
michael@0 | 325 | memset(value->data, 0, value->size); |
michael@0 | 326 | return value; |
michael@0 | 327 | } |
michael@0 | 328 | |
michael@0 | 329 | #define INVALID_INDEX 0xffffffff |
michael@0 | 330 | |
michael@0 | 331 | CK_ULONG |
michael@0 | 332 | handleArray(char *vname, int *error) |
michael@0 | 333 | { |
michael@0 | 334 | char *bracket; |
michael@0 | 335 | CK_ULONG index = INVALID_INDEX; |
michael@0 | 336 | |
michael@0 | 337 | if ((bracket = strchr(vname,'[')) != 0) { |
michael@0 | 338 | char *tmpv = bracket+1; |
michael@0 | 339 | *bracket = 0; |
michael@0 | 340 | bracket = strchr(tmpv,']'); |
michael@0 | 341 | |
michael@0 | 342 | if (bracket == 0) { |
michael@0 | 343 | fprintf(stderr,"%s: missing closing brace\n", vname); |
michael@0 | 344 | return INVALID_INDEX; |
michael@0 | 345 | } |
michael@0 | 346 | *bracket = 0; |
michael@0 | 347 | |
michael@0 | 348 | index = getValue(tmpv, error); |
michael@0 | 349 | if (*error == 1) { |
michael@0 | 350 | return INVALID_INDEX; |
michael@0 | 351 | } else if (index == INVALID_INDEX) { |
michael@0 | 352 | fprintf(stderr, "%s: 0x%lx is an invalid index\n",vname,index); |
michael@0 | 353 | *error = 1; |
michael@0 | 354 | } |
michael@0 | 355 | } |
michael@0 | 356 | return index; |
michael@0 | 357 | } |
michael@0 | 358 | |
michael@0 | 359 | void * |
michael@0 | 360 | makeArrayTarget(const char *vname, const Value *value, CK_ULONG index) |
michael@0 | 361 | { |
michael@0 | 362 | char * target; |
michael@0 | 363 | CK_ULONG elementSize; |
michael@0 | 364 | |
michael@0 | 365 | if (index >= (CK_ULONG)value->arraySize) { |
michael@0 | 366 | fprintf(stderr, "%s[%lu]: index larger than array size (%d)\n", |
michael@0 | 367 | vname, index, value->arraySize); |
michael@0 | 368 | return NULL; |
michael@0 | 369 | } |
michael@0 | 370 | |
michael@0 | 371 | target = (char *)value->data; |
michael@0 | 372 | elementSize = value->size/value->arraySize; |
michael@0 | 373 | target += index * elementSize; |
michael@0 | 374 | return target; |
michael@0 | 375 | } |
michael@0 | 376 | |
michael@0 | 377 | /* |
michael@0 | 378 | * look up a variable from the variable chain |
michael@0 | 379 | */ |
michael@0 | 380 | static Variable *varHead = NULL; |
michael@0 | 381 | Value * |
michael@0 | 382 | varLookup(const char *bp, char *vname, int max, int *error) |
michael@0 | 383 | { |
michael@0 | 384 | Variable *current; |
michael@0 | 385 | CK_ULONG index = INVALID_INDEX; |
michael@0 | 386 | int isArray = 0; |
michael@0 | 387 | char *ptr; |
michael@0 | 388 | *error = 0; |
michael@0 | 389 | |
michael@0 | 390 | if (bp != NULL) { |
michael@0 | 391 | readChars(bp, vname, max); |
michael@0 | 392 | } |
michael@0 | 393 | |
michael@0 | 394 | /* don't make numbers into variables */ |
michael@0 | 395 | if (isNum(vname[0])) { |
michael@0 | 396 | return NULL; |
michael@0 | 397 | } |
michael@0 | 398 | /* nor consts */ |
michael@0 | 399 | if (isConst(vname)) { |
michael@0 | 400 | return NULL; |
michael@0 | 401 | } |
michael@0 | 402 | /* handle sizeof() */ |
michael@0 | 403 | if ((ptr = isSize(vname, &isArray)) != NULL) { |
michael@0 | 404 | CK_ULONG size; |
michael@0 | 405 | Value *targetValue = NULL; |
michael@0 | 406 | Value *sourceValue = varLookup(NULL, ptr, 0, error); |
michael@0 | 407 | if (!sourceValue) { |
michael@0 | 408 | if (*error == 0) { |
michael@0 | 409 | /* just didn't find it */ |
michael@0 | 410 | *error = 1; |
michael@0 | 411 | fprintf(stderr,"Couldn't find variable %s to take size of\n", |
michael@0 | 412 | ptr); |
michael@0 | 413 | return NULL; |
michael@0 | 414 | } |
michael@0 | 415 | } |
michael@0 | 416 | size = isArray ? sourceValue->arraySize : sourceValue->size; |
michael@0 | 417 | targetValue = NewValue(ArgULong,1); |
michael@0 | 418 | memcpy(targetValue->data, &size, sizeof(size)); |
michael@0 | 419 | |
michael@0 | 420 | return targetValue; |
michael@0 | 421 | } |
michael@0 | 422 | |
michael@0 | 423 | /* modifies vname */ |
michael@0 | 424 | index = handleArray(vname, error); |
michael@0 | 425 | if (*error == 1) { |
michael@0 | 426 | return NULL; |
michael@0 | 427 | } |
michael@0 | 428 | |
michael@0 | 429 | for (current = varHead; current; current = current->next) { |
michael@0 | 430 | if (PL_strcasecmp(current->vname, vname) == 0) { |
michael@0 | 431 | char *target; |
michael@0 | 432 | if (index == INVALID_INDEX) { |
michael@0 | 433 | (current->value->reference)++; |
michael@0 | 434 | return current->value; |
michael@0 | 435 | } |
michael@0 | 436 | target = makeArrayTarget(vname, current->value, index); |
michael@0 | 437 | if (target) { |
michael@0 | 438 | Value *element = NewValue(current->value->type, 1); |
michael@0 | 439 | if (!element) { |
michael@0 | 440 | fprintf(stderr, "MEMORY ERROR!\n"); |
michael@0 | 441 | *error = 1; |
michael@0 | 442 | } |
michael@0 | 443 | argFreeData(element); |
michael@0 | 444 | element->data = target; |
michael@0 | 445 | element->type |= ArgStatic; |
michael@0 | 446 | return element; |
michael@0 | 447 | } |
michael@0 | 448 | *error = 1; |
michael@0 | 449 | return NULL; |
michael@0 | 450 | } |
michael@0 | 451 | } |
michael@0 | 452 | return NULL; |
michael@0 | 453 | } |
michael@0 | 454 | |
michael@0 | 455 | static CK_RV |
michael@0 | 456 | list(void) |
michael@0 | 457 | { |
michael@0 | 458 | Variable *current; |
michael@0 | 459 | |
michael@0 | 460 | if (varHead) { |
michael@0 | 461 | printf(" %10s\t%16s\t%8s\tSize\tElements\n","Name","Type","Const"); |
michael@0 | 462 | } else { |
michael@0 | 463 | printf(" no variables set\n"); |
michael@0 | 464 | } |
michael@0 | 465 | |
michael@0 | 466 | for (current = varHead; current; current = current->next) { |
michael@0 | 467 | printf(" %10s\t%16s\t%8s\t%d\t%d\n", current->vname, |
michael@0 | 468 | valueString[current->value->type&ArgMask], |
michael@0 | 469 | constTypeString[current->value->constType], |
michael@0 | 470 | current->value->size, current->value->arraySize); |
michael@0 | 471 | } |
michael@0 | 472 | return CKR_OK; |
michael@0 | 473 | } |
michael@0 | 474 | |
michael@0 | 475 | CK_RV |
michael@0 | 476 | printFlags(const char *s, CK_ULONG flags, ConstType type) |
michael@0 | 477 | { |
michael@0 | 478 | CK_ULONG i; |
michael@0 | 479 | int needComma = 0; |
michael@0 | 480 | |
michael@0 | 481 | printf("%s",s); |
michael@0 | 482 | for (i=1; i ; i=i << 1) { |
michael@0 | 483 | if (flags & i) { |
michael@0 | 484 | printf("%s",needComma?",":""); |
michael@0 | 485 | printConst(i, type, 0); |
michael@0 | 486 | needComma=1; |
michael@0 | 487 | } |
michael@0 | 488 | } |
michael@0 | 489 | if (!needComma) { |
michael@0 | 490 | printf("Empty"); |
michael@0 | 491 | } |
michael@0 | 492 | printf("\n"); |
michael@0 | 493 | return CKR_OK; |
michael@0 | 494 | } |
michael@0 | 495 | |
michael@0 | 496 | /* |
michael@0 | 497 | * add a new variable to the chain |
michael@0 | 498 | */ |
michael@0 | 499 | const char * |
michael@0 | 500 | AddVariable(const char *bp, Value **ptr) |
michael@0 | 501 | { |
michael@0 | 502 | char vname[512]; |
michael@0 | 503 | Variable *current; |
michael@0 | 504 | int index = INVALID_INDEX; |
michael@0 | 505 | int size; |
michael@0 | 506 | int error = 0; |
michael@0 | 507 | |
michael@0 | 508 | bp = readChars(bp,vname,sizeof(vname)); |
michael@0 | 509 | |
michael@0 | 510 | /* don't make numbers into variables */ |
michael@0 | 511 | if (isNum(vname[0])) { |
michael@0 | 512 | return bp; |
michael@0 | 513 | } |
michael@0 | 514 | /* or consts */ |
michael@0 | 515 | if (isConst(vname)) { |
michael@0 | 516 | return bp; |
michael@0 | 517 | } |
michael@0 | 518 | /* or NULLs */ |
michael@0 | 519 | if (vname[0] == 0) { |
michael@0 | 520 | return bp; |
michael@0 | 521 | } |
michael@0 | 522 | /* or sizeof */ |
michael@0 | 523 | if (isSize(vname, NULL)) { |
michael@0 | 524 | return bp; |
michael@0 | 525 | } |
michael@0 | 526 | /* arrays values should be written back to the original */ |
michael@0 | 527 | index = handleArray(vname, &error); |
michael@0 | 528 | if (error == 1) { |
michael@0 | 529 | return bp; |
michael@0 | 530 | } |
michael@0 | 531 | |
michael@0 | 532 | |
michael@0 | 533 | for (current = varHead; current; current = current->next) { |
michael@0 | 534 | if (PL_strcasecmp(current->vname,vname) == 0) { |
michael@0 | 535 | char *target; |
michael@0 | 536 | /* found a complete object, return the found one */ |
michael@0 | 537 | if (index == INVALID_INDEX) { |
michael@0 | 538 | argFree(*ptr); |
michael@0 | 539 | *ptr = current->value; |
michael@0 | 540 | return bp; |
michael@0 | 541 | } |
michael@0 | 542 | /* found an array, update the array element */ |
michael@0 | 543 | target = makeArrayTarget(vname, current->value, index); |
michael@0 | 544 | if (target) { |
michael@0 | 545 | memcpy(target, (*ptr)->data, (*ptr)->size); |
michael@0 | 546 | argFreeData(*ptr); |
michael@0 | 547 | (*ptr)->data = target; |
michael@0 | 548 | (*ptr)->type |= ArgStatic; |
michael@0 | 549 | } |
michael@0 | 550 | return bp; |
michael@0 | 551 | } |
michael@0 | 552 | } |
michael@0 | 553 | |
michael@0 | 554 | /* we are looking for an array and didn't find one */ |
michael@0 | 555 | if (index != INVALID_INDEX) { |
michael@0 | 556 | return bp; |
michael@0 | 557 | } |
michael@0 | 558 | |
michael@0 | 559 | |
michael@0 | 560 | current = (Variable *)malloc(sizeof(Variable)); |
michael@0 | 561 | size = strlen(vname); |
michael@0 | 562 | current->vname = (char *)malloc(size+1); |
michael@0 | 563 | strcpy(current->vname,vname); |
michael@0 | 564 | current->value = *ptr; |
michael@0 | 565 | (*ptr)->reference++; |
michael@0 | 566 | |
michael@0 | 567 | current->next = varHead; |
michael@0 | 568 | varHead = current; |
michael@0 | 569 | return bp; |
michael@0 | 570 | } |
michael@0 | 571 | |
michael@0 | 572 | ArgType |
michael@0 | 573 | FindTypeByName(const char *typeName) |
michael@0 | 574 | { |
michael@0 | 575 | int i; |
michael@0 | 576 | |
michael@0 | 577 | for (i=0; i < valueCount; i++) { |
michael@0 | 578 | if (PL_strcasecmp(typeName,valueString[i]) == 0) { |
michael@0 | 579 | return (ArgType) i; |
michael@0 | 580 | } |
michael@0 | 581 | if (valueString[i][0] == 'C' && valueString[i][1] == 'K' && |
michael@0 | 582 | valueString[i][2] == '_' && |
michael@0 | 583 | (PL_strcasecmp(typeName,&valueString[i][3]) == 0)) { |
michael@0 | 584 | return (ArgType) i; |
michael@0 | 585 | } |
michael@0 | 586 | } |
michael@0 | 587 | return ArgNone; |
michael@0 | 588 | } |
michael@0 | 589 | |
michael@0 | 590 | CK_RV |
michael@0 | 591 | ArrayVariable(const char *bp, const char *typeName, CK_ULONG count) |
michael@0 | 592 | { |
michael@0 | 593 | ArgType type; |
michael@0 | 594 | Value *value; /* new Value */ |
michael@0 | 595 | |
michael@0 | 596 | type = FindTypeByName(typeName); |
michael@0 | 597 | if (type == ArgNone) { |
michael@0 | 598 | fprintf(stderr,"Invalid type (%s)\n", typeName); |
michael@0 | 599 | return CKR_FUNCTION_FAILED; |
michael@0 | 600 | } |
michael@0 | 601 | value = NewValue(type, count); |
michael@0 | 602 | (void) AddVariable(bp, &value); |
michael@0 | 603 | return CKR_OK; |
michael@0 | 604 | } |
michael@0 | 605 | |
michael@0 | 606 | #define MAX_TEMPLATE 25 |
michael@0 | 607 | |
michael@0 | 608 | CK_RV |
michael@0 | 609 | ArrayTemplate(const char *bp, char *attributes) |
michael@0 | 610 | { |
michael@0 | 611 | char aname[512]; |
michael@0 | 612 | CK_ULONG attributeTypes[MAX_TEMPLATE]; |
michael@0 | 613 | CK_ATTRIBUTE *template; |
michael@0 | 614 | Value *value; /* new Value */ |
michael@0 | 615 | char *ap; |
michael@0 | 616 | int i, count = 0; |
michael@0 | 617 | |
michael@0 | 618 | memcpy(aname,attributes,strlen(attributes)+1); |
michael@0 | 619 | |
michael@0 | 620 | for (ap = aname, count = 0; ap && *ap && count < MAX_TEMPLATE; count++) { |
michael@0 | 621 | char *cur = ap; |
michael@0 | 622 | ConstType type; |
michael@0 | 623 | |
michael@0 | 624 | ap = strchr(ap,','); |
michael@0 | 625 | if (ap) { |
michael@0 | 626 | *ap++ = 0; |
michael@0 | 627 | } |
michael@0 | 628 | |
michael@0 | 629 | (void)constLookup(cur, &attributeTypes[count], &type); |
michael@0 | 630 | if ((type != ConstAttribute) && (type != ConstNone)) { |
michael@0 | 631 | fprintf(stderr, "Unknown Attribute %s\n", cur); |
michael@0 | 632 | return CKR_FUNCTION_FAILED; |
michael@0 | 633 | } |
michael@0 | 634 | } |
michael@0 | 635 | |
michael@0 | 636 | value = NewValue(ArgAttribute, count); |
michael@0 | 637 | |
michael@0 | 638 | template = (CK_ATTRIBUTE *)value->data; |
michael@0 | 639 | for (i=0; i < count ; i++) { |
michael@0 | 640 | template[i].type = attributeTypes[i]; |
michael@0 | 641 | } |
michael@0 | 642 | (void) AddVariable(bp, &value); |
michael@0 | 643 | return CKR_OK; |
michael@0 | 644 | } |
michael@0 | 645 | |
michael@0 | 646 | CK_RV |
michael@0 | 647 | BuildTemplate(Value *vp) |
michael@0 | 648 | { |
michael@0 | 649 | CK_ATTRIBUTE *template = (CK_ATTRIBUTE *)vp->data; |
michael@0 | 650 | int i; |
michael@0 | 651 | |
michael@0 | 652 | for (i=0; i < vp->arraySize; i++) { |
michael@0 | 653 | if (((signed long)template[i].ulValueLen) > 0) { |
michael@0 | 654 | if (template[i].pValue) free(template[i].pValue); |
michael@0 | 655 | template[i].pValue = malloc(template[i].ulValueLen); |
michael@0 | 656 | } |
michael@0 | 657 | } |
michael@0 | 658 | return CKR_OK; |
michael@0 | 659 | } |
michael@0 | 660 | |
michael@0 | 661 | CK_RV |
michael@0 | 662 | SetTemplate(Value *vp, CK_ULONG index, CK_ULONG value) |
michael@0 | 663 | { |
michael@0 | 664 | CK_ATTRIBUTE *template = (CK_ATTRIBUTE *)vp->data; |
michael@0 | 665 | int isbool = 0; |
michael@0 | 666 | CK_ULONG len; |
michael@0 | 667 | ConstType attrType; |
michael@0 | 668 | |
michael@0 | 669 | if (index >= (CK_ULONG) vp->arraySize) { |
michael@0 | 670 | fprintf(stderr,"index (%lu) greater than array (%d)\n", |
michael@0 | 671 | index, vp->arraySize); |
michael@0 | 672 | return CKR_ARGUMENTS_BAD; |
michael@0 | 673 | } |
michael@0 | 674 | attrType = getConstFromAttribute(template[index].type); |
michael@0 | 675 | |
michael@0 | 676 | if (attrType == ConstNone) { |
michael@0 | 677 | fprintf(stderr,"can't set index (%lu) because ", index); |
michael@0 | 678 | printConst(template[index].type,ConstAttribute, 0); |
michael@0 | 679 | fprintf(stderr, " is not a CK_BBOOL or CK_ULONG\n"); |
michael@0 | 680 | return CKR_ARGUMENTS_BAD; |
michael@0 | 681 | } |
michael@0 | 682 | isbool = (attrType == ConstBool); |
michael@0 | 683 | len = isbool ? sizeof (CK_BBOOL) : sizeof(CK_ULONG); |
michael@0 | 684 | if ((template[index].ulValueLen != len) || (template[index].pValue)) { |
michael@0 | 685 | free(template[index].pValue); |
michael@0 | 686 | template[index].pValue = malloc(len); |
michael@0 | 687 | template[index].ulValueLen = len; |
michael@0 | 688 | } |
michael@0 | 689 | if (isbool) { |
michael@0 | 690 | *(CK_BBOOL *)template[index].pValue = (CK_BBOOL) value; |
michael@0 | 691 | } else { |
michael@0 | 692 | *(CK_ULONG *)template[index].pValue = (CK_ULONG) value; |
michael@0 | 693 | } |
michael@0 | 694 | return CKR_OK; |
michael@0 | 695 | |
michael@0 | 696 | } |
michael@0 | 697 | |
michael@0 | 698 | CK_RV |
michael@0 | 699 | NewMechanism(const char *bp, CK_ULONG mechType) |
michael@0 | 700 | { |
michael@0 | 701 | Value *value; /* new Value */ |
michael@0 | 702 | CK_MECHANISM *mechanism; |
michael@0 | 703 | |
michael@0 | 704 | value = NewValue(ArgMechanism, 1); |
michael@0 | 705 | mechanism = (CK_MECHANISM *)value->data; |
michael@0 | 706 | mechanism->mechanism = mechType; |
michael@0 | 707 | mechanism->pParameter = NULL; |
michael@0 | 708 | mechanism->ulParameterLen = 0; |
michael@0 | 709 | (void) AddVariable(bp, &value); |
michael@0 | 710 | return CKR_OK; |
michael@0 | 711 | } |
michael@0 | 712 | |
michael@0 | 713 | CK_RV |
michael@0 | 714 | NewInitializeArgs(const char *bp, CK_ULONG flags, const char *param) |
michael@0 | 715 | { |
michael@0 | 716 | Value *value; /* new Value */ |
michael@0 | 717 | CK_C_INITIALIZE_ARGS *init; |
michael@0 | 718 | |
michael@0 | 719 | value = NewValue(ArgInitializeArgs, 1); |
michael@0 | 720 | init = (CK_C_INITIALIZE_ARGS *)value->data; |
michael@0 | 721 | init->flags = flags; |
michael@0 | 722 | if (strcmp(param, "null") != 0) { |
michael@0 | 723 | init->LibraryParameters = (CK_CHAR_PTR *)strdup(param); |
michael@0 | 724 | } |
michael@0 | 725 | (void) AddVariable(bp, &value); |
michael@0 | 726 | return CKR_OK; |
michael@0 | 727 | } |
michael@0 | 728 | |
michael@0 | 729 | /* |
michael@0 | 730 | * add a new variable to the chain |
michael@0 | 731 | */ |
michael@0 | 732 | CK_RV |
michael@0 | 733 | DeleteVariable(const char *bp) |
michael@0 | 734 | { |
michael@0 | 735 | char vname[512]; |
michael@0 | 736 | Variable **current; |
michael@0 | 737 | |
michael@0 | 738 | bp = readChars(bp,vname,sizeof(vname)); |
michael@0 | 739 | |
michael@0 | 740 | for (current = &varHead; *current; current = &(*current)->next) { |
michael@0 | 741 | if (PL_strcasecmp((*current)->vname,vname) == 0) { |
michael@0 | 742 | argFree((*current)->value); |
michael@0 | 743 | *current = (*current)->next; |
michael@0 | 744 | break; |
michael@0 | 745 | } |
michael@0 | 746 | } |
michael@0 | 747 | return CKR_OK; |
michael@0 | 748 | } |
michael@0 | 749 | |
michael@0 | 750 | /* |
michael@0 | 751 | * convert an octal value to integer |
michael@0 | 752 | */ |
michael@0 | 753 | CK_ULONG |
michael@0 | 754 | otoi(const char *o) |
michael@0 | 755 | { |
michael@0 | 756 | CK_ULONG value = 0; |
michael@0 | 757 | |
michael@0 | 758 | while (*o) { |
michael@0 | 759 | if ((*o >= '0') && (*o <= '7')) { |
michael@0 | 760 | value = (value << 3) | (unsigned)(*o - '0'); |
michael@0 | 761 | } else { |
michael@0 | 762 | break; |
michael@0 | 763 | } |
michael@0 | 764 | } |
michael@0 | 765 | return value; |
michael@0 | 766 | } |
michael@0 | 767 | |
michael@0 | 768 | /* |
michael@0 | 769 | * convert a hex value to integer |
michael@0 | 770 | */ |
michael@0 | 771 | CK_ULONG |
michael@0 | 772 | htoi(const char *x) |
michael@0 | 773 | { |
michael@0 | 774 | CK_ULONG value = 0; |
michael@0 | 775 | |
michael@0 | 776 | while (*x) { |
michael@0 | 777 | if ((*x >= '0') && (*x <= '9')) { |
michael@0 | 778 | value = (value << 4) | (unsigned)(*x - '0'); |
michael@0 | 779 | } else if ((*x >= 'a') && (*x <= 'f')) { |
michael@0 | 780 | value = (value << 4) | (unsigned)(*x - 'a'); |
michael@0 | 781 | } else if ((*x >= 'A') && (*x <= 'F')) { |
michael@0 | 782 | value = (value << 4) | (unsigned)(*x - 'A'); |
michael@0 | 783 | } else { |
michael@0 | 784 | break; |
michael@0 | 785 | } |
michael@0 | 786 | } |
michael@0 | 787 | return value; |
michael@0 | 788 | } |
michael@0 | 789 | |
michael@0 | 790 | |
michael@0 | 791 | /* |
michael@0 | 792 | * look up or decode a constant value |
michael@0 | 793 | */ |
michael@0 | 794 | const char * |
michael@0 | 795 | constLookup(const char *bp, CK_ULONG *value, ConstType *type) |
michael@0 | 796 | { |
michael@0 | 797 | char vname[512]; |
michael@0 | 798 | int i; |
michael@0 | 799 | |
michael@0 | 800 | bp = readChars(bp,vname,sizeof(vname)); |
michael@0 | 801 | |
michael@0 | 802 | for (i=0; i < constCount; i++) { |
michael@0 | 803 | if ((PL_strcasecmp(consts[i].name,vname) == 0) || |
michael@0 | 804 | PL_strcasecmp(consts[i].name+5,vname) == 0) { |
michael@0 | 805 | *value = consts[i].value; |
michael@0 | 806 | *type = consts[i].type; |
michael@0 | 807 | return bp; |
michael@0 | 808 | } |
michael@0 | 809 | } |
michael@0 | 810 | |
michael@0 | 811 | *type = ConstNone; |
michael@0 | 812 | if (vname[0] == '0' && vname[1] == 'X') { |
michael@0 | 813 | *value = htoi(&vname[2]); |
michael@0 | 814 | } else if (vname[0] == '0') { |
michael@0 | 815 | *value = otoi(&vname[1]); |
michael@0 | 816 | } else { |
michael@0 | 817 | *value = atoi(vname); |
michael@0 | 818 | } |
michael@0 | 819 | return bp; |
michael@0 | 820 | } |
michael@0 | 821 | |
michael@0 | 822 | int |
michael@0 | 823 | ArgSize(ArgType type) |
michael@0 | 824 | { |
michael@0 | 825 | int size=0; |
michael@0 | 826 | type &= ArgMask; |
michael@0 | 827 | |
michael@0 | 828 | switch (type) { |
michael@0 | 829 | case ArgNone: |
michael@0 | 830 | size = 0; |
michael@0 | 831 | break; |
michael@0 | 832 | case ArgULong: |
michael@0 | 833 | size = sizeof(CK_ULONG); |
michael@0 | 834 | break; |
michael@0 | 835 | case ArgVar: |
michael@0 | 836 | size = 1; /* get's changed later */ |
michael@0 | 837 | break; |
michael@0 | 838 | case ArgChar: |
michael@0 | 839 | case ArgUTF8: |
michael@0 | 840 | size = 1; |
michael@0 | 841 | break; |
michael@0 | 842 | case ArgInfo: |
michael@0 | 843 | size = sizeof(CK_INFO); |
michael@0 | 844 | break; |
michael@0 | 845 | case ArgSlotInfo: |
michael@0 | 846 | size = sizeof(CK_SLOT_INFO); |
michael@0 | 847 | break; |
michael@0 | 848 | case ArgTokenInfo: |
michael@0 | 849 | size = sizeof(CK_TOKEN_INFO); |
michael@0 | 850 | break; |
michael@0 | 851 | case ArgSessionInfo: |
michael@0 | 852 | size = sizeof(CK_SESSION_INFO); |
michael@0 | 853 | break; |
michael@0 | 854 | case ArgAttribute: |
michael@0 | 855 | size = sizeof(CK_ATTRIBUTE); |
michael@0 | 856 | break; |
michael@0 | 857 | case ArgMechanism: |
michael@0 | 858 | size = sizeof(CK_MECHANISM); |
michael@0 | 859 | break; |
michael@0 | 860 | case ArgMechanismInfo: |
michael@0 | 861 | size = sizeof(CK_MECHANISM_INFO); |
michael@0 | 862 | break; |
michael@0 | 863 | case ArgInitializeArgs: |
michael@0 | 864 | size = sizeof(CK_C_INITIALIZE_ARGS); |
michael@0 | 865 | break; |
michael@0 | 866 | case ArgFunctionList: |
michael@0 | 867 | size = sizeof(CK_FUNCTION_LIST); |
michael@0 | 868 | break; |
michael@0 | 869 | default: |
michael@0 | 870 | break; |
michael@0 | 871 | } |
michael@0 | 872 | |
michael@0 | 873 | return (size); |
michael@0 | 874 | } |
michael@0 | 875 | |
michael@0 | 876 | CK_RV |
michael@0 | 877 | restore(const char *filename,Value *ptr) |
michael@0 | 878 | { |
michael@0 | 879 | int fd,size; |
michael@0 | 880 | |
michael@0 | 881 | fd = open(filename,O_RDONLY|O_BINARY); |
michael@0 | 882 | if (fd < 0) { |
michael@0 | 883 | perror(filename); |
michael@0 | 884 | return CKR_FUNCTION_FAILED; |
michael@0 | 885 | } |
michael@0 | 886 | |
michael@0 | 887 | size = read(fd,ptr->data,ptr->size); |
michael@0 | 888 | if (systemFlags & FLAG_VerifyFile) { |
michael@0 | 889 | printDump(ptr->data,ptr->size); |
michael@0 | 890 | } |
michael@0 | 891 | if (size < 0) { |
michael@0 | 892 | perror(filename); |
michael@0 | 893 | return CKR_FUNCTION_FAILED; |
michael@0 | 894 | } else if (size != ptr->size) { |
michael@0 | 895 | fprintf(stderr,"%s: only read %d bytes, needed to read %d bytes\n", |
michael@0 | 896 | filename,size,ptr->size); |
michael@0 | 897 | return CKR_FUNCTION_FAILED; |
michael@0 | 898 | } |
michael@0 | 899 | close(fd); |
michael@0 | 900 | return CKR_OK; |
michael@0 | 901 | } |
michael@0 | 902 | |
michael@0 | 903 | CK_RV |
michael@0 | 904 | save(const char *filename,Value *ptr) |
michael@0 | 905 | { |
michael@0 | 906 | int fd,size; |
michael@0 | 907 | |
michael@0 | 908 | fd = open(filename,O_WRONLY|O_BINARY|O_CREAT,0666); |
michael@0 | 909 | if (fd < 0) { |
michael@0 | 910 | perror(filename); |
michael@0 | 911 | return CKR_FUNCTION_FAILED; |
michael@0 | 912 | } |
michael@0 | 913 | |
michael@0 | 914 | size = write(fd,ptr->data,ptr->size); |
michael@0 | 915 | if (size < 0) { |
michael@0 | 916 | perror(filename); |
michael@0 | 917 | return CKR_FUNCTION_FAILED; |
michael@0 | 918 | } else if (size != ptr->size) { |
michael@0 | 919 | fprintf(stderr,"%s: only wrote %d bytes, need to write %d bytes\n", |
michael@0 | 920 | filename,size,ptr->size); |
michael@0 | 921 | return CKR_FUNCTION_FAILED; |
michael@0 | 922 | } |
michael@0 | 923 | close(fd); |
michael@0 | 924 | return CKR_OK; |
michael@0 | 925 | } |
michael@0 | 926 | |
michael@0 | 927 | static CK_RV |
michael@0 | 928 | increment(Value *ptr, CK_ULONG value) |
michael@0 | 929 | { |
michael@0 | 930 | if ((ptr->type & ArgMask) != ArgULong) { |
michael@0 | 931 | return CKR_ARGUMENTS_BAD; |
michael@0 | 932 | } |
michael@0 | 933 | *(CK_ULONG *)ptr->data += value; |
michael@0 | 934 | return CKR_OK; |
michael@0 | 935 | } |
michael@0 | 936 | |
michael@0 | 937 | static CK_RV |
michael@0 | 938 | decrement(Value *ptr, CK_ULONG value) |
michael@0 | 939 | { |
michael@0 | 940 | if ((ptr->type & ArgMask) != ArgULong) { |
michael@0 | 941 | return CKR_ARGUMENTS_BAD; |
michael@0 | 942 | } |
michael@0 | 943 | *(CK_ULONG *)ptr->data -= value; |
michael@0 | 944 | return CKR_OK; |
michael@0 | 945 | } |
michael@0 | 946 | |
michael@0 | 947 | CK_RV |
michael@0 | 948 | printArg(Value *ptr,int arg_number) |
michael@0 | 949 | { |
michael@0 | 950 | ArgType type = ptr->type & ArgMask; |
michael@0 | 951 | CK_INFO *info; |
michael@0 | 952 | CK_SLOT_INFO *slotInfo; |
michael@0 | 953 | CK_TOKEN_INFO *tokenInfo; |
michael@0 | 954 | CK_SESSION_INFO *sessionInfo; |
michael@0 | 955 | CK_ATTRIBUTE *attribute; |
michael@0 | 956 | CK_MECHANISM *mechanism; |
michael@0 | 957 | CK_MECHANISM_INFO *mechanismInfo; |
michael@0 | 958 | CK_C_INITIALIZE_ARGS *initArgs; |
michael@0 | 959 | CK_FUNCTION_LIST *functionList; |
michael@0 | 960 | CK_RV ckrv = CKR_OK; |
michael@0 | 961 | ConstType constType; |
michael@0 | 962 | |
michael@0 | 963 | if (arg_number) { |
michael@0 | 964 | printf("Arg %d: \n",arg_number); |
michael@0 | 965 | } |
michael@0 | 966 | if (ptr->arraySize > 1) { |
michael@0 | 967 | Value element; |
michael@0 | 968 | int i; |
michael@0 | 969 | int elementSize = ptr->size/ptr->arraySize; |
michael@0 | 970 | char *dp = (char *)ptr->data; |
michael@0 | 971 | |
michael@0 | 972 | /* build a temporary Value to hold a single element */ |
michael@0 | 973 | element.type = type; |
michael@0 | 974 | element.constType = ptr->constType; |
michael@0 | 975 | element.size = elementSize; |
michael@0 | 976 | element.filename = ptr->filename; |
michael@0 | 977 | element.reference = 1; |
michael@0 | 978 | element.arraySize = 1; |
michael@0 | 979 | for (i=0; i < ptr->arraySize; i++) { |
michael@0 | 980 | printf(" -----[ %d ] -----\n", i); |
michael@0 | 981 | element.data = (void *) &dp[i*elementSize]; |
michael@0 | 982 | (void) printArg(&element, 0); |
michael@0 | 983 | } |
michael@0 | 984 | return ckrv; |
michael@0 | 985 | } |
michael@0 | 986 | if (ptr->data == NULL) { |
michael@0 | 987 | printf(" NULL ptr to a %s\n", valueString[type]); |
michael@0 | 988 | return ckrv; |
michael@0 | 989 | } |
michael@0 | 990 | switch (type) { |
michael@0 | 991 | case ArgNone: |
michael@0 | 992 | printf(" None\n"); |
michael@0 | 993 | break; |
michael@0 | 994 | case ArgULong: |
michael@0 | 995 | printf(" %lu (0x%lx)\n", *((CK_ULONG *)ptr->data), |
michael@0 | 996 | *((CK_ULONG *)ptr->data)); |
michael@0 | 997 | if (ptr->constType != ConstNone) { |
michael@0 | 998 | printf(" "); |
michael@0 | 999 | printConst(*(CK_ULONG *)ptr->data,ptr->constType,1); |
michael@0 | 1000 | } |
michael@0 | 1001 | break; |
michael@0 | 1002 | case ArgVar: |
michael@0 | 1003 | printf(" %s\n",(char *)ptr->data); |
michael@0 | 1004 | break; |
michael@0 | 1005 | case ArgUTF8: |
michael@0 | 1006 | printf(" %s\n",(char *)ptr->data); |
michael@0 | 1007 | break; |
michael@0 | 1008 | case ArgChar: |
michael@0 | 1009 | printDump(ptr->data,ptr->size); |
michael@0 | 1010 | break; |
michael@0 | 1011 | case ArgInfo: |
michael@0 | 1012 | #define VERSION(x) (x).major, (x).minor |
michael@0 | 1013 | info = (CK_INFO *)ptr->data; |
michael@0 | 1014 | printf(" Cryptoki Version: %d.%02d\n", |
michael@0 | 1015 | VERSION(info->cryptokiVersion)); |
michael@0 | 1016 | printf(" Manufacturer ID: "); |
michael@0 | 1017 | printChars((char *)info->manufacturerID, |
michael@0 | 1018 | sizeof(info->manufacturerID)); |
michael@0 | 1019 | printFlags(" Flags: ", info->flags, ConstInfoFlags); |
michael@0 | 1020 | printf(" Library Description: "); |
michael@0 | 1021 | printChars((char *)info->libraryDescription, |
michael@0 | 1022 | sizeof(info->libraryDescription)); |
michael@0 | 1023 | printf(" Library Version: %d.%02d\n", |
michael@0 | 1024 | VERSION(info->libraryVersion)); |
michael@0 | 1025 | break; |
michael@0 | 1026 | case ArgSlotInfo: |
michael@0 | 1027 | slotInfo = (CK_SLOT_INFO *)ptr->data; |
michael@0 | 1028 | printf(" Slot Description: "); |
michael@0 | 1029 | printChars((char *)slotInfo->slotDescription, |
michael@0 | 1030 | sizeof(slotInfo->slotDescription)); |
michael@0 | 1031 | printf(" Manufacturer ID: "); |
michael@0 | 1032 | printChars((char *)slotInfo->manufacturerID, |
michael@0 | 1033 | sizeof(slotInfo->manufacturerID)); |
michael@0 | 1034 | printFlags(" Flags: ", slotInfo->flags, ConstSlotFlags); |
michael@0 | 1035 | printf(" Hardware Version: %d.%02d\n", |
michael@0 | 1036 | VERSION(slotInfo->hardwareVersion)); |
michael@0 | 1037 | printf(" Firmware Version: %d.%02d\n", |
michael@0 | 1038 | VERSION(slotInfo->firmwareVersion)); |
michael@0 | 1039 | break; |
michael@0 | 1040 | case ArgTokenInfo: |
michael@0 | 1041 | tokenInfo = (CK_TOKEN_INFO *)ptr->data; |
michael@0 | 1042 | printf(" Label: "); |
michael@0 | 1043 | printChars((char *) tokenInfo->label,sizeof(tokenInfo->label)); |
michael@0 | 1044 | printf(" Manufacturer ID: "); |
michael@0 | 1045 | printChars((char *)tokenInfo->manufacturerID, |
michael@0 | 1046 | sizeof(tokenInfo->manufacturerID)); |
michael@0 | 1047 | printf(" Model: "); |
michael@0 | 1048 | printChars((char *)tokenInfo->model,sizeof(tokenInfo->model)); |
michael@0 | 1049 | printf(" Serial Number: "); |
michael@0 | 1050 | printChars((char *)tokenInfo->serialNumber, |
michael@0 | 1051 | sizeof(tokenInfo->serialNumber)); |
michael@0 | 1052 | printFlags(" Flags: ", tokenInfo->flags, ConstTokenFlags); |
michael@0 | 1053 | printf(" Max Session Count: "); |
michael@0 | 1054 | printConst(tokenInfo->ulMaxSessionCount, ConstAvailableSizes, 1); |
michael@0 | 1055 | printf(" Session Count: "); |
michael@0 | 1056 | printConst(tokenInfo->ulSessionCount, ConstCurrentSize, 1); |
michael@0 | 1057 | printf(" RW Session Count: "); |
michael@0 | 1058 | printConst(tokenInfo->ulMaxRwSessionCount, ConstAvailableSizes, 1); |
michael@0 | 1059 | printf(" Max Pin Length : "); |
michael@0 | 1060 | printConst(tokenInfo->ulMaxPinLen, ConstCurrentSize, 1); |
michael@0 | 1061 | printf(" Min Pin Length : "); |
michael@0 | 1062 | printConst(tokenInfo->ulMinPinLen, ConstCurrentSize, 1); |
michael@0 | 1063 | printf(" Total Public Memory: "); |
michael@0 | 1064 | printConst(tokenInfo->ulTotalPublicMemory, ConstAvailableSizes, 1); |
michael@0 | 1065 | printf(" Free Public Memory: "); |
michael@0 | 1066 | printConst(tokenInfo->ulFreePublicMemory, ConstCurrentSize, 1); |
michael@0 | 1067 | printf(" Total Private Memory: "); |
michael@0 | 1068 | printConst(tokenInfo->ulTotalPrivateMemory, ConstAvailableSizes, 1); |
michael@0 | 1069 | printf(" Free Private Memory: "); |
michael@0 | 1070 | printConst(tokenInfo->ulFreePrivateMemory, ConstCurrentSize, 1); |
michael@0 | 1071 | printf(" Hardware Version: %d.%02d\n", |
michael@0 | 1072 | VERSION(tokenInfo->hardwareVersion)); |
michael@0 | 1073 | printf(" Firmware Version: %d.%02d\n", |
michael@0 | 1074 | VERSION(tokenInfo->firmwareVersion)); |
michael@0 | 1075 | printf(" UTC Time: "); |
michael@0 | 1076 | printChars((char *)tokenInfo->utcTime,sizeof(tokenInfo->utcTime)); |
michael@0 | 1077 | break; |
michael@0 | 1078 | case ArgSessionInfo: |
michael@0 | 1079 | sessionInfo = (CK_SESSION_INFO *)ptr->data; |
michael@0 | 1080 | printf(" SlotID: 0x%08lx\n", sessionInfo->slotID); |
michael@0 | 1081 | printf(" State: "); |
michael@0 | 1082 | printConst(sessionInfo->state, ConstSessionState, 1); |
michael@0 | 1083 | printFlags(" Flags: ", sessionInfo->flags, ConstSessionFlags); |
michael@0 | 1084 | printf(" Device error: %lu 0x%08lx\n",sessionInfo->ulDeviceError, |
michael@0 | 1085 | sessionInfo->ulDeviceError); |
michael@0 | 1086 | break; |
michael@0 | 1087 | case ArgAttribute: |
michael@0 | 1088 | attribute = (CK_ATTRIBUTE *)ptr->data; |
michael@0 | 1089 | printf(" Attribute Type: "); |
michael@0 | 1090 | printConst(attribute->type, ConstAttribute, 1); |
michael@0 | 1091 | printf(" Attribute Data: "); |
michael@0 | 1092 | if (attribute->pValue == NULL) { |
michael@0 | 1093 | printf("NULL\n"); |
michael@0 | 1094 | printf("Attribute Len: %lu\n",attribute->ulValueLen); |
michael@0 | 1095 | } else { |
michael@0 | 1096 | constType = getConstFromAttribute(attribute->type); |
michael@0 | 1097 | if (constType != ConstNone) { |
michael@0 | 1098 | CK_ULONG value = (constType == ConstBool) ? |
michael@0 | 1099 | *(CK_BBOOL *)attribute->pValue : |
michael@0 | 1100 | *(CK_ULONG *)attribute->pValue; |
michael@0 | 1101 | printConst(value, constType, 1); |
michael@0 | 1102 | } else { |
michael@0 | 1103 | printf("\n"); |
michael@0 | 1104 | printDump(attribute->pValue, attribute->ulValueLen); |
michael@0 | 1105 | } |
michael@0 | 1106 | } |
michael@0 | 1107 | break; |
michael@0 | 1108 | case ArgMechanism: |
michael@0 | 1109 | mechanism = (CK_MECHANISM *)ptr->data; |
michael@0 | 1110 | printf(" Mechanism Type: "); |
michael@0 | 1111 | printConst(mechanism->mechanism, ConstMechanism, 1); |
michael@0 | 1112 | printf(" Mechanism Data:\n"); |
michael@0 | 1113 | printDump(mechanism->pParameter, mechanism->ulParameterLen); |
michael@0 | 1114 | break; |
michael@0 | 1115 | case ArgMechanismInfo: |
michael@0 | 1116 | mechanismInfo = (CK_MECHANISM_INFO *)ptr->data; |
michael@0 | 1117 | printf(" Minimum Key Size: %ld\n",mechanismInfo->ulMinKeySize); |
michael@0 | 1118 | printf(" Maximum Key Size: %ld\n",mechanismInfo->ulMaxKeySize); |
michael@0 | 1119 | printFlags(" Flags: ", mechanismInfo->flags, ConstMechanismFlags); |
michael@0 | 1120 | break; |
michael@0 | 1121 | case ArgInitializeArgs: |
michael@0 | 1122 | initArgs = (CK_C_INITIALIZE_ARGS *)ptr->data; |
michael@0 | 1123 | printFlags(" Flags: ", initArgs->flags, ConstInitializeFlags); |
michael@0 | 1124 | if (initArgs->LibraryParameters) { |
michael@0 | 1125 | printf("Params: %s\n",(char *)initArgs->LibraryParameters); |
michael@0 | 1126 | } |
michael@0 | 1127 | case ArgFunctionList: |
michael@0 | 1128 | functionList = (CK_FUNCTION_LIST *)ptr->data; |
michael@0 | 1129 | printf(" Version: %d.%02d\n", VERSION(functionList->version)); |
michael@0 | 1130 | #ifdef notdef |
michael@0 | 1131 | #undef CK_NEED_ARG_LIST |
michael@0 | 1132 | #define CK_PKCS11_FUNCTION_INFO(func) \ |
michael@0 | 1133 | printf(" %s: 0x%08lx\n", #func, (unsigned long) functionList->func ); |
michael@0 | 1134 | #include "pkcs11f.h" |
michael@0 | 1135 | #undef CK_NEED_ARG_LIST |
michael@0 | 1136 | #undef CK_PKCS11_FUNCTION_INFO |
michael@0 | 1137 | #endif |
michael@0 | 1138 | default: |
michael@0 | 1139 | ckrv = CKR_ARGUMENTS_BAD; |
michael@0 | 1140 | break; |
michael@0 | 1141 | } |
michael@0 | 1142 | |
michael@0 | 1143 | return ckrv; |
michael@0 | 1144 | } |
michael@0 | 1145 | |
michael@0 | 1146 | |
michael@0 | 1147 | /* |
michael@0 | 1148 | * Feeling ambitious? turn this whole thing into lexx yacc parser |
michael@0 | 1149 | * with full expressions. |
michael@0 | 1150 | */ |
michael@0 | 1151 | Value ** |
michael@0 | 1152 | parseArgs(int index, const char * bp) |
michael@0 | 1153 | { |
michael@0 | 1154 | const Commands *cp = &commands[index]; |
michael@0 | 1155 | int size = strlen(cp->fname); |
michael@0 | 1156 | int i; |
michael@0 | 1157 | CK_ULONG value; |
michael@0 | 1158 | char vname[512]; |
michael@0 | 1159 | Value **argList,*possible; |
michael@0 | 1160 | ConstType constType; |
michael@0 | 1161 | |
michael@0 | 1162 | /* |
michael@0 | 1163 | * skip pass the command |
michael@0 | 1164 | */ |
michael@0 | 1165 | if ((cp->fname[0] == 'C') && (cp->fname[1] == '_') && (bp[1] != '_')) { |
michael@0 | 1166 | size -= 2; |
michael@0 | 1167 | } |
michael@0 | 1168 | bp += size; |
michael@0 | 1169 | |
michael@0 | 1170 | /* |
michael@0 | 1171 | * Initialize our argument list |
michael@0 | 1172 | */ |
michael@0 | 1173 | argList = (Value **)malloc(sizeof(Value*)*MAX_ARGS); |
michael@0 | 1174 | for (i=0; i < MAX_ARGS; i++) { argList[i] = NULL; } |
michael@0 | 1175 | |
michael@0 | 1176 | /* |
michael@0 | 1177 | * Walk the argument list parsing it... |
michael@0 | 1178 | */ |
michael@0 | 1179 | for (i=0 ;i < MAX_ARGS; i++) { |
michael@0 | 1180 | ArgType type = cp->args[i] & ArgMask; |
michael@0 | 1181 | int error; |
michael@0 | 1182 | |
michael@0 | 1183 | /* strip blanks */ |
michael@0 | 1184 | bp = strip(bp); |
michael@0 | 1185 | |
michael@0 | 1186 | /* if we hit ArgNone, we've nabbed all the arguments we need */ |
michael@0 | 1187 | if (type == ArgNone) { |
michael@0 | 1188 | break; |
michael@0 | 1189 | } |
michael@0 | 1190 | |
michael@0 | 1191 | /* if we run out of space in the line, we weren't given enough |
michael@0 | 1192 | * arguments... */ |
michael@0 | 1193 | if (*bp == '\0') { |
michael@0 | 1194 | /* we're into optional arguments, ok to quit now */ |
michael@0 | 1195 | if (cp->args[i] & ArgOpt) { |
michael@0 | 1196 | break; |
michael@0 | 1197 | } |
michael@0 | 1198 | fprintf(stderr,"%s: only %d args found,\n",cp->fname,i); |
michael@0 | 1199 | parseFree(argList); |
michael@0 | 1200 | return NULL; |
michael@0 | 1201 | } |
michael@0 | 1202 | |
michael@0 | 1203 | /* collect all the rest of the command line and send |
michael@0 | 1204 | * it as a single argument */ |
michael@0 | 1205 | if (cp->args[i] & ArgFull) { |
michael@0 | 1206 | int size = strlen(bp)+1; |
michael@0 | 1207 | argList[i] = NewValue(type, size); |
michael@0 | 1208 | memcpy(argList[i]->data, bp, size); |
michael@0 | 1209 | break; |
michael@0 | 1210 | } |
michael@0 | 1211 | |
michael@0 | 1212 | /* |
michael@0 | 1213 | * look up the argument in our variable list first... only |
michael@0 | 1214 | * exception is the new argument type for set... |
michael@0 | 1215 | */ |
michael@0 | 1216 | error = 0; |
michael@0 | 1217 | if ((cp->args[i] != (ArgVar|ArgNew)) && |
michael@0 | 1218 | (possible = varLookup(bp,vname,sizeof(vname),&error))) { |
michael@0 | 1219 | /* ints are only compatible with other ints... all other types |
michael@0 | 1220 | * are interchangeable... */ |
michael@0 | 1221 | if (type != ArgVar) { /* ArgVar's match anyone */ |
michael@0 | 1222 | if ((type == ArgULong) ^ |
michael@0 | 1223 | ((possible->type & ArgMask) == ArgULong)) { |
michael@0 | 1224 | fprintf(stderr,"%s: Arg %d incompatible type with <%s>\n", |
michael@0 | 1225 | cp->fname,i+1,vname); |
michael@0 | 1226 | argFree(possible); |
michael@0 | 1227 | parseFree(argList); |
michael@0 | 1228 | return NULL; |
michael@0 | 1229 | } |
michael@0 | 1230 | /* |
michael@0 | 1231 | * ... that is as long as they are big enough... |
michael@0 | 1232 | */ |
michael@0 | 1233 | if (ArgSize(type) > possible->size) { |
michael@0 | 1234 | fprintf(stderr, |
michael@0 | 1235 | "%s: Arg %d %s is too small (%d bytes needs to be %d bytes)\n", |
michael@0 | 1236 | cp->fname,i+1,vname,possible->size,ArgSize(type)); |
michael@0 | 1237 | argFree(possible); |
michael@0 | 1238 | parseFree(argList); |
michael@0 | 1239 | return NULL; |
michael@0 | 1240 | } |
michael@0 | 1241 | } |
michael@0 | 1242 | |
michael@0 | 1243 | /* everything looks kosher here, use it */ |
michael@0 | 1244 | argList[i] = possible; |
michael@0 | 1245 | |
michael@0 | 1246 | bp = readChars(bp,vname,sizeof(vname)); |
michael@0 | 1247 | if (cp->args[i] & ArgOut) { |
michael@0 | 1248 | possible->type |= ArgOut; |
michael@0 | 1249 | } |
michael@0 | 1250 | continue; |
michael@0 | 1251 | } |
michael@0 | 1252 | |
michael@0 | 1253 | if (error == 1) { |
michael@0 | 1254 | parseFree(argList); |
michael@0 | 1255 | return NULL; |
michael@0 | 1256 | } |
michael@0 | 1257 | |
michael@0 | 1258 | /* create space for our argument */ |
michael@0 | 1259 | argList[i] = NewValue(type, 1); |
michael@0 | 1260 | |
michael@0 | 1261 | if ((PL_strncasecmp(bp, "null", 4) == 0) && ((bp[4] == 0) |
michael@0 | 1262 | || (bp[4] == ' ') || (bp[4] =='\t') || (bp[4] =='\n'))) { |
michael@0 | 1263 | if (cp->args[i] == ArgULong) { |
michael@0 | 1264 | fprintf(stderr, "%s: Arg %d CK_ULONG can't be NULL\n", |
michael@0 | 1265 | cp->fname,i+1); |
michael@0 | 1266 | parseFree(argList); |
michael@0 | 1267 | return NULL; |
michael@0 | 1268 | } |
michael@0 | 1269 | argFreeData(argList[i]); |
michael@0 | 1270 | argList[i]->data = NULL; |
michael@0 | 1271 | argList[i]->size = 0; |
michael@0 | 1272 | bp += 4; |
michael@0 | 1273 | if (*bp) bp++; |
michael@0 | 1274 | continue; |
michael@0 | 1275 | } |
michael@0 | 1276 | |
michael@0 | 1277 | /* if we're an output variable, we need to add it */ |
michael@0 | 1278 | if (cp->args[i] & ArgOut) { |
michael@0 | 1279 | if (PL_strncasecmp(bp,"file(",5) == 0 /* ) */ ) { |
michael@0 | 1280 | char filename[512]; |
michael@0 | 1281 | bp = readChars(bp+5,filename,sizeof(filename)); |
michael@0 | 1282 | size = PL_strlen(filename); |
michael@0 | 1283 | if ((size > 0) && (/* ( */filename[size-1] == ')')) { |
michael@0 | 1284 | filename[size-1] = 0; |
michael@0 | 1285 | } |
michael@0 | 1286 | filename[size] = 0; |
michael@0 | 1287 | argList[i]->filename = (char *)malloc(size+1); |
michael@0 | 1288 | |
michael@0 | 1289 | PL_strcpy(argList[i]->filename,filename); |
michael@0 | 1290 | |
michael@0 | 1291 | argList[i]->type |= ArgOut|ArgFile; |
michael@0 | 1292 | break; |
michael@0 | 1293 | } |
michael@0 | 1294 | bp = AddVariable(bp,&argList[i]); |
michael@0 | 1295 | argList[i]->type |= ArgOut; |
michael@0 | 1296 | continue; |
michael@0 | 1297 | } |
michael@0 | 1298 | |
michael@0 | 1299 | if (PL_strncasecmp(bp, "file(", 5) == 0 /* ) */ ) { |
michael@0 | 1300 | char filename[512]; |
michael@0 | 1301 | |
michael@0 | 1302 | bp = readChars(bp+5,filename,sizeof(filename)); |
michael@0 | 1303 | size = PL_strlen(filename); |
michael@0 | 1304 | if ((size > 0) && ( /* ( */ filename[size-1] == ')')) { |
michael@0 | 1305 | filename[size-1] = 0; |
michael@0 | 1306 | } |
michael@0 | 1307 | |
michael@0 | 1308 | if (restore(filename,argList[i]) != CKR_OK) { |
michael@0 | 1309 | parseFree(argList); |
michael@0 | 1310 | return NULL; |
michael@0 | 1311 | } |
michael@0 | 1312 | continue; |
michael@0 | 1313 | } |
michael@0 | 1314 | |
michael@0 | 1315 | switch (type) { |
michael@0 | 1316 | case ArgULong: |
michael@0 | 1317 | bp = constLookup(bp, &value, &constType); |
michael@0 | 1318 | *(int *)argList[i]->data = value; |
michael@0 | 1319 | argList[i]->constType = constType; |
michael@0 | 1320 | break; |
michael@0 | 1321 | case ArgVar: |
michael@0 | 1322 | argFreeData(argList[i]); |
michael@0 | 1323 | size = getEnd(bp)+1; |
michael@0 | 1324 | argList[i]->data = (void *)malloc(size); |
michael@0 | 1325 | argList[i]->size = size; |
michael@0 | 1326 | /* fall through */ |
michael@0 | 1327 | case ArgInfo: |
michael@0 | 1328 | case ArgSlotInfo: |
michael@0 | 1329 | case ArgTokenInfo: |
michael@0 | 1330 | case ArgSessionInfo: |
michael@0 | 1331 | case ArgAttribute: |
michael@0 | 1332 | case ArgMechanism: |
michael@0 | 1333 | case ArgMechanismInfo: |
michael@0 | 1334 | case ArgInitializeArgs: |
michael@0 | 1335 | case ArgUTF8: |
michael@0 | 1336 | case ArgChar: |
michael@0 | 1337 | bp = readChars(bp,(char *)argList[i]->data,argList[i]->size); |
michael@0 | 1338 | case ArgNone: |
michael@0 | 1339 | default: |
michael@0 | 1340 | break; |
michael@0 | 1341 | } |
michael@0 | 1342 | } |
michael@0 | 1343 | |
michael@0 | 1344 | return argList; |
michael@0 | 1345 | } |
michael@0 | 1346 | |
michael@0 | 1347 | /* lookup the command in the array */ |
michael@0 | 1348 | int |
michael@0 | 1349 | lookup(const char *buf) |
michael@0 | 1350 | { |
michael@0 | 1351 | int size,i; |
michael@0 | 1352 | int buflen; |
michael@0 | 1353 | |
michael@0 | 1354 | buflen = PL_strlen(buf); |
michael@0 | 1355 | |
michael@0 | 1356 | for ( i = 0; i < commandCount; i++) { |
michael@0 | 1357 | size = PL_strlen(commands[i].fname); |
michael@0 | 1358 | |
michael@0 | 1359 | if (size <= buflen) { |
michael@0 | 1360 | if (PL_strncasecmp(buf,commands[i].fname,size) == 0) { |
michael@0 | 1361 | return i; |
michael@0 | 1362 | } |
michael@0 | 1363 | } |
michael@0 | 1364 | if (size-2 <= buflen) { |
michael@0 | 1365 | if (commands[i].fname[0] == 'C' && commands[i].fname[1] == '_' && |
michael@0 | 1366 | (PL_strncasecmp(buf,&commands[i].fname[2],size-2) == 0)) { |
michael@0 | 1367 | return i; |
michael@0 | 1368 | } |
michael@0 | 1369 | } |
michael@0 | 1370 | } |
michael@0 | 1371 | fprintf(stderr,"Can't find command %s\n",buf); |
michael@0 | 1372 | return -1; |
michael@0 | 1373 | } |
michael@0 | 1374 | |
michael@0 | 1375 | void |
michael@0 | 1376 | putOutput(Value **ptr) |
michael@0 | 1377 | { |
michael@0 | 1378 | int i; |
michael@0 | 1379 | |
michael@0 | 1380 | for (i=0; i < MAX_ARGS; i++) { |
michael@0 | 1381 | ArgType type; |
michael@0 | 1382 | |
michael@0 | 1383 | if (ptr[i] == NULL) break; |
michael@0 | 1384 | |
michael@0 | 1385 | type = ptr[i]->type; |
michael@0 | 1386 | |
michael@0 | 1387 | ptr[i]->type &= ~ArgOut; |
michael@0 | 1388 | if (type == ArgNone) { |
michael@0 | 1389 | break; |
michael@0 | 1390 | } |
michael@0 | 1391 | if (type & ArgOut) { |
michael@0 | 1392 | (void) printArg(ptr[i],i+1); |
michael@0 | 1393 | } |
michael@0 | 1394 | if (type & ArgFile) { |
michael@0 | 1395 | save(ptr[i]->filename,ptr[i]); |
michael@0 | 1396 | free(ptr[i]->filename); |
michael@0 | 1397 | ptr[i]->filename= NULL; /* paranoia */ |
michael@0 | 1398 | } |
michael@0 | 1399 | } |
michael@0 | 1400 | } |
michael@0 | 1401 | |
michael@0 | 1402 | CK_RV |
michael@0 | 1403 | unloadModule(Module *module) |
michael@0 | 1404 | { |
michael@0 | 1405 | char *disableUnload = NULL; |
michael@0 | 1406 | |
michael@0 | 1407 | disableUnload = PR_GetEnv("NSS_DISABLE_UNLOAD"); |
michael@0 | 1408 | |
michael@0 | 1409 | if (module->library && !disableUnload) { |
michael@0 | 1410 | PR_UnloadLibrary(module->library); |
michael@0 | 1411 | } |
michael@0 | 1412 | |
michael@0 | 1413 | module->library = NULL; |
michael@0 | 1414 | module->functionList = NULL; |
michael@0 | 1415 | |
michael@0 | 1416 | return CKR_OK; |
michael@0 | 1417 | } |
michael@0 | 1418 | |
michael@0 | 1419 | CK_RV |
michael@0 | 1420 | loadModule(Module *module, char *library) |
michael@0 | 1421 | { |
michael@0 | 1422 | PRLibrary *newLibrary; |
michael@0 | 1423 | CK_C_GetFunctionList getFunctionList; |
michael@0 | 1424 | CK_FUNCTION_LIST *functionList; |
michael@0 | 1425 | CK_RV ckrv; |
michael@0 | 1426 | |
michael@0 | 1427 | newLibrary = PR_LoadLibrary(library); |
michael@0 | 1428 | if (!newLibrary) { |
michael@0 | 1429 | fprintf(stderr,"Couldn't load library %s\n",library); |
michael@0 | 1430 | return CKR_FUNCTION_FAILED; |
michael@0 | 1431 | } |
michael@0 | 1432 | getFunctionList = (CK_C_GetFunctionList) |
michael@0 | 1433 | PR_FindSymbol(newLibrary,"C_GetFunctionList"); |
michael@0 | 1434 | if (!getFunctionList) { |
michael@0 | 1435 | fprintf(stderr,"Couldn't find \"C_GetFunctionList\" in %s\n",library); |
michael@0 | 1436 | return CKR_FUNCTION_FAILED; |
michael@0 | 1437 | } |
michael@0 | 1438 | |
michael@0 | 1439 | ckrv = (*getFunctionList)(&functionList); |
michael@0 | 1440 | if (ckrv != CKR_OK) { |
michael@0 | 1441 | return ckrv; |
michael@0 | 1442 | } |
michael@0 | 1443 | |
michael@0 | 1444 | if (module->library) { |
michael@0 | 1445 | PR_UnloadLibrary(module->library); |
michael@0 | 1446 | } |
michael@0 | 1447 | |
michael@0 | 1448 | module->library = newLibrary; |
michael@0 | 1449 | module->functionList = functionList; |
michael@0 | 1450 | |
michael@0 | 1451 | return CKR_OK; |
michael@0 | 1452 | } |
michael@0 | 1453 | |
michael@0 | 1454 | static void |
michael@0 | 1455 | printHelp(int index, int full) |
michael@0 | 1456 | { |
michael@0 | 1457 | int j; |
michael@0 | 1458 | printf(" %s", commands[index].fname); |
michael@0 | 1459 | for (j=0; j < MAX_ARGS; j++) { |
michael@0 | 1460 | ArgType type = commands[index].args[j] & ArgMask; |
michael@0 | 1461 | if (type == ArgNone) { |
michael@0 | 1462 | break; |
michael@0 | 1463 | } |
michael@0 | 1464 | printf(" %s", valueString[type]); |
michael@0 | 1465 | } |
michael@0 | 1466 | printf("\n"); |
michael@0 | 1467 | printf(" %s\n",commands[index].helpString); |
michael@0 | 1468 | } |
michael@0 | 1469 | |
michael@0 | 1470 | /* add Topical help here ! */ |
michael@0 | 1471 | static CK_RV |
michael@0 | 1472 | printTopicHelp(char *topic) |
michael@0 | 1473 | { |
michael@0 | 1474 | int size,i; |
michael@0 | 1475 | int topicLen; |
michael@0 | 1476 | |
michael@0 | 1477 | topicLen = PL_strlen(topic); |
michael@0 | 1478 | |
michael@0 | 1479 | for ( i = 0; i < topicCount; i++) { |
michael@0 | 1480 | size = PL_strlen(topics[i].name); |
michael@0 | 1481 | |
michael@0 | 1482 | if (size <= topicLen) { |
michael@0 | 1483 | if (PL_strncasecmp(topic,topics[i].name,size) == 0) { |
michael@0 | 1484 | break; |
michael@0 | 1485 | } |
michael@0 | 1486 | } |
michael@0 | 1487 | } |
michael@0 | 1488 | |
michael@0 | 1489 | if (i == topicCount) { |
michael@0 | 1490 | fprintf(stderr,"Can't find topic '%s'\n", topic); |
michael@0 | 1491 | return CKR_DATA_INVALID; |
michael@0 | 1492 | } |
michael@0 | 1493 | |
michael@0 | 1494 | printf(" %s", topic); |
michael@0 | 1495 | printf("\n"); |
michael@0 | 1496 | printf(" %s\n",topics[i].helpString); |
michael@0 | 1497 | return CKR_OK; |
michael@0 | 1498 | } |
michael@0 | 1499 | |
michael@0 | 1500 | static CK_RV |
michael@0 | 1501 | printGeneralHelp(void) |
michael@0 | 1502 | { |
michael@0 | 1503 | int i; |
michael@0 | 1504 | printf(" To get help on commands, select from the list below:"); |
michael@0 | 1505 | for ( i = 0; i < commandCount; i++) { |
michael@0 | 1506 | if (i % 5 == 0) printf("\n"); |
michael@0 | 1507 | printf("%s,", commands[i].fname); |
michael@0 | 1508 | } |
michael@0 | 1509 | printf("\n"); |
michael@0 | 1510 | /* print help topics */ |
michael@0 | 1511 | printf(" To get help on a topic, select from the list below:"); |
michael@0 | 1512 | for ( i = 0; i < topicCount; i++) { |
michael@0 | 1513 | if (i % 5 == 0) printf("\n"); |
michael@0 | 1514 | printf("%s,", topics[i].name); |
michael@0 | 1515 | } |
michael@0 | 1516 | printf("\n"); |
michael@0 | 1517 | return CKR_OK; |
michael@0 | 1518 | } |
michael@0 | 1519 | |
michael@0 | 1520 | static CK_RV |
michael@0 | 1521 | quitIf(CK_ULONG a, const char *cmp, CK_ULONG b) |
michael@0 | 1522 | { |
michael@0 | 1523 | if (strcmp(cmp, "<") == 0) { |
michael@0 | 1524 | return (a < b) ? CKR_QUIT : CKR_OK; |
michael@0 | 1525 | } else if (strcmp(cmp, ">") == 0) { |
michael@0 | 1526 | return (a > b) ? CKR_QUIT : CKR_OK; |
michael@0 | 1527 | } else if (strcmp(cmp, "<=") == 0) { |
michael@0 | 1528 | return (a <= b) ? CKR_QUIT : CKR_OK; |
michael@0 | 1529 | } else if (strcmp(cmp, ">=") == 0) { |
michael@0 | 1530 | return (a >= b) ? CKR_QUIT : CKR_OK; |
michael@0 | 1531 | } else if (strcmp(cmp, "=") == 0) { |
michael@0 | 1532 | return (a == b) ? CKR_QUIT : CKR_OK; |
michael@0 | 1533 | } else if (strcmp(cmp, "!=") == 0) { |
michael@0 | 1534 | return (a != b) ? CKR_QUIT : CKR_OK; |
michael@0 | 1535 | } |
michael@0 | 1536 | printf("Unkown integer comparator: '%s'\n", cmp); |
michael@0 | 1537 | return CKR_ARGUMENTS_BAD; |
michael@0 | 1538 | } |
michael@0 | 1539 | |
michael@0 | 1540 | static CK_RV |
michael@0 | 1541 | quitIfString(const char *a, const char *cmp, const char *b) |
michael@0 | 1542 | { |
michael@0 | 1543 | |
michael@0 | 1544 | if (strcmp(cmp, "=") == 0) { |
michael@0 | 1545 | return (strcmp(a,b) == 0) ? CKR_QUIT : CKR_OK; |
michael@0 | 1546 | } else if (strcmp(cmp, "!=") == 0) { |
michael@0 | 1547 | return (strcmp(a,b) != 0) ? CKR_QUIT : CKR_OK; |
michael@0 | 1548 | } |
michael@0 | 1549 | printf("Unkown string comparator: '%s'\n", cmp); |
michael@0 | 1550 | return CKR_ARGUMENTS_BAD; |
michael@0 | 1551 | } |
michael@0 | 1552 | |
michael@0 | 1553 | CK_RV run(const char *); |
michael@0 | 1554 | CK_RV timeCommand(const char *); |
michael@0 | 1555 | CK_RV loop(const char *filename, const char *var, |
michael@0 | 1556 | CK_ULONG start, CK_ULONG end, CK_ULONG step) ; |
michael@0 | 1557 | |
michael@0 | 1558 | /* |
michael@0 | 1559 | * Actually dispatch the function... Bad things happen |
michael@0 | 1560 | * if these don't match the commands array. |
michael@0 | 1561 | */ |
michael@0 | 1562 | CK_RV |
michael@0 | 1563 | do_func(int index, Value **a) |
michael@0 | 1564 | { |
michael@0 | 1565 | int value, helpIndex; |
michael@0 | 1566 | static Module module = { NULL, NULL} ; |
michael@0 | 1567 | CK_FUNCTION_LIST *func = module.functionList; |
michael@0 | 1568 | |
michael@0 | 1569 | switch (commands[index].fType) { |
michael@0 | 1570 | case F_C_Initialize: |
michael@0 | 1571 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1572 | return func->C_Initialize((void *)a[0]->data); |
michael@0 | 1573 | case F_C_Finalize: |
michael@0 | 1574 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1575 | return func->C_Finalize((void *)a[0]->data); |
michael@0 | 1576 | case F_C_GetInfo: |
michael@0 | 1577 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1578 | return func->C_GetInfo((CK_INFO *)a[0]->data); |
michael@0 | 1579 | case F_C_GetFunctionList: |
michael@0 | 1580 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1581 | return func->C_GetFunctionList((CK_FUNCTION_LIST **)a[0]->data); |
michael@0 | 1582 | case F_C_GetSlotList: |
michael@0 | 1583 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1584 | return func->C_GetSlotList((CK_BBOOL)*(CK_ULONG *)a[0]->data, |
michael@0 | 1585 | (CK_SLOT_ID *)a[1]->data, |
michael@0 | 1586 | (CK_ULONG *)a[2]->data); |
michael@0 | 1587 | case F_C_GetSlotInfo: |
michael@0 | 1588 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1589 | return func->C_GetSlotInfo(*(CK_ULONG *)a[0]->data, |
michael@0 | 1590 | (CK_SLOT_INFO *)a[1]->data); |
michael@0 | 1591 | case F_C_GetTokenInfo: |
michael@0 | 1592 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1593 | return func->C_GetTokenInfo(*(CK_ULONG *)a[0]->data, |
michael@0 | 1594 | (CK_TOKEN_INFO *)a[1]->data); |
michael@0 | 1595 | case F_C_GetMechanismList: |
michael@0 | 1596 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1597 | if (a[1]->data) { |
michael@0 | 1598 | a[1]->constType = ConstMechanism; |
michael@0 | 1599 | } |
michael@0 | 1600 | return func->C_GetMechanismList(*(CK_ULONG *)a[0]->data, |
michael@0 | 1601 | (CK_MECHANISM_TYPE*)a[1]->data, |
michael@0 | 1602 | (CK_ULONG *)a[2]->data); |
michael@0 | 1603 | case F_C_GetMechanismInfo: |
michael@0 | 1604 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1605 | return func->C_GetMechanismInfo(*(CK_ULONG *)a[0]->data, |
michael@0 | 1606 | *(CK_ULONG *)a[1]->data, |
michael@0 | 1607 | (CK_MECHANISM_INFO *)a[2]->data); |
michael@0 | 1608 | case F_C_InitToken: |
michael@0 | 1609 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1610 | return func->C_InitToken(*(CK_ULONG *)a[0]->data, |
michael@0 | 1611 | (CK_CHAR *)a[1]->data, |
michael@0 | 1612 | *(CK_ULONG *)a[2]->data, |
michael@0 | 1613 | (CK_CHAR *)a[3]->data); |
michael@0 | 1614 | case F_C_InitPIN: |
michael@0 | 1615 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1616 | return func->C_InitPIN(*(CK_ULONG *)a[0]->data, |
michael@0 | 1617 | (CK_CHAR *)a[1]->data, |
michael@0 | 1618 | *(CK_ULONG *)a[2]->data); |
michael@0 | 1619 | case F_C_SetPIN: |
michael@0 | 1620 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1621 | return func->C_SetPIN(*(CK_ULONG *)a[0]->data, |
michael@0 | 1622 | (CK_CHAR *)a[1]->data, |
michael@0 | 1623 | *(CK_ULONG *)a[2]->data, |
michael@0 | 1624 | (CK_CHAR *)a[3]->data, |
michael@0 | 1625 | *(CK_ULONG *)a[4]->data); |
michael@0 | 1626 | case F_C_OpenSession: |
michael@0 | 1627 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1628 | return func->C_OpenSession(*(CK_ULONG *)a[0]->data, |
michael@0 | 1629 | *(CK_ULONG *)a[1]->data, |
michael@0 | 1630 | (void *)NULL, |
michael@0 | 1631 | (CK_NOTIFY) NULL, |
michael@0 | 1632 | (CK_ULONG *)a[2]->data); |
michael@0 | 1633 | case F_C_CloseSession: |
michael@0 | 1634 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1635 | return func->C_CloseSession(*(CK_ULONG *)a[0]->data); |
michael@0 | 1636 | case F_C_CloseAllSessions: |
michael@0 | 1637 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1638 | return func->C_CloseAllSessions(*(CK_ULONG *)a[0]->data); |
michael@0 | 1639 | case F_C_GetSessionInfo: |
michael@0 | 1640 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1641 | return func->C_GetSessionInfo(*(CK_ULONG *)a[0]->data, |
michael@0 | 1642 | (CK_SESSION_INFO *)a[1]->data); |
michael@0 | 1643 | case F_C_GetOperationState: |
michael@0 | 1644 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1645 | return func->C_GetOperationState(*(CK_ULONG *)a[0]->data, |
michael@0 | 1646 | (CK_BYTE *)a[1]->data, |
michael@0 | 1647 | (CK_ULONG *)a[2]->data); |
michael@0 | 1648 | case F_C_SetOperationState: |
michael@0 | 1649 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1650 | return func->C_SetOperationState(*(CK_ULONG *)a[0]->data, |
michael@0 | 1651 | (CK_CHAR *)a[1]->data, |
michael@0 | 1652 | *(CK_ULONG *)a[2]->data, |
michael@0 | 1653 | *(CK_ULONG *)a[3]->data, |
michael@0 | 1654 | *(CK_ULONG *)a[4]->data); |
michael@0 | 1655 | case F_C_Login: |
michael@0 | 1656 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1657 | return func->C_Login(*(CK_ULONG *)a[0]->data, |
michael@0 | 1658 | *(CK_ULONG *)a[1]->data, |
michael@0 | 1659 | (CK_CHAR *)a[2]->data, |
michael@0 | 1660 | *(CK_ULONG *)a[3]->data); |
michael@0 | 1661 | case F_C_Logout: |
michael@0 | 1662 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1663 | return func->C_Logout(*(CK_ULONG *)a[0]->data); |
michael@0 | 1664 | case F_C_CreateObject: |
michael@0 | 1665 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1666 | return func->C_CreateObject(*(CK_ULONG *)a[0]->data, |
michael@0 | 1667 | (CK_ATTRIBUTE *)a[1]->data, |
michael@0 | 1668 | *(CK_ULONG *)a[2]->data, |
michael@0 | 1669 | (CK_ULONG *)a[3]->data); |
michael@0 | 1670 | case F_C_CopyObject: |
michael@0 | 1671 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1672 | return func->C_CopyObject(*(CK_ULONG *)a[0]->data, |
michael@0 | 1673 | *(CK_ULONG *)a[0]->data, |
michael@0 | 1674 | (CK_ATTRIBUTE *)a[1]->data, |
michael@0 | 1675 | *(CK_ULONG *)a[2]->data, |
michael@0 | 1676 | (CK_ULONG *)a[3]->data); |
michael@0 | 1677 | case F_C_DestroyObject: |
michael@0 | 1678 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1679 | return func->C_DestroyObject(*(CK_ULONG *)a[0]->data, |
michael@0 | 1680 | *(CK_ULONG *)a[1]->data); |
michael@0 | 1681 | case F_C_GetObjectSize: |
michael@0 | 1682 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1683 | return func->C_GetObjectSize(*(CK_ULONG *)a[0]->data, |
michael@0 | 1684 | *(CK_ULONG *)a[1]->data, |
michael@0 | 1685 | (CK_ULONG *)a[2]->data); |
michael@0 | 1686 | case F_C_GetAttributeValue: |
michael@0 | 1687 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1688 | return func->C_GetAttributeValue(*(CK_ULONG *)a[0]->data, |
michael@0 | 1689 | *(CK_ULONG *)a[1]->data, |
michael@0 | 1690 | (CK_ATTRIBUTE *)a[2]->data, |
michael@0 | 1691 | *(CK_ULONG *)a[3]->data); |
michael@0 | 1692 | case F_C_SetAttributeValue: |
michael@0 | 1693 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1694 | return func->C_SetAttributeValue(*(CK_ULONG *)a[0]->data, |
michael@0 | 1695 | *(CK_ULONG *)a[1]->data, |
michael@0 | 1696 | (CK_ATTRIBUTE *)a[2]->data, |
michael@0 | 1697 | *(CK_ULONG *)a[3]->data); |
michael@0 | 1698 | case F_C_FindObjectsInit: |
michael@0 | 1699 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1700 | return func->C_FindObjectsInit(*(CK_ULONG *)a[0]->data, |
michael@0 | 1701 | (CK_ATTRIBUTE *)a[1]->data, |
michael@0 | 1702 | *(CK_ULONG *)a[2]->data); |
michael@0 | 1703 | case F_C_FindObjects: |
michael@0 | 1704 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1705 | return func->C_FindObjects(*(CK_ULONG *)a[0]->data, |
michael@0 | 1706 | (CK_ULONG *)a[1]->data, |
michael@0 | 1707 | *(CK_ULONG *)a[2]->data, |
michael@0 | 1708 | (CK_ULONG *)a[3]->data); |
michael@0 | 1709 | case F_C_FindObjectsFinal: |
michael@0 | 1710 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1711 | return func->C_FindObjectsFinal(*(CK_ULONG *)a[0]->data); |
michael@0 | 1712 | case F_C_EncryptInit: |
michael@0 | 1713 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1714 | return func->C_EncryptInit(*(CK_ULONG *)a[0]->data, |
michael@0 | 1715 | (CK_MECHANISM *)a[1]->data, |
michael@0 | 1716 | *(CK_ULONG *)a[2]->data); |
michael@0 | 1717 | case F_C_Encrypt: |
michael@0 | 1718 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1719 | return func->C_Encrypt(*(CK_ULONG *)a[0]->data, |
michael@0 | 1720 | (CK_CHAR *)a[1]->data, |
michael@0 | 1721 | *(CK_ULONG *)a[2]->data, |
michael@0 | 1722 | (CK_CHAR *)a[3]->data, |
michael@0 | 1723 | (CK_ULONG *)a[4]->data); |
michael@0 | 1724 | case F_C_EncryptUpdate: |
michael@0 | 1725 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1726 | return func->C_EncryptUpdate(*(CK_ULONG *)a[0]->data, |
michael@0 | 1727 | (CK_CHAR *)a[1]->data, |
michael@0 | 1728 | *(CK_ULONG *)a[2]->data, |
michael@0 | 1729 | (CK_CHAR *)a[3]->data, |
michael@0 | 1730 | (CK_ULONG *)a[4]->data); |
michael@0 | 1731 | case F_C_EncryptFinal: |
michael@0 | 1732 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1733 | return func->C_EncryptFinal(*(CK_ULONG *)a[0]->data, |
michael@0 | 1734 | (CK_CHAR *)a[1]->data, |
michael@0 | 1735 | (CK_ULONG *)a[2]->data); |
michael@0 | 1736 | case F_C_DecryptInit: |
michael@0 | 1737 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1738 | return func->C_DecryptInit(*(CK_ULONG *)a[0]->data, |
michael@0 | 1739 | (CK_MECHANISM *)a[1]->data, |
michael@0 | 1740 | *(CK_ULONG *)a[2]->data); |
michael@0 | 1741 | case F_C_Decrypt: |
michael@0 | 1742 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1743 | return func->C_Decrypt(*(CK_ULONG *)a[0]->data, |
michael@0 | 1744 | (CK_CHAR *)a[1]->data, |
michael@0 | 1745 | *(CK_ULONG *)a[2]->data, |
michael@0 | 1746 | (CK_CHAR *)a[3]->data, |
michael@0 | 1747 | (CK_ULONG *)a[4]->data); |
michael@0 | 1748 | case F_C_DecryptUpdate: |
michael@0 | 1749 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1750 | return func->C_DecryptUpdate(*(CK_ULONG *)a[0]->data, |
michael@0 | 1751 | (CK_CHAR *)a[1]->data, |
michael@0 | 1752 | *(CK_ULONG *)a[2]->data, |
michael@0 | 1753 | (CK_CHAR *)a[3]->data, |
michael@0 | 1754 | (CK_ULONG *)a[4]->data); |
michael@0 | 1755 | case F_C_DecryptFinal: |
michael@0 | 1756 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1757 | return func->C_DecryptFinal(*(CK_ULONG *)a[0]->data, |
michael@0 | 1758 | (CK_CHAR *)a[1]->data, |
michael@0 | 1759 | (CK_ULONG *)a[2]->data); |
michael@0 | 1760 | case F_C_DigestInit: |
michael@0 | 1761 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1762 | return func->C_DigestInit(*(CK_ULONG *)a[0]->data, |
michael@0 | 1763 | (CK_MECHANISM *)a[1]->data); |
michael@0 | 1764 | case F_C_Digest: |
michael@0 | 1765 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1766 | return func->C_Digest(*(CK_ULONG *)a[0]->data, |
michael@0 | 1767 | (CK_CHAR *)a[1]->data, |
michael@0 | 1768 | *(CK_ULONG *)a[2]->data, |
michael@0 | 1769 | (CK_CHAR *)a[3]->data, |
michael@0 | 1770 | (CK_ULONG *)a[4]->data); |
michael@0 | 1771 | case F_C_DigestUpdate: |
michael@0 | 1772 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1773 | return func->C_DigestUpdate(*(CK_ULONG *)a[0]->data, |
michael@0 | 1774 | (CK_CHAR *)a[1]->data, |
michael@0 | 1775 | *(CK_ULONG *)a[2]->data); |
michael@0 | 1776 | case F_C_DigestKey: |
michael@0 | 1777 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1778 | return func->C_DigestKey(*(CK_ULONG *)a[0]->data, |
michael@0 | 1779 | *(CK_ULONG *)a[1]->data); |
michael@0 | 1780 | case F_C_DigestFinal: |
michael@0 | 1781 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1782 | return func->C_DigestFinal(*(CK_ULONG *)a[0]->data, |
michael@0 | 1783 | (CK_CHAR *)a[1]->data, |
michael@0 | 1784 | (CK_ULONG *)a[2]->data); |
michael@0 | 1785 | case F_C_SignInit: |
michael@0 | 1786 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1787 | return func->C_SignInit(*(CK_ULONG *)a[0]->data, |
michael@0 | 1788 | (CK_MECHANISM *)a[1]->data, |
michael@0 | 1789 | *(CK_ULONG *)a[2]->data); |
michael@0 | 1790 | case F_C_Sign: |
michael@0 | 1791 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1792 | return func->C_Sign(*(CK_ULONG *)a[0]->data, |
michael@0 | 1793 | (CK_CHAR *)a[1]->data, |
michael@0 | 1794 | *(CK_ULONG *)a[2]->data, |
michael@0 | 1795 | (CK_CHAR *)a[3]->data, |
michael@0 | 1796 | (CK_ULONG *)a[4]->data); |
michael@0 | 1797 | case F_C_SignUpdate: |
michael@0 | 1798 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1799 | return func->C_SignUpdate(*(CK_ULONG *)a[0]->data, |
michael@0 | 1800 | (CK_CHAR *)a[1]->data, |
michael@0 | 1801 | *(CK_ULONG *)a[2]->data); |
michael@0 | 1802 | case F_C_SignFinal: |
michael@0 | 1803 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1804 | return func->C_SignFinal(*(CK_ULONG *)a[0]->data, |
michael@0 | 1805 | (CK_CHAR *)a[1]->data, |
michael@0 | 1806 | (CK_ULONG *)a[2]->data); |
michael@0 | 1807 | |
michael@0 | 1808 | case F_C_SignRecoverInit: |
michael@0 | 1809 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1810 | return func->C_SignRecoverInit(*(CK_ULONG *)a[0]->data, |
michael@0 | 1811 | (CK_MECHANISM *)a[1]->data, |
michael@0 | 1812 | *(CK_ULONG *)a[2]->data); |
michael@0 | 1813 | case F_C_SignRecover: |
michael@0 | 1814 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1815 | return func->C_SignRecover(*(CK_ULONG *)a[0]->data, |
michael@0 | 1816 | (CK_CHAR *)a[1]->data, |
michael@0 | 1817 | *(CK_ULONG *)a[2]->data, |
michael@0 | 1818 | (CK_CHAR *)a[3]->data, |
michael@0 | 1819 | (CK_ULONG *)a[4]->data); |
michael@0 | 1820 | case F_C_VerifyInit: |
michael@0 | 1821 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1822 | return func->C_VerifyInit(*(CK_ULONG *)a[0]->data, |
michael@0 | 1823 | (CK_MECHANISM *)a[1]->data, |
michael@0 | 1824 | *(CK_ULONG *)a[2]->data); |
michael@0 | 1825 | case F_C_Verify: |
michael@0 | 1826 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1827 | return func->C_Verify(*(CK_ULONG *)a[0]->data, |
michael@0 | 1828 | (CK_CHAR *)a[1]->data, |
michael@0 | 1829 | *(CK_ULONG *)a[2]->data, |
michael@0 | 1830 | (CK_CHAR *)a[3]->data, |
michael@0 | 1831 | *(CK_ULONG *)a[4]->data); |
michael@0 | 1832 | case F_C_VerifyUpdate: |
michael@0 | 1833 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1834 | return func->C_VerifyUpdate(*(CK_ULONG *)a[0]->data, |
michael@0 | 1835 | (CK_CHAR *)a[1]->data, |
michael@0 | 1836 | *(CK_ULONG *)a[2]->data); |
michael@0 | 1837 | case F_C_VerifyFinal: |
michael@0 | 1838 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1839 | return func->C_VerifyFinal(*(CK_ULONG *)a[0]->data, |
michael@0 | 1840 | (CK_CHAR *)a[1]->data, |
michael@0 | 1841 | *(CK_ULONG *)a[2]->data); |
michael@0 | 1842 | |
michael@0 | 1843 | case F_C_VerifyRecoverInit: |
michael@0 | 1844 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1845 | return func->C_VerifyRecoverInit(*(CK_ULONG *)a[0]->data, |
michael@0 | 1846 | (CK_MECHANISM *)a[1]->data, |
michael@0 | 1847 | *(CK_ULONG *)a[2]->data); |
michael@0 | 1848 | case F_C_VerifyRecover: |
michael@0 | 1849 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1850 | return func->C_VerifyRecover(*(CK_ULONG *)a[0]->data, |
michael@0 | 1851 | (CK_CHAR *)a[1]->data, |
michael@0 | 1852 | *(CK_ULONG *)a[2]->data, |
michael@0 | 1853 | (CK_CHAR *)a[3]->data, |
michael@0 | 1854 | (CK_ULONG *)a[4]->data); |
michael@0 | 1855 | case F_C_DigestEncryptUpdate: |
michael@0 | 1856 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1857 | return func->C_DigestEncryptUpdate(*(CK_ULONG *)a[0]->data, |
michael@0 | 1858 | (CK_CHAR *)a[1]->data, |
michael@0 | 1859 | *(CK_ULONG *)a[2]->data, |
michael@0 | 1860 | (CK_CHAR *)a[3]->data, |
michael@0 | 1861 | (CK_ULONG *)a[4]->data); |
michael@0 | 1862 | case F_C_DecryptDigestUpdate: |
michael@0 | 1863 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1864 | return func->C_DecryptDigestUpdate(*(CK_ULONG *)a[0]->data, |
michael@0 | 1865 | (CK_CHAR *)a[1]->data, |
michael@0 | 1866 | *(CK_ULONG *)a[2]->data, |
michael@0 | 1867 | (CK_CHAR *)a[3]->data, |
michael@0 | 1868 | (CK_ULONG *)a[4]->data); |
michael@0 | 1869 | case F_C_SignEncryptUpdate: |
michael@0 | 1870 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1871 | return func->C_SignEncryptUpdate(*(CK_ULONG *)a[0]->data, |
michael@0 | 1872 | (CK_CHAR *)a[1]->data, |
michael@0 | 1873 | *(CK_ULONG *)a[2]->data, |
michael@0 | 1874 | (CK_CHAR *)a[3]->data, |
michael@0 | 1875 | (CK_ULONG *)a[4]->data); |
michael@0 | 1876 | case F_C_DecryptVerifyUpdate: |
michael@0 | 1877 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1878 | return func->C_DecryptVerifyUpdate(*(CK_ULONG *)a[0]->data, |
michael@0 | 1879 | (CK_CHAR *)a[1]->data, |
michael@0 | 1880 | *(CK_ULONG *)a[2]->data, |
michael@0 | 1881 | (CK_CHAR *)a[3]->data, |
michael@0 | 1882 | (CK_ULONG *)a[4]->data); |
michael@0 | 1883 | case F_C_GenerateKey: |
michael@0 | 1884 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1885 | return func->C_GenerateKey(*(CK_ULONG *)a[0]->data, |
michael@0 | 1886 | (CK_MECHANISM *)a[1]->data, |
michael@0 | 1887 | (CK_ATTRIBUTE *)a[2]->data, |
michael@0 | 1888 | *(CK_ULONG *)a[3]->data, |
michael@0 | 1889 | (CK_ULONG *)a[4]->data); |
michael@0 | 1890 | case F_C_GenerateKeyPair: |
michael@0 | 1891 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1892 | return func->C_GenerateKeyPair(*(CK_ULONG *)a[0]->data, |
michael@0 | 1893 | (CK_MECHANISM *)a[1]->data, |
michael@0 | 1894 | (CK_ATTRIBUTE *)a[2]->data, |
michael@0 | 1895 | *(CK_ULONG *)a[3]->data, |
michael@0 | 1896 | (CK_ATTRIBUTE *)a[4]->data, |
michael@0 | 1897 | *(CK_ULONG *)a[5]->data, |
michael@0 | 1898 | (CK_ULONG *)a[6]->data, |
michael@0 | 1899 | (CK_ULONG *)a[7]->data); |
michael@0 | 1900 | case F_C_WrapKey: |
michael@0 | 1901 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1902 | return func->C_WrapKey(*(CK_ULONG *)a[0]->data, |
michael@0 | 1903 | (CK_MECHANISM *)a[1]->data, |
michael@0 | 1904 | *(CK_ULONG *)a[2]->data, |
michael@0 | 1905 | *(CK_ULONG *)a[3]->data, |
michael@0 | 1906 | (CK_CHAR *)a[5]->data, |
michael@0 | 1907 | (CK_ULONG *)a[6]->data); |
michael@0 | 1908 | case F_C_UnwrapKey: |
michael@0 | 1909 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1910 | return func->C_UnwrapKey(*(CK_ULONG *)a[0]->data, |
michael@0 | 1911 | (CK_MECHANISM *)a[1]->data, |
michael@0 | 1912 | *(CK_ULONG *)a[2]->data, |
michael@0 | 1913 | (CK_CHAR *)a[3]->data, |
michael@0 | 1914 | *(CK_ULONG *)a[4]->data, |
michael@0 | 1915 | (CK_ATTRIBUTE *)a[5]->data, |
michael@0 | 1916 | *(CK_ULONG *)a[6]->data, |
michael@0 | 1917 | (CK_ULONG *)a[7]->data); |
michael@0 | 1918 | case F_C_DeriveKey: |
michael@0 | 1919 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1920 | return func->C_DeriveKey (*(CK_ULONG *)a[0]->data, |
michael@0 | 1921 | (CK_MECHANISM *)a[1]->data, |
michael@0 | 1922 | *(CK_ULONG *)a[2]->data, |
michael@0 | 1923 | (CK_ATTRIBUTE *)a[3]->data, |
michael@0 | 1924 | *(CK_ULONG *)a[4]->data, |
michael@0 | 1925 | (CK_ULONG *)a[5]->data); |
michael@0 | 1926 | case F_C_SeedRandom: |
michael@0 | 1927 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1928 | return func->C_SeedRandom(*(CK_ULONG *)a[0]->data, |
michael@0 | 1929 | (CK_CHAR *)a[1]->data, |
michael@0 | 1930 | *(CK_ULONG *)a[2]->data); |
michael@0 | 1931 | case F_C_GenerateRandom: |
michael@0 | 1932 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1933 | return func->C_GenerateRandom(*(CK_ULONG *)a[0]->data, |
michael@0 | 1934 | (CK_CHAR *)a[1]->data, |
michael@0 | 1935 | *(CK_ULONG *)a[2]->data); |
michael@0 | 1936 | case F_C_GetFunctionStatus: |
michael@0 | 1937 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1938 | return func->C_GetFunctionStatus(*(CK_ULONG *)a[0]->data); |
michael@0 | 1939 | case F_C_CancelFunction: |
michael@0 | 1940 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1941 | return func->C_CancelFunction(*(CK_ULONG *)a[0]->data); |
michael@0 | 1942 | case F_C_WaitForSlotEvent: |
michael@0 | 1943 | if (!func) return CKR_CRYPTOKI_NOT_INITIALIZED; |
michael@0 | 1944 | return func->C_WaitForSlotEvent(*(CK_ULONG *)a[0]->data, |
michael@0 | 1945 | (CK_ULONG *)a[1]->data, |
michael@0 | 1946 | (void *)a[2]->data); |
michael@0 | 1947 | /* set a variable */ |
michael@0 | 1948 | case F_SetVar: |
michael@0 | 1949 | case F_SetStringVar: |
michael@0 | 1950 | (void) DeleteVariable(a[0]->data); |
michael@0 | 1951 | (void) AddVariable(a[0]->data,&a[1]); |
michael@0 | 1952 | return CKR_OK; |
michael@0 | 1953 | /* print a value */ |
michael@0 | 1954 | case F_Print: |
michael@0 | 1955 | return printArg(a[0],0); |
michael@0 | 1956 | case F_SaveVar: |
michael@0 | 1957 | return save(a[0]->data,a[1]); |
michael@0 | 1958 | case F_RestoreVar: |
michael@0 | 1959 | return restore(a[0]->data,a[1]); |
michael@0 | 1960 | case F_Delete: |
michael@0 | 1961 | return DeleteVariable(a[0]->data); |
michael@0 | 1962 | case F_Increment: |
michael@0 | 1963 | return increment(a[0], *(CK_ULONG *)a[1]->data); |
michael@0 | 1964 | case F_Decrement: |
michael@0 | 1965 | return decrement(a[0], *(CK_ULONG *)a[1]->data); |
michael@0 | 1966 | case F_List: |
michael@0 | 1967 | return list(); |
michael@0 | 1968 | case F_Run: |
michael@0 | 1969 | return run(a[0]->data); |
michael@0 | 1970 | case F_Time: |
michael@0 | 1971 | return timeCommand(a[0]->data); |
michael@0 | 1972 | case F_Load: |
michael@0 | 1973 | return loadModule(&module,a[0]->data); |
michael@0 | 1974 | case F_Unload: |
michael@0 | 1975 | return unloadModule(&module); |
michael@0 | 1976 | case F_NewArray: |
michael@0 | 1977 | (void) DeleteVariable(a[0]->data); |
michael@0 | 1978 | return ArrayVariable(a[0]->data,a[1]->data,*(CK_ULONG *)a[2]->data); |
michael@0 | 1979 | case F_NewTemplate: |
michael@0 | 1980 | (void) DeleteVariable(a[0]->data); |
michael@0 | 1981 | return ArrayTemplate(a[0]->data,a[1]->data); |
michael@0 | 1982 | case F_BuildTemplate: |
michael@0 | 1983 | return BuildTemplate(a[0]); |
michael@0 | 1984 | case F_SetTemplate: |
michael@0 | 1985 | return SetTemplate(a[0], |
michael@0 | 1986 | *(CK_ULONG *)a[1]->data, |
michael@0 | 1987 | *(CK_ULONG *)a[2]->data); |
michael@0 | 1988 | case F_NewMechanism: |
michael@0 | 1989 | (void) DeleteVariable(a[0]->data); |
michael@0 | 1990 | return NewMechanism(a[0]->data,*(CK_ULONG *)a[1]->data); |
michael@0 | 1991 | case F_NewInitializeArgs: |
michael@0 | 1992 | (void) DeleteVariable(a[0]->data); |
michael@0 | 1993 | return NewInitializeArgs(a[0]->data,*(CK_ULONG *)a[1]->data,a[2]->data); |
michael@0 | 1994 | case F_System: |
michael@0 | 1995 | value = *(int *)a[0]->data; |
michael@0 | 1996 | if (value & 0x80000000) { |
michael@0 | 1997 | systemFlags &= ~value; |
michael@0 | 1998 | } else { |
michael@0 | 1999 | systemFlags |= value; |
michael@0 | 2000 | } |
michael@0 | 2001 | return CKR_OK; |
michael@0 | 2002 | case F_Loop: |
michael@0 | 2003 | return loop(a[0]->data,a[1]->data,*(CK_ULONG *)a[2]->data, |
michael@0 | 2004 | *(CK_ULONG *)a[3]->data,*(CK_ULONG *)a[4]->data); |
michael@0 | 2005 | case F_Help: |
michael@0 | 2006 | if (a[0]) { |
michael@0 | 2007 | helpIndex = lookup(a[0]->data); |
michael@0 | 2008 | if (helpIndex < 0) { |
michael@0 | 2009 | return printTopicHelp(a[0]->data); |
michael@0 | 2010 | } |
michael@0 | 2011 | printHelp(helpIndex, 1); |
michael@0 | 2012 | return CKR_OK; |
michael@0 | 2013 | } |
michael@0 | 2014 | return printGeneralHelp(); |
michael@0 | 2015 | case F_QuitIfString: |
michael@0 | 2016 | return quitIfString(a[0]->data,a[1]->data,a[2]->data); |
michael@0 | 2017 | case F_QuitIf: |
michael@0 | 2018 | return quitIf(*(CK_ULONG *)a[0]->data,a[1]->data,*(CK_ULONG *)a[2]->data); |
michael@0 | 2019 | case F_Quit: |
michael@0 | 2020 | return CKR_QUIT; |
michael@0 | 2021 | default: |
michael@0 | 2022 | fprintf(stderr, |
michael@0 | 2023 | "Function %s not yet supported\n",commands[index].fname ); |
michael@0 | 2024 | return CKR_OK; |
michael@0 | 2025 | } |
michael@0 | 2026 | /* Not Reached */ |
michael@0 | 2027 | return CKR_OK; |
michael@0 | 2028 | } |
michael@0 | 2029 | |
michael@0 | 2030 | CK_RV |
michael@0 | 2031 | processCommand(const char * buf) |
michael@0 | 2032 | { |
michael@0 | 2033 | CK_RV error = CKR_OK; |
michael@0 | 2034 | int index; |
michael@0 | 2035 | const char *bp; |
michael@0 | 2036 | Value **arglist; |
michael@0 | 2037 | |
michael@0 | 2038 | bp = strip(buf); |
michael@0 | 2039 | /* allow comments and blank lines in scripts */ |
michael@0 | 2040 | if ((*bp == '#') || (*bp == 0) || (*bp == '\n')){ |
michael@0 | 2041 | return CKR_OK; |
michael@0 | 2042 | } |
michael@0 | 2043 | |
michael@0 | 2044 | index = lookup(bp); |
michael@0 | 2045 | |
michael@0 | 2046 | if (index < 0) { |
michael@0 | 2047 | return CKR_OK; |
michael@0 | 2048 | } |
michael@0 | 2049 | |
michael@0 | 2050 | arglist = parseArgs(index,bp); |
michael@0 | 2051 | if (arglist == NULL) { |
michael@0 | 2052 | return CKR_OK; |
michael@0 | 2053 | } |
michael@0 | 2054 | |
michael@0 | 2055 | error = do_func(index,arglist); |
michael@0 | 2056 | if (error == CKR_OK) { |
michael@0 | 2057 | putOutput(arglist); |
michael@0 | 2058 | } else if (error != CKR_QUIT) { |
michael@0 | 2059 | printf(">> Error : "); |
michael@0 | 2060 | printConst(error, ConstResult, 1); |
michael@0 | 2061 | } |
michael@0 | 2062 | |
michael@0 | 2063 | parseFree(arglist); |
michael@0 | 2064 | return error; |
michael@0 | 2065 | } |
michael@0 | 2066 | |
michael@0 | 2067 | CK_RV |
michael@0 | 2068 | timeCommand(const char *command) |
michael@0 | 2069 | { |
michael@0 | 2070 | CK_RV ckrv; |
michael@0 | 2071 | PRIntervalTime startTime = PR_IntervalNow(); |
michael@0 | 2072 | PRIntervalTime endTime; |
michael@0 | 2073 | PRIntervalTime elapsedTime; |
michael@0 | 2074 | |
michael@0 | 2075 | ckrv = processCommand(command); |
michael@0 | 2076 | |
michael@0 | 2077 | endTime = PR_IntervalNow(); |
michael@0 | 2078 | elapsedTime = endTime - startTime; |
michael@0 | 2079 | printf("Time -- %d msec \n", |
michael@0 | 2080 | PR_IntervalToMilliseconds(elapsedTime)); |
michael@0 | 2081 | |
michael@0 | 2082 | return ckrv; |
michael@0 | 2083 | } |
michael@0 | 2084 | |
michael@0 | 2085 | |
michael@0 | 2086 | |
michael@0 | 2087 | CK_RV |
michael@0 | 2088 | process(FILE *inFile,int user) |
michael@0 | 2089 | { |
michael@0 | 2090 | char buf[2048]; |
michael@0 | 2091 | CK_RV error; |
michael@0 | 2092 | CK_RV ckrv = CKR_OK; |
michael@0 | 2093 | |
michael@0 | 2094 | if (user) { printf("pkcs11> "); fflush(stdout); } |
michael@0 | 2095 | |
michael@0 | 2096 | while (fgets(buf,2048,inFile) != NULL) { |
michael@0 | 2097 | |
michael@0 | 2098 | if (!user) printf("* %s",buf); |
michael@0 | 2099 | error = processCommand(buf); |
michael@0 | 2100 | if (error == CKR_QUIT) { |
michael@0 | 2101 | break; |
michael@0 | 2102 | } else if (error != CKR_OK) { |
michael@0 | 2103 | ckrv = error; |
michael@0 | 2104 | } |
michael@0 | 2105 | if (user) { |
michael@0 | 2106 | printf("pkcs11> "); fflush(stdout); |
michael@0 | 2107 | } |
michael@0 | 2108 | } |
michael@0 | 2109 | return ckrv; |
michael@0 | 2110 | } |
michael@0 | 2111 | |
michael@0 | 2112 | CK_RV |
michael@0 | 2113 | run(const char *filename) |
michael@0 | 2114 | { |
michael@0 | 2115 | FILE *infile; |
michael@0 | 2116 | CK_RV ckrv; |
michael@0 | 2117 | |
michael@0 | 2118 | infile = fopen(filename,"r"); |
michael@0 | 2119 | |
michael@0 | 2120 | if (infile == NULL) { |
michael@0 | 2121 | perror(filename); |
michael@0 | 2122 | return CKR_FUNCTION_FAILED; |
michael@0 | 2123 | } |
michael@0 | 2124 | |
michael@0 | 2125 | ckrv = process(infile, 0); |
michael@0 | 2126 | |
michael@0 | 2127 | fclose(infile); |
michael@0 | 2128 | return ckrv; |
michael@0 | 2129 | } |
michael@0 | 2130 | |
michael@0 | 2131 | CK_RV |
michael@0 | 2132 | loop(const char *filename, const char *var, |
michael@0 | 2133 | CK_ULONG start, CK_ULONG end, CK_ULONG step) |
michael@0 | 2134 | { |
michael@0 | 2135 | CK_ULONG i = 0; |
michael@0 | 2136 | Value *value = 0; |
michael@0 | 2137 | CK_RV ckrv; |
michael@0 | 2138 | |
michael@0 | 2139 | for (i=start; i < end; i += step) |
michael@0 | 2140 | { |
michael@0 | 2141 | value = NewValue(ArgULong, 1); |
michael@0 | 2142 | *(CK_ULONG *)value->data = i; |
michael@0 | 2143 | DeleteVariable(var); |
michael@0 | 2144 | AddVariable(var, &value); |
michael@0 | 2145 | ckrv = run(filename); |
michael@0 | 2146 | argFree(value); |
michael@0 | 2147 | if (ckrv == CKR_QUIT) { |
michael@0 | 2148 | break; |
michael@0 | 2149 | } |
michael@0 | 2150 | } |
michael@0 | 2151 | return ckrv; |
michael@0 | 2152 | } |
michael@0 | 2153 | |
michael@0 | 2154 | int |
michael@0 | 2155 | main(int argc, char **argv) |
michael@0 | 2156 | { |
michael@0 | 2157 | /* I suppose that some day we could parse some arguments */ |
michael@0 | 2158 | (void) process(stdin, 1); |
michael@0 | 2159 | return 0; |
michael@0 | 2160 | } |