Commit 0811954b authored by Geoff Simmons's avatar Geoff Simmons

base64 now uses statically intitialized constant tables, event

function for initialization is no longer necessary
parent 1cc6c652
......@@ -9,6 +9,7 @@ libvmod_blobcode_la_SOURCES = \
vmod_blobcode.c \
vmod_blobcode.h \
id.c \
base64.h \
base64.c \
hex.c
......@@ -20,6 +21,8 @@ nodist_libvmod_blobcode_la_SOURCES = \
wb.h \
wb.c
base64.o: base64.c base64.h
parse_encoding.h: parse_encoding.c
parse_encoding.c: gen_enum_parse.pl
......
......@@ -28,41 +28,11 @@
#include <errno.h>
#include "vmod_blobcode.h"
#include "base64.h"
#include "vrt.h"
#include "vas.h"
#define ILLEGAL -1
#define PAD -2
struct b64_state_s {
unsigned magic;
#define B64_STATE_MAGIC 0xc0b64b64
unsigned char bytes[2];
ssize_t l;
};
static struct b64_alphabet {
char *b64;
char i64[256];
int padding;
} b64_alphabet[__MAX_ENCODING];
static void
alpha_init(struct b64_alphabet *alpha)
{
int i;
const char *p;
for (i = 0; i < 256; i++)
alpha->i64[i] = ILLEGAL;
for (p = alpha->b64, i = 0; *p; p++, i++)
alpha->i64[(int)*p] = (char)i;
if (alpha->padding)
alpha->i64[alpha->padding] = PAD;
}
static inline int
decode(char *restrict *restrict dest, char *restrict const buf,
const size_t maxlen, unsigned u, const int n)
......@@ -82,26 +52,6 @@ decode(char *restrict *restrict dest, char *restrict const buf,
return 1;
}
void
base64_init(void)
{
b64_alphabet[BASE64].b64 =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef"
"ghijklmnopqrstuvwxyz0123456789+/";
b64_alphabet[BASE64].padding = '=';
b64_alphabet[BASE64URL].b64 =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef"
"ghijklmnopqrstuvwxyz0123456789-_";
b64_alphabet[BASE64URL].padding = '=';
b64_alphabet[BASE64URLNOPAD].b64 =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef"
"ghijklmnopqrstuvwxyz0123456789-_";
b64_alphabet[BASE64URLNOPAD].padding = 0;
alpha_init(&b64_alphabet[BASE64]);
alpha_init(&b64_alphabet[BASE64URL]);
alpha_init(&b64_alphabet[BASE64URLNOPAD]);
}
/*
* Base64-encode *in (size: inlen) into the blob supplied as rblob. If
* there is insufficient space, it will bail out and return
......@@ -118,13 +68,14 @@ base64_encode(const enum encoding enc, char *restrict const buf,
const size_t maxlen, const char *restrict const inbuf,
const size_t inlength)
{
struct b64_alphabet *alpha = &b64_alphabet[enc];
const struct b64_alphabet *alpha = &b64_alphabet[enc];
char *p = buf;
const char *in = inbuf;
size_t outlen = maxlen, inlen = inlength;
unsigned char tmp[3], idx;
AN(buf);
AN(alpha);
if (in == NULL || inlen == 0)
return 0;
if ((enc != BASE64URLNOPAD && outlen < base64_encode_l(inlen))
......@@ -189,12 +140,13 @@ ssize_t
base64_decode(const enum encoding dec, char *restrict const buf,
const size_t maxlen, const char *restrict const p, va_list ap)
{
struct b64_alphabet *alpha = &b64_alphabet[dec];
const struct b64_alphabet *alpha = &b64_alphabet[dec];
char *dest = buf;
unsigned u = 0, term = 0;
int n = 0;
AN(buf);
AN(alpha);
for (const char *s = p; s != vrt_magic_string_end;
s = va_arg(ap, const char *)) {
......@@ -208,7 +160,7 @@ base64_decode(const enum encoding dec, char *restrict const buf,
while (n < 4) {
char b = alpha->i64[(unsigned) *s++];
u <<= 6;
if (b == ILLEGAL) {
if (b == ILL) {
errno = EINVAL;
return -1;
}
......
/*-
* Copyright 2015-2016 UPLEX - Nils Goroll Systemoptimierung
* All rights reserved.
*
* Authors: Nils Goroll <nils.goroll@uplex.de>
* Geoffrey Simmons <geoffrey.simmons@uplex.de>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "vmod_blobcode.h"
#define ILL -1
#define PAD -2
static const struct b64_alphabet {
const char *b64;
const int8_t i64[256];
const int padding;
} b64_alphabet[] = {
[BASE64] = {
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef"
"ghijklmnopqrstuvwxyz0123456789+/",
{
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
ILL, ILL, ILL, 62, ILL, ILL, ILL, 63, /* +, - */
52, 53, 54, 55, 56, 57, 58, 59, /* 0 -7 */
60, 61, ILL, ILL, ILL, PAD, ILL, ILL, /* 8, 9, = */
ILL, 0, 1, 2, 3, 4, 5, 6, /* A - G */
7, 8, 9, 10, 11, 12, 13, 14, /* H - O */
15, 16, 17, 18, 19, 20, 21, 22, /* P - W */
23, 24, 25, ILL, ILL, ILL, ILL, ILL, /* X, Y, Z */
ILL, 26, 27, 28, 29, 30, 31, 32, /* a - g */
33, 34, 35, 36, 37, 38, 39, 40, /* h - o */
41, 42, 43, 44, 45, 46, 47, 48, /* p - w */
49, 50, 51, ILL, ILL, ILL, ILL, ILL, /* x, y, z */
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
},
'='
},
[BASE64URL] = {
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef"
"ghijklmnopqrstuvwxyz0123456789-_",
{
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
ILL, ILL, ILL, ILL, ILL, 62, ILL, ILL, /* - */
52, 53, 54, 55, 56, 57, 58, 59, /* 0 -7 */
60, 61, ILL, ILL, ILL, PAD, ILL, ILL, /* 8, 9, = */
ILL, 0, 1, 2, 3, 4, 5, 6, /* A - G */
7, 8, 9, 10, 11, 12, 13, 14, /* H - O */
15, 16, 17, 18, 19, 20, 21, 22, /* P - W */
23, 24, 25, ILL, ILL, ILL, ILL, 63, /* X-Z, _ */
ILL, 26, 27, 28, 29, 30, 31, 32, /* a - g */
33, 34, 35, 36, 37, 38, 39, 40, /* h - o */
41, 42, 43, 44, 45, 46, 47, 48, /* p - w */
49, 50, 51, ILL, ILL, ILL, ILL, ILL, /* x, y, z */
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
},
'='
},
[BASE64URLNOPAD] = {
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef"
"ghijklmnopqrstuvwxyz0123456789-_",
{
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
ILL, ILL, ILL, ILL, ILL, 62, ILL, ILL, /* - */
52, 53, 54, 55, 56, 57, 58, 59, /* 0 -7 */
60, 61, ILL, ILL, ILL, ILL, ILL, ILL, /* 8, 9 */
ILL, 0, 1, 2, 3, 4, 5, 6, /* A - G */
7, 8, 9, 10, 11, 12, 13, 14, /* H - O */
15, 16, 17, 18, 19, 20, 21, 22, /* P - W */
23, 24, 25, ILL, ILL, ILL, ILL, 63, /* X-Z, _ */
ILL, 26, 27, 28, 29, 30, 31, 32, /* a - g */
33, 34, 35, 36, 37, 38, 39, 40, /* h - o */
41, 42, 43, 44, 45, 46, 47, 48, /* p - w */
49, 50, 51, ILL, ILL, ILL, ILL, ILL, /* x, y, z */
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
},
0
},
};
......@@ -177,19 +177,6 @@ err_decode(VRT_CTX, const char *enc)
}
}
/* init / event handler */
int __match_proto__(vmod_event_f)
event(VRT_CTX, struct vmod_priv *priv, enum vcl_event_e event)
{
(void) priv;
if (event != VCL_EVENT_LOAD)
return 0;
base64_init();
return (0);
}
/* Objects */
VCL_VOID __match_proto__(td_blobcode_blob__init)
......
......@@ -107,7 +107,6 @@ encode_f id_encode;
decode_f id_decode;
/* base64.c */
void base64_init(void);
encode_f base64_encode;
decode_f base64_decode;
......
......@@ -134,8 +134,6 @@ byte. For example::
= blobcode.encode(IDENTITY, blobcode.decode(HEX,
req.http.First + req.http.Second));
$Event event
$Function BLOB decode(ENUM {IDENTITY, BASE64, BASE64URL, BASE64URLNOPAD,
HEX} decoding, STRING_LIST encoded)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment