Coverage Report

Created: 2026-02-10 07:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/glib-2.80.0/subprojects/pcre2-10.42/src/pcre2_dfa_match.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-2022 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 the external function pcre2_dfa_match(), which is an
43
alternative matching function that uses a sort of DFA algorithm (not a true
44
FSM). This is NOT Perl-compatible, but it has advantages in certain
45
applications. */
46
47
48
/* NOTE ABOUT PERFORMANCE: A user of this function sent some code that improved
49
the performance of his patterns greatly. I could not use it as it stood, as it
50
was not thread safe, and made assumptions about pattern sizes. Also, it caused
51
test 7 to loop, and test 9 to crash with a segfault.
52
53
The issue is the check for duplicate states, which is done by a simple linear
54
search up the state list. (Grep for "duplicate" below to find the code.) For
55
many patterns, there will never be many states active at one time, so a simple
56
linear search is fine. In patterns that have many active states, it might be a
57
bottleneck. The suggested code used an indexing scheme to remember which states
58
had previously been used for each character, and avoided the linear search when
59
it knew there was no chance of a duplicate. This was implemented when adding
60
states to the state lists.
61
62
I wrote some thread-safe, not-limited code to try something similar at the time
63
of checking for duplicates (instead of when adding states), using index vectors
64
on the stack. It did give a 13% improvement with one specially constructed
65
pattern for certain subject strings, but on other strings and on many of the
66
simpler patterns in the test suite it did worse. The major problem, I think,
67
was the extra time to initialize the index. This had to be done for each call
68
of internal_dfa_match(). (The supplied patch used a static vector, initialized
69
only once - I suspect this was the cause of the problems with the tests.)
70
71
Overall, I concluded that the gains in some cases did not outweigh the losses
72
in others, so I abandoned this code. */
73
74
75
#ifdef HAVE_CONFIG_H
76
#include "config.h"
77
#endif
78
79
0
#define NLBLOCK mb             /* Block containing newline information */
80
0
#define PSSTART start_subject  /* Field containing processed string start */
81
0
#define PSEND   end_subject    /* Field containing processed string end */
82
83
#include "pcre2_internal.h"
84
85
#define PUBLIC_DFA_MATCH_OPTIONS \
86
0
  (PCRE2_ANCHORED|PCRE2_ENDANCHORED|PCRE2_NOTBOL|PCRE2_NOTEOL|PCRE2_NOTEMPTY| \
87
0
   PCRE2_NOTEMPTY_ATSTART|PCRE2_NO_UTF_CHECK|PCRE2_PARTIAL_HARD| \
88
0
   PCRE2_PARTIAL_SOFT|PCRE2_DFA_SHORTEST|PCRE2_DFA_RESTART| \
89
0
   PCRE2_COPY_MATCHED_SUBJECT)
90
91
92
/*************************************************
93
*      Code parameters and static tables         *
94
*************************************************/
95
96
/* These are offsets that are used to turn the OP_TYPESTAR and friends opcodes
97
into others, under special conditions. A gap of 20 between the blocks should be
98
enough. The resulting opcodes don't have to be less than 256 because they are
99
never stored, so we push them well clear of the normal opcodes. */
100
101
0
#define OP_PROP_EXTRA       300
102
0
#define OP_EXTUNI_EXTRA     320
103
0
#define OP_ANYNL_EXTRA      340
104
0
#define OP_HSPACE_EXTRA     360
105
0
#define OP_VSPACE_EXTRA     380
106
107
108
/* This table identifies those opcodes that are followed immediately by a
109
character that is to be tested in some way. This makes it possible to
110
centralize the loading of these characters. In the case of Type * etc, the
111
"character" is the opcode for \D, \d, \S, \s, \W, or \w, which will always be a
112
small value. Non-zero values in the table are the offsets from the opcode where
113
the character is to be found. ***NOTE*** If the start of this table is
114
modified, the three tables that follow must also be modified. */
115
116
static const uint8_t coptable[] = {
117
  0,                             /* End                                    */
118
  0, 0, 0, 0, 0,                 /* \A, \G, \K, \B, \b                     */
119
  0, 0, 0, 0, 0, 0,              /* \D, \d, \S, \s, \W, \w                 */
120
  0, 0, 0,                       /* Any, AllAny, Anybyte                   */
121
  0, 0,                          /* \P, \p                                 */
122
  0, 0, 0, 0, 0,                 /* \R, \H, \h, \V, \v                     */
123
  0,                             /* \X                                     */
124
  0, 0, 0, 0, 0, 0,              /* \Z, \z, $, $M, ^, ^M                   */
125
  1,                             /* Char                                   */
126
  1,                             /* Chari                                  */
127
  1,                             /* not                                    */
128
  1,                             /* noti                                   */
129
  /* Positive single-char repeats                                          */
130
  1, 1, 1, 1, 1, 1,              /* *, *?, +, +?, ?, ??                    */
131
  1+IMM2_SIZE, 1+IMM2_SIZE,      /* upto, minupto                          */
132
  1+IMM2_SIZE,                   /* exact                                  */
133
  1, 1, 1, 1+IMM2_SIZE,          /* *+, ++, ?+, upto+                      */
134
  1, 1, 1, 1, 1, 1,              /* *I, *?I, +I, +?I, ?I, ??I              */
135
  1+IMM2_SIZE, 1+IMM2_SIZE,      /* upto I, minupto I                      */
136
  1+IMM2_SIZE,                   /* exact I                                */
137
  1, 1, 1, 1+IMM2_SIZE,          /* *+I, ++I, ?+I, upto+I                  */
138
  /* Negative single-char repeats - only for chars < 256                   */
139
  1, 1, 1, 1, 1, 1,              /* NOT *, *?, +, +?, ?, ??                */
140
  1+IMM2_SIZE, 1+IMM2_SIZE,      /* NOT upto, minupto                      */
141
  1+IMM2_SIZE,                   /* NOT exact                              */
142
  1, 1, 1, 1+IMM2_SIZE,          /* NOT *+, ++, ?+, upto+                  */
143
  1, 1, 1, 1, 1, 1,              /* NOT *I, *?I, +I, +?I, ?I, ??I          */
144
  1+IMM2_SIZE, 1+IMM2_SIZE,      /* NOT upto I, minupto I                  */
145
  1+IMM2_SIZE,                   /* NOT exact I                            */
146
  1, 1, 1, 1+IMM2_SIZE,          /* NOT *+I, ++I, ?+I, upto+I              */
147
  /* Positive type repeats                                                 */
148
  1, 1, 1, 1, 1, 1,              /* Type *, *?, +, +?, ?, ??               */
149
  1+IMM2_SIZE, 1+IMM2_SIZE,      /* Type upto, minupto                     */
150
  1+IMM2_SIZE,                   /* Type exact                             */
151
  1, 1, 1, 1+IMM2_SIZE,          /* Type *+, ++, ?+, upto+                 */
152
  /* Character class & ref repeats                                         */
153
  0, 0, 0, 0, 0, 0,              /* *, *?, +, +?, ?, ??                    */
154
  0, 0,                          /* CRRANGE, CRMINRANGE                    */
155
  0, 0, 0, 0,                    /* Possessive *+, ++, ?+, CRPOSRANGE      */
156
  0,                             /* CLASS                                  */
157
  0,                             /* NCLASS                                 */
158
  0,                             /* XCLASS - variable length               */
159
  0,                             /* REF                                    */
160
  0,                             /* REFI                                   */
161
  0,                             /* DNREF                                  */
162
  0,                             /* DNREFI                                 */
163
  0,                             /* RECURSE                                */
164
  0,                             /* CALLOUT                                */
165
  0,                             /* CALLOUT_STR                            */
166
  0,                             /* Alt                                    */
167
  0,                             /* Ket                                    */
168
  0,                             /* KetRmax                                */
169
  0,                             /* KetRmin                                */
170
  0,                             /* KetRpos                                */
171
  0,                             /* Reverse                                */
172
  0,                             /* Assert                                 */
173
  0,                             /* Assert not                             */
174
  0,                             /* Assert behind                          */
175
  0,                             /* Assert behind not                      */
176
  0,                             /* NA assert                              */
177
  0,                             /* NA assert behind                       */
178
  0,                             /* ONCE                                   */
179
  0,                             /* SCRIPT_RUN                             */
180
  0, 0, 0, 0, 0,                 /* BRA, BRAPOS, CBRA, CBRAPOS, COND       */
181
  0, 0, 0, 0, 0,                 /* SBRA, SBRAPOS, SCBRA, SCBRAPOS, SCOND  */
182
  0, 0,                          /* CREF, DNCREF                           */
183
  0, 0,                          /* RREF, DNRREF                           */
184
  0, 0,                          /* FALSE, TRUE                            */
185
  0, 0, 0,                       /* BRAZERO, BRAMINZERO, BRAPOSZERO        */
186
  0, 0, 0,                       /* MARK, PRUNE, PRUNE_ARG                 */
187
  0, 0, 0, 0,                    /* SKIP, SKIP_ARG, THEN, THEN_ARG         */
188
  0, 0,                          /* COMMIT, COMMIT_ARG                     */
189
  0, 0, 0,                       /* FAIL, ACCEPT, ASSERT_ACCEPT            */
190
  0, 0, 0                        /* CLOSE, SKIPZERO, DEFINE                */
191
};
192
193
/* This table identifies those opcodes that inspect a character. It is used to
194
remember the fact that a character could have been inspected when the end of
195
the subject is reached. ***NOTE*** If the start of this table is modified, the
196
two tables that follow must also be modified. */
197
198
static const uint8_t poptable[] = {
199
  0,                             /* End                                    */
200
  0, 0, 0, 1, 1,                 /* \A, \G, \K, \B, \b                     */
201
  1, 1, 1, 1, 1, 1,              /* \D, \d, \S, \s, \W, \w                 */
202
  1, 1, 1,                       /* Any, AllAny, Anybyte                   */
203
  1, 1,                          /* \P, \p                                 */
204
  1, 1, 1, 1, 1,                 /* \R, \H, \h, \V, \v                     */
205
  1,                             /* \X                                     */
206
  0, 0, 0, 0, 0, 0,              /* \Z, \z, $, $M, ^, ^M                   */
207
  1,                             /* Char                                   */
208
  1,                             /* Chari                                  */
209
  1,                             /* not                                    */
210
  1,                             /* noti                                   */
211
  /* Positive single-char repeats                                          */
212
  1, 1, 1, 1, 1, 1,              /* *, *?, +, +?, ?, ??                    */
213
  1, 1, 1,                       /* upto, minupto, exact                   */
214
  1, 1, 1, 1,                    /* *+, ++, ?+, upto+                      */
215
  1, 1, 1, 1, 1, 1,              /* *I, *?I, +I, +?I, ?I, ??I              */
216
  1, 1, 1,                       /* upto I, minupto I, exact I             */
217
  1, 1, 1, 1,                    /* *+I, ++I, ?+I, upto+I                  */
218
  /* Negative single-char repeats - only for chars < 256                   */
219
  1, 1, 1, 1, 1, 1,              /* NOT *, *?, +, +?, ?, ??                */
220
  1, 1, 1,                       /* NOT upto, minupto, exact               */
221
  1, 1, 1, 1,                    /* NOT *+, ++, ?+, upto+                  */
222
  1, 1, 1, 1, 1, 1,              /* NOT *I, *?I, +I, +?I, ?I, ??I          */
223
  1, 1, 1,                       /* NOT upto I, minupto I, exact I         */
224
  1, 1, 1, 1,                    /* NOT *+I, ++I, ?+I, upto+I              */
225
  /* Positive type repeats                                                 */
226
  1, 1, 1, 1, 1, 1,              /* Type *, *?, +, +?, ?, ??               */
227
  1, 1, 1,                       /* Type upto, minupto, exact              */
228
  1, 1, 1, 1,                    /* Type *+, ++, ?+, upto+                 */
229
  /* Character class & ref repeats                                         */
230
  1, 1, 1, 1, 1, 1,              /* *, *?, +, +?, ?, ??                    */
231
  1, 1,                          /* CRRANGE, CRMINRANGE                    */
232
  1, 1, 1, 1,                    /* Possessive *+, ++, ?+, CRPOSRANGE      */
233
  1,                             /* CLASS                                  */
234
  1,                             /* NCLASS                                 */
235
  1,                             /* XCLASS - variable length               */
236
  0,                             /* REF                                    */
237
  0,                             /* REFI                                   */
238
  0,                             /* DNREF                                  */
239
  0,                             /* DNREFI                                 */
240
  0,                             /* RECURSE                                */
241
  0,                             /* CALLOUT                                */
242
  0,                             /* CALLOUT_STR                            */
243
  0,                             /* Alt                                    */
244
  0,                             /* Ket                                    */
245
  0,                             /* KetRmax                                */
246
  0,                             /* KetRmin                                */
247
  0,                             /* KetRpos                                */
248
  0,                             /* Reverse                                */
249
  0,                             /* Assert                                 */
250
  0,                             /* Assert not                             */
251
  0,                             /* Assert behind                          */
252
  0,                             /* Assert behind not                      */
253
  0,                             /* NA assert                              */
254
  0,                             /* NA assert behind                       */
255
  0,                             /* ONCE                                   */
256
  0,                             /* SCRIPT_RUN                             */
257
  0, 0, 0, 0, 0,                 /* BRA, BRAPOS, CBRA, CBRAPOS, COND       */
258
  0, 0, 0, 0, 0,                 /* SBRA, SBRAPOS, SCBRA, SCBRAPOS, SCOND  */
259
  0, 0,                          /* CREF, DNCREF                           */
260
  0, 0,                          /* RREF, DNRREF                           */
261
  0, 0,                          /* FALSE, TRUE                            */
262
  0, 0, 0,                       /* BRAZERO, BRAMINZERO, BRAPOSZERO        */
263
  0, 0, 0,                       /* MARK, PRUNE, PRUNE_ARG                 */
264
  0, 0, 0, 0,                    /* SKIP, SKIP_ARG, THEN, THEN_ARG         */
265
  0, 0,                          /* COMMIT, COMMIT_ARG                     */
266
  0, 0, 0,                       /* FAIL, ACCEPT, ASSERT_ACCEPT            */
267
  0, 0, 0                        /* CLOSE, SKIPZERO, DEFINE                */
268
};
269
270
/* These 2 tables allow for compact code for testing for \D, \d, \S, \s, \W,
271
and \w */
272
273
static const uint8_t toptable1[] = {
274
  0, 0, 0, 0, 0, 0,
275
  ctype_digit, ctype_digit,
276
  ctype_space, ctype_space,
277
  ctype_word,  ctype_word,
278
  0, 0                            /* OP_ANY, OP_ALLANY */
279
};
280
281
static const uint8_t toptable2[] = {
282
  0, 0, 0, 0, 0, 0,
283
  ctype_digit, 0,
284
  ctype_space, 0,
285
  ctype_word,  0,
286
  1, 1                            /* OP_ANY, OP_ALLANY */
287
};
288
289
290
/* Structure for holding data about a particular state, which is in effect the
291
current data for an active path through the match tree. It must consist
292
entirely of ints because the working vector we are passed, and which we put
293
these structures in, is a vector of ints. */
294
295
typedef struct stateblock {
296
  int offset;                     /* Offset to opcode (-ve has meaning) */
297
  int count;                      /* Count for repeats */
298
  int data;                       /* Some use extra data */
299
} stateblock;
300
301
0
#define INTS_PER_STATEBLOCK  (int)(sizeof(stateblock)/sizeof(int))
302
303
304
/* Before version 10.32 the recursive calls of internal_dfa_match() were passed
305
local working space and output vectors that were created on the stack. This has
306
caused issues for some patterns, especially in small-stack environments such as
307
Windows. A new scheme is now in use which sets up a vector on the stack, but if
308
this is too small, heap memory is used, up to the heap_limit. The main
309
parameters are all numbers of ints because the workspace is a vector of ints.
310
311
The size of the starting stack vector, DFA_START_RWS_SIZE, is in bytes, and is
312
defined in pcre2_internal.h so as to be available to pcre2test when it is
313
finding the minimum heap requirement for a match. */
314
315
0
#define OVEC_UNIT  (sizeof(PCRE2_SIZE)/sizeof(int))
316
317
0
#define RWS_BASE_SIZE   (DFA_START_RWS_SIZE/sizeof(int))  /* Stack vector */
318
0
#define RWS_RSIZE       1000                    /* Work size for recursion */
319
0
#define RWS_OVEC_RSIZE  (1000*OVEC_UNIT)        /* Ovector for recursion */
320
0
#define RWS_OVEC_OSIZE  (2*OVEC_UNIT)           /* Ovector in other cases */
321
322
/* This structure is at the start of each workspace block. */
323
324
typedef struct RWS_anchor {
325
  struct RWS_anchor *next;
326
  uint32_t size;  /* Number of ints */
327
  uint32_t free;  /* Number of ints */
328
} RWS_anchor;
329
330
0
#define RWS_ANCHOR_SIZE (sizeof(RWS_anchor)/sizeof(int))
331
332
333
334
/*************************************************
335
*               Process a callout                *
336
*************************************************/
337
338
/* This function is called to perform a callout.
339
340
Arguments:
341
  code              current code pointer
342
  offsets           points to current capture offsets
343
  current_subject   start of current subject match
344
  ptr               current position in subject
345
  mb                the match block
346
  extracode         extra code offset when called from condition
347
  lengthptr         where to return the callout length
348
349
Returns:            the return from the callout
350
*/
351
352
static int
353
do_callout_dfa(PCRE2_SPTR code, PCRE2_SIZE *offsets, PCRE2_SPTR current_subject,
354
  PCRE2_SPTR ptr, dfa_match_block *mb, PCRE2_SIZE extracode,
355
  PCRE2_SIZE *lengthptr)
356
0
{
357
0
pcre2_callout_block *cb = mb->cb;
358
359
0
*lengthptr = (code[extracode] == OP_CALLOUT)?
360
0
  (PCRE2_SIZE)PRIV(OP_lengths)[OP_CALLOUT] :
361
0
  (PCRE2_SIZE)GET(code, 1 + 2*LINK_SIZE + extracode);
362
363
0
if (mb->callout == NULL) return 0;    /* No callout provided */
364
365
/* Fixed fields in the callout block are set once and for all at the start of
366
matching. */
367
368
0
cb->offset_vector    = offsets;
369
0
cb->start_match      = (PCRE2_SIZE)(current_subject - mb->start_subject);
370
0
cb->current_position = (PCRE2_SIZE)(ptr - mb->start_subject);
371
0
cb->pattern_position = GET(code, 1 + extracode);
372
0
cb->next_item_length = GET(code, 1 + LINK_SIZE + extracode);
373
374
0
if (code[extracode] == OP_CALLOUT)
375
0
  {
376
0
  cb->callout_number = code[1 + 2*LINK_SIZE + extracode];
377
0
  cb->callout_string_offset = 0;
378
0
  cb->callout_string = NULL;
379
0
  cb->callout_string_length = 0;
380
0
  }
381
0
else
382
0
  {
383
0
  cb->callout_number = 0;
384
0
  cb->callout_string_offset = GET(code, 1 + 3*LINK_SIZE + extracode);
385
0
  cb->callout_string = code + (1 + 4*LINK_SIZE + extracode) + 1;
386
0
  cb->callout_string_length = *lengthptr - (1 + 4*LINK_SIZE) - 2;
387
0
  }
388
389
0
return (mb->callout)(cb, mb->callout_data);
390
0
}
391
392
393
394
/*************************************************
395
*         Expand local workspace memory          *
396
*************************************************/
397
398
/* This function is called when internal_dfa_match() is about to be called
399
recursively and there is insufficient working space left in the current
400
workspace block. If there's an existing next block, use it; otherwise get a new
401
block unless the heap limit is reached.
402
403
Arguments:
404
  rwsptr     pointer to block pointer (updated)
405
  ovecsize   space needed for an ovector
406
  mb         the match block
407
408
Returns:     0 rwsptr has been updated
409
            !0 an error code
410
*/
411
412
static int
413
more_workspace(RWS_anchor **rwsptr, unsigned int ovecsize, dfa_match_block *mb)
414
0
{
415
0
RWS_anchor *rws = *rwsptr;
416
0
RWS_anchor *new;
417
418
0
if (rws->next != NULL)
419
0
  {
420
0
  new = rws->next;
421
0
  }
422
423
/* Sizes in the RWS_anchor blocks are in units of sizeof(int), but
424
mb->heap_limit and mb->heap_used are in kibibytes. Play carefully, to avoid
425
overflow. */
426
427
0
else
428
0
  {
429
0
  uint32_t newsize = (rws->size >= UINT32_MAX/2)? UINT32_MAX/2 : rws->size * 2;
430
0
  uint32_t newsizeK = newsize/(1024/sizeof(int));
431
432
0
  if (newsizeK + mb->heap_used > mb->heap_limit)
433
0
    newsizeK = (uint32_t)(mb->heap_limit - mb->heap_used);
434
0
  newsize = newsizeK*(1024/sizeof(int));
435
436
0
  if (newsize < RWS_RSIZE + ovecsize + RWS_ANCHOR_SIZE)
437
0
    return PCRE2_ERROR_HEAPLIMIT;
438
0
  new = mb->memctl.malloc(newsize*sizeof(int), mb->memctl.memory_data);
439
0
  if (new == NULL) return PCRE2_ERROR_NOMEMORY;
440
0
  mb->heap_used += newsizeK;
441
0
  new->next = NULL;
442
0
  new->size = newsize;
443
0
  rws->next = new;
444
0
  }
445
446
0
new->free = new->size - RWS_ANCHOR_SIZE;
447
0
*rwsptr = new;
448
0
return 0;
449
0
}
450
451
452
453
/*************************************************
454
*     Match a Regular Expression - DFA engine    *
455
*************************************************/
456
457
/* This internal function applies a compiled pattern to a subject string,
458
starting at a given point, using a DFA engine. This function is called from the
459
external one, possibly multiple times if the pattern is not anchored. The
460
function calls itself recursively for some kinds of subpattern.
461
462
Arguments:
463
  mb                the match_data block with fixed information
464
  this_start_code   the opening bracket of this subexpression's code
465
  current_subject   where we currently are in the subject string
466
  start_offset      start offset in the subject string
467
  offsets           vector to contain the matching string offsets
468
  offsetcount       size of same
469
  workspace         vector of workspace
470
  wscount           size of same
471
  rlevel            function call recursion level
472
473
Returns:            > 0 => number of match offset pairs placed in offsets
474
                    = 0 => offsets overflowed; longest matches are present
475
                     -1 => failed to match
476
                   < -1 => some kind of unexpected problem
477
478
The following macros are used for adding states to the two state vectors (one
479
for the current character, one for the following character). */
480
481
#define ADD_ACTIVE(x,y) \
482
0
  if (active_count++ < wscount) \
483
0
    { \
484
0
    next_active_state->offset = (x); \
485
0
    next_active_state->count  = (y); \
486
0
    next_active_state++; \
487
0
    } \
488
0
  else return PCRE2_ERROR_DFA_WSSIZE
489
490
#define ADD_ACTIVE_DATA(x,y,z) \
491
  if (active_count++ < wscount) \
492
    { \
493
    next_active_state->offset = (x); \
494
    next_active_state->count  = (y); \
495
    next_active_state->data   = (z); \
496
    next_active_state++; \
497
    } \
498
  else return PCRE2_ERROR_DFA_WSSIZE
499
500
#define ADD_NEW(x,y) \
501
0
  if (new_count++ < wscount) \
502
0
    { \
503
0
    next_new_state->offset = (x); \
504
0
    next_new_state->count  = (y); \
505
0
    next_new_state++; \
506
0
    } \
507
0
  else return PCRE2_ERROR_DFA_WSSIZE
508
509
#define ADD_NEW_DATA(x,y,z) \
510
0
  if (new_count++ < wscount) \
511
0
    { \
512
0
    next_new_state->offset = (x); \
513
0
    next_new_state->count  = (y); \
514
0
    next_new_state->data   = (z); \
515
0
    next_new_state++; \
516
0
    } \
517
0
  else return PCRE2_ERROR_DFA_WSSIZE
518
519
/* And now, here is the code */
520
521
static int
522
internal_dfa_match(
523
  dfa_match_block *mb,
524
  PCRE2_SPTR this_start_code,
525
  PCRE2_SPTR current_subject,
526
  PCRE2_SIZE start_offset,
527
  PCRE2_SIZE *offsets,
528
  uint32_t offsetcount,
529
  int *workspace,
530
  int wscount,
531
  uint32_t rlevel,
532
  int *RWS)
533
0
{
534
0
stateblock *active_states, *new_states, *temp_states;
535
0
stateblock *next_active_state, *next_new_state;
536
0
const uint8_t *ctypes, *lcc, *fcc;
537
0
PCRE2_SPTR ptr;
538
0
PCRE2_SPTR end_code;
539
0
dfa_recursion_info new_recursive;
540
0
int active_count, new_count, match_count;
541
542
/* Some fields in the mb block are frequently referenced, so we load them into
543
independent variables in the hope that this will perform better. */
544
545
0
PCRE2_SPTR start_subject = mb->start_subject;
546
0
PCRE2_SPTR end_subject = mb->end_subject;
547
0
PCRE2_SPTR start_code = mb->start_code;
548
549
0
#ifdef SUPPORT_UNICODE
550
0
BOOL utf = (mb->poptions & PCRE2_UTF) != 0;
551
0
BOOL utf_or_ucp = utf || (mb->poptions & PCRE2_UCP) != 0;
552
#else
553
BOOL utf = FALSE;
554
#endif
555
556
0
BOOL reset_could_continue = FALSE;
557
558
0
if (mb->match_call_count++ >= mb->match_limit) return PCRE2_ERROR_MATCHLIMIT;
559
0
if (rlevel++ > mb->match_limit_depth) return PCRE2_ERROR_DEPTHLIMIT;
560
0
offsetcount &= (uint32_t)(-2);  /* Round down */
561
562
0
wscount -= 2;
563
0
wscount = (wscount - (wscount % (INTS_PER_STATEBLOCK * 2))) /
564
0
          (2 * INTS_PER_STATEBLOCK);
565
566
0
ctypes = mb->tables + ctypes_offset;
567
0
lcc = mb->tables + lcc_offset;
568
0
fcc = mb->tables + fcc_offset;
569
570
0
match_count = PCRE2_ERROR_NOMATCH;   /* A negative number */
571
572
0
active_states = (stateblock *)(workspace + 2);
573
0
next_new_state = new_states = active_states + wscount;
574
0
new_count = 0;
575
576
/* The first thing in any (sub) pattern is a bracket of some sort. Push all
577
the alternative states onto the list, and find out where the end is. This
578
makes is possible to use this function recursively, when we want to stop at a
579
matching internal ket rather than at the end.
580
581
If we are dealing with a backward assertion we have to find out the maximum
582
amount to move back, and set up each alternative appropriately. */
583
584
0
if (*this_start_code == OP_ASSERTBACK || *this_start_code == OP_ASSERTBACK_NOT)
585
0
  {
586
0
  size_t max_back = 0;
587
0
  size_t gone_back;
588
589
0
  end_code = this_start_code;
590
0
  do
591
0
    {
592
0
    size_t back = (size_t)GET(end_code, 2+LINK_SIZE);
593
0
    if (back > max_back) max_back = back;
594
0
    end_code += GET(end_code, 1);
595
0
    }
596
0
  while (*end_code == OP_ALT);
597
598
  /* If we can't go back the amount required for the longest lookbehind
599
  pattern, go back as far as we can; some alternatives may still be viable. */
600
601
0
#ifdef SUPPORT_UNICODE
602
  /* In character mode we have to step back character by character */
603
604
0
  if (utf)
605
0
    {
606
0
    for (gone_back = 0; gone_back < max_back; gone_back++)
607
0
      {
608
0
      if (current_subject <= start_subject) break;
609
0
      current_subject--;
610
0
      ACROSSCHAR(current_subject > start_subject, current_subject,
611
0
        current_subject--);
612
0
      }
613
0
    }
614
0
  else
615
0
#endif
616
617
  /* In byte-mode we can do this quickly. */
618
619
0
    {
620
0
    size_t current_offset = (size_t)(current_subject - start_subject);
621
0
    gone_back = (current_offset < max_back)? current_offset : max_back;
622
0
    current_subject -= gone_back;
623
0
    }
624
625
  /* Save the earliest consulted character */
626
627
0
  if (current_subject < mb->start_used_ptr)
628
0
    mb->start_used_ptr = current_subject;
629
630
  /* Now we can process the individual branches. There will be an OP_REVERSE at
631
  the start of each branch, except when the length of the branch is zero. */
632
633
0
  end_code = this_start_code;
634
0
  do
635
0
    {
636
0
    uint32_t revlen = (end_code[1+LINK_SIZE] == OP_REVERSE)? 1 + LINK_SIZE : 0;
637
0
    size_t back = (revlen == 0)? 0 : (size_t)GET(end_code, 2+LINK_SIZE);
638
0
    if (back <= gone_back)
639
0
      {
640
0
      int bstate = (int)(end_code - start_code + 1 + LINK_SIZE + revlen);
641
0
      ADD_NEW_DATA(-bstate, 0, (int)(gone_back - back));
642
0
      }
643
0
    end_code += GET(end_code, 1);
644
0
    }
645
0
  while (*end_code == OP_ALT);
646
0
 }
647
648
/* This is the code for a "normal" subpattern (not a backward assertion). The
649
start of a whole pattern is always one of these. If we are at the top level,
650
we may be asked to restart matching from the same point that we reached for a
651
previous partial match. We still have to scan through the top-level branches to
652
find the end state. */
653
654
0
else
655
0
  {
656
0
  end_code = this_start_code;
657
658
  /* Restarting */
659
660
0
  if (rlevel == 1 && (mb->moptions & PCRE2_DFA_RESTART) != 0)
661
0
    {
662
0
    do { end_code += GET(end_code, 1); } while (*end_code == OP_ALT);
663
0
    new_count = workspace[1];
664
0
    if (!workspace[0])
665
0
      memcpy(new_states, active_states, (size_t)new_count * sizeof(stateblock));
666
0
    }
667
668
  /* Not restarting */
669
670
0
  else
671
0
    {
672
0
    int length = 1 + LINK_SIZE +
673
0
      ((*this_start_code == OP_CBRA || *this_start_code == OP_SCBRA ||
674
0
        *this_start_code == OP_CBRAPOS || *this_start_code == OP_SCBRAPOS)
675
0
        ? IMM2_SIZE:0);
676
0
    do
677
0
      {
678
0
      ADD_NEW((int)(end_code - start_code + length), 0);
679
0
      end_code += GET(end_code, 1);
680
0
      length = 1 + LINK_SIZE;
681
0
      }
682
0
    while (*end_code == OP_ALT);
683
0
    }
684
0
  }
685
686
0
workspace[0] = 0;    /* Bit indicating which vector is current */
687
688
/* Loop for scanning the subject */
689
690
0
ptr = current_subject;
691
0
for (;;)
692
0
  {
693
0
  int i, j;
694
0
  int clen, dlen;
695
0
  uint32_t c, d;
696
0
  int forced_fail = 0;
697
0
  BOOL partial_newline = FALSE;
698
0
  BOOL could_continue = reset_could_continue;
699
0
  reset_could_continue = FALSE;
700
701
0
  if (ptr > mb->last_used_ptr) mb->last_used_ptr = ptr;
702
703
  /* Make the new state list into the active state list and empty the
704
  new state list. */
705
706
0
  temp_states = active_states;
707
0
  active_states = new_states;
708
0
  new_states = temp_states;
709
0
  active_count = new_count;
710
0
  new_count = 0;
711
712
0
  workspace[0] ^= 1;              /* Remember for the restarting feature */
713
0
  workspace[1] = active_count;
714
715
  /* Set the pointers for adding new states */
716
717
0
  next_active_state = active_states + active_count;
718
0
  next_new_state = new_states;
719
720
  /* Load the current character from the subject outside the loop, as many
721
  different states may want to look at it, and we assume that at least one
722
  will. */
723
724
0
  if (ptr < end_subject)
725
0
    {
726
0
    clen = 1;        /* Number of data items in the character */
727
0
#ifdef SUPPORT_UNICODE
728
0
    GETCHARLENTEST(c, ptr, clen);
729
#else
730
    c = *ptr;
731
#endif  /* SUPPORT_UNICODE */
732
0
    }
733
0
  else
734
0
    {
735
0
    clen = 0;        /* This indicates the end of the subject */
736
0
    c = NOTACHAR;    /* This value should never actually be used */
737
0
    }
738
739
  /* Scan up the active states and act on each one. The result of an action
740
  may be to add more states to the currently active list (e.g. on hitting a
741
  parenthesis) or it may be to put states on the new list, for considering
742
  when we move the character pointer on. */
743
744
0
  for (i = 0; i < active_count; i++)
745
0
    {
746
0
    stateblock *current_state = active_states + i;
747
0
    BOOL caseless = FALSE;
748
0
    PCRE2_SPTR code;
749
0
    uint32_t codevalue;
750
0
    int state_offset = current_state->offset;
751
0
    int rrc;
752
0
    int count;
753
754
    /* A negative offset is a special case meaning "hold off going to this
755
    (negated) state until the number of characters in the data field have
756
    been skipped". If the could_continue flag was passed over from a previous
757
    state, arrange for it to passed on. */
758
759
0
    if (state_offset < 0)
760
0
      {
761
0
      if (current_state->data > 0)
762
0
        {
763
0
        ADD_NEW_DATA(state_offset, current_state->count,
764
0
          current_state->data - 1);
765
0
        if (could_continue) reset_could_continue = TRUE;
766
0
        continue;
767
0
        }
768
0
      else
769
0
        {
770
0
        current_state->offset = state_offset = -state_offset;
771
0
        }
772
0
      }
773
774
    /* Check for a duplicate state with the same count, and skip if found.
775
    See the note at the head of this module about the possibility of improving
776
    performance here. */
777
778
0
    for (j = 0; j < i; j++)
779
0
      {
780
0
      if (active_states[j].offset == state_offset &&
781
0
          active_states[j].count == current_state->count)
782
0
        goto NEXT_ACTIVE_STATE;
783
0
      }
784
785
    /* The state offset is the offset to the opcode */
786
787
0
    code = start_code + state_offset;
788
0
    codevalue = *code;
789
790
    /* If this opcode inspects a character, but we are at the end of the
791
    subject, remember the fact for use when testing for a partial match. */
792
793
0
    if (clen == 0 && poptable[codevalue] != 0)
794
0
      could_continue = TRUE;
795
796
    /* If this opcode is followed by an inline character, load it. It is
797
    tempting to test for the presence of a subject character here, but that
798
    is wrong, because sometimes zero repetitions of the subject are
799
    permitted.
800
801
    We also use this mechanism for opcodes such as OP_TYPEPLUS that take an
802
    argument that is not a data character - but is always one byte long because
803
    the values are small. We have to take special action to deal with  \P, \p,
804
    \H, \h, \V, \v and \X in this case. To keep the other cases fast, convert
805
    these ones to new opcodes. */
806
807
0
    if (coptable[codevalue] > 0)
808
0
      {
809
0
      dlen = 1;
810
0
#ifdef SUPPORT_UNICODE
811
0
      if (utf) { GETCHARLEN(d, (code + coptable[codevalue]), dlen); } else
812
0
#endif  /* SUPPORT_UNICODE */
813
0
      d = code[coptable[codevalue]];
814
0
      if (codevalue >= OP_TYPESTAR)
815
0
        {
816
0
        switch(d)
817
0
          {
818
0
          case OP_ANYBYTE: return PCRE2_ERROR_DFA_UITEM;
819
0
          case OP_NOTPROP:
820
0
          case OP_PROP: codevalue += OP_PROP_EXTRA; break;
821
0
          case OP_ANYNL: codevalue += OP_ANYNL_EXTRA; break;
822
0
          case OP_EXTUNI: codevalue += OP_EXTUNI_EXTRA; break;
823
0
          case OP_NOT_HSPACE:
824
0
          case OP_HSPACE: codevalue += OP_HSPACE_EXTRA; break;
825
0
          case OP_NOT_VSPACE:
826
0
          case OP_VSPACE: codevalue += OP_VSPACE_EXTRA; break;
827
0
          default: break;
828
0
          }
829
0
        }
830
0
      }
831
0
    else
832
0
      {
833
0
      dlen = 0;         /* Not strictly necessary, but compilers moan */
834
0
      d = NOTACHAR;     /* if these variables are not set. */
835
0
      }
836
837
838
    /* Now process the individual opcodes */
839
840
0
    switch (codevalue)
841
0
      {
842
/* ========================================================================== */
843
      /* These cases are never obeyed. This is a fudge that causes a compile-
844
      time error if the vectors coptable or poptable, which are indexed by
845
      opcode, are not the correct length. It seems to be the only way to do
846
      such a check at compile time, as the sizeof() operator does not work
847
      in the C preprocessor. */
848
849
0
      case OP_TABLE_LENGTH:
850
0
      case OP_TABLE_LENGTH +
851
0
        ((sizeof(coptable) == OP_TABLE_LENGTH) &&
852
0
         (sizeof(poptable) == OP_TABLE_LENGTH)):
853
0
      return 0;
854
855
/* ========================================================================== */
856
      /* Reached a closing bracket. If not at the end of the pattern, carry
857
      on with the next opcode. For repeating opcodes, also add the repeat
858
      state. Note that KETRPOS will always be encountered at the end of the
859
      subpattern, because the possessive subpattern repeats are always handled
860
      using recursive calls. Thus, it never adds any new states.
861
862
      At the end of the (sub)pattern, unless we have an empty string and
863
      PCRE2_NOTEMPTY is set, or PCRE2_NOTEMPTY_ATSTART is set and we are at the
864
      start of the subject, save the match data, shifting up all previous
865
      matches so we always have the longest first. */
866
867
0
      case OP_KET:
868
0
      case OP_KETRMIN:
869
0
      case OP_KETRMAX:
870
0
      case OP_KETRPOS:
871
0
      if (code != end_code)
872
0
        {
873
0
        ADD_ACTIVE(state_offset + 1 + LINK_SIZE, 0);
874
0
        if (codevalue != OP_KET)
875
0
          {
876
0
          ADD_ACTIVE(state_offset - (int)GET(code, 1), 0);
877
0
          }
878
0
        }
879
0
      else
880
0
        {
881
0
        if (ptr > current_subject ||
882
0
            ((mb->moptions & PCRE2_NOTEMPTY) == 0 &&
883
0
              ((mb->moptions & PCRE2_NOTEMPTY_ATSTART) == 0 ||
884
0
                current_subject > start_subject + mb->start_offset)))
885
0
          {
886
0
          if (match_count < 0) match_count = (offsetcount >= 2)? 1 : 0;
887
0
            else if (match_count > 0 && ++match_count * 2 > (int)offsetcount)
888
0
              match_count = 0;
889
0
          count = ((match_count == 0)? (int)offsetcount : match_count * 2) - 2;
890
0
          if (count > 0) (void)memmove(offsets + 2, offsets,
891
0
            (size_t)count * sizeof(PCRE2_SIZE));
892
0
          if (offsetcount >= 2)
893
0
            {
894
0
            offsets[0] = (PCRE2_SIZE)(current_subject - start_subject);
895
0
            offsets[1] = (PCRE2_SIZE)(ptr - start_subject);
896
0
            }
897
0
          if ((mb->moptions & PCRE2_DFA_SHORTEST) != 0) return match_count;
898
0
          }
899
0
        }
900
0
      break;
901
902
/* ========================================================================== */
903
      /* These opcodes add to the current list of states without looking
904
      at the current character. */
905
906
      /*-----------------------------------------------------------------*/
907
0
      case OP_ALT:
908
0
      do { code += GET(code, 1); } while (*code == OP_ALT);
909
0
      ADD_ACTIVE((int)(code - start_code), 0);
910
0
      break;
911
912
      /*-----------------------------------------------------------------*/
913
0
      case OP_BRA:
914
0
      case OP_SBRA:
915
0
      do
916
0
        {
917
0
        ADD_ACTIVE((int)(code - start_code + 1 + LINK_SIZE), 0);
918
0
        code += GET(code, 1);
919
0
        }
920
0
      while (*code == OP_ALT);
921
0
      break;
922
923
      /*-----------------------------------------------------------------*/
924
0
      case OP_CBRA:
925
0
      case OP_SCBRA:
926
0
      ADD_ACTIVE((int)(code - start_code + 1 + LINK_SIZE + IMM2_SIZE),  0);
927
0
      code += GET(code, 1);
928
0
      while (*code == OP_ALT)
929
0
        {
930
0
        ADD_ACTIVE((int)(code - start_code + 1 + LINK_SIZE),  0);
931
0
        code += GET(code, 1);
932
0
        }
933
0
      break;
934
935
      /*-----------------------------------------------------------------*/
936
0
      case OP_BRAZERO:
937
0
      case OP_BRAMINZERO:
938
0
      ADD_ACTIVE(state_offset + 1, 0);
939
0
      code += 1 + GET(code, 2);
940
0
      while (*code == OP_ALT) code += GET(code, 1);
941
0
      ADD_ACTIVE((int)(code - start_code + 1 + LINK_SIZE), 0);
942
0
      break;
943
944
      /*-----------------------------------------------------------------*/
945
0
      case OP_SKIPZERO:
946
0
      code += 1 + GET(code, 2);
947
0
      while (*code == OP_ALT) code += GET(code, 1);
948
0
      ADD_ACTIVE((int)(code - start_code + 1 + LINK_SIZE), 0);
949
0
      break;
950
951
      /*-----------------------------------------------------------------*/
952
0
      case OP_CIRC:
953
0
      if (ptr == start_subject && (mb->moptions & PCRE2_NOTBOL) == 0)
954
0
        { ADD_ACTIVE(state_offset + 1, 0); }
955
0
      break;
956
957
      /*-----------------------------------------------------------------*/
958
0
      case OP_CIRCM:
959
0
      if ((ptr == start_subject && (mb->moptions & PCRE2_NOTBOL) == 0) ||
960
0
          ((ptr != end_subject || (mb->poptions & PCRE2_ALT_CIRCUMFLEX) != 0 )
961
0
            && WAS_NEWLINE(ptr)))
962
0
        { ADD_ACTIVE(state_offset + 1, 0); }
963
0
      break;
964
965
      /*-----------------------------------------------------------------*/
966
0
      case OP_EOD:
967
0
      if (ptr >= end_subject)
968
0
        {
969
0
        if ((mb->moptions & PCRE2_PARTIAL_HARD) != 0)
970
0
          return PCRE2_ERROR_PARTIAL;
971
0
        else { ADD_ACTIVE(state_offset + 1, 0); }
972
0
        }
973
0
      break;
974
975
      /*-----------------------------------------------------------------*/
976
0
      case OP_SOD:
977
0
      if (ptr == start_subject) { ADD_ACTIVE(state_offset + 1, 0); }
978
0
      break;
979
980
      /*-----------------------------------------------------------------*/
981
0
      case OP_SOM:
982
0
      if (ptr == start_subject + start_offset) { ADD_ACTIVE(state_offset + 1, 0); }
983
0
      break;
984
985
986
/* ========================================================================== */
987
      /* These opcodes inspect the next subject character, and sometimes
988
      the previous one as well, but do not have an argument. The variable
989
      clen contains the length of the current character and is zero if we are
990
      at the end of the subject. */
991
992
      /*-----------------------------------------------------------------*/
993
0
      case OP_ANY:
994
0
      if (clen > 0 && !IS_NEWLINE(ptr))
995
0
        {
996
0
        if (ptr + 1 >= mb->end_subject &&
997
0
            (mb->moptions & (PCRE2_PARTIAL_HARD)) != 0 &&
998
0
            NLBLOCK->nltype == NLTYPE_FIXED &&
999
0
            NLBLOCK->nllen == 2 &&
1000
0
            c == NLBLOCK->nl[0])
1001
0
          {
1002
0
          could_continue = partial_newline = TRUE;
1003
0
          }
1004
0
        else
1005
0
          {
1006
0
          ADD_NEW(state_offset + 1, 0);
1007
0
          }
1008
0
        }
1009
0
      break;
1010
1011
      /*-----------------------------------------------------------------*/
1012
0
      case OP_ALLANY:
1013
0
      if (clen > 0)
1014
0
        { ADD_NEW(state_offset + 1, 0); }
1015
0
      break;
1016
1017
      /*-----------------------------------------------------------------*/
1018
0
      case OP_EODN:
1019
0
      if (clen == 0 || (IS_NEWLINE(ptr) && ptr == end_subject - mb->nllen))
1020
0
        {
1021
0
        if ((mb->moptions & PCRE2_PARTIAL_HARD) != 0)
1022
0
          return PCRE2_ERROR_PARTIAL;
1023
0
        ADD_ACTIVE(state_offset + 1, 0);
1024
0
        }
1025
0
      break;
1026
1027
      /*-----------------------------------------------------------------*/
1028
0
      case OP_DOLL:
1029
0
      if ((mb->moptions & PCRE2_NOTEOL) == 0)
1030
0
        {
1031
0
        if (clen == 0 && (mb->moptions & PCRE2_PARTIAL_HARD) != 0)
1032
0
          could_continue = TRUE;
1033
0
        else if (clen == 0 ||
1034
0
            ((mb->poptions & PCRE2_DOLLAR_ENDONLY) == 0 && IS_NEWLINE(ptr) &&
1035
0
               (ptr == end_subject - mb->nllen)
1036
0
            ))
1037
0
          { ADD_ACTIVE(state_offset + 1, 0); }
1038
0
        else if (ptr + 1 >= mb->end_subject &&
1039
0
                 (mb->moptions & (PCRE2_PARTIAL_HARD|PCRE2_PARTIAL_SOFT)) != 0 &&
1040
0
                 NLBLOCK->nltype == NLTYPE_FIXED &&
1041
0
                 NLBLOCK->nllen == 2 &&
1042
0
                 c == NLBLOCK->nl[0])
1043
0
          {
1044
0
          if ((mb->moptions & PCRE2_PARTIAL_HARD) != 0)
1045
0
            {
1046
0
            reset_could_continue = TRUE;
1047
0
            ADD_NEW_DATA(-(state_offset + 1), 0, 1);
1048
0
            }
1049
0
          else could_continue = partial_newline = TRUE;
1050
0
          }
1051
0
        }
1052
0
      break;
1053
1054
      /*-----------------------------------------------------------------*/
1055
0
      case OP_DOLLM:
1056
0
      if ((mb->moptions & PCRE2_NOTEOL) == 0)
1057
0
        {
1058
0
        if (clen == 0 && (mb->moptions & PCRE2_PARTIAL_HARD) != 0)
1059
0
          could_continue = TRUE;
1060
0
        else if (clen == 0 ||
1061
0
            ((mb->poptions & PCRE2_DOLLAR_ENDONLY) == 0 && IS_NEWLINE(ptr)))
1062
0
          { ADD_ACTIVE(state_offset + 1, 0); }
1063
0
        else if (ptr + 1 >= mb->end_subject &&
1064
0
                 (mb->moptions & (PCRE2_PARTIAL_HARD|PCRE2_PARTIAL_SOFT)) != 0 &&
1065
0
                 NLBLOCK->nltype == NLTYPE_FIXED &&
1066
0
                 NLBLOCK->nllen == 2 &&
1067
0
                 c == NLBLOCK->nl[0])
1068
0
          {
1069
0
          if ((mb->moptions & PCRE2_PARTIAL_HARD) != 0)
1070
0
            {
1071
0
            reset_could_continue = TRUE;
1072
0
            ADD_NEW_DATA(-(state_offset + 1), 0, 1);
1073
0
            }
1074
0
          else could_continue = partial_newline = TRUE;
1075
0
          }
1076
0
        }
1077
0
      else if (IS_NEWLINE(ptr))
1078
0
        { ADD_ACTIVE(state_offset + 1, 0); }
1079
0
      break;
1080
1081
      /*-----------------------------------------------------------------*/
1082
1083
0
      case OP_DIGIT:
1084
0
      case OP_WHITESPACE:
1085
0
      case OP_WORDCHAR:
1086
0
      if (clen > 0 && c < 256 &&
1087
0
            ((ctypes[c] & toptable1[codevalue]) ^ toptable2[codevalue]) != 0)
1088
0
        { ADD_NEW(state_offset + 1, 0); }
1089
0
      break;
1090
1091
      /*-----------------------------------------------------------------*/
1092
0
      case OP_NOT_DIGIT:
1093
0
      case OP_NOT_WHITESPACE:
1094
0
      case OP_NOT_WORDCHAR:
1095
0
      if (clen > 0 && (c >= 256 ||
1096
0
            ((ctypes[c] & toptable1[codevalue]) ^ toptable2[codevalue]) != 0))
1097
0
        { ADD_NEW(state_offset + 1, 0); }
1098
0
      break;
1099
1100
      /*-----------------------------------------------------------------*/
1101
0
      case OP_WORD_BOUNDARY:
1102
0
      case OP_NOT_WORD_BOUNDARY:
1103
0
        {
1104
0
        int left_word, right_word;
1105
1106
0
        if (ptr > start_subject)
1107
0
          {
1108
0
          PCRE2_SPTR temp = ptr - 1;
1109
0
          if (temp < mb->start_used_ptr) mb->start_used_ptr = temp;
1110
0
#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32
1111
0
          if (utf) { BACKCHAR(temp); }
1112
0
#endif
1113
0
          GETCHARTEST(d, temp);
1114
0
#ifdef SUPPORT_UNICODE
1115
0
          if ((mb->poptions & PCRE2_UCP) != 0)
1116
0
            {
1117
0
            if (d == '_') left_word = TRUE; else
1118
0
              {
1119
0
              uint32_t cat = UCD_CATEGORY(d);
1120
0
              left_word = (cat == ucp_L || cat == ucp_N);
1121
0
              }
1122
0
            }
1123
0
          else
1124
0
#endif
1125
0
          left_word = d < 256 && (ctypes[d] & ctype_word) != 0;
1126
0
          }
1127
0
        else left_word = FALSE;
1128
1129
0
        if (clen > 0)
1130
0
          {
1131
0
          if (ptr >= mb->last_used_ptr)
1132
0
            {
1133
0
            PCRE2_SPTR temp = ptr + 1;
1134
0
#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32
1135
0
            if (utf) { FORWARDCHARTEST(temp, mb->end_subject); }
1136
0
#endif
1137
0
            mb->last_used_ptr = temp;
1138
0
            }
1139
0
#ifdef SUPPORT_UNICODE
1140
0
          if ((mb->poptions & PCRE2_UCP) != 0)
1141
0
            {
1142
0
            if (c == '_') right_word = TRUE; else
1143
0
              {
1144
0
              uint32_t cat = UCD_CATEGORY(c);
1145
0
              right_word = (cat == ucp_L || cat == ucp_N);
1146
0
              }
1147
0
            }
1148
0
          else
1149
0
#endif
1150
0
          right_word = c < 256 && (ctypes[c] & ctype_word) != 0;
1151
0
          }
1152
0
        else right_word = FALSE;
1153
1154
0
        if ((left_word == right_word) == (codevalue == OP_NOT_WORD_BOUNDARY))
1155
0
          { ADD_ACTIVE(state_offset + 1, 0); }
1156
0
        }
1157
0
      break;
1158
1159
1160
      /*-----------------------------------------------------------------*/
1161
      /* Check the next character by Unicode property. We will get here only
1162
      if the support is in the binary; otherwise a compile-time error occurs.
1163
      */
1164
1165
0
#ifdef SUPPORT_UNICODE
1166
0
      case OP_PROP:
1167
0
      case OP_NOTPROP:
1168
0
      if (clen > 0)
1169
0
        {
1170
0
        BOOL OK;
1171
0
        const uint32_t *cp;
1172
0
        const ucd_record * prop = GET_UCD(c);
1173
0
        switch(code[1])
1174
0
          {
1175
0
          case PT_ANY:
1176
0
          OK = TRUE;
1177
0
          break;
1178
1179
0
          case PT_LAMP:
1180
0
          OK = prop->chartype == ucp_Lu || prop->chartype == ucp_Ll ||
1181
0
               prop->chartype == ucp_Lt;
1182
0
          break;
1183
1184
0
          case PT_GC:
1185
0
          OK = PRIV(ucp_gentype)[prop->chartype] == code[2];
1186
0
          break;
1187
1188
0
          case PT_PC:
1189
0
          OK = prop->chartype == code[2];
1190
0
          break;
1191
1192
0
          case PT_SC:
1193
0
          OK = prop->script == code[2];
1194
0
          break;
1195
1196
0
          case PT_SCX:
1197
0
          OK = (prop->script == code[2] ||
1198
0
                MAPBIT(PRIV(ucd_script_sets) + UCD_SCRIPTX_PROP(prop), code[2]) != 0);
1199
0
          break;
1200
1201
          /* These are specials for combination cases. */
1202
1203
0
          case PT_ALNUM:
1204
0
          OK = PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
1205
0
               PRIV(ucp_gentype)[prop->chartype] == ucp_N;
1206
0
          break;
1207
1208
          /* Perl space used to exclude VT, but from Perl 5.18 it is included,
1209
          which means that Perl space and POSIX space are now identical. PCRE
1210
          was changed at release 8.34. */
1211
1212
0
          case PT_SPACE:    /* Perl space */
1213
0
          case PT_PXSPACE:  /* POSIX space */
1214
0
          switch(c)
1215
0
            {
1216
0
            HSPACE_CASES:
1217
0
            VSPACE_CASES:
1218
0
            OK = TRUE;
1219
0
            break;
1220
1221
0
            default:
1222
0
            OK = PRIV(ucp_gentype)[prop->chartype] == ucp_Z;
1223
0
            break;
1224
0
            }
1225
0
          break;
1226
1227
0
          case PT_WORD:
1228
0
          OK = PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
1229
0
               PRIV(ucp_gentype)[prop->chartype] == ucp_N ||
1230
0
               c == CHAR_UNDERSCORE;
1231
0
          break;
1232
1233
0
          case PT_CLIST:
1234
0
          cp = PRIV(ucd_caseless_sets) + code[2];
1235
0
          for (;;)
1236
0
            {
1237
0
            if (c < *cp) { OK = FALSE; break; }
1238
0
            if (c == *cp++) { OK = TRUE; break; }
1239
0
            }
1240
0
          break;
1241
1242
0
          case PT_UCNC:
1243
0
          OK = c == CHAR_DOLLAR_SIGN || c == CHAR_COMMERCIAL_AT ||
1244
0
               c == CHAR_GRAVE_ACCENT || (c >= 0xa0 && c <= 0xd7ff) ||
1245
0
               c >= 0xe000;
1246
0
          break;
1247
1248
0
          case PT_BIDICL:
1249
0
          OK = UCD_BIDICLASS(c) == code[2];
1250
0
          break;
1251
1252
0
          case PT_BOOL:
1253
0
          OK = MAPBIT(PRIV(ucd_boolprop_sets) +
1254
0
            UCD_BPROPS_PROP(prop), code[2]) != 0;
1255
0
          break;
1256
1257
          /* Should never occur, but keep compilers from grumbling. */
1258
1259
0
          default:
1260
0
          OK = codevalue != OP_PROP;
1261
0
          break;
1262
0
          }
1263
1264
0
        if (OK == (codevalue == OP_PROP)) { ADD_NEW(state_offset + 3, 0); }
1265
0
        }
1266
0
      break;
1267
0
#endif
1268
1269
1270
1271
/* ========================================================================== */
1272
      /* These opcodes likewise inspect the subject character, but have an
1273
      argument that is not a data character. It is one of these opcodes:
1274
      OP_ANY, OP_ALLANY, OP_DIGIT, OP_NOT_DIGIT, OP_WHITESPACE, OP_NOT_SPACE,
1275
      OP_WORDCHAR, OP_NOT_WORDCHAR. The value is loaded into d. */
1276
1277
0
      case OP_TYPEPLUS:
1278
0
      case OP_TYPEMINPLUS:
1279
0
      case OP_TYPEPOSPLUS:
1280
0
      count = current_state->count;  /* Already matched */
1281
0
      if (count > 0) { ADD_ACTIVE(state_offset + 2, 0); }
1282
0
      if (clen > 0)
1283
0
        {
1284
0
        if (d == OP_ANY && ptr + 1 >= mb->end_subject &&
1285
0
            (mb->moptions & (PCRE2_PARTIAL_HARD)) != 0 &&
1286
0
            NLBLOCK->nltype == NLTYPE_FIXED &&
1287
0
            NLBLOCK->nllen == 2 &&
1288
0
            c == NLBLOCK->nl[0])
1289
0
          {
1290
0
          could_continue = partial_newline = TRUE;
1291
0
          }
1292
0
        else if ((c >= 256 && d != OP_DIGIT && d != OP_WHITESPACE && d != OP_WORDCHAR) ||
1293
0
            (c < 256 &&
1294
0
              (d != OP_ANY || !IS_NEWLINE(ptr)) &&
1295
0
              ((ctypes[c] & toptable1[d]) ^ toptable2[d]) != 0))
1296
0
          {
1297
0
          if (count > 0 && codevalue == OP_TYPEPOSPLUS)
1298
0
            {
1299
0
            active_count--;            /* Remove non-match possibility */
1300
0
            next_active_state--;
1301
0
            }
1302
0
          count++;
1303
0
          ADD_NEW(state_offset, count);
1304
0
          }
1305
0
        }
1306
0
      break;
1307
1308
      /*-----------------------------------------------------------------*/
1309
0
      case OP_TYPEQUERY:
1310
0
      case OP_TYPEMINQUERY:
1311
0
      case OP_TYPEPOSQUERY:
1312
0
      ADD_ACTIVE(state_offset + 2, 0);
1313
0
      if (clen > 0)
1314
0
        {
1315
0
        if (d == OP_ANY && ptr + 1 >= mb->end_subject &&
1316
0
            (mb->moptions & (PCRE2_PARTIAL_HARD)) != 0 &&
1317
0
            NLBLOCK->nltype == NLTYPE_FIXED &&
1318
0
            NLBLOCK->nllen == 2 &&
1319
0
            c == NLBLOCK->nl[0])
1320
0
          {
1321
0
          could_continue = partial_newline = TRUE;
1322
0
          }
1323
0
        else if ((c >= 256 && d != OP_DIGIT && d != OP_WHITESPACE && d != OP_WORDCHAR) ||
1324
0
            (c < 256 &&
1325
0
              (d != OP_ANY || !IS_NEWLINE(ptr)) &&
1326
0
              ((ctypes[c] & toptable1[d]) ^ toptable2[d]) != 0))
1327
0
          {
1328
0
          if (codevalue == OP_TYPEPOSQUERY)
1329
0
            {
1330
0
            active_count--;            /* Remove non-match possibility */
1331
0
            next_active_state--;
1332
0
            }
1333
0
          ADD_NEW(state_offset + 2, 0);
1334
0
          }
1335
0
        }
1336
0
      break;
1337
1338
      /*-----------------------------------------------------------------*/
1339
0
      case OP_TYPESTAR:
1340
0
      case OP_TYPEMINSTAR:
1341
0
      case OP_TYPEPOSSTAR:
1342
0
      ADD_ACTIVE(state_offset + 2, 0);
1343
0
      if (clen > 0)
1344
0
        {
1345
0
        if (d == OP_ANY && ptr + 1 >= mb->end_subject &&
1346
0
            (mb->moptions & (PCRE2_PARTIAL_HARD)) != 0 &&
1347
0
            NLBLOCK->nltype == NLTYPE_FIXED &&
1348
0
            NLBLOCK->nllen == 2 &&
1349
0
            c == NLBLOCK->nl[0])
1350
0
          {
1351
0
          could_continue = partial_newline = TRUE;
1352
0
          }
1353
0
        else if ((c >= 256 && d != OP_DIGIT && d != OP_WHITESPACE && d != OP_WORDCHAR) ||
1354
0
            (c < 256 &&
1355
0
              (d != OP_ANY || !IS_NEWLINE(ptr)) &&
1356
0
              ((ctypes[c] & toptable1[d]) ^ toptable2[d]) != 0))
1357
0
          {
1358
0
          if (codevalue == OP_TYPEPOSSTAR)
1359
0
            {
1360
0
            active_count--;            /* Remove non-match possibility */
1361
0
            next_active_state--;
1362
0
            }
1363
0
          ADD_NEW(state_offset, 0);
1364
0
          }
1365
0
        }
1366
0
      break;
1367
1368
      /*-----------------------------------------------------------------*/
1369
0
      case OP_TYPEEXACT:
1370
0
      count = current_state->count;  /* Number already matched */
1371
0
      if (clen > 0)
1372
0
        {
1373
0
        if (d == OP_ANY && ptr + 1 >= mb->end_subject &&
1374
0
            (mb->moptions & (PCRE2_PARTIAL_HARD)) != 0 &&
1375
0
            NLBLOCK->nltype == NLTYPE_FIXED &&
1376
0
            NLBLOCK->nllen == 2 &&
1377
0
            c == NLBLOCK->nl[0])
1378
0
          {
1379
0
          could_continue = partial_newline = TRUE;
1380
0
          }
1381
0
        else if ((c >= 256 && d != OP_DIGIT && d != OP_WHITESPACE && d != OP_WORDCHAR) ||
1382
0
            (c < 256 &&
1383
0
              (d != OP_ANY || !IS_NEWLINE(ptr)) &&
1384
0
              ((ctypes[c] & toptable1[d]) ^ toptable2[d]) != 0))
1385
0
          {
1386
0
          if (++count >= (int)GET2(code, 1))
1387
0
            { ADD_NEW(state_offset + 1 + IMM2_SIZE + 1, 0); }
1388
0
          else
1389
0
            { ADD_NEW(state_offset, count); }
1390
0
          }
1391
0
        }
1392
0
      break;
1393
1394
      /*-----------------------------------------------------------------*/
1395
0
      case OP_TYPEUPTO:
1396
0
      case OP_TYPEMINUPTO:
1397
0
      case OP_TYPEPOSUPTO:
1398
0
      ADD_ACTIVE(state_offset + 2 + IMM2_SIZE, 0);
1399
0
      count = current_state->count;  /* Number already matched */
1400
0
      if (clen > 0)
1401
0
        {
1402
0
        if (d == OP_ANY && ptr + 1 >= mb->end_subject &&
1403
0
            (mb->moptions & (PCRE2_PARTIAL_HARD)) != 0 &&
1404
0
            NLBLOCK->nltype == NLTYPE_FIXED &&
1405
0
            NLBLOCK->nllen == 2 &&
1406
0
            c == NLBLOCK->nl[0])
1407
0
          {
1408
0
          could_continue = partial_newline = TRUE;
1409
0
          }
1410
0
        else if ((c >= 256 && d != OP_DIGIT && d != OP_WHITESPACE && d != OP_WORDCHAR) ||
1411
0
            (c < 256 &&
1412
0
              (d != OP_ANY || !IS_NEWLINE(ptr)) &&
1413
0
              ((ctypes[c] & toptable1[d]) ^ toptable2[d]) != 0))
1414
0
          {
1415
0
          if (codevalue == OP_TYPEPOSUPTO)
1416
0
            {
1417
0
            active_count--;           /* Remove non-match possibility */
1418
0
            next_active_state--;
1419
0
            }
1420
0
          if (++count >= (int)GET2(code, 1))
1421
0
            { ADD_NEW(state_offset + 2 + IMM2_SIZE, 0); }
1422
0
          else
1423
0
            { ADD_NEW(state_offset, count); }
1424
0
          }
1425
0
        }
1426
0
      break;
1427
1428
/* ========================================================================== */
1429
      /* These are virtual opcodes that are used when something like
1430
      OP_TYPEPLUS has OP_PROP, OP_NOTPROP, OP_ANYNL, or OP_EXTUNI as its
1431
      argument. It keeps the code above fast for the other cases. The argument
1432
      is in the d variable. */
1433
1434
0
#ifdef SUPPORT_UNICODE
1435
0
      case OP_PROP_EXTRA + OP_TYPEPLUS:
1436
0
      case OP_PROP_EXTRA + OP_TYPEMINPLUS:
1437
0
      case OP_PROP_EXTRA + OP_TYPEPOSPLUS:
1438
0
      count = current_state->count;           /* Already matched */
1439
0
      if (count > 0) { ADD_ACTIVE(state_offset + 4, 0); }
1440
0
      if (clen > 0)
1441
0
        {
1442
0
        BOOL OK;
1443
0
        const uint32_t *cp;
1444
0
        const ucd_record * prop = GET_UCD(c);
1445
0
        switch(code[2])
1446
0
          {
1447
0
          case PT_ANY:
1448
0
          OK = TRUE;
1449
0
          break;
1450
1451
0
          case PT_LAMP:
1452
0
          OK = prop->chartype == ucp_Lu || prop->chartype == ucp_Ll ||
1453
0
            prop->chartype == ucp_Lt;
1454
0
          break;
1455
1456
0
          case PT_GC:
1457
0
          OK = PRIV(ucp_gentype)[prop->chartype] == code[3];
1458
0
          break;
1459
1460
0
          case PT_PC:
1461
0
          OK = prop->chartype == code[3];
1462
0
          break;
1463
1464
0
          case PT_SC:
1465
0
          OK = prop->script == code[3];
1466
0
          break;
1467
1468
0
          case PT_SCX:
1469
0
          OK = (prop->script == code[3] ||
1470
0
                MAPBIT(PRIV(ucd_script_sets) + UCD_SCRIPTX_PROP(prop), code[3]) != 0);
1471
0
          break;
1472
1473
          /* These are specials for combination cases. */
1474
1475
0
          case PT_ALNUM:
1476
0
          OK = PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
1477
0
               PRIV(ucp_gentype)[prop->chartype] == ucp_N;
1478
0
          break;
1479
1480
          /* Perl space used to exclude VT, but from Perl 5.18 it is included,
1481
          which means that Perl space and POSIX space are now identical. PCRE
1482
          was changed at release 8.34. */
1483
1484
0
          case PT_SPACE:    /* Perl space */
1485
0
          case PT_PXSPACE:  /* POSIX space */
1486
0
          switch(c)
1487
0
            {
1488
0
            HSPACE_CASES:
1489
0
            VSPACE_CASES:
1490
0
            OK = TRUE;
1491
0
            break;
1492
1493
0
            default:
1494
0
            OK = PRIV(ucp_gentype)[prop->chartype] == ucp_Z;
1495
0
            break;
1496
0
            }
1497
0
          break;
1498
1499
0
          case PT_WORD:
1500
0
          OK = PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
1501
0
               PRIV(ucp_gentype)[prop->chartype] == ucp_N ||
1502
0
               c == CHAR_UNDERSCORE;
1503
0
          break;
1504
1505
0
          case PT_CLIST:
1506
0
          cp = PRIV(ucd_caseless_sets) + code[3];
1507
0
          for (;;)
1508
0
            {
1509
0
            if (c < *cp) { OK = FALSE; break; }
1510
0
            if (c == *cp++) { OK = TRUE; break; }
1511
0
            }
1512
0
          break;
1513
1514
0
          case PT_UCNC:
1515
0
          OK = c == CHAR_DOLLAR_SIGN || c == CHAR_COMMERCIAL_AT ||
1516
0
               c == CHAR_GRAVE_ACCENT || (c >= 0xa0 && c <= 0xd7ff) ||
1517
0
               c >= 0xe000;
1518
0
          break;
1519
1520
0
          case PT_BIDICL:
1521
0
          OK = UCD_BIDICLASS(c) == code[3];
1522
0
          break;
1523
1524
0
          case PT_BOOL:
1525
0
          OK = MAPBIT(PRIV(ucd_boolprop_sets) +
1526
0
            UCD_BPROPS_PROP(prop), code[3]) != 0;
1527
0
          break;
1528
1529
          /* Should never occur, but keep compilers from grumbling. */
1530
1531
0
          default:
1532
0
          OK = codevalue != OP_PROP;
1533
0
          break;
1534
0
          }
1535
1536
0
        if (OK == (d == OP_PROP))
1537
0
          {
1538
0
          if (count > 0 && codevalue == OP_PROP_EXTRA + OP_TYPEPOSPLUS)
1539
0
            {
1540
0
            active_count--;           /* Remove non-match possibility */
1541
0
            next_active_state--;
1542
0
            }
1543
0
          count++;
1544
0
          ADD_NEW(state_offset, count);
1545
0
          }
1546
0
        }
1547
0
      break;
1548
1549
      /*-----------------------------------------------------------------*/
1550
0
      case OP_EXTUNI_EXTRA + OP_TYPEPLUS:
1551
0
      case OP_EXTUNI_EXTRA + OP_TYPEMINPLUS:
1552
0
      case OP_EXTUNI_EXTRA + OP_TYPEPOSPLUS:
1553
0
      count = current_state->count;  /* Already matched */
1554
0
      if (count > 0) { ADD_ACTIVE(state_offset + 2, 0); }
1555
0
      if (clen > 0)
1556
0
        {
1557
0
        int ncount = 0;
1558
0
        if (count > 0 && codevalue == OP_EXTUNI_EXTRA + OP_TYPEPOSPLUS)
1559
0
          {
1560
0
          active_count--;           /* Remove non-match possibility */
1561
0
          next_active_state--;
1562
0
          }
1563
0
        (void)PRIV(extuni)(c, ptr + clen, mb->start_subject, end_subject, utf,
1564
0
          &ncount);
1565
0
        count++;
1566
0
        ADD_NEW_DATA(-state_offset, count, ncount);
1567
0
        }
1568
0
      break;
1569
0
#endif
1570
1571
      /*-----------------------------------------------------------------*/
1572
0
      case OP_ANYNL_EXTRA + OP_TYPEPLUS:
1573
0
      case OP_ANYNL_EXTRA + OP_TYPEMINPLUS:
1574
0
      case OP_ANYNL_EXTRA + OP_TYPEPOSPLUS:
1575
0
      count = current_state->count;  /* Already matched */
1576
0
      if (count > 0) { ADD_ACTIVE(state_offset + 2, 0); }
1577
0
      if (clen > 0)
1578
0
        {
1579
0
        int ncount = 0;
1580
0
        switch (c)
1581
0
          {
1582
0
          case CHAR_VT:
1583
0
          case CHAR_FF:
1584
0
          case CHAR_NEL:
1585
0
#ifndef EBCDIC
1586
0
          case 0x2028:
1587
0
          case 0x2029:
1588
0
#endif  /* Not EBCDIC */
1589
0
          if (mb->bsr_convention == PCRE2_BSR_ANYCRLF) break;
1590
0
          goto ANYNL01;
1591
1592
0
          case CHAR_CR:
1593
0
          if (ptr + 1 < end_subject && UCHAR21TEST(ptr + 1) == CHAR_LF) ncount = 1;
1594
          /* Fall through */
1595
1596
0
          ANYNL01:
1597
0
          case CHAR_LF:
1598
0
          if (count > 0 && codevalue == OP_ANYNL_EXTRA + OP_TYPEPOSPLUS)
1599
0
            {
1600
0
            active_count--;           /* Remove non-match possibility */
1601
0
            next_active_state--;
1602
0
            }
1603
0
          count++;
1604
0
          ADD_NEW_DATA(-state_offset, count, ncount);
1605
0
          break;
1606
1607
0
          default:
1608
0
          break;
1609
0
          }
1610
0
        }
1611
0
      break;
1612
1613
      /*-----------------------------------------------------------------*/
1614
0
      case OP_VSPACE_EXTRA + OP_TYPEPLUS:
1615
0
      case OP_VSPACE_EXTRA + OP_TYPEMINPLUS:
1616
0
      case OP_VSPACE_EXTRA + OP_TYPEPOSPLUS:
1617
0
      count = current_state->count;  /* Already matched */
1618
0
      if (count > 0) { ADD_ACTIVE(state_offset + 2, 0); }
1619
0
      if (clen > 0)
1620
0
        {
1621
0
        BOOL OK;
1622
0
        switch (c)
1623
0
          {
1624
0
          VSPACE_CASES:
1625
0
          OK = TRUE;
1626
0
          break;
1627
1628
0
          default:
1629
0
          OK = FALSE;
1630
0
          break;
1631
0
          }
1632
1633
0
        if (OK == (d == OP_VSPACE))
1634
0
          {
1635
0
          if (count > 0 && codevalue == OP_VSPACE_EXTRA + OP_TYPEPOSPLUS)
1636
0
            {
1637
0
            active_count--;           /* Remove non-match possibility */
1638
0
            next_active_state--;
1639
0
            }
1640
0
          count++;
1641
0
          ADD_NEW_DATA(-state_offset, count, 0);
1642
0
          }
1643
0
        }
1644
0
      break;
1645
1646
      /*-----------------------------------------------------------------*/
1647
0
      case OP_HSPACE_EXTRA + OP_TYPEPLUS:
1648
0
      case OP_HSPACE_EXTRA + OP_TYPEMINPLUS:
1649
0
      case OP_HSPACE_EXTRA + OP_TYPEPOSPLUS:
1650
0
      count = current_state->count;  /* Already matched */
1651
0
      if (count > 0) { ADD_ACTIVE(state_offset + 2, 0); }
1652
0
      if (clen > 0)
1653
0
        {
1654
0
        BOOL OK;
1655
0
        switch (c)
1656
0
          {
1657
0
          HSPACE_CASES:
1658
0
          OK = TRUE;
1659
0
          break;
1660
1661
0
          default:
1662
0
          OK = FALSE;
1663
0
          break;
1664
0
          }
1665
1666
0
        if (OK == (d == OP_HSPACE))
1667
0
          {
1668
0
          if (count > 0 && codevalue == OP_HSPACE_EXTRA + OP_TYPEPOSPLUS)
1669
0
            {
1670
0
            active_count--;           /* Remove non-match possibility */
1671
0
            next_active_state--;
1672
0
            }
1673
0
          count++;
1674
0
          ADD_NEW_DATA(-state_offset, count, 0);
1675
0
          }
1676
0
        }
1677
0
      break;
1678
1679
      /*-----------------------------------------------------------------*/
1680
0
#ifdef SUPPORT_UNICODE
1681
0
      case OP_PROP_EXTRA + OP_TYPEQUERY:
1682
0
      case OP_PROP_EXTRA + OP_TYPEMINQUERY:
1683
0
      case OP_PROP_EXTRA + OP_TYPEPOSQUERY:
1684
0
      count = 4;
1685
0
      goto QS1;
1686
1687
0
      case OP_PROP_EXTRA + OP_TYPESTAR:
1688
0
      case OP_PROP_EXTRA + OP_TYPEMINSTAR:
1689
0
      case OP_PROP_EXTRA + OP_TYPEPOSSTAR:
1690
0
      count = 0;
1691
1692
0
      QS1:
1693
1694
0
      ADD_ACTIVE(state_offset + 4, 0);
1695
0
      if (clen > 0)
1696
0
        {
1697
0
        BOOL OK;
1698
0
        const uint32_t *cp;
1699
0
        const ucd_record * prop = GET_UCD(c);
1700
0
        switch(code[2])
1701
0
          {
1702
0
          case PT_ANY:
1703
0
          OK = TRUE;
1704
0
          break;
1705
1706
0
          case PT_LAMP:
1707
0
          OK = prop->chartype == ucp_Lu || prop->chartype == ucp_Ll ||
1708
0
            prop->chartype == ucp_Lt;
1709
0
          break;
1710
1711
0
          case PT_GC:
1712
0
          OK = PRIV(ucp_gentype)[prop->chartype] == code[3];
1713
0
          break;
1714
1715
0
          case PT_PC:
1716
0
          OK = prop->chartype == code[3];
1717
0
          break;
1718
1719
0
          case PT_SC:
1720
0
          OK = prop->script == code[3];
1721
0
          break;
1722
1723
0
          case PT_SCX:
1724
0
          OK = (prop->script == code[3] ||
1725
0
                MAPBIT(PRIV(ucd_script_sets) + UCD_SCRIPTX_PROP(prop), code[3]) != 0);
1726
0
          break;
1727
1728
          /* These are specials for combination cases. */
1729
1730
0
          case PT_ALNUM:
1731
0
          OK = PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
1732
0
               PRIV(ucp_gentype)[prop->chartype] == ucp_N;
1733
0
          break;
1734
1735
          /* Perl space used to exclude VT, but from Perl 5.18 it is included,
1736
          which means that Perl space and POSIX space are now identical. PCRE
1737
          was changed at release 8.34. */
1738
1739
0
          case PT_SPACE:    /* Perl space */
1740
0
          case PT_PXSPACE:  /* POSIX space */
1741
0
          switch(c)
1742
0
            {
1743
0
            HSPACE_CASES:
1744
0
            VSPACE_CASES:
1745
0
            OK = TRUE;
1746
0
            break;
1747
1748
0
            default:
1749
0
            OK = PRIV(ucp_gentype)[prop->chartype] == ucp_Z;
1750
0
            break;
1751
0
            }
1752
0
          break;
1753
1754
0
          case PT_WORD:
1755
0
          OK = PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
1756
0
               PRIV(ucp_gentype)[prop->chartype] == ucp_N ||
1757
0
               c == CHAR_UNDERSCORE;
1758
0
          break;
1759
1760
0
          case PT_CLIST:
1761
0
          cp = PRIV(ucd_caseless_sets) + code[3];
1762
0
          for (;;)
1763
0
            {
1764
0
            if (c < *cp) { OK = FALSE; break; }
1765
0
            if (c == *cp++) { OK = TRUE; break; }
1766
0
            }
1767
0
          break;
1768
1769
0
          case PT_UCNC:
1770
0
          OK = c == CHAR_DOLLAR_SIGN || c == CHAR_COMMERCIAL_AT ||
1771
0
               c == CHAR_GRAVE_ACCENT || (c >= 0xa0 && c <= 0xd7ff) ||
1772
0
               c >= 0xe000;
1773
0
          break;
1774
1775
0
          case PT_BIDICL:
1776
0
          OK = UCD_BIDICLASS(c) == code[3];
1777
0
          break;
1778
1779
0
          case PT_BOOL:
1780
0
          OK = MAPBIT(PRIV(ucd_boolprop_sets) +
1781
0
            UCD_BPROPS_PROP(prop), code[3]) != 0;
1782
0
          break;
1783
1784
          /* Should never occur, but keep compilers from grumbling. */
1785
1786
0
          default:
1787
0
          OK = codevalue != OP_PROP;
1788
0
          break;
1789
0
          }
1790
1791
0
        if (OK == (d == OP_PROP))
1792
0
          {
1793
0
          if (codevalue == OP_PROP_EXTRA + OP_TYPEPOSSTAR ||
1794
0
              codevalue == OP_PROP_EXTRA + OP_TYPEPOSQUERY)
1795
0
            {
1796
0
            active_count--;           /* Remove non-match possibility */
1797
0
            next_active_state--;
1798
0
            }
1799
0
          ADD_NEW(state_offset + count, 0);
1800
0
          }
1801
0
        }
1802
0
      break;
1803
1804
      /*-----------------------------------------------------------------*/
1805
0
      case OP_EXTUNI_EXTRA + OP_TYPEQUERY:
1806
0
      case OP_EXTUNI_EXTRA + OP_TYPEMINQUERY:
1807
0
      case OP_EXTUNI_EXTRA + OP_TYPEPOSQUERY:
1808
0
      count = 2;
1809
0
      goto QS2;
1810
1811
0
      case OP_EXTUNI_EXTRA + OP_TYPESTAR:
1812
0
      case OP_EXTUNI_EXTRA + OP_TYPEMINSTAR:
1813
0
      case OP_EXTUNI_EXTRA + OP_TYPEPOSSTAR:
1814
0
      count = 0;
1815
1816
0
      QS2:
1817
1818
0
      ADD_ACTIVE(state_offset + 2, 0);
1819
0
      if (clen > 0)
1820
0
        {
1821
0
        int ncount = 0;
1822
0
        if (codevalue == OP_EXTUNI_EXTRA + OP_TYPEPOSSTAR ||
1823
0
            codevalue == OP_EXTUNI_EXTRA + OP_TYPEPOSQUERY)
1824
0
          {
1825
0
          active_count--;           /* Remove non-match possibility */
1826
0
          next_active_state--;
1827
0
          }
1828
0
        (void)PRIV(extuni)(c, ptr + clen, mb->start_subject, end_subject, utf,
1829
0
          &ncount);
1830
0
        ADD_NEW_DATA(-(state_offset + count), 0, ncount);
1831
0
        }
1832
0
      break;
1833
0
#endif
1834
1835
      /*-----------------------------------------------------------------*/
1836
0
      case OP_ANYNL_EXTRA + OP_TYPEQUERY:
1837
0
      case OP_ANYNL_EXTRA + OP_TYPEMINQUERY:
1838
0
      case OP_ANYNL_EXTRA + OP_TYPEPOSQUERY:
1839
0
      count = 2;
1840
0
      goto QS3;
1841
1842
0
      case OP_ANYNL_EXTRA + OP_TYPESTAR:
1843
0
      case OP_ANYNL_EXTRA + OP_TYPEMINSTAR:
1844
0
      case OP_ANYNL_EXTRA + OP_TYPEPOSSTAR:
1845
0
      count = 0;
1846
1847
0
      QS3:
1848
0
      ADD_ACTIVE(state_offset + 2, 0);
1849
0
      if (clen > 0)
1850
0
        {
1851
0
        int ncount = 0;
1852
0
        switch (c)
1853
0
          {
1854
0
          case CHAR_VT:
1855
0
          case CHAR_FF:
1856
0
          case CHAR_NEL:
1857
0
#ifndef EBCDIC
1858
0
          case 0x2028:
1859
0
          case 0x2029:
1860
0
#endif  /* Not EBCDIC */
1861
0
          if (mb->bsr_convention == PCRE2_BSR_ANYCRLF) break;
1862
0
          goto ANYNL02;
1863
1864
0
          case CHAR_CR:
1865
0
          if (ptr + 1 < end_subject && UCHAR21TEST(ptr + 1) == CHAR_LF) ncount = 1;
1866
          /* Fall through */
1867
1868
0
          ANYNL02:
1869
0
          case CHAR_LF:
1870
0
          if (codevalue == OP_ANYNL_EXTRA + OP_TYPEPOSSTAR ||
1871
0
              codevalue == OP_ANYNL_EXTRA + OP_TYPEPOSQUERY)
1872
0
            {
1873
0
            active_count--;           /* Remove non-match possibility */
1874
0
            next_active_state--;
1875
0
            }
1876
0
          ADD_NEW_DATA(-(state_offset + (int)count), 0, ncount);
1877
0
          break;
1878
1879
0
          default:
1880
0
          break;
1881
0
          }
1882
0
        }
1883
0
      break;
1884
1885
      /*-----------------------------------------------------------------*/
1886
0
      case OP_VSPACE_EXTRA + OP_TYPEQUERY:
1887
0
      case OP_VSPACE_EXTRA + OP_TYPEMINQUERY:
1888
0
      case OP_VSPACE_EXTRA + OP_TYPEPOSQUERY:
1889
0
      count = 2;
1890
0
      goto QS4;
1891
1892
0
      case OP_VSPACE_EXTRA + OP_TYPESTAR:
1893
0
      case OP_VSPACE_EXTRA + OP_TYPEMINSTAR:
1894
0
      case OP_VSPACE_EXTRA + OP_TYPEPOSSTAR:
1895
0
      count = 0;
1896
1897
0
      QS4:
1898
0
      ADD_ACTIVE(state_offset + 2, 0);
1899
0
      if (clen > 0)
1900
0
        {
1901
0
        BOOL OK;
1902
0
        switch (c)
1903
0
          {
1904
0
          VSPACE_CASES:
1905
0
          OK = TRUE;
1906
0
          break;
1907
1908
0
          default:
1909
0
          OK = FALSE;
1910
0
          break;
1911
0
          }
1912
0
        if (OK == (d == OP_VSPACE))
1913
0
          {
1914
0
          if (codevalue == OP_VSPACE_EXTRA + OP_TYPEPOSSTAR ||
1915
0
              codevalue == OP_VSPACE_EXTRA + OP_TYPEPOSQUERY)
1916
0
            {
1917
0
            active_count--;           /* Remove non-match possibility */
1918
0
            next_active_state--;
1919
0
            }
1920
0
          ADD_NEW_DATA(-(state_offset + (int)count), 0, 0);
1921
0
          }
1922
0
        }
1923
0
      break;
1924
1925
      /*-----------------------------------------------------------------*/
1926
0
      case OP_HSPACE_EXTRA + OP_TYPEQUERY:
1927
0
      case OP_HSPACE_EXTRA + OP_TYPEMINQUERY:
1928
0
      case OP_HSPACE_EXTRA + OP_TYPEPOSQUERY:
1929
0
      count = 2;
1930
0
      goto QS5;
1931
1932
0
      case OP_HSPACE_EXTRA + OP_TYPESTAR:
1933
0
      case OP_HSPACE_EXTRA + OP_TYPEMINSTAR:
1934
0
      case OP_HSPACE_EXTRA + OP_TYPEPOSSTAR:
1935
0
      count = 0;
1936
1937
0
      QS5:
1938
0
      ADD_ACTIVE(state_offset + 2, 0);
1939
0
      if (clen > 0)
1940
0
        {
1941
0
        BOOL OK;
1942
0
        switch (c)
1943
0
          {
1944
0
          HSPACE_CASES:
1945
0
          OK = TRUE;
1946
0
          break;
1947
1948
0
          default:
1949
0
          OK = FALSE;
1950
0
          break;
1951
0
          }
1952
1953
0
        if (OK == (d == OP_HSPACE))
1954
0
          {
1955
0
          if (codevalue == OP_HSPACE_EXTRA + OP_TYPEPOSSTAR ||
1956
0
              codevalue == OP_HSPACE_EXTRA + OP_TYPEPOSQUERY)
1957
0
            {
1958
0
            active_count--;           /* Remove non-match possibility */
1959
0
            next_active_state--;
1960
0
            }
1961
0
          ADD_NEW_DATA(-(state_offset + (int)count), 0, 0);
1962
0
          }
1963
0
        }
1964
0
      break;
1965
1966
      /*-----------------------------------------------------------------*/
1967
0
#ifdef SUPPORT_UNICODE
1968
0
      case OP_PROP_EXTRA + OP_TYPEEXACT:
1969
0
      case OP_PROP_EXTRA + OP_TYPEUPTO:
1970
0
      case OP_PROP_EXTRA + OP_TYPEMINUPTO:
1971
0
      case OP_PROP_EXTRA + OP_TYPEPOSUPTO:
1972
0
      if (codevalue != OP_PROP_EXTRA + OP_TYPEEXACT)
1973
0
        { ADD_ACTIVE(state_offset + 1 + IMM2_SIZE + 3, 0); }
1974
0
      count = current_state->count;  /* Number already matched */
1975
0
      if (clen > 0)
1976
0
        {
1977
0
        BOOL OK;
1978
0
        const uint32_t *cp;
1979
0
        const ucd_record * prop = GET_UCD(c);
1980
0
        switch(code[1 + IMM2_SIZE + 1])
1981
0
          {
1982
0
          case PT_ANY:
1983
0
          OK = TRUE;
1984
0
          break;
1985
1986
0
          case PT_LAMP:
1987
0
          OK = prop->chartype == ucp_Lu || prop->chartype == ucp_Ll ||
1988
0
            prop->chartype == ucp_Lt;
1989
0
          break;
1990
1991
0
          case PT_GC:
1992
0
          OK = PRIV(ucp_gentype)[prop->chartype] == code[1 + IMM2_SIZE + 2];
1993
0
          break;
1994
1995
0
          case PT_PC:
1996
0
          OK = prop->chartype == code[1 + IMM2_SIZE + 2];
1997
0
          break;
1998
1999
0
          case PT_SC:
2000
0
          OK = prop->script == code[1 + IMM2_SIZE + 2];
2001
0
          break;
2002
2003
0
          case PT_SCX:
2004
0
          OK = (prop->script == code[1 + IMM2_SIZE + 2] ||
2005
0
                MAPBIT(PRIV(ucd_script_sets) + UCD_SCRIPTX_PROP(prop),
2006
0
                  code[1 + IMM2_SIZE + 2]) != 0);
2007
0
          break;
2008
2009
          /* These are specials for combination cases. */
2010
2011
0
          case PT_ALNUM:
2012
0
          OK = PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
2013
0
               PRIV(ucp_gentype)[prop->chartype] == ucp_N;
2014
0
          break;
2015
2016
          /* Perl space used to exclude VT, but from Perl 5.18 it is included,
2017
          which means that Perl space and POSIX space are now identical. PCRE
2018
          was changed at release 8.34. */
2019
2020
0
          case PT_SPACE:    /* Perl space */
2021
0
          case PT_PXSPACE:  /* POSIX space */
2022
0
          switch(c)
2023
0
            {
2024
0
            HSPACE_CASES:
2025
0
            VSPACE_CASES:
2026
0
            OK = TRUE;
2027
0
            break;
2028
2029
0
            default:
2030
0
            OK = PRIV(ucp_gentype)[prop->chartype] == ucp_Z;
2031
0
            break;
2032
0
            }
2033
0
          break;
2034
2035
0
          case PT_WORD:
2036
0
          OK = PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
2037
0
               PRIV(ucp_gentype)[prop->chartype] == ucp_N ||
2038
0
               c == CHAR_UNDERSCORE;
2039
0
          break;
2040
2041
0
          case PT_CLIST:
2042
0
          cp = PRIV(ucd_caseless_sets) + code[1 + IMM2_SIZE + 2];
2043
0
          for (;;)
2044
0
            {
2045
0
            if (c < *cp) { OK = FALSE; break; }
2046
0
            if (c == *cp++) { OK = TRUE; break; }
2047
0
            }
2048
0
          break;
2049
2050
0
          case PT_UCNC:
2051
0
          OK = c == CHAR_DOLLAR_SIGN || c == CHAR_COMMERCIAL_AT ||
2052
0
               c == CHAR_GRAVE_ACCENT || (c >= 0xa0 && c <= 0xd7ff) ||
2053
0
               c >= 0xe000;
2054
0
          break;
2055
2056
0
          case PT_BIDICL:
2057
0
          OK = UCD_BIDICLASS(c) == code[1 + IMM2_SIZE + 2];
2058
0
          break;
2059
2060
0
          case PT_BOOL:
2061
0
          OK = MAPBIT(PRIV(ucd_boolprop_sets) +
2062
0
            UCD_BPROPS_PROP(prop), code[1 + IMM2_SIZE + 2]) != 0;
2063
0
          break;
2064
2065
          /* Should never occur, but keep compilers from grumbling. */
2066
2067
0
          default:
2068
0
          OK = codevalue != OP_PROP;
2069
0
          break;
2070
0
          }
2071
2072
0
        if (OK == (d == OP_PROP))
2073
0
          {
2074
0
          if (codevalue == OP_PROP_EXTRA + OP_TYPEPOSUPTO)
2075
0
            {
2076
0
            active_count--;           /* Remove non-match possibility */
2077
0
            next_active_state--;
2078
0
            }
2079
0
          if (++count >= (int)GET2(code, 1))
2080
0
            { ADD_NEW(state_offset + 1 + IMM2_SIZE + 3, 0); }
2081
0
          else
2082
0
            { ADD_NEW(state_offset, count); }
2083
0
          }
2084
0
        }
2085
0
      break;
2086
2087
      /*-----------------------------------------------------------------*/
2088
0
      case OP_EXTUNI_EXTRA + OP_TYPEEXACT:
2089
0
      case OP_EXTUNI_EXTRA + OP_TYPEUPTO:
2090
0
      case OP_EXTUNI_EXTRA + OP_TYPEMINUPTO:
2091
0
      case OP_EXTUNI_EXTRA + OP_TYPEPOSUPTO:
2092
0
      if (codevalue != OP_EXTUNI_EXTRA + OP_TYPEEXACT)
2093
0
        { ADD_ACTIVE(state_offset + 2 + IMM2_SIZE, 0); }
2094
0
      count = current_state->count;  /* Number already matched */
2095
0
      if (clen > 0)
2096
0
        {
2097
0
        PCRE2_SPTR nptr;
2098
0
        int ncount = 0;
2099
0
        if (codevalue == OP_EXTUNI_EXTRA + OP_TYPEPOSUPTO)
2100
0
          {
2101
0
          active_count--;           /* Remove non-match possibility */
2102
0
          next_active_state--;
2103
0
          }
2104
0
        nptr = PRIV(extuni)(c, ptr + clen, mb->start_subject, end_subject, utf,
2105
0
          &ncount);
2106
0
        if (nptr >= end_subject && (mb->moptions & PCRE2_PARTIAL_HARD) != 0)
2107
0
            reset_could_continue = TRUE;
2108
0
        if (++count >= (int)GET2(code, 1))
2109
0
          { ADD_NEW_DATA(-(state_offset + 2 + IMM2_SIZE), 0, ncount); }
2110
0
        else
2111
0
          { ADD_NEW_DATA(-state_offset, count, ncount); }
2112
0
        }
2113
0
      break;
2114
0
#endif
2115
2116
      /*-----------------------------------------------------------------*/
2117
0
      case OP_ANYNL_EXTRA + OP_TYPEEXACT:
2118
0
      case OP_ANYNL_EXTRA + OP_TYPEUPTO:
2119
0
      case OP_ANYNL_EXTRA + OP_TYPEMINUPTO:
2120
0
      case OP_ANYNL_EXTRA + OP_TYPEPOSUPTO:
2121
0
      if (codevalue != OP_ANYNL_EXTRA + OP_TYPEEXACT)
2122
0
        { ADD_ACTIVE(state_offset + 2 + IMM2_SIZE, 0); }
2123
0
      count = current_state->count;  /* Number already matched */
2124
0
      if (clen > 0)
2125
0
        {
2126
0
        int ncount = 0;
2127
0
        switch (c)
2128
0
          {
2129
0
          case CHAR_VT:
2130
0
          case CHAR_FF:
2131
0
          case CHAR_NEL:
2132
0
#ifndef EBCDIC
2133
0
          case 0x2028:
2134
0
          case 0x2029:
2135
0
#endif  /* Not EBCDIC */
2136
0
          if (mb->bsr_convention == PCRE2_BSR_ANYCRLF) break;
2137
0
          goto ANYNL03;
2138
2139
0
          case CHAR_CR:
2140
0
          if (ptr + 1 < end_subject && UCHAR21TEST(ptr + 1) == CHAR_LF) ncount = 1;
2141
          /* Fall through */
2142
2143
0
          ANYNL03:
2144
0
          case CHAR_LF:
2145
0
          if (codevalue == OP_ANYNL_EXTRA + OP_TYPEPOSUPTO)
2146
0
            {
2147
0
            active_count--;           /* Remove non-match possibility */
2148
0
            next_active_state--;
2149
0
            }
2150
0
          if (++count >= (int)GET2(code, 1))
2151
0
            { ADD_NEW_DATA(-(state_offset + 2 + IMM2_SIZE), 0, ncount); }
2152
0
          else
2153
0
            { ADD_NEW_DATA(-state_offset, count, ncount); }
2154
0
          break;
2155
2156
0
          default:
2157
0
          break;
2158
0
          }
2159
0
        }
2160
0
      break;
2161
2162
      /*-----------------------------------------------------------------*/
2163
0
      case OP_VSPACE_EXTRA + OP_TYPEEXACT:
2164
0
      case OP_VSPACE_EXTRA + OP_TYPEUPTO:
2165
0
      case OP_VSPACE_EXTRA + OP_TYPEMINUPTO:
2166
0
      case OP_VSPACE_EXTRA + OP_TYPEPOSUPTO:
2167
0
      if (codevalue != OP_VSPACE_EXTRA + OP_TYPEEXACT)
2168
0
        { ADD_ACTIVE(state_offset + 2 + IMM2_SIZE, 0); }
2169
0
      count = current_state->count;  /* Number already matched */
2170
0
      if (clen > 0)
2171
0
        {
2172
0
        BOOL OK;
2173
0
        switch (c)
2174
0
          {
2175
0
          VSPACE_CASES:
2176
0
          OK = TRUE;
2177
0
          break;
2178
2179
0
          default:
2180
0
          OK = FALSE;
2181
0
          }
2182
2183
0
        if (OK == (d == OP_VSPACE))
2184
0
          {
2185
0
          if (codevalue == OP_VSPACE_EXTRA + OP_TYPEPOSUPTO)
2186
0
            {
2187
0
            active_count--;           /* Remove non-match possibility */
2188
0
            next_active_state--;
2189
0
            }
2190
0
          if (++count >= (int)GET2(code, 1))
2191
0
            { ADD_NEW_DATA(-(state_offset + 2 + IMM2_SIZE), 0, 0); }
2192
0
          else
2193
0
            { ADD_NEW_DATA(-state_offset, count, 0); }
2194
0
          }
2195
0
        }
2196
0
      break;
2197
2198
      /*-----------------------------------------------------------------*/
2199
0
      case OP_HSPACE_EXTRA + OP_TYPEEXACT:
2200
0
      case OP_HSPACE_EXTRA + OP_TYPEUPTO:
2201
0
      case OP_HSPACE_EXTRA + OP_TYPEMINUPTO:
2202
0
      case OP_HSPACE_EXTRA + OP_TYPEPOSUPTO:
2203
0
      if (codevalue != OP_HSPACE_EXTRA + OP_TYPEEXACT)
2204
0
        { ADD_ACTIVE(state_offset + 2 + IMM2_SIZE, 0); }
2205
0
      count = current_state->count;  /* Number already matched */
2206
0
      if (clen > 0)
2207
0
        {
2208
0
        BOOL OK;
2209
0
        switch (c)
2210
0
          {
2211
0
          HSPACE_CASES:
2212
0
          OK = TRUE;
2213
0
          break;
2214
2215
0
          default:
2216
0
          OK = FALSE;
2217
0
          break;
2218
0
          }
2219
2220
0
        if (OK == (d == OP_HSPACE))
2221
0
          {
2222
0
          if (codevalue == OP_HSPACE_EXTRA + OP_TYPEPOSUPTO)
2223
0
            {
2224
0
            active_count--;           /* Remove non-match possibility */
2225
0
            next_active_state--;
2226
0
            }
2227
0
          if (++count >= (int)GET2(code, 1))
2228
0
            { ADD_NEW_DATA(-(state_offset + 2 + IMM2_SIZE), 0, 0); }
2229
0
          else
2230
0
            { ADD_NEW_DATA(-state_offset, count, 0); }
2231
0
          }
2232
0
        }
2233
0
      break;
2234
2235
/* ========================================================================== */
2236
      /* These opcodes are followed by a character that is usually compared
2237
      to the current subject character; it is loaded into d. We still get
2238
      here even if there is no subject character, because in some cases zero
2239
      repetitions are permitted. */
2240
2241
      /*-----------------------------------------------------------------*/
2242
0
      case OP_CHAR:
2243
0
      if (clen > 0 && c == d) { ADD_NEW(state_offset + dlen + 1, 0); }
2244
0
      break;
2245
2246
      /*-----------------------------------------------------------------*/
2247
0
      case OP_CHARI:
2248
0
      if (clen == 0) break;
2249
2250
0
#ifdef SUPPORT_UNICODE
2251
0
      if (utf_or_ucp)
2252
0
        {
2253
0
        if (c == d) { ADD_NEW(state_offset + dlen + 1, 0); } else
2254
0
          {
2255
0
          unsigned int othercase;
2256
0
          if (c < 128)
2257
0
            othercase = fcc[c];
2258
0
          else
2259
0
            othercase = UCD_OTHERCASE(c);
2260
0
          if (d == othercase) { ADD_NEW(state_offset + dlen + 1, 0); }
2261
0
          }
2262
0
        }
2263
0
      else
2264
0
#endif  /* SUPPORT_UNICODE */
2265
      /* Not UTF or UCP mode */
2266
0
        {
2267
0
        if (TABLE_GET(c, lcc, c) == TABLE_GET(d, lcc, d))
2268
0
          { ADD_NEW(state_offset + 2, 0); }
2269
0
        }
2270
0
      break;
2271
2272
2273
0
#ifdef SUPPORT_UNICODE
2274
      /*-----------------------------------------------------------------*/
2275
      /* This is a tricky one because it can match more than one character.
2276
      Find out how many characters to skip, and then set up a negative state
2277
      to wait for them to pass before continuing. */
2278
2279
0
      case OP_EXTUNI:
2280
0
      if (clen > 0)
2281
0
        {
2282
0
        int ncount = 0;
2283
0
        PCRE2_SPTR nptr = PRIV(extuni)(c, ptr + clen, mb->start_subject,
2284
0
          end_subject, utf, &ncount);
2285
0
        if (nptr >= end_subject && (mb->moptions & PCRE2_PARTIAL_HARD) != 0)
2286
0
            reset_could_continue = TRUE;
2287
0
        ADD_NEW_DATA(-(state_offset + 1), 0, ncount);
2288
0
        }
2289
0
      break;
2290
0
#endif
2291
2292
      /*-----------------------------------------------------------------*/
2293
      /* This is a tricky like EXTUNI because it too can match more than one
2294
      character (when CR is followed by LF). In this case, set up a negative
2295
      state to wait for one character to pass before continuing. */
2296
2297
0
      case OP_ANYNL:
2298
0
      if (clen > 0) switch(c)
2299
0
        {
2300
0
        case CHAR_VT:
2301
0
        case CHAR_FF:
2302
0
        case CHAR_NEL:
2303
0
#ifndef EBCDIC
2304
0
        case 0x2028:
2305
0
        case 0x2029:
2306
0
#endif  /* Not EBCDIC */
2307
0
        if (mb->bsr_convention == PCRE2_BSR_ANYCRLF) break;
2308
        /* Fall through */
2309
2310
0
        case CHAR_LF:
2311
0
        ADD_NEW(state_offset + 1, 0);
2312
0
        break;
2313
2314
0
        case CHAR_CR:
2315
0
        if (ptr + 1 >= end_subject)
2316
0
          {
2317
0
          ADD_NEW(state_offset + 1, 0);
2318
0
          if ((mb->moptions & PCRE2_PARTIAL_HARD) != 0)
2319
0
            reset_could_continue = TRUE;
2320
0
          }
2321
0
        else if (UCHAR21TEST(ptr + 1) == CHAR_LF)
2322
0
          {
2323
0
          ADD_NEW_DATA(-(state_offset + 1), 0, 1);
2324
0
          }
2325
0
        else
2326
0
          {
2327
0
          ADD_NEW(state_offset + 1, 0);
2328
0
          }
2329
0
        break;
2330
0
        }
2331
0
      break;
2332
2333
      /*-----------------------------------------------------------------*/
2334
0
      case OP_NOT_VSPACE:
2335
0
      if (clen > 0) switch(c)
2336
0
        {
2337
0
        VSPACE_CASES:
2338
0
        break;
2339
2340
0
        default:
2341
0
        ADD_NEW(state_offset + 1, 0);
2342
0
        break;
2343
0
        }
2344
0
      break;
2345
2346
      /*-----------------------------------------------------------------*/
2347
0
      case OP_VSPACE:
2348
0
      if (clen > 0) switch(c)
2349
0
        {
2350
0
        VSPACE_CASES:
2351
0
        ADD_NEW(state_offset + 1, 0);
2352
0
        break;
2353
2354
0
        default:
2355
0
        break;
2356
0
        }
2357
0
      break;
2358
2359
      /*-----------------------------------------------------------------*/
2360
0
      case OP_NOT_HSPACE:
2361
0
      if (clen > 0) switch(c)
2362
0
        {
2363
0
        HSPACE_CASES:
2364
0
        break;
2365
2366
0
        default:
2367
0
        ADD_NEW(state_offset + 1, 0);
2368
0
        break;
2369
0
        }
2370
0
      break;
2371
2372
      /*-----------------------------------------------------------------*/
2373
0
      case OP_HSPACE:
2374
0
      if (clen > 0) switch(c)
2375
0
        {
2376
0
        HSPACE_CASES:
2377
0
        ADD_NEW(state_offset + 1, 0);
2378
0
        break;
2379
2380
0
        default:
2381
0
        break;
2382
0
        }
2383
0
      break;
2384
2385
      /*-----------------------------------------------------------------*/
2386
      /* Match a negated single character casefully. */
2387
2388
0
      case OP_NOT:
2389
0
      if (clen > 0 && c != d) { ADD_NEW(state_offset + dlen + 1, 0); }
2390
0
      break;
2391
2392
      /*-----------------------------------------------------------------*/
2393
      /* Match a negated single character caselessly. */
2394
2395
0
      case OP_NOTI:
2396
0
      if (clen > 0)
2397
0
        {
2398
0
        uint32_t otherd;
2399
0
#ifdef SUPPORT_UNICODE
2400
0
        if (utf_or_ucp && d >= 128)
2401
0
          otherd = UCD_OTHERCASE(d);
2402
0
        else
2403
0
#endif  /* SUPPORT_UNICODE */
2404
0
        otherd = TABLE_GET(d, fcc, d);
2405
0
        if (c != d && c != otherd)
2406
0
          { ADD_NEW(state_offset + dlen + 1, 0); }
2407
0
        }
2408
0
      break;
2409
2410
      /*-----------------------------------------------------------------*/
2411
0
      case OP_PLUSI:
2412
0
      case OP_MINPLUSI:
2413
0
      case OP_POSPLUSI:
2414
0
      case OP_NOTPLUSI:
2415
0
      case OP_NOTMINPLUSI:
2416
0
      case OP_NOTPOSPLUSI:
2417
0
      caseless = TRUE;
2418
0
      codevalue -= OP_STARI - OP_STAR;
2419
2420
      /* Fall through */
2421
0
      case OP_PLUS:
2422
0
      case OP_MINPLUS:
2423
0
      case OP_POSPLUS:
2424
0
      case OP_NOTPLUS:
2425
0
      case OP_NOTMINPLUS:
2426
0
      case OP_NOTPOSPLUS:
2427
0
      count = current_state->count;  /* Already matched */
2428
0
      if (count > 0) { ADD_ACTIVE(state_offset + dlen + 1, 0); }
2429
0
      if (clen > 0)
2430
0
        {
2431
0
        uint32_t otherd = NOTACHAR;
2432
0
        if (caseless)
2433
0
          {
2434
0
#ifdef SUPPORT_UNICODE
2435
0
          if (utf_or_ucp && d >= 128)
2436
0
            otherd = UCD_OTHERCASE(d);
2437
0
          else
2438
0
#endif  /* SUPPORT_UNICODE */
2439
0
          otherd = TABLE_GET(d, fcc, d);
2440
0
          }
2441
0
        if ((c == d || c == otherd) == (codevalue < OP_NOTSTAR))
2442
0
          {
2443
0
          if (count > 0 &&
2444
0
              (codevalue == OP_POSPLUS || codevalue == OP_NOTPOSPLUS))
2445
0
            {
2446
0
            active_count--;             /* Remove non-match possibility */
2447
0
            next_active_state--;
2448
0
            }
2449
0
          count++;
2450
0
          ADD_NEW(state_offset, count);
2451
0
          }
2452
0
        }
2453
0
      break;
2454
2455
      /*-----------------------------------------------------------------*/
2456
0
      case OP_QUERYI:
2457
0
      case OP_MINQUERYI:
2458
0
      case OP_POSQUERYI:
2459
0
      case OP_NOTQUERYI:
2460
0
      case OP_NOTMINQUERYI:
2461
0
      case OP_NOTPOSQUERYI:
2462
0
      caseless = TRUE;
2463
0
      codevalue -= OP_STARI - OP_STAR;
2464
      /* Fall through */
2465
0
      case OP_QUERY:
2466
0
      case OP_MINQUERY:
2467
0
      case OP_POSQUERY:
2468
0
      case OP_NOTQUERY:
2469
0
      case OP_NOTMINQUERY:
2470
0
      case OP_NOTPOSQUERY:
2471
0
      ADD_ACTIVE(state_offset + dlen + 1, 0);
2472
0
      if (clen > 0)
2473
0
        {
2474
0
        uint32_t otherd = NOTACHAR;
2475
0
        if (caseless)
2476
0
          {
2477
0
#ifdef SUPPORT_UNICODE
2478
0
          if (utf_or_ucp && d >= 128)
2479
0
            otherd = UCD_OTHERCASE(d);
2480
0
          else
2481
0
#endif  /* SUPPORT_UNICODE */
2482
0
          otherd = TABLE_GET(d, fcc, d);
2483
0
          }
2484
0
        if ((c == d || c == otherd) == (codevalue < OP_NOTSTAR))
2485
0
          {
2486
0
          if (codevalue == OP_POSQUERY || codevalue == OP_NOTPOSQUERY)
2487
0
            {
2488
0
            active_count--;            /* Remove non-match possibility */
2489
0
            next_active_state--;
2490
0
            }
2491
0
          ADD_NEW(state_offset + dlen + 1, 0);
2492
0
          }
2493
0
        }
2494
0
      break;
2495
2496
      /*-----------------------------------------------------------------*/
2497
0
      case OP_STARI:
2498
0
      case OP_MINSTARI:
2499
0
      case OP_POSSTARI:
2500
0
      case OP_NOTSTARI:
2501
0
      case OP_NOTMINSTARI:
2502
0
      case OP_NOTPOSSTARI:
2503
0
      caseless = TRUE;
2504
0
      codevalue -= OP_STARI - OP_STAR;
2505
      /* Fall through */
2506
0
      case OP_STAR:
2507
0
      case OP_MINSTAR:
2508
0
      case OP_POSSTAR:
2509
0
      case OP_NOTSTAR:
2510
0
      case OP_NOTMINSTAR:
2511
0
      case OP_NOTPOSSTAR:
2512
0
      ADD_ACTIVE(state_offset + dlen + 1, 0);
2513
0
      if (clen > 0)
2514
0
        {
2515
0
        uint32_t otherd = NOTACHAR;
2516
0
        if (caseless)
2517
0
          {
2518
0
#ifdef SUPPORT_UNICODE
2519
0
          if (utf_or_ucp && d >= 128)
2520
0
            otherd = UCD_OTHERCASE(d);
2521
0
          else
2522
0
#endif  /* SUPPORT_UNICODE */
2523
0
          otherd = TABLE_GET(d, fcc, d);
2524
0
          }
2525
0
        if ((c == d || c == otherd) == (codevalue < OP_NOTSTAR))
2526
0
          {
2527
0
          if (codevalue == OP_POSSTAR || codevalue == OP_NOTPOSSTAR)
2528
0
            {
2529
0
            active_count--;            /* Remove non-match possibility */
2530
0
            next_active_state--;
2531
0
            }
2532
0
          ADD_NEW(state_offset, 0);
2533
0
          }
2534
0
        }
2535
0
      break;
2536
2537
      /*-----------------------------------------------------------------*/
2538
0
      case OP_EXACTI:
2539
0
      case OP_NOTEXACTI:
2540
0
      caseless = TRUE;
2541
0
      codevalue -= OP_STARI - OP_STAR;
2542
      /* Fall through */
2543
0
      case OP_EXACT:
2544
0
      case OP_NOTEXACT:
2545
0
      count = current_state->count;  /* Number already matched */
2546
0
      if (clen > 0)
2547
0
        {
2548
0
        uint32_t otherd = NOTACHAR;
2549
0
        if (caseless)
2550
0
          {
2551
0
#ifdef SUPPORT_UNICODE
2552
0
          if (utf_or_ucp && d >= 128)
2553
0
            otherd = UCD_OTHERCASE(d);
2554
0
          else
2555
0
#endif  /* SUPPORT_UNICODE */
2556
0
          otherd = TABLE_GET(d, fcc, d);
2557
0
          }
2558
0
        if ((c == d || c == otherd) == (codevalue < OP_NOTSTAR))
2559
0
          {
2560
0
          if (++count >= (int)GET2(code, 1))
2561
0
            { ADD_NEW(state_offset + dlen + 1 + IMM2_SIZE, 0); }
2562
0
          else
2563
0
            { ADD_NEW(state_offset, count); }
2564
0
          }
2565
0
        }
2566
0
      break;
2567
2568
      /*-----------------------------------------------------------------*/
2569
0
      case OP_UPTOI:
2570
0
      case OP_MINUPTOI:
2571
0
      case OP_POSUPTOI:
2572
0
      case OP_NOTUPTOI:
2573
0
      case OP_NOTMINUPTOI:
2574
0
      case OP_NOTPOSUPTOI:
2575
0
      caseless = TRUE;
2576
0
      codevalue -= OP_STARI - OP_STAR;
2577
      /* Fall through */
2578
0
      case OP_UPTO:
2579
0
      case OP_MINUPTO:
2580
0
      case OP_POSUPTO:
2581
0
      case OP_NOTUPTO:
2582
0
      case OP_NOTMINUPTO:
2583
0
      case OP_NOTPOSUPTO:
2584
0
      ADD_ACTIVE(state_offset + dlen + 1 + IMM2_SIZE, 0);
2585
0
      count = current_state->count;  /* Number already matched */
2586
0
      if (clen > 0)
2587
0
        {
2588
0
        uint32_t otherd = NOTACHAR;
2589
0
        if (caseless)
2590
0
          {
2591
0
#ifdef SUPPORT_UNICODE
2592
0
          if (utf_or_ucp && d >= 128)
2593
0
            otherd = UCD_OTHERCASE(d);
2594
0
          else
2595
0
#endif  /* SUPPORT_UNICODE */
2596
0
          otherd = TABLE_GET(d, fcc, d);
2597
0
          }
2598
0
        if ((c == d || c == otherd) == (codevalue < OP_NOTSTAR))
2599
0
          {
2600
0
          if (codevalue == OP_POSUPTO || codevalue == OP_NOTPOSUPTO)
2601
0
            {
2602
0
            active_count--;             /* Remove non-match possibility */
2603
0
            next_active_state--;
2604
0
            }
2605
0
          if (++count >= (int)GET2(code, 1))
2606
0
            { ADD_NEW(state_offset + dlen + 1 + IMM2_SIZE, 0); }
2607
0
          else
2608
0
            { ADD_NEW(state_offset, count); }
2609
0
          }
2610
0
        }
2611
0
      break;
2612
2613
2614
/* ========================================================================== */
2615
      /* These are the class-handling opcodes */
2616
2617
0
      case OP_CLASS:
2618
0
      case OP_NCLASS:
2619
0
      case OP_XCLASS:
2620
0
        {
2621
0
        BOOL isinclass = FALSE;
2622
0
        int next_state_offset;
2623
0
        PCRE2_SPTR ecode;
2624
2625
        /* For a simple class, there is always just a 32-byte table, and we
2626
        can set isinclass from it. */
2627
2628
0
        if (codevalue != OP_XCLASS)
2629
0
          {
2630
0
          ecode = code + 1 + (32 / sizeof(PCRE2_UCHAR));
2631
0
          if (clen > 0)
2632
0
            {
2633
0
            isinclass = (c > 255)? (codevalue == OP_NCLASS) :
2634
0
              ((((uint8_t *)(code + 1))[c/8] & (1u << (c&7))) != 0);
2635
0
            }
2636
0
          }
2637
2638
        /* An extended class may have a table or a list of single characters,
2639
        ranges, or both, and it may be positive or negative. There's a
2640
        function that sorts all this out. */
2641
2642
0
        else
2643
0
         {
2644
0
         ecode = code + GET(code, 1);
2645
0
         if (clen > 0) isinclass = PRIV(xclass)(c, code + 1 + LINK_SIZE, utf);
2646
0
         }
2647
2648
        /* At this point, isinclass is set for all kinds of class, and ecode
2649
        points to the byte after the end of the class. If there is a
2650
        quantifier, this is where it will be. */
2651
2652
0
        next_state_offset = (int)(ecode - start_code);
2653
2654
0
        switch (*ecode)
2655
0
          {
2656
0
          case OP_CRSTAR:
2657
0
          case OP_CRMINSTAR:
2658
0
          case OP_CRPOSSTAR:
2659
0
          ADD_ACTIVE(next_state_offset + 1, 0);
2660
0
          if (isinclass)
2661
0
            {
2662
0
            if (*ecode == OP_CRPOSSTAR)
2663
0
              {
2664
0
              active_count--;           /* Remove non-match possibility */
2665
0
              next_active_state--;
2666
0
              }
2667
0
            ADD_NEW(state_offset, 0);
2668
0
            }
2669
0
          break;
2670
2671
0
          case OP_CRPLUS:
2672
0
          case OP_CRMINPLUS:
2673
0
          case OP_CRPOSPLUS:
2674
0
          count = current_state->count;  /* Already matched */
2675
0
          if (count > 0) { ADD_ACTIVE(next_state_offset + 1, 0); }
2676
0
          if (isinclass)
2677
0
            {
2678
0
            if (count > 0 && *ecode == OP_CRPOSPLUS)
2679
0
              {
2680
0
              active_count--;           /* Remove non-match possibility */
2681
0
              next_active_state--;
2682
0
              }
2683
0
            count++;
2684
0
            ADD_NEW(state_offset, count);
2685
0
            }
2686
0
          break;
2687
2688
0
          case OP_CRQUERY:
2689
0
          case OP_CRMINQUERY:
2690
0
          case OP_CRPOSQUERY:
2691
0
          ADD_ACTIVE(next_state_offset + 1, 0);
2692
0
          if (isinclass)
2693
0
            {
2694
0
            if (*ecode == OP_CRPOSQUERY)
2695
0
              {
2696
0
              active_count--;           /* Remove non-match possibility */
2697
0
              next_active_state--;
2698
0
              }
2699
0
            ADD_NEW(next_state_offset + 1, 0);
2700
0
            }
2701
0
          break;
2702
2703
0
          case OP_CRRANGE:
2704
0
          case OP_CRMINRANGE:
2705
0
          case OP_CRPOSRANGE:
2706
0
          count = current_state->count;  /* Already matched */
2707
0
          if (count >= (int)GET2(ecode, 1))
2708
0
            { ADD_ACTIVE(next_state_offset + 1 + 2 * IMM2_SIZE, 0); }
2709
0
          if (isinclass)
2710
0
            {
2711
0
            int max = (int)GET2(ecode, 1 + IMM2_SIZE);
2712
2713
0
            if (*ecode == OP_CRPOSRANGE && count >= (int)GET2(ecode, 1))
2714
0
              {
2715
0
              active_count--;           /* Remove non-match possibility */
2716
0
              next_active_state--;
2717
0
              }
2718
2719
0
            if (++count >= max && max != 0)   /* Max 0 => no limit */
2720
0
              { ADD_NEW(next_state_offset + 1 + 2 * IMM2_SIZE, 0); }
2721
0
            else
2722
0
              { ADD_NEW(state_offset, count); }
2723
0
            }
2724
0
          break;
2725
2726
0
          default:
2727
0
          if (isinclass) { ADD_NEW(next_state_offset, 0); }
2728
0
          break;
2729
0
          }
2730
0
        }
2731
0
      break;
2732
2733
/* ========================================================================== */
2734
      /* These are the opcodes for fancy brackets of various kinds. We have
2735
      to use recursion in order to handle them. The "always failing" assertion
2736
      (?!) is optimised to OP_FAIL when compiling, so we have to support that,
2737
      though the other "backtracking verbs" are not supported. */
2738
2739
0
      case OP_FAIL:
2740
0
      forced_fail++;    /* Count FAILs for multiple states */
2741
0
      break;
2742
2743
0
      case OP_ASSERT:
2744
0
      case OP_ASSERT_NOT:
2745
0
      case OP_ASSERTBACK:
2746
0
      case OP_ASSERTBACK_NOT:
2747
0
        {
2748
0
        int rc;
2749
0
        int *local_workspace;
2750
0
        PCRE2_SIZE *local_offsets;
2751
0
        PCRE2_SPTR endasscode = code + GET(code, 1);
2752
0
        RWS_anchor *rws = (RWS_anchor *)RWS;
2753
2754
0
        if (rws->free < RWS_RSIZE + RWS_OVEC_OSIZE)
2755
0
          {
2756
0
          rc = more_workspace(&rws, RWS_OVEC_OSIZE, mb);
2757
0
          if (rc != 0) return rc;
2758
0
          RWS = (int *)rws;
2759
0
          }
2760
2761
0
        local_offsets = (PCRE2_SIZE *)(RWS + rws->size - rws->free);
2762
0
        local_workspace = ((int *)local_offsets) + RWS_OVEC_OSIZE;
2763
0
        rws->free -= RWS_RSIZE + RWS_OVEC_OSIZE;
2764
2765
0
        while (*endasscode == OP_ALT) endasscode += GET(endasscode, 1);
2766
2767
0
        rc = internal_dfa_match(
2768
0
          mb,                                   /* static match data */
2769
0
          code,                                 /* this subexpression's code */
2770
0
          ptr,                                  /* where we currently are */
2771
0
          (PCRE2_SIZE)(ptr - start_subject),    /* start offset */
2772
0
          local_offsets,                        /* offset vector */
2773
0
          RWS_OVEC_OSIZE/OVEC_UNIT,             /* size of same */
2774
0
          local_workspace,                      /* workspace vector */
2775
0
          RWS_RSIZE,                            /* size of same */
2776
0
          rlevel,                               /* function recursion level */
2777
0
          RWS);                                 /* recursion workspace */
2778
2779
0
        rws->free += RWS_RSIZE + RWS_OVEC_OSIZE;
2780
2781
0
        if (rc < 0 && rc != PCRE2_ERROR_NOMATCH) return rc;
2782
0
        if ((rc >= 0) == (codevalue == OP_ASSERT || codevalue == OP_ASSERTBACK))
2783
0
            { ADD_ACTIVE((int)(endasscode + LINK_SIZE + 1 - start_code), 0); }
2784
0
        }
2785
0
      break;
2786
2787
      /*-----------------------------------------------------------------*/
2788
0
      case OP_COND:
2789
0
      case OP_SCOND:
2790
0
        {
2791
0
        int codelink = (int)GET(code, 1);
2792
0
        PCRE2_UCHAR condcode;
2793
2794
        /* Because of the way auto-callout works during compile, a callout item
2795
        is inserted between OP_COND and an assertion condition. This does not
2796
        happen for the other conditions. */
2797
2798
0
        if (code[LINK_SIZE + 1] == OP_CALLOUT
2799
0
            || code[LINK_SIZE + 1] == OP_CALLOUT_STR)
2800
0
          {
2801
0
          PCRE2_SIZE callout_length;
2802
0
          rrc = do_callout_dfa(code, offsets, current_subject, ptr, mb,
2803
0
            1 + LINK_SIZE, &callout_length);
2804
0
          if (rrc < 0) return rrc;                 /* Abandon */
2805
0
          if (rrc > 0) break;                      /* Fail this thread */
2806
0
          code += callout_length;                  /* Skip callout data */
2807
0
          }
2808
2809
0
        condcode = code[LINK_SIZE+1];
2810
2811
        /* Back reference conditions and duplicate named recursion conditions
2812
        are not supported */
2813
2814
0
        if (condcode == OP_CREF || condcode == OP_DNCREF ||
2815
0
            condcode == OP_DNRREF)
2816
0
          return PCRE2_ERROR_DFA_UCOND;
2817
2818
        /* The DEFINE condition is always false, and the assertion (?!) is
2819
        converted to OP_FAIL. */
2820
2821
0
        if (condcode == OP_FALSE || condcode == OP_FAIL)
2822
0
          { ADD_ACTIVE(state_offset + codelink + LINK_SIZE + 1, 0); }
2823
2824
        /* There is also an always-true condition */
2825
2826
0
        else if (condcode == OP_TRUE)
2827
0
          { ADD_ACTIVE(state_offset + LINK_SIZE + 2, 0); }
2828
2829
        /* The only supported version of OP_RREF is for the value RREF_ANY,
2830
        which means "test if in any recursion". We can't test for specifically
2831
        recursed groups. */
2832
2833
0
        else if (condcode == OP_RREF)
2834
0
          {
2835
0
          unsigned int value = GET2(code, LINK_SIZE + 2);
2836
0
          if (value != RREF_ANY) return PCRE2_ERROR_DFA_UCOND;
2837
0
          if (mb->recursive != NULL)
2838
0
            { ADD_ACTIVE(state_offset + LINK_SIZE + 2 + IMM2_SIZE, 0); }
2839
0
          else { ADD_ACTIVE(state_offset + codelink + LINK_SIZE + 1, 0); }
2840
0
          }
2841
2842
        /* Otherwise, the condition is an assertion */
2843
2844
0
        else
2845
0
          {
2846
0
          int rc;
2847
0
          int *local_workspace;
2848
0
          PCRE2_SIZE *local_offsets;
2849
0
          PCRE2_SPTR asscode = code + LINK_SIZE + 1;
2850
0
          PCRE2_SPTR endasscode = asscode + GET(asscode, 1);
2851
0
          RWS_anchor *rws = (RWS_anchor *)RWS;
2852
2853
0
          if (rws->free < RWS_RSIZE + RWS_OVEC_OSIZE)
2854
0
            {
2855
0
            rc = more_workspace(&rws, RWS_OVEC_OSIZE, mb);
2856
0
            if (rc != 0) return rc;
2857
0
            RWS = (int *)rws;
2858
0
            }
2859
2860
0
          local_offsets = (PCRE2_SIZE *)(RWS + rws->size - rws->free);
2861
0
          local_workspace = ((int *)local_offsets) + RWS_OVEC_OSIZE;
2862
0
          rws->free -= RWS_RSIZE + RWS_OVEC_OSIZE;
2863
2864
0
          while (*endasscode == OP_ALT) endasscode += GET(endasscode, 1);
2865
2866
0
          rc = internal_dfa_match(
2867
0
            mb,                                   /* fixed match data */
2868
0
            asscode,                              /* this subexpression's code */
2869
0
            ptr,                                  /* where we currently are */
2870
0
            (PCRE2_SIZE)(ptr - start_subject),    /* start offset */
2871
0
            local_offsets,                        /* offset vector */
2872
0
            RWS_OVEC_OSIZE/OVEC_UNIT,             /* size of same */
2873
0
            local_workspace,                      /* workspace vector */
2874
0
            RWS_RSIZE,                            /* size of same */
2875
0
            rlevel,                               /* function recursion level */
2876
0
            RWS);                                 /* recursion workspace */
2877
2878
0
          rws->free += RWS_RSIZE + RWS_OVEC_OSIZE;
2879
2880
0
          if (rc < 0 && rc != PCRE2_ERROR_NOMATCH) return rc;
2881
0
          if ((rc >= 0) ==
2882
0
                (condcode == OP_ASSERT || condcode == OP_ASSERTBACK))
2883
0
            { ADD_ACTIVE((int)(endasscode + LINK_SIZE + 1 - start_code), 0); }
2884
0
          else
2885
0
            { ADD_ACTIVE(state_offset + codelink + LINK_SIZE + 1, 0); }
2886
0
          }
2887
0
        }
2888
0
      break;
2889
2890
      /*-----------------------------------------------------------------*/
2891
0
      case OP_RECURSE:
2892
0
        {
2893
0
        int rc;
2894
0
        int *local_workspace;
2895
0
        PCRE2_SIZE *local_offsets;
2896
0
        RWS_anchor *rws = (RWS_anchor *)RWS;
2897
0
        dfa_recursion_info *ri;
2898
0
        PCRE2_SPTR callpat = start_code + GET(code, 1);
2899
0
        uint32_t recno = (callpat == mb->start_code)? 0 :
2900
0
          GET2(callpat, 1 + LINK_SIZE);
2901
2902
0
        if (rws->free < RWS_RSIZE + RWS_OVEC_RSIZE)
2903
0
          {
2904
0
          rc = more_workspace(&rws, RWS_OVEC_RSIZE, mb);
2905
0
          if (rc != 0) return rc;
2906
0
          RWS = (int *)rws;
2907
0
          }
2908
2909
0
        local_offsets = (PCRE2_SIZE *)(RWS + rws->size - rws->free);
2910
0
        local_workspace = ((int *)local_offsets) + RWS_OVEC_RSIZE;
2911
0
        rws->free -= RWS_RSIZE + RWS_OVEC_RSIZE;
2912
2913
        /* Check for repeating a recursion without advancing the subject
2914
        pointer. This should catch convoluted mutual recursions. (Some simple
2915
        cases are caught at compile time.) */
2916
2917
0
        for (ri = mb->recursive; ri != NULL; ri = ri->prevrec)
2918
0
          if (recno == ri->group_num && ptr == ri->subject_position)
2919
0
            return PCRE2_ERROR_RECURSELOOP;
2920
2921
        /* Remember this recursion and where we started it so as to
2922
        catch infinite loops. */
2923
2924
0
        new_recursive.group_num = recno;
2925
0
        new_recursive.subject_position = ptr;
2926
0
        new_recursive.prevrec = mb->recursive;
2927
0
        mb->recursive = &new_recursive;
2928
2929
0
        rc = internal_dfa_match(
2930
0
          mb,                                   /* fixed match data */
2931
0
          callpat,                              /* this subexpression's code */
2932
0
          ptr,                                  /* where we currently are */
2933
0
          (PCRE2_SIZE)(ptr - start_subject),    /* start offset */
2934
0
          local_offsets,                        /* offset vector */
2935
0
          RWS_OVEC_RSIZE/OVEC_UNIT,             /* size of same */
2936
0
          local_workspace,                      /* workspace vector */
2937
0
          RWS_RSIZE,                            /* size of same */
2938
0
          rlevel,                               /* function recursion level */
2939
0
          RWS);                                 /* recursion workspace */
2940
2941
0
        rws->free += RWS_RSIZE + RWS_OVEC_RSIZE;
2942
0
        mb->recursive = new_recursive.prevrec;  /* Done this recursion */
2943
2944
        /* Ran out of internal offsets */
2945
2946
0
        if (rc == 0) return PCRE2_ERROR_DFA_RECURSE;
2947
2948
        /* For each successful matched substring, set up the next state with a
2949
        count of characters to skip before trying it. Note that the count is in
2950
        characters, not bytes. */
2951
2952
0
        if (rc > 0)
2953
0
          {
2954
0
          for (rc = rc*2 - 2; rc >= 0; rc -= 2)
2955
0
            {
2956
0
            PCRE2_SIZE charcount = local_offsets[rc+1] - local_offsets[rc];
2957
0
#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32
2958
0
            if (utf)
2959
0
              {
2960
0
              PCRE2_SPTR p = start_subject + local_offsets[rc];
2961
0
              PCRE2_SPTR pp = start_subject + local_offsets[rc+1];
2962
0
              while (p < pp) if (NOT_FIRSTCU(*p++)) charcount--;
2963
0
              }
2964
0
#endif
2965
0
            if (charcount > 0)
2966
0
              {
2967
0
              ADD_NEW_DATA(-(state_offset + LINK_SIZE + 1), 0,
2968
0
                (int)(charcount - 1));
2969
0
              }
2970
0
            else
2971
0
              {
2972
0
              ADD_ACTIVE(state_offset + LINK_SIZE + 1, 0);
2973
0
              }
2974
0
            }
2975
0
          }
2976
0
        else if (rc != PCRE2_ERROR_NOMATCH) return rc;
2977
0
        }
2978
0
      break;
2979
2980
      /*-----------------------------------------------------------------*/
2981
0
      case OP_BRAPOS:
2982
0
      case OP_SBRAPOS:
2983
0
      case OP_CBRAPOS:
2984
0
      case OP_SCBRAPOS:
2985
0
      case OP_BRAPOSZERO:
2986
0
        {
2987
0
        int rc;
2988
0
        int *local_workspace;
2989
0
        PCRE2_SIZE *local_offsets;
2990
0
        PCRE2_SIZE charcount, matched_count;
2991
0
        PCRE2_SPTR local_ptr = ptr;
2992
0
        RWS_anchor *rws = (RWS_anchor *)RWS;
2993
0
        BOOL allow_zero;
2994
2995
0
        if (rws->free < RWS_RSIZE + RWS_OVEC_OSIZE)
2996
0
          {
2997
0
          rc = more_workspace(&rws, RWS_OVEC_OSIZE, mb);
2998
0
          if (rc != 0) return rc;
2999
0
          RWS = (int *)rws;
3000
0
          }
3001
3002
0
        local_offsets = (PCRE2_SIZE *)(RWS + rws->size - rws->free);
3003
0
        local_workspace = ((int *)local_offsets) + RWS_OVEC_OSIZE;
3004
0
        rws->free -= RWS_RSIZE + RWS_OVEC_OSIZE;
3005
3006
0
        if (codevalue == OP_BRAPOSZERO)
3007
0
          {
3008
0
          allow_zero = TRUE;
3009
0
          codevalue = *(++code);  /* Codevalue will be one of above BRAs */
3010
0
          }
3011
0
        else allow_zero = FALSE;
3012
3013
        /* Loop to match the subpattern as many times as possible as if it were
3014
        a complete pattern. */
3015
3016
0
        for (matched_count = 0;; matched_count++)
3017
0
          {
3018
0
          rc = internal_dfa_match(
3019
0
            mb,                                   /* fixed match data */
3020
0
            code,                                 /* this subexpression's code */
3021
0
            local_ptr,                            /* where we currently are */
3022
0
            (PCRE2_SIZE)(ptr - start_subject),    /* start offset */
3023
0
            local_offsets,                        /* offset vector */
3024
0
            RWS_OVEC_OSIZE/OVEC_UNIT,             /* size of same */
3025
0
            local_workspace,                      /* workspace vector */
3026
0
            RWS_RSIZE,                            /* size of same */
3027
0
            rlevel,                               /* function recursion level */
3028
0
            RWS);                                 /* recursion workspace */
3029
3030
          /* Failed to match */
3031
3032
0
          if (rc < 0)
3033
0
            {
3034
0
            if (rc != PCRE2_ERROR_NOMATCH) return rc;
3035
0
            break;
3036
0
            }
3037
3038
          /* Matched: break the loop if zero characters matched. */
3039
3040
0
          charcount = local_offsets[1] - local_offsets[0];
3041
0
          if (charcount == 0) break;
3042
0
          local_ptr += charcount;    /* Advance temporary position ptr */
3043
0
          }
3044
3045
0
        rws->free += RWS_RSIZE + RWS_OVEC_OSIZE;
3046
3047
        /* At this point we have matched the subpattern matched_count
3048
        times, and local_ptr is pointing to the character after the end of the
3049
        last match. */
3050
3051
0
        if (matched_count > 0 || allow_zero)
3052
0
          {
3053
0
          PCRE2_SPTR end_subpattern = code;
3054
0
          int next_state_offset;
3055
3056
0
          do { end_subpattern += GET(end_subpattern, 1); }
3057
0
            while (*end_subpattern == OP_ALT);
3058
0
          next_state_offset =
3059
0
            (int)(end_subpattern - start_code + LINK_SIZE + 1);
3060
3061
          /* Optimization: if there are no more active states, and there
3062
          are no new states yet set up, then skip over the subject string
3063
          right here, to save looping. Otherwise, set up the new state to swing
3064
          into action when the end of the matched substring is reached. */
3065
3066
0
          if (i + 1 >= active_count && new_count == 0)
3067
0
            {
3068
0
            ptr = local_ptr;
3069
0
            clen = 0;
3070
0
            ADD_NEW(next_state_offset, 0);
3071
0
            }
3072
0
          else
3073
0
            {
3074
0
            PCRE2_SPTR p = ptr;
3075
0
            PCRE2_SPTR pp = local_ptr;
3076
0
            charcount = (PCRE2_SIZE)(pp - p);
3077
0
#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32
3078
0
            if (utf) while (p < pp) if (NOT_FIRSTCU(*p++)) charcount--;
3079
0
#endif
3080
0
            ADD_NEW_DATA(-next_state_offset, 0, (int)(charcount - 1));
3081
0
            }
3082
0
          }
3083
0
        }
3084
0
      break;
3085
3086
      /*-----------------------------------------------------------------*/
3087
0
      case OP_ONCE:
3088
0
        {
3089
0
        int rc;
3090
0
        int *local_workspace;
3091
0
        PCRE2_SIZE *local_offsets;
3092
0
        RWS_anchor *rws = (RWS_anchor *)RWS;
3093
3094
0
        if (rws->free < RWS_RSIZE + RWS_OVEC_OSIZE)
3095
0
          {
3096
0
          rc = more_workspace(&rws, RWS_OVEC_OSIZE, mb);
3097
0
          if (rc != 0) return rc;
3098
0
          RWS = (int *)rws;
3099
0
          }
3100
3101
0
        local_offsets = (PCRE2_SIZE *)(RWS + rws->size - rws->free);
3102
0
        local_workspace = ((int *)local_offsets) + RWS_OVEC_OSIZE;
3103
0
        rws->free -= RWS_RSIZE + RWS_OVEC_OSIZE;
3104
3105
0
        rc = internal_dfa_match(
3106
0
          mb,                                   /* fixed match data */
3107
0
          code,                                 /* this subexpression's code */
3108
0
          ptr,                                  /* where we currently are */
3109
0
          (PCRE2_SIZE)(ptr - start_subject),    /* start offset */
3110
0
          local_offsets,                        /* offset vector */
3111
0
          RWS_OVEC_OSIZE/OVEC_UNIT,             /* size of same */
3112
0
          local_workspace,                      /* workspace vector */
3113
0
          RWS_RSIZE,                            /* size of same */
3114
0
          rlevel,                               /* function recursion level */
3115
0
          RWS);                                 /* recursion workspace */
3116
3117
0
        rws->free += RWS_RSIZE + RWS_OVEC_OSIZE;
3118
3119
0
        if (rc >= 0)
3120
0
          {
3121
0
          PCRE2_SPTR end_subpattern = code;
3122
0
          PCRE2_SIZE charcount = local_offsets[1] - local_offsets[0];
3123
0
          int next_state_offset, repeat_state_offset;
3124
3125
0
          do { end_subpattern += GET(end_subpattern, 1); }
3126
0
            while (*end_subpattern == OP_ALT);
3127
0
          next_state_offset =
3128
0
            (int)(end_subpattern - start_code + LINK_SIZE + 1);
3129
3130
          /* If the end of this subpattern is KETRMAX or KETRMIN, we must
3131
          arrange for the repeat state also to be added to the relevant list.
3132
          Calculate the offset, or set -1 for no repeat. */
3133
3134
0
          repeat_state_offset = (*end_subpattern == OP_KETRMAX ||
3135
0
                                 *end_subpattern == OP_KETRMIN)?
3136
0
            (int)(end_subpattern - start_code - GET(end_subpattern, 1)) : -1;
3137
3138
          /* If we have matched an empty string, add the next state at the
3139
          current character pointer. This is important so that the duplicate
3140
          checking kicks in, which is what breaks infinite loops that match an
3141
          empty string. */
3142
3143
0
          if (charcount == 0)
3144
0
            {
3145
0
            ADD_ACTIVE(next_state_offset, 0);
3146
0
            }
3147
3148
          /* Optimization: if there are no more active states, and there
3149
          are no new states yet set up, then skip over the subject string
3150
          right here, to save looping. Otherwise, set up the new state to swing
3151
          into action when the end of the matched substring is reached. */
3152
3153
0
          else if (i + 1 >= active_count && new_count == 0)
3154
0
            {
3155
0
            ptr += charcount;
3156
0
            clen = 0;
3157
0
            ADD_NEW(next_state_offset, 0);
3158
3159
            /* If we are adding a repeat state at the new character position,
3160
            we must fudge things so that it is the only current state.
3161
            Otherwise, it might be a duplicate of one we processed before, and
3162
            that would cause it to be skipped. */
3163
3164
0
            if (repeat_state_offset >= 0)
3165
0
              {
3166
0
              next_active_state = active_states;
3167
0
              active_count = 0;
3168
0
              i = -1;
3169
0
              ADD_ACTIVE(repeat_state_offset, 0);
3170
0
              }
3171
0
            }
3172
0
          else
3173
0
            {
3174
0
#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH != 32
3175
0
            if (utf)
3176
0
              {
3177
0
              PCRE2_SPTR p = start_subject + local_offsets[0];
3178
0
              PCRE2_SPTR pp = start_subject + local_offsets[1];
3179
0
              while (p < pp) if (NOT_FIRSTCU(*p++)) charcount--;
3180
0
              }
3181
0
#endif
3182
0
            ADD_NEW_DATA(-next_state_offset, 0, (int)(charcount - 1));
3183
0
            if (repeat_state_offset >= 0)
3184
0
              { ADD_NEW_DATA(-repeat_state_offset, 0, (int)(charcount - 1)); }
3185
0
            }
3186
0
          }
3187
0
        else if (rc != PCRE2_ERROR_NOMATCH) return rc;
3188
0
        }
3189
0
      break;
3190
3191
3192
/* ========================================================================== */
3193
      /* Handle callouts */
3194
3195
0
      case OP_CALLOUT:
3196
0
      case OP_CALLOUT_STR:
3197
0
        {
3198
0
        PCRE2_SIZE callout_length;
3199
0
        rrc = do_callout_dfa(code, offsets, current_subject, ptr, mb, 0,
3200
0
          &callout_length);
3201
0
        if (rrc < 0) return rrc;   /* Abandon */
3202
0
        if (rrc == 0)
3203
0
          { ADD_ACTIVE(state_offset + (int)callout_length, 0); }
3204
0
        }
3205
0
      break;
3206
3207
3208
/* ========================================================================== */
3209
0
      default:        /* Unsupported opcode */
3210
0
      return PCRE2_ERROR_DFA_UITEM;
3211
0
      }
3212
3213
0
    NEXT_ACTIVE_STATE: continue;
3214
3215
0
    }      /* End of loop scanning active states */
3216
3217
  /* We have finished the processing at the current subject character. If no
3218
  new states have been set for the next character, we have found all the
3219
  matches that we are going to find. If partial matching has been requested,
3220
  check for appropriate conditions.
3221
3222
  The "forced_ fail" variable counts the number of (*F) encountered for the
3223
  character. If it is equal to the original active_count (saved in
3224
  workspace[1]) it means that (*F) was found on every active state. In this
3225
  case we don't want to give a partial match.
3226
3227
  The "could_continue" variable is true if a state could have continued but
3228
  for the fact that the end of the subject was reached. */
3229
3230
0
  if (new_count <= 0)
3231
0
    {
3232
0
    if (could_continue &&                            /* Some could go on, and */
3233
0
        forced_fail != workspace[1] &&               /* Not all forced fail & */
3234
0
        (                                            /* either... */
3235
0
        (mb->moptions & PCRE2_PARTIAL_HARD) != 0      /* Hard partial */
3236
0
        ||                                           /* or... */
3237
0
        ((mb->moptions & PCRE2_PARTIAL_SOFT) != 0 &&  /* Soft partial and */
3238
0
         match_count < 0)                             /* no matches */
3239
0
        ) &&                                         /* And... */
3240
0
        (
3241
0
        partial_newline ||                   /* Either partial NL */
3242
0
          (                                  /* or ... */
3243
0
          ptr >= end_subject &&              /* End of subject and */
3244
0
            (                                  /* either */
3245
0
            ptr > mb->start_used_ptr ||        /* Inspected non-empty string */
3246
0
            mb->allowemptypartial              /* or pattern has lookbehind */
3247
0
            )                                  /* or could match empty */
3248
0
          )
3249
0
        ))
3250
0
      match_count = PCRE2_ERROR_PARTIAL;
3251
0
    break;  /* Exit from loop along the subject string */
3252
0
    }
3253
3254
  /* One or more states are active for the next character. */
3255
3256
0
  ptr += clen;    /* Advance to next subject character */
3257
0
  }               /* Loop to move along the subject string */
3258
3259
/* Control gets here from "break" a few lines above. If we have a match and
3260
PCRE2_ENDANCHORED is set, the match fails. */
3261
3262
0
if (match_count >= 0 &&
3263
0
    ((mb->moptions | mb->poptions) & PCRE2_ENDANCHORED) != 0 &&
3264
0
    ptr < end_subject)
3265
0
  match_count = PCRE2_ERROR_NOMATCH;
3266
3267
0
return match_count;
3268
0
}
3269
3270
3271
3272
/*************************************************
3273
*     Match a pattern using the DFA algorithm    *
3274
*************************************************/
3275
3276
/* This function matches a compiled pattern to a subject string, using the
3277
alternate matching algorithm that finds all matches at once.
3278
3279
Arguments:
3280
  code          points to the compiled pattern
3281
  subject       subject string
3282
  length        length of subject string
3283
  startoffset   where to start matching in the subject
3284
  options       option bits
3285
  match_data    points to a match data structure
3286
  gcontext      points to a match context
3287
  workspace     pointer to workspace
3288
  wscount       size of workspace
3289
3290
Returns:        > 0 => number of match offset pairs placed in offsets
3291
                = 0 => offsets overflowed; longest matches are present
3292
                 -1 => failed to match
3293
               < -1 => some kind of unexpected problem
3294
*/
3295
3296
PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
3297
pcre2_dfa_match(const pcre2_code *code, PCRE2_SPTR subject, PCRE2_SIZE length,
3298
  PCRE2_SIZE start_offset, uint32_t options, pcre2_match_data *match_data,
3299
  pcre2_match_context *mcontext, int *workspace, PCRE2_SIZE wscount)
