1 /*************************************************
2 * Perl-Compatible Regular Expressions *
3 *************************************************/
6 /* PCRE is a library of functions to support regular expressions whose syntax
7 and semantics are as close as possible to those of the Perl 5 language.
9 Written by Philip Hazel
10 Copyright (c) 1997-2006 University of Cambridge
11 Copyright (c) 2004, 2005 Apple Computer, Inc.
13 -----------------------------------------------------------------------------
14 Redistribution and use in source and binary forms, with or without
15 modification, are permitted provided that the following conditions are met:
17 * Redistributions of source code must retain the above copyright notice,
18 this list of conditions and the following disclaimer.
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.
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.
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 -----------------------------------------------------------------------------
42 /* This header contains definitions that are shared between the different
43 modules, but which are not relevant to the exported API. This includes some
44 functions whose names all begin with "_pcre_". */
46 #ifndef PCRE_INTERNAL_H
47 #define PCRE_INTERNAL_H
49 #include "Assertions.h"
52 #pragma warning(disable: 4232)
53 #pragma warning(disable: 4244)
56 #define _pcre_OP_lengths kjs_pcre_OP_lengths
57 #define _pcre_default_tables kjs_pcre_default_tables
58 #define _pcre_ord2utf8 kjs_pcre_ord2utf8
59 #define _pcre_printint kjs_pcre_printint
60 #define _pcre_try_flipped kjs_pcre_try_flipped
61 #define _pcre_ucp_findchar kjs_pcre_ucp_findchar
62 #define _pcre_utf8_table1 kjs_pcre_utf8_table1
63 #define _pcre_utf8_table1_size kjs_pcre_utf8_table1_size
64 #define _pcre_utf8_table2 kjs_pcre_utf8_table2
65 #define _pcre_utf8_table3 kjs_pcre_utf8_table3
66 #define _pcre_utf8_table4 kjs_pcre_utf8_table4
67 #define _pcre_utt kjs_pcre_utt
68 #define _pcre_utt_size kjs_pcre_utt_size
69 #define _pcre_valid_utf8 kjs_pcre_valid_utf8
70 #define _pcre_xclass kjs_pcre_xclass
72 /* Define DEBUG to get debugging output on stdout. */
78 /* Use a macro for debugging printing, 'cause that eliminates the use of #ifdef
79 inline, and there are *still* stupid compilers about that don't like indented
80 pre-processor statements, or at least there were when I first wrote this. After
81 all, it had only been about 10 years then... */
84 #define DPRINTF(p) printf p
86 #define DPRINTF(p) /*nothing*/
90 /* Get the definitions provided by running "configure" */
92 #include "pcre-config.h"
94 /* Standard C headers plus the external interface definition. The only time
95 setjmp and stdarg are used is when NO_RECURSE is set. */
106 /* Include the public PCRE header and the definitions of UCP character property
111 typedef unsigned short pcre_uint16;
112 typedef unsigned pcre_uint32;
113 typedef unsigned char uschar;
115 typedef JSRegExp pcre;
117 typedef JSRegExpChar pcre_char;
118 typedef JSRegExpChar pcre_uchar;
119 typedef const JSRegExpChar* USPTR;
121 /* Temporary fastMalloc/fastFree until we port to C++. */
125 extern void* (*pcre_malloc)(size_t);
126 extern void (*pcre_free)(void*);
131 /* PCRE keeps offsets in its compiled code as 2-byte quantities (always stored
132 in big-endian order) by default. These are used, for example, to link from the
133 start of a subpattern to its alternatives and its end. The use of 2 bytes per
134 offset limits the size of the compiled regex to around 64K, which is big enough
135 for almost everybody. However, I received a request for an even bigger limit.
136 For this reason, and also to make the code easier to maintain, the storing and
137 loading of offsets from the byte string is now handled by the macros that are
140 The macros are controlled by the value of LINK_SIZE. This defaults to 2 in
141 the config.h file, but can be overridden by using -D on the command line. This
142 is automated on Unix systems via the "configure" command. */
148 (a[(n)+1] = (d) & 255)
151 (((a)[n] << 8) | (a)[(n)+1])
153 #define MAX_PATTERN_SIZE (1 << 16)
159 (a[n] = (d) >> 16), \
160 (a[(n)+1] = (d) >> 8), \
161 (a[(n)+2] = (d) & 255)
164 (((a)[n] << 16) | ((a)[(n)+1] << 8) | (a)[(n)+2])
166 #define MAX_PATTERN_SIZE (1 << 24)
172 (a[n] = (d) >> 24), \
173 (a[(n)+1] = (d) >> 16), \
174 (a[(n)+2] = (d) >> 8), \
175 (a[(n)+3] = (d) & 255)
178 (((a)[n] << 24) | ((a)[(n)+1] << 16) | ((a)[(n)+2] << 8) | (a)[(n)+3])
180 #define MAX_PATTERN_SIZE (1 << 30) /* Keep it positive */
184 #error LINK_SIZE must be either 2, 3, or 4
188 /* Convenience macro defined in terms of the others */
190 #define PUTINC(a,n,d) PUT(a,n,d), a += LINK_SIZE
193 /* PCRE uses some other 2-byte quantities that do not change when the size of
194 offsets changes. There are used for repeat counts and for other things such as
195 capturing parenthesis numbers in back references. */
197 #define PUT2(a,n,d) \
202 (((a)[n] << 8) | (a)[(n)+1])
204 #define PUT2INC(a,n,d) PUT2(a,n,d), a += 2
207 /* When UTF-8 encoding is being used, a character is no longer just a single
208 byte. The macros for character handling generate simple sequences when used in
209 byte-mode, and more complicated ones for UTF-8 characters. */
211 /* Get the next UTF-8 character, not advancing the pointer, incrementing length
212 if there are extra bytes. This is called when we know we are in UTF-8 mode. */
214 #define GETUTF8CHARLEN(c, eptr, len) \
216 if ((c & 0xc0) == 0xc0) \
219 int gcaa = _pcre_utf8_table4[c & 0x3f]; /* Number of additional bytes */ \
221 c = (c & _pcre_utf8_table3[gcaa]) << gcss; \
222 for (gcii = 1; gcii <= gcaa; gcii++) \
225 c |= (eptr[gcii] & 0x3f) << gcss; \
230 /* Get the next UTF-8 character, advancing the pointer. This is called when we
231 know we are in UTF-8 mode. */
233 #define GETUTF8CHARINC(c, eptr) \
235 if ((c & 0xc0) == 0xc0) \
237 int gcaa = _pcre_utf8_table4[c & 0x3f]; /* Number of additional bytes */ \
239 c = (c & _pcre_utf8_table3[gcaa]) << gcss; \
243 c |= (*eptr++ & 0x3f) << gcss; \
247 #define LEAD_OFFSET (0xd800 - (0x10000 >> 10))
248 #define SURROGATE_OFFSET (0x10000 - (0xd800 << 10) - 0xdc00)
250 #define IS_LEADING_SURROGATE(c) (((c) & ~0x3ff) == 0xd800)
251 #define IS_TRAILING_SURROGATE(c) (((c) & ~0x3ff) == 0xdc00)
253 #define DECODE_SURROGATE_PAIR(l, t) (((l) << 10) + (t) + SURROGATE_OFFSET)
254 #define LEADING_SURROGATE(c) (LEAD_OFFSET + ((c) >> 10))
255 #define TRAILING_SURROGATE(c) (0xdc00 + ((c) & 0x3FF))
257 #define GETCHAR(c, eptr) \
259 if (IS_LEADING_SURROGATE(c)) \
260 c = DECODE_SURROGATE_PAIR(c, eptr[1])
262 #define GETCHARTEST(c, eptr) GETCHAR(c, eptr)
264 #define GETCHARINC(c, eptr) \
266 if (IS_LEADING_SURROGATE(c)) \
267 c = DECODE_SURROGATE_PAIR(c, *eptr++)
269 #define GETCHARINCTEST(c, eptr) GETCHARINC(c, eptr)
271 #define GETCHARLEN(c, eptr, len) \
273 if (IS_LEADING_SURROGATE(c)) \
275 c = DECODE_SURROGATE_PAIR(c, eptr[1]); \
279 #define GETCHARLENEND(c, eptr, end, len) \
281 if (IS_LEADING_SURROGATE(c)) \
283 c = DECODE_SURROGATE_PAIR(c, eptr + 1 < end ? eptr[1] : 0); \
287 #define ISMIDCHAR(c) IS_TRAILING_SURROGATE(c)
289 /* If the pointer is not at the start of a character, move it back until
290 it is. Called only in UTF-8 mode. */
292 #define BACKCHAR(eptr) while(ISMIDCHAR(*eptr)) eptr--;
295 /* In case there is no definition of offsetof() provided - though any proper
296 Standard C system should have one. */
299 #define offsetof(p_type,field) ((size_t)&(((p_type *)0)->field))
303 /* Private options flags start at the most significant end of the four bytes,
304 but skip the top bit so we can use ints for convenience without getting tangled
305 with negative values. The public options defined in pcre.h start at the least
306 significant end. Make sure they don't overlap! */
308 #define PCRE_FIRSTSET 0x40000000 /* first_byte is set */
309 #define PCRE_REQCHSET 0x20000000 /* req_byte is set */
310 #define PCRE_STARTLINE 0x10000000 /* start after \n for multiline */
311 #define PCRE_ANCHORED 0x02000000 /* can't use partial with this regex */
312 #define PCRE_CASELESS JS_REGEXP_CASELESS
313 #define PCRE_MULTILINE JS_REGEXP_MULTILINE
315 /* Negative values for the firstchar and reqchar variables */
317 #define REQ_UNSET (-2)
318 #define REQ_NONE (-1)
320 /* The maximum remaining length of subject we are prepared to search for a
323 #define REQ_BYTE_MAX 1000
325 /* Flags added to firstbyte or reqbyte; a "non-literal" item is either a
326 variable-length repeat, or a anything other than literal characters. */
328 #define REQ_CASELESS 0x0100 /* indicates caselessness */
329 #define REQ_VARY 0x0200 /* reqbyte followed non-literal item */
331 /* Miscellaneous definitions */
338 /* Flag bits and data types for the extended class (OP_XCLASS) for classes that
339 contain UTF-8 characters with values greater than 255. */
341 #define XCL_NOT 0x01 /* Flag: this is a negative class */
342 #define XCL_MAP 0x02 /* Flag: a 32-byte map is present */
344 #define XCL_END 0 /* Marks end of individual items */
345 #define XCL_SINGLE 1 /* Single item (one multibyte char) follows */
346 #define XCL_RANGE 2 /* A range (two multibyte chars) follows */
348 /* These are escaped items that aren't just an encoding of a particular data
349 value such as \n. They must have non-zero values, as check_escape() returns
350 their negation. Also, they must appear in the same order as in the opcode
351 definitions below, up to ESC_z. There's a dummy for OP_ANY because it
352 corresponds to "." rather than an escape sequence. The final one must be
353 ESC_REF as subsequent values are used for \1, \2, \3, etc. There is are two
354 tests in the code for an escape greater than ESC_b and less than ESC_Z to
355 detect the types that may be repeated. These are the types that consume
356 characters. If any new escapes are put in between that don't consume a
357 character, that code will have to change. */
359 enum { ESC_B = 1, ESC_b, ESC_D, ESC_d, ESC_S, ESC_s, ESC_W, ESC_w, ESC_REF };
361 /* Opcode table: OP_BRA must be last, as all values >= it are used for brackets
362 that extract substrings. Starting from 1 (i.e. after OP_END), the values up to
363 OP_EOD must correspond in order to the list of escapes immediately above.
364 Note that whenever this list is updated, the two macro definitions that follow
365 must also be updated to match. */
368 OP_END, /* End of pattern */
370 /* Values corresponding to backslashed metacharacters */
372 OP_NOT_WORD_BOUNDARY, /* \B */
373 OP_WORD_BOUNDARY, /* \b */
374 OP_NOT_DIGIT, /* \D */
376 OP_NOT_WHITESPACE, /* \S */
377 OP_WHITESPACE, /* \s */
378 OP_NOT_WORDCHAR, /* \W */
379 OP_WORDCHAR, /* \w */
381 OP_ANY, /* . -- Match any character */
385 OP_CHAR, /* Match one character, casefully */
386 OP_CHARNC, /* Match one character, caselessly */
387 OP_ASCII_CHAR, /* Match one ASCII (0-127) character. */
388 OP_ASCII_LETTER_NC, /* Match one ASCII letter, caselessly. */
389 OP_NOT, /* Match anything but the following char */
391 OP_STAR, /* The maximizing and minimizing versions of */
392 OP_MINSTAR, /* all these opcodes must come in pairs, with */
393 OP_PLUS, /* the minimizing one second. */
394 OP_MINPLUS, /* This first set applies to single characters */
397 OP_UPTO, /* From 0 to n matches */
399 OP_EXACT, /* Exactly n matches */
401 OP_NOTSTAR, /* This set applies to "not" single characters */
411 OP_TYPESTAR, /* This set applies to character types such as \d */
421 OP_CRSTAR, /* These are for character classes and back refs */
427 OP_CRRANGE, /* These are different to the three sets above. */
430 OP_CLASS, /* Match a character class, chars < 256 only */
431 OP_NCLASS, /* Same, but the bitmap was created from a negative
432 class - the difference is relevant when a UTF-8
433 character > 255 is encountered. */
435 OP_XCLASS, /* Extended class for handling UTF-8 chars within the
436 class. This does both positive and negative. */
438 OP_REF, /* Match a back reference */
440 OP_ALT, /* Start of alternation */
441 OP_KET, /* End of group that doesn't have an unbounded repeat */
442 OP_KETRMAX, /* These two must remain together and in this */
443 OP_KETRMIN, /* order. They are for groups the repeat for ever. */
445 /* The assertions must come before ONCE and COND */
447 OP_ASSERT, /* Positive lookahead */
448 OP_ASSERT_NOT, /* Negative lookahead */
450 /* ONCE and COND must come after the assertions, with ONCE first, as there's
451 a test for >= ONCE for a subpattern that isn't an assertion. */
453 OP_ONCE, /* Once matched, don't back up into the subpattern */
455 OP_BRAZERO, /* These two must remain together and in this */
456 OP_BRAMINZERO, /* order. */
458 OP_BRANUMBER, /* Used for extracting brackets whose number is greater
459 than can fit into an opcode. */
461 OP_BRA /* This and greater values are used for brackets that
462 extract substrings up to EXTRACT_BASIC_MAX. After
463 that, use is made of OP_BRANUMBER. */
466 /* WARNING WARNING WARNING: There is an implicit assumption in pcre.c and
467 study.c that all opcodes are less than 128 in value. This makes handling UTF-8
468 character sequences easier. */
470 /* The highest extraction number before we have to start using additional
471 bytes. (Originally PCRE didn't have support for extraction counts highter than
472 this number.) The value is limited by the number of opcodes left after OP_BRA,
473 i.e. 255 - OP_BRA. We actually set it a bit lower to leave room for additional
476 #define EXTRACT_BASIC_MAX 100
479 /* This macro defines the length of fixed length operations in the compiled
480 regex. The lengths are used when searching for specific things, and also in the
481 debugging printing of a compiled regex. We use a macro so that it can be
482 defined close to the definitions of the opcodes themselves.
484 As things have been extended, some of these are no longer fixed lenths, but are
485 minima instead. For example, the length of a single-character repeat may vary
486 in UTF-8 mode. The code that uses this table must know about such things. */
490 1, 1, 1, 1, 1, 1, 1, 1, /* \B, \b, \D, \d, \S, \s, \W, \w */ \
493 2, 2, /* Char, Charnc - minimum lengths */ \
494 2, 2, /* ASCII char or non-cased */ \
496 /* Positive single-char repeats ** These are */ \
497 2, 2, 2, 2, 2, 2, /* *, *?, +, +?, ?, ?? ** minima in */ \
498 4, 4, 4, /* upto, minupto, exact ** UTF-8 mode */ \
499 /* Negative single-char repeats - only for chars < 256 */ \
500 2, 2, 2, 2, 2, 2, /* NOT *, *?, +, +?, ?, ?? */ \
501 4, 4, 4, /* NOT upto, minupto, exact */ \
502 /* Positive type repeats */ \
503 2, 2, 2, 2, 2, 2, /* Type *, *?, +, +?, ?, ?? */ \
504 4, 4, 4, /* Type upto, minupto, exact */ \
505 /* Character class & ref repeats */ \
506 1, 1, 1, 1, 1, 1, /* *, *?, +, +?, ?, ?? */ \
507 5, 5, /* CRRANGE, CRMINRANGE */ \
510 0, /* XCLASS - variable length */ \
512 1+LINK_SIZE, /* Alt */ \
513 1+LINK_SIZE, /* Ket */ \
514 1+LINK_SIZE, /* KetRmax */ \
515 1+LINK_SIZE, /* KetRmin */ \
516 1+LINK_SIZE, /* Assert */ \
517 1+LINK_SIZE, /* Assert not */ \
518 1+LINK_SIZE, /* Once */ \
519 1, 1, /* BRAZERO, BRAMINZERO */ \
521 1+LINK_SIZE /* BRA */ \
524 /* Error code numbers. They are given names so that they can more easily be
527 enum { ERR0, ERR1, ERR2, ERR3, ERR4, ERR5, ERR6, ERR7, ERR8, ERR9,
528 ERR10, ERR11, ERR12, ERR13, ERR14, ERR15, ERR16, ERR17, ERR18, ERR19,
529 ERR20, ERR21, ERR22, ERR23, ERR24, ERR25, ERR26, ERR27, ERR28, ERR29,
530 ERR30, ERR31, ERR32, ERR33, ERR34, ERR35, ERR36, ERR37, ERR38, ERR39,
531 ERR40, ERR41, ERR42, ERR43, ERR44, ERR45, ERR46, ERR47 };
533 /* The real format of the start of the pcre block; the index of names and the
534 code vector run on as long as necessary after the end. We store an explicit
535 offset to the name table so that if a regex is compiled on one host, saved, and
536 then run on another where the size of pointers is different, all might still
537 be well. For the case of compiled-on-4 and run-on-8, we include an extra
538 pointer that is always NULL. For future-proofing, a few dummy fields were
539 originally included - even though you can never get this planning right - but
540 there is only one left now.
543 Because people can now save and re-use compiled patterns, any additions to this
544 structure should be made at the end, and something earlier (e.g. a new
545 flag in the options or one of the dummy fields) should indicate that the new
546 fields are present. Currently PCRE always sets the dummy fields to zero.
550 typedef struct real_pcre {
551 pcre_uint32 size; /* Total that was malloced */
554 pcre_uint16 top_bracket;
555 pcre_uint16 top_backref;
556 pcre_uint16 first_byte;
557 pcre_uint16 req_byte;
560 /* Structure for passing "static" information around between the functions
561 doing the compiling, so that they are thread-safe. */
563 typedef struct compile_data {
564 const uschar *lcc; /* Points to lower casing table */
565 const uschar *fcc; /* Points to case-flipping table */
566 const uschar *cbits; /* Points to character type table */
567 const uschar *ctypes; /* Points to table of type maps */
568 const uschar *start_code; /* The start of the compiled code */
569 const pcre_uchar *start_pattern; /* The start of the pattern */
570 int top_backref; /* Maximum back reference */
571 unsigned int backref_map; /* Bitmap of low back refs */
572 int req_varyopt; /* "After variable item" flag for reqbyte */
575 /* When compiling in a mode that doesn't use recursive calls to match(),
576 a structure is used to remember local variables on the heap. It is defined in
577 pcre.c, close to the match() function, so that it is easy to keep it in step
578 with any changes of local variable. However, the pointer to the current frame
579 must be saved in some "static" place over a longjmp(). We declare the
580 structure here so that we can put a pointer in the match_data structure.
581 NOTE: This isn't used for a "normal" compilation of pcre. */
585 /* Structure for passing "static" information around between the functions
586 doing traditional NFA matching, so that they are thread-safe. */
588 typedef struct match_data {
589 unsigned long int match_call_count; /* As it says */
590 int *offset_vector; /* Offset vector */
591 int offset_end; /* One past the end */
592 int offset_max; /* The maximum usable for return data */
593 const uschar *lcc; /* Points to lower casing table */
594 const uschar *ctypes; /* Points to table of type maps */
595 BOOL offset_overflow; /* Set if too many extractions */
596 USPTR start_subject; /* Start of the subject string */
597 USPTR end_subject; /* End of the subject string */
598 USPTR start_match; /* Start of this match attempt */
599 USPTR end_match_ptr; /* Subject position at end match */
600 int end_offset_top; /* Highwater mark at end of match */
605 /* Bit definitions for entries in the pcre_ctypes table. */
607 #define ctype_space 0x01
608 #define ctype_digit 0x04
609 #define ctype_xdigit 0x08
610 #define ctype_word 0x10 /* alphameric or '_' */
612 /* Offsets for the bitmap tables in pcre_cbits. Each table contains a set
613 of bits for a class map. Some classes are built by combining these tables. */
615 #define cbit_space 0 /* \s */
616 #define cbit_digit 32 /* \d */
617 #define cbit_word 64 /* \w */
618 #define cbit_length 96 /* Length of the cbits table */
620 /* Offsets of the various tables from the base tables pointer, and
624 #define fcc_offset 256
625 #define cbits_offset 512
626 #define ctypes_offset (cbits_offset + cbit_length)
627 #define tables_length (ctypes_offset + 128)
629 /* Layout of the UCP type table that translates property names into types and
639 /* Internal shared data tables. These are tables that are used by more than one
640 of the exported public functions. They have to be "external" in the C sense,
641 but are not part of the PCRE public API. The data for these tables is in the
642 pcre_tables.c module. */
644 extern const int _pcre_utf8_table1[];
645 extern const int _pcre_utf8_table2[];
646 extern const int _pcre_utf8_table3[];
647 extern const uschar _pcre_utf8_table4[];
649 extern const int _pcre_utf8_table1_size;
651 extern const uschar _pcre_default_tables[];
653 extern const uschar _pcre_OP_lengths[];
656 /* Internal shared functions. These are functions that are used by more than
657 one of the exported public functions. They have to be "external" in the C
658 sense, but are not part of the PCRE public API. */
660 extern int _pcre_ord2utf8(int, uschar *);
661 extern int _pcre_ucp_othercase(const int);
662 extern BOOL _pcre_xclass(int, const uschar *);
664 #define IS_NEWLINE(nl) ((nl) == 0xA || (nl) == 0xD || (nl) == 0x2028 || (nl) == 0x2029)
668 /* End of pcre_internal.h */