下面的代碼是php里面的base64編碼邏輯,確實比我之前的要美觀很多,我只是簡單的用C++的類進行了一下封裝,刪除了一些沒用的邏輯,基本上還是原來PHP的代碼:
#include <iostream> #include <cstring> #include <cctype> using namespace std; class Base64{ private: static const char base64_pad = '='; public: unsigned char * Encode(const unsigned char *str, int length, int &ret_length); unsigned char * Decode(const unsigned char *str, int length, int &ret_length); }; unsigned char * Base64::Encode(const unsigned char *str, int length, int &ret_length) /* {{{ */ { /* {{{ base64 tables */ const char base64_table[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/', '\0' }; const unsigned char *current = str; unsigned char *p; unsigned char * result; result = new unsigned char[(length+2)/3*4]; p = result; if (length < 0) { ret_length = 0; return NULL; } while (length > 2) { /* keep going until we have less than 24 bits */ *p++ = base64_table[current[0] >> 2]; *p++= base64_table[((current[0] & 0x03) << 4) + (current[1] >> 4)]; *p++= base64_table[((current[1] & 0x0f) << 2) + (current[2] >> 6)]; *p++= base64_table[current[2] & 0x3f]; current += 3; length -= 3; /* we just handle 3 octets of data */ } /* now deal with the tail end of things */ if (length != 0) { *p++= base64_table[current[0] >> 2]; if (length > 1) { *p++= base64_table[((current[0] & 0x03) << 4) + (current[1] >> 4)]; *p++= base64_table[(current[1] & 0x0f) << 2]; *p++= base64_pad; } else { *p++ = base64_table[(current[0] & 0x03) << 4]; *p++ = base64_pad; *p++ = base64_pad; } } ret_length = (int)(p - result); *p = '\0'; return result; } unsigned char * Base64::Decode(const unsigned char *str, int length, int &ret_length) /* {{{ */ { const short base64_reverse_table[256] = { -2, -2, -2, -2, -2, -2, -2, -2, -2, -1, -1, -2, -2, -1, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -1, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, 62, -2, -2, -2, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -2, -2, -2, -2, -2, -2, -2, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -2, -2, -2, -2, -2, -2, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2 }; const unsigned char *current = str; int ch, i = 0, j = 0, k; /* this sucks for threaded environments */ unsigned char * result; result = new unsigned char[length]; /* run through the whole string, converting as we go */ while ((ch = *current++) != '\0' && length-- > 0) { if (ch == base64_pad) { if (*current != '=' && (i % 4) == 1) { return NULL; } continue; } ch = base64_reverse_table[ch]; if (ch < 0 || ch == -1) { /* a space or some other separator character, we simply skip over */ continue; } else if (ch == -2) { return NULL; } switch(i % 4) { case 0: result[j] = ch << 2; break; case 1: result[j++] |= ch >> 4; result[j] = (ch & 0x0f) << 4; break; case 2: result[j++] |= ch >>2; result[j] = (ch & 0x03) << 6; break; case 3: result[j++] |= ch; break; } i++; cout << "result == " << result << endl; } k = j; /* mop things up if we ended on a boundary */ if (ch == base64_pad) { switch(i % 4) { case 1: return NULL; case 2: k++; case 3: result[k] = 0; } } if(ret_length) { ret_length = j; } result[j] = '\0'; return result; } int main() { unsigned char str[] = " aHR0cDo vL2ltZy52LmNtY20uY29tLzdkNjZkNy0wYzJkLTVhNTgtYTA3Mi03MWY4MjhiOTRjYmNfY3JvcF8yMTZ{4MTUwLmpwZw= ======"; unsigned char *normal,*encoded; int i,len = 0,ret_len=0; cout << "original string : " << str << endl; len = sizeof(str)-1; Base64 * base64 = new Base64(); cout << "str = " << str << endl; normal = base64->Decode(str,len,ret_len); cout << "base64 decode : " << normal <<endl; delete [] encoded; delete [] normal; return 0; }
上面的代碼對php源碼中的邏輯做了優化,刪除了decode方法中判斷結尾的“=”號時多余的邏輯,以免干擾視線。具體刪除的代碼參照php源碼中ext/standard/base64.c
上一篇php實現base64的代碼也進行了調整,提高了容錯。