diff --git a/encode.c b/encode.c index 08d50f8..aa5f7f2 100644 --- a/encode.c +++ b/encode.c @@ -33,75 +33,76 @@ unsigned char asciitob64[] = { int -encode(unsigned char *op, int olen, unsigned char *sp, int url) +encode(unsigned char *octbuf, int octbufsize, unsigned char *sxtbuf, int urlencoded) { - int tmp; unsigned char *table; - unsigned char *sbeg; - unsigned char *tend; + unsigned char *sxtbegin; + unsigned char *octleft; + int highorderbits; - table = (!url) ? b64toascii : b64urltoascii; + table = (!urlencoded) ? b64toascii : b64urltoascii; - sbeg = sp; - tend = op + olen - (olen % 3); - while (op < tend) { - *sp++ = table[(*op & ~3) >> 2]; - tmp = (*op++ & 3) << 4; - *sp++ = table[tmp + ((*op & ~15) >> 4)]; - tmp = (*op++ & 15) << 2; - *sp++ = table[tmp + ((*op & 192) >> 6)]; - *sp++ = table[*op++ & ~192]; + sxtbegin = sxtbuf; + octleft = octbuf + octbufsize - (octbufsize % 3); + while (octbuf < octleft) { + *sxtbuf++ = table[(*octbuf & ~3) >> 2]; + highorderbits = (*octbuf++ & 3) << 4; + *sxtbuf++ = table[highorderbits + ((*octbuf & ~15) >> 4)]; + highorderbits = (*octbuf++ & 15) << 2; + *sxtbuf++ = table[highorderbits + ((*octbuf & 192) >> 6)]; + *sxtbuf++ = table[*octbuf++ & ~192]; } - switch (olen % 3) { + switch (octbufsize % 3) { case 2: - *sp++ = table[(*op & ~3) >> 2]; - tmp = (*op++ & 3) << 4; - *sp++ = table[tmp + ((*op & ~15) >> 4)]; - *sp++ = table[(*op & 15) << 2]; - *sp++ = PADDING; + *sxtbuf++ = table[(*octbuf & ~3) >> 2]; + highorderbits = (*octbuf++ & 3) << 4; + *sxtbuf++ = table[highorderbits + ((*octbuf & ~15) >> 4)]; + *sxtbuf++ = table[(*octbuf & 15) << 2]; + *sxtbuf++ = PADDING; break; case 1: - *sp++ = table[(*op & ~3) >> 2]; - *sp++ = table[(*op & 3) << 4]; - *sp++ = PADDING; - *sp++ = PADDING; + *sxtbuf++ = table[(*octbuf & ~3) >> 2]; + *sxtbuf++ = table[(*octbuf & 3) << 4]; + *sxtbuf++ = PADDING; + *sxtbuf++ = PADDING; break; } - return sp-sbeg; + return sxtbuf-sxtbegin; } int -decode(unsigned char *sp, int slen, unsigned char *op) +decode(unsigned char *sxtbuf, int sxtbufsize, unsigned char *octbuf) { - int tmp, b; - unsigned char *obeg; - unsigned char *qend; + unsigned char *octbegin; + unsigned char *sxtleft; + int highorderbits; + int loworderbits; - obeg = op; - qend = sp + slen - (slen % 4); - while (sp < qend) { - tmp = asciitob64[*sp++] << 2; - b = asciitob64[*sp++]; - *op++ = tmp + ((b & ~15) >> 4); - tmp = (b & 15) << 4; - b = asciitob64[*sp++]; - *op++ = tmp + ((b & ~3) >> 2); - *op++ = ((b & 3) << 6) + asciitob64[*sp++]; + octbegin = octbuf; + sxtleft = sxtbuf + sxtbufsize - (sxtbufsize % 4); + while (sxtbuf < sxtleft) { + highorderbits = asciitob64[*sxtbuf++] << 2; + loworderbits = asciitob64[*sxtbuf++]; + *octbuf++ = highorderbits + ((loworderbits & ~15) >> 4); + highorderbits = (loworderbits & 15) << 4; + loworderbits = asciitob64[*sxtbuf++]; + *octbuf++ = highorderbits + ((loworderbits & ~3) >> 2); + *octbuf++ = ((loworderbits & 3) << 6) + asciitob64[*sxtbuf++]; } - switch (slen % 4) { + switch (sxtbufsize % 4) { case 3: - tmp = asciitob64[*sp++] << 2; - b = asciitob64[*sp++]; - *op++ = tmp + ((b & ~15) >> 4); - tmp = (b & 15) << 4; - *op++ = tmp + ((asciitob64[*sp++] & ~3) >> 2); + highorderbits = asciitob64[*sxtbuf++] << 2; + loworderbits = asciitob64[*sxtbuf++]; + *octbuf++ = highorderbits + ((loworderbits & ~15) >> 4); + highorderbits = (loworderbits & 15) << 4; + *octbuf++ = highorderbits + ((asciitob64[*sxtbuf++] & ~3) >> 2); break; case 2: - tmp = asciitob64[*sp++] << 2; - *op++ = tmp + ((asciitob64[*sp++] & ~15) >> 4); + highorderbits = asciitob64[*sxtbuf++] << 2; + *octbuf++ = highorderbits + ((asciitob64[*sxtbuf++] & ~15) >> 4); break; } - return op-obeg; + return octbuf-octbegin; } diff --git a/encode.h b/encode.h index 7e6f217..5c48d1a 100644 --- a/encode.h +++ b/encode.h @@ -2,9 +2,9 @@ #define ENCODE_H int -encode(unsigned char *op, int olen, unsigned char *sp, int url); +encode(unsigned char *octbuf, int octbufsize, unsigned char *sxtbuf, int urlencoded); int -decode(unsigned char *sp, int slen, unsigned char *op); +decode(unsigned char *sxtbuf, int sxtbufsize, unsigned char *octbuf); #endif