Path: blob/master/thirdparty/pcre2/src/pcre2_serialize.c
9898 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*/3940/* This module contains functions for serializing and deserializing41a sequence of compiled codes. */424344#ifdef HAVE_CONFIG_H45#include "config.h"46#endif474849#include "pcre2_internal.h"5051/* Magic number to provide a small check against being handed junk. */5253#define SERIALIZED_DATA_MAGIC 0x50523253u5455/* Deserialization is limited to the current PCRE version and56character width. */5758#define SERIALIZED_DATA_VERSION \59((PCRE2_MAJOR) | ((PCRE2_MINOR) << 16))6061#define SERIALIZED_DATA_CONFIG \62(sizeof(PCRE2_UCHAR) | ((sizeof(void*)) << 8) | ((sizeof(PCRE2_SIZE)) << 16))63646566/*************************************************67* Serialize compiled patterns *68*************************************************/6970PCRE2_EXP_DEFN int32_t PCRE2_CALL_CONVENTION71pcre2_serialize_encode(const pcre2_code **codes, int32_t number_of_codes,72uint8_t **serialized_bytes, PCRE2_SIZE *serialized_size,73pcre2_general_context *gcontext)74{75uint8_t *bytes;76uint8_t *dst_bytes;77int32_t i;78PCRE2_SIZE total_size;79const pcre2_real_code *re;80const uint8_t *tables;81pcre2_serialized_data *data;8283const pcre2_memctl *memctl = (gcontext != NULL) ?84&gcontext->memctl : &PRIV(default_compile_context).memctl;8586if (codes == NULL || serialized_bytes == NULL || serialized_size == NULL)87return PCRE2_ERROR_NULL;8889if (number_of_codes <= 0) return PCRE2_ERROR_BADDATA;9091/* Compute total size. */92total_size = sizeof(pcre2_serialized_data) + TABLES_LENGTH;93tables = NULL;9495for (i = 0; i < number_of_codes; i++)96{97if (codes[i] == NULL) return PCRE2_ERROR_NULL;98re = (const pcre2_real_code *)(codes[i]);99if (re->magic_number != MAGIC_NUMBER) return PCRE2_ERROR_BADMAGIC;100if (tables == NULL)101tables = re->tables;102else if (tables != re->tables)103return PCRE2_ERROR_MIXEDTABLES;104total_size += re->blocksize;105}106107/* Initialize the byte stream. */108bytes = memctl->malloc(total_size + sizeof(pcre2_memctl), memctl->memory_data);109if (bytes == NULL) return PCRE2_ERROR_NOMEMORY;110111/* The controller is stored as a hidden parameter. */112memcpy(bytes, memctl, sizeof(pcre2_memctl));113bytes += sizeof(pcre2_memctl);114115data = (pcre2_serialized_data *)bytes;116data->magic = SERIALIZED_DATA_MAGIC;117data->version = SERIALIZED_DATA_VERSION;118data->config = SERIALIZED_DATA_CONFIG;119data->number_of_codes = number_of_codes;120121/* Copy all compiled code data. */122dst_bytes = bytes + sizeof(pcre2_serialized_data);123memcpy(dst_bytes, tables, TABLES_LENGTH);124dst_bytes += TABLES_LENGTH;125126for (i = 0; i < number_of_codes; i++)127{128re = (const pcre2_real_code *)(codes[i]);129(void)memcpy(dst_bytes, (const char *)re, re->blocksize);130131/* Certain fields in the compiled code block are re-set during132deserialization. In order to ensure that the serialized data stream is always133the same for the same pattern, set them to zero here. We can't assume the134copy of the pattern is correctly aligned for accessing the fields as part of135a structure. Note the use of sizeof(void *) in the second of these, to136specify the size of a pointer. If sizeof(uint8_t *) is used (tables is a137pointer to uint8_t), gcc gives a warning because the first argument is also a138pointer to uint8_t. Casting the first argument to (void *) can stop this, but139it didn't stop Coverity giving the same complaint. */140141(void)memset(dst_bytes + offsetof(pcre2_real_code, memctl), 0,142sizeof(pcre2_memctl));143(void)memset(dst_bytes + offsetof(pcre2_real_code, tables), 0,144sizeof(void *));145(void)memset(dst_bytes + offsetof(pcre2_real_code, executable_jit), 0,146sizeof(void *));147148dst_bytes += re->blocksize;149}150151*serialized_bytes = bytes;152*serialized_size = total_size;153return number_of_codes;154}155156157/*************************************************158* Deserialize compiled patterns *159*************************************************/160161PCRE2_EXP_DEFN int32_t PCRE2_CALL_CONVENTION162pcre2_serialize_decode(pcre2_code **codes, int32_t number_of_codes,163const uint8_t *bytes, pcre2_general_context *gcontext)164{165const pcre2_serialized_data *data = (const pcre2_serialized_data *)bytes;166const pcre2_memctl *memctl = (gcontext != NULL) ?167&gcontext->memctl : &PRIV(default_compile_context).memctl;168169const uint8_t *src_bytes;170pcre2_real_code *dst_re;171uint8_t *tables;172int32_t i, j;173174/* Sanity checks. */175176if (data == NULL || codes == NULL) return PCRE2_ERROR_NULL;177if (number_of_codes <= 0) return PCRE2_ERROR_BADDATA;178if (data->number_of_codes <= 0) return PCRE2_ERROR_BADSERIALIZEDDATA;179if (data->magic != SERIALIZED_DATA_MAGIC) return PCRE2_ERROR_BADMAGIC;180if (data->version != SERIALIZED_DATA_VERSION) return PCRE2_ERROR_BADMODE;181if (data->config != SERIALIZED_DATA_CONFIG) return PCRE2_ERROR_BADMODE;182183if (number_of_codes > data->number_of_codes)184number_of_codes = data->number_of_codes;185186src_bytes = bytes + sizeof(pcre2_serialized_data);187188/* Decode tables. The reference count for the tables is stored immediately189following them. */190191tables = memctl->malloc(TABLES_LENGTH + sizeof(PCRE2_SIZE), memctl->memory_data);192if (tables == NULL) return PCRE2_ERROR_NOMEMORY;193194memcpy(tables, src_bytes, TABLES_LENGTH);195*(PCRE2_SIZE *)(tables + TABLES_LENGTH) = number_of_codes;196src_bytes += TABLES_LENGTH;197198/* Decode the byte stream. We must not try to read the size from the compiled199code block in the stream, because it might be unaligned, which causes errors on200hardware such as Sparc-64 that doesn't like unaligned memory accesses. The type201of the blocksize field is given its own name to ensure that it is the same here202as in the block. */203204for (i = 0; i < number_of_codes; i++)205{206CODE_BLOCKSIZE_TYPE blocksize;207memcpy(&blocksize, src_bytes + offsetof(pcre2_real_code, blocksize),208sizeof(CODE_BLOCKSIZE_TYPE));209if (blocksize <= sizeof(pcre2_real_code))210return PCRE2_ERROR_BADSERIALIZEDDATA;211212/* The allocator provided by gcontext replaces the original one. */213214dst_re = (pcre2_real_code *)PRIV(memctl_malloc)(blocksize,215(pcre2_memctl *)gcontext);216if (dst_re == NULL)217{218memctl->free(tables, memctl->memory_data);219for (j = 0; j < i; j++)220{221memctl->free(codes[j], memctl->memory_data);222codes[j] = NULL;223}224return PCRE2_ERROR_NOMEMORY;225}226227/* The new allocator must be preserved. */228229memcpy(((uint8_t *)dst_re) + sizeof(pcre2_memctl),230src_bytes + sizeof(pcre2_memctl), blocksize - sizeof(pcre2_memctl));231if (dst_re->magic_number != MAGIC_NUMBER ||232dst_re->name_entry_size > MAX_NAME_SIZE + IMM2_SIZE + 1 ||233dst_re->name_count > MAX_NAME_COUNT)234{235memctl->free(dst_re, memctl->memory_data);236return PCRE2_ERROR_BADSERIALIZEDDATA;237}238239/* At the moment only one table is supported. */240241dst_re->tables = tables;242dst_re->executable_jit = NULL;243dst_re->flags |= PCRE2_DEREF_TABLES;244245codes[i] = dst_re;246src_bytes += blocksize;247}248249return number_of_codes;250}251252253/*************************************************254* Get the number of serialized patterns *255*************************************************/256257PCRE2_EXP_DEFN int32_t PCRE2_CALL_CONVENTION258pcre2_serialize_get_number_of_codes(const uint8_t *bytes)259{260const pcre2_serialized_data *data = (const pcre2_serialized_data *)bytes;261262if (data == NULL) return PCRE2_ERROR_NULL;263if (data->magic != SERIALIZED_DATA_MAGIC) return PCRE2_ERROR_BADMAGIC;264if (data->version != SERIALIZED_DATA_VERSION) return PCRE2_ERROR_BADMODE;265if (data->config != SERIALIZED_DATA_CONFIG) return PCRE2_ERROR_BADMODE;266267return data->number_of_codes;268}269270271/*************************************************272* Free the allocated stream *273*************************************************/274275PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION276pcre2_serialize_free(uint8_t *bytes)277{278if (bytes != NULL)279{280pcre2_memctl *memctl = (pcre2_memctl *)(bytes - sizeof(pcre2_memctl));281memctl->free(memctl, memctl->memory_data);282}283}284285/* End of pcre2_serialize.c */286287288