Skip to content
0.2.0-dev Download ZIP
557 lines ·17.4 KB Raw
1/*************************************************
2* Perl-Compatible Regular Expressions *
3*************************************************/
4
5/* PCRE is a library of functions to support regular expressions whose syntax
6and 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-2024 University of Cambridge
11
12-----------------------------------------------------------------------------
13Redistribution and use in source and binary forms, with or without
14modification, 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
27THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
31LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37POSSIBILITY OF SUCH DAMAGE.
38-----------------------------------------------------------------------------
39*/
40
41
42#include "pcre2_internal.h"
43
44
45
46/*************************************************
47* Default malloc/free functions *
48*************************************************/
49
50/* Ignore the "user data" argument in each case. */
51
52static void *default_malloc(size_t size, void *data)
53{
54(void)data;
55return malloc(size);
56}
57
58
59static void default_free(void *block, void *data)
60{
61(void)data;
62free(block);
63}
64
65
66
67/*************************************************
68* Get a block and save memory control *
69*************************************************/
70
71/* This internal function is called to get a block of memory in which the
72memory control data is to be stored at the start for future use.
73
74Arguments:
75 size amount of memory required
76 memctl pointer to a memctl block or NULL
77
78Returns: pointer to memory or NULL on failure
79*/
80
81extern void *
82PRIV(memctl_malloc)(size_t size, pcre2_memctl *memctl)
83{
84pcre2_memctl *newmemctl;
85void *yield = (memctl == NULL)? malloc(size) :
86 memctl->malloc(size, memctl->memory_data);
87if (yield == NULL) return NULL;
88newmemctl = (pcre2_memctl *)yield;
89if (memctl == NULL)
90 {
91 newmemctl->malloc = default_malloc;
92 newmemctl->free = default_free;
93 newmemctl->memory_data = NULL;
94 }
95else *newmemctl = *memctl;
96return yield;
97}
98
99
100
101/*************************************************
102* Create and initialize contexts *
103*************************************************/
104
105/* Initializing for compile and match contexts is done in separate, private
106functions so that these can be called from functions such as pcre2_compile()
107when an external context is not supplied. The initializing functions have an
108option to set up default memory management. */
109
110PCRE2_EXP_DEFN pcre2_general_context * PCRE2_CALL_CONVENTION
111pcre2_general_context_create(void *(*private_malloc)(size_t, void *),
112 void (*private_free)(void *, void *), void *memory_data)
113{
114pcre2_general_context *gcontext;
115if (private_malloc == NULL) private_malloc = default_malloc;
116if (private_free == NULL) private_free = default_free;
117gcontext = private_malloc(sizeof(pcre2_real_general_context), memory_data);
118if (gcontext == NULL) return NULL;
119gcontext->memctl.malloc = private_malloc;
120gcontext->memctl.free = private_free;
121gcontext->memctl.memory_data = memory_data;
122return gcontext;
123}
124
125
126/* A default compile context is set up to save having to initialize at run time
127when no context is supplied to the compile function. */
128
129pcre2_compile_context PRIV(default_compile_context) = {
130 { default_malloc, default_free, NULL }, /* Default memory handling */
131 NULL, /* Stack guard */
132 NULL, /* Stack guard data */
133 PRIV(default_tables), /* Character tables */
134 PCRE2_UNSET, /* Max pattern length */
135 PCRE2_UNSET, /* Max pattern compiled length */
136 BSR_DEFAULT, /* Backslash R default */
137 NEWLINE_DEFAULT, /* Newline convention */
138 PARENS_NEST_LIMIT, /* As it says */
139 0, /* Extra options */
140 MAX_VARLOOKBEHIND, /* As it says */
141 PCRE2_OPTIMIZATION_ALL /* All optimizations enabled */
142 };
143
144/* The create function copies the default into the new memory, but must
145override the default memory handling functions if a gcontext was provided. */
146
147PCRE2_EXP_DEFN pcre2_compile_context * PCRE2_CALL_CONVENTION
148pcre2_compile_context_create(pcre2_general_context *gcontext)
149{
150pcre2_compile_context *ccontext = PRIV(memctl_malloc)(
151 sizeof(pcre2_real_compile_context), (pcre2_memctl *)gcontext);
152if (ccontext == NULL) return NULL;
153*ccontext = PRIV(default_compile_context);
154if (gcontext != NULL)
155 *((pcre2_memctl *)ccontext) = *((pcre2_memctl *)gcontext);
156return ccontext;
157}
158
159
160/* A default match context is set up to save having to initialize at run time
161when no context is supplied to a match function. */
162
163pcre2_match_context PRIV(default_match_context) = {
164 { default_malloc, default_free, NULL },
165#ifdef SUPPORT_JIT
166 NULL, /* JIT callback */
167 NULL, /* JIT callback data */
168#endif
169 NULL, /* Callout function */
170 NULL, /* Callout data */
171 NULL, /* Substitute callout function */
172 NULL, /* Substitute callout data */
173 NULL, /* Substitute case callout function */
174 NULL, /* Substitute case 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
181override the default memory handling functions if a gcontext was provided. */
182
183PCRE2_EXP_DEFN pcre2_match_context * PCRE2_CALL_CONVENTION
184pcre2_match_context_create(pcre2_general_context *gcontext)
185{
186pcre2_match_context *mcontext = PRIV(memctl_malloc)(
187 sizeof(pcre2_real_match_context), (pcre2_memctl *)gcontext);
188if (mcontext == NULL) return NULL;
189*mcontext = PRIV(default_match_context);
190if (gcontext != NULL)
191 *((pcre2_memctl *)mcontext) = *((pcre2_memctl *)gcontext);
192return mcontext;
193}
194
195
196/* A default convert context is set up to save having to initialize at run time
197when no context is supplied to the convert function. */
198
199pcre2_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
211override the default memory handling functions if a gcontext was provided. */
212
213PCRE2_EXP_DEFN pcre2_convert_context * PCRE2_CALL_CONVENTION
214pcre2_convert_context_create(pcre2_general_context *gcontext)
215{
216pcre2_convert_context *ccontext = PRIV(memctl_malloc)(
217 sizeof(pcre2_real_convert_context), (pcre2_memctl *)gcontext);
218if (ccontext == NULL) return NULL;
219*ccontext = PRIV(default_convert_context);
220if (gcontext != NULL)
221 *((pcre2_memctl *)ccontext) = *((pcre2_memctl *)gcontext);
222return ccontext;
223}
224
225
226/*************************************************
227* Context copy functions *
228*************************************************/
229
230PCRE2_EXP_DEFN pcre2_general_context * PCRE2_CALL_CONVENTION
231pcre2_general_context_copy(pcre2_general_context *gcontext)
232{
233pcre2_general_context *newcontext =
234 gcontext->memctl.malloc(sizeof(pcre2_real_general_context),
235 gcontext->memctl.memory_data);
236if (newcontext == NULL) return NULL;
237memcpy(newcontext, gcontext, sizeof(pcre2_real_general_context));
238return newcontext;
239}
240
241
242PCRE2_EXP_DEFN pcre2_compile_context * PCRE2_CALL_CONVENTION
243pcre2_compile_context_copy(pcre2_compile_context *ccontext)
244{
245pcre2_compile_context *newcontext =
246 ccontext->memctl.malloc(sizeof(pcre2_real_compile_context),
247 ccontext->memctl.memory_data);
248if (newcontext == NULL) return NULL;
249memcpy(newcontext, ccontext, sizeof(pcre2_real_compile_context));
250return newcontext;
251}
252
253
254PCRE2_EXP_DEFN pcre2_match_context * PCRE2_CALL_CONVENTION
255pcre2_match_context_copy(pcre2_match_context *mcontext)
256{
257pcre2_match_context *newcontext =
258 mcontext->memctl.malloc(sizeof(pcre2_real_match_context),
259 mcontext->memctl.memory_data);
260if (newcontext == NULL) return NULL;
261memcpy(newcontext, mcontext, sizeof(pcre2_real_match_context));
262return newcontext;
263}
264
265
266PCRE2_EXP_DEFN pcre2_convert_context * PCRE2_CALL_CONVENTION
267pcre2_convert_context_copy(pcre2_convert_context *ccontext)
268{
269pcre2_convert_context *newcontext =
270 ccontext->memctl.malloc(sizeof(pcre2_real_convert_context),
271 ccontext->memctl.memory_data);
272if (newcontext == NULL) return NULL;
273memcpy(newcontext, ccontext, sizeof(pcre2_real_convert_context));
274return newcontext;
275}
276
277
278/*************************************************
279* Context free functions *
280*************************************************/
281
282PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
283pcre2_general_context_free(pcre2_general_context *gcontext)
284{
285if (gcontext != NULL)
286 gcontext->memctl.free(gcontext, gcontext->memctl.memory_data);
287}
288
289
290PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
291pcre2_compile_context_free(pcre2_compile_context *ccontext)
292{
293if (ccontext != NULL)
294 ccontext->memctl.free(ccontext, ccontext->memctl.memory_data);
295}
296
297
298PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
299pcre2_match_context_free(pcre2_match_context *mcontext)
300{
301if (mcontext != NULL)
302 mcontext->memctl.free(mcontext, mcontext->memctl.memory_data);
303}
304
305
306PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
307pcre2_convert_context_free(pcre2_convert_context *ccontext)
308{
309if (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
319data is given. Only some of the functions are able to test the validity of the
320data. */
321
322
323/* ------------ Compile context ------------ */
324
325PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
326pcre2_set_character_tables(pcre2_compile_context *ccontext,
327 const uint8_t *tables)
328{
329ccontext->tables = tables;
330return 0;
331}
332
333PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
334pcre2_set_bsr(pcre2_compile_context *ccontext, uint32_t value)
335{
336switch(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
348PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
349pcre2_set_max_pattern_length(pcre2_compile_context *ccontext, PCRE2_SIZE length)
350{
351ccontext->max_pattern_length = length;
352return 0;
353}
354
355PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
356pcre2_set_max_pattern_compiled_length(pcre2_compile_context *ccontext, PCRE2_SIZE length)
357{
358ccontext->max_pattern_compiled_length = length;
359return 0;
360}
361
362PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
363pcre2_set_newline(pcre2_compile_context *ccontext, uint32_t newline)
364{
365switch(newline)
366 {
367 case PCRE2_NEWLINE_CR:
368 case PCRE2_NEWLINE_LF:
369 case PCRE2_NEWLINE_CRLF:
370 case PCRE2_NEWLINE_ANY:
371 case PCRE2_NEWLINE_ANYCRLF:
372 case PCRE2_NEWLINE_NUL:
373 ccontext->newline_convention = newline;
374 return 0;
375
376 default:
377 return PCRE2_ERROR_BADDATA;
378 }
379}
380
381PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
382pcre2_set_max_varlookbehind(pcre2_compile_context *ccontext, uint32_t limit)
383{
384ccontext->max_varlookbehind = limit;
385return 0;
386}
387
388PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
389pcre2_set_parens_nest_limit(pcre2_compile_context *ccontext, uint32_t limit)
390{
391ccontext->parens_nest_limit = limit;
392return 0;
393}
394
395PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
396pcre2_set_compile_extra_options(pcre2_compile_context *ccontext, uint32_t options)
397{
398ccontext->extra_options = options;
399return 0;
400}
401
402PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
403pcre2_set_compile_recursion_guard(pcre2_compile_context *ccontext,
404 int (*guard)(uint32_t, void *), void *user_data)
405{
406ccontext->stack_guard = guard;
407ccontext->stack_guard_data = user_data;
408return 0;
409}
410
411PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
412pcre2_set_optimize(pcre2_compile_context *ccontext, uint32_t directive)
413{
414if (ccontext == NULL)
415 return PCRE2_ERROR_NULL;
416
417switch (directive)
418 {
419 case PCRE2_OPTIMIZATION_NONE:
420 ccontext->optimization_flags = 0;
421 break;
422
423 case PCRE2_OPTIMIZATION_FULL:
424 ccontext->optimization_flags = PCRE2_OPTIMIZATION_ALL;
425 break;
426
427 default:
428 if (directive >= PCRE2_AUTO_POSSESS && directive <= PCRE2_START_OPTIMIZE_OFF)
429 {
430 /* Even directive numbers starting from 64 switch a bit on;
431 * Odd directive numbers starting from 65 switch a bit off */
432 if ((directive & 1) != 0)
433 ccontext->optimization_flags &= ~(1u << ((directive >> 1) - 32));
434 else
435 ccontext->optimization_flags |= 1u << ((directive >> 1) - 32);
436 return 0;
437 }
438 return PCRE2_ERROR_BADOPTION;
439 }
440
441return 0;
442}
443
444/* ------------ Match context ------------ */
445
446PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
447pcre2_set_callout(pcre2_match_context *mcontext,
448 int (*callout)(pcre2_callout_block *, void *), void *callout_data)
449{
450mcontext->callout = callout;
451mcontext->callout_data = callout_data;
452return 0;
453}
454
455PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
456pcre2_set_substitute_callout(pcre2_match_context *mcontext,
457 int (*substitute_callout)(pcre2_substitute_callout_block *, void *),
458 void *substitute_callout_data)
459{
460mcontext->substitute_callout = substitute_callout;
461mcontext->substitute_callout_data = substitute_callout_data;
462return 0;
463}
464
465PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
466pcre2_set_substitute_case_callout(pcre2_match_context *mcontext,
467 PCRE2_SIZE (*substitute_case_callout)(PCRE2_SPTR, PCRE2_SIZE, PCRE2_UCHAR *,
468 PCRE2_SIZE, int, void *),
469 void *substitute_case_callout_data)
470{
471mcontext->substitute_case_callout = substitute_case_callout;
472mcontext->substitute_case_callout_data = substitute_case_callout_data;
473return 0;
474}
475
476PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
477pcre2_set_heap_limit(pcre2_match_context *mcontext, uint32_t limit)
478{
479mcontext->heap_limit = limit;
480return 0;
481}
482
483PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
484pcre2_set_match_limit(pcre2_match_context *mcontext, uint32_t limit)
485{
486mcontext->match_limit = limit;
487return 0;
488}
489
490PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
491pcre2_set_depth_limit(pcre2_match_context *mcontext, uint32_t limit)
492{
493mcontext->depth_limit = limit;
494return 0;
495}
496
497PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
498pcre2_set_offset_limit(pcre2_match_context *mcontext, PCRE2_SIZE limit)
499{
500mcontext->offset_limit = limit;
501return 0;
502}
503
504/* These functions became obsolete at release 10.30. The first is kept as a
505synonym for backwards compatibility. The second now does nothing. */
506
507PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
508pcre2_set_recursion_limit(pcre2_match_context *mcontext, uint32_t limit)
509{
510return pcre2_set_depth_limit(mcontext, limit);
511}
512
513PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
514pcre2_set_recursion_memory_management(pcre2_match_context *mcontext,
515 void *(*mymalloc)(size_t, void *), void (*myfree)(void *, void *),
516 void *mydata)
517{
518(void)mcontext;
519(void)mymalloc;
520(void)myfree;
521(void)mydata;
522return 0;
523}
524
525
526/* ------------ Convert context ------------ */
527
528PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
529pcre2_set_glob_separator(pcre2_convert_context *ccontext, uint32_t separator)
530{
531if (separator != CHAR_SLASH && separator != CHAR_BACKSLASH &&
532 separator != CHAR_DOT) return PCRE2_ERROR_BADDATA;
533ccontext->glob_separator = separator;
534return 0;
535}
536
537static const char *globpunct =
538 STR_EXCLAMATION_MARK STR_QUOTATION_MARK STR_NUMBER_SIGN STR_DOLLAR_SIGN
539 STR_PERCENT_SIGN STR_AMPERSAND STR_APOSTROPHE STR_LEFT_PARENTHESIS
540 STR_RIGHT_PARENTHESIS STR_ASTERISK STR_PLUS STR_COMMA STR_MINUS STR_DOT
541 STR_SLASH STR_COLON STR_SEMICOLON STR_LESS_THAN_SIGN STR_EQUALS_SIGN
542 STR_GREATER_THAN_SIGN STR_QUESTION_MARK STR_COMMERCIAL_AT
543 STR_LEFT_SQUARE_BRACKET STR_BACKSLASH STR_RIGHT_SQUARE_BRACKET
544 STR_CIRCUMFLEX_ACCENT STR_UNDERSCORE STR_GRAVE_ACCENT STR_LEFT_CURLY_BRACKET
545 STR_VERTICAL_LINE STR_RIGHT_CURLY_BRACKET STR_TILDE;
546
547PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
548pcre2_set_glob_escape(pcre2_convert_context *ccontext, uint32_t escape)
549{
550if (escape > 255 || (escape != 0 && strchr(globpunct, escape) == NULL))
551 return PCRE2_ERROR_BADDATA;
552ccontext->glob_escape = escape;
553return 0;
554}
555
556/* End of pcre2_context.c */
557