Back to index

lightning-sunbird  0.9+nobinonly
atob.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is the Netscape security libraries.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 1994-2000
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either the GNU General Public License Version 2 or later (the "GPL"), or
00025  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 
00037 #include "plgetopt.h"
00038 #include "secutil.h"
00039 #include "nssb64.h"
00040 #include <errno.h>
00041 
00042 #if defined(XP_WIN) || (defined(__sun) && !defined(SVR4))
00043 #if !defined(WIN32)
00044 extern int fread(char *, size_t, size_t, FILE*);
00045 extern int fwrite(char *, size_t, size_t, FILE*);
00046 extern int fprintf(FILE *, char *, ...);
00047 #endif
00048 #endif
00049 
00050 #if defined(WIN32)
00051 #include "fcntl.h"
00052 #include "io.h"
00053 #endif
00054 
00055 static PRInt32 
00056 output_binary (void *arg, const unsigned char *obuf, PRInt32 size)
00057 {
00058     FILE *outFile = arg;
00059     int nb;
00060 
00061     nb = fwrite(obuf, 1, size, outFile);
00062     if (nb != size) {
00063        PORT_SetError(SEC_ERROR_IO);
00064        return -1;
00065     }
00066 
00067     return nb;
00068 }
00069 
00070 static SECStatus
00071 decode_file(FILE *outFile, FILE *inFile)
00072 {
00073     NSSBase64Decoder *cx;
00074     int nb;
00075     SECStatus status = SECFailure;
00076     char ibuf[4096];
00077 
00078     cx = NSSBase64Decoder_Create(output_binary, outFile);
00079     if (!cx) {
00080        return -1;
00081     }
00082 
00083     for (;;) {
00084        if (feof(inFile)) break;
00085        nb = fread(ibuf, 1, sizeof(ibuf), inFile);
00086        if (nb != sizeof(ibuf)) {
00087            if (nb == 0) {
00088               if (ferror(inFile)) {
00089                   PORT_SetError(SEC_ERROR_IO);
00090                   goto loser;
00091               }
00092               /* eof */
00093               break;
00094            }
00095        }
00096 
00097        status = NSSBase64Decoder_Update(cx, ibuf, nb);
00098        if (status != SECSuccess) goto loser;
00099     }
00100 
00101     return NSSBase64Decoder_Destroy(cx, PR_FALSE);
00102 
00103   loser:
00104     (void) NSSBase64Decoder_Destroy(cx, PR_TRUE);
00105     return status;
00106 }
00107 
00108 static void Usage(char *progName)
00109 {
00110     fprintf(stderr,
00111            "Usage: %s [-i input] [-o output]\n",
00112            progName);
00113     fprintf(stderr, "%-20s Define an input file to use (default is stdin)\n",
00114            "-i input");
00115     fprintf(stderr, "%-20s Define an output file to use (default is stdout)\n",
00116            "-o output");
00117     exit(-1);
00118 }
00119 
00120 int main(int argc, char **argv)
00121 {
00122     char *progName;
00123     SECStatus rv;
00124     FILE *inFile, *outFile;
00125     PLOptState *optstate;
00126     PLOptStatus status;
00127 
00128     inFile = 0;
00129     outFile = 0;
00130     progName = strrchr(argv[0], '/');
00131     progName = progName ? progName+1 : argv[0];
00132 
00133     /* Parse command line arguments */
00134     optstate = PL_CreateOptState(argc, argv, "i:o:");
00135     while ((status = PL_GetNextOpt(optstate)) == PL_OPT_OK) {
00136        switch (optstate->option) {
00137          case '?':
00138            Usage(progName);
00139            break;
00140 
00141          case 'i':
00142            inFile = fopen(optstate->value, "r");
00143            if (!inFile) {
00144               fprintf(stderr, "%s: unable to open \"%s\" for reading\n",
00145                      progName, optstate->value);
00146               return -1;
00147            }
00148            break;
00149 
00150          case 'o':
00151            outFile = fopen(optstate->value, "wb");
00152            if (!outFile) {
00153               fprintf(stderr, "%s: unable to open \"%s\" for writing\n",
00154                      progName, optstate->value);
00155               return -1;
00156            }
00157            break;
00158        }
00159     }
00160     if (!inFile) inFile = stdin;
00161     if (!outFile) {
00162 #if defined(WIN32)
00163        int smrv = _setmode(_fileno(stdout), _O_BINARY);
00164        if (smrv == -1) {
00165            fprintf(stderr,
00166            "%s: Cannot change stdout to binary mode. Use -o option instead.\n",
00167                    progName);
00168            return smrv;
00169        }
00170 #endif
00171        outFile = stdout;
00172     }
00173     rv = decode_file(outFile, inFile);
00174     if (rv != SECSuccess) {
00175        fprintf(stderr, "%s: lossage: error=%d errno=%d\n",
00176               progName, PORT_GetError(), errno);
00177        return -1;
00178     }
00179     return 0;
00180 }