/[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 442 by ph10, Fri Sep 11 10:21:02 2009 UTC revision 642 by ph10, Thu Jul 28 18:59:40 2011 UTC
# Line 7  and semantics are as close as possible t Line 7  and semantics are as close as possible t
7  below for why this module is different).  below for why this module is different).
8    
9                         Written by Philip Hazel                         Written by Philip Hazel
10             Copyright (c) 1997-2009 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 45  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  /* 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  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  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.  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  The issue is the check for duplicate states, which is done by a simple linear
# Line 68  was the extra time to initialize the ind Line 68  was the extra time to initialize the ind
68  of internal_dfa_exec(). (The supplied patch used a static vector, initialized  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.)  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  Overall, I concluded that the gains in some cases did not outweigh the losses
72  in others, so I abandoned this code. */  in others, so I abandoned this code. */
73    
74    
# Line 106  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 const 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, 0,                       /* Any, AllAny, 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 142  static const uschar coptable[] = { Line 151  static const 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,                             /* Reverse                                */
163    0,                             /* Assert                                 */    0,                             /* Assert                                 */
164    0,                             /* Assert not                             */    0,                             /* Assert not                             */
165    0,                             /* Assert behind                          */    0,                             /* Assert behind                          */
166    0,                             /* Assert behind not                      */    0,                             /* Assert behind not                      */
167      0, 0, 0, 0, 0, 0,              /* ONCE, BRA, BRAPOS, CBRA, CBRAPOS, COND */
168      0, 0, 0, 0, 0,                 /* SBRA, SBRAPOS, SCBRA, SCBRAPOS, SCOND  */
169      0, 0,                          /* CREF, NCREF                            */
170      0, 0,                          /* RREF, NRREF                            */
171      0,                             /* DEF                                    */
172      0, 0, 0,                       /* BRAZERO, BRAMINZERO, BRAPOSZERO        */
173      0, 0, 0,                       /* MARK, PRUNE, PRUNE_ARG                 */
174      0, 0, 0, 0,                    /* SKIP, SKIP_ARG, THEN, THEN_ARG         */
175      0, 0, 0, 0,                    /* COMMIT, FAIL, ACCEPT, ASSERT_ACCEPT    */
176      0, 0                           /* CLOSE, SKIPZERO  */
177    };
178    
179    /* This table identifies those opcodes that inspect a character. It is used to
180    remember the fact that a character could have been inspected when the end of
181    the subject is reached. ***NOTE*** If the start of this table is modified, the
182    two tables that follow must also be modified. */
183    
184    static const uschar poptable[] = {
185      0,                             /* End                                    */
186      0, 0, 0, 1, 1,                 /* \A, \G, \K, \B, \b                     */
187      1, 1, 1, 1, 1, 1,              /* \D, \d, \S, \s, \W, \w                 */
188      1, 1, 1,                       /* Any, AllAny, Anybyte                   */
189      1, 1,                          /* \P, \p                                 */
190      1, 1, 1, 1, 1,                 /* \R, \H, \h, \V, \v                     */
191      1,                             /* \X                                     */
192      0, 0, 0, 0, 0, 0,              /* \Z, \z, ^, ^M, $, $M                   */
193      1,                             /* Char                                   */
194      1,                             /* Chari                                  */
195      1,                             /* not                                    */
196      1,                             /* noti                                   */
197      /* Positive single-char repeats                                          */
198      1, 1, 1, 1, 1, 1,              /* *, *?, +, +?, ?, ??                    */
199      1, 1, 1,                       /* upto, minupto, exact                   */
200      1, 1, 1, 1,                    /* *+, ++, ?+, upto+                      */
201      1, 1, 1, 1, 1, 1,              /* *I, *?I, +I, +?I, ?I, ??I              */
202      1, 1, 1,                       /* upto I, minupto I, exact I             */
203      1, 1, 1, 1,                    /* *+I, ++I, ?+I, upto+I                  */
204      /* Negative single-char repeats - only for chars < 256                   */
205      1, 1, 1, 1, 1, 1,              /* NOT *, *?, +, +?, ?, ??                */
206      1, 1, 1,                       /* NOT upto, minupto, exact               */
207      1, 1, 1, 1,                    /* NOT *+, ++, ?+, upto+                  */
208      1, 1, 1, 1, 1, 1,              /* NOT *I, *?I, +I, +?I, ?I, ??I          */
209      1, 1, 1,                       /* NOT upto I, minupto I, exact I         */
210      1, 1, 1, 1,                    /* NOT *+I, ++I, ?+I, upto+I              */
211      /* Positive type repeats                                                 */
212      1, 1, 1, 1, 1, 1,              /* Type *, *?, +, +?, ?, ??               */
213      1, 1, 1,                       /* Type upto, minupto, exact              */
214      1, 1, 1, 1,                    /* Type *+, ++, ?+, upto+                 */
215      /* Character class & ref repeats                                         */
216      1, 1, 1, 1, 1, 1,              /* *, *?, +, +?, ?, ??                    */
217      1, 1,                          /* CRRANGE, CRMINRANGE                    */
218      1,                             /* CLASS                                  */
219      1,                             /* NCLASS                                 */
220      1,                             /* XCLASS - variable length               */
221      0,                             /* REF                                    */
222      0,                             /* REFI                                   */
223      0,                             /* RECURSE                                */
224      0,                             /* CALLOUT                                */
225      0,                             /* Alt                                    */
226      0,                             /* Ket                                    */
227      0,                             /* KetRmax                                */
228      0,                             /* KetRmin                                */
229      0,                             /* KetRpos                                */
230    0,                             /* Reverse                                */    0,                             /* Reverse                                */
231    0, 0, 0, 0,                    /* ONCE, BRA, CBRA, COND                  */    0,                             /* Assert                                 */
232    0, 0, 0,                       /* SBRA, SCBRA, SCOND                     */    0,                             /* Assert not                             */
233    0,                             /* CREF                                   */    0,                             /* Assert behind                          */
234    0,                             /* RREF                                   */    0,                             /* Assert behind not                      */
235      0, 0, 0, 0, 0, 0,              /* ONCE, BRA, BRAPOS, CBRA, CBRAPOS, COND */
236      0, 0, 0, 0, 0,                 /* SBRA, SBRAPOS, SCBRA, SCBRAPOS, SCOND  */
237      0, 0,                          /* CREF, NCREF                            */
238      0, 0,                          /* RREF, NRREF                            */
239    0,                             /* DEF                                    */    0,                             /* DEF                                    */
240    0, 0,                          /* BRAZERO, BRAMINZERO                    */    0, 0, 0,                       /* BRAZERO, BRAMINZERO, BRAPOSZERO        */
241    0, 0, 0, 0,                    /* PRUNE, SKIP, THEN, COMMIT              */    0, 0, 0,                       /* MARK, PRUNE, PRUNE_ARG                 */
242    0, 0, 0                        /* FAIL, ACCEPT, SKIPZERO                 */    0, 0, 0, 0,                    /* SKIP, SKIP_ARG, THEN, THEN_ARG         */
243      0, 0, 0, 0,                    /* COMMIT, FAIL, ACCEPT, ASSERT_ACCEPT    */
244      0, 0                           /* CLOSE, SKIPZERO                        */
245  };  };
246    
247  /* 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,
# Line 191  these structures in, is a vector of ints Line 272  these structures in, is a vector of ints
272  typedef struct stateblock {  typedef struct stateblock {
273    int offset;                     /* Offset to opcode */    int offset;                     /* Offset to opcode */
274    int count;                      /* Count for repeats */    int count;                      /* Count for repeats */
   int ims;                        /* ims flag bits */  
275    int data;                       /* Some use extra data */    int data;                       /* Some use extra data */
276  } stateblock;  } stateblock;
277    
278  #define INTS_PER_STATEBLOCK  (sizeof(stateblock)/sizeof(int))  #define INTS_PER_STATEBLOCK  (sizeof(stateblock)/sizeof(int))
279    
280    
281  #ifdef DEBUG  #ifdef PCRE_DEBUG
282  /*************************************************  /*************************************************
283  *             Print character string             *  *             Print character string             *
284  *************************************************/  *************************************************/
# Line 247  Arguments: Line 327  Arguments:
327    offsetcount       size of same    offsetcount       size of same
328    workspace         vector of workspace    workspace         vector of workspace
329    wscount           size of same    wscount           size of same
   ims               the current ims flags  
330    rlevel            function call recursion level    rlevel            function call recursion level
   recursing         regex recursive call level  
