Coverage Report

Created: 2026-12-03 06:40

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