Path: blob/master/thirdparty/pcre2/src/pcre2_context.c
21438 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#include "pcre2_internal.h"42434445/*************************************************46* Default malloc/free functions *47*************************************************/4849/* Ignore the "user data" argument in each case. */5051static void *default_malloc(size_t size, void *data)52{53(void)data;54return malloc(size);55}565758static void default_free(void *block, void *data)59{60(void)data;61free(block);62}63646566/*************************************************67* Get a block and save memory control *68*************************************************/6970/* This internal function is called to get a block of memory in which the71memory control data is to be stored at the start for future use.7273Arguments:74size amount of memory required75memctl pointer to a memctl block or NULL7677Returns: pointer to memory or NULL on failure78*/7980extern void *81PRIV(memctl_malloc)(size_t size, pcre2_memctl *memctl)82{83pcre2_memctl *newmemctl;84void *yield = (memctl == NULL)? malloc(size) :85memctl->malloc(size, memctl->memory_data);86if (yield == NULL) return NULL;87newmemctl = (pcre2_memctl *)yield;88if (memctl == NULL)89{90newmemctl->malloc = default_malloc;91newmemctl->free = default_free;92newmemctl->memory_data = NULL;93}94else *newmemctl = *memctl;95return yield;96}979899100/*************************************************101* Create and initialize contexts *102*************************************************/103104/* Initializing for compile and match contexts is done in separate, private105functions so that these can be called from functions such as pcre2_compile()106when an external context is not supplied. The initializing functions have an107option to set up default memory management. */108109PCRE2_EXP_DEFN pcre2_general_context * PCRE2_CALL_CONVENTION110pcre2_general_context_create(void *(*private_malloc)(size_t, void *),111void (*private_free)(void *, void *), void *memory_data)112{113pcre2_general_context *gcontext;114if (private_malloc == NULL) private_malloc = default_malloc;115if (private_free == NULL) private_free = default_free;116gcontext = private_malloc(sizeof(pcre2_real_general_context), memory_data);117if (gcontext == NULL) return NULL;118gcontext->memctl.malloc = private_malloc;119gcontext->memctl.free = private_free;120gcontext->memctl.memory_data = memory_data;121return gcontext;122}123124125/* A default compile context is set up to save having to initialize at run time126when no context is supplied to the compile function. */127128pcre2_compile_context PRIV(default_compile_context) = {129{ default_malloc, default_free, NULL }, /* Default memory handling */130NULL, /* Stack guard */131NULL, /* Stack guard data */132PRIV(default_tables), /* Character tables */133PCRE2_UNSET, /* Max pattern length */134PCRE2_UNSET, /* Max pattern compiled length */135BSR_DEFAULT, /* Backslash R default */136NEWLINE_DEFAULT, /* Newline convention */137PARENS_NEST_LIMIT, /* As it says */1380, /* Extra options */139MAX_VARLOOKBEHIND, /* As it says */140PCRE2_OPTIMIZATION_ALL /* All optimizations enabled */141};142143/* The create function copies the default into the new memory, but must144override the default memory handling functions if a gcontext was provided. */145146PCRE2_EXP_DEFN pcre2_compile_context * PCRE2_CALL_CONVENTION147pcre2_compile_context_create(pcre2_general_context *gcontext)148{149pcre2_compile_context *ccontext = PRIV(memctl_malloc)(150sizeof(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}157158159/* A default match context is set up to save having to initialize at run time160when no context is supplied to a match function. */161162pcre2_match_context PRIV(default_match_context) = {163{ default_malloc, default_free, NULL },164#ifdef SUPPORT_JIT165NULL, /* JIT callback */166NULL, /* JIT callback data */167#endif168NULL, /* Callout function */169NULL, /* Callout data */170NULL, /* Substitute callout function */171NULL, /* Substitute callout data */172NULL, /* Substitute case callout function */173NULL, /* Substitute case callout data */174PCRE2_UNSET, /* Offset limit */175HEAP_LIMIT,176MATCH_LIMIT,177MATCH_LIMIT_DEPTH };178179/* The create function copies the default into the new memory, but must180override the default memory handling functions if a gcontext was provided. */181182PCRE2_EXP_DEFN pcre2_match_context * PCRE2_CALL_CONVENTION183pcre2_match_context_create(pcre2_general_context *gcontext)184{185pcre2_match_context *mcontext = PRIV(memctl_malloc)(186sizeof(pcre2_real_match_context), (pcre2_memctl *)gcontext);187if (mcontext == NULL) return NULL;188*mcontext = PRIV(default_match_context);189if (gcontext != NULL)190*((pcre2_memctl *)mcontext) = *((pcre2_memctl *)gcontext);191return mcontext;192}193194195/* A default convert context is set up to save having to initialize at run time196when no context is supplied to the convert function. */197198pcre2_convert_context PRIV(default_convert_context) = {199{ default_malloc, default_free, NULL }, /* Default memory handling */200#ifdef _WIN32201CHAR_BACKSLASH, /* Default path separator */202CHAR_GRAVE_ACCENT /* Default escape character */203#else /* Not Windows */204CHAR_SLASH, /* Default path separator */205CHAR_BACKSLASH /* Default escape character */206#endif207};208209/* The create function copies the default into the new memory, but must210override the default memory handling functions if a gcontext was provided. */211212PCRE2_EXP_DEFN pcre2_convert_context * PCRE2_CALL_CONVENTION213pcre2_convert_context_create(pcre2_general_context *gcontext)214{215pcre2_convert_context *ccontext = PRIV(memctl_malloc)(216sizeof(pcre2_real_convert_context), (pcre2_memctl *)gcontext);217if (ccontext == NULL) return NULL;218*ccontext = PRIV(default_convert_context);219if (gcontext != NULL)220*((pcre2_memctl *)ccontext) = *((pcre2_memctl *)gcontext);221return ccontext;222}223224225/*************************************************226* Context copy functions *227*************************************************/228229PCRE2_EXP_DEFN pcre2_general_context * PCRE2_CALL_CONVENTION230pcre2_general_context_copy(pcre2_general_context *gcontext)231{232pcre2_general_context *newcontext =233gcontext->memctl.malloc(sizeof(pcre2_real_general_context),234gcontext->memctl.memory_data);235if (newcontext == NULL) return NULL;236memcpy(newcontext, gcontext, sizeof(pcre2_real_general_context));237return newcontext;238}239240241PCRE2_EXP_DEFN pcre2_compile_context * PCRE2_CALL_CONVENTION242pcre2_compile_context_copy(pcre2_compile_context *ccontext)243{244pcre2_compile_context *newcontext =245ccontext->memctl.malloc(sizeof(pcre2_real_compile_context),246ccontext->memctl.memory_data);247if (newcontext == NULL) return NULL;248memcpy(newcontext, ccontext, sizeof(pcre2_real_compile_context));249return newcontext;250}251252253PCRE2_EXP_DEFN pcre2_match_context * PCRE2_CALL_CONVENTION254pcre2_match_context_copy(pcre2_match_context *mcontext)255{256pcre2_match_context *newcontext =257mcontext->memctl.malloc(sizeof(pcre2_real_match_context),258mcontext->memctl.memory_data);259if (newcontext == NULL) return NULL;260memcpy(newcontext, mcontext, sizeof(pcre2_real_match_context));261return newcontext;262}263264265PCRE2_EXP_DEFN pcre2_convert_context * PCRE2_CALL_CONVENTION266pcre2_convert_context_copy(pcre2_convert_context *ccontext)267{268pcre2_convert_context *newcontext =269ccontext->memctl.malloc(sizeof(pcre2_real_convert_context),270ccontext->memctl.memory_data);271if (newcontext == NULL) return NULL;272memcpy(newcontext, ccontext, sizeof(pcre2_real_convert_context));273return newcontext;274}275276277/*************************************************278* Context free functions *279*************************************************/280281PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION282pcre2_general_context_free(pcre2_general_context *gcontext)283{284if (gcontext != NULL)285gcontext->memctl.free(gcontext, gcontext->memctl.memory_data);286}287288289PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION290pcre2_compile_context_free(pcre2_compile_context *ccontext)291{292if (ccontext != NULL)293ccontext->memctl.free(ccontext, ccontext->memctl.memory_data);294}295296297PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION298pcre2_match_context_free(pcre2_match_context *mcontext)299{300if (mcontext != NULL)301mcontext->memctl.free(mcontext, mcontext->memctl.memory_data);302}303304305PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION306pcre2_convert_context_free(pcre2_convert_context *ccontext)307{308if (ccontext != NULL)309ccontext->memctl.free(ccontext, ccontext->memctl.memory_data);310}311312313/*************************************************314* Set values in contexts *315*************************************************/316317/* All these functions return 0 for success or PCRE2_ERROR_BADDATA if invalid318data is given. Only some of the functions are able to test the validity of the319data. */320321322/* ------------ Compile context ------------ */323324PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION325pcre2_set_character_tables(pcre2_compile_context *ccontext,326const uint8_t *tables)327{328ccontext->tables = tables;329return 0;330}331332PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION333pcre2_set_bsr(pcre2_compile_context *ccontext, uint32_t value)334{335switch(value)336{337case PCRE2_BSR_ANYCRLF:338case PCRE2_BSR_UNICODE:339ccontext->bsr_convention = value;340return 0;341342default:343return PCRE2_ERROR_BADDATA;344}345}346347PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION348pcre2_set_max_pattern_length(pcre2_compile_context *ccontext, PCRE2_SIZE length)349{350ccontext->max_pattern_length = length;351return 0;352}353354PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION355pcre2_set_max_pattern_compiled_length(pcre2_compile_context *ccontext, PCRE2_SIZE length)356{357ccontext->max_pattern_compiled_length = length;358return 0;359}360361PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION362pcre2_set_newline(pcre2_compile_context *ccontext, uint32_t newline)363{364switch(newline)365{366case PCRE2_NEWLINE_CR:367case PCRE2_NEWLINE_LF:368case PCRE2_NEWLINE_CRLF:369case PCRE2_NEWLINE_ANY:370case PCRE2_NEWLINE_ANYCRLF:371case PCRE2_NEWLINE_NUL:372ccontext->newline_convention = newline;373return 0;374375default:376return PCRE2_ERROR_BADDATA;377}378}379380PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION381pcre2_set_max_varlookbehind(pcre2_compile_context *ccontext, uint32_t limit)382{383ccontext->max_varlookbehind = limit;384return 0;385}386387PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION388pcre2_set_parens_nest_limit(pcre2_compile_context *ccontext, uint32_t limit)389{390ccontext->parens_nest_limit = limit;391return 0;392}393394PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION395pcre2_set_compile_extra_options(pcre2_compile_context *ccontext, uint32_t options)396{397ccontext->extra_options = options;398return 0;399}400401PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION402pcre2_set_compile_recursion_guard(pcre2_compile_context *ccontext,403int (*guard)(uint32_t, void *), void *user_data)404{405ccontext->stack_guard = guard;406ccontext->stack_guard_data = user_data;407return 0;408}409410PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION411pcre2_set_optimize(pcre2_compile_context *ccontext, uint32_t directive)412{413if (ccontext == NULL)414return PCRE2_ERROR_NULL;415416switch (directive)417{418case PCRE2_OPTIMIZATION_NONE:419ccontext->optimization_flags = 0;420break;421422case PCRE2_OPTIMIZATION_FULL:423ccontext->optimization_flags = PCRE2_OPTIMIZATION_ALL;424break;425426default:427if (directive >= PCRE2_AUTO_POSSESS && directive <= PCRE2_START_OPTIMIZE_OFF)428{429/* Even directive numbers starting from 64 switch a bit on;430* Odd directive numbers starting from 65 switch a bit off */431if ((directive & 1) != 0)432ccontext->optimization_flags &= ~(1u << ((directive >> 1) - 32));433else434ccontext->optimization_flags |= 1u << ((directive >> 1) - 32);435return 0;436}437return PCRE2_ERROR_BADOPTION;438}439440return 0;441}442443/* ------------ Match context ------------ */444445PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION446pcre2_set_callout(pcre2_match_context *mcontext,447int (*callout)(pcre2_callout_block *, void *), void *callout_data)448{449mcontext->callout = callout;450mcontext->callout_data = callout_data;451return 0;452}453454PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION455pcre2_set_substitute_callout(pcre2_match_context *mcontext,456int (*substitute_callout)(pcre2_substitute_callout_block *, void *),457void *substitute_callout_data)458{459mcontext->substitute_callout = substitute_callout;460mcontext->substitute_callout_data = substitute_callout_data;461return 0;462}463464PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION465pcre2_set_substitute_case_callout(pcre2_match_context *mcontext,466PCRE2_SIZE (*substitute_case_callout)(PCRE2_SPTR, PCRE2_SIZE, PCRE2_UCHAR *,467PCRE2_SIZE, int, void *),468void *substitute_case_callout_data)469{470mcontext->substitute_case_callout = substitute_case_callout;471mcontext->substitute_case_callout_data = substitute_case_callout_data;472return 0;473}474475PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION476pcre2_set_heap_limit(pcre2_match_context *mcontext, uint32_t limit)477{478mcontext->heap_limit = limit;479return 0;480}481482PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION483pcre2_set_match_limit(pcre2_match_context *mcontext, uint32_t limit)484{485mcontext->match_limit = limit;486return 0;487}488489PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION490pcre2_set_depth_limit(pcre2_match_context *mcontext, uint32_t limit)491{492mcontext->depth_limit = limit;493return 0;494}495496PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION497pcre2_set_offset_limit(pcre2_match_context *mcontext, PCRE2_SIZE limit)498{499mcontext->offset_limit = limit;500return 0;501}502503/* These functions became obsolete at release 10.30. The first is kept as a504synonym for backwards compatibility. The second now does nothing. */505506PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION507pcre2_set_recursion_limit(pcre2_match_context *mcontext, uint32_t limit)508{509return pcre2_set_depth_limit(mcontext, limit);510}511512PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION513pcre2_set_recursion_memory_management(pcre2_match_context *mcontext,514void *(*mymalloc)(size_t, void *), void (*myfree)(void *, void *),515void *mydata)516{517(void)mcontext;518(void)mymalloc;519(void)myfree;520(void)mydata;521return 0;522}523524525/* ------------ Convert context ------------ */526527PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION528pcre2_set_glob_separator(pcre2_convert_context *ccontext, uint32_t separator)529{530if (separator != CHAR_SLASH && separator != CHAR_BACKSLASH &&531separator != CHAR_DOT) return PCRE2_ERROR_BADDATA;532ccontext->glob_separator = separator;533return 0;534}535536static const char *globpunct =537STR_EXCLAMATION_MARK STR_QUOTATION_MARK STR_NUMBER_SIGN STR_DOLLAR_SIGN538STR_PERCENT_SIGN STR_AMPERSAND STR_APOSTROPHE STR_LEFT_PARENTHESIS539STR_RIGHT_PARENTHESIS STR_ASTERISK STR_PLUS STR_COMMA STR_MINUS STR_DOT540STR_SLASH STR_COLON STR_SEMICOLON STR_LESS_THAN_SIGN STR_EQUALS_SIGN541STR_GREATER_THAN_SIGN STR_QUESTION_MARK STR_COMMERCIAL_AT542STR_LEFT_SQUARE_BRACKET STR_BACKSLASH STR_RIGHT_SQUARE_BRACKET543STR_CIRCUMFLEX_ACCENT STR_UNDERSCORE STR_GRAVE_ACCENT STR_LEFT_CURLY_BRACKET544STR_VERTICAL_LINE STR_RIGHT_CURLY_BRACKET STR_TILDE;545546PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION547pcre2_set_glob_escape(pcre2_convert_context *ccontext, uint32_t escape)548{549if (escape > 255 || (escape != 0 && strchr(globpunct, escape) == NULL))550return PCRE2_ERROR_BADDATA;551ccontext->glob_escape = escape;552return 0;553}554555/* End of pcre2_context.c */556557558559