331    
332  Returns:            > 0 => number of match offset pairs placed in offsets  Returns:            > 0 => number of match offset pairs placed in offsets
333                      = 0 => offsets overflowed; longest matches are present                      = 0 => offsets overflowed; longest matches are present
# Line 264  for the current character, one for the f Line 342  for the current character, one for the f
342      { \      { \
343      next_active_state->offset = (x); \      next_active_state->offset = (x); \
344      next_active_state->count  = (y); \      next_active_state->count  = (y); \
     next_active_state->ims    = ims; \  
345      next_active_state++; \      next_active_state++; \
346      DPRINTF(("%.*sADD_ACTIVE(%d,%d)\n", rlevel*2-2, SP, (x), (y))); \      DPRINTF(("%.*sADD_ACTIVE(%d,%d)\n", rlevel*2-2, SP, (x), (y))); \
347      } \      } \
# Line 275  for the current character, one for the f Line 352  for the current character, one for the f
352      { \      { \
353      next_active_state->offset = (x); \      next_active_state->offset = (x); \
354      next_active_state->count  = (y); \      next_active_state->count  = (y); \
     next_active_state->ims    = ims; \  
355      next_active_state->data   = (z); \      next_active_state->data   = (z); \
356      next_active_state++; \      next_active_state++; \
357      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 287  for the current character, one for the f Line 363  for the current character, one for the f
363      { \      { \
364      next_new_state->offset = (x); \      next_new_state->offset = (x); \
365      next_new_state->count  = (y); \      next_new_state->count  = (y); \
     next_new_state->ims    = ims; \  
366      next_new_state++; \      next_new_state++; \
367      DPRINTF(("%.*sADD_NEW(%d,%d)\n", rlevel*2-2, SP, (x), (y))); \      DPRINTF(("%.*sADD_NEW(%d,%d)\n", rlevel*2-2, SP, (x), (y))); \
368      } \      } \
# Line 298  for the current character, one for the f Line 373  for the current character, one for the f
373      { \      { \
374      next_new_state->offset = (x); \      next_new_state->offset = (x); \
375      next_new_state->count  = (y); \      next_new_state->count  = (y); \
     next_new_state->ims    = ims; \  
376      next_new_state->data   = (z); \      next_new_state->data   = (z); \
377      next_new_state++; \      next_new_state++; \
378      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 317  internal_dfa_exec( Line 391  internal_dfa_exec(
391    int offsetcount,    int offsetcount,
392    int *workspace,    int *workspace,
393    int wscount,    int wscount,
394    int ims,    int  rlevel)
   int  rlevel,  
   int  recursing)  
395  {  {
396  stateblock *active_states, *new_states, *temp_states;  stateblock *active_states, *new_states, *temp_states;
397  stateblock *next_active_state, *next_new_state;  stateblock *next_active_state, *next_new_state;
# Line 328  const uschar *ctypes, *lcc, *fcc; Line 400  const uschar *ctypes, *lcc, *fcc;
400  const uschar *ptr;  const uschar *ptr;
401  const uschar *end_code, *first_op;  const uschar *end_code, *first_op;
402    
403    dfa_recursion_info new_recursive;
404    
405  int active_count, new_count, match_count;  int active_count, new_count, match_count;
406    
407  /* Some fields in the md block are frequently referenced, so we load them into  /* Some fields in the md block are frequently referenced, so we load them into
# Line 351  wscount = (wscount - (wscount % (INTS_PE Line 425  wscount = (wscount - (wscount % (INTS_PE
425            (2 * INTS_PER_STATEBLOCK);            (2 * INTS_PER_STATEBLOCK);
426    
427  DPRINTF(("\n%.*s---------------------\n"  DPRINTF(("\n%.*s---------------------\n"
428    "%.*sCall to internal_dfa_exec f=%d r=%d\n",    "%.*sCall to internal_dfa_exec f=%d\n",
429    rlevel*2-2, SP, rlevel*2-2, SP, rlevel, recursing));    rlevel*2-2, SP, rlevel*2-2, SP, rlevel));
430    
431  ctypes = md->tables + ctypes_offset;  ctypes = md->tables + ctypes_offset;
432  lcc = md->tables + lcc_offset;  lcc = md->tables + lcc_offset;
# Line 365  next_new_state = new_states = active_sta Line 439  next_new_state = new_states = active_sta
439  new_count = 0;  new_count = 0;
440    
441  first_op = this_start_code + 1 + LINK_SIZE +  first_op = this_start_code + 1 + LINK_SIZE +
442    ((*this_start_code == OP_CBRA || *this_start_code == OP_SCBRA)? 2:0);    ((*this_start_code == OP_CBRA || *this_start_code == OP_SCBRA ||
443        *this_start_code == OP_CBRAPOS || *this_start_code == OP_SCBRAPOS)? 2:0);
444    
445  /* 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
446  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 414  if (*first_op == OP_REVERSE) Line 489  if (*first_op == OP_REVERSE)
489    
490      {      {
491      gone_back = (current_subject - max_back < start_subject)?      gone_back = (current_subject - max_back < start_subject)?
492        current_subject - start_subject : max_back;        (int)(current_subject - start_subject) : max_back;
493      current_subject -= gone_back;      current_subject -= gone_back;
494      }      }
495    
496    /* Save the earliest consulted character */    /* Save the earliest consulted character */
497    
498    if (current_subject < md->start_used_ptr)    if (current_subject < md->start_used_ptr)
499      md->start_used_ptr = current_subject;      md->start_used_ptr = current_subject;
500    
501    /* Now we can process the individual branches. */    /* Now we can process the individual branches. */
502    
# Line 431  if (*first_op == OP_REVERSE) Line 506  if (*first_op == OP_REVERSE)
506      int back = GET(end_code, 2+LINK_SIZE);      int back = GET(end_code, 2+LINK_SIZE);
507      if (back <= gone_back)      if (back <= gone_back)
508        {        {
509        int bstate = end_code - start_code + 2 + 2*LINK_SIZE;        int bstate = (int)(end_code - start_code + 2 + 2*LINK_SIZE);
510        ADD_NEW_DATA(-bstate, 0, gone_back - back);        ADD_NEW_DATA(-bstate, 0, gone_back - back);
511        }        }
512      end_code += GET(end_code, 1);      end_code += GET(end_code, 1);
# Line 464  else Line 539  else
539    else    else
540      {      {
541      int length = 1 + LINK_SIZE +      int length = 1 + LINK_SIZE +
542        ((*this_start_code == OP_CBRA || *this_start_code == OP_SCBRA)? 2:0);        ((*this_start_code == OP_CBRA || *this_start_code == OP_SCBRA ||
543            *this_start_code == OP_CBRAPOS || *this_start_code == OP_SCBRAPOS)?
544            2:0);
545      do      do
546        {        {
547        ADD_NEW(end_code - start_code + length, 0);        ADD_NEW((int)(end_code - start_code + length), 0);
548        end_code += GET(end_code, 1);        end_code += GET(end_code, 1);
549        length = 1 + LINK_SIZE;        length = 1 + LINK_SIZE;
550        }        }
# Line 488  for (;;) Line 565  for (;;)
565    int clen, dlen;    int clen, dlen;
566    unsigned int c, d;    unsigned int c, d;
567    int forced_fail = 0;    int forced_fail = 0;
568    int reached_end = 0;    BOOL could_continue = FALSE;
569    
570    /* 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
571    new state list. */    new state list. */
# Line 502  for (;;) Line 579  for (;;)
579    workspace[0] ^= 1;              /* Remember for the restarting feature */    workspace[0] ^= 1;              /* Remember for the restarting feature */
580    workspace[1] = active_count;    workspace[1] = active_count;
581    
582  #ifdef DEBUG  #ifdef PCRE_DEBUG
583    printf("%.*sNext character: rest of subject = \"", rlevel*2-2, SP);    printf("%.*sNext character: rest of subject = \"", rlevel*2-2, SP);
584    pchars((uschar *)ptr, strlen((char *)ptr), stdout);    pchars((uschar *)ptr, strlen((char *)ptr), stdout);
585    printf("\"\n");    printf("\"\n");
# Line 544  for (;;) Line 621  for (;;)
621    for (i = 0; i < active_count; i++)    for (i = 0; i < active_count; i++)
622      {      {
623      stateblock *current_state = active_states + i;      stateblock *current_state = active_states + i;
624        BOOL caseless = FALSE;
625      const uschar *code;      const uschar *code;
626      int state_offset = current_state->offset;      int state_offset = current_state->offset;
627      int count, codevalue, rrc;      int count, codevalue, rrc;
628    
629  #ifdef DEBUG  #ifdef PCRE_DEBUG
630      printf ("%.*sProcessing state %d c=", rlevel*2-2, SP, state_offset);      printf ("%.*sProcessing state %d c=", rlevel*2-2, SP, state_offset);
631      if (clen == 0) printf("EOL\n");      if (clen == 0) printf("EOL\n");
632        else if (c > 32 && c < 127) printf("'%c'\n", c);        else if (c > 32 && c < 127) printf("'%c'\n", c);
633          else printf("0x%02x\n", c);          else printf("0x%02x\n", c);
634  #endif  #endif
635    
     /* This variable is referred to implicity in the ADD_xxx macros. */  
   
     ims = current_state->ims;  
   
636      /* 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
637      (negated) state until the number of characters in the data field have      (negated) state until the number of characters in the data field have
638      been skipped". */      been skipped". */
# Line 578  for (;;) Line 652  for (;;)
652          }          }
653        }        }
654    
655      /* 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.
656      See the note at the head of this module about the possibility of improving      See the note at the head of this module about the possibility of improving
657      performance here. */      performance here. */
658    
# Line 597  for (;;) Line 671  for (;;)
671      code = start_code + state_offset;      code = start_code + state_offset;
672      codevalue = *code;      codevalue = *code;
673    
674        /* If this opcode inspects a character, but we are at the end of the
675        subject, remember the fact for use when testing for a partial match. */
676    
677        if (clen == 0 && poptable[codevalue] != 0)
678          could_continue = TRUE;
679    
680      /* 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
681      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
682      is wrong, because sometimes zero repetitions of the subject are      is wrong, because sometimes zero repetitions of the subject are
# Line 643  for (;;) Line 723  for (;;)
723    
724      switch (codevalue)      switch (codevalue)
725        {        {
726    /* ========================================================================== */
727          /* These cases are never obeyed. This is a fudge that causes a compile-
728          time error if the vectors coptable or poptable, which are indexed by
729          opcode, are not the correct length. It seems to be the only way to do
730          such a check at compile time, as the sizeof() operator does not work
731          in the C preprocessor. */
732    
733          case OP_TABLE_LENGTH:
734          case OP_TABLE_LENGTH +
735            ((sizeof(coptable) == OP_TABLE_LENGTH) &&
736             (sizeof(poptable) == OP_TABLE_LENGTH)):
737          break;
738    
739  /* ========================================================================== */  /* ========================================================================== */
740        /* 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
741        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
742        PCRE_NOTEMPTY is set, or PCRE_NOTEMPTY_ATSTART is set and we are at the        state. Note that KETRPOS will always be encountered at the end of the
743          subpattern, because the possessive subpattern repeats are always handled
744          using recursive calls. Thus, it never adds any new states.
745    
746          At the end of the (sub)pattern, unless we have an empty string and
747          PCRE_NOTEMPTY is set, or PCRE_NOTEMPTY_ATSTART is set and we are at the
748        start of the subject, save the match data, shifting up all previous        start of the subject, save the match data, shifting up all previous
749        matches so we always have the longest first. */        matches so we always have the longest first. */
750    
751        case OP_KET:        case OP_KET:
752        case OP_KETRMIN:        case OP_KETRMIN:
753        case OP_KETRMAX:        case OP_KETRMAX:
754          case OP_KETRPOS:
755        if (code != end_code)        if (code != end_code)
756          {          {
757          ADD_ACTIVE(state_offset + 1 + LINK_SIZE, 0);          ADD_ACTIVE(state_offset + 1 + LINK_SIZE, 0);
# Line 662  for (;;) Line 760  for (;;)
760            ADD_ACTIVE(state_offset - GET(code, 1), 0);            ADD_ACTIVE(state_offset - GET(code, 1), 0);
761            }            }
762          }          }
763        else        else
764          {          {
765          reached_end++;    /* Count branches that reach the end */          if (ptr > current_subject ||
         if (ptr > current_subject ||  
766              ((md->moptions & PCRE_NOTEMPTY) == 0 &&              ((md->moptions & PCRE_NOTEMPTY) == 0 &&
767                ((md->moptions & PCRE_NOTEMPTY_ATSTART) == 0 ||                ((md->moptions & PCRE_NOTEMPTY_ATSTART) == 0 ||
768                  current_subject > start_subject + md->start_offset)))                  current_subject > start_subject + md->start_offset)))
# Line 677  for (;;) Line 774  for (;;)
774            if (count > 0) memmove(offsets + 2, offsets, count * sizeof(int));            if (count > 0) memmove(offsets + 2, offsets, count * sizeof(int));
775            if (offsetcount >= 2)            if (offsetcount >= 2)
776              {              {
777              offsets[0] = current_subject - start_subject;              offsets[0] = (int)(current_subject - start_subject);
778              offsets[1] = ptr - start_subject;              offsets[1] = (int)(ptr - start_subject);
779              DPRINTF(("%.*sSet matched string = \"%.*s\"\n", rlevel*2-2, SP,              DPRINTF(("%.*sSet matched string = \"%.*s\"\n", rlevel*2-2, SP,
780                offsets[1] - offsets[0], current_subject));                offsets[1] - offsets[0], current_subject));
781              }              }
# Line 689  for (;;) Line 786  for (;;)
786                match_count, rlevel*2-2, SP));                match_count, rlevel*2-2, SP));
787              return match_count;              return match_count;
788              }              }
789            }            }
790          }          }
791        break;        break;
792    
# Line 700  for (;;) Line 797  for (;;)
797        /*-----------------------------------------------------------------*/        /*-----------------------------------------------------------------*/
798        case OP_ALT:        case OP_ALT:
799        do { code += GET(code, 1); } while (*code == OP_ALT);        do { code += GET(code, 1); } while (*code == OP_ALT);
800        ADD_ACTIVE(code - start_code, 0);        ADD_ACTIVE((int)(code - start_code), 0);
801        break;        break;
802    
803        /*-----------------------------------------------------------------*/        /*-----------------------------------------------------------------*/
# Line 708  for (;;) Line 805  for (;;)
805        case OP_SBRA:        case OP_SBRA:
806        do        do
807          {          {
808          ADD_ACTIVE(code - start_code + 1 + LINK_SIZE, 0);          ADD_ACTIVE((int)(code - start_code + 1 + LINK_SIZE), 0);
809          code += GET(code, 1);          code += GET(code, 1);
810          }          }
811        while (*code == OP_ALT);        while (*code == OP_ALT);
# Line 717  for (;;) Line 814  for (;;)
814        /*-----------------------------------------------------------------*/        /*-----------------------------------------------------------------*/
815        case OP_CBRA:        case OP_CBRA:
816        case OP_SCBRA:        case OP_SCBRA:
817        ADD_ACTIVE(code - start_code + 3 + LINK_SIZE,  0);        ADD_ACTIVE((int)(code - start_code + 3 + LINK_SIZE),  0);
818        code += GET(code, 1);        code += GET(code, 1);
819        while (*code == OP_ALT)        while (*code == OP_ALT)
820          {          {
821          ADD_ACTIVE(code - start_code + 1 + LINK_SIZE,  0);          ADD_ACTIVE((int)(code - start_code + 1 + LINK_SIZE),  0);
822          code += GET(code, 1);          code += GET(code, 1);
823          }          }
824        break;        break;
# Line 732  for (;;) Line 829  for (;;)
829        ADD_ACTIVE(state_offset + 1, 0);        ADD_ACTIVE(state_offset + 1, 0);
830        code += 1 + GET(code, 2);        code += 1 + GET(code, 2);
831        while (*code == OP_ALT) code += GET(code, 1);        while (*code == OP_ALT) code += GET(code, 1);
832        ADD_ACTIVE(code - start_code + 1 + LINK_SIZE, 0);        ADD_ACTIVE((int)(code - start_code + 1 + LINK_SIZE), 0);
833        break;        break;
834    
835        /*-----------------------------------------------------------------*/        /*-----------------------------------------------------------------*/
836        case OP_SKIPZERO:        case OP_SKIPZERO:
837        code += 1 + GET(code, 2);        code += 1 + GET(code, 2);
838        while (*code == OP_ALT) code += GET(code, 1);        while (*code == OP_ALT) code += GET(code, 1);
839        ADD_ACTIVE(code - start_code + 1 + LINK_SIZE, 0);        ADD_ACTIVE((int)(code - start_code + 1 + LINK_SIZE), 0);
840        break;        break;
841    
842        /*-----------------------------------------------------------------*/        /*-----------------------------------------------------------------*/
843        case OP_CIRC:        case OP_CIRC:
844        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)))  
845          { ADD_ACTIVE(state_offset + 1, 0); }          { ADD_ACTIVE(state_offset + 1, 0); }
846        break;        break;
847    
848        /*-----------------------------------------------------------------*/        /*-----------------------------------------------------------------*/
849        case OP_EOD:        case OP_CIRCM:
850        if (ptr >= end_subject) { ADD_ACTIVE(state_offset + 1, 0); }        if ((ptr == start_subject && (md->moptions & PCRE_NOTBOL) == 0) ||
851              (ptr != end_subject && WAS_NEWLINE(ptr)))
852            { ADD_ACTIVE(state_offset + 1, 0); }
853        break;        break;
854    
855        /*-----------------------------------------------------------------*/        /*-----------------------------------------------------------------*/
856        case OP_OPT:        case OP_EOD:
857        ims = code[1];        if (ptr >= end_subject)
858        ADD_ACTIVE(state_offset + 2, 0);          {
859            if ((md->moptions & PCRE_PARTIAL_HARD) != 0)
860              could_continue = TRUE;
861            else { ADD_ACTIVE(state_offset + 1, 0); }
862            }
863        break;        break;
864    
865        /*-----------------------------------------------------------------*/        /*-----------------------------------------------------------------*/
# Line 793  for (;;) Line 893  for (;;)
893    
894        /*-----------------------------------------------------------------*/        /*-----------------------------------------------------------------*/
895        case OP_EODN:        case OP_EODN:
896        if (clen == 0 || (IS_NEWLINE(ptr) && ptr == end_subject - md->nllen))        if (clen == 0 && (md->moptions & PCRE_PARTIAL_HARD) != 0)
897            could_continue = TRUE;
898          else if (clen == 0 || (IS_NEWLINE(ptr) && ptr == end_subject - md->nllen))
899          { ADD_ACTIVE(state_offset + 1, 0); }          { ADD_ACTIVE(state_offset + 1, 0); }
900        break;        break;
901    
# Line 801  for (;;) Line 903  for (;;)
903        case OP_DOLL:        case OP_DOLL:
904        if ((md->moptions & PCRE_NOTEOL) == 0)        if ((md->moptions & PCRE_NOTEOL) == 0)
905          {          {
906          if (clen == 0 ||          if (clen == 0 && (md->moptions & PCRE_PARTIAL_HARD) != 0)
907              could_continue = TRUE;
908            else if (clen == 0 ||
909              ((md->poptions & PCRE_DOLLAR_ENDONLY) == 0 && IS_NEWLINE(ptr) &&              ((md->poptions & PCRE_DOLLAR_ENDONLY) == 0 && IS_NEWLINE(ptr) &&
910                 ((ims & PCRE_MULTILINE) != 0 || ptr == end_subject - md->nllen)                 (ptr == end_subject - md->nllen)
911              ))              ))
912            { ADD_ACTIVE(state_offset + 1, 0); }            { ADD_ACTIVE(state_offset + 1, 0); }
913          }          }
914        else if ((ims & PCRE_MULTILINE) != 0 && IS_NEWLINE(ptr))        break;
915    
916          /*-----------------------------------------------------------------*/
917          case OP_DOLLM:
918          if ((md->moptions & PCRE_NOTEOL) == 0)
919            {
920            if (clen == 0 && (md->moptions & PCRE_PARTIAL_HARD) != 0)
921              could_continue = TRUE;
922            else if (clen == 0 ||
923                ((md->poptions & PCRE_DOLLAR_ENDONLY) == 0 && IS_NEWLINE(ptr)))
924              { ADD_ACTIVE(state_offset + 1, 0); }
925            }
926          else if (IS_NEWLINE(ptr))
927          { ADD_ACTIVE(state_offset + 1, 0); }          { ADD_ACTIVE(state_offset + 1, 0); }
928        break;        break;
929    
# Line 839  for (;;) Line 955  for (;;)
955          if (ptr > start_subject)          if (ptr > start_subject)
956            {            {
957            const uschar *temp = ptr - 1;            const uschar *temp = ptr - 1;
958            if (temp < md->start_used_ptr) md->start_used_ptr = temp;            if (temp < md->start_used_ptr) md->start_used_ptr = temp;
959  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
960            if (utf8) BACKCHAR(temp);            if (utf8) BACKCHAR(temp);
961  #endif  #endif
962            GETCHARTEST(d, temp);            GETCHARTEST(d, temp);
963    #ifdef SUPPORT_UCP
964              if ((md->poptions & PCRE_UCP) != 0)
965                {
966                if (d == '_') left_word = TRUE; else
967                  {
968                  int cat = UCD_CATEGORY(d);
969                  left_word = (cat == ucp_L || cat == ucp_N);
970                  }
971                }
972              else
973    #endif
974            left_word = d < 256 && (ctypes[d] & ctype_word) != 0;            left_word = d < 256 && (ctypes[d] & ctype_word) != 0;
975            }            }
976          else left_word = 0;          else left_word = FALSE;
977    
978          if (clen > 0)          if (clen > 0)
979              {
980    #ifdef SUPPORT_UCP
981              if ((md->poptions & PCRE_UCP) != 0)
982                {
983                if (c == '_') right_word = TRUE; else
984                  {
985                  int cat = UCD_CATEGORY(c);
986                  right_word = (cat == ucp_L || cat == ucp_N);
987                  }
988                }
989              else
990    #endif
991            right_word = c < 256 && (ctypes[c] & ctype_word) != 0;            right_word = c < 256 && (ctypes[c] & ctype_word) != 0;
992          else              /* This is a fudge to ensure that if this is the */            }
993            {               /* last item in the pattern, we don't count it as */          else right_word = FALSE;
           reached_end--;  /* reached, thus disabling a partial match. */  
           right_word = 0;  
           }  
