outfile = PR_Open(bltest.options[opt_Output].arg, PR_WRONLY|PR_CREATE_FILE, 00660);
char * filename = bltest.options[opt_Output].arg; if (bltest.options[opt_SelfTestDir].activated && testdir && filename && filename[0] != '/') filename = PR_smprintf("%s/tests/%s/%s", testdir, mode_strings[cipherInfo.mode], filename); outfile = PR_Open(filename, PR_WRONLY|PR_CREATE_FILE, 00660);
int main(int argc, char **argv){    char *infileName, *outfileName, *keyfileName, *ivfileName;    SECStatus rv;    bltestCipherInfo	 cipherInfo;    bltestParams	*params;    PRFileDesc		*file, *infile, *outfile;    char		*instr = NULL;    PRArenaPool		*arena;    bltestIOMode	 ioMode;    int			 keysize, bufsize, exponent;    int			 i, commandsEntered;    int			 inoff, outoff;    secuCommand bltest;    bltest.numCommands = sizeof(bltest_commands) / sizeof(secuCommandFlag);    bltest.numOptions = sizeof(bltest_options) / sizeof(secuCommandFlag);    bltest.commands = bltest_commands;    bltest.options = bltest_options;    progName = strrchr(argv[0], '/');    if (!progName) 	progName = strrchr(argv[0], '\\');    progName = progName ? progName+1 : argv[0];    rv = RNG_RNGInit();    if (rv != SECSuccess) {    	SECU_PrintPRandOSError(progName);	return -1;    }    RNG_SystemInfoForRNG();    rv = SECU_ParseCommandLine(argc, argv, progName, &bltest);    PORT_Memset(&cipherInfo, 0, sizeof(cipherInfo));    arena = PORT_NewArena(BLTEST_DEFAULT_CHUNKSIZE);    cipherInfo.arena = arena;    params = &cipherInfo.params;    /* set some defaults */    infileName = outfileName = keyfileName = ivfileName = NULL;    /* Check the number of commands entered on the command line. */    commandsEntered = 0;    for (i=0; i<bltest.numCommands; i++)	if (bltest.commands[i].activated)	    commandsEntered++;    if (commandsEntered > 1 &&	!(commandsEntered == 2 && bltest.commands[cmd_SelfTest].activated)) {	fprintf(stderr, "%s: one command at a time!\n", progName);	Usage();    }    if (commandsEntered == 0) {	fprintf(stderr, "%s: you must enter a command!\n", progName);	Usage();    }    if (bltest.commands[cmd_Sign].activated)	bltest.commands[cmd_Encrypt].activated = PR_TRUE;    if (bltest.commands[cmd_Verify].activated)	bltest.commands[cmd_Decrypt].activated = PR_TRUE;    if (bltest.commands[cmd_Hash].activated)	bltest.commands[cmd_Encrypt].activated = PR_TRUE;    inoff = outoff = 0;    if (bltest.options[opt_InputOffset].activated)	inoff = PORT_Atoi(bltest.options[opt_InputOffset].arg);    if (bltest.options[opt_OutputOffset].activated)	outoff = PORT_Atoi(bltest.options[opt_OutputOffset].arg);    testdir = (bltest.options[opt_SelfTestDir].activated) ?                  strdup(bltest.options[opt_SelfTestDir].arg) : ".";    /*     * Handle three simple cases first     */    /* Do BLAPI self-test */    if (bltest.commands[cmd_SelfTest].activated) {	PRBool encrypt = PR_TRUE, decrypt = PR_TRUE;	/* user may specified a set of ciphers to test.  parse them. */	bltestCipherMode modesToTest[NUMMODES];	int numModesToTest = 0;	char *tok, *str;	str = bltest.options[opt_Mode].arg;	while (str) {	    tok = strchr(str, ',');	    if (tok) *tok = '\0';	    modesToTest[numModesToTest++] = get_mode(str);	    if (tok) {		*tok = ',';		str = tok + 1;	    } else {		break;	    }	}	if (bltest.commands[cmd_Decrypt].activated &&	    !bltest.commands[cmd_Encrypt].activated)	    encrypt = PR_FALSE;	if (bltest.commands[cmd_Encrypt].activated &&	    !bltest.commands[cmd_Decrypt].activated)	    decrypt = PR_FALSE;	return blapi_selftest(modesToTest, numModesToTest, inoff, outoff,	                      encrypt, decrypt);    }    /* Do FIPS self-test */    if (bltest.commands[cmd_FIPS].activated) {	CK_RV ckrv = pk11_fipsPowerUpSelfTest();	fprintf(stdout, "CK_RV: %ld.\n", ckrv);	return 0;    }    /*     * Check command line arguments for Encrypt/Decrypt/Hash/Sign/Verify     */    if ((bltest.commands[cmd_Decrypt].activated ||	 bltest.commands[cmd_Verify].activated) &&	bltest.options[opt_BufSize].activated) {	fprintf(stderr, "%s: cannot use a nonce as input to decrypt/verify.\n",			 progName);	Usage();    }    if (bltest.options[opt_Mode].activated) {	cipherInfo.mode = get_mode(bltest.options[opt_Mode].arg);	if (cipherInfo.mode == bltestINVALID) {	    fprintf(stderr, "%s: Invalid mode \"%s\"\n", progName,			     bltest.options[opt_Mode].arg);	    Usage();	}    } else {	fprintf(stderr, "%s: You must specify a cipher mode with -m.\n",			 progName);	Usage();    }    if (bltest.options[opt_Repetitions].activated) {	cipherInfo.repetitions = PORT_Atoi(bltest.options[opt_Repetitions].arg);    } else {	cipherInfo.repetitions = 0;    }    if (bltest.options[opt_CXReps].activated) {	cipherInfo.cxreps = PORT_Atoi(bltest.options[opt_CXReps].arg);    } else {	cipherInfo.cxreps = 0;    }    /* Dump a file (rsakey, dsakey, etc.) */    if (bltest.commands[cmd_Dump].activated) {	return dump_file(cipherInfo.mode, bltest.options[opt_Input].arg);    }    /* default input mode is binary */    ioMode = (bltest.options[opt_B64].activated)     ? bltestBase64Encoded :	     (bltest.options[opt_Hex].activated)     ? bltestHexStream :	     (bltest.options[opt_HexWSpc].activated) ? bltestHexSpaceDelim :						       bltestBinary;    if (bltest.options[opt_KeySize].activated)	keysize = PORT_Atoi(bltest.options[opt_KeySize].arg);    else	keysize = 0;    if (bltest.options[opt_Exponent].activated)	exponent = PORT_Atoi(bltest.options[opt_Exponent].arg);    else	exponent = 65537;    /* Set up an encryption key. */    keysize = 0;    file = NULL;    if (is_symmkeyCipher(cipherInfo.mode)) {	char *keystr = NULL;  /* if key is on command line */	if (bltest.options[opt_Key].activated) {	    if (bltest.options[opt_CmdLine].activated) {		keystr = bltest.options[opt_Key].arg;	    } else {		file = PR_Open(bltest.options[opt_Key].arg, PR_RDONLY, 00660);	    }	} else {	    if (bltest.options[opt_KeySize].activated)		keysize = PORT_Atoi(bltest.options[opt_KeySize].arg);	    else		keysize = 8; /* use 64-bit default (DES) */	    /* save the random key for reference */	    file = PR_Open("tmp.key", PR_WRONLY|PR_CREATE_FILE, 00660);	}	params->key.mode = ioMode;	setupIO(cipherInfo.arena, &params->key, file, keystr, keysize);	if (file)	    PR_Close(file);    } else if (is_pubkeyCipher(cipherInfo.mode)) {	if (bltest.options[opt_Key].activated) {	    file = PR_Open(bltest.options[opt_Key].arg, PR_RDONLY, 00660);	} else {	    if (bltest.options[opt_KeySize].activated)		keysize = PORT_Atoi(bltest.options[opt_KeySize].arg);	    else		keysize = 64; /* use 512-bit default */	    file = PR_Open("tmp.key", PR_WRONLY|PR_CREATE_FILE, 00660);	}	params->key.mode = bltestBase64Encoded;	pubkeyInitKey(&cipherInfo, file, keysize, exponent);	PR_Close(file);    }    /* set up an initialization vector. */    if (cipher_requires_IV(cipherInfo.mode)) {	char *ivstr = NULL;	bltestSymmKeyParams *skp;	file = NULL;	if (cipherInfo.mode == bltestRC5_CBC)	    skp = (bltestSymmKeyParams *)&params->rc5;	else	    skp = &params->sk;	if (bltest.options[opt_IV].activated) {	    if (bltest.options[opt_CmdLine].activated) {		ivstr = bltest.options[opt_IV].arg;	    } else {		file = PR_Open(bltest.options[opt_IV].arg, PR_RDONLY, 00660);	    }	} else {	    /* save the random iv for reference */	    file = PR_Open("tmp.iv", PR_WRONLY|PR_CREATE_FILE, 00660);	}	memset(&skp->iv, 0, sizeof skp->iv);	skp->iv.mode = ioMode;	setupIO(cipherInfo.arena, &skp->iv, file, ivstr, keysize);	if (file)	    PR_Close(file);    }    if (bltest.commands[cmd_Verify].activated) {	if (!bltest.options[opt_SigFile].activated) {	    fprintf(stderr, "%s: You must specify a signature file with -f.\n",	            progName);	    exit(-1);	}	file = PR_Open(bltest.options[opt_SigFile].arg, PR_RDONLY, 00660);	memset(&cipherInfo.params.dsa.sig, 0, sizeof(bltestIO));	cipherInfo.params.dsa.sig.mode = ioMode;	setupIO(cipherInfo.arena, &cipherInfo.params.dsa.sig, file, NULL, 0);    }    if (bltest.options[opt_PQGFile].activated) {	file = PR_Open(bltest.options[opt_PQGFile].arg, PR_RDONLY, 00660);	params->dsa.pqgdata.mode = bltestBase64Encoded;	setupIO(cipherInfo.arena, &params->dsa.pqgdata, file, NULL, 0);    }    /* Set up the input buffer */    if (bltest.options[opt_Input].activated) {	if (bltest.options[opt_CmdLine].activated) {	    instr = bltest.options[opt_Input].arg;	    infile = NULL;	} else {	    infile = PR_Open(bltest.options[opt_Input].arg, PR_RDONLY, 00660);	}    } else if (bltest.options[opt_BufSize].activated) {	/* save the random plaintext for reference */	infile = PR_Open("tmp.in", PR_WRONLY|PR_CREATE_FILE, 00660);    } else {	infile = PR_STDIN;    }    cipherInfo.input.mode = ioMode;    /* Set up the output stream */    if (bltest.options[opt_Output].activated) {	outfile = PR_Open(bltest.options[opt_Output].arg,			  PR_WRONLY|PR_CREATE_FILE, 00660);    } else {	outfile = PR_STDOUT;    }    cipherInfo.output.mode = ioMode;    if (is_hashCipher(cipherInfo.mode))	cipherInfo.params.hash.restart = bltest.options[opt_Restart].activated;    bufsize = 0;    if (bltest.options[opt_BufSize].activated)	bufsize = PORT_Atoi(bltest.options[opt_BufSize].arg);    /*infile = NULL;*/    setupIO(cipherInfo.arena, &cipherInfo.input, infile, instr, bufsize);    misalignBuffer(cipherInfo.arena, &cipherInfo.input, inoff);    cipherInit(&cipherInfo, bltest.commands[cmd_Encrypt].activated);    misalignBuffer(cipherInfo.arena, &cipherInfo.output, outoff);    if (!bltest.commands[cmd_Nonce].activated) {	if (bltest.options[opt_MonteCarlo].activated) {	    int mciter;	    for (mciter=0; mciter<10000; mciter++) {		cipherDoOp(&cipherInfo);		memcpy(cipherInfo.input.buf.data,		       cipherInfo.output.buf.data,		       cipherInfo.input.buf.len);	    }	} else {	    cipherDoOp(&cipherInfo);	}	cipherFinish(&cipherInfo);	finishIO(&cipherInfo.output, outfile);    }    if (cipherInfo.repetitions > 0 || cipherInfo.cxreps > 0)	dump_performance_info(&cipherInfo, 	                      bltest.commands[cmd_Encrypt].activated,	                      (cipherInfo.repetitions == 0));    if (infile && infile != PR_STDIN)	PR_Close(infile);    if (outfile && outfile != PR_STDOUT)	PR_Close(outfile);    PORT_FreeArena(cipherInfo.arena, PR_TRUE);    /*NSS_Shutdown();*/    return SECSuccess;}