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 1299
hex_encode_l(size_t l)
62
{
63 1299
        return ((l << 1) + 1);
64
}
65
66
size_t
67 610
hex_decode_l(size_t l)
68
{
69 610
        return ((l + 1) >> 1);
70
}
71
72
static inline char
73 65310
hex2byte(const unsigned char hi, const unsigned char lo)
74
{
75 65310
        return ((hex_nibble[hi - '0'] << 4) | hex_nibble[lo - '0']);
76
}
77
78
ssize_t
79 1200
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 1200
        char *p = buf;
84 1200
        const char *alphabet = hex_alphabet[0];
85
        size_t i;
86
87 1200
        AN(buf);
88 1200
        assert(enc == HEX);
89 1200
        if (in == NULL || inlen == 0)
90 50
                return (0);
91 1150
        if (buflen < hex_encode_l(inlen))
92 10
                return (-1);
93
94 1140
        if (kase == UPPER)
95 310
                alphabet = hex_alphabet[1];
96
97 76887
        for (i = 0; i < inlen; i++) {
98 75747
                *p++ = alphabet[(in[i] & 0xf0) >> 4];
99 75747
                *p++ = alphabet[in[i] & 0x0f];
100 75747
        }
101
102 1140
        return (p - buf);
103 1200
}
104
105
ssize_t
106 1180
hex_decode(const enum encoding dec, blob_dest_t buf,
107
    blob_len_t buflen, ssize_t n, VCL_STRANDS strings)
108
{
109 1180
        char *dest = buf;
110
        const char *b, *s;
111 1180
        unsigned char extranib = 0;
112 1180
        size_t len = 0;
113
        int i;
114
115 1180
        AN(buf);
116 1180
        AN(strings);
117 1180
        assert(dec == HEX);
118
119 2940
        for (i = 0; i < strings->n; i++) {
120 1810
                s = strings->p[i];
121
122 1810
                if (s == NULL)
123 240
                        continue;
124 1570
                b = s;
125 229720
                while (*s) {
126 228200
                        if (!isxdigit(*s++)) {
127 50
                                errno = EINVAL;
128 50
                                return (-1);
129
                        }
130
                }
131 1520
                len += s - b;
132 1520
        }
133
134 1130
        if (len == 0)
135 60
                return (0);
136 1070
        if (n >= 0 && len > (size_t)n)
137 360
                len = n;
138
139 1070
        if (((len+1) >> 1) > buflen) {
140 10
                errno = ENOMEM;
141 10
                return (-1);
142
        }
143 1060
        if (len & 1) {
144 130
                extranib = '0';
145 130
                len++;
146 130
        }
147
148 2590
        for (i = 0; len > 0 && i < strings->n; i++) {
149 1530
                s = strings->p[i];
150
151 1530
                if (s == NULL || *s == '\0')
152 320
                        continue;
153 1210
                if (extranib) {
154 250
                        *dest++ = hex2byte(extranib, *s++);
155 250
                        len -= 2;
156 250
                }
157 66270
                while (len >= 2 && *s && *(s+1)) {
158 65060
                        *dest++ = hex2byte(*s, *(s+1));
159 65060
                        s += 2;
160 65060
                        len -= 2;
161
                }
162 1210
                extranib = *s;
163 1210
        }
164 1060
        assert(dest <= buf + buflen);
165 1060
        return (dest - buf);
166 1180
}