这是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] == '