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