PageRenderTime 40ms CodeModel.GetById 14ms app.highlight 20ms RepoModel.GetById 1ms app.codeStats 1ms

/security/nss/cmd/atob/atob.c

http://github.com/zpao/v8monkey
C | 180 lines | 125 code | 18 blank | 37 comment | 25 complexity | 30a83acacbbfba429383d8ae1d3d5c8f MD5 | raw file
  1/* ***** BEGIN LICENSE BLOCK *****
  2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3 *
  4 * The contents of this file are subject to the Mozilla Public License Version
  5 * 1.1 (the "License"); you may not use this file except in compliance with
  6 * the License. You may obtain a copy of the License at
  7 * http://www.mozilla.org/MPL/
  8 *
  9 * Software distributed under the License is distributed on an "AS IS" basis,
 10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 11 * for the specific language governing rights and limitations under the
 12 * License.
 13 *
 14 * The Original Code is the Netscape security libraries.
 15 *
 16 * The Initial Developer of the Original Code is
 17 * Netscape Communications Corporation.
 18 * Portions created by the Initial Developer are Copyright (C) 1994-2000
 19 * the Initial Developer. All Rights Reserved.
 20 *
 21 * Contributor(s):
 22 *
 23 * Alternatively, the contents of this file may be used under the terms of
 24 * either the GNU General Public License Version 2 or later (the "GPL"), or
 25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 26 * in which case the provisions of the GPL or the LGPL are applicable instead
 27 * of those above. If you wish to allow use of your version of this file only
 28 * under the terms of either the GPL or the LGPL, and not to allow others to
 29 * use your version of this file under the terms of the MPL, indicate your
 30 * decision by deleting the provisions above and replace them with the notice
 31 * and other provisions required by the GPL or the LGPL. If you do not delete
 32 * the provisions above, a recipient may use your version of this file under
 33 * the terms of any one of the MPL, the GPL or the LGPL.
 34 *
 35 * ***** END LICENSE BLOCK ***** */
 36
 37#include "plgetopt.h"
 38#include "secutil.h"
 39#include "nssb64.h"
 40#include <errno.h>
 41
 42#if defined(XP_WIN) || (defined(__sun) && !defined(SVR4))
 43#if !defined(WIN32)
 44extern int fread(char *, size_t, size_t, FILE*);
 45extern int fwrite(char *, size_t, size_t, FILE*);
 46extern int fprintf(FILE *, char *, ...);
 47#endif
 48#endif
 49
 50#if defined(WIN32)
 51#include "fcntl.h"
 52#include "io.h"
 53#endif
 54
 55static PRInt32 
 56output_binary (void *arg, const unsigned char *obuf, PRInt32 size)
 57{
 58    FILE *outFile = arg;
 59    int nb;
 60
 61    nb = fwrite(obuf, 1, size, outFile);
 62    if (nb != size) {
 63	PORT_SetError(SEC_ERROR_IO);
 64	return -1;
 65    }
 66
 67    return nb;
 68}
 69
 70static SECStatus
 71decode_file(FILE *outFile, FILE *inFile)
 72{
 73    NSSBase64Decoder *cx;
 74    int nb;
 75    SECStatus status = SECFailure;
 76    char ibuf[4096];
 77
 78    cx = NSSBase64Decoder_Create(output_binary, outFile);
 79    if (!cx) {
 80	return -1;
 81    }
 82
 83    for (;;) {
 84	if (feof(inFile)) break;
 85	nb = fread(ibuf, 1, sizeof(ibuf), inFile);
 86	if (nb != sizeof(ibuf)) {
 87	    if (nb == 0) {
 88		if (ferror(inFile)) {
 89		    PORT_SetError(SEC_ERROR_IO);
 90		    goto loser;
 91		}
 92		/* eof */
 93		break;
 94	    }
 95	}
 96
 97	status = NSSBase64Decoder_Update(cx, ibuf, nb);
 98	if (status != SECSuccess) goto loser;
 99    }
100
101    return NSSBase64Decoder_Destroy(cx, PR_FALSE);
102
103  loser:
104    (void) NSSBase64Decoder_Destroy(cx, PR_TRUE);
105    return status;
106}
107
108static void Usage(char *progName)
109{
110    fprintf(stderr,
111	    "Usage: %s [-i input] [-o output]\n",
112	    progName);
113    fprintf(stderr, "%-20s Define an input file to use (default is stdin)\n",
114	    "-i input");
115    fprintf(stderr, "%-20s Define an output file to use (default is stdout)\n",
116	    "-o output");
117    exit(-1);
118}
119
120int main(int argc, char **argv)
121{
122    char *progName;
123    SECStatus rv;
124    FILE *inFile, *outFile;
125    PLOptState *optstate;
126    PLOptStatus status;
127
128    inFile = 0;
129    outFile = 0;
130    progName = strrchr(argv[0], '/');
131    progName = progName ? progName+1 : argv[0];
132
133    /* Parse command line arguments */
134    optstate = PL_CreateOptState(argc, argv, "i:o:");
135    while ((status = PL_GetNextOpt(optstate)) == PL_OPT_OK) {
136	switch (optstate->option) {
137	  case '?':
138	    Usage(progName);
139	    break;
140
141	  case 'i':
142	    inFile = fopen(optstate->value, "r");
143	    if (!inFile) {
144		fprintf(stderr, "%s: unable to open \"%s\" for reading\n",
145			progName, optstate->value);
146		return -1;
147	    }
148	    break;
149
150	  case 'o':
151	    outFile = fopen(optstate->value, "wb");
152	    if (!outFile) {
153		fprintf(stderr, "%s: unable to open \"%s\" for writing\n",
154			progName, optstate->value);
155		return -1;
156	    }
157	    break;
158	}
159    }
160    if (!inFile) inFile = stdin;
161    if (!outFile) {
162#if defined(WIN32)
163	int smrv = _setmode(_fileno(stdout), _O_BINARY);
164	if (smrv == -1) {
165	    fprintf(stderr,
166	    "%s: Cannot change stdout to binary mode. Use -o option instead.\n",
167	            progName);
168	    return smrv;
169	}
170#endif
171    	outFile = stdout;
172    }
173    rv = decode_file(outFile, inFile);
174    if (rv != SECSuccess) {
175	fprintf(stderr, "%s: lossage: error=%d errno=%d\n",
176		progName, PORT_GetError(), errno);
177	return -1;
178    }
179    return 0;
180}