utils: base64: fix + more tests
authorNiki Roo <niki@nikiroo.be>
Sun, 20 Mar 2022 10:13:13 +0000 (11:13 +0100)
committerNiki Roo <niki@nikiroo.be>
Sun, 20 Mar 2022 10:13:13 +0000 (11:13 +0100)
src/tests/utils/base64.c
src/utils/base64.c

index 18e567f788eb78a280cb7344360857955fac5cb1..a477a30666fbd034eec098664382a96f05d10f3e 100644 (file)
@@ -33,18 +33,41 @@ static char decoded[] = "This is Le Test, we will UTF-8 the String, too!";
 static char encoded[] =
                "VGhpcyBpcyBMZSBUZXN0LCB3ZSB3aWxsIFVURi04IHRoZSBTdHJpbmcsIHRvbyE=";
 
+static char decoded_utf8[] = "Le café d'Abigaëlle";
+static char encoded_utf8[] = "TGUgY2Fmw6kgZCdBYmlnYcOrbGxl";
+
 START(decode)
-               ASSERT_EQUALS_STR("decoding", decoded, base64_decode(encoded));
+               char *tmp = base64_decode(encoded);
+               ASSERT_EQUALS_STR("decoding", decoded, tmp);
+               free(tmp);
                END
 
 START(encode)
-               ASSERT_EQUALS_STR("encoding", encoded, base64_encode(decoded));
+               char *tmp = base64_encode(decoded);
+               ASSERT_EQUALS_STR("encoding", encoded, tmp);
+               free(tmp);
+               END
+
+START(utf8)
+               char *tmp;
+
+               tmp = base64_decode(encoded_utf8);
+               ASSERT_EQUALS_STR("UTF-8 decoding", decoded_utf8, tmp);
+               free(tmp);
+
+               tmp = base64_encode(decoded_utf8);
+               ASSERT_EQUALS_STR("UTF-8 encoding", encoded_utf8, tmp);
+               free(tmp);
+
                END
 
 START(both_ways)
                char *enc = base64_encode(decoded);
                char *dec = base64_decode(enc);
-               ASSERT_EQUALS_STR("both ways", decoded, dec);
+               ASSERT_EQUALS_STR("both ways DEC", decoded, dec);
+               ASSERT_EQUALS_STR("both ways ENC", encoded, enc);
+               free(dec);
+               free(enc);
                END
 
 START(big)
