pcre2_serialize.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*************************************************
  2. * Perl-Compatible Regular Expressions *
  3. *************************************************/
  4. /* PCRE is a library of functions to support regular expressions whose syntax
  5. and semantics are as close as possible to those of the Perl 5 language.
  6. Written by Philip Hazel
  7. Original API code Copyright (c) 1997-2012 University of Cambridge
  8. New API code Copyright (c) 2016-2020 University of Cambridge
  9. -----------------------------------------------------------------------------
  10. Redistribution and use in source and binary forms, with or without
  11. modification, are permitted provided that the following conditions are met:
  12. * Redistributions of source code must retain the above copyright notice,
  13. this list of conditions and the following disclaimer.
  14. * Redistributions in binary form must reproduce the above copyright
  15. notice, this list of conditions and the following disclaimer in the
  16. documentation and/or other materials provided with the distribution.
  17. * Neither the name of the University of Cambridge nor the names of its
  18. contributors may be used to endorse or promote products derived from
  19. this software without specific prior written permission.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  24. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. POSSIBILITY OF SUCH DAMAGE.
  31. -----------------------------------------------------------------------------
  32. */
  33. /* This module contains functions for serializing and deserializing
  34. a sequence of compiled codes. */
  35. #ifdef HAVE_CONFIG_H
  36. #include "config.h"
  37. #endif
  38. #include "pcre2_internal.h"
  39. /* Magic number to provide a small check against being handed junk. */
  40. #define SERIALIZED_DATA_MAGIC 0x50523253u
  41. /* Deserialization is limited to the current PCRE version and
  42. character width. */
  43. #define SERIALIZED_DATA_VERSION \
  44. ((PCRE2_MAJOR) | ((PCRE2_MINOR) << 16))
  45. #define SERIALIZED_DATA_CONFIG \
  46. (sizeof(PCRE2_UCHAR) | ((sizeof(void*)) << 8) | ((sizeof(PCRE2_SIZE)) << 16))
  47. /*************************************************
  48. * Serialize compiled patterns *
  49. *************************************************/
  50. PCRE2_EXP_DEFN int32_t PCRE2_CALL_CONVENTION
  51. pcre2_serialize_encode(const pcre2_code **codes, int32_t number_of_codes,
  52. uint8_t **serialized_bytes, PCRE2_SIZE *serialized_size,
  53. pcre2_general_context *gcontext)
  54. {
  55. uint8_t *bytes;
  56. uint8_t *dst_bytes;
  57. int32_t i;
  58. PCRE2_SIZE total_size;
  59. const pcre2_real_code *re;
  60. const uint8_t *tables;
  61. pcre2_serialized_data *data;
  62. const pcre2_memctl *memctl = (gcontext != NULL) ?
  63. &gcontext->memctl : &PRIV(default_compile_context).memctl;
  64. if (codes == NULL || serialized_bytes == NULL || serialized_size == NULL)
  65. return PCRE2_ERROR_NULL;
  66. if (number_of_codes <= 0) return PCRE2_ERROR_BADDATA;
  67. /* Compute total size. */
  68. total_size = sizeof(pcre2_serialized_data) + TABLES_LENGTH;
  69. tables = NULL;
  70. for (i = 0; i < number_of_codes; i++)
  71. {
  72. if (codes[i] == NULL) return PCRE2_ERROR_NULL;
  73. re = (const pcre2_real_code *)(codes[i]);
  74. if (re->magic_number != MAGIC_NUMBER) return PCRE2_ERROR_BADMAGIC;
  75. if (tables == NULL)
  76. tables = re->tables;
  77. else if (tables != re->tables)
  78. return PCRE2_ERROR_MIXEDTABLES;
  79. total_size += re->blocksize;
  80. }
  81. /* Initialize the byte stream. */
  82. bytes = memctl->malloc(total_size + sizeof(pcre2_memctl), memctl->memory_data);
  83. if (bytes == NULL) return PCRE2_ERROR_NOMEMORY;
  84. /* The controller is stored as a hidden parameter. */
  85. memcpy(bytes, memctl, sizeof(pcre2_memctl));
  86. bytes += sizeof(pcre2_memctl);
  87. data = (pcre2_serialized_data *)bytes;
  88. data->magic = SERIALIZED_DATA_MAGIC;
  89. data->version = SERIALIZED_DATA_VERSION;
  90. data->config = SERIALIZED_DATA_CONFIG;
  91. data->number_of_codes = number_of_codes;
  92. /* Copy all compiled code data. */
  93. dst_bytes = bytes + sizeof(pcre2_serialized_data);
  94. memcpy(dst_bytes, tables, TABLES_LENGTH);
  95. dst_bytes += TABLES_LENGTH;
  96. for (i = 0; i < number_of_codes; i++)
  97. {
  98. re = (const pcre2_real_code *)(codes[i]);
  99. (void)memcpy(dst_bytes, (char *)re, re->blocksize);
  100. /* Certain fields in the compiled code block are re-set during
  101. deserialization. In order to ensure that the serialized data stream is always
  102. the same for the same pattern, set them to zero here. We can't assume the
  103. copy of the pattern is correctly aligned for accessing the fields as part of
  104. a structure. Note the use of sizeof(void *) in the second of these, to
  105. specify the size of a pointer. If sizeof(uint8_t *) is used (tables is a
  106. pointer to uint8_t), gcc gives a warning because the first argument is also a
  107. pointer to uint8_t. Casting the first argument to (void *) can stop this, but
  108. it didn't stop Coverity giving the same complaint. */
  109. (void)memset(dst_bytes + offsetof(pcre2_real_code, memctl), 0,
  110. sizeof(pcre2_memctl));
  111. (void)memset(dst_bytes + offsetof(pcre2_real_code, tables), 0,
  112. sizeof(void *));
  113. (void)memset(dst_bytes + offsetof(pcre2_real_code, executable_jit), 0,
  114. sizeof(void *));
  115. dst_bytes += re->blocksize;
  116. }
  117. *serialized_bytes = bytes;
  118. *serialized_size = total_size;
  119. return number_of_codes;
  120. }
  121. /*************************************************
  122. * Deserialize compiled patterns *
  123. *************************************************/
  124. PCRE2_EXP_DEFN int32_t PCRE2_CALL_CONVENTION
  125. pcre2_serialize_decode(pcre2_code **codes, int32_t number_of_codes,
  126. const uint8_t *bytes, pcre2_general_context *gcontext)
  127. {
  128. const pcre2_serialized_data *data = (const pcre2_serialized_data *)bytes;
  129. const pcre2_memctl *memctl = (gcontext != NULL) ?
  130. &gcontext->memctl : &PRIV(default_compile_context).memctl;
  131. const uint8_t *src_bytes;
  132. pcre2_real_code *dst_re;
  133. uint8_t *tables;
  134. int32_t i, j;
  135. /* Sanity checks. */
  136. if (data == NULL || codes == NULL) return PCRE2_ERROR_NULL;
  137. if (number_of_codes <= 0) return PCRE2_ERROR_BADDATA;
  138. if (data->number_of_codes <= 0) return PCRE2_ERROR_BADSERIALIZEDDATA;
  139. if (data->magic != SERIALIZED_DATA_MAGIC) return PCRE2_ERROR_BADMAGIC;
  140. if (data->version != SERIALIZED_DATA_VERSION) return PCRE2_ERROR_BADMODE;
  141. if (data->config != SERIALIZED_DATA_CONFIG) return PCRE2_ERROR_BADMODE;
  142. if (number_of_codes > data->number_of_codes)
  143. number_of_codes = data->number_of_codes;
  144. src_bytes = bytes + sizeof(pcre2_serialized_data);
  145. /* Decode tables. The reference count for the tables is stored immediately
  146. following them. */
  147. tables = memctl->malloc(TABLES_LENGTH + sizeof(PCRE2_SIZE), memctl->memory_data);
  148. if (tables == NULL) return PCRE2_ERROR_NOMEMORY;
  149. memcpy(tables, src_bytes, TABLES_LENGTH);
  150. *(PCRE2_SIZE *)(tables + TABLES_LENGTH) = number_of_codes;
  151. src_bytes += TABLES_LENGTH;
  152. /* Decode the byte stream. We must not try to read the size from the compiled
  153. code block in the stream, because it might be unaligned, which causes errors on
  154. hardware such as Sparc-64 that doesn't like unaligned memory accesses. The type
  155. of the blocksize field is given its own name to ensure that it is the same here
  156. as in the block. */
  157. for (i = 0; i < number_of_codes; i++)
  158. {
  159. CODE_BLOCKSIZE_TYPE blocksize;
  160. memcpy(&blocksize, src_bytes + offsetof(pcre2_real_code, blocksize),
  161. sizeof(CODE_BLOCKSIZE_TYPE));
  162. if (blocksize <= sizeof(pcre2_real_code))
  163. return PCRE2_ERROR_BADSERIALIZEDDATA;
  164. /* The allocator provided by gcontext replaces the original one. */
  165. dst_re = (pcre2_real_code *)PRIV(memctl_malloc)(blocksize,
  166. (pcre2_memctl *)gcontext);
  167. if (dst_re == NULL)
  168. {
  169. memctl->free(tables, memctl->memory_data);
  170. for (j = 0; j < i; j++)
  171. {
  172. memctl->free(codes[j], memctl->memory_data);
  173. codes[j] = NULL;
  174. }
  175. return PCRE2_ERROR_NOMEMORY;
  176. }
  177. /* The new allocator must be preserved. */
  178. memcpy(((uint8_t *)dst_re) + sizeof(pcre2_memctl),
  179. src_bytes + sizeof(pcre2_memctl), blocksize - sizeof(pcre2_memctl));
  180. if (dst_re->magic_number != MAGIC_NUMBER ||
  181. dst_re->name_entry_size > MAX_NAME_SIZE + IMM2_SIZE + 1 ||
  182. dst_re->name_count > MAX_NAME_COUNT)
  183. {
  184. memctl->free(dst_re, memctl->memory_data);
  185. return PCRE2_ERROR_BADSERIALIZEDDATA;
  186. }
  187. /* At the moment only one table is supported. */
  188. dst_re->tables = tables;
  189. dst_re->executable_jit = NULL;
  190. dst_re->flags |= PCRE2_DEREF_TABLES;
  191. codes[i] = dst_re;
  192. src_bytes += blocksize;
  193. }
  194. return number_of_codes;
  195. }
  196. /*************************************************
  197. * Get the number of serialized patterns *
  198. *************************************************/
  199. PCRE2_EXP_DEFN int32_t PCRE2_CALL_CONVENTION
  200. pcre2_serialize_get_number_of_codes(const uint8_t *bytes)
  201. {
  202. const pcre2_serialized_data *data = (const pcre2_serialized_data *)bytes;
  203. if (data == NULL) return PCRE2_ERROR_NULL;
  204. if (data->magic != SERIALIZED_DATA_MAGIC) return PCRE2_ERROR_BADMAGIC;
  205. if (data->version != SERIALIZED_DATA_VERSION) return PCRE2_ERROR_BADMODE;
  206. if (data->config != SERIALIZED_DATA_CONFIG) return PCRE2_ERROR_BADMODE;
  207. return data->number_of_codes;
  208. }
  209. /*************************************************
  210. * Free the allocated stream *
  211. *************************************************/
  212. PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
  213. pcre2_serialize_free(uint8_t *bytes)
  214. {
  215. if (bytes != NULL)
  216. {
  217. pcre2_memctl *memctl = (pcre2_memctl *)(bytes - sizeof(pcre2_memctl));
  218. memctl->free(memctl, memctl->memory_data);
  219. }
  220. }
  221. /* End of pcre2_serialize.c */