diff options
author | George Hazan <george.hazan@gmail.com> | 2013-06-24 14:39:57 +0000 |
---|---|---|
committer | George Hazan <george.hazan@gmail.com> | 2013-06-24 14:39:57 +0000 |
commit | 0bcb3eb5504e5614c0a499269f0c8ea7e5b58127 (patch) | |
tree | 656651d915de94228203db0cce7201e398bdd5dd /src | |
parent | ff3e2e1dc74ea1f983a3b95773800de2ac5d7ea2 (diff) |
less shitty variant of mir_base64_decode
git-svn-id: http://svn.miranda-ng.org/main/trunk@5120 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
Diffstat (limited to 'src')
-rw-r--r-- | src/mir_core/http.cpp | 35 |
1 files changed, 15 insertions, 20 deletions
diff --git a/src/mir_core/http.cpp b/src/mir_core/http.cpp index 03c6f9b91f..5cc95c58bf 100644 --- a/src/mir_core/http.cpp +++ b/src/mir_core/http.cpp @@ -97,7 +97,7 @@ MIR_CORE_DLL(char*) mir_base64_encode(const BYTE *input, unsigned inputLen) /////////////////////////////////////////////////////////////////////////////////////////
-static int Base64DecodeTable[] =
+static BYTE Base64DecodeTable[] =
{
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
@@ -122,38 +122,33 @@ MIR_CORE_DLL(void*) mir_base64_decode(const char *input, unsigned *outputLen) if (input == NULL)
return NULL;
- size_t i = 0;
- BYTE chr[3], enc[4];
-
size_t length = strlen(input);
size_t nLength = (length / 4) * 3;
+ const char *stop = input + length;
char *output = (char *)mir_alloc(nLength+1);
char *p = output;
- while (i < length)
- {
- enc[0] = Base64DecodeTable[input[i++]];
- enc[1] = Base64DecodeTable[input[i++]];
- enc[2] = Base64DecodeTable[input[i++]];
- enc[3] = Base64DecodeTable[input[i++]];
-
- if (enc[0] == -1 || enc[1] == -1) break;
-
- chr[0] = (enc[0] << 2) | (enc[1] >> 4);
- chr[1] = ((enc[1] & 15) << 4) | (enc[2] >> 2);
- chr[2] = ((enc[2] & 3) << 6) | enc[3];
+ while (input < stop) {
+ BYTE e0 = Base64DecodeTable[*input++];
+ BYTE e1 = Base64DecodeTable[*input++];
+ BYTE e2 = Base64DecodeTable[*input++];
+ BYTE e3 = Base64DecodeTable[*input++];
- *p++ = chr[0];
+ if (e0 == (BYTE)-1 || e1 == (BYTE)-1)
+ break;
- if (enc[2] != -1) *p++ = chr[1];
- if (enc[3] != -1) *p++ = chr[2];
+ *p++ = (e0 << 2) | (e1 >> 4);
+ if (e2 != (BYTE)-1)
+ *p++ = ((e1 & 15) << 4) | (e2 >> 2);
+ if (e3 != (BYTE)-1)
+ *p++ = ((e2 & 3) << 6) | e3;
}
*p = 0;
if (outputLen != NULL)
- *outputLen = (unsigned)nLength;
+ *outputLen = p - output;
return output;
}
|