varnish-cache/vmod/vmod_blob_hex.c
1
/*-
2
 * Copyright 2016 UPLEX - Nils Goroll Systemoptimierung
3
 * All rights reserved.
4
 *
5
 * Authors: Nils Goroll <nils.goroll@uplex.de>
6
 *          Geoffrey Simmons <geoffrey.simmons@uplex.de>
7
 *
8
 * SPDX-License-Identifier: BSD-2-Clause
9
 *
10
 * Redistribution and use in source and binary forms, with or without
11
 * modification, are permitted provided that the following conditions are met:
12
 * 1. Redistributions of source code must retain the above copyright notice,
13
 *    this list of conditions and the following disclaimer.
14
 * 2. Redistributions in binary form must reproduce the above copyright notice,
15
 *    this list of conditions and the following disclaimer in the documentation
16
 *    and/or other materials provided with the distribution.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
19
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
 * DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
22
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 *
29
 */
30
31
#include "config.h"
32
33
#include <ctype.h>
34
35
#include "vdef.h"
36
#include "vrt.h"
37
#include "vas.h"
38
39
#include "vmod_blob.h"
40
41
const char hex_alphabet[][16] = {
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 2599
hex_encode_l(size_t l)
62
{
63 2599
        return ((l << 1) + 1);
64
}
65
66
size_t
67 1220
hex_decode_l(size_t l)
68
{
69 1220
        return ((l + 1) >> 1);
70
}
71
72
static inline char
73 130620
hex2byte(const unsigned char hi, const unsigned char lo)
74
{
75 130620
        return ((hex_nibble[hi - '0'] << 4) | hex_nibble[lo - '0']);
76
}
77
78
ssize_t
79 2400
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 2400
        char *p = buf;
84 2400
        const char *alphabet = hex_alphabet[0];
85
        size_t i;
86
87 2400
        AN(buf);
88 2400
        assert(enc == HEX);
89 2400
        if (in == NULL || inlen == 0)
90 100
                return (0);
91 2298
        if (buflen < hex_encode_l(inlen))
92 20
                return (-1);
93
94 2280
        if (kase == UPPER)
95 620
                alphabet = hex_alphabet[1];
96
97 153918
        for (i = 0; i < inlen; i++) {
98 151603
                *p++ = alphabet[(in[i] & 0xf0) >> 4];
99 151603
                *p++ = alphabet[in[i] & 0x0f];
100 151603
        }
101
102 2280
        return (p - buf);
103 2400
}
104
105
ssize_t
106 2360
hex_decode(const enum encoding dec, blob_dest_t buf,
107
    blob_len_t buflen, ssize_t n, VCL_STRANDS strings)
108
{
109 2360
        char *dest = buf;
110
        const char *b, *s;
111 2360
        unsigned char extranib = 0;
112 2360
        size_t len = 0;
113
        int i;
114
115 2360
        AN(buf);
116 2360
        AN(strings);
117 2360
        assert(dec == HEX);
118
119 5880
        for (i = 0; i < strings->n; i++) {
120 3620
                s = strings->p[i];
121
122 3620
                if (s == NULL)
123 480
                        continue;
124 3140
                b = s;
125 459440
                while (*s) {
126 456400
                        if (!isxdigit(*s++)) {
127 100
                                errno = EINVAL;
128 100
                                return (-1);
129
                        }
130
                }
131 3040
                len += s - b;
132 3040
        }
133
134 2260
        if (len == 0)
135 120
                return (0);
136 2140
        if (n >= 0 && len > (size_t)n)
137 720
                len = n;
138
139 2140
        if (((len+1) >> 1) > buflen) {
140 20
                errno = ENOMEM;
141 20
                return (-1);
142
        }
143 2120
        if (len & 1) {
144 260
                extranib = '0';
145 260
                len++;
146 260
        }
147
148 5180
        for (i = 0; len > 0 && i < strings->n; i++) {
149 3060
                s = strings->p[i];
150
151 3060
                if (s == NULL || *s == '\0')
152 640
                        continue;
153 2420
                if (extranib) {
154 500
                        *dest++ = hex2byte(extranib, *s++);
155 500
                        len -= 2;
156 500
                }
157 132540
                while (len >= 2 && *s && *(s+1)) {
158 130120
                        *dest++ = hex2byte(*s, *(s+1));
159 130120
                        s += 2;
160 130120
                        len -= 2;
161
                }
162 2420
                extranib = *s;
163 2420
        }
164 2120
        assert(dest <= buf + buflen);
165 2120
        return (dest - buf);
166 2360
}