varnish-cache/vmod/vmod_blob_url.c
0
/*-
1
 * Copyright 2015-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 "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 98
url_encode_l(size_t l)
48
{
49 98
        return ((l * 3) + 1);
50
}
51
52
size_t
53 469
url_decode_l(size_t l)
54
{
55 469
        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 67207
isunreserved(const uint8_t c)
71
{
72 67207
        return (unreserved[c >> 3] & (1 << (c & 7)));
73
}
74
75
static inline int
76 92596
isoutofrange(const uint8_t c)
77
{
78 92596
        return (c < '0' || c > 'f');
79
}
80
81
ssize_t
82 651
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 651
        char *p = buf;
87 651
        const char * const end = buf + buflen;
88 651
        const char *alphabet = hex_alphabet[0];
89
        size_t i;
90
91 651
        AN(buf);
92 651
        assert(enc == URL);
93 651
        if (in == NULL || inlen == 0)
94 21
                return (0);
95
96 630
        if (kase == UPPER)
97 224
                alphabet = hex_alphabet[1];
98
99 67837
        for (i = 0; i < inlen; i++) {
100 67207
                if (isunreserved(in[i])) {
101 22862
                        if (p == end)
102 0
                                return (-1);
103 22862
                        *p++ = in[i];
104 22862
                }
105
                else {
106 44345
                        if (p + 3 > end)
107 0
                                return (-1);
108 44345
                        *p++ = '%';
109 44345
                        *p++ = alphabet[(in[i] & 0xf0) >> 4];
110 44345
                        *p++ = alphabet[in[i] & 0x0f];
111
                }
112 67207
        }
113
114 630
        return (p - buf);
115 651
}
116
117
ssize_t
118 819
url_decode(const enum encoding dec, blob_dest_t buf,
119
    blob_len_t buflen, ssize_t n, VCL_STRANDS strings)
120
{
121 819
        char *dest = buf;
122 819
        const char * const end = buf + buflen;
123
        const char *s;
124 819
        size_t len = SIZE_MAX;
125 819
        uint8_t nib = 0, nib2;
126 819
        enum state_e state = NORMAL;
127
        int i;
128
129 819
        AN(buf);
130 819
        CHECK_OBJ_NOTNULL(strings, STRANDS_MAGIC);
131 819
        assert(dec == URL);
132
133 819
        if (n >= 0)
134 371
                len = n;
135
136 1876
        for (i = 0; len > 0 && i < strings->n; i++) {
137 1106
                s = strings->p[i];
138
139 1106
                if (s == NULL || *s == '\0')
140 224
                        continue;
141 164059
                while (*s && len) {
142 163226
                        switch (state) {
143
                        case NORMAL:
144 70630
                                if (*s == '%')
145 46361
                                        state = PERCENT;
146
                                else {
147 24269
                                        if (dest == end) {
148 7
                                                errno = ENOMEM;
149 7
                                                return (-1);
150
                                        }
151 24262
                                        *dest++ = *s;
152
                                }
153 70623
                                break;
154
                        case PERCENT:
155 46326
                                if (isoutofrange(*s) ||
156 46305
                                    (nib = hex_nibble[*s - '0']) == ILL) {
157 21
                                        errno = EINVAL;
158 21
                                        return (-1);
159
                                }
160 46305
                                state = FIRSTNIB;
161 46305
                                break;
162
                        case FIRSTNIB:
163 46270
                                if (dest == end) {
164 0
                                        errno = ENOMEM;
165 0
                                        return (-1);
166
                                }
167 46270
                                if (isoutofrange(*s) ||
168 46249
                                    (nib2 = hex_nibble[*s - '0']) == ILL) {
169 21
                                        errno = EINVAL;
170 21
                                        return (-1);
171
                                }
172 46249
                                *dest++ = (nib << 4) | nib2;
173 46249
                                nib = 0;
174 46249
                                state = NORMAL;
175 46249
                                break;
176
                        default:
177 0
                                WRONG("illegal URL decode state");
178 0
                        }
179 163177
                        s++;
180 163177
                        len--;
181
                }
182 833
        }
183 770
        if (state != NORMAL) {
184 70
                errno = EINVAL;
185 70
                return (-1);
186
        }
187 700
        assert(dest <= end);
188 700
        return (dest - buf);
189 819
}