/[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 603 by ph10, Fri May 27 10:14:09 2011 UTC revision 723 by ph10, Sat Oct 8 15:55:23 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      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)             -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 85  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 119  for (;;) Line 122  for (;;)
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      d = find_minlength(cc, startcode, options);      case OP_ONCE_NC:
131        d = find_minlength(cc, startcode, options, recurse_depth);
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 179  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 260  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 359  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;
# Line 376  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 390  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    
# Line 406  for (;;) Line 439  for (;;)
439      of a character, we must take special action for UTF-8 characters. As it      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      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      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      future that may change, so we include them here so as not to leave a
443      gotcha for a future maintainer. */      gotcha for a future maintainer. */
444    
445      case OP_UPTO:      case OP_UPTO:
# Line 468  for (;;) Line 501  for (;;)
501    
502      /* The remaining opcodes are just skipped over. */      /* The remaining opcodes are just skipped over. */
503    
     case OP_ACCEPT:  
504      case OP_CLOSE:      case OP_CLOSE:
505      case OP_COMMIT:      case OP_COMMIT:
506      case OP_FAIL:      case OP_FAIL:
507      case OP_PRUNE:      case OP_PRUNE:
508      case OP_SET_SOM:      case OP_SET_SOM:
509      case OP_SKIP:      case OP_SKIP:
510      case OP_THEN:      case OP_THEN:
511      cc += _pcre_OP_lengths[op];      cc += _pcre_OP_lengths[op];
512      break;      break;
513    
514      /* This should not occur: we list all opcodes explicitly so that when      /* This should not occur: we list all opcodes explicitly so that when
515      new ones get added they are properly considered. */      new ones get added they are properly considered. */
516    
# Line 635  Arguments: Line 667  Arguments:
667  Returns:       SSB_FAIL     => Failed to find any starting bytes  Returns:       SSB_FAIL     => Failed to find any starting bytes
668                 SSB_DONE     => Found mandatory starting bytes                 SSB_DONE     => Found mandatory starting bytes
669                 SSB_CONTINUE => Found optional starting bytes                 SSB_CONTINUE => Found optional starting bytes
670                   SSB_UNKNOWN  => Hit an unrecognized opcode
671  */  */
672    
673  static int  static int
674  set_start_bits(const uschar *code, uschar *start_bits, BOOL utf8,  set_start_bits(const uschar *code, uschar *start_bits, BOOL utf8,
675    compile_data *cd)    compile_data *cd)
676  {  {
677  register int c;  register int c;
# Line 664  volatile int dummy; Line 697  volatile int dummy;
697    
698  do  do
699    {    {
   const uschar *tcode = code + (((int)*code == OP_CBRA)? 3:1) + LINK_SIZE;  
700    BOOL try_next = TRUE;    BOOL try_next = TRUE;
701      const uschar *tcode = code + 1 + LINK_SIZE;
702    
703      if (*code == OP_CBRA || *code == OP_SCBRA ||
704          *code == OP_CBRAPOS || *code == OP_SCBRAPOS) tcode += 2;
705    
706    while (try_next)    /* Loop for items in this branch */    while (try_next)    /* Loop for items in this branch */
707      {      {
708      int rc;      int rc;
709    
710      switch(*tcode)      switch(*tcode)
711        {        {
712        /* Fail if we reach something we don't understand */        /* If we reach something we don't understand, it means a new opcode has
713          been created that hasn't been added to this code. Hopefully this problem
714          will be discovered during testing. */
715    
716        default:        default:
717          return SSB_UNKNOWN;
718    
719          /* Fail for a valid opcode that implies no starting bits. */
720    
721          case OP_ACCEPT:
722          case OP_ASSERT_ACCEPT:
723          case OP_ALLANY:
724          case OP_ANY:
725          case OP_ANYBYTE:
726          case OP_CIRC:
727          case OP_CIRCM:
728          case OP_CLOSE:
729          case OP_COMMIT:
730          case OP_COND:
731          case OP_CREF:
732          case OP_DEF:
733          case OP_DOLL:
734          case OP_DOLLM:
735          case OP_END:
736          case OP_EOD:
737          case OP_EODN:
738          case OP_EXTUNI:
739          case OP_FAIL:
740          case OP_MARK:
741          case OP_NCREF:
742          case OP_NOT:
743          case OP_NOTEXACT:
744          case OP_NOTEXACTI:
745          case OP_NOTI:
746          case OP_NOTMINPLUS:
747          case OP_NOTMINPLUSI:
748          case OP_NOTMINQUERY:
749          case OP_NOTMINQUERYI:
750          case OP_NOTMINSTAR:
751          case OP_NOTMINSTARI:
752          case OP_NOTMINUPTO:
753          case OP_NOTMINUPTOI:
754          case OP_NOTPLUS:
755          case OP_NOTPLUSI:
756          case OP_NOTPOSPLUS:
757          case OP_NOTPOSPLUSI:
758          case OP_NOTPOSQUERY:
759          case OP_NOTPOSQUERYI:
760          case OP_NOTPOSSTAR:
761          case OP_NOTPOSSTARI:
762          case OP_NOTPOSUPTO:
763          case OP_NOTPOSUPTOI:
764          case OP_NOTPROP:
765          case OP_NOTQUERY:
766          case OP_NOTQUERYI:
767          case OP_NOTSTAR:
768          case OP_NOTSTARI:
769          case OP_NOTUPTO:
770          case OP_NOTUPTOI:
771          case OP_NOT_HSPACE:
772          case OP_NOT_VSPACE:
773          case OP_NRREF:
774          case OP_PROP:
775          case OP_PRUNE:
776          case OP_PRUNE_ARG:
777          case OP_RECURSE:
778          case OP_REF:
779          case OP_REFI:
780          case OP_REVERSE:
781          case OP_RREF:
782          case OP_SCOND:
783          case OP_SET_SOM:
784          case OP_SKIP:
785          case OP_SKIP_ARG:
786          case OP_SOD:
787          case OP_SOM:
788          case OP_THEN:
789          case OP_THEN_ARG:
790          case OP_XCLASS:
791        return SSB_FAIL;        return SSB_FAIL;
792    
793          /* We can ignore word boundary tests. */
794    
795          case OP_WORD_BOUNDARY:
796          case OP_NOT_WORD_BOUNDARY:
797          tcode++;
798          break;
799    
800        /* 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
801        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
802        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 686  do Line 806  do
806        case OP_SBRA:        case OP_SBRA:
807        case OP_CBRA:        case OP_CBRA:
808        case OP_SCBRA:        case OP_SCBRA:
809          case OP_BRAPOS:
810          case OP_SBRAPOS:
811          case OP_CBRAPOS:
812          case OP_SCBRAPOS:
813        case OP_ONCE:        case OP_ONCE:
814          case OP_ONCE_NC:
815        case OP_ASSERT:        case OP_ASSERT:
816        rc = set_start_bits(tcode, start_bits, utf8, cd);        rc = set_start_bits(tcode, start_bits, utf8, cd);
817        if (rc == SSB_FAIL) return SSB_FAIL;        if (rc == SSB_FAIL || rc == SSB_UNKNOWN) return rc;
818        if (rc == SSB_DONE) try_next = FALSE; else        if (rc == SSB_DONE) try_next = FALSE; else
819          {          {
820          do tcode += GET(tcode, 1); while (*tcode == OP_ALT);          do tcode += GET(tcode, 1); while (*tcode == OP_ALT);
# Line 712  do Line 837  do
837        case OP_KET:        case OP_KET:
838        case OP_KETRMAX:        case OP_KETRMAX:
839        case OP_KETRMIN:        case OP_KETRMIN:
840          case OP_KETRPOS:
841        return SSB_CONTINUE;        return SSB_CONTINUE;
842    
843        /* Skip over callout */        /* Skip over callout */
# Line 733  do Line 859  do
859    
860        case OP_BRAZERO:        case OP_BRAZERO:
861        case OP_BRAMINZERO:        case OP_BRAMINZERO:
862        if (set_start_bits(++tcode, start_bits, utf8, cd) == SSB_FAIL)        case OP_BRAPOSZERO:
863          return SSB_FAIL;        rc = set_start_bits(++tcode, start_bits, utf8, cd);
864          if (rc == SSB_FAIL || rc == SSB_UNKNOWN) return rc;
865  /* =========================================================================  /* =========================================================================
866        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,
867        which was an old fudge for the benefit of OS/2.        which was an old fudge for the benefit of OS/2.
# Line 1036  do Line 1163  do
1163            for (c = 0; c < 32; c++) start_bits[c] |= tcode[c];            for (c = 0; c < 32; c++) start_bits[c] |= tcode[c];
1164            }            }
1165    
1166          /* Advance past the bit map, and act on what follows */          /* Advance past the bit map, and act on what follows. For a zero
1167            minimum repeat, continue; otherwise stop processing. */
1168    
1169          tcode += 32;          tcode += 32;
1170          switch (*tcode)          switch (*tcode)
# Line 1072  return yield; Line 1200  return yield;
1200    
1201    
1202    
1203    
1204    
1205  /*************************************************  /*************************************************
1206  *          Study a compiled expression           *  *          Study a compiled expression           *
1207  *************************************************/  *************************************************/
# Line 1097  pcre_study(const pcre *external_re, int Line 1227  pcre_study(const pcre *external_re, int
1227  int min;  int min;
1228  BOOL bits_set = FALSE;  BOOL bits_set = FALSE;
1229  uschar start_bits[32];  uschar start_bits[32];
1230  pcre_extra *extra;  pcre_extra *extra = NULL;
1231  pcre_study_data *study;  pcre_study_data *study;
1232  const uschar *tables;  const uschar *tables;
1233  uschar *code;  uschar *code;
# Line 1128  seeking a list of starting bytes. */ Line 1258  seeking a list of starting bytes. */
1258  if ((re->options & PCRE_ANCHORED) == 0 &&  if ((re->options & PCRE_ANCHORED) == 0 &&
1259      (re->flags & (PCRE_FIRSTSET|PCRE_STARTLINE)) == 0)      (re->flags & (PCRE_FIRSTSET|PCRE_STARTLINE)) == 0)
1260    {    {
1261      int rc;
1262    
1263    /* Set the character tables in the block that is passed around */    /* Set the character tables in the block that is passed around */
1264    
1265    tables = re->tables;    tables = re->tables;
# Line 1143  if ((re->options & PCRE_ANCHORED) == 0 & Line 1275  if ((re->options & PCRE_ANCHORED) == 0 &
1275    /* 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. */
1276    
1277    memset(start_bits, 0, 32 * sizeof(uschar));    memset(start_bits, 0, 32 * sizeof(uschar));
1278    bits_set = set_start_bits(code, start_bits, (re->options & PCRE_UTF8) != 0,    rc = set_start_bits(code, start_bits, (re->options & PCRE_UTF8) != 0,
1279      &compile_block) == SSB_DONE;      &compile_block);
1280      bits_set = rc == SSB_DONE;
1281      if (rc == SSB_UNKNOWN)
1282        {
1283        *errorptr = "internal error: opcode not recognized";
1284        return NULL;
1285        }
1286    }    }
1287    
1288  /* Find the minimum length of subject string. */  /* Find the minimum length of subject string. */
1289    
1290  switch(min = find_minlength(code, code, re->options))  switch(min = find_minlength(code, code, re->options, 0))
1291    {    {
1292    case -2: *errorptr = "internal error: missing capturing bracket"; break;    case -2: *errorptr = "internal error: missing capturing bracket"; return NULL;
1293    case -3: *errorptr = "internal error: opcode not recognized"; break;    case -3: *errorptr = "internal error: opcode not recognized"; return NULL;
1294    default: break;    default: break;
1295    }    }
1296    
1297  /* Return NULL if no optimization is possible. */  /* If a set of starting bytes has been identified, or if the minimum length is
1298    greater than zero, or if JIT optimization has been requested, get a pcre_extra
1299    block and a pcre_study_data block. The study data is put in the latter, which
1300    is pointed to by the former, which may also get additional data set later by
1301    the calling program. At the moment, the size of pcre_study_data is fixed. We
1302    nevertheless save it in a field for returning via the pcre_fullinfo() function
1303    so that if it becomes variable in the future, we don't have to change that
1304    code. */
1305    
1306    if (bits_set || min > 0
1307    #ifdef SUPPORT_JIT
1308        || (options & PCRE_STUDY_JIT_COMPILE) != 0
1309    #endif
1310      )
1311      {
1312      extra = (pcre_extra *)(pcre_malloc)
1313        (sizeof(pcre_extra) + sizeof(pcre_study_data));
1314      if (extra == NULL)
1315        {
1316        *errorptr = "failed to get memory";
1317        return NULL;
1318        }
1319    
1320  if (!bits_set && min < 0) return NULL;    study = (pcre_study_data *)((char *)extra + sizeof(pcre_extra));
1321      extra->flags = PCRE_EXTRA_STUDY_DATA;
1322      extra->study_data = study;
1323    
1324  /* Get a pcre_extra block and a pcre_study_data block. The study data is put in    study->size = sizeof(pcre_study_data);
1325  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. */  
1326    
1327  extra = (pcre_extra *)(pcre_malloc)    if (bits_set)
1328    (sizeof(pcre_extra) + sizeof(pcre_study_data));      {
1329        study->flags |= PCRE_STUDY_MAPPED;
1330        memcpy(study->start_bits, start_bits, sizeof(start_bits));
1331        }
1332    
1333  if (extra == NULL)    /* Always set the minlength value in the block, because the JIT compiler
1334    {    makes use of it. However, don't set the bit unless the length is greater than
1335    *errorptr = "failed to get memory";    zero - the interpretive pcre_exec() and pcre_dfa_exec() needn't waste time
1336    return NULL;    checking the zero case. */
1337    
1338      if (min > 0)
1339        {
1340        study->flags |= PCRE_STUDY_MINLEN;
1341        study->minlength = min;
1342        }
1343      else study->minlength = 0;
1344    
1345      /* If JIT support was compiled and requested, attempt the JIT compilation.
1346      If no starting bytes were found, and the minimum length is zero, and JIT
1347      compilation fails, abandon the extra block and return NULL. */
1348    
1349    #ifdef SUPPORT_JIT
1350      extra->executable_jit = NULL;
1351      if ((options & PCRE_STUDY_JIT_COMPILE) != 0) _pcre_jit_compile(re, extra);
1352      if (study->flags == 0 && (extra->flags & PCRE_EXTRA_EXECUTABLE_JIT) == 0)
1353        {
1354        pcre_free_study(extra);
1355        extra = NULL;
1356        }
1357    #endif
1358    }    }
1359    
1360  study = (pcre_study_data *)((char *)extra + sizeof(pcre_extra));  return extra;
1361  extra->flags = PCRE_EXTRA_STUDY_DATA;  }
 extra->study_data = study;  
1362    
 study->size = sizeof(pcre_study_data);  
 study->flags = 0;  
1363    
1364  if (bits_set)  /*************************************************
1365    {  *          Free the study data                   *
1366    study->flags |= PCRE_STUDY_MAPPED;  *************************************************/
   memcpy(study->start_bits, start_bits, sizeof(start_bits));  
   }  
1367    
1368  if (min >= 0)  /* This function frees the memory that was obtained by pcre_study().
   {  
   study->flags |= PCRE_STUDY_MINLEN;  
   study->minlength = min;  
   }  
1369    
1370  return extra;  Argument:   a pointer to the pcre_extra block
1371    Returns:    nothing
1372    */
1373    
1374    PCRE_EXP_DEFN void
1375    pcre_free_study(pcre_extra *extra)
1376    {
1377    #ifdef SUPPORT_JIT
1378    if ((extra->flags & PCRE_EXTRA_EXECUTABLE_JIT) != 0 &&
1379         extra->executable_jit != NULL)
1380      _pcre_jit_free(extra->executable_jit);
1381    #endif
1382    pcre_free(extra);
1383  }  }
1384    
1385  /* End of pcre_study.c */  /* End of pcre_study.c */

Legend:
Removed from v.603  
changed lines
  Added in v.723

  ViewVC Help
Powered by ViewVC 1.1.5