/[pcre]/code/trunk/pcre_dfa_exec.c
ViewVC logotype

Diff of /code/trunk/pcre_dfa_exec.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 226 by ph10, Tue Aug 21 11:46:08 2007 UTC revision 606 by ph10, Mon Jun 6 17:46:22 2011 UTC
# Line 3  Line 3 
3  *************************************************/  *************************************************/
4    
5  /* PCRE is a library of functions to support regular expressions whose syntax  /* 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.  and semantics are as close as possible to those of the Perl 5 language (but see
7    below for why this module is different).
8    
9                         Written by Philip Hazel                         Written by Philip Hazel
10             Copyright (c) 1997-2007 University of Cambridge             Copyright (c) 1997-2011 University of Cambridge
11    
12  -----------------------------------------------------------------------------  -----------------------------------------------------------------------------
13  Redistribution and use in source and binary forms, with or without  Redistribution and use in source and binary forms, with or without
# Line 44  FSM). This is NOT Perl- compatible, but Line 45  FSM). This is NOT Perl- compatible, but
45  applications. */  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_exec(). (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    
76  #ifdef HAVE_CONFIG_H  #ifdef HAVE_CONFIG_H
77  #include <config.h>  #include "config.h"
78  #endif  #endif
79    
80  #define NLBLOCK md             /* Block containing newline information */  #define NLBLOCK md             /* Block containing newline information */
# Line 60  applications. */ Line 89  applications. */
89  #define SP "                   "  #define SP "                   "
90    
91    
   
92  /*************************************************  /*************************************************
93  *      Code parameters and static tables         *  *      Code parameters and static tables         *
94  *************************************************/  *************************************************/
# Line 78  never stored, so we push them well clear Line 106  never stored, so we push them well clear
106    
107    
108  /* This table identifies those opcodes that are followed immediately by a  /* This table identifies those opcodes that are followed immediately by a
109  character that is to be tested in some way. This makes is possible to  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  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  "character" is the opcode for \D, \d, \S, \s, \W, or \w, which will always be a
112  small value. ***NOTE*** If the start of this table is modified, the two tables  small value. Non-zero values in the table are the offsets from the opcode where
113  that follow must also be modified. */  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 uschar coptable[] = {  static const uschar coptable[] = {
117    0,                             /* End                                    */    0,                             /* End                                    */
118    0, 0, 0, 0, 0,                 /* \A, \G, \K, \B, \b                     */    0, 0, 0, 0, 0,                 /* \A, \G, \K, \B, \b                     */
119    0, 0, 0, 0, 0, 0,              /* \D, \d, \S, \s, \W, \w                 */    0, 0, 0, 0, 0, 0,              /* \D, \d, \S, \s, \W, \w                 */
120    0, 0,                          /* Any, Anybyte                           */    0, 0, 0,                       /* Any, AllAny, Anybyte                   */
121    0, 0, 0,                       /* NOTPROP, PROP, EXTUNI                  */    0, 0,                          /* \P, \p                                 */
122    0, 0, 0, 0, 0,                 /* \R, \H, \h, \V, \v                     */    0, 0, 0, 0, 0,                 /* \R, \H, \h, \V, \v                     */
123    0, 0, 0, 0, 0,                 /* \Z, \z, Opt, ^, $                      */    0,                             /* \X                                     */
124      0, 0, 0, 0, 0, 0,              /* \Z, \z, ^, ^M, $, $M                   */
125    1,                             /* Char                                   */    1,                             /* Char                                   */
126    1,                             /* Charnc                                 */    1,                             /* Chari                                  */
127    1,                             /* not                                    */    1,                             /* not                                    */
128      1,                             /* noti                                   */
129    /* Positive single-char repeats                                          */    /* Positive single-char repeats                                          */
130    1, 1, 1, 1, 1, 1,              /* *, *?, +, +?, ?, ??                    */    1, 1, 1, 1, 1, 1,              /* *, *?, +, +?, ?, ??                    */
131    3, 3, 3,                       /* upto, minupto, exact                   */    3, 3, 3,                       /* upto, minupto, exact                   */
132    1, 1, 1, 3,                    /* *+, ++, ?+, upto+                      */    1, 1, 1, 3,                    /* *+, ++, ?+, upto+                      */
133      1, 1, 1, 1, 1, 1,              /* *I, *?I, +I, +?I, ?I, ??I              */
134      3, 3, 3,                       /* upto I, minupto I, exact I             */
135      1, 1, 1, 3,                    /* *+I, ++I, ?+I, upto+I                  */
136    /* Negative single-char repeats - only for chars < 256                   */    /* Negative single-char repeats - only for chars < 256                   */
137    1, 1, 1, 1, 1, 1,              /* NOT *, *?, +, +?, ?, ??                */    1, 1, 1, 1, 1, 1,              /* NOT *, *?, +, +?, ?, ??                */
138    3, 3, 3,                       /* NOT upto, minupto, exact               */    3, 3, 3,                       /* NOT upto, minupto, exact               */
139    1, 1, 1, 3,                    /* NOT *+, ++, ?+, updo+                  */    1, 1, 1, 3,                    /* NOT *+, ++, ?+, upto+                  */
140      1, 1, 1, 1, 1, 1,              /* NOT *I, *?I, +I, +?I, ?I, ??I          */
141      3, 3, 3,                       /* NOT upto I, minupto I, exact I         */
142      1, 1, 1, 3,                    /* NOT *+I, ++I, ?+I, upto+I              */
143    /* Positive type repeats                                                 */    /* Positive type repeats                                                 */
144    1, 1, 1, 1, 1, 1,              /* Type *, *?, +, +?, ?, ??               */    1, 1, 1, 1, 1, 1,              /* Type *, *?, +, +?, ?, ??               */
145    3, 3, 3,                       /* Type upto, minupto, exact              */    3, 3, 3,                       /* Type upto, minupto, exact              */
# Line 114  static uschar coptable[] = { Line 151  static uschar coptable[] = {
151    0,                             /* NCLASS                                 */    0,                             /* NCLASS                                 */
152    0,                             /* XCLASS - variable length               */    0,                             /* XCLASS - variable length               */
153    0,                             /* REF                                    */    0,                             /* REF                                    */
154      0,                             /* REFI                                   */
155    0,                             /* RECURSE                                */    0,                             /* RECURSE                                */
156    0,                             /* CALLOUT                                */    0,                             /* CALLOUT                                */
157    0,                             /* Alt                                    */    0,                             /* Alt                                    */
158    0,                             /* Ket                                    */    0,                             /* Ket                                    */
159    0,                             /* KetRmax                                */    0,                             /* KetRmax                                */
160    0,                             /* KetRmin                                */    0,                             /* KetRmin                                */
161      0,                             /* KetRpos                                */
162    0,                             /* Assert                                 */    0,                             /* Assert                                 */
163    0,                             /* Assert not                             */    0,                             /* Assert not                             */
164    0,                             /* Assert behind                          */    0,                             /* Assert behind                          */
165    0,                             /* Assert behind not                      */    0,                             /* Assert behind not                      */
166    0,                             /* Reverse                                */    0,                             /* Reverse                                */
167    0, 0, 0, 0,                    /* ONCE, BRA, CBRA, COND                  */    0, 0, 0, 0, 0, 0,              /* ONCE, BRA, BRAPOS, CBRA, CBRAPOS, COND */
168    0, 0, 0,                       /* SBRA, SCBRA, SCOND                     */    0, 0, 0, 0, 0,                 /* SBRA, SBRAPOS, SCBRA, SCBRAPOS, SCOND  */
169    0,                             /* CREF                                   */    0, 0,                          /* CREF, NCREF                            */
170    0,                             /* RREF                                   */    0, 0,                          /* RREF, NRREF                            */
171    0,                             /* DEF                                    */    0,                             /* DEF                                    */
172    0, 0,                          /* BRAZERO, BRAMINZERO                    */    0, 0, 0,                       /* BRAZERO, BRAMINZERO, BRAPOSZERO        */
173    0, 0, 0, 0,                    /* PRUNE, SKIP, THEN, COMMIT              */    0, 0, 0,                       /* MARK, PRUNE, PRUNE_ARG,                */
174    0, 0                           /* FAIL, ACCEPT                           */    0, 0, 0, 0,                    /* SKIP, SKIP_ARG, THEN, THEN_ARG,        */
175      0, 0, 0, 0, 0                  /* COMMIT, FAIL, ACCEPT, CLOSE, SKIPZERO  */
176    };
177    
178    /* This table identifies those opcodes that inspect a character. It is used to
179    remember the fact that a character could have been inspected when the end of
180    the subject is reached. ***NOTE*** If the start of this table is modified, the
181    two tables that follow must also be modified. */
182    
183    static const uschar poptable[] = {
184      0,                             /* End                                    */
185      0, 0, 0, 1, 1,                 /* \A, \G, \K, \B, \b                     */
186      1, 1, 1, 1, 1, 1,              /* \D, \d, \S, \s, \W, \w                 */
187      1, 1, 1,                       /* Any, AllAny, Anybyte                   */
188      1, 1,                          /* \P, \p                                 */
189      1, 1, 1, 1, 1,                 /* \R, \H, \h, \V, \v                     */
190      1,                             /* \X                                     */
191      0, 0, 0, 0, 0, 0,              /* \Z, \z, ^, ^M, $, $M                   */
192      1,                             /* Char                                   */
193      1,                             /* Chari                                  */
194      1,                             /* not                                    */
195      1,                             /* noti                                   */
196      /* Positive single-char repeats                                          */
197      1, 1, 1, 1, 1, 1,              /* *, *?, +, +?, ?, ??                    */
198      1, 1, 1,                       /* upto, minupto, exact                   */
199      1, 1, 1, 1,                    /* *+, ++, ?+, upto+                      */
200      1, 1, 1, 1, 1, 1,              /* *I, *?I, +I, +?I, ?I, ??I              */
201      1, 1, 1,                       /* upto I, minupto I, exact I             */
202      1, 1, 1, 1,                    /* *+I, ++I, ?+I, upto+I                  */
203      /* Negative single-char repeats - only for chars < 256                   */
204      1, 1, 1, 1, 1, 1,              /* NOT *, *?, +, +?, ?, ??                */
205      1, 1, 1,                       /* NOT upto, minupto, exact               */
206      1, 1, 1, 1,                    /* NOT *+, ++, ?+, upto+                  */
207      1, 1, 1, 1, 1, 1,              /* NOT *I, *?I, +I, +?I, ?I, ??I          */
208      1, 1, 1,                       /* NOT upto I, minupto I, exact I         */
209      1, 1, 1, 1,                    /* NOT *+I, ++I, ?+I, upto+I              */
210      /* Positive type repeats                                                 */
211      1, 1, 1, 1, 1, 1,              /* Type *, *?, +, +?, ?, ??               */
212      1, 1, 1,                       /* Type upto, minupto, exact              */
213      1, 1, 1, 1,                    /* Type *+, ++, ?+, upto+                 */
214      /* Character class & ref repeats                                         */
215      1, 1, 1, 1, 1, 1,              /* *, *?, +, +?, ?, ??                    */
216      1, 1,                          /* CRRANGE, CRMINRANGE                    */
217      1,                             /* CLASS                                  */
218      1,                             /* NCLASS                                 */
219      1,                             /* XCLASS - variable length               */
220      0,                             /* REF                                    */
221      0,                             /* REFI                                   */
222      0,                             /* RECURSE                                */
223      0,                             /* CALLOUT                                */
224      0,                             /* Alt                                    */
225      0,                             /* Ket                                    */
226      0,                             /* KetRmax                                */
227      0,                             /* KetRmin                                */
228      0,                             /* KetRpos                                */
229      0,                             /* Assert                                 */
230      0,                             /* Assert not                             */
231      0,                             /* Assert behind                          */
232      0,                             /* Assert behind not                      */
233      0,                             /* Reverse                                */
234      0, 0, 0, 0, 0, 0,              /* ONCE, BRA, BRAPOS, CBRA, CBRAPOS, COND */
235      0, 0, 0, 0, 0,                 /* SBRA, SBRAPOS, SCBRA, SCBRAPOS, SCOND  */
236      0, 0,                          /* CREF, NCREF                            */
237      0, 0,                          /* RREF, NRREF                            */
238      0,                             /* DEF                                    */
239      0, 0, 0,                       /* BRAZERO, BRAMINZERO, BRAPOSZERO        */
240      0, 0, 0,                       /* MARK, PRUNE, PRUNE_ARG,                */
241      0, 0, 0, 0,                    /* SKIP, SKIP_ARG, THEN, THEN_ARG,        */
242      0, 0, 0, 0, 0                  /* COMMIT, FAIL, ACCEPT, CLOSE, SKIPZERO  */
243  };  };
244    
245  /* These 2 tables allow for compact code for testing for \D, \d, \S, \s, \W,  /* These 2 tables allow for compact code for testing for \D, \d, \S, \s, \W,
246  and \w */  and \w */
247    
248  static uschar toptable1[] = {  static const uschar toptable1[] = {
249    0, 0, 0, 0, 0, 0,    0, 0, 0, 0, 0, 0,
250    ctype_digit, ctype_digit,    ctype_digit, ctype_digit,
251    ctype_space, ctype_space,    ctype_space, ctype_space,
252    ctype_word,  ctype_word,    ctype_word,  ctype_word,
253    0                               /* OP_ANY */    0, 0                            /* OP_ANY, OP_ALLANY */
254  };  };
255    
256  static uschar toptable2[] = {  static const uschar toptable2[] = {
257    0, 0, 0, 0, 0, 0,    0, 0, 0, 0, 0, 0,
258    ctype_digit, 0,    ctype_digit, 0,
259    ctype_space, 0,    ctype_space, 0,
260    ctype_word,  0,    ctype_word,  0,
261    1                               /* OP_ANY */    1, 1                            /* OP_ANY, OP_ALLANY */
262  };  };
263    
264    
# Line 163  these structures in, is a vector of ints Line 270  these structures in, is a vector of ints
270  typedef struct stateblock {  typedef struct stateblock {
271    int offset;                     /* Offset to opcode */    int offset;                     /* Offset to opcode */
272    int count;                      /* Count for repeats */    int count;                      /* Count for repeats */
   int ims;                        /* ims flag bits */  
273    int data;                       /* Some use extra data */    int data;                       /* Some use extra data */
274  } stateblock;  } stateblock;
275    
276  #define INTS_PER_STATEBLOCK  (sizeof(stateblock)/sizeof(int))  #define INTS_PER_STATEBLOCK  (sizeof(stateblock)/sizeof(int))
277    
278    
279  #ifdef DEBUG  #ifdef PCRE_DEBUG
280  /*************************************************  /*************************************************
281  *             Print character string             *  *             Print character string             *
282  *************************************************/  *************************************************/
# Line 219  Arguments: Line 325  Arguments:
325    offsetcount       size of same    offsetcount       size of same
326    workspace         vector of workspace    workspace         vector of workspace
327    wscount           size of same    wscount           size of same
   ims               the current ims flags  
328    rlevel            function call recursion level    rlevel            function call recursion level
329    recursing         regex recursive call level    recursing         regex recursive call level
330    
331  Returns:            > 0 =>  Returns:            > 0 => number of match offset pairs placed in offsets
332                      = 0 =>                      = 0 => offsets overflowed; longest matches are present
333                       -1 => failed to match                       -1 => failed to match
334                     < -1 => some kind of unexpected problem                     < -1 => some kind of unexpected problem
335    
# Line 236  for the current character, one for the f Line 341  for the current character, one for the f
341      { \      { \
342      next_active_state->offset = (x); \      next_active_state->offset = (x); \
343      next_active_state->count  = (y); \      next_active_state->count  = (y); \
     next_active_state->ims    = ims; \  
344      next_active_state++; \      next_active_state++; \
345      DPRINTF(("%.*sADD_ACTIVE(%d,%d)\n", rlevel*2-2, SP, (x), (y))); \      DPRINTF(("%.*sADD_ACTIVE(%d,%d)\n", rlevel*2-2, SP, (x), (y))); \
346      } \      } \
# Line 247  for the current character, one for the f Line 351  for the current character, one for the f
351      { \      { \
352      next_active_state->offset = (x); \      next_active_state->offset = (x); \
353      next_active_state->count  = (y); \      next_active_state->count  = (y); \
     next_active_state->ims    = ims; \  
354      next_active_state->data   = (z); \      next_active_state->data   = (z); \
355      next_active_state++; \      next_active_state++; \
356      DPRINTF(("%.*sADD_ACTIVE_DATA(%d,%d,%d)\n", rlevel*2-2, SP, (x), (y), (z))); \      DPRINTF(("%.*sADD_ACTIVE_DATA(%d,%d,%d)\n", rlevel*2-2, SP, (x), (y), (z))); \
# Line 259  for the current character, one for the f Line 362  for the current character, one for the f
362      { \      { \
363      next_new_state->offset = (x); \      next_new_state->offset = (x); \
364      next_new_state->count  = (y); \      next_new_state->count  = (y); \
     next_new_state->ims    = ims; \  
365      next_new_state++; \      next_new_state++; \
366      DPRINTF(("%.*sADD_NEW(%d,%d)\n", rlevel*2-2, SP, (x), (y))); \      DPRINTF(("%.*sADD_NEW(%d,%d)\n", rlevel*2-2, SP, (x), (y))); \
367      } \      } \
# Line 270  for the current character, one for the f Line 372  for the current character, one for the f
372      { \      { \
373      next_new_state->offset = (x); \      next_new_state->offset = (x); \
374      next_new_state->count  = (y); \      next_new_state->count  = (y); \
     next_new_state->ims    = ims; \  
375      next_new_state->data   = (z); \      next_new_state->data   = (z); \
376      next_new_state++; \      next_new_state++; \
377      DPRINTF(("%.*sADD_NEW_DATA(%d,%d,%d)\n", rlevel*2-2, SP, (x), (y), (z))); \      DPRINTF(("%.*sADD_NEW_DATA(%d,%d,%d)\n", rlevel*2-2, SP, (x), (y), (z))); \
# Line 289  internal_dfa_exec( Line 390  internal_dfa_exec(
390    int offsetcount,    int offsetcount,
391    int *workspace,    int *workspace,
392    int wscount,    int wscount,
   int ims,  
393    int  rlevel,    int  rlevel,
394    int  recursing)    int  recursing)
395  {  {
# Line 337  next_new_state = new_states = active_sta Line 437  next_new_state = new_states = active_sta
437  new_count = 0;  new_count = 0;
438    
439  first_op = this_start_code + 1 + LINK_SIZE +  first_op = this_start_code + 1 + LINK_SIZE +
440    ((*this_start_code == OP_CBRA || *this_start_code == OP_SCBRA)? 2:0);    ((*this_start_code == OP_CBRA || *this_start_code == OP_SCBRA ||
441        *this_start_code == OP_CBRAPOS || *this_start_code == OP_SCBRAPOS)? 2:0);
442    
443  /* The first thing in any (sub) pattern is a bracket of some sort. Push all  /* The first thing in any (sub) pattern is a bracket of some sort. Push all
444  the alternative states onto the list, and find out where the end is. This  the alternative states onto the list, and find out where the end is. This
# Line 386  if (*first_op == OP_REVERSE) Line 487  if (*first_op == OP_REVERSE)
487    
488      {      {
489      gone_back = (current_subject - max_back < start_subject)?      gone_back = (current_subject - max_back < start_subject)?
490        current_subject - start_subject : max_back;        (int)(current_subject - start_subject) : max_back;
491      current_subject -= gone_back;      current_subject -= gone_back;
492      }      }
493    
494      /* Save the earliest consulted character */
495    
496      if (current_subject < md->start_used_ptr)
497        md->start_used_ptr = current_subject;
498    
499    /* Now we can process the individual branches. */    /* Now we can process the individual branches. */
500    
501    end_code = this_start_code;    end_code = this_start_code;
# Line 398  if (*first_op == OP_REVERSE) Line 504  if (*first_op == OP_REVERSE)
504      int back = GET(end_code, 2+LINK_SIZE);      int back = GET(end_code, 2+LINK_SIZE);
505      if (back <= gone_back)      if (back <= gone_back)
506        {        {
507        int bstate = end_code - start_code + 2 + 2*LINK_SIZE;        int bstate = (int)(end_code - start_code + 2 + 2*LINK_SIZE);
508        ADD_NEW_DATA(-bstate, 0, gone_back - back);        ADD_NEW_DATA(-bstate, 0, gone_back - back);
509        }        }
510      end_code += GET(end_code, 1);      end_code += GET(end_code, 1);
# Line 431  else Line 537  else
537    else    else
538      {      {
539      int length = 1 + LINK_SIZE +      int length = 1 + LINK_SIZE +
540        ((*this_start_code == OP_CBRA || *this_start_code == OP_SCBRA)? 2:0);        ((*this_start_code == OP_CBRA || *this_start_code == OP_SCBRA ||
541            *this_start_code == OP_CBRAPOS || *this_start_code == OP_SCBRAPOS)?
542            2:0);
543      do      do
544        {        {
545        ADD_NEW(end_code - start_code + length, 0);        ADD_NEW((int)(end_code - start_code + length), 0);
546        end_code += GET(end_code, 1);        end_code += GET(end_code, 1);
547        length = 1 + LINK_SIZE;        length = 1 + LINK_SIZE;
548        }        }
# Line 454  for (;;) Line 562  for (;;)
562    int i, j;    int i, j;
563    int clen, dlen;    int clen, dlen;
564    unsigned int c, d;    unsigned int c, d;
565      int forced_fail = 0;
566      BOOL could_continue = FALSE;
567    
568    /* Make the new state list into the active state list and empty the    /* Make the new state list into the active state list and empty the
569    new state list. */    new state list. */
# Line 467  for (;;) Line 577  for (;;)
577    workspace[0] ^= 1;              /* Remember for the restarting feature */    workspace[0] ^= 1;              /* Remember for the restarting feature */
578    workspace[1] = active_count;    workspace[1] = active_count;
579    
580  #ifdef DEBUG  #ifdef PCRE_DEBUG
581    printf("%.*sNext character: rest of subject = \"", rlevel*2-2, SP);    printf("%.*sNext character: rest of subject = \"", rlevel*2-2, SP);
582    pchars((uschar *)ptr, strlen((char *)ptr), stdout);    pchars((uschar *)ptr, strlen((char *)ptr), stdout);
583    printf("\"\n");    printf("\"\n");
# Line 509  for (;;) Line 619  for (;;)
619    for (i = 0; i < active_count; i++)    for (i = 0; i < active_count; i++)
620      {      {
621      stateblock *current_state = active_states + i;      stateblock *current_state = active_states + i;
622        BOOL caseless = FALSE;
623      const uschar *code;      const uschar *code;
624      int state_offset = current_state->offset;      int state_offset = current_state->offset;
625      int count, codevalue;      int count, codevalue, rrc;
 #ifdef SUPPORT_UCP  
     int chartype, script;  
 #endif  
626    
627  #ifdef DEBUG  #ifdef PCRE_DEBUG
628      printf ("%.*sProcessing state %d c=", rlevel*2-2, SP, state_offset);      printf ("%.*sProcessing state %d c=", rlevel*2-2, SP, state_offset);
629      if (clen == 0) printf("EOL\n");      if (clen == 0) printf("EOL\n");
630        else if (c > 32 && c < 127) printf("'%c'\n", c);        else if (c > 32 && c < 127) printf("'%c'\n", c);
631          else printf("0x%02x\n", c);          else printf("0x%02x\n", c);
632  #endif  #endif
633    
     /* This variable is referred to implicity in the ADD_xxx macros. */  
   
     ims = current_state->ims;  
   
634      /* A negative offset is a special case meaning "hold off going to this      /* A negative offset is a special case meaning "hold off going to this
635      (negated) state until the number of characters in the data field have      (negated) state until the number of characters in the data field have
636      been skipped". */      been skipped". */
# Line 546  for (;;) Line 650  for (;;)
650          }          }
651        }        }
652    
653      /* Check for a duplicate state with the same count, and skip if found. */      /* Check for a duplicate state with the same count, and skip if found.
654        See the note at the head of this module about the possibility of improving
655        performance here. */
656    
657      for (j = 0; j < i; j++)      for (j = 0; j < i; j++)
658        {        {
# Line 563  for (;;) Line 669  for (;;)
669      code = start_code + state_offset;      code = start_code + state_offset;
670      codevalue = *code;      codevalue = *code;
671    
672        /* If this opcode inspects a character, but we are at the end of the
673        subject, remember the fact for use when testing for a partial match. */
674    
675        if (clen == 0 && poptable[codevalue] != 0)
676          could_continue = TRUE;
677    
678      /* If this opcode is followed by an inline character, load it. It is      /* If this opcode is followed by an inline character, load it. It is
679      tempting to test for the presence of a subject character here, but that      tempting to test for the presence of a subject character here, but that
680      is wrong, because sometimes zero repetitions of the subject are      is wrong, because sometimes zero repetitions of the subject are
# Line 609  for (;;) Line 721  for (;;)
721    
722      switch (codevalue)      switch (codevalue)
723        {        {
724    /* ========================================================================== */
725          /* These cases are never obeyed. This is a fudge that causes a compile-
726          time error if the vectors coptable or poptable, which are indexed by
727          opcode, are not the correct length. It seems to be the only way to do
728          such a check at compile time, as the sizeof() operator does not work
729          in the C preprocessor. */
730    
731          case OP_TABLE_LENGTH:
732          case OP_TABLE_LENGTH +
733            ((sizeof(coptable) == OP_TABLE_LENGTH) &&
734             (sizeof(poptable) == OP_TABLE_LENGTH)):
735          break;
736    
737  /* ========================================================================== */  /* ========================================================================== */
738        /* Reached a closing bracket. If not at the end of the pattern, carry        /* Reached a closing bracket. If not at the end of the pattern, carry
739        on with the next opcode. Otherwise, unless we have an empty string and        on with the next opcode. For repeating opcodes, also add the repeat
740        PCRE_NOTEMPTY is set, save the match data, shifting up all previous        state. Note that KETRPOS will always be encountered at the end of the
741          subpattern, because the possessive subpattern repeats are always handled
742          using recursive calls. Thus, it never adds any new states.
743    
744          At the end of the (sub)pattern, unless we have an empty string and
745          PCRE_NOTEMPTY is set, or PCRE_NOTEMPTY_ATSTART is set and we are at the
746          start of the subject, save the match data, shifting up all previous
747        matches so we always have the longest first. */        matches so we always have the longest first. */
748    
749        case OP_KET:        case OP_KET:
750        case OP_KETRMIN:        case OP_KETRMIN:
751        case OP_KETRMAX:        case OP_KETRMAX:
752          case OP_KETRPOS:
753        if (code != end_code)        if (code != end_code)
754          {          {
755          ADD_ACTIVE(state_offset + 1 + LINK_SIZE, 0);          ADD_ACTIVE(state_offset + 1 + LINK_SIZE, 0);
# Line 627  for (;;) Line 758  for (;;)
758            ADD_ACTIVE(state_offset - GET(code, 1), 0);            ADD_ACTIVE(state_offset - GET(code, 1), 0);
759            }            }
760          }          }
761        else if (ptr > current_subject || (md->moptions & PCRE_NOTEMPTY) == 0)        else
762          {          {
763          if (match_count < 0) match_count = (offsetcount >= 2)? 1 : 0;          if (ptr > current_subject ||
764            else if (match_count > 0 && ++match_count * 2 >= offsetcount)              ((md->moptions & PCRE_NOTEMPTY) == 0 &&
765              match_count = 0;                ((md->moptions & PCRE_NOTEMPTY_ATSTART) == 0 ||
766          count = ((match_count == 0)? offsetcount : match_count * 2) - 2;                  current_subject > start_subject + md->start_offset)))
767          if (count > 0) memmove(offsets + 2, offsets, count * sizeof(int));            {
768          if (offsetcount >= 2)            if (match_count < 0) match_count = (offsetcount >= 2)? 1 : 0;
769            {              else if (match_count > 0 && ++match_count * 2 >= offsetcount)
770            offsets[0] = current_subject - start_subject;                match_count = 0;
771            offsets[1] = ptr - start_subject;            count = ((match_count == 0)? offsetcount : match_count * 2) - 2;
772            DPRINTF(("%.*sSet matched string = \"%.*s\"\n", rlevel*2-2, SP,            if (count > 0) memmove(offsets + 2, offsets, count * sizeof(int));
773              offsets[1] - offsets[0], current_subject));            if (offsetcount >= 2)
774            }              {
775          if ((md->moptions & PCRE_DFA_SHORTEST) != 0)              offsets[0] = (int)(current_subject - start_subject);
776            {              offsets[1] = (int)(ptr - start_subject);
777            DPRINTF(("%.*sEnd of internal_dfa_exec %d: returning %d\n"              DPRINTF(("%.*sSet matched string = \"%.*s\"\n", rlevel*2-2, SP,
778              "%.*s---------------------\n\n", rlevel*2-2, SP, rlevel,                offsets[1] - offsets[0], current_subject));
779              match_count, rlevel*2-2, SP));              }
780            return match_count;            if ((md->moptions & PCRE_DFA_SHORTEST) != 0)
781                {
782                DPRINTF(("%.*sEnd of internal_dfa_exec %d: returning %d\n"
783                  "%.*s---------------------\n\n", rlevel*2-2, SP, rlevel,
784                  match_count, rlevel*2-2, SP));
785                return match_count;
786                }
787            }            }
788          }          }
789        break;        break;
# Line 658  for (;;) Line 795  for (;;)
795        /*-----------------------------------------------------------------*/        /*-----------------------------------------------------------------*/
796        case OP_ALT:        case OP_ALT:
797        do { code += GET(code, 1); } while (*code == OP_ALT);        do { code += GET(code, 1); } while (*code == OP_ALT);
798        ADD_ACTIVE(code - start_code, 0);        ADD_ACTIVE((int)(code - start_code), 0);
799        break;        break;
800    
801        /*-----------------------------------------------------------------*/        /*-----------------------------------------------------------------*/
# Line 666  for (;;) Line 803  for (;;)
803        case OP_SBRA:        case OP_SBRA:
804        do        do
805          {          {
806          ADD_ACTIVE(code - start_code + 1 + LINK_SIZE, 0);          ADD_ACTIVE((int)(code - start_code + 1 + LINK_SIZE), 0);
807          code += GET(code, 1);          code += GET(code, 1);
808          }          }
809        while (*code == OP_ALT);        while (*code == OP_ALT);
# Line 675  for (;;) Line 812  for (;;)
812        /*-----------------------------------------------------------------*/        /*-----------------------------------------------------------------*/
813        case OP_CBRA:        case OP_CBRA:
814        case OP_SCBRA:        case OP_SCBRA:
815        ADD_ACTIVE(code - start_code + 3 + LINK_SIZE,  0);        ADD_ACTIVE((int)(code - start_code + 3 + LINK_SIZE),  0);
816        code += GET(code, 1);        code += GET(code, 1);
817        while (*code == OP_ALT)        while (*code == OP_ALT)
818          {          {
819          ADD_ACTIVE(code - start_code + 1 + LINK_SIZE,  0);          ADD_ACTIVE((int)(code - start_code + 1 + LINK_SIZE),  0);
820          code += GET(code, 1);          code += GET(code, 1);
821          }          }
822        break;        break;
# Line 690  for (;;) Line 827  for (;;)
827        ADD_ACTIVE(state_offset + 1, 0);        ADD_ACTIVE(state_offset + 1, 0);
828        code += 1 + GET(code, 2);        code += 1 + GET(code, 2);
829        while (*code == OP_ALT) code += GET(code, 1);        while (*code == OP_ALT) code += GET(code, 1);
830        ADD_ACTIVE(code - start_code + 1 + LINK_SIZE, 0);        ADD_ACTIVE((int)(code - start_code + 1 + LINK_SIZE), 0);
831          break;
832    
833          /*-----------------------------------------------------------------*/
834          case OP_SKIPZERO:
835          code += 1 + GET(code, 2);
836          while (*code == OP_ALT) code += GET(code, 1);
837          ADD_ACTIVE((int)(code - start_code + 1 + LINK_SIZE), 0);
838        break;        break;
839    
840        /*-----------------------------------------------------------------*/        /*-----------------------------------------------------------------*/
841        case OP_CIRC:        case OP_CIRC:
842        if ((ptr == start_subject && (md->moptions & PCRE_NOTBOL) == 0) ||        if (ptr == start_subject && (md->moptions & PCRE_NOTBOL) == 0)
           ((ims & PCRE_MULTILINE) != 0 &&  
             ptr != end_subject &&  
             WAS_NEWLINE(ptr)))  
843          { ADD_ACTIVE(state_offset + 1, 0); }          { ADD_ACTIVE(state_offset + 1, 0); }
844        break;        break;
845    
846        /*-----------------------------------------------------------------*/        /*-----------------------------------------------------------------*/
847        case OP_EOD:        case OP_CIRCM:
848        if (ptr >= end_subject) { ADD_ACTIVE(state_offset + 1, 0); }        if ((ptr == start_subject && (md->moptions & PCRE_NOTBOL) == 0) ||
849              (ptr != end_subject && WAS_NEWLINE(ptr)))
850            { ADD_ACTIVE(state_offset + 1, 0); }
851        break;        break;
852    
853        /*-----------------------------------------------------------------*/        /*-----------------------------------------------------------------*/
854        case OP_OPT:        case OP_EOD:
855        ims = code[1];        if (ptr >= end_subject)
856        ADD_ACTIVE(state_offset + 2, 0);          {
857            if ((md->moptions & PCRE_PARTIAL_HARD) != 0)
858              could_continue = TRUE;
859            else { ADD_ACTIVE(state_offset + 1, 0); }
860            }
861        break;        break;
862    
863        /*-----------------------------------------------------------------*/        /*-----------------------------------------------------------------*/
# Line 732  for (;;) Line 879  for (;;)
879    
880        /*-----------------------------------------------------------------*/        /*-----------------------------------------------------------------*/
881        case OP_ANY:        case OP_ANY:
882        if (clen > 0 && ((ims & PCRE_DOTALL) != 0 || !IS_NEWLINE(ptr)))        if (clen > 0 && !IS_NEWLINE(ptr))
883            { ADD_NEW(state_offset + 1, 0); }
884          break;
885    
886          /*-----------------------------------------------------------------*/
887          case OP_ALLANY:
888          if (clen > 0)
889          { ADD_NEW(state_offset + 1, 0); }          { ADD_NEW(state_offset + 1, 0); }
890        break;        break;
891    
892        /*-----------------------------------------------------------------*/        /*-----------------------------------------------------------------*/
893        case OP_EODN:        case OP_EODN:
894        if (clen == 0 || (IS_NEWLINE(ptr) && ptr == end_subject - md->nllen))        if (clen == 0 && (md->moptions & PCRE_PARTIAL_HARD) != 0)
895            could_continue = TRUE;
896          else if (clen == 0 || (IS_NEWLINE(ptr) && ptr == end_subject - md->nllen))
897          { ADD_ACTIVE(state_offset + 1, 0); }          { ADD_ACTIVE(state_offset + 1, 0); }
898        break;        break;
899    
# Line 746  for (;;) Line 901  for (;;)
901        case OP_DOLL:        case OP_DOLL:
902        if ((md->moptions & PCRE_NOTEOL) == 0)        if ((md->moptions & PCRE_NOTEOL) == 0)
903          {          {
904          if (clen == 0 ||          if (clen == 0 && (md->moptions & PCRE_PARTIAL_HARD) != 0)
905              (IS_NEWLINE(ptr) &&            could_continue = TRUE;
906                 ((ims & PCRE_MULTILINE) != 0 || ptr == end_subject - md->nllen)          else if (clen == 0 ||
907                ((md->poptions & PCRE_DOLLAR_ENDONLY) == 0 && IS_NEWLINE(ptr) &&
908                   (ptr == end_subject - md->nllen)
909              ))              ))
910            { ADD_ACTIVE(state_offset + 1, 0); }            { ADD_ACTIVE(state_offset + 1, 0); }
911          }          }
912        else if ((ims & PCRE_MULTILINE) != 0 && IS_NEWLINE(ptr))        break;
913    
914          /*-----------------------------------------------------------------*/
915          case OP_DOLLM:
916          if ((md->moptions & PCRE_NOTEOL) == 0)
917            {
918            if (clen == 0 && (md->moptions & PCRE_PARTIAL_HARD) != 0)
919              could_continue = TRUE;
920            else if (clen == 0 ||
921                ((md->poptions & PCRE_DOLLAR_ENDONLY) == 0 && IS_NEWLINE(ptr)))
922              { ADD_ACTIVE(state_offset + 1, 0); }
923            }
924          else if (IS_NEWLINE(ptr))
925          { ADD_ACTIVE(state_offset + 1, 0); }          { ADD_ACTIVE(state_offset + 1, 0); }
926        break;        break;
927    
# Line 784  for (;;) Line 953  for (;;)
953          if (ptr > start_subject)          if (ptr > start_subject)
954            {            {
955            const uschar *temp = ptr - 1;            const uschar *temp = ptr - 1;
956              if (temp < md->start_used_ptr) md->start_used_ptr = temp;
957  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
958            if (utf8) BACKCHAR(temp);            if (utf8) BACKCHAR(temp);
959  #endif  #endif
960            GETCHARTEST(d, temp);            GETCHARTEST(d, temp);
961    #ifdef SUPPORT_UCP
962              if ((md->poptions & PCRE_UCP) != 0)
963                {
964                if (d == '_') left_word = TRUE; else
965                  {
966                  int cat = UCD_CATEGORY(d);
967                  left_word = (cat == ucp_L || cat == ucp_N);
968                  }
969                }
970              else
971    #endif
972            left_word = d < 256 && (ctypes[d] & ctype_word) != 0;            left_word = d < 256 && (ctypes[d] & ctype_word) != 0;
973            }            }
974          else left_word = 0;          else left_word = FALSE;
975    
976          if (clen > 0) right_word = c < 256 && (ctypes[c] & ctype_word) != 0;          if (clen > 0)
977            else right_word = 0;            {
978    #ifdef SUPPORT_UCP
979              if ((md->poptions & PCRE_UCP) != 0)
980                {
981                if (c == '_') right_word = TRUE; else
982                  {
983                  int cat = UCD_CATEGORY(c);
984                  right_word = (cat == ucp_L || cat == ucp_N);
985                  }
986                }
987              else
988    #endif
989              right_word = c < 256 && (ctypes[c] & ctype_word) != 0;
990              }
991            else right_word = FALSE;
992    
993          if ((left_word == right_word) == (codevalue == OP_NOT_WORD_BOUNDARY))          if ((left_word == right_word) == (codevalue == OP_NOT_WORD_BOUNDARY))
994            { ADD_ACTIVE(state_offset + 1, 0); }            { ADD_ACTIVE(state_offset + 1, 0); }
# Line 812  for (;;) Line 1007  for (;;)
1007        if (clen > 0)        if (clen > 0)
1008          {          {
1009          BOOL OK;          BOOL OK;
1010          int category = _pcre_ucp_findprop(c, &chartype, &script);          const ucd_record * prop = GET_UCD(c);
1011          switch(code[1])          switch(code[1])
1012            {            {
1013            case PT_ANY:            case PT_ANY:
# Line 820  for (;;) Line 1015  for (;;)
1015            break;            break;
1016    
1017            case PT_LAMP:            case PT_LAMP:
1018            OK = chartype == ucp_Lu || chartype == ucp_Ll || chartype == ucp_Lt;            OK = prop->chartype == ucp_Lu || prop->chartype == ucp_Ll ||
1019                   prop->chartype == ucp_Lt;
1020            break;            break;
1021    
1022            case PT_GC:            case PT_GC:
1023            OK = category == code[2];            OK = _pcre_ucp_gentype[prop->chartype] == code[2];
1024            break;            break;
1025    
1026            case PT_PC:            case PT_PC:
1027            OK = chartype == code[2];            OK = prop->chartype == code[2];
1028            break;            break;
1029    
1030            case PT_SC:            case PT_SC:
1031            OK = script == code[2];            OK = prop->script == code[2];
1032              break;
1033    
1034              /* These are specials for combination cases. */
1035    
1036              case PT_ALNUM:
1037              OK = _pcre_ucp_gentype[prop->chartype] == ucp_L ||
1038                   _pcre_ucp_gentype[prop->chartype] == ucp_N;
1039              break;
1040    
1041              case PT_SPACE:    /* Perl space */
1042              OK = _pcre_ucp_gentype[prop->chartype] == ucp_Z ||
1043                   c == CHAR_HT || c == CHAR_NL || c == CHAR_FF || c == CHAR_CR;
1044              break;
1045    
1046              case PT_PXSPACE:  /* POSIX space */
1047              OK = _pcre_ucp_gentype[prop->chartype] == ucp_Z ||
1048                   c == CHAR_HT || c == CHAR_NL || c == CHAR_VT ||
1049                   c == CHAR_FF || c == CHAR_CR;
1050              break;
1051    
1052              case PT_WORD:
1053              OK = _pcre_ucp_gentype[prop->chartype] == ucp_L ||
1054                   _pcre_ucp_gentype[prop->chartype] == ucp_N ||
1055                   c == CHAR_UNDERSCORE;
1056            break;            break;
1057    
1058            /* Should never occur, but keep compilers from grumbling. */            /* Should never occur, but keep compilers from grumbling. */
# Line 852  for (;;) Line 1072  for (;;)
1072  /* ========================================================================== */  /* ========================================================================== */
1073        /* These opcodes likewise inspect the subject character, but have an        /* These opcodes likewise inspect the subject character, but have an
1074        argument that is not a data character. It is one of these opcodes:        argument that is not a data character. It is one of these opcodes:
1075        OP_ANY, OP_DIGIT, OP_NOT_DIGIT, OP_WHITESPACE, OP_NOT_SPACE, OP_WORDCHAR,        OP_ANY, OP_ALLANY, OP_DIGIT, OP_NOT_DIGIT, OP_WHITESPACE, OP_NOT_SPACE,
1076        OP_NOT_WORDCHAR. The value is loaded into d. */        OP_WORDCHAR, OP_NOT_WORDCHAR. The value is loaded into d. */
1077    
1078        case OP_TYPEPLUS:        case OP_TYPEPLUS:
1079        case OP_TYPEMINPLUS:        case OP_TYPEMINPLUS:
# Line 864  for (;;) Line 1084  for (;;)
1084          {          {
1085          if ((c >= 256 && d != OP_DIGIT && d != OP_WHITESPACE && d != OP_WORDCHAR) ||          if ((c >= 256 && d != OP_DIGIT && d != OP_WHITESPACE && d != OP_WORDCHAR) ||
1086              (c < 256 &&              (c < 256 &&
1087                (d != OP_ANY ||                (d != OP_ANY || !IS_NEWLINE(ptr)) &&
                (ims & PCRE_DOTALL) != 0 ||  
                !IS_NEWLINE(ptr)  
               ) &&  
1088                ((ctypes[c] & toptable1[d]) ^ toptable2[d]) != 0))                ((ctypes[c] & toptable1[d]) ^ toptable2[d]) != 0))
1089            {            {
1090            if (count > 0 && codevalue == OP_TYPEPOSPLUS)            if (count > 0 && codevalue == OP_TYPEPOSPLUS)
# Line 890  for (;;) Line 1107  for (;;)
1107          {          {
1108          if ((c >= 256 && d != OP_DIGIT && d != OP_WHITESPACE && d != OP_WORDCHAR) ||          if ((c >= 256 && d != OP_DIGIT && d != OP_WHITESPACE && d != OP_WORDCHAR) ||
1109              (c < 256 &&              (c < 256 &&
1110                (d != OP_ANY ||                (d != OP_ANY || !IS_NEWLINE(ptr)) &&
                (ims & PCRE_DOTALL) != 0 ||  
                !IS_NEWLINE(ptr)  
               ) &&  
1111                ((ctypes[c] & toptable1[d]) ^ toptable2[d]) != 0))                ((ctypes[c] & toptable1[d]) ^ toptable2[d]) != 0))
1112            {            {
1113            if (codevalue == OP_TYPEPOSQUERY)            if (codevalue == OP_TYPEPOSQUERY)
# Line 915  for (;;) Line 1129  for (;;)
1129          {          {
1130          if ((c >= 256 && d != OP_DIGIT && d != OP_WHITESPACE && d != OP_WORDCHAR) ||          if ((c >= 256 && d != OP_DIGIT && d != OP_WHITESPACE && d != OP_WORDCHAR) ||
1131              (c < 256 &&              (c < 256 &&
1132                (d != OP_ANY ||                (d != OP_ANY || !IS_NEWLINE(ptr)) &&
                (ims & PCRE_DOTALL) != 0 ||  
                !IS_NEWLINE(ptr)  
               ) &&  
1133                ((ctypes[c] & toptable1[d]) ^ toptable2[d]) != 0))                ((ctypes[c] & toptable1[d]) ^ toptable2[d]) != 0))
1134            {            {
1135            if (codevalue == OP_TYPEPOSSTAR)            if (codevalue == OP_TYPEPOSSTAR)
# Line 938  for (;;) Line 1149  for (;;)
1149          {          {
1150          if ((c >= 256 && d != OP_DIGIT && d != OP_WHITESPACE && d != OP_WORDCHAR) ||          if ((c >= 256 && d != OP_DIGIT && d != OP_WHITESPACE && d != OP_WORDCHAR) ||
1151              (c < 256 &&              (c < 256 &&
1152                (d != OP_ANY ||                (d != OP_ANY || !IS_NEWLINE(ptr)) &&
                (ims & PCRE_DOTALL) != 0 ||  
                !IS_NEWLINE(ptr)  
               ) &&  
1153                ((ctypes[c] & toptable1[d]) ^ toptable2[d]) != 0))                ((ctypes[c] & toptable1[d]) ^ toptable2[d]) != 0))
1154            {            {
1155            if (++count >= GET2(code, 1))            if (++count >= GET2(code, 1))
# Line 962  for (;;) Line 1170  for (;;)
1170          {          {
1171          if ((c >= 256 && d != OP_DIGIT && d != OP_WHITESPACE && d != OP_WORDCHAR) ||          if ((c >= 256 && d != OP_DIGIT && d != OP_WHITESPACE && d != OP_WORDCHAR) ||
1172              (c < 256 &&              (c < 256 &&
1173                (d != OP_ANY ||                (d != OP_ANY || !IS_NEWLINE(ptr)) &&
                (ims & PCRE_DOTALL) != 0 ||  
                !IS_NEWLINE(ptr)  
               ) &&  
1174                ((ctypes[c] & toptable1[d]) ^ toptable2[d]) != 0))                ((ctypes[c] & toptable1[d]) ^ toptable2[d]) != 0))
1175            {            {
1176            if (codevalue == OP_TYPEPOSUPTO)            if (codevalue == OP_TYPEPOSUPTO)
# Line 996  for (;;) Line 1201  for (;;)
1201        if (clen > 0)        if (clen > 0)
1202          {          {
1203          BOOL OK;          BOOL OK;
1204          int category = _pcre_ucp_findprop(c, &chartype, &script);          const ucd_record * prop = GET_UCD(c);
1205          switch(code[2])          switch(code[2])
1206            {            {
1207            case PT_ANY:            case PT_ANY:
# Line 1004  for (;;) Line 1209  for (;;)
1209            break;            break;
1210    
1211            case PT_LAMP:            case PT_LAMP:
1212            OK = chartype == ucp_Lu || chartype == ucp_Ll || chartype == ucp_Lt;            OK = prop->chartype == ucp_Lu || prop->chartype == ucp_Ll ||
1213                prop->chartype == ucp_Lt;
1214            break;            break;
1215    
1216            case PT_GC:            case PT_GC:
1217            OK = category == code[3];            OK = _pcre_ucp_gentype[prop->chartype] == code[3];
1218            break;            break;
1219    
1220            case PT_PC:            case PT_PC:
1221            OK = chartype == code[3];            OK = prop->chartype == code[3];
1222            break;            break;
1223    
1224            case PT_SC:            case PT_SC:
1225            OK = script == code[3];            OK = prop->script == code[3];
1226              break;
1227    
1228              /* These are specials for combination cases. */
1229    
1230              case PT_ALNUM:
1231              OK = _pcre_ucp_gentype[prop->chartype] == ucp_L ||
1232                   _pcre_ucp_gentype[prop->chartype] == ucp_N;
1233              break;
1234    
1235              case PT_SPACE:    /* Perl space */
1236              OK = _pcre_ucp_gentype[prop->chartype] == ucp_Z ||
1237                   c == CHAR_HT || c == CHAR_NL || c == CHAR_FF || c == CHAR_CR;
1238              break;
1239    
1240              case PT_PXSPACE:  /* POSIX space */
1241              OK = _pcre_ucp_gentype[prop->chartype] == ucp_Z ||
1242                   c == CHAR_HT || c == CHAR_NL || c == CHAR_VT ||
1243                   c == CHAR_FF || c == CHAR_CR;
1244              break;
1245    
1246              case PT_WORD:
1247              OK = _pcre_ucp_gentype[prop->chartype] == ucp_L ||
1248                   _pcre_ucp_gentype[prop->chartype] == ucp_N ||
1249                   c == CHAR_UNDERSCORE;
1250            break;            break;
1251    
1252            /* Should never occur, but keep compilers from grumbling. */            /* Should never occur, but keep compilers from grumbling. */
# Line 1045  for (;;) Line 1275  for (;;)
1275        case OP_EXTUNI_EXTRA + OP_TYPEPOSPLUS:        case OP_EXTUNI_EXTRA + OP_TYPEPOSPLUS:
1276        count = current_state->count;  /* Already matched */        count = current_state->count;  /* Already matched */
1277        if (count > 0) { ADD_ACTIVE(state_offset + 2, 0); }        if (count > 0) { ADD_ACTIVE(state_offset + 2, 0); }
1278        if (clen > 0 && _pcre_ucp_findprop(c, &chartype, &script) != ucp_M)        if (clen > 0 && UCD_CATEGORY(c) != ucp_M)
1279          {          {
1280          const uschar *nptr = ptr + clen;          const uschar *nptr = ptr + clen;
1281          int ncount = 0;          int ncount = 0;
# Line 1059  for (;;) Line 1289  for (;;)
1289            int nd;            int nd;
1290            int ndlen = 1;            int ndlen = 1;
1291            GETCHARLEN(nd, nptr, ndlen);            GETCHARLEN(nd, nptr, ndlen);
1292            if (_pcre_ucp_findprop(nd, &chartype, &script) != ucp_M) break;            if (UCD_CATEGORY(nd) != ucp_M) break;
1293            ncount++;            ncount++;
1294            nptr += ndlen;            nptr += ndlen;
1295            }            }
# Line 1080  for (;;) Line 1310  for (;;)
1310          int ncount = 0;          int ncount = 0;
1311          switch (c)          switch (c)
1312            {            {
           case 0x000d:  
           if (ptr + 1 < end_subject && ptr[1] == 0x0a) ncount = 1;  
           /* Fall through */  
           case 0x000a:  
1313            case 0x000b:            case 0x000b:
1314            case 0x000c:            case 0x000c:
1315            case 0x0085:            case 0x0085:
1316            case 0x2028:            case 0x2028:
1317            case 0x2029:            case 0x2029:
1318              if ((md->moptions & PCRE_BSR_ANYCRLF) != 0) break;
1319              goto ANYNL01;
1320    
1321              case 0x000d:
1322              if (ptr + 1 < end_subject && ptr[1] == 0x0a) ncount = 1;
1323              /* Fall through */
1324    
1325              ANYNL01:
1326              case 0x000a:
1327            if (count > 0 && codevalue == OP_ANYNL_EXTRA + OP_TYPEPOSPLUS)            if (count > 0 && codevalue == OP_ANYNL_EXTRA + OP_TYPEPOSPLUS)
1328              {              {
1329              active_count--;           /* Remove non-match possibility */              active_count--;           /* Remove non-match possibility */
# Line 1097  for (;;) Line 1332  for (;;)
1332            count++;            count++;
1333            ADD_NEW_DATA(-state_offset, count, ncount);            ADD_NEW_DATA(-state_offset, count, ncount);
1334            break;            break;
1335    
1336            default:            default:
1337            break;            break;
1338            }            }
# Line 1212  for (;;) Line 1448  for (;;)
1448        if (clen > 0)        if (clen > 0)
1449          {          {
1450          BOOL OK;          BOOL OK;
1451          int category = _pcre_ucp_findprop(c, &chartype, &script);          const ucd_record * prop = GET_UCD(c);
1452          switch(code[2])          switch(code[2])
1453            {            {
1454            case PT_ANY:            case PT_ANY:
# Line 1220  for (;;) Line 1456  for (;;)
1456            break;            break;
1457    
1458            case PT_LAMP:            case PT_LAMP:
1459            OK = chartype == ucp_Lu || chartype == ucp_Ll || chartype == ucp_Lt;            OK = prop->chartype == ucp_Lu || prop->chartype == ucp_Ll ||
1460                prop->chartype == ucp_Lt;
1461            break;            break;
1462    
1463            case PT_GC:            case PT_GC:
1464            OK = category == code[3];            OK = _pcre_ucp_gentype[prop->chartype] == code[3];
1465            break;            break;
1466    
1467            case PT_PC:            case PT_PC:
1468            OK = chartype == code[3];            OK = prop->chartype == code[3];
1469            break;            break;
1470    
1471            case PT_SC:            case PT_SC:
1472            OK = script == code[3];            OK = prop->script == code[3];
1473              break;
1474    
1475              /* These are specials for combination cases. */
1476    
1477              case PT_ALNUM:
1478              OK = _pcre_ucp_gentype[prop->chartype] == ucp_L ||
1479                   _pcre_ucp_gentype[prop->chartype] == ucp_N;
1480              break;
1481    
1482              case PT_SPACE:    /* Perl space */
1483              OK = _pcre_ucp_gentype[prop->chartype] == ucp_Z ||
1484                   c == CHAR_HT || c == CHAR_NL || c == CHAR_FF || c == CHAR_CR;
1485              break;
1486    
1487              case PT_PXSPACE:  /* POSIX space */
1488              OK = _pcre_ucp_gentype[prop->chartype] == ucp_Z ||
1489                   c == CHAR_HT || c == CHAR_NL || c == CHAR_VT ||
1490                   c == CHAR_FF || c == CHAR_CR;
1491              break;
1492    
1493              case PT_WORD:
1494              OK = _pcre_ucp_gentype[prop->chartype] == ucp_L ||
1495                   _pcre_ucp_gentype[prop->chartype] == ucp_N ||
1496                   c == CHAR_UNDERSCORE;
1497            break;            break;
1498    
1499            /* Should never occur, but keep compilers from grumbling. */            /* Should never occur, but keep compilers from grumbling. */
# Line 1270  for (;;) Line 1531  for (;;)
1531        QS2:        QS2:
1532    
1533        ADD_ACTIVE(state_offset + 2, 0);        ADD_ACTIVE(state_offset + 2, 0);
1534        if (clen > 0 && _pcre_ucp_findprop(c, &chartype, &script) != ucp_M)        if (clen > 0 && UCD_CATEGORY(c) != ucp_M)
1535          {          {
1536          const uschar *nptr = ptr + clen;          const uschar *nptr = ptr + clen;
1537          int ncount = 0;          int ncount = 0;
# Line 1285  for (;;) Line 1546  for (;;)
1546            int nd;            int nd;
1547            int ndlen = 1;            int ndlen = 1;
1548            GETCHARLEN(nd, nptr, ndlen);            GETCHARLEN(nd, nptr, ndlen);
1549            if (_pcre_ucp_findprop(nd, &chartype, &script) != ucp_M) break;            if (UCD_CATEGORY(nd) != ucp_M) break;
1550            ncount++;            ncount++;
1551            nptr += ndlen;            nptr += ndlen;
1552            }            }
# Line 1313  for (;;) Line 1574  for (;;)
1574          int ncount = 0;          int ncount = 0;
1575          switch (c)          switch (c)
1576            {            {
           case 0x000d:  
           if (ptr + 1 < end_subject && ptr[1] == 0x0a) ncount = 1;  
           /* Fall through */  
           case 0x000a:  
1577            case 0x000b:            case 0x000b:
1578            case 0x000c:            case 0x000c:
1579            case 0x0085:            case 0x0085:
1580            case 0x2028:            case 0x2028:
1581            case 0x2029:            case 0x2029:
1582              if ((md->moptions & PCRE_BSR_ANYCRLF) != 0) break;
1583              goto ANYNL02;
1584    
1585              case 0x000d:
1586              if (ptr + 1 < end_subject && ptr[1] == 0x0a) ncount = 1;
1587              /* Fall through */
1588    
1589              ANYNL02:
1590              case 0x000a:
1591            if (codevalue == OP_ANYNL_EXTRA + OP_TYPEPOSSTAR ||            if (codevalue == OP_ANYNL_EXTRA + OP_TYPEPOSSTAR ||
1592                codevalue == OP_ANYNL_EXTRA + OP_TYPEPOSQUERY)                codevalue == OP_ANYNL_EXTRA + OP_TYPEPOSQUERY)
1593              {              {
# Line 1330  for (;;) Line 1596  for (;;)
1596              }              }
1597            ADD_NEW_DATA(-(state_offset + count), 0, ncount);            ADD_NEW_DATA(-(state_offset + count), 0, ncount);
1598            break;            break;
1599    
1600            default:            default:
1601            break;            break;
1602            }            }
# Line 1453  for (;;) Line 1720  for (;;)
1720        if (clen > 0)        if (clen > 0)
1721          {          {
1722          BOOL OK;          BOOL OK;
1723          int category = _pcre_ucp_findprop(c, &chartype, &script);          const ucd_record * prop = GET_UCD(c);
1724          switch(code[4])          switch(code[4])
1725            {            {
1726            case PT_ANY:            case PT_ANY:
# Line 1461  for (;;) Line 1728  for (;;)
1728            break;            break;
1729    
1730            case PT_LAMP:            case PT_LAMP:
1731            OK = chartype == ucp_Lu || chartype == ucp_Ll || chartype == ucp_Lt;            OK = prop->chartype == ucp_Lu || prop->chartype == ucp_Ll ||
1732                prop->chartype == ucp_Lt;
1733            break;            break;
1734    
1735            case PT_GC:            case PT_GC:
1736            OK = category == code[5];            OK = _pcre_ucp_gentype[prop->chartype] == code[5];
1737            break;            break;
1738    
1739            case PT_PC:            case PT_PC:
1740            OK = chartype == code[5];            OK = prop->chartype == code[5];
1741            break;            break;
1742    
1743            case PT_SC:            case PT_SC:
1744            OK = script == code[5];            OK = prop->script == code[5];
1745              break;
1746    
1747              /* These are specials for combination cases. */
1748    
1749              case PT_ALNUM:
1750              OK = _pcre_ucp_gentype[prop->chartype] == ucp_L ||
1751                   _pcre_ucp_gentype[prop->chartype] == ucp_N;
1752              break;
1753    
1754              case PT_SPACE:    /* Perl space */
1755              OK = _pcre_ucp_gentype[prop->chartype] == ucp_Z ||
1756                   c == CHAR_HT || c == CHAR_NL || c == CHAR_FF || c == CHAR_CR;
1757              break;
1758    
1759              case PT_PXSPACE:  /* POSIX space */
1760              OK = _pcre_ucp_gentype[prop->chartype] == ucp_Z ||
1761                   c == CHAR_HT || c == CHAR_NL || c == CHAR_VT ||
1762                   c == CHAR_FF || c == CHAR_CR;
1763              break;
1764    
1765              case PT_WORD:
1766              OK = _pcre_ucp_gentype[prop->chartype] == ucp_L ||
1767                   _pcre_ucp_gentype[prop->chartype] == ucp_N ||
1768                   c == CHAR_UNDERSCORE;
1769            break;            break;
1770    
1771            /* Should never occur, but keep compilers from grumbling. */            /* Should never occur, but keep compilers from grumbling. */
# Line 1506  for (;;) Line 1798  for (;;)
1798        if (codevalue != OP_EXTUNI_EXTRA + OP_TYPEEXACT)        if (codevalue != OP_EXTUNI_EXTRA + OP_TYPEEXACT)
1799          { ADD_ACTIVE(state_offset + 4, 0); }          { ADD_ACTIVE(state_offset + 4, 0); }
1800        count = current_state->count;  /* Number already matched */        count = current_state->count;  /* Number already matched */
1801        if (clen > 0 && _pcre_ucp_findprop(c, &chartype, &script) != ucp_M)        if (clen > 0 && UCD_CATEGORY(c) != ucp_M)
1802          {          {
1803          const uschar *nptr = ptr + clen;          const uschar *nptr = ptr + clen;
1804          int ncount = 0;          int ncount = 0;
# Line 1520  for (;;) Line 1812  for (;;)
1812            int nd;            int nd;
1813            int ndlen = 1;            int ndlen = 1;
1814            GETCHARLEN(nd, nptr, ndlen);            GETCHARLEN(nd, nptr, ndlen);
1815            if (_pcre_ucp_findprop(nd, &chartype, &script) != ucp_M) break;            if (UCD_CATEGORY(nd) != ucp_M) break;
1816            ncount++;            ncount++;
1817            nptr += ndlen;            nptr += ndlen;
1818            }            }
# Line 1545  for (;;) Line 1837  for (;;)
1837          int ncount = 0;          int ncount = 0;
1838          switch (c)          switch (c)
1839            {            {
           case 0x000d:  
           if (ptr + 1 < end_subject && ptr[1] == 0x0a) ncount = 1;  
           /* Fall through */  
           case 0x000a:  
1840            case 0x000b:            case 0x000b:
1841            case 0x000c:            case 0x000c:
1842            case 0x0085:            case 0x0085:
1843            case 0x2028:            case 0x2028:
1844            case 0x2029:            case 0x2029:
1845              if ((md->moptions & PCRE_BSR_ANYCRLF) != 0) break;
1846              goto ANYNL03;
1847    
1848              case 0x000d:
1849              if (ptr + 1 < end_subject && ptr[1] == 0x0a) ncount = 1;
1850              /* Fall through */
1851    
1852              ANYNL03:
1853              case 0x000a:
1854            if (codevalue == OP_ANYNL_EXTRA + OP_TYPEPOSUPTO)            if (codevalue == OP_ANYNL_EXTRA + OP_TYPEPOSUPTO)
1855              {              {
1856              active_count--;           /* Remove non-match possibility */              active_count--;           /* Remove non-match possibility */
# Line 1564  for (;;) Line 1861  for (;;)
1861            else            else
1862              { ADD_NEW_DATA(-state_offset, count, ncount); }              { ADD_NEW_DATA(-state_offset, count, ncount); }
1863            break;            break;
1864    
1865            default:            default:
1866            break;            break;
1867            }            }
# Line 1679  for (;;) Line 1977  for (;;)
1977        break;        break;
1978    
1979        /*-----------------------------------------------------------------*/        /*-----------------------------------------------------------------*/
1980        case OP_CHARNC:        case OP_CHARI:
1981        if (clen == 0) break;        if (clen == 0) break;
1982    
1983  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
# Line 1694  for (;;) Line 1992  for (;;)
1992            other case of the character. */            other case of the character. */
1993    
1994  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
1995            othercase = _pcre_ucp_othercase(c);            othercase = UCD_OTHERCASE(c);
1996  #else  #else
1997            othercase = NOTACHAR;            othercase = NOTACHAR;
1998  #endif  #endif
# Line 1719  for (;;) Line 2017  for (;;)
2017        to wait for them to pass before continuing. */        to wait for them to pass before continuing. */
2018    
2019        case OP_EXTUNI:        case OP_EXTUNI:
2020        if (clen > 0 && _pcre_ucp_findprop(c, &chartype, &script) != ucp_M)        if (clen > 0 && UCD_CATEGORY(c) != ucp_M)
2021          {          {
2022          const uschar *nptr = ptr + clen;          const uschar *nptr = ptr + clen;
2023          int ncount = 0;          int ncount = 0;
# Line 1727  for (;;) Line 2025  for (;;)
2025            {            {
2026            int nclen = 1;            int nclen = 1;
2027            GETCHARLEN(c, nptr, nclen);            GETCHARLEN(c, nptr, nclen);
2028            if (_pcre_ucp_findprop(c, &chartype, &script) != ucp_M) break;            if (UCD_CATEGORY(c) != ucp_M) break;
2029            ncount++;            ncount++;
2030            nptr += nclen;            nptr += nclen;
2031            }            }
# Line 1744  for (;;) Line 2042  for (;;)
2042        case OP_ANYNL:        case OP_ANYNL:
2043        if (clen > 0) switch(c)        if (clen > 0) switch(c)
2044          {          {
         case 0x000a:  
2045          case 0x000b:          case 0x000b:
2046          case 0x000c:          case 0x000c:
2047          case 0x0085:          case 0x0085:
2048          case 0x2028:          case 0x2028:
2049          case 0x2029:          case 0x2029:
2050            if ((md->moptions & PCRE_BSR_ANYCRLF) != 0) break;
2051    
2052            case 0x000a:
2053          ADD_NEW(state_offset + 1, 0);          ADD_NEW(state_offset + 1, 0);
2054          break;          break;
2055    
2056          case 0x000d:          case 0x000d:
2057          if (ptr + 1 < end_subject && ptr[1] == 0x0a)          if (ptr + 1 < end_subject && ptr[1] == 0x0a)
2058            {            {
# Line 1862  for (;;) Line 2163  for (;;)
2163        break;        break;
2164    
2165        /*-----------------------------------------------------------------*/        /*-----------------------------------------------------------------*/
2166        /* Match a negated single character. This is only used for one-byte        /* Match a negated single character casefully. This is only used for
2167        characters, that is, we know that d < 256. The character we are        one-byte characters, that is, we know that d < 256. The character we are
2168        checking (c) can be multibyte. */        checking (c) can be multibyte. */
2169    
2170        case OP_NOT:        case OP_NOT:
2171        if (clen > 0)        if (clen > 0 && c != d) { ADD_NEW(state_offset + dlen + 1, 0); }
         {  
         unsigned int otherd = ((ims & PCRE_CASELESS) != 0)? fcc[d] : d;  
         if (c != d && c != otherd) { ADD_NEW(state_offset + dlen + 1, 0); }  
         }  
2172        break;        break;
2173    
2174        /*-----------------------------------------------------------------*/        /*-----------------------------------------------------------------*/
2175          /* Match a negated single character caselessly. This is only used for
2176          one-byte characters, that is, we know that d < 256. The character we are
2177          checking (c) can be multibyte. */
2178    
2179          case OP_NOTI:
2180          if (clen > 0 && c != d && c != fcc[d])
2181            { ADD_NEW(state_offset + dlen + 1, 0); }
2182          break;
2183    
2184          /*-----------------------------------------------------------------*/
2185          case OP_PLUSI:
2186          case OP_MINPLUSI:
2187          case OP_POSPLUSI:
2188          case OP_NOTPLUSI:
2189          case OP_NOTMINPLUSI:
2190          case OP_NOTPOSPLUSI:
2191          caseless = TRUE;
2192          codevalue -= OP_STARI - OP_STAR;
2193    
2194          /* Fall through */
2195        case OP_PLUS:        case OP_PLUS:
2196        case OP_MINPLUS:        case OP_MINPLUS:
2197        case OP_POSPLUS:        case OP_POSPLUS:
# Line 1886  for (;;) Line 2203  for (;;)
2203        if (clen > 0)        if (clen > 0)
2204          {          {
2205          unsigned int otherd = NOTACHAR;          unsigned int otherd = NOTACHAR;
2206          if ((ims & PCRE_CASELESS) != 0)          if (caseless)
2207            {            {
2208  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2209            if (utf8 && d >= 128)            if (utf8 && d >= 128)
2210              {              {
2211  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
2212              otherd = _pcre_ucp_othercase(d);              otherd = UCD_OTHERCASE(d);
2213  #endif  /* SUPPORT_UCP */  #endif  /* SUPPORT_UCP */
2214              }              }
2215            else            else
# Line 1914  for (;;) Line 2231  for (;;)
2231        break;        break;
2232    
2233        /*-----------------------------------------------------------------*/        /*-----------------------------------------------------------------*/
2234          case OP_QUERYI:
2235          case OP_MINQUERYI:
2236          case OP_POSQUERYI:
2237          case OP_NOTQUERYI:
2238          case OP_NOTMINQUERYI:
2239          case OP_NOTPOSQUERYI:
2240          caseless = TRUE;
2241          codevalue -= OP_STARI - OP_STAR;
2242          /* Fall through */
2243        case OP_QUERY:        case OP_QUERY:
2244        case OP_MINQUERY:        case OP_MINQUERY:
2245        case OP_POSQUERY:        case OP_POSQUERY:
# Line 1924  for (;;) Line 2250  for (;;)
2250        if (clen > 0)        if (clen > 0)
2251          {          {
2252          unsigned int otherd = NOTACHAR;          unsigned int otherd = NOTACHAR;
2253          if ((ims & PCRE_CASELESS) != 0)          if (caseless)
2254            {            {
2255  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2256            if (utf8 && d >= 128)            if (utf8 && d >= 128)
2257              {              {
2258  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
2259              otherd = _pcre_ucp_othercase(d);              otherd = UCD_OTHERCASE(d);
2260  #endif  /* SUPPORT_UCP */  #endif  /* SUPPORT_UCP */
2261              }              }
2262            else            else
# Line 1950  for (;;) Line 2276  for (;;)
2276        break;        break;
2277    
2278        /*-----------------------------------------------------------------*/        /*-----------------------------------------------------------------*/
2279          case OP_STARI:
2280          case OP_MINSTARI:
2281          case OP_POSSTARI:
2282          case OP_NOTSTARI:
2283          case OP_NOTMINSTARI:
2284          case OP_NOTPOSSTARI:
2285          caseless = TRUE;
2286          codevalue -= OP_STARI - OP_STAR;
2287          /* Fall through */
2288        case OP_STAR:        case OP_STAR:
2289        case OP_MINSTAR:        case OP_MINSTAR:
2290        case OP_POSSTAR:        case OP_POSSTAR:
# Line 1960  for (;;) Line 2295  for (;;)
2295        if (clen > 0)        if (clen > 0)
2296          {          {
2297          unsigned int otherd = NOTACHAR;          unsigned int otherd = NOTACHAR;
2298          if ((ims & PCRE_CASELESS) != 0)          if (caseless)
2299            {            {
2300  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2301            if (utf8 && d >= 128)            if (utf8 && d >= 128)
2302              {              {
2303  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
2304              otherd = _pcre_ucp_othercase(d);              otherd = UCD_OTHERCASE(d);
2305  #endif  /* SUPPORT_UCP */  #endif  /* SUPPORT_UCP */
2306              }              }
2307            else            else
# Line 1986  for (;;) Line 2321  for (;;)
2321        break;        break;
2322    
2323        /*-----------------------------------------------------------------*/        /*-----------------------------------------------------------------*/
2324          case OP_EXACTI:
2325          case OP_NOTEXACTI:
2326          caseless = TRUE;
2327          codevalue -= OP_STARI - OP_STAR;
2328          /* Fall through */
2329        case OP_EXACT:        case OP_EXACT:
2330        case OP_NOTEXACT:        case OP_NOTEXACT:
2331        count = current_state->count;  /* Number already matched */        count = current_state->count;  /* Number already matched */
2332        if (clen > 0)        if (clen > 0)
2333          {          {
2334          unsigned int otherd = NOTACHAR;          unsigned int otherd = NOTACHAR;
2335          if ((ims & PCRE_CASELESS) != 0)          if (caseless)
2336            {            {
2337  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2338            if (utf8 && d >= 128)            if (utf8 && d >= 128)
2339              {              {
2340  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
2341              otherd = _pcre_ucp_othercase(d);              otherd = UCD_OTHERCASE(d);
2342  #endif  /* SUPPORT_UCP */  #endif  /* SUPPORT_UCP */
2343              }              }
2344            else            else
# Line 2016  for (;;) Line 2356  for (;;)
2356        break;        break;
2357    
2358        /*-----------------------------------------------------------------*/        /*-----------------------------------------------------------------*/
2359          case OP_UPTOI:
2360          case OP_MINUPTOI:
2361          case OP_POSUPTOI:
2362          case OP_NOTUPTOI:
2363          case OP_NOTMINUPTOI:
2364          case OP_NOTPOSUPTOI:
2365          caseless = TRUE;
2366          codevalue -= OP_STARI - OP_STAR;
2367          /* Fall through */
2368        case OP_UPTO:        case OP_UPTO:
2369        case OP_MINUPTO:        case OP_MINUPTO:
2370        case OP_POSUPTO:        case OP_POSUPTO:
# Line 2027  for (;;) Line 2376  for (;;)
2376        if (clen > 0)        if (clen > 0)
2377          {          {
2378          unsigned int otherd = NOTACHAR;          unsigned int otherd = NOTACHAR;
2379          if ((ims & PCRE_CASELESS) != 0)          if (caseless)
2380            {            {
2381  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2382            if (utf8 && d >= 128)            if (utf8 && d >= 128)
2383              {              {
2384  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
2385              otherd = _pcre_ucp_othercase(d);              otherd = UCD_OTHERCASE(d);
2386  #endif  /* SUPPORT_UCP */  #endif  /* SUPPORT_UCP */
2387              }              }
2388            else            else
# Line 2094  for (;;) Line 2443  for (;;)
2443          points to the byte after the end of the class. If there is a          points to the byte after the end of the class. If there is a
2444          quantifier, this is where it will be. */          quantifier, this is where it will be. */
2445    
2446          next_state_offset = ecode - start_code;          next_state_offset = (int)(ecode - start_code);
2447    
2448          switch (*ecode)          switch (*ecode)
2449            {            {
# Line 2141  for (;;) Line 2490  for (;;)
2490    
2491  /* ========================================================================== */  /* ========================================================================== */
2492        /* These are the opcodes for fancy brackets of various kinds. We have        /* These are the opcodes for fancy brackets of various kinds. We have
2493        to use recursion in order to handle them. */        to use recursion in order to handle them. The "always failing" assertion
2494          (?!) is optimised to OP_FAIL when compiling, so we have to support that,
2495          though the other "backtracking verbs" are not supported. */
2496    
2497          case OP_FAIL:
2498          forced_fail++;    /* Count FAILs for multiple states */
2499          break;
2500    
2501        case OP_ASSERT:        case OP_ASSERT:
2502        case OP_ASSERT_NOT:        case OP_ASSERT_NOT:
# Line 2159  for (;;) Line 2514  for (;;)
2514            md,                                   /* static match data */            md,                                   /* static match data */
2515            code,                                 /* this subexpression's code */            code,                                 /* this subexpression's code */
2516            ptr,                                  /* where we currently are */            ptr,                                  /* where we currently are */
2517            ptr - start_subject,                  /* start offset */            (int)(ptr - start_subject),           /* start offset */
2518            local_offsets,                        /* offset vector */            local_offsets,                        /* offset vector */
2519            sizeof(local_offsets)/sizeof(int),    /* size of same */            sizeof(local_offsets)/sizeof(int),    /* size of same */
2520            local_workspace,                      /* workspace vector */            local_workspace,                      /* workspace vector */
2521            sizeof(local_workspace)/sizeof(int),  /* size of same */            sizeof(local_workspace)/sizeof(int),  /* size of same */
           ims,                                  /* the current ims flags */  
2522            rlevel,                               /* function recursion level */            rlevel,                               /* function recursion level */
2523            recursing);                           /* pass on regex recursion */            recursing);                           /* pass on regex recursion */
2524    
2525            if (rc == PCRE_ERROR_DFA_UITEM) return rc;
2526          if ((rc >= 0) == (codevalue == OP_ASSERT || codevalue == OP_ASSERTBACK))          if ((rc >= 0) == (codevalue == OP_ASSERT || codevalue == OP_ASSERTBACK))
2527              { ADD_ACTIVE(endasscode + LINK_SIZE + 1 - start_code, 0); }              { ADD_ACTIVE((int)(endasscode + LINK_SIZE + 1 - start_code), 0); }
2528          }          }
2529        break;        break;
2530    
# Line 2179  for (;;) Line 2534  for (;;)
2534          {          {
2535          int local_offsets[1000];          int local_offsets[1000];
2536          int local_workspace[1000];          int local_workspace[1000];
2537          int condcode = code[LINK_SIZE+1];          int codelink = GET(code, 1);
2538            int condcode;
2539    
2540            /* Because of the way auto-callout works during compile, a callout item
2541            is inserted between OP_COND and an assertion condition. This does not
2542            happen for the other conditions. */
2543    
2544            if (code[LINK_SIZE+1] == OP_CALLOUT)
2545              {
2546              rrc = 0;
2547              if (pcre_callout != NULL)
2548                {
2549                pcre_callout_block cb;
2550                cb.version          = 1;   /* Version 1 of the callout block */
2551                cb.callout_number   = code[LINK_SIZE+2];
2552                cb.offset_vector    = offsets;
2553                cb.subject          = (PCRE_SPTR)start_subject;
2554                cb.subject_length   = (int)(end_subject - start_subject);
2555                cb.start_match      = (int)(current_subject - start_subject);
2556                cb.current_position = (int)(ptr - start_subject);
2557                cb.pattern_position = GET(code, LINK_SIZE + 3);
2558                cb.next_item_length = GET(code, 3 + 2*LINK_SIZE);
2559                cb.capture_top      = 1;
2560                cb.capture_last     = -1;
2561                cb.callout_data     = md->callout_data;
2562                if ((rrc = (*pcre_callout)(&cb)) < 0) return rrc;   /* Abandon */
2563                }
2564              if (rrc > 0) break;                      /* Fail this thread */
2565              code += _pcre_OP_lengths[OP_CALLOUT];    /* Skip callout data */
2566              }
2567    
2568            condcode = code[LINK_SIZE+1];
2569    
2570          /* Back reference conditions are not supported */          /* Back reference conditions are not supported */
2571    
2572          if (condcode == OP_CREF) return PCRE_ERROR_DFA_UCOND;          if (condcode == OP_CREF || condcode == OP_NCREF)
2573              return PCRE_ERROR_DFA_UCOND;
2574    
2575          /* The DEFINE condition is always false */          /* The DEFINE condition is always false */
2576    
2577          if (condcode == OP_DEF)          if (condcode == OP_DEF)
2578            {            { ADD_ACTIVE(state_offset + codelink + LINK_SIZE + 1, 0); }
           ADD_ACTIVE(state_offset + GET(code, 1) + LINK_SIZE + 1, 0);  
           }  
2579    
2580          /* The only supported version of OP_RREF is for the value RREF_ANY,          /* The only supported version of OP_RREF is for the value RREF_ANY,
2581          which means "test if in any recursion". We can't test for specifically          which means "test if in any recursion". We can't test for specifically
2582          recursed groups. */          recursed groups. */
2583    
2584          else if (condcode == OP_RREF)          else if (condcode == OP_RREF || condcode == OP_NRREF)
2585            {            {
2586            int value = GET2(code, LINK_SIZE+2);            int value = GET2(code, LINK_SIZE+2);
2587            if (value != RREF_ANY) return PCRE_ERROR_DFA_UCOND;            if (value != RREF_ANY) return PCRE_ERROR_DFA_UCOND;
2588            if (recursing > 0) { ADD_ACTIVE(state_offset + LINK_SIZE + 4, 0); }            if (recursing > 0)
2589              else { ADD_ACTIVE(state_offset + GET(code, 1) + LINK_SIZE + 1, 0); }              { ADD_ACTIVE(state_offset + LINK_SIZE + 4, 0); }
2590              else { ADD_ACTIVE(state_offset + codelink + LINK_SIZE + 1, 0); }
2591            }            }
2592    
2593          /* Otherwise, the condition is an assertion */          /* Otherwise, the condition is an assertion */
# Line 2218  for (;;) Line 2604  for (;;)
2604              md,                                   /* fixed match data */              md,                                   /* fixed match data */
2605              asscode,                              /* this subexpression's code */              asscode,                              /* this subexpression's code */
2606              ptr,                                  /* where we currently are */              ptr,                                  /* where we currently are */
2607              ptr - start_subject,                  /* start offset */              (int)(ptr - start_subject),           /* start offset */
2608              local_offsets,                        /* offset vector */              local_offsets,                        /* offset vector */
2609              sizeof(local_offsets)/sizeof(int),    /* size of same */              sizeof(local_offsets)/sizeof(int),    /* size of same */
2610              local_workspace,                      /* workspace vector */              local_workspace,                      /* workspace vector */
2611              sizeof(local_workspace)/sizeof(int),  /* size of same */              sizeof(local_workspace)/sizeof(int),  /* size of same */
             ims,                                  /* the current ims flags */  
2612              rlevel,                               /* function recursion level */              rlevel,                               /* function recursion level */
2613              recursing);                           /* pass on regex recursion */              recursing);                           /* pass on regex recursion */
2614    
2615              if (rc == PCRE_ERROR_DFA_UITEM) return rc;
2616            if ((rc >= 0) ==            if ((rc >= 0) ==
2617                  (condcode == OP_ASSERT || condcode == OP_ASSERTBACK))                  (condcode == OP_ASSERT || condcode == OP_ASSERTBACK))
2618              { ADD_ACTIVE(endasscode + LINK_SIZE + 1 - start_code, 0); }              { ADD_ACTIVE((int)(endasscode + LINK_SIZE + 1 - start_code), 0); }
2619            else            else
2620              { ADD_ACTIVE(state_offset + GET(code, 1) + LINK_SIZE + 1, 0); }              { ADD_ACTIVE(state_offset + codelink + LINK_SIZE + 1, 0); }
2621            }            }
2622          }          }
2623        break;        break;
# Line 2250  for (;;) Line 2636  for (;;)
2636            md,                                   /* fixed match data */            md,                                   /* fixed match data */
2637            start_code + GET(code, 1),            /* this subexpression's code */            start_code + GET(code, 1),            /* this subexpression's code */
2638            ptr,                                  /* where we currently are */            ptr,                                  /* where we currently are */
2639            ptr - start_subject,                  /* start offset */            (int)(ptr - start_subject),           /* start offset */
2640            local_offsets,                        /* offset vector */            local_offsets,                        /* offset vector */
2641            sizeof(local_offsets)/sizeof(int),    /* size of same */            sizeof(local_offsets)/sizeof(int),    /* size of same */
2642            local_workspace,                      /* workspace vector */            local_workspace,                      /* workspace vector */
2643            sizeof(local_workspace)/sizeof(int),  /* size of same */            sizeof(local_workspace)/sizeof(int),  /* size of same */
           ims,                                  /* the current ims flags */  
2644            rlevel,                               /* function recursion level */            rlevel,                               /* function recursion level */
2645            recursing + 1);                       /* regex recurse level */            recursing + 1);                       /* regex recurse level */
2646    
# Line 2293  for (;;) Line 2678  for (;;)
2678        break;        break;
2679    
2680        /*-----------------------------------------------------------------*/        /*-----------------------------------------------------------------*/
2681          case OP_BRAPOS:
2682          case OP_SBRAPOS:
2683          case OP_CBRAPOS:
2684          case OP_SCBRAPOS:
2685          case OP_BRAPOSZERO:
2686            {
2687            int charcount, matched_count;
2688            const uschar *local_ptr = ptr;
2689            BOOL allow_zero;
2690    
2691            if (codevalue == OP_BRAPOSZERO)
2692              {
2693              allow_zero = TRUE;
2694              codevalue = *(++code);  /* Codevalue will be one of above BRAs */
2695              }
2696            else allow_zero = FALSE;
2697    
2698            /* Loop to match the subpattern as many times as possible as if it were
2699            a complete pattern. */
2700    
2701            for (matched_count = 0;; matched_count++)
2702              {
2703              int local_offsets[2];
2704              int local_workspace[1000];
2705    
2706              int rc = internal_dfa_exec(
2707                md,                                   /* fixed match data */
2708                code,                                 /* this subexpression's code */
2709                local_ptr,                            /* where we currently are */
2710                (int)(ptr - start_subject),           /* start offset */
2711                local_offsets,                        /* offset vector */
2712                sizeof(local_offsets)/sizeof(int),    /* size of same */
2713                local_workspace,                      /* workspace vector */
2714                sizeof(local_workspace)/sizeof(int),  /* size of same */
2715                rlevel,                               /* function recursion level */
2716                recursing);                           /* pass on regex recursion */
2717    
2718              /* Failed to match */
2719    
2720              if (rc < 0)
2721                {
2722                if (rc != PCRE_ERROR_NOMATCH) return rc;
2723                break;
2724                }
2725    
2726              /* Matched: break the loop if zero characters matched. */
2727    
2728              charcount = local_offsets[1] - local_offsets[0];
2729              if (charcount == 0) break;
2730              local_ptr += charcount;    /* Advance temporary position ptr */
2731              }
2732    
2733            /* At this point we have matched the subpattern matched_count
2734            times, and local_ptr is pointing to the character after the end of the
2735            last match. */
2736    
2737            if (matched_count > 0 || allow_zero)
2738              {
2739              const uschar *end_subpattern = code;
2740              int next_state_offset;
2741    
2742              do { end_subpattern += GET(end_subpattern, 1); }
2743                while (*end_subpattern == OP_ALT);
2744              next_state_offset =
2745                (int)(end_subpattern - start_code + LINK_SIZE + 1);
2746    
2747              /* Optimization: if there are no more active states, and there
2748              are no new states yet set up, then skip over the subject string
2749              right here, to save looping. Otherwise, set up the new state to swing
2750              into action when the end of the matched substring is reached. */
2751    
2752              if (i + 1 >= active_count && new_count == 0)
2753                {
2754                ptr = local_ptr;
2755                clen = 0;
2756                ADD_NEW(next_state_offset, 0);
2757                }
2758              else
2759                {
2760                const uschar *p = ptr;
2761                const uschar *pp = local_ptr;
2762                charcount = pp - p;
2763                while (p < pp) if ((*p++ & 0xc0) == 0x80) charcount--;
2764                ADD_NEW_DATA(-next_state_offset, 0, (charcount - 1));
2765                }
2766              }
2767            }
2768          break;
2769    
2770          /*-----------------------------------------------------------------*/
2771        case OP_ONCE:        case OP_ONCE:
2772          {          {
2773          int local_offsets[2];          int local_offsets[2];
# Line 2302  for (;;) Line 2777  for (;;)
2777            md,                                   /* fixed match data */            md,                                   /* fixed match data */
2778            code,                                 /* this subexpression's code */            code,                                 /* this subexpression's code */
2779            ptr,                                  /* where we currently are */            ptr,                                  /* where we currently are */
2780            ptr - start_subject,                  /* start offset */            (int)(ptr - start_subject),           /* start offset */
2781            local_offsets,                        /* offset vector */            local_offsets,                        /* offset vector */
2782            sizeof(local_offsets)/sizeof(int),    /* size of same */            sizeof(local_offsets)/sizeof(int),    /* size of same */
2783            local_workspace,                      /* workspace vector */            local_workspace,                      /* workspace vector */
2784            sizeof(local_workspace)/sizeof(int),  /* size of same */            sizeof(local_workspace)/sizeof(int),  /* size of same */
           ims,                                  /* the current ims flags */  
2785            rlevel,                               /* function recursion level */            rlevel,                               /* function recursion level */
2786            recursing);                           /* pass on regex recursion */            recursing);                           /* pass on regex recursion */
2787    
# Line 2319  for (;;) Line 2793  for (;;)
2793    
2794            do { end_subpattern += GET(end_subpattern, 1); }            do { end_subpattern += GET(end_subpattern, 1); }
2795              while (*end_subpattern == OP_ALT);              while (*end_subpattern == OP_ALT);
2796            next_state_offset = end_subpattern - start_code + LINK_SIZE + 1;            next_state_offset =
2797                (int)(end_subpattern - start_code + LINK_SIZE + 1);
2798    
2799            /* If the end of this subpattern is KETRMAX or KETRMIN, we must            /* If the end of this subpattern is KETRMAX or KETRMIN, we must
2800            arrange for the repeat state also to be added to the relevant list.            arrange for the repeat state also to be added to the relevant list.
# Line 2327  for (;;) Line 2802  for (;;)
2802    
2803            repeat_state_offset = (*end_subpattern == OP_KETRMAX ||            repeat_state_offset = (*end_subpattern == OP_KETRMAX ||
2804                                   *end_subpattern == OP_KETRMIN)?                                   *end_subpattern == OP_KETRMIN)?
2805              end_subpattern - start_code - GET(end_subpattern, 1) : -1;              (int)(end_subpattern - start_code - GET(end_subpattern, 1)) : -1;
2806    
2807            /* If we have matched an empty string, add the next state at the            /* If we have matched an empty string, add the next state at the
2808            current character pointer. This is important so that the duplicate            current character pointer. This is important so that the duplicate
# Line 2342  for (;;) Line 2817  for (;;)
2817            /* Optimization: if there are no more active states, and there            /* Optimization: if there are no more active states, and there
2818            are no new states yet set up, then skip over the subject string            are no new states yet set up, then skip over the subject string
2819            right here, to save looping. Otherwise, set up the new state to swing            right here, to save looping. Otherwise, set up the new state to swing
2820            into action when the end of the substring is reached. */            into action when the end of the matched substring is reached. */
2821    
2822            else if (i + 1 >= active_count && new_count == 0)            else if (i + 1 >= active_count && new_count == 0)
2823              {              {
# Line 2372  for (;;) Line 2847  for (;;)
2847              if (repeat_state_offset >= 0)              if (repeat_state_offset >= 0)
2848                { ADD_NEW_DATA(-repeat_state_offset, 0, (charcount - 1)); }                { ADD_NEW_DATA(-repeat_state_offset, 0, (charcount - 1)); }
2849              }              }
   
2850            }            }
2851          else if (rc != PCRE_ERROR_NOMATCH) return rc;          else if (rc != PCRE_ERROR_NOMATCH) return rc;
2852          }          }
# Line 2383  for (;;) Line 2857  for (;;)
2857        /* Handle callouts */        /* Handle callouts */
2858    
2859        case OP_CALLOUT:        case OP_CALLOUT:
2860          rrc = 0;
2861        if (pcre_callout != NULL)        if (pcre_callout != NULL)
2862          {          {
         int rrc;  
2863          pcre_callout_block cb;          pcre_callout_block cb;
2864          cb.version          = 1;   /* Version 1 of the callout block */          cb.version          = 1;   /* Version 1 of the callout block */
2865          cb.callout_number   = code[1];          cb.callout_number   = code[1];
2866          cb.offset_vector    = offsets;          cb.offset_vector    = offsets;
2867          cb.subject          = (PCRE_SPTR)start_subject;          cb.subject          = (PCRE_SPTR)start_subject;
2868          cb.subject_length   = end_subject - start_subject;          cb.subject_length   = (int)(end_subject - start_subject);
2869          cb.start_match      = current_subject - start_subject;          cb.start_match      = (int)(current_subject - start_subject);
2870          cb.current_position = ptr - start_subject;          cb.current_position = (int)(ptr - start_subject);
2871          cb.pattern_position = GET(code, 2);          cb.pattern_position = GET(code, 2);
2872          cb.next_item_length = GET(code, 2 + LINK_SIZE);          cb.next_item_length = GET(code, 2 + LINK_SIZE);
2873          cb.capture_top      = 1;          cb.capture_top      = 1;
2874          cb.capture_last     = -1;          cb.capture_last     = -1;
2875          cb.callout_data     = md->callout_data;          cb.callout_data     = md->callout_data;
2876          if ((rrc = (*pcre_callout)(&cb)) < 0) return rrc;   /* Abandon */          if ((rrc = (*pcre_callout)(&cb)) < 0) return rrc;   /* Abandon */
         if (rrc == 0) { ADD_ACTIVE(state_offset + 2 + 2*LINK_SIZE, 0); }  
2877          }          }
2878          if (rrc == 0)
2879            { ADD_ACTIVE(state_offset + _pcre_OP_lengths[OP_CALLOUT], 0); }
2880        break;        break;
2881    
2882    
# Line 2417  for (;;) Line 2892  for (;;)
2892    /* We have finished the processing at the current subject character. If no    /* We have finished the processing at the current subject character. If no
2893    new states have been set for the next character, we have found all the    new states have been set for the next character, we have found all the
2894    matches that we are going to find. If we are at the top level and partial    matches that we are going to find. If we are at the top level and partial
2895    matching has been requested, check for appropriate conditions. */    matching has been requested, check for appropriate conditions.
2896    
2897      The "forced_ fail" variable counts the number of (*F) encountered for the
2898      character. If it is equal to the original active_count (saved in
2899      workspace[1]) it means that (*F) was found on every active state. In this
2900      case we don't want to give a partial match.
2901    
2902      The "could_continue" variable is true if a state could have continued but
2903      for the fact that the end of the subject was reached. */
2904    
2905    if (new_count <= 0)    if (new_count <= 0)
2906      {      {
2907      if (match_count < 0 &&                     /* No matches found */      if (rlevel == 1 &&                               /* Top level, and */
2908          rlevel == 1 &&                         /* Top level match function */          could_continue &&                            /* Some could go on */
2909          (md->moptions & PCRE_PARTIAL) != 0 &&  /* Want partial matching */          forced_fail != workspace[1] &&               /* Not all forced fail & */
2910            (                                            /* either... */
2911            (md->moptions & PCRE_PARTIAL_HARD) != 0      /* Hard partial */
2912            ||                                           /* or... */
2913            ((md->moptions & PCRE_PARTIAL_SOFT) != 0 &&  /* Soft partial and */
2914             match_count < 0)                            /* no matches */
2915            ) &&                                         /* And... */
2916          ptr >= end_subject &&                  /* Reached end of subject */          ptr >= end_subject &&                  /* Reached end of subject */
2917          ptr > current_subject)                 /* Matched non-empty string */          ptr > md->start_used_ptr)              /* Inspected non-empty string */
2918        {        {
2919        if (offsetcount >= 2)        if (offsetcount >= 2)
2920          {          {
2921          offsets[0] = current_subject - start_subject;          offsets[0] = (int)(md->start_used_ptr - start_subject);
2922          offsets[1] = end_subject - start_subject;          offsets[1] = (int)(end_subject - start_subject);
2923          }          }
2924        match_count = PCRE_ERROR_PARTIAL;        match_count = PCRE_ERROR_PARTIAL;
2925        }        }
# Line 2484  Returns: > 0 => number of match Line 2973  Returns: > 0 => number of match
2973                   < -1 => some kind of unexpected problem                   < -1 => some kind of unexpected problem
2974  */  */
2975    
2976  PCRE_EXP_DEFN int  PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
2977  pcre_dfa_exec(const pcre *argument_re, const pcre_extra *extra_data,  pcre_dfa_exec(const pcre *argument_re, const pcre_extra *extra_data,
2978    const char *subject, int length, int start_offset, int options, int *offsets,    const char *subject, int length, int start_offset, int options, int *offsets,
2979    int offsetcount, int *workspace, int wscount)    int offsetcount, int *workspace, int wscount)
# Line 2515  if (re == NULL || subject == NULL || wor Line 3004  if (re == NULL || subject == NULL || wor
3004     (offsets == NULL && offsetcount > 0)) return PCRE_ERROR_NULL;     (offsets == NULL && offsetcount > 0)) return PCRE_ERROR_NULL;
3005  if (offsetcount < 0) return PCRE_ERROR_BADCOUNT;  if (offsetcount < 0) return PCRE_ERROR_BADCOUNT;
3006  if (wscount < 20) return PCRE_ERROR_DFA_WSSIZE;  if (wscount < 20) return PCRE_ERROR_DFA_WSSIZE;
3007    if (start_offset < 0 || start_offset > length) return PCRE_ERROR_BADOFFSET;
3008    
3009  /* We need to find the pointer to any study data before we test for byte  /* We need to find the pointer to any study data before we test for byte
3010  flipping, so we scan the extra_data block first. This may set two fields in the  flipping, so we scan the extra_data block first. This may set two fields in the
# Line 2571  md->start_code = (const uschar *)argumen Line 3061  md->start_code = (const uschar *)argumen
3061      re->name_table_offset + re->name_count * re->name_entry_size;      re->name_table_offset + re->name_count * re->name_entry_size;
3062  md->start_subject = (const unsigned char *)subject;  md->start_subject = (const unsigned char *)subject;
3063  md->end_subject = end_subject;  md->end_subject = end_subject;
3064    md->start_offset = start_offset;
3065  md->moptions = options;  md->moptions = options;
3066  md->poptions = re->options;  md->poptions = re->options;
3067    
3068    /* If the BSR option is not set at match time, copy what was set
3069    at compile time. */
3070    
3071    if ((md->moptions & (PCRE_BSR_ANYCRLF|PCRE_BSR_UNICODE)) == 0)
3072      {
3073      if ((re->options & (PCRE_BSR_ANYCRLF|PCRE_BSR_UNICODE)) != 0)
3074        md->moptions |= re->options & (PCRE_BSR_ANYCRLF|PCRE_BSR_UNICODE);
3075    #ifdef BSR_ANYCRLF
3076      else md->moptions |= PCRE_BSR_ANYCRLF;
3077    #endif
3078      }
3079    
3080  /* Handle different types of newline. The three bits give eight cases. If  /* Handle different types of newline. The three bits give eight cases. If
3081  nothing is set at run time, whatever was used at compile time applies. */  nothing is set at run time, whatever was used at compile time applies. */
3082    
# Line 2581  switch ((((options & PCRE_NEWLINE_BITS) Line 3084  switch ((((options & PCRE_NEWLINE_BITS)
3084           PCRE_NEWLINE_BITS)           PCRE_NEWLINE_BITS)
3085    {    {
3086    case 0: newline = NEWLINE; break;   /* Compile-time default */    case 0: newline = NEWLINE; break;   /* Compile-time default */
3087    case PCRE_NEWLINE_CR: newline = '\r'; break;    case PCRE_NEWLINE_CR: newline = CHAR_CR; break;
3088    case PCRE_NEWLINE_LF: newline = '\n'; break;    case PCRE_NEWLINE_LF: newline = CHAR_NL; break;
3089    case PCRE_NEWLINE_CR+    case PCRE_NEWLINE_CR+
3090         PCRE_NEWLINE_LF: newline = ('\r' << 8) | '\n'; break;         PCRE_NEWLINE_LF: newline = (CHAR_CR << 8) | CHAR_NL; break;
3091    case PCRE_NEWLINE_ANY: newline = -1; break;    case PCRE_NEWLINE_ANY: newline = -1; break;
3092    case PCRE_NEWLINE_ANYCRLF: newline = -2; break;    case PCRE_NEWLINE_ANYCRLF: newline = -2; break;
3093    default: return PCRE_ERROR_BADNEWLINE;    default: return PCRE_ERROR_BADNEWLINE;
# Line 2620  back the character offset. */ Line 3123  back the character offset. */
3123  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
3124  if (utf8 && (options & PCRE_NO_UTF8_CHECK) == 0)  if (utf8 && (options & PCRE_NO_UTF8_CHECK) == 0)
3125    {    {
3126    if (_pcre_valid_utf8((uschar *)subject, length) >= 0)    int erroroffset;
3127      return PCRE_ERROR_BADUTF8;    int errorcode = _pcre_valid_utf8((uschar *)subject, length, &erroroffset);
3128    if (start_offset > 0 && start_offset < length)    if (errorcode != 0)
3129      {      {
3130      int tb = ((uschar *)subject)[start_offset];      if (offsetcount >= 2)
     if (tb > 127)  
3131        {        {
3132        tb &= 0xc0;        offsets[0] = erroroffset;
3133        if (tb != 0 && tb != 0xc0) return PCRE_ERROR_BADUTF8_OFFSET;        offsets[1] = errorcode;
3134        }        }
3135      }      return (errorcode <= PCRE_UTF8_ERR5 && (options & PCRE_PARTIAL_HARD) != 0)?
3136          PCRE_ERROR_SHORTUTF8 : PCRE_ERROR_BADUTF8;
3137        }
3138      if (start_offset > 0 && start_offset < length &&
3139            (((USPTR)subject)[start_offset] & 0xc0) == 0x80)
3140        return PCRE_ERROR_BADUTF8_OFFSET;
3141    }    }
3142  #endif  #endif
3143    
# Line 2644  if (md->tables == NULL) md->tables = _pc Line 3151  if (md->tables == NULL) md->tables = _pc
3151  used in a loop when finding where to start. */  used in a loop when finding where to start. */
3152    
3153  lcc = md->tables + lcc_offset;  lcc = md->tables + lcc_offset;
3154  startline = (re->options & PCRE_STARTLINE) != 0;  startline = (re->flags & PCRE_STARTLINE) != 0;
3155  firstline = (re->options & PCRE_FIRSTLINE) != 0;  firstline = (re->options & PCRE_FIRSTLINE) != 0;
3156    
3157  /* Set up the first character to match, if available. The first_byte value is  /* Set up the first character to match, if available. The first_byte value is
# Line 2655  studied, there may be a bitmap of possib Line 3162  studied, there may be a bitmap of possib
3162    
3163  if (!anchored)  if (!anchored)
3164    {    {
3165    if ((re->options & PCRE_FIRSTSET) != 0)    if ((re->flags & PCRE_FIRSTSET) != 0)
3166      {      {
3167      first_byte = re->first_byte & 255;      first_byte = re->first_byte & 255;
3168      if ((first_byte_caseless = ((re->first_byte & REQ_CASELESS) != 0)) == TRUE)      if ((first_byte_caseless = ((re->first_byte & REQ_CASELESS) != 0)) == TRUE)
# Line 2663  if (!anchored) Line 3170  if (!anchored)
3170      }      }
3171    else    else
3172      {      {
3173      if (startline && study != NULL &&      if (!startline && study != NULL &&
3174           (study->options & PCRE_STUDY_MAPPED) != 0)           (study->flags & PCRE_STUDY_MAPPED) != 0)
3175        start_bits = study->start_bits;        start_bits = study->start_bits;
3176      }      }
3177    }    }
# Line 2672  if (!anchored) Line 3179  if (!anchored)
3179  /* For anchored or unanchored matches, there may be a "last known required  /* For anchored or unanchored matches, there may be a "last known required
3180  character" set. */  character" set. */
3181    
3182  if ((re->options & PCRE_REQCHSET) != 0)  if ((re->flags & PCRE_REQCHSET) != 0)
3183    {    {
3184    req_byte = re->req_byte & 255;    req_byte = re->req_byte & 255;
3185    req_byte_caseless = (re->req_byte & REQ_CASELESS) != 0;    req_byte_caseless = (re->req_byte & REQ_CASELESS) != 0;
# Line 2680  if ((re->options & PCRE_REQCHSET) != 0) Line 3187  if ((re->options & PCRE_REQCHSET) != 0)
3187    }    }
3188    
3189  /* Call the main matching function, looping for a non-anchored regex after a  /* Call the main matching function, looping for a non-anchored regex after a
3190  failed match. Unless restarting, optimize by moving to the first match  failed match. If not restarting, perform certain optimizations at the start of
3191  character if possible, when not anchored. Then unless wanting a partial match,  a match. */
 check for a required later character. */  
3192    
3193  for (;;)  for (;;)
3194    {    {
# Line 2692  for (;;) Line 3198  for (;;)
3198      {      {
3199      const uschar *save_end_subject = end_subject;      const uschar *save_end_subject = end_subject;
3200    
3201      /* Advance to a unique first char if possible. If firstline is TRUE, the      /* If firstline is TRUE, the start of the match is constrained to the first
3202      start of the match is constrained to the first line of a multiline string.      line of a multiline string. Implement this by temporarily adjusting
3203      Implement this by temporarily adjusting end_subject so that we stop      end_subject so that we stop scanning at a newline. If the match fails at
3204      scanning at a newline. If the match fails at the newline, later code breaks      the newline, later code breaks this loop. */
     this loop. */  
3205    
3206      if (firstline)      if (firstline)
3207        {        {
3208        const uschar *t = current_subject;        USPTR t = current_subject;
3209    #ifdef SUPPORT_UTF8
3210          if (utf8)
3211            {
3212            while (t < md->end_subject && !IS_NEWLINE(t))
3213              {
3214              t++;
3215              while (t < end_subject && (*t & 0xc0) == 0x80) t++;
3216              }
3217            }
3218          else
3219    #endif
3220        while (t < md->end_subject && !IS_NEWLINE(t)) t++;        while (t < md->end_subject && !IS_NEWLINE(t)) t++;
3221        end_subject = t;        end_subject = t;
3222        }        }
3223    
3224      if (first_byte >= 0)      /* There are some optimizations that avoid running the match if a known
3225        starting point is not found. However, there is an option that disables
3226        these, for testing and for ensuring that all callouts do actually occur.
3227        The option can be set in the regex by (*NO_START_OPT) or passed in
3228        match-time options. */
3229    
3230        if (((options | re->options) & PCRE_NO_START_OPTIMIZE) == 0)
3231        {        {
3232        if (first_byte_caseless)        /* Advance to a known first byte. */
3233          while (current_subject < end_subject &&  
3234                 lcc[*current_subject] != first_byte)        if (first_byte >= 0)
3235            current_subject++;          {
3236        else          if (first_byte_caseless)
3237          while (current_subject < end_subject && *current_subject != first_byte)            while (current_subject < end_subject &&
3238            current_subject++;                   lcc[*current_subject] != first_byte)
3239        }              current_subject++;
3240            else
3241              while (current_subject < end_subject &&
3242                     *current_subject != first_byte)
3243                current_subject++;
3244            }
3245    
3246      /* Or to just after a linebreak for a multiline match if possible */        /* Or to just after a linebreak for a multiline match if possible */
3247    
3248      else if (startline)        else if (startline)
       {  
       if (current_subject > md->start_subject + start_offset)  
3249          {          {
3250          while (current_subject <= end_subject && !WAS_NEWLINE(current_subject))          if (current_subject > md->start_subject + start_offset)
3251            current_subject++;            {
3252    #ifdef SUPPORT_UTF8
3253              if (utf8)
3254                {
3255                while (current_subject < end_subject &&
3256                       !WAS_NEWLINE(current_subject))
3257                  {
3258                  current_subject++;
3259                  while(current_subject < end_subject &&
3260                        (*current_subject & 0xc0) == 0x80)
3261                    current_subject++;
3262                  }
3263                }
3264              else
3265    #endif
3266              while (current_subject < end_subject && !WAS_NEWLINE(current_subject))
3267                current_subject++;
3268    
3269              /* If we have just passed a CR and the newline option is ANY or
3270              ANYCRLF, and we are now at a LF, advance the match position by one
3271              more character. */
3272    
3273          /* If we have just passed a CR and the newline option is ANY or            if (current_subject[-1] == CHAR_CR &&
3274          ANYCRLF, and we are now at a LF, advance the match position by one more                 (md->nltype == NLTYPE_ANY || md->nltype == NLTYPE_ANYCRLF) &&
3275          character. */                 current_subject < end_subject &&
3276                   *current_subject == CHAR_NL)
3277          if (current_subject[-1] == '\r' &&              current_subject++;
3278               (md->nltype == NLTYPE_ANY || md->nltype == NLTYPE_ANYCRLF) &&            }
              current_subject < end_subject &&  
              *current_subject == '\n')  
           current_subject++;  
3279          }          }
       }  
3280    
3281      /* Or to a non-unique first char after study */        /* Or to a non-unique first char after study */
3282    
3283      else if (start_bits != NULL)        else if (start_bits != NULL)
       {  
       while (current_subject < end_subject)  
3284          {          {
3285          register unsigned int c = *current_subject;          while (current_subject < end_subject)
3286          if ((start_bits[c/8] & (1 << (c&7))) == 0) current_subject++;            {
3287              register unsigned int c = *current_subject;
3288              if ((start_bits[c/8] & (1 << (c&7))) == 0)
3289                {
3290                current_subject++;
3291    #ifdef SUPPORT_UTF8
3292                if (utf8)
3293                  while(current_subject < end_subject &&
3294                        (*current_subject & 0xc0) == 0x80) current_subject++;
3295    #endif
3296                }
3297            else break;            else break;
3298              }
3299          }          }
3300        }        }
3301    
3302      /* Restore fudged end_subject */      /* Restore fudged end_subject */
3303    
3304      end_subject = save_end_subject;      end_subject = save_end_subject;
     }  
   
   /* If req_byte is set, we know that that character must appear in the subject  
   for the match to succeed. If the first character is set, req_byte must be  
   later in the subject; otherwise the test starts at the match point. This  
   optimization can save a huge amount of work in patterns with nested unlimited  
   repeats that aren't going to match. Writing separate code for cased/caseless  
   versions makes it go faster, as does using an autoincrement and backing off  
   on a match.  
   
   HOWEVER: when the subject string is very, very long, searching to its end can  
   take a long time, and give bad performance on quite ordinary patterns. This  
   showed up when somebody was matching /^C/ on a 32-megabyte string... so we  
   don't do this when the string is sufficiently long.  
   
   ALSO: this processing is disabled when partial matching is requested.  
   */  
   
   if (req_byte >= 0 &&  
       end_subject - current_subject < REQ_BYTE_MAX &&  
       (options & PCRE_PARTIAL) == 0)  
     {  
     register const uschar *p = current_subject + ((first_byte >= 0)? 1 : 0);  
3305    
3306      /* We don't need to repeat the search if we haven't yet reached the      /* The following two optimizations are disabled for partial matching or if
3307      place we found it at last time. */      disabling is explicitly requested (and of course, by the test above, this
3308        code is not obeyed when restarting after a partial match). */
3309    
3310      if (p > req_byte_ptr)      if ((options & PCRE_NO_START_OPTIMIZE) == 0 &&
3311            (options & (PCRE_PARTIAL_HARD|PCRE_PARTIAL_SOFT)) == 0)
3312        {        {
3313        if (req_byte_caseless)        /* If the pattern was studied, a minimum subject length may be set. This
3314          {        is a lower bound; no actual string of that length may actually match the
3315          while (p < end_subject)        pattern. Although the value is, strictly, in characters, we treat it as
3316            {        bytes to avoid spending too much time in this optimization. */
3317            register int pp = *p++;  
3318            if (pp == req_byte || pp == req_byte2) { p--; break; }        if (study != NULL && (study->flags & PCRE_STUDY_MINLEN) != 0 &&
3319            }            (pcre_uint32)(end_subject - current_subject) < study->minlength)
3320          }          return PCRE_ERROR_NOMATCH;
3321        else  
3322          /* If req_byte is set, we know that that character must appear in the
3323          subject for the match to succeed. If the first character is set, req_byte
3324          must be later in the subject; otherwise the test starts at the match
3325          point. This optimization can save a huge amount of work in patterns with
3326          nested unlimited repeats that aren't going to match. Writing separate
3327          code for cased/caseless versions makes it go faster, as does using an
3328          autoincrement and backing off on a match.
3329    
3330          HOWEVER: when the subject string is very, very long, searching to its end
3331          can take a long time, and give bad performance on quite ordinary
3332          patterns. This showed up when somebody was matching /^C/ on a 32-megabyte
3333          string... so we don't do this when the string is sufficiently long. */
3334    
3335          if (req_byte >= 0 && end_subject - current_subject < REQ_BYTE_MAX)
3336          {          {
3337          while (p < end_subject)          register const uschar *p = current_subject + ((first_byte >= 0)? 1 : 0);
3338    
3339            /* We don't need to repeat the search if we haven't yet reached the
3340            place we found it at last time. */
3341    
3342            if (p > req_byte_ptr)
3343            {            {
3344            if (*p++ == req_byte) { p--; break; }            if (req_byte_caseless)
3345            }              {
3346          }              while (p < end_subject)
3347                  {
3348                  register int pp = *p++;
3349                  if (pp == req_byte || pp == req_byte2) { p--; break; }
3350                  }
3351                }
3352              else
3353                {
3354                while (p < end_subject)
3355                  {
3356                  if (*p++ == req_byte) { p--; break; }
3357                  }
3358                }
3359    
3360        /* If we can't find the required character, break the matching loop,            /* If we can't find the required character, break the matching loop,
3361        which will cause a return or PCRE_ERROR_NOMATCH. */            which will cause a return or PCRE_ERROR_NOMATCH. */
3362    
3363        if (p >= end_subject) break;            if (p >= end_subject) break;
3364    
3365        /* If we have found the required character, save the point where we            /* If we have found the required character, save the point where we
3366        found it, so that we don't search again next time round the loop if            found it, so that we don't search again next time round the loop if
3367        the start hasn't passed this character yet. */            the start hasn't passed this character yet. */
3368    
3369        req_byte_ptr = p;            req_byte_ptr = p;
3370              }
3371            }
3372        }        }
3373      }      }   /* End of optimizations that are done when not restarting */
3374    
3375    /* OK, now we can do the business */    /* OK, now we can do the business */
3376    
3377      md->start_used_ptr = current_subject;
3378    
3379    rc = internal_dfa_exec(    rc = internal_dfa_exec(
3380      md,                                /* fixed match data */      md,                                /* fixed match data */
3381      md->start_code,                    /* this subexpression's code */      md->start_code,                    /* this subexpression's code */
# Line 2821  for (;;) Line 3385  for (;;)
3385      offsetcount,                       /* size of same */      offsetcount,                       /* size of same */
3386      workspace,                         /* workspace vector */      workspace,                         /* workspace vector */
3387      wscount,                           /* size of same */      wscount,                           /* size of same */
     re->options & (PCRE_CASELESS|PCRE_MULTILINE|PCRE_DOTALL), /* ims flags */  
3388      0,                                 /* function recurse level */      0,                                 /* function recurse level */
3389      0);                                /* regex recurse level */      0);                                /* regex recurse level */
3390    
# Line 2842  for (;;) Line 3405  for (;;)
3405      }      }
3406    if (current_subject > end_subject) break;    if (current_subject > end_subject) break;
3407    
3408    /* If we have just passed a CR and we are now at a LF, and the pattern does    /* If we have just passed a CR and we are now at a LF, and the pattern does
3409    not contain any explicit matches for \r or \n, and the newline option is CRLF    not contain any explicit matches for \r or \n, and the newline option is CRLF
3410    or ANY or ANYCRLF, advance the match position by one more character. */    or ANY or ANYCRLF, advance the match position by one more character. */
3411    
3412    if (current_subject[-1] == '\r' &&    if (current_subject[-1] == CHAR_CR &&
3413        current_subject < end_subject &&        current_subject < end_subject &&
3414        *current_subject == '\n' &&        *current_subject == CHAR_NL &&
3415        (re->options & PCRE_HASCRORLF) == 0 &&        (re->flags & PCRE_HASCRORLF) == 0 &&
3416          (md->nltype == NLTYPE_ANY ||          (md->nltype == NLTYPE_ANY ||
3417           md->nltype == NLTYPE_ANYCRLF ||           md->nltype == NLTYPE_ANYCRLF ||
3418           md->nllen == 2))           md->nllen == 2))

Legend:
Removed from v.226  
changed lines
  Added in v.606

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12