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

Diff of /code/trunk/pcre_study.c

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

revision 467 by ph10, Mon Oct 19 11:43:18 2009 UTC revision 729 by ph10, Mon Oct 10 16:02:20 2011 UTC
# Line 6  Line 6 
6  and semantics are as close as possible to those of the Perl 5 language.  and semantics are as close as possible to those of the Perl 5 language.
7    
8                         Written by Philip Hazel                         Written by Philip Hazel
9             Copyright (c) 1997-2009 University of Cambridge             Copyright (c) 1997-2010 University of Cambridge
10    
11  -----------------------------------------------------------------------------  -----------------------------------------------------------------------------
12  Redistribution and use in source and binary forms, with or without  Redistribution and use in source and binary forms, with or without
# Line 48  supporting functions. */ Line 48  supporting functions. */
48    
49  #include "pcre_internal.h"  #include "pcre_internal.h"
50    
51    #define SET_BIT(c) start_bits[c/8] |= (1 << (c&7))
52    
53  /* Returns from set_start_bits() */  /* Returns from set_start_bits() */
54    
55  enum { SSB_FAIL, SSB_DONE, SSB_CONTINUE };  enum { SSB_FAIL, SSB_DONE, SSB_CONTINUE, SSB_UNKNOWN };
56    
57    
58    
# Line 65  string of that length that matches. In U Line 66  string of that length that matches. In U
66  rather than bytes.  rather than bytes.
67    
68  Arguments:  Arguments:
69    code       pointer to start of group (the bracket)    code            pointer to start of group (the bracket)
70    startcode  pointer to start of the whole pattern    startcode       pointer to start of the whole pattern
71    options    the compiling options    options         the compiling options
72      int             RECURSE depth
73    
74  Returns:   the minimum length  Returns:   the minimum length
75             -1 if \C was encountered             -1 if \C in UTF-8 mode or (*ACCEPT) was encountered
76             -2 internal error (missing capturing bracket)             -2 internal error (missing capturing bracket)
77               -3 internal error (opcode not listed)
78  */  */
79    
80  static int  static int
81  find_minlength(const uschar *code, const uschar *startcode, int options)  find_minlength(const uschar *code, const uschar *startcode, int options,
82      int recurse_depth)
83  {  {
84  int length = -1;  int length = -1;
85  BOOL utf8 = (options & PCRE_UTF8) != 0;  BOOL utf8 = (options & PCRE_UTF8) != 0;
# Line 83  BOOL had_recurse = FALSE; Line 87  BOOL had_recurse = FALSE;
87  register int branchlength = 0;  register int branchlength = 0;
88  register uschar *cc = (uschar *)code + 1 + LINK_SIZE;  register uschar *cc = (uschar *)code + 1 + LINK_SIZE;
89    
90  if (*code == OP_CBRA || *code == OP_SCBRA) cc += 2;  if (*code == OP_CBRA || *code == OP_SCBRA ||
91        *code == OP_CBRAPOS || *code == OP_SCBRAPOS) cc += 2;
92    
93  /* Scan along the opcodes for this branch. If we get to the end of the  /* Scan along the opcodes for this branch. If we get to the end of the
94  branch, check the length against that of the other branches. */  branch, check the length against that of the other branches. */
# Line 96  for (;;) Line 101  for (;;)
101    
102    switch (op)    switch (op)
103      {      {
104        case OP_COND:
105        case OP_SCOND:
106    
107        /* If there is only one branch in a condition, the implied branch has zero
108        length, so we don't add anything. This covers the DEFINE "condition"
109        automatically. */
110    
111        cs = cc + GET(cc, 1);
112        if (*cs != OP_ALT)
113          {
114          cc = cs + 1 + LINK_SIZE;
115          break;
116          }
117    
118        /* Otherwise we can fall through and treat it the same as any other
119        subpattern. */
120    
121      case OP_CBRA:      case OP_CBRA:
122      case OP_SCBRA:      case OP_SCBRA:
123      case OP_BRA:      case OP_BRA:
124      case OP_SBRA:      case OP_SBRA:
125        case OP_CBRAPOS:
126        case OP_SCBRAPOS:
127        case OP_BRAPOS:
128        case OP_SBRAPOS:
129      case OP_ONCE:      case OP_ONCE:
130      case OP_COND:      case OP_ONCE_NC:
131      case OP_SCOND:      d = find_minlength(cc, startcode, options, recurse_depth);
     d = find_minlength(cc, startcode, options);  
132      if (d < 0) return d;      if (d < 0) return d;
133      branchlength += d;      branchlength += d;
134      do cc += GET(cc, 1); while (*cc == OP_ALT);      do cc += GET(cc, 1); while (*cc == OP_ALT);
135      cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;
136      break;      break;
137    
138        /* ACCEPT makes things far too complicated; we have to give up. */
139    
140        case OP_ACCEPT:
141        case OP_ASSERT_ACCEPT:
142        return -1;
143    
144      /* Reached end of a branch; if it's a ket it is the end of a nested      /* Reached end of a branch; if it's a ket it is the end of a nested
145      call. If it's ALT it is an alternation in a nested call. If it is      call. If it's ALT it is an alternation in a nested call. If it is END it's
146      END it's the end of the outer call. All can be handled by the same code. */      the end of the outer call. All can be handled by the same code. If an
147        ACCEPT was previously encountered, use the length that was in force at that
148        time, and pass back the shortest ACCEPT length. */
149    
150      case OP_ALT:      case OP_ALT:
151      case OP_KET:      case OP_KET:
152      case OP_KETRMAX:      case OP_KETRMAX:
153      case OP_KETRMIN:      case OP_KETRMIN:
154        case OP_KETRPOS:
155      case OP_END:      case OP_END:
156      if (length < 0 || (!had_recurse && branchlength < length))      if (length < 0 || (!had_recurse && branchlength < length))
157        length = branchlength;        length = branchlength;
158      if (*cc != OP_ALT) return length;      if (op != OP_ALT) return length;
159      cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;
160      branchlength = 0;      branchlength = 0;
161      had_recurse = FALSE;      had_recurse = FALSE;
# Line 144  for (;;) Line 178  for (;;)
178      case OP_RREF:      case OP_RREF:
179      case OP_NRREF:      case OP_NRREF:
180      case OP_DEF:      case OP_DEF:
     case OP_OPT:  
181      case OP_CALLOUT:      case OP_CALLOUT:
182      case OP_SOD:      case OP_SOD:
183      case OP_SOM:      case OP_SOM:
184      case OP_EOD:      case OP_EOD:
185      case OP_EODN:      case OP_EODN:
186      case OP_CIRC:      case OP_CIRC:
187        case OP_CIRCM:
188      case OP_DOLL:      case OP_DOLL:
189        case OP_DOLLM:
190      case OP_NOT_WORD_BOUNDARY:      case OP_NOT_WORD_BOUNDARY:
191      case OP_WORD_BOUNDARY:      case OP_WORD_BOUNDARY:
192      cc += _pcre_OP_lengths[*cc];      cc += _pcre_OP_lengths[*cc];
# Line 161  for (;;) Line 196  for (;;)
196    
197      case OP_BRAZERO:      case OP_BRAZERO:
198      case OP_BRAMINZERO:      case OP_BRAMINZERO:
199        case OP_BRAPOSZERO:
200      case OP_SKIPZERO:      case OP_SKIPZERO:
201      cc += _pcre_OP_lengths[*cc];      cc += _pcre_OP_lengths[*cc];
202      do cc += GET(cc, 1); while (*cc == OP_ALT);      do cc += GET(cc, 1); while (*cc == OP_ALT);
# Line 170  for (;;) Line 206  for (;;)
206      /* Handle literal characters and + repetitions */      /* Handle literal characters and + repetitions */
207    
208      case OP_CHAR:      case OP_CHAR:
209      case OP_CHARNC:      case OP_CHARI:
210      case OP_NOT:      case OP_NOT:
211        case OP_NOTI:
212      case OP_PLUS:      case OP_PLUS:
213        case OP_PLUSI:
214      case OP_MINPLUS:      case OP_MINPLUS:
215        case OP_MINPLUSI:
216      case OP_POSPLUS:      case OP_POSPLUS:
217        case OP_POSPLUSI:
218      case OP_NOTPLUS:      case OP_NOTPLUS:
219        case OP_NOTPLUSI:
220      case OP_NOTMINPLUS:      case OP_NOTMINPLUS:
221        case OP_NOTMINPLUSI:
222      case OP_NOTPOSPLUS:      case OP_NOTPOSPLUS:
223        case OP_NOTPOSPLUSI:
224      branchlength++;      branchlength++;
225      cc += 2;      cc += 2;
226  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
# Line 196  for (;;) Line 239  for (;;)
239      need to skip over a multibyte character in UTF8 mode.  */      need to skip over a multibyte character in UTF8 mode.  */
240    
241      case OP_EXACT:      case OP_EXACT:
242        case OP_EXACTI:
243      case OP_NOTEXACT:      case OP_NOTEXACT:
244        case OP_NOTEXACTI:
245      branchlength += GET2(cc,1);      branchlength += GET2(cc,1);
246      cc += 4;      cc += 4;
247  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
# Line 233  for (;;) Line 278  for (;;)
278      cc++;      cc++;
279      break;      break;
280    
281      /* "Any newline" might match two characters */      /* "Any newline" might match two characters, but it also might match just
282        one. */
283    
284      case OP_ANYNL:      case OP_ANYNL:
285      branchlength += 2;      branchlength += 1;
286      cc++;      cc++;
287      break;      break;
288    
# Line 314  for (;;) Line 360  for (;;)
360      logic is that a recursion can only make sense if there is another      logic is that a recursion can only make sense if there is another
361      alternation that stops the recursing. That will provide the minimum length      alternation that stops the recursing. That will provide the minimum length
362      (when no recursion happens). A backreference within the group that it is      (when no recursion happens). A backreference within the group that it is
363      referencing behaves in the same way.      referencing behaves in the same way.
364    
365      If PCRE_JAVASCRIPT_COMPAT is set, a backreference to an unset bracket      If PCRE_JAVASCRIPT_COMPAT is set, a backreference to an unset bracket
366      matches an empty string (by default it causes a matching failure), so in      matches an empty string (by default it causes a matching failure), so in
367      that case we must set the minimum length to zero. */      that case we must set the minimum length to zero. */
368    
369      case OP_REF:      case OP_REF:
370        case OP_REFI:
371      if ((options & PCRE_JAVASCRIPT_COMPAT) == 0)      if ((options & PCRE_JAVASCRIPT_COMPAT) == 0)
372        {        {
373        ce = cs = (uschar *)_pcre_find_bracket(startcode, utf8, GET2(cc, 1));        ce = cs = (uschar *)_pcre_find_bracket(startcode, utf8, GET2(cc, 1));
374        if (cs == NULL) return -2;        if (cs == NULL) return -2;
375        do ce += GET(ce, 1); while (*ce == OP_ALT);        do ce += GET(ce, 1); while (*ce == OP_ALT);
# Line 331  for (;;) Line 378  for (;;)
378          d = 0;          d = 0;
379          had_recurse = TRUE;          had_recurse = TRUE;
380          }          }
381        else d = find_minlength(cs, startcode, options);        else
382            {
383            d = find_minlength(cs, startcode, options, recurse_depth);
384            }
385        }        }
386      else d = 0;      else d = 0;
387      cc += 3;      cc += 3;
388    
389      /* Handle repeated back references */      /* Handle repeated back references */
# Line 348  for (;;) Line 398  for (;;)
398        cc++;        cc++;
399        break;        break;
400    
401          case OP_CRPLUS:
402          case OP_CRMINPLUS:
403          min = 1;
404          cc++;
405          break;
406    
407        case OP_CRRANGE:        case OP_CRRANGE:
408        case OP_CRMINRANGE:        case OP_CRMINRANGE:
409        min = GET2(cc, 1);        min = GET2(cc, 1);
# Line 362  for (;;) Line 418  for (;;)
418      branchlength += min * d;      branchlength += min * d;
419      break;      break;
420    
421        /* We can easily detect direct recursion, but not mutual recursion. This is
422        caught by a recursion depth count. */
423    
424      case OP_RECURSE:      case OP_RECURSE:
425      cs = ce = (uschar *)startcode + GET(cc, 1);      cs = ce = (uschar *)startcode + GET(cc, 1);
426      if (cs == NULL) return -2;      if (cs == NULL) return -2;
427      do ce += GET(ce, 1); while (*ce == OP_ALT);      do ce += GET(ce, 1); while (*ce == OP_ALT);
428      if (cc > cs && cc < ce)      if ((cc > cs && cc < ce) || recurse_depth > 10)
429        had_recurse = TRUE;        had_recurse = TRUE;
430      else      else
431        branchlength += find_minlength(cs, startcode, options);        {
432          branchlength += find_minlength(cs, startcode, options, recurse_depth + 1);
433          }
434      cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;
435      break;      break;
436    
437      /* Anything else does not or need not match a character. We can get the      /* Anything else does not or need not match a character. We can get the
438      item's length from the table, but for those that can match zero occurrences      item's length from the table, but for those that can match zero occurrences
439      of a character, we must take special action for UTF-8 characters. */      of a character, we must take special action for UTF-8 characters. As it
440        happens, the "NOT" versions of these opcodes are used at present only for
441        ASCII characters, so they could be omitted from this list. However, in
442        future that may change, so we include them here so as not to leave a
443        gotcha for a future maintainer. */
444    
445      case OP_UPTO:      case OP_UPTO:
446        case OP_UPTOI:
447      case OP_NOTUPTO:      case OP_NOTUPTO:
448        case OP_NOTUPTOI:
449      case OP_MINUPTO:      case OP_MINUPTO:
450        case OP_MINUPTOI:
451      case OP_NOTMINUPTO:      case OP_NOTMINUPTO:
452        case OP_NOTMINUPTOI:
453      case OP_POSUPTO:      case OP_POSUPTO:
454        case OP_POSUPTOI:
455        case OP_NOTPOSUPTO:
456        case OP_NOTPOSUPTOI:
457    
458      case OP_STAR:      case OP_STAR:
459        case OP_STARI:
460        case OP_NOTSTAR:
461        case OP_NOTSTARI:
462      case OP_MINSTAR:      case OP_MINSTAR:
463        case OP_MINSTARI:
464      case OP_NOTMINSTAR:      case OP_NOTMINSTAR:
465        case OP_NOTMINSTARI:
466      case OP_POSSTAR:      case OP_POSSTAR:
467        case OP_POSSTARI:
468      case OP_NOTPOSSTAR:      case OP_NOTPOSSTAR:
469        case OP_NOTPOSSTARI:
470    
471      case OP_QUERY:      case OP_QUERY:
472        case OP_QUERYI:
473        case OP_NOTQUERY:
474        case OP_NOTQUERYI:
475      case OP_MINQUERY:      case OP_MINQUERY:
476        case OP_MINQUERYI:
477      case OP_NOTMINQUERY:      case OP_NOTMINQUERY:
478        case OP_NOTMINQUERYI:
479      case OP_POSQUERY:      case OP_POSQUERY:
480        case OP_POSQUERYI:
481      case OP_NOTPOSQUERY:      case OP_NOTPOSQUERY:
482        case OP_NOTPOSQUERYI:
483    
484      cc += _pcre_OP_lengths[op];      cc += _pcre_OP_lengths[op];
485  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
486      if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f];      if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f];
487  #endif  #endif
488      break;      break;
489    
490      /* For the record, these are the opcodes that are matched by "default":      /* Skip these, but we need to add in the name length. */
     OP_ACCEPT, OP_CLOSE, OP_COMMIT, OP_FAIL, OP_PRUNE, OP_SET_SOM, OP_SKIP,  
     OP_THEN. */  
