X-Git-Url: https://glassweightruler.freedombox.rocks/gitweb/Ventoy.git/blobdiff_plain/c87ad1d734f472332fea8e80eb74cda144f9e85b..3f09fb9a2389c5c212a70feada3206a3cb523c95:/Plugson/src/Core/ventoy_util.c diff --git a/Plugson/src/Core/ventoy_util.c b/Plugson/src/Core/ventoy_util.c index 2a00433..6d3d9e7 100644 --- a/Plugson/src/Core/ventoy_util.c +++ b/Plugson/src/Core/ventoy_util.c @@ -257,4 +257,48 @@ if (backup) #endif } +static const char g_encoding_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', '+', '/'}; + +char * ventoy_base64_encode(const char *data, int input_length, int *output_length) +{ + int i = 0; + int j = 0; + char *encoded_data = NULL; + int mod_table[] = {0, 2, 1}; + + *output_length = 4 * ((input_length + 2) / 3); + encoded_data = malloc(*output_length + 4); + if (!encoded_data) + { + return NULL; + } + + while (i < input_length) + { + unsigned int octet_a = i < input_length ? (unsigned char)data[i++] : 0; + unsigned int octet_b = i < input_length ? (unsigned char)data[i++] : 0; + unsigned int octet_c = i < input_length ? (unsigned char)data[i++] : 0; + + unsigned int triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c; + + encoded_data[j++] = g_encoding_table[(triple >> 3 * 6) & 0x3F]; + encoded_data[j++] = g_encoding_table[(triple >> 2 * 6) & 0x3F]; + encoded_data[j++] = g_encoding_table[(triple >> 1 * 6) & 0x3F]; + encoded_data[j++] = g_encoding_table[(triple >> 0 * 6) & 0x3F]; + } + + for (i = 0; i < mod_table[input_length % 3]; i++) + { + encoded_data[*output_length - 1 - i] = '='; + } + + return encoded_data; +}