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
36
#include "vmod_blob.h"
37
38
/* Decoder states */
39
enum state_e {
40
        NORMAL,
41
        PERCENT,  /* just read '%' */
42
        FIRSTNIB, /* just read the first nibble after '%' */
43
};
44
45
size_t
46 350
url_encode_l(size_t l)
47
{
48 350
        return ((l * 3) + 1);
49
}
50
51
size_t
52 1675
url_decode_l(size_t l)
53
{
54 1675
        return (l);
55
}
56
57
/*
58
 * Bitmap of unreserved characters according to RFC 3986 section 2.3
59
 * (locale-independent and cacheline friendly)
60
 */
61
static const uint8_t unreserved[] = {
62
        0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0xff, 0x03,
63
        0xfe, 0xff, 0xff, 0x87, 0xfe, 0xff, 0xff, 0x47,
64
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
65
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
66
};
67
68
static inline int
69 240025
isunreserved(const uint8_t c)
70
{
71 240025
        return (unreserved[c >> 3] & (1 << (c & 7)));
72
}
73
74
static inline int
75 330700
isoutofrange(const uint8_t c)
76
{
77 330700
        return (c < '0' || c > 'f');
78
}
79
80
ssize_t
81 2325
url_encode(const enum encoding enc, const enum case_e kase,
82
    blob_dest_t buf, blob_len_t buflen,
83
    blob_src_t in, blob_len_t inlen)
84
{
85 2325
        char *p = buf;
86 2325
        const char * const end = buf + buflen;
87 2325
        const char *alphabet = hex_alphabet[0];
88
        size_t i;
89
90 2325
        AN(buf);
91 2325
        assert(enc == URL);
92 2325
        if (in == NULL || inlen == 0)
93 75
                return (0);
94
95 2250
        if (kase == UPPER)
96 800
                alphabet = hex_alphabet[1];
97
98 242275
        for (i = 0; i < inlen; i++) {
99 240025
                if (isunreserved(in[i])) {
100 81650
                        if (p == end)
101 0
                                return (-1);
102 81650
                        *p++ = in[i];
103 81650
                }
104
                else {
105 158375
                        if (p + 3 > end)
106 0
                                return (-1);
107 158375
                        *p++ = '%';
108 158375
                        *p++ = alphabet[(in[i] & 0xf0) >> 4];
109 158375
                        *p++ = alphabet[in[i] & 0x0f];
110
                }
111 240025
        }
112
113 2250
        return (p - buf);
114 2325
}
115
116
ssize_t
117 2925
url_decode(const enum encoding dec, blob_dest_t buf,
118
    blob_len_t buflen, ssize_t n, VCL_STRANDS strings)
119
{
120 2925
        char *dest = buf;
121 2925
        const char * const end = buf + buflen;
122
        const char *s;
123 2925
        size_t len = SIZE_MAX;
124 2925
        uint8_t nib = 0, nib2;
125 2925
        enum state_e state = NORMAL;
126
        int i;
127
128 2925
        AN(buf);
129 2925
        AN(strings);
130 2925
        assert(dec == URL);
131
132 2925
        if (n >= 0)
133 1325
                len = n;
134
135 6700
        for (i = 0; len > 0 && i < strings->n; i++) {
136 3950
                s = strings->p[i];
137
138 3950
                if (s == NULL || *s == '\0')
139 800
                        continue;
140 585925
                while (*s && len) {
141 582950
                        switch (state) {
142
                        case NORMAL:
143 252250
                                if (*s == '%')
144 165575
                                        state = PERCENT;
145
                                else {
146 86675
                                        if (dest == end) {
147 25
                                                errno = ENOMEM;
148 25
                                                return (-1);
149
                                        }
150 86650
                                        *dest++ = *s;
151
                                }
152 252225
                                break;
153
                        case PERCENT:
154 165450
                                if (isoutofrange(*s) ||
155 165375
                                    (nib = hex_nibble[*s - '0']) == ILL) {
156 75
                                        errno = EINVAL;
157 75
                                        return (-1);
158
                                }
159 165375
                                state = FIRSTNIB;
160 165375
                                break;
161
                        case FIRSTNIB:
162 165250
                                if (dest == end) {
163 0
                                        errno = ENOMEM;
164 0
                                        return (-1);
165
                                }
166 165250
                                if (isoutofrange(*s) ||
167 165175
                                    (nib2 = hex_nibble[*s - '0']) == ILL) {
168 75
                                        errno = EINVAL;
169 75
                                        return (-1);
170
                                }
171 165175
                                *dest++ = (nib << 4) | nib2;
172 165175
                                nib = 0;
173 165175
                                state = NORMAL;
174 165175
                                break;
175
                        default:
176 0
                                WRONG("illegal URL decode state");
177 0
                        }
178 582775
                        s++;
179 582775
                        len--;
180
                }
181 2975
        }
182 2750
        if (state != NORMAL) {
183 250
                errno = EINVAL;
184 250
                return (-1);
185
        }
186 2500
        assert(dest <= end);
187 2500
        return (dest - buf);
188 2925
}