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
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.
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 ***** */
42 #if defined(XP_WIN) || (defined(__sun) && !defined(SVR4))
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 *, ...);
56 output_binary (void *arg
, const unsigned char *obuf
, PRInt32 size
)
61 nb
= fwrite(obuf
, 1, size
, outFile
);
63 PORT_SetError(SEC_ERROR_IO
);
71 decode_file(FILE *outFile
, FILE *inFile
)
75 SECStatus status
= SECFailure
;
78 cx
= NSSBase64Decoder_Create(output_binary
, outFile
);
84 if (feof(inFile
)) break;
85 nb
= fread(ibuf
, 1, sizeof(ibuf
), inFile
);
86 if (nb
!= sizeof(ibuf
)) {
89 PORT_SetError(SEC_ERROR_IO
);
97 status
= NSSBase64Decoder_Update(cx
, ibuf
, nb
);
98 if (status
!= SECSuccess
) goto loser
;
101 return NSSBase64Decoder_Destroy(cx
, PR_FALSE
);
104 (void) NSSBase64Decoder_Destroy(cx
, PR_TRUE
);
108 static void Usage(char *progName
)
111 "Usage: %s [-i input] [-o output]\n",
113 fprintf(stderr
, "%-20s Define an input file to use (default is stdin)\n",
115 fprintf(stderr
, "%-20s Define an output file to use (default is stdout)\n",
120 int main(int argc
, char **argv
)
124 FILE *inFile
, *outFile
;
125 PLOptState
*optstate
;
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
) {
142 inFile
= fopen(optstate
->value
, "r");
144 fprintf(stderr
, "%s: unable to open \"%s\" for reading\n",
145 progName
, optstate
->value
);
151 outFile
= fopen(optstate
->value
, "wb");
153 fprintf(stderr
, "%s: unable to open \"%s\" for writing\n",
154 progName
, optstate
->value
);
160 if (!inFile
) inFile
= stdin
;
163 int smrv
= _setmode(_fileno(stdout
), _O_BINARY
);
166 "%s: Cannot change stdout to binary mode. Use -o option instead.\n",
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
);