improve decode time
atob() has been replaced with a lookup table, removing previous conditionals and function calls necessary to decode.
This commit is contained in:
parent
debab62506
commit
aacf2324c7
3 changed files with 44 additions and 40 deletions
76
encode.c
76
encode.c
|
@ -2,15 +2,35 @@
|
||||||
|
|
||||||
#define PADDING '='
|
#define PADDING '='
|
||||||
|
|
||||||
unsigned char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
|
static
|
||||||
"abcdefghijklmnopqrstuvwxyz" \
|
unsigned char b64toascii[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
|
||||||
"0123456789" \
|
"abcdefghijklmnopqrstuvwxyz" \
|
||||||
"+/";
|
"0123456789" \
|
||||||
|
"+/";
|
||||||
|
|
||||||
|
static
|
||||||
|
unsigned char b64urltoascii[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
|
||||||
|
"abcdefghijklmnopqrstuvwxyz" \
|
||||||
|
"0123456789" \
|
||||||
|
"-_";
|
||||||
|
|
||||||
|
static
|
||||||
|
unsigned char asciitob64[] = {
|
||||||
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
|
0, 0, 0, 62, 0, 62, 0, 63, 52, 53,
|
||||||
|
54, 55, 56, 57, 58, 59, 60, 61, 0, 0,
|
||||||
|
0, 0, 0, 0, 0, 0, 1, 2, 3, 4,
|
||||||
|
5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
|
||||||
|
15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
|
||||||
|
25, 0, 0, 0, 0, 63, 0, 26, 27, 28,
|
||||||
|
29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
|
||||||
|
39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
|
||||||
|
49, 50, 51
|
||||||
|
};
|
||||||
|
|
||||||
unsigned char b64url[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
|
|
||||||
"abcdefghijklmnopqrstuvwxyz" \
|
|
||||||
"0123456789" \
|
|
||||||
"-_";
|
|
||||||
|
|
||||||
int
|
int
|
||||||
encode(unsigned char *op, int olen, unsigned char *sp, int url)
|
encode(unsigned char *op, int olen, unsigned char *sp, int url)
|
||||||
|
@ -20,7 +40,7 @@ encode(unsigned char *op, int olen, unsigned char *sp, int url)
|
||||||
unsigned char *sbeg;
|
unsigned char *sbeg;
|
||||||
unsigned char *tend;
|
unsigned char *tend;
|
||||||
|
|
||||||
table = (!url) ? b64 : b64url;
|
table = (!url) ? b64toascii : b64urltoascii;
|
||||||
|
|
||||||
sbeg = sp;
|
sbeg = sp;
|
||||||
tend = op + olen - (olen % 3);
|
tend = op + olen - (olen % 3);
|
||||||
|
@ -52,52 +72,36 @@ encode(unsigned char *op, int olen, unsigned char *sp, int url)
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
decode(unsigned char *sp, int slen, unsigned char *op, int url)
|
decode(unsigned char *sp, int slen, unsigned char *op)
|
||||||
{
|
{
|
||||||
int tmp, b, atob(int c, int url);
|
int tmp, b;
|
||||||
unsigned char *obeg;
|
unsigned char *obeg;
|
||||||
unsigned char *qend;
|
unsigned char *qend;
|
||||||
|
|
||||||
obeg = op;
|
obeg = op;
|
||||||
qend = sp + slen - (slen % 4);
|
qend = sp + slen - (slen % 4);
|
||||||
while (sp < qend) {
|
while (sp < qend) {
|
||||||
tmp = atob(*sp++, url) << 2;
|
tmp = asciitob64[*sp++] << 2;
|
||||||
b = atob(*sp++, url);
|
b = asciitob64[*sp++];
|
||||||
*op++ = tmp + ((b & ~15) >> 4);
|
*op++ = tmp + ((b & ~15) >> 4);
|
||||||
tmp = (b & 15) << 4;
|
tmp = (b & 15) << 4;
|
||||||
b = atob(*sp++, url);
|
b = asciitob64[*sp++];
|
||||||
*op++ = tmp + ((b & ~3) >> 2);
|
*op++ = tmp + ((b & ~3) >> 2);
|
||||||
*op++ = ((b & 3) << 6) + atob(*sp++, url);
|
*op++ = ((b & 3) << 6) + asciitob64[*sp++];
|
||||||
}
|
}
|
||||||
switch (slen % 4) {
|
switch (slen % 4) {
|
||||||
case 3:
|
case 3:
|
||||||
tmp = atob(*sp++, url) << 2;
|
tmp = asciitob64[*sp++] << 2;
|
||||||
b = atob(*sp++, url);
|
b = asciitob64[*sp++];
|
||||||
*op++ = tmp + ((b & ~15) >> 4);
|
*op++ = tmp + ((b & ~15) >> 4);
|
||||||
tmp = (b & 15) << 4;
|
tmp = (b & 15) << 4;
|
||||||
*op++ = tmp + ((atob(*sp++, url) & ~3) >> 2);
|
*op++ = tmp + ((asciitob64[*sp++] & ~3) >> 2);
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
tmp = atob(*sp++, url) << 2;
|
tmp = asciitob64[*sp++] << 2;
|
||||||
*op++ = tmp + ((atob(*sp++, url) & ~15) >> 4);
|
*op++ = tmp + ((asciitob64[*sp++] & ~15) >> 4);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return op-obeg;
|
return op-obeg;
|
||||||
}
|
}
|
||||||
|
|
||||||
int atob(int c, int url)
|
|
||||||
{
|
|
||||||
if (c >= 'A' && c <= 'Z')
|
|
||||||
c -= 'A';
|
|
||||||
else if (c >= 'a' && c <= 'z')
|
|
||||||
c = c - 'a' + 26;
|
|
||||||
else if (c >= '0' && c <= '9')
|
|
||||||
c = c - '0' + 26 * 2;
|
|
||||||
else if (c == ((!url) ? '+' : '-'))
|
|
||||||
c = 62;
|
|
||||||
else
|
|
||||||
c = 63;
|
|
||||||
|
|
||||||
return c;
|
|
||||||
}
|
|
||||||
|
|
2
encode.h
2
encode.h
|
@ -5,6 +5,6 @@ int
|
||||||
encode(unsigned char *op, int olen, unsigned char *sp, int url);
|
encode(unsigned char *op, int olen, unsigned char *sp, int url);
|
||||||
|
|
||||||
int
|
int
|
||||||
decode(unsigned char *sp, int slen, unsigned char *op, int url);
|
decode(unsigned char *sp, int slen, unsigned char *op);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
6
main.c
6
main.c
|
@ -4,8 +4,8 @@
|
||||||
#include "output.h"
|
#include "output.h"
|
||||||
#include "encode.h"
|
#include "encode.h"
|
||||||
|
|
||||||
#define OCTETBUF 300
|
#define OCTETBUF 1500
|
||||||
#define SXTETBUF 400
|
#define SXTETBUF 2000
|
||||||
|
|
||||||
unsigned char obuf[OCTETBUF];
|
unsigned char obuf[OCTETBUF];
|
||||||
unsigned char sbuf[SXTETBUF];
|
unsigned char sbuf[SXTETBUF];
|
||||||
|
@ -54,7 +54,7 @@ main(int argc, char *argv[])
|
||||||
}
|
}
|
||||||
if (dec) {
|
if (dec) {
|
||||||
while ((n = readb((in) ? in : stdin, sbuf, SXTETBUF))) {
|
while ((n = readb((in) ? in : stdin, sbuf, SXTETBUF))) {
|
||||||
n = decode(sbuf, n, obuf, url);
|
n = decode(sbuf, n, obuf);
|
||||||
fwrite(obuf, sizeof(*obuf), n, (out) ? out : stdout);
|
fwrite(obuf, sizeof(*obuf), n, (out) ? out : stdout);
|
||||||
}
|
}
|
||||||
} else if (wrp) {
|
} else if (wrp) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue