varnish-cache/bin/varnishd/cache/cache_vrt_re.c
0
/*-
1
 * Copyright (c) 2006 Verdens Gang AS
2
 * Copyright (c) 2006-2015 Varnish Software AS
3
 * All rights reserved.
4
 *
5
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
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
11
 * are met:
12
 * 1. Redistributions of source code must retain the above copyright
13
 *    notice, this list of conditions and the following disclaimer.
14
 * 2. Redistributions in binary form must reproduce the above copyright
15
 *    notice, this list of conditions and the following disclaimer in the
16
 *    documentation and/or other materials provided with the distribution.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
22
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28
 * SUCH DAMAGE.
29
 *
30
 * Runtime support for compiled VCL programs, regexps
31
 */
32
33
#include "config.h"
34
35
#include <ctype.h>
36
37
#include "cache_varnishd.h"
38
#include "vcc_interface.h"
39
40
void
41 88675
VPI_re_init(vre_t **rep, const char *re)
42
{
43
        vre_t *t;
44
        int error, erroroffset;
45
46
        /* This was already check-compiled by the VCL compiler */
47 177350
        t = VRE_compile(re, 0, &error, &erroroffset,
48 88675
            cache_param->pcre2_jit_compilation);
49 88675
        AN(t);
50 88675
        *rep = t;
51 88675
}
52
53
void
54 5725
VPI_re_fini(vre_t *rep)
55
{
56
        vre_t *vv;
57
58 5725
        vv = rep;
59 5725
        if (rep != NULL)
60 5725
                VRE_free(&vv);
61 5725
}
62
63
VCL_BOOL
64 125014
VRT_re_match(VRT_CTX, const char *s, VCL_REGEX re)
65
{
66
        struct vsb vsb[1];
67
        char errbuf[VRE_ERROR_LEN];
68
        int i;
69
70 125014
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
71 125014
        if (s == NULL)
72 59600
                s = "";
73 125014
        AN(re);
74 125014
        i = VRE_match(re, s, 0, 0, &cache_param->vre_limits);
75 125014
        if (i >= 0)
76 4575
                return (1);
77 120439
        if (i < VRE_ERROR_NOMATCH ) {
78 25
                AN(VSB_init(vsb, errbuf, sizeof errbuf));
79 25
                AZ(VRE_error(vsb, i));
80 25
                AZ(VSB_finish(vsb));
81 25
                VSB_fini(vsb);
82 25
                VRT_fail(ctx, "Regexp matching failed: %s", errbuf);
83 25
        }
84 120439
        return (0);
85 125014
}
86
87
VCL_STRING
88 1550
VRT_regsub(VRT_CTX, int all, VCL_STRING str, VCL_REGEX re, VCL_STRING sub)
89
{
90
        struct vsb vsb[1];
91
        const char *res;
92
        uintptr_t snap;
93
        int i;
94
95 1550
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
96 1550
        AN(re);
97 1550
        if (str == NULL)
98 75
                str = "";
99 1550
        if (sub == NULL)
100 25
                sub = "";
101
102 1550
        snap = WS_Snapshot(ctx->ws);
103 1550
        WS_VSB_new(vsb, ctx->ws);
104 1550
        i = VRE_sub(re, str, sub, vsb, &cache_param->vre_limits, all);
105 1550
        res = WS_VSB_finish(vsb, ctx->ws, NULL);
106
107 1550
        if (i < VRE_ERROR_NOMATCH)
108 0
                VRT_fail(ctx, "regsub: Regexp matching returned %d", i);
109 1550
        else if (res == NULL)
110 0
                VRT_fail(ctx, "regsub: Out of workspace");
111 1550
        else if (i > 0)
112 925
                return (res);
113 625
        WS_Reset(ctx->ws, snap);
114 625
        return (str);
115 1550
}