vinyl-cache/lib/libvinyl/vre.c
0
/*-
1
 * Copyright (c) 2006-2011 Varnish Software AS
2
 * All rights reserved.
3
 *
4
 * Author: Tollef Fog Heen <tfheen@redpill-linpro.com>
5
 *
6
 * SPDX-License-Identifier: BSD-2-Clause
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
 * SUCH DAMAGE.
28
 */
29
30
#include "config.h"
31
32
#include <ctype.h>
33
#include <string.h>
34
#include <unistd.h>
35
36
#include "vdef.h"
37
38
#include "vas.h"        // XXX Flexelint "not used" - but req'ed for assert()
39
#include "vsb.h"
40
#include "miniobj.h"
41
42
#include "vre.h"
43
#include "vre_pcre2.h"
44
45
/* should be turned into an error sooner or later */
46
#if !defined(pcre2_set_depth_limit)
47
#  warning pcre2 missing pcre2_set_depth_limit - update recommended
48
#  define pcre2_set_depth_limit(r, d) pcre2_set_recursion_limit(r, d)
49
#endif
50
51
#define VRE_PACKED_RE           (pcre2_code *)(-1)
52
53
struct vre {
54
        unsigned                magic;
55
#define VRE_MAGIC               0xe83097dc
56
        pcre2_code              *re;
57
        pcre2_match_context     *re_ctx;
58
};
59
60
/*
61
 * We don't want to spread or even expose the majority of PCRE2 options
62
 * and errors so we establish our own symbols and implement hard linkage
63
 * to PCRE2 here.
64
 */
65
const int VRE_ERROR_NOMATCH = PCRE2_ERROR_NOMATCH;
66
67
const unsigned VRE_CASELESS = PCRE2_CASELESS;
68
const unsigned VRE_NO_AUTO_CAPTURE = PCRE2_NO_AUTO_CAPTURE;
69
70
vre_t *
71 57225
VRE_compile(const char *pattern, unsigned options,
72
    int *errptr, int *erroffset, unsigned jit)
73
{
74
        PCRE2_SIZE erroff;
75
        vre_t *v;
76
77 57225
        AN(pattern);
78 57225
        AN(errptr);
79 57225
        AN(erroffset);
80
81 57225
        *errptr = 0;
82 57225
        *erroffset = -1;
83
84 57225
        ALLOC_OBJ(v, VRE_MAGIC);
85 57225
        if (v == NULL) {
86 0
                *errptr = PCRE2_ERROR_NOMEMORY;
87 0
                return (NULL);
88
        }
89 114450
        v->re = pcre2_compile((PCRE2_SPTR8)pattern, PCRE2_ZERO_TERMINATED,
90 57225
            options, errptr, &erroff, NULL);
91 57225
        *erroffset = erroff;
92 57225
        if (v->re == NULL) {
93 15
                VRE_free(&v);
94 15
                return (NULL);
95
        }
96 57210
        v->re_ctx = pcre2_match_context_create(NULL);
97 57210
        if (v->re_ctx == NULL) {
98 0
                *errptr = PCRE2_ERROR_NOMEMORY;
99 0
                VRE_free(&v);
100 0
                return (NULL);
101
        }
102
#if USE_PCRE2_JIT
103 57210
        if (jit)
104 21354
                (void)pcre2_jit_compile(v->re, PCRE2_JIT_COMPLETE);
105
#else
106
        (void)jit;
107
#endif
108 57210
        return (v);
109 57225
}
110
111
int
112 18
VRE_error(struct vsb *vsb, int err)
113
{
114
        char buf[VRE_ERROR_LEN];
115
        int i;
116
117 18
        CHECK_OBJ_NOTNULL(vsb, VSB_MAGIC);
118 18
        i = pcre2_get_error_message(err, (PCRE2_UCHAR *)buf, VRE_ERROR_LEN);
119 18
        if (i == PCRE2_ERROR_BADDATA) {
120 0
                VSB_printf(vsb, "unknown pcre2 error code (%d)", err);
121 0
                return (-1);
122
        }
123 18
        VSB_cat(vsb, buf);
124 18
        return (0);
125 18
}
126
127
pcre2_code *
128 35420
VRE_unpack(const vre_t *code)
129
{
130
131
        /* XXX: The ban code ensures that regex "lumps" are pointer-aligned,
132
         * but coming for example from a VMOD there is no guarantee. Should
133
         * we formally require that code is properly aligned?
134
         */
135 35420
        CHECK_OBJ_NOTNULL(code, VRE_MAGIC);
136 35420
        if (code->re == VRE_PACKED_RE) {
137 69
                AZ(code->re_ctx);
138 69
                return (TRUST_ME(code + 1));
139
        }
140 35351
        return (code->re);
141 35420
}
142
143
static void
144 35302
vre_limit(const vre_t *code, const volatile struct vre_limits *lim)
145
{
146
147 35302
        CHECK_OBJ_NOTNULL(code, VRE_MAGIC);
148
149 35302
        if (lim == NULL)
150 10788
                return;
151
152 24514
        assert(code->re != VRE_PACKED_RE);
153
154
        /* XXX: not reentrant */
155 24514
        AN(code->re_ctx);
156 24514
        AZ(pcre2_set_match_limit(code->re_ctx, lim->match));
157 24514
        AZ(pcre2_set_depth_limit(code->re_ctx, lim->depth));
158 35302
}
159
160
vre_t *
161 87
VRE_export(const vre_t *code, size_t *sz)
162
{
163
        pcre2_code *re;
164
        vre_t *exp;
165
166 87
        CHECK_OBJ_NOTNULL(code, VRE_MAGIC);
167 87
        re = VRE_unpack(code);
168 87
        AZ(pcre2_pattern_info(re, PCRE2_INFO_SIZE, sz));
169
170 87
        exp = malloc(sizeof(*exp) + *sz);
171 87
        if (exp == NULL)
172 0
                return (NULL);
173
174 87
        INIT_OBJ(exp, VRE_MAGIC);
175 87
        exp->re = VRE_PACKED_RE;
176 87
        memcpy(exp + 1, re, *sz);
177 87
        *sz += sizeof(*exp);
178 87
        return (exp);
179 87
}
180
181
static int
182 35334
vre_capture(const vre_t *code, const char *subject, size_t length,
183
    size_t offset, int options, txt *groups, size_t *count,
184
    pcre2_match_data **datap)
