Code Coverage: vinyl-cache/vmod/vmod_blob_url.c
 
82217aa4fc7094b3610340078afaf78cd95a8ec9
 
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
url_encode_l(size_t l)
48 14
{
49 14
        return ((l * 3) + 1);
50 14
}
51
52
size_t
53
url_decode_l(size_t l)
54 67
{
55 67
        return (l);
56 67
}
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
isunreserved(const uint8_t c)
71 9602
{
72 9602
        return (unreserved[c >> 3] & (1 << (c & 7)));
73 9602
}
74
75
static inline int
76
isoutofrange(const uint8_t c)
77 13230
{
78 13230
        return (c < '0' || c > 'f');
79 13230
}
80
81
ssize_t
82
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 94
{
86 94
        char *p = buf;
87 94
        const char * const end = buf + buflen;
88 94
        const char *alphabet = hex_alphabet[0];
89 94
        size_t i;
90
91 94
        AN(buf);
92 94
        assert(enc == URL);
93 94
        if (in == NULL || inlen == 0)
94 3
                return (0);
95
96 91
        if (kase == UPPER)
97 33
                alphabet = hex_alphabet[1];
98
99 9693
        for (i = 0; i < inlen; i++) {
100 9602
                if (isunreserved(in[i])) {
101 3267
                        if (p == end)
102 0
                                return (-1);
103 3267
                        *p++ = in[i];
104 3267
                }
105 6335
                else {
106 6335
                        if (p + 3 > end)
107 0
                                return (-1);
108 6335
                        *p++ = '%';
109 6335
                        *p++ = alphabet[(in[i] & 0xf0) >> 4];
110 6335
                        *p++ = alphabet[in[i] & 0x0f];
111 6335
                }
112 9602
        }
113
114 91
        return (p - buf);
115 91
}
116
117
ssize_t
118
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 118
{
121 118
        char *dest = buf;
122 118
        const char * const end = buf + buflen;
123 118
        const char *s;
124 118
        size_t len = SIZE_MAX;
125 118
        size_t consumed = 0;
126 118
        uint8_t nib = 0, nib2;
127 118
        enum state_e state = NORMAL;
128 118
        int i;
129 118
        ssize_t r;
130
131 118
        AN(buf);
132 118
        CHECK_OBJ_NOTNULL(strings, STRANDS_MAGIC);
133 118
        assert(dec == URL);
134
135
        /*lint --e{801} goto used as defer pattern */
136
137 118
        if (n >= 0)
138 53
                len = n;
139
140 270
        for (i = 0; len > 0 && i < strings->n; i++) {
141 159
                s = strings->p[i];
142
143 159
                if (s == NULL || *s == '\0')
144 32
                        continue;
145 23441
                while (*s && len) {
146 23321
                        if (dest == end) {
147 1
                                errno = ENOMEM;
148 1
                                r = -1;
149 1
                                goto out;
150 1
                        }
151 23320
                        switch (state) {
152 10090
                        case NORMAL:
153 10090
                                if (*s == '%')
154 6624
                                        state = PERCENT;
155 3466
                                else
156 3466
                                        *dest++ = *s;
157 10090
                                break;
158 6619
                        case PERCENT:
159 6619
                                if (isoutofrange(*s) ||
160 6619
                                    (nib = hex_nibble[*s - '0']) == ILL) {
161 3
                                        errno = EINVAL;
162 3
                                        r = -1;
163 3
                                        goto out;
164 3
                                }
165 6616
                                state = FIRSTNIB;
166 6616
                                break;
167 6611
                        case FIRSTNIB:
168 6611
                                if (isoutofrange(*s) ||
169 6611
                                    (nib2 = hex_nibble[*s - '0']) == ILL) {
170 3
                                        errno = EINVAL;
171 3
                                        r = -1;
172 3
                                        goto out;
173 3
                                }
174 6608
                                *dest++ = (nib << 4) | nib2;
175 6608
                                nib = 0;
176 6608
                                state = NORMAL;
177 6608
                                break;
178 0
                        default:
179 0
                                WRONG("illegal URL decode state");
180 23320
                        }
181 23314
                        consumed++;
182 23314
                        s++;
183 23314
                        len--;
184 23314
                }
185 127
        }
186 111
        if (state != NORMAL) {
187 10
                errno = EINVAL;
188 10
                r = -1;
189 10
        }
190 101
        else
191 101
                r = dest - buf;
192
193 118
    out:
194 118
        if (consumedp)
195 62
                *consumedp = consumed;
196 118
        assert(dest <= end);
197 118
        return (r);
198 118
}