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