/[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 613 by ph10, Sat Jul 2 16:59:52 2011 UTC revision 708 by ph10, Fri Sep 23 11:03:03 2011 UTC
# 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    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    BOOL *had_accept_ptr)    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 127  for (;;) Line 127  for (;;)
127      case OP_BRAPOS:      case OP_BRAPOS:
128      case OP_SBRAPOS:      case OP_SBRAPOS:
129      case OP_ONCE:      case OP_ONCE:
130      d = find_minlength(cc, startcode, options, had_accept_ptr);      d = find_minlength(cc, startcode, options, recurse_depth);
131      if (d < 0) return d;      if (d < 0) return d;
132      branchlength += d;      branchlength += d;
     if (*had_accept_ptr) return branchlength;  
133      do cc += GET(cc, 1); while (*cc == OP_ALT);      do cc += GET(cc, 1); while (*cc == OP_ALT);
134      cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;
135      break;      break;
136    
137        /* ACCEPT makes things far too complicated; we have to give up. */
138    
139        case OP_ACCEPT:
140        case OP_ASSERT_ACCEPT:
141        return -1;
142    
143      /* 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
144      call. If it's ALT it is an alternation in a nested call. If it is END it's      call. If it's ALT it is an alternation in a nested call. If it is END it's
145      the end of the outer call. All can be handled by the same code. If it is      the end of the outer call. All can be handled by the same code. If an
146      ACCEPT, it is essentially the same as END, but we set a flag so that      ACCEPT was previously encountered, use the length that was in force at that
147      counting stops. */      time, and pass back the shortest ACCEPT length. */
148    
     case OP_ACCEPT:  
     case OP_ASSERT_ACCEPT:  
     *had_accept_ptr = TRUE;  
     /* Fall through */  
149      case OP_ALT:      case OP_ALT:
150      case OP_KET:      case OP_KET:
151      case OP_KETRMAX:      case OP_KETRMAX:
# Line 276  for (;;) Line 277  for (;;)
277      cc++;      cc++;
278      break;      break;
279    
280      /* "Any newline" might match two characters, but it also might match just      /* "Any newline" might match two characters, but it also might match just
281      one. */      one. */
282    
283      case OP_ANYNL:      case OP_ANYNL:
# Line 376  for (;;) Line 377  for (;;)
377          d = 0;          d = 0;
378          had_recurse = TRUE;          had_recurse = TRUE;
379          }          }
380        else        else
381          {          {
382          d = find_minlength(cs, startcode, options, had_accept_ptr);          d = find_minlength(cs, startcode, options, recurse_depth);
383          *had_accept_ptr = FALSE;          }
         }  
384        }        }
385      else d = 0;      else d = 0;
386      cc += 3;      cc += 3;
# Line 417  for (;;) Line 417  for (;;)
417      branchlength += min * d;      branchlength += min * d;
418      break;      break;
419    
420        /* We can easily detect direct recursion, but not mutual recursion. This is
421        caught by a recursion depth count. */
422    
423      case OP_RECURSE:      case OP_RECURSE:
424      cs = ce = (uschar *)startcode + GET(cc, 1);      cs = ce = (uschar *)startcode + GET(cc, 1);
425      if (cs == NULL) return -2;      if (cs == NULL) return -2;
426      do ce += GET(ce, 1); while (*ce == OP_ALT);      do ce += GET(ce, 1); while (*ce == OP_ALT);
427      if (cc > cs && cc < ce)      if ((cc > cs && cc < ce) || recurse_depth > 10)
428        had_recurse = TRUE;        had_recurse = TRUE;
429      else      else
430        {        {
431        branchlength += find_minlength(cs, startcode, options, had_accept_ptr);        branchlength += find_minlength(cs, startcode, options, recurse_depth + 1);
432        *had_accept_ptr = FALSE;        }
       }  
433      cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;
434      break;      break;
435    
# Line 495  for (;;) Line 497  for (;;)
497      case OP_THEN_ARG:      case OP_THEN_ARG:
498      cc += _pcre_OP_lengths[op] + cc[1+LINK_SIZE];      cc += _pcre_OP_lengths[op] + cc[1+LINK_SIZE];
499      break;      break;
500    
501      /* The remaining opcodes are just skipped over. */      /* The remaining opcodes are just skipped over. */
502    
503      case OP_CLOSE:      case OP_CLOSE:
# Line 716  do Line 718  do
718        /* Fail for a valid opcode that implies no starting bits. */        /* Fail for a valid opcode that implies no starting bits. */
719    
720        case OP_ACCEPT:        case OP_ACCEPT:
721        case OP_ASSERT_ACCEPT:        case OP_ASSERT_ACCEPT:
722        case OP_ALLANY:        case OP_ALLANY:
723        case OP_ANY:        case OP_ANY:
724        case OP_ANYBYTE:        case OP_ANYBYTE:
725        case OP_CIRC:        case OP_CIRC:
726        case OP_CIRCM:        case OP_CIRCM:
727        case OP_CLOSE:        case OP_CLOSE:
728        case OP_COMMIT:        case OP_COMMIT:
729        case OP_COND:        case OP_COND:
730        case OP_CREF:        case OP_CREF:
731        case OP_DEF:        case OP_DEF:
732        case OP_DOLL:        case OP_DOLL:
733        case OP_DOLLM:        case OP_DOLLM:
734        case OP_END:        case OP_END:
735        case OP_EOD:        case OP_EOD:
736        case OP_EODN:        case OP_EODN:
737        case OP_EXTUNI:        case OP_EXTUNI:
738        case OP_FAIL:        case OP_FAIL:
739        case OP_MARK:        case OP_MARK:
# Line 739  do Line 741  do
741        case OP_NOT:        case OP_NOT:
742        case OP_NOTEXACT:        case OP_NOTEXACT:
743        case OP_NOTEXACTI:        case OP_NOTEXACTI:
744        case OP_NOTI:        case OP_NOTI:
745        case OP_NOTMINPLUS:        case OP_NOTMINPLUS:
746        case OP_NOTMINPLUSI:        case OP_NOTMINPLUSI:
747        case OP_NOTMINQUERY:        case OP_NOTMINQUERY:
# Line 767  do Line 769  do
769        case OP_NOTUPTOI:        case OP_NOTUPTOI:
770        case OP_NOT_HSPACE:        case OP_NOT_HSPACE:
771        case OP_NOT_VSPACE:        case OP_NOT_VSPACE:
       case OP_NOT_WORD_BOUNDARY:  
772        case OP_NRREF:        case OP_NRREF:
773        case OP_PROP:        case OP_PROP:
774        case OP_PRUNE:        case OP_PRUNE:
# Line 777  do Line 778  do
778        case OP_REFI:        case OP_REFI:
779        case OP_REVERSE:        case OP_REVERSE:
780        case OP_RREF:        case OP_RREF:
781        case OP_SCOND:        case OP_SCOND:
782        case OP_SET_SOM:        case OP_SET_SOM:
783        case OP_SKIP:        case OP_SKIP:
784        case OP_SKIP_ARG:        case OP_SKIP_ARG:
# Line 785  do Line 786  do
786        case OP_SOM:        case OP_SOM:
787        case OP_THEN:        case OP_THEN:
788        case OP_THEN_ARG:        case OP_THEN_ARG:
       case OP_WORD_BOUNDARY:  
789        case OP_XCLASS:        case OP_XCLASS:
790        return SSB_FAIL;        return SSB_FAIL;
791    
792          /* We can ignore word boundary tests. */
793    
794          case OP_WORD_BOUNDARY:
795          case OP_NOT_WORD_BOUNDARY:
796          tcode++;
797          break;
798    
799        /* 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
800        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
801        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 1154  do Line 1161  do
1161            for (c = 0; c < 32; c++) start_bits[c] |= tcode[c];            for (c = 0; c < 32; c++) start_bits[c] |= tcode[c];
1162            }            }
1163    
1164          /* Advance past the bit map, and act on what follows. For a zero          /* Advance past the bit map, and act on what follows. For a zero
1165          minimum repeat, continue; otherwise stop processing. */          minimum repeat, continue; otherwise stop processing. */
1166    
1167          tcode += 32;          tcode += 32;
# Line 1172  do Line 1179  do
1179            if (((tcode[1] << 8) + tcode[2]) == 0) tcode += 5;            if (((tcode[1] << 8) + tcode[2]) == 0) tcode += 5;
1180              else try_next = FALSE;              else try_next = FALSE;
1181            break;            break;
1182    
1183            default:            default:
1184            try_next = FALSE;            try_next = FALSE;
1185            break;            break;
# Line 1217  pcre_study(const pcre *external_re, int Line 1224  pcre_study(const pcre *external_re, int
1224  {  {
1225  int min;  int min;
1226  BOOL bits_set = FALSE;  BOOL bits_set = FALSE;
 BOOL had_accept = FALSE;  
1227  uschar start_bits[32];  uschar start_bits[32];
1228  pcre_extra *extra;  pcre_extra *extra = NULL;
1229  pcre_study_data *study;  pcre_study_data *study;
1230  const uschar *tables;  const uschar *tables;
1231  uschar *code;  uschar *code;
# Line 1270  if ((re->options & PCRE_ANCHORED) == 0 & Line 1276  if ((re->options & PCRE_ANCHORED) == 0 &
1276    rc = set_start_bits(code, start_bits, (re->options & PCRE_UTF8) != 0,    rc = set_start_bits(code, start_bits, (re->options & PCRE_UTF8) != 0,
1277      &compile_block);      &compile_block);
1278    bits_set = rc == SSB_DONE;    bits_set = rc == SSB_DONE;
1279    if (rc == SSB_UNKNOWN) *errorptr = "internal error: opcode not recognized";    if (rc == SSB_UNKNOWN)
1280        {
1281        *errorptr = "internal error: opcode not recognized";
1282        return NULL;
1283        }
1284    }    }
1285    
1286  /* Find the minimum length of subject string. */  /* Find the minimum length of subject string. */
1287    
1288  switch(min = find_minlength(code, code, re->options, &had_accept))  switch(min = find_minlength(code, code, re->options, 0))
1289    {    {
1290    case -2: *errorptr = "internal error: missing capturing bracket"; break;    case -2: *errorptr = "internal error: missing capturing bracket"; return NULL;
1291    case -3: *errorptr = "internal error: opcode not recognized"; break;    case -3: *errorptr = "internal error: opcode not recognized"; return NULL;
1292    default: break;    default: break;
1293    }    }
1294    
1295  /* Return NULL if there's been an error or if no optimization is possible. */  /* If a set of starting bytes has been identified, or if the minimum length is
1296    greater than zero, or if JIT optimization has been requested, get a pcre_extra
1297    block and a pcre_study_data block. The study data is put in the latter, which
1298    is pointed to by the former, which may also get additional data set later by
1299    the calling program. At the moment, the size of pcre_study_data is fixed. We
1300    nevertheless save it in a field for returning via the pcre_fullinfo() function
1301    so that if it becomes variable in the future, we don't have to change that
1302    code. */
1303    
1304    if (bits_set || min > 0
1305    #ifdef SUPPORT_JIT
1306        || (options & PCRE_STUDY_JIT_COMPILE) != 0
1307    #endif
1308      )
1309      {
1310      extra = (pcre_extra *)(pcre_malloc)
1311        (sizeof(pcre_extra) + sizeof(pcre_study_data));
1312      if (extra == NULL)
1313        {
1314        *errorptr = "failed to get memory";
1315        return NULL;
1316        }
1317    
1318  if (*errorptr != NULL || (!bits_set && min < 0)) return NULL;    study = (pcre_study_data *)((char *)extra + sizeof(pcre_extra));
1319      extra->flags = PCRE_EXTRA_STUDY_DATA;
1320      extra->study_data = study;
1321    
1322  /* Get a pcre_extra block and a pcre_study_data block. The study data is put in    study->size = sizeof(pcre_study_data);
1323  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. */  
1324    
1325  extra = (pcre_extra *)(pcre_malloc)    if (bits_set)
1326    (sizeof(pcre_extra) + sizeof(pcre_study_data));      {
1327        study->flags |= PCRE_STUDY_MAPPED;
1328        memcpy(study->start_bits, start_bits, sizeof(start_bits));
1329        }
1330    
1331  if (extra == NULL)    /* Always set the minlength value in the block, because the JIT compiler
1332    {    makes use of it. However, don't set the bit unless the length is greater than
1333    *errorptr = "failed to get memory";    zero - the interpretive pcre_exec() and pcre_dfa_exec() needn't waste time
1334    return NULL;    checking the zero case. */
1335    
1336      if (min > 0)
1337        {
1338        study->flags |= PCRE_STUDY_MINLEN;
1339        study->minlength = min;
1340        }
1341      else study->minlength = 0;
1342    
1343      /* If JIT support was compiled and requested, attempt the JIT compilation.
1344      If no starting bytes were found, and the minimum length is zero, and JIT
1345      compilation fails, abandon the extra block and return NULL. */
1346    
1347    #ifdef SUPPORT_JIT
1348      extra->executable_jit = NULL;
1349      if ((options & PCRE_STUDY_JIT_COMPILE) != 0) _pcre_jit_compile(re, extra);
1350      if (study->flags == 0 && (extra->flags & PCRE_EXTRA_EXECUTABLE_JIT) == 0)
1351        {
1352        pcre_free_study(extra);
1353        extra = NULL;
1354        }
1355    #endif
1356    }    }
1357    
1358  study = (pcre_study_data *)((char *)extra + sizeof(pcre_extra));  return extra;
1359  extra->flags = PCRE_EXTRA_STUDY_DATA;  }
 extra->study_data = study;  
1360    
 study->size = sizeof(pcre_study_data);  
 study->flags = 0;  
1361    
1362  if (bits_set)  /*************************************************
1363    {  *          Free the study data                   *
1364    study->flags |= PCRE_STUDY_MAPPED;  *************************************************/
   memcpy(study->start_bits, start_bits, sizeof(start_bits));  
   }  
1365    
1366  if (min >= 0)  /* This function frees the memory that was obtained by pcre_study().
   {  
   study->flags |= PCRE_STUDY_MINLEN;  
   study->minlength = min;  
   }  
1367    
1368  return extra;  Argument:   a pointer to the pcre_extra block
1369    Returns:    nothing
1370    */
1371    
1372    PCRE_EXP_DEFN void
1373    pcre_free_study(pcre_extra *extra)
1374    {
1375    #ifdef SUPPORT_JIT
1376    if ((extra->flags & PCRE_EXTRA_EXECUTABLE_JIT) != 0 &&
1377         extra->executable_jit != NULL)
1378      _pcre_jit_free(extra->executable_jit);
1379    #endif
1380    pcre_free(extra);
1381  }  }
1382    
1383  /* End of pcre_study.c */  /* End of pcre_study.c */

Legend:
Removed from v.613  
changed lines
  Added in v.708

  ViewVC Help
Powered by ViewVC 1.1.5