| | 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 |
|
|
38 |
|
#include "vmod_blob.h" |
39 |
|
|
40 |
|
const char hex_alphabet[][16] = { |
41 |
|
"0123456789abcdef", |
42 |
|
"0123456789ABCDEF" |
43 |
|
}; |
44 |
|
|
45 |
|
/* |
46 |
|
* Shift the ASCII table over so that it begins at '0', and replace the |
47 |
|
* hex digits with their binary values. This fits all of the hex digits |
48 |
|
* into 55 bytes (cacheline friendly). |
49 |
|
*/ |
50 |
|
const uint8_t hex_nibble[] = { |
51 |
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, |
52 |
|
ILL, ILL, ILL, ILL, ILL, ILL, ILL, 10, 11, 12, |
53 |
|
13, 14, 15, ILL, ILL, ILL, ILL, ILL, ILL, ILL, |
54 |
|
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL, |
55 |
|
ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL, 10, |
56 |
|
11, 12, 13, 14, 15 |
57 |
|
}; |
58 |
|
|
59 |
|
size_t |
60 |
5198 |
hex_encode_l(size_t l) |
61 |
|
{ |
62 |
5198 |
return ((l << 1) + 1); |
63 |
|
} |
64 |
|
|
65 |
|
size_t |
66 |
2440 |
hex_decode_l(size_t l) |
67 |
|
{ |
68 |
2440 |
return ((l + 1) >> 1); |
69 |
|
} |
70 |
|
|
71 |
|
static inline char |
72 |
261720 |
hex2byte(const unsigned char hi, const unsigned char lo) |
73 |
|
{ |
74 |
261720 |
return ((hex_nibble[hi - '0'] << 4) | hex_nibble[lo - '0']); |
75 |
|
} |
76 |
|
|
77 |
|
ssize_t |
78 |
4799 |
hex_encode(const enum encoding enc, const enum case_e kase, |
79 |
|
blob_dest_t buf, blob_len_t buflen, |
80 |
|
blob_src_t in, blob_len_t inlen) |
81 |
|
{ |
82 |
4799 |
char *p = buf; |
83 |
4799 |
const char *alphabet = hex_alphabet[0]; |
84 |
|
size_t i; |
85 |
|
|
86 |
4799 |
AN(buf); |
87 |
4799 |
assert(enc == HEX); |
88 |
4799 |
if (in == NULL || inlen == 0) |
89 |
200 |
return (0); |
90 |
4599 |
if (buflen < hex_encode_l(inlen)) |
91 |
40 |
return (-1); |
92 |
|
|
93 |
4559 |
if (kase == UPPER) |
94 |
1240 |
alphabet = hex_alphabet[1]; |
95 |
|
|
96 |
307497 |
for (i = 0; i < inlen; i++) { |
97 |
302938 |
*p++ = alphabet[(in[i] & 0xf0) >> 4]; |
98 |
302938 |
*p++ = alphabet[in[i] & 0x0f]; |
99 |
302938 |
} |
100 |
|
|
101 |
4559 |
return (p - buf); |
102 |
4799 |
} |
103 |
|
|
104 |
|
ssize_t |
105 |
4920 |
hex_decode(const enum encoding dec, blob_dest_t buf, |
106 |
|
blob_len_t buflen, ssize_t n, VCL_STRANDS strings) |
107 |
|
{ |
108 |
4920 |
char *dest = buf; |
109 |
|
const char *b, *s; |
110 |
4920 |
unsigned char extranib = 0; |
111 |
4920 |
size_t len = 0; |
112 |
|
int i; |
113 |
|
|
114 |
4920 |
AN(buf); |
115 |
4920 |
AN(strings); |
116 |
4920 |
assert(dec == HEX); |
117 |
|
|
118 |
12160 |
for (i = 0; i < strings->n; i++) { |
119 |
7440 |
s = strings->p[i]; |
120 |
|
|
121 |
7440 |
if (s == NULL) |
122 |
960 |
continue; |
123 |
6480 |
b = s; |
124 |
920040 |
while (*s) { |
125 |
913760 |
if (!isxdigit(*s++)) { |
126 |
200 |
errno = EINVAL; |
127 |
200 |
return (-1); |
128 |
|
} |
129 |
|
} |
130 |
6280 |
len += s - b; |
131 |
6280 |
} |
132 |
|
|
133 |
4720 |
if (len == 0) |
134 |
240 |
return (0); |
135 |
4480 |
if (n >= 0 && len > (size_t)n) |
136 |
1440 |
len = n; |
137 |
|
|
138 |
4480 |
if (((len+1) >> 1) > buflen) { |
139 |
40 |
errno = ENOMEM; |
140 |
40 |
return (-1); |
141 |
|
} |
142 |
4440 |
if (len & 1) { |
143 |
520 |
extranib = '0'; |
144 |
520 |
len++; |
145 |
520 |
} |
146 |
|
|
147 |
10760 |
for (i = 0; len > 0 && i < strings->n; i++) { |
148 |
6320 |
s = strings->p[i]; |
149 |
|
|
150 |
6320 |
if (s == NULL || *s == '\0') |
151 |
1280 |
continue; |
152 |
5040 |
if (extranib) { |
153 |
1000 |
*dest++ = hex2byte(extranib, *s++); |
154 |
1000 |
len -= 2; |
155 |
1000 |
} |
156 |
265760 |
while (len >= 2 && *s && *(s+1)) { |
157 |
260720 |
*dest++ = hex2byte(*s, *(s+1)); |
158 |
260720 |
s += 2; |
159 |
260720 |
len -= 2; |
160 |
|
} |
161 |
5040 |
extranib = *s; |
162 |
5040 |
} |
163 |
4440 |
assert(dest <= buf + buflen); |
164 |
4440 |
return (dest - buf); |
165 |
4920 |
} |