@@ -88,6 +111,7 @@ Suite *test_base64(const char title[]) {
        tcase_add_checked_fixture(core, setup, teardown);
        tcase_add_test(core, decode);
        tcase_add_test(core, encode);
+       tcase_add_test(core, utf8);
 
        suite_add_tcase(suite, core);
 
index 87e6da86133a4d8a9cbdd36b02b0c26cc8a50e89..d151fe3a5812baff413b6f217b75f977a9ed1fa0 100644 (file)
 
 #include "base64.h"
 
-#include <stdint.h>
-#include <stddef.h>
 #include <stdlib.h>
 #include <string.h>
 
-
-static char *create_dtable();
-
 static char 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', '+', '/' };
 
-static char *decoding_table = NULL;
-
-static int b64_mod_table[] = { 0, 2, 1 };
-
-static char *create_dtable() {
-       char *decoding_table = malloc(256 * sizeof(char));
-       for (int i = 0; i < 64; i++)
-               decoding_table[(unsigned char) encoding_table[i]] = i;
+static int decoding_table_flg = 0;
+static char decoding_table[256];
 
-       return decoding_table;
+static void init_dtable() {
+       if (!decoding_table_flg) {
+               for (int i = 0; i < 64; i++)
+                       decoding_table[(unsigned char) encoding_table[i]] = i;
+               decoding_table_flg = 1;
+       }
 }
 
 char *base64_encode(const char *data) {
        size_t input_length = strlen(data);
-       size_t output_length = 4 * ((input_length + 2) / 3) + 1;
+       size_t output_length = 4 * ((input_length + 2) / 3);
 
-       char *encoded_data = malloc(output_length);
-       if (encoded_data == NULL)
+       char *encoded_data = malloc(output_length + 1);
+       if (!encoded_data)
                return NULL;
 
        for (unsigned int i = 0, j = 0; i < input_length;) {
-               uint32_t octet_a = i < input_length ? (unsigned char) data[i++] : 0;
-               uint32_t octet_b = i < input_length ? (unsigned char) data[i++] : 0;
-               uint32_t octet_c = i < input_length ? (unsigned char) data[i++] : 0;
+               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;
 
-               uint32_t triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;
+               unsigned int triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;
 
                encoded_data[j++] = (char) encoding_table[(triple >> 3 * 6) & 0x3F];
                encoded_data[j++] = (char) encoding_table[(triple >> 2 * 6) & 0x3F];
@@ -66,39 +60,38 @@ char *base64_encode(const char *data) {
                encoded_data[j++] = (char) encoding_table[(triple >> 0 * 6) & 0x3F];
        }
 
-       for (unsigned int i = 0; (int) i < b64_mod_table[input_length % 3]; i++)
-               encoded_data[output_length - 2 - i] = '=';
+       if ((input_length % 3) > 0)
+               encoded_data[output_length - 1] = '=';
+       if ((input_length % 3) == 1)
+               encoded_data[output_length - 2] = '=';
 
-       encoded_data[output_length - 1] = '\0';
+       encoded_data[output_length] = '\0';
 
        return encoded_data;
 }
 
 char *base64_decode(const char *data) {
-       if (!decoding_table)
-               decoding_table=create_dtable();
+       init_dtable();
 
        size_t input_length = strlen(data);
-       size_t output_length = 4 * ((input_length + 2) / 3) + 1;
-
        if (input_length % 4 != 0)
                return NULL;
 
-       output_length = (input_length / 4 * 3) + 1;
-       if (data[input_length - 2] == '=')
+       size_t output_length = ((input_length / 4) * 3);
+       if (data[input_length - 1] == '=')
                output_length--;
-       if (data[input_length - 3] == '=')
+       if (data[input_length - 2] == '=')
                output_length--;
 
-       char *decoded_data = malloc(output_length);
-       if (decoded_data == NULL)
+       char *decoded_data = malloc(output_length + 1);
+       if (!decoded_data)
                return NULL;
 
        for (unsigned int i = 0, j = 0; i < input_length; i += 4) {
-               uint32_t sextet_a = 0;
-               uint32_t sextet_b = 0;
-               uint32_t sextet_c = 0;
-               uint32_t sextet_d = 0;
+               unsigned int sextet_a = 0;
+               unsigned int sextet_b = 0;
+               unsigned int sextet_c = 0;
+               unsigned int sextet_d = 0;
 
                if (data[i] != '=') {
                        sextet_a = decoding_table[(unsigned char) data[i + 0]];
@@ -107,18 +100,18 @@ char *base64_decode(const char *data) {
                        sextet_d = decoding_table[(unsigned char) data[i + 3]];
                }
 
-               uint32_t triple = (sextet_a << 3 * 6) + (sextet_b << 2 * 6)
+               unsigned int triple = (sextet_a << 3 * 6) + (sextet_b << 2 * 6)
                                + (sextet_c << 1 * 6) + (sextet_d << 0 * 6);
 
-               if (j + 1 < output_length)
+               if (j < output_length)
                        decoded_data[j++] = (char) ((triple >> 2 * 8) & 0xFF);
-               if (j + 1 < output_length)
+               if (j < output_length)
                        decoded_data[j++] = (char) ((triple >> 1 * 8) & 0xFF);
-               if (j + 1 < output_length)
+               if (j < output_length)
                        decoded_data[j++] = (char) ((triple >> 0 * 8) & 0xFF);
        }
 
-       decoded_data[output_length - 1] = '\0';
+       decoded_data[output_length] = '\0';
        return decoded_data;
 }