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

Change b64_*() to return the number of chars written to the target buffer

Changed order of arguments.
parent ed36c383
No related branches found
No related tags found
No related merge requests found
...@@ -42,23 +42,23 @@ ...@@ -42,23 +42,23 @@
static const char * base64alphabet = static const char * base64alphabet =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
char * b64_decode(char *target, const char *source, size_t tlen, size_t slen) int b64_decode(char *target, size_t tlen, const char *source size_t slen)
{ {
const char *read; const char *inp;
char *write; char *outp;
char *tend; char *outend;
const char *send; const char *inend;
int bits=0; int bits=0;
int working=0; int working=0;
char * i; char * i;
write=target; outp=target;
read=source; inp=source;
tend=target+tlen; outend=target+tlen;
send=source+slen; inend=source+slen;
for(;write<tend && read<send;read++) { for(;outp<outend && inp<inend;inp++) {
working<<=6; working<<=6;
i=strchr(base64alphabet,(char)*read); i=strchr(base64alphabet,(char)*inp);
if(i==NULL) { if(i==NULL) {
break; break;
} }
...@@ -66,18 +66,18 @@ char * b64_decode(char *target, const char *source, size_t tlen, size_t slen) ...@@ -66,18 +66,18 @@ char * b64_decode(char *target, const char *source, size_t tlen, size_t slen)
working |= (i-base64alphabet); working |= (i-base64alphabet);
bits+=6; bits+=6;
if(bits>8) { if(bits>8) {
*(write++)=(char)((working&(0xFF<<(bits-8)))>>(bits-8)); *(outp++)=(char)((working&(0xFF<<(bits-8)))>>(bits-8));
bits-=8; bits-=8;
} }
} }
if(bits) if(bits)
*(write++)=(char)((working&(0xFF<<(bits-8)))>>(bits-8)); *(outp++)=(char)((working&(0xFF<<(bits-8)))>>(bits-8));
if(write == tend) { if(outp == outend) {
*(--write)=0; *(--outp)=0;
return(NULL); return(-1);
} }
*write=0; *outp=0;
return(target); return(outp-target);
} }
static int add_char(char *pos, char ch, int done, char *end) static int add_char(char *pos, char ch, int done, char *end)
...@@ -92,68 +92,68 @@ static int add_char(char *pos, char ch, int done, char *end) ...@@ -92,68 +92,68 @@ static int add_char(char *pos, char ch, int done, char *end)
return(0); return(0);
} }
char * b64_encode(char *target, const char *source, size_t tlen, size_t slen) { int b64_encode(char *target, size_t tlen, const char *source, size_t slen) {
const char *read; const char *inp;
char *write; char *outp;
char *tend; char *outend;
const char *send; const char *inend;
char *tmpbuf=NULL; char *tmpbuf=NULL;
int done=0; int done=0;
char enc; char enc;
int buf=0; int buf=0;
read=source; inp=source;
if(source==target) { if(source==target) {
tmpbuf=(char *)malloc(tlen); tmpbuf=(char *)malloc(tlen);
if(tmpbuf==NULL) if(tmpbuf==NULL)
return(NULL); return(-1);
write=tmpbuf; outp=tmpbuf;
} }
else else
write=target; outp=target;
tend=write+tlen; outend=outp+tlen;
send=read+slen; inend=inp+slen;
for(;(read < send) && !done;) { for(;(inp < inend) && !done;) {
enc=*(read++); enc=*(inp++);
buf=(enc & 0x03)<<4; buf=(enc & 0x03)<<4;
enc=(enc&0xFC)>>2; enc=(enc&0xFC)>>2;
if(add_char(write++, enc, done, tend)) { if(add_char(outp++, enc, done, outend)) {
if(target==source) if(target==source)
free(tmpbuf); free(tmpbuf);
return(NULL); return(-1);
} }
enc=buf|((*read & 0xF0) >> 4); enc=buf|((*inp & 0xF0) >> 4);
if(add_char(write++, enc, done, tend)) { if(add_char(outp++, enc, done, outend)) {
if(target==source) if(target==source)
free(tmpbuf); free(tmpbuf);
return(NULL); return(-1);
} }
if(read==send) if(inp==inend)
done=1; done=1;
buf=(*(read++)<<2)&0x3C; buf=(*(inp++)<<2)&0x3C;
enc=buf|((*read & 0xC0)>>6); enc=buf|((*inp & 0xC0)>>6);
if(add_char(write++, enc, done, tend)) { if(add_char(outp++, enc, done, outend)) {
if(target==source) if(target==source)
free(tmpbuf); free(tmpbuf);
return(NULL); return(-1);
} }
if(read==send) if(inp==inend)
done=1; done=1;
enc=((int)*(read++))&0x3F; enc=((int)*(inp++))&0x3F;
if(add_char(write++, enc, done, tend)) { if(add_char(outp++, enc, done, outend)) {
if(target==source) if(target==source)
free(tmpbuf); free(tmpbuf);
return(NULL); return(-1);
} }
if(read==send) if(inp==inend)
done=1; done=1;
} }
if(write<tend) if(outp<outend)
*write=0; *outp=0;
if(target==source) { if(target==source) {
memcpy(target,tmpbuf,tlen); memcpy(target,tmpbuf,tlen);
free(tmpbuf); free(tmpbuf);
} }
return(target); return(outp-target);
} }
...@@ -35,5 +35,5 @@ ...@@ -35,5 +35,5 @@
* Note: If this box doesn't appear square, then you need to fix your tabs. * * Note: If this box doesn't appear square, then you need to fix your tabs. *
****************************************************************************/ ****************************************************************************/
char * b64_encode(char *target, const char *source, size_t tlen, size_t slen); int b64_encode(char *target, size_t tlen, const char *source, size_t slen);
char * b64_decode(char *target, const char *source, size_t tlen, size_t slen); int b64_decode(char *target, size_t tlen, const char *source, size_t slen);
...@@ -109,7 +109,7 @@ BOOL base64out(SOCKET socket, char * pathfile) ...@@ -109,7 +109,7 @@ BOOL base64out(SOCKET socket, char * pathfile)
return(FALSE); return(FALSE);
while(1) { while(1) {
bytesread=fread(in,1,57,fp); bytesread=fread(in,1,57,fp);
if((b64_encode(out,in,sizeof(out),bytesread)==NULL) if((b64_encode(out,sizeof(out),in,bytesread)==-1)
|| !sockprintf(socket,out)) { || !sockprintf(socket,out)) {
fclose(fp); fclose(fp);
return(FALSE); return(FALSE);
......
...@@ -1164,7 +1164,7 @@ static BOOL parse_headers(http_session_t * session) ...@@ -1164,7 +1164,7 @@ static BOOL parse_headers(http_session_t * session)
if(p==NULL) if(p==NULL)
break; break;
while(*p && *p<' ') p++; while(*p && *p<' ') p++;
b64_decode(p,p,strlen(p),strlen(p)); b64_decode(p,strlen(p),p,strlen(p));
SAFECOPY(session->req.auth,p); SAFECOPY(session->req.auth,p);
break; break;
case HEAD_LENGTH: case HEAD_LENGTH:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment