rugged 1.9.4 → 1.9.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (91) hide show
  1. checksums.yaml +4 -4
  2. data/lib/rugged/version.rb +1 -1
  3. data/vendor/libgit2/CMakeLists.txt +1 -1
  4. data/vendor/libgit2/COPYING +83 -11
  5. data/vendor/libgit2/cmake/SelectRegex.cmake +8 -3
  6. data/vendor/libgit2/deps/pcre2/CMakeLists.txt +110 -0
  7. data/vendor/libgit2/deps/pcre2/LICENCE.md +104 -0
  8. data/vendor/libgit2/deps/pcre2/config.h.in +54 -0
  9. data/vendor/libgit2/deps/pcre2/pcre2.h +1079 -0
  10. data/vendor/libgit2/deps/pcre2/pcre2_auto_possess.c +1416 -0
  11. data/vendor/libgit2/deps/{pcre/pcre_chartables.c → pcre2/pcre2_chartables.c} +41 -47
  12. data/vendor/libgit2/deps/{pcre/pcre_ord2utf8.c → pcre2/pcre2_chkdint.c} +36 -36
  13. data/vendor/libgit2/deps/pcre2/pcre2_compile.c +11345 -0
  14. data/vendor/libgit2/deps/pcre2/pcre2_compile.h +356 -0
  15. data/vendor/libgit2/deps/pcre2/pcre2_compile_cgroup.c +632 -0
  16. data/vendor/libgit2/deps/pcre2/pcre2_compile_class.c +2777 -0
  17. data/vendor/libgit2/deps/pcre2/pcre2_config.c +250 -0
  18. data/vendor/libgit2/deps/pcre2/pcre2_context.c +557 -0
  19. data/vendor/libgit2/deps/pcre2/pcre2_convert.c +1263 -0
  20. data/vendor/libgit2/deps/{pcre/pcre_dfa_exec.c → pcre2/pcre2_dfa_match.c} +1402 -943
  21. data/vendor/libgit2/deps/pcre2/pcre2_error.c +384 -0
  22. data/vendor/libgit2/deps/pcre2/pcre2_extuni.c +159 -0
  23. data/vendor/libgit2/deps/pcre2/pcre2_find_bracket.c +217 -0
  24. data/vendor/libgit2/deps/pcre2/pcre2_fuzzsupport.c +844 -0
  25. data/vendor/libgit2/deps/pcre2/pcre2_internal.h +2353 -0
  26. data/vendor/libgit2/deps/pcre2/pcre2_intmodedep.h +1044 -0
  27. data/vendor/libgit2/deps/pcre2/pcre2_maketables.c +177 -0
  28. data/vendor/libgit2/deps/pcre2/pcre2_match.c +8244 -0
  29. data/vendor/libgit2/deps/pcre2/pcre2_match_data.c +184 -0
  30. data/vendor/libgit2/deps/pcre2/pcre2_match_next.c +171 -0
  31. data/vendor/libgit2/deps/{pcre/pcre_newline.c → pcre2/pcre2_newline.c} +92 -63
  32. data/vendor/libgit2/deps/{pcre/pcre_refcount.c → pcre2/pcre2_ord2utf.c} +64 -38
  33. data/vendor/libgit2/deps/pcre2/pcre2_pattern_info.c +430 -0
  34. data/vendor/libgit2/deps/pcre2/pcre2_printint_inc.h +1098 -0
  35. data/vendor/libgit2/deps/pcre2/pcre2_script_run.c +344 -0
  36. data/vendor/libgit2/deps/pcre2/pcre2_serialize.c +284 -0
  37. data/vendor/libgit2/deps/pcre2/pcre2_string_utils.c +199 -0
  38. data/vendor/libgit2/deps/pcre2/pcre2_study.c +2087 -0
  39. data/vendor/libgit2/deps/pcre2/pcre2_substitute.c +1761 -0
  40. data/vendor/libgit2/deps/pcre2/pcre2_substring.c +553 -0
  41. data/vendor/libgit2/deps/pcre2/pcre2_tables.c +310 -0
  42. data/vendor/libgit2/deps/pcre2/pcre2_ucd.c +5805 -0
  43. data/vendor/libgit2/deps/pcre2/pcre2_ucp.h +408 -0
  44. data/vendor/libgit2/deps/pcre2/pcre2_ucptables_inc.h +1596 -0
  45. data/vendor/libgit2/deps/pcre2/pcre2_util.h +179 -0
  46. data/vendor/libgit2/deps/pcre2/pcre2_valid_utf.c +397 -0
  47. data/vendor/libgit2/deps/pcre2/pcre2_xclass.c +547 -0
  48. data/vendor/libgit2/include/git2/common.h +13 -1
  49. data/vendor/libgit2/include/git2/version.h +2 -2
  50. data/vendor/libgit2/src/libgit2/blame.c +6 -0
  51. data/vendor/libgit2/src/libgit2/delta.c +8 -0
  52. data/vendor/libgit2/src/libgit2/indexer.c +3 -2
  53. data/vendor/libgit2/src/libgit2/settings.c +9 -0
  54. data/vendor/libgit2/src/libgit2/streams/openssl.c +4 -1
  55. data/vendor/libgit2/src/libgit2/submodule.c +77 -12
  56. data/vendor/libgit2/src/libgit2/transports/http.c +4 -4
  57. data/vendor/libgit2/src/libgit2/transports/smart_pkt.c +2 -1
  58. data/vendor/libgit2/src/util/fs_path.h +10 -10
  59. data/vendor/libgit2/src/util/regexp.c +27 -12
  60. data/vendor/libgit2/src/util/regexp.h +7 -2
  61. metadata +44 -37
  62. data/vendor/libgit2/deps/pcre/CMakeLists.txt +0 -140
  63. data/vendor/libgit2/deps/pcre/COPYING +0 -5
  64. data/vendor/libgit2/deps/pcre/LICENCE +0 -93
  65. data/vendor/libgit2/deps/pcre/cmake/COPYING-CMAKE-SCRIPTS +0 -22
  66. data/vendor/libgit2/deps/pcre/cmake/FindEditline.cmake +0 -17
  67. data/vendor/libgit2/deps/pcre/cmake/FindPackageHandleStandardArgs.cmake +0 -58
  68. data/vendor/libgit2/deps/pcre/cmake/FindReadline.cmake +0 -29
  69. data/vendor/libgit2/deps/pcre/config.h.in +0 -57
  70. data/vendor/libgit2/deps/pcre/pcre.h +0 -641
  71. data/vendor/libgit2/deps/pcre/pcre_byte_order.c +0 -319
  72. data/vendor/libgit2/deps/pcre/pcre_compile.c +0 -9815
  73. data/vendor/libgit2/deps/pcre/pcre_config.c +0 -190
  74. data/vendor/libgit2/deps/pcre/pcre_exec.c +0 -7173
  75. data/vendor/libgit2/deps/pcre/pcre_fullinfo.c +0 -245
  76. data/vendor/libgit2/deps/pcre/pcre_get.c +0 -669
  77. data/vendor/libgit2/deps/pcre/pcre_globals.c +0 -86
  78. data/vendor/libgit2/deps/pcre/pcre_internal.h +0 -2787
  79. data/vendor/libgit2/deps/pcre/pcre_jit_compile.c +0 -11913
  80. data/vendor/libgit2/deps/pcre/pcre_maketables.c +0 -156
  81. data/vendor/libgit2/deps/pcre/pcre_printint.c +0 -834
  82. data/vendor/libgit2/deps/pcre/pcre_string_utils.c +0 -211
  83. data/vendor/libgit2/deps/pcre/pcre_study.c +0 -1686
  84. data/vendor/libgit2/deps/pcre/pcre_tables.c +0 -727
  85. data/vendor/libgit2/deps/pcre/pcre_ucd.c +0 -3644
  86. data/vendor/libgit2/deps/pcre/pcre_valid_utf8.c +0 -301
  87. data/vendor/libgit2/deps/pcre/pcre_version.c +0 -98
  88. data/vendor/libgit2/deps/pcre/pcre_xclass.c +0 -268
  89. data/vendor/libgit2/deps/pcre/pcreposix.c +0 -420
  90. data/vendor/libgit2/deps/pcre/pcreposix.h +0 -117
  91. data/vendor/libgit2/deps/pcre/ucp.h +0 -224
@@ -0,0 +1,2087 @@
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
+ /* This module contains functions for scanning a compiled pattern and
43
+ collecting data (e.g. minimum matching length). */
44
+
45
+
46
+ #include "pcre2_internal.h"
47
+
48
+
49
+
50
+ /* The maximum remembered capturing brackets minimum. */
51
+
52
+ #define MAX_CACHE_BACKREF 128
53
+
54
+ /* Set a bit in the starting code unit bit map. */
55
+
56
+ #define SET_BIT(c) re->start_bitmap[(c)/8] |= (1u << ((c)&7))
57
+
58
+ /* Returns from set_start_bits() */
59
+
60
+ enum { SSB_FAIL, SSB_DONE, SSB_CONTINUE, SSB_UNKNOWN, SSB_TOODEEP };
61
+
62
+
63
+ /*************************************************
64
+ * Find the minimum subject length for a group *
65
+ *************************************************/
66
+
67
+ /* Scan a parenthesized group and compute the minimum length of subject that
68
+ is needed to match it. This is a lower bound; it does not mean there is a
69
+ string of that length that matches. In UTF mode, the result is in characters
70
+ rather than code units. The field in a compiled pattern for storing the minimum
71
+ length is 16-bits long (on the grounds that anything longer than that is
72
+ pathological), so we give up when we reach that amount. This also means that
73
+ integer overflow for really crazy patterns cannot happen.
74
+
75
+ Backreference minimum lengths are cached to speed up multiple references. This
76
+ function is called only when the highest back reference in the pattern is less
77
+ than or equal to MAX_CACHE_BACKREF, which is one less than the size of the
78
+ caching vector. The zeroth element contains the number of the highest set
79
+ value.
80
+
81
+ Arguments:
82
+ re compiled pattern block
83
+ code pointer to start of group (the bracket)
84
+ startcode pointer to start of the whole pattern's code
85
+ utf UTF flag
86
+ recurses chain of recurse_check to catch mutual recursion
87
+ countptr pointer to call count (to catch over complexity)
88
+ backref_cache vector for caching back references.
89
+
90
+ This function is no longer called when the pattern contains (*ACCEPT); however,
91
+ the old code for returning -1 is retained, just in case.
92
+
93
+ Returns: the minimum length
94
+ -1 \C in UTF-8 mode
95
+ or (*ACCEPT)
96
+ or pattern too complicated
97
+ -2 internal error (missing capturing bracket)
98
+ -3 internal error (opcode not listed)
99
+ */
100
+
101
+ static int
102
+ find_minlength(const pcre2_real_code *re, PCRE2_SPTR code,
103
+ PCRE2_SPTR startcode, BOOL utf, recurse_check *recurses, int *countptr,
104
+ int *backref_cache)
105
+ {
106
+ int length = -1;
107
+ int branchlength = 0;
108
+ int prev_cap_recno = -1;
109
+ int prev_cap_d = 0;
110
+ int prev_recurse_recno = -1;
111
+ int prev_recurse_d = 0;
112
+ uint32_t once_fudge = 0;
113
+ BOOL had_recurse = FALSE;
114
+ BOOL dupcapused = (re->flags & PCRE2_DUPCAPUSED) != 0;
115
+ PCRE2_SPTR nextbranch = code + GET(code, 1);
116
+ PCRE2_SPTR cc = code + 1 + LINK_SIZE;
117
+ recurse_check this_recurse;
118
+
119
+ /* If this is a "could be empty" group, its minimum length is 0. */
120
+
121
+ if (*code >= OP_SBRA && *code <= OP_SCOND) return 0;
122
+
123
+ /* Skip over capturing bracket number */
124
+
125
+ if (*code == OP_CBRA || *code == OP_CBRAPOS) cc += IMM2_SIZE;
126
+
127
+ /* A large and/or complex regex can take too long to process. */
128
+
129
+ if ((*countptr)++ > 1000) return -1;
130
+
131
+ /* Scan along the opcodes for this branch. If we get to the end of the branch,
132
+ check the length against that of the other branches. If the accumulated length
133
+ passes 16-bits, reset to that value and skip the rest of the branch. */
134
+
135
+ for (;;)
136
+ {
137
+ int d, min, recno;
138
+ PCRE2_UCHAR op;
139
+ PCRE2_SPTR cs, ce;
140
+
141
+ if (branchlength >= (int)UINT16_MAX)
142
+ {
143
+ branchlength = UINT16_MAX;
144
+ cc = nextbranch;
145
+ }
146
+
147
+ op = *cc;
148
+ switch (op)
149
+ {
150
+ case OP_COND:
151
+ case OP_SCOND:
152
+
153
+ /* If there is only one branch in a condition, the implied branch has zero
154
+ length, so we don't add anything. This covers the DEFINE "condition"
155
+ automatically. If there are two branches we can treat it the same as any
156
+ other non-capturing subpattern. */
157
+
158
+ cs = cc + GET(cc, 1);
159
+ if (*cs != OP_ALT)
160
+ {
161
+ cc = cs + 1 + LINK_SIZE;
162
+ break;
163
+ }
164
+ goto PROCESS_NON_CAPTURE;
165
+
166
+ case OP_BRA:
167
+ /* There's a special case of OP_BRA, when it is wrapped round a repeated
168
+ OP_RECURSE. We'd like to process the latter at this level so that
169
+ remembering the value works for repeated cases. So we do nothing, but
170
+ set a fudge value to skip over the OP_KET after the recurse. */
171
+
172
+ if (cc[1+LINK_SIZE] == OP_RECURSE && cc[2*(1+LINK_SIZE)] == OP_KET)
173
+ {
174
+ once_fudge = 1 + LINK_SIZE;
175
+ cc += 1 + LINK_SIZE;
176
+ break;
177
+ }
178
+ PCRE2_FALLTHROUGH /* Fall through */
179
+
180
+ case OP_ONCE:
181
+ case OP_SCRIPT_RUN:
182
+ case OP_SBRA:
183
+ case OP_BRAPOS:
184
+ case OP_SBRAPOS:
185
+ PROCESS_NON_CAPTURE:
186
+ d = find_minlength(re, cc, startcode, utf, recurses, countptr,
187
+ backref_cache);
188
+ if (d < 0) return d;
189
+ branchlength += d;
190
+ do cc += GET(cc, 1); while (*cc == OP_ALT);
191
+ cc += 1 + LINK_SIZE;
192
+ break;
193
+
194
+ /* To save time for repeated capturing subpatterns, we remember the
195
+ length of the previous one. Unfortunately we can't do the same for
196
+ the unnumbered ones above. Nor can we do this if (?| is present in the
197
+ pattern because captures with the same number are not then identical. */
198
+
199
+ case OP_CBRA:
200
+ case OP_SCBRA:
201
+ case OP_CBRAPOS:
202
+ case OP_SCBRAPOS:
203
+ recno = (int)GET2(cc, 1+LINK_SIZE);
204
+ if (dupcapused || recno != prev_cap_recno)
205
+ {
206
+ prev_cap_recno = recno;
207
+ prev_cap_d = find_minlength(re, cc, startcode, utf, recurses, countptr,
208
+ backref_cache);
209
+ if (prev_cap_d < 0) return prev_cap_d;
210
+ }
211
+ branchlength += prev_cap_d;
212
+ do cc += GET(cc, 1); while (*cc == OP_ALT);
213
+ cc += 1 + LINK_SIZE;
214
+ break;
215
+
216
+ /* ACCEPT makes things far too complicated; we have to give up. In fact,
217
+ from 10.34 onwards, if a pattern contains (*ACCEPT), this function is not
218
+ used. However, leave the code in place, just in case. */
219
+
220
+ case OP_ACCEPT:
221
+ case OP_ASSERT_ACCEPT:
222
+ return -1;
223
+
224
+ /* Reached end of a branch; if it's a ket it is the end of a nested
225
+ call. If it's ALT it is an alternation in a nested call. If it is END it's
226
+ the end of the outer call. All can be handled by the same code. If the
227
+ length of any branch is zero, there is no need to scan any subsequent
228
+ branches. */
229
+
230
+ case OP_ALT:
231
+ case OP_KET:
232
+ case OP_KETRMAX:
233
+ case OP_KETRMIN:
234
+ case OP_KETRPOS:
235
+ case OP_END:
236
+ if (length < 0 || (!had_recurse && branchlength < length))
237
+ length = branchlength;
238
+ if (op != OP_ALT || length == 0) return length;
239
+ nextbranch = cc + GET(cc, 1);
240
+ cc += 1 + LINK_SIZE;
241
+ branchlength = 0;
242
+ had_recurse = FALSE;
243
+ break;
244
+
245
+ /* Skip over assertive subpatterns */
246
+
247
+ case OP_ASSERT:
248
+ case OP_ASSERT_NOT:
249
+ case OP_ASSERTBACK:
250
+ case OP_ASSERTBACK_NOT:
251
+ case OP_ASSERT_NA:
252
+ case OP_ASSERT_SCS:
253
+ case OP_ASSERTBACK_NA:
254
+ do cc += GET(cc, 1); while (*cc == OP_ALT);
255
+ PCRE2_FALLTHROUGH /* Fall through */
256
+
257
+ /* Skip over things that don't match chars */
258
+
259
+ case OP_REVERSE:
260
+ case OP_VREVERSE:
261
+ case OP_CREF:
262
+ case OP_DNCREF:
263
+ case OP_RREF:
264
+ case OP_DNRREF:
265
+ case OP_FALSE:
266
+ case OP_TRUE:
267
+ case OP_CALLOUT:
268
+ case OP_SOD:
269
+ case OP_SOM:
270
+ case OP_EOD:
271
+ case OP_EODN:
272
+ case OP_CIRC:
273
+ case OP_CIRCM:
274
+ case OP_DOLL:
275
+ case OP_DOLLM:
276
+ case OP_NOT_WORD_BOUNDARY:
277
+ case OP_WORD_BOUNDARY:
278
+ case OP_NOT_UCP_WORD_BOUNDARY:
279
+ case OP_UCP_WORD_BOUNDARY:
280
+ cc += PRIV(OP_lengths)[*cc];
281
+ break;
282
+
283
+ case OP_CALLOUT_STR:
284
+ cc += GET(cc, 1 + 2*LINK_SIZE);
285
+ break;
286
+
287
+ /* Skip over a subpattern that has a {0} or {0,x} quantifier */
288
+
289
+ case OP_BRAZERO:
290
+ case OP_BRAMINZERO:
291
+ case OP_BRAPOSZERO:
292
+ case OP_SKIPZERO:
293
+ cc += PRIV(OP_lengths)[*cc];
294
+ do cc += GET(cc, 1); while (*cc == OP_ALT);
295
+ cc += 1 + LINK_SIZE;
296
+ break;
297
+
298
+ /* Handle literal characters and + repetitions */
299
+
300
+ case OP_CHAR:
301
+ case OP_CHARI:
302
+ case OP_NOT:
303
+ case OP_NOTI:
304
+ case OP_PLUS:
305
+ case OP_PLUSI:
306
+ case OP_MINPLUS:
307
+ case OP_MINPLUSI:
308
+ case OP_POSPLUS:
309
+ case OP_POSPLUSI:
310
+ case OP_NOTPLUS:
311
+ case OP_NOTPLUSI:
312
+ case OP_NOTMINPLUS:
313
+ case OP_NOTMINPLUSI:
314
+ case OP_NOTPOSPLUS:
315
+ case OP_NOTPOSPLUSI:
316
+ branchlength++;
317
+ cc += 2;
318
+ #ifdef SUPPORT_UNICODE
319
+ if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
320
+ #endif
321
+ break;
322
+
323
+ case OP_TYPEPLUS:
324
+ case OP_TYPEMINPLUS:
325
+ case OP_TYPEPOSPLUS:
326
+ branchlength++;
327
+ cc += (cc[1] == OP_PROP || cc[1] == OP_NOTPROP)? 4 : 2;
328
+ break;
329
+
330
+ /* Handle exact repetitions. The count is already in characters, but we
331
+ may need to skip over a multibyte character in UTF mode. */
332
+
333
+ case OP_EXACT:
334
+ case OP_EXACTI:
335
+ case OP_NOTEXACT:
336
+ case OP_NOTEXACTI:
337
+ branchlength += GET2(cc,1);
338
+ cc += 2 + IMM2_SIZE;
339
+ #ifdef SUPPORT_UNICODE
340
+ if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
341
+ #endif
342
+ break;
343
+
344
+ case OP_TYPEEXACT:
345
+ branchlength += GET2(cc,1);
346
+ cc += 2 + IMM2_SIZE + ((cc[1 + IMM2_SIZE] == OP_PROP
347
+ || cc[1 + IMM2_SIZE] == OP_NOTPROP)? 2 : 0);
348
+ break;
349
+
350
+ /* Handle single-char non-literal matchers */
351
+
352
+ case OP_PROP:
353
+ case OP_NOTPROP:
354
+ cc += 2;
355
+ PCRE2_FALLTHROUGH /* Fall through */
356
+
357
+ case OP_NOT_DIGIT:
358
+ case OP_DIGIT:
359
+ case OP_NOT_WHITESPACE:
360
+ case OP_WHITESPACE:
361
+ case OP_NOT_WORDCHAR:
362
+ case OP_WORDCHAR:
363
+ case OP_ANY:
364
+ case OP_ALLANY:
365
+ case OP_EXTUNI:
366
+ case OP_HSPACE:
367
+ case OP_NOT_HSPACE:
368
+ case OP_VSPACE:
369
+ case OP_NOT_VSPACE:
370
+ branchlength++;
371
+ cc++;
372
+ break;
373
+
374
+ /* "Any newline" might match two characters, but it also might match just
375
+ one. */
376
+
377
+ case OP_ANYNL:
378
+ branchlength += 1;
379
+ cc++;
380
+ break;
381
+
382
+ /* The single-byte matcher means we can't proceed in UTF mode. (In
383
+ non-UTF mode \C will actually be turned into OP_ALLANY, so won't ever
384
+ appear, but leave the code, just in case.) */
385
+
386
+ case OP_ANYBYTE:
387
+ #ifdef SUPPORT_UNICODE
388
+ if (utf) return -1;
389
+ #endif
390
+ branchlength++;
391
+ cc++;
392
+ break;
393
+
394
+ /* For repeated character types, we have to test for \p and \P, which have
395
+ an extra two bytes of parameters. */
396
+
397
+ case OP_TYPESTAR:
398
+ case OP_TYPEMINSTAR:
399
+ case OP_TYPEQUERY:
400
+ case OP_TYPEMINQUERY:
401
+ case OP_TYPEPOSSTAR:
402
+ case OP_TYPEPOSQUERY:
403
+ if (cc[1] == OP_PROP || cc[1] == OP_NOTPROP) cc += 2;
404
+ cc += PRIV(OP_lengths)[op];
405
+ break;
406
+
407
+ case OP_TYPEUPTO:
408
+ case OP_TYPEMINUPTO:
409
+ case OP_TYPEPOSUPTO:
410
+ if (cc[1 + IMM2_SIZE] == OP_PROP
411
+ || cc[1 + IMM2_SIZE] == OP_NOTPROP) cc += 2;
412
+ cc += PRIV(OP_lengths)[op];
413
+ break;
414
+
415
+ /* Check a class for variable quantification */
416
+
417
+ case OP_CLASS:
418
+ case OP_NCLASS:
419
+ #ifdef SUPPORT_WIDE_CHARS
420
+ case OP_XCLASS:
421
+ case OP_ECLASS:
422
+ /* The original code caused an unsigned overflow in 64 bit systems,
423
+ so now we use a conditional statement. */
424
+ if (op == OP_XCLASS || op == OP_ECLASS)
425
+ cc += GET(cc, 1);
426
+ else
427
+ #endif
428
+ cc += PRIV(OP_lengths)[OP_CLASS];
429
+
430
+ switch (*cc)
431
+ {
432
+ case OP_CRPLUS:
433
+ case OP_CRMINPLUS:
434
+ case OP_CRPOSPLUS:
435
+ branchlength++;
436
+ PCRE2_FALLTHROUGH /* Fall through */
437
+
438
+ case OP_CRSTAR:
439
+ case OP_CRMINSTAR:
440
+ case OP_CRQUERY:
441
+ case OP_CRMINQUERY:
442
+ case OP_CRPOSSTAR:
443
+ case OP_CRPOSQUERY:
444
+ cc++;
445
+ break;
446
+
447
+ case OP_CRRANGE:
448
+ case OP_CRMINRANGE:
449
+ case OP_CRPOSRANGE:
450
+ branchlength += GET2(cc,1);
451
+ cc += 1 + 2 * IMM2_SIZE;
452
+ break;
453
+
454
+ default:
455
+ branchlength++;
456
+ break;
457
+ }
458
+ break;
459
+
460
+ /* Backreferences and subroutine calls (OP_RECURSE) are treated in the same
461
+ way: we find the minimum length for the subpattern. A recursion
462
+ (backreference or subroutine) causes an a flag to be set that causes the
463
+ length of this branch to be ignored. The logic is that a recursion can only
464
+ make sense if there is another alternative that stops the recursing. That
465
+ will provide the minimum length (when no recursion happens).
466
+
467
+ If PCRE2_MATCH_UNSET_BACKREF is set, a backreference to an unset bracket
468
+ matches an empty string (by default it causes a matching failure), so in
469
+ that case we must set the minimum length to zero.
470
+
471
+ For backreferenes, if duplicate numbers are present in the pattern we check
472
+ for a reference to a duplicate. If it is, we don't know which version will
473
+ be referenced, so we have to set the minimum length to zero. */
474
+
475
+ /* Duplicate named pattern back reference. */
476
+
477
+ case OP_DNREF:
478
+ case OP_DNREFI:
479
+ if (!dupcapused && (re->overall_options & PCRE2_MATCH_UNSET_BACKREF) == 0)
480
+ {
481
+ int count = GET2(cc, 1+IMM2_SIZE);
482
+ PCRE2_SPTR slot =
483
+ (PCRE2_SPTR)((const uint8_t *)re + sizeof(pcre2_real_code)) +
484
+ GET2(cc, 1) * re->name_entry_size;
485
+
486
+ d = INT_MAX;
487
+
488
+ /* Scan all groups with the same name; find the shortest. */
489
+
490
+ while (count-- > 0)
491
+ {
492
+ int dd, i;
493
+ recno = GET2(slot, 0);
494
+
495
+ if (recno <= backref_cache[0] && backref_cache[recno] >= 0)
496
+ dd = backref_cache[recno];
497
+ else
498
+ {
499
+ ce = cs = PRIV(find_bracket)(startcode, utf, recno);
500
+ if (cs == NULL) return -2;
501
+ do ce += GET(ce, 1); while (*ce == OP_ALT);
502
+
503
+ dd = 0;
504
+ if (!dupcapused || PRIV(find_bracket)(ce, utf, recno) == NULL)
505
+ {
506
+ if (cc > cs && cc < ce) /* Simple recursion */
507
+ {
508
+ had_recurse = TRUE;
509
+ }
510
+ else
511
+ {
512
+ recurse_check *r = recurses;
513
+ for (r = recurses; r != NULL; r = r->prev)
514
+ if (r->group == cs) break;
515
+ if (r != NULL) /* Mutual recursion */
516
+ {
517
+ had_recurse = TRUE;
518
+ }
519
+ else
520
+ {
521
+ this_recurse.prev = recurses; /* No recursion */
522
+ this_recurse.group = cs;
523
+ dd = find_minlength(re, cs, startcode, utf, &this_recurse,
524
+ countptr, backref_cache);
525
+ if (dd < 0) return dd;
526
+ }
527
+ }
528
+ }
529
+
530
+ backref_cache[recno] = dd;
531
+ for (i = backref_cache[0] + 1; i < recno; i++) backref_cache[i] = -1;
532
+ backref_cache[0] = recno;
533
+ }
534
+
535
+ if (dd < d) d = dd;
536
+ if (d <= 0) break; /* No point looking at any more */
537
+ slot += re->name_entry_size;
538
+ }
539
+ }
540
+ else d = 0;
541
+ cc += PRIV(OP_lengths)[*cc];
542
+ goto REPEAT_BACK_REFERENCE;
543
+
544
+ /* Single back reference by number. References by name are converted to by
545
+ number when there is no duplication. */
546
+
547
+ case OP_REF:
548
+ case OP_REFI:
549
+ recno = GET2(cc, 1);
550
+ if (recno <= backref_cache[0] && backref_cache[recno] >= 0)
551
+ d = backref_cache[recno];
552
+ else
553
+ {
554
+ int i;
555
+ d = 0;
556
+
557
+ if ((re->overall_options & PCRE2_MATCH_UNSET_BACKREF) == 0)
558
+ {
559
+ ce = cs = PRIV(find_bracket)(startcode, utf, recno);
560
+ if (cs == NULL) return -2;
561
+ do ce += GET(ce, 1); while (*ce == OP_ALT);
562
+
563
+ if (!dupcapused || PRIV(find_bracket)(ce, utf, recno) == NULL)
564
+ {
565
+ if (cc > cs && cc < ce) /* Simple recursion */
566
+ {
567
+ had_recurse = TRUE;
568
+ }
569
+ else
570
+ {
571
+ recurse_check *r = recurses;
572
+ for (r = recurses; r != NULL; r = r->prev) if (r->group == cs) break;
573
+ if (r != NULL) /* Mutual recursion */
574
+ {
575
+ had_recurse = TRUE;
576
+ }
577
+ else /* No recursion */
578
+ {
579
+ this_recurse.prev = recurses;
580
+ this_recurse.group = cs;
581
+ d = find_minlength(re, cs, startcode, utf, &this_recurse, countptr,
582
+ backref_cache);
583
+ if (d < 0) return d;
584
+ }
585
+ }
586
+ }
587
+ }
588
+
589
+ backref_cache[recno] = d;
590
+ for (i = backref_cache[0] + 1; i < recno; i++) backref_cache[i] = -1;
591
+ backref_cache[0] = recno;
592
+ }
593
+
594
+ cc += PRIV(OP_lengths)[*cc];
595
+
596
+ /* Handle repeated back references */
597
+
598
+ REPEAT_BACK_REFERENCE:
599
+ switch (*cc)
600
+ {
601
+ case OP_CRSTAR:
602
+ case OP_CRMINSTAR:
603
+ case OP_CRQUERY:
604
+ case OP_CRMINQUERY:
605
+ case OP_CRPOSSTAR:
606
+ case OP_CRPOSQUERY:
607
+ min = 0;
608
+ cc++;
609
+ break;
610
+
611
+ case OP_CRPLUS:
612
+ case OP_CRMINPLUS:
613
+ case OP_CRPOSPLUS:
614
+ min = 1;
615
+ cc++;
616
+ break;
617
+
618
+ case OP_CRRANGE:
619
+ case OP_CRMINRANGE:
620
+ case OP_CRPOSRANGE:
621
+ min = GET2(cc, 1);
622
+ cc += 1 + 2 * IMM2_SIZE;
623
+ break;
624
+
625
+ default:
626
+ min = 1;
627
+ break;
628
+ }
629
+
630
+ /* Take care not to overflow: (1) min and d are ints, so check that their
631
+ product is not greater than INT_MAX. (2) branchlength is limited to
632
+ UINT16_MAX (checked at the top of the loop). */
633
+
634
+ if ((d > 0 && (INT_MAX/d) < min) || (int)UINT16_MAX - branchlength < min*d)
635
+ branchlength = UINT16_MAX;
636
+ else branchlength += min * d;
637
+ break;
638
+
639
+ /* Recursion always refers to the first occurrence of a subpattern with a
640
+ given number. Therefore, we can always make use of caching, even when the
641
+ pattern contains multiple subpatterns with the same number. */
642
+
643
+ case OP_RECURSE:
644
+ cs = ce = startcode + GET(cc, 1);
645
+ recno = GET2(cs, 1+LINK_SIZE);
646
+ if (recno == prev_recurse_recno)
647
+ {
648
+ branchlength += prev_recurse_d;
649
+ }
650
+ else
651
+ {
652
+ do ce += GET(ce, 1); while (*ce == OP_ALT);
653
+ if (cc > cs && cc < ce) /* Simple recursion */
654
+ had_recurse = TRUE;
655
+ else
656
+ {
657
+ recurse_check *r = recurses;
658
+ for (r = recurses; r != NULL; r = r->prev) if (r->group == cs) break;
659
+ if (r != NULL) /* Mutual recursion */
660
+ had_recurse = TRUE;
661
+ else
662
+ {
663
+ this_recurse.prev = recurses;
664
+ this_recurse.group = cs;
665
+ prev_recurse_d = find_minlength(re, cs, startcode, utf, &this_recurse,
666
+ countptr, backref_cache);
667
+ if (prev_recurse_d < 0) return prev_recurse_d;
668
+ prev_recurse_recno = recno;
669
+ branchlength += prev_recurse_d;
670
+ }
671
+ }
672
+ }
673
+ cc += 1 + LINK_SIZE + once_fudge;
674
+ once_fudge = 0;
675
+ break;
676
+
677
+ /* Anything else does not or need not match a character. We can get the
678
+ item's length from the table, but for those that can match zero occurrences
679
+ of a character, we must take special action for UTF-8 characters. As it
680
+ happens, the "NOT" versions of these opcodes are used at present only for
681
+ ASCII characters, so they could be omitted from this list. However, in
682
+ future that may change, so we include them here so as not to leave a
683
+ gotcha for a future maintainer. */
684
+
685
+ case OP_UPTO:
686
+ case OP_UPTOI:
687
+ case OP_NOTUPTO:
688
+ case OP_NOTUPTOI:
689
+ case OP_MINUPTO:
690
+ case OP_MINUPTOI:
691
+ case OP_NOTMINUPTO:
692
+ case OP_NOTMINUPTOI:
693
+ case OP_POSUPTO:
694
+ case OP_POSUPTOI:
695
+ case OP_NOTPOSUPTO:
696
+ case OP_NOTPOSUPTOI:
697
+
698
+ case OP_STAR:
699
+ case OP_STARI:
700
+ case OP_NOTSTAR:
701
+ case OP_NOTSTARI:
702
+ case OP_MINSTAR:
703
+ case OP_MINSTARI:
704
+ case OP_NOTMINSTAR:
705
+ case OP_NOTMINSTARI:
706
+ case OP_POSSTAR:
707
+ case OP_POSSTARI:
708
+ case OP_NOTPOSSTAR:
709
+ case OP_NOTPOSSTARI:
710
+
711
+ case OP_QUERY:
712
+ case OP_QUERYI:
713
+ case OP_NOTQUERY:
714
+ case OP_NOTQUERYI:
715
+ case OP_MINQUERY:
716
+ case OP_MINQUERYI:
717
+ case OP_NOTMINQUERY:
718
+ case OP_NOTMINQUERYI:
719
+ case OP_POSQUERY:
720
+ case OP_POSQUERYI:
721
+ case OP_NOTPOSQUERY:
722
+ case OP_NOTPOSQUERYI:
723
+
724
+ cc += PRIV(OP_lengths)[op];
725
+ #ifdef SUPPORT_UNICODE
726
+ if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
727
+ #endif
728
+ break;
729
+
730
+ /* Skip these, but we need to add in the name length. */
731
+
732
+ case OP_MARK:
733
+ case OP_COMMIT_ARG:
734
+ case OP_PRUNE_ARG:
735
+ case OP_SKIP_ARG:
736
+ case OP_THEN_ARG:
737
+ cc += PRIV(OP_lengths)[op] + cc[1];
738
+ break;
739
+
740
+ /* The remaining opcodes are just skipped over. */
741
+
742
+ case OP_CLOSE:
743
+ case OP_COMMIT:
744
+ case OP_FAIL:
745
+ case OP_PRUNE:
746
+ case OP_SET_SOM:
747
+ case OP_SKIP:
748
+ case OP_THEN:
749
+ cc += PRIV(OP_lengths)[op];
750
+ break;
751
+
752
+ /* This should not occur: we list all opcodes explicitly so that when
753
+ new ones get added they are properly considered. */
754
+
755
+ /* LCOV_EXCL_START */
756
+ default:
757
+ PCRE2_DEBUG_UNREACHABLE();
758
+ return -3;
759
+ /* LCOV_EXCL_STOP */
760
+ }
761
+ }
762
+
763
+ /* LCOV_EXCL_START */
764
+ PCRE2_DEBUG_UNREACHABLE(); /* Control should never reach here */
765
+ return -3; /* Avoid compiler warnings */
766
+ /* LCOV_EXCL_STOP */
767
+ }
768
+
769
+
770
+
771
+ /*************************************************
772
+ * Set a bit and maybe its alternate case *
773
+ *************************************************/
774
+
775
+ /* Given a character, set its first code unit's bit in the table, and also the
776
+ corresponding bit for the other version of a letter if we are caseless.
777
+
778
+ Arguments:
779
+ re points to the regex block
780
+ p points to the first code unit of the character
781
+ caseless TRUE if caseless
782
+ utf TRUE for UTF mode
783
+ ucp TRUE for UCP mode
784
+
785
+ Returns: pointer after the character
786
+ */
787
+
788
+ static PCRE2_SPTR
789
+ set_table_bit(pcre2_real_code *re, PCRE2_SPTR p, BOOL caseless, BOOL utf,
790
+ BOOL ucp)
791
+ {
792
+ uint32_t c = *p++; /* First code unit */
793
+
794
+ (void)utf; /* Stop compiler warnings when UTF not supported */
795
+ (void)ucp;
796
+
797
+ /* In 16-bit and 32-bit modes, code units greater than 0xff set the bit for
798
+ 0xff. */
799
+
800
+ #if PCRE2_CODE_UNIT_WIDTH != 8
801
+ if (c > 0xff) SET_BIT(0xff); else
802
+ #endif
803
+
804
+ SET_BIT(c);
805
+
806
+ /* In UTF-8 or UTF-16 mode, pick up the remaining code units in order to find
807
+ the end of the character, even when caseless. */
808
+
809
+ #ifdef SUPPORT_UNICODE
810
+ if (utf)
811
+ {
812
+ #if PCRE2_CODE_UNIT_WIDTH == 8
813
+ if (c >= 0xc0) GETUTF8INC(c, p);
814
+ #elif PCRE2_CODE_UNIT_WIDTH == 16
815
+ if ((c & 0xfc00) == 0xd800) GETUTF16INC(c, p);
816
+ #endif
817
+ }
818
+ #endif /* SUPPORT_UNICODE */
819
+
820
+ /* If caseless, handle the other case of the character. */
821
+
822
+ if (caseless)
823
+ {
824
+ #ifdef SUPPORT_UNICODE
825
+ if (utf || ucp)
826
+ {
827
+ c = UCD_OTHERCASE(c);
828
+ #if PCRE2_CODE_UNIT_WIDTH == 8
829
+ if (utf)
830
+ {
831
+ PCRE2_UCHAR buff[6];
832
+ (void)PRIV(ord2utf)(c, buff);
833
+ SET_BIT(buff[0]);
834
+ }
835
+ else if (c < 256) SET_BIT(c);
836
+ #else /* 16-bit or 32-bit mode */
837
+ if (c > 0xff) SET_BIT(0xff); else SET_BIT(c);
838
+ #endif
839
+ }
840
+
841
+ else
842
+ #endif /* SUPPORT_UNICODE */
843
+
844
+ /* Not UTF or UCP */
845
+
846
+ if (MAX_255(c)) SET_BIT(re->tables[fcc_offset + c]);
847
+ }
848
+
849
+ return p;
850
+ }
851
+
852
+
853
+
854
+ /*************************************************
855
+ * Set bits for a positive character type *
856
+ *************************************************/
857
+
858
+ /* This function sets starting bits for a character type. In UTF-8 mode, we can
859
+ only do a direct setting for bytes less than 128, as otherwise there can be
860
+ confusion with bytes in the middle of UTF-8 characters. In a "traditional"
861
+ environment, the tables will only recognize ASCII characters anyway, but in at
862
+ least one Windows environment, some higher bytes bits were set in the tables.
863
+ So we deal with that case by considering the UTF-8 encoding.
864
+
865
+ Arguments:
866
+ re the regex block
867
+ cbit type the type of character wanted
868
+ table_limit 32 for non-UTF-8; 16 for UTF-8
869
+
870
+ Returns: nothing
871
+ */
872
+
873
+ static void
874
+ set_type_bits(pcre2_real_code *re, int cbit_type, unsigned int table_limit)
875
+ {
876
+ uint32_t c;
877
+ for (c = 0; c < table_limit; c++)
878
+ re->start_bitmap[c] |= re->tables[c+cbits_offset+cbit_type];
879
+ #if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
880
+ if (table_limit == 32) return;
881
+ for (c = 128; c < 256; c++)
882
+ {
883
+ if ((re->tables[cbits_offset + c/8] & (1u << (c&7))) != 0)
884
+ {
885
+ PCRE2_UCHAR buff[6];
886
+ (void)PRIV(ord2utf)(c, buff);
887
+ SET_BIT(buff[0]);
888
+ }
889
+ }
890
+ #endif /* UTF-8 */
891
+ }
892
+
893
+
894
+ /*************************************************
895
+ * Set bits for a negative character type *
896
+ *************************************************/
897
+
898
+ /* This function sets starting bits for a negative character type such as \D.
899
+ In UTF-8 mode, we can only do a direct setting for bytes less than 128, as
900
+ otherwise there can be confusion with bytes in the middle of UTF-8 characters.
901
+ Unlike in the positive case, where we can set appropriate starting bits for
902
+ specific high-valued UTF-8 characters, in this case we have to set the bits for
903
+ all high-valued characters. The lowest is 0xc2, but we overkill by starting at
904
+ 0xc0 (192) for simplicity.
905
+
906
+ Arguments:
907
+ re the regex block
908
+ cbit type the type of character wanted
909
+ table_limit 32 for non-UTF-8; 16 for UTF-8
910
+
911
+ Returns: nothing
912
+ */
913
+
914
+ static void
915
+ set_nottype_bits(pcre2_real_code *re, int cbit_type, unsigned int table_limit)
916
+ {
917
+ uint32_t c;
918
+ for (c = 0; c < table_limit; c++)
919
+ re->start_bitmap[c] |= (uint8_t)(~(re->tables[c+cbits_offset+cbit_type]));
920
+ #if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
921
+ if (table_limit != 32) for (c = 24; c < 32; c++) re->start_bitmap[c] = 0xff;
922
+ #endif
923
+ }
924
+
925
+
926
+
927
+ #if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
928
+ /*************************************************
929
+ * Set starting bits for a character list. *
930
+ *************************************************/
931
+
932
+ /* This function sets starting bits for a character list. It enumerates
933
+ all characters and character ranges in the character list, and sets
934
+ the starting bits accordingly.
935
+
936
+ Arguments:
937
+ code pointer to the code
938
+ start_bitmap pointer to the starting bitmap
939
+
940
+ Returns: nothing
941
+ */
942
+ static void
943
+ study_char_list(PCRE2_SPTR code, uint8_t *start_bitmap,
944
+ const uint8_t *char_lists_end)
945
+ {
946
+ uint32_t type, list_ind;
947
+ uint32_t char_list_add = XCL_CHAR_LIST_LOW_16_ADD;
948
+ uint32_t range_start = ~(uint32_t)0, range_end = 0;
949
+ const uint8_t *next_char;
950
+ PCRE2_UCHAR start_buffer[6], end_buffer[6];
951
+ PCRE2_UCHAR start, end;
952
+
953
+ /* Only needed in 8-bit mode at the moment. */
954
+ type = (uint32_t)(code[0] << 8) | code[1];
955
+ code += 2;
956
+
957
+ /* Align characters. */
958
+ next_char = char_lists_end - (GET(code, 0) << 1);
959
+ type &= XCL_TYPE_MASK;
960
+ list_ind = 0;
961
+
962
+ if ((type & XCL_BEGIN_WITH_RANGE) != 0)
963
+ range_start = XCL_CHAR_LIST_LOW_16_START;
964
+
965
+ while (type > 0)
966
+ {
967
+ uint32_t item_count = type & XCL_ITEM_COUNT_MASK;
968
+
969
+ if (item_count == XCL_ITEM_COUNT_MASK)
970
+ {
971
+ if (list_ind <= 1)
972
+ {
973
+ item_count = *(const uint16_t*)next_char;
974
+ next_char += 2;
975
+ }
976
+ else
977
+ {
978
+ item_count = *(const uint32_t*)next_char;
979
+ next_char += 4;
980
+ }
981
+ }
982
+
983
+ while (item_count > 0)
984
+ {
985
+ if (list_ind <= 1)
986
+ {
987
+ range_end = *(const uint16_t*)next_char;
988
+ next_char += 2;
989
+ }
990
+ else
991
+ {
992
+ range_end = *(const uint32_t*)next_char;
993
+ next_char += 4;
994
+ }
995
+
996
+ if ((range_end & XCL_CHAR_END) != 0)
997
+ {
998
+ range_end = char_list_add + (range_end >> XCL_CHAR_SHIFT);
999
+
1000
+ PRIV(ord2utf)(range_end, end_buffer);
1001
+ end = end_buffer[0];
1002
+
1003
+ if (range_start < range_end)
1004
+ {
1005
+ PRIV(ord2utf)(range_start, start_buffer);
1006
+ for (start = start_buffer[0]; start <= end; start++)
1007
+ start_bitmap[start / 8] |= (1u << (start & 7));
1008
+ }
1009
+ else
1010
+ start_bitmap[end / 8] |= (1u << (end & 7));
1011
+
1012
+ range_start = ~(uint32_t)0;
1013
+ }
1014
+ else
1015
+ range_start = char_list_add + (range_end >> XCL_CHAR_SHIFT);
1016
+
1017
+ item_count--;
1018
+ }
1019
+
1020
+ list_ind++;
1021
+ type >>= XCL_TYPE_BIT_LEN;
1022
+
1023
+ if (range_start == ~(uint32_t)0)
1024
+ {
1025
+ if ((type & XCL_BEGIN_WITH_RANGE) != 0)
1026
+ {
1027
+ /* In 8 bit mode XCL_CHAR_LIST_HIGH_32_START is not possible. */
1028
+ if (list_ind == 1) range_start = XCL_CHAR_LIST_HIGH_16_START;
1029
+ else range_start = XCL_CHAR_LIST_LOW_32_START;
1030
+ }
1031
+ }
1032
+ else if ((type & XCL_BEGIN_WITH_RANGE) == 0)
1033
+ {
1034
+ PRIV(ord2utf)(range_start, start_buffer);
1035
+
1036
+ /* In 8 bit mode XCL_CHAR_LIST_LOW_32_END and
1037
+ XCL_CHAR_LIST_HIGH_32_END are not possible. */
1038
+ if (list_ind == 1) range_end = XCL_CHAR_LIST_LOW_16_END;
1039
+ else range_end = XCL_CHAR_LIST_HIGH_16_END;
1040
+
1041
+ PRIV(ord2utf)(range_end, end_buffer);
1042
+ end = end_buffer[0];
1043
+
1044
+ for (start = start_buffer[0]; start <= end; start++)
1045
+ start_bitmap[start / 8] |= (1u << (start & 7));
1046
+
1047
+ range_start = ~(uint32_t)0;
1048
+ }
1049
+
1050
+ /* In 8 bit mode XCL_CHAR_LIST_HIGH_32_ADD is not possible. */
1051
+ if (list_ind == 1) char_list_add = XCL_CHAR_LIST_HIGH_16_ADD;
1052
+ else char_list_add = XCL_CHAR_LIST_LOW_32_ADD;
1053
+ }
1054
+ }
1055
+ #endif
1056
+
1057
+
1058
+
1059
+ /*************************************************
1060
+ * Create bitmap of starting code units *
1061
+ *************************************************/
1062
+
1063
+ /* This function scans a compiled unanchored expression recursively and
1064
+ attempts to build a bitmap of the set of possible starting code units whose
1065
+ values are less than 256. In 16-bit and 32-bit mode, values above 255 all cause
1066
+ the 255 bit to be set. When calling set[_not]_type_bits() in UTF-8 (sic) mode
1067
+ we pass a value of 16 rather than 32 as the final argument. (See comments in
1068
+ those functions for the reason.)
1069
+
1070
+ The SSB_CONTINUE return is useful for parenthesized groups in patterns such as
1071
+ (a*)b where the group provides some optional starting code units but scanning
1072
+ must continue at the outer level to find at least one mandatory code unit. At
1073
+ the outermost level, this function fails unless the result is SSB_DONE.
1074
+
1075
+ We restrict recursion (for nested groups) to 1000 to avoid stack overflow
1076
+ issues.
1077
+
1078
+ Arguments:
1079
+ re points to the compiled regex block
1080
+ code points to an expression
1081
+ utf TRUE if in UTF mode
1082
+ ucp TRUE if in UCP mode
1083
+ depthptr pointer to recurse depth
1084
+
1085
+ Returns: SSB_FAIL => Failed to find any starting code units
1086
+ SSB_DONE => Found mandatory starting code units
1087
+ SSB_CONTINUE => Found optional starting code units
1088
+ SSB_UNKNOWN => Hit an unrecognized opcode
1089
+ SSB_TOODEEP => Recursion is too deep
1090
+ */
1091
+
1092
+ static int
1093
+ set_start_bits(pcre2_real_code *re, PCRE2_SPTR code, BOOL utf, BOOL ucp,
1094
+ int *depthptr)
1095
+ {
1096
+ uint32_t c;
1097
+ int yield = SSB_DONE;
1098
+ BOOL done;
1099
+
1100
+ #if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
1101
+ int table_limit = utf? 16:32;
1102
+ #else
1103
+ int table_limit = 32;
1104
+ #endif
1105
+
1106
+ *depthptr += 1;
1107
+ if (*depthptr > 1000) return SSB_TOODEEP;
1108
+
1109
+ do
1110
+ {
1111
+ BOOL try_next = TRUE;
1112
+ PCRE2_SPTR tcode = code + 1 + LINK_SIZE;
1113
+
1114
+ if (*code == OP_CBRA || *code == OP_SCBRA ||
1115
+ *code == OP_CBRAPOS || *code == OP_SCBRAPOS) tcode += IMM2_SIZE;
1116
+
1117
+ while (try_next) /* Loop for items in this branch */
1118
+ {
1119
+ int rc;
1120
+ PCRE2_SPTR ncode;
1121
+ const uint8_t *classmap = NULL;
1122
+ #ifdef SUPPORT_WIDE_CHARS
1123
+ PCRE2_UCHAR xclassflags;
1124
+ #endif
1125
+
1126
+ switch(*tcode)
1127
+ {
1128
+ /* If we reach something we don't understand, it means a new opcode has
1129
+ been created that hasn't been added to this function. Hopefully this
1130
+ problem will be discovered during testing. */
1131
+
1132
+ default:
1133
+ return SSB_UNKNOWN;
1134
+
1135
+ /* Fail for a valid opcode that implies no starting bits. */
1136
+
1137
+ case OP_ACCEPT:
1138
+ case OP_ASSERT_ACCEPT:
1139
+ case OP_ALLANY:
1140
+ case OP_ANY:
1141
+ case OP_ANYBYTE:
1142
+ case OP_CIRCM:
1143
+ case OP_CLOSE:
1144
+ case OP_COMMIT:
1145
+ case OP_COMMIT_ARG:
1146
+ case OP_COND:
1147
+ case OP_CREF:
1148
+ case OP_FALSE:
1149
+ case OP_TRUE:
1150
+ case OP_DNCREF:
1151
+ case OP_DNREF:
1152
+ case OP_DNREFI:
1153
+ case OP_DNRREF:
1154
+ case OP_DOLL:
1155
+ case OP_DOLLM:
1156
+ case OP_END:
1157
+ case OP_EOD:
1158
+ case OP_EODN:
1159
+ case OP_EXTUNI:
1160
+ case OP_FAIL:
1161
+ case OP_MARK:
1162
+ case OP_NOT:
1163
+ case OP_NOTEXACT:
1164
+ case OP_NOTEXACTI:
1165
+ case OP_NOTI:
1166
+ case OP_NOTMINPLUS:
1167
+ case OP_NOTMINPLUSI:
1168
+ case OP_NOTMINQUERY:
1169
+ case OP_NOTMINQUERYI:
1170
+ case OP_NOTMINSTAR:
1171
+ case OP_NOTMINSTARI:
1172
+ case OP_NOTMINUPTO:
1173
+ case OP_NOTMINUPTOI:
1174
+ case OP_NOTPLUS:
1175
+ case OP_NOTPLUSI:
1176
+ case OP_NOTPOSPLUS:
1177
+ case OP_NOTPOSPLUSI:
1178
+ case OP_NOTPOSQUERY:
1179
+ case OP_NOTPOSQUERYI:
1180
+ case OP_NOTPOSSTAR:
1181
+ case OP_NOTPOSSTARI:
1182
+ case OP_NOTPOSUPTO:
1183
+ case OP_NOTPOSUPTOI:
1184
+ case OP_NOTPROP:
1185
+ case OP_NOTQUERY:
1186
+ case OP_NOTQUERYI:
1187
+ case OP_NOTSTAR:
1188
+ case OP_NOTSTARI:
1189
+ case OP_NOTUPTO:
1190
+ case OP_NOTUPTOI:
1191
+ case OP_NOT_HSPACE:
1192
+ case OP_NOT_VSPACE:
1193
+ case OP_PRUNE:
1194
+ case OP_PRUNE_ARG:
1195
+ case OP_RECURSE:
1196
+ case OP_REF:
1197
+ case OP_REFI:
1198
+ case OP_REVERSE:
1199
+ case OP_VREVERSE:
1200
+ case OP_RREF:
1201
+ case OP_SCOND:
1202
+ case OP_SET_SOM:
1203
+ case OP_SKIP:
1204
+ case OP_SKIP_ARG:
1205
+ case OP_SOD:
1206
+ case OP_SOM:
1207
+ case OP_THEN:
1208
+ case OP_THEN_ARG:
1209
+ return SSB_FAIL;
1210
+
1211
+ /* OP_CIRC happens only at the start of an anchored branch (multiline ^
1212
+ uses OP_CIRCM). Skip over it. */
1213
+
1214
+ case OP_CIRC:
1215
+ tcode += PRIV(OP_lengths)[OP_CIRC];
1216
+ break;
1217
+
1218
+ /* A "real" property test implies no starting bits, but the fake property
1219
+ PT_CLIST identifies a list of characters. These lists are short, as they
1220
+ are used for characters with more than one "other case", so there is no
1221
+ point in recognizing them for OP_NOTPROP. */
1222
+
1223
+ case OP_PROP:
1224
+ if (tcode[1] != PT_CLIST) return SSB_FAIL;
1225
+ {
1226
+ const uint32_t *p = PRIV(ucd_caseless_sets) + tcode[2];
1227
+ while ((c = *p++) < NOTACHAR)
1228
+ {
1229
+ #if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
1230
+ if (utf)
1231
+ {
1232
+ PCRE2_UCHAR buff[6];
1233
+ (void)PRIV(ord2utf)(c, buff);
1234
+ c = buff[0];
1235
+ }
1236
+ #endif
1237
+ if (c > 0xff) SET_BIT(0xff); else SET_BIT(c);
1238
+ }
1239
+ }
1240
+ try_next = FALSE;
1241
+ break;
1242
+
1243
+ /* We can ignore word boundary tests. */
1244
+
1245
+ case OP_WORD_BOUNDARY:
1246
+ case OP_NOT_WORD_BOUNDARY:
1247
+ case OP_UCP_WORD_BOUNDARY:
1248
+ case OP_NOT_UCP_WORD_BOUNDARY:
1249
+ tcode++;
1250
+ break;
1251
+
1252
+ /* For a positive lookahead assertion, inspect what immediately follows,
1253
+ ignoring intermediate assertions and callouts. If the next item is one
1254
+ that sets a mandatory character, skip this assertion. Otherwise, treat it
1255
+ the same as other bracket groups. */
1256
+
1257
+ case OP_ASSERT:
1258
+ case OP_ASSERT_NA:
1259
+ ncode = tcode + GET(tcode, 1);
1260
+ while (*ncode == OP_ALT) ncode += GET(ncode, 1);
1261
+ ncode += 1 + LINK_SIZE;
1262
+
1263
+ /* Skip irrelevant items */
1264
+
1265
+ for (done = FALSE; !done;)
1266
+ {
1267
+ switch (*ncode)
1268
+ {
1269
+ case OP_ASSERT:
1270
+ case OP_ASSERT_NOT:
1271
+ case OP_ASSERTBACK:
1272
+ case OP_ASSERTBACK_NOT:
1273
+ case OP_ASSERT_NA:
1274
+ case OP_ASSERTBACK_NA:
1275
+ case OP_ASSERT_SCS:
1276
+ ncode += GET(ncode, 1);
1277
+ while (*ncode == OP_ALT) ncode += GET(ncode, 1);
1278
+ ncode += 1 + LINK_SIZE;
1279
+ break;
1280
+
1281
+ case OP_WORD_BOUNDARY:
1282
+ case OP_NOT_WORD_BOUNDARY:
1283
+ case OP_UCP_WORD_BOUNDARY:
1284
+ case OP_NOT_UCP_WORD_BOUNDARY:
1285
+ ncode++;
1286
+ break;
1287
+
1288
+ case OP_CALLOUT:
1289
+ ncode += PRIV(OP_lengths)[OP_CALLOUT];
1290
+ break;
1291
+
1292
+ case OP_CALLOUT_STR:
1293
+ ncode += GET(ncode, 1 + 2*LINK_SIZE);
1294
+ break;
1295
+
1296
+ default:
1297
+ done = TRUE;
1298
+ break;
1299
+ }
1300
+ }
1301
+
1302
+ /* Now check the next significant item. */
1303
+
1304
+ switch(*ncode)
1305
+ {
1306
+ default:
1307
+ break;
1308
+
1309
+ case OP_PROP:
1310
+ if (ncode[1] != PT_CLIST) break;
1311
+ PCRE2_FALLTHROUGH /* Fall through */
1312
+ case OP_ANYNL:
1313
+ case OP_CHAR:
1314
+ case OP_CHARI:
1315
+ case OP_EXACT:
1316
+ case OP_EXACTI:
1317
+ case OP_HSPACE:
1318
+ case OP_MINPLUS:
1319
+ case OP_MINPLUSI:
1320
+ case OP_PLUS:
1321
+ case OP_PLUSI:
1322
+ case OP_POSPLUS:
1323
+ case OP_POSPLUSI:
1324
+ case OP_VSPACE:
1325
+ /* Note that these types will only be present in non-UCP mode. */
1326
+ case OP_DIGIT:
1327
+ case OP_NOT_DIGIT:
1328
+ case OP_WORDCHAR:
1329
+ case OP_NOT_WORDCHAR:
1330
+ case OP_WHITESPACE:
1331
+ case OP_NOT_WHITESPACE:
1332
+ tcode = ncode;
1333
+ continue; /* With the following significant opcode */
1334
+ }
1335
+ PCRE2_FALLTHROUGH /* Fall through */
1336
+
1337
+ /* For a group bracket or a positive assertion without an immediately
1338
+ following mandatory setting, recurse to set bits from within the
1339
+ subpattern. If it can't find anything, we have to give up. If it finds
1340
+ some mandatory character(s), we are done for this branch. Otherwise,
1341
+ carry on scanning after the subpattern. */
1342
+
1343
+ case OP_BRA:
1344
+ case OP_SBRA:
1345
+ case OP_CBRA:
1346
+ case OP_SCBRA:
1347
+ case OP_BRAPOS:
1348
+ case OP_SBRAPOS:
1349
+ case OP_CBRAPOS:
1350
+ case OP_SCBRAPOS:
1351
+ case OP_ONCE:
1352
+ case OP_SCRIPT_RUN:
1353
+ rc = set_start_bits(re, tcode, utf, ucp, depthptr);
1354
+ if (rc == SSB_DONE)
1355
+ {
1356
+ try_next = FALSE;
1357
+ }
1358
+ else if (rc == SSB_CONTINUE)
1359
+ {
1360
+ do tcode += GET(tcode, 1); while (*tcode == OP_ALT);
1361
+ tcode += 1 + LINK_SIZE;
1362
+ }
1363
+ else return rc; /* FAIL, UNKNOWN, or TOODEEP */
1364
+ break;
1365
+
1366
+ /* If we hit ALT or KET, it means we haven't found anything mandatory in
1367
+ this branch, though we might have found something optional. For ALT, we
1368
+ continue with the next alternative, but we have to arrange that the final
1369
+ result from subpattern is SSB_CONTINUE rather than SSB_DONE. For KET,
1370
+ return SSB_CONTINUE: if this is the top level, that indicates failure,
1371
+ but after a nested subpattern, it causes scanning to continue. */
1372
+
1373
+ case OP_ALT:
1374
+ yield = SSB_CONTINUE;
1375
+ try_next = FALSE;
1376
+ break;
1377
+
1378
+ case OP_KET:
1379
+ case OP_KETRMAX:
1380
+ case OP_KETRMIN:
1381
+ case OP_KETRPOS:
1382
+ return SSB_CONTINUE;
1383
+
1384
+ /* Skip over callout */
1385
+
1386
+ case OP_CALLOUT:
1387
+ tcode += PRIV(OP_lengths)[OP_CALLOUT];
1388
+ break;
1389
+
1390
+ case OP_CALLOUT_STR:
1391
+ tcode += GET(tcode, 1 + 2*LINK_SIZE);
1392
+ break;
1393
+
1394
+ /* Skip over lookbehind, negative lookahead, and scan substring
1395
+ assertions */
1396
+
1397
+ case OP_ASSERT_NOT:
1398
+ case OP_ASSERTBACK:
1399
+ case OP_ASSERTBACK_NOT:
1400
+ case OP_ASSERTBACK_NA:
1401
+ case OP_ASSERT_SCS:
1402
+ do tcode += GET(tcode, 1); while (*tcode == OP_ALT);
1403
+ tcode += 1 + LINK_SIZE;
1404
+ break;
1405
+
1406
+ /* BRAZERO does the bracket, but carries on. */
1407
+
1408
+ case OP_BRAZERO:
1409
+ case OP_BRAMINZERO:
1410
+ case OP_BRAPOSZERO:
1411
+ rc = set_start_bits(re, ++tcode, utf, ucp, depthptr);
1412
+ if (rc == SSB_FAIL || rc == SSB_UNKNOWN || rc == SSB_TOODEEP) return rc;
1413
+ do tcode += GET(tcode,1); while (*tcode == OP_ALT);
1414
+ tcode += 1 + LINK_SIZE;
1415
+ break;
1416
+
1417
+ /* SKIPZERO skips the bracket. */
1418
+
1419
+ case OP_SKIPZERO:
1420
+ tcode++;
1421
+ do tcode += GET(tcode,1); while (*tcode == OP_ALT);
1422
+ tcode += 1 + LINK_SIZE;
1423
+ break;
1424
+
1425
+ /* Single-char * or ? sets the bit and tries the next item */
1426
+
1427
+ case OP_STAR:
1428
+ case OP_MINSTAR:
1429
+ case OP_POSSTAR:
1430
+ case OP_QUERY:
1431
+ case OP_MINQUERY:
1432
+ case OP_POSQUERY:
1433
+ tcode = set_table_bit(re, tcode + 1, FALSE, utf, ucp);
1434
+ break;
1435
+
1436
+ case OP_STARI:
1437
+ case OP_MINSTARI:
1438
+ case OP_POSSTARI:
1439
+ case OP_QUERYI:
1440
+ case OP_MINQUERYI:
1441
+ case OP_POSQUERYI:
1442
+ tcode = set_table_bit(re, tcode + 1, TRUE, utf, ucp);
1443
+ break;
1444
+
1445
+ /* Single-char upto sets the bit and tries the next */
1446
+
1447
+ case OP_UPTO:
1448
+ case OP_MINUPTO:
1449
+ case OP_POSUPTO:
1450
+ tcode = set_table_bit(re, tcode + 1 + IMM2_SIZE, FALSE, utf, ucp);
1451
+ break;
1452
+
1453
+ case OP_UPTOI:
1454
+ case OP_MINUPTOI:
1455
+ case OP_POSUPTOI:
1456
+ tcode = set_table_bit(re, tcode + 1 + IMM2_SIZE, TRUE, utf, ucp);
1457
+ break;
1458
+
1459
+ /* At least one single char sets the bit and stops */
1460
+
1461
+ case OP_EXACT:
1462
+ tcode += IMM2_SIZE;
1463
+ PCRE2_FALLTHROUGH /* Fall through */
1464
+ case OP_CHAR:
1465
+ case OP_PLUS:
1466
+ case OP_MINPLUS:
1467
+ case OP_POSPLUS:
1468
+ (void)set_table_bit(re, tcode + 1, FALSE, utf, ucp);
1469
+ try_next = FALSE;
1470
+ break;
1471
+
1472
+ case OP_EXACTI:
1473
+ tcode += IMM2_SIZE;
1474
+ PCRE2_FALLTHROUGH /* Fall through */
1475
+ case OP_CHARI:
1476
+ case OP_PLUSI:
1477
+ case OP_MINPLUSI:
1478
+ case OP_POSPLUSI:
1479
+ (void)set_table_bit(re, tcode + 1, TRUE, utf, ucp);
1480
+ try_next = FALSE;
1481
+ break;
1482
+
1483
+ /* Special spacing and line-terminating items. These recognize specific
1484
+ lists of characters. The difference between VSPACE and ANYNL is that the
1485
+ latter can match the two-character CRLF sequence, but that is not
1486
+ relevant for finding the first character, so their code here is
1487
+ identical. */
1488
+
1489
+ case OP_HSPACE:
1490
+ SET_BIT(CHAR_HT);
1491
+ SET_BIT(CHAR_SPACE);
1492
+
1493
+ /* For the 16-bit and 32-bit libraries (which can never be EBCDIC), set
1494
+ the bits for NBSP and for code units >= 255, independently of UTF. */
1495
+
1496
+ #if PCRE2_CODE_UNIT_WIDTH != 8
1497
+ SET_BIT(CHAR_NBSP);
1498
+ SET_BIT(0xFF);
1499
+ #else
1500
+ /* For the 8-bit library in UTF-8 mode, set the bits for the first code
1501
+ units of horizontal space characters. */
1502
+
1503
+ #ifdef SUPPORT_UNICODE
1504
+ if (utf)
1505
+ {
1506
+ SET_BIT(0xC2); /* For U+00A0 */
1507
+ SET_BIT(0xE1); /* For U+1680, U+180E */
1508
+ SET_BIT(0xE2); /* For U+2000 - U+200A, U+202F, U+205F */
1509
+ SET_BIT(0xE3); /* For U+3000 */
1510
+ }
1511
+ else
1512
+ #endif
1513
+ /* For the 8-bit library not in UTF-8 mode, set the bit for NBSP. */
1514
+ {
1515
+ SET_BIT(CHAR_NBSP);
1516
+ }
1517
+ #endif /* 8-bit support */
1518
+
1519
+ try_next = FALSE;
1520
+ break;
1521
+
1522
+ case OP_ANYNL:
1523
+ case OP_VSPACE:
1524
+ SET_BIT(CHAR_LF);
1525
+ SET_BIT(CHAR_VT);
1526
+ SET_BIT(CHAR_FF);
1527
+ SET_BIT(CHAR_CR);
1528
+
1529
+ /* For the 16-bit and 32-bit libraries (which can never be EBCDIC), set
1530
+ the bits for NEL and for code units >= 255, independently of UTF. */
1531
+
1532
+ #if PCRE2_CODE_UNIT_WIDTH != 8
1533
+ SET_BIT(CHAR_NEL);
1534
+ SET_BIT(0xFF);
1535
+ #else
1536
+ /* For the 8-bit library in UTF-8 mode, set the bits for the first code
1537
+ units of vertical space characters. */
1538
+
1539
+ #ifdef SUPPORT_UNICODE
1540
+ if (utf)
1541
+ {
1542
+ SET_BIT(0xC2); /* For U+0085 (NEL) */
1543
+ SET_BIT(0xE2); /* For U+2028, U+2029 */
1544
+ }
1545
+ else
1546
+ #endif
1547
+ /* For the 8-bit library not in UTF-8 mode, set the bit for NEL. */
1548
+ {
1549
+ SET_BIT(CHAR_NEL);
1550
+ }
1551
+ #endif /* 8-bit support */
1552
+
1553
+ try_next = FALSE;
1554
+ break;
1555
+
1556
+ /* Single character types set the bits and stop. Note that if PCRE2_UCP
1557
+ is set, we do not see these opcodes because \d etc are converted to
1558
+ properties. Therefore, these apply in the case when only characters less
1559
+ than 256 are recognized to match the types. */
1560
+
1561
+ case OP_NOT_DIGIT:
1562
+ set_nottype_bits(re, cbit_digit, table_limit);
1563
+ try_next = FALSE;
1564
+ break;
1565
+
1566
+ case OP_DIGIT:
1567
+ set_type_bits(re, cbit_digit, table_limit);
1568
+ try_next = FALSE;
1569
+ break;
1570
+
1571
+ case OP_NOT_WHITESPACE:
1572
+ set_nottype_bits(re, cbit_space, table_limit);
1573
+ try_next = FALSE;
1574
+ break;
1575
+
1576
+ case OP_WHITESPACE:
1577
+ set_type_bits(re, cbit_space, table_limit);
1578
+ try_next = FALSE;
1579
+ break;
1580
+
1581
+ case OP_NOT_WORDCHAR:
1582
+ set_nottype_bits(re, cbit_word, table_limit);
1583
+ try_next = FALSE;
1584
+ break;
1585
+
1586
+ case OP_WORDCHAR:
1587
+ set_type_bits(re, cbit_word, table_limit);
1588
+ try_next = FALSE;
1589
+ break;
1590
+
1591
+ /* One or more character type fudges the pointer and restarts, knowing
1592
+ it will hit a single character type and stop there. */
1593
+
1594
+ case OP_TYPEPLUS:
1595
+ case OP_TYPEMINPLUS:
1596
+ case OP_TYPEPOSPLUS:
1597
+ tcode++;
1598
+ break;
1599
+
1600
+ case OP_TYPEEXACT:
1601
+ tcode += 1 + IMM2_SIZE;
1602
+ break;
1603
+
1604
+ /* Zero or more repeats of character types set the bits and then
1605
+ try again. */
1606
+
1607
+ case OP_TYPEUPTO:
1608
+ case OP_TYPEMINUPTO:
1609
+ case OP_TYPEPOSUPTO:
1610
+ tcode += IMM2_SIZE;
1611
+ PCRE2_FALLTHROUGH /* Fall through */
1612
+
1613
+ case OP_TYPESTAR:
1614
+ case OP_TYPEMINSTAR:
1615
+ case OP_TYPEPOSSTAR:
1616
+ case OP_TYPEQUERY:
1617
+ case OP_TYPEMINQUERY:
1618
+ case OP_TYPEPOSQUERY:
1619
+ switch(tcode[1])
1620
+ {
1621
+ default:
1622
+ case OP_ANY:
1623
+ case OP_ALLANY:
1624
+ return SSB_FAIL;
1625
+
1626
+ case OP_HSPACE:
1627
+ SET_BIT(CHAR_HT);
1628
+ SET_BIT(CHAR_SPACE);
1629
+
1630
+ /* For the 16-bit and 32-bit libraries (which can never be EBCDIC), set
1631
+ the bits for NBSP and for code units >= 255, independently of UTF. */
1632
+
1633
+ #if PCRE2_CODE_UNIT_WIDTH != 8
1634
+ SET_BIT(CHAR_NBSP);
1635
+ SET_BIT(0xFF);
1636
+ #else
1637
+ /* For the 8-bit library in UTF-8 mode, set the bits for the first code
1638
+ units of horizontal space characters. */
1639
+
1640
+ #ifdef SUPPORT_UNICODE
1641
+ if (utf)
1642
+ {
1643
+ SET_BIT(0xC2); /* For U+00A0 */
1644
+ SET_BIT(0xE1); /* For U+1680, U+180E */
1645
+ SET_BIT(0xE2); /* For U+2000 - U+200A, U+202F, U+205F */
1646
+ SET_BIT(0xE3); /* For U+3000 */
1647
+ }
1648
+ else
1649
+ #endif
1650
+ /* For the 8-bit library not in UTF-8 mode, set the bit for NBSP. */
1651
+ {
1652
+ SET_BIT(CHAR_NBSP);
1653
+ }
1654
+ #endif /* 8-bit support */
1655
+ break;
1656
+
1657
+ case OP_ANYNL:
1658
+ case OP_VSPACE:
1659
+ SET_BIT(CHAR_LF);
1660
+ SET_BIT(CHAR_VT);
1661
+ SET_BIT(CHAR_FF);
1662
+ SET_BIT(CHAR_CR);
1663
+
1664
+ /* For the 16-bit and 32-bit libraries (which can never be EBCDIC), set
1665
+ the bits for NEL and for code units >= 255, independently of UTF. */
1666
+
1667
+ #if PCRE2_CODE_UNIT_WIDTH != 8
1668
+ SET_BIT(CHAR_NEL);
1669
+ SET_BIT(0xFF);
1670
+ #else
1671
+ /* For the 8-bit library in UTF-8 mode, set the bits for the first code
1672
+ units of vertical space characters. */
1673
+
1674
+ #ifdef SUPPORT_UNICODE
1675
+ if (utf)
1676
+ {
1677
+ SET_BIT(0xC2); /* For U+0085 (NEL) */
1678
+ SET_BIT(0xE2); /* For U+2028, U+2029 */
1679
+ }
1680
+ else
1681
+ #endif
1682
+ /* For the 8-bit library not in UTF-8 mode, set the bit for NEL. */
1683
+ {
1684
+ SET_BIT(CHAR_NEL);
1685
+ }
1686
+ #endif /* 8-bit support */
1687
+ break;
1688
+
1689
+ case OP_NOT_DIGIT:
1690
+ set_nottype_bits(re, cbit_digit, table_limit);
1691
+ break;
1692
+
1693
+ case OP_DIGIT:
1694
+ set_type_bits(re, cbit_digit, table_limit);
1695
+ break;
1696
+
1697
+ case OP_NOT_WHITESPACE:
1698
+ set_nottype_bits(re, cbit_space, table_limit);
1699
+ break;
1700
+
1701
+ case OP_WHITESPACE:
1702
+ set_type_bits(re, cbit_space, table_limit);
1703
+ break;
1704
+
1705
+ case OP_NOT_WORDCHAR:
1706
+ set_nottype_bits(re, cbit_word, table_limit);
1707
+ break;
1708
+
1709
+ case OP_WORDCHAR:
1710
+ set_type_bits(re, cbit_word, table_limit);
1711
+ break;
1712
+ }
1713
+
1714
+ tcode += 2;
1715
+ break;
1716
+
1717
+ /* Set-based ECLASS: treat it the same as a "complex" XCLASS; give up. */
1718
+
1719
+ #ifdef SUPPORT_WIDE_CHARS
1720
+ case OP_ECLASS:
1721
+ return SSB_FAIL;
1722
+ #endif
1723
+
1724
+ /* Extended class: if there are any property checks, or if this is a
1725
+ negative XCLASS without a map, give up. If there are no property checks,
1726
+ there must be wide characters on the XCLASS list, because otherwise an
1727
+ XCLASS would not have been created. This means that code points >= 255
1728
+ are potential starters. In the UTF-8 case we can scan them and set bits
1729
+ for the relevant leading bytes. */
1730
+
1731
+ #ifdef SUPPORT_WIDE_CHARS
1732
+ case OP_XCLASS:
1733
+ xclassflags = tcode[1 + LINK_SIZE];
1734
+ if ((xclassflags & XCL_HASPROP) != 0 ||
1735
+ (xclassflags & (XCL_MAP|XCL_NOT)) == XCL_NOT)
1736
+ return SSB_FAIL;
1737
+
1738
+ /* We have a positive XCLASS or a negative one without a map. Set up the
1739
+ map pointer if there is one, and fall through. */
1740
+
1741
+ classmap = ((xclassflags & XCL_MAP) == 0)? NULL :
1742
+ (const uint8_t *)(tcode + 1 + LINK_SIZE + 1);
1743
+
1744
+ /* In UTF-8 mode, scan the character list and set bits for leading bytes,
1745
+ then jump to handle the map. */
1746
+
1747
+ #if PCRE2_CODE_UNIT_WIDTH == 8
1748
+ if (utf && (xclassflags & XCL_NOT) == 0)
1749
+ {
1750
+ PCRE2_UCHAR b, e;
1751
+ PCRE2_SPTR p = tcode + 1 + LINK_SIZE + 1 + ((classmap == NULL)? 0:32);
1752
+ tcode += GET(tcode, 1);
1753
+
1754
+ if (*p >= XCL_LIST)
1755
+ {
1756
+ study_char_list(p, re->start_bitmap,
1757
+ ((const uint8_t *)re + re->code_start));
1758
+ goto HANDLE_CLASSMAP;
1759
+ }
1760
+
1761
+ for (;;) switch (*p++)
1762
+ {
1763
+ case XCL_SINGLE:
1764
+ b = *p++;
1765
+ while ((*p & 0xc0) == 0x80) p++;
1766
+ re->start_bitmap[b/8] |= (1u << (b&7));
1767
+ break;
1768
+
1769
+ case XCL_RANGE:
1770
+ b = *p++;
1771
+ while ((*p & 0xc0) == 0x80) p++;
1772
+ e = *p++;
1773
+ while ((*p & 0xc0) == 0x80) p++;
1774
+ for (; b <= e; b++)
1775
+ re->start_bitmap[b/8] |= (1u << (b&7));
1776
+ break;
1777
+
1778
+ case XCL_END:
1779
+ goto HANDLE_CLASSMAP;
1780
+
1781
+ /* LCOV_EXCL_START */
1782
+ default:
1783
+ PCRE2_DEBUG_UNREACHABLE();
1784
+ return SSB_UNKNOWN; /* Internal error, should not occur */
1785
+ /* LCOV_EXCL_STOP */
1786
+ }
1787
+ }
1788
+ #endif /* SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8 */
1789
+ #endif /* SUPPORT_WIDE_CHARS */
1790
+
1791
+ /* It seems that the fall through comment must be outside the #ifdef if
1792
+ it is to avoid the gcc compiler warning. */
1793
+
1794
+ PCRE2_FALLTHROUGH /* Fall through */
1795
+
1796
+ /* Enter here for a negative non-XCLASS. In the 8-bit library, if we are
1797
+ in UTF mode, any byte with a value >= 0xc4 is a potentially valid starter
1798
+ because it starts a character with a value > 255. In 8-bit non-UTF mode,
1799
+ there is no difference between CLASS and NCLASS. In all other wide
1800
+ character modes, set the 0xFF bit to indicate code units >= 255. */
1801
+
1802
+ case OP_NCLASS:
1803
+ #if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
1804
+ if (utf)
1805
+ {
1806
+ re->start_bitmap[24] |= 0xf0; /* Bits for 0xc4 - 0xc8 */
1807
+ memset(re->start_bitmap+25, 0xff, 7); /* Bits for 0xc9 - 0xff */
1808
+ }
1809
+ PCRE2_FALLTHROUGH /* Fall through */
1810
+ #elif PCRE2_CODE_UNIT_WIDTH != 8
1811
+ SET_BIT(0xFF); /* For characters >= 255 */
1812
+ PCRE2_FALLTHROUGH /* Fall through */
1813
+ #endif
1814
+
1815
+ /* Enter here for a positive non-XCLASS. If we have fallen through from
1816
+ an XCLASS, classmap will already be set; just advance the code pointer.
1817
+ Otherwise, set up classmap for a non-XCLASS and advance past it. */
1818
+
1819
+ case OP_CLASS:
1820
+ if (*tcode == OP_XCLASS) tcode += GET(tcode, 1); else
1821
+ {
1822
+ classmap = (const uint8_t *)(++tcode);
1823
+ tcode += 32 / sizeof(PCRE2_UCHAR);
1824
+ }
1825
+
1826
+ /* When wide characters are supported, classmap may be NULL. In UTF-8
1827
+ (sic) mode, the bits in a class bit map correspond to character values,
1828
+ not to byte values. However, the bit map we are constructing is for byte
1829
+ values. So we have to do a conversion for characters whose code point is
1830
+ greater than 127. In fact, there are only two possible starting bytes for
1831
+ characters in the range 128 - 255. */
1832
+
1833
+ #if defined SUPPORT_WIDE_CHARS && PCRE2_CODE_UNIT_WIDTH == 8
1834
+ HANDLE_CLASSMAP:
1835
+ #endif
1836
+ if (classmap != NULL)
1837
+ {
1838
+ #if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
1839
+ if (utf)
1840
+ {
1841
+ for (c = 0; c < 16; c++) re->start_bitmap[c] |= classmap[c];
1842
+ for (c = 128; c < 256; c++)
1843
+ {
1844
+ if ((classmap[c/8] & (1u << (c&7))) != 0)
1845
+ {
1846
+ int d = (c >> 6) | 0xc0; /* Set bit for this starter */
1847
+ re->start_bitmap[d/8] |= (1u << (d&7)); /* and then skip on to the */
1848
+ c = (c & 0xc0) + 0x40 - 1; /* next relevant character. */
1849
+ }
1850
+ }
1851
+ }
1852
+ else
1853
+ #endif
1854
+ /* In all modes except UTF-8, the two bit maps are compatible. */
1855
+
1856
+ {
1857
+ for (c = 0; c < 32; c++) re->start_bitmap[c] |= classmap[c];
1858
+ }
1859
+ }
1860
+
1861
+ /* Act on what follows the class. For a zero minimum repeat, continue;
1862
+ otherwise stop processing. */
1863
+
1864
+ switch (*tcode)
1865
+ {
1866
+ case OP_CRSTAR:
1867
+ case OP_CRMINSTAR:
1868
+ case OP_CRQUERY:
1869
+ case OP_CRMINQUERY:
1870
+ case OP_CRPOSSTAR:
1871
+ case OP_CRPOSQUERY:
1872
+ tcode++;
1873
+ break;
1874
+
1875
+ case OP_CRRANGE:
1876
+ case OP_CRMINRANGE:
1877
+ case OP_CRPOSRANGE:
1878
+ if (GET2(tcode, 1) == 0) tcode += 1 + 2 * IMM2_SIZE;
1879
+ else try_next = FALSE;
1880
+ break;
1881
+
1882
+ default:
1883
+ try_next = FALSE;
1884
+ break;
1885
+ }
1886
+ break; /* End of class handling case */
1887
+ } /* End of switch for opcodes */
1888
+ } /* End of try_next loop */
1889
+
1890
+ code += GET(code, 1); /* Advance to next branch */
1891
+ }
1892
+ while (*code == OP_ALT);
1893
+
1894
+ return yield;
1895
+ }
1896
+
1897
+
1898
+
1899
+ /*************************************************
1900
+ * Study a compiled expression *
1901
+ *************************************************/
1902
+
1903
+ /* This function is handed a compiled expression that it must study to produce
1904
+ information that will speed up the matching.
1905
+
1906
+ Argument:
1907
+ re points to the compiled expression
1908
+
1909
+ Returns: 0 normally; non-zero should never normally occur
1910
+ 1 unknown opcode in set_start_bits
1911
+ 2 missing capturing bracket
1912
+ 3 unknown opcode in find_minlength
1913
+ */
1914
+
1915
+ int
1916
+ PRIV(study)(pcre2_real_code *re)
1917
+ {
1918
+ int count = 0;
1919
+ PCRE2_UCHAR *code;
1920
+ BOOL utf = (re->overall_options & PCRE2_UTF) != 0;
1921
+ BOOL ucp = (re->overall_options & PCRE2_UCP) != 0;
1922
+
1923
+ /* Find start of compiled code */
1924
+
1925
+ code = (PCRE2_UCHAR *)((uint8_t *)re + re->code_start);
1926
+
1927
+ /* For a pattern that has a first code unit, or a multiline pattern that
1928
+ matches only at "line start", there is no point in seeking a list of starting
1929
+ code units. */
1930
+
1931
+ if ((re->flags & (PCRE2_FIRSTSET|PCRE2_STARTLINE)) == 0)
1932
+ {
1933
+ int depth = 0;
1934
+ int rc = set_start_bits(re, code, utf, ucp, &depth);
1935
+ /* LCOV_EXCL_START */
1936
+ if (rc == SSB_UNKNOWN)
1937
+ {
1938
+ PCRE2_DEBUG_UNREACHABLE();
1939
+ return 1;
1940
+ }
1941
+ /* LCOV_EXCL_STOP */
1942
+
1943
+ /* If a list of starting code units was set up, scan the list to see if only
1944
+ one or two were listed. Having only one listed is rare because usually a
1945
+ single starting code unit will have been recognized and PCRE2_FIRSTSET set.
1946
+ If two are listed, see if they are caseless versions of the same character;
1947
+ if so we can replace the list with a caseless first code unit. This gives
1948
+ better performance and is plausibly worth doing for patterns such as [Ww]ord
1949
+ or (word|WORD). */
1950
+
1951
+ if (rc == SSB_DONE)
1952
+ {
1953
+ int i;
1954
+ int a = -1;
1955
+ int b = -1;
1956
+ uint8_t *p = re->start_bitmap;
1957
+ uint32_t flags = PCRE2_FIRSTMAPSET;
1958
+
1959
+ for (i = 0; i < 256; p++, i += 8)
1960
+ {
1961
+ uint8_t x = *p;
1962
+ if (x != 0)
1963
+ {
1964
+ int c;
1965
+ uint8_t y = x & (~x + 1); /* Least significant bit */
1966
+ if (y != x) goto DONE; /* More than one bit set */
1967
+
1968
+ /* In the 16-bit and 32-bit libraries, the bit for 0xff means "0xff and
1969
+ all wide characters", so we cannot use it here. */
1970
+
1971
+ #if PCRE2_CODE_UNIT_WIDTH != 8
1972
+ if (i == 248 && x == 0x80) goto DONE;
1973
+ #endif
1974
+
1975
+ /* Compute the character value */
1976
+
1977
+ c = i;
1978
+ switch (x)
1979
+ {
1980
+ case 1: break;
1981
+ case 2: c += 1; break; case 4: c += 2; break;
1982
+ case 8: c += 3; break; case 16: c += 4; break;
1983
+ case 32: c += 5; break; case 64: c += 6; break;
1984
+ case 128: c += 7; break;
1985
+ }
1986
+
1987
+ /* c contains the code unit value, in the range 0-255. In 8-bit UTF
1988
+ mode, only values < 128 can be used. In all the other cases, c is a
1989
+ character value. */
1990
+
1991
+ #if PCRE2_CODE_UNIT_WIDTH == 8
1992
+ if (utf && c > 127) goto DONE;
1993
+ #endif
1994
+ if (a < 0) a = c; /* First one found, save in a */
1995
+ else if (b < 0) /* Second one found */
1996
+ {
1997
+ int d = TABLE_GET((unsigned int)c, re->tables + fcc_offset, c);
1998
+
1999
+ #ifdef SUPPORT_UNICODE
2000
+ if (utf || ucp)
2001
+ {
2002
+ if (UCD_CASESET(c) != 0) goto DONE; /* Multiple case set */
2003
+ if (c > 127) d = UCD_OTHERCASE(c);
2004
+ }
2005
+ #endif /* SUPPORT_UNICODE */
2006
+
2007
+ if (d != a) goto DONE; /* Not the other case of a */
2008
+ b = c; /* Save second in b */
2009
+
2010
+ #ifdef EBCDIC
2011
+ /* To match ASCII (which puts the uppercase one in a), swap a & b
2012
+ if needed. This doesn't really matter, but neatens the tests. */
2013
+ if (TABLE_GET((unsigned int)a, re->tables + lcc_offset, a) == a)
2014
+ {
2015
+ b = a;
2016
+ a = c;
2017
+ }
2018
+ #endif
2019
+ }
2020
+ else goto DONE; /* More than two characters found */
2021
+ }
2022
+ }
2023
+
2024
+ /* Replace the start code unit bits with a first code unit. If it is the
2025
+ same as a required later code unit, then clear the required later code
2026
+ unit. This is because a search for a required code unit starts after an
2027
+ explicit first code unit, but at a code unit found from the bitmap.
2028
+ Patterns such as /a*a/ don't work if both the start unit and required
2029
+ unit are the same. */
2030
+
2031
+ if (a >= 0) {
2032
+ if ((re->flags & PCRE2_LASTSET) && (re->last_codeunit == (uint32_t)a || (b >= 0 && re->last_codeunit == (uint32_t)b))) {
2033
+ re->flags &= ~(PCRE2_LASTSET | PCRE2_LASTCASELESS);
2034
+ re->last_codeunit = 0;
2035
+ }
2036
+ re->first_codeunit = a;
2037
+ flags = PCRE2_FIRSTSET;
2038
+ if (b >= 0) flags |= PCRE2_FIRSTCASELESS;
2039
+ }
2040
+
2041
+ DONE:
2042
+ re->flags |= flags;
2043
+ }
2044
+ }
2045
+
2046
+ /* Find the minimum length of subject string. If the pattern can match an empty
2047
+ string, the minimum length is already known. If the pattern contains (*ACCEPT)
2048
+ all bets are off, and we don't even try to find a minimum length. If there are
2049
+ more back references than the size of the vector we are going to cache them in,
2050
+ do nothing. A pattern that complicated will probably take a long time to
2051
+ analyze and may in any case turn out to be too complicated. Note that back
2052
+ reference minima are held as 16-bit numbers. */
2053
+
2054
+ if ((re->flags & (PCRE2_MATCH_EMPTY|PCRE2_HASACCEPT)) == 0 &&
2055
+ re->top_backref <= MAX_CACHE_BACKREF)
2056
+ {
2057
+ int min;
2058
+ int backref_cache[MAX_CACHE_BACKREF+1];
2059
+ backref_cache[0] = 0; /* Highest one that is set */
2060
+ min = find_minlength(re, code, code, utf, NULL, &count, backref_cache);
2061
+ switch(min)
2062
+ {
2063
+ case -1: /* \C in UTF mode or over-complex regex */
2064
+ break; /* Leave minlength unchanged (will be zero) */
2065
+
2066
+ /* LCOV_EXCL_START */
2067
+ case -2:
2068
+ PCRE2_DEBUG_UNREACHABLE();
2069
+ return 2; /* missing capturing bracket */
2070
+ /* LCOV_EXCL_STOP */
2071
+
2072
+ /* LCOV_EXCL_START */
2073
+ case -3:
2074
+ PCRE2_DEBUG_UNREACHABLE();
2075
+ return 3; /* unrecognized opcode */
2076
+ /* LCOV_EXCL_STOP */
2077
+
2078
+ default:
2079
+ re->minlength = (min > (int)UINT16_MAX)? (int)UINT16_MAX : min;
2080
+ break;
2081
+ }
2082
+ }
2083
+
2084
+ return 0;
2085
+ }
2086
+
2087
+ /* End of pcre2_study.c */