/src/pcre2-10.39/src/pcre2_context.c
Line | Count | Source |
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-2018 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 | | |
56 | | static void *default_malloc(size_t size, void *data) |
57 | 5.21M | { |
58 | 5.21M | (void)data; |
59 | 5.21M | return malloc(size); |
60 | 5.21M | } |
61 | | |
62 | | |
63 | | static void default_free(void *block, void *data) |
64 | 5.93M | { |
65 | 5.93M | (void)data; |
66 | 5.93M | free(block); |
67 | 5.93M | } |
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 * |
86 | | PRIV(memctl_malloc)(size_t size, pcre2_memctl *memctl) |
87 | 4.99M | { |
88 | 4.99M | pcre2_memctl *newmemctl; |
89 | 4.99M | void *yield = (memctl == NULL)? malloc(size) : |
90 | 4.99M | memctl->malloc(size, memctl->memory_data); |
91 | 4.99M | if (yield == NULL) return NULL; |
92 | 4.99M | newmemctl = (pcre2_memctl *)yield; |
93 | 4.99M | if (memctl == NULL) |
94 | 725k | { |
95 | 725k | newmemctl->malloc = default_malloc; |
96 | 725k | newmemctl->free = default_free; |
97 | 725k | newmemctl->memory_data = NULL; |
98 | 725k | } |
99 | 4.27M | else *newmemctl = *memctl; |
100 | 4.99M | return yield; |
101 | 4.99M | } |
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 |
115 | | pcre2_general_context_create(void *(*private_malloc)(size_t, void *), |
116 | | void (*private_free)(void *, void *), void *memory_data) |
117 | 0 | { |
118 | 0 | pcre2_general_context *gcontext; |
119 | 0 | if (private_malloc == NULL) private_malloc = default_malloc; |
120 | 0 | if (private_free == NULL) private_free = default_free; |
121 | 0 | gcontext = private_malloc(sizeof(pcre2_real_general_context), memory_data); |
122 | 0 | if (gcontext == NULL) return NULL; |
123 | 0 | gcontext->memctl.malloc = private_malloc; |
124 | 0 | gcontext->memctl.free = private_free; |
125 | 0 | gcontext->memctl.memory_data = memory_data; |
126 | 0 | return gcontext; |
127 | 0 | } |
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 | | |
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. */ |
146 | | |
147 | | PCRE2_EXP_DEFN pcre2_compile_context * PCRE2_CALL_CONVENTION |
148 | | pcre2_compile_context_create(pcre2_general_context *gcontext) |
149 | 0 | { |
150 | 0 | pcre2_compile_context *ccontext = PRIV(memctl_malloc)( |
151 | 0 | sizeof(pcre2_real_compile_context), (pcre2_memctl *)gcontext); |
152 | 0 | if (ccontext == NULL) return NULL; |
153 | 0 | *ccontext = PRIV(default_compile_context); |
154 | 0 | if (gcontext != NULL) |
155 | 0 | *((pcre2_memctl *)ccontext) = *((pcre2_memctl *)gcontext); |
156 | 0 | return ccontext; |
157 | 0 | } |
158 | | |
159 | | |
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. */ |
162 | | |
163 | | const pcre2_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 | | PCRE2_UNSET, /* Offset limit */ |
174 | | HEAP_LIMIT, |
175 | | MATCH_LIMIT, |
176 | | MATCH_LIMIT_DEPTH }; |
177 | | |
178 | | /* The create function copies the default into the new memory, but must |
179 | | override the default memory handling functions if a gcontext was provided. */ |
180 | | |
181 | | PCRE2_EXP_DEFN pcre2_match_context * PCRE2_CALL_CONVENTION |
182 | | pcre2_match_context_create(pcre2_general_context *gcontext) |
183 | 725k | { |
184 | 725k | pcre2_match_context *mcontext = PRIV(memctl_malloc)( |
185 | 725k | sizeof(pcre2_real_match_context), (pcre2_memctl *)gcontext); |
186 | 725k | if (mcontext == NULL) return NULL; |
187 | 725k | *mcontext = PRIV(default_match_context); |
188 | 725k | if (gcontext != NULL) |
189 | 0 | *((pcre2_memctl *)mcontext) = *((pcre2_memctl *)gcontext); |
190 | 725k | return mcontext; |
191 | 725k | } |
192 | | |
193 | | |
194 | | /* A default convert context is set up to save having to initialize at run time |
195 | | when no context is supplied to the convert function. */ |
196 | | |
197 | | const 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 |
209 | | override the default memory handling functions if a gcontext was provided. */ |
210 | | |
211 | | PCRE2_EXP_DEFN pcre2_convert_context * PCRE2_CALL_CONVENTION |
212 | | pcre2_convert_context_create(pcre2_general_context *gcontext) |
213 | 0 | { |
214 | 0 | pcre2_convert_context *ccontext = PRIV(memctl_malloc)( |
215 | 0 | sizeof(pcre2_real_convert_context), (pcre2_memctl *)gcontext); |
216 | 0 | if (ccontext == NULL) return NULL; |
217 | 0 | *ccontext = PRIV(default_convert_context); |
218 | 0 | if (gcontext != NULL) |
219 | 0 | *((pcre2_memctl *)ccontext) = *((pcre2_memctl *)gcontext); |
220 | 0 | return ccontext; |
221 | 0 | } |
222 | | |
223 | | |
224 | | /************************************************* |
225 | | * Context copy functions * |
226 | | *************************************************/ |
227 | | |
228 | | PCRE2_EXP_DEFN pcre2_general_context * PCRE2_CALL_CONVENTION |
229 | | pcre2_general_context_copy(pcre2_general_context *gcontext) |
230 | 0 | { |
231 | 0 | pcre2_general_context *new = |
232 | 0 | gcontext->memctl.malloc(sizeof(pcre2_real_general_context), |
233 | 0 | gcontext->memctl.memory_data); |
234 | 0 | if (new == NULL) return NULL; |
235 | 0 | memcpy(new, gcontext, sizeof(pcre2_real_general_context)); |
236 | 0 | return new; |
237 | 0 | } |
238 | | |
239 | | |
240 | | PCRE2_EXP_DEFN pcre2_compile_context * PCRE2_CALL_CONVENTION |
241 | | pcre2_compile_context_copy(pcre2_compile_context *ccontext) |
242 | 0 | { |
243 | 0 | pcre2_compile_context *new = |
244 | 0 | ccontext->memctl.malloc(sizeof(pcre2_real_compile_context), |
245 | 0 | ccontext->memctl.memory_data); |
246 | 0 | if (new == NULL) return NULL; |
247 | 0 | memcpy(new, ccontext, sizeof(pcre2_real_compile_context)); |
248 | 0 | return new; |
249 | 0 | } |
250 | | |
251 | | |
252 | | PCRE2_EXP_DEFN pcre2_match_context * PCRE2_CALL_CONVENTION |
253 | | pcre2_match_context_copy(pcre2_match_context *mcontext) |
254 | 0 | { |
255 | 0 | pcre2_match_context *new = |
256 | 0 | mcontext->memctl.malloc(sizeof(pcre2_real_match_context), |
257 | 0 | mcontext->memctl.memory_data); |
258 | 0 | if (new == NULL) return NULL; |
259 | 0 | memcpy(new, mcontext, sizeof(pcre2_real_match_context)); |
260 | 0 | return new; |
261 | 0 | } |
262 | | |
263 | | |
264 | | |
265 | | PCRE2_EXP_DEFN pcre2_convert_context * PCRE2_CALL_CONVENTION |
266 | | pcre2_convert_context_copy(pcre2_convert_context *ccontext) |
267 | 0 | { |
268 | 0 | pcre2_convert_context *new = |
269 | 0 | ccontext->memctl.malloc(sizeof(pcre2_real_convert_context), |
270 | 0 | ccontext->memctl.memory_data); |
271 | 0 | if (new == NULL) return NULL; |
272 | 0 | memcpy(new, ccontext, sizeof(pcre2_real_convert_context)); |
273 | 0 | return new; |
274 | 0 | } |
275 | | |
276 | | |
277 | | /************************************************* |
278 | | * Context free functions * |
279 | | *************************************************/ |
280 | | |
281 | | PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION |
282 | | pcre2_general_context_free(pcre2_general_context *gcontext) |
283 | 0 | { |
284 | 0 | if (gcontext != NULL) |
285 | 0 | gcontext->memctl.free(gcontext, gcontext->memctl.memory_data); |
286 | 0 | } |
287 | | |
288 | | |
289 | | PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION |
290 | | pcre2_compile_context_free(pcre2_compile_context *ccontext) |
291 | 0 | { |
292 | 0 | if (ccontext != NULL) |
293 | 0 | ccontext->memctl.free(ccontext, ccontext->memctl.memory_data); |
294 | 0 | } |
295 | | |
296 | | |
297 | | PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION |
298 | | pcre2_match_context_free(pcre2_match_context *mcontext) |
299 | 721k | { |
300 | 721k | if (mcontext != NULL) |
301 | 721k | mcontext->memctl.free(mcontext, mcontext->memctl.memory_data); |
302 | 721k | } |
303 | | |
304 | | |
305 | | PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION |
306 | | pcre2_convert_context_free(pcre2_convert_context *ccontext) |
307 | 0 | { |
308 | 0 | if (ccontext != NULL) |
309 | 0 | ccontext->memctl.free(ccontext, ccontext->memctl.memory_data); |
310 | 0 | } |
311 | | |
312 | | |
313 | | /************************************************* |
314 | | * Set values in contexts * |
315 | | *************************************************/ |
316 | | |
317 | | /* All these functions return 0 for success or PCRE2_ERROR_BADDATA if invalid |
318 | | data is given. Only some of the functions are able to test the validity of the |
319 | | data. */ |
320 | | |
321 | | |
322 | | /* ------------ Compile context ------------ */ |
323 | | |
324 | | PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION |
325 | | pcre2_set_character_tables(pcre2_compile_context *ccontext, |
326 | | const uint8_t *tables) |
327 | 0 | { |
328 | 0 | ccontext->tables = tables; |
329 | 0 | return 0; |
330 | 0 | } |
331 | | |
332 | | PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION |
333 | | pcre2_set_bsr(pcre2_compile_context *ccontext, uint32_t value) |
334 | 0 | { |
335 | 0 | switch(value) |
336 | 0 | { |
337 | 0 | case PCRE2_BSR_ANYCRLF: |
338 | 0 | case PCRE2_BSR_UNICODE: |
339 | 0 | ccontext->bsr_convention = value; |
340 | 0 | return 0; |
341 | | |
342 | 0 | default: |
343 | 0 | return PCRE2_ERROR_BADDATA; |
344 | 0 | } |
345 | 0 | } |
346 | | |
347 | | PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION |
348 | | pcre2_set_max_pattern_length(pcre2_compile_context *ccontext, PCRE2_SIZE length) |
349 | 0 | { |
350 | 0 | ccontext->max_pattern_length = length; |
351 | 0 | return 0; |
352 | 0 | } |
353 | | |
354 | | PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION |
355 | | pcre2_set_newline(pcre2_compile_context *ccontext, uint32_t newline) |
356 | 0 | { |
357 | 0 | switch(newline) |
358 | 0 | { |
359 | 0 | case PCRE2_NEWLINE_CR: |
360 | 0 | case PCRE2_NEWLINE_LF: |
361 | 0 | case PCRE2_NEWLINE_CRLF: |
362 | 0 | case PCRE2_NEWLINE_ANY: |
363 | 0 | case PCRE2_NEWLINE_ANYCRLF: |
364 | 0 | case PCRE2_NEWLINE_NUL: |
365 | 0 | ccontext->newline_convention = newline; |
366 | 0 | return 0; |
367 | | |
368 | 0 | default: |
369 | 0 | return PCRE2_ERROR_BADDATA; |
370 | 0 | } |
371 | 0 | } |
372 | | |
373 | | PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION |
374 | | pcre2_set_parens_nest_limit(pcre2_compile_context *ccontext, uint32_t limit) |
375 | 0 | { |
376 | 0 | ccontext->parens_nest_limit = limit; |
377 | 0 | return 0; |
378 | 0 | } |
379 | | |
380 | | PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION |
381 | | pcre2_set_compile_extra_options(pcre2_compile_context *ccontext, uint32_t options) |
382 | 0 | { |
383 | 0 | ccontext->extra_options = options; |
384 | 0 | return 0; |
385 | 0 | } |
386 | | |
387 | | PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION |
388 | | pcre2_set_compile_recursion_guard(pcre2_compile_context *ccontext, |
389 | | int (*guard)(uint32_t, void *), void *user_data) |
390 | 0 | { |
391 | 0 | ccontext->stack_guard = guard; |
392 | 0 | ccontext->stack_guard_data = user_data; |
393 | 0 | return 0; |
394 | 0 | } |
395 | | |
396 | | |
397 | | /* ------------ Match context ------------ */ |
398 | | |
399 | | PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION |
400 | | pcre2_set_callout(pcre2_match_context *mcontext, |
401 | | int (*callout)(pcre2_callout_block *, void *), void *callout_data) |
402 | 0 | { |
403 | 0 | mcontext->callout = callout; |
404 | 0 | mcontext->callout_data = callout_data; |
405 | 0 | return 0; |
406 | 0 | } |
407 | | |
408 | | PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION |
409 | | pcre2_set_substitute_callout(pcre2_match_context *mcontext, |
410 | | int (*substitute_callout)(pcre2_substitute_callout_block *, void *), |
411 | | void *substitute_callout_data) |
412 | 0 | { |
413 | 0 | mcontext->substitute_callout = substitute_callout; |
414 | 0 | mcontext->substitute_callout_data = substitute_callout_data; |
415 | 0 | return 0; |
416 | 0 | } |
417 | | |
418 | | PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION |
419 | | pcre2_set_heap_limit(pcre2_match_context *mcontext, uint32_t limit) |
420 | 0 | { |
421 | 0 | mcontext->heap_limit = limit; |
422 | 0 | return 0; |
423 | 0 | } |
424 | | |
425 | | PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION |
426 | | pcre2_set_match_limit(pcre2_match_context *mcontext, uint32_t limit) |
427 | 725k | { |
428 | 725k | mcontext->match_limit = limit; |
429 | 725k | return 0; |
430 | 725k | } |
431 | | |
432 | | PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION |
433 | | pcre2_set_depth_limit(pcre2_match_context *mcontext, uint32_t limit) |
434 | 725k | { |
435 | 725k | mcontext->depth_limit = limit; |
436 | 725k | return 0; |
437 | 725k | } |
438 | | |
439 | | PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION |
440 | | pcre2_set_offset_limit(pcre2_match_context *mcontext, PCRE2_SIZE limit) |
441 | 0 | { |
442 | 0 | mcontext->offset_limit = limit; |
443 | 0 | return 0; |
444 | 0 | } |
445 | | |
446 | | /* This function became obsolete at release 10.30. It is kept as a synonym for |
447 | | backwards compatibility. */ |
448 | | |
449 | | PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION |
450 | | pcre2_set_recursion_limit(pcre2_match_context *mcontext, uint32_t limit) |
451 | 725k | { |
452 | 725k | return pcre2_set_depth_limit(mcontext, limit); |
453 | 725k | } |
454 | | |
455 | | PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION |
456 | | pcre2_set_recursion_memory_management(pcre2_match_context *mcontext, |
457 | | void *(*mymalloc)(size_t, void *), void (*myfree)(void *, void *), |
458 | | void *mydata) |
459 | 0 | { |
460 | 0 | (void)mcontext; |
461 | 0 | (void)mymalloc; |
462 | 0 | (void)myfree; |
463 | 0 | (void)mydata; |
464 | 0 | return 0; |
465 | 0 | } |
466 | | |
467 | | /* ------------ Convert context ------------ */ |
468 | | |
469 | | PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION |
470 | | pcre2_set_glob_separator(pcre2_convert_context *ccontext, uint32_t separator) |
471 | 0 | { |
472 | 0 | if (separator != CHAR_SLASH && separator != CHAR_BACKSLASH && |
473 | 0 | separator != CHAR_DOT) return PCRE2_ERROR_BADDATA; |
474 | 0 | ccontext->glob_separator = separator; |
475 | 0 | return 0; |
476 | 0 | } |
477 | | |
478 | | PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION |
479 | | pcre2_set_glob_escape(pcre2_convert_context *ccontext, uint32_t escape) |
480 | 0 | { |
481 | 0 | if (escape > 255 || (escape != 0 && !ispunct(escape))) |
482 | 0 | return PCRE2_ERROR_BADDATA; |
483 | 0 | ccontext->glob_escape = escape; |
484 | 0 | return 0; |
485 | 0 | } |
486 | | |
487 | | /* End of pcre2_context.c */ |
488 | | |