| | varnish-cache/vmod/vmod_blob_hex.c |
| 0 |
|
/*- |
| 1 |
|
* Copyright 2016 UPLEX - Nils Goroll Systemoptimierung |
| 2 |
|
* All rights reserved. |
| 3 |
|
* |
| 4 |
|
* Authors: Nils Goroll <nils.goroll@uplex.de> |
| 5 |
|
* Geoffrey Simmons <geoffrey.simmons@uplex.de> |
| 6 |
|
* |
| 7 |
|
* SPDX-License-Identifier: BSD-2-Clause |
| 8 |
|
* |
| 9 |
|
* Redistribution and use in source and binary forms, with or without |
| 10 |
|
* modification, are permitted provided that the following conditions are met: |
| 11 |
|
* 1. Redistributions of source code must retain the above copyright notice, |
| 12 |
|
* this list of conditions and the following disclaimer. |
| 13 |
|
* 2. Redistributions in binary form must reproduce the above copyright notice, |
| 14 |
|
* this list of conditions and the following disclaimer in the documentation |
| 15 |
|
* and/or other materials provided with the distribution. |
| 16 |
|
* |
| 17 |
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY |
| 18 |
|
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 19 |
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 20 |
|
* DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY |
| 21 |
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 22 |
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 23 |
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 24 |
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 |
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 26 |
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 |
|
* |
| 28 |
|
*/ |
| 29 |
|
|
| 30 |
|
#include "config.h" |
| 31 |
|
|
| 32 |
|
#include <ctype.h> |
| 33 |
|
|
| 34 |
|
#include "vdef.h" |
| 35 |
|
#include "vrt.h" |
| 36 |
|
#include "vas.h" |
| 37 |
|
#include "miniobj.h" |
| 38 |
|
|
| 39 |
|
#include "vmod_blob.h" |
| 40 |
|
|
| 41 |
|
const char hex_alphabet[][17] = { |
| 42 |
|
"0123456789abcdef", |
| 43 |
|
"0123456789ABCDEF" |
| 44 |
|
}; |
| 45 |
|
|
| 46 |
|
/* |
| 47 |
|
* Shift the ASCII table over so that it begins at '0', and replace the |
| 48 |
|
* hex digits with their binary values. This fits all of the hex digits |
| 49 |
|
* into 55 bytes (cacheline friendly). |
| 50 |
|
*/ |
| 51 |
|
const uint8_t hex_nibble[] = { |
| 52 |
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, |
| 53 |
|
ILL, ILL, ILL, ILL, ILL, ILL, ILL, 10, 11, 12, |
| 54 |
|
13, 14, 15, ILL, ILL, ILL, ILL, ILL, ILL, ILL, |
| 55 |
|
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL, |
| 56 |
|
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL, 10, |
| 57 |
|
11, 12, 13, 14, 15 |
| 58 |
|
}; |
| 59 |
|
|
| 60 |
|
size_t |
| 61 |
5200 |
hex_encode_l(size_t l) |
| 62 |
|
{ |
| 63 |
5200 |
return ((l << 1) + 1); |
| 64 |
|
} |
| 65 |
|
|
| 66 |
|
size_t |
| 67 |
2440 |
hex_decode_l(size_t l) |
| 68 |
|
{ |
| 69 |
2440 |
return ((l + 1) >> 1); |
| 70 |
|
} |
| 71 |
|
|
| 72 |
|
static inline char |
| 73 |
264280 |
hex2byte(const unsigned char hi, const unsigned char lo) |
| 74 |
|
{ |
| 75 |
264280 |
return ((hex_nibble[hi - '0'] << 4) | hex_nibble[lo - '0']); |
| 76 |
|
} |
| 77 |
|
|
| 78 |
|
ssize_t |
| 79 |
4800 |
hex_encode(const enum encoding enc, const enum case_e kase, |
| 80 |
|
blob_dest_t buf, blob_len_t buflen, |
| 81 |
|
blob_src_t in, blob_len_t inlen) |
| 82 |
|
{ |
| 83 |
4800 |
char *p = buf; |
| 84 |
4800 |
const char *alphabet = hex_alphabet[0]; |
| 85 |
|
size_t i; |
| 86 |
|
|
| 87 |
4800 |
AN(buf); |
| 88 |
4800 |
assert(enc == HEX); |
| 89 |
4800 |
if (in == NULL || inlen == 0) |
| 90 |
200 |
return (0); |
| 91 |
4600 |
if (buflen < hex_encode_l(inlen)) |
| 92 |
40 |
return (-1); |
| 93 |
|
|
| 94 |
4560 |
if (kase == UPPER) |
| 95 |
1240 |
alphabet = hex_alphabet[1]; |
| 96 |
|
|
| 97 |
307642 |
for (i = 0; i < inlen; i++) { |
| 98 |
303082 |
*p++ = alphabet[(in[i] & 0xf0) >> 4]; |
| 99 |
303082 |
*p++ = alphabet[in[i] & 0x0f]; |
| 100 |
303082 |
} |
| 101 |
|
|
| 102 |
4560 |
return (p - buf); |
| 103 |
4800 |
} |
| 104 |
|
|
| 105 |
|
ssize_t |
| 106 |
5000 |
hex_decode(const enum encoding dec, blob_dest_t buf, |
| 107 |
|
blob_len_t buflen, ssize_t n, VCL_STRANDS strings) |
| 108 |
|
{ |
| 109 |
5000 |
char *dest = buf; |
| 110 |
|
const char *b, *s; |
| 111 |
5000 |
unsigned char extranib = 0; |
| 112 |
5000 |
size_t len = 0; |
| 113 |
|
int i; |
| 114 |
|
|
| 115 |
5000 |
AN(buf); |
| 116 |
5000 |
CHECK_OBJ_NOTNULL(strings, STRANDS_MAGIC); |
| 117 |
5000 |
assert(dec == HEX); |
| 118 |
|
|
| 119 |
12320 |
for (i = 0; i < strings->n; i++) { |
| 120 |
7520 |
s = strings->p[i]; |
| 121 |
|
|
| 122 |
7520 |
if (s == NULL) |
| 123 |
960 |
continue; |
| 124 |
6560 |
b = s; |
| 125 |
925240 |
while (*s) { |
| 126 |
918880 |
if (!isxdigit(*s++)) { |
| 127 |
200 |
errno = EINVAL; |
| 128 |
200 |
return (-1); |
| 129 |
|
} |
| 130 |
|
} |
| 131 |
6360 |
len += s - b; |
| 132 |
6360 |
} |
| 133 |
|
|
| 134 |
4800 |
if (len == 0) |
| 135 |
240 |
return (0); |
| 136 |
4560 |
if (n >= 0 && len > (size_t)n) |
| 137 |
1440 |
len = n; |
| 138 |
|
|
| 139 |
4560 |
if (((len+1) >> 1) > buflen) { |
| 140 |
40 |
errno = ENOMEM; |
| 141 |
40 |
return (-1); |
| 142 |
|
} |
| 143 |
4520 |
if (len & 1) { |
| 144 |
520 |
extranib = '0'; |
| 145 |
520 |
len++; |
| 146 |
520 |
} |
| 147 |
|
|
| 148 |
10920 |
for (i = 0; len > 0 && i < strings->n; i++) { |
| 149 |
6400 |
s = strings->p[i]; |
| 150 |
|
|
| 151 |
6400 |
if (s == NULL || *s == '\0') |
| 152 |
1280 |
continue; |
| 153 |
5120 |
if (extranib) { |
| 154 |
1000 |
*dest++ = hex2byte(extranib, *s++); |
| 155 |
1000 |
len -= 2; |
| 156 |
1000 |
} |
| 157 |
268400 |
while (len >= 2 && *s && *(s+1)) { |
| 158 |
263280 |
*dest++ = hex2byte(*s, *(s+1)); |
| 159 |
263280 |
s += 2; |
| 160 |
263280 |
len -= 2; |
| 161 |
|
} |
| 162 |
5120 |
extranib = *s; |
| 163 |
5120 |
} |
| 164 |
4520 |
assert(dest <= buf + buflen); |
| 165 |
4520 |
return (dest - buf); |
| 166 |
5000 |
} |