varnish-cache/vmod/vmod_blob_hex.c
0
/*-
1
 * Copyright 2016 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
38
#include "vmod_blob.h"
39
40
const char hex_alphabet[][16] = {
41
        "0123456789abcdef",
42
        "0123456789ABCDEF"
43
};
44
45
/*
46
 * Shift the ASCII table over so that it begins at '0', and replace the
47
 * hex digits with their binary values. This fits all of the hex digits
48
 * into 55 bytes (cacheline friendly).
49
 */
50
const uint8_t hex_nibble[] = {
51
        0,   1,   2,   3,   4,   5,   6,   7,   8,   9,
52
        ILL, ILL, ILL, ILL, ILL, ILL, ILL, 10,  11,  12,
53
        13,  14,  15,  ILL, ILL, ILL, ILL, ILL, ILL, ILL,
54
        ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
55
        ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL, 10,
56
        11,  12,  13,  14,  15
57
};
58
59
size_t
60 3250
hex_encode_l(size_t l)
61
{
62 3250
        return ((l << 1) + 1);
63
}
64
65
size_t
66 1525
hex_decode_l(size_t l)
67
{
68 1525
        return ((l + 1) >> 1);
69
}
70
71
static inline char
72 163575
hex2byte(const unsigned char hi, const unsigned char lo)
73
{
74 163575
        return ((hex_nibble[hi - '0'] << 4) | hex_nibble[lo - '0']);
75
}
76
77
ssize_t
78 3000
hex_encode(const enum encoding enc, const enum case_e kase,
79
    blob_dest_t buf, blob_len_t buflen,
80
    blob_src_t in, blob_len_t inlen)
81
{
82 3000
        char *p = buf;
83 3000
        const char *alphabet = hex_alphabet[0];
84
        size_t i;
85
86 3000
        AN(buf);
87 3000
        assert(enc == HEX);
88 3000
        if (in == NULL || inlen == 0)
89 125
                return (0);
90 2875
        if (buflen < hex_encode_l(inlen))
91 25
                return (-1);
92
93 2850
        if (kase == UPPER)
94 775
                alphabet = hex_alphabet[1];
95
96 192391
        for (i = 0; i < inlen; i++) {
97 189541
                *p++ = alphabet[(in[i] & 0xf0) >> 4];
98 189541
                *p++ = alphabet[in[i] & 0x0f];
99 189541
        }
100
101 2850
        return (p - buf);
102 3000
}
103
104
ssize_t
105 3075
hex_decode(const enum encoding dec, blob_dest_t buf,
106
    blob_len_t buflen, ssize_t n, VCL_STRANDS strings)
107
{
108 3075
        char *dest = buf;
109
        const char *b, *s;
110 3075
        unsigned char extranib = 0;
111 3075
        size_t len = 0;
112
        int i;
113
114 3075
        AN(buf);
115 3075
        AN(strings);
116 3075
        assert(dec == HEX);
117
118 7600
        for (i = 0; i < strings->n; i++) {
119 4650
                s = strings->p[i];
120
121 4650
                if (s == NULL)
122 600
                        continue;
123 4050
                b = s;
124 575025
                while (*s) {
125 571100
                        if (!isxdigit(*s++)) {
126 125
                                errno = EINVAL;
127 125
                                return (-1);
128
                        }
129
                }
130 3925
                len += s - b;
131 3925
        }
132
133 2950
        if (len == 0)
134 150
                return (0);
135 2800
        if (n >= 0 && len > (size_t)n)
136 900
                len = n;
137
138 2800
        if (((len+1) >> 1) > buflen) {
139 25
                errno = ENOMEM;
140 25
                return (-1);
141
        }
142 2775
        if (len & 1) {
143 325
                extranib = '0';
144 325
                len++;
145 325
        }
146
147 6725
        for (i = 0; len > 0 && i < strings->n; i++) {
148 3950
                s = strings->p[i];
149
150 3950
                if (s == NULL || *s == '\0')
151 800
                        continue;
152 3150
                if (extranib) {
153 625
                        *dest++ = hex2byte(extranib, *s++);
154 625
                        len -= 2;
155 625
                }
156 166100
                while (len >= 2 && *s && *(s+1)) {
157 162950
                        *dest++ = hex2byte(*s, *(s+1));
158 162950
                        s += 2;
159 162950
                        len -= 2;
160
                }
161 3150
                extranib = *s;
162 3150
        }
163 2775
        assert(dest <= buf + buflen);
164 2775
        return (dest - buf);
165 3075
}