Commit f16546e7 authored by Geoff Simmons's avatar Geoff Simmons

encapsulate en-/decode into static functions in the main source

parent 253e7c8e
......@@ -42,18 +42,6 @@ static const struct vmod_priv const null_blob[1] =
}
};
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);
}
static inline VCL_STRING
concat_va(struct ws *ws, const char *p, va_list ap)
{
......@@ -121,9 +109,53 @@ decode_l_va(enum encoding dec, const char * const p, va_list ap)
}
}
static inline ssize_t __match_proto__(decode_f)
decode(const enum encoding dec, char *restrict const buf,
const size_t maxlen, const char *restrict const p, va_list ap)
{
switch(dec) {
case IDENTITY:
return id_decode(dec, buf, maxlen, p, ap);
default:
WRONG("Illegal decoding");
}
}
static inline ssize_t __match_proto__(encode_f)
encode(const enum encoding enc, char *restrict const buf, const size_t maxlen,
const char *restrict const in, const size_t inlen)
{
switch(enc) {
case IDENTITY:
return id_encode(enc, buf, maxlen, in, inlen);
case BASE64:
case BASE64URL:
case BASE64URLNOPAD:
return base64_encode(enc, buf, maxlen, in, inlen);
case HEX:
case HEXLC:
return hex_encode(enc, buf, maxlen, in, inlen);
default:
WRONG("Illegal encoding");
}
}
/* 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);
}
/* Functions */
VCL_BLOB
VCL_BLOB __match_proto__(td_convert_decode)
vmod_decode(VRT_CTX, VCL_ENUM decs, const char *p, ...) {
enum encoding dec = parse_encoding(decs);
va_list ap;
......@@ -133,8 +165,6 @@ vmod_decode(VRT_CTX, VCL_ENUM decs, const char *p, ...) {
ssize_t len;
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
assert(dec != _INVALID);
assert(dec == IDENTITY); // XXX
snap = WS_Snapshot(ctx->ws);
if ((b = WS_Alloc(ctx->ws, sizeof(struct vmod_priv))) == NULL)
......@@ -146,7 +176,7 @@ vmod_decode(VRT_CTX, VCL_ENUM decs, const char *p, ...) {
buf = wb_buf(&wb);
va_start(ap, p);
len = id_decode(dec, buf, wb_space(&wb), p, ap);
len = decode(dec, buf, wb_space(&wb), p, ap);
va_end(ap);
if (len == -1) {
......@@ -167,7 +197,7 @@ vmod_decode(VRT_CTX, VCL_ENUM decs, const char *p, ...) {
return b;
}
VCL_STRING
VCL_STRING __match_proto__(td_convert_encode)
vmod_encode(VRT_CTX, VCL_ENUM encs, VCL_BLOB b) {
enum encoding enc = parse_encoding(encs);
struct wb_s wb;
......@@ -179,25 +209,7 @@ vmod_encode(VRT_CTX, VCL_ENUM encs, VCL_BLOB b) {
if (wb_create(ctx->ws, &wb) == NULL)
return NULL;
switch(enc) {
case IDENTITY:
len = id_encode(enc, wb_buf(&wb), wb_space(&wb), b->priv,
b->len);
break;
case BASE64:
case BASE64URL:
case BASE64URLNOPAD:
len = base64_encode(enc, wb_buf(&wb), wb_space(&wb), b->priv,
b->len);
break;
case HEX:
case HEXLC:
len = hex_encode(enc, wb_buf(&wb), wb_space(&wb), b->priv,
b->len);
break;
default:
WRONG("Illegal encoding");
}
len = encode(enc, wb_buf(&wb), wb_space(&wb), b->priv, b->len);
if (len == -1) {
wb_reset(&wb);
......@@ -243,13 +255,7 @@ vmod_transcode(VRT_CTX, VCL_ENUM decs, VCL_ENUM encs, const char *p, ...) {
char buf[l];
va_start(ap, p);
switch(dec) {
case IDENTITY:
len = id_decode(dec, buf, l, p, ap);
break;
default:
WRONG("Illegal decoding");
}
len = decode(dec, buf, l, p, ap);
va_end(ap);
if (len == -1)
......@@ -258,23 +264,7 @@ vmod_transcode(VRT_CTX, VCL_ENUM decs, VCL_ENUM encs, const char *p, ...) {
if (wb_create(ctx->ws, &wb) == NULL)
return NULL;
switch(enc) {
case IDENTITY:
len = id_encode(enc, wb_buf(&wb), wb_space(&wb), buf, len);
break;
case HEX:
case HEXLC:
len = hex_encode(enc, wb_buf(&wb), wb_space(&wb), buf, len);
break;
case BASE64:
case BASE64URL:
case BASE64URLNOPAD:
len = base64_encode(enc, wb_buf(&wb), wb_space(&wb), buf, len);
break;
default:
WRONG("Illegal encoding");
}
len = encode(enc, wb_buf(&wb), wb_space(&wb), buf, len);
if (len == -1) {
wb_reset(&wb);
return NULL;
......
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