/src/php-src/ext/pcre/pcre2lib/pcre2_pattern_info.c
Line | Count | Source (jump to first uncovered line) |
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 | | #ifdef HAVE_CONFIG_H |
43 | | #include "config.h" |
44 | | #endif |
45 | | |
46 | | #include "pcre2_internal.h" |
47 | | |
48 | | |
49 | | /************************************************* |
50 | | * Return info about compiled pattern * |
51 | | *************************************************/ |
52 | | |
53 | | /* |
54 | | Arguments: |
55 | | code points to compiled code |
56 | | what what information is required |
57 | | where where to put the information; if NULL, return length |
58 | | |
59 | | Returns: 0 when data returned |
60 | | > 0 when length requested |
61 | | < 0 on error or unset value |
62 | | */ |
63 | | |
64 | | PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION |
65 | | pcre2_pattern_info(const pcre2_code *code, uint32_t what, void *where) |
66 | 2.33k | { |
67 | 2.33k | const pcre2_real_code *re = (const pcre2_real_code *)code; |
68 | | |
69 | 2.33k | if (where == NULL) /* Requests field length */ |
70 | 0 | { |
71 | 0 | switch(what) |
72 | 0 | { |
73 | 0 | case PCRE2_INFO_ALLOPTIONS: |
74 | 0 | case PCRE2_INFO_ARGOPTIONS: |
75 | 0 | case PCRE2_INFO_BACKREFMAX: |
76 | 0 | case PCRE2_INFO_BSR: |
77 | 0 | case PCRE2_INFO_CAPTURECOUNT: |
78 | 0 | case PCRE2_INFO_DEPTHLIMIT: |
79 | 0 | case PCRE2_INFO_EXTRAOPTIONS: |
80 | 0 | case PCRE2_INFO_FIRSTCODETYPE: |
81 | 0 | case PCRE2_INFO_FIRSTCODEUNIT: |
82 | 0 | case PCRE2_INFO_HASBACKSLASHC: |
83 | 0 | case PCRE2_INFO_HASCRORLF: |
84 | 0 | case PCRE2_INFO_HEAPLIMIT: |
85 | 0 | case PCRE2_INFO_JCHANGED: |
86 | 0 | case PCRE2_INFO_LASTCODETYPE: |
87 | 0 | case PCRE2_INFO_LASTCODEUNIT: |
88 | 0 | case PCRE2_INFO_MATCHEMPTY: |
89 | 0 | case PCRE2_INFO_MATCHLIMIT: |
90 | 0 | case PCRE2_INFO_MAXLOOKBEHIND: |
91 | 0 | case PCRE2_INFO_MINLENGTH: |
92 | 0 | case PCRE2_INFO_NAMEENTRYSIZE: |
93 | 0 | case PCRE2_INFO_NAMECOUNT: |
94 | 0 | case PCRE2_INFO_NEWLINE: |
95 | 0 | return sizeof(uint32_t); |
96 | | |
97 | 0 | case PCRE2_INFO_FIRSTBITMAP: |
98 | 0 | return sizeof(const uint8_t *); |
99 | | |
100 | 0 | case PCRE2_INFO_JITSIZE: |
101 | 0 | case PCRE2_INFO_SIZE: |
102 | 0 | case PCRE2_INFO_FRAMESIZE: |
103 | 0 | return sizeof(size_t); |
104 | | |
105 | 0 | case PCRE2_INFO_NAMETABLE: |
106 | 0 | return sizeof(PCRE2_SPTR); |
107 | 0 | } |
108 | 0 | } |
109 | | |
110 | 2.33k | if (re == NULL) return PCRE2_ERROR_NULL; |
111 | | |
112 | | /* Check that the first field in the block is the magic number. If it is not, |
113 | | return with PCRE2_ERROR_BADMAGIC. */ |
114 | | |
115 | 2.33k | if (re->magic_number != MAGIC_NUMBER) return PCRE2_ERROR_BADMAGIC; |
116 | | |
117 | | /* Check that this pattern was compiled in the correct bit mode */ |
118 | | |
119 | 2.33k | if ((re->flags & (PCRE2_CODE_UNIT_WIDTH/8)) == 0) return PCRE2_ERROR_BADMODE; |
120 | | |
121 | 2.33k | switch(what) |
122 | 2.33k | { |
123 | 0 | case PCRE2_INFO_ALLOPTIONS: |
124 | 0 | *((uint32_t *)where) = re->overall_options; |
125 | 0 | break; |
126 | | |
127 | 0 | case PCRE2_INFO_ARGOPTIONS: |
128 | 0 | *((uint32_t *)where) = re->compile_options; |
129 | 0 | break; |
130 | | |
131 | 0 | case PCRE2_INFO_BACKREFMAX: |
132 | 0 | *((uint32_t *)where) = re->top_backref; |
133 | 0 | break; |
134 | | |
135 | 0 | case PCRE2_INFO_BSR: |
136 | 0 | *((uint32_t *)where) = re->bsr_convention; |
137 | 0 | break; |
138 | | |
139 | 1.16k | case PCRE2_INFO_CAPTURECOUNT: |
140 | 1.16k | *((uint32_t *)where) = re->top_bracket; |
141 | 1.16k | break; |
142 | | |
143 | 0 | case PCRE2_INFO_DEPTHLIMIT: |
144 | 0 | *((uint32_t *)where) = re->limit_depth; |
145 | 0 | if (re->limit_depth == UINT32_MAX) return PCRE2_ERROR_UNSET; |
146 | 0 | break; |
147 | | |
148 | 0 | case PCRE2_INFO_EXTRAOPTIONS: |
149 | 0 | *((uint32_t *)where) = re->extra_options; |
150 | 0 | break; |
151 | | |
152 | 0 | case PCRE2_INFO_FIRSTCODETYPE: |
153 | 0 | *((uint32_t *)where) = ((re->flags & PCRE2_FIRSTSET) != 0)? 1 : |
154 | 0 | ((re->flags & PCRE2_STARTLINE) != 0)? 2 : 0; |
155 | 0 | break; |
156 | | |
157 | 0 | case PCRE2_INFO_FIRSTCODEUNIT: |
158 | 0 | *((uint32_t *)where) = ((re->flags & PCRE2_FIRSTSET) != 0)? |
159 | 0 | re->first_codeunit : 0; |
160 | 0 | break; |
161 | | |
162 | 0 | case PCRE2_INFO_FIRSTBITMAP: |
163 | 0 | *((const uint8_t **)where) = ((re->flags & PCRE2_FIRSTMAPSET) != 0)? |
164 | 0 | &(re->start_bitmap[0]) : NULL; |
165 | 0 | break; |
166 | | |
167 | 0 | case PCRE2_INFO_FRAMESIZE: |
168 | 0 | *((size_t *)where) = offsetof(heapframe, ovector) + |
169 | 0 | re->top_bracket * 2 * sizeof(PCRE2_SIZE); |
170 | 0 | break; |
171 | | |
172 | 0 | case PCRE2_INFO_HASBACKSLASHC: |
173 | 0 | *((uint32_t *)where) = (re->flags & PCRE2_HASBKC) != 0; |
174 | 0 | break; |
175 | | |
176 | 0 | case PCRE2_INFO_HASCRORLF: |
177 | 0 | *((uint32_t *)where) = (re->flags & PCRE2_HASCRORLF) != 0; |
178 | 0 | break; |
179 | | |
180 | 0 | case PCRE2_INFO_HEAPLIMIT: |
181 | 0 | *((uint32_t *)where) = re->limit_heap; |
182 | 0 | if (re->limit_heap == UINT32_MAX) return PCRE2_ERROR_UNSET; |
183 | 0 | break; |
184 | | |
185 | 0 | case PCRE2_INFO_JCHANGED: |
186 | 0 | *((uint32_t *)where) = (re->flags & PCRE2_JCHANGED) != 0; |
187 | 0 | break; |
188 | | |
189 | 0 | case PCRE2_INFO_JITSIZE: |
190 | | #ifdef SUPPORT_JIT |
191 | | *((size_t *)where) = (re->executable_jit != NULL)? |
192 | | PRIV(jit_get_size)(re->executable_jit) : 0; |
193 | | #else |
194 | 0 | *((size_t *)where) = 0; |
195 | 0 | #endif |
196 | 0 | break; |
197 | | |
198 | 0 | case PCRE2_INFO_LASTCODETYPE: |
199 | 0 | *((uint32_t *)where) = ((re->flags & PCRE2_LASTSET) != 0)? 1 : 0; |
200 | 0 | break; |
201 | | |
202 | 0 | case PCRE2_INFO_LASTCODEUNIT: |
203 | 0 | *((uint32_t *)where) = ((re->flags & PCRE2_LASTSET) != 0)? |
204 | 0 | re->last_codeunit : 0; |
205 | 0 | break; |
206 | | |
207 | 0 | case PCRE2_INFO_MATCHEMPTY: |
208 | 0 | *((uint32_t *)where) = (re->flags & PCRE2_MATCH_EMPTY) != 0; |
209 | 0 | break; |
210 | | |
211 | 0 | case PCRE2_INFO_MATCHLIMIT: |
212 | 0 | *((uint32_t *)where) = re->limit_match; |
213 | 0 | if (re->limit_match == UINT32_MAX) return PCRE2_ERROR_UNSET; |
214 | 0 | break; |
215 | | |
216 | 0 | case PCRE2_INFO_MAXLOOKBEHIND: |
217 | 0 | *((uint32_t *)where) = re->max_lookbehind; |
218 | 0 | break; |
219 | | |
220 | 0 | case PCRE2_INFO_MINLENGTH: |
221 | 0 | *((uint32_t *)where) = re->minlength; |
222 | 0 | break; |
223 | | |
224 | 0 | case PCRE2_INFO_NAMEENTRYSIZE: |
225 | 0 | *((uint32_t *)where) = re->name_entry_size; |
226 | 0 | break; |
227 | | |
228 | 1.16k | case PCRE2_INFO_NAMECOUNT: |
229 | 1.16k | *((uint32_t *)where) = re->name_count; |
230 | 1.16k | break; |
231 | | |
232 | 0 | case PCRE2_INFO_NAMETABLE: |
233 | 0 | *((PCRE2_SPTR *)where) = (PCRE2_SPTR)((const char *)re + |
234 | 0 | sizeof(pcre2_real_code)); |
235 | 0 | break; |
236 | | |
237 | 0 | case PCRE2_INFO_NEWLINE: |
238 | 0 | *((uint32_t *)where) = re->newline_convention; |
239 | 0 | break; |
240 | | |
241 | 0 | case PCRE2_INFO_SIZE: |
242 | 0 | *((size_t *)where) = re->blocksize; |
243 | 0 | break; |
244 | | |
245 | 0 | default: return PCRE2_ERROR_BADOPTION; |
246 | 2.33k | } |
247 | | |
248 | 2.33k | return 0; |
249 | 2.33k | } |
250 | | |
251 | | |
252 | | |
253 | | /************************************************* |
254 | | * Callout enumerator * |
255 | | *************************************************/ |
256 | | |
257 | | /* |
258 | | Arguments: |
259 | | code points to compiled code |
260 | | callback function called for each callout block |
261 | | callout_data user data passed to the callback |
262 | | |
263 | | Returns: 0 when successfully completed |
264 | | < 0 on local error |
265 | | != 0 for callback error |
266 | | */ |
267 | | |
268 | | PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION |
269 | | pcre2_callout_enumerate(const pcre2_code *code, |
270 | | int (*callback)(pcre2_callout_enumerate_block *, void *), void *callout_data) |
271 | 0 | { |
272 | 0 | const pcre2_real_code *re = (const pcre2_real_code *)code; |
273 | 0 | pcre2_callout_enumerate_block cb; |
274 | 0 | PCRE2_SPTR cc; |
275 | 0 | #ifdef SUPPORT_UNICODE |
276 | 0 | BOOL utf; |
277 | 0 | #endif |
278 | |
|
279 | 0 | if (re == NULL) return PCRE2_ERROR_NULL; |
280 | | |
281 | 0 | #ifdef SUPPORT_UNICODE |
282 | 0 | utf = (re->overall_options & PCRE2_UTF) != 0; |
283 | 0 | #endif |
284 | | |
285 | | /* Check that the first field in the block is the magic number. If it is not, |
286 | | return with PCRE2_ERROR_BADMAGIC. */ |
287 | |
|
288 | 0 | if (re->magic_number != MAGIC_NUMBER) return PCRE2_ERROR_BADMAGIC; |
289 | | |
290 | | /* Check that this pattern was compiled in the correct bit mode */ |
291 | | |
292 | 0 | if ((re->flags & (PCRE2_CODE_UNIT_WIDTH/8)) == 0) return PCRE2_ERROR_BADMODE; |
293 | | |
294 | 0 | cb.version = 0; |
295 | 0 | cc = (PCRE2_SPTR)((const uint8_t *)re + sizeof(pcre2_real_code)) |
296 | 0 | + re->name_count * re->name_entry_size; |
297 | |
|
298 | 0 | while (TRUE) |
299 | 0 | { |
300 | 0 | int rc; |
301 | 0 | switch (*cc) |
302 | 0 | { |
303 | 0 | case OP_END: |
304 | 0 | return 0; |
305 | | |
306 | 0 | case OP_CHAR: |
307 | 0 | case OP_CHARI: |
308 | 0 | case OP_NOT: |
309 | 0 | case OP_NOTI: |
310 | 0 | case OP_STAR: |
311 | 0 | case OP_MINSTAR: |
312 | 0 | case OP_PLUS: |
313 | 0 | case OP_MINPLUS: |
314 | 0 | case OP_QUERY: |
315 | 0 | case OP_MINQUERY: |
316 | 0 | case OP_UPTO: |
317 | 0 | case OP_MINUPTO: |
318 | 0 | case OP_EXACT: |
319 | 0 | case OP_POSSTAR: |
320 | 0 | case OP_POSPLUS: |
321 | 0 | case OP_POSQUERY: |
322 | 0 | case OP_POSUPTO: |
323 | 0 | case OP_STARI: |
324 | 0 | case OP_MINSTARI: |
325 | 0 | case OP_PLUSI: |
326 | 0 | case OP_MINPLUSI: |
327 | 0 | case OP_QUERYI: |
328 | 0 | case OP_MINQUERYI: |
329 | 0 | case OP_UPTOI: |
330 | 0 | case OP_MINUPTOI: |
331 | 0 | case OP_EXACTI: |
332 | 0 | case OP_POSSTARI: |
333 | 0 | case OP_POSPLUSI: |
334 | 0 | case OP_POSQUERYI: |
335 | 0 | case OP_POSUPTOI: |
336 | 0 | case OP_NOTSTAR: |
337 | 0 | case OP_NOTMINSTAR: |
338 | 0 | case OP_NOTPLUS: |
339 | 0 | case OP_NOTMINPLUS: |
340 | 0 | case OP_NOTQUERY: |
341 | 0 | case OP_NOTMINQUERY: |
342 | 0 | case OP_NOTUPTO: |
343 | 0 | case OP_NOTMINUPTO: |
344 | 0 | case OP_NOTEXACT: |
345 | 0 | case OP_NOTPOSSTAR: |
346 | 0 | case OP_NOTPOSPLUS: |
347 | 0 | case OP_NOTPOSQUERY: |
348 | 0 | case OP_NOTPOSUPTO: |
349 | 0 | case OP_NOTSTARI: |
350 | 0 | case OP_NOTMINSTARI: |
351 | 0 | case OP_NOTPLUSI: |
352 | 0 | case OP_NOTMINPLUSI: |
353 | 0 | case OP_NOTQUERYI: |
354 | 0 | case OP_NOTMINQUERYI: |
355 | 0 | case OP_NOTUPTOI: |
356 | 0 | case OP_NOTMINUPTOI: |
357 | 0 | case OP_NOTEXACTI: |
358 | 0 | case OP_NOTPOSSTARI: |
359 | 0 | case OP_NOTPOSPLUSI: |
360 | 0 | case OP_NOTPOSQUERYI: |
361 | 0 | case OP_NOTPOSUPTOI: |
362 | 0 | cc += PRIV(OP_lengths)[*cc]; |
363 | 0 | #ifdef SUPPORT_UNICODE |
364 | 0 | if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]); |
365 | 0 | #endif |
366 | 0 | break; |
367 | | |
368 | 0 | case OP_TYPESTAR: |
369 | 0 | case OP_TYPEMINSTAR: |
370 | 0 | case OP_TYPEPLUS: |
371 | 0 | case OP_TYPEMINPLUS: |
372 | 0 | case OP_TYPEQUERY: |
373 | 0 | case OP_TYPEMINQUERY: |
374 | 0 | case OP_TYPEUPTO: |
375 | 0 | case OP_TYPEMINUPTO: |
376 | 0 | case OP_TYPEEXACT: |
377 | 0 | case OP_TYPEPOSSTAR: |
378 | 0 | case OP_TYPEPOSPLUS: |
379 | 0 | case OP_TYPEPOSQUERY: |
380 | 0 | case OP_TYPEPOSUPTO: |
381 | 0 | cc += PRIV(OP_lengths)[*cc]; |
382 | 0 | #ifdef SUPPORT_UNICODE |
383 | 0 | if (cc[-1] == OP_PROP || cc[-1] == OP_NOTPROP) cc += 2; |
384 | 0 | #endif |
385 | 0 | break; |
386 | | |
387 | 0 | #ifdef SUPPORT_WIDE_CHARS |
388 | 0 | case OP_XCLASS: |
389 | 0 | case OP_ECLASS: |
390 | 0 | cc += GET(cc, 1); |
391 | 0 | break; |
392 | 0 | #endif |
393 | | |
394 | 0 | case OP_MARK: |
395 | 0 | case OP_COMMIT_ARG: |
396 | 0 | case OP_PRUNE_ARG: |
397 | 0 | case OP_SKIP_ARG: |
398 | 0 | case OP_THEN_ARG: |
399 | 0 | cc += PRIV(OP_lengths)[*cc] + cc[1]; |
400 | 0 | break; |
401 | | |
402 | 0 | case OP_CALLOUT: |
403 | 0 | cb.pattern_position = GET(cc, 1); |
404 | 0 | cb.next_item_length = GET(cc, 1 + LINK_SIZE); |
405 | 0 | cb.callout_number = cc[1 + 2*LINK_SIZE]; |
406 | 0 | cb.callout_string_offset = 0; |
407 | 0 | cb.callout_string_length = 0; |
408 | 0 | cb.callout_string = NULL; |
409 | 0 | rc = callback(&cb, callout_data); |
410 | 0 | if (rc != 0) return rc; |
411 | 0 | cc += PRIV(OP_lengths)[*cc]; |
412 | 0 | break; |
413 | | |
414 | 0 | case OP_CALLOUT_STR: |
415 | 0 | cb.pattern_position = GET(cc, 1); |
416 | 0 | cb.next_item_length = GET(cc, 1 + LINK_SIZE); |
417 | 0 | cb.callout_number = 0; |
418 | 0 | cb.callout_string_offset = GET(cc, 1 + 3*LINK_SIZE); |
419 | 0 | cb.callout_string_length = |
420 | 0 | GET(cc, 1 + 2*LINK_SIZE) - (1 + 4*LINK_SIZE) - 2; |
421 | 0 | cb.callout_string = cc + (1 + 4*LINK_SIZE) + 1; |
422 | 0 | rc = callback(&cb, callout_data); |
423 | 0 | if (rc != 0) return rc; |
424 | 0 | cc += GET(cc, 1 + 2*LINK_SIZE); |
425 | 0 | break; |
426 | | |
427 | 0 | default: |
428 | 0 | cc += PRIV(OP_lengths)[*cc]; |
429 | 0 | break; |
430 | 0 | } |
431 | 0 | } |
432 | 0 | } |
433 | | |
434 | | /* End of pcre2_pattern_info.c */ |