new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / src / external / pcre2-10.32 / doc / pcre2serialize.3
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 uint32_t *\fIbytes\fP,"
10 .B "  pcre2_general_context *\fIgcontext\fP);"
11 .sp
12 .B int32_t pcre2_serialize_encode(pcre2_code **\fIcodes\fP,
13 .B "  int32_t \fInumber_of_codes\fP, uint32_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 mangagement 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_MEMORY       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 mangagement 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   int32_t number_of_codes;
145   pcre2_code *list_of_codes[2];
146   uint8_t *bytes = <serialized data>;
147   int32_t number_of_codes =
148     pcre2_serialize_decode(list_of_codes, 2, bytes, NULL);
149 .sp
150 If the vector is not large enough for all the patterns in the byte stream, it
151 is filled with those that fit, and the remainder are ignored. The yield of the
152 function is the number of decoded patterns, or one of the following negative
153 error codes:
154 .sp
155   PCRE2_ERROR_BADDATA    second argument is zero or less
156   PCRE2_ERROR_BADMAGIC   mismatch of id bytes in the data
157   PCRE2_ERROR_BADMODE    mismatch of code unit size or PCRE2 version
158   PCRE2_ERROR_BADSERIALIZEDDATA  other sanity check failure
159   PCRE2_ERROR_MEMORY     memory allocation failed
160   PCRE2_ERROR_NULL       first or third argument is NULL
161 .sp
162 PCRE2_ERROR_BADMAGIC may mean that the data is corrupt, or that it was compiled
163 on a system with different endianness.
164 .P
165 Decoded patterns can be used for matching in the usual way, and must be freed
166 by calling \fBpcre2_code_free()\fP. However, be aware that there is a potential
167 race issue if you are using multiple patterns that were decoded from a single
168 byte stream in a multithreaded application. A single copy of the character
169 tables is used by all the decoded patterns and a reference count is used to
170 arrange for its memory to be automatically freed when the last pattern is
171 freed, but there is no locking on this reference count. Therefore, if you want
172 to call \fBpcre2_code_free()\fP for these patterns in different threads, you
173 must arrange your own locking, and ensure that \fBpcre2_code_free()\fP cannot
174 be called by two threads at the same time.
175 .P
176 If a pattern was processed by \fBpcre2_jit_compile()\fP before being
177 serialized, the JIT data is discarded and so is no longer available after a
178 save/restore cycle. You can, however, process a restored pattern with
179 \fBpcre2_jit_compile()\fP if you wish.
180 .
181 .
182 .
183 .SH AUTHOR
184 .rs
185 .sp
186 .nf
187 Philip Hazel
188 University Computing Service
189 Cambridge, England.
190 .fi
191 .
192 .
193 .SH REVISION
194 .rs
195 .sp
196 .nf
197 Last updated: 27 June 2018
198 Copyright (c) 1997-2018 University of Cambridge.
199 .fi