From a06dda4c4f3ebd68b381b09e2be152e8159988bb Mon Sep 17 00:00:00 2001 From: phk Date: Fri, 15 Sep 2006 19:25:06 +0000 Subject: [PATCH] Add a base64 decoder. git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@996 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- varnish-cache/include/varnishapi.h | 4 ++ varnish-cache/lib/libvarnishapi/Makefile.am | 1 + varnish-cache/lib/libvarnishapi/base64.c | 78 +++++++++++++++++++++ 3 files changed, 83 insertions(+) create mode 100644 varnish-cache/lib/libvarnishapi/base64.c diff --git a/varnish-cache/include/varnishapi.h b/varnish-cache/include/varnishapi.h index 87145bac..a160a7c5 100644 --- a/varnish-cache/include/varnishapi.h +++ b/varnish-cache/include/varnishapi.h @@ -7,6 +7,10 @@ #define V_DEAD __attribute__ ((noreturn)) +/* base64.c */ +void base64_init(void); +int base64_decode(char *d, unsigned dlen, const char *s); + /* shmlog.c */ typedef int vsl_handler(void *priv, unsigned tag, unsigned fd, unsigned len, unsigned spec, const char *ptr); #define VSL_S_CLIENT (1 << 0) diff --git a/varnish-cache/lib/libvarnishapi/Makefile.am b/varnish-cache/lib/libvarnishapi/Makefile.am index 0187acb7..d8681151 100644 --- a/varnish-cache/lib/libvarnishapi/Makefile.am +++ b/varnish-cache/lib/libvarnishapi/Makefile.am @@ -5,6 +5,7 @@ INCLUDES = -I$(top_srcdir)/include lib_LTLIBRARIES = libvarnishapi.la libvarnishapi_la_SOURCES = \ + base64.c \ shmlog.c libvarnishapi_la_CFLAGS = -include config.h diff --git a/varnish-cache/lib/libvarnishapi/base64.c b/varnish-cache/lib/libvarnishapi/base64.c new file mode 100644 index 00000000..5f2cf257 --- /dev/null +++ b/varnish-cache/lib/libvarnishapi/base64.c @@ -0,0 +1,78 @@ +/* $Id$ */ + +static const char *b64 = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + +static char i64[256]; + +void +base64_init(void) +{ + int i; + const char *p; + + for (i = 0; i < 256; i++) + i64[i] = -1; + for (p = b64, i = 0; *p; p++, i++) + i64[*p] = i; + i64['='] = 0; +} + +int +base64_decode(char *d, unsigned dlen, const char *s) +{ + unsigned u, v, l; + int i; + + l = 0; + while (*s) { + for (v = 0; v < 4; v++) { + if (!*s) + break; + i = i64[*s++]; + if (i < 0) + return (-1); + u <<= 6; + u |= i; + } + for (v = 0; v < 3; v++) { + if (l >= dlen - 1) + return (-1); + *d = (u >> 16) & 0xff; + u <<= 8; + l++; + d++; + } + } + printf("\n"); + *d = '\0'; + return (0); +} + +#ifdef TEST_DRIVER +#include + +const char *test1 = +"TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz" +"IHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg" +"dGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu" +"dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo" +"ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4="; + +int +main(int argc, char **argv) +{ + int i; + char buf[BUFSIZ]; + unsigned l; + + (void)argc; + (void)argv; + + base64_init(); + l = sizeof buf; + base64_decode(buf, &l, test1); + printf("%s\n", buf); + return (0); +} +#endif -- 2.39.5