|
|
@ -3038,6 +3038,100 @@ unsigned char *DecompressData(unsigned char *compData, int compDataLength, int * |
|
|
|
return data; |
|
|
|
} |
|
|
|
|
|
|
|
// Encode data to Base64 string |
|
|
|
char *EncodeDataBase64(const unsigned char *data, int dataLength, int *outputLength) |
|
|
|
{ |
|
|
|
static const unsigned char base64encodeTable[] = { |
|
|
|
'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', '+', '/' |
|
|
|
}; |
|
|
|
|
|
|
|
static const int modTable[] = { 0, 2, 1 }; |
|
|
|
|
|
|
|
*outputLength = 4*((dataLength + 2)/3); |
|
|
|
|
|
|
|
char *encodedData = RL_MALLOC(*outputLength); |
|
|
|
|
|
|
|
if (encodedData == NULL) return NULL; |
|
|
|
|
|
|
|
for (int i = 0, j = 0; i < dataLength;) |
|
|
|
{ |
|
|
|
unsigned int octetA = (i < dataLength)? (unsigned char)data[i++] : 0; |
|
|
|
unsigned int octetB = (i < dataLength)? (unsigned char)data[i++] : 0; |
|
|
|
unsigned int octetC = (i < dataLength)? (unsigned char)data[i++] : 0; |
|
|
|
|
|
|
|
unsigned int triple = (octetA << 0x10) + (octetB << 0x08) + octetC; |
|
|
|
|
|
|
|
encodedData[j++] = base64encodeTable[(triple >> 3*6) & 0x3F]; |
|
|
|
encodedData[j++] = base64encodeTable[(triple >> 2*6) & 0x3F]; |
|
|
|
encodedData[j++] = base64encodeTable[(triple >> 1*6) & 0x3F]; |
|
|
|
encodedData[j++] = base64encodeTable[(triple >> 0*6) & 0x3F]; |
|
|
|
} |
|
|
|
|
|
|
|
for (int i = 0; i < modTable[dataLength%3]; i++) encodedData[*outputLength - 1 - i] = '='; |
|
|
|
|
|
|
|
return encodedData; |
|
|
|
} |
|
|
|
|
|
|
|
// Decode Base64 string data |
|
|
|
unsigned char *DecodeDataBase64(unsigned char *data, int *outputLength) |
|
|
|
{ |
|
|
|
static const unsigned char base64decodeTable[] = { |
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
|
|
0, 0, 0, 62, 0, 0, 0, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0, 0, 0, 0, 0, 0, 0, 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, 0, 0, 0, 0, 0, 0, 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 |
|
|
|
}; |
|
|
|
|
|
|
|
// Get output size of Base64 input data |
|
|
|
int outLength = 0; |
|
|
|
for (int i = 0; data[4*i] != 0; i++) |
|
|
|
{ |
|
|
|
if (data[4*i + 3] == '=') |
|
|
|
{ |
|
|
|
if (data[4*i + 2] == '=') outLength += 1; |
|
|
|
else outLength += 2; |
|
|
|
} |
|
|
|
else outLength += 3; |
|
|
|
} |
|
|
|
|
|
|
|
// Allocate memory to store decoded Base64 data |
|
|
|
unsigned char *decodedData = (unsigned char *)RL_MALLOC(outLength); |
|
|
|
|
|
|
|
for (int i = 0; i < outLength/3; i++) |
|
|
|
{ |
|
|
|
unsigned char a = base64decodeTable[(int)data[4*i]]; |
|
|
|
unsigned char b = base64decodeTable[(int)data[4*i + 1]]; |
|
|
|
unsigned char c = base64decodeTable[(int)data[4*i + 2]]; |
|
|
|
unsigned char d = base64decodeTable[(int)data[4*i + 3]]; |
|
|
|
|
|
|
|
decodedData[3*i] = (a << 2) | (b >> 4); |
|
|
|
decodedData[3*i + 1] = (b << 4) | (c >> 2); |
|
|
|
decodedData[3*i + 2] = (c << 6) | d; |
|
|
|
} |
|
|
|
|
|
|
|
if (outLength%3 == 1) |
|
|
|
{ |
|
|
|
int n = outLength/3; |
|
|
|
unsigned char a = base64decodeTable[(int)data[4*n]]; |
|
|
|
unsigned char b = base64decodeTable[(int)data[4*n + 1]]; |
|
|
|
decodedData[outLength - 1] = (a << 2) | (b >> 4); |
|
|
|
} |
|
|
|
else if (outLength%3 == 2) |
|
|
|
{ |
|
|
|
int n = outLength/3; |
|
|
|
unsigned char a = base64decodeTable[(int)data[4*n]]; |
|
|
|
unsigned char b = base64decodeTable[(int)data[4*n + 1]]; |
|
|
|
unsigned char c = base64decodeTable[(int)data[4*n + 2]]; |
|
|
|
decodedData[outLength - 2] = (a << 2) | (b >> 4); |
|
|
|
decodedData[outLength - 1] = (b << 4) | (c >> 2); |
|
|
|
} |
|
|
|
|
|
|
|
*outputLength = outLength; |
|
|
|
return decodedData; |
|
|
|
} |
|
|
|
|
|
|
|
// Save integer value to storage file (to defined position) |
|
|
|
// NOTE: Storage positions is directly related to file memory layout (4 bytes each integer) |
|
|
|
bool SaveStorageValue(unsigned int position, int value) |
|
|
|