github的一些开源项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

198 lines
8.7 KiB

  1. .TH PCRE2SERIALIZE 3 "27 June 2018" "PCRE2 10.32"
  2. .SH NAME
  3. PCRE2 - Perl-compatible regular expressions (revised API)
  4. .SH "SAVING AND RE-USING PRECOMPILED PCRE2 PATTERNS"
  5. .rs
  6. .sp
  7. .nf
  8. .B int32_t pcre2_serialize_decode(pcre2_code **\fIcodes\fP,
  9. .B " int32_t \fInumber_of_codes\fP, const uint8_t *\fIbytes\fP,"
  10. .B " pcre2_general_context *\fIgcontext\fP);"
  11. .sp
  12. .B int32_t pcre2_serialize_encode(const pcre2_code **\fIcodes\fP,
  13. .B " int32_t \fInumber_of_codes\fP, uint8_t **\fIserialized_bytes\fP,"
  14. .B " PCRE2_SIZE *\fIserialized_size\fP, pcre2_general_context *\fIgcontext\fP);"
  15. .sp
  16. .B void pcre2_serialize_free(uint8_t *\fIbytes\fP);
  17. .sp
  18. .B int32_t pcre2_serialize_get_number_of_codes(const uint8_t *\fIbytes\fP);
  19. .fi
  20. .sp
  21. If you are running an application that uses a large number of regular
  22. expression patterns, it may be useful to store them in a precompiled form
  23. instead of having to compile them every time the application is run. However,
  24. if you are using the just-in-time optimization feature, it is not possible to
  25. save and reload the JIT data, because it is position-dependent. The host on
  26. which the patterns are reloaded must be running the same version of PCRE2, with
  27. the same code unit width, and must also have the same endianness, pointer width
  28. and PCRE2_SIZE type. For example, patterns compiled on a 32-bit system using
  29. PCRE2's 16-bit library cannot be reloaded on a 64-bit system, nor can they be
  30. reloaded using the 8-bit library.
  31. .P
  32. Note that "serialization" in PCRE2 does not convert compiled patterns to an
  33. abstract format like Java or .NET serialization. The serialized output is
  34. really just a bytecode dump, which is why it can only be reloaded in the same
  35. environment as the one that created it. Hence the restrictions mentioned above.
  36. Applications that are not statically linked with a fixed version of PCRE2 must
  37. be prepared to recompile patterns from their sources, in order to be immune to
  38. PCRE2 upgrades.
  39. .
  40. .
  41. .SH "SECURITY CONCERNS"
  42. .rs
  43. .sp
  44. The facility for saving and restoring compiled patterns is intended for use
  45. within individual applications. As such, the data supplied to
  46. \fBpcre2_serialize_decode()\fP is expected to be trusted data, not data from
  47. arbitrary external sources. There is only some simple consistency checking, not
  48. complete validation of what is being re-loaded. Corrupted data may cause
  49. undefined results. For example, if the length field of a pattern in the
  50. serialized data is corrupted, the deserializing code may read beyond the end of
  51. the byte stream that is passed to it.
  52. .
  53. .
  54. .SH "SAVING COMPILED PATTERNS"
  55. .rs
  56. .sp
  57. Before compiled patterns can be saved they must be serialized, which in PCRE2
  58. means converting the pattern to a stream of bytes. A single byte stream may
  59. contain any number of compiled patterns, but they must all use the same
  60. character tables. A single copy of the tables is included in the byte stream
  61. (its size is 1088 bytes). For more details of character tables, see the
  62. .\" HTML <a href="pcre2api.html#localesupport">
  63. .\" </a>
  64. section on locale support
  65. .\"
  66. in the
  67. .\" HREF
  68. \fBpcre2api\fP
  69. .\"
  70. documentation.
  71. .P
  72. The function \fBpcre2_serialize_encode()\fP creates a serialized byte stream
  73. from a list of compiled patterns. Its first two arguments specify the list,
  74. being a pointer to a vector of pointers to compiled patterns, and the length of
  75. the vector. The third and fourth arguments point to variables which are set to
  76. point to the created byte stream and its length, respectively. The final
  77. argument is a pointer to a general context, which can be used to specify custom
  78. memory management functions. If this argument is NULL, \fBmalloc()\fP is used
  79. to obtain memory for the byte stream. The yield of the function is the number
  80. of serialized patterns, or one of the following negative error codes:
  81. .sp
  82. PCRE2_ERROR_BADDATA the number of patterns is zero or less
  83. PCRE2_ERROR_BADMAGIC mismatch of id bytes in one of the patterns
  84. PCRE2_ERROR_NOMEMORY memory allocation failed
  85. PCRE2_ERROR_MIXEDTABLES the patterns do not all use the same tables
  86. PCRE2_ERROR_NULL the 1st, 3rd, or 4th argument is NULL
  87. .sp
  88. PCRE2_ERROR_BADMAGIC means either that a pattern's code has been corrupted, or
  89. that a slot in the vector does not point to a compiled pattern.
  90. .P
  91. Once a set of patterns has been serialized you can save the data in any
  92. appropriate manner. Here is sample code that compiles two patterns and writes
  93. them to a file. It assumes that the variable \fIfd\fP refers to a file that is
  94. open for output. The error checking that should be present in a real
  95. application has been omitted for simplicity.
  96. .sp
  97. int errorcode;
  98. uint8_t *bytes;
  99. PCRE2_SIZE erroroffset;
  100. PCRE2_SIZE bytescount;
  101. pcre2_code *list_of_codes[2];
  102. list_of_codes[0] = pcre2_compile("first pattern",
  103. PCRE2_ZERO_TERMINATED, 0, &errorcode, &erroroffset, NULL);
  104. list_of_codes[1] = pcre2_compile("second pattern",
  105. PCRE2_ZERO_TERMINATED, 0, &errorcode, &erroroffset, NULL);
  106. errorcode = pcre2_serialize_encode(list_of_codes, 2, &bytes,
  107. &bytescount, NULL);
  108. errorcode = fwrite(bytes, 1, bytescount, fd);
  109. .sp
  110. Note that the serialized data is binary data that may contain any of the 256
  111. possible byte values. On systems that make a distinction between binary and
  112. non-binary data, be sure that the file is opened for binary output.
  113. .P
  114. Serializing a set of patterns leaves the original data untouched, so they can
  115. still be used for matching. Their memory must eventually be freed in the usual
  116. way by calling \fBpcre2_code_free()\fP. When you have finished with the byte
  117. stream, it too must be freed by calling \fBpcre2_serialize_free()\fP. If this
  118. function is called with a NULL argument, it returns immediately without doing
  119. anything.
  120. .
  121. .
  122. .SH "RE-USING PRECOMPILED PATTERNS"
  123. .rs
  124. .sp
  125. In order to re-use a set of saved patterns you must first make the serialized
  126. byte stream available in main memory (for example, by reading from a file). The
  127. management of this memory block is up to the application. You can use the
  128. \fBpcre2_serialize_get_number_of_codes()\fP function to find out how many
  129. compiled patterns are in the serialized data without actually decoding the
  130. patterns:
  131. .sp
  132. uint8_t *bytes = <serialized data>;
  133. int32_t number_of_codes = pcre2_serialize_get_number_of_codes(bytes);
  134. .sp
  135. The \fBpcre2_serialize_decode()\fP function reads a byte stream and recreates
  136. the compiled patterns in new memory blocks, setting pointers to them in a
  137. vector. The first two arguments are a pointer to a suitable vector and its
  138. length, and the third argument points to a byte stream. The final argument is a
  139. pointer to a general context, which can be used to specify custom memory
  140. management functions for the decoded patterns. If this argument is NULL,
  141. \fBmalloc()\fP and \fBfree()\fP are used. After deserialization, the byte
  142. stream is no longer needed and can be discarded.
  143. .sp
  144. pcre2_code *list_of_codes[2];
  145. uint8_t *bytes = <serialized data>;
  146. int32_t number_of_codes =
  147. pcre2_serialize_decode(list_of_codes, 2, bytes, NULL);
  148. .sp
  149. If the vector is not large enough for all the patterns in the byte stream, it
  150. is filled with those that fit, and the remainder are ignored. The yield of the
  151. function is the number of decoded patterns, or one of the following negative
  152. error codes:
  153. .sp
  154. PCRE2_ERROR_BADDATA second argument is zero or less
  155. PCRE2_ERROR_BADMAGIC mismatch of id bytes in the data
  156. PCRE2_ERROR_BADMODE mismatch of code unit size or PCRE2 version
  157. PCRE2_ERROR_BADSERIALIZEDDATA other sanity check failure
  158. PCRE2_ERROR_MEMORY memory allocation failed
  159. PCRE2_ERROR_NULL first or third argument is NULL
  160. .sp
  161. PCRE2_ERROR_BADMAGIC may mean that the data is corrupt, or that it was compiled
  162. on a system with different endianness.
  163. .P
  164. Decoded patterns can be used for matching in the usual way, and must be freed
  165. by calling \fBpcre2_code_free()\fP. However, be aware that there is a potential
  166. race issue if you are using multiple patterns that were decoded from a single
  167. byte stream in a multithreaded application. A single copy of the character
  168. tables is used by all the decoded patterns and a reference count is used to
  169. arrange for its memory to be automatically freed when the last pattern is
  170. freed, but there is no locking on this reference count. Therefore, if you want
  171. to call \fBpcre2_code_free()\fP for these patterns in different threads, you
  172. must arrange your own locking, and ensure that \fBpcre2_code_free()\fP cannot
  173. be called by two threads at the same time.
  174. .P
  175. If a pattern was processed by \fBpcre2_jit_compile()\fP before being
  176. serialized, the JIT data is discarded and so is no longer available after a
  177. save/restore cycle. You can, however, process a restored pattern with
  178. \fBpcre2_jit_compile()\fP if you wish.
  179. .
  180. .
  181. .
  182. .SH AUTHOR
  183. .rs
  184. .sp
  185. .nf
  186. Philip Hazel
  187. Retired from University Computing Service
  188. Cambridge, England.
  189. .fi
  190. .
  191. .
  192. .SH REVISION
  193. .rs
  194. .sp
  195. .nf
  196. Last updated: 27 June 2018
  197. Copyright (c) 1997-2018 University of Cambridge.
  198. .fi