Install PCRE 6.7 in in place of 6.2.
[exim.git] / src / src / pcre / pcre_study.c
1 /* $Cambridge: exim/src/src/pcre/pcre_study.c,v 1.3 2006/11/07 16:50:36 ph10 Exp $ */
2
3 /*************************************************
4 *      Perl-Compatible Regular Expressions       *
5 *************************************************/
6
7 /* PCRE is a library of functions to support regular expressions whose syntax
8 and semantics are as close as possible to those of the Perl 5 language.
9
10                        Written by Philip Hazel
11            Copyright (c) 1997-2006 University of Cambridge
12
13 -----------------------------------------------------------------------------
14 Redistribution and use in source and binary forms, with or without
15 modification, are permitted provided that the following conditions are met:
16
17     * Redistributions of source code must retain the above copyright notice,
18       this list of conditions and the following disclaimer.
19
20     * Redistributions in binary form must reproduce the above copyright
21       notice, this list of conditions and the following disclaimer in the
22       documentation and/or other materials provided with the distribution.
23
24     * Neither the name of the University of Cambridge nor the names of its
25       contributors may be used to endorse or promote products derived from
26       this software without specific prior written permission.
27
28 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
29 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
32 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 POSSIBILITY OF SUCH DAMAGE.
39 -----------------------------------------------------------------------------
40 */
41
42
43 /* This module contains the external function pcre_study(), along with local
44 supporting functions. */
45
46
47 #include "pcre_internal.h"
48
49
50 /*************************************************
51 *      Set a bit and maybe its alternate case    *
52 *************************************************/
53
54 /* Given a character, set its bit in the table, and also the bit for the other
55 version of a letter if we are caseless.
56
57 Arguments:
58   start_bits    points to the bit map
59   c             is the character
60   caseless      the caseless flag
61   cd            the block with char table pointers
62
63 Returns:        nothing
64 */
65
66 static void
67 set_bit(uschar *start_bits, unsigned int c, BOOL caseless, compile_data *cd)
68 {
69 start_bits[c/8] |= (1 << (c&7));
70 if (caseless && (cd->ctypes[c] & ctype_letter) != 0)
71   start_bits[cd->fcc[c]/8] |= (1 << (cd->fcc[c]&7));
72 }
73
74
75
76 /*************************************************
77 *          Create bitmap of starting chars       *
78 *************************************************/
79
80 /* This function scans a compiled unanchored expression and attempts to build a
81 bitmap of the set of initial characters. If it can't, it returns FALSE. As time
82 goes by, we may be able to get more clever at doing this.
83
84 Arguments:
85   code         points to an expression
86   start_bits   points to a 32-byte table, initialized to 0
87   caseless     the current state of the caseless flag
88   utf8         TRUE if in UTF-8 mode
89   cd           the block with char table pointers
90
91 Returns:       TRUE if table built, FALSE otherwise
92 */
93
94 static BOOL
95 set_start_bits(const uschar *code, uschar *start_bits, BOOL caseless,
96   BOOL utf8, compile_data *cd)
97 {
98 register int c;
99
100 #if 0
101 /* ========================================================================= */
102 /* The following comment and code was inserted in January 1999. In May 2006,
103 when it was observed to cause compiler warnings about unused values, I took it
104 out again. If anybody is still using OS/2, they will have to put it back
105 manually. */
106
107 /* This next statement and the later reference to dummy are here in order to
108 trick the optimizer of the IBM C compiler for OS/2 into generating correct
109 code. Apparently IBM isn't going to fix the problem, and we would rather not
110 disable optimization (in this module it actually makes a big difference, and
111 the pcre module can use all the optimization it can get). */
112
113 volatile int dummy;
114 /* ========================================================================= */
115 #endif
116
117 do
118   {
119   const uschar *tcode = code + 1 + LINK_SIZE;
120   BOOL try_next = TRUE;
121
122   while (try_next)
123     {
124     /* If a branch starts with a bracket or a positive lookahead assertion,
125     recurse to set bits from within them. That's all for this branch. */
126
127     if ((int)*tcode >= OP_BRA || *tcode == OP_ASSERT)
128       {
129       if (!set_start_bits(tcode, start_bits, caseless, utf8, cd))
130         return FALSE;
131       try_next = FALSE;
132       }
133
134     else switch(*tcode)
135       {
136       default:
137       return FALSE;
138
139       /* Skip over callout */
140
141       case OP_CALLOUT:
142       tcode += 2 + 2*LINK_SIZE;
143       break;
144
145       /* Skip over extended extraction bracket number */
146
147       case OP_BRANUMBER:
148       tcode += 3;
149       break;
150
151       /* Skip over lookbehind and negative lookahead assertions */
152
153       case OP_ASSERT_NOT:
154       case OP_ASSERTBACK:
155       case OP_ASSERTBACK_NOT:
156       do tcode += GET(tcode, 1); while (*tcode == OP_ALT);
157       tcode += 1+LINK_SIZE;
158       break;
159
160       /* Skip over an option setting, changing the caseless flag */
161
162       case OP_OPT:
163       caseless = (tcode[1] & PCRE_CASELESS) != 0;
164       tcode += 2;
165       break;
166
167       /* BRAZERO does the bracket, but carries on. */
168
169       case OP_BRAZERO:
170       case OP_BRAMINZERO:
171       if (!set_start_bits(++tcode, start_bits, caseless, utf8, cd))
172         return FALSE;
173 /* =========================================================================
174       See the comment at the head of this function concerning the next line,
175       which was an old fudge for the benefit of OS/2.
176       dummy = 1;
177   ========================================================================= */
178       do tcode += GET(tcode,1); while (*tcode == OP_ALT);
179       tcode += 1+LINK_SIZE;
180       break;
181
182       /* Single-char * or ? sets the bit and tries the next item */
183
184       case OP_STAR:
185       case OP_MINSTAR:
186       case OP_QUERY:
187       case OP_MINQUERY:
188       set_bit(start_bits, tcode[1], caseless, cd);
189       tcode += 2;
190 #ifdef SUPPORT_UTF8
191       if (utf8) while ((*tcode & 0xc0) == 0x80) tcode++;
192 #endif
193       break;
194
195       /* Single-char upto sets the bit and tries the next */
196
197       case OP_UPTO:
198       case OP_MINUPTO:
199       set_bit(start_bits, tcode[3], caseless, cd);
200       tcode += 4;
201 #ifdef SUPPORT_UTF8
202       if (utf8) while ((*tcode & 0xc0) == 0x80) tcode++;
203 #endif
204       break;
205
206       /* At least one single char sets the bit and stops */
207
208       case OP_EXACT:       /* Fall through */
209       tcode += 2;
210
211       case OP_CHAR:
212       case OP_CHARNC:
213       case OP_PLUS:
214       case OP_MINPLUS:
215       set_bit(start_bits, tcode[1], caseless, cd);
216       try_next = FALSE;
217       break;
218
219       /* Single character type sets the bits and stops */
220
221       case OP_NOT_DIGIT:
222       for (c = 0; c < 32; c++)
223         start_bits[c] |= ~cd->cbits[c+cbit_digit];
224       try_next = FALSE;
225       break;
226
227       case OP_DIGIT:
228       for (c = 0; c < 32; c++)
229         start_bits[c] |= cd->cbits[c+cbit_digit];
230       try_next = FALSE;
231       break;
232
233       /* The cbit_space table has vertical tab as whitespace; we have to
234       discard it. */
235
236       case OP_NOT_WHITESPACE:
237       for (c = 0; c < 32; c++)
238         {
239         int d = cd->cbits[c+cbit_space];
240         if (c == 1) d &= ~0x08;
241         start_bits[c] |= ~d;
242         }
243       try_next = FALSE;
244       break;
245
246       /* The cbit_space table has vertical tab as whitespace; we have to
247       discard it. */
248
249       case OP_WHITESPACE:
250       for (c = 0; c < 32; c++)
251         {
252         int d = cd->cbits[c+cbit_space];
253         if (c == 1) d &= ~0x08;
254         start_bits[c] |= d;
255         }
256       try_next = FALSE;
257       break;
258
259       case OP_NOT_WORDCHAR:
260       for (c = 0; c < 32; c++)
261         start_bits[c] |= ~cd->cbits[c+cbit_word];
262       try_next = FALSE;
263       break;
264
265       case OP_WORDCHAR:
266       for (c = 0; c < 32; c++)
267         start_bits[c] |= cd->cbits[c+cbit_word];
268       try_next = FALSE;
269       break;
270
271       /* One or more character type fudges the pointer and restarts, knowing
272       it will hit a single character type and stop there. */
273
274       case OP_TYPEPLUS:
275       case OP_TYPEMINPLUS:
276       tcode++;
277       break;
278
279       case OP_TYPEEXACT:
280       tcode += 3;
281       break;
282
283       /* Zero or more repeats of character types set the bits and then
284       try again. */
285
286       case OP_TYPEUPTO:
287       case OP_TYPEMINUPTO:
288       tcode += 2;               /* Fall through */
289
290       case OP_TYPESTAR:
291       case OP_TYPEMINSTAR:
292       case OP_TYPEQUERY:
293       case OP_TYPEMINQUERY:
294       switch(tcode[1])
295         {
296         case OP_ANY:
297         return FALSE;
298
299         case OP_NOT_DIGIT:
300         for (c = 0; c < 32; c++)
301           start_bits[c] |= ~cd->cbits[c+cbit_digit];
302         break;
303
304         case OP_DIGIT:
305         for (c = 0; c < 32; c++)
306           start_bits[c] |= cd->cbits[c+cbit_digit];
307         break;
308
309         /* The cbit_space table has vertical tab as whitespace; we have to
310         discard it. */
311
312         case OP_NOT_WHITESPACE:
313         for (c = 0; c < 32; c++)
314           {
315           int d = cd->cbits[c+cbit_space];
316           if (c == 1) d &= ~0x08;
317           start_bits[c] |= ~d;
318           }
319         break;
320
321         /* The cbit_space table has vertical tab as whitespace; we have to
322         discard it. */
323
324         case OP_WHITESPACE:
325         for (c = 0; c < 32; c++)
326           {
327           int d = cd->cbits[c+cbit_space];
328           if (c == 1) d &= ~0x08;
329           start_bits[c] |= d;
330           }
331         break;
332
333         case OP_NOT_WORDCHAR:
334         for (c = 0; c < 32; c++)
335           start_bits[c] |= ~cd->cbits[c+cbit_word];
336         break;
337
338         case OP_WORDCHAR:
339         for (c = 0; c < 32; c++)
340           start_bits[c] |= cd->cbits[c+cbit_word];
341         break;
342         }
343
344       tcode += 2;
345       break;
346
347       /* Character class where all the information is in a bit map: set the
348       bits and either carry on or not, according to the repeat count. If it was
349       a negative class, and we are operating with UTF-8 characters, any byte
350       with a value >= 0xc4 is a potentially valid starter because it starts a
351       character with a value > 255. */
352
353       case OP_NCLASS:
354       if (utf8)
355         {
356         start_bits[24] |= 0xf0;              /* Bits for 0xc4 - 0xc8 */
357         memset(start_bits+25, 0xff, 7);      /* Bits for 0xc9 - 0xff */
358         }
359       /* Fall through */
360
361       case OP_CLASS:
362         {
363         tcode++;
364
365         /* In UTF-8 mode, the bits in a bit map correspond to character
366         values, not to byte values. However, the bit map we are constructing is
367         for byte values. So we have to do a conversion for characters whose
368         value is > 127. In fact, there are only two possible starting bytes for
369         characters in the range 128 - 255. */
370
371         if (utf8)
372           {
373           for (c = 0; c < 16; c++) start_bits[c] |= tcode[c];
374           for (c = 128; c < 256; c++)
375             {
376             if ((tcode[c/8] && (1 << (c&7))) != 0)
377               {
378               int d = (c >> 6) | 0xc0;            /* Set bit for this starter */
379               start_bits[d/8] |= (1 << (d&7));    /* and then skip on to the */
380               c = (c & 0xc0) + 0x40 - 1;          /* next relevant character. */
381               }
382             }
383           }
384
385         /* In non-UTF-8 mode, the two bit maps are completely compatible. */
386
387         else
388           {
389           for (c = 0; c < 32; c++) start_bits[c] |= tcode[c];
390           }
391
392         /* Advance past the bit map, and act on what follows */
393
394         tcode += 32;
395         switch (*tcode)
396           {
397           case OP_CRSTAR:
398           case OP_CRMINSTAR:
399           case OP_CRQUERY:
400           case OP_CRMINQUERY:
401           tcode++;
402           break;
403
404           case OP_CRRANGE:
405           case OP_CRMINRANGE:
406           if (((tcode[1] << 8) + tcode[2]) == 0) tcode += 5;
407             else try_next = FALSE;
408           break;
409
410           default:
411           try_next = FALSE;
412           break;
413           }
414         }
415       break; /* End of bitmap class handling */
416
417       }      /* End of switch */
418     }        /* End of try_next loop */
419
420   code += GET(code, 1);   /* Advance to next branch */
421   }
422 while (*code == OP_ALT);
423 return TRUE;
424 }
425
426
427
428 /*************************************************
429 *          Study a compiled expression           *
430 *************************************************/
431
432 /* This function is handed a compiled expression that it must study to produce
433 information that will speed up the matching. It returns a pcre_extra block
434 which then gets handed back to pcre_exec().
435
436 Arguments:
437   re        points to the compiled expression
438   options   contains option bits
439   errorptr  points to where to place error messages;
440             set NULL unless error
441
442 Returns:    pointer to a pcre_extra block, with study_data filled in and the
443               appropriate flag set;
444             NULL on error or if no optimization possible
445 */
446
447 PCRE_DATA_SCOPE pcre_extra *
448 pcre_study(const pcre *external_re, int options, const char **errorptr)
449 {
450 uschar start_bits[32];
451 pcre_extra *extra;
452 pcre_study_data *study;
453 const uschar *tables;
454 uschar *code;
455 compile_data compile_block;
456 const real_pcre *re = (const real_pcre *)external_re;
457
458 *errorptr = NULL;
459
460 if (re == NULL || re->magic_number != MAGIC_NUMBER)
461   {
462   *errorptr = "argument is not a compiled regular expression";
463   return NULL;
464   }
465
466 if ((options & ~PUBLIC_STUDY_OPTIONS) != 0)
467   {
468   *errorptr = "unknown or incorrect option bit(s) set";
469   return NULL;
470   }
471
472 code = (uschar *)re + re->name_table_offset +
473   (re->name_count * re->name_entry_size);
474
475 /* For an anchored pattern, or an unanchored pattern that has a first char, or
476 a multiline pattern that matches only at "line starts", no further processing
477 at present. */
478
479 if ((re->options & (PCRE_ANCHORED|PCRE_FIRSTSET|PCRE_STARTLINE)) != 0)
480   return NULL;
481
482 /* Set the character tables in the block that is passed around */
483
484 tables = re->tables;
485 if (tables == NULL)
486   (void)pcre_fullinfo(external_re, NULL, PCRE_INFO_DEFAULT_TABLES,
487   (void *)(&tables));
488
489 compile_block.lcc = tables + lcc_offset;
490 compile_block.fcc = tables + fcc_offset;
491 compile_block.cbits = tables + cbits_offset;
492 compile_block.ctypes = tables + ctypes_offset;
493
494 /* See if we can find a fixed set of initial characters for the pattern. */
495
496 memset(start_bits, 0, 32 * sizeof(uschar));
497 if (!set_start_bits(code, start_bits, (re->options & PCRE_CASELESS) != 0,
498   (re->options & PCRE_UTF8) != 0, &compile_block)) return NULL;
499
500 /* Get a pcre_extra block and a pcre_study_data block. The study data is put in
501 the latter, which is pointed to by the former, which may also get additional
502 data set later by the calling program. At the moment, the size of
503 pcre_study_data is fixed. We nevertheless save it in a field for returning via
504 the pcre_fullinfo() function so that if it becomes variable in the future, we
505 don't have to change that code. */
506
507 extra = (pcre_extra *)(pcre_malloc)
508   (sizeof(pcre_extra) + sizeof(pcre_study_data));
509
510 if (extra == NULL)
511   {
512   *errorptr = "failed to get memory";
513   return NULL;
514   }
515
516 study = (pcre_study_data *)((char *)extra + sizeof(pcre_extra));
517 extra->flags = PCRE_EXTRA_STUDY_DATA;
518 extra->study_data = study;
519
520 study->size = sizeof(pcre_study_data);
521 study->options = PCRE_STUDY_MAPPED;
522 memcpy(study->start_bits, start_bits, sizeof(start_bits));
523
524 return extra;
525 }
526
527 /* End of pcre_study.c */