michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "plgetopt.h" michael@0: #include "secutil.h" michael@0: #include "nssb64.h" michael@0: #include michael@0: michael@0: #if defined(XP_WIN) || (defined(__sun) && !defined(SVR4)) michael@0: #if !defined(WIN32) michael@0: extern int fread(char *, size_t, size_t, FILE*); michael@0: extern int fwrite(char *, size_t, size_t, FILE*); michael@0: extern int fprintf(FILE *, char *, ...); michael@0: #endif michael@0: #endif michael@0: michael@0: #if defined(WIN32) michael@0: #include "fcntl.h" michael@0: #include "io.h" michael@0: #endif michael@0: michael@0: static PRInt32 michael@0: output_binary (void *arg, const unsigned char *obuf, PRInt32 size) michael@0: { michael@0: FILE *outFile = arg; michael@0: int nb; michael@0: michael@0: nb = fwrite(obuf, 1, size, outFile); michael@0: if (nb != size) { michael@0: PORT_SetError(SEC_ERROR_IO); michael@0: return -1; michael@0: } michael@0: michael@0: return nb; michael@0: } michael@0: michael@0: static PRBool michael@0: isBase64Char(char c) michael@0: { michael@0: return ((c >= 'A' && c <= 'Z') michael@0: || (c >= 'a' && c <= 'z') michael@0: || (c >= '0' && c <= '9') michael@0: || c == '+' michael@0: || c == '/' michael@0: || c == '='); michael@0: } michael@0: michael@0: static SECStatus michael@0: decode_file(FILE *outFile, FILE *inFile) michael@0: { michael@0: NSSBase64Decoder *cx; michael@0: SECStatus status = SECFailure; michael@0: char ibuf[4096]; michael@0: const char *ptr; michael@0: michael@0: cx = NSSBase64Decoder_Create(output_binary, outFile); michael@0: if (!cx) { michael@0: return -1; michael@0: } michael@0: michael@0: for (;;) { michael@0: if (feof(inFile)) break; michael@0: if (!fgets(ibuf, sizeof(ibuf), inFile)) { michael@0: if (ferror(inFile)) { michael@0: PORT_SetError(SEC_ERROR_IO); michael@0: goto loser; michael@0: } michael@0: /* eof */ michael@0: break; michael@0: } michael@0: for (ptr = ibuf; *ptr; ++ptr) { michael@0: char c = *ptr; michael@0: if (c == '\n' || c == '\r') { michael@0: break; /* found end of line */ michael@0: } michael@0: if (!isBase64Char(c)) { michael@0: ptr = ibuf; /* ignore line */ michael@0: break; michael@0: } michael@0: } michael@0: if (ibuf == ptr) { michael@0: continue; /* skip empty or non-base64 line */ michael@0: } michael@0: michael@0: status = NSSBase64Decoder_Update(cx, ibuf, ptr-ibuf); michael@0: if (status != SECSuccess) goto loser; michael@0: } michael@0: michael@0: return NSSBase64Decoder_Destroy(cx, PR_FALSE); michael@0: michael@0: loser: michael@0: (void) NSSBase64Decoder_Destroy(cx, PR_TRUE); michael@0: return status; michael@0: } michael@0: michael@0: static void Usage(char *progName) michael@0: { michael@0: fprintf(stderr, michael@0: "Usage: %s [-i input] [-o output]\n", michael@0: progName); michael@0: fprintf(stderr, "%-20s Define an input file to use (default is stdin)\n", michael@0: "-i input"); michael@0: fprintf(stderr, "%-20s Define an output file to use (default is stdout)\n", michael@0: "-o output"); michael@0: exit(-1); michael@0: } michael@0: michael@0: int main(int argc, char **argv) michael@0: { michael@0: char *progName; michael@0: SECStatus rv; michael@0: FILE *inFile, *outFile; michael@0: PLOptState *optstate; michael@0: PLOptStatus status; michael@0: michael@0: inFile = 0; michael@0: outFile = 0; michael@0: progName = strrchr(argv[0], '/'); michael@0: progName = progName ? progName+1 : argv[0]; michael@0: michael@0: /* Parse command line arguments */ michael@0: optstate = PL_CreateOptState(argc, argv, "?hi:o:"); michael@0: while ((status = PL_GetNextOpt(optstate)) == PL_OPT_OK) { michael@0: switch (optstate->option) { michael@0: case '?': michael@0: case 'h': michael@0: Usage(progName); michael@0: break; michael@0: michael@0: case 'i': michael@0: inFile = fopen(optstate->value, "r"); michael@0: if (!inFile) { michael@0: fprintf(stderr, "%s: unable to open \"%s\" for reading\n", michael@0: progName, optstate->value); michael@0: return -1; michael@0: } michael@0: break; michael@0: michael@0: case 'o': michael@0: outFile = fopen(optstate->value, "wb"); michael@0: if (!outFile) { michael@0: fprintf(stderr, "%s: unable to open \"%s\" for writing\n", michael@0: progName, optstate->value); michael@0: return -1; michael@0: } michael@0: break; michael@0: } michael@0: } michael@0: if (!inFile) inFile = stdin; michael@0: if (!outFile) { michael@0: #if defined(WIN32) michael@0: int smrv = _setmode(_fileno(stdout), _O_BINARY); michael@0: if (smrv == -1) { michael@0: fprintf(stderr, michael@0: "%s: Cannot change stdout to binary mode. Use -o option instead.\n", michael@0: progName); michael@0: return smrv; michael@0: } michael@0: #endif michael@0: outFile = stdout; michael@0: } michael@0: rv = decode_file(outFile, inFile); michael@0: if (rv != SECSuccess) { michael@0: fprintf(stderr, "%s: lossage: error=%d errno=%d\n", michael@0: progName, PORT_GetError(), errno); michael@0: return -1; michael@0: } michael@0: return 0; michael@0: }