summaryrefslogtreecommitdiff
path: root/src/mir_core/http.cpp
diff options
context:
space:
mode:
authorGeorge Hazan <george.hazan@gmail.com>2013-07-01 21:25:22 +0000
committerGeorge Hazan <george.hazan@gmail.com>2013-07-01 21:25:22 +0000
commit0c8a910aab2206fbdde108b6a116a1e2cc964ae5 (patch)
tree26f1bf1ffd42808d5a3c09d617bafb66d654d347 /src/mir_core/http.cpp
parent14b001eb1f01efb4c32753431522bb1271c557a6 (diff)
fix for incomplete buffers in mir_base64_encode
git-svn-id: http://svn.miranda-ng.org/main/trunk@5207 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
Diffstat (limited to 'src/mir_core/http.cpp')
-rw-r--r--src/mir_core/http.cpp20
1 files changed, 9 insertions, 11 deletions
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]; }
}