994    
995          if ((left_word == right_word) == (codevalue == OP_NOT_WORD_BOUNDARY))          if ((left_word == right_word) == (codevalue == OP_NOT_WORD_BOUNDARY))
996            { ADD_ACTIVE(state_offset + 1, 0); }            { ADD_ACTIVE(state_offset + 1, 0); }
# Line 881  for (;;) Line 1017  for (;;)
1017            break;            break;
1018    
1019            case PT_LAMP:            case PT_LAMP:
1020            OK = prop->chartype == ucp_Lu || prop->chartype == ucp_Ll || prop->chartype == ucp_Lt;            OK = prop->chartype == ucp_Lu || prop->chartype == ucp_Ll ||
1021                   prop->chartype == ucp_Lt;
1022            break;            break;
1023    
1024            case PT_GC:            case PT_GC:
# Line 896  for (;;) Line 1033  for (;;)
1033            OK = prop->script == code[2];            OK = prop->script == code[2];
1034            break;            break;
1035    
1036              /* These are specials for combination cases. */
1037    
1038              case PT_ALNUM:
1039              OK = _pcre_ucp_gentype[prop->chartype] == ucp_L ||
1040                   _pcre_ucp_gentype[prop->chartype] == ucp_N;
1041              break;
1042    
1043              case PT_SPACE:    /* Perl space */
1044              OK = _pcre_ucp_gentype[prop->chartype] == ucp_Z ||
1045                   c == CHAR_HT || c == CHAR_NL || c == CHAR_FF || c == CHAR_CR;
1046              break;
1047    
1048              case PT_PXSPACE:  /* POSIX space */
1049              OK = _pcre_ucp_gentype[prop->chartype] == ucp_Z ||
1050                   c == CHAR_HT || c == CHAR_NL || c == CHAR_VT ||
1051                   c == CHAR_FF || c == CHAR_CR;
1052              break;
1053    
1054              case PT_WORD:
1055              OK = _pcre_ucp_gentype[prop->chartype] == ucp_L ||
1056                   _pcre_ucp_gentype[prop->chartype] == ucp_N ||
1057                   c == CHAR_UNDERSCORE;
1058              break;
1059    
1060            /* Should never occur, but keep compilers from grumbling. */            /* Should never occur, but keep compilers from grumbling. */
1061    
1062            default:            default:
# Line 1050  for (;;) Line 1211  for (;;)
1211            break;            break;
1212    
1213            case PT_LAMP:            case PT_LAMP:
1214            OK = prop->chartype == ucp_Lu || prop->chartype == ucp_Ll || prop->chartype == ucp_Lt;            OK = prop->chartype == ucp_Lu || prop->chartype == ucp_Ll ||
1215                prop->chartype == ucp_Lt;
1216            break;            break;
1217    
1218            case PT_GC:            case PT_GC:
# Line 1065  for (;;) Line 1227  for (;;)
1227            OK = prop->script == code[3];            OK = prop->script == code[3];
1228            break;            break;
1229    
1230              /* These are specials for combination cases. */
1231    
1232              case PT_ALNUM:
1233              OK = _pcre_ucp_gentype[prop->chartype] == ucp_L ||
1234                   _pcre_ucp_gentype[prop->chartype] == ucp_N;
1235              break;
1236    
1237              case PT_SPACE:    /* Perl space */
1238              OK = _pcre_ucp_gentype[prop->chartype] == ucp_Z ||
1239                   c == CHAR_HT || c == CHAR_NL || c == CHAR_FF || c == CHAR_CR;
1240              break;
1241    
1242              case PT_PXSPACE:  /* POSIX space */
1243              OK = _pcre_ucp_gentype[prop->chartype] == ucp_Z ||
1244                   c == CHAR_HT || c == CHAR_NL || c == CHAR_VT ||
1245                   c == CHAR_FF || c == CHAR_CR;
1246              break;
1247    
1248              case PT_WORD:
1249              OK = _pcre_ucp_gentype[prop->chartype] == ucp_L ||
1250                   _pcre_ucp_gentype[prop->chartype] == ucp_N ||
1251                   c == CHAR_UNDERSCORE;
1252              break;
1253    
1254            /* Should never occur, but keep compilers from grumbling. */            /* Should never occur, but keep compilers from grumbling. */
1255    
1256            default:            default:
# Line 1272  for (;;) Line 1458  for (;;)
1458            break;            break;
1459    
1460            case PT_LAMP:            case PT_LAMP:
1461            OK = prop->chartype == ucp_Lu || prop->chartype == ucp_Ll || prop->chartype == ucp_Lt;            OK = prop->chartype == ucp_Lu || prop->chartype == ucp_Ll ||
1462                prop->chartype == ucp_Lt;
1463            break;            break;
1464    
1465            case PT_GC:            case PT_GC:
# Line 1287  for (;;) Line 1474  for (;;)
1474            OK = prop->script == code[3];            OK = prop->script == code[3];
1475            break;            break;
1476    
1477              /* These are specials for combination cases. */
1478    
1479              case PT_ALNUM:
1480              OK = _pcre_ucp_gentype[prop->chartype] == ucp_L ||
1481                   _pcre_ucp_gentype[prop->chartype] == ucp_N;
1482              break;
1483    
1484              case PT_SPACE:    /* Perl space */
1485              OK = _pcre_ucp_gentype[prop->chartype] == ucp_Z ||
1486                   c == CHAR_HT || c == CHAR_NL || c == CHAR_FF || c == CHAR_CR;
1487              break;
1488    
1489              case PT_PXSPACE:  /* POSIX space */
1490              OK = _pcre_ucp_gentype[prop->chartype] == ucp_Z ||
1491                   c == CHAR_HT || c == CHAR_NL || c == CHAR_VT ||
1492                   c == CHAR_FF || c == CHAR_CR;
1493              break;
1494    
1495              case PT_WORD:
1496              OK = _pcre_ucp_gentype[prop->chartype] == ucp_L ||
1497                   _pcre_ucp_gentype[prop->chartype] == ucp_N ||
1498                   c == CHAR_UNDERSCORE;
1499              break;
1500    
1501            /* Should never occur, but keep compilers from grumbling. */            /* Should never occur, but keep compilers from grumbling. */
1502    
1503            default:            default:
# Line 1519  for (;;) Line 1730  for (;;)
1730            break;            break;
1731    
1732            case PT_LAMP:            case PT_LAMP:
1733            OK = prop->chartype == ucp_Lu || prop->chartype == ucp_Ll || prop->chartype == ucp_Lt;            OK = prop->chartype == ucp_Lu || prop->chartype == ucp_Ll ||
1734                prop->chartype == ucp_Lt;
1735            break;            break;
1736    
1737            case PT_GC:            case PT_GC:
# Line 1534  for (;;) Line 1746  for (;;)
1746            OK = prop->script == code[5];            OK = prop->script == code[5];
1747            break;            break;
1748    
1749              /* These are specials for combination cases. */
1750    
1751              case PT_ALNUM:
1752              OK = _pcre_ucp_gentype[prop->chartype] == ucp_L ||
1753                   _pcre_ucp_gentype[prop->chartype] == ucp_N;
1754              break;
1755    
1756              case PT_SPACE:    /* Perl space */
1757              OK = _pcre_ucp_gentype[prop->chartype] == ucp_Z ||
1758                   c == CHAR_HT || c == CHAR_NL || c == CHAR_FF || c == CHAR_CR;
1759              break;
1760    
1761              case PT_PXSPACE:  /* POSIX space */
1762              OK = _pcre_ucp_gentype[prop->chartype] == ucp_Z ||
1763                   c == CHAR_HT || c == CHAR_NL || c == CHAR_VT ||
1764                   c == CHAR_FF || c == CHAR_CR;
1765              break;
1766    
1767              case PT_WORD:
1768              OK = _pcre_ucp_gentype[prop->chartype] == ucp_L ||
1769                   _pcre_ucp_gentype[prop->chartype] == ucp_N ||
1770                   c == CHAR_UNDERSCORE;
1771              break;
1772    
1773            /* Should never occur, but keep compilers from grumbling. */            /* Should never occur, but keep compilers from grumbling. */
1774    
1775            default:            default:
# Line 1743  for (;;) Line 1979  for (;;)
1979        break;        break;
1980    
1981        /*-----------------------------------------------------------------*/        /*-----------------------------------------------------------------*/
1982        case OP_CHARNC:        case OP_CHARI:
1983        if (clen == 0) break;        if (clen == 0) break;
1984    
1985  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
# Line 1929  for (;;) Line 2165  for (;;)
2165        break;        break;
2166    
2167        /*-----------------------------------------------------------------*/        /*-----------------------------------------------------------------*/
2168        /* Match a negated single character. This is only used for one-byte        /* Match a negated single character casefully. This is only used for
2169        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
2170        checking (c) can be multibyte. */        checking (c) can be multibyte. */
2171    
2172        case OP_NOT:        case OP_NOT:
2173        if (clen > 0)        if (clen > 0 && c != d) { ADD_NEW(state_offset + dlen + 1, 0); }
2174          {        break;
2175          unsigned int otherd = ((ims & PCRE_CASELESS) != 0)? fcc[d] : d;  
2176          if (c != d && c != otherd) { ADD_NEW(state_offset + dlen + 1, 0); }        /*-----------------------------------------------------------------*/
2177          }        /* Match a negated single character caselessly. This is only used for
2178          one-byte characters, that is, we know that d < 256. The character we are
2179          checking (c) can be multibyte. */
2180    
2181          case OP_NOTI:
2182          if (clen > 0 && c != d && c != fcc[d])
2183            { ADD_NEW(state_offset + dlen + 1, 0); }
2184        break;        break;
2185    
2186        /*-----------------------------------------------------------------*/        /*-----------------------------------------------------------------*/
2187          case OP_PLUSI:
2188          case OP_MINPLUSI:
2189          case OP_POSPLUSI:
2190          case OP_NOTPLUSI:
2191          case OP_NOTMINPLUSI:
2192          case OP_NOTPOSPLUSI:
2193          caseless = TRUE;
2194          codevalue -= OP_STARI - OP_STAR;
2195    
2196          /* Fall through */
2197        case OP_PLUS:        case OP_PLUS:
2198        case OP_MINPLUS:        case OP_MINPLUS:
2199        case OP_POSPLUS:        case OP_POSPLUS:
# Line 1953  for (;;) Line 2205  for (;;)
2205        if (clen > 0)        if (clen > 0)
2206          {          {
2207          unsigned int otherd = NOTACHAR;          unsigned int otherd = NOTACHAR;
2208          if ((ims & PCRE_CASELESS) != 0)          if (caseless)
2209            {            {
2210  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2211            if (utf8 && d >= 128)            if (utf8 && d >= 128)
# Line 1981  for (;;) Line 2233  for (;;)
2233        break;        break;
2234    
2235        /*-----------------------------------------------------------------*/        /*-----------------------------------------------------------------*/
2236          case OP_QUERYI:
2237          case OP_MINQUERYI:
2238          case OP_POSQUERYI:
2239          case OP_NOTQUERYI:
2240          case OP_NOTMINQUERYI:
2241          case OP_NOTPOSQUERYI:
2242          caseless = TRUE;
2243          codevalue -= OP_STARI - OP_STAR;
2244          /* Fall through */
2245        case OP_QUERY:        case OP_QUERY:
2246        case OP_MINQUERY:        case OP_MINQUERY:
2247        case OP_POSQUERY:        case OP_POSQUERY:
# Line 1991  for (;;) Line 2252  for (;;)
2252        if (clen > 0)        if (clen > 0)
2253          {          {
2254          unsigned int otherd = NOTACHAR;          unsigned int otherd = NOTACHAR;
2255          if ((ims & PCRE_CASELESS) != 0)          if (caseless)
2256            {            {
2257  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2258            if (utf8 && d >= 128)            if (utf8 && d >= 128)
# Line 2017  for (;;) Line 2278  for (;;)
2278        break;        break;
2279    
2280        /*-----------------------------------------------------------------*/        /*-----------------------------------------------------------------*/
2281          case OP_STARI:
2282          case OP_MINSTARI:
2283          case OP_POSSTARI:
2284          case OP_NOTSTARI:
2285          case OP_NOTMINSTARI:
2286          case OP_NOTPOSSTARI:
2287          caseless = TRUE;
2288          codevalue -= OP_STARI - OP_STAR;
2289          /* Fall through */
2290        case OP_STAR:        case OP_STAR:
2291        case OP_MINSTAR:        case OP_MINSTAR:
2292        case OP_POSSTAR:        case OP_POSSTAR:
# Line 2027  for (;;) Line 2297  for (;;)
2297        if (clen > 0)        if (clen > 0)
2298          {          {
2299          unsigned int otherd = NOTACHAR;          unsigned int otherd = NOTACHAR;
2300          if ((ims & PCRE_CASELESS) != 0)          if (caseless)
2301            {            {
2302  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2303            if (utf8 && d >= 128)            if (utf8 && d >= 128)
# Line 2053  for (;;) Line 2323  for (;;)
2323        break;        break;
2324    
2325        /*-----------------------------------------------------------------*/        /*-----------------------------------------------------------------*/
2326          case OP_EXACTI:
2327          case OP_NOTEXACTI:
2328          caseless = TRUE;
2329          codevalue -= OP_STARI - OP_STAR;
2330          /* Fall through */
2331        case OP_EXACT:        case OP_EXACT:
2332        case OP_NOTEXACT:        case OP_NOTEXACT:
2333        count = current_state->count;  /* Number already matched */        count = current_state->count;  /* Number already matched */
2334        if (clen > 0)        if (clen > 0)
2335          {          {
2336          unsigned int otherd = NOTACHAR;          unsigned int otherd = NOTACHAR;
2337          if ((ims & PCRE_CASELESS) != 0)          if (caseless)
2338            {            {
2339  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2340            if (utf8 && d >= 128)            if (utf8 && d >= 128)
# Line 2083  for (;;) Line 2358  for (;;)
2358        break;        break;
2359    
2360        /*-----------------------------------------------------------------*/        /*-----------------------------------------------------------------*/
2361          case OP_UPTOI:
2362          case OP_MINUPTOI:
2363          case OP_POSUPTOI:
2364          case OP_NOTUPTOI:
2365          case OP_NOTMINUPTOI:
2366          case OP_NOTPOSUPTOI:
2367          caseless = TRUE;
2368          codevalue -= OP_STARI - OP_STAR;
2369          /* Fall through */
2370        case OP_UPTO:        case OP_UPTO:
2371        case OP_MINUPTO:        case OP_MINUPTO:
2372        case OP_POSUPTO:        case OP_POSUPTO:
# Line 2094  for (;;) Line 2378  for (;;)
2378        if (clen > 0)        if (clen > 0)
2379          {          {
2380          unsigned int otherd = NOTACHAR;          unsigned int otherd = NOTACHAR;
2381          if ((ims & PCRE_CASELESS) != 0)          if (caseless)
2382            {            {
2383  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2384            if (utf8 && d >= 128)            if (utf8 && d >= 128)
# Line 2161  for (;;) Line 2445  for (;;)
2445          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
2446          quantifier, this is where it will be. */          quantifier, this is where it will be. */
2447    
2448          next_state_offset = ecode - start_code;          next_state_offset = (int)(ecode - start_code);
2449    
2450          switch (*ecode)          switch (*ecode)
2451            {            {
# Line 2232  for (;;) Line 2516  for (;;)
2516            md,                                   /* static match data */            md,                                   /* static match data */
2517            code,                                 /* this subexpression's code */            code,                                 /* this subexpression's code */
2518            ptr,                                  /* where we currently are */            ptr,                                  /* where we currently are */
2519            ptr - start_subject,                  /* start offset */            (int)(ptr - start_subject),           /* start offset */
2520            local_offsets,                        /* offset vector */            local_offsets,                        /* offset vector */
2521            sizeof(local_offsets)/sizeof(int),    /* size of same */            sizeof(local_offsets)/sizeof(int),    /* size of same */
2522            local_workspace,                      /* workspace vector */            local_workspace,                      /* workspace vector */
2523            sizeof(local_workspace)/sizeof(int),  /* size of same */            sizeof(local_workspace)/sizeof(int),  /* size of same */
2524            ims,                                  /* the current ims flags */            rlevel);                              /* function recursion level */
           rlevel,                               /* function recursion level */  
           recursing);                           /* pass on regex recursion */  
2525    
2526            if (rc == PCRE_ERROR_DFA_UITEM) return rc;
2527          if ((rc >= 0) == (codevalue == OP_ASSERT || codevalue == OP_ASSERTBACK))          if ((rc >= 0) == (codevalue == OP_ASSERT || codevalue == OP_ASSERTBACK))
2528              { ADD_ACTIVE(endasscode + LINK_SIZE + 1 - start_code, 0); }              { ADD_ACTIVE((int)(endasscode + LINK_SIZE + 1 - start_code), 0); }
2529          }          }
2530        break;        break;
2531    
# Line 2269  for (;;) Line 2552  for (;;)
2552              cb.callout_number   = code[LINK_SIZE+2];              cb.callout_number   = code[LINK_SIZE+2];
2553              cb.offset_vector    = offsets;              cb.offset_vector    = offsets;
2554              cb.subject          = (PCRE_SPTR)start_subject;              cb.subject          = (PCRE_SPTR)start_subject;
2555              cb.subject_length   = end_subject - start_subject;              cb.subject_length   = (int)(end_subject - start_subject);
2556              cb.start_match      = current_subject - start_subject;              cb.start_match      = (int)(current_subject - start_subject);
2557              cb.current_position = ptr - start_subject;              cb.current_position = (int)(ptr - start_subject);
2558              cb.pattern_position = GET(code, LINK_SIZE + 3);              cb.pattern_position = GET(code, LINK_SIZE + 3);
2559              cb.next_item_length = GET(code, 3 + 2*LINK_SIZE);              cb.next_item_length = GET(code, 3 + 2*LINK_SIZE);
2560              cb.capture_top      = 1;              cb.capture_top      = 1;
# Line 2287  for (;;) Line 2570  for (;;)
2570    
2571          /* Back reference conditions are not supported */          /* Back reference conditions are not supported */
2572    
2573          if (condcode == OP_CREF) return PCRE_ERROR_DFA_UCOND;          if (condcode == OP_CREF || condcode == OP_NCREF)
2574              return PCRE_ERROR_DFA_UCOND;
2575    
2576          /* The DEFINE condition is always false */          /* The DEFINE condition is always false */
2577    
# Line 2298  for (;;) Line 2582  for (;;)
2582          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
2583          recursed groups. */          recursed groups. */
2584    
2585          else if (condcode == OP_RREF)          else if (condcode == OP_RREF || condcode == OP_NRREF)
2586            {            {
2587            int value = GET2(code, LINK_SIZE+2);            int value = GET2(code, LINK_SIZE+2);
2588            if (value != RREF_ANY) return PCRE_ERROR_DFA_UCOND;            if (value != RREF_ANY) return PCRE_ERROR_DFA_UCOND;
2589            if (recursing > 0)            if (md->recursive != NULL)
2590              { ADD_ACTIVE(state_offset + LINK_SIZE + 4, 0); }              { ADD_ACTIVE(state_offset + LINK_SIZE + 4, 0); }
2591            else { ADD_ACTIVE(state_offset + codelink + LINK_SIZE + 1, 0); }            else { ADD_ACTIVE(state_offset + codelink + LINK_SIZE + 1, 0); }
2592            }            }
# Line 2321  for (;;) Line 2605  for (;;)
2605              md,                                   /* fixed match data */              md,                                   /* fixed match data */
2606              asscode,                              /* this subexpression's code */              asscode,                              /* this subexpression's code */
2607              ptr,                                  /* where we currently are */              ptr,                                  /* where we currently are */
2608              ptr - start_subject,                  /* start offset */              (int)(ptr - start_subject),           /* start offset */
2609              local_offsets,                        /* offset vector */              local_offsets,                        /* offset vector */
2610              sizeof(local_offsets)/sizeof(int),    /* size of same */              sizeof(local_offsets)/sizeof(int),    /* size of same */
2611              local_workspace,                      /* workspace vector */              local_workspace,                      /* workspace vector */
2612              sizeof(local_workspace)/sizeof(int),  /* size of same */              sizeof(local_workspace)/sizeof(int),  /* size of same */
2613              ims,                                  /* the current ims flags */              rlevel);                              /* function recursion level */
             rlevel,                               /* function recursion level */  
             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 + codelink + LINK_SIZE + 1, 0); }              { ADD_ACTIVE(state_offset + codelink + LINK_SIZE + 1, 0); }
2621            }            }
# Line 2342  for (;;) Line 2625  for (;;)
2625        /*-----------------------------------------------------------------*/        /*-----------------------------------------------------------------*/
2626        case OP_RECURSE:        case OP_RECURSE:
2627          {          {
2628            dfa_recursion_info *ri;
2629          int local_offsets[1000];          int local_offsets[1000];
2630          int local_workspace[1000];          int local_workspace[1000];
2631            const uschar *callpat = start_code + GET(code, 1);
2632            int recno = (callpat == md->start_code)? 0 :
2633              GET2(callpat, 1 + LINK_SIZE);
2634          int rc;          int rc;
2635    
2636          DPRINTF(("%.*sStarting regex recursion %d\n", rlevel*2-2, SP,          DPRINTF(("%.*sStarting regex recursion\n", rlevel*2-2, SP));
2637            recursing + 1));  
2638            /* Check for repeating a recursion without advancing the subject
2639            pointer. This should catch convoluted mutual recursions. (Some simple
2640            cases are caught at compile time.) */
2641    
2642            for (ri = md->recursive; ri != NULL; ri = ri->prevrec)
2643              if (recno == ri->group_num && ptr == ri->subject_position)
2644                return PCRE_ERROR_RECURSELOOP;
2645    
2646            /* Remember this recursion and where we started it so as to
2647            catch infinite loops. */
2648    
2649            new_recursive.group_num = recno;
2650            new_recursive.subject_position = ptr;
2651            new_recursive.prevrec = md->recursive;
2652            md->recursive = &new_recursive;
2653    
2654          rc = internal_dfa_exec(          rc = internal_dfa_exec(
2655            md,                                   /* fixed match data */            md,                                   /* fixed match data */
2656            start_code + GET(code, 1),            /* this subexpression's code */            callpat,                              /* this subexpression's code */
2657            ptr,                                  /* where we currently are */            ptr,                                  /* where we currently are */
2658            ptr - start_subject,                  /* start offset */            (int)(ptr - start_subject),           /* start offset */
2659            local_offsets,                        /* offset vector */            local_offsets,                        /* offset vector */
2660            sizeof(local_offsets)/sizeof(int),    /* size of same */            sizeof(local_offsets)/sizeof(int),    /* size of same */
2661            local_workspace,                      /* workspace vector */            local_workspace,                      /* workspace vector */
2662            sizeof(local_workspace)/sizeof(int),  /* size of same */            sizeof(local_workspace)/sizeof(int),  /* size of same */
2663            ims,                                  /* the current ims flags */            rlevel);                              /* function recursion level */
           rlevel,                               /* function recursion level */  
           recursing + 1);                       /* regex recurse level */  
2664    
2665          DPRINTF(("%.*sReturn from regex recursion %d: rc=%d\n", rlevel*2-2, SP,          md->recursive = new_recursive.prevrec;  /* Done this recursion */
2666            recursing + 1, rc));  
2667            DPRINTF(("%.*sReturn from regex recursion: rc=%d\n", rlevel*2-2, SP,
2668              rc));
2669    
2670          /* Ran out of internal offsets */          /* Ran out of internal offsets */
2671    
# Line 2396  for (;;) Line 2698  for (;;)
2698        break;        break;
2699    
2700        /*-----------------------------------------------------------------*/        /*-----------------------------------------------------------------*/
2701          case OP_BRAPOS:
2702          case OP_SBRAPOS:
2703          case OP_CBRAPOS:
2704          case OP_SCBRAPOS:
2705          case OP_BRAPOSZERO:
2706            {
2707            int charcount, matched_count;
2708            const uschar *local_ptr = ptr;
2709            BOOL allow_zero;
2710    
2711            if (codevalue == OP_BRAPOSZERO)
2712              {
2713              allow_zero = TRUE;
2714              codevalue = *(++code);  /* Codevalue will be one of above BRAs */
2715              }
2716            else allow_zero = FALSE;
2717    
2718            /* Loop to match the subpattern as many times as possible as if it were
2719            a complete pattern. */
2720    
2721            for (matched_count = 0;; matched_count++)
2722              {
2723              int local_offsets[2];
2724              int local_workspace[1000];
2725    
2726              int rc = internal_dfa_exec(
2727                md,                                   /* fixed match data */
2728                code,                                 /* this subexpression's code */
2729                local_ptr,                            /* where we currently are */
2730                (int)(ptr - start_subject),           /* start offset */
2731                local_offsets,                        /* offset vector */
2732                sizeof(local_offsets)/sizeof(int),    /* size of same */
2733                local_workspace,                      /* workspace vector */
2734                sizeof(local_workspace)/sizeof(int),  /* size of same */
2735                rlevel);                              /* function recursion level */
2736    
2737              /* Failed to match */
2738    
2739              if (rc < 0)
2740                {
2741                if (rc != PCRE_ERROR_NOMATCH) return rc;
2742                break;
2743                }
2744    
2745              /* Matched: break the loop if zero characters matched. */
2746    
2747              charcount = local_offsets[1] - local_offsets[0];
2748              if (charcount == 0) break;
2749              local_ptr += charcount;    /* Advance temporary position ptr */
2750              }
2751    
2752            /* At this point we have matched the subpattern matched_count
2753            times, and local_ptr is pointing to the character after the end of the
2754            last match. */
2755    
2756            if (matched_count > 0 || allow_zero)
2757              {
2758              const uschar *end_subpattern = code;
2759              int next_state_offset;
2760    
2761              do { end_subpattern += GET(end_subpattern, 1); }
2762                while (*end_subpattern == OP_ALT);
2763              next_state_offset =
2764                (int)(end_subpattern - start_code + LINK_SIZE + 1);
2765    
2766              /* Optimization: if there are no more active states, and there
2767              are no new states yet set up, then skip over the subject string
2768              right here, to save looping. Otherwise, set up the new state to swing
2769              into action when the end of the matched substring is reached. */
2770    
2771              if (i + 1 >= active_count && new_count == 0)
2772                {
2773                ptr = local_ptr;
2774                clen = 0;
2775                ADD_NEW(next_state_offset, 0);
2776                }
2777              else
2778                {
2779                const uschar *p = ptr;
2780                const uschar *pp = local_ptr;
2781                charcount = pp - p;
2782                while (p < pp) if ((*p++ & 0xc0) == 0x80) charcount--;
2783                ADD_NEW_DATA(-next_state_offset, 0, (charcount - 1));
2784                }
2785              }
2786            }
2787          break;
2788    
2789          /*-----------------------------------------------------------------*/
2790        case OP_ONCE:        case OP_ONCE:
2791          {          {
2792          int local_offsets[2];          int local_offsets[2];
# Line 2405  for (;;) Line 2796  for (;;)
2796            md,                                   /* fixed match data */            md,                                   /* fixed match data */
2797            code,                                 /* this subexpression's code */            code,                                 /* this subexpression's code */
2798            ptr,                                  /* where we currently are */            ptr,                                  /* where we currently are */
2799            ptr - start_subject,                  /* start offset */            (int)(ptr - start_subject),           /* start offset */
2800            local_offsets,                        /* offset vector */            local_offsets,                        /* offset vector */
2801            sizeof(local_offsets)/sizeof(int),    /* size of same */            sizeof(local_offsets)/sizeof(int),    /* size of same */
2802            local_workspace,                      /* workspace vector */            local_workspace,                      /* workspace vector */
2803            sizeof(local_workspace)/sizeof(int),  /* size of same */            sizeof(local_workspace)/sizeof(int),  /* size of same */
2804            ims,                                  /* the current ims flags */            rlevel);                              /* function recursion level */
           rlevel,                               /* function recursion level */  
           recursing);                           /* pass on regex recursion */  
2805    
2806          if (rc >= 0)          if (rc >= 0)
2807            {            {
# Line 2422  for (;;) Line 2811  for (;;)
2811    
2812            do { end_subpattern += GET(end_subpattern, 1); }            do { end_subpattern += GET(end_subpattern, 1); }
2813              while (*end_subpattern == OP_ALT);              while (*end_subpattern == OP_ALT);
2814            next_state_offset = end_subpattern - start_code + LINK_SIZE + 1;            next_state_offset =
2815                (int)(end_subpattern - start_code + LINK_SIZE + 1);
2816    
2817            /* If the end of this subpattern is KETRMAX or KETRMIN, we must            /* If the end of this subpattern is KETRMAX or KETRMIN, we must
2818            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 2430  for (;;) Line 2820  for (;;)
2820    
2821            repeat_state_offset = (*end_subpattern == OP_KETRMAX ||            repeat_state_offset = (*end_subpattern == OP_KETRMAX ||
2822                                   *end_subpattern == OP_KETRMIN)?                                   *end_subpattern == OP_KETRMIN)?
2823              end_subpattern - start_code - GET(end_subpattern, 1) : -1;              (int)(end_subpattern - start_code - GET(end_subpattern, 1)) : -1;
2824    
2825            /* 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
2826            current character pointer. This is important so that the duplicate            current character pointer. This is important so that the duplicate
# Line 2445  for (;;) Line 2835  for (;;)
2835            /* Optimization: if there are no more active states, and there            /* Optimization: if there are no more active states, and there
2836            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
2837            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
2838            into action when the end of the substring is reached. */            into action when the end of the matched substring is reached. */
2839    
2840            else if (i + 1 >= active_count && new_count == 0)            else if (i + 1 >= active_count && new_count == 0)
2841              {              {
# Line 2475  for (;;) Line 2865  for (;;)
2865              if (repeat_state_offset >= 0)              if (repeat_state_offset >= 0)
2866                { ADD_NEW_DATA(-repeat_state_offset, 0, (charcount - 1)); }                { ADD_NEW_DATA(-repeat_state_offset, 0, (charcount - 1)); }
2867              }              }
   
2868            }            }
2869          else if (rc != PCRE_ERROR_NOMATCH) return rc;          else if (rc != PCRE_ERROR_NOMATCH) return rc;
2870          }          }
# Line 2494  for (;;) Line 2883  for (;;)
2883          cb.callout_number   = code[1];          cb.callout_number   = code[1];
2884          cb.offset_vector    = offsets;          cb.offset_vector    = offsets;
2885          cb.subject          = (PCRE_SPTR)start_subject;          cb.subject          = (PCRE_SPTR)start_subject;
2886          cb.subject_length   = end_subject - start_subject;          cb.subject_length   = (int)(end_subject - start_subject);
2887          cb.start_match      = current_subject - start_subject;          cb.start_match      = (int)(current_subject - start_subject);
2888          cb.current_position = ptr - start_subject;          cb.current_position = (int)(ptr - start_subject);
2889          cb.pattern_position = GET(code, 2);          cb.pattern_position = GET(code, 2);
2890          cb.next_item_length = GET(code, 2 + LINK_SIZE);          cb.next_item_length = GET(code, 2 + LINK_SIZE);
2891          cb.capture_top      = 1;          cb.capture_top      = 1;
# Line 2521  for (;;) Line 2910  for (;;)
2910    /* We have finished the processing at the current subject character. If no    /* We have finished the processing at the current subject character. If no
2911    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
2912    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
2913    matching has been requested, check for appropriate conditions. The "forced_    matching has been requested, check for appropriate conditions.
2914    fail" variable counts the number of (*F) encountered for the character. If it  
2915    is equal to the original active_count (saved in workspace[1]) it means that    The "forced_ fail" variable counts the number of (*F) encountered for the
2916    (*F) was found on every active state. In this case we don't want to give a    character. If it is equal to the original active_count (saved in
2917    partial match. */    workspace[1]) it means that (*F) was found on every active state. In this
2918      case we don't want to give a partial match.
2919    
2920      The "could_continue" variable is true if a state could have continued but
2921      for the fact that the end of the subject was reached. */
2922    
2923    if (new_count <= 0)    if (new_count <= 0)
2924      {      {
2925      if (rlevel == 1 &&                               /* Top level, and */      if (rlevel == 1 &&                               /* Top level, and */
2926          reached_end != workspace[1] &&               /* Not all reached end */          could_continue &&                            /* Some could go on */
2927          forced_fail != workspace[1] &&               /* Not all forced fail & */          forced_fail != workspace[1] &&               /* Not all forced fail & */
2928          (                                            /* either... */          (                                            /* either... */
2929          (md->moptions & PCRE_PARTIAL_HARD) != 0      /* Hard partial */          (md->moptions & PCRE_PARTIAL_HARD) != 0      /* Hard partial */
# Line 2538  for (;;) Line 2931  for (;;)
2931          ((md->moptions & PCRE_PARTIAL_SOFT) != 0 &&  /* Soft partial and */          ((md->moptions & PCRE_PARTIAL_SOFT) != 0 &&  /* Soft partial and */
2932           match_count < 0)                            /* no matches */           match_count < 0)                            /* no matches */
2933          ) &&                                         /* And... */          ) &&                                         /* And... */
2934          ptr >= end_subject &&                     /* Reached end of subject */          ptr >= end_subject &&                  /* Reached end of subject */
2935          ptr > current_subject)                    /* Matched non-empty string */          ptr > md->start_used_ptr)              /* Inspected non-empty string */
2936        {        {
2937        if (offsetcount >= 2)        if (offsetcount >= 2)
2938          {          {
2939          offsets[0] = md->start_used_ptr - start_subject;          offsets[0] = (int)(md->start_used_ptr - start_subject);
2940          offsets[1] = end_subject - start_subject;          offsets[1] = (int)(end_subject - start_subject);
2941          }          }
2942        match_count = PCRE_ERROR_PARTIAL;        match_count = PCRE_ERROR_PARTIAL;
2943        }        }
# Line 2629  if (re == NULL || subject == NULL || wor Line 3022  if (re == NULL || subject == NULL || wor
3022     (offsets == NULL && offsetcount > 0)) return PCRE_ERROR_NULL;     (offsets == NULL && offsetcount > 0)) return PCRE_ERROR_NULL;
3023  if (offsetcount < 0) return PCRE_ERROR_BADCOUNT;  if (offsetcount < 0) return PCRE_ERROR_BADCOUNT;
3024  if (wscount < 20) return PCRE_ERROR_DFA_WSSIZE;  if (wscount < 20) return PCRE_ERROR_DFA_WSSIZE;
3025    if (start_offset < 0 || start_offset > length) return PCRE_ERROR_BADOFFSET;
3026    
3027  /* 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
3028  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 2747  back the character offset. */ Line 3141  back the character offset. */
3141  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
3142  if (utf8 && (options & PCRE_NO_UTF8_CHECK) == 0)  if (utf8 && (options & PCRE_NO_UTF8_CHECK) == 0)
3143    {    {
3144    if (_pcre_valid_utf8((uschar *)subject, length) >= 0)    int erroroffset;
3145      return PCRE_ERROR_BADUTF8;    int errorcode = _pcre_valid_utf8((uschar *)subject, length, &erroroffset);
3146    if (start_offset > 0 && start_offset < length)    if (errorcode != 0)
3147      {      {
3148      int tb = ((uschar *)subject)[start_offset];      if (offsetcount >= 2)
     if (tb > 127)  
3149        {        {
3150        tb &= 0xc0;        offsets[0] = erroroffset;
3151        if (tb != 0 && tb != 0xc0) return PCRE_ERROR_BADUTF8_OFFSET;        offsets[1] = errorcode;
3152        }        }
3153      }      return (errorcode <= PCRE_UTF8_ERR5 && (options & PCRE_PARTIAL_HARD) != 0)?
3154          PCRE_ERROR_SHORTUTF8 : PCRE_ERROR_BADUTF8;
3155        }
3156      if (start_offset > 0 && start_offset < length &&
3157            (((USPTR)subject)[start_offset] & 0xc0) == 0x80)
3158        return PCRE_ERROR_BADUTF8_OFFSET;
3159    }    }
3160  #endif  #endif
3161    
# Line 2790  if (!anchored) Line 3188  if (!anchored)
3188      }      }
3189    else    else
3190      {      {
3191      if (startline && study != NULL &&      if (!startline && study != NULL &&
3192           (study->options & PCRE_STUDY_MAPPED) != 0)           (study->flags & PCRE_STUDY_MAPPED) != 0)
3193        start_bits = study->start_bits;        start_bits = study->start_bits;
3194      }      }
3195    }    }
# Line 2842  for (;;) Line 3240  for (;;)
3240        }        }
3241    
3242      /* There are some optimizations that avoid running the match if a known      /* There are some optimizations that avoid running the match if a known
3243      starting point is not found, or if a known later character is not present.      starting point is not found. However, there is an option that disables
3244      However, there is an option that disables these, for testing and for      these, for testing and for ensuring that all callouts do actually occur.
3245      ensuring that all callouts do actually occur. */      The option can be set in the regex by (*NO_START_OPT) or passed in
3246        match-time options. */
3247    
3248      if ((options & PCRE_NO_START_OPTIMIZE) == 0)      if (((options | re->options) & PCRE_NO_START_OPTIMIZE) == 0)
3249        {        {
   
3250        /* Advance to a known first byte. */        /* Advance to a known first byte. */
3251    
3252        if (first_byte >= 0)        if (first_byte >= 0)
# Line 2905  for (;;) Line 3303  for (;;)
3303          while (current_subject < end_subject)          while (current_subject < end_subject)
3304            {            {
3305            register unsigned int c = *current_subject;            register unsigned int c = *current_subject;
3306            if ((start_bits[c/8] & (1 << (c&7))) == 0) current_subject++;            if ((start_bits[c/8] & (1 << (c&7))) == 0)
3307              else break;              {
3308                current_subject++;
3309    #ifdef SUPPORT_UTF8
3310                if (utf8)
3311                  while(current_subject < end_subject &&
3312                        (*current_subject & 0xc0) == 0x80) current_subject++;
3313    #endif
3314                }
3315              else break;
3316            }            }
3317          }          }
3318        }        }
# Line 2914  for (;;) Line 3320  for (;;)
3320      /* Restore fudged end_subject */      /* Restore fudged end_subject */
3321    
3322      end_subject = save_end_subject;      end_subject = save_end_subject;
     }  
3323    
3324    /* If req_byte is set, we know that that character must appear in the subject      /* The following two optimizations are disabled for partial matching or if
3325    for the match to succeed. If the first character is set, req_byte must be      disabling is explicitly requested (and of course, by the test above, this
3326    later in the subject; otherwise the test starts at the match point. This      code is not obeyed when restarting after a partial match). */
   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, and can  
   also be explicitly deactivated. Furthermore, we have to disable when  
   restarting after a partial match, because the required character may have  
   already been matched. */  
   
   if ((options & PCRE_NO_START_OPTIMIZE) == 0 &&  
       req_byte >= 0 &&  
       end_subject - current_subject < REQ_BYTE_MAX &&  
       (options & (PCRE_PARTIAL_HARD|PCRE_PARTIAL_SOFT|PCRE_DFA_RESTART)) == 0)  
     {  
     register const uschar *p = current_subject + ((first_byte >= 0)? 1 : 0);  
   
     /* We don't need to repeat the search if we haven't yet reached the  
     place we found it at last time. */  
3327    
3328      if (p > req_byte_ptr)      if ((options & PCRE_NO_START_OPTIMIZE) == 0 &&
3329            (options & (PCRE_PARTIAL_HARD|PCRE_PARTIAL_SOFT)) == 0)
3330        {        {
3331        if (req_byte_caseless)        /* If the pattern was studied, a minimum subject length may be set. This
3332          {        is a lower bound; no actual string of that length may actually match the
3333          while (p < end_subject)        pattern. Although the value is, strictly, in characters, we treat it as
3334            {        bytes to avoid spending too much time in this optimization. */
3335            register int pp = *p++;  
3336            if (pp == req_byte || pp == req_byte2) { p--; break; }        if (study != NULL && (study->flags & PCRE_STUDY_MINLEN) != 0 &&
3337            }            (pcre_uint32)(end_subject - current_subject) < study->minlength)
3338          }          return PCRE_ERROR_NOMATCH;
3339        else  
3340          /* If req_byte is set, we know that that character must appear in the
3341          subject for the match to succeed. If the first character is set, req_byte
3342          must be later in the subject; otherwise the test starts at the match
3343          point. This optimization can save a huge amount of work in patterns with
3344          nested unlimited repeats that aren't going to match. Writing separate
3345          code for cased/caseless versions makes it go faster, as does using an
3346          autoincrement and backing off on a match.
3347    
3348          HOWEVER: when the subject string is very, very long, searching to its end
3349          can take a long time, and give bad performance on quite ordinary
3350          patterns. This showed up when somebody was matching /^C/ on a 32-megabyte
3351          string... so we don't do this when the string is sufficiently long. */
3352    
3353          if (req_byte >= 0 && end_subject - current_subject < REQ_BYTE_MAX)
3354          {          {
3355          while (p < end_subject)          register const uschar *p = current_subject + ((first_byte >= 0)? 1 : 0);
3356    
3357            /* We don't need to repeat the search if we haven't yet reached the
3358            place we found it at last time. */
3359    
3360            if (p > req_byte_ptr)
3361            {            {
3362            if (*p++ == req_byte) { p--; break; }            if (req_byte_caseless)
3363            }              {
3364          }              while (p < end_subject)
3365                  {
3366                  register int pp = *p++;
3367                  if (pp == req_byte || pp == req_byte2) { p--; break; }
3368                  }
3369                }
3370              else
3371                {
3372                while (p < end_subject)
3373                  {
3374                  if (*p++ == req_byte) { p--; break; }
3375                  }
3376                }
3377    
3378        /* If we can't find the required character, break the matching loop,            /* If we can't find the required character, break the matching loop,
3379        which will cause a return or PCRE_ERROR_NOMATCH. */            which will cause a return or PCRE_ERROR_NOMATCH. */
3380    
3381        if (p >= end_subject) break;            if (p >= end_subject) break;
3382    
3383        /* If we have found the required character, save the point where we            /* If we have found the required character, save the point where we
3384        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
3385        the start hasn't passed this character yet. */            the start hasn't passed this character yet. */
3386    
3387        req_byte_ptr = p;            req_byte_ptr = p;
3388              }
3389            }
3390        }        }
3391      }      }   /* End of optimizations that are done when not restarting */
3392    
3393    /* OK, now we can do the business */    /* OK, now we can do the business */
3394    
3395    md->start_used_ptr = current_subject;    md->start_used_ptr = current_subject;
3396      md->recursive = NULL;
3397    
3398    rc = internal_dfa_exec(    rc = internal_dfa_exec(
3399      md,                                /* fixed match data */      md,                                /* fixed match data */
3400      md->start_code,                    /* this subexpression's code */      md->start_code,                    /* this subexpression's code */
# Line 2988  for (;;) Line 3404  for (;;)
3404      offsetcount,                       /* size of same */      offsetcount,                       /* size of same */
3405      workspace,                         /* workspace vector */      workspace,                         /* workspace vector */
3406      wscount,                           /* size of same */      wscount,                           /* size of same */
3407      re->options & (PCRE_CASELESS|PCRE_MULTILINE|PCRE_DOTALL), /* ims flags */      0);                                /* function recurse level */
     0,                                 /* function recurse level */  
     0);                                /* regex recurse level */  
3408    
3409    /* Anything other than "no match" means we are done, always; otherwise, carry    /* Anything other than "no match" means we are done, always; otherwise, carry
3410    on only if not anchored. */    on only if not anchored. */

Legend:
Removed from v.442  
changed lines
  Added in v.642

  ViewVC Help
Powered by ViewVC 1.1.5