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 2080
hex_encode_l(size_t l)
61
{
62 2080
        return ((l << 1) + 1);
63
}
64
65
size_t
66 976
hex_decode_l(size_t l)
67
{
68 976
        return ((l + 1) >> 1);
69
}
70
71
static inline char
72 104688
hex2byte(const unsigned char hi, const unsigned char lo)
73
{
74 104688
        return ((hex_nibble[hi - '0'] << 4) | hex_nibble[lo - '0']);
75
}
76
77
ssize_t
78 1920
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 1920
        char *p = buf;
83 1920
        const char *alphabet = hex_alphabet[0];
84
        size_t i;
85
86 1920
        AN(buf);
87 1920
        assert(enc == HEX);
88 1920
        if (in == NULL || inlen == 0)
89 80
                return (0);
90 1840
        if (buflen < hex_encode_l(inlen))
91 16
                return (-1);
92
93 1824
        if (kase == UPPER)
94 496
                alphabet = hex_alphabet[1];
95
96 123124
        for (i = 0; i < inlen; i++) {
97 121300
                *p++ = alphabet[(in[i] & 0xf0) >> 4];
98 121300
                *p++ = alphabet[in[i] & 0x0f];
99 121300
        }
100
101 1824
        return (p - buf);
102 1920
}
103
104
ssize_t
105 1968
hex_decode(const enum encoding dec, blob_dest_t buf,
106
    blob_len_t buflen, ssize_t n, VCL_STRANDS strings)
107
{
108 1968
        char *dest = buf;
109
        const char *b, *s;
110 1968
        unsigned char extranib = 0;
111 1968
        size_t len = 0;
112
        int i;
113
114 1968
        AN(buf);
115 1968
        AN(strings);
116 1968
        assert(dec == HEX);
117
118 4864
        for (i = 0; i < strings->n; i++) {
119 2976
                s = strings->p[i];
120
121 2976
                if (s == NULL)
122 384
                        continue;
123 2592
                b = s;
124 368016
                while (*s) {
125 365504
                        if (!isxdigit(*s++)) {
126 80
                                errno = EINVAL;
127 80
                                return (-1);
128
                        }
129
                }
130 2512
                len += s - b;
131 2512
        }
132
133 1888
        if (len == 0)
134 96
                return (0);
135 1792
        if (n >= 0 && len > (size_t)n)
136 576
                len = n;
137
138 1792
        if (((len+1) >> 1) > buflen) {
139 16
                errno = ENOMEM;
140 16
                return (-1);
141
        }
142 1776
        if (len & 1) {
143 208
                extranib = '0';
144 208
                len++;
145 208
        }
146
147 4304
        for (i = 0; len > 0 && i < strings->n; i++) {
148 2528
                s = strings->p[i];
149
150 2528
                if (s == NULL || *s == '\0')
151 512
                        continue;
152 2016
                if (extranib) {
153 400
                        *dest++ = hex2byte(extranib, *s++);
154 400
                        len -= 2;
155 400
                }
156 106304
                while (len >= 2 && *s && *(s+1)) {
157 104288
                        *dest++ = hex2byte(*s, *(s+1));
158 104288
                        s += 2;
159 104288
                        len -= 2;
160
                }
161 2016
                extranib = *s;
162 2016
        }
163 1776
        assert(dest <= buf + buflen);
164 1776
        return (dest - buf);
165 1968
}