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-2014 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 const* in, unsigned& resultSize,
00041                             Boolean trimTrailingZeros) {
00042   if (in == NULL) return NULL; // sanity check
00043   return base64Decode(in, strlen(in), resultSize, trimTrailingZeros);
00044 }
00045 
00046 unsigned char* base64Decode(char const* in, unsigned inSize,
00047                             unsigned& resultSize,
00048                             Boolean trimTrailingZeros) {
00049   static Boolean haveInitializedBase64DecodeTable = False;
00050   if (!haveInitializedBase64DecodeTable) {
00051     initBase64DecodeTable();
00052     haveInitializedBase64DecodeTable = True;
00053   }
00054 
00055   unsigned char* out = (unsigned char*)strDupSize(in); // ensures we have enough space
00056   int k = 0;
00057   int paddingCount = 0;
00058   int const jMax = inSize - 3;
00059      // in case "inSize" is not a multiple of 4 (although it should be)
00060   for (int j = 0; j < jMax; j += 4) {
00061     char inTmp[4], outTmp[4];
00062     for (int i = 0; i < 4; ++i) {
00063       inTmp[i] = in[i+j];
00064       if (inTmp[i] == '=') ++paddingCount;
00065       outTmp[i] = base64DecodeTable[(unsigned char)inTmp[i]];
00066       if ((outTmp[i]&0x80) != 0) outTmp[i] = 0; // this happens only if there was an invalid character; pretend that it was 'A'
00067     }
00068 
00069     out[k++] = (outTmp[0]<<2) | (outTmp[1]>>4);
00070     out[k++] = (outTmp[1]<<4) | (outTmp[2]>>2);
00071     out[k++] = (outTmp[2]<<6) | outTmp[3];
00072   }
00073 
00074   if (trimTrailingZeros) {
00075     while (paddingCount > 0 && k > 0 && out[k-1] == '\0') { --k; --paddingCount; }
00076   }
00077   resultSize = k;
00078   unsigned char* result = new unsigned char[resultSize];
00079   memmove(result, out, resultSize);
00080   delete[] out;
00081 
00082   return result;
00083 }
00084 
00085 static const char base64Char[] =
00086 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
00087 
00088 char* base64Encode(char const* origSigned, unsigned origLength) {
00089   unsigned char const* orig = (unsigned char const*)origSigned; // in case any input bytes have the MSB set
00090   if (orig == NULL) return NULL;
00091 
00092   unsigned const numOrig24BitValues = origLength/3;
00093   Boolean havePadding = origLength > numOrig24BitValues*3;
00094   Boolean havePadding2 = origLength == numOrig24BitValues*3 + 2;
00095   unsigned const numResultBytes = 4*(numOrig24BitValues + havePadding);
00096   char* result = new char[numResultBytes+1]; // allow for trailing '\0'
00097 
00098   // Map each full group of 3 input bytes into 4 output base-64 characters:
00099   unsigned i;
00100   for (i = 0; i < numOrig24BitValues; ++i) {
00101     result[4*i+0] = base64Char[(orig[3*i]>>2)&0x3F];
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) | (orig[3*i+2]>>6))&0x3F];
00104     result[4*i+3] = base64Char[orig[3*i+2]&0x3F];
00105   }
00106 
00107   // Now, take padding into account.  (Note: i == numOrig24BitValues)
00108   if (havePadding) {
00109     result[4*i+0] = base64Char[(orig[3*i]>>2)&0x3F];
00110     if (havePadding2) {
00111       result[4*i+1] = base64Char[(((orig[3*i]&0x3)<<4) | (orig[3*i+1]>>4))&0x3F];
00112       result[4*i+2] = base64Char[(orig[3*i+1]<<2)&0x3F];
00113     } else {
00114       result[4*i+1] = base64Char[((orig[3*i]&0x3)<<4)&0x3F];
00115       result[4*i+2] = '=';
00116     }
00117     result[4*i+3] = '=';
00118   }
00119 
00120   result[numResultBytes] = '\0';
00121   return result;
00122 }

Generated on Tue Mar 25 14:35:34 2014 for live by  doxygen 1.5.2