1 /*************************************************
2 * Perl-Compatible Regular Expressions *
3 *************************************************/
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.
8 Written by Philip Hazel
9 Original API code Copyright (c) 1997-2012 University of Cambridge
10 New API code Copyright (c) 2016-2017 University of Cambridge
12 -----------------------------------------------------------------------------
13 Redistribution and use in source and binary forms, with or without
14 modification, are permitted provided that the following conditions are met:
16 * Redistributions of source code must retain the above copyright notice,
17 this list of conditions and the following disclaimer.
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.
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.
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 -----------------------------------------------------------------------------
46 #include "pcre2_internal.h"
50 /*************************************************
51 * Default malloc/free functions *
52 *************************************************/
54 /* Ignore the "user data" argument in each case. */
56 static void *default_malloc(size_t size, void *data)
63 static void default_free(void *block, void *data)
71 /*************************************************
72 * Get a block and save memory control *
73 *************************************************/
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.
79 size amount of memory required
80 memctl pointer to a memctl block or NULL
82 Returns: pointer to memory or NULL on failure
86 PRIV(memctl_malloc)(size_t size, pcre2_memctl *memctl)
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;
95 newmemctl->malloc = default_malloc;
96 newmemctl->free = default_free;
97 newmemctl->memory_data = NULL;
99 else *newmemctl = *memctl;
105 /*************************************************
106 * Create and initialize contexts *
107 *************************************************/
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. */
114 PCRE2_EXP_DEFN pcre2_general_context * PCRE2_CALL_CONVENTION
115 pcre2_general_context_create(void *(*private_malloc)(size_t, void *),
116 void (*private_free)(void *, void *), void *memory_data)
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;
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. */
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 */
144 /* The create function copies the default into the new memory, but must
145 override the default memory handling functions if a gcontext was provided. */
147 PCRE2_EXP_DEFN pcre2_compile_context * PCRE2_CALL_CONVENTION
148 pcre2_compile_context_create(pcre2_general_context *gcontext)
150 pcre2_compile_context *ccontext = PRIV(memctl_malloc)(
151 sizeof(pcre2_real_compile_context), (pcre2_memctl *)gcontext);
152 if (ccontext == NULL) return NULL;
153 *ccontext = PRIV(default_compile_context);
154 if (gcontext != NULL)
155 *((pcre2_memctl *)ccontext) = *((pcre2_memctl *)gcontext);
160 /* A default match context is set up to save having to initialize at run time
161 when no context is supplied to a match function. */
163 const pcre2_match_context PRIV(default_match_context) = {
164 { default_malloc, default_free, NULL },
171 PCRE2_UNSET, /* Offset limit */
176 /* The create function copies the default into the new memory, but must
177 override the default memory handling functions if a gcontext was provided. */
179 PCRE2_EXP_DEFN pcre2_match_context * PCRE2_CALL_CONVENTION
180 pcre2_match_context_create(pcre2_general_context *gcontext)
182 pcre2_match_context *mcontext = PRIV(memctl_malloc)(
183 sizeof(pcre2_real_match_context), (pcre2_memctl *)gcontext);
184 if (mcontext == NULL) return NULL;
185 *mcontext = PRIV(default_match_context);
186 if (gcontext != NULL)
187 *((pcre2_memctl *)mcontext) = *((pcre2_memctl *)gcontext);
192 /* A default convert context is set up to save having to initialize at run time
193 when no context is supplied to the convert function. */
195 const pcre2_convert_context PRIV(default_convert_context) = {
196 { default_malloc, default_free, NULL }, /* Default memory handling */
198 CHAR_BACKSLASH, /* Default path separator */
199 CHAR_GRAVE_ACCENT /* Default escape character */
200 #else /* Not Windows */
201 CHAR_SLASH, /* Default path separator */
202 CHAR_BACKSLASH /* Default escape character */
206 /* The create function copies the default into the new memory, but must
207 override the default memory handling functions if a gcontext was provided. */
209 PCRE2_EXP_DEFN pcre2_convert_context * PCRE2_CALL_CONVENTION
210 pcre2_convert_context_create(pcre2_general_context *gcontext)
212 pcre2_convert_context *ccontext = PRIV(memctl_malloc)(
213 sizeof(pcre2_real_convert_context), (pcre2_memctl *)gcontext);
214 if (ccontext == NULL) return NULL;
215 *ccontext = PRIV(default_convert_context);
216 if (gcontext != NULL)
217 *((pcre2_memctl *)ccontext) = *((pcre2_memctl *)gcontext);
222 /*************************************************
223 * Context copy functions *
224 *************************************************/
226 PCRE2_EXP_DEFN pcre2_general_context * PCRE2_CALL_CONVENTION
227 pcre2_general_context_copy(pcre2_general_context *gcontext)
229 pcre2_general_context *new =
230 gcontext->memctl.malloc(sizeof(pcre2_real_general_context),
231 gcontext->memctl.memory_data);
232 if (new == NULL) return NULL;
233 memcpy(new, gcontext, sizeof(pcre2_real_general_context));
238 PCRE2_EXP_DEFN pcre2_compile_context * PCRE2_CALL_CONVENTION
239 pcre2_compile_context_copy(pcre2_compile_context *ccontext)
241 pcre2_compile_context *new =
242 ccontext->memctl.malloc(sizeof(pcre2_real_compile_context),
243 ccontext->memctl.memory_data);
244 if (new == NULL) return NULL;
245 memcpy(new, ccontext, sizeof(pcre2_real_compile_context));
250 PCRE2_EXP_DEFN pcre2_match_context * PCRE2_CALL_CONVENTION
251 pcre2_match_context_copy(pcre2_match_context *mcontext)
253 pcre2_match_context *new =
254 mcontext->memctl.malloc(sizeof(pcre2_real_match_context),
255 mcontext->memctl.memory_data);
256 if (new == NULL) return NULL;
257 memcpy(new, mcontext, sizeof(pcre2_real_match_context));
263 PCRE2_EXP_DEFN pcre2_convert_context * PCRE2_CALL_CONVENTION
264 pcre2_convert_context_copy(pcre2_convert_context *ccontext)
266 pcre2_convert_context *new =
267 ccontext->memctl.malloc(sizeof(pcre2_real_convert_context),
268 ccontext->memctl.memory_data);
269 if (new == NULL) return NULL;
270 memcpy(new, ccontext, sizeof(pcre2_real_convert_context));
275 /*************************************************
276 * Context free functions *
277 *************************************************/
279 PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
280 pcre2_general_context_free(pcre2_general_context *gcontext)
282 if (gcontext != NULL)
283 gcontext->memctl.free(gcontext, gcontext->memctl.memory_data);
287 PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
288 pcre2_compile_context_free(pcre2_compile_context *ccontext)
290 if (ccontext != NULL)
291 ccontext->memctl.free(ccontext, ccontext->memctl.memory_data);
295 PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
296 pcre2_match_context_free(pcre2_match_context *mcontext)
298 if (mcontext != NULL)
299 mcontext->memctl.free(mcontext, mcontext->memctl.memory_data);
303 PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
304 pcre2_convert_context_free(pcre2_convert_context *ccontext)
306 if (ccontext != NULL)
307 ccontext->memctl.free(ccontext, ccontext->memctl.memory_data);
311 /*************************************************
312 * Set values in contexts *
313 *************************************************/
315 /* All these functions return 0 for success or PCRE2_ERROR_BADDATA if invalid
316 data is given. Only some of the functions are able to test the validity of the
320 /* ------------ Compile context ------------ */
322 PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
323 pcre2_set_character_tables(pcre2_compile_context *ccontext,
324 const unsigned char *tables)
326 ccontext->tables = tables;
330 PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
331 pcre2_set_bsr(pcre2_compile_context *ccontext, uint32_t value)
335 case PCRE2_BSR_ANYCRLF:
336 case PCRE2_BSR_UNICODE:
337 ccontext->bsr_convention = value;
341 return PCRE2_ERROR_BADDATA;
345 PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
346 pcre2_set_max_pattern_length(pcre2_compile_context *ccontext, PCRE2_SIZE length)
348 ccontext->max_pattern_length = length;
352 PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
353 pcre2_set_newline(pcre2_compile_context *ccontext, uint32_t newline)
357 case PCRE2_NEWLINE_CR:
358 case PCRE2_NEWLINE_LF:
359 case PCRE2_NEWLINE_CRLF:
360 case PCRE2_NEWLINE_ANY:
361 case PCRE2_NEWLINE_ANYCRLF:
362 case PCRE2_NEWLINE_NUL:
363 ccontext->newline_convention = newline;
367 return PCRE2_ERROR_BADDATA;
371 PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
372 pcre2_set_parens_nest_limit(pcre2_compile_context *ccontext, uint32_t limit)
374 ccontext->parens_nest_limit = limit;
378 PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
379 pcre2_set_compile_extra_options(pcre2_compile_context *ccontext, uint32_t options)
381 ccontext->extra_options = options;
385 PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
386 pcre2_set_compile_recursion_guard(pcre2_compile_context *ccontext,
387 int (*guard)(uint32_t, void *), void *user_data)
389 ccontext->stack_guard = guard;
390 ccontext->stack_guard_data = user_data;
395 /* ------------ Match context ------------ */
397 PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
398 pcre2_set_callout(pcre2_match_context *mcontext,
399 int (*callout)(pcre2_callout_block *, void *), void *callout_data)
401 mcontext->callout = callout;
402 mcontext->callout_data = callout_data;
406 PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
407 pcre2_set_heap_limit(pcre2_match_context *mcontext, uint32_t limit)
409 mcontext->heap_limit = limit;
413 PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
414 pcre2_set_match_limit(pcre2_match_context *mcontext, uint32_t limit)
416 mcontext->match_limit = limit;
420 PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
421 pcre2_set_depth_limit(pcre2_match_context *mcontext, uint32_t limit)
423 mcontext->depth_limit = limit;
427 PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
428 pcre2_set_offset_limit(pcre2_match_context *mcontext, PCRE2_SIZE limit)
430 mcontext->offset_limit = limit;
434 /* This function became obsolete at release 10.30. It is kept as a synonym for
435 backwards compatibility. */
437 PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
438 pcre2_set_recursion_limit(pcre2_match_context *mcontext, uint32_t limit)
440 return pcre2_set_depth_limit(mcontext, limit);
443 PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
444 pcre2_set_recursion_memory_management(pcre2_match_context *mcontext,
445 void *(*mymalloc)(size_t, void *), void (*myfree)(void *, void *),
455 /* ------------ Convert context ------------ */
457 PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
458 pcre2_set_glob_separator(pcre2_convert_context *ccontext, uint32_t separator)
460 if (separator != CHAR_SLASH && separator != CHAR_BACKSLASH &&
461 separator != CHAR_DOT) return PCRE2_ERROR_BADDATA;
462 ccontext->glob_separator = separator;
466 PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
467 pcre2_set_glob_escape(pcre2_convert_context *ccontext, uint32_t escape)
469 if (escape > 255 || (escape != 0 && !ispunct(escape)))
470 return PCRE2_ERROR_BADDATA;
471 ccontext->glob_escape = escape;
475 /* End of pcre2_context.c */