1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/security/nss/cmd/btoa/btoa.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,199 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#include "plgetopt.h" 1.9 +#include "secutil.h" 1.10 +#include "nssb64.h" 1.11 +#include <errno.h> 1.12 + 1.13 +#if defined(XP_WIN) || (defined(__sun) && !defined(SVR4)) 1.14 +#if !defined(WIN32) 1.15 +extern int fread(char *, size_t, size_t, FILE*); 1.16 +extern int fwrite(char *, size_t, size_t, FILE*); 1.17 +extern int fprintf(FILE *, char *, ...); 1.18 +#endif 1.19 +#endif 1.20 + 1.21 +#if defined(WIN32) 1.22 +#include "fcntl.h" 1.23 +#include "io.h" 1.24 +#endif 1.25 + 1.26 +static PRInt32 1.27 +output_ascii (void *arg, const char *obuf, PRInt32 size) 1.28 +{ 1.29 + FILE *outFile = arg; 1.30 + int nb; 1.31 + 1.32 + nb = fwrite(obuf, 1, size, outFile); 1.33 + if (nb != size) { 1.34 + PORT_SetError(SEC_ERROR_IO); 1.35 + return -1; 1.36 + } 1.37 + 1.38 + return nb; 1.39 +} 1.40 + 1.41 +static SECStatus 1.42 +encode_file(FILE *outFile, FILE *inFile) 1.43 +{ 1.44 + NSSBase64Encoder *cx; 1.45 + int nb; 1.46 + SECStatus status = SECFailure; 1.47 + unsigned char ibuf[4096]; 1.48 + 1.49 + cx = NSSBase64Encoder_Create(output_ascii, outFile); 1.50 + if (!cx) { 1.51 + return -1; 1.52 + } 1.53 + 1.54 + for (;;) { 1.55 + if (feof(inFile)) break; 1.56 + nb = fread(ibuf, 1, sizeof(ibuf), inFile); 1.57 + if (nb != sizeof(ibuf)) { 1.58 + if (nb == 0) { 1.59 + if (ferror(inFile)) { 1.60 + PORT_SetError(SEC_ERROR_IO); 1.61 + goto loser; 1.62 + } 1.63 + /* eof */ 1.64 + break; 1.65 + } 1.66 + } 1.67 + 1.68 + status = NSSBase64Encoder_Update(cx, ibuf, nb); 1.69 + if (status != SECSuccess) goto loser; 1.70 + } 1.71 + 1.72 + status = NSSBase64Encoder_Destroy(cx, PR_FALSE); 1.73 + if (status != SECSuccess) 1.74 + return status; 1.75 + 1.76 + /* 1.77 + * Add a trailing CRLF. Note this must be done *after* the call 1.78 + * to Destroy above (because only then are we sure all data has 1.79 + * been written out). 1.80 + */ 1.81 + fwrite("\r\n", 1, 2, outFile); 1.82 + return SECSuccess; 1.83 + 1.84 + loser: 1.85 + (void) NSSBase64Encoder_Destroy(cx, PR_TRUE); 1.86 + return status; 1.87 +} 1.88 + 1.89 +static void Usage(char *progName) 1.90 +{ 1.91 + fprintf(stderr, 1.92 + "Usage: %s [-i input] [-o output]\n", 1.93 + progName); 1.94 + fprintf(stderr, "%-20s Define an input file to use (default is stdin)\n", 1.95 + "-i input"); 1.96 + fprintf(stderr, "%-20s Define an output file to use (default is stdout)\n", 1.97 + "-o output"); 1.98 + fprintf(stderr, "%-20s Wrap output in BEGIN/END lines and the given suffix\n", 1.99 + "-w suffix"); 1.100 + fprintf(stderr, "%-20s (use \"c\" as a shortcut for suffix CERTIFICATE)\n", 1.101 + ""); 1.102 + exit(-1); 1.103 +} 1.104 + 1.105 +int main(int argc, char **argv) 1.106 +{ 1.107 + char *progName; 1.108 + SECStatus rv; 1.109 + FILE *inFile, *outFile; 1.110 + PLOptState *optstate; 1.111 + PLOptStatus status; 1.112 + char *suffix = NULL; 1.113 + 1.114 + inFile = 0; 1.115 + outFile = 0; 1.116 + progName = strrchr(argv[0], '/'); 1.117 + if (!progName) 1.118 + progName = strrchr(argv[0], '\\'); 1.119 + progName = progName ? progName+1 : argv[0]; 1.120 + 1.121 + /* Parse command line arguments */ 1.122 + optstate = PL_CreateOptState(argc, argv, "i:o:w:"); 1.123 + while ((status = PL_GetNextOpt(optstate)) == PL_OPT_OK) { 1.124 + switch (optstate->option) { 1.125 + default: 1.126 + Usage(progName); 1.127 + break; 1.128 + 1.129 + case 'i': 1.130 + inFile = fopen(optstate->value, "rb"); 1.131 + if (!inFile) { 1.132 + fprintf(stderr, "%s: unable to open \"%s\" for reading\n", 1.133 + progName, optstate->value); 1.134 + return -1; 1.135 + } 1.136 + break; 1.137 + 1.138 + case 'o': 1.139 + outFile = fopen(optstate->value, "wb"); 1.140 + if (!outFile) { 1.141 + fprintf(stderr, "%s: unable to open \"%s\" for writing\n", 1.142 + progName, optstate->value); 1.143 + return -1; 1.144 + } 1.145 + break; 1.146 + 1.147 + case 'w': 1.148 + if (!strcmp(optstate->value, "c")) 1.149 + suffix = strdup("CERTIFICATE"); 1.150 + else 1.151 + suffix = strdup(optstate->value); 1.152 + break; 1.153 + } 1.154 + } 1.155 + if (status == PL_OPT_BAD) 1.156 + Usage(progName); 1.157 + if (!inFile) { 1.158 +#if defined(WIN32) 1.159 + /* If we're going to read binary data from stdin, we must put stdin 1.160 + ** into O_BINARY mode or else incoming \r\n's will become \n's. 1.161 + */ 1.162 + 1.163 + int smrv = _setmode(_fileno(stdin), _O_BINARY); 1.164 + if (smrv == -1) { 1.165 + fprintf(stderr, 1.166 + "%s: Cannot change stdin to binary mode. Use -i option instead.\n", 1.167 + progName); 1.168 + return smrv; 1.169 + } 1.170 +#endif 1.171 + inFile = stdin; 1.172 + } 1.173 + if (!outFile) { 1.174 +#if defined(WIN32) 1.175 + /* We're going to write binary data to stdout. We must put stdout 1.176 + ** into O_BINARY mode or else outgoing \r\n's will become \r\r\n's. 1.177 + */ 1.178 + 1.179 + int smrv = _setmode(_fileno(stdout), _O_BINARY); 1.180 + if (smrv == -1) { 1.181 + fprintf(stderr, 1.182 + "%s: Cannot change stdout to binary mode. Use -o option instead.\n", 1.183 + progName); 1.184 + return smrv; 1.185 + } 1.186 +#endif 1.187 + outFile = stdout; 1.188 + } 1.189 + if (suffix) { 1.190 + fprintf(outFile, "-----BEGIN %s-----\n", suffix); 1.191 + } 1.192 + rv = encode_file(outFile, inFile); 1.193 + if (rv != SECSuccess) { 1.194 + fprintf(stderr, "%s: lossage: error=%d errno=%d\n", 1.195 + progName, PORT_GetError(), errno); 1.196 + return -1; 1.197 + } 1.198 + if (suffix) { 1.199 + fprintf(outFile, "-----END %s-----\n", suffix); 1.200 + } 1.201 + return 0; 1.202 +}