Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members | Related Pages | Examples

vos/corelibs/typechain/handlers/type_base64.cc

Go to the documentation of this file.
00001 /* $Id: type_base64.cc,v 1.12 2003/05/29 23:24:47 reed Exp $ */
00002 
00003 /** @file type_base64.cc
00004     Defines type handler class for base64-encoded data.
00005     Based on public domain code by John Walker, modified for use on C++ strings, rather than files. (should try streams sometime?)
00006     @author Reed Hedges 12/01
00007 */
00008 
00009 /*
00010     Copyright (C) 2001, 2002 Reed Hedges <reed@zerohour.net>
00011 
00012     This library is free software; you can redistribute it and/or
00013     modify it under the terms of the GNU Lesser General Public
00014     License as published by the Free Software Foundation; either
00015     version 2 of the License, or (at your option) any later version.
00016 
00017     This library is distributed in the hope that it will be useful,
00018     but WITHOUT ANY WARRANTY; without even the implied warranty of
00019     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020     Lesser General Public License for more details.
00021 
00022     You should have received a copy of the GNU Lesser General Public
00023     License along with this library; if not, write to the Free Software
00024     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
00025 */
00026 
00027 
00028 #include "type_base64.hh"
00029 
00030 
00031 /*  Comment from top of base64 code on which this Type handler is based:
00032 
00033        Encode or decode file as MIME base64 (RFC 1341)
00034 
00035             by John Walker
00036             http://www.fourmilab.ch/
00037 
00038             11th August 1997
00039 
00040         This program is in the public domain.
00041 
00042         Permission to use, copy, modify, and distribute this software and its
00043         documentation for any purpose and without fee is hereby granted,
00044         without any conditions or restrictions.  This software is provided ``as
00045         is'' without express or implied warranty.
00046 
00047     Modified by Reed Hedges <reed@zerohour.net> for C++ and strings. 12/18/01
00048 
00049 */
00050 
00051 #include <stdio.h>
00052 
00053 
00054 /* Called when loaded as shared library */
00055 
00056 extern "C" void plugin_init(void *param) {
00057     void (*registerHandler)(TypeHandler* handler) = (void (*)(TypeHandler*))param;
00058     registerHandler(new TypeBase64());
00059 }
00060 
00061 
00062 
00063 
00064 /* Constructor */
00065 
00066 TypeBase64::TypeBase64() : TypeHandler("base64", "b64") {
00067 
00068     /* use \n as end of line delimiter */
00069     eol = "\n";
00070 
00071     /* turn error reporting (stderr) on */
00072     errcheck = 1;
00073 
00074 
00075     /* create magic decoding table */
00076     int i;
00077     for (i = 0; i < 255; i++) {
00078     dtable[i] = 0x80;
00079     }
00080     for (i = 'A'; i <= 'Z'; i++) {
00081         dtable[i] = 0 + (i - 'A');
00082     }
00083     for (i = 'a'; i <= 'z'; i++) {
00084         dtable[i] = 26 + (i - 'a');
00085     }
00086     for (i = '0'; i <= '9'; i++) {
00087         dtable[i] = 52 + (i - '0');
00088     }
00089     dtable['+'] = 62;
00090     dtable['/'] = 63;
00091     dtable['='] = 0;
00092 }
00093 
00094 /*  Decode base64.  */
00095 
00096 void TypeBase64::decode(string& data, TypeParams& params)
00097 {
00098 
00099     string newdata("");
00100     int i;
00101     long count = 0;
00102     string::const_iterator s;
00103 
00104     fprintf(stderr, "Base64: decoding...\n");
00105 
00106     for(s = data.begin(); s != data.end();) {
00107 
00108         byte a[4],  // chunk of input data
00109             b[4],   // corrsponding entries from dtable
00110             o[3];   // chunk of decoded output data
00111 
00112         for (i = 0; i < 4; i++) {
00113 
00114             // check for premature end of data
00115             if (s == data.end()) {
00116                 if (errcheck && (i > 0)) {
00117                     fprintf(stderr, "TypeBase64: Input file incomplete.\n");
00118                 }
00119                 data = newdata;
00120                 return;
00121             }
00122 
00123             int c;
00124             // get next 'significant' character from data
00125             do {
00126                 c = (*s);
00127                 s++;
00128                 if(s == data.end()) {
00129                     data = newdata;
00130                     return;
00131                     // THIS IS BAD-- LAST CHAR WILL BE SKIPPED -- 
00132                     // THOUGH LAST CHAR SHOULD BE = OR \n ANYWAY.
00133                 }
00134             } while( c <= ' ' );
00135 
00136             count++;
00137 
00138 
00139             if (dtable[c] & 0x80) {
00140                 if (errcheck) {
00141                     fprintf(stderr, "TypeBase64: Illegal character '%c' in input file.\n", c);
00142                     data = newdata;
00143                     return;
00144                 }
00145                 /* Ignoring errors: discard invalid character. */
00146                 i--;
00147                 continue;
00148             }
00149             a[i] = (byte) c;
00150             b[i] = (byte) dtable[c];
00151         }
00152         o[0] = (b[0] << 2) | (b[1] >> 4);
00153         o[1] = (b[1] << 4) | (b[2] >> 2);
00154         o[2] = (b[2] << 6) | b[3];
00155         newdata += string((char*)o, 3);
00156         //fprintf(stderr, "Base64: [%ld] original chunk=%c%c%c%c.  dcorresponding decoding values=%i,%i,%i,%i.  new chunk = \"%s\"\n", count , a[0], a[1], a[2], a[3], b[0], b[1], b[2], b[3], o);
00157 
00158         if (i < 3) {
00159             fprintf(stderr, "TypeBase64: end of data, but no '=' ??\n");
00160             data = newdata;
00161             return;
00162         }
00163     }
00164     fprintf(stderr, "Base64: done.\n");
00165     data = newdata;
00166 }
00167 
00168 
00169 
00170 
00171 
00172 /*  ENCODE  --  Encode binary file into base64.  */
00173 
00174 
00175 int inchar() {
00176     // jw had this get next char from input stream/buffer
00177     return EOF;
00178 }
00179 int ochar(int c) {
00180     //jw had this print char to output file
00181     putchar(c);
00182     return 1;
00183 }
00184 
00185 void TypeBase64::encode(string& data, TypeParams& params)
00186 {
00187     return;
00188 
00189     // XXX XXX TODO TODO XXX XXX TODO TODO XXX XXX
00190     
00191     int i, hiteof = 0;
00192 
00193     /*  Fill dtable with character encodings.  */
00194 
00195     for (i = 0; i < 26; i++) {
00196         dtable[i] = 'A' + i;
00197         dtable[26 + i] = 'a' + i;
00198     }
00199     for (i = 0; i < 10; i++) {
00200         dtable[52 + i] = '0' + i;
00201     }
00202     dtable[62] = '+';
00203     dtable[63] = '/';
00204 
00205     while (!hiteof) {
00206     byte igroup[3], ogroup[4];
00207     int c, n;
00208 
00209     igroup[0] = igroup[1] = igroup[2] = 0;
00210     for (n = 0; n < 3; n++) {
00211         c = inchar();
00212         if (c == EOF) {
00213         hiteof = 1;
00214         break;
00215         }
00216         igroup[n] = (byte) c;
00217     }
00218     if (n > 0) {
00219         ogroup[0] = dtable[igroup[0] >> 2];
00220         ogroup[1] = dtable[((igroup[0] & 3) << 4) | (igroup[1] >> 4)];
00221         ogroup[2] = dtable[((igroup[1] & 0xF) << 2) | (igroup[2] >> 6)];
00222         ogroup[3] = dtable[igroup[2] & 0x3F];
00223 
00224             /* Replace characters in output stream with "=" pad
00225            characters if fewer than three characters were
00226            read from the end of the input stream. */
00227 
00228         if (n < 3) {
00229                 ogroup[3] = '=';
00230         if (n < 2) {
00231                     ogroup[2] = '=';
00232         }
00233         }
00234         for (i = 0; i < 4; i++) {
00235         ochar(ogroup[i]);
00236         }
00237     }
00238     }
00239     if (fputs(eol, stdout) == EOF) {
00240         exit(1);
00241     }
00242 
00243 }
00244 
00245 
00246 
00247 

Generated on Tue Aug 12 03:55:38 2003 for Interreality Project - VOS by doxygen 1.3.2