vinyl-cache/vmod/vmod_blob_url.c
0
/*-
1
 * Copyright 2015-2016,2026 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 "vdef.h"
33
#include "vrt.h"
34
#include "vas.h"
35
#include "miniobj.h"
36
37
#include "vmod_blob.h"
38
39
/* Decoder states */
40
enum state_e {
41
        NORMAL,
42
        PERCENT,  /* just read '%' */
43
        FIRSTNIB, /* just read the first nibble after '%' */
44
};
45
46
size_t
47 294
url_encode_l(size_t l)
48
{
49 294
        return ((l * 3) + 1);
50
}
51
52
size_t
53 1407
url_decode_l(size_t l)
54
{
55 1407
        return (l);
56
}
57
58
/*
59
 * Bitmap of unreserved characters according to RFC 3986 section 2.3
60
 * (locale-independent and cacheline friendly)
61
 */
62
static const uint8_t unreserved[] = {
63
        0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0xff, 0x03,
64
        0xfe, 0xff, 0xff, 0x87, 0xfe, 0xff, 0xff, 0x47,
65
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
66
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
67
};
68
69
static inline int
70 201642
isunreserved(const uint8_t c)
71
{
72 201642
        return (unreserved[c >> 3] & (1 << (c & 7)));
73
}
74
75
static inline int
76 277830
isoutofrange(const uint8_t c)
77
{
78 277830
        return (c < '0' || c > 'f');
79
}
80
81
ssize_t
82 1974
url_encode(const enum encoding enc, const enum case_e kase,
83
    blob_dest_t buf, blob_len_t buflen,
84
    blob_src_t in, blob_len_t inlen)
85
{
86 1974
        char *p = buf;
87 1974
        const char * const end = buf + buflen;
88 1974
        const char *alphabet = hex_alphabet[0];
89
        size_t i;
90
91 1974
        AN(buf);
92 1974
        assert(enc == URL);
93 1974
        if (in == NULL || inlen == 0)
94 63
                return (0);
95
96 1911
        if (kase == UPPER)
97 693
                alphabet = hex_alphabet[1];
98
99 203553
        for (i = 0; i < inlen; i++) {
100 201642
                if (isunreserved(in[i])) {
101 68607
                        if (p == end)
102 0
                                return (-1);
103 68607
                        *p++ = in[i];
104 68607
                }
105
                else {
106 133035
                        if (p + 3 > end)
107 0
                                return (-1);
108 133035
                        *p++ = '%';
109 133035
                        *p++ = alphabet[(in[i] & 0xf0) >> 4];
110 133035
                        *p++ = alphabet[in[i] & 0x0f];
111
                }
112 201642
        }
113
114 1911
        return (p - buf);
115 1974
}
116
117
ssize_t
118 2478
url_decode(const enum encoding dec, blob_dest_t buf,
119
    blob_len_t buflen, ssize_t n, VCL_STRANDS strings, size_t *consumedp)
120
{
121 2478
        char *dest = buf;
122 2478
        const char * const end = buf + buflen;
123
        const char *s;
124 2478
        size_t len = SIZE_MAX;
125 2478
        size_t consumed = 0;
126 2478
        uint8_t nib = 0, nib2;
127 2478
        enum state_e state = NORMAL;
128
        int i;
129
        ssize_t r;
130
131 2478
        AN(buf);
132 2478
        CHECK_OBJ_NOTNULL(strings, STRANDS_MAGIC);
133 2478
        assert(dec == URL);
134
135
        /*lint --e{801} goto used as defer pattern */
136
137 2478
        if (n >= 0)
138 1113
                len = n;
139
140 5670
        for (i = 0; len > 0 && i < strings->n; i++) {
141 3339
                s = strings->p[i];
142
143 3339
                if (s == NULL || *s == '\0')
144 672
                        continue;
145 492261
                while (*s && len) {
146 489741
                        if (dest == end) {
147 21
                                errno = ENOMEM;
148 21
                                r = -1;
149 21
                                goto out;
150
                        }
151 489720
                        switch (state) {
152
                        case NORMAL:
153 211890
                                if (*s == '%')
154 139104
                                        state = PERCENT;
155
                                else
156 72786
                                        *dest++ = *s;
157 211890
                                break;
158
                        case PERCENT:
159 138999
                                if (isoutofrange(*s) ||
160 138936
                                    (nib = hex_nibble[*s - '0']) == ILL) {
161 63
                                        errno = EINVAL;
162 63
                                        r = -1;
163 63
                                        goto out;
164
                                }
165 138936
                                state = FIRSTNIB;
166 138936
                                break;
167
                        case FIRSTNIB:
168 138831
                                if (isoutofrange(*s) ||
169 138768
                                    (nib2 = hex_nibble[*s - '0']) == ILL) {
170 63
                                        errno = EINVAL;
171 63
                                        r = -1;
172 63
                                        goto out;
173
                                }
174 138768
                                *dest++ = (nib << 4) | nib2;
175 138768
                                nib = 0;
176 138768
                                state = NORMAL;
177 138768
                                break;
178
                        default:
179 0
                                WRONG("illegal URL decode state");
180 0
                        }
181 489594
                        consumed++;
182 489594
                        s++;
183 489594
                        len--;
184
                }
185 2520
        }
186 4662
        if (state != NORMAL) {
187 210
                errno = EINVAL;
188 210
                r = -1;
189 210
        }
190
        else
191 2121
                r = dest - buf;
192
193
    out:
194 2478
        if (consumedp)
195 1302
                *consumedp = consumed;
196 2478
        assert(dest <= end);
197 2478
        return (r);
198
}