Commit edca76e2 authored by Geoff Simmons's avatar Geoff Simmons

some optimization for hex_decode()

parent 09f3a681
......@@ -28,6 +28,7 @@
#include <ctype.h>
#include <errno.h>
#include <stdint.h>
#include "vmod_convert.h"
......@@ -39,7 +40,19 @@ static const char hex_alphabet[][16] = {
"0123456789ABCDEF"
};
static char nibble[128];
/*
* Shift the ASCII table over so that it begins at '0', and replace the
* hex digits with their binary values. This fits all of the hex digits
* into 54 bytes (cacheline friendly).
*/
static const uint8_t nibble[] = {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 10, 11, 12,
13, 14, 15, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 10, 11,
12, 13, 14, 15
};
static inline int
isxstr(const char * const p, ssize_t *l)
......@@ -75,6 +88,12 @@ check(const char * const p, va_list ap)
return len;
}
static inline char
hex2byte(const unsigned char hi, const unsigned char lo)
{
return (nibble[hi - '0'] << 4) | nibble[lo - '0'];
}
static inline void
decode_str(const char *restrict const p, char *restrict *restrict dest,
unsigned char *restrict extranib)
......@@ -83,28 +102,15 @@ decode_str(const char *restrict const p, char *restrict *restrict dest,
char *d = *dest;
if (*extranib)
*d++ = (nibble[*extranib] << 4) | nibble[*s++];
*d++ = hex2byte(*extranib, *s++);
while (*s && *(s+1)) {
*d++ = (nibble[*s] << 4) | nibble[*(s+1)];
*d++ = hex2byte(*s, *(s+1));
s += 2;
}
*extranib = *s;
*dest += d - *dest;
}
void
hex_init(void)
{
for (int i = 0; i < 128; i++)
nibble[i] = -1;
for (char c = '0'; c <= '9'; c++)
nibble[(int) c] = c - '0';
for (char c = 'a'; c <= 'f'; c++)
nibble[(int) c] = c - 'a' + 10;
for (char c = 'A'; c <= 'F'; c++)
nibble[(int) c] = c - 'A' + 10;
}
ssize_t
hex_encode(const enum encoding enc, char *restrict const buf,
const size_t maxlen, const char *restrict const in,
......
......@@ -151,7 +151,6 @@ event(VRT_CTX, struct vmod_priv *priv, enum vcl_event_e event)
if (event != VCL_EVENT_LOAD)
return 0;
hex_init();
base64_init();
return (0);
}
......
......@@ -109,6 +109,5 @@ void base64_init(void);
encode_f base64_encode;
/* hex.c */
void hex_init(void);
encode_f hex_encode;
decode_f hex_decode;
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