security/nss/cmd/atob/atob.c

Wed, 31 Dec 2014 07:16:47 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:16:47 +0100
branch
TOR_BUG_9701
changeset 3
141e0f1194b1
permissions
-rw-r--r--

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 #include "plgetopt.h"
michael@0 6 #include "secutil.h"
michael@0 7 #include "nssb64.h"
michael@0 8 #include <errno.h>
michael@0 9
michael@0 10 #if defined(XP_WIN) || (defined(__sun) && !defined(SVR4))
michael@0 11 #if !defined(WIN32)
michael@0 12 extern int fread(char *, size_t, size_t, FILE*);
michael@0 13 extern int fwrite(char *, size_t, size_t, FILE*);
michael@0 14 extern int fprintf(FILE *, char *, ...);
michael@0 15 #endif
michael@0 16 #endif
michael@0 17
michael@0 18 #if defined(WIN32)
michael@0 19 #include "fcntl.h"
michael@0 20 #include "io.h"
michael@0 21 #endif
michael@0 22
michael@0 23 static PRInt32
michael@0 24 output_binary (void *arg, const unsigned char *obuf, PRInt32 size)
michael@0 25 {
michael@0 26 FILE *outFile = arg;
michael@0 27 int nb;
michael@0 28
michael@0 29 nb = fwrite(obuf, 1, size, outFile);
michael@0 30 if (nb != size) {
michael@0 31 PORT_SetError(SEC_ERROR_IO);
michael@0 32 return -1;
michael@0 33 }
michael@0 34
michael@0 35 return nb;
michael@0 36 }
michael@0 37
michael@0 38 static PRBool
michael@0 39 isBase64Char(char c)
michael@0 40 {
michael@0 41 return ((c >= 'A' && c <= 'Z')
michael@0 42 || (c >= 'a' && c <= 'z')
michael@0 43 || (c >= '0' && c <= '9')
michael@0 44 || c == '+'
michael@0 45 || c == '/'
michael@0 46 || c == '=');
michael@0 47 }
michael@0 48
michael@0 49 static SECStatus
michael@0 50 decode_file(FILE *outFile, FILE *inFile)
michael@0 51 {
michael@0 52 NSSBase64Decoder *cx;
michael@0 53 SECStatus status = SECFailure;
michael@0 54 char ibuf[4096];
michael@0 55 const char *ptr;
michael@0 56
michael@0 57 cx = NSSBase64Decoder_Create(output_binary, outFile);
michael@0 58 if (!cx) {
michael@0 59 return -1;
michael@0 60 }
michael@0 61
michael@0 62 for (;;) {
michael@0 63 if (feof(inFile)) break;
michael@0 64 if (!fgets(ibuf, sizeof(ibuf), inFile)) {
michael@0 65 if (ferror(inFile)) {
michael@0 66 PORT_SetError(SEC_ERROR_IO);
michael@0 67 goto loser;
michael@0 68 }
michael@0 69 /* eof */
michael@0 70 break;
michael@0 71 }
michael@0 72 for (ptr = ibuf; *ptr; ++ptr) {
michael@0 73 char c = *ptr;
michael@0 74 if (c == '\n' || c == '\r') {
michael@0 75 break; /* found end of line */
michael@0 76 }
michael@0 77 if (!isBase64Char(c)) {
michael@0 78 ptr = ibuf; /* ignore line */
michael@0 79 break;
michael@0 80 }
michael@0 81 }
michael@0 82 if (ibuf == ptr) {
michael@0 83 continue; /* skip empty or non-base64 line */
michael@0 84 }
michael@0 85
michael@0 86 status = NSSBase64Decoder_Update(cx, ibuf, ptr-ibuf);
michael@0 87 if (status != SECSuccess) goto loser;
michael@0 88 }
michael@0 89
michael@0 90 return NSSBase64Decoder_Destroy(cx, PR_FALSE);
michael@0 91
michael@0 92 loser:
michael@0 93 (void) NSSBase64Decoder_Destroy(cx, PR_TRUE);
michael@0 94 return status;
michael@0 95 }
michael@0 96
michael@0 97 static void Usage(char *progName)
michael@0 98 {
michael@0 99 fprintf(stderr,
michael@0 100 "Usage: %s [-i input] [-o output]\n",
michael@0 101 progName);
michael@0 102 fprintf(stderr, "%-20s Define an input file to use (default is stdin)\n",
michael@0 103 "-i input");
michael@0 104 fprintf(stderr, "%-20s Define an output file to use (default is stdout)\n",
michael@0 105 "-o output");
michael@0 106 exit(-1);
michael@0 107 }
michael@0 108
michael@0 109 int main(int argc, char **argv)
michael@0 110 {
michael@0 111 char *progName;
michael@0 112 SECStatus rv;
michael@0 113 FILE *inFile, *outFile;
michael@0 114 PLOptState *optstate;
michael@0 115 PLOptStatus status;
michael@0 116
michael@0 117 inFile = 0;
michael@0 118 outFile = 0;
michael@0 119 progName = strrchr(argv[0], '/');
michael@0 120 progName = progName ? progName+1 : argv[0];
michael@0 121
michael@0 122 /* Parse command line arguments */
michael@0 123 optstate = PL_CreateOptState(argc, argv, "?hi:o:");
michael@0 124 while ((status = PL_GetNextOpt(optstate)) == PL_OPT_OK) {
michael@0 125 switch (optstate->option) {
michael@0 126 case '?':
michael@0 127 case 'h':
michael@0 128 Usage(progName);
michael@0 129 break;
michael@0 130
michael@0 131 case 'i':
michael@0 132 inFile = fopen(optstate->value, "r");
michael@0 133 if (!inFile) {
michael@0 134 fprintf(stderr, "%s: unable to open \"%s\" for reading\n",
michael@0 135 progName, optstate->value);
michael@0 136 return -1;
michael@0 137 }
michael@0 138 break;
michael@0 139
michael@0 140 case 'o':
michael@0 141 outFile = fopen(optstate->value, "wb");
michael@0 142 if (!outFile) {
michael@0 143 fprintf(stderr, "%s: unable to open \"%s\" for writing\n",
michael@0 144 progName, optstate->value);
michael@0 145 return -1;
michael@0 146 }
michael@0 147 break;
michael@0 148 }
michael@0 149 }
michael@0 150 if (!inFile) inFile = stdin;
michael@0 151 if (!outFile) {
michael@0 152 #if defined(WIN32)
michael@0 153 int smrv = _setmode(_fileno(stdout), _O_BINARY);
michael@0 154 if (smrv == -1) {
michael@0 155 fprintf(stderr,
michael@0 156 "%s: Cannot change stdout to binary mode. Use -o option instead.\n",
michael@0 157 progName);
michael@0 158 return smrv;
michael@0 159 }
michael@0 160 #endif
michael@0 161 outFile = stdout;
michael@0 162 }
michael@0 163 rv = decode_file(outFile, inFile);
michael@0 164 if (rv != SECSuccess) {
michael@0 165 fprintf(stderr, "%s: lossage: error=%d errno=%d\n",
michael@0 166 progName, PORT_GetError(), errno);
michael@0 167 return -1;
michael@0 168 }
michael@0 169 return 0;
michael@0 170 }

mercurial