/[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 550 by ph10, Sun Oct 10 16:24:11 2010 UTC revision 691 by ph10, Sun Sep 11 14:31:21 2011 UTC
# Line 52  supporting functions. */ Line 52  supporting functions. */
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 66  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      had_accept  pointer to flag for (*ACCEPT) encountered
73      int         RECURSE depth
74    
75  Returns:   the minimum length  Returns:   the minimum length
76             -1 if \C was encountered             -1 if \C was encountered
77             -2 internal error (missing capturing bracket)             -2 internal error (missing capturing bracket)
78               -3 internal error (opcode not listed)
79  */  */
80    
81  static int  static int
82  find_minlength(const uschar *code, const uschar *startcode, int options)  find_minlength(const uschar *code, const uschar *startcode, int options,
83      BOOL *had_accept_ptr, int recurse_depth)
84  {  {
85  int length = -1;  int length = -1;
86  BOOL utf8 = (options & PCRE_UTF8) != 0;  BOOL utf8 = (options & PCRE_UTF8) != 0;
# Line 84  BOOL had_recurse = FALSE; Line 88  BOOL had_recurse = FALSE;
88  register int branchlength = 0;  register int branchlength = 0;
89  register uschar *cc = (uschar *)code + 1 + LINK_SIZE;  register uschar *cc = (uschar *)code + 1 + LINK_SIZE;
90    
91  if (*code == OP_CBRA || *code == OP_SCBRA) cc += 2;  if (*code == OP_CBRA || *code == OP_SCBRA ||
92        *code == OP_CBRAPOS || *code == OP_SCBRAPOS) cc += 2;
93    
94  /* 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
95  branch, check the length against that of the other branches. */  branch, check the length against that of the other branches. */
# Line 118  for (;;) Line 123  for (;;)
123      case OP_SCBRA:      case OP_SCBRA:
124      case OP_BRA:      case OP_BRA:
125      case OP_SBRA:      case OP_SBRA:
126        case OP_CBRAPOS:
127        case OP_SCBRAPOS:
128        case OP_BRAPOS:
129        case OP_SBRAPOS:
130      case OP_ONCE:      case OP_ONCE:
131      d = find_minlength(cc, startcode, options);      d = find_minlength(cc, startcode, options, had_accept_ptr, recurse_depth);
132      if (d < 0) return d;      if (d < 0) return d;
133      branchlength += d;      branchlength += d;
134        if (*had_accept_ptr) return branchlength;
135      do cc += GET(cc, 1); while (*cc == OP_ALT);      do cc += GET(cc, 1); while (*cc == OP_ALT);
136      cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;
137      break;      break;
138    
139      /* 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
140      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
141      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 it is
142        ACCEPT, it is essentially the same as END, but we set a flag so that
143        counting stops. */
144    
145        case OP_ACCEPT:
146        case OP_ASSERT_ACCEPT:
147        *had_accept_ptr = TRUE;
148        /* Fall through */
149      case OP_ALT:      case OP_ALT:
150      case OP_KET:      case OP_KET:
151      case OP_KETRMAX:      case OP_KETRMAX:
152      case OP_KETRMIN:      case OP_KETRMIN:
153        case OP_KETRPOS:
154      case OP_END:      case OP_END:
155      if (length < 0 || (!had_recurse && branchlength < length))      if (length < 0 || (!had_recurse && branchlength < length))
156        length = branchlength;        length = branchlength;
157      if (*cc != OP_ALT) return length;      if (op != OP_ALT) return length;
158      cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;
159      branchlength = 0;      branchlength = 0;
160      had_recurse = FALSE;      had_recurse = FALSE;
# Line 160  for (;;) Line 177  for (;;)
177      case OP_RREF:      case OP_RREF:
178      case OP_NRREF:      case OP_NRREF:
179      case OP_DEF:      case OP_DEF:
     case OP_OPT:  
180      case OP_CALLOUT:      case OP_CALLOUT:
181      case OP_SOD:      case OP_SOD:
182      case OP_SOM:      case OP_SOM:
183      case OP_EOD:      case OP_EOD:
184      case OP_EODN:      case OP_EODN:
185      case OP_CIRC:      case OP_CIRC:
186        case OP_CIRCM:
187      case OP_DOLL:      case OP_DOLL:
188        case OP_DOLLM:
189      case OP_NOT_WORD_BOUNDARY:      case OP_NOT_WORD_BOUNDARY:
190      case OP_WORD_BOUNDARY:      case OP_WORD_BOUNDARY:
191      cc += _pcre_OP_lengths[*cc];      cc += _pcre_OP_lengths[*cc];
# Line 177  for (;;) Line 195  for (;;)
195    
196      case OP_BRAZERO:      case OP_BRAZERO:
197      case OP_BRAMINZERO:      case OP_BRAMINZERO:
198        case OP_BRAPOSZERO:
199      case OP_SKIPZERO:      case OP_SKIPZERO:
200      cc += _pcre_OP_lengths[*cc];      cc += _pcre_OP_lengths[*cc];
201      do cc += GET(cc, 1); while (*cc == OP_ALT);      do cc += GET(cc, 1); while (*cc == OP_ALT);
# Line 186  for (;;) Line 205  for (;;)
205      /* Handle literal characters and + repetitions */      /* Handle literal characters and + repetitions */
206    
207      case OP_CHAR:      case OP_CHAR:
208      case OP_CHARNC:      case OP_CHARI:
209      case OP_NOT:      case OP_NOT:
210        case OP_NOTI:
211      case OP_PLUS:      case OP_PLUS:
212        case OP_PLUSI:
213      case OP_MINPLUS:      case OP_MINPLUS:
214        case OP_MINPLUSI:
215      case OP_POSPLUS:      case OP_POSPLUS:
216        case OP_POSPLUSI:
217      case OP_NOTPLUS:      case OP_NOTPLUS:
218        case OP_NOTPLUSI:
219      case OP_NOTMINPLUS:      case OP_NOTMINPLUS:
220        case OP_NOTMINPLUSI:
221      case OP_NOTPOSPLUS:      case OP_NOTPOSPLUS:
222        case OP_NOTPOSPLUSI:
223      branchlength++;      branchlength++;
224      cc += 2;      cc += 2;
225  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
# Line 212  for (;;) Line 238  for (;;)
238      need to skip over a multibyte character in UTF8 mode.  */      need to skip over a multibyte character in UTF8 mode.  */
239    
240      case OP_EXACT:      case OP_EXACT:
241        case OP_EXACTI:
242      case OP_NOTEXACT:      case OP_NOTEXACT:
243        case OP_NOTEXACTI:
244      branchlength += GET2(cc,1);      branchlength += GET2(cc,1);
245      cc += 4;      cc += 4;
246  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
# Line 249  for (;;) Line 277  for (;;)
277      cc++;      cc++;
278      break;      break;
279    
280      /* "Any newline" might match two characters */      /* "Any newline" might match two characters, but it also might match just
281        one. */
282    
283      case OP_ANYNL:      case OP_ANYNL:
284      branchlength += 2;      branchlength += 1;
285      cc++;      cc++;
286      break;      break;
287    
# Line 337  for (;;) Line 366  for (;;)
366      that case we must set the minimum length to zero. */      that case we must set the minimum length to zero. */
367    
368      case OP_REF:      case OP_REF:
369        case OP_REFI:
370      if ((options & PCRE_JAVASCRIPT_COMPAT) == 0)      if ((options & PCRE_JAVASCRIPT_COMPAT) == 0)
371        {        {
372        ce = cs = (uschar *)_pcre_find_bracket(startcode, utf8, GET2(cc, 1));        ce = cs = (uschar *)_pcre_find_bracket(startcode, utf8, GET2(cc, 1));
# Line 347  for (;;) Line 377  for (;;)
377          d = 0;          d = 0;
378          had_recurse = TRUE;          had_recurse = TRUE;
379          }          }
380        else d = find_minlength(cs, startcode, options);        else
381            {
382            d = find_minlength(cs, startcode, options, had_accept_ptr,
383              recurse_depth);
384            *had_accept_ptr = FALSE;
385            }
386        }        }
387      else d = 0;      else d = 0;
388      cc += 3;      cc += 3;
# Line 364  for (;;) Line 399  for (;;)
399        cc++;        cc++;
400        break;        break;
401    
402          case OP_CRPLUS:
403          case OP_CRMINPLUS:
404          min = 1;
405          cc++;
406          break;
407    
408        case OP_CRRANGE:        case OP_CRRANGE:
409        case OP_CRMINRANGE:        case OP_CRMINRANGE:
410        min = GET2(cc, 1);        min = GET2(cc, 1);
# Line 378  for (;;) Line 419  for (;;)
419      branchlength += min * d;      branchlength += min * d;
420      break;      break;
421    
422        /* We can easily detect direct recursion, but not mutual recursion. This is
423        caught by a recursion depth count. */
424    
425      case OP_RECURSE:      case OP_RECURSE:
426      cs = ce = (uschar *)startcode + GET(cc, 1);      cs = ce = (uschar *)startcode + GET(cc, 1);
427      if (cs == NULL) return -2;      if (cs == NULL) return -2;
428      do ce += GET(ce, 1); while (*ce == OP_ALT);      do ce += GET(ce, 1); while (*ce == OP_ALT);
429      if (cc > cs && cc < ce)      if ((cc > cs && cc < ce) || recurse_depth > 10)
430        had_recurse = TRUE;        had_recurse = TRUE;
431      else      else
432        branchlength += find_minlength(cs, startcode, options);        {
433          branchlength += find_minlength(cs, startcode, options, had_accept_ptr,
434            recurse_depth + 1);
435          *had_accept_ptr = FALSE;
436          }
437      cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;
438      break;      break;
439    
440      /* 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
441      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
442      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
443        happens, the "NOT" versions of these opcodes are used at present only for
444        ASCII characters, so they could be omitted from this list. However, in
445        future that may change, so we include them here so as not to leave a
446        gotcha for a future maintainer. */
447    
448      case OP_UPTO:      case OP_UPTO:
449        case OP_UPTOI:
450      case OP_NOTUPTO:      case OP_NOTUPTO:
451        case OP_NOTUPTOI:
452      case OP_MINUPTO:      case OP_MINUPTO:
453        case OP_MINUPTOI:
454      case OP_NOTMINUPTO:      case OP_NOTMINUPTO:
455        case OP_NOTMINUPTOI:
456      case OP_POSUPTO:      case OP_POSUPTO:
457        case OP_POSUPTOI:
458        case OP_NOTPOSUPTO:
459        case OP_NOTPOSUPTOI:
460    
461      case OP_STAR:      case OP_STAR:
462        case OP_STARI:
463        case OP_NOTSTAR:
464        case OP_NOTSTARI:
465      case OP_MINSTAR:      case OP_MINSTAR:
466        case OP_MINSTARI:
467      case OP_NOTMINSTAR:      case OP_NOTMINSTAR:
468        case OP_NOTMINSTARI:
469      case OP_POSSTAR:      case OP_POSSTAR:
470        case OP_POSSTARI:
471      case OP_NOTPOSSTAR:      case OP_NOTPOSSTAR:
472        case OP_NOTPOSSTARI:
473    
474      case OP_QUERY:      case OP_QUERY:
475        case OP_QUERYI:
476        case OP_NOTQUERY:
477        case OP_NOTQUERYI:
478      case OP_MINQUERY:      case OP_MINQUERY:
479        case OP_MINQUERYI:
480      case OP_NOTMINQUERY:      case OP_NOTMINQUERY:
481        case OP_NOTMINQUERYI:
482      case OP_POSQUERY:      case OP_POSQUERY:
483        case OP_POSQUERYI:
484      case OP_NOTPOSQUERY:      case OP_NOTPOSQUERY:
485        case OP_NOTPOSQUERYI:
486    
487      cc += _pcre_OP_lengths[op];      cc += _pcre_OP_lengths[op];
488  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
489      if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f];      if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f];
# Line 426  for (;;) Line 502  for (;;)
502      cc += _pcre_OP_lengths[op] + cc[1+LINK_SIZE];      cc += _pcre_OP_lengths[op] + cc[1+LINK_SIZE];
503      break;      break;
504    
505      /* For the record, these are the opcodes that are matched by "default":      /* The remaining opcodes are just skipped over. */
     OP_ACCEPT, OP_CLOSE, OP_COMMIT, OP_FAIL, OP_PRUNE, OP_SET_SOM, OP_SKIP,  
     OP_THEN. */  
