15 | 15 |
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
16 | 16 |
*/
|
17 | 17 |
|
18 | |
#ifdef PCRE
|
|
18 |
#ifdef PCRE2
|
19 | 19 |
|
20 | 20 |
#include <sys/types.h>
|
21 | 21 |
|
22 | |
#include <pcre.h>
|
|
22 |
#define PCRE2_CODE_UNIT_WIDTH 8
|
|
23 |
|
|
24 |
#include <pcre2.h>
|
23 | 25 |
#include <string.h>
|
24 | 26 |
|
25 | 27 |
#include "fdm.h"
|
|
27 | 29 |
int
|
28 | 30 |
re_compile(struct re *re, const char *s, int flags, char **cause)
|
29 | 31 |
{
|
30 | |
const char *error;
|
31 | |
int off;
|
|
32 |
char error[256];
|
|
33 |
PCRE2_SIZE off;
|
|
34 |
int errorcode;
|
32 | 35 |
|
33 | 36 |
if (s == NULL)
|
34 | 37 |
fatalx("null regexp");
|
|
37 | 40 |
return (0);
|
38 | 41 |
re->flags = flags;
|
39 | 42 |
|
40 | |
flags = PCRE_MULTILINE;
|
|
43 |
flags = PCRE2_MULTILINE;
|
41 | 44 |
if (re->flags & RE_IGNCASE)
|
42 | |
flags |= PCRE_CASELESS;
|
|
45 |
flags |= PCRE2_CASELESS;
|
43 | 46 |
|
44 | |
if ((re->pcre = pcre_compile(s, flags, &error, &off, NULL)) == NULL) {
|
|
47 |
re->pcre2 = pcre2_compile(s, PCRE2_ZERO_TERMINATED, flags, &errorcode,
|
|
48 |
&off, NULL);
|
|
49 |
if (re->pcre2 == NULL) {
|
|
50 |
pcre2_get_error_message(errorcode, error, sizeof(error));
|
45 | 51 |
*cause = xstrdup(error);
|
46 | 52 |
return (-1);
|
47 | 53 |
}
|
|
59 | 65 |
re_block(struct re *re, const void *buf, size_t len, struct rmlist *rml,
|
60 | 66 |
char **cause)
|
61 | 67 |
{
|
62 | |
int res, pm[NPMATCH * 3];
|
63 | |
u_int i, j;
|
|
68 |
int res;
|
|
69 |
pcre2_match_data *pmd;
|
|
70 |
PCRE2_SIZE *ovector;
|
|
71 |
u_int i, j;
|
64 | 72 |
|
65 | 73 |
if (len > INT_MAX)
|
66 | 74 |
fatalx("buffer too big");
|
|
75 | 83 |
return (0);
|
76 | 84 |
}
|
77 | 85 |
|
78 | |
res = pcre_exec(re->pcre, NULL, buf, len, 0, 0, pm, NPMATCH * 3);
|
79 | |
if (res < 0 && res != PCRE_ERROR_NOMATCH) {
|
|
86 |
pmd = pcre2_match_data_create_from_pattern(re->pcre2, NULL);
|
|
87 |
res = pcre2_match(re->pcre2, buf, len, 0, 0, pmd, NULL);
|
|
88 |
if (res < 0 && res != PCRE2_ERROR_NOMATCH) {
|
80 | 89 |
xasprintf(cause, "%s: regexec failed", re->str);
|
|
90 |
pcre2_match_data_free(pmd);
|
81 | 91 |
return (-1);
|
82 | 92 |
}
|
83 | 93 |
|
84 | 94 |
if (rml != NULL) {
|
85 | |
for (i = 0; i < NPMATCH; i++) {
|
|
95 |
ovector = pcre2_get_ovector_pointer(pmd);
|
|
96 |
for (i = 0; i < res; i++) {
|
86 | 97 |
j = i * 2;
|
87 | |
if (pm[j + 1] <= pm[j])
|
|
98 |
if (ovector[j + 1] <= ovector[j])
|
88 | 99 |
break;
|
89 | 100 |
rml->list[i].valid = 1;
|
90 | |
rml->list[i].so = pm[j];
|
91 | |
rml->list[i].eo = pm[j + 1];
|
|
101 |
rml->list[i].so = ovector[j];
|
|
102 |
rml->list[i].eo = ovector[j + 1];
|
92 | 103 |
}
|
93 | 104 |
rml->valid = 1;
|
94 | 105 |
}
|
95 | 106 |
|
96 | |
return (res != PCRE_ERROR_NOMATCH);
|
|
107 |
return (res != PCRE2_ERROR_NOMATCH);
|
97 | 108 |
}
|
98 | 109 |
|
99 | 110 |
void
|
100 | 111 |
re_free(struct re *re)
|
101 | 112 |
{
|
102 | 113 |
xfree(re->str);
|
103 | |
pcre_free(re->pcre);
|
|
114 |
pcre2_code_free(re->pcre2);
|
104 | 115 |
}
|
105 | 116 |
|
106 | |
#endif /* PCRE */
|
|
117 |
#endif /* PCRE2 */
|