diff --git a/src/switch_utils.c b/src/switch_utils.c index e6e4917b1e..e4e26a6eef 100644 --- a/src/switch_utils.c +++ b/src/switch_utils.c @@ -26,7 +26,7 @@ * Anthony Minessale II * Juan Jose Comellas * Seven Du - * + * Windy Wang * * switch_utils.c -- Compatibility and Helper Code * @@ -1081,7 +1081,7 @@ SWITCH_DECLARE(switch_size_t) switch_b64_decode(const char *in, char *out, switc while (l >= 8) { op[ol++] = (char) ((b >> (l -= 8)) % 256); - if (ol >= olen - 2) { + if (ol >= olen - 1) { goto end; } } diff --git a/tests/unit/switch_utils.c b/tests/unit/switch_utils.c index 72c5817b71..7b7fa3729a 100644 --- a/tests/unit/switch_utils.c +++ b/tests/unit/switch_utils.c @@ -23,7 +23,7 @@ * * Contributor(s): * Seven Du - * + * Windy Wang * * switch_utils.c -- tests switch_utils * @@ -63,6 +63,24 @@ FST_TEST_BEGIN(benchmark) } FST_TEST_END() +FST_TEST_BEGIN(b64) +{ + char *str = "ABC"; + unsigned char b64_str[6]; + char decoded_str[4]; + switch_status_t status = switch_b64_encode((unsigned char *)str, strlen(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, "QUJD"); + + switch_size_t 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 == 4); +} +FST_TEST_END() + + FST_SUITE_END() FST_MINCORE_END()