Code Coverage: vinyl-cache/vmod/vmod_blob_hex.c
 
82217aa4fc7094b3610340078afaf78cd95a8ec9
 
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
hex_encode_l(size_t l)
62 134
{
63 134
        return ((l << 1) + 1);
64 134
}
65
66
size_t
67
hex_decode_l(size_t l)
68 61
{
69 61
        return ((l + 1) >> 1);
70 61
}
71
72
static inline char
73
hex2byte(const unsigned char hi, const unsigned char lo)
74 6686
{
75 6686
        return ((hex_nibble[hi - '0'] << 4) | hex_nibble[lo - '0']);
76 6686
}
77
78
ssize_t
79
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 124
{
83 124
        char *p = buf;
84 124
        const char *alphabet = hex_alphabet[0];
85 124
        size_t i;
86
87 124
        AN(buf);
88 124
        assert(enc == HEX);
89 124
        if (in == NULL || inlen == 0)
90 5
                return (0);
91 119
        if (buflen < hex_encode_l(inlen))
92 2
                return (-1);
93
94 117
        if (kase == UPPER)
95 31
                alphabet = hex_alphabet[1];
96
97 106003
        for (i = 0; i < inlen; i++) {
98 105886
                *p++ = alphabet[(in[i] & 0xf0) >> 4];
99 105886
                *p++ = alphabet[in[i] & 0x0f];
100 105886
        }
101
102 117
        return (p - buf);
103 119
}
104
105
ssize_t
106
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 133
{
109 133
        char *dest = buf;
110 133
        const char * const end = buf + buflen;
111 133
        const char *b;
112 133
        unsigned char extranib = 0;
113 133
        size_t len = 0;
114 133
        ssize_t r, consumed = 0;
115
116 133
        AN(buf);
117 133
        CHECK_OBJ_NOTNULL(strings, STRANDS_MAGIC);
118 133
        assert(dec == HEX);
119
120
        /*lint --e{801} goto used as defer pattern */
121
122 196
        STRANDS_FOREACH(s, strings) {
123 196
                if (s == NULL || *s == '\0')
124 52
                        continue;
125 144
                b = s;
126 23269
                while (*s) {
127 23130
                        if (!isxdigit(*s++)) {
128 5
                                errno = EINVAL;
129 5
                                r = -1;
130 5
                                goto out;
131 5
                        }
132 23130
                }
133 139
                len += s - b;
134 139
        }
135
136 128
        if (len == 0) {
137 6
                r = 0;
138 6
                goto out;
139 6
        }
140
141 122
        if (n >= 0 && len > (size_t)n)
142 36
                len = n;
143
144 122
        if (len & 1) {
145 13
                extranib = '0';
146 13
                len++;
147 13
        }
148
149 172
        STRANDS_FOREACH(s, strings) {
150 172
                if (len == 0)
151 3
                        break;
152 169
                if (s == NULL || *s == '\0')
153 32
                        continue;
154 137
                if (extranib) {
155 25
                        if (dest == end) {
156 0
                                errno = ENOMEM;
157 0
                                r = -1;
158 0
                                goto out;
159 0
                        }
160 25
                        *dest++ = hex2byte(extranib, *s++);
161 25
                        consumed++;
162 25
                        len -= 2;
163 25
                }
164 6798
                while (len >= 2 && *s && *(s+1)) {
165 6662
                        if (dest == end) {
166 1
                                errno = ENOMEM;
167 1
                                r = -1;
168 1
                                goto out;
169 1
                        }
170 6661
                        *dest++ = hex2byte(*s, *(s+1));
171 6661
                        consumed += 2;
172 6661
                        s += 2;
173 6661
                        len -= 2;
174 6661
                }
175 136
                extranib = *s;
176 136
        }
177 121
        assert(dest <= end);
178 121
        r = dest - buf;
179 133
    out:
180 133
        if (consumedp)
181 50
                *consumedp = consumed;
182 133
        return (r);
183 121
}