Back to index

lightning-sunbird  0.9+nobinonly
btoa.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_ascii (void *arg, const 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 encode_file(FILE *outFile, FILE *inFile)
00072 {
00073     NSSBase64Encoder *cx;
00074     int nb;
00075     SECStatus status = SECFailure;
00076     unsigned char ibuf[4096];
00077 
00078     cx = NSSBase64Encoder_Create(output_ascii, 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 = NSSBase64Encoder_Update(cx, ibuf, nb);
00098        if (status != SECSuccess) goto loser;
00099     }
00100 
00101     status = NSSBase64Encoder_Destroy(cx, PR_FALSE);
00102     if (status != SECSuccess)
00103        return status;
00104 
00105     /*
00106      * Add a trailing CRLF.  Note this must be done *after* the call
00107      * to Destroy above (because only then are we sure all data has
00108      * been written out).
00109      */
00110     fwrite("\r\n", 1, 2, outFile);
00111     return SECSuccess;
00112 
00113   loser:
00114     (void) NSSBase64Encoder_Destroy(cx, PR_TRUE);
00115     return status;
00116 }
00117 
00118 static void Usage(char *progName)
00119 {
00120     fprintf(stderr,
00121            "Usage: %s [-i input] [-o output]\n",
00122            progName);
00123     fprintf(stderr, "%-20s Define an input file to use (default is stdin)\n",
00124            "-i input");
00125     fprintf(stderr, "%-20s Define an output file to use (default is stdout)\n",
00126            "-o output");
00127     exit(-1);
00128 }
00129 
00130 int main(int argc, char **argv)
00131 {
00132     char *progName;
00133     SECStatus rv;
00134     FILE *inFile, *outFile;
00135     PLOptState *optstate;
00136     PLOptStatus status;
00137 
00138     inFile = 0;
00139     outFile = 0;
00140     progName = strrchr(argv[0], '/');
00141     if (!progName)
00142        progName = strrchr(argv[0], '\\');
00143     progName = progName ? progName+1 : argv[0];
00144 
00145     /* Parse command line arguments */
00146     optstate = PL_CreateOptState(argc, argv, "i:o:");
00147     while ((status = PL_GetNextOpt(optstate)) == PL_OPT_OK) {
00148        switch (optstate->option) {
00149          default:
00150            Usage(progName);
00151            break;
00152 
00153          case 'i':
00154            inFile = fopen(optstate->value, "rb");
00155            if (!inFile) {
00156               fprintf(stderr, "%s: unable to open \"%s\" for reading\n",
00157                      progName, optstate->value);
00158               return -1;
00159            }
00160            break;
00161 
00162          case 'o':
00163            outFile = fopen(optstate->value, "wb");
00164            if (!outFile) {
00165               fprintf(stderr, "%s: unable to open \"%s\" for writing\n",
00166                      progName, optstate->value);
00167               return -1;
00168            }
00169            break;
00170        }
00171     }
00172     if (status == PL_OPT_BAD)
00173        Usage(progName);
00174     if (!inFile) {
00175 #if defined(WIN32)
00176        /* If we're going to read binary data from stdin, we must put stdin
00177        ** into O_BINARY mode or else incoming \r\n's will become \n's.
00178        */
00179 
00180        int smrv = _setmode(_fileno(stdin), _O_BINARY);
00181        if (smrv == -1) {
00182            fprintf(stderr,
00183            "%s: Cannot change stdin to binary mode. Use -i option instead.\n",
00184                    progName);
00185            return smrv;
00186        }
00187 #endif
00188        inFile = stdin;
00189     }
00190     if (!outFile) {
00191 #if defined(WIN32)
00192        /* We're going to write binary data to stdout. We must put stdout
00193        ** into O_BINARY mode or else outgoing \r\n's will become \r\r\n's.
00194        */
00195 
00196        int smrv = _setmode(_fileno(stdout), _O_BINARY);
00197        if (smrv == -1) {
00198            fprintf(stderr,
00199            "%s: Cannot change stdout to binary mode. Use -o option instead.\n",
00200                    progName);
00201            return smrv;
00202        }
00203 #endif
00204        outFile = stdout;
00205     }
00206     rv = encode_file(outFile, inFile);
00207     if (rv != SECSuccess) {
00208        fprintf(stderr, "%s: lossage: error=%d errno=%d\n",
00209               progName, PORT_GetError(), errno);
00210        return -1;
00211     }
00212     return 0;
00213 }