pcre2_serialize.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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-2017 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. memcpy(dst_bytes, (char *)re, re->blocksize);
  100. dst_bytes += re->blocksize;
  101. }
  102. *serialized_bytes = bytes;
  103. *serialized_size = total_size;
  104. return number_of_codes;
  105. }
  106. /*************************************************
  107. * Deserialize compiled patterns *
  108. *************************************************/
  109. PCRE2_EXP_DEFN int32_t PCRE2_CALL_CONVENTION
  110. pcre2_serialize_decode(pcre2_code **codes, int32_t number_of_codes,
  111. const uint8_t *bytes, pcre2_general_context *gcontext)
  112. {
  113. const pcre2_serialized_data *data = (const pcre2_serialized_data *)bytes;
  114. const pcre2_memctl *memctl = (gcontext != NULL) ?
  115. &gcontext->memctl : &PRIV(default_compile_context).memctl;
  116. const uint8_t *src_bytes;
  117. pcre2_real_code *dst_re;
  118. uint8_t *tables;
  119. int32_t i, j;
  120. /* Sanity checks. */
  121. if (data == NULL || codes == NULL) return PCRE2_ERROR_NULL;
  122. if (number_of_codes <= 0) return PCRE2_ERROR_BADDATA;
  123. if (data->number_of_codes <= 0) return PCRE2_ERROR_BADSERIALIZEDDATA;
  124. if (data->magic != SERIALIZED_DATA_MAGIC) return PCRE2_ERROR_BADMAGIC;
  125. if (data->version != SERIALIZED_DATA_VERSION) return PCRE2_ERROR_BADMODE;
  126. if (data->config != SERIALIZED_DATA_CONFIG) return PCRE2_ERROR_BADMODE;
  127. if (number_of_codes > data->number_of_codes)
  128. number_of_codes = data->number_of_codes;
  129. src_bytes = bytes + sizeof(pcre2_serialized_data);
  130. /* Decode tables. The reference count for the tables is stored immediately
  131. following them. */
  132. tables = memctl->malloc(tables_length + sizeof(PCRE2_SIZE), memctl->memory_data);
  133. if (tables == NULL) return PCRE2_ERROR_NOMEMORY;
  134. memcpy(tables, src_bytes, tables_length);
  135. *(PCRE2_SIZE *)(tables + tables_length) = number_of_codes;
  136. src_bytes += tables_length;
  137. /* Decode the byte stream. We must not try to read the size from the compiled
  138. code block in the stream, because it might be unaligned, which causes errors on
  139. hardware such as Sparc-64 that doesn't like unaligned memory accesses. The type
  140. of the blocksize field is given its own name to ensure that it is the same here
  141. as in the block. */
  142. for (i = 0; i < number_of_codes; i++)
  143. {
  144. CODE_BLOCKSIZE_TYPE blocksize;
  145. memcpy(&blocksize, src_bytes + offsetof(pcre2_real_code, blocksize),
  146. sizeof(CODE_BLOCKSIZE_TYPE));
  147. if (blocksize <= sizeof(pcre2_real_code))
  148. return PCRE2_ERROR_BADSERIALIZEDDATA;
  149. /* The allocator provided by gcontext replaces the original one. */
  150. dst_re = (pcre2_real_code *)PRIV(memctl_malloc)(blocksize,
  151. (pcre2_memctl *)gcontext);
  152. if (dst_re == NULL)
  153. {
  154. memctl->free(tables, memctl->memory_data);
  155. for (j = 0; j < i; j++)
  156. {
  157. memctl->free(codes[j], memctl->memory_data);
  158. codes[j] = NULL;
  159. }
  160. return PCRE2_ERROR_NOMEMORY;
  161. }
  162. /* The new allocator must be preserved. */
  163. memcpy(((uint8_t *)dst_re) + sizeof(pcre2_memctl),
  164. src_bytes + sizeof(pcre2_memctl), blocksize - sizeof(pcre2_memctl));
  165. if (dst_re->magic_number != MAGIC_NUMBER ||
  166. dst_re->name_entry_size > MAX_NAME_SIZE + IMM2_SIZE + 1 ||
  167. dst_re->name_count > MAX_NAME_COUNT)
  168. {
  169. memctl->free(dst_re, memctl->memory_data);
  170. return PCRE2_ERROR_BADSERIALIZEDDATA;
  171. }
  172. /* At the moment only one table is supported. */
  173. dst_re->tables = tables;
  174. dst_re->executable_jit = NULL;
  175. dst_re->flags |= PCRE2_DEREF_TABLES;
  176. codes[i] = dst_re;
  177. src_bytes += blocksize;
  178. }
  179. return number_of_codes;
  180. }
  181. /*************************************************
  182. * Get the number of serialized patterns *
  183. *************************************************/
  184. PCRE2_EXP_DEFN int32_t PCRE2_CALL_CONVENTION
  185. pcre2_serialize_get_number_of_codes(const uint8_t *bytes)
  186. {
  187. const pcre2_serialized_data *data = (const pcre2_serialized_data *)bytes;
  188. if (data == NULL) return PCRE2_ERROR_NULL;
  189. if (data->magic != SERIALIZED_DATA_MAGIC) return PCRE2_ERROR_BADMAGIC;
  190. if (data->version != SERIALIZED_DATA_VERSION) return PCRE2_ERROR_BADMODE;
  191. if (data->config != SERIALIZED_DATA_CONFIG) return PCRE2_ERROR_BADMODE;
  192. return data->number_of_codes;
  193. }
  194. /*************************************************
  195. * Free the allocated stream *
  196. *************************************************/
  197. PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
  198. pcre2_serialize_free(uint8_t *bytes)
  199. {
  200. if (bytes != NULL)
  201. {
  202. pcre2_memctl *memctl = (pcre2_memctl *)(bytes - sizeof(pcre2_memctl));
  203. memctl->free(memctl, memctl->memory_data);
  204. }
  205. }
  206. /* End of pcre2_serialize.c */