security/nss/cmd/atob/atob.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/nss/cmd/atob/atob.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,170 @@
     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_binary (void *arg, const unsigned 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 PRBool
    1.42 +isBase64Char(char c)
    1.43 +{
    1.44 +    return ((c >= 'A' && c <= 'Z')
    1.45 +	    || (c >= 'a' && c <= 'z')
    1.46 +	    || (c >= '0' && c <= '9')
    1.47 +	    || c == '+'
    1.48 +	    || c == '/'
    1.49 +	    || c == '=');
    1.50 +}
    1.51 +
    1.52 +static SECStatus
    1.53 +decode_file(FILE *outFile, FILE *inFile)
    1.54 +{
    1.55 +    NSSBase64Decoder *cx;
    1.56 +    SECStatus status = SECFailure;
    1.57 +    char ibuf[4096];
    1.58 +    const char *ptr;
    1.59 +
    1.60 +    cx = NSSBase64Decoder_Create(output_binary, outFile);
    1.61 +    if (!cx) {
    1.62 +	return -1;
    1.63 +    }
    1.64 +
    1.65 +    for (;;) {
    1.66 +	if (feof(inFile)) break;
    1.67 +	if (!fgets(ibuf, sizeof(ibuf), inFile)) {
    1.68 +	    if (ferror(inFile)) {
    1.69 +		PORT_SetError(SEC_ERROR_IO);
    1.70 +		goto loser;
    1.71 +	    }
    1.72 +	    /* eof */
    1.73 +	    break;
    1.74 +	}
    1.75 +	for (ptr = ibuf; *ptr; ++ptr) {
    1.76 +	    char c = *ptr;
    1.77 +	    if (c == '\n' || c == '\r') {
    1.78 +		break; /* found end of line */
    1.79 +	    }
    1.80 +	    if (!isBase64Char(c)) {
    1.81 +	      ptr = ibuf; /* ignore line */
    1.82 +	      break;
    1.83 +	    }
    1.84 +	}
    1.85 +	if (ibuf == ptr) {
    1.86 +	    continue; /* skip empty or non-base64 line */
    1.87 +	}
    1.88 +
    1.89 +	status = NSSBase64Decoder_Update(cx, ibuf, ptr-ibuf);
    1.90 +	if (status != SECSuccess) goto loser;
    1.91 +    }
    1.92 +
    1.93 +    return NSSBase64Decoder_Destroy(cx, PR_FALSE);
    1.94 +
    1.95 +  loser:
    1.96 +    (void) NSSBase64Decoder_Destroy(cx, PR_TRUE);
    1.97 +    return status;
    1.98 +}
    1.99 +
   1.100 +static void Usage(char *progName)
   1.101 +{
   1.102 +    fprintf(stderr,
   1.103 +	    "Usage: %s [-i input] [-o output]\n",
   1.104 +	    progName);
   1.105 +    fprintf(stderr, "%-20s Define an input file to use (default is stdin)\n",
   1.106 +	    "-i input");
   1.107 +    fprintf(stderr, "%-20s Define an output file to use (default is stdout)\n",
   1.108 +	    "-o output");
   1.109 +    exit(-1);
   1.110 +}
   1.111 +
   1.112 +int main(int argc, char **argv)
   1.113 +{
   1.114 +    char *progName;
   1.115 +    SECStatus rv;
   1.116 +    FILE *inFile, *outFile;
   1.117 +    PLOptState *optstate;
   1.118 +    PLOptStatus status;
   1.119 +
   1.120 +    inFile = 0;
   1.121 +    outFile = 0;
   1.122 +    progName = strrchr(argv[0], '/');
   1.123 +    progName = progName ? progName+1 : argv[0];
   1.124 +
   1.125 +    /* Parse command line arguments */
   1.126 +    optstate = PL_CreateOptState(argc, argv, "?hi:o:");
   1.127 +    while ((status = PL_GetNextOpt(optstate)) == PL_OPT_OK) {
   1.128 +	switch (optstate->option) {
   1.129 +	  case '?':
   1.130 +	  case 'h':
   1.131 +	    Usage(progName);
   1.132 +	    break;
   1.133 +
   1.134 +	  case 'i':
   1.135 +	    inFile = fopen(optstate->value, "r");
   1.136 +	    if (!inFile) {
   1.137 +		fprintf(stderr, "%s: unable to open \"%s\" for reading\n",
   1.138 +			progName, optstate->value);
   1.139 +		return -1;
   1.140 +	    }
   1.141 +	    break;
   1.142 +
   1.143 +	  case 'o':
   1.144 +	    outFile = fopen(optstate->value, "wb");
   1.145 +	    if (!outFile) {
   1.146 +		fprintf(stderr, "%s: unable to open \"%s\" for writing\n",
   1.147 +			progName, optstate->value);
   1.148 +		return -1;
   1.149 +	    }
   1.150 +	    break;
   1.151 +	}
   1.152 +    }
   1.153 +    if (!inFile) inFile = stdin;
   1.154 +    if (!outFile) {
   1.155 +#if defined(WIN32)
   1.156 +	int smrv = _setmode(_fileno(stdout), _O_BINARY);
   1.157 +	if (smrv == -1) {
   1.158 +	    fprintf(stderr,
   1.159 +	    "%s: Cannot change stdout to binary mode. Use -o option instead.\n",
   1.160 +	            progName);
   1.161 +	    return smrv;
   1.162 +	}
   1.163 +#endif
   1.164 +    	outFile = stdout;
   1.165 +    }
   1.166 +    rv = decode_file(outFile, inFile);
   1.167 +    if (rv != SECSuccess) {
   1.168 +	fprintf(stderr, "%s: lossage: error=%d errno=%d\n",
   1.169 +		progName, PORT_GetError(), errno);
   1.170 +	return -1;
   1.171 +    }
   1.172 +    return 0;
   1.173 +}

mercurial