1 /*************************************************
2 * Perl-Compatible Regular Expressions *
3 *************************************************/
4
5 /* PCRE is a library of functions to support regular expressions whose syntax
6 and semantics are as close as possible to those of the Perl 5 language.
7
8 Written by Philip Hazel
9 Original API code Copyright (c) 1997-2012 University of Cambridge
10 New API code Copyright (c) 2016-2023 University of Cambridge
11
12 -----------------------------------------------------------------------------
13 Redistribution and use in source and binary forms, with or without
14 modification, are permitted provided that the following conditions are met:
15
16 * Redistributions of source code must retain the above copyright notice,
17 this list of conditions and the following disclaimer.
18
19 * Redistributions in binary form must reproduce the above copyright
20 notice, this list of conditions and the following disclaimer in the
21 documentation and/or other materials provided with the distribution.
22
23 * Neither the name of the University of Cambridge nor the names of its
24 contributors may be used to endorse or promote products derived from
25 this software without specific prior written permission.
26
27 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
31 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 POSSIBILITY OF SUCH DAMAGE.
38 -----------------------------------------------------------------------------
39 */
40
41
42 #ifdef HAVE_CONFIG_H
43 #include "config.h"
44 #endif
45
46 #include "pcre2_internal.h"
47
48
49
50 /*************************************************
51 * Default malloc/free functions *
52 *************************************************/
53
54 /* Ignore the "user data" argument in each case. */
55
default_malloc(size_t size,void * data)56 static void *default_malloc(size_t size, void *data)
57 {
58 (void)data;
59 return malloc(size);
60 }
61
62
default_free(void * block,void * data)63 static void default_free(void *block, void *data)
64 {
65 (void)data;
66 free(block);
67 }
68
69
70
71 /*************************************************
72 * Get a block and save memory control *
73 *************************************************/
74
75 /* This internal function is called to get a block of memory in which the
76 memory control data is to be stored at the start for future use.
77
78 Arguments:
79 size amount of memory required
80 memctl pointer to a memctl block or NULL
81
82 Returns: pointer to memory or NULL on failure
83 */
84
85 extern void *
PRIV(memctl_malloc)86 PRIV(memctl_malloc)(size_t size, pcre2_memctl *memctl)
87 {
88 pcre2_memctl *newmemctl;
89 void *yield = (memctl == NULL)? malloc(size) :
90 memctl->malloc(size, memctl->memory_data);
91 if (yield == NULL) return NULL;
92 newmemctl = (pcre2_memctl *)yield;
93 if (memctl == NULL)
94 {
95 newmemctl->malloc = default_malloc;
96 newmemctl->free = default_free;
97 newmemctl->memory_data = NULL;
98 }
99 else *newmemctl = *memctl;
100 return yield;
101 }
102
103
104
105 /*************************************************
106 * Create and initialize contexts *
107 *************************************************/
108
109 /* Initializing for compile and match contexts is done in separate, private
110 functions so that these can be called from functions such as pcre2_compile()
111 when an external context is not supplied. The initializing functions have an
112 option to set up default memory management. */
113
114 PCRE2_EXP_DEFN pcre2_general_context * PCRE2_CALL_CONVENTION
pcre2_general_context_create(void * (* private_malloc)(size_t,void *),void (* private_free)(void *,void *),void * memory_data)115 pcre2_general_context_create(void *(*private_malloc)(size_t, void *),
116 void (*private_free)(void *, void *), void *memory_data)
117 {
118 pcre2_general_context *gcontext;
119 if (private_malloc == NULL) private_malloc = default_malloc;
120 if (private_free == NULL) private_free = default_free;
121 gcontext = private_malloc(sizeof(pcre2_real_general_context), memory_data);
122 if (gcontext == NULL) return NULL;
123 gcontext->memctl.malloc = private_malloc;
124 gcontext->memctl.free = private_free;
125 gcontext->memctl.memory_data = memory_data;
126 return gcontext;
127 }
128
129
130 /* A default compile context is set up to save having to initialize at run time
131 when no context is supplied to the compile function. */
132
133 const pcre2_compile_context PRIV(default_compile_context) = {
134 { default_malloc, default_free, NULL }, /* Default memory handling */
135 NULL, /* Stack guard */
136 NULL, /* Stack guard data */
137 PRIV(default_tables), /* Character tables */
138 PCRE2_UNSET, /* Max pattern length */
139 BSR_DEFAULT, /* Backslash R default */
140 NEWLINE_DEFAULT, /* Newline convention */
141 PARENS_NEST_LIMIT, /* As it says */
142 0, /* Extra options */
143 MAX_VARLOOKBEHIND /* As it says */
144 };
145
146 /* The create function copies the default into the new memory, but must
147 override the default memory handling functions if a gcontext was provided. */
148
149 PCRE2_EXP_DEFN pcre2_compile_context * PCRE2_CALL_CONVENTION
pcre2_compile_context_create(pcre2_general_context * gcontext)150 pcre2_compile_context_create(pcre2_general_context *gcontext)
151 {
152 pcre2_compile_context *ccontext = PRIV(memctl_malloc)(
153 sizeof(pcre2_real_compile_context), (pcre2_memctl *)gcontext);
154 if (ccontext == NULL) return NULL;
155 *ccontext = PRIV(default_compile_context);
156 if (gcontext != NULL)
157 *((pcre2_memctl *)ccontext) = *((pcre2_memctl *)gcontext);
158 return ccontext;
159 }
160
161
162 /* A default match context is set up to save having to initialize at run time
163 when no context is supplied to a match function. */
164
165 const pcre2_match_context PRIV(default_match_context) = {
166 { default_malloc, default_free, NULL },
167 #ifdef SUPPORT_JIT
168 NULL, /* JIT callback */
169 NULL, /* JIT callback data */
170 #endif
171 NULL, /* Callout function */
172 NULL, /* Callout data */
173 NULL, /* Substitute callout function */
174 NULL, /* Substitute callout data */
175 PCRE2_UNSET, /* Offset limit */
176 HEAP_LIMIT,
177 MATCH_LIMIT,
178 MATCH_LIMIT_DEPTH };
179
180 /* The create function copies the default into the new memory, but must
181 override the default memory handling functions if a gcontext was provided. */
182
183 PCRE2_EXP_DEFN pcre2_match_context * PCRE2_CALL_CONVENTION
pcre2_match_context_create(pcre2_general_context * gcontext)184 pcre2_match_context_create(pcre2_general_context *gcontext)
185 {
186 pcre2_match_context *mcontext = PRIV(memctl_malloc)(
187 sizeof(pcre2_real_match_context), (pcre2_memctl *)gcontext);
188 if (mcontext == NULL) return NULL;
189 *mcontext = PRIV(default_match_context);
190 if (gcontext != NULL)
191 *((pcre2_memctl *)mcontext) = *((pcre2_memctl *)gcontext);
192 return mcontext;
193 }
194
195
196 /* A default convert context is set up to save having to initialize at run time
197 when no context is supplied to the convert function. */
198
199 const pcre2_convert_context PRIV(default_convert_context) = {
200 { default_malloc, default_free, NULL }, /* Default memory handling */
201 #ifdef _WIN32
202 CHAR_BACKSLASH, /* Default path separator */
203 CHAR_GRAVE_ACCENT /* Default escape character */
204 #else /* Not Windows */
205 CHAR_SLASH, /* Default path separator */
206 CHAR_BACKSLASH /* Default escape character */
207 #endif
208 };
209
210 /* The create function copies the default into the new memory, but must
211 override the default memory handling functions if a gcontext was provided. */
212
213 PCRE2_EXP_DEFN pcre2_convert_context * PCRE2_CALL_CONVENTION
pcre2_convert_context_create(pcre2_general_context * gcontext)214 pcre2_convert_context_create(pcre2_general_context *gcontext)
215 {
216 pcre2_convert_context *ccontext = PRIV(memctl_malloc)(
217 sizeof(pcre2_real_convert_context), (pcre2_memctl *)gcontext);
218 if (ccontext == NULL) return NULL;
219 *ccontext = PRIV(default_convert_context);
220 if (gcontext != NULL)
221 *((pcre2_memctl *)ccontext) = *((pcre2_memctl *)gcontext);
222 return ccontext;
223 }
224
225
226 /*************************************************
227 * Context copy functions *
228 *************************************************/
229
230 PCRE2_EXP_DEFN pcre2_general_context * PCRE2_CALL_CONVENTION
pcre2_general_context_copy(pcre2_general_context * gcontext)231 pcre2_general_context_copy(pcre2_general_context *gcontext)
232 {
233 pcre2_general_context *newcontext =
234 gcontext->memctl.malloc(sizeof(pcre2_real_general_context),
235 gcontext->memctl.memory_data);
236 if (newcontext == NULL) return NULL;
237 memcpy(newcontext, gcontext, sizeof(pcre2_real_general_context));
238 return newcontext;
239 }
240
241
242 PCRE2_EXP_DEFN pcre2_compile_context * PCRE2_CALL_CONVENTION
pcre2_compile_context_copy(pcre2_compile_context * ccontext)243 pcre2_compile_context_copy(pcre2_compile_context *ccontext)
244 {
245 pcre2_compile_context *newcontext =
246 ccontext->memctl.malloc(sizeof(pcre2_real_compile_context),
247 ccontext->memctl.memory_data);
248 if (newcontext == NULL) return NULL;
249 memcpy(newcontext, ccontext, sizeof(pcre2_real_compile_context));
250 return newcontext;
251 }
252
253
254 PCRE2_EXP_DEFN pcre2_match_context * PCRE2_CALL_CONVENTION
pcre2_match_context_copy(pcre2_match_context * mcontext)255 pcre2_match_context_copy(pcre2_match_context *mcontext)
256 {
257 pcre2_match_context *newcontext =
258 mcontext->memctl.malloc(sizeof(pcre2_real_match_context),
259 mcontext->memctl.memory_data);
260 if (newcontext == NULL) return NULL;
261 memcpy(newcontext, mcontext, sizeof(pcre2_real_match_context));
262 return newcontext;
263 }
264
265
266 PCRE2_EXP_DEFN pcre2_convert_context * PCRE2_CALL_CONVENTION
pcre2_convert_context_copy(pcre2_convert_context * ccontext)267 pcre2_convert_context_copy(pcre2_convert_context *ccontext)
268 {
269 pcre2_convert_context *newcontext =
270 ccontext->memctl.malloc(sizeof(pcre2_real_convert_context),
271 ccontext->memctl.memory_data);
272 if (newcontext == NULL) return NULL;
273 memcpy(newcontext, ccontext, sizeof(pcre2_real_convert_context));
274 return newcontext;
275 }
276
277
278 /*************************************************
279 * Context free functions *
280 *************************************************/
281
282 PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
pcre2_general_context_free(pcre2_general_context * gcontext)283 pcre2_general_context_free(pcre2_general_context *gcontext)
284 {
285 if (gcontext != NULL)
286 gcontext->memctl.free(gcontext, gcontext->memctl.memory_data);
287 }
288
289
290 PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
pcre2_compile_context_free(pcre2_compile_context * ccontext)291 pcre2_compile_context_free(pcre2_compile_context *ccontext)
292 {
293 if (ccontext != NULL)
294 ccontext->memctl.free(ccontext, ccontext->memctl.memory_data);
295 }
296
297
298 PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
pcre2_match_context_free(pcre2_match_context * mcontext)299 pcre2_match_context_free(pcre2_match_context *mcontext)
300 {
301 if (mcontext != NULL)
302 mcontext->memctl.free(mcontext, mcontext->memctl.memory_data);
303 }
304
305
306 PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
pcre2_convert_context_free(pcre2_convert_context * ccontext)307 pcre2_convert_context_free(pcre2_convert_context *ccontext)
308 {
309 if (ccontext != NULL)
310 ccontext->memctl.free(ccontext, ccontext->memctl.memory_data);
311 }
312
313
314 /*************************************************
315 * Set values in contexts *
316 *************************************************/
317
318 /* All these functions return 0 for success or PCRE2_ERROR_BADDATA if invalid
319 data is given. Only some of the functions are able to test the validity of the
320 data. */
321
322
323 /* ------------ Compile context ------------ */
324
325 PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
pcre2_set_character_tables(pcre2_compile_context * ccontext,const uint8_t * tables)326 pcre2_set_character_tables(pcre2_compile_context *ccontext,
327 const uint8_t *tables)
328 {
329 ccontext->tables = tables;
330 return 0;
331 }
332
333 PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
pcre2_set_bsr(pcre2_compile_context * ccontext,uint32_t value)334 pcre2_set_bsr(pcre2_compile_context *ccontext, uint32_t value)
335 {
336 switch(value)
337 {
338 case PCRE2_BSR_ANYCRLF:
339 case PCRE2_BSR_UNICODE:
340 ccontext->bsr_convention = value;
341 return 0;
342
343 default:
344 return PCRE2_ERROR_BADDATA;
345 }
346 }
347
348 PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
pcre2_set_max_pattern_length(pcre2_compile_context * ccontext,PCRE2_SIZE length)349 pcre2_set_max_pattern_length(pcre2_compile_context *ccontext, PCRE2_SIZE length)
350 {
351 ccontext->max_pattern_length = length;
352 return 0;
353 }
354
355 PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
pcre2_set_newline(pcre2_compile_context * ccontext,uint32_t newline)356 pcre2_set_newline(pcre2_compile_context *ccontext, uint32_t newline)
357 {
358 switch(newline)
359 {
360 case PCRE2_NEWLINE_CR:
361 case PCRE2_NEWLINE_LF:
362 case PCRE2_NEWLINE_CRLF:
363 case PCRE2_NEWLINE_ANY:
364 case PCRE2_NEWLINE_ANYCRLF:
365 case PCRE2_NEWLINE_NUL:
366 ccontext->newline_convention = newline;
367 return 0;
368
369 default:
370 return PCRE2_ERROR_BADDATA;
371 }
372 }
373
374 PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
pcre2_set_max_varlookbehind(pcre2_compile_context * ccontext,uint32_t limit)375 pcre2_set_max_varlookbehind(pcre2_compile_context *ccontext, uint32_t limit)
376 {
377 ccontext->max_varlookbehind = limit;
378 return 0;
379 }
380
381 PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
pcre2_set_parens_nest_limit(pcre2_compile_context * ccontext,uint32_t limit)382 pcre2_set_parens_nest_limit(pcre2_compile_context *ccontext, uint32_t limit)
383 {
384 ccontext->parens_nest_limit = limit;
385 return 0;
386 }
387
388 PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
pcre2_set_compile_extra_options(pcre2_compile_context * ccontext,uint32_t options)389 pcre2_set_compile_extra_options(pcre2_compile_context *ccontext, uint32_t options)
390 {
391 ccontext->extra_options = options;
392 return 0;
393 }
394
395 PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
pcre2_set_compile_recursion_guard(pcre2_compile_context * ccontext,int (* guard)(uint32_t,void *),void * user_data)396 pcre2_set_compile_recursion_guard(pcre2_compile_context *ccontext,
397 int (*guard)(uint32_t, void *), void *user_data)
398 {
399 ccontext->stack_guard = guard;
400 ccontext->stack_guard_data = user_data;
401 return 0;
402 }
403
404
405 /* ------------ Match context ------------ */
406
407 PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
pcre2_set_callout(pcre2_match_context * mcontext,int (* callout)(pcre2_callout_block *,void *),void * callout_data)408 pcre2_set_callout(pcre2_match_context *mcontext,
409 int (*callout)(pcre2_callout_block *, void *), void *callout_data)
410 {
411 mcontext->callout = callout;
412 mcontext->callout_data = callout_data;
413 return 0;
414 }
415
416 PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
pcre2_set_substitute_callout(pcre2_match_context * mcontext,int (* substitute_callout)(pcre2_substitute_callout_block *,void *),void * substitute_callout_data)417 pcre2_set_substitute_callout(pcre2_match_context *mcontext,
418 int (*substitute_callout)(pcre2_substitute_callout_block *, void *),
419 void *substitute_callout_data)
420 {
421 mcontext->substitute_callout = substitute_callout;
422 mcontext->substitute_callout_data = substitute_callout_data;
423 return 0;
424 }
425
426 PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
pcre2_set_heap_limit(pcre2_match_context * mcontext,uint32_t limit)427 pcre2_set_heap_limit(pcre2_match_context *mcontext, uint32_t limit)
428 {
429 mcontext->heap_limit = limit;
430 return 0;
431 }
432
433 PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
pcre2_set_match_limit(pcre2_match_context * mcontext,uint32_t limit)434 pcre2_set_match_limit(pcre2_match_context *mcontext, uint32_t limit)
435 {
436 mcontext->match_limit = limit;
437 return 0;
438 }
439
440 PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
pcre2_set_depth_limit(pcre2_match_context * mcontext,uint32_t limit)441 pcre2_set_depth_limit(pcre2_match_context *mcontext, uint32_t limit)
442 {
443 mcontext->depth_limit = limit;
444 return 0;
445 }
446
447 PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
pcre2_set_offset_limit(pcre2_match_context * mcontext,PCRE2_SIZE limit)448 pcre2_set_offset_limit(pcre2_match_context *mcontext, PCRE2_SIZE limit)
449 {
450 mcontext->offset_limit = limit;
451 return 0;
452 }
453
454 /* These functions became obsolete at release 10.30. The first is kept as a
455 synonym for backwards compatibility. The second now does nothing. Exclude both
456 from coverage reports. */
457
458 /* LCOV_EXCL_START */
459
460 PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
pcre2_set_recursion_limit(pcre2_match_context * mcontext,uint32_t limit)461 pcre2_set_recursion_limit(pcre2_match_context *mcontext, uint32_t limit)
462 {
463 return pcre2_set_depth_limit(mcontext, limit);
464 }
465
466 PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
pcre2_set_recursion_memory_management(pcre2_match_context * mcontext,void * (* mymalloc)(size_t,void *),void (* myfree)(void *,void *),void * mydata)467 pcre2_set_recursion_memory_management(pcre2_match_context *mcontext,
468 void *(*mymalloc)(size_t, void *), void (*myfree)(void *, void *),
469 void *mydata)
470 {
471 (void)mcontext;
472 (void)mymalloc;
473 (void)myfree;
474 (void)mydata;
475 return 0;
476 }
477
478 /* LCOV_EXCL_STOP */
479
480
481 /* ------------ Convert context ------------ */
482
483 PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
pcre2_set_glob_separator(pcre2_convert_context * ccontext,uint32_t separator)484 pcre2_set_glob_separator(pcre2_convert_context *ccontext, uint32_t separator)
485 {
486 if (separator != CHAR_SLASH && separator != CHAR_BACKSLASH &&
487 separator != CHAR_DOT) return PCRE2_ERROR_BADDATA;
488 ccontext->glob_separator = separator;
489 return 0;
490 }
491
492 PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
pcre2_set_glob_escape(pcre2_convert_context * ccontext,uint32_t escape)493 pcre2_set_glob_escape(pcre2_convert_context *ccontext, uint32_t escape)
494 {
495 if (escape > 255 || (escape != 0 && !ispunct(escape)))
496 return PCRE2_ERROR_BADDATA;
497 ccontext->glob_escape = escape;
498 return 0;
499 }
500
501 /* End of pcre2_context.c */
502
503