491    
492      default:      case OP_MARK:
493        case OP_PRUNE_ARG:
494        case OP_SKIP_ARG:
495        case OP_THEN_ARG:
496        cc += _pcre_OP_lengths[op] + cc[1];
497        break;
498    
499        /* The remaining opcodes are just skipped over. */
500    
501        case OP_CLOSE:
502        case OP_COMMIT:
503        case OP_FAIL:
504        case OP_PRUNE:
505        case OP_SET_SOM:
506        case OP_SKIP:
507        case OP_THEN:
508      cc += _pcre_OP_lengths[op];      cc += _pcre_OP_lengths[op];
509      break;      break;
510    
511        /* This should not occur: we list all opcodes explicitly so that when
512        new ones get added they are properly considered. */
513    
514        default:
515        return -3;
516      }      }
517    }    }
518  /* Control never gets here */  /* Control never gets here */
# Line 416  for (;;) Line 524  for (;;)
524  *      Set a bit and maybe its alternate case    *  *      Set a bit and maybe its alternate case    *
525  *************************************************/  *************************************************/
526    
527  /* Given a character, set its bit in the table, and also the bit for the other  /* Given a character, set its first byte's bit in the table, and also the
528  version of a letter if we are caseless.  corresponding bit for the other version of a letter if we are caseless. In
529    UTF-8 mode, for characters greater than 127, we can only do the caseless thing
530    when Unicode property support is available.
531    
532  Arguments:  Arguments:
533    start_bits    points to the bit map    start_bits    points to the bit map
534    c             is the character    p             points to the character
535    caseless      the caseless flag    caseless      the caseless flag
536    cd            the block with char table pointers    cd            the block with char table pointers
537      utf8          TRUE for UTF-8 mode
538    
539  Returns:        nothing  Returns:        pointer after the character
540    */
541    
542    static const uschar *
543    set_table_bit(uschar *start_bits, const uschar *p, BOOL caseless,
544      compile_data *cd, BOOL utf8)
545    {
546    unsigned int c = *p;
547    
548    SET_BIT(c);
549    
550    #ifdef SUPPORT_UTF8
551    if (utf8 && c > 127)
552      {
553      GETCHARINC(c, p);
554    #ifdef SUPPORT_UCP
555      if (caseless)
556        {
557        uschar buff[8];
558        c = UCD_OTHERCASE(c);
559        (void)_pcre_ord2utf8(c, buff);
560        SET_BIT(buff[0]);
561        }
562    #endif
563      return p;
564      }
565    #endif
566    
567    /* Not UTF-8 mode, or character is less than 127. */
568    
569    if (caseless && (cd->ctypes[c] & ctype_letter) != 0) SET_BIT(cd->fcc[c]);
570    return p + 1;
571    }
572    
573    
574    
575    /*************************************************
576    *     Set bits for a positive character type     *
577    *************************************************/
578    
579    /* This function sets starting bits for a character type. In UTF-8 mode, we can
580    only do a direct setting for bytes less than 128, as otherwise there can be
581    confusion with bytes in the middle of UTF-8 characters. In a "traditional"
582    environment, the tables will only recognize ASCII characters anyway, but in at
583    least one Windows environment, some higher bytes bits were set in the tables.
584    So we deal with that case by considering the UTF-8 encoding.
585    
586    Arguments:
587      start_bits     the starting bitmap
588      cbit type      the type of character wanted
589      table_limit    32 for non-UTF-8; 16 for UTF-8
590      cd             the block with char table pointers
591    
592    Returns:         nothing
593  */  */
594    
595  static void  static void
596  set_bit(uschar *start_bits, unsigned int c, BOOL caseless, compile_data *cd)  set_type_bits(uschar *start_bits, int cbit_type, int table_limit,
597      compile_data *cd)
598  {  {
599  start_bits[c/8] |= (1 << (c&7));  register int c;
600  if (caseless && (cd->ctypes[c] & ctype_letter) != 0)  for (c = 0; c < table_limit; c++) start_bits[c] |= cd->cbits[c+cbit_type];
601    start_bits[cd->fcc[c]/8] |= (1 << (cd->fcc[c]&7));  if (table_limit == 32) return;
602    for (c = 128; c < 256; c++)
603      {
604      if ((cd->cbits[c/8] & (1 << (c&7))) != 0)
605        {
606        uschar buff[8];
607        (void)_pcre_ord2utf8(c, buff);
608        SET_BIT(buff[0]);
609        }
610      }
611    }
612    
613    
614    /*************************************************
615    *     Set bits for a negative character type     *
616    *************************************************/
617    
618    /* This function sets starting bits for a negative character type such as \D.
619    In UTF-8 mode, we can only do a direct setting for bytes less than 128, as
620    otherwise there can be confusion with bytes in the middle of UTF-8 characters.
621    Unlike in the positive case, where we can set appropriate starting bits for
622    specific high-valued UTF-8 characters, in this case we have to set the bits for
623    all high-valued characters. The lowest is 0xc2, but we overkill by starting at
624    0xc0 (192) for simplicity.
625    
626    Arguments:
627      start_bits     the starting bitmap
628      cbit type      the type of character wanted
629      table_limit    32 for non-UTF-8; 16 for UTF-8
630      cd             the block with char table pointers
631    
632    Returns:         nothing
633    */
634    
635    static void
636    set_nottype_bits(uschar *start_bits, int cbit_type, int table_limit,
637      compile_data *cd)
638    {
639    register int c;
640    for (c = 0; c < table_limit; c++) start_bits[c] |= ~cd->cbits[c+cbit_type];
641    if (table_limit != 32) for (c = 24; c < 32; c++) start_bits[c] = 0xff;
642  }  }
643    
644    
# Line 453  function fails unless the result is SSB_ Line 658  function fails unless the result is SSB_
658  Arguments:  Arguments:
659    code         points to an expression    code         points to an expression
660    start_bits   points to a 32-byte table, initialized to 0    start_bits   points to a 32-byte table, initialized to 0
   caseless     the current state of the caseless flag  
