Path: blob/master/thirdparty/pcre2/src/pcre2_context.c
9903 views
/*************************************************1* Perl-Compatible Regular Expressions *2*************************************************/34/* PCRE is a library of functions to support regular expressions whose syntax5and semantics are as close as possible to those of the Perl 5 language.67Written by Philip Hazel8Original API code Copyright (c) 1997-2012 University of Cambridge9New API code Copyright (c) 2016-2024 University of Cambridge1011-----------------------------------------------------------------------------12Redistribution and use in source and binary forms, with or without13modification, are permitted provided that the following conditions are met:1415* Redistributions of source code must retain the above copyright notice,16this list of conditions and the following disclaimer.1718* Redistributions in binary form must reproduce the above copyright19notice, this list of conditions and the following disclaimer in the20documentation and/or other materials provided with the distribution.2122* Neither the name of the University of Cambridge nor the names of its23contributors may be used to endorse or promote products derived from24this software without specific prior written permission.2526THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"27AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE28IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE29ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE30LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR31CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF32SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS33INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN34CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)35ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE36POSSIBILITY OF SUCH DAMAGE.37-----------------------------------------------------------------------------38*/394041#ifdef HAVE_CONFIG_H42#include "config.h"43#endif4445#include "pcre2_internal.h"46474849/*************************************************50* Default malloc/free functions *51*************************************************/5253/* Ignore the "user data" argument in each case. */5455static void *default_malloc(size_t size, void *data)56{57(void)data;58return malloc(size);59}606162static void default_free(void *block, void *data)63{64(void)data;65free(block);66}67686970/*************************************************71* Get a block and save memory control *72*************************************************/7374/* This internal function is called to get a block of memory in which the75memory control data is to be stored at the start for future use.7677Arguments:78size amount of memory required79memctl pointer to a memctl block or NULL8081Returns: pointer to memory or NULL on failure82*/8384extern void *85PRIV(memctl_malloc)(size_t size, pcre2_memctl *memctl)86{87pcre2_memctl *newmemctl;88void *yield = (memctl == NULL)? malloc(size) :89memctl->malloc(size, memctl->memory_data);90if (yield == NULL) return NULL;91newmemctl = (pcre2_memctl *)yield;92if (memctl == NULL)93{94newmemctl->malloc = default_malloc;95newmemctl->free = default_free;96newmemctl->memory_data = NULL;97}98else *newmemctl = *memctl;99return yield;100}101102103104/*************************************************105* Create and initialize contexts *106*************************************************/107108/* Initializing for compile and match contexts is done in separate, private109functions so that these can be called from functions such as pcre2_compile()110when an external context is not supplied. The initializing functions have an111option to set up default memory management. */112113PCRE2_EXP_DEFN pcre2_general_context * PCRE2_CALL_CONVENTION114pcre2_general_context_create(void *(*private_malloc)(size_t, void *),115void (*private_free)(void *, void *), void *memory_data)116{117pcre2_general_context *gcontext;118if (private_malloc == NULL) private_malloc = default_malloc;119if (private_free == NULL) private_free = default_free;120gcontext = private_malloc(sizeof(pcre2_real_general_context), memory_data);121if (gcontext == NULL) return NULL;122gcontext->memctl.malloc = private_malloc;123gcontext->memctl.free = private_free;124gcontext->memctl.memory_data = memory_data;125return gcontext;126}127128129/* A default compile context is set up to save having to initialize at run time130when no context is supplied to the compile function. */131132pcre2_compile_context PRIV(default_compile_context) = {133{ default_malloc, default_free, NULL }, /* Default memory handling */134NULL, /* Stack guard */135NULL, /* Stack guard data */136PRIV(default_tables), /* Character tables */137PCRE2_UNSET, /* Max pattern length */138PCRE2_UNSET, /* Max pattern compiled length */139BSR_DEFAULT, /* Backslash R default */140NEWLINE_DEFAULT, /* Newline convention */141PARENS_NEST_LIMIT, /* As it says */1420, /* Extra options */143MAX_VARLOOKBEHIND, /* As it says */144PCRE2_OPTIMIZATION_ALL /* All optimizations enabled */145};146147/* The create function copies the default into the new memory, but must148override the default memory handling functions if a gcontext was provided. */149150PCRE2_EXP_DEFN pcre2_compile_context * PCRE2_CALL_CONVENTION151pcre2_compile_context_create(pcre2_general_context *gcontext)152{153pcre2_compile_context *ccontext = PRIV(memctl_malloc)(154sizeof(pcre2_real_compile_context), (pcre2_memctl *)gcontext);155if (ccontext == NULL) return NULL;156*ccontext = PRIV(default_compile_context);157if (gcontext != NULL)158*((pcre2_memctl *)ccontext) = *((pcre2_memctl *)gcontext);159return ccontext;160}161162163/* A default match context is set up to save having to initialize at run time164when no context is supplied to a match function. */165166pcre2_match_context PRIV(default_match_context) = {167{ default_malloc, default_free, NULL },168#ifdef SUPPORT_JIT169NULL, /* JIT callback */170NULL, /* JIT callback data */171#endif172NULL, /* Callout function */173NULL, /* Callout data */174NULL, /* Substitute callout function */175NULL, /* Substitute callout data */176NULL, /* Substitute case callout function */177NULL, /* Substitute case callout data */178PCRE2_UNSET, /* Offset limit */179HEAP_LIMIT,180MATCH_LIMIT,181MATCH_LIMIT_DEPTH };182183/* The create function copies the default into the new memory, but must184override the default memory handling functions if a gcontext was provided. */185186PCRE2_EXP_DEFN pcre2_match_context * PCRE2_CALL_CONVENTION187pcre2_match_context_create(pcre2_general_context *gcontext)188{189pcre2_match_context *mcontext = PRIV(memctl_malloc)(190sizeof(pcre2_real_match_context), (pcre2_memctl *)gcontext);191if (mcontext == NULL) return NULL;192*mcontext = PRIV(default_match_context);193if (gcontext != NULL)194*((pcre2_memctl *)mcontext) = *((pcre2_memctl *)gcontext);195return mcontext;196}197198199/* A default convert context is set up to save having to initialize at run time200when no context is supplied to the convert function. */201202pcre2_convert_context PRIV(default_convert_context) = {203{ default_malloc, default_free, NULL }, /* Default memory handling */204#ifdef _WIN32205CHAR_BACKSLASH, /* Default path separator */206CHAR_GRAVE_ACCENT /* Default escape character */207#else /* Not Windows */208CHAR_SLASH, /* Default path separator */209CHAR_BACKSLASH /* Default escape character */210#endif211};212213/* The create function copies the default into the new memory, but must214override the default memory handling functions if a gcontext was provided. */215216PCRE2_EXP_DEFN pcre2_convert_context * PCRE2_CALL_CONVENTION217pcre2_convert_context_create(pcre2_general_context *gcontext)218{219pcre2_convert_context *ccontext = PRIV(memctl_malloc)(220sizeof(pcre2_real_convert_context), (pcre2_memctl *)gcontext);221if (ccontext == NULL) return NULL;222*ccontext = PRIV(default_convert_context);223if (gcontext != NULL)224*((pcre2_memctl *)ccontext) = *((pcre2_memctl *)gcontext);225return ccontext;226}227228229/*************************************************230* Context copy functions *231*************************************************/232233PCRE2_EXP_DEFN pcre2_general_context * PCRE2_CALL_CONVENTION234pcre2_general_context_copy(pcre2_general_context *gcontext)235{236pcre2_general_context *newcontext =237gcontext->memctl.malloc(sizeof(pcre2_real_general_context),238gcontext->memctl.memory_data);239if (newcontext == NULL) return NULL;240memcpy(newcontext, gcontext, sizeof(pcre2_real_general_context));241return newcontext;242}243244245PCRE2_EXP_DEFN pcre2_compile_context * PCRE2_CALL_CONVENTION246pcre2_compile_context_copy(pcre2_compile_context *ccontext)247{248pcre2_compile_context *newcontext =249ccontext->memctl.malloc(sizeof(pcre2_real_compile_context),250ccontext->memctl.memory_data);251if (newcontext == NULL) return NULL;252memcpy(newcontext, ccontext, sizeof(pcre2_real_compile_context));253return newcontext;254}255256257PCRE2_EXP_DEFN pcre2_match_context * PCRE2_CALL_CONVENTION258pcre2_match_context_copy(pcre2_match_context *mcontext)259{260pcre2_match_context *newcontext =261mcontext->memctl.malloc(sizeof(pcre2_real_match_context),262mcontext->memctl.memory_data);263if (newcontext == NULL) return NULL;264memcpy(newcontext, mcontext, sizeof(pcre2_real_match_context));265return newcontext;266}267268269PCRE2_EXP_DEFN pcre2_convert_context * PCRE2_CALL_CONVENTION270pcre2_convert_context_copy(pcre2_convert_context *ccontext)271{272pcre2_convert_context *newcontext =273ccontext->memctl.malloc(sizeof(pcre2_real_convert_context),274ccontext->memctl.memory_data);275if (newcontext == NULL) return NULL;276memcpy(newcontext, ccontext, sizeof(pcre2_real_convert_context));277return newcontext;278}279280281/*************************************************282* Context free functions *283*************************************************/284285PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION286pcre2_general_context_free(pcre2_general_context *gcontext)287{288if (gcontext != NULL)289gcontext->memctl.free(gcontext, gcontext->memctl.memory_data);290}291292293PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION294pcre2_compile_context_free(pcre2_compile_context *ccontext)295{296if (ccontext != NULL)297ccontext->memctl.free(ccontext, ccontext->memctl.memory_data);298}299300301PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION302pcre2_match_context_free(pcre2_match_context *mcontext)303{304if (mcontext != NULL)305mcontext->memctl.free(mcontext, mcontext->memctl.memory_data);306}307308309PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION310pcre2_convert_context_free(pcre2_convert_context *ccontext)311{312if (ccontext != NULL)313ccontext->memctl.free(ccontext, ccontext->memctl.memory_data);314}315316317/*************************************************318* Set values in contexts *319*************************************************/320321/* All these functions return 0 for success or PCRE2_ERROR_BADDATA if invalid322data is given. Only some of the functions are able to test the validity of the323data. */324325326/* ------------ Compile context ------------ */327328PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION329pcre2_set_character_tables(pcre2_compile_context *ccontext,330const uint8_t *tables)331{332ccontext->tables = tables;333return 0;334}335336PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION337pcre2_set_bsr(pcre2_compile_context *ccontext, uint32_t value)338{339switch(value)340{341case PCRE2_BSR_ANYCRLF:342case PCRE2_BSR_UNICODE:343ccontext->bsr_convention = value;344return 0;345346default:347return PCRE2_ERROR_BADDATA;348}349}350351PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION352pcre2_set_max_pattern_length(pcre2_compile_context *ccontext, PCRE2_SIZE length)353{354ccontext->max_pattern_length = length;355return 0;356}357358PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION359pcre2_set_max_pattern_compiled_length(pcre2_compile_context *ccontext, PCRE2_SIZE length)360{361ccontext->max_pattern_compiled_length = length;362return 0;363}364365PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION366pcre2_set_newline(pcre2_compile_context *ccontext, uint32_t newline)367{368switch(newline)369{370case PCRE2_NEWLINE_CR:371case PCRE2_NEWLINE_LF:372case PCRE2_NEWLINE_CRLF:373case PCRE2_NEWLINE_ANY:374case PCRE2_NEWLINE_ANYCRLF:375case PCRE2_NEWLINE_NUL:376ccontext->newline_convention = newline;377return 0;378379default:380return PCRE2_ERROR_BADDATA;381}382}383384PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION385pcre2_set_max_varlookbehind(pcre2_compile_context *ccontext, uint32_t limit)386{387ccontext->max_varlookbehind = limit;388return 0;389}390391PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION392pcre2_set_parens_nest_limit(pcre2_compile_context *ccontext, uint32_t limit)393{394ccontext->parens_nest_limit = limit;395return 0;396}397398PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION399pcre2_set_compile_extra_options(pcre2_compile_context *ccontext, uint32_t options)400{401ccontext->extra_options = options;402return 0;403}404405PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION406pcre2_set_compile_recursion_guard(pcre2_compile_context *ccontext,407int (*guard)(uint32_t, void *), void *user_data)408{409ccontext->stack_guard = guard;410ccontext->stack_guard_data = user_data;411return 0;412}413414PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION415pcre2_set_optimize(pcre2_compile_context *ccontext, uint32_t directive)416{417if (ccontext == NULL)418return PCRE2_ERROR_NULL;419420switch (directive)421{422case PCRE2_OPTIMIZATION_NONE:423ccontext->optimization_flags = 0;424break;425426case PCRE2_OPTIMIZATION_FULL:427ccontext->optimization_flags = PCRE2_OPTIMIZATION_ALL;428break;429430default:431if (directive >= PCRE2_AUTO_POSSESS && directive <= PCRE2_START_OPTIMIZE_OFF)432{433/* Even directive numbers starting from 64 switch a bit on;434* Odd directive numbers starting from 65 switch a bit off */435if ((directive & 1) != 0)436ccontext->optimization_flags &= ~(1u << ((directive >> 1) - 32));437else438ccontext->optimization_flags |= 1u << ((directive >> 1) - 32);439return 0;440}441return PCRE2_ERROR_BADOPTION;442}443444return 0;445}446447/* ------------ Match context ------------ */448449PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION450pcre2_set_callout(pcre2_match_context *mcontext,451int (*callout)(pcre2_callout_block *, void *), void *callout_data)452{453mcontext->callout = callout;454mcontext->callout_data = callout_data;455return 0;456}457458PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION459pcre2_set_substitute_callout(pcre2_match_context *mcontext,460int (*substitute_callout)(pcre2_substitute_callout_block *, void *),461void *substitute_callout_data)462{463mcontext->substitute_callout = substitute_callout;464mcontext->substitute_callout_data = substitute_callout_data;465return 0;466}467468PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION469pcre2_set_substitute_case_callout(pcre2_match_context *mcontext,470PCRE2_SIZE (*substitute_case_callout)(PCRE2_SPTR, PCRE2_SIZE, PCRE2_UCHAR *,471PCRE2_SIZE, int, void *),472void *substitute_case_callout_data)473{474mcontext->substitute_case_callout = substitute_case_callout;475mcontext->substitute_case_callout_data = substitute_case_callout_data;476return 0;477}478479PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION480pcre2_set_heap_limit(pcre2_match_context *mcontext, uint32_t limit)481{482mcontext->heap_limit = limit;483return 0;484}485486PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION487pcre2_set_match_limit(pcre2_match_context *mcontext, uint32_t limit)488{489mcontext->match_limit = limit;490return 0;491}492493PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION494pcre2_set_depth_limit(pcre2_match_context *mcontext, uint32_t limit)495{496mcontext->depth_limit = limit;497return 0;498}499500PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION501pcre2_set_offset_limit(pcre2_match_context *mcontext, PCRE2_SIZE limit)502{503mcontext->offset_limit = limit;504return 0;505}506507/* These functions became obsolete at release 10.30. The first is kept as a508synonym for backwards compatibility. The second now does nothing. Exclude both509from coverage reports. */510511/* LCOV_EXCL_START */512513PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION514pcre2_set_recursion_limit(pcre2_match_context *mcontext, uint32_t limit)515{516return pcre2_set_depth_limit(mcontext, limit);517}518519PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION520pcre2_set_recursion_memory_management(pcre2_match_context *mcontext,521void *(*mymalloc)(size_t, void *), void (*myfree)(void *, void *),522void *mydata)523{524(void)mcontext;525(void)mymalloc;526(void)myfree;527(void)mydata;528return 0;529}530531/* LCOV_EXCL_STOP */532533534/* ------------ Convert context ------------ */535536PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION537pcre2_set_glob_separator(pcre2_convert_context *ccontext, uint32_t separator)538{539if (separator != CHAR_SLASH && separator != CHAR_BACKSLASH &&540separator != CHAR_DOT) return PCRE2_ERROR_BADDATA;541ccontext->glob_separator = separator;542return 0;543}544545PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION546pcre2_set_glob_escape(pcre2_convert_context *ccontext, uint32_t escape)547{548if (escape > 255 || (escape != 0 && !ispunct(escape)))549return PCRE2_ERROR_BADDATA;550ccontext->glob_escape = escape;551return 0;552}553554/* End of pcre2_context.c */555556557558