185
{
186
        pcre2_match_data *data;
187
        pcre2_code *re;
188
        PCRE2_SIZE *ovector, b, e;
189
        size_t nov, g;
190
        int matches;
191
192 35334
        re = VRE_unpack(code);
193
194 35334
        if (datap != NULL && *datap != NULL) {
195 33
                data = *datap;
196 33
                *datap = NULL;
197 33
        } else {
198 35301
                data = pcre2_match_data_create_from_pattern(re, NULL);
199 35301
                AN(data);
200
        }
201
202 35334
        ovector = pcre2_get_ovector_pointer(data);
203 35334
        nov = 2L * pcre2_get_ovector_count(data);
204 106508
        for (g = 0; g < nov; g++)
205 71174
                ovector[g] = PCRE2_UNSET;
206
207 70668
        matches = pcre2_match(re, (PCRE2_SPTR)subject, length, offset,
208 35334
            options, data, code->re_ctx);
209
210 35334
        if (groups != NULL) {
211 219
                AN(count);
212 219
                AN(*count);
213 219
                ovector = pcre2_get_ovector_pointer(data);
214 219
                nov = vmin_t(size_t, pcre2_get_ovector_count(data), *count);
215 516
                for (g = 0; g < nov; g++) {
216 297
                        b = ovector[2 * g];
217 297
                        e = ovector[2 * g + 1];
218 297
                        if (b == PCRE2_UNSET) {
219 105
                                groups->b = groups->e = "";
220 105
                        } else {
221 192
                                groups->b = subject + b;
222 192
                                groups->e = subject + e;
223
                        }
224 297
                        groups++;
225 297
                }
226 219
                *count = nov;
227 219
        }
228
229 35334
        if (datap != NULL && matches > VRE_ERROR_NOMATCH)
230 117
                *datap = data;
231
        else
232 35217
                pcre2_match_data_free(data);
233 35334
        return (matches);
234
}
235
236
int
237 35116
VRE_match(const vre_t *code, const char *subject, size_t length,
238
    int options, const volatile struct vre_limits *lim)
239
{
240
241 35116
        CHECK_OBJ_NOTNULL(code, VRE_MAGIC);
242 35116
        AN(subject);
243
244 35116
        if (length == 0)
245 24936
                length = PCRE2_ZERO_TERMINATED;
246 35116
        vre_limit(code, lim);
247 35116
        return (vre_capture(code, subject, length, 0, options,
248
            NULL, NULL, NULL));
249
}
250
251
int
252 0
VRE_capture(const vre_t *code, const char *subject, size_t length, int options,
253
    txt *groups, size_t count, const volatile struct vre_limits *lim)