3300
0
{
3301
0
int rc;
3302
0
int was_zero_terminated = 0;
3303
3304
0
const pcre2_real_code *re = (const pcre2_real_code *)code;
3305
3306
0
PCRE2_SPTR start_match;
3307
0
PCRE2_SPTR end_subject;
3308
0
PCRE2_SPTR bumpalong_limit;
3309
0
PCRE2_SPTR req_cu_ptr;
3310
3311
0
BOOL utf, anchored, startline, firstline;
3312
0
BOOL has_first_cu = FALSE;
3313
0
BOOL has_req_cu = FALSE;
3314
3315
0
#if PCRE2_CODE_UNIT_WIDTH == 8
3316
0
PCRE2_SPTR memchr_found_first_cu = NULL;
3317
0
PCRE2_SPTR memchr_found_first_cu2 = NULL;
3318
0
#endif
3319
3320
0
PCRE2_UCHAR first_cu = 0;
3321
0
PCRE2_UCHAR first_cu2 = 0;
3322
0
PCRE2_UCHAR req_cu = 0;
3323
0
PCRE2_UCHAR req_cu2 = 0;
3324
3325
0
const uint8_t *start_bits = NULL;
3326
3327
/* We need to have mb pointing to a match block, because the IS_NEWLINE macro
3328
is used below, and it expects NLBLOCK to be defined as a pointer. */
3329
3330
0
pcre2_callout_block cb;
3331
0
dfa_match_block actual_match_block;
3332
0
dfa_match_block *mb = &actual_match_block;
3333
3334
/* Set up a starting block of memory for use during recursive calls to
3335
internal_dfa_match(). By putting this on the stack, it minimizes resource use
3336
in the case when it is not needed. If this is too small, more memory is
3337
obtained from the heap. At the start of each block is an anchor structure.*/
3338
3339
0
int base_recursion_workspace[RWS_BASE_SIZE];
3340
0
RWS_anchor *rws = (RWS_anchor *)base_recursion_workspace;
3341
0
rws->next = NULL;
3342
0
rws->size = RWS_BASE_SIZE;
3343
0
rws->free = RWS_BASE_SIZE - RWS_ANCHOR_SIZE;
3344
3345
/* Recognize NULL, length 0 as an empty string. */
3346
3347
0
if (subject == NULL && length == 0) subject = (PCRE2_SPTR)"";
3348
3349
/* Plausibility checks */
3350
3351
0
if ((options & ~PUBLIC_DFA_MATCH_OPTIONS) != 0) return PCRE2_ERROR_BADOPTION;
3352
0
if (re == NULL || subject == NULL || workspace == NULL || match_data == NULL)
3353
0
  return PCRE2_ERROR_NULL;
3354
3355
0
if (length == PCRE2_ZERO_TERMINATED)
3356
0
  {
3357
0
  length = PRIV(strlen)(subject);
3358
0
  was_zero_terminated = 1;
3359
0
  }
3360
3361
0
if (wscount < 20) return PCRE2_ERROR_DFA_WSSIZE;
3362
0
if (start_offset > length) return PCRE2_ERROR_BADOFFSET;
3363
3364
/* Partial matching and PCRE2_ENDANCHORED are currently not allowed at the same
3365
time. */
3366
3367
0
if ((options & (PCRE2_PARTIAL_HARD|PCRE2_PARTIAL_SOFT)) != 0 &&
3368
0
   ((re->overall_options | options) & PCRE2_ENDANCHORED) != 0)
3369
0
  return PCRE2_ERROR_BADOPTION;
3370
3371
/* Invalid UTF support is not available for DFA matching. */
3372
3373
0
if ((re->overall_options & PCRE2_MATCH_INVALID_UTF) != 0)
3374
0
  return PCRE2_ERROR_DFA_UINVALID_UTF;
3375
3376
/* Check that the first field in the block is the magic number. If it is not,
3377
return with PCRE2_ERROR_BADMAGIC. */
3378
3379
0
if (re->magic_number != MAGIC_NUMBER) return PCRE2_ERROR_BADMAGIC;
3380
3381
/* Check the code unit width. */
3382
3383
0
if ((re->flags & PCRE2_MODE_MASK) != PCRE2_CODE_UNIT_WIDTH/8)
3384
0
  return PCRE2_ERROR_BADMODE;
3385
3386
/* PCRE2_NOTEMPTY and PCRE2_NOTEMPTY_ATSTART are match-time flags in the
3387
options variable for this function. Users of PCRE2 who are not calling the
3388
function directly would like to have a way of setting these flags, in the same
3389
way that they can set pcre2_compile() flags like PCRE2_NO_AUTOPOSSESS with
3390
constructions like (*NO_AUTOPOSSESS). To enable this, (*NOTEMPTY) and
3391
(*NOTEMPTY_ATSTART) set bits in the pattern's "flag" function which can now be
3392
transferred to the options for this function. The bits are guaranteed to be
3393
adjacent, but do not have the same values. This bit of Boolean trickery assumes
3394
that the match-time bits are not more significant than the flag bits. If by
3395
accident this is not the case, a compile-time division by zero error will
3396
occur. */
3397
3398
0
#define FF (PCRE2_NOTEMPTY_SET|PCRE2_NE_ATST_SET)
3399
0
#define OO (PCRE2_NOTEMPTY|PCRE2_NOTEMPTY_ATSTART)
3400
0
options |= (re->flags & FF) / ((FF & (~FF+1)) / (OO & (~OO+1)));
3401
0
#undef FF
3402
0
#undef OO
3403
3404
/* If restarting after a partial match, do some sanity checks on the contents
3405
of the workspace. */
3406
3407
0
if ((options & PCRE2_DFA_RESTART) != 0)
3408
0
  {
3409
0
  if ((workspace[0] & (-2)) != 0 || workspace[1] < 1 ||
3410
0
    workspace[1] > (int)((wscount - 2)/INTS_PER_STATEBLOCK))
3411
0
      return PCRE2_ERROR_DFA_BADRESTART;
3412
0
  }
3413
3414
/* Set some local values */
3415
3416
0
utf = (re->overall_options & PCRE2_UTF) != 0;
3417
0
start_match = subject + start_offset;
3418
0
end_subject = subject + length;
3419
0
req_cu_ptr = start_match - 1;
3420
0
anchored = (options & (PCRE2_ANCHORED|PCRE2_DFA_RESTART)) != 0 ||
3421
0
  (re->overall_options & PCRE2_ANCHORED) != 0;
3422
3423
/* The "must be at the start of a line" flags are used in a loop when finding
3424
where to start. */
3425
3426
0
startline = (re->flags & PCRE2_STARTLINE) != 0;
3427
0
firstline = (re->overall_options & PCRE2_FIRSTLINE) != 0;
3428
0
bumpalong_limit = end_subject;
3429
3430
/* Initialize and set up the fixed fields in the callout block, with a pointer
3431
in the match block. */
3432
3433
0
mb->cb = &cb;
3434
0
cb.version = 2;
3435
0
cb.subject = subject;
3436
0
cb.subject_length = (PCRE2_SIZE)(end_subject - subject);
3437
0
cb.callout_flags = 0;
3438
0
cb.capture_top      = 1;      /* No capture support */
3439
0
cb.capture_last     = 0;
3440
0
cb.mark             = NULL;   /* No (*MARK) support */
3441
3442
/* Get data from the match context, if present, and fill in the remaining
3443
fields in the match block. It is an error to set an offset limit without
3444
setting the flag at compile time. */
3445
3446
0
if (mcontext == NULL)
3447
0
  {
3448
0
  mb->callout = NULL;
3449
0
  mb->memctl = re->memctl;
3450
0
  mb->match_limit = PRIV(default_match_context).match_limit;
3451
0
  mb->match_limit_depth = PRIV(default_match_context).depth_limit;
3452
0
  mb->heap_limit = PRIV(default_match_context).heap_limit;
3453
0
  }
3454
0
else
3455
0
  {
3456
0
  if (mcontext->offset_limit != PCRE2_UNSET)
3457
0
    {
3458
0
    if ((re->overall_options & PCRE2_USE_OFFSET_LIMIT) == 0)
3459
0
      return PCRE2_ERROR_BADOFFSETLIMIT;
3460
0
    bumpalong_limit = subject + mcontext->offset_limit;
3461
0
    }
3462
0
  mb->callout = mcontext->callout;
3463
0
  mb->callout_data = mcontext->callout_data;
3464
0
  mb->memctl = mcontext->memctl;
3465
0
  mb->match_limit = mcontext->match_limit;
3466
0
  mb->match_limit_depth = mcontext->depth_limit;
3467
0
  mb->heap_limit = mcontext->heap_limit;
3468
0
  }
3469
3470
0
if (mb->match_limit > re->limit_match)
3471
0
  mb->match_limit = re->limit_match;
3472
3473
0
if (mb->match_limit_depth > re->limit_depth)
3474
0
  mb->match_limit_depth = re->limit_depth;
3475
3476
0
if (mb->heap_limit > re->limit_heap)
3477
0
  mb->heap_limit = re->limit_heap;
3478
3479
0
mb->start_code = (PCRE2_UCHAR *)((uint8_t *)re + sizeof(pcre2_real_code)) +
3480
0
  re->name_count * re->name_entry_size;
3481
0
mb->tables = re->tables;
3482
0
mb->start_subject = subject;
3483
0
mb->end_subject = end_subject;
3484
0
mb->start_offset = start_offset;
3485
0
mb->allowemptypartial = (re->max_lookbehind > 0) ||
3486
0
  (re->flags & PCRE2_MATCH_EMPTY) != 0;
3487
0
mb->moptions = options;
3488
0
mb->poptions = re->overall_options;
3489
0
mb->match_call_count = 0;
3490
0
mb->heap_used = 0;
3491
3492
/* Process the \R and newline settings. */
3493
3494
0
mb->bsr_convention = re->bsr_convention;
3495
0
mb->nltype = NLTYPE_FIXED;
3496
0
switch(re->newline_convention)
3497
0
  {
3498
0
  case PCRE2_NEWLINE_CR:
3499
0
  mb->nllen = 1;
3500
0
  mb->nl[0] = CHAR_CR;
3501
0
  break;
3502
3503
0
  case PCRE2_NEWLINE_LF:
3504
0
  mb->nllen = 1;
3505
0
  mb->nl[0] = CHAR_NL;
3506
0
  break;
3507
3508
0
  case PCRE2_NEWLINE_NUL:
3509
0
  mb->nllen = 1;
3510
0
  mb->nl[0] = CHAR_NUL;
3511
0
  break;
3512
3513
0
  case PCRE2_NEWLINE_CRLF:
3514
0
  mb->nllen = 2;
3515
0
  mb->nl[0] = CHAR_CR;
3516
0
  mb->nl[1] = CHAR_NL;
3517
0
  break;
3518
3519
0
  case PCRE2_NEWLINE_ANY:
3520
0
  mb->nltype = NLTYPE_ANY;
3521
0
  break;
3522
3523
0
  case PCRE2_NEWLINE_ANYCRLF:
3524
0
  mb->nltype = NLTYPE_ANYCRLF;
3525
0
  break;
3526
3527
0
  default: return PCRE2_ERROR_INTERNAL;
3528
0
  }
3529
3530
/* Check a UTF string for validity if required. For 8-bit and 16-bit strings,
3531
we must also check that a starting offset does not point into the middle of a
3532
multiunit character. We check only the portion of the subject that is going to
3533
be inspected during matching - from the offset minus the maximum back reference
3534
to the given length. This saves time when a small part of a large subject is
3535
being matched by the use of a starting offset. Note that the maximum lookbehind
3536
is a number of characters, not code units. */
3537
3538
0
#ifdef SUPPORT_UNICODE
3539
0
if (utf && (options & PCRE2_NO_UTF_CHECK) == 0)
3540
0
  {
3541
0
  PCRE2_SPTR check_subject = start_match;  /* start_match includes offset */
3542
3543
0
  if (start_offset > 0)
3544
0
    {
3545
0
#if PCRE2_CODE_UNIT_WIDTH != 32
3546
0
    unsigned int i;
3547
0
    if (start_match < end_subject && NOT_FIRSTCU(*start_match))
3548
0
      return PCRE2_ERROR_BADUTFOFFSET;
3549
0
    for (i = re->max_lookbehind; i > 0 && check_subject > subject; i--)
3550
0
      {
3551
0
      check_subject--;
3552
0
      while (check_subject > subject &&
3553
0
#if PCRE2_CODE_UNIT_WIDTH == 8
3554
0
      (*check_subject & 0xc0) == 0x80)
3555
#else  /* 16-bit */
3556
      (*check_subject & 0xfc00) == 0xdc00)
3557
#endif /* PCRE2_CODE_UNIT_WIDTH == 8 */
3558
0
        check_subject--;
3559
0
      }
3560
#else   /* In the 32-bit library, one code unit equals one character. */
3561
    check_subject -= re->max_lookbehind;
3562
    if (check_subject < subject) check_subject = subject;
3563
#endif  /* PCRE2_CODE_UNIT_WIDTH != 32 */
3564
0
    }
3565
3566
  /* Validate the relevant portion of the subject. After an error, adjust the
3567
  offset to be an absolute offset in the whole string. */
3568
3569
0
  match_data->rc = PRIV(valid_utf)(check_subject,
3570
0
    length - (PCRE2_SIZE)(check_subject - subject), &(match_data->startchar));
3571
0
  if (match_data->rc != 0)
3572
0
    {
3573
0
    match_data->startchar += (PCRE2_SIZE)(check_subject - subject);
3574
0
    return match_data->rc;
3575
0
    }
3576
0
  }
3577
0
#endif  /* SUPPORT_UNICODE */
3578
3579
/* Set up the first code unit to match, if available. If there's no first code
3580
unit there may be a bitmap of possible first characters. */
3581
3582
0
if ((re->flags & PCRE2_FIRSTSET) != 0)
3583
0
  {
3584
0
  has_first_cu = TRUE;
3585
0
  first_cu = first_cu2 = (PCRE2_UCHAR)(re->first_codeunit);
3586
0
  if ((re->flags & PCRE2_FIRSTCASELESS) != 0)
3587
0
    {
3588
0
    first_cu2 = TABLE_GET(first_cu, mb->tables + fcc_offset, first_cu);
3589
0
#ifdef SUPPORT_UNICODE
3590
0
#if PCRE2_CODE_UNIT_WIDTH == 8
3591
0
    if (first_cu > 127 && !utf && (re->overall_options & PCRE2_UCP) != 0)
3592
0
      first_cu2 = (PCRE2_UCHAR)UCD_OTHERCASE(first_cu);
3593
#else
3594
    if (first_cu > 127 && (utf || (re->overall_options & PCRE2_UCP) != 0))
3595
      first_cu2 = (PCRE2_UCHAR)UCD_OTHERCASE(first_cu);
3596
#endif
3597
0
#endif  /* SUPPORT_UNICODE */
3598
0
    }
3599
0
  }
