From aacf2324c7ba1893fb2eccd63c16208706910994 Mon Sep 17 00:00:00 2001 From: Eric Date: Thu, 29 May 2025 16:02:45 +0200 Subject: [PATCH] improve decode time atob() has been replaced with a lookup table, removing previous conditionals and function calls necessary to decode. --- encode.c | 76 +++++++++++++++++++++++++++++--------------------------- encode.h | 2 +- main.c | 6 ++--- 3 files changed, 44 insertions(+), 40 deletions(-) diff --git a/encode.c b/encode.c index 861bae9..08d50f8 100644 --- a/encode.c +++ b/encode.c @@ -2,15 +2,35 @@ #define PADDING '=' -unsigned char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \ - "abcdefghijklmnopqrstuvwxyz" \ - "0123456789" \ - "+/"; +static +unsigned char b64toascii[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \ + "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 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 *tend; - table = (!url) ? b64 : b64url; + table = (!url) ? b64toascii : b64urltoascii; sbeg = sp; tend = op + olen - (olen % 3); @@ -52,52 +72,36 @@ encode(unsigned char *op, int olen, unsigned char *sp, int url) } 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 *qend; obeg = op; qend = sp + slen - (slen % 4); while (sp < qend) { - tmp = atob(*sp++, url) << 2; - b = atob(*sp++, url); + tmp = asciitob64[*sp++] << 2; + b = asciitob64[*sp++]; *op++ = tmp + ((b & ~15) >> 4); tmp = (b & 15) << 4; - b = atob(*sp++, url); + b = asciitob64[*sp++]; *op++ = tmp + ((b & ~3) >> 2); - *op++ = ((b & 3) << 6) + atob(*sp++, url); + *op++ = ((b & 3) << 6) + asciitob64[*sp++]; } switch (slen % 4) { case 3: - tmp = atob(*sp++, url) << 2; - b = atob(*sp++, url); + tmp = asciitob64[*sp++] << 2; + b = asciitob64[*sp++]; *op++ = tmp + ((b & ~15) >> 4); tmp = (b & 15) << 4; - *op++ = tmp + ((atob(*sp++, url) & ~3) >> 2); + *op++ = tmp + ((asciitob64[*sp++] & ~3) >> 2); break; case 2: - tmp = atob(*sp++, url) << 2; - *op++ = tmp + ((atob(*sp++, url) & ~15) >> 4); + tmp = asciitob64[*sp++] << 2; + *op++ = tmp + ((asciitob64[*sp++] & ~15) >> 4); break; } 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; -} diff --git a/encode.h b/encode.h index 425038c..7e6f217 100644 --- a/encode.h +++ b/encode.h @@ -5,6 +5,6 @@ int encode(unsigned char *op, int olen, unsigned char *sp, int url); int -decode(unsigned char *sp, int slen, unsigned char *op, int url); +decode(unsigned char *sp, int slen, unsigned char *op); #endif diff --git a/main.c b/main.c index d7d8530..bc612d5 100644 --- a/main.c +++ b/main.c @@ -4,8 +4,8 @@ #include "output.h" #include "encode.h" -#define OCTETBUF 300 -#define SXTETBUF 400 +#define OCTETBUF 1500 +#define SXTETBUF 2000 unsigned char obuf[OCTETBUF]; unsigned char sbuf[SXTETBUF]; @@ -54,7 +54,7 @@ main(int argc, char *argv[]) } if (dec) { 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); } } else if (wrp) {