varnish-cache/lib/libvarnish/vre.c
1
/*-
2
 * Copyright (c) 2006-2011 Varnish Software AS
3
 * All rights reserved.
4
 *
5
 * Author: Tollef Fog Heen <tfheen@redpill-linpro.com>
6
 *
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions
9
 * are met:
10
 * 1. Redistributions of source code must retain the above copyright
11
 *    notice, this list of conditions and the following disclaimer.
12
 * 2. Redistributions in binary form must reproduce the above copyright
13
 *    notice, this list of conditions and the following disclaimer in the
14
 *    documentation and/or other materials provided with the distribution.
15
 *
16
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
20
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26
 * SUCH DAMAGE.
27
 */
28
29
#include "config.h"
30
31
#include <pcre.h>
32
#include <string.h>
33
34
#include "vdef.h"
35
36
#include "vas.h"        // XXX Flexelint "not used" - but req'ed for assert()
37
#include "miniobj.h"
38
39
#include "vre.h"
40
41
#if defined(USE_PCRE_JIT)
42
#define VRE_STUDY_JIT_COMPILE PCRE_STUDY_JIT_COMPILE
43
#else
44
#define VRE_STUDY_JIT_COMPILE 0
45
#endif
46
47
const unsigned VRE_has_jit = VRE_STUDY_JIT_COMPILE;
48
49
#if PCRE_MAJOR < 8 || (PCRE_MAJOR == 8 && PCRE_MINOR < 20)
50
#  define pcre_free_study pcre_free
51
#endif
52
53
struct vre {
54
        unsigned                magic;
55
#define VRE_MAGIC               0xe83097dc
56
        pcre                    *re;
57
        pcre_extra              *re_extra;
58
        int                     my_extra;
59
};
60
61
/*
62
 * We don't want to spread or even expose the majority of PCRE options
63
 * so we establish our own options and implement hard linkage to PCRE
64
 * here.
65
 */
66
const unsigned VRE_CASELESS = PCRE_CASELESS;
67
const unsigned VRE_NOTEMPTY = PCRE_NOTEMPTY;
68
69
vre_t *
70 3908
VRE_compile(const char *pattern, unsigned options,
71
    const char **errptr, int *erroffset)
72
{
73
        vre_t *v;
74 3908
        *errptr = NULL; *erroffset = 0;
75
76 3908
        ALLOC_OBJ(v, VRE_MAGIC);
77 3908
        if (v == NULL) {
78 0
                *errptr = "Out of memory for VRE";
79 0
                return (NULL);
80
        }
81 3908
        v->re = pcre_compile(pattern, options, errptr, erroffset, NULL);
82 3908
        if (v->re == NULL) {
83 2
                VRE_free(&v);
84 2
                return (NULL);
85
        }
86 3906
        v->re_extra = pcre_study(v->re, VRE_STUDY_JIT_COMPILE, errptr);
87 3906
        if (*errptr != NULL) {
88 0
                VRE_free(&v);
89 0
                return (NULL);
90
        }
91 3906
        if (v->re_extra == NULL) {
92
                /* allocate our own */
93 0
                v->re_extra = calloc(1, sizeof(pcre_extra));
94 0
                v->my_extra = 1;
95 0
                if (v->re_extra == NULL) {
96 0
                        *errptr = "Out of memory for pcre_extra";
97 0
                        VRE_free(&v);
98 0
                        return (NULL);
99
                }
100
        }
101 3906
        return (v);
102
}
103
104
int
105 3003
VRE_exec(const vre_t *code, const char *subject, int length,
106
    int startoffset, int options, int *ovector, int ovecsize,
107
    const volatile struct vre_limits *lim)
108
{
109 3003
        CHECK_OBJ_NOTNULL(code, VRE_MAGIC);
110
        int ov[30];
111
112 3003
        if (ovector == NULL) {
113 2946
                ovector = ov;
114 2946
                ovecsize = sizeof(ov)/sizeof(ov[0]);
115
        }
116
117 3003
        if (lim != NULL) {
118 1937
                code->re_extra->match_limit = lim->match;
119 1937
                code->re_extra->flags |= PCRE_EXTRA_MATCH_LIMIT;
120 1937
                code->re_extra->match_limit_recursion = lim->match_recursion;
121 1937
                code->re_extra->flags |= PCRE_EXTRA_MATCH_LIMIT_RECURSION;
122
        } else {
123 1066
                code->re_extra->flags &= ~PCRE_EXTRA_MATCH_LIMIT;
124 1066
                code->re_extra->flags &= ~PCRE_EXTRA_MATCH_LIMIT_RECURSION;
125
        }
126
127 3003
        return (pcre_exec(code->re, code->re_extra, subject, length,
128
            startoffset, options, ovector, ovecsize));
129
}
130
131
void
132 2450
VRE_free(vre_t **vv)
133
{
134 2450
        vre_t *v = *vv;
135
136 2450
        *vv = NULL;
137 2450
        CHECK_OBJ(v, VRE_MAGIC);
138 2450
        if (v->re_extra != NULL) {
139 2448
                if (v->my_extra)
140 0
                        free(v->re_extra);
141
                else
142 2448
                        pcre_free_study(v->re_extra);
143
        }
144 2450
        if (v->re != NULL)
145 2448
                pcre_free(v->re);
146 2450
        FREE_OBJ(v);
147 2450
}