254
{
255
        int i;
256
257 0
        CHECK_OBJ_NOTNULL(code, VRE_MAGIC);
258 0
        AN(subject);
259 0
        AN(groups);
260 0
        AN(count);
261
262 0
        if (length == 0)
263 0
                length = PCRE2_ZERO_TERMINATED;
264 0
        vre_limit(code, lim);
265 0
        i = vre_capture(code, subject, length, 0, options,
266 0
            groups, &count, NULL);
267
268 0
        if (i <= 0)
269 0
                return (i);
270 0
        return (count);
271 0
}
272
273
int
274 186
VRE_sub(const vre_t *code, const char *subject, const char *replacement,
275
    struct vsb *vsb, const volatile struct vre_limits *lim, int all)
276
{
277 186
        pcre2_match_data *data = NULL;
278
        txt groups[10];
279
        size_t count;
280 186
        int i, offset = 0;
281
        const char *s, *e;
282
        unsigned x;
283
284 186
        CHECK_OBJ_NOTNULL(code, VRE_MAGIC);
285 186
        CHECK_OBJ_NOTNULL(vsb, VSB_MAGIC);
286 186
        AN(subject);
287 186
        AN(replacement);
288
289 186
        vre_limit(code, lim);
290 186
        count = 10;
291 372
        i = vre_capture(code, subject, PCRE2_ZERO_TERMINATED, offset, 0,
292 186
            groups, &count, &data);
293
294 186
        if (i <= VRE_ERROR_NOMATCH) {
295 75
                AZ(data);
296 75
                return (i);
297
        }
298
299 111
        do {
300 117
                AN(data); /* check reuse across successful captures */
301 117
                AN(count);
302
303
                /* Copy prefix to match */
304 117
                s = subject + offset;
305 117
                VSB_bcat(vsb, s, pdiff(s, groups[0].b));
306 615
                for (s = e = replacement; *e != '\0'; e++ ) {
307 498
                        if (*e != '\\' || e[1] == '\0')
308 408
                                continue;
309 90
                        VSB_bcat(vsb, s, pdiff(s, e));
310 90
                        s = ++e;
311 90
                        if (isdigit(*e)) {
312 78
                                s++;
313 78
                                x = *e - '0';
314 78
                                if (x >= count)
315 18
                                        continue;
316 60
                                VSB_bcat(vsb, groups[x].b, Tlen(groups[x]));
317 60
                                continue;
318
                        }
319 12
                }
320 117
                VSB_bcat(vsb, s, pdiff(s, e));
321 117
                offset = pdiff(subject, groups[0].e);
322 117
                if (!all)
323 84
                        break;
324 33
                count = 10;
325 66
                i = vre_capture(code, subject, PCRE2_ZERO_TERMINATED, offset,
326 33
                    PCRE2_NOTEMPTY, groups, &count, &data);
327
328 33
                if (i < VRE_ERROR_NOMATCH) {
329 0
                        AZ(data);
330 0
                        return (i);
331
                }
332 33
        } while (i != VRE_ERROR_NOMATCH);
333
334 111
        if (data != NULL) {
335 84
                assert(i > VRE_ERROR_NOMATCH);
336 84
                AZ(all);
337 84
                pcre2_match_data_free(data);
338 84
        }
339
340
        /* Copy suffix to match */
341 111
        VSB_cat(vsb, subject + offset);
342 111
        return (1);
343 186
}
344
345
void
346 42675
VRE_free(vre_t **vv)
347
{
348
        vre_t *v;
349
350 42675
        TAKE_OBJ_NOTNULL(v, vv, VRE_MAGIC);
351
352 42675
        if (v->re == VRE_PACKED_RE) {
353 87
                v->re = NULL;
354 87
                AZ(v->re_ctx);
355 87
        }
356
357 42675
        if (v->re_ctx != NULL)
358 42573
                pcre2_match_context_free(v->re_ctx);
359 42675
        if (v->re != NULL)
360 42573
                pcre2_code_free(v->re);
361 42675
        FREE_OBJ(v);
362 42675
}
363
364
void
365 18
VRE_quote(struct vsb *vsb, const char *src)
366
{
367
        const char *b, *e;
368
369 18
        CHECK_OBJ_NOTNULL(vsb, VSB_MAGIC);
370 18
        if (src == NULL)
371 0
                return;
372 27
        for (b = src; (e = strstr(b, "\\E")) != NULL; b = e + 2)
373 9
                VSB_printf(vsb, "\\Q%.*s\\\\EE", (int)(e - b), b);
374 18
        if (*b != '\0')
375 9
                VSB_printf(vsb, "\\Q%s\\E", b);
376 18
}