vinyl-cache/vmod/vmod_blob_hex.c
0
/*-
1
 * Copyright 2016,2026 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 2814
hex_encode_l(size_t l)
62
{
63 2814
        return ((l << 1) + 1);
64
}
65
66
size_t
67 1281
hex_decode_l(size_t l)
68
{
69 1281
        return ((l + 1) >> 1);
70
}
71
72
static inline char
73 138810
hex2byte(const unsigned char hi, const unsigned char lo)
74
{
75 138810
        return ((hex_nibble[hi - '0'] << 4) | hex_nibble[lo - '0']);
76
}
77
78
ssize_t
79 2604
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 2604
        char *p = buf;
84 2604
        const char *alphabet = hex_alphabet[0];
85
        size_t i;
86
87 2604
        AN(buf);
88 2604
        assert(enc == HEX);
89 2604
        if (in == NULL || inlen == 0)
90 105
                return (0);
91 2499
        if (buflen < hex_encode_l(inlen))
92 42
                return (-1);
93
94 2457
        if (kase == UPPER)
95 651
                alphabet = hex_alphabet[1];
96
97 2225884
        for (i = 0; i < inlen; i++) {
98 2223427
                *p++ = alphabet[(in[i] & 0xf0) >> 4];
99 2223427
                *p++ = alphabet[in[i] & 0x0f];
100 2223427
        }
101
102 2457
        return (p - buf);
103 2604
}
104
105
ssize_t
106 2646
hex_decode(const enum encoding dec, blob_dest_t buf,
107
    blob_len_t buflen, ssize_t n, VCL_STRANDS strings, size_t *consumedp)
108
{
109 2646
        char *dest = buf;
110 2646
        const char * const end = buf + buflen;
111
        const char *b, *s;
112 2646
        unsigned char extranib = 0;
113 2646
        size_t len = 0;
114
        int i;
115 2646
        ssize_t r, consumed = 0;
116
117 2646
        AN(buf);
118 2646
        CHECK_OBJ_NOTNULL(strings, STRANDS_MAGIC);
119 2646
        assert(dec == HEX);
120
121
        /*lint --e{801} goto used as defer pattern */
122
123 6510
        for (i = 0; i < strings->n; i++) {
124 3969
                s = strings->p[i];
125
126 3969
                if (s == NULL)
127 504
                        continue;
128 3465
                b = s;
129 485898
                while (*s) {
130 482538
                        if (!isxdigit(*s++)) {
131 105
                                errno = EINVAL;
132 105
                                r = -1;
133 105
                                goto out;
134
                        }
135
                }
136 3360
                len += s - b;
137 3360
        }
138
139 2541
        if (len == 0) {
140 126
                r = 0;
141 126
                goto out;
142
        }
143
144 2415
        if (n >= 0 && len > (size_t)n)
145 756
                len = n;
146
147 2415
        if (len & 1) {
148 273
                extranib = '0';
149 273
                len++;
150 273
        }
151
152 5796
        for (i = 0; len > 0 && i < strings->n; i++) {
153 3402
                s = strings->p[i];
154
155 3402
                if (s == NULL || *s == '\0')
156 672
                        continue;
157 2730
                if (extranib) {
158 525
                        if (dest == end) {
159 0
                                errno = ENOMEM;
160 0
                                r = -1;
161 0
                                goto out;
162
                        }
163 525
                        *dest++ = hex2byte(extranib, *s++);
164 525
                        consumed++;
165 525
                        len -= 2;
166 525
                }
167 141015
                while (len >= 2 && *s && *(s+1)) {
168 138306
                        if (dest == end) {
169 21
                                errno = ENOMEM;
170 21
                                r = -1;
171 21
                                goto out;
172
                        }
173 138285
                        *dest++ = hex2byte(*s, *(s+1));
174 138285
                        consumed += 2;
175 138285
                        s += 2;
176 138285
                        len -= 2;
177
                }
178 2709
                extranib = *s;
179 2709
        }
180 2394
        assert(dest <= end);
181 2394
        r = dest - buf;
182
    out:
183 2646
        if (consumedp)
184 1050
                *consumedp = consumed;
185 2646
        return (r);
186
}