varnish-cache/vmod/vmod_blob_url.c
1
/*-
2
 * Copyright 2015-2016 UPLEX - Nils Goroll Systemoptimierung
3
 * All rights reserved.
4
 *
5
 * Authors: Nils Goroll <nils.goroll@uplex.de>
6
 *          Geoffrey Simmons <geoffrey.simmons@uplex.de>
7
 *
8
 * SPDX-License-Identifier: BSD-2-Clause
9
 *
10
 * Redistribution and use in source and binary forms, with or without
11
 * modification, are permitted provided that the following conditions are met:
12
 * 1. Redistributions of source code must retain the above copyright notice,
13
 *    this list of conditions and the following disclaimer.
14
 * 2. Redistributions in binary form must reproduce the above copyright notice,
15
 *    this list of conditions and the following disclaimer in the documentation
16
 *    and/or other materials provided with the distribution.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
19
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
 * DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
22
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 *
29
 */
30
31
#include "config.h"
32
33
#include "vdef.h"
34
#include "vrt.h"
35
#include "vas.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 140
url_encode_l(size_t l)
48
{
49 140
        return ((l * 3) + 1);
50
}
51
52
size_t
53 670
url_decode_l(size_t l)
54
{
55 670
        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 96010
isunreserved(const uint8_t c)
71
{
72 96010
        return (unreserved[c >> 3] & (1 << (c & 7)));
73
}
74
75
static inline int
76 132280
isoutofrange(const uint8_t c)
77
{
78 132280
        return (c < '0' || c > 'f');
79
}
80
81
ssize_t
82 930
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 930
        char *p = buf;
87 930
        const char * const end = buf + buflen;
88 930
        const char *alphabet = hex_alphabet[0];
89
        size_t i;
90
91 930
        AN(buf);
92 930
        assert(enc == URL);
93 930
        if (in == NULL || inlen == 0)
94 30
                return (0);
95
96 900
        if (kase == UPPER)
97 320
                alphabet = hex_alphabet[1];
98
99 96910
        for (i = 0; i < inlen; i++) {
100 96010
                if (isunreserved(in[i])) {
101 32660
                        if (p == end)
102 0
                                return (-1);
103 32660
                        *p++ = in[i];
104 32660
                }
105
                else {
106 63350
                        if (p + 3 > end)
107 0
                                return (-1);
108 63350
                        *p++ = '%';
109 63350
                        *p++ = alphabet[(in[i] & 0xf0) >> 4];
110 63350
                        *p++ = alphabet[in[i] & 0x0f];
111
                }
112 96010
        }
113
114 900
        return (p - buf);
115 930
}
116
117
ssize_t
118 1170
url_decode(const enum encoding dec, blob_dest_t buf,
119
    blob_len_t buflen, ssize_t n, VCL_STRANDS strings)
120
{
121 1170
        char *dest = buf;
122 1170
        const char * const end = buf + buflen;
123
        const char *s;
124 1170
        size_t len = SIZE_MAX;
125 1170
        uint8_t nib = 0, nib2;
126 1170
        enum state_e state = NORMAL;
127
        int i;
128
129 1170
        AN(buf);
130 1170
        AN(strings);
131 1170
        assert(dec == URL);
132
133 1170
        if (n >= 0)
134 530
                len = n;
135
136 2680
        for (i = 0; len > 0 && i < strings->n; i++) {
137 1580
                s = strings->p[i];
138
139 1580
                if (s == NULL || *s == '\0')
140 320
                        continue;
141 234370
                while (*s && len) {
142 233180
                        switch (state) {
143
                        case NORMAL:
144 100900
                                if (*s == '%')
145 66230
                                        state = PERCENT;
146
                                else {
147 34670
                                        if (dest == end) {
148 10
                                                errno = ENOMEM;
149 10
                                                return (-1);
150
                                        }
151 34660
                                        *dest++ = *s;
152
                                }
153 100890
                                break;
154
                        case PERCENT:
155 66180
                                if (isoutofrange(*s) ||
156 66150
                                    (nib = hex_nibble[*s - '0']) == ILL) {
157 30
                                        errno = EINVAL;
158 30
                                        return (-1);
159
                                }
160 66150
                                state = FIRSTNIB;
161 66150
                                break;
162
                        case FIRSTNIB:
163 66100
                                if (dest == end) {
164 0
                                        errno = ENOMEM;
165 0
                                        return (-1);
166
                                }
167 66100
                                if (isoutofrange(*s) ||
168 66070
                                    (nib2 = hex_nibble[*s - '0']) == ILL) {
169 30
                                        errno = EINVAL;
170 30
                                        return (-1);
171
                                }
172 66070
                                *dest++ = (nib << 4) | nib2;
173 66070
                                nib = 0;
174 66070
                                state = NORMAL;
175 66070
                                break;
176
                        default:
177 0
                                WRONG("illegal URL decode state");
178 0
                        }
179 233110
                        s++;
180 233110
                        len--;
181
                }
182 1190
        }
183 1100
        if (state != NORMAL) {
184 100
                errno = EINVAL;
185 100
                return (-1);
186
        }
187 1000
        assert(dest <= end);
188 1000
        return (dest - buf);
189 1170
}