blob: f904a494a018bbbc7f54ec7b52e8f9aa99d56587 [file] [log] [blame]
Janis Danisevskis112c9cc2016-03-31 13:35:25 +01001/*************************************************
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
Elliott Hughes0c26e192019-08-07 12:24:46 -070010 New API code Copyright (c) 2016-2018 University of Cambridge
Janis Danisevskis112c9cc2016-03-31 13:35:25 +010011
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 *
86PRIV(memctl_malloc)(size_t size, pcre2_memctl *memctl)
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
114PCRE2_EXP_DEFN pcre2_general_context * PCRE2_CALL_CONVENTION
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 BSR_DEFAULT, /* Backslash R default */
140 NEWLINE_DEFAULT, /* Newline convention */
Elliott Hughes9bc971b2018-07-27 13:23:14 -0700141 PARENS_NEST_LIMIT, /* As it says */
142 0 }; /* Extra options */
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100143
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
163const pcre2_match_context PRIV(default_match_context) = {
164 { default_malloc, default_free, NULL },
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100165#ifdef SUPPORT_JIT
Elliott Hughes0c26e192019-08-07 12:24:46 -0700166 NULL, /* JIT callback */
167 NULL, /* JIT callback data */
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100168#endif
Elliott Hughes0c26e192019-08-07 12:24:46 -0700169 NULL, /* Callout function */
170 NULL, /* Callout data */
171 NULL, /* Substitute callout function */
172 NULL, /* Substitute callout data */
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100173 PCRE2_UNSET, /* Offset limit */
Elliott Hughes9bc971b2018-07-27 13:23:14 -0700174 HEAP_LIMIT,
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100175 MATCH_LIMIT,
Elliott Hughes9bc971b2018-07-27 13:23:14 -0700176 MATCH_LIMIT_DEPTH };
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100177
178/* The create function copies the default into the new memory, but must
179override the default memory handling functions if a gcontext was provided. */
180
181PCRE2_EXP_DEFN pcre2_match_context * PCRE2_CALL_CONVENTION
182pcre2_match_context_create(pcre2_general_context *gcontext)
183{
184pcre2_match_context *mcontext = PRIV(memctl_malloc)(
185 sizeof(pcre2_real_match_context), (pcre2_memctl *)gcontext);
186if (mcontext == NULL) return NULL;
187*mcontext = PRIV(default_match_context);
188if (gcontext != NULL)
189 *((pcre2_memctl *)mcontext) = *((pcre2_memctl *)gcontext);
190return mcontext;
191}
192
193
Elliott Hughes9bc971b2018-07-27 13:23:14 -0700194/* A default convert context is set up to save having to initialize at run time
195when no context is supplied to the convert function. */
196
197const pcre2_convert_context PRIV(default_convert_context) = {
198 { default_malloc, default_free, NULL }, /* Default memory handling */
199#ifdef _WIN32
200 CHAR_BACKSLASH, /* Default path separator */
201 CHAR_GRAVE_ACCENT /* Default escape character */
202#else /* Not Windows */
203 CHAR_SLASH, /* Default path separator */
204 CHAR_BACKSLASH /* Default escape character */
205#endif
206 };
207
208/* The create function copies the default into the new memory, but must
209override the default memory handling functions if a gcontext was provided. */
210
211PCRE2_EXP_DEFN pcre2_convert_context * PCRE2_CALL_CONVENTION
212pcre2_convert_context_create(pcre2_general_context *gcontext)
213{
214pcre2_convert_context *ccontext = PRIV(memctl_malloc)(
215 sizeof(pcre2_real_convert_context), (pcre2_memctl *)gcontext);
216if (ccontext == NULL) return NULL;
217*ccontext = PRIV(default_convert_context);
218if (gcontext != NULL)
219 *((pcre2_memctl *)ccontext) = *((pcre2_memctl *)gcontext);
220return ccontext;
221}
222
223
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100224/*************************************************
225* Context copy functions *
226*************************************************/
227
228PCRE2_EXP_DEFN pcre2_general_context * PCRE2_CALL_CONVENTION
229pcre2_general_context_copy(pcre2_general_context *gcontext)
230{
231pcre2_general_context *new =
232 gcontext->memctl.malloc(sizeof(pcre2_real_general_context),
233 gcontext->memctl.memory_data);
234if (new == NULL) return NULL;
235memcpy(new, gcontext, sizeof(pcre2_real_general_context));
236return new;
237}
238
239
240PCRE2_EXP_DEFN pcre2_compile_context * PCRE2_CALL_CONVENTION
241pcre2_compile_context_copy(pcre2_compile_context *ccontext)
242{
243pcre2_compile_context *new =
244 ccontext->memctl.malloc(sizeof(pcre2_real_compile_context),
245 ccontext->memctl.memory_data);
246if (new == NULL) return NULL;
247memcpy(new, ccontext, sizeof(pcre2_real_compile_context));
248return new;
249}
250
251
252PCRE2_EXP_DEFN pcre2_match_context * PCRE2_CALL_CONVENTION
253pcre2_match_context_copy(pcre2_match_context *mcontext)
254{
255pcre2_match_context *new =
256 mcontext->memctl.malloc(sizeof(pcre2_real_match_context),
257 mcontext->memctl.memory_data);
258if (new == NULL) return NULL;
259memcpy(new, mcontext, sizeof(pcre2_real_match_context));
260return new;
261}
262
263
264
Elliott Hughes9bc971b2018-07-27 13:23:14 -0700265PCRE2_EXP_DEFN pcre2_convert_context * PCRE2_CALL_CONVENTION
266pcre2_convert_context_copy(pcre2_convert_context *ccontext)
267{
268pcre2_convert_context *new =
269 ccontext->memctl.malloc(sizeof(pcre2_real_convert_context),
270 ccontext->memctl.memory_data);
271if (new == NULL) return NULL;
272memcpy(new, ccontext, sizeof(pcre2_real_convert_context));
273return new;
274}
275
276
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100277/*************************************************
278* Context free functions *
279*************************************************/
280
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100281PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
282pcre2_general_context_free(pcre2_general_context *gcontext)
283{
284if (gcontext != NULL)
285 gcontext->memctl.free(gcontext, gcontext->memctl.memory_data);
286}
287
288
289PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
290pcre2_compile_context_free(pcre2_compile_context *ccontext)
291{
292if (ccontext != NULL)
293 ccontext->memctl.free(ccontext, ccontext->memctl.memory_data);
294}
295
296
297PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
298pcre2_match_context_free(pcre2_match_context *mcontext)
299{
300if (mcontext != NULL)
301 mcontext->memctl.free(mcontext, mcontext->memctl.memory_data);
302}
303
304
Elliott Hughes9bc971b2018-07-27 13:23:14 -0700305PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
306pcre2_convert_context_free(pcre2_convert_context *ccontext)
307{
308if (ccontext != NULL)
309 ccontext->memctl.free(ccontext, ccontext->memctl.memory_data);
310}
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100311
312
313/*************************************************
314* Set values in contexts *
315*************************************************/
316
317/* All these functions return 0 for success or PCRE2_ERROR_BADDATA if invalid
318data is given. Only some of the functions are able to test the validity of the
319data. */
320
321
Elliott Hughes9bc971b2018-07-27 13:23:14 -0700322/* ------------ Compile context ------------ */
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100323
324PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
325pcre2_set_character_tables(pcre2_compile_context *ccontext,
Elliott Hughes2dbd7d22020-06-03 14:32:37 -0700326 const uint8_t *tables)
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100327{
328ccontext->tables = tables;
329return 0;
330}
331
332PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
333pcre2_set_bsr(pcre2_compile_context *ccontext, uint32_t value)
334{
335switch(value)
336 {
337 case PCRE2_BSR_ANYCRLF:
338 case PCRE2_BSR_UNICODE:
339 ccontext->bsr_convention = value;
340 return 0;
341
342 default:
343 return PCRE2_ERROR_BADDATA;
344 }
345}
346
347PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
348pcre2_set_max_pattern_length(pcre2_compile_context *ccontext, PCRE2_SIZE length)
349{
350ccontext->max_pattern_length = length;
351return 0;
352}
353
354PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
355pcre2_set_newline(pcre2_compile_context *ccontext, uint32_t newline)
356{
357switch(newline)
358 {
359 case PCRE2_NEWLINE_CR:
360 case PCRE2_NEWLINE_LF:
361 case PCRE2_NEWLINE_CRLF:
362 case PCRE2_NEWLINE_ANY:
363 case PCRE2_NEWLINE_ANYCRLF:
Elliott Hughes9bc971b2018-07-27 13:23:14 -0700364 case PCRE2_NEWLINE_NUL:
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100365 ccontext->newline_convention = newline;
366 return 0;
367
368 default:
369 return PCRE2_ERROR_BADDATA;
370 }
371}
372
373PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
374pcre2_set_parens_nest_limit(pcre2_compile_context *ccontext, uint32_t limit)
375{
376ccontext->parens_nest_limit = limit;
377return 0;
378}
379
380PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
Elliott Hughes9bc971b2018-07-27 13:23:14 -0700381pcre2_set_compile_extra_options(pcre2_compile_context *ccontext, uint32_t options)
382{
383ccontext->extra_options = options;
384return 0;
385}
386
387PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100388pcre2_set_compile_recursion_guard(pcre2_compile_context *ccontext,
389 int (*guard)(uint32_t, void *), void *user_data)
390{
391ccontext->stack_guard = guard;
392ccontext->stack_guard_data = user_data;
393return 0;
394}
395
396
Elliott Hughes9bc971b2018-07-27 13:23:14 -0700397/* ------------ Match context ------------ */
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100398
399PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
400pcre2_set_callout(pcre2_match_context *mcontext,
401 int (*callout)(pcre2_callout_block *, void *), void *callout_data)
402{
403mcontext->callout = callout;
404mcontext->callout_data = callout_data;
405return 0;
406}
407
408PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
Elliott Hughes0c26e192019-08-07 12:24:46 -0700409pcre2_set_substitute_callout(pcre2_match_context *mcontext,
410 int (*substitute_callout)(pcre2_substitute_callout_block *, void *),
411 void *substitute_callout_data)
412{
413mcontext->substitute_callout = substitute_callout;
414mcontext->substitute_callout_data = substitute_callout_data;
415return 0;
416}
417
418PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
Elliott Hughes9bc971b2018-07-27 13:23:14 -0700419pcre2_set_heap_limit(pcre2_match_context *mcontext, uint32_t limit)
420{
421mcontext->heap_limit = limit;
422return 0;
423}
424
425PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100426pcre2_set_match_limit(pcre2_match_context *mcontext, uint32_t limit)
427{
428mcontext->match_limit = limit;
429return 0;
430}
431
432PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
Elliott Hughes9bc971b2018-07-27 13:23:14 -0700433pcre2_set_depth_limit(pcre2_match_context *mcontext, uint32_t limit)
434{
435mcontext->depth_limit = limit;
436return 0;
437}
438
439PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100440pcre2_set_offset_limit(pcre2_match_context *mcontext, PCRE2_SIZE limit)
441{
442mcontext->offset_limit = limit;
443return 0;
444}
445
Elliott Hughes9bc971b2018-07-27 13:23:14 -0700446/* This function became obsolete at release 10.30. It is kept as a synonym for
447backwards compatibility. */
448
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100449PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
450pcre2_set_recursion_limit(pcre2_match_context *mcontext, uint32_t limit)
451{
Elliott Hughes9bc971b2018-07-27 13:23:14 -0700452return pcre2_set_depth_limit(mcontext, limit);
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100453}
454
455PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
456pcre2_set_recursion_memory_management(pcre2_match_context *mcontext,
457 void *(*mymalloc)(size_t, void *), void (*myfree)(void *, void *),
458 void *mydata)
459{
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100460(void)mcontext;
461(void)mymalloc;
462(void)myfree;
463(void)mydata;
Elliott Hughes9bc971b2018-07-27 13:23:14 -0700464return 0;
465}
466
467/* ------------ Convert context ------------ */
468
469PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
470pcre2_set_glob_separator(pcre2_convert_context *ccontext, uint32_t separator)
471{
472if (separator != CHAR_SLASH && separator != CHAR_BACKSLASH &&
473 separator != CHAR_DOT) return PCRE2_ERROR_BADDATA;
474ccontext->glob_separator = separator;
475return 0;
476}
477
478PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
479pcre2_set_glob_escape(pcre2_convert_context *ccontext, uint32_t escape)
480{
481if (escape > 255 || (escape != 0 && !ispunct(escape)))
482 return PCRE2_ERROR_BADDATA;
483ccontext->glob_escape = escape;
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100484return 0;
485}
486
487/* End of pcre2_context.c */
Elliott Hughes9bc971b2018-07-27 13:23:14 -0700488