Curl_base64_decode() now returns an allocated buffer

This commit is contained in:
Daniel Stenberg 2005-02-22 12:10:30 +00:00
parent 19f66c7575
commit 527f70e540
6 changed files with 45 additions and 23 deletions

View File

@ -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;
}

View File

@ -23,5 +23,5 @@
* $Id$
***************************************************************************/
size_t Curl_base64_encode(const char *input, size_t size, char **str);
size_t Curl_base64_decode(const char *source, char *dest);
size_t Curl_base64_decode(const char *source, unsigned char **outptr);
#endif

View File

@ -166,12 +166,7 @@ int Curl_input_negotiate(struct connectdata *conn, char *header)
len = strlen(header);
if (len > 0) {
int rawlen;
input_token.length = (len+3)/4 * 3;
input_token.value = malloc(input_token.length);
if (input_token.value == NULL)
return ENOMEM;
rawlen = Curl_base64_decode(header, input_token.value);
int rawlen = Curl_base64_decode(header, &input_token.value);
if (rawlen < 0)
return -1;
input_token.length = rawlen;

View File

@ -123,17 +123,17 @@ CURLntlm Curl_input_ntlm(struct connectdata *conn,
32 (48) start of data block
*/
size_t size;
unsigned char *buffer = (unsigned char *)malloc(strlen(header));
if (buffer == NULL)
unsigned char *buffer;
size = Curl_base64_decode(header, &buffer);
if(!buffer)
return CURLNTLM_BAD;
size = Curl_base64_decode(header, (char *)buffer);
ntlm->state = NTLMSTATE_TYPE2; /* we got a type-2 */
if(size >= 48)
/* the nonce of interest is index [24 .. 31], 8 bytes */
memcpy(ntlm->nonce, &buffer[24], 8);
/* FIX: add an else here! */
/* at index decimal 20, there's a 32bit NTLM flag field */

View File

@ -199,6 +199,7 @@ krb4_auth(void *app_data, struct connectdata *conn)
{
int ret;
char *p;
unsigned char *ptr;
int len;
KTEXT_ST adat;
MSG_DAT msg_data;
@ -275,11 +276,17 @@ krb4_auth(void *app_data, struct connectdata *conn)
return AUTH_ERROR;
}
p += 5;
len = Curl_base64_decode(p, (char *)adat.dat);
if(len < 0) {
len = Curl_base64_decode(p, &ptr);
if(len > sizeof(adat.dat)-1) {
free(ptr);
len=0;
}
if(!len || !ptr) {
Curl_failf(data, "Failed to decode base64 from server");
return AUTH_ERROR;
}
memcpy((char *)adat.dat, ptr, len);
free(ptr);
adat.length = len;
ret = krb_rd_safe(adat.dat, adat.length, &d->key,
(struct sockaddr_in *)hisctladdr,
@ -321,6 +328,7 @@ CURLcode Curl_krb_kauth(struct connectdata *conn)
ssize_t nread;
int save;
CURLcode result;
unsigned char *ptr;
save = Curl_set_command_prot(conn, prot_private);
@ -346,12 +354,18 @@ CURLcode Curl_krb_kauth(struct connectdata *conn)
}
p += 2;
tmp = Curl_base64_decode(p, (char *)tkt.dat);
if(tmp < 0) {
tmp = Curl_base64_decode(p, &ptr);
if(len > sizeof(tkt.dat)-1) {
free(ptr);
len=0;
}
if(!len || !ptr) {
Curl_failf(conn->data, "Failed to decode base64 in reply.\n");
Curl_set_command_prot(conn, save);
return CURLE_FTP_WEIRD_SERVER_REPLY;
}
memcpy((char *)tkt.dat, ptr, tmp);
free(ptr);
tkt.length = tmp;
tktcopy.length = tkt.length;

View File

@ -61,11 +61,11 @@ char *appendstring(char *string, /* original string */
{
size_t len = strlen(buffer);
size_t needed_len = len + *stringlen + 1;
char buf64[256]; /* big enough? */
unsigned char *buf64=NULL;
if(base64) {
/* decode the given buffer first */
len = Curl_base64_decode(buffer, buf64); /* updated len */
len = Curl_base64_decode(buffer, &buf64); /* updated len */
buffer = buf64;
needed_len = len + *stringlen + 1; /* recalculate */
}
@ -87,6 +87,9 @@ char *appendstring(char *string, /* original string */
*stringlen += len;
string[*stringlen]=0;
if(buf64)
free(buf64);
return string;
}