axmol/core/base/base64.cpp

197 lines
5.6 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2021-12-25 10:04:45 +08:00
https://axis-project.github.io/
2019-11-23 20:27:39 +08:00
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include "base/base64.h"
NS_AX_BEGIN
2019-11-23 20:27:39 +08:00
unsigned char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
2021-12-25 10:04:45 +08:00
int _base64Decode(const unsigned char* input, unsigned int input_len, unsigned char* output, unsigned int* output_len)
2019-11-23 20:27:39 +08:00
{
static char inalphabet[256], decoder[256];
int c = 0, char_count, errors = 0;
2021-12-25 10:04:45 +08:00
unsigned int input_idx = 0;
2019-11-23 20:27:39 +08:00
unsigned int output_idx = 0;
auto alphabetSize = sizeof(alphabet);
2021-12-25 10:04:45 +08:00
for (size_t i = 0; i < alphabetSize; i++)
{
2019-11-23 20:27:39 +08:00
inalphabet[alphabet[i]] = 1;
2021-12-25 10:04:45 +08:00
decoder[alphabet[i]] = i;
2019-11-23 20:27:39 +08:00
}
char_count = 0;
2021-12-25 10:04:45 +08:00
int bits = 0;
for (input_idx = 0; input_idx < input_len; input_idx++)
{
c = input[input_idx];
2019-11-23 20:27:39 +08:00
if (c == '=')
break;
2021-12-25 10:04:45 +08:00
if (c > 255 || !inalphabet[c])
2019-11-23 20:27:39 +08:00
continue;
bits += decoder[c];
char_count++;
2021-12-25 10:04:45 +08:00
if (char_count == 4)
{
output[output_idx++] = (bits >> 16);
output[output_idx++] = ((bits >> 8) & 0xff);
output[output_idx++] = (bits & 0xff);
bits = 0;
char_count = 0;
}
else
{
2019-11-23 20:27:39 +08:00
bits <<= 6;
}
}
2021-12-25 10:04:45 +08:00
if (c == '=')
{
switch (char_count)
{
case 1:
2022-07-15 19:17:01 +08:00
#if (AX_TARGET_PLATFORM != AX_PLATFORM_BADA)
2021-12-25 10:04:45 +08:00
fprintf(stderr, "base64Decode: encoding incomplete: at least 2 bits missing");
2019-11-23 20:27:39 +08:00
#endif
2021-12-25 10:04:45 +08:00
errors++;
break;
case 2:
output[output_idx++] = (bits >> 10);
break;
case 3:
output[output_idx++] = (bits >> 16);
output[output_idx++] = ((bits >> 8) & 0xff);
break;
}
}
else if (input_idx < input_len)
{
if (char_count)
{
2022-07-15 19:17:01 +08:00
#if (AX_TARGET_PLATFORM != AX_PLATFORM_BADA)
2021-12-25 10:04:45 +08:00
fprintf(stderr, "base64 encoding incomplete: at least %d bits truncated", ((4 - char_count) * 6));
2019-11-23 20:27:39 +08:00
#endif
errors++;
}
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
*output_len = output_idx;
return errors;
}
2021-12-25 10:04:45 +08:00
void _base64Encode(const unsigned char* input, unsigned int input_len, char* output)
2019-11-23 20:27:39 +08:00
{
unsigned int char_count;
unsigned int bits;
2021-12-25 10:04:45 +08:00
unsigned int input_idx = 0;
2019-11-23 20:27:39 +08:00
unsigned int output_idx = 0;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
char_count = 0;
2021-12-25 10:04:45 +08:00
bits = 0;
for (input_idx = 0; input_idx < input_len; input_idx++)
{
bits |= input[input_idx];
2019-11-23 20:27:39 +08:00
char_count++;
2021-12-25 10:04:45 +08:00
if (char_count == 3)
{
output[output_idx++] = alphabet[(bits >> 18) & 0x3f];
output[output_idx++] = alphabet[(bits >> 12) & 0x3f];
output[output_idx++] = alphabet[(bits >> 6) & 0x3f];
output[output_idx++] = alphabet[bits & 0x3f];
bits = 0;
char_count = 0;
}
else
{
2019-11-23 20:27:39 +08:00
bits <<= 8;
}
}
2021-12-25 10:04:45 +08:00
if (char_count)
{
if (char_count == 1)
{
2019-11-23 20:27:39 +08:00
bits <<= 8;
}
2021-12-25 10:04:45 +08:00
output[output_idx++] = alphabet[(bits >> 18) & 0x3f];
output[output_idx++] = alphabet[(bits >> 12) & 0x3f];
if (char_count > 1)
{
output[output_idx++] = alphabet[(bits >> 6) & 0x3f];
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
else
{
output[output_idx++] = '=';
}
output[output_idx++] = '=';
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
output[output_idx++] = 0;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
int base64Decode(const unsigned char* in, unsigned int inLength, unsigned char** out)
2019-11-23 20:27:39 +08:00
{
unsigned int outLength = 0;
2021-12-25 10:04:45 +08:00
// should be enough to store 6-bit buffers in 8-bit buffers
2019-11-23 20:27:39 +08:00
*out = (unsigned char*)malloc(inLength / 4 * 3 + 1);
2021-12-25 10:04:45 +08:00
if (*out)
{
2019-11-23 20:27:39 +08:00
int ret = _base64Decode(in, inLength, *out, &outLength);
2021-12-25 10:04:45 +08:00
if (ret > 0)
2019-11-23 20:27:39 +08:00
{
2022-07-15 19:17:01 +08:00
#if (AX_TARGET_PLATFORM != AX_PLATFORM_BADA)
2019-11-23 20:27:39 +08:00
printf("Base64Utils: error decoding");
#endif
free(*out);
2021-12-25 10:04:45 +08:00
*out = nullptr;
2019-11-23 20:27:39 +08:00
outLength = 0;
}
}
return outLength;
}
2021-12-25 10:04:45 +08:00
int base64Encode(const unsigned char* in, unsigned int inLength, char** out)
{
2019-11-23 20:27:39 +08:00
unsigned int outLength = (inLength + 2) / 3 * 4;
2021-12-25 10:04:45 +08:00
// should be enough to store 8-bit buffers in 6-bit buffers
*out = (char*)malloc(outLength + 1);
if (*out)
{
2019-11-23 20:27:39 +08:00
_base64Encode(in, inLength, *out);
}
return outLength;
}
2021-12-25 10:04:45 +08:00
} // namespace cocos2d