506    
507      default:      case OP_CLOSE:
508        case OP_COMMIT:
509        case OP_FAIL:
510        case OP_PRUNE:
511        case OP_SET_SOM:
512        case OP_SKIP:
513        case OP_THEN:
514      cc += _pcre_OP_lengths[op];      cc += _pcre_OP_lengths[op];
515      break;      break;
516    
517        /* This should not occur: we list all opcodes explicitly so that when
518        new ones get added they are properly considered. */
519    
520        default:
521        return -3;
522      }      }
523    }    }
524  /* Control never gets here */  /* Control never gets here */
# Line 578  function fails unless the result is SSB_ Line 664  function fails unless the result is SSB_
664  Arguments:  Arguments:
665    code         points to an expression    code         points to an expression
666    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  
667    utf8         TRUE if in UTF-8 mode    utf8         TRUE if in UTF-8 mode
668    cd           the block with char table pointers    cd           the block with char table pointers
669    
670  Returns:       SSB_FAIL     => Failed to find any starting bytes  Returns:       SSB_FAIL     => Failed to find any starting bytes
671                 SSB_DONE     => Found mandatory starting bytes                 SSB_DONE     => Found mandatory starting bytes
672                 SSB_CONTINUE => Found optional starting bytes                 SSB_CONTINUE => Found optional starting bytes
673                   SSB_UNKNOWN  => Hit an unrecognized opcode
674  */  */
675    
676  static int  static int
677  set_start_bits(const uschar *code, uschar *start_bits, BOOL caseless,  set_start_bits(const uschar *code, uschar *start_bits, BOOL utf8,
678    BOOL utf8, compile_data *cd)    compile_data *cd)
679  {  {
680  register int c;  register int c;
681  int yield = SSB_DONE;  int yield = SSB_DONE;
# Line 614  volatile int dummy; Line 700  volatile int dummy;
700    
701  do  do
702    {    {
   const uschar *tcode = code + (((int)*code == OP_CBRA)? 3:1) + LINK_SIZE;  
703    BOOL try_next = TRUE;    BOOL try_next = TRUE;
704      const uschar *tcode = code + 1 + LINK_SIZE;
705    
706      if (*code == OP_CBRA || *code == OP_SCBRA ||
707          *code == OP_CBRAPOS || *code == OP_SCBRAPOS) tcode += 2;
708    
709    while (try_next)    /* Loop for items in this branch */    while (try_next)    /* Loop for items in this branch */
710      {      {
711      int rc;      int rc;
712    
713      switch(*tcode)      switch(*tcode)
714        {        {
715        /* Fail if we reach something we don't understand */        /* If we reach something we don't understand, it means a new opcode has
716          been created that hasn't been added to this code. Hopefully this problem
717          will be discovered during testing. */
718    
719        default:        default:
720          return SSB_UNKNOWN;
721    
722          /* Fail for a valid opcode that implies no starting bits. */
723    
724          case OP_ACCEPT:
725          case OP_ASSERT_ACCEPT:
726          case OP_ALLANY:
727          case OP_ANY:
728          case OP_ANYBYTE:
729          case OP_CIRC:
730          case OP_CIRCM:
731          case OP_CLOSE:
732          case OP_COMMIT:
733          case OP_COND:
734          case OP_CREF:
735          case OP_DEF:
736          case OP_DOLL:
737          case OP_DOLLM:
738          case OP_END:
739          case OP_EOD:
740          case OP_EODN:
741          case OP_EXTUNI:
742          case OP_FAIL:
743          case OP_MARK:
744          case OP_NCREF:
745          case OP_NOT:
746          case OP_NOTEXACT:
747          case OP_NOTEXACTI:
748          case OP_NOTI:
749          case OP_NOTMINPLUS:
750          case OP_NOTMINPLUSI:
751          case OP_NOTMINQUERY:
752          case OP_NOTMINQUERYI:
753          case OP_NOTMINSTAR:
754          case OP_NOTMINSTARI:
755          case OP_NOTMINUPTO:
756          case OP_NOTMINUPTOI:
757          case OP_NOTPLUS:
758          case OP_NOTPLUSI:
759          case OP_NOTPOSPLUS:
760          case OP_NOTPOSPLUSI:
761          case OP_NOTPOSQUERY:
762          case OP_NOTPOSQUERYI:
763          case OP_NOTPOSSTAR:
764          case OP_NOTPOSSTARI:
765          case OP_NOTPOSUPTO:
766          case OP_NOTPOSUPTOI:
767          case OP_NOTPROP:
768          case OP_NOTQUERY:
769          case OP_NOTQUERYI:
770          case OP_NOTSTAR:
771          case OP_NOTSTARI:
772          case OP_NOTUPTO:
773          case OP_NOTUPTOI:
774          case OP_NOT_HSPACE:
775          case OP_NOT_VSPACE:
776          case OP_NRREF:
777          case OP_PROP:
778          case OP_PRUNE:
779          case OP_PRUNE_ARG:
780          case OP_RECURSE:
781          case OP_REF:
782          case OP_REFI:
783          case OP_REVERSE:
784          case OP_RREF:
785          case OP_SCOND:
786          case OP_SET_SOM:
787          case OP_SKIP:
788          case OP_SKIP_ARG:
789          case OP_SOD:
790          case OP_SOM:
791          case OP_THEN:
792          case OP_THEN_ARG:
793          case OP_XCLASS:
794        return SSB_FAIL;        return SSB_FAIL;
795    
796          /* We can ignore word boundary tests. */
797    
798          case OP_WORD_BOUNDARY:
799          case OP_NOT_WORD_BOUNDARY:
800          tcode++;
801          break;
802    
803        /* 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
804        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
805        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 636  do Line 809  do
809        case OP_SBRA:        case OP_SBRA:
810        case OP_CBRA:        case OP_CBRA:
811        case OP_SCBRA:        case OP_SCBRA:
812          case OP_BRAPOS:
813          case OP_SBRAPOS:
814          case OP_CBRAPOS:
815          case OP_SCBRAPOS:
816        case OP_ONCE:        case OP_ONCE:
817        case OP_ASSERT:        case OP_ASSERT:
818        rc = set_start_bits(tcode, start_bits, caseless, utf8, cd);        rc = set_start_bits(tcode, start_bits, utf8, cd);
819        if (rc == SSB_FAIL) return SSB_FAIL;        if (rc == SSB_FAIL || rc == SSB_UNKNOWN) return rc;
820        if (rc == SSB_DONE) try_next = FALSE; else        if (rc == SSB_DONE) try_next = FALSE; else
821          {          {
822          do tcode += GET(tcode, 1); while (*tcode == OP_ALT);          do tcode += GET(tcode, 1); while (*tcode == OP_ALT);
# Line 662  do Line 839  do
839        case OP_KET:        case OP_KET:
840        case OP_KETRMAX:        case OP_KETRMAX:
841        case OP_KETRMIN:        case OP_KETRMIN:
842          case OP_KETRPOS:
843        return SSB_CONTINUE;        return SSB_CONTINUE;
844    
845        /* Skip over callout */        /* Skip over callout */
# Line 679  do Line 857  do
857        tcode += 1 + LINK_SIZE;        tcode += 1 + LINK_SIZE;
858        break;        break;
859    
       /* Skip over an option setting, changing the caseless flag */  
   
       case OP_OPT:  
       caseless = (tcode[1] & PCRE_CASELESS) != 0;  
       tcode += 2;  
       break;  
   
860        /* BRAZERO does the bracket, but carries on. */        /* BRAZERO does the bracket, but carries on. */
861    
862        case OP_BRAZERO:        case OP_BRAZERO:
863        case OP_BRAMINZERO:        case OP_BRAMINZERO:
864        if (set_start_bits(++tcode, start_bits, caseless, utf8, cd) == SSB_FAIL)        case OP_BRAPOSZERO:
865          return SSB_FAIL;        rc = set_start_bits(++tcode, start_bits, utf8, cd);
866          if (rc == SSB_FAIL || rc == SSB_UNKNOWN) return rc;
867  /* =========================================================================  /* =========================================================================
868        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,
869        which was an old fudge for the benefit of OS/2.        which was an old fudge for the benefit of OS/2.
# Line 717  do Line 889  do
889        case OP_QUERY:        case OP_QUERY:
890        case OP_MINQUERY:        case OP_MINQUERY:
891        case OP_POSQUERY:        case OP_POSQUERY:
892        tcode = set_table_bit(start_bits, tcode + 1, caseless, cd, utf8);        tcode = set_table_bit(start_bits, tcode + 1, FALSE, cd, utf8);
893          break;
894    
895          case OP_STARI:
896          case OP_MINSTARI:
897          case OP_POSSTARI:
898          case OP_QUERYI:
899          case OP_MINQUERYI:
900          case OP_POSQUERYI:
901          tcode = set_table_bit(start_bits, tcode + 1, TRUE, cd, utf8);
902        break;        break;
903    
904        /* Single-char upto sets the bit and tries the next */        /* Single-char upto sets the bit and tries the next */
# Line 725  do Line 906  do
906        case OP_UPTO:        case OP_UPTO:
907        case OP_MINUPTO:        case OP_MINUPTO:
908        case OP_POSUPTO:        case OP_POSUPTO:
909        tcode = set_table_bit(start_bits, tcode + 3, caseless, cd, utf8);        tcode = set_table_bit(start_bits, tcode + 3, FALSE, cd, utf8);
910          break;
911    
912          case OP_UPTOI:
913          case OP_MINUPTOI:
914          case OP_POSUPTOI:
915          tcode = set_table_bit(start_bits, tcode + 3, TRUE, cd, utf8);
916        break;        break;
917    
918        /* At least one single char sets the bit and stops */        /* At least one single char sets the bit and stops */
919    
920        case OP_EXACT:       /* Fall through */        case OP_EXACT:
921        tcode += 2;        tcode += 2;
922          /* Fall through */
923        case OP_CHAR:        case OP_CHAR:
       case OP_CHARNC:  
924        case OP_PLUS:        case OP_PLUS:
925        case OP_MINPLUS:        case OP_MINPLUS:
926        case OP_POSPLUS:        case OP_POSPLUS:
927        (void)set_table_bit(start_bits, tcode + 1, caseless, cd, utf8);        (void)set_table_bit(start_bits, tcode + 1, FALSE, cd, utf8);
928          try_next = FALSE;
929          break;
930    
931          case OP_EXACTI:
932          tcode += 2;
933          /* Fall through */
934          case OP_CHARI:
935          case OP_PLUSI:
936          case OP_MINPLUSI:
937          case OP_POSPLUSI:
938          (void)set_table_bit(start_bits, tcode + 1, TRUE, cd, utf8);
939        try_next = FALSE;        try_next = FALSE;
940        break;        break;
941    
# Line 968  do Line 1165  do
1165            for (c = 0; c < 32; c++) start_bits[c] |= tcode[c];            for (c = 0; c < 32; c++) start_bits[c] |= tcode[c];
1166            }            }
1167    
1168          /* Advance past the bit map, and act on what follows */          /* Advance past the bit map, and act on what follows. For a zero
1169            minimum repeat, continue; otherwise stop processing. */
1170    
1171          tcode += 32;          tcode += 32;
1172          switch (*tcode)          switch (*tcode)
# Line 1004  return yield; Line 1202  return yield;
1202    
1203    
1204    
1205    
1206    
1207  /*************************************************  /*************************************************
1208  *          Study a compiled expression           *  *          Study a compiled expression           *
1209  *************************************************/  *************************************************/
# Line 1028  pcre_study(const pcre *external_re, int Line 1228  pcre_study(const pcre *external_re, int
1228  {  {
1229  int min;  int min;
1230  BOOL bits_set = FALSE;  BOOL bits_set = FALSE;
1231    BOOL had_accept = FALSE;
1232  uschar start_bits[32];  uschar start_bits[32];
1233  pcre_extra *extra;  pcre_extra *extra = NULL;
1234  pcre_study_data *study;  pcre_study_data *study;
1235  const uschar *tables;  const uschar *tables;
1236  uschar *code;  uschar *code;
# Line 1060  seeking a list of starting bytes. */ Line 1261  seeking a list of starting bytes. */
1261  if ((re->options & PCRE_ANCHORED) == 0 &&  if ((re->options & PCRE_ANCHORED) == 0 &&
1262      (re->flags & (PCRE_FIRSTSET|PCRE_STARTLINE)) == 0)      (re->flags & (PCRE_FIRSTSET|PCRE_STARTLINE)) == 0)
1263    {    {
1264      int rc;
1265    
1266    /* Set the character tables in the block that is passed around */    /* Set the character tables in the block that is passed around */
1267    
1268    tables = re->tables;    tables = re->tables;
# Line 1075  if ((re->options & PCRE_ANCHORED) == 0 & Line 1278  if ((re->options & PCRE_ANCHORED) == 0 &
1278    /* 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. */
1279    
1280    memset(start_bits, 0, 32 * sizeof(uschar));    memset(start_bits, 0, 32 * sizeof(uschar));
1281    bits_set = set_start_bits(code, start_bits,    rc = set_start_bits(code, start_bits, (re->options & PCRE_UTF8) != 0,
1282      (re->options & PCRE_CASELESS) != 0, (re->options & PCRE_UTF8) != 0,      &compile_block);
1283      &compile_block) == SSB_DONE;    bits_set = rc == SSB_DONE;
1284      if (rc == SSB_UNKNOWN)
1285        {
1286        *errorptr = "internal error: opcode not recognized";
1287        return NULL;
1288        }
1289    }    }
1290    
1291  /* Find the minimum length of subject string. */  /* Find the minimum length of subject string. */
1292    
1293  min = find_minlength(code, code, re->options);  switch(min = find_minlength(code, code, re->options, &had_accept, 0))
1294      {
1295      case -2: *errorptr = "internal error: missing capturing bracket"; return NULL;
1296      case -3: *errorptr = "internal error: opcode not recognized"; return NULL;
1297      default: break;
1298      }
1299    
1300  /* Return NULL if no optimization is possible. */  /* If a set of starting bytes has been identified, or if the minimum length is
1301    greater than zero, or if JIT optimization has been requested, get a pcre_extra
1302    block and a pcre_study_data block. The study data is put in the latter, which
1303    is pointed to by the former, which may also get additional data set later by
1304    the calling program. At the moment, the size of pcre_study_data is fixed. We
1305    nevertheless save it in a field for returning via the pcre_fullinfo() function
1306    so that if it becomes variable in the future, we don't have to change that
1307    code. */
1308    
1309    if (bits_set || min > 0
1310    #ifdef SUPPORT_JIT
1311        || (options & PCRE_STUDY_JIT_COMPILE) != 0
1312    #endif
1313      )
1314      {
1315      extra = (pcre_extra *)(pcre_malloc)
1316        (sizeof(pcre_extra) + sizeof(pcre_study_data));
1317      if (extra == NULL)
1318        {
1319        *errorptr = "failed to get memory";
1320        return NULL;
1321        }
1322    
1323  if (!bits_set && min < 0) return NULL;    study = (pcre_study_data *)((char *)extra + sizeof(pcre_extra));
1324      extra->flags = PCRE_EXTRA_STUDY_DATA;
1325      extra->study_data = study;
1326    
1327  /* Get a pcre_extra block and a pcre_study_data block. The study data is put in    study->size = sizeof(pcre_study_data);
1328  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. */  
1329    
1330  extra = (pcre_extra *)(pcre_malloc)    if (bits_set)
1331    (sizeof(pcre_extra) + sizeof(pcre_study_data));      {
1332        study->flags |= PCRE_STUDY_MAPPED;
1333        memcpy(study->start_bits, start_bits, sizeof(start_bits));
1334        }
1335    
1336  if (extra == NULL)    /* Always set the minlength value in the block, because the JIT compiler
1337    {    makes use of it. However, don't set the bit unless the length is greater than
1338    *errorptr = "failed to get memory";    zero - the interpretive pcre_exec() and pcre_dfa_exec() needn't waste time
1339    return NULL;    checking this case. */
1340    
1341      study->minlength = min;
1342      if (min > 0) study->flags |= PCRE_STUDY_MINLEN;
1343    
1344      /* If JIT support was compiled and requested, attempt the JIT compilation.
1345      If no starting bytes were found, and the minimum length is zero, and JIT
1346      compilation fails, abandon the extra block and return NULL. */
1347    
1348    #ifdef SUPPORT_JIT
1349      extra->executable_jit = NULL;
1350      if ((options & PCRE_STUDY_JIT_COMPILE) != 0) _pcre_jit_compile(re, extra);
1351      if (study->flags == 0 && (extra->flags & PCRE_EXTRA_EXECUTABLE_JIT) == 0)
1352        {
1353        pcre_free_study(extra);
1354        extra = NULL;
1355        }
1356    #endif
1357    }    }
1358    
1359  study = (pcre_study_data *)((char *)extra + sizeof(pcre_extra));  return extra;
1360  extra->flags = PCRE_EXTRA_STUDY_DATA;  }
 extra->study_data = study;  
1361    
 study->size = sizeof(pcre_study_data);  
 study->flags = 0;  
1362    
1363  if (bits_set)  /*************************************************
1364    {  *          Free the study data                   *
1365    study->flags |= PCRE_STUDY_MAPPED;  *************************************************/
   memcpy(study->start_bits, start_bits, sizeof(start_bits));  
   }  
1366    
1367  if (min >= 0)  /* This function frees the memory that was obtained by pcre_study().
   {  
   study->flags |= PCRE_STUDY_MINLEN;  
   study->minlength = min;  
   }  
1368    
1369  return extra;  Argument:   a pointer to the pcre_extra block
1370    Returns:    nothing
1371    */
1372    
1373    PCRE_EXP_DEFN void
1374    pcre_free_study(pcre_extra *extra)
1375    {
1376    #ifdef SUPPORT_JIT
1377    if ((extra->flags & PCRE_EXTRA_EXECUTABLE_JIT) != 0 &&
1378         extra->executable_jit != NULL)
1379      _pcre_jit_free(extra->executable_jit);
1380    #endif
1381    pcre_free(extra);
1382  }  }
1383    
1384  /* End of pcre_study.c */  /* End of pcre_study.c */

Legend:
Removed from v.550  
changed lines
  Added in v.691

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12