liveMedia/Base64.cpp File Reference

#include "Base64.hh"
#include <strDup.hh>
#include <string.h>

Include dependency graph for Base64.cpp:

Go to the source code of this file.

Functions

static void initBase64DecodeTable ()
unsigned char * base64Decode (char *in, unsigned &resultSize, Boolean trimTrailingZeros)
char * base64Encode (char const *origSigned, unsigned origLength)

Variables

static char base64DecodeTable [256]
static const char base64Char []


Function Documentation

unsigned char* base64Decode ( char *  in,
unsigned &  resultSize,
Boolean  trimTrailingZeros 
)

Definition at line 40 of file Base64.cpp.

References base64DecodeTable, False, initBase64DecodeTable(), strDupSize(), and True.

Referenced by QuickTimeFileSink::addAtom_hdlr2(), and parseSPropParameterSets().

00041                                                        {
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 }

char* base64Encode ( char const *  origSigned,
unsigned  origLength 
)

Definition at line 79 of file Base64.cpp.

References base64Char, and NULL.

Referenced by RTSPClient::createAuthenticatorString(), and RTSPClient::sendRequest().

00079                                                                 {
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 }

static void initBase64DecodeTable (  )  [static]

Definition at line 27 of file Base64.cpp.

References base64DecodeTable.

Referenced by base64Decode().

00027                                     {
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 }


Variable Documentation

const char base64Char[] [static]

Initial value:

"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"

Definition at line 76 of file Base64.cpp.

Referenced by base64Encode().

char base64DecodeTable[256] [static]

Definition at line 25 of file Base64.cpp.

Referenced by base64Decode(), and initBase64DecodeTable().


Generated on Tue Oct 7 15:38:23 2008 for live by  doxygen 1.5.2