| | 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 |
153480 |
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 |
306960 |
t = VRE_compile(re, 0, &error, &erroroffset, |
| 48 |
153480 |
cache_param->pcre2_jit_compilation); |
| 49 |
153480 |
AN(t); |
| 50 |
153480 |
*rep = t; |
| 51 |
153480 |
} |
| 52 |
|
|
| 53 |
|
void |
| 54 |
9383 |
VPI_re_fini(vre_t *rep) |
| 55 |
|
{ |
| 56 |
|
vre_t *vv; |
| 57 |
|
|
| 58 |
9383 |
vv = rep; |
| 59 |
9383 |
if (rep != NULL) |
| 60 |
9383 |
VRE_free(&vv); |
| 61 |
9383 |
} |
| 62 |
|
|
| 63 |
|
static void |
| 64 |
40 |
re_fail(VRT_CTX, const char *pfx, int res) |
| 65 |
|
{ |
| 66 |
|
struct vsb vsb[1]; |
| 67 |
|
char errbuf[VRE_ERROR_LEN]; |
| 68 |
|
|
| 69 |
40 |
assert(res < VRE_ERROR_NOMATCH); |
| 70 |
40 |
AN(VSB_init(vsb, errbuf, sizeof errbuf)); |
| 71 |
40 |
AZ(VRE_error(vsb, res)); |
| 72 |
40 |
AZ(VSB_finish(vsb)); |
| 73 |
40 |
VSB_fini(vsb); |
| 74 |
40 |
VRT_fail(ctx, "%sRegexp matching failed: %s", pfx, errbuf); |
| 75 |
40 |
} |
| 76 |
|
|
| 77 |
|
VCL_BOOL |
| 78 |
215620 |
VRT_re_match(VRT_CTX, const char *s, VCL_REGEX re) |
| 79 |
|
{ |
| 80 |
|
int i; |
| 81 |
|
|
| 82 |
215620 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
| 83 |
215620 |
if (s == NULL) |
| 84 |
101600 |
s = ""; |
| 85 |
215620 |
AN(re); |
| 86 |
215620 |
i = VRE_match(re, s, 0, 0, &cache_param->vre_limits); |
| 87 |
215620 |
if (i >= 0) |
| 88 |
8000 |
return (1); |
| 89 |
207620 |
if (i < VRE_ERROR_NOMATCH) |
| 90 |
40 |
re_fail(ctx, "", i); |
| 91 |
207620 |
return (0); |
| 92 |
215620 |
} |
| 93 |
|
|
| 94 |
|
VCL_STRING |
| 95 |
2480 |
VRT_regsub(VRT_CTX, int all, VCL_STRING str, VCL_REGEX re, VCL_STRING sub) |
| 96 |
|
{ |
| 97 |
|
struct vsb vsb[1]; |
| 98 |
|
const char *res; |
| 99 |
|
uintptr_t snap; |
| 100 |
|
int i; |
| 101 |
|
|
| 102 |
2480 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
| 103 |
2480 |
AN(re); |
| 104 |
2480 |
if (str == NULL) |
| 105 |
120 |
str = ""; |
| 106 |
2480 |
if (sub == NULL) |
| 107 |
40 |
sub = ""; |
| 108 |
|
|
| 109 |
2480 |
snap = WS_Snapshot(ctx->ws); |
| 110 |
2480 |
WS_VSB_new(vsb, ctx->ws); |
| 111 |
2480 |
i = VRE_sub(re, str, sub, vsb, &cache_param->vre_limits, all); |
| 112 |
2480 |
res = WS_VSB_finish(vsb, ctx->ws, NULL); |
| 113 |
|
|
| 114 |
2480 |
if (i < VRE_ERROR_NOMATCH) |
| 115 |
0 |
re_fail(ctx, "regsub: ", i); |
| 116 |
2480 |
else if (res == NULL) |
| 117 |
0 |
VRT_fail(ctx, "regsub: Out of workspace"); |
| 118 |
2480 |
else if (i > 0) |
| 119 |
1480 |
return (res); |
| 120 |
1000 |
WS_Reset(ctx->ws, snap); |
| 121 |
1000 |
return (str); |
| 122 |
2480 |
} |