LPSTR _base64Encode(LPBYTE source, SIZE_T sourceSize, SIZE_T *destSize); LPBYTE _base64Decode(LPSTR source, SIZE_T sourceSize, SIZE_T *destSize);
LPSTR base64Encode(LPBYTE source, SIZE_T sourceSize, SIZE_T *destSize) { static const char cb64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; LPBYTE dest = (LPBYTE)Mem::alloc((sourceSize + 2) / 3 * 4 + 1); if(dest != NULL) { LPBYTE p = dest; BYTE cur[3]; while(sourceSize > 0) { DWORD len = 0; for(DWORD i = 0; i < 3; i++) { if(sourceSize > 0) { sourceSize--; len++; cur[i] = source[i]; } else cur[i] = 0; } source += 3; p[0] = cb64[cur[0] >> 2]; p[1] = cb64[((cur[0] & 0x03) << 4) | ((cur[1] & 0xF0) >> 4)]; p[2] = (BYTE)(len > 1 ? cb64[((cur[1] & 0x0F) << 2) | ((cur[2] & 0xC0) >> 6) ] : '='); p[3] = (BYTE)(len > 2 ? cb64[cur[2] & 0x3F] : '='); p += 4; } *p = 0; if(destSize)*destSize = (SIZE_T)(p - dest); } return (LPSTR)dest; } ////////////////////////// LPBYTE base64Decode(LPSTR source, SIZE_T sourceSize, SIZE_T *destSize) { static const char cd64[] = "|$$$}rstuvwxyz{$$$$$$$>?@ABCDEFGHIJKLMNOPQRSTUVW$$$$$$XYZ[\\]^_`abcdefghijklmnopq"; LPBYTE dest = (LPBYTE)Mem::alloc(sourceSize + sizeof(BYTE)); if(dest != NULL) { LPBYTE p = (LPBYTE)source; LPBYTE e = p + sourceSize; LPBYTE r = (LPBYTE)dest; BYTE in[4], out[3], v; int len, i; while(p < e) { for(len = 0, i = 0; i < 4 && p < e; i++) { v = 0; while(p < e && v == 0) { v = (BYTE)*(p++); v = (BYTE)((v < 43 || v > 122) ? 0 : cd64[v - 43]); if(v != 0)v = (BYTE)((v == '$') ? 0 : v - 61); } if(v != 0) { len++; in[i] = (BYTE)(v - 1); } } if(len) { out[0] = (BYTE)(in[0] << 2 | in[1] >> 4); out[1] = (BYTE)(in[1] << 4 | in[2] >> 2); out[2] = (BYTE)(((in[2] << 6) & 0xC0) | in[3]); for(i = 0; i < len - 1; i++){*(r++) = out[i]; if(i==0)i=0;/*instrict*/} } } *r = 0; if(destSize)*destSize = (SIZE_T)(r - dest); } return dest; }
Aucun commentaire:
Enregistrer un commentaire