From 0c8a910aab2206fbdde108b6a116a1e2cc964ae5 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Mon, 1 Jul 2013 21:25:22 +0000 Subject: fix for incomplete buffers in mir_base64_encode git-svn-id: http://svn.miranda-ng.org/main/trunk@5207 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- src/mir_core/http.cpp | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) (limited to 'src/mir_core/http.cpp') diff --git a/src/mir_core/http.cpp b/src/mir_core/http.cpp index 5cc95c58bf..e0e2c8176b 100644 --- a/src/mir_core/http.cpp +++ b/src/mir_core/http.cpp @@ -68,25 +68,23 @@ MIR_CORE_DLL(char*) mir_base64_encode(const BYTE *input, unsigned inputLen) BYTE chr[3]; - size_t length = inputLen; - size_t nLength = 4 * ((length + 2) / 3) + 1; - - char *output = (char *)mir_alloc(nLength); + char *output = (char*)mir_alloc(4 * ((inputLen + 2) / 3) + 1); char *p = output; - for (size_t i=0; i < length; ) + for (unsigned i=0; i < inputLen; ) { + int rest = 0; chr[0] = input[i++]; - chr[1] = input[i++]; - chr[2] = input[i++]; + chr[1] = (i < inputLen) ? input[i++] : rest++, 0; + chr[2] = (i < inputLen) ? input[i++] : rest++, 0; *p++ = cb64[ chr[0] >> 2 ]; *p++ = cb64[ ((chr[0] & 0x03) << 4) | (chr[1] >> 4) ]; - BYTE b2 = ((chr[1] & 0x0F) << 2) | (chr[2] >> 6), - b3 = chr[2] & 0x3F; + int b2 = ((chr[1] & 0x0F) << 2) | (chr[2] >> 6), + b3 = chr[2] & 0x3F; - if (i - 2 >= length) { *p++ = '='; *p++ = '='; } - else if (i - 1 >= length) { *p++ = cb64[b2]; *p++ = '='; } + if (rest == 2) { *p++ = '='; *p++ = '='; } + else if (rest == 1) { *p++ = cb64[b2]; *p++ = '='; } else { *p++ = cb64[b2]; *p++ = cb64[b3]; } } -- cgit v1.2.3