]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
[core] fix base64 decoded size when encoded string contains padding = 2618/head
authorSeven Du <dujinfang@x-y-t.cn>
Tue, 8 Oct 2024 16:34:22 +0000 (00:34 +0800)
committerSeven Du <dujinfang@x-y-t.cn>
Wed, 9 Oct 2024 14:53:00 +0000 (22:53 +0800)
src/switch_utils.c
tests/unit/switch_utils.c

index aa3fc74cae35f06935c7eea254b2a32aefe0b961..1293ca3fbdced69214e6055aca672fe847446f49 100644 (file)
@@ -1076,7 +1076,7 @@ SWITCH_DECLARE(switch_size_t) switch_b64_decode(const char *in, char *out, switc
                l64[(int) switch_b64_table[i]] = (char) i;
        }
 
-       for (ip = in; ip && *ip; ip++) {
+       for (ip = in; ip && *ip && (*ip != '='); ip++) {
                c = l64[(int) *ip];
                if (c == -1) {
                        continue;
index 1fc291ea96d8fdd64e7972f2a688110d1b20e9bf..391ec6e8e6dab1b4384250733956d6a93192776c 100644 (file)
@@ -80,6 +80,49 @@ FST_TEST_BEGIN(b64)
 }
 FST_TEST_END()
 
+FST_TEST_BEGIN(b64_pad2)
+{
+    switch_size_t size;
+    char str[] = {0, 0, 0, 0};
+    unsigned char b64_str[128];
+    char decoded_str[128];
+       int i;
+    switch_status_t status = switch_b64_encode((unsigned char *)str, sizeof(str), b64_str, sizeof(b64_str));
+    switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "b64_str: %s\n", b64_str);
+    fst_check(status == SWITCH_STATUS_SUCCESS);
+    fst_check_string_equals((const char *)b64_str, "AAAAAA==");
+
+    size = switch_b64_decode((const char *)b64_str, decoded_str, sizeof(decoded_str));
+    switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "decoded_str: %s\n", decoded_str);
+    fst_check_string_equals(decoded_str, str);
+    fst_check(size == sizeof(str) + 1);
+       for (i = 0; i < sizeof(str); i++) {
+               fst_check(decoded_str[i] == str[i]);
+       }
+}
+FST_TEST_END()
+
+FST_TEST_BEGIN(b64_pad1)
+{
+    switch_size_t size;
+    char str[] = {0, 0, 0, 0, 0};
+    unsigned char b64_str[128];
+    char decoded_str[128];
+       int i;
+    switch_status_t status = switch_b64_encode((unsigned char *)str, sizeof(str), b64_str, sizeof(b64_str));
+    switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "b64_str: %s\n", b64_str);
+    fst_check(status == SWITCH_STATUS_SUCCESS);
+    fst_check_string_equals((const char *)b64_str, "AAAAAAA=");
+
+    size = switch_b64_decode((const char *)b64_str, decoded_str, sizeof(decoded_str));
+    switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "decoded_str: %s\n", decoded_str);
+    fst_check_string_equals(decoded_str, str);
+    fst_check(size == sizeof(str) + 1);
+               for (i = 0; i < sizeof(str); i++) {
+               fst_check(decoded_str[i] == str[i]);
+       }
+}
+FST_TEST_END()
 
 FST_SUITE_END()