Skip to content
Snippets Groups Projects
Commit 9ba68b0a authored by deuce's avatar deuce
Browse files

Use alloca() instead of malloc()/free() for tempbuf

parent 0a566ccb
No related branches found
No related tags found
No related merge requests found
......@@ -110,7 +110,7 @@ int b64_encode(char *target, size_t tlen, const char *source, size_t slen) {
slen=strlen(source);
inp=source;
if(source==target) {
tmpbuf=(char *)malloc(tlen);
tmpbuf=(char *)alloca(tlen);
if(tmpbuf==NULL)
return(-1);
outp=tmpbuf;
......@@ -124,43 +124,29 @@ int b64_encode(char *target, size_t tlen, const char *source, size_t slen) {
enc=*(inp++);
buf=(enc & 0x03)<<4;
enc=(enc&0xFC)>>2;
if(add_char(outp++, enc, done, outend)) {
if(target==source)
free(tmpbuf);
if(add_char(outp++, enc, done, outend))
return(-1);
}
enc=buf|((*inp & 0xF0) >> 4);
if(add_char(outp++, enc, done, outend)) {
if(target==source)
free(tmpbuf);
if(add_char(outp++, enc, done, outend))
return(-1);
}
if(inp==inend)
done=1;
buf=(*(inp++)<<2)&0x3C;
enc=buf|((*inp & 0xC0)>>6);
if(add_char(outp++, enc, done, outend)) {
if(target==source)
free(tmpbuf);
if(add_char(outp++, enc, done, outend))
return(-1);
}
if(inp==inend)
done=1;
enc=((int)*(inp++))&0x3F;
if(add_char(outp++, enc, done, outend)) {
if(target==source)
free(tmpbuf);
if(add_char(outp++, enc, done, outend))
return(-1);
}
if(inp==inend)
done=1;
}
if(outp<outend)
*outp=0;
if(target==source) {
if(target==source)
memcpy(target,tmpbuf,tlen);
free(tmpbuf);
}
return(outp-target);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment