aboutsummaryrefslogtreecommitdiff
path: root/lib/base64.c
diff options
context:
space:
mode:
authorDaniel Stenberg <daniel@haxx.se>2005-02-22 12:10:30 +0000
committerDaniel Stenberg <daniel@haxx.se>2005-02-22 12:10:30 +0000
commit527f70e540b68bcdb338cd5a133bbf17daf0105a (patch)
tree1baed9cd34c4b5ff16f5c4e850a27325c5d2dbda /lib/base64.c
parent19f66c757573b4940295a83e08eeb86c878b1def (diff)
Curl_base64_decode() now returns an allocated buffer
Diffstat (limited to 'lib/base64.c')
-rw-r--r--lib/base64.c22
1 files changed, 16 insertions, 6 deletions
diff --git a/lib/base64.c b/lib/base64.c
index 7cb66c5c4..54e4ed5d7 100644
--- a/lib/base64.c
+++ b/lib/base64.c
@@ -76,10 +76,10 @@ static void decodeQuantum(unsigned char *dest, const char *src)
/*
* Curl_base64_decode()
*
- * Given a base64 string at src, decode it into the memory pointed to by
- * dest. Returns the length of the decoded data.
+ * Given a base64 string at src, decode it and return an allocated memory in
+ * the *outptr. Returns the length of the decoded data.
*/
-size_t Curl_base64_decode(const char *src, char *dest)
+size_t Curl_base64_decode(const char *src, unsigned char **outptr)
{
int length = 0;
int equalsTerm = 0;
@@ -87,6 +87,9 @@ size_t Curl_base64_decode(const char *src, char *dest)
int numQuantums;
unsigned char lastQuantum[3];
size_t rawlen=0;
+ unsigned char *newstr;
+
+ *outptr = NULL;
while((src[length] != '=') && src[length])
length++;
@@ -97,15 +100,22 @@ size_t Curl_base64_decode(const char *src, char *dest)
rawlen = (numQuantums * 3) - equalsTerm;
+ newstr = malloc(rawlen+1);
+ if(!newstr)
+ return 0;
+
+ *outptr = newstr;
+
for(i = 0; i < numQuantums - 1; i++) {
- decodeQuantum((unsigned char *)dest, src);
- dest += 3; src += 4;
+ decodeQuantum((unsigned char *)newstr, src);
+ newstr += 3; src += 4;
}
decodeQuantum(lastQuantum, src);
for(i = 0; i < 3 - equalsTerm; i++)
- dest[i] = lastQuantum[i];
+ newstr[i] = lastQuantum[i];
+ newstr[i] = 0; /* zero terminate */
return rawlen;
}