Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/pcre2/src/pcre2_serialize.c
21646 views
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-2024 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
/* This module contains functions for serializing and deserializing
43
a sequence of compiled codes. */
44
45
46
#include "pcre2_internal.h"
47
48
49
50
/* Magic number to provide a small check against being handed junk. */
51
52
#define SERIALIZED_DATA_MAGIC 0x50523253u
53
54
/* Deserialization is limited to the current PCRE version and
55
character width. */
56
57
#define SERIALIZED_DATA_VERSION \
58
((PCRE2_MAJOR) | ((PCRE2_MINOR) << 16))
59
60
#define SERIALIZED_DATA_CONFIG \
61
(sizeof(PCRE2_UCHAR) | ((sizeof(void*)) << 8) | ((sizeof(PCRE2_SIZE)) << 16))
62
63
64
65
/*************************************************
66
* Serialize compiled patterns *
67
*************************************************/
68
69
PCRE2_EXP_DEFN int32_t PCRE2_CALL_CONVENTION
70
pcre2_serialize_encode(const pcre2_code **codes, int32_t number_of_codes,
71
uint8_t **serialized_bytes, PCRE2_SIZE *serialized_size,
72
pcre2_general_context *gcontext)
73
{
74
uint8_t *bytes;
75
uint8_t *dst_bytes;
76
int32_t i;
77
PCRE2_SIZE total_size;
78
const pcre2_real_code *re;
79
const uint8_t *tables;
80
pcre2_serialized_data *data;
81
82
const pcre2_memctl *memctl = (gcontext != NULL) ?
83
&gcontext->memctl : &PRIV(default_compile_context).memctl;
84
85
if (codes == NULL || serialized_bytes == NULL || serialized_size == NULL)
86
return PCRE2_ERROR_NULL;
87
88
if (number_of_codes <= 0) return PCRE2_ERROR_BADDATA;
89
90
/* Compute total size. */
91
total_size = sizeof(pcre2_serialized_data) + TABLES_LENGTH;
92
tables = NULL;
93
94
for (i = 0; i < number_of_codes; i++)
95
{
96
if (codes[i] == NULL) return PCRE2_ERROR_NULL;
97
re = (const pcre2_real_code *)(codes[i]);
98
if (re->magic_number != MAGIC_NUMBER) return PCRE2_ERROR_BADMAGIC;
99
if (tables == NULL)
100
tables = re->tables;
101
else if (tables != re->tables)
102
return PCRE2_ERROR_MIXEDTABLES;
103
total_size += re->blocksize;
104
}
105
106
/* Initialize the byte stream. */
107
bytes = memctl->malloc(total_size + sizeof(pcre2_memctl), memctl->memory_data);
108
if (bytes == NULL) return PCRE2_ERROR_NOMEMORY;
109
110
/* The controller is stored as a hidden parameter. */
111
memcpy(bytes, memctl, sizeof(pcre2_memctl));
112
bytes += sizeof(pcre2_memctl);
113
114
data = (pcre2_serialized_data *)bytes;
115
data->magic = SERIALIZED_DATA_MAGIC;
116
data->version = SERIALIZED_DATA_VERSION;
117
data->config = SERIALIZED_DATA_CONFIG;
118
data->number_of_codes = number_of_codes;
119
120
/* Copy all compiled code data. */
121
dst_bytes = bytes + sizeof(pcre2_serialized_data);
122
memcpy(dst_bytes, tables, TABLES_LENGTH);
123
dst_bytes += TABLES_LENGTH;
124
125
for (i = 0; i < number_of_codes; i++)
126
{
127
re = (const pcre2_real_code *)(codes[i]);
128
(void)memcpy(dst_bytes, (const char *)re, re->blocksize);
129
130
/* Certain fields in the compiled code block are re-set during
131
deserialization. In order to ensure that the serialized data stream is always
132
the same for the same pattern, set them to zero here. We can't assume the
133
copy of the pattern is correctly aligned for accessing the fields as part of
134
a structure. Note the use of sizeof(void *) in the second of these, to
135
specify the size of a pointer. If sizeof(uint8_t *) is used (tables is a
136
pointer to uint8_t), gcc gives a warning because the first argument is also a
137
pointer to uint8_t. Casting the first argument to (void *) can stop this, but
138
it didn't stop Coverity giving the same complaint. */
139
140
(void)memset(dst_bytes + offsetof(pcre2_real_code, memctl), 0,
141
sizeof(pcre2_memctl));
142
(void)memset(dst_bytes + offsetof(pcre2_real_code, tables), 0,
143
sizeof(void *));
144
(void)memset(dst_bytes + offsetof(pcre2_real_code, executable_jit), 0,
145
sizeof(void *));
146
147
dst_bytes += re->blocksize;
148
}
149
150
*serialized_bytes = bytes;
151
*serialized_size = total_size;
152
return number_of_codes;
153
}
154
155
156
/*************************************************
157
* Deserialize compiled patterns *
158
*************************************************/
159
160
PCRE2_EXP_DEFN int32_t PCRE2_CALL_CONVENTION
161
pcre2_serialize_decode(pcre2_code **codes, int32_t number_of_codes,
162
const uint8_t *bytes, pcre2_general_context *gcontext)
163
{
164
const pcre2_serialized_data *data = (const pcre2_serialized_data *)bytes;
165
const pcre2_memctl *memctl = (gcontext != NULL) ?
166
&gcontext->memctl : &PRIV(default_compile_context).memctl;
167
168
const uint8_t *src_bytes;
169
pcre2_real_code *dst_re;
170
uint8_t *tables;
171
int32_t i, j;
172
173
/* Sanity checks. */
174
175
if (data == NULL || codes == NULL) return PCRE2_ERROR_NULL;
176
if (number_of_codes <= 0) return PCRE2_ERROR_BADDATA;
177
if (data->number_of_codes <= 0) return PCRE2_ERROR_BADSERIALIZEDDATA;
178
if (data->magic != SERIALIZED_DATA_MAGIC) return PCRE2_ERROR_BADMAGIC;
179
if (data->version != SERIALIZED_DATA_VERSION) return PCRE2_ERROR_BADMODE;
180
if (data->config != SERIALIZED_DATA_CONFIG) return PCRE2_ERROR_BADMODE;
181
182
if (number_of_codes > data->number_of_codes)
183
number_of_codes = data->number_of_codes;
184
185
src_bytes = bytes + sizeof(pcre2_serialized_data);
186
187
/* Decode tables. The reference count for the tables is stored immediately
188
following them. */
189
190
tables = memctl->malloc(TABLES_LENGTH + sizeof(PCRE2_SIZE), memctl->memory_data);
191
if (tables == NULL) return PCRE2_ERROR_NOMEMORY;
192
193
memcpy(tables, src_bytes, TABLES_LENGTH);
194
*(PCRE2_SIZE *)(tables + TABLES_LENGTH) = number_of_codes;
195
src_bytes += TABLES_LENGTH;
196
197
/* Decode the byte stream. We must not try to read the size from the compiled
198
code block in the stream, because it might be unaligned, which causes errors on
199
hardware such as Sparc-64 that doesn't like unaligned memory accesses. The type
200
of the blocksize field is given its own name to ensure that it is the same here
201
as in the block. */
202
203
for (i = 0; i < number_of_codes; i++)
204
{
205
CODE_BLOCKSIZE_TYPE blocksize;
206
memcpy(&blocksize, src_bytes + offsetof(pcre2_real_code, blocksize),
207
sizeof(CODE_BLOCKSIZE_TYPE));
208
if (blocksize <= sizeof(pcre2_real_code))
209
return PCRE2_ERROR_BADSERIALIZEDDATA;
210
211
/* The allocator provided by gcontext replaces the original one. */
212
213
dst_re = (pcre2_real_code *)PRIV(memctl_malloc)(blocksize,
214
(pcre2_memctl *)gcontext);
215
if (dst_re == NULL)
216
{
217
memctl->free(tables, memctl->memory_data);
218
for (j = 0; j < i; j++)
219
{
220
memctl->free(codes[j], memctl->memory_data);
221
codes[j] = NULL;
222
}
223
return PCRE2_ERROR_NOMEMORY;
224
}
225
226
/* The new allocator must be preserved. */
227
228
memcpy(((uint8_t *)dst_re) + sizeof(pcre2_memctl),
229
src_bytes + sizeof(pcre2_memctl), blocksize - sizeof(pcre2_memctl));
230
if (dst_re->magic_number != MAGIC_NUMBER ||
231
dst_re->name_entry_size > MAX_NAME_SIZE + IMM2_SIZE + 1 ||
232
dst_re->name_count > MAX_NAME_COUNT)
233
{
234
memctl->free(dst_re, memctl->memory_data);
235
return PCRE2_ERROR_BADSERIALIZEDDATA;
236
}
237
238
/* At the moment only one table is supported. */
239
240
dst_re->tables = tables;
241
dst_re->executable_jit = NULL;
242
dst_re->flags |= PCRE2_DEREF_TABLES;
243
244
codes[i] = dst_re;
245
src_bytes += blocksize;
246
}
247
248
return number_of_codes;
249
}
250
251
252
/*************************************************
253
* Get the number of serialized patterns *
254
*************************************************/
255
256
PCRE2_EXP_DEFN int32_t PCRE2_CALL_CONVENTION
257
pcre2_serialize_get_number_of_codes(const uint8_t *bytes)
258
{
259
const pcre2_serialized_data *data = (const pcre2_serialized_data *)bytes;
260
261
if (data == NULL) return PCRE2_ERROR_NULL;
262
if (data->magic != SERIALIZED_DATA_MAGIC) return PCRE2_ERROR_BADMAGIC;
263
if (data->version != SERIALIZED_DATA_VERSION) return PCRE2_ERROR_BADMODE;
264
if (data->config != SERIALIZED_DATA_CONFIG) return PCRE2_ERROR_BADMODE;
265
266
return data->number_of_codes;
267
}
268
269
270
/*************************************************
271
* Free the allocated stream *
272
*************************************************/
273
274
PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
275
pcre2_serialize_free(uint8_t *bytes)
276
{
277
if (bytes != NULL)
278
{
279
pcre2_memctl *memctl = (pcre2_memctl *)(bytes - sizeof(pcre2_memctl));
280
memctl->free(memctl, memctl->memory_data);
281
}
282
}
283
284
/* End of pcre2_serialize.c */
285
286