Base64 Encoding in AngelScript

AngelScript doesn’t have a built-in base64 encoding/decoding library, so we’ll implement a simple version of base64 encoding and decoding. This example will demonstrate the concept, but it’s not as robust as a full implementation.

string base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

string base64_encode(const string &in data) {
    string encoded;
    int i = 0;
    int j = 0;
    array<uint8> char_array_3(3);
    array<uint8> char_array_4(4);

    for (i = 0; i < data.length(); i++) {
        char_array_3[j++] = data[i];
        if (j == 3) {
            char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
            char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
            char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
            char_array_4[3] = char_array_3[2] & 0x3f;

            for (int i = 0; i < 4; i++)
                encoded += base64_chars[char_array_4[i]];
            j = 0;
        }
    }

    if (j > 0) {
        for (int k = j; k < 3; k++)
            char_array_3[k] = '\0';

        char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
        char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
        char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);

        for (int k = 0; k < j + 1; k++)
            encoded += base64_chars[char_array_4[k]];

        while (j++ < 3)
            encoded += '=';
    }

    return encoded;
}

string base64_decode(const string &in encoded_data) {
    int in_len = encoded_data.length();
    int i = 0;
    int j = 0;
    int in_ = 0;
    array<uint8> char_array_4(4);
    array<uint8> char_array_3(3);
    string decoded;

    while (in_len-- && (encoded_data[in_] != '=') && is_base64(encoded_data[in_])) {
        char_array_4[i++] = encoded_data[in_]; in_++;
        if (i == 4) {
            for (i = 0; i < 4; i++)
                char_array_4[i] = base64_chars.find(char_array_4[i]);

            char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
            char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
            char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];

            for (i = 0; i < 3; i++)
                decoded += char_array_3[i];
            i = 0;
        }
    }

    if (i > 0) {
        for (j = 0; j < i; j++)
            char_array_4[j] = base64_chars.find(char_array_4[j]);

        char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
        char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);

        for (j = 0; j < i - 1; j++)
            decoded += char_array_3[j];
    }

    return decoded;
}

bool is_base64(uint8 c) {
    return (isalnum(c) || (c == '+') || (c == '/'));
}

void main() {
    // Here's the string we'll encode/decode.
    string data = "abc123!?$*&()'-=@~";

    // Encode using our base64 encoding function
    string sEnc = base64_encode(data);
    print(sEnc);

    // Decode the encoded string
    string sDec = base64_decode(sEnc);
    print(sDec);
    print("");

    // AngelScript doesn't have a built-in URL-safe base64 encoding,
    // so we'll use a simple replacement to demonstrate the concept
    string uEnc = sEnc.replace("+", "-").replace("/", "_");
    print(uEnc);
    string uDec = base64_decode(uEnc.replace("-", "+").replace("_", "/"));
    print(uDec);
}

This script provides a basic implementation of base64 encoding and decoding in AngelScript. Here’s what it does:

  1. We define the base64 character set and implement base64_encode and base64_decode functions.

  2. The main function demonstrates the usage:

    • We start with a sample string to encode.
    • We encode the string using our base64_encode function and print the result.
    • We then decode the encoded string using base64_decode and print the result.
    • To demonstrate URL-safe encoding, we replace ‘+’ with ‘-’ and ‘/’ with ‘_’ in the encoded string.
    • Finally, we decode the URL-safe string by first reversing the replacements.

This implementation provides a basic understanding of base64 encoding/decoding in AngelScript. Note that for production use, you would want to use a more robust and well-tested library.

When you run this script, you should see output similar to:

YWJjMTIzIT8kKiYoKSctPUB+
abc123!?$*&()'-=@~

YWJjMTIzIT8kKiYoKSctPUB-
abc123!?$*&()'-=@~

The string encodes to slightly different values with the standard and URL-safe base64 encoders (trailing + vs -) but they both decode to the original string as desired.