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 536
hex_encode_l(size_t l)
62
{
63 536
        return ((l << 1) + 1);
64
}
65
66
size_t
67 244
hex_decode_l(size_t l)
68
{
69 244
        return ((l + 1) >> 1);
70
}
71
72
static inline char
73 26440
hex2byte(const unsigned char hi, const unsigned char lo)
74
{
75 26440
        return ((hex_nibble[hi - '0'] << 4) | hex_nibble[lo - '0']);
76
}
77
78
ssize_t
79 496
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 496
        char *p = buf;
84 496
        const char *alphabet = hex_alphabet[0];
85
        size_t i;
86
87 496
        AN(buf);
88 496
        assert(enc == HEX);
89 496
        if (in == NULL || inlen == 0)
90 20
                return (0);
91 476
        if (buflen < hex_encode_l(inlen))
92 8
                return (-1);
93
94 468
        if (kase == UPPER)
95 124
                alphabet = hex_alphabet[1];
96
97 423976
        for (i = 0; i < inlen; i++) {
98 423508
                *p++ = alphabet[(in[i] & 0xf0) >> 4];
99 423508
                *p++ = alphabet[in[i] & 0x0f];
100 423508
        }
101
102 468
        return (p - buf);
103 496
}
104
105
ssize_t
106 504
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 504
        char *dest = buf;
110 504
        const char * const end = buf + buflen;
111
        const char *b, *s;
112 504
        unsigned char extranib = 0;
113 504
        size_t len = 0;
114
        int i;
115 504
        ssize_t r, consumed = 0;
116
117 504
        AN(buf);
118 504
        CHECK_OBJ_NOTNULL(strings, STRANDS_MAGIC);
119 504
        assert(dec == HEX);
120
121
        /*lint --e{801} goto used as defer pattern */
122
123 1240
        for (i = 0; i < strings->n; i++) {
124 756
                s = strings->p[i];
125
126 756
                if (s == NULL)
127 96
                        continue;
128 660
                b = s;
129 92552
                while (*s) {
130 91912
                        if (!isxdigit(*s++)) {
131 20
                                errno = EINVAL;
132 20
                                r = -1;
133 20
                                goto out;
134
                        }
135
                }
136 640
                len += s - b;
137 640
        }
138
139 484
        if (len == 0) {
140 24
                r = 0;
141 24
                goto out;
142
        }
143
144 460
        if (n >= 0 && len > (size_t)n)
145 144
                len = n;
146
147 460
        if (len & 1) {
148 52
                extranib = '0';
149 52
                len++;
150 52
        }
151
152 1104
        for (i = 0; len > 0 && i < strings->n; i++) {
153 648
                s = strings->p[i];
154
155 648
                if (s == NULL || *s == '\0')
156 128
                        continue;
157 520
                if (extranib) {
158 100
                        if (dest == end) {
159 0
                                errno = ENOMEM;
160 0
                                r = -1;
161 0
                                goto out;
162
                        }
163 100
                        *dest++ = hex2byte(extranib, *s++);
164 100
                        consumed++;
165 100
                        len -= 2;
166 100
                }
167 26860
                while (len >= 2 && *s && *(s+1)) {
168 26344
                        if (dest == end) {
169 4
                                errno = ENOMEM;
170 4
                                r = -1;
171 4
                                goto out;
172
                        }
173 26340
                        *dest++ = hex2byte(*s, *(s+1));
174 26340
                        consumed += 2;
175 26340
                        s += 2;
176 26340
                        len -= 2;
177
                }
178 516
                extranib = *s;
179 516
        }
180 456
        assert(dest <= end);
181 456
        r = dest - buf;
182
    out:
183 504
        if (consumedp)
184 200
                *consumedp = consumed;
185 504
        return (r);
186
}