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 147200
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 294400
        t = VRE_compile(re, 0, &error, &erroroffset,
48 147200
            cache_param->pcre2_jit_compilation);
49 147200
        AN(t);
50 147200
        *rep = t;
51 147200
}
52
53
void
54 9378
VPI_re_fini(vre_t *rep)
55
{
56
        vre_t *vv;
57
58 9378
        vv = rep;
59 9378
        if (rep != NULL)
60 9378
                VRE_free(&vv);
61 9378
}
62
63
VCL_BOOL
64 204814
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 204814
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
71 204814
        if (s == NULL)
72 96634
                s = "";
73 204814
        AN(re);
74 204814
        i = VRE_match(re, s, 0, 0, &cache_param->vre_limits);
75 204814
        if (i >= 0)
76 7480
                return (1);
77 197334
        if (i < VRE_ERROR_NOMATCH ) {
78 40
                AN(VSB_init(vsb, errbuf, sizeof errbuf));
79 40
                AZ(VRE_error(vsb, i));
80 40
                AZ(VSB_finish(vsb));
81 40
                VSB_fini(vsb);
82 40
                VRT_fail(ctx, "Regexp matching failed: %s", errbuf);
83 40
        }
84 197334
        return (0);
85 204814
}
86
87
VCL_STRING
88 2480
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 2480
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
96 2480
        AN(re);
97 2480
        if (str == NULL)
98 120
                str = "";
99 2480
        if (sub == NULL)
100 40
                sub = "";
101
102 2480
        snap = WS_Snapshot(ctx->ws);
103 2480
        WS_VSB_new(vsb, ctx->ws);
104 2480
        i = VRE_sub(re, str, sub, vsb, &cache_param->vre_limits, all);
105 2480
        res = WS_VSB_finish(vsb, ctx->ws, NULL);
106
107 2480
        if (i < VRE_ERROR_NOMATCH)
108 0
                VRT_fail(ctx, "regsub: Regexp matching returned %d", i);
109 2480
        else if (res == NULL)
110 0
                VRT_fail(ctx, "regsub: Out of workspace");
111 2480
        else if (i > 0)
112 1480
                return (res);
113 1000
        WS_Reset(ctx->ws, snap);
114 1000
        return (str);
115 2480
}