3600
0
else
3601
0
  if (!startline && (re->flags & PCRE2_FIRSTMAPSET) != 0)
3602
0
    start_bits = re->start_bitmap;
3603
3604
/* There may be a "last known required code unit" set. */
3605
3606
0
if ((re->flags & PCRE2_LASTSET) != 0)
3607
0
  {
3608
0
  has_req_cu = TRUE;
3609
0
  req_cu = req_cu2 = (PCRE2_UCHAR)(re->last_codeunit);
3610
0
  if ((re->flags & PCRE2_LASTCASELESS) != 0)
3611
0
    {
3612
0
    req_cu2 = TABLE_GET(req_cu, mb->tables + fcc_offset, req_cu);
3613
0
#ifdef SUPPORT_UNICODE
3614
0
#if PCRE2_CODE_UNIT_WIDTH == 8
3615
0
    if (req_cu > 127 && !utf && (re->overall_options & PCRE2_UCP) != 0)
3616
0
      req_cu2 = (PCRE2_UCHAR)UCD_OTHERCASE(req_cu);
3617
#else
3618
    if (req_cu > 127 && (utf || (re->overall_options & PCRE2_UCP) != 0))
3619
      req_cu2 = (PCRE2_UCHAR)UCD_OTHERCASE(req_cu);
3620
#endif
3621
0
#endif  /* SUPPORT_UNICODE */
3622
0
    }
3623
0
  }
