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
#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 130
hex_encode_l(size_t l)
62
{
63 130
        return ((l << 1) + 1);
64
}
65
66
size_t
67 61
hex_decode_l(size_t l)
68
{
69 61
        return ((l + 1) >> 1);
70
}
71
72
static inline char
73 6607
hex2byte(const unsigned char hi, const unsigned char lo)
74
{
75 6607
        return ((hex_nibble[hi - '0'] << 4) | hex_nibble[lo - '0']);
76
}
77
78
ssize_t
79 120
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 120
        char *p = buf;
84 120
        const char *alphabet = hex_alphabet[0];
85
        size_t i;
86
87 120
        AN(buf);
88 120
        assert(enc == HEX);
89 120
        if (in == NULL || inlen == 0)
90 5
                return (0);
91 115
        if (buflen < hex_encode_l(inlen))
92 1
                return (-1);
93
94 114
        if (kase == UPPER)
95 31
                alphabet = hex_alphabet[1];
96
97 7704
        for (i = 0; i < inlen; i++) {
98 7590
                *p++ = alphabet[(in[i] & 0xf0) >> 4];
99 7590
                *p++ = alphabet[in[i] & 0x0f];
100 7590
        }
101
102 114
        return (p - buf);
103 120
}
104
105
ssize_t
106 125
hex_decode(const enum encoding dec, blob_dest_t buf,
107
    blob_len_t buflen, ssize_t n, VCL_STRANDS strings)
108
{
109 125
        char *dest = buf;
110
        const char *b, *s;
111 125
        unsigned char extranib = 0;
112 125
        size_t len = 0;
113
        int i;
114
115 125
        AN(buf);
116 125
        CHECK_OBJ_NOTNULL(strings, STRANDS_MAGIC);
117 125
        assert(dec == HEX);
118
119 308
        for (i = 0; i < strings->n; i++) {
120 188
                s = strings->p[i];
121
122 188
                if (s == NULL)
123 24
                        continue;
124 164
                b = s;
125 23131
                while (*s) {
126 22972
                        if (!isxdigit(*s++)) {
127 5
                                errno = EINVAL;
128 5
                                return (-1);
129
                        }
130
                }
131 159
                len += s - b;
132 159
        }
133
134 120
        if (len == 0)
135 6
                return (0);
136 114
        if (n >= 0 && len > (size_t)n)
137 36
                len = n;
138
139 114
        if (((len+1) >> 1) > buflen) {
140 1
                errno = ENOMEM;
141 1
                return (-1);
142
        }
143 113
        if (len & 1) {
144 13
                extranib = '0';
145 13
                len++;
146 13
        }
147
148 273
        for (i = 0; len > 0 && i < strings->n; i++) {
149 160
                s = strings->p[i];
150
151 160
                if (s == NULL || *s == '\0')
152 32
                        continue;
153 128
                if (extranib) {
154 25
                        *dest++ = hex2byte(extranib, *s++);
155 25
                        len -= 2;
156 25
                }
157 6710
                while (len >= 2 && *s && *(s+1)) {
158 6582
                        *dest++ = hex2byte(*s, *(s+1));
159 6582
                        s += 2;
160 6582
                        len -= 2;
161
                }
162 128
                extranib = *s;
163 128
        }
164 113
        assert(dest <= buf + buflen);
165 113
        return (dest - buf);
166 125
}