Path: blob/master/thirdparty/pcre2/src/pcre2_serialize.c
21646 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/* This module contains functions for serializing and deserializing42a sequence of compiled codes. */434445#include "pcre2_internal.h"46474849/* Magic number to provide a small check against being handed junk. */5051#define SERIALIZED_DATA_MAGIC 0x50523253u5253/* Deserialization is limited to the current PCRE version and54character width. */5556#define SERIALIZED_DATA_VERSION \57((PCRE2_MAJOR) | ((PCRE2_MINOR) << 16))5859#define SERIALIZED_DATA_CONFIG \60(sizeof(PCRE2_UCHAR) | ((sizeof(void*)) << 8) | ((sizeof(PCRE2_SIZE)) << 16))61626364/*************************************************65* Serialize compiled patterns *66*************************************************/6768PCRE2_EXP_DEFN int32_t PCRE2_CALL_CONVENTION69pcre2_serialize_encode(const pcre2_code **codes, int32_t number_of_codes,70uint8_t **serialized_bytes, PCRE2_SIZE *serialized_size,71pcre2_general_context *gcontext)72{73uint8_t *bytes;74uint8_t *dst_bytes;75int32_t i;76PCRE2_SIZE total_size;77const pcre2_real_code *re;78const uint8_t *tables;79pcre2_serialized_data *data;8081const pcre2_memctl *memctl = (gcontext != NULL) ?82&gcontext->memctl : &PRIV(default_compile_context).memctl;8384if (codes == NULL || serialized_bytes == NULL || serialized_size == NULL)85return PCRE2_ERROR_NULL;8687if (number_of_codes <= 0) return PCRE2_ERROR_BADDATA;8889/* Compute total size. */90total_size = sizeof(pcre2_serialized_data) + TABLES_LENGTH;91tables = NULL;9293for (i = 0; i < number_of_codes; i++)94{95if (codes[i] == NULL) return PCRE2_ERROR_NULL;96re = (const pcre2_real_code *)(codes[i]);97if (re->magic_number != MAGIC_NUMBER) return PCRE2_ERROR_BADMAGIC;98if (tables == NULL)99tables = re->tables;100else if (tables != re->tables)101return PCRE2_ERROR_MIXEDTABLES;102total_size += re->blocksize;103}104105/* Initialize the byte stream. */106bytes = memctl->malloc(total_size + sizeof(pcre2_memctl), memctl->memory_data);107if (bytes == NULL) return PCRE2_ERROR_NOMEMORY;108109/* The controller is stored as a hidden parameter. */110memcpy(bytes, memctl, sizeof(pcre2_memctl));111bytes += sizeof(pcre2_memctl);112113data = (pcre2_serialized_data *)bytes;114data->magic = SERIALIZED_DATA_MAGIC;115data->version = SERIALIZED_DATA_VERSION;116data->config = SERIALIZED_DATA_CONFIG;117data->number_of_codes = number_of_codes;118119/* Copy all compiled code data. */120dst_bytes = bytes + sizeof(pcre2_serialized_data);121memcpy(dst_bytes, tables, TABLES_LENGTH);122dst_bytes += TABLES_LENGTH;123124for (i = 0; i < number_of_codes; i++)125{126re = (const pcre2_real_code *)(codes[i]);127(void)memcpy(dst_bytes, (const char *)re, re->blocksize);128129/* Certain fields in the compiled code block are re-set during130deserialization. In order to ensure that the serialized data stream is always131the same for the same pattern, set them to zero here. We can't assume the132copy of the pattern is correctly aligned for accessing the fields as part of133a structure. Note the use of sizeof(void *) in the second of these, to134specify the size of a pointer. If sizeof(uint8_t *) is used (tables is a135pointer to uint8_t), gcc gives a warning because the first argument is also a136pointer to uint8_t. Casting the first argument to (void *) can stop this, but137it didn't stop Coverity giving the same complaint. */138139(void)memset(dst_bytes + offsetof(pcre2_real_code, memctl), 0,140sizeof(pcre2_memctl));141(void)memset(dst_bytes + offsetof(pcre2_real_code, tables), 0,142sizeof(void *));143(void)memset(dst_bytes + offsetof(pcre2_real_code, executable_jit), 0,144sizeof(void *));145146dst_bytes += re->blocksize;147}148149*serialized_bytes = bytes;150*serialized_size = total_size;151return number_of_codes;152}153154155/*************************************************156* Deserialize compiled patterns *157*************************************************/158159PCRE2_EXP_DEFN int32_t PCRE2_CALL_CONVENTION160pcre2_serialize_decode(pcre2_code **codes, int32_t number_of_codes,161const uint8_t *bytes, pcre2_general_context *gcontext)162{163const pcre2_serialized_data *data = (const pcre2_serialized_data *)bytes;164const pcre2_memctl *memctl = (gcontext != NULL) ?165&gcontext->memctl : &PRIV(default_compile_context).memctl;166167const uint8_t *src_bytes;168pcre2_real_code *dst_re;169uint8_t *tables;170int32_t i, j;171172/* Sanity checks. */173174if (data == NULL || codes == NULL) return PCRE2_ERROR_NULL;175if (number_of_codes <= 0) return PCRE2_ERROR_BADDATA;176if (data->number_of_codes <= 0) return PCRE2_ERROR_BADSERIALIZEDDATA;177if (data->magic != SERIALIZED_DATA_MAGIC) return PCRE2_ERROR_BADMAGIC;178if (data->version != SERIALIZED_DATA_VERSION) return PCRE2_ERROR_BADMODE;179if (data->config != SERIALIZED_DATA_CONFIG) return PCRE2_ERROR_BADMODE;180181if (number_of_codes > data->number_of_codes)182number_of_codes = data->number_of_codes;183184src_bytes = bytes + sizeof(pcre2_serialized_data);185186/* Decode tables. The reference count for the tables is stored immediately187following them. */188189tables = memctl->malloc(TABLES_LENGTH + sizeof(PCRE2_SIZE), memctl->memory_data);190if (tables == NULL) return PCRE2_ERROR_NOMEMORY;191192memcpy(tables, src_bytes, TABLES_LENGTH);193*(PCRE2_SIZE *)(tables + TABLES_LENGTH) = number_of_codes;194src_bytes += TABLES_LENGTH;195196/* Decode the byte stream. We must not try to read the size from the compiled197code block in the stream, because it might be unaligned, which causes errors on198hardware such as Sparc-64 that doesn't like unaligned memory accesses. The type199of the blocksize field is given its own name to ensure that it is the same here200as in the block. */201202for (i = 0; i < number_of_codes; i++)203{204CODE_BLOCKSIZE_TYPE blocksize;205memcpy(&blocksize, src_bytes + offsetof(pcre2_real_code, blocksize),206sizeof(CODE_BLOCKSIZE_TYPE));207if (blocksize <= sizeof(pcre2_real_code))208return PCRE2_ERROR_BADSERIALIZEDDATA;209210/* The allocator provided by gcontext replaces the original one. */211212dst_re = (pcre2_real_code *)PRIV(memctl_malloc)(blocksize,213(pcre2_memctl *)gcontext);214if (dst_re == NULL)215{216memctl->free(tables, memctl->memory_data);217for (j = 0; j < i; j++)218{219memctl->free(codes[j], memctl->memory_data);220codes[j] = NULL;221}222return PCRE2_ERROR_NOMEMORY;223}224225/* The new allocator must be preserved. */226227memcpy(((uint8_t *)dst_re) + sizeof(pcre2_memctl),228src_bytes + sizeof(pcre2_memctl), blocksize - sizeof(pcre2_memctl));229if (dst_re->magic_number != MAGIC_NUMBER ||230dst_re->name_entry_size > MAX_NAME_SIZE + IMM2_SIZE + 1 ||231dst_re->name_count > MAX_NAME_COUNT)232{233memctl->free(dst_re, memctl->memory_data);234return PCRE2_ERROR_BADSERIALIZEDDATA;235}236237/* At the moment only one table is supported. */238239dst_re->tables = tables;240dst_re->executable_jit = NULL;241dst_re->flags |= PCRE2_DEREF_TABLES;242243codes[i] = dst_re;244src_bytes += blocksize;245}246247return number_of_codes;248}249250251/*************************************************252* Get the number of serialized patterns *253*************************************************/254255PCRE2_EXP_DEFN int32_t PCRE2_CALL_CONVENTION256pcre2_serialize_get_number_of_codes(const uint8_t *bytes)257{258const pcre2_serialized_data *data = (const pcre2_serialized_data *)bytes;259260if (data == NULL) return PCRE2_ERROR_NULL;261if (data->magic != SERIALIZED_DATA_MAGIC) return PCRE2_ERROR_BADMAGIC;262if (data->version != SERIALIZED_DATA_VERSION) return PCRE2_ERROR_BADMODE;263if (data->config != SERIALIZED_DATA_CONFIG) return PCRE2_ERROR_BADMODE;264265return data->number_of_codes;266}267268269/*************************************************270* Free the allocated stream *271*************************************************/272273PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION274pcre2_serialize_free(uint8_t *bytes)275{276if (bytes != NULL)277{278pcre2_memctl *memctl = (pcre2_memctl *)(bytes - sizeof(pcre2_memctl));279memctl->free(memctl, memctl->memory_data);280}281}282283/* End of pcre2_serialize.c */284285286