php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
pcre2_context.c
Go to the documentation of this file.
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#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
56static void *default_malloc(size_t size, void *data)
57{
58(void)data;
59return malloc(size);
60}
61
62
63static void default_free(void *block, void *data)
64{
65(void)data;
66free(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
76memory control data is to be stored at the start for future use.
77
78Arguments:
79 size amount of memory required
80 memctl pointer to a memctl block or NULL
81
82Returns: pointer to memory or NULL on failure
83*/
84
85extern void *
87{
88pcre2_memctl *newmemctl;
89void *yield = (memctl == NULL)? malloc(size) :
90 memctl->malloc(size, memctl->memory_data);
91if (yield == NULL) return NULL;
92newmemctl = (pcre2_memctl *)yield;
93if (memctl == NULL)
94 {
95 newmemctl->malloc = default_malloc;
96 newmemctl->free = default_free;
97 newmemctl->memory_data = NULL;
98 }
99else *newmemctl = *memctl;
100return 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
110functions so that these can be called from functions such as pcre2_compile()
111when an external context is not supplied. The initializing functions have an
112option to set up default memory management. */
113
115pcre2_general_context_create(void *(*private_malloc)(size_t, void *),
116 void (*private_free)(void *, void *), void *memory_data)
117{
118pcre2_general_context *gcontext;
119if (private_malloc == NULL) private_malloc = default_malloc;
120if (private_free == NULL) private_free = default_free;
121gcontext = private_malloc(sizeof(pcre2_real_general_context), memory_data);
122if (gcontext == NULL) return NULL;
123gcontext->memctl.malloc = private_malloc;
124gcontext->memctl.free = private_free;
125gcontext->memctl.memory_data = memory_data;
126return gcontext;
127}
128
129
130/* A default compile context is set up to save having to initialize at run time
131when no context is supplied to the compile function. */
132
133const 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 PCRE2_UNSET, /* Max pattern compiled length */
140 BSR_DEFAULT, /* Backslash R default */
141 NEWLINE_DEFAULT, /* Newline convention */
142 PARENS_NEST_LIMIT, /* As it says */
143 0, /* Extra options */
144 MAX_VARLOOKBEHIND /* As it says */
145 };
146
147/* The create function copies the default into the new memory, but must
148override the default memory handling functions if a gcontext was provided. */
149
152{
154 sizeof(pcre2_real_compile_context), (pcre2_memctl *)gcontext);
155if (ccontext == NULL) return NULL;
156*ccontext = PRIV(default_compile_context);
157if (gcontext != NULL)
158 *((pcre2_memctl *)ccontext) = *((pcre2_memctl *)gcontext);
159return ccontext;
160}
161
162
163/* A default match context is set up to save having to initialize at run time
164when no context is supplied to a match function. */
165
166const pcre2_match_context PRIV(default_match_context) = {
167 { default_malloc, default_free, NULL },
168#ifdef SUPPORT_JIT
169 NULL, /* JIT callback */
170 NULL, /* JIT callback data */
171#endif
172 NULL, /* Callout function */
173 NULL, /* Callout data */
174 NULL, /* Substitute callout function */
175 NULL, /* Substitute callout data */
176 PCRE2_UNSET, /* Offset limit */
180
181/* The create function copies the default into the new memory, but must
182override the default memory handling functions if a gcontext was provided. */
183
186{
188 sizeof(pcre2_real_match_context), (pcre2_memctl *)gcontext);
189if (mcontext == NULL) return NULL;
190*mcontext = PRIV(default_match_context);
191if (gcontext != NULL)
192 *((pcre2_memctl *)mcontext) = *((pcre2_memctl *)gcontext);
193return mcontext;
194}
195
196
197/* A default convert context is set up to save having to initialize at run time
198when no context is supplied to the convert function. */
199
200const pcre2_convert_context PRIV(default_convert_context) = {
201 { default_malloc, default_free, NULL }, /* Default memory handling */
202#ifdef _WIN32
203 CHAR_BACKSLASH, /* Default path separator */
204 CHAR_GRAVE_ACCENT /* Default escape character */
205#else /* Not Windows */
206 CHAR_SLASH, /* Default path separator */
207 CHAR_BACKSLASH /* Default escape character */
208#endif
209 };
210
211/* The create function copies the default into the new memory, but must
212override the default memory handling functions if a gcontext was provided. */
213
216{
218 sizeof(pcre2_real_convert_context), (pcre2_memctl *)gcontext);
219if (ccontext == NULL) return NULL;
220*ccontext = PRIV(default_convert_context);
221if (gcontext != NULL)
222 *((pcre2_memctl *)ccontext) = *((pcre2_memctl *)gcontext);
223return ccontext;
224}
225
226
227/*************************************************
228* Context copy functions *
229*************************************************/
230
233{
234pcre2_general_context *newcontext =
235 gcontext->memctl.malloc(sizeof(pcre2_real_general_context),
236 gcontext->memctl.memory_data);
237if (newcontext == NULL) return NULL;
238memcpy(newcontext, gcontext, sizeof(pcre2_real_general_context));
239return newcontext;
240}
241
242
245{
246pcre2_compile_context *newcontext =
247 ccontext->memctl.malloc(sizeof(pcre2_real_compile_context),
248 ccontext->memctl.memory_data);
249if (newcontext == NULL) return NULL;
250memcpy(newcontext, ccontext, sizeof(pcre2_real_compile_context));
251return newcontext;
252}
253
254
257{
258pcre2_match_context *newcontext =
259 mcontext->memctl.malloc(sizeof(pcre2_real_match_context),
260 mcontext->memctl.memory_data);
261if (newcontext == NULL) return NULL;
262memcpy(newcontext, mcontext, sizeof(pcre2_real_match_context));
263return newcontext;
264}
265
266
269{
270pcre2_convert_context *newcontext =
271 ccontext->memctl.malloc(sizeof(pcre2_real_convert_context),
272 ccontext->memctl.memory_data);
273if (newcontext == NULL) return NULL;
274memcpy(newcontext, ccontext, sizeof(pcre2_real_convert_context));
275return newcontext;
276}
277
278
279/*************************************************
280* Context free functions *
281*************************************************/
282
285{
286if (gcontext != NULL)
287 gcontext->memctl.free(gcontext, gcontext->memctl.memory_data);
288}
289
290
293{
294if (ccontext != NULL)
295 ccontext->memctl.free(ccontext, ccontext->memctl.memory_data);
296}
297
298
301{
302if (mcontext != NULL)
303 mcontext->memctl.free(mcontext, mcontext->memctl.memory_data);
304}
305
306
309{
310if (ccontext != NULL)
311 ccontext->memctl.free(ccontext, ccontext->memctl.memory_data);
312}
313
314
315/*************************************************
316* Set values in contexts *
317*************************************************/
318
319/* All these functions return 0 for success or PCRE2_ERROR_BADDATA if invalid
320data is given. Only some of the functions are able to test the validity of the
321data. */
322
323
324/* ------------ Compile context ------------ */
325
328 const uint8_t *tables)
329{
330ccontext->tables = tables;
331return 0;
332}
333
336{
337switch(value)
338 {
341 ccontext->bsr_convention = value;
342 return 0;
343
344 default:
345 return PCRE2_ERROR_BADDATA;
346 }
347}
348
351{
352ccontext->max_pattern_length = length;
353return 0;
354}
355
358{
359ccontext->max_pattern_compiled_length = length;
360return 0;
361}
362
364pcre2_set_newline(pcre2_compile_context *ccontext, uint32_t newline)
365{
366switch(newline)
367 {
368 case PCRE2_NEWLINE_CR:
369 case PCRE2_NEWLINE_LF:
374 ccontext->newline_convention = newline;
375 return 0;
376
377 default:
378 return PCRE2_ERROR_BADDATA;
379 }
380}
381
384{
385ccontext->max_varlookbehind = limit;
386return 0;
387}
388
391{
392ccontext->parens_nest_limit = limit;
393return 0;
394}
395
398{
399ccontext->extra_options = options;
400return 0;
401}
402
405 int (*guard)(uint32_t, void *), void *user_data)
406{
407ccontext->stack_guard = guard;
408ccontext->stack_guard_data = user_data;
409return 0;
410}
411
412
413/* ------------ Match context ------------ */
414
417 int (*callout)(pcre2_callout_block *, void *), void *callout_data)
418{
419mcontext->callout = callout;
420mcontext->callout_data = callout_data;
421return 0;
422}
423
426 int (*substitute_callout)(pcre2_substitute_callout_block *, void *),
427 void *substitute_callout_data)
428{
429mcontext->substitute_callout = substitute_callout;
430mcontext->substitute_callout_data = substitute_callout_data;
431return 0;
432}
433
436{
437mcontext->heap_limit = limit;
438return 0;
439}
440
443{
444mcontext->match_limit = limit;
445return 0;
446}
447
450{
451mcontext->depth_limit = limit;
452return 0;
453}
454
457{
458mcontext->offset_limit = limit;
459return 0;
460}
461
462/* These functions became obsolete at release 10.30. The first is kept as a
463synonym for backwards compatibility. The second now does nothing. Exclude both
464from coverage reports. */
465
466/* LCOV_EXCL_START */
467
470{
471return pcre2_set_depth_limit(mcontext, limit);
472}
473
476 void *(*mymalloc)(size_t, void *), void (*myfree)(void *, void *),
477 void *mydata)
478{
479(void)mcontext;
480(void)mymalloc;
481(void)myfree;
482(void)mydata;
483return 0;
484}
485
486/* LCOV_EXCL_STOP */
487
488
489/* ------------ Convert context ------------ */
490
493{
494if (separator != CHAR_SLASH && separator != CHAR_BACKSLASH &&
495 separator != CHAR_DOT) return PCRE2_ERROR_BADDATA;
496ccontext->glob_separator = separator;
497return 0;
498}
499
502{
503if (escape > 255 || (escape != 0 && !ispunct(escape)))
504 return PCRE2_ERROR_BADDATA;
505ccontext->glob_escape = escape;
506return 0;
507}
508
509/* End of pcre2_context.c */
510
new_type size
Definition ffi.c:4365
memcpy(ptr1, ptr2, size)
#define NULL
Definition gdcache.h:45
#define pcre2_compile_context_copy
Definition pcre2.h:854
#define pcre2_convert_context
Definition pcre2.h:842
#define pcre2_set_offset_limit
Definition pcre2.h:909
#define pcre2_real_general_context
Definition pcre2.h:827
#define PCRE2_BSR_ANYCRLF
Definition pcre2.h:218
#define pcre2_set_compile_extra_options
Definition pcre2.h:897
#define pcre2_convert_context_free
Definition pcre2.h:860
#define pcre2_general_context
Definition pcre2.h:840
#define pcre2_set_bsr
Definition pcre2.h:894
#define PCRE2_UNSET
Definition pcre2.h:482
#define pcre2_set_callout
Definition pcre2.h:895
#define pcre2_set_heap_limit
Definition pcre2.h:902
#define pcre2_set_substitute_callout
Definition pcre2.h:910
#define PCRE2_NEWLINE_ANYCRLF
Definition pcre2.h:214
#define pcre2_general_context_free
Definition pcre2.h:865
#define pcre2_set_max_pattern_compiled_length
Definition pcre2.h:906
#define pcre2_match_context_free
Definition pcre2.h:884
#define pcre2_set_glob_escape
Definition pcre2.h:900
#define pcre2_set_match_limit
Definition pcre2.h:903
#define pcre2_compile_context_free
Definition pcre2.h:856
#define PCRE2_NEWLINE_CR
Definition pcre2.h:210
#define pcre2_substitute_callout_block
Definition pcre2.h:839
#define pcre2_real_compile_context
Definition pcre2.h:828
#define pcre2_general_context_copy
Definition pcre2.h:863
#define pcre2_set_compile_recursion_guard
Definition pcre2.h:898
#define PCRE2_SIZE
Definition pcre2.h:479
#define pcre2_set_character_tables
Definition pcre2.h:896
#define pcre2_set_depth_limit
Definition pcre2.h:899
#define pcre2_general_context_create
Definition pcre2.h:864
#define pcre2_real_match_context
Definition pcre2.h:830
#define pcre2_match_context
Definition pcre2.h:843
#define pcre2_set_newline
Definition pcre2.h:907
#define pcre2_convert_context_copy
Definition pcre2.h:858
#define pcre2_set_parens_nest_limit
Definition pcre2.h:908
#define PCRE2_CALL_CONVENTION
Definition pcre2.h:81
#define pcre2_set_glob_separator
Definition pcre2.h:901
#define PCRE2_ERROR_BADDATA
Definition pcre2.h:370
#define PCRE2_NEWLINE_CRLF
Definition pcre2.h:212
#define pcre2_set_recursion_limit
Definition pcre2.h:925
#define pcre2_set_recursion_memory_management
Definition pcre2.h:928
#define PCRE2_NEWLINE_NUL
Definition pcre2.h:215
#define pcre2_convert_context_create
Definition pcre2.h:859
#define pcre2_set_max_pattern_length
Definition pcre2.h:905
#define pcre2_set_max_varlookbehind
Definition pcre2.h:904
#define pcre2_match_context_create
Definition pcre2.h:883
#define PCRE2_BSR_UNICODE
Definition pcre2.h:217
#define pcre2_match_context_copy
Definition pcre2.h:882
#define pcre2_real_convert_context
Definition pcre2.h:829
#define pcre2_callout_block
Definition pcre2.h:837
#define pcre2_compile_context_create
Definition pcre2.h:855
#define PCRE2_NEWLINE_ANY
Definition pcre2.h:213
#define PCRE2_NEWLINE_LF
Definition pcre2.h:211
#define pcre2_compile_context
Definition pcre2.h:841
void *PRIV memctl_malloc(size_t size, pcre2_memctl *memctl)
#define CHAR_DOT
#define CHAR_GRAVE_ACCENT
#define CHAR_BACKSLASH
#define CHAR_SLASH
#define BSR_DEFAULT
#define PCRE2_EXP_DEFN
#define PRIV(name)
#define MATCH_LIMIT_DEPTH
Definition config.h:64
#define NEWLINE_DEFAULT
Definition config.h:89
#define MAX_VARLOOKBEHIND
Definition config.h:104
#define HEAP_LIMIT
Definition config.h:35
#define MATCH_LIMIT
Definition config.h:52
#define PARENS_NEST_LIMIT
Definition config.h:42
PHP_JSON_API size_t int options
Definition php_json.h:102
zend_constant * data
void *(* malloc)(size_t, void *)
void(* free)(void *, void *)
ZEND_API void(ZEND_FASTCALL *zend_touch_vm_stack_data)(void *vm_stack_data)
value