首页 > 代码库 > Base64编码

Base64编码

这是Live555的源码

  1 #include <strDup.hh>
  2 #include <string.h>
  3 
  4 static char base64DecodeTable[256];
  5 
  6 static void initBase64DecodeTable() {
  7   int i;
  8   for (i = 0; i < 256; ++i) base64DecodeTable[i] = (char)0x80;
  9       // default value: invalid
 10 
 11   for (i = A; i <= Z; ++i) base64DecodeTable[i] = 0 + (i - A);
 12   for (i = a; i <= z; ++i) base64DecodeTable[i] = 26 + (i - a);
 13   for (i = 0; i <= 9; ++i) base64DecodeTable[i] = 52 + (i - 0);
 14   base64DecodeTable[(unsigned char)+] = 62;
 15   base64DecodeTable[(unsigned char)/] = 63;
 16   base64DecodeTable[(unsigned char)=] = 0;
 17 }
 18 
 19 unsigned char* base64Decode(char const* in, unsigned& resultSize,
 20                 Boolean trimTrailingZeros) {
 21   if (in == NULL) return NULL; // sanity check
 22   return base64Decode(in, strlen(in), resultSize, trimTrailingZeros);
 23 }
 24 
 25 unsigned char* base64Decode(char const* in, unsigned inSize,
 26                 unsigned& resultSize,
 27                 Boolean trimTrailingZeros) {
 28   static Boolean haveInitializedBase64DecodeTable = False;
 29   if (!haveInitializedBase64DecodeTable) {
 30     initBase64DecodeTable();
 31     haveInitializedBase64DecodeTable = True;
 32   }
 33 
 34   unsigned char* out = (unsigned char*)strDupSize(in); // ensures we have enough space
 35   int k = 0;
 36   int paddingCount = 0;
 37   int const jMax = inSize - 3;
 38      // in case "inSize" is not a multiple of 4 (although it should be)
 39   for (int j = 0; j < jMax; j += 4) {
 40     char inTmp[4], outTmp[4];
 41     for (int i = 0; i < 4; ++i) {
 42       inTmp[i] = in[i+j];
 43       if (inTmp[i] == =) ++paddingCount;
 44       outTmp[i] = base64DecodeTable[(unsigned char)inTmp[i]];
 45       if ((outTmp[i]&0x80) != 0) outTmp[i] = 0; // this happens only if there was an invalid character; pretend that it was ‘A‘
 46     }
 47 
 48     out[k++] = (outTmp[0]<<2) | (outTmp[1]>>4);
 49     out[k++] = (outTmp[1]<<4) | (outTmp[2]>>2);
 50     out[k++] = (outTmp[2]<<6) | outTmp[3];
 51   }
 52 
 53   if (trimTrailingZeros) {
 54     while (paddingCount > 0 && k > 0 && out[k-1] == \0) { --k; --paddingCount; }
 55   }
 56   resultSize = k;
 57   unsigned char* result = new unsigned char[resultSize];
 58   memmove(result, out, resultSize);
 59   delete[] out;
 60 
 61   return result;
 62 }
 63 
 64 static const char base64Char[] =
 65 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
 66 
 67 char* base64Encode(char const* origSigned, unsigned origLength) {
 68   unsigned char const* orig = (unsigned char const*)origSigned; // in case any input bytes have the MSB set
 69   if (orig == NULL) return NULL;
 70 
 71   unsigned const numOrig24BitValues = origLength/3;
 72   Boolean havePadding = origLength > numOrig24BitValues*3;
 73   Boolean havePadding2 = origLength == numOrig24BitValues*3 + 2;
 74   unsigned const numResultBytes = 4*(numOrig24BitValues + havePadding);
 75   char* result = new char[numResultBytes+1]; // allow for trailing ‘\0‘
 76 
 77   // Map each full group of 3 input bytes into 4 output base-64 characters:
 78   unsigned i;
 79   for (i = 0; i < numOrig24BitValues; ++i) {
 80     result[4*i+0] = base64Char[(orig[3*i]>>2)&0x3F];
 81     result[4*i+1] = base64Char[(((orig[3*i]&0x3)<<4) | (orig[3*i+1]>>4))&0x3F];
 82     result[4*i+2] = base64Char[((orig[3*i+1]<<2) | (orig[3*i+2]>>6))&0x3F];
 83     result[4*i+3] = base64Char[orig[3*i+2]&0x3F];
 84   }
 85 
 86   // Now, take padding into account.  (Note: i == numOrig24BitValues)
 87   if (havePadding) {
 88     result[4*i+0] = base64Char[(orig[3*i]>>2)&0x3F];
 89     if (havePadding2) {
 90       result[4*i+1] = base64Char[(((orig[3*i]&0x3)<<4) | (orig[3*i+1]>>4))&0x3F];
 91       result[4*i+2] = base64Char[(orig[3*i+1]<<2)&0x3F];
 92     } else {
 93       result[4*i+1] = base64Char[((orig[3*i]&0x3)<<4)&0x3F];
 94       result[4*i+2] = =;
 95     }
 96     result[4*i+3] = =;
 97   }
 98 
 99   result[numResultBytes] = \0;
100   return result;
101 }