nss: import at 3.0.1 beta 1
[mozilla-nss.git] / security / nss / cmd / atob / atob.c
blobe5fad05ecf2bb06ee89e6035df296a743ec91ebd
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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.
14 * The Original Code is the Netscape security libraries.
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.
21 * Contributor(s):
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.
35 * ***** END LICENSE BLOCK ***** */
37 #include "plgetopt.h"
38 #include "secutil.h"
39 #include "nssb64.h"
40 #include <errno.h>
42 #if defined(XP_WIN) || (defined(__sun) && !defined(SVR4))
43 #if !defined(WIN32)
44 extern int fread(char *, size_t, size_t, FILE*);
45 extern int fwrite(char *, size_t, size_t, FILE*);
46 extern int fprintf(FILE *, char *, ...);
47 #endif
48 #endif
50 #if defined(WIN32)
51 #include "fcntl.h"
52 #include "io.h"
53 #endif
55 static PRInt32
56 output_binary (void *arg, const unsigned char *obuf, PRInt32 size)
58 FILE *outFile = arg;
59 int nb;
61 nb = fwrite(obuf, 1, size, outFile);
62 if (nb != size) {
63 PORT_SetError(SEC_ERROR_IO);
64 return -1;
67 return nb;
70 static SECStatus
71 decode_file(FILE *outFile, FILE *inFile)
73 NSSBase64Decoder *cx;
74 int nb;
75 SECStatus status = SECFailure;
76 char ibuf[4096];
78 cx = NSSBase64Decoder_Create(output_binary, outFile);
79 if (!cx) {
80 return -1;
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;
92 /* eof */
93 break;
97 status = NSSBase64Decoder_Update(cx, ibuf, nb);
98 if (status != SECSuccess) goto loser;
101 return NSSBase64Decoder_Destroy(cx, PR_FALSE);
103 loser:
104 (void) NSSBase64Decoder_Destroy(cx, PR_TRUE);
105 return status;
108 static void Usage(char *progName)
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);
120 int main(int argc, char **argv)
122 char *progName;
123 SECStatus rv;
124 FILE *inFile, *outFile;
125 PLOptState *optstate;
126 PLOptStatus status;
128 inFile = 0;
129 outFile = 0;
130 progName = strrchr(argv[0], '/');
131 progName = progName ? progName+1 : argv[0];
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;
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;
148 break;
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;
157 break;
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;
170 #endif
171 outFile = stdout;
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;
179 return 0;