liveMedia/Base64.cpp

Go to the documentation of this file.
00001 /**********
00002 This library is free software; you can redistribute it and/or modify it under
00003 the terms of the GNU Lesser General Public License as published by the
00004 Free Software Foundation; either version 2.1 of the License, or (at your
00005 option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)
00006 
00007 This library is distributed in the hope that it will be useful, but WITHOUT
00008 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00009 FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
00010 more details.
00011 
00012 You should have received a copy of the GNU Lesser General Public License
00013 along with this library; if not, write to the Free Software Foundation, Inc.,
00014 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
00015 **********/
00016 // "liveMedia"
00017 // Copyright (c) 1996-2012 Live Networks, Inc.  All rights reserved.
00018 // Base64 encoding and decoding
00019 // implementation
00020 
00021 #include "Base64.hh"
00022 #include <strDup.hh>
00023 #include <string.h>
00024 
00025 static char base64DecodeTable[256];
00026 
00027 static void initBase64DecodeTable() {
00028   int i;
00029   for (i = 0; i < 256; ++i) base64DecodeTable[i] = (char)0x80;
00030       // default value: invalid
00031 
00032   for (i = 'A'; i <= 'Z'; ++i) base64DecodeTable[i] = 0 + (i - 'A');
00033   for (i = 'a'; i <= 'z'; ++i) base64DecodeTable[i] = 26 + (i - 'a');
00034   for (i = '0'; i <= '9'; ++i) base64DecodeTable[i] = 52 + (i - '0');
00035   base64DecodeTable[(unsigned char)'+'] = 62;
00036   base64DecodeTable[(unsigned char)'/'] = 63;
00037   base64DecodeTable[(unsigned char)'='] = 0;
00038 }
00039 
00040 unsigned char* base64Decode(char* in, unsigned& resultSize,
00041                             Boolean trimTrailingZeros) {
00042   static Boolean haveInitedBase64DecodeTable = False;
00043   if (!haveInitedBase64DecodeTable) {
00044     initBase64DecodeTable();
00045     haveInitedBase64DecodeTable = True;
00046   }
00047 
00048   unsigned char* out = (unsigned char*)strDupSize(in); // ensures we have enough space
00049   int k = 0;
00050   int const jMax = strlen(in) - 3;
00051      // in case "in" is not a multiple of 4 bytes (although it should be)
00052   for (int j = 0; j < jMax; j += 4) {
00053     char inTmp[4], outTmp[4];
00054     for (int i = 0; i < 4; ++i) {
00055       inTmp[i] = in[i+j];
00056       outTmp[i] = base64DecodeTable[(unsigned char)inTmp[i]];
00057       if ((outTmp[i]&0x80) != 0) outTmp[i] = 0; // pretend the input was 'A'
00058     }
00059 
00060     out[k++] = (outTmp[0]<<2) | (outTmp[1]>>4);
00061     out[k++] = (outTmp[1]<<4) | (outTmp[2]>>2);
00062     out[k++] = (outTmp[2]<<6) | outTmp[3];
00063   }
00064 
00065   if (trimTrailingZeros) {
00066     while (k > 0 && out[k-1] == '\0') --k;
00067   }
00068   resultSize = k;
00069   unsigned char* result = new unsigned char[resultSize];
00070   memmove(result, out, resultSize);
00071   delete[] out;
00072 
00073   return result;
00074 }
00075 
00076 static const char base64Char[] =
00077 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
00078 
00079 char* base64Encode(char const* origSigned, unsigned origLength) {
00080   unsigned char const* orig = (unsigned char const*)origSigned; // in case any input bytes have the MSB set
00081   if (orig == NULL) return NULL;
00082 
00083   unsigned const numOrig24BitValues = origLength/3;
00084   Boolean havePadding = origLength > numOrig24BitValues*3;
00085   Boolean havePadding2 = origLength == numOrig24BitValues*3 + 2;
00086   unsigned const numResultBytes = 4*(numOrig24BitValues + havePadding);
00087   char* result = new char[numResultBytes+1]; // allow for trailing '\0'
00088 
00089   // Map each full group of 3 input bytes into 4 output base-64 characters:
00090   unsigned i;
00091   for (i = 0; i < numOrig24BitValues; ++i) {
00092     result[4*i+0] = base64Char[(orig[3*i]>>2)&0x3F];
00093     result[4*i+1] = base64Char[(((orig[3*i]&0x3)<<4) | (orig[3*i+1]>>4))&0x3F];
00094     result[4*i+2] = base64Char[((orig[3*i+1]<<2) | (orig[3*i+2]>>6))&0x3F];
00095     result[4*i+3] = base64Char[orig[3*i+2]&0x3F];
00096   }
00097 
00098   // Now, take padding into account.  (Note: i == numOrig24BitValues)
00099   if (havePadding) {
00100     result[4*i+0] = base64Char[(orig[3*i]>>2)&0x3F];
00101     if (havePadding2) {
00102       result[4*i+1] = base64Char[(((orig[3*i]&0x3)<<4) | (orig[3*i+1]>>4))&0x3F];
00103       result[4*i+2] = base64Char[(orig[3*i+1]<<2)&0x3F];
00104     } else {
00105       result[4*i+1] = base64Char[((orig[3*i]&0x3)<<4)&0x3F];
00106       result[4*i+2] = '=';
00107     }
00108     result[4*i+3] = '=';
00109   }
00110 
00111   result[numResultBytes] = '\0';
00112   return result;
00113 }

Generated on Thu Feb 2 23:51:29 2012 for live by  doxygen 1.5.2