661    utf8         TRUE if in UTF-8 mode    utf8         TRUE if in UTF-8 mode
662    cd           the block with char table pointers    cd           the block with char table pointers
663    
664  Returns:       SSB_FAIL     => Failed to find any starting bytes  Returns:       SSB_FAIL     => Failed to find any starting bytes
665                 SSB_DONE     => Found mandatory starting bytes                 SSB_DONE     => Found mandatory starting bytes
666                 SSB_CONTINUE => Found optional starting bytes                 SSB_CONTINUE => Found optional starting bytes
667                   SSB_UNKNOWN  => Hit an unrecognized opcode
668  */  */
669    
670  static int  static int
671  set_start_bits(const uschar *code, uschar *start_bits, BOOL caseless,  set_start_bits(const uschar *code, uschar *start_bits, BOOL utf8,
672    BOOL utf8, compile_data *cd)    compile_data *cd)
673  {  {
674  register int c;  register int c;
675  int yield = SSB_DONE;  int yield = SSB_DONE;
676    int table_limit = utf8? 16:32;
677    
678  #if 0  #if 0
679  /* ========================================================================= */  /* ========================================================================= */
# Line 488  volatile int dummy; Line 694  volatile int dummy;
694    
695  do  do
696    {    {
   const uschar *tcode = code + (((int)*code == OP_CBRA)? 3:1) + LINK_SIZE;  
697    BOOL try_next = TRUE;    BOOL try_next = TRUE;
698      const uschar *tcode = code + 1 + LINK_SIZE;
699    
700      if (*code == OP_CBRA || *code == OP_SCBRA ||
701          *code == OP_CBRAPOS || *code == OP_SCBRAPOS) tcode += 2;
702    
703    while (try_next)    /* Loop for items in this branch */    while (try_next)    /* Loop for items in this branch */
704      {      {
705      int rc;      int rc;
706    
707      switch(*tcode)      switch(*tcode)
708        {        {
709        /* Fail if we reach something we don't understand */        /* If we reach something we don't understand, it means a new opcode has
710          been created that hasn't been added to this code. Hopefully this problem
711          will be discovered during testing. */
712    
713        default:        default:
714          return SSB_UNKNOWN;
715    
716          /* Fail for a valid opcode that implies no starting bits. */
717    
718          case OP_ACCEPT:
719          case OP_ASSERT_ACCEPT:
720          case OP_ALLANY:
721          case OP_ANY:
722          case OP_ANYBYTE:
723          case OP_CIRC:
724          case OP_CIRCM:
725          case OP_CLOSE:
726          case OP_COMMIT:
727          case OP_COND:
728          case OP_CREF:
729          case OP_DEF:
730          case OP_DOLL:
731          case OP_DOLLM:
732          case OP_END:
733          case OP_EOD:
734          case OP_EODN:
735          case OP_EXTUNI:
736          case OP_FAIL:
737          case OP_MARK:
738          case OP_NCREF:
739          case OP_NOT:
740          case OP_NOTEXACT:
741          case OP_NOTEXACTI:
742          case OP_NOTI:
743          case OP_NOTMINPLUS:
744          case OP_NOTMINPLUSI:
745          case OP_NOTMINQUERY:
746          case OP_NOTMINQUERYI:
747          case OP_NOTMINSTAR:
748          case OP_NOTMINSTARI:
749          case OP_NOTMINUPTO:
750          case OP_NOTMINUPTOI:
751          case OP_NOTPLUS:
752          case OP_NOTPLUSI:
753          case OP_NOTPOSPLUS:
754          case OP_NOTPOSPLUSI:
755          case OP_NOTPOSQUERY:
756          case OP_NOTPOSQUERYI:
757          case OP_NOTPOSSTAR:
758          case OP_NOTPOSSTARI:
759          case OP_NOTPOSUPTO:
760          case OP_NOTPOSUPTOI:
761          case OP_NOTPROP:
762          case OP_NOTQUERY:
763          case OP_NOTQUERYI:
764          case OP_NOTSTAR:
765          case OP_NOTSTARI:
766          case OP_NOTUPTO:
767          case OP_NOTUPTOI:
768          case OP_NOT_HSPACE:
769          case OP_NOT_VSPACE:
770          case OP_NRREF:
771          case OP_PROP:
772          case OP_PRUNE:
773          case OP_PRUNE_ARG:
774          case OP_RECURSE:
775          case OP_REF:
776          case OP_REFI:
777          case OP_REVERSE:
778          case OP_RREF:
779          case OP_SCOND:
780          case OP_SET_SOM:
781          case OP_SKIP:
782          case OP_SKIP_ARG:
783          case OP_SOD:
784          case OP_SOM:
785          case OP_THEN:
786          case OP_THEN_ARG:
787          case OP_XCLASS:
788        return SSB_FAIL;        return SSB_FAIL;
789    
790          /* We can ignore word boundary tests. */
791    
792          case OP_WORD_BOUNDARY:
793          case OP_NOT_WORD_BOUNDARY:
794          tcode++;
795          break;
796    
797        /* If we hit a bracket or a positive lookahead assertion, recurse to set        /* If we hit a bracket or a positive lookahead assertion, recurse to set
798        bits from within the subpattern. If it can't find anything, we have to        bits from within the subpattern. If it can't find anything, we have to
799        give up. If it finds some mandatory character(s), we are done for this        give up. If it finds some mandatory character(s), we are done for this
# Line 510  do Line 803  do
803        case OP_SBRA:        case OP_SBRA:
804        case OP_CBRA:        case OP_CBRA:
805        case OP_SCBRA:        case OP_SCBRA:
806          case OP_BRAPOS:
807          case OP_SBRAPOS:
808          case OP_CBRAPOS:
809          case OP_SCBRAPOS:
810        case OP_ONCE:        case OP_ONCE:
811          case OP_ONCE_NC:
812        case OP_ASSERT:        case OP_ASSERT:
813        rc = set_start_bits(tcode, start_bits, caseless, utf8, cd);        rc = set_start_bits(tcode, start_bits, utf8, cd);
814        if (rc == SSB_FAIL) return SSB_FAIL;        if (rc == SSB_FAIL || rc == SSB_UNKNOWN) return rc;
815        if (rc == SSB_DONE) try_next = FALSE; else        if (rc == SSB_DONE) try_next = FALSE; else
816          {          {
817          do tcode += GET(tcode, 1); while (*tcode == OP_ALT);          do tcode += GET(tcode, 1); while (*tcode == OP_ALT);
# Line 536  do Line 834  do
834        case OP_KET:        case OP_KET:
835        case OP_KETRMAX:        case OP_KETRMAX:
836        case OP_KETRMIN:        case OP_KETRMIN:
837          case OP_KETRPOS:
838        return SSB_CONTINUE;        return SSB_CONTINUE;
839    
840        /* Skip over callout */        /* Skip over callout */
# Line 553  do Line 852  do
852        tcode += 1 + LINK_SIZE;        tcode += 1 + LINK_SIZE;
853        break;        break;
854    
       /* Skip over an option setting, changing the caseless flag */  
   
       case OP_OPT:  
       caseless = (tcode[1] & PCRE_CASELESS) != 0;  
       tcode += 2;  
       break;  
   
855        /* BRAZERO does the bracket, but carries on. */        /* BRAZERO does the bracket, but carries on. */
856    
857        case OP_BRAZERO:        case OP_BRAZERO:
858        case OP_BRAMINZERO:        case OP_BRAMINZERO:
859        if (set_start_bits(++tcode, start_bits, caseless, utf8, cd) == SSB_FAIL)        case OP_BRAPOSZERO:
860          return SSB_FAIL;        rc = set_start_bits(++tcode, start_bits, utf8, cd);
861          if (rc == SSB_FAIL || rc == SSB_UNKNOWN) return rc;
862  /* =========================================================================  /* =========================================================================
863        See the comment at the head of this function concerning the next line,        See the comment at the head of this function concerning the next line,
864        which was an old fudge for the benefit of OS/2.        which was an old fudge for the benefit of OS/2.
# Line 591  do Line 884  do
884        case OP_QUERY:        case OP_QUERY:
885        case OP_MINQUERY:        case OP_MINQUERY:
886        case OP_POSQUERY:        case OP_POSQUERY:
887        set_bit(start_bits, tcode[1], caseless, cd);        tcode = set_table_bit(start_bits, tcode + 1, FALSE, cd, utf8);
888        tcode += 2;        break;
889  #ifdef SUPPORT_UTF8  
890        if (utf8 && tcode[-1] >= 0xc0)        case OP_STARI:
891          tcode += _pcre_utf8_table4[tcode[-1] & 0x3f];        case OP_MINSTARI:
892  #endif        case OP_POSSTARI:
893          case OP_QUERYI:
894          case OP_MINQUERYI:
895          case OP_POSQUERYI:
896          tcode = set_table_bit(start_bits, tcode + 1, TRUE, cd, utf8);
897        break;        break;
898    
899        /* Single-char upto sets the bit and tries the next */        /* Single-char upto sets the bit and tries the next */
# Line 604  do Line 901  do
901        case OP_UPTO:        case OP_UPTO:
902        case OP_MINUPTO:        case OP_MINUPTO:
903        case OP_POSUPTO:        case OP_POSUPTO:
904        set_bit(start_bits, tcode[3], caseless, cd);        tcode = set_table_bit(start_bits, tcode + 3, FALSE, cd, utf8);
905        tcode += 4;        break;
906  #ifdef SUPPORT_UTF8  
907        if (utf8 && tcode[-1] >= 0xc0)        case OP_UPTOI:
908          tcode += _pcre_utf8_table4[tcode[-1] & 0x3f];        case OP_MINUPTOI:
909  #endif        case OP_POSUPTOI:
910          tcode = set_table_bit(start_bits, tcode + 3, TRUE, cd, utf8);
911        break;        break;
912    
913        /* At least one single char sets the bit and stops */        /* At least one single char sets the bit and stops */
914    
915        case OP_EXACT:       /* Fall through */        case OP_EXACT:
916        tcode += 2;        tcode += 2;
917          /* Fall through */
918        case OP_CHAR:        case OP_CHAR:
       case OP_CHARNC:  
919        case OP_PLUS:        case OP_PLUS:
920        case OP_MINPLUS:        case OP_MINPLUS:
921        case OP_POSPLUS:        case OP_POSPLUS:
922        set_bit(start_bits, tcode[1], caseless, cd);        (void)set_table_bit(start_bits, tcode + 1, FALSE, cd, utf8);
923        try_next = FALSE;        try_next = FALSE;
924        break;        break;
925    
926        /* Single character type sets the bits and stops */        case OP_EXACTI:
927          tcode += 2;
928          /* Fall through */
929          case OP_CHARI:
930          case OP_PLUSI:
931          case OP_MINPLUSI:
932          case OP_POSPLUSI:
933          (void)set_table_bit(start_bits, tcode + 1, TRUE, cd, utf8);
934          try_next = FALSE;
935          break;
936    
937          /* Special spacing and line-terminating items. These recognize specific
938          lists of characters. The difference between VSPACE and ANYNL is that the
939          latter can match the two-character CRLF sequence, but that is not
940          relevant for finding the first character, so their code here is
941          identical. */
942    
943          case OP_HSPACE:
944          SET_BIT(0x09);
945          SET_BIT(0x20);
946          if (utf8)
947            {
948            SET_BIT(0xC2);  /* For U+00A0 */
949            SET_BIT(0xE1);  /* For U+1680, U+180E */
950            SET_BIT(0xE2);  /* For U+2000 - U+200A, U+202F, U+205F */
951            SET_BIT(0xE3);  /* For U+3000 */
952            }
953          else SET_BIT(0xA0);
954          try_next = FALSE;
955          break;
956    
957          case OP_ANYNL:
958          case OP_VSPACE:
959          SET_BIT(0x0A);
960          SET_BIT(0x0B);
961          SET_BIT(0x0C);
962          SET_BIT(0x0D);
963          if (utf8)
964            {
965            SET_BIT(0xC2);  /* For U+0085 */
966            SET_BIT(0xE2);  /* For U+2028, U+2029 */
967            }
968          else SET_BIT(0x85);
969          try_next = FALSE;
970          break;
971    
972          /* Single character types set the bits and stop. Note that if PCRE_UCP
973          is set, we do not see these op codes because \d etc are converted to
974          properties. Therefore, these apply in the case when only characters less
975          than 256 are recognized to match the types. */
976    
977        case OP_NOT_DIGIT:        case OP_NOT_DIGIT:
978        for (c = 0; c < 32; c++)        set_nottype_bits(start_bits, cbit_digit, table_limit, cd);
         start_bits[c] |= ~cd->cbits[c+cbit_digit];  
979        try_next = FALSE;        try_next = FALSE;
980        break;        break;
981    
982        case OP_DIGIT:        case OP_DIGIT:
983        for (c = 0; c < 32; c++)        set_type_bits(start_bits, cbit_digit, table_limit, cd);
         start_bits[c] |= cd->cbits[c+cbit_digit];  
984        try_next = FALSE;        try_next = FALSE;
985        break;        break;
986    
987        /* The cbit_space table has vertical tab as whitespace; we have to        /* The cbit_space table has vertical tab as whitespace; we have to
988        discard it. */        ensure it is set as not whitespace. */
989    
990        case OP_NOT_WHITESPACE:        case OP_NOT_WHITESPACE:
991        for (c = 0; c < 32; c++)        set_nottype_bits(start_bits, cbit_space, table_limit, cd);
992          {        start_bits[1] |= 0x08;
         int d = cd->cbits[c+cbit_space];  
         if (c == 1) d &= ~0x08;  
         start_bits[c] |= ~d;  
         }  
993        try_next = FALSE;        try_next = FALSE;
994        break;        break;
995    
996        /* The cbit_space table has vertical tab as whitespace; we have to        /* The cbit_space table has vertical tab as whitespace; we have to
997        discard it. */        not set it from the table. */
998    
999        case OP_WHITESPACE:        case OP_WHITESPACE:
1000        for (c = 0; c < 32; c++)        c = start_bits[1];    /* Save in case it was already set */
1001          {        set_type_bits(start_bits, cbit_space, table_limit, cd);
1002          int d = cd->cbits[c+cbit_space];        start_bits[1] = (start_bits[1] & ~0x08) | c;
         if (c == 1) d &= ~0x08;  
         start_bits[c] |= d;  
         }  
1003        try_next = FALSE;        try_next = FALSE;
1004        break;        break;
1005    
1006        case OP_NOT_WORDCHAR:        case OP_NOT_WORDCHAR:
1007        for (c = 0; c < 32; c++)        set_nottype_bits(start_bits, cbit_word, table_limit, cd);
         start_bits[c] |= ~cd->cbits[c+cbit_word];  
1008        try_next = FALSE;        try_next = FALSE;
1009        break;        break;
1010    
1011        case OP_WORDCHAR:        case OP_WORDCHAR:
1012        for (c = 0; c < 32; c++)        set_type_bits(start_bits, cbit_word, table_limit, cd);
         start_bits[c] |= cd->cbits[c+cbit_word];  
1013        try_next = FALSE;        try_next = FALSE;
1014        break;        break;
1015    
# Line 683  do Line 1018  do
1018    
1019        case OP_TYPEPLUS:        case OP_TYPEPLUS:
1020        case OP_TYPEMINPLUS:        case OP_TYPEMINPLUS:
1021          case OP_TYPEPOSPLUS:
1022        tcode++;        tcode++;
1023        break;        break;
1024    
# Line 706  do Line 1042  do
1042        case OP_TYPEPOSQUERY:        case OP_TYPEPOSQUERY:
1043        switch(tcode[1])        switch(tcode[1])
1044          {          {
1045            default:
1046          case OP_ANY:          case OP_ANY:
1047          case OP_ALLANY:          case OP_ALLANY:
1048          return SSB_FAIL;          return SSB_FAIL;
1049    
1050            case OP_HSPACE:
1051            SET_BIT(0x09);
1052            SET_BIT(0x20);
1053            if (utf8)
1054              {
1055              SET_BIT(0xC2);  /* For U+00A0 */
1056              SET_BIT(0xE1);  /* For U+1680, U+180E */
1057              SET_BIT(0xE2);  /* For U+2000 - U+200A, U+202F, U+205F */
1058              SET_BIT(0xE3);  /* For U+3000 */
1059              }
1060            else SET_BIT(0xA0);
1061            break;
1062    
1063            case OP_ANYNL:
1064            case OP_VSPACE:
1065            SET_BIT(0x0A);
1066            SET_BIT(0x0B);
1067            SET_BIT(0x0C);
1068            SET_BIT(0x0D);
1069            if (utf8)
1070              {
1071              SET_BIT(0xC2);  /* For U+0085 */
1072              SET_BIT(0xE2);  /* For U+2028, U+2029 */
1073              }
1074            else SET_BIT(0x85);
1075            break;
1076    
1077          case OP_NOT_DIGIT:          case OP_NOT_DIGIT:
1078          for (c = 0; c < 32; c++)          set_nottype_bits(start_bits, cbit_digit, table_limit, cd);
           start_bits[c] |= ~cd->cbits[c+cbit_digit];  
1079          break;          break;
1080    
1081          case OP_DIGIT:          case OP_DIGIT:
1082          for (c = 0; c < 32; c++)          set_type_bits(start_bits, cbit_digit, table_limit, cd);
           start_bits[c] |= cd->cbits[c+cbit_digit];  
1083          break;          break;
1084    
1085          /* The cbit_space table has vertical tab as whitespace; we have to          /* The cbit_space table has vertical tab as whitespace; we have to
1086          discard it. */          ensure it gets set as not whitespace. */
1087    
1088          case OP_NOT_WHITESPACE:          case OP_NOT_WHITESPACE:
1089          for (c = 0; c < 32; c++)          set_nottype_bits(start_bits, cbit_space, table_limit, cd);
1090            {          start_bits[1] |= 0x08;
           int d = cd->cbits[c+cbit_space];  
           if (c == 1) d &= ~0x08;  
           start_bits[c] |= ~d;  
           }  
1091          break;          break;
1092    
1093          /* The cbit_space table has vertical tab as whitespace; we have to          /* The cbit_space table has vertical tab as whitespace; we have to
1094          discard it. */          avoid setting it. */
1095    
1096          case OP_WHITESPACE:          case OP_WHITESPACE:
1097          for (c = 0; c < 32; c++)          c = start_bits[1];    /* Save in case it was already set */
1098            {          set_type_bits(start_bits, cbit_space, table_limit, cd);
1099            int d = cd->cbits[c+cbit_space];          start_bits[1] = (start_bits[1] & ~0x08) | c;
           if (c == 1) d &= ~0x08;  
           start_bits[c] |= d;  
           }  
1100          break;          break;
1101    
1102          case OP_NOT_WORDCHAR:          case OP_NOT_WORDCHAR:
1103          for (c = 0; c < 32; c++)          set_nottype_bits(start_bits, cbit_word, table_limit, cd);
           start_bits[c] |= ~cd->cbits[c+cbit_word];  
1104          break;          break;
1105    
1106          case OP_WORDCHAR:          case OP_WORDCHAR:
1107          for (c = 0; c < 32; c++)          set_type_bits(start_bits, cbit_word, table_limit, cd);
           start_bits[c] |= cd->cbits[c+cbit_word];  
1108          break;          break;
1109          }          }
1110    
# Line 807  do Line 1160  do
1160            for (c = 0; c < 32; c++) start_bits[c] |= tcode[c];            for (c = 0; c < 32; c++) start_bits[c] |= tcode[c];
1161            }            }
1162    
1163          /* Advance past the bit map, and act on what follows */          /* Advance past the bit map, and act on what follows. For a zero
1164            minimum repeat, continue; otherwise stop processing. */
1165    
1166          tcode += 32;          tcode += 32;
1167          switch (*tcode)          switch (*tcode)
# Line 843  return yield; Line 1197  return yield;
1197    
1198    
1199    
1200    
1201    
1202  /*************************************************  /*************************************************
1203  *          Study a compiled expression           *  *          Study a compiled expression           *
1204  *************************************************/  *************************************************/
# Line 868  pcre_study(const pcre *external_re, int Line 1224  pcre_study(const pcre *external_re, int
1224  int min;  int min;
1225  BOOL bits_set = FALSE;  BOOL bits_set = FALSE;
1226  uschar start_bits[32];  uschar start_bits[32];
1227  pcre_extra *extra;  pcre_extra *extra = NULL;
1228  pcre_study_data *study;  pcre_study_data *study;
1229  const uschar *tables;  const uschar *tables;
1230  uschar *code;  uschar *code;
# Line 899  seeking a list of starting bytes. */ Line 1255  seeking a list of starting bytes. */
1255  if ((re->options & PCRE_ANCHORED) == 0 &&  if ((re->options & PCRE_ANCHORED) == 0 &&
1256      (re->flags & (PCRE_FIRSTSET|PCRE_STARTLINE)) == 0)      (re->flags & (PCRE_FIRSTSET|PCRE_STARTLINE)) == 0)
1257    {    {
1258      int rc;
1259    
1260    /* Set the character tables in the block that is passed around */    /* Set the character tables in the block that is passed around */
1261    
1262    tables = re->tables;    tables = re->tables;
# Line 914  if ((re->options & PCRE_ANCHORED) == 0 & Line 1272  if ((re->options & PCRE_ANCHORED) == 0 &
1272    /* See if we can find a fixed set of initial characters for the pattern. */    /* See if we can find a fixed set of initial characters for the pattern. */
1273    
1274    memset(start_bits, 0, 32 * sizeof(uschar));    memset(start_bits, 0, 32 * sizeof(uschar));
1275    bits_set = set_start_bits(code, start_bits,    rc = set_start_bits(code, start_bits, (re->options & PCRE_UTF8) != 0,
1276      (re->options & PCRE_CASELESS) != 0, (re->options & PCRE_UTF8) != 0,      &compile_block);
1277      &compile_block) == SSB_DONE;    bits_set = rc == SSB_DONE;
1278      if (rc == SSB_UNKNOWN)
1279        {
1280        *errorptr = "internal error: opcode not recognized";
1281        return NULL;
1282        }
1283    }    }
1284    
1285  /* Find the minimum length of subject string. */  /* Find the minimum length of subject string. */
1286    
1287  min = find_minlength(code, code, re->options);  switch(min = find_minlength(code, code, re->options, 0))
1288      {
1289      case -2: *errorptr = "internal error: missing capturing bracket"; return NULL;
1290      case -3: *errorptr = "internal error: opcode not recognized"; return NULL;
1291      default: break;
1292      }
1293    
1294  /* Return NULL if no optimization is possible. */  /* If a set of starting bytes has been identified, or if the minimum length is
1295    greater than zero, or if JIT optimization has been requested, get a pcre_extra
1296    block and a pcre_study_data block. The study data is put in the latter, which
1297    is pointed to by the former, which may also get additional data set later by
1298    the calling program. At the moment, the size of pcre_study_data is fixed. We
1299    nevertheless save it in a field for returning via the pcre_fullinfo() function
1300    so that if it becomes variable in the future, we don't have to change that
1301    code. */
1302    
1303    if (bits_set || min > 0
1304    #ifdef SUPPORT_JIT
1305        || (options & PCRE_STUDY_JIT_COMPILE) != 0
1306    #endif
1307      )
1308      {
1309      extra = (pcre_extra *)(pcre_malloc)
1310        (sizeof(pcre_extra) + sizeof(pcre_study_data));
1311      if (extra == NULL)
1312        {
1313        *errorptr = "failed to get memory";
1314        return NULL;
1315        }
1316    
1317  if (!bits_set && min < 0) return NULL;    study = (pcre_study_data *)((char *)extra + sizeof(pcre_extra));
1318      extra->flags = PCRE_EXTRA_STUDY_DATA;
1319      extra->study_data = study;
1320    
1321  /* Get a pcre_extra block and a pcre_study_data block. The study data is put in    study->size = sizeof(pcre_study_data);
1322  the latter, which is pointed to by the former, which may also get additional    study->flags = 0;
 data set later by the calling program. At the moment, the size of  
 pcre_study_data is fixed. We nevertheless save it in a field for returning via  
 the pcre_fullinfo() function so that if it becomes variable in the future, we  
 don't have to change that code. */  
1323    
1324  extra = (pcre_extra *)(pcre_malloc)    if (bits_set)
1325    (sizeof(pcre_extra) + sizeof(pcre_study_data));      {
1326        study->flags |= PCRE_STUDY_MAPPED;
1327        memcpy(study->start_bits, start_bits, sizeof(start_bits));
1328        }
1329    
1330  if (extra == NULL)    /* Always set the minlength value in the block, because the JIT compiler
1331    {    makes use of it. However, don't set the bit unless the length is greater than
1332    *errorptr = "failed to get memory";    zero - the interpretive pcre_exec() and pcre_dfa_exec() needn't waste time
1333    return NULL;    checking the zero case. */
1334    
1335      if (min > 0)
1336        {
1337        study->flags |= PCRE_STUDY_MINLEN;
1338        study->minlength = min;
1339        }
1340      else study->minlength = 0;
1341    
1342      /* If JIT support was compiled and requested, attempt the JIT compilation.
1343      If no starting bytes were found, and the minimum length is zero, and JIT
1344      compilation fails, abandon the extra block and return NULL. */
1345    
1346    #ifdef SUPPORT_JIT
1347      extra->executable_jit = NULL;
1348      if ((options & PCRE_STUDY_JIT_COMPILE) != 0) _pcre_jit_compile(re, extra);
1349      if (study->flags == 0 && (extra->flags & PCRE_EXTRA_EXECUTABLE_JIT) == 0)
1350        {
1351        pcre_free_study(extra);
1352        extra = NULL;
1353        }
1354    #endif
1355    }    }
1356    
1357  study = (pcre_study_data *)((char *)extra + sizeof(pcre_extra));  return extra;
1358  extra->flags = PCRE_EXTRA_STUDY_DATA;  }
 extra->study_data = study;  
1359    
 study->size = sizeof(pcre_study_data);  
 study->flags = 0;  
1360    
1361  if (bits_set)  /*************************************************
1362    {  *          Free the study data                   *
1363    study->flags |= PCRE_STUDY_MAPPED;  *************************************************/
   memcpy(study->start_bits, start_bits, sizeof(start_bits));  
   }  
1364    
1365  if (min >= 0)  /* This function frees the memory that was obtained by pcre_study().
   {  
   study->flags |= PCRE_STUDY_MINLEN;  
   study->minlength = min;  
   }  
1366    
1367  return extra;  Argument:   a pointer to the pcre_extra block
1368    Returns:    nothing
1369    */
1370    
1371    PCRE_EXP_DEFN void
1372    pcre_free_study(pcre_extra *extra)
1373    {
1374    #ifdef SUPPORT_JIT
1375    if ((extra->flags & PCRE_EXTRA_EXECUTABLE_JIT) != 0 &&
1376         extra->executable_jit != NULL)
1377      _pcre_jit_free(extra->executable_jit);
1378    #endif
1379    pcre_free(extra);
1380  }  }
1381    
1382  /* End of pcre_study.c */  /* End of pcre_study.c */

Legend:
Removed from v.467  
changed lines
  Added in v.729

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12