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 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 *
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 */
141  PARENS_NEST_LIMIT };                       /* As it says */
142
143/* The create function copies the default into the new memory, but must
144override the default memory handling functions if a gcontext was provided. */
145
146PCRE2_EXP_DEFN pcre2_compile_context * PCRE2_CALL_CONVENTION
147pcre2_compile_context_create(pcre2_general_context *gcontext)
148{
149pcre2_compile_context *ccontext = PRIV(memctl_malloc)(
150  sizeof(pcre2_real_compile_context), (pcre2_memctl *)gcontext);
151if (ccontext == NULL) return NULL;
152*ccontext = PRIV(default_compile_context);
153if (gcontext != NULL)
154  *((pcre2_memctl *)ccontext) = *((pcre2_memctl *)gcontext);
155return ccontext;
156}
157
158
159/* A default match context is set up to save having to initialize at run time
160when no context is supplied to a match function. */
161
162const pcre2_match_context PRIV(default_match_context) = {
163  { default_malloc, default_free, NULL },
164#ifdef HEAP_MATCH_RECURSE
165  { default_malloc, default_free, NULL },
166#endif
167#ifdef SUPPORT_JIT
168  NULL,
169  NULL,
170#endif
171  NULL,
172  NULL,
173  PCRE2_UNSET,   /* Offset limit */
174  MATCH_LIMIT,
175  MATCH_LIMIT_RECURSION };
176
177/* The create function copies the default into the new memory, but must
178override the default memory handling functions if a gcontext was provided. */
179
180PCRE2_EXP_DEFN pcre2_match_context * PCRE2_CALL_CONVENTION
181pcre2_match_context_create(pcre2_general_context *gcontext)
182{
183pcre2_match_context *mcontext = PRIV(memctl_malloc)(
184  sizeof(pcre2_real_match_context), (pcre2_memctl *)gcontext);
185if (mcontext == NULL) return NULL;
186*mcontext = PRIV(default_match_context);
187if (gcontext != NULL)
188  *((pcre2_memctl *)mcontext) = *((pcre2_memctl *)gcontext);
189return mcontext;
190}
191
192
193/*************************************************
194*              Context copy functions            *
195*************************************************/
196
197PCRE2_EXP_DEFN pcre2_general_context * PCRE2_CALL_CONVENTION
198pcre2_general_context_copy(pcre2_general_context *gcontext)
199{
200pcre2_general_context *new =
201  gcontext->memctl.malloc(sizeof(pcre2_real_general_context),
202  gcontext->memctl.memory_data);
203if (new == NULL) return NULL;
204memcpy(new, gcontext, sizeof(pcre2_real_general_context));
205return new;
206}
207
208
209PCRE2_EXP_DEFN pcre2_compile_context * PCRE2_CALL_CONVENTION
210pcre2_compile_context_copy(pcre2_compile_context *ccontext)
211{
212pcre2_compile_context *new =
213  ccontext->memctl.malloc(sizeof(pcre2_real_compile_context),
214  ccontext->memctl.memory_data);
215if (new == NULL) return NULL;
216memcpy(new, ccontext, sizeof(pcre2_real_compile_context));
217return new;
218}
219
220
221PCRE2_EXP_DEFN pcre2_match_context * PCRE2_CALL_CONVENTION
222pcre2_match_context_copy(pcre2_match_context *mcontext)
223{
224pcre2_match_context *new =
225  mcontext->memctl.malloc(sizeof(pcre2_real_match_context),
226  mcontext->memctl.memory_data);
227if (new == NULL) return NULL;
228memcpy(new, mcontext, sizeof(pcre2_real_match_context));
229return new;
230}
231
232
233
234/*************************************************
235*              Context free functions            *
236*************************************************/
237
238
239PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
240pcre2_general_context_free(pcre2_general_context *gcontext)
241{
242if (gcontext != NULL)
243  gcontext->memctl.free(gcontext, gcontext->memctl.memory_data);
244}
245
246
247PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
248pcre2_compile_context_free(pcre2_compile_context *ccontext)
249{
250if (ccontext != NULL)
251  ccontext->memctl.free(ccontext, ccontext->memctl.memory_data);
252}
253
254
255PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
256pcre2_match_context_free(pcre2_match_context *mcontext)
257{
258if (mcontext != NULL)
259  mcontext->memctl.free(mcontext, mcontext->memctl.memory_data);
260}
261
262
263
264
265/*************************************************
266*             Set values in contexts             *
267*************************************************/
268
269/* All these functions return 0 for success or PCRE2_ERROR_BADDATA if invalid
270data is given. Only some of the functions are able to test the validity of the
271data. */
272
273
274/* ------------ Compile contexts ------------ */
275
276PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
277pcre2_set_character_tables(pcre2_compile_context *ccontext,
278  const unsigned char *tables)
279{
280ccontext->tables = tables;
281return 0;
282}
283
284PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
285pcre2_set_bsr(pcre2_compile_context *ccontext, uint32_t value)
286{
287switch(value)
288  {
289  case PCRE2_BSR_ANYCRLF:
290  case PCRE2_BSR_UNICODE:
291  ccontext->bsr_convention = value;
292  return 0;
293
294  default:
295  return PCRE2_ERROR_BADDATA;
296  }
297}
298
299PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
300pcre2_set_max_pattern_length(pcre2_compile_context *ccontext, PCRE2_SIZE length)
301{
302ccontext->max_pattern_length = length;
303return 0;
304}
305
306PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
307pcre2_set_newline(pcre2_compile_context *ccontext, uint32_t newline)
308{
309switch(newline)
310  {
311  case PCRE2_NEWLINE_CR:
312  case PCRE2_NEWLINE_LF:
313  case PCRE2_NEWLINE_CRLF:
314  case PCRE2_NEWLINE_ANY:
315  case PCRE2_NEWLINE_ANYCRLF:
316  ccontext->newline_convention = newline;
317  return 0;
318
319  default:
320  return PCRE2_ERROR_BADDATA;
321  }
322}
323
324PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
325pcre2_set_parens_nest_limit(pcre2_compile_context *ccontext, uint32_t limit)
326{
327ccontext->parens_nest_limit = limit;
328return 0;
329}
330
331PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
332pcre2_set_compile_recursion_guard(pcre2_compile_context *ccontext,
333  int (*guard)(uint32_t, void *), void *user_data)
334{
335ccontext->stack_guard = guard;
336ccontext->stack_guard_data = user_data;
337return 0;
338}
339
340
341/* ------------ Match contexts ------------ */
342
343PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
344pcre2_set_callout(pcre2_match_context *mcontext,
345  int (*callout)(pcre2_callout_block *, void *), void *callout_data)
346{
347mcontext->callout = callout;
348mcontext->callout_data = callout_data;
349return 0;
350}
351
352PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
353pcre2_set_match_limit(pcre2_match_context *mcontext, uint32_t limit)
354{
355mcontext->match_limit = limit;
356return 0;
357}
358
359PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
360pcre2_set_offset_limit(pcre2_match_context *mcontext, PCRE2_SIZE limit)
361{
362mcontext->offset_limit = limit;
363return 0;
364}
365
366PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
367pcre2_set_recursion_limit(pcre2_match_context *mcontext, uint32_t limit)
368{
369mcontext->recursion_limit = limit;
370return 0;
371}
372
373PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
374pcre2_set_recursion_memory_management(pcre2_match_context *mcontext,
375  void *(*mymalloc)(size_t, void *), void (*myfree)(void *, void *),
376  void *mydata)
377{
378#ifdef HEAP_MATCH_RECURSE
379mcontext->stack_memctl.malloc = mymalloc;
380mcontext->stack_memctl.free = myfree;
381mcontext->stack_memctl.memory_data = mydata;
382#else
383(void)mcontext;
384(void)mymalloc;
385(void)myfree;
386(void)mydata;
387#endif
388return 0;
389}
390
391/* End of pcre2_context.c */
392