From a0fa658e43635bf703701568eb24bf1003ec1b21 Mon Sep 17 00:00:00 2001 From: Sangyoon Jang Date: Thu, 19 Mar 2015 13:39:30 +0900 Subject: Fix bundle_decode g_base64_decode() decodes null-terminated string, but the result of bundle_encode() is raw data(not null-terminated). for decode raw data, it should use g_base64_decode_step(). Change-Id: Ia3d9cda70002cc0b2b53b8e315e0d109893e5d32 Signed-off-by: Sangyoon Jang --- src/bundle.c | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/bundle.c b/src/bundle.c index 6f8618d..81ac13b 100644 --- a/src/bundle.c +++ b/src/bundle.c @@ -517,6 +517,8 @@ bundle_decode(const bundle_raw *r, const int data_size) bundle *b; bundle_raw *p_r; unsigned char *d_str; + gint state = 0; + guint save = 0; unsigned int d_len_raw; unsigned char *d_r; unsigned int d_len; @@ -534,8 +536,25 @@ bundle_decode(const bundle_raw *r, const int data_size) return NULL; } - /* base 64 decode of input string*/ - d_str = g_base64_decode((char *)r, &d_len_raw); + /* base 64 decode of input string + * Since base64 encodes 3 bytes in 4 chars (+3 may be needed in case of non-zero state) + * refer to: https://developer.gnome.org/glib/stable/glib-Base64-Encoding.html#g-base64-decode-step + */ + d_str = malloc((data_size / 4) * 3 + 3); + if (unlikely(NULL == d_str)) { + set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY); + free(extract_cksum); + return NULL; + } + + d_len_raw = g_base64_decode_step((char *)r, data_size, d_str, &state, &save); + if (d_len_raw < CHECKSUM_LENGTH) { + set_last_result(BUNDLE_ERROR_INVALID_PARAMETER); + free(d_str); + free(extract_cksum); + return NULL; + } + /*extract checksum from the received string */ strncpy(extract_cksum, (const char *)d_str, CHECKSUM_LENGTH); /* compute checksum for the data */ -- cgit v1.2.3