28
28
29
29
#define OSCAP_PCRE_EXEC_RECURSION_LIMIT_DEFAULT 3500
30
30
31
- #ifdef HAVE_PCRE2
32
31
#define PCRE2_CODE_UNIT_WIDTH 8
33
32
#define PCRE2_ERR_BUF_SIZE 127
34
33
#include <pcre2.h>
35
- #else
36
- #include <pcre.h>
37
- #endif
34
+
38
35
39
36
#include "debug_priv.h"
40
37
#include "oscap_pcre.h"
41
38
42
39
43
40
struct oscap_pcre {
44
- #ifdef HAVE_PCRE2
45
41
pcre2_code_8 * re ;
46
42
pcre2_match_context_8 * re_ctx ;
47
- #else
48
- pcre * re ;
49
- struct pcre_extra * re_extra ;
50
- #endif
51
43
};
52
44
53
45
54
46
static inline int _oscap_pcre_opts_to_pcre (oscap_pcre_options_t opts )
55
47
{
56
48
int res = 0 ;
57
- #ifdef HAVE_PCRE2
58
49
if (opts & OSCAP_PCRE_OPTS_UTF8 )
59
50
res |= PCRE2_UTF ;
60
51
if (opts & OSCAP_PCRE_OPTS_MULTILINE )
@@ -67,27 +58,13 @@ static inline int _oscap_pcre_opts_to_pcre(oscap_pcre_options_t opts)
67
58
res |= PCRE2_NO_UTF_CHECK ;
68
59
if (opts & OSCAP_PCRE_OPTS_PARTIAL )
69
60
res |= PCRE2_PARTIAL_SOFT ;
70
- #else
71
- if (opts & OSCAP_PCRE_OPTS_UTF8 )
72
- res |= PCRE_UTF8 ;
73
- if (opts & OSCAP_PCRE_OPTS_MULTILINE )
74
- res |= PCRE_MULTILINE ;
75
- if (opts & OSCAP_PCRE_OPTS_DOTALL )
76
- res |= PCRE_DOTALL ;
77
- if (opts & OSCAP_PCRE_OPTS_CASELESS )
78
- res |= PCRE_CASELESS ;
79
- if (opts & OSCAP_PCRE_OPTS_NO_UTF8_CHECK )
80
- res |= PCRE_NO_UTF8_CHECK ;
81
- if (opts & OSCAP_PCRE_OPTS_PARTIAL )
82
- res |= PCRE_PARTIAL ;
83
- #endif
61
+
84
62
return res ;
85
63
};
86
64
87
65
static inline oscap_pcre_error_t _pcre_error_to_oscap_pcre (int res )
88
66
{
89
67
switch (res ) {
90
- #ifdef HAVE_PCRE2
91
68
case PCRE2_ERROR_NOMATCH :
92
69
return OSCAP_PCRE_ERR_NOMATCH ;
93
70
case PCRE2_ERROR_PARTIAL :
@@ -116,18 +93,6 @@ static inline oscap_pcre_error_t _pcre_error_to_oscap_pcre(int res)
116
93
return OSCAP_PCRE_ERR_BADUTF8 ;
117
94
case PCRE2_ERROR_RECURSIONLIMIT :
118
95
return OSCAP_PCRE_ERR_RECURSIONLIMIT ;
119
- #else
120
- case PCRE_ERROR_NOMATCH :
121
- return OSCAP_PCRE_ERR_NOMATCH ;
122
- case PCRE_ERROR_PARTIAL :
123
- return OSCAP_PCRE_ERR_PARTIAL ;
124
- case PCRE_ERROR_BADPARTIAL :
125
- return OSCAP_PCRE_ERR_BADPARTIAL ;
126
- case PCRE_ERROR_BADUTF8 :
127
- return OSCAP_PCRE_ERR_BADUTF8 ;
128
- case PCRE_ERROR_RECURSIONLIMIT :
129
- return OSCAP_PCRE_ERR_RECURSIONLIMIT ;
130
- #endif
131
96
default :
132
97
dW ("Unknown PCRE error code: %d" , res );
133
98
return OSCAP_PCRE_ERR_UNKNOWN ;
@@ -138,7 +103,6 @@ oscap_pcre_t *oscap_pcre_compile(const char *pattern, oscap_pcre_options_t optio
138
103
char * * errptr , int * erroffset )
139
104
{
140
105
oscap_pcre_t * res = malloc (sizeof (oscap_pcre_t ));
141
- #ifdef HAVE_PCRE2
142
106
int errno ;
143
107
PCRE2_SIZE erroffset2 ;
144
108
res -> re_ctx = NULL ;
@@ -151,58 +115,27 @@ oscap_pcre_t *oscap_pcre_compile(const char *pattern, oscap_pcre_options_t optio
151
115
* erroffset = erroffset2 ;
152
116
* errptr = strdup ((const char * )errmsg );
153
117
}
154
- #else
155
- res -> re_extra = NULL ;
156
- dD ("pcre_compile: patt=%s" , pattern );
157
- res -> re = pcre_compile (pattern , _oscap_pcre_opts_to_pcre (options ), (const char * * )errptr , erroffset , NULL );
158
- if (res -> re == NULL )
159
- dW ("pcre_compile: error (at offset %d): %s" , * erroffset , * errptr );
160
- #endif
118
+
161
119
if (res -> re == NULL ) {
162
120
free (res );
163
121
res = NULL ;
164
122
}
165
123
return res ;
166
124
}
167
125
168
- void oscap_pcre_optimize (oscap_pcre_t * opcre )
169
- {
170
- #ifdef HAVE_PCRE2
171
- // This is a NOOP for PCRE2 as all patterns are optimized
172
- // unless the library configured differently.
173
- #else
174
- const char * errptr = NULL ;
175
- pcre_extra * extra = pcre_study (opcre -> re , 0 , & errptr );
176
- if (extra != NULL ) {
177
- if (opcre -> re_extra != NULL )
178
- free (opcre -> re_extra );
179
- opcre -> re_extra = extra ;
180
- }
181
- #endif
182
- }
183
-
184
126
void oscap_pcre_set_match_limit_recursion (oscap_pcre_t * opcre , unsigned long limit )
185
127
{
186
- #ifdef HAVE_PCRE2
187
128
if (opcre -> re_ctx == NULL ) {
188
129
opcre -> re_ctx = pcre2_match_context_create_8 (NULL );
189
130
}
190
131
pcre2_set_depth_limit_8 (opcre -> re_ctx , limit );
191
- #else
192
- if (opcre -> re_extra == NULL ) {
193
- opcre -> re_extra = calloc (1 , sizeof (struct pcre_extra ));
194
- }
195
- opcre -> re_extra -> match_limit_recursion = limit ;
196
- opcre -> re_extra -> flags = PCRE_EXTRA_MATCH_LIMIT_RECURSION ;
197
- #endif
198
132
}
199
133
200
134
int oscap_pcre_exec (const oscap_pcre_t * opcre , const char * subject ,
201
135
int length , int startoffset , oscap_pcre_options_t options ,
202
136
int * ovector , int ovecsize )
203
137
{
204
138
int rc = 0 ;
205
- #ifdef HAVE_PCRE2
206
139
// The ovecsize is multiplied by 3 in the code for compatibility with PCRE1
207
140
int ovecsize2 = ovecsize /3 ;
208
141
pcre2_match_data_8 * mdata = pcre2_match_data_create_8 (ovecsize2 , NULL );
@@ -221,26 +154,16 @@ int oscap_pcre_exec(const oscap_pcre_t *opcre, const char *subject,
221
154
}
222
155
}
223
156
pcre2_match_data_free_8 (mdata );
224
- #else
225
- dD ("pcre_exec: subj=%s" , subject );
226
- rc = pcre_exec (opcre -> re , opcre -> re_extra , subject , length , startoffset , _oscap_pcre_opts_to_pcre (options ), ovector , ovecsize );
227
- dD ("pcre_exec: rc=%d, " , rc );
228
- #endif
157
+
229
158
return rc >= 0 ? rc : _pcre_error_to_oscap_pcre (rc );
230
159
}
231
160
232
161
void oscap_pcre_free (oscap_pcre_t * opcre )
233
162
{
234
163
if (opcre != NULL ) {
235
- #ifdef HAVE_PCRE2
236
164
if (opcre -> re_ctx != NULL )
237
165
pcre2_match_context_free_8 (opcre -> re_ctx );
238
166
pcre2_code_free_8 (opcre -> re );
239
- #else
240
- if (opcre -> re_extra != NULL )
241
- free (opcre -> re_extra );
242
- pcre_free (opcre -> re );
243
- #endif
244
167
free (opcre );
245
168
}
246
169
}
@@ -318,11 +241,7 @@ int oscap_pcre_get_substrings(char *str, int *ofs, oscap_pcre_t *re, int want_su
318
241
void oscap_pcre_err_free (char * err )
319
242
{
320
243
if (err != NULL ) {
321
- #ifdef HAVE_PCRE2
322
244
free (err );
323
- #else
324
- // PCRE1 error messages are static, NOOP.
325
- #endif
326
245
}
327
246
}
328
247
0 commit comments