3624
3625
/* If the match data block was previously used with PCRE2_COPY_MATCHED_SUBJECT,
3626
free the memory that was obtained. */
3627
3628
0
if ((match_data->flags & PCRE2_MD_COPIED_SUBJECT) != 0)
3629
0
  {
3630
0
  match_data->memctl.free((void *)match_data->subject,
3631
0
    match_data->memctl.memory_data);
3632
0
  match_data->flags &= ~PCRE2_MD_COPIED_SUBJECT;
3633
0
  }
3634
3635
/* Fill in fields that are always returned in the match data. */
3636
3637
0
match_data->code = re;
3638
0
match_data->subject = NULL;  /* Default for no match */
3639
0
match_data->mark = NULL;
3640
0
match_data->matchedby = PCRE2_MATCHEDBY_DFA_INTERPRETER;
3641
3642
/* Call the main matching function, looping for a non-anchored regex after a
3643
failed match. If not restarting, perform certain optimizations at the start of
3644
a match. */
3645
3646
0
for (;;)
3647
0
  {
3648
  /* ----------------- Start of match optimizations ---------------- */
3649
3650
  /* There are some optimizations that avoid running the match if a known
3651
  starting point is not found, or if a known later code unit is not present.
3652
  However, there is an option (settable at compile time) that disables
3653
  these, for testing and for ensuring that all callouts do actually occur.
3654
  The optimizations must also be avoided when restarting a DFA match. */
3655
3656
0
  if ((re->overall_options & PCRE2_NO_START_OPTIMIZE) == 0 &&
3657
0
      (options & PCRE2_DFA_RESTART) == 0)
3658
0
    {
3659
    /* If firstline is TRUE, the start of the match is constrained to the first
3660
    line of a multiline string. That is, the match must be before or at the
3661
    first newline following the start of matching. Temporarily adjust
3662
    end_subject so that we stop the optimization scans for a first code unit
3663
    immediately after the first character of a newline (the first code unit can
3664
    legitimately be a newline). If the match fails at the newline, later code
3665
    breaks this loop. */
3666
3667
0
    if (firstline)
3668
0
      {
3669
0
      PCRE2_SPTR t = start_match;
3670
0
#ifdef SUPPORT_UNICODE
3671
0
      if (utf)
3672
0
        {
3673
0
        while (t < end_subject && !IS_NEWLINE(t))
3674
0
          {
3675
0
          t++;
3676
0
          ACROSSCHAR(t < end_subject, t, t++);
3677
0
          }
3678
0
        }
3679
0
      else
3680
0
#endif
3681
0
      while (t < end_subject && !IS_NEWLINE(t)) t++;
3682
0
      end_subject = t;
3683
0
      }
3684
3685
    /* Anchored: check the first code unit if one is recorded. This may seem
3686
    pointless but it can help in detecting a no match case without scanning for
3687
    the required code unit. */
3688
3689
0
    if (anchored)
3690
0
      {
3691
0
      if (has_first_cu || start_bits != NULL)
3692
0
        {
3693
0
        BOOL ok = start_match < end_subject;
3694
0
        if (ok)
3695
0
          {
3696
0
          PCRE2_UCHAR c = UCHAR21TEST(start_match);
3697
0
          ok = has_first_cu && (c == first_cu || c == first_cu2);
3698
0
          if (!ok && start_bits != NULL)
3699
0
            {
3700
#if PCRE2_CODE_UNIT_WIDTH != 8
3701
            if (c > 255) c = 255;
3702
#endif
3703
0
            ok = (start_bits[c/8] & (1u << (c&7))) != 0;
3704
0
            }
3705
0
          }
3706
0
        if (!ok) break;
3707
0
        }
3708
0
      }
3709
3710
    /* Not anchored. Advance to a unique first code unit if there is one. */
3711
3712
0
    else
3713
0
      {
3714
0
      if (has_first_cu)
3715
0
        {
3716
0
        if (first_cu != first_cu2)  /* Caseless */
3717
0
          {
3718
          /* In 16-bit and 32_bit modes we have to do our own search, so can
3719
          look for both cases at once. */
3720
3721
#if PCRE2_CODE_UNIT_WIDTH != 8
3722
          PCRE2_UCHAR smc;
3723
          while (start_match < end_subject &&
3724
                (smc = UCHAR21TEST(start_match)) != first_cu &&
3725
                 smc != first_cu2)
3726
            start_match++;
3727
#else
3728
          /* In 8-bit mode, the use of memchr() gives a big speed up, even
3729
          though we have to call it twice in order to find the earliest
3730
          occurrence of the code unit in either of its cases. Caching is used
3731
          to remember the positions of previously found code units. This can
3732
          make a huge difference when the strings are very long and only one
3733
          case is actually present. */
3734
3735
0
          PCRE2_SPTR pp1 = NULL;
3736
0
          PCRE2_SPTR pp2 = NULL;
3737
0
          PCRE2_SIZE searchlength = end_subject - start_match;
3738
3739
          /* If we haven't got a previously found position for first_cu, or if
3740
          the current starting position is later, we need to do a search. If
3741
          the code unit is not found, set it to the end. */
3742
3743
0
          if (memchr_found_first_cu == NULL ||
3744
0
              start_match > memchr_found_first_cu)
3745
0
            {
3746
0
            pp1 = memchr(start_match, first_cu, searchlength);
3747
0
            memchr_found_first_cu = (pp1 == NULL)? end_subject : pp1;
3748
0
            }
3749
3750
          /* If the start is before a previously found position, use the
3751
          previous position, or NULL if a previous search failed. */
3752
3753
0
          else pp1 = (memchr_found_first_cu == end_subject)? NULL :
3754
0
            memchr_found_first_cu;
3755
3756
          /* Do the same thing for the other case. */
3757
3758
0
          if (memchr_found_first_cu2 == NULL ||
3759
0
              start_match > memchr_found_first_cu2)
3760
0
            {
3761
0
            pp2 = memchr(start_match, first_cu2, searchlength);
3762
0
            memchr_found_first_cu2 = (pp2 == NULL)? end_subject : pp2;
3763
0
            }
3764
3765
0
          else pp2 = (memchr_found_first_cu2 == end_subject)? NULL :
3766
0
            memchr_found_first_cu2;
3767
3768
          /* Set the start to the end of the subject if neither case was found.
3769
          Otherwise, use the earlier found point. */
3770
3771
0
          if (pp1 == NULL)
3772
0
            start_match = (pp2 == NULL)? end_subject : pp2;
3773
0
          else
3774
0
            start_match = (pp2 == NULL || pp1 < pp2)? pp1 : pp2;
3775
3776
0
#endif  /* 8-bit handling */
3777
0
          }
3778
3779
        /* The caseful case is much simpler. */
3780
3781
0
        else
3782
0
          {
3783
#if PCRE2_CODE_UNIT_WIDTH != 8
3784
          while (start_match < end_subject && UCHAR21TEST(start_match) !=
3785
                 first_cu)
3786
            start_match++;
3787
#else  /* 8-bit code units */
3788
0
          start_match = memchr(start_match, first_cu, end_subject - start_match);
3789
0
          if (start_match == NULL) start_match = end_subject;
3790
0
#endif
3791
0
          }
3792
3793
        /* If we can't find the required code unit, having reached the true end
3794
        of the subject, break the bumpalong loop, to force a match failure,
3795
        except when doing partial matching, when we let the next cycle run at
3796
        the end of the subject. To see why, consider the pattern /(?<=abc)def/,
3797
        which partially matches "abc", even though the string does not contain
3798
        the starting character "d". If we have not reached the true end of the
3799
        subject (PCRE2_FIRSTLINE caused end_subject to be temporarily modified)
3800
        we also let the cycle run, because the matching string is legitimately
3801
        allowed to start with the first code unit of a newline. */
3802
3803
0
        if ((mb->moptions & (PCRE2_PARTIAL_HARD|PCRE2_PARTIAL_SOFT)) == 0 &&
3804
0
            start_match >= mb->end_subject)
3805
0
          break;
3806
0
        }
3807
3808
      /* If there's no first code unit, advance to just after a linebreak for a
3809
      multiline match if required. */
3810
3811
0
      else if (startline)
3812
0
        {
3813
0
        if (start_match > mb->start_subject + start_offset)
3814
0
          {
3815
0
#ifdef SUPPORT_UNICODE
3816
0
          if (utf)
3817
0
            {
3818
0
            while (start_match < end_subject && !WAS_NEWLINE(start_match))
3819
0
              {
3820
0
              start_match++;
3821
0
              ACROSSCHAR(start_match < end_subject, start_match, start_match++);
3822
0
              }
3823
0
            }
3824
0
          else
3825
0
#endif
3826
0
          while (start_match < end_subject && !WAS_NEWLINE(start_match))
3827
0
            start_match++;
3828
3829
          /* If we have just passed a CR and the newline option is ANY or
3830
          ANYCRLF, and we are now at a LF, advance the match position by one
3831
          more code unit. */
3832
3833
0
          if (start_match[-1] == CHAR_CR &&
3834
0
               (mb->nltype == NLTYPE_ANY || mb->nltype == NLTYPE_ANYCRLF) &&
3835
0
               start_match < end_subject &&
3836
0
               UCHAR21TEST(start_match) == CHAR_NL)
3837
0
            start_match++;
3838
0
          }
3839
0
        }
3840
3841
      /* If there's no first code unit or a requirement for a multiline line
3842
      start, advance to a non-unique first code unit if any have been
3843
      identified. The bitmap contains only 256 bits. When code units are 16 or
3844
      32 bits wide, all code units greater than 254 set the 255 bit. */
3845
3846
0
      else if (start_bits != NULL)
3847
0
        {
3848
0
        while (start_match < end_subject)
3849
0
          {
3850
0
          uint32_t c = UCHAR21TEST(start_match);
3851
#if PCRE2_CODE_UNIT_WIDTH != 8
3852
          if (c > 255) c = 255;
3853
#endif
3854
0
          if ((start_bits[c/8] & (1u << (c&7))) != 0) break;
3855
0
          start_match++;
3856
0
          }
3857
3858
        /* See comment above in first_cu checking about the next line. */
3859
3860
0
        if ((mb->moptions & (PCRE2_PARTIAL_HARD|PCRE2_PARTIAL_SOFT)) == 0 &&
3861
0
            start_match >= mb->end_subject)
3862
0
          break;
3863
0
        }
3864
0
      }  /* End of first code unit handling */
3865
3866
    /* Restore fudged end_subject */
3867
3868
0
    end_subject = mb->end_subject;
3869
3870
    /* The following two optimizations are disabled for partial matching. */
3871
3872
0
    if ((mb->moptions & (PCRE2_PARTIAL_HARD|PCRE2_PARTIAL_SOFT)) == 0)
3873
0
      {
3874
0
      PCRE2_SPTR p;
3875
3876
      /* The minimum matching length is a lower bound; no actual string of that
3877
      length may actually match the pattern. Although the value is, strictly,
3878
      in characters, we treat it as code units to avoid spending too much time
3879
      in this optimization. */
3880
3881
0
      if (end_subject - start_match < re->minlength) goto NOMATCH_EXIT;
3882
3883
      /* If req_cu is set, we know that that code unit must appear in the
3884
      subject for the match to succeed. If the first code unit is set, req_cu
3885
      must be later in the subject; otherwise the test starts at the match
3886
      point. This optimization can save a huge amount of backtracking in
3887
      patterns with nested unlimited repeats that aren't going to match.
3888
      Writing separate code for cased/caseless versions makes it go faster, as
3889
      does using an autoincrement and backing off on a match. As in the case of
3890
      the first code unit, using memchr() in the 8-bit library gives a big
3891
      speed up. Unlike the first_cu check above, we do not need to call
3892
      memchr() twice in the caseless case because we only need to check for the
3893
      presence of the character in either case, not find the first occurrence.
3894
3895
      The search can be skipped if the code unit was found later than the
3896
      current starting point in a previous iteration of the bumpalong loop.
3897
3898
      HOWEVER: when the subject string is very, very long, searching to its end
3899
      can take a long time, and give bad performance on quite ordinary
3900
      patterns. This showed up when somebody was matching something like
3901
      /^\d+C/ on a 32-megabyte string... so we don't do this when the string is
3902
      sufficiently long, but it's worth searching a lot more for unanchored
3903
      patterns. */
3904
3905
0
      p = start_match + (has_first_cu? 1:0);
3906
0
      if (has_req_cu && p > req_cu_ptr)
3907
0
        {
3908
0
        PCRE2_SIZE check_length = end_subject - start_match;
3909
3910
0
        if (check_length < REQ_CU_MAX ||
3911
0
              (!anchored && check_length < REQ_CU_MAX * 1000))
3912
0
          {
3913
0
          if (req_cu != req_cu2)  /* Caseless */
3914
0
            {
3915
#if PCRE2_CODE_UNIT_WIDTH != 8
3916
            while (p < end_subject)
3917
              {
3918
              uint32_t pp = UCHAR21INCTEST(p);
3919
              if (pp == req_cu || pp == req_cu2) { p--; break; }
3920
              }
3921
#else  /* 8-bit code units */
3922
0
            PCRE2_SPTR pp = p;
3923
0
            p = memchr(pp, req_cu, end_subject - pp);
3924
0
            if (p == NULL)
3925
0
              {
3926
0
              p = memchr(pp, req_cu2, end_subject - pp);
3927
0
              if (p == NULL) p = end_subject;
3928
0
              }
3929
0
#endif /* PCRE2_CODE_UNIT_WIDTH != 8 */
3930
0
            }
3931
3932
          /* The caseful case */
3933
3934
0
          else
3935
0
            {
3936
#if PCRE2_CODE_UNIT_WIDTH != 8
3937
            while (p < end_subject)
3938
              {
3939
              if (UCHAR21INCTEST(p) == req_cu) { p--; break; }
3940
              }
3941
3942
#else  /* 8-bit code units */
3943
0
            p = memchr(p, req_cu, end_subject - p);
3944
0
            if (p == NULL) p = end_subject;
3945
0
#endif
3946
0
            }
3947
3948
          /* If we can't find the required code unit, break the matching loop,
3949
          forcing a match failure. */
3950
3951
0
          if (p >= end_subject) break;
3952
3953
          /* If we have found the required code unit, save the point where we
3954
          found it, so that we don't search again next time round the loop if
3955
          the start hasn't passed this code unit yet. */
3956
3957
0
          req_cu_ptr = p;
3958
0
          }
3959
0
        }
3960
0
      }
3961
0
    }
3962
3963
  /* ------------ End of start of match optimizations ------------ */
3964
3965
  /* Give no match if we have passed the bumpalong limit. */
3966
3967
0
  if (start_match > bumpalong_limit) break;
3968
3969
  /* OK, now we can do the business */
3970
3971
0
  mb->start_used_ptr = start_match;
3972
0
  mb->last_used_ptr = start_match;
3973
0
  mb->recursive = NULL;
3974
3975
0
  rc = internal_dfa_match(
3976
0
    mb,                           /* fixed match data */
3977
0
    mb->start_code,               /* this subexpression's code */
3978
0
    start_match,                  /* where we currently are */
3979
0
    start_offset,                 /* start offset in subject */
3980
0
    match_data->ovector,          /* offset vector */
3981
0
    (uint32_t)match_data->oveccount * 2,  /* actual size of same */
3982
0
    workspace,                    /* workspace vector */
3983
0
    (int)wscount,                 /* size of same */
3984
0
    0,                            /* function recurse level */
3985
0
    base_recursion_workspace);    /* initial workspace for recursion */
3986
3987
  /* Anything other than "no match" means we are done, always; otherwise, carry
3988
  on only if not anchored. */
3989
3990
0
  if (rc != PCRE2_ERROR_NOMATCH || anchored)
3991
0
    {
3992
0
    if (rc == PCRE2_ERROR_PARTIAL && match_data->oveccount > 0)
3993
0
      {
3994
0
      match_data->ovector[0] = (PCRE2_SIZE)(start_match - subject);
3995
0
      match_data->ovector[1] = (PCRE2_SIZE)(end_subject - subject);
3996
0
      }
3997
0
    match_data->leftchar = (PCRE2_SIZE)(mb->start_used_ptr - subject);
3998
0
    match_data->rightchar = (PCRE2_SIZE)( mb->last_used_ptr - subject);
3999
0
    match_data->startchar = (PCRE2_SIZE)(start_match - subject);
4000
0
    match_data->rc = rc;
4001
4002
0
    if (rc >= 0 &&(options & PCRE2_COPY_MATCHED_SUBJECT) != 0)
4003
0
      {
4004
0
      length = CU2BYTES(length + was_zero_terminated);
4005
0
      match_data->subject = match_data->memctl.malloc(length,
4006
0
        match_data->memctl.memory_data);
4007
0
      if (match_data->subject == NULL) return PCRE2_ERROR_NOMEMORY;
4008
0
      memcpy((void *)match_data->subject, subject, length);
4009
0
      match_data->flags |= PCRE2_MD_COPIED_SUBJECT;
4010
0
      }
4011
0
    else
4012
0
      {
4013
0
      if (rc >= 0 || rc == PCRE2_ERROR_PARTIAL) match_data->subject = subject;
4014
0
      }
4015
0
    goto EXIT;
4016
0
    }
4017
4018
  /* Advance to the next subject character unless we are at the end of a line
4019
  and firstline is set. */
4020
4021
0
  if (firstline && IS_NEWLINE(start_match)) break;
4022
0
  start_match++;
4023
0
#ifdef SUPPORT_UNICODE
4024
0
  if (utf)
4025
0
    {
4026
0
    ACROSSCHAR(start_match < end_subject, start_match, start_match++);
4027
0
    }
4028
0
#endif
4029
0
  if (start_match > end_subject) break;
4030
4031
  /* If we have just passed a CR and we are now at a LF, and the pattern does
4032
  not contain any explicit matches for \r or \n, and the newline option is CRLF
4033
  or ANY or ANYCRLF, advance the match position by one more character. */
4034
4035
0
  if (UCHAR21TEST(start_match - 1) == CHAR_CR &&
4036
0
      start_match < end_subject &&
4037
0
      UCHAR21TEST(start_match) == CHAR_NL &&
4038
0
      (re->flags & PCRE2_HASCRORLF) == 0 &&
4039
0
        (mb->nltype == NLTYPE_ANY ||
4040
0
         mb->nltype == NLTYPE_ANYCRLF ||
4041
0
         mb->nllen == 2))
4042
0
    start_match++;
4043
4044
0
  }   /* "Bumpalong" loop */
4045
4046
0
NOMATCH_EXIT:
4047
0
rc = PCRE2_ERROR_NOMATCH;
4048
4049
0
EXIT:
4050
0
while (rws->next != NULL)
4051
0
  {
4052
0
  RWS_anchor *next = rws->next;
4053
0
  rws->next = next->next;
4054
0
  mb->memctl.free(next, mb->memctl.memory_data);
4055
0
  }
4056
4057
0
return rc;
4058
0
}
4059
4060
/* These #undefs are here to enable unity builds with CMake. */
4061
4062
#undef NLBLOCK /* Block containing newline information */
4063
#undef PSSTART /* Field containing processed string start */
4064
#undef PSEND   /* Field containing processed string end */
4065
4066
/* End of pcre2_dfa_match.c */