• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 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 
default_malloc(size_t size,void * data)56 static void *default_malloc(size_t size, void *data)
57 {
58 (void)data;
59 return malloc(size);
60 }
61 
62 
default_free(void * block,void * data)63 static void default_free(void *block, void *data)
64 {
65 (void)data;
66 free(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
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 *
PRIV(memctl_malloc)86 PRIV(memctl_malloc)(size_t size, pcre2_memctl *memctl)
87 {
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;
93 if (memctl == NULL)
94   {
95   newmemctl->malloc = default_malloc;
96   newmemctl->free = default_free;
97   newmemctl->memory_data = NULL;
98   }
99 else *newmemctl = *memctl;
100 return 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
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
pcre2_general_context_create(void * (* private_malloc)(size_t,void *),void (* private_free)(void *,void *),void * memory_data)115 pcre2_general_context_create(void *(*private_malloc)(size_t, void *),
116   void (*private_free)(void *, void *), void *memory_data)
117 {
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;
126 return gcontext;
127 }
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 
143 /* The create function copies the default into the new memory, but must
144 override the default memory handling functions if a gcontext was provided. */
145 
146 PCRE2_EXP_DEFN pcre2_compile_context * PCRE2_CALL_CONVENTION
pcre2_compile_context_create(pcre2_general_context * gcontext)147 pcre2_compile_context_create(pcre2_general_context *gcontext)
148 {
149 pcre2_compile_context *ccontext = PRIV(memctl_malloc)(
150   sizeof(pcre2_real_compile_context), (pcre2_memctl *)gcontext);
151 if (ccontext == NULL) return NULL;
152 *ccontext = PRIV(default_compile_context);
153 if (gcontext != NULL)
154   *((pcre2_memctl *)ccontext) = *((pcre2_memctl *)gcontext);
155 return ccontext;
156 }
157 
158 
159 /* A default match context is set up to save having to initialize at run time
160 when no context is supplied to a match function. */
161 
162 const 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
178 override the default memory handling functions if a gcontext was provided. */
179 
180 PCRE2_EXP_DEFN pcre2_match_context * PCRE2_CALL_CONVENTION
pcre2_match_context_create(pcre2_general_context * gcontext)181 pcre2_match_context_create(pcre2_general_context *gcontext)
182 {
183 pcre2_match_context *mcontext = PRIV(memctl_malloc)(
184   sizeof(pcre2_real_match_context), (pcre2_memctl *)gcontext);
185 if (mcontext == NULL) return NULL;
186 *mcontext = PRIV(default_match_context);
187 if (gcontext != NULL)
188   *((pcre2_memctl *)mcontext) = *((pcre2_memctl *)gcontext);
189 return mcontext;
190 }
191 
192 
193 /*************************************************
194 *              Context copy functions            *
195 *************************************************/
196 
197 PCRE2_EXP_DEFN pcre2_general_context * PCRE2_CALL_CONVENTION
pcre2_general_context_copy(pcre2_general_context * gcontext)198 pcre2_general_context_copy(pcre2_general_context *gcontext)
199 {
200 pcre2_general_context *new =
201   gcontext->memctl.malloc(sizeof(pcre2_real_general_context),
202   gcontext->memctl.memory_data);
203 if (new == NULL) return NULL;
204 memcpy(new, gcontext, sizeof(pcre2_real_general_context));
205 return new;
206 }
207 
208 
209 PCRE2_EXP_DEFN pcre2_compile_context * PCRE2_CALL_CONVENTION
pcre2_compile_context_copy(pcre2_compile_context * ccontext)210 pcre2_compile_context_copy(pcre2_compile_context *ccontext)
211 {
212 pcre2_compile_context *new =
213   ccontext->memctl.malloc(sizeof(pcre2_real_compile_context),
214   ccontext->memctl.memory_data);
215 if (new == NULL) return NULL;
216 memcpy(new, ccontext, sizeof(pcre2_real_compile_context));
217 return new;
218 }
219 
220 
221 PCRE2_EXP_DEFN pcre2_match_context * PCRE2_CALL_CONVENTION
pcre2_match_context_copy(pcre2_match_context * mcontext)222 pcre2_match_context_copy(pcre2_match_context *mcontext)
223 {
224 pcre2_match_context *new =
225   mcontext->memctl.malloc(sizeof(pcre2_real_match_context),
226   mcontext->memctl.memory_data);
227 if (new == NULL) return NULL;
228 memcpy(new, mcontext, sizeof(pcre2_real_match_context));
229 return new;
230 }
231 
232 
233 
234 /*************************************************
235 *              Context free functions            *
236 *************************************************/
237 
238 
239 PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
pcre2_general_context_free(pcre2_general_context * gcontext)240 pcre2_general_context_free(pcre2_general_context *gcontext)
241 {
242 if (gcontext != NULL)
243   gcontext->memctl.free(gcontext, gcontext->memctl.memory_data);
244 }
245 
246 
247 PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
pcre2_compile_context_free(pcre2_compile_context * ccontext)248 pcre2_compile_context_free(pcre2_compile_context *ccontext)
249 {
250 if (ccontext != NULL)
251   ccontext->memctl.free(ccontext, ccontext->memctl.memory_data);
252 }
253 
254 
255 PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
pcre2_match_context_free(pcre2_match_context * mcontext)256 pcre2_match_context_free(pcre2_match_context *mcontext)
257 {
258 if (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
270 data is given. Only some of the functions are able to test the validity of the
271 data. */
272 
273 
274 /* ------------ Compile contexts ------------ */
275 
276 PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
pcre2_set_character_tables(pcre2_compile_context * ccontext,const unsigned char * tables)277 pcre2_set_character_tables(pcre2_compile_context *ccontext,
278   const unsigned char *tables)
279 {
280 ccontext->tables = tables;
281 return 0;
282 }
283 
284 PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
pcre2_set_bsr(pcre2_compile_context * ccontext,uint32_t value)285 pcre2_set_bsr(pcre2_compile_context *ccontext, uint32_t value)
286 {
287 switch(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 
299 PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
pcre2_set_max_pattern_length(pcre2_compile_context * ccontext,PCRE2_SIZE length)300 pcre2_set_max_pattern_length(pcre2_compile_context *ccontext, PCRE2_SIZE length)
301 {
302 ccontext->max_pattern_length = length;
303 return 0;
304 }
305 
306 PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
pcre2_set_newline(pcre2_compile_context * ccontext,uint32_t newline)307 pcre2_set_newline(pcre2_compile_context *ccontext, uint32_t newline)
308 {
309 switch(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 
324 PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
pcre2_set_parens_nest_limit(pcre2_compile_context * ccontext,uint32_t limit)325 pcre2_set_parens_nest_limit(pcre2_compile_context *ccontext, uint32_t limit)
326 {
327 ccontext->parens_nest_limit = limit;
328 return 0;
329 }
330 
331 PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
pcre2_set_compile_recursion_guard(pcre2_compile_context * ccontext,int (* guard)(uint32_t,void *),void * user_data)332 pcre2_set_compile_recursion_guard(pcre2_compile_context *ccontext,
333   int (*guard)(uint32_t, void *), void *user_data)
334 {
335 ccontext->stack_guard = guard;
336 ccontext->stack_guard_data = user_data;
337 return 0;
338 }
339 
340 
341 /* ------------ Match contexts ------------ */
342 
343 PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
pcre2_set_callout(pcre2_match_context * mcontext,int (* callout)(pcre2_callout_block *,void *),void * callout_data)344 pcre2_set_callout(pcre2_match_context *mcontext,
345   int (*callout)(pcre2_callout_block *, void *), void *callout_data)
346 {
347 mcontext->callout = callout;
348 mcontext->callout_data = callout_data;
349 return 0;
350 }
351 
352 PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
pcre2_set_match_limit(pcre2_match_context * mcontext,uint32_t limit)353 pcre2_set_match_limit(pcre2_match_context *mcontext, uint32_t limit)
354 {
355 mcontext->match_limit = limit;
356 return 0;
357 }
358 
359 PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
pcre2_set_offset_limit(pcre2_match_context * mcontext,PCRE2_SIZE limit)360 pcre2_set_offset_limit(pcre2_match_context *mcontext, PCRE2_SIZE limit)
361 {
362 mcontext->offset_limit = limit;
363 return 0;
364 }
365 
366 PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
pcre2_set_recursion_limit(pcre2_match_context * mcontext,uint32_t limit)367 pcre2_set_recursion_limit(pcre2_match_context *mcontext, uint32_t limit)
368 {
369 mcontext->recursion_limit = limit;
370 return 0;
371 }
372 
373 PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
pcre2_set_recursion_memory_management(pcre2_match_context * mcontext,void * (* mymalloc)(size_t,void *),void (* myfree)(void *,void *),void * mydata)374 pcre2_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
379 mcontext->stack_memctl.malloc = mymalloc;
380 mcontext->stack_memctl.free = myfree;
381 mcontext->stack_memctl.memory_data = mydata;
382 #else
383 (void)mcontext;
384 (void)mymalloc;
385 (void)myfree;
386 (void)mydata;
387 #endif
388 return 0;
389 }
390 
391 /* End of pcre2_context.c */
392