Install PCRE 6.7 in in place of 6.2.
[exim.git] / src / src / pcre / pcre_internal.h
1 /* $Cambridge: exim/src/src/pcre/pcre_internal.h,v 1.3 2006/11/07 16:50:36 ph10 Exp $ */
2
3 /*************************************************
4 *      Perl-Compatible Regular Expressions       *
5 *************************************************/
6
7
8 /* PCRE is a library of functions to support regular expressions whose syntax
9 and semantics are as close as possible to those of the Perl 5 language.
10
11                        Written by Philip Hazel
12            Copyright (c) 1997-2006 University of Cambridge
13
14 -----------------------------------------------------------------------------
15 Redistribution and use in source and binary forms, with or without
16 modification, are permitted provided that the following conditions are met:
17
18     * Redistributions of source code must retain the above copyright notice,
19       this list of conditions and the following disclaimer.
20
21     * Redistributions in binary form must reproduce the above copyright
22       notice, this list of conditions and the following disclaimer in the
23       documentation and/or other materials provided with the distribution.
24
25     * Neither the name of the University of Cambridge nor the names of its
26       contributors may be used to endorse or promote products derived from
27       this software without specific prior written permission.
28
29 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
30 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
33 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 POSSIBILITY OF SUCH DAMAGE.
40 -----------------------------------------------------------------------------
41 */
42
43 /* This header contains definitions that are shared between the different
44 modules, but which are not relevant to the exported API. This includes some
45 functions whose names all begin with "_pcre_". */
46
47 #ifndef PCRE_INTERNAL_H
48 #define PCRE_INTERNAL_H
49
50 /* Define DEBUG to get debugging output on stdout. */
51
52 #if 0
53 #define DEBUG
54 #endif
55
56 /* Use a macro for debugging printing, 'cause that eliminates the use of #ifdef
57 inline, and there are *still* stupid compilers about that don't like indented
58 pre-processor statements, or at least there were when I first wrote this. After
59 all, it had only been about 10 years then... */
60
61 #ifdef DEBUG
62 #define DPRINTF(p) printf p
63 #else
64 #define DPRINTF(p) /*nothing*/
65 #endif
66
67
68 /* Get the definitions provided by running "configure" */
69
70 #include "config.h"
71
72 /* Standard C headers plus the external interface definition. The only time
73 setjmp and stdarg are used is when NO_RECURSE is set. */
74
75 #include <ctype.h>
76 #include <limits.h>
77 #include <setjmp.h>
78 #include <stdarg.h>
79 #include <stddef.h>
80 #include <stdio.h>
81 #include <stdlib.h>
82 #include <string.h>
83
84 #ifndef PCRE_SPY
85 #define PCRE_DEFINITION       /* Win32 __declspec(export) trigger for .dll */
86 #endif
87
88 /* We need to have types that specify unsigned 16-bit and 32-bit integers. We
89 cannot determine these outside the compilation (e.g. by running a program as
90 part of "configure") because PCRE is often cross-compiled for use on other
91 systems. Instead we make use of the maximum sizes that are available at
92 preprocessor time in standard C environments. */
93
94 #if USHRT_MAX == 65535
95   typedef unsigned short pcre_uint16;
96 #elif UINT_MAX == 65535
97   typedef unsigned int pcre_uint16;
98 #else
99   #error Cannot determine a type for 16-bit unsigned integers
100 #endif
101
102 #if UINT_MAX == 4294967295
103   typedef unsigned int pcre_uint32;
104 #elif ULONG_MAX == 4294967295
105   typedef unsigned long int pcre_uint32;
106 #else
107   #error Cannot determine a type for 32-bit unsigned integers
108 #endif
109
110 /* All character handling must be done as unsigned characters. Otherwise there
111 are problems with top-bit-set characters and functions such as isspace().
112 However, we leave the interface to the outside world as char *, because that
113 should make things easier for callers. We define a short type for unsigned char
114 to save lots of typing. I tried "uchar", but it causes problems on Digital
115 Unix, where it is defined in sys/types, so use "uschar" instead. */
116
117 typedef unsigned char uschar;
118
119 /* PCRE is able to support 3 different kinds of newline (CR, LF, CRLF). The
120 following macro is used to package up testing for newlines. NLBLOCK is defined
121 in the various modules to indicate in which datablock the parameters exist. */
122
123 #define IS_NEWLINE(p) \
124   ((p)[0] == NLBLOCK->nl[0] && \
125   (NLBLOCK->nllen == 1 || (p)[1] == NLBLOCK->nl[1]))
126
127 /* When PCRE is compiled as a C++ library, the subject pointer can be replaced
128 with a custom type. This makes it possible, for example, to allow pcre_exec()
129 to process subject strings that are discontinuous by using a smart pointer
130 class. It must always be possible to inspect all of the subject string in
131 pcre_exec() because of the way it backtracks. Two macros are required in the
132 normal case, for sign-unspecified and unsigned char pointers. The former is
133 used for the external interface and appears in pcre.h, which is why its name
134 must begin with PCRE_. */
135
136 #ifdef CUSTOM_SUBJECT_PTR
137 #define PCRE_SPTR CUSTOM_SUBJECT_PTR
138 #define USPTR CUSTOM_SUBJECT_PTR
139 #else
140 #define PCRE_SPTR const char *
141 #define USPTR const unsigned char *
142 #endif
143
144 /* Include the public PCRE header and the definitions of UCP character property
145 values. */
146
147 #include "pcre.h"
148 #include "ucp.h"
149
150 /* When compiling for use with the Virtual Pascal compiler, these functions
151 need to have their names changed. PCRE must be compiled with the -DVPCOMPAT
152 option on the command line. */
153
154 #ifdef VPCOMPAT
155 #define strncmp(s1,s2,m) _strncmp(s1,s2,m)
156 #define memcpy(d,s,n)    _memcpy(d,s,n)
157 #define memmove(d,s,n)   _memmove(d,s,n)
158 #define memset(s,c,n)    _memset(s,c,n)
159 #else  /* VPCOMPAT */
160
161 /* To cope with SunOS4 and other systems that lack memmove() but have bcopy(),
162 define a macro for memmove() if HAVE_MEMMOVE is false, provided that HAVE_BCOPY
163 is set. Otherwise, include an emulating function for those systems that have
164 neither (there some non-Unix environments where this is the case). This assumes
165 that all calls to memmove are moving strings upwards in store, which is the
166 case in PCRE. */
167
168 #if ! HAVE_MEMMOVE
169 #undef  memmove        /* some systems may have a macro */
170 #if HAVE_BCOPY
171 #define memmove(a, b, c) bcopy(b, a, c)
172 #else  /* HAVE_BCOPY */
173 static void *
174 pcre_memmove(unsigned char *dest, const unsigned char *src, size_t n)
175 {
176 size_t i;
177 dest += n;
178 src += n;
179 for (i = 0; i < n; ++i) *(--dest) =  *(--src);
180 return dest;
181 }
182 #define memmove(a, b, c) pcre_memmove(a, b, c)
183 #endif   /* not HAVE_BCOPY */
184 #endif   /* not HAVE_MEMMOVE */
185 #endif   /* not VPCOMPAT */
186
187
188 /* PCRE keeps offsets in its compiled code as 2-byte quantities (always stored
189 in big-endian order) by default. These are used, for example, to link from the
190 start of a subpattern to its alternatives and its end. The use of 2 bytes per
191 offset limits the size of the compiled regex to around 64K, which is big enough
192 for almost everybody. However, I received a request for an even bigger limit.
193 For this reason, and also to make the code easier to maintain, the storing and
194 loading of offsets from the byte string is now handled by the macros that are
195 defined here.
196
197 The macros are controlled by the value of LINK_SIZE. This defaults to 2 in
198 the config.h file, but can be overridden by using -D on the command line. This
199 is automated on Unix systems via the "configure" command. */
200
201 #if LINK_SIZE == 2
202
203 #define PUT(a,n,d)   \
204   (a[n] = (d) >> 8), \
205   (a[(n)+1] = (d) & 255)
206
207 #define GET(a,n) \
208   (((a)[n] << 8) | (a)[(n)+1])
209
210 #define MAX_PATTERN_SIZE (1 << 16)
211
212
213 #elif LINK_SIZE == 3
214
215 #define PUT(a,n,d)       \
216   (a[n] = (d) >> 16),    \
217   (a[(n)+1] = (d) >> 8), \
218   (a[(n)+2] = (d) & 255)
219
220 #define GET(a,n) \
221   (((a)[n] << 16) | ((a)[(n)+1] << 8) | (a)[(n)+2])
222
223 #define MAX_PATTERN_SIZE (1 << 24)
224
225
226 #elif LINK_SIZE == 4
227
228 #define PUT(a,n,d)        \
229   (a[n] = (d) >> 24),     \
230   (a[(n)+1] = (d) >> 16), \
231   (a[(n)+2] = (d) >> 8),  \
232   (a[(n)+3] = (d) & 255)
233
234 #define GET(a,n) \
235   (((a)[n] << 24) | ((a)[(n)+1] << 16) | ((a)[(n)+2] << 8) | (a)[(n)+3])
236
237 #define MAX_PATTERN_SIZE (1 << 30)   /* Keep it positive */
238
239
240 #else
241 #error LINK_SIZE must be either 2, 3, or 4
242 #endif
243
244
245 /* Convenience macro defined in terms of the others */
246
247 #define PUTINC(a,n,d)   PUT(a,n,d), a += LINK_SIZE
248
249
250 /* PCRE uses some other 2-byte quantities that do not change when the size of
251 offsets changes. There are used for repeat counts and for other things such as
252 capturing parenthesis numbers in back references. */
253
254 #define PUT2(a,n,d)   \
255   a[n] = (d) >> 8; \
256   a[(n)+1] = (d) & 255
257
258 #define GET2(a,n) \
259   (((a)[n] << 8) | (a)[(n)+1])
260
261 #define PUT2INC(a,n,d)  PUT2(a,n,d), a += 2
262
263
264 /* When UTF-8 encoding is being used, a character is no longer just a single
265 byte. The macros for character handling generate simple sequences when used in
266 byte-mode, and more complicated ones for UTF-8 characters. */
267
268 #ifndef SUPPORT_UTF8
269 #define GETCHAR(c, eptr) c = *eptr;
270 #define GETCHARTEST(c, eptr) c = *eptr;
271 #define GETCHARINC(c, eptr) c = *eptr++;
272 #define GETCHARINCTEST(c, eptr) c = *eptr++;
273 #define GETCHARLEN(c, eptr, len) c = *eptr;
274 #define BACKCHAR(eptr)
275
276 #else   /* SUPPORT_UTF8 */
277
278 /* Get the next UTF-8 character, not advancing the pointer. This is called when
279 we know we are in UTF-8 mode. */
280
281 #define GETCHAR(c, eptr) \
282   c = *eptr; \
283   if ((c & 0xc0) == 0xc0) \
284     { \
285     int gcii; \
286     int gcaa = _pcre_utf8_table4[c & 0x3f];  /* Number of additional bytes */ \
287     int gcss = 6*gcaa; \
288     c = (c & _pcre_utf8_table3[gcaa]) << gcss; \
289     for (gcii = 1; gcii <= gcaa; gcii++) \
290       { \
291       gcss -= 6; \
292       c |= (eptr[gcii] & 0x3f) << gcss; \
293       } \
294     }
295
296 /* Get the next UTF-8 character, testing for UTF-8 mode, and not advancing the
297 pointer. */
298
299 #define GETCHARTEST(c, eptr) \
300   c = *eptr; \
301   if (utf8 && (c & 0xc0) == 0xc0) \
302     { \
303     int gcii; \
304     int gcaa = _pcre_utf8_table4[c & 0x3f];  /* Number of additional bytes */ \
305     int gcss = 6*gcaa; \
306     c = (c & _pcre_utf8_table3[gcaa]) << gcss; \
307     for (gcii = 1; gcii <= gcaa; gcii++) \
308       { \
309       gcss -= 6; \
310       c |= (eptr[gcii] & 0x3f) << gcss; \
311       } \
312     }
313
314 /* Get the next UTF-8 character, advancing the pointer. This is called when we
315 know we are in UTF-8 mode. */
316
317 #define GETCHARINC(c, eptr) \
318   c = *eptr++; \
319   if ((c & 0xc0) == 0xc0) \
320     { \
321     int gcaa = _pcre_utf8_table4[c & 0x3f];  /* Number of additional bytes */ \
322     int gcss = 6*gcaa; \
323     c = (c & _pcre_utf8_table3[gcaa]) << gcss; \
324     while (gcaa-- > 0) \
325       { \
326       gcss -= 6; \
327       c |= (*eptr++ & 0x3f) << gcss; \
328       } \
329     }
330
331 /* Get the next character, testing for UTF-8 mode, and advancing the pointer */
332
333 #define GETCHARINCTEST(c, eptr) \
334   c = *eptr++; \
335   if (utf8 && (c & 0xc0) == 0xc0) \
336     { \
337     int gcaa = _pcre_utf8_table4[c & 0x3f];  /* Number of additional bytes */ \
338     int gcss = 6*gcaa; \
339     c = (c & _pcre_utf8_table3[gcaa]) << gcss; \
340     while (gcaa-- > 0) \
341       { \
342       gcss -= 6; \
343       c |= (*eptr++ & 0x3f) << gcss; \
344       } \
345     }
346
347 /* Get the next UTF-8 character, not advancing the pointer, incrementing length
348 if there are extra bytes. This is called when we know we are in UTF-8 mode. */
349
350 #define GETCHARLEN(c, eptr, len) \
351   c = *eptr; \
352   if ((c & 0xc0) == 0xc0) \
353     { \
354     int gcii; \
355     int gcaa = _pcre_utf8_table4[c & 0x3f];  /* Number of additional bytes */ \
356     int gcss = 6*gcaa; \
357     c = (c & _pcre_utf8_table3[gcaa]) << gcss; \
358     for (gcii = 1; gcii <= gcaa; gcii++) \
359       { \
360       gcss -= 6; \
361       c |= (eptr[gcii] & 0x3f) << gcss; \
362       } \
363     len += gcaa; \
364     }
365
366 /* If the pointer is not at the start of a character, move it back until
367 it is. Called only in UTF-8 mode. */
368
369 #define BACKCHAR(eptr) while((*eptr & 0xc0) == 0x80) eptr--;
370
371 #endif
372
373
374 /* In case there is no definition of offsetof() provided - though any proper
375 Standard C system should have one. */
376
377 #ifndef offsetof
378 #define offsetof(p_type,field) ((size_t)&(((p_type *)0)->field))
379 #endif
380
381
382 /* These are the public options that can change during matching. */
383
384 #define PCRE_IMS (PCRE_CASELESS|PCRE_MULTILINE|PCRE_DOTALL)
385
386 /* Private options flags start at the most significant end of the four bytes.
387 The public options defined in pcre.h start at the least significant end. Make
388 sure they don't overlap! The bits are getting a bit scarce now -- when we run
389 out, there is a dummy word in the structure that could be used for the private
390 bits. */
391
392 #define PCRE_NOPARTIAL     0x80000000  /* can't use partial with this regex */
393 #define PCRE_FIRSTSET      0x40000000  /* first_byte is set */
394 #define PCRE_REQCHSET      0x20000000  /* req_byte is set */
395 #define PCRE_STARTLINE     0x10000000  /* start after \n for multiline */
396 #define PCRE_JCHANGED      0x08000000  /* j option changes within regex */
397
398 /* Options for the "extra" block produced by pcre_study(). */
399
400 #define PCRE_STUDY_MAPPED   0x01     /* a map of starting chars exists */
401
402 /* Masks for identifying the public options that are permitted at compile
403 time, run time, or study time, respectively. */
404
405 #define PUBLIC_OPTIONS \
406   (PCRE_CASELESS|PCRE_EXTENDED|PCRE_ANCHORED|PCRE_MULTILINE| \
407    PCRE_DOTALL|PCRE_DOLLAR_ENDONLY|PCRE_EXTRA|PCRE_UNGREEDY|PCRE_UTF8| \
408    PCRE_NO_AUTO_CAPTURE|PCRE_NO_UTF8_CHECK|PCRE_AUTO_CALLOUT|PCRE_FIRSTLINE| \
409    PCRE_DUPNAMES|PCRE_NEWLINE_CR|PCRE_NEWLINE_LF)
410
411 #define PUBLIC_EXEC_OPTIONS \
412   (PCRE_ANCHORED|PCRE_NOTBOL|PCRE_NOTEOL|PCRE_NOTEMPTY|PCRE_NO_UTF8_CHECK| \
413    PCRE_PARTIAL|PCRE_NEWLINE_CR|PCRE_NEWLINE_LF)
414
415 #define PUBLIC_DFA_EXEC_OPTIONS \
416   (PCRE_ANCHORED|PCRE_NOTBOL|PCRE_NOTEOL|PCRE_NOTEMPTY|PCRE_NO_UTF8_CHECK| \
417    PCRE_PARTIAL|PCRE_DFA_SHORTEST|PCRE_DFA_RESTART|PCRE_NEWLINE_CR| \
418    PCRE_NEWLINE_LF)
419
420 #define PUBLIC_STUDY_OPTIONS 0   /* None defined */
421
422 /* Magic number to provide a small check against being handed junk. Also used
423 to detect whether a pattern was compiled on a host of different endianness. */
424
425 #define MAGIC_NUMBER  0x50435245UL   /* 'PCRE' */
426
427 /* Negative values for the firstchar and reqchar variables */
428
429 #define REQ_UNSET (-2)
430 #define REQ_NONE  (-1)
431
432 /* The maximum remaining length of subject we are prepared to search for a
433 req_byte match. */
434
435 #define REQ_BYTE_MAX 1000
436
437 /* Flags added to firstbyte or reqbyte; a "non-literal" item is either a
438 variable-length repeat, or a anything other than literal characters. */
439
440 #define REQ_CASELESS 0x0100    /* indicates caselessness */
441 #define REQ_VARY     0x0200    /* reqbyte followed non-literal item */
442
443 /* Miscellaneous definitions */
444
445 typedef int BOOL;
446
447 #define FALSE   0
448 #define TRUE    1
449
450 /* Escape items that are just an encoding of a particular data value. Note that
451 ESC_n is defined as yet another macro, which is set in config.h to either \n
452 (the default) or \r (which some people want). */
453
454 #ifndef ESC_e
455 #define ESC_e 27
456 #endif
457
458 #ifndef ESC_f
459 #define ESC_f '\f'
460 #endif
461
462 #ifndef ESC_n
463 #define ESC_n NEWLINE
464 #endif
465
466 #ifndef ESC_r
467 #define ESC_r '\r'
468 #endif
469
470 /* We can't officially use ESC_t because it is a POSIX reserved identifier
471 (presumably because of all the others like size_t). */
472
473 #ifndef ESC_tee
474 #define ESC_tee '\t'
475 #endif
476
477 /* Codes for different types of Unicode property */
478
479 #define PT_ANY        0    /* Any property - matches all chars */
480 #define PT_LAMP       1    /* L& - the union of Lu, Ll, Lt */
481 #define PT_GC         2    /* General characteristic (e.g. L) */
482 #define PT_PC         3    /* Particular characteristic (e.g. Lu) */
483 #define PT_SC         4    /* Script (e.g. Han) */
484
485 /* Flag bits and data types for the extended class (OP_XCLASS) for classes that
486 contain UTF-8 characters with values greater than 255. */
487
488 #define XCL_NOT    0x01    /* Flag: this is a negative class */
489 #define XCL_MAP    0x02    /* Flag: a 32-byte map is present */
490
491 #define XCL_END       0    /* Marks end of individual items */
492 #define XCL_SINGLE    1    /* Single item (one multibyte char) follows */
493 #define XCL_RANGE     2    /* A range (two multibyte chars) follows */
494 #define XCL_PROP      3    /* Unicode property (2-byte property code follows) */
495 #define XCL_NOTPROP   4    /* Unicode inverted property (ditto) */
496
497 /* These are escaped items that aren't just an encoding of a particular data
498 value such as \n. They must have non-zero values, as check_escape() returns
499 their negation. Also, they must appear in the same order as in the opcode
500 definitions below, up to ESC_z. There's a dummy for OP_ANY because it
501 corresponds to "." rather than an escape sequence. The final one must be
502 ESC_REF as subsequent values are used for \1, \2, \3, etc. There is are two
503 tests in the code for an escape greater than ESC_b and less than ESC_Z to
504 detect the types that may be repeated. These are the types that consume
505 characters. If any new escapes are put in between that don't consume a
506 character, that code will have to change. */
507
508 enum { ESC_A = 1, ESC_G, ESC_B, ESC_b, ESC_D, ESC_d, ESC_S, ESC_s, ESC_W,
509        ESC_w, ESC_dum1, ESC_C, ESC_P, ESC_p, ESC_X, ESC_Z, ESC_z, ESC_E,
510        ESC_Q, ESC_REF };
511
512 /* Opcode table: OP_BRA must be last, as all values >= it are used for brackets
513 that extract substrings. Starting from 1 (i.e. after OP_END), the values up to
514 OP_EOD must correspond in order to the list of escapes immediately above.
515 Note that whenever this list is updated, the two macro definitions that follow
516 must also be updated to match. */
517
518 enum {
519   OP_END,            /* 0 End of pattern */
520
521   /* Values corresponding to backslashed metacharacters */
522
523   OP_SOD,            /* 1 Start of data: \A */
524   OP_SOM,            /* 2 Start of match (subject + offset): \G */
525   OP_NOT_WORD_BOUNDARY,  /*  3 \B */
526   OP_WORD_BOUNDARY,      /*  4 \b */
527   OP_NOT_DIGIT,          /*  5 \D */
528   OP_DIGIT,              /*  6 \d */
529   OP_NOT_WHITESPACE,     /*  7 \S */
530   OP_WHITESPACE,         /*  8 \s */
531   OP_NOT_WORDCHAR,       /*  9 \W */
532   OP_WORDCHAR,           /* 10 \w */
533   OP_ANY,            /* 11 Match any character */
534   OP_ANYBYTE,        /* 12 Match any byte (\C); different to OP_ANY for UTF-8 */
535   OP_NOTPROP,        /* 13 \P (not Unicode property) */
536   OP_PROP,           /* 14 \p (Unicode property) */
537   OP_EXTUNI,         /* 15 \X (extended Unicode sequence */
538   OP_EODN,           /* 16 End of data or \n at end of data: \Z. */
539   OP_EOD,            /* 17 End of data: \z */
540
541   OP_OPT,            /* 18 Set runtime options */
542   OP_CIRC,           /* 19 Start of line - varies with multiline switch */
543   OP_DOLL,           /* 20 End of line - varies with multiline switch */
544   OP_CHAR,           /* 21 Match one character, casefully */
545   OP_CHARNC,         /* 22 Match one character, caselessly */
546   OP_NOT,            /* 23 Match one character, not the following one */
547
548   OP_STAR,           /* 24 The maximizing and minimizing versions of */
549   OP_MINSTAR,        /* 25 all these opcodes must come in pairs, with */
550   OP_PLUS,           /* 26 the minimizing one second. */
551   OP_MINPLUS,        /* 27 This first set applies to single characters */
552   OP_QUERY,          /* 28 */
553   OP_MINQUERY,       /* 29 */
554   OP_UPTO,           /* 30 From 0 to n matches */
555   OP_MINUPTO,        /* 31 */
556   OP_EXACT,          /* 32 Exactly n matches */
557
558   OP_NOTSTAR,        /* 33 The maximizing and minimizing versions of */
559   OP_NOTMINSTAR,     /* 34 all these opcodes must come in pairs, with */
560   OP_NOTPLUS,        /* 35 the minimizing one second. */
561   OP_NOTMINPLUS,     /* 36 This set applies to "not" single characters */
562   OP_NOTQUERY,       /* 37 */
563   OP_NOTMINQUERY,    /* 38 */
564   OP_NOTUPTO,        /* 39 From 0 to n matches */
565   OP_NOTMINUPTO,     /* 40 */
566   OP_NOTEXACT,       /* 41 Exactly n matches */
567
568   OP_TYPESTAR,       /* 42 The maximizing and minimizing versions of */
569   OP_TYPEMINSTAR,    /* 43 all these opcodes must come in pairs, with */
570   OP_TYPEPLUS,       /* 44 the minimizing one second. These codes must */
571   OP_TYPEMINPLUS,    /* 45 be in exactly the same order as those above. */
572   OP_TYPEQUERY,      /* 46 This set applies to character types such as \d */
573   OP_TYPEMINQUERY,   /* 47 */
574   OP_TYPEUPTO,       /* 48 From 0 to n matches */
575   OP_TYPEMINUPTO,    /* 49 */
576   OP_TYPEEXACT,      /* 50 Exactly n matches */
577
578   OP_CRSTAR,         /* 51 The maximizing and minimizing versions of */
579   OP_CRMINSTAR,      /* 52 all these opcodes must come in pairs, with */
580   OP_CRPLUS,         /* 53 the minimizing one second. These codes must */
581   OP_CRMINPLUS,      /* 54 be in exactly the same order as those above. */
582   OP_CRQUERY,        /* 55 These are for character classes and back refs */
583   OP_CRMINQUERY,     /* 56 */
584   OP_CRRANGE,        /* 57 These are different to the three sets above. */
585   OP_CRMINRANGE,     /* 58 */
586
587   OP_CLASS,          /* 59 Match a character class, chars < 256 only */
588   OP_NCLASS,         /* 60 Same, but the bitmap was created from a negative
589                            class - the difference is relevant only when a UTF-8
590                            character > 255 is encountered. */
591
592   OP_XCLASS,         /* 61 Extended class for handling UTF-8 chars within the
593                            class. This does both positive and negative. */
594
595   OP_REF,            /* 62 Match a back reference */
596   OP_RECURSE,        /* 63 Match a numbered subpattern (possibly recursive) */
597   OP_CALLOUT,        /* 64 Call out to external function if provided */
598
599   OP_ALT,            /* 65 Start of alternation */
600   OP_KET,            /* 66 End of group that doesn't have an unbounded repeat */
601   OP_KETRMAX,        /* 67 These two must remain together and in this */
602   OP_KETRMIN,        /* 68 order. They are for groups the repeat for ever. */
603
604   /* The assertions must come before ONCE and COND */
605
606   OP_ASSERT,         /* 69 Positive lookahead */
607   OP_ASSERT_NOT,     /* 70 Negative lookahead */
608   OP_ASSERTBACK,     /* 71 Positive lookbehind */
609   OP_ASSERTBACK_NOT, /* 72 Negative lookbehind */
610   OP_REVERSE,        /* 73 Move pointer back - used in lookbehind assertions */
611
612   /* ONCE and COND must come after the assertions, with ONCE first, as there's
613   a test for >= ONCE for a subpattern that isn't an assertion. */
614
615   OP_ONCE,           /* 74 Once matched, don't back up into the subpattern */
616   OP_COND,           /* 75 Conditional group */
617   OP_CREF,           /* 76 Used to hold an extraction string number (cond ref) */
618
619   OP_BRAZERO,        /* 77 These two must remain together and in this */
620   OP_BRAMINZERO,     /* 78 order. */
621
622   OP_BRANUMBER,      /* 79 Used for extracting brackets whose number is greater
623                            than can fit into an opcode. */
624
625   OP_BRA             /* 80 This and greater values are used for brackets that
626                            extract substrings up to EXTRACT_BASIC_MAX. After
627                            that, use is made of OP_BRANUMBER. */
628 };
629
630 /* WARNING WARNING WARNING: There is an implicit assumption in pcre.c and
631 study.c that all opcodes are less than 128 in value. This makes handling UTF-8
632 character sequences easier. */
633
634 /* The highest extraction number before we have to start using additional
635 bytes. (Originally PCRE didn't have support for extraction counts highter than
636 this number.) The value is limited by the number of opcodes left after OP_BRA,
637 i.e. 255 - OP_BRA. We actually set it a bit lower to leave room for additional
638 opcodes. */
639
640 #define EXTRACT_BASIC_MAX  100
641
642
643 /* This macro defines textual names for all the opcodes. These are used only
644 for debugging. The macro is referenced only in pcre_printint.c. */
645
646 #define OP_NAME_LIST \
647   "End", "\\A", "\\G", "\\B", "\\b", "\\D", "\\d",                \
648   "\\S", "\\s", "\\W", "\\w", "Any", "Anybyte",                   \
649   "notprop", "prop", "extuni",                                    \
650   "\\Z", "\\z",                                                   \
651   "Opt", "^", "$", "char", "charnc", "not",                       \
652   "*", "*?", "+", "+?", "?", "??", "{", "{", "{",                 \
653   "*", "*?", "+", "+?", "?", "??", "{", "{", "{",                 \
654   "*", "*?", "+", "+?", "?", "??", "{", "{", "{",                 \
655   "*", "*?", "+", "+?", "?", "??", "{", "{",                      \
656   "class", "nclass", "xclass", "Ref", "Recurse", "Callout",       \
657   "Alt", "Ket", "KetRmax", "KetRmin", "Assert", "Assert not",     \
658   "AssertB", "AssertB not", "Reverse", "Once", "Cond", "Cond ref",\
659   "Brazero", "Braminzero", "Branumber", "Bra"
660
661
662 /* This macro defines the length of fixed length operations in the compiled
663 regex. The lengths are used when searching for specific things, and also in the
664 debugging printing of a compiled regex. We use a macro so that it can be
665 defined close to the definitions of the opcodes themselves.
666
667 As things have been extended, some of these are no longer fixed lenths, but are
668 minima instead. For example, the length of a single-character repeat may vary
669 in UTF-8 mode. The code that uses this table must know about such things. */
670
671 #define OP_LENGTHS \
672   1,                             /* End                                    */ \
673   1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* \A, \G, \B, \B, \D, \d, \S, \s, \W, \w */ \
674   1, 1,                          /* Any, Anybyte                           */ \
675   3, 3, 1,                       /* NOTPROP, PROP, EXTUNI                  */ \
676   1, 1, 2, 1, 1,                 /* \Z, \z, Opt, ^, $                      */ \
677   2,                             /* Char  - the minimum length             */ \
678   2,                             /* Charnc  - the minimum length           */ \
679   2,                             /* not                                    */ \
680   /* Positive single-char repeats                            ** These are  */ \
681   2, 2, 2, 2, 2, 2,              /* *, *?, +, +?, ?, ??      ** minima in  */ \
682   4, 4, 4,                       /* upto, minupto, exact     ** UTF-8 mode */ \
683   /* Negative single-char repeats - only for chars < 256                   */ \
684   2, 2, 2, 2, 2, 2,              /* NOT *, *?, +, +?, ?, ??                */ \
685   4, 4, 4,                       /* NOT upto, minupto, exact               */ \
686   /* Positive type repeats                                                 */ \
687   2, 2, 2, 2, 2, 2,              /* Type *, *?, +, +?, ?, ??               */ \
688   4, 4, 4,                       /* Type upto, minupto, exact              */ \
689   /* Character class & ref repeats                                         */ \
690   1, 1, 1, 1, 1, 1,              /* *, *?, +, +?, ?, ??                    */ \
691   5, 5,                          /* CRRANGE, CRMINRANGE                    */ \
692  33,                             /* CLASS                                  */ \
693  33,                             /* NCLASS                                 */ \
694   0,                             /* XCLASS - variable length               */ \
695   3,                             /* REF                                    */ \
696   1+LINK_SIZE,                   /* RECURSE                                */ \
697   2+2*LINK_SIZE,                 /* CALLOUT                                */ \
698   1+LINK_SIZE,                   /* Alt                                    */ \
699   1+LINK_SIZE,                   /* Ket                                    */ \
700   1+LINK_SIZE,                   /* KetRmax                                */ \
701   1+LINK_SIZE,                   /* KetRmin                                */ \
702   1+LINK_SIZE,                   /* Assert                                 */ \
703   1+LINK_SIZE,                   /* Assert not                             */ \
704   1+LINK_SIZE,                   /* Assert behind                          */ \
705   1+LINK_SIZE,                   /* Assert behind not                      */ \
706   1+LINK_SIZE,                   /* Reverse                                */ \
707   1+LINK_SIZE,                   /* Once                                   */ \
708   1+LINK_SIZE,                   /* COND                                   */ \
709   3,                             /* CREF                                   */ \
710   1, 1,                          /* BRAZERO, BRAMINZERO                    */ \
711   3,                             /* BRANUMBER                              */ \
712   1+LINK_SIZE                    /* BRA                                    */ \
713
714
715 /* A magic value for OP_CREF to indicate the "in recursion" condition. */
716
717 #define CREF_RECURSE  0xffff
718
719 /* Error code numbers. They are given names so that they can more easily be
720 tracked. */
721
722 enum { ERR0,  ERR1,  ERR2,  ERR3,  ERR4,  ERR5,  ERR6,  ERR7,  ERR8,  ERR9,
723        ERR10, ERR11, ERR12, ERR13, ERR14, ERR15, ERR16, ERR17, ERR18, ERR19,
724        ERR20, ERR21, ERR22, ERR23, ERR24, ERR25, ERR26, ERR27, ERR28, ERR29,
725        ERR30, ERR31, ERR32, ERR33, ERR34, ERR35, ERR36, ERR37, ERR38, ERR39,
726        ERR40, ERR41, ERR42, ERR43, ERR44, ERR45, ERR46, ERR47, ERR48, ERR49,
727        ERR50, ERR51 };
728
729 /* The real format of the start of the pcre block; the index of names and the
730 code vector run on as long as necessary after the end. We store an explicit
731 offset to the name table so that if a regex is compiled on one host, saved, and
732 then run on another where the size of pointers is different, all might still
733 be well. For the case of compiled-on-4 and run-on-8, we include an extra
734 pointer that is always NULL. For future-proofing, a few dummy fields were
735 originally included - even though you can never get this planning right - but
736 there is only one left now.
737
738 NOTE NOTE NOTE:
739 Because people can now save and re-use compiled patterns, any additions to this
740 structure should be made at the end, and something earlier (e.g. a new
741 flag in the options or one of the dummy fields) should indicate that the new
742 fields are present. Currently PCRE always sets the dummy fields to zero.
743 NOTE NOTE NOTE:
744 */
745
746 typedef struct real_pcre {
747   pcre_uint32 magic_number;
748   pcre_uint32 size;               /* Total that was malloced */
749   pcre_uint32 options;
750   pcre_uint32 dummy1;             /* For future use, maybe */
751
752   pcre_uint16 top_bracket;
753   pcre_uint16 top_backref;
754   pcre_uint16 first_byte;
755   pcre_uint16 req_byte;
756   pcre_uint16 name_table_offset;  /* Offset to name table that follows */
757   pcre_uint16 name_entry_size;    /* Size of any name items */
758   pcre_uint16 name_count;         /* Number of name items */
759   pcre_uint16 ref_count;          /* Reference count */
760
761   const unsigned char *tables;    /* Pointer to tables or NULL for std */
762   const unsigned char *nullpad;   /* NULL padding */
763 } real_pcre;
764
765 /* The format of the block used to store data from pcre_study(). The same
766 remark (see NOTE above) about extending this structure applies. */
767
768 typedef struct pcre_study_data {
769   pcre_uint32 size;               /* Total that was malloced */
770   pcre_uint32 options;
771   uschar start_bits[32];
772 } pcre_study_data;
773
774 /* Structure for passing "static" information around between the functions
775 doing the compiling, so that they are thread-safe. */
776
777 typedef struct compile_data {
778   const uschar *lcc;            /* Points to lower casing table */
779   const uschar *fcc;            /* Points to case-flipping table */
780   const uschar *cbits;          /* Points to character type table */
781   const uschar *ctypes;         /* Points to table of type maps */
782   const uschar *start_code;     /* The start of the compiled code */
783   const uschar *start_pattern;  /* The start of the pattern */
784   uschar *name_table;           /* The name/number table */
785   int  names_found;             /* Number of entries so far */
786   int  name_entry_size;         /* Size of each entry */
787   int  top_backref;             /* Maximum back reference */
788   unsigned int backref_map;     /* Bitmap of low back refs */
789   int  req_varyopt;             /* "After variable item" flag for reqbyte */
790   BOOL nopartial;               /* Set TRUE if partial won't work */
791   int  nllen;                   /* 1 or 2 for newline string length */
792   uschar nl[4];                 /* Newline string */
793 } compile_data;
794
795 /* Structure for maintaining a chain of pointers to the currently incomplete
796 branches, for testing for left recursion. */
797
798 typedef struct branch_chain {
799   struct branch_chain *outer;
800   uschar *current;
801 } branch_chain;
802
803 /* Structure for items in a linked list that represents an explicit recursive
804 call within the pattern. */
805
806 typedef struct recursion_info {
807   struct recursion_info *prevrec; /* Previous recursion record (or NULL) */
808   int group_num;                /* Number of group that was called */
809   const uschar *after_call;     /* "Return value": points after the call in the expr */
810   USPTR save_start;             /* Old value of md->start_match */
811   int *offset_save;             /* Pointer to start of saved offsets */
812   int saved_max;                /* Number of saved offsets */
813 } recursion_info;
814
815 /* When compiling in a mode that doesn't use recursive calls to match(),
816 a structure is used to remember local variables on the heap. It is defined in
817 pcre_exec.c, close to the match() function, so that it is easy to keep it in
818 step with any changes of local variable. However, the pointer to the current
819 frame must be saved in some "static" place over a longjmp(). We declare the
820 structure here so that we can put a pointer in the match_data structure. NOTE:
821 This isn't used for a "normal" compilation of pcre. */
822
823 struct heapframe;
824
825 /* Structure for passing "static" information around between the functions
826 doing traditional NFA matching, so that they are thread-safe. */
827
828 typedef struct match_data {
829   unsigned long int match_call_count;      /* As it says */
830   unsigned long int match_limit;           /* As it says */
831   unsigned long int match_limit_recursion; /* As it says */
832   int   *offset_vector;         /* Offset vector */
833   int    offset_end;            /* One past the end */
834   int    offset_max;            /* The maximum usable for return data */
835   int    nllen;                 /* 1 or 2 for newline string length */
836   uschar nl[4];                 /* Newline string */
837   const uschar *lcc;            /* Points to lower casing table */
838   const uschar *ctypes;         /* Points to table of type maps */
839   BOOL   offset_overflow;       /* Set if too many extractions */
840   BOOL   notbol;                /* NOTBOL flag */
841   BOOL   noteol;                /* NOTEOL flag */
842   BOOL   utf8;                  /* UTF8 flag */
843   BOOL   endonly;               /* Dollar not before final \n */
844   BOOL   notempty;              /* Empty string match not wanted */
845   BOOL   partial;               /* PARTIAL flag */
846   BOOL   hitend;                /* Hit the end of the subject at some point */
847   const uschar *start_code;     /* For use when recursing */
848   USPTR  start_subject;         /* Start of the subject string */
849   USPTR  end_subject;           /* End of the subject string */
850   USPTR  start_match;           /* Start of this match attempt */
851   USPTR  end_match_ptr;         /* Subject position at end match */
852   int    end_offset_top;        /* Highwater mark at end of match */
853   int    capture_last;          /* Most recent capture number */
854   int    start_offset;          /* The start offset value */
855   recursion_info *recursive;    /* Linked list of recursion data */
856   void  *callout_data;          /* To pass back to callouts */
857   struct heapframe *thisframe;  /* Used only when compiling for no recursion */
858 } match_data;
859
860 /* A similar structure is used for the same purpose by the DFA matching
861 functions. */
862
863 typedef struct dfa_match_data {
864   const uschar *start_code;     /* Start of the compiled pattern */
865   const uschar *start_subject;  /* Start of the subject string */
866   const uschar *end_subject;    /* End of subject string */
867   const uschar *tables;         /* Character tables */
868   int   moptions;               /* Match options */
869   int   poptions;               /* Pattern options */
870   int    nllen;                 /* 1 or 2 for newline string length */
871   uschar nl[4];                 /* Newline string */
872   void  *callout_data;          /* To pass back to callouts */
873 } dfa_match_data;
874
875 /* Bit definitions for entries in the pcre_ctypes table. */
876
877 #define ctype_space   0x01
878 #define ctype_letter  0x02
879 #define ctype_digit   0x04
880 #define ctype_xdigit  0x08
881 #define ctype_word    0x10   /* alphameric or '_' */
882 #define ctype_meta    0x80   /* regexp meta char or zero (end pattern) */
883
884 /* Offsets for the bitmap tables in pcre_cbits. Each table contains a set
885 of bits for a class map. Some classes are built by combining these tables. */
886
887 #define cbit_space     0      /* [:space:] or \s */
888 #define cbit_xdigit   32      /* [:xdigit:] */
889 #define cbit_digit    64      /* [:digit:] or \d */
890 #define cbit_upper    96      /* [:upper:] */
891 #define cbit_lower   128      /* [:lower:] */
892 #define cbit_word    160      /* [:word:] or \w */
893 #define cbit_graph   192      /* [:graph:] */
894 #define cbit_print   224      /* [:print:] */
895 #define cbit_punct   256      /* [:punct:] */
896 #define cbit_cntrl   288      /* [:cntrl:] */
897 #define cbit_length  320      /* Length of the cbits table */
898
899 /* Offsets of the various tables from the base tables pointer, and
900 total length. */
901
902 #define lcc_offset      0
903 #define fcc_offset    256
904 #define cbits_offset  512
905 #define ctypes_offset (cbits_offset + cbit_length)
906 #define tables_length (ctypes_offset + 256)
907
908 /* Layout of the UCP type table that translates property names into types and
909 codes. */
910
911 typedef struct {
912   const char *name;
913   pcre_uint16 type;
914   pcre_uint16 value;
915 } ucp_type_table;
916
917
918 /* Internal shared data tables. These are tables that are used by more than one
919 of the exported public functions. They have to be "external" in the C sense,
920 but are not part of the PCRE public API. The data for these tables is in the
921 pcre_tables.c module. */
922
923 extern const int    _pcre_utf8_table1[];
924 extern const int    _pcre_utf8_table2[];
925 extern const int    _pcre_utf8_table3[];
926 extern const uschar _pcre_utf8_table4[];
927
928 extern const int    _pcre_utf8_table1_size;
929
930 extern const ucp_type_table _pcre_utt[];
931 extern const int _pcre_utt_size;
932
933 extern const uschar _pcre_default_tables[];
934
935 extern const uschar _pcre_OP_lengths[];
936
937
938 /* Internal shared functions. These are functions that are used by more than
939 one of the exported public functions. They have to be "external" in the C
940 sense, but are not part of the PCRE public API. */
941
942 extern int         _pcre_ord2utf8(int, uschar *);
943 extern real_pcre * _pcre_try_flipped(const real_pcre *, real_pcre *,
944                      const pcre_study_data *, pcre_study_data *);
945 extern int         _pcre_ucp_findprop(const unsigned int, int *, int *);
946 extern int         _pcre_ucp_othercase(const int);
947 extern int         _pcre_valid_utf8(const uschar *, int);
948 extern BOOL        _pcre_xclass(int, const uschar *);
949
950 #endif
951
952 /* End of pcre_internal.h */