blob: 06c3e9dfc34a6bc606ddfd215f24fb09c10b3a81 [file] [log] [blame]
darin@apple.comee752e72007-11-11 18:56:13 +00001/* This is JavaScriptCore's variant of the PCRE library. While this library
2started out as a copy of PCRE, many of the features of PCRE have been
3removed. This library now supports only the regular expression features
4required by the JavaScript language specification, and has only the functions
5needed by JavaScriptCore and the rest of WebKit.
darind7737ab2005-09-09 00:51:07 +00006
darin@apple.comee752e72007-11-11 18:56:13 +00007 Originally written by Philip Hazel
darinae790da2007-10-17 05:38:39 +00008 Copyright (c) 1997-2006 University of Cambridge
darin@apple.comee752e72007-11-11 18:56:13 +00009 Copyright (C) 2002, 2004, 2006, 2007 Apple Inc. All rights reserved.
darind7737ab2005-09-09 00:51:07 +000010
11-----------------------------------------------------------------------------
12Redistribution and use in source and binary forms, with or without
13modification, are permitted provided that the following conditions are met:
14
15 * Redistributions of source code must retain the above copyright notice,
16 this list of conditions and the following disclaimer.
17
18 * Redistributions in binary form must reproduce the above copyright
19 notice, this list of conditions and the following disclaimer in the
20 documentation and/or other materials provided with the distribution.
21
22 * Neither the name of the University of Cambridge nor the names of its
23 contributors may be used to endorse or promote products derived from
24 this software without specific prior written permission.
25
26THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36POSSIBILITY OF SUCH DAMAGE.
37-----------------------------------------------------------------------------
38*/
39
ddkilzer60a7a802007-01-01 05:07:40 +000040/* This header contains definitions that are shared between the different
41modules, but which are not relevant to the exported API. This includes some
42functions whose names all begin with "_pcre_". */
43
44#ifndef PCRE_INTERNAL_H
45#define PCRE_INTERNAL_H
46
darin@apple.com7f984872007-11-12 23:04:41 +000047/* Bit definitions for entries in the pcre_ctypes table. */
48
49#define ctype_space 0x01
50#define ctype_xdigit 0x08
51#define ctype_word 0x10 /* alphameric or '_' */
52
53/* Offsets for the bitmap tables in pcre_cbits. Each table contains a set
54of bits for a class map. Some classes are built by combining these tables. */
55
56#define cbit_space 0 /* \s */
57#define cbit_digit 32 /* \d */
58#define cbit_word 64 /* \w */
59#define cbit_length 96 /* Length of the cbits table */
60
61/* Offsets of the various tables from the base tables pointer, and
62total length. */
63
64#define lcc_offset 0
65#define fcc_offset 128
66#define cbits_offset 256
67#define ctypes_offset (cbits_offset + cbit_length)
68#define tables_length (ctypes_offset + 128)
69
darin@apple.com68af1ff2007-11-11 19:24:37 +000070#ifndef DFTABLES
71
darin@apple.comcff41592008-06-28 22:21:20 +000072// Change the following to 1 to dump used regular expressions at process exit time.
73#define REGEXP_HISTOGRAM 0
74
thatcherdc18a362006-08-31 21:28:29 +000075#include "Assertions.h"
76
darinb847b442006-10-27 16:48:28 +000077#if COMPILER(MSVC)
78#pragma warning(disable: 4232)
79#pragma warning(disable: 4244)
80#endif
81
darin@apple.com28e8eb92007-12-17 04:19:25 +000082#include "pcre.h"
83
darin@apple.comee752e72007-11-11 18:56:13 +000084/* The value of LINK_SIZE determines the number of bytes used to store links as
85offsets within the compiled regex. The default is 2, which allows for compiled
darin@apple.com28e8eb92007-12-17 04:19:25 +000086patterns up to 64K long. */
darin@apple.comee752e72007-11-11 18:56:13 +000087
88#define LINK_SIZE 2
89
darind7737ab2005-09-09 00:51:07 +000090/* Define DEBUG to get debugging output on stdout. */
91
ddkilzer60a7a802007-01-01 05:07:40 +000092#if 0
darind7737ab2005-09-09 00:51:07 +000093#define DEBUG
ddkilzer60a7a802007-01-01 05:07:40 +000094#endif
darind7737ab2005-09-09 00:51:07 +000095
96/* Use a macro for debugging printing, 'cause that eliminates the use of #ifdef
97inline, and there are *still* stupid compilers about that don't like indented
98pre-processor statements, or at least there were when I first wrote this. After
99all, it had only been about 10 years then... */
100
101#ifdef DEBUG
102#define DPRINTF(p) printf p
103#else
104#define DPRINTF(p) /*nothing*/
105#endif
106
darind7737ab2005-09-09 00:51:07 +0000107/* PCRE keeps offsets in its compiled code as 2-byte quantities (always stored
108in big-endian order) by default. These are used, for example, to link from the
109start of a subpattern to its alternatives and its end. The use of 2 bytes per
110offset limits the size of the compiled regex to around 64K, which is big enough
111for almost everybody. However, I received a request for an even bigger limit.
112For this reason, and also to make the code easier to maintain, the storing and
darin@apple.com28e8eb92007-12-17 04:19:25 +0000113loading of offsets from the byte string is now handled by the functions that are
114defined here. */
darind7737ab2005-09-09 00:51:07 +0000115
116/* PCRE uses some other 2-byte quantities that do not change when the size of
117offsets changes. There are used for repeat counts and for other things such as
118capturing parenthesis numbers in back references. */
119
darin@apple.com28e8eb92007-12-17 04:19:25 +0000120static inline void put2ByteValue(unsigned char* opcodePtr, int value)
eric@webkit.org487902b2007-11-15 02:00:15 +0000121{
darin@apple.com28e8eb92007-12-17 04:19:25 +0000122 ASSERT(value >= 0 && value <= 0xFFFF);
123 opcodePtr[0] = value >> 8;
124 opcodePtr[1] = value;
eric@webkit.org487902b2007-11-15 02:00:15 +0000125}
darind7737ab2005-09-09 00:51:07 +0000126
darin@apple.com28e8eb92007-12-17 04:19:25 +0000127static inline int get2ByteValue(const unsigned char* opcodePtr)
eric@webkit.org487902b2007-11-15 02:00:15 +0000128{
darin@apple.com28e8eb92007-12-17 04:19:25 +0000129 return (opcodePtr[0] << 8) | opcodePtr[1];
eric@webkit.org487902b2007-11-15 02:00:15 +0000130}
darind7737ab2005-09-09 00:51:07 +0000131
darin@apple.com28e8eb92007-12-17 04:19:25 +0000132static inline void put2ByteValueAndAdvance(unsigned char*& opcodePtr, int value)
eric@webkit.orga28f14e2007-11-29 11:05:07 +0000133{
darin@apple.com28e8eb92007-12-17 04:19:25 +0000134 put2ByteValue(opcodePtr, value);
eric@webkit.org5b0a0092007-11-29 11:24:00 +0000135 opcodePtr += 2;
ggarenfe71ca72005-11-06 20:41:08 +0000136}
137
darin@apple.com28e8eb92007-12-17 04:19:25 +0000138static inline void putLinkValueAllowZero(unsigned char* opcodePtr, int value)
139{
140 put2ByteValue(opcodePtr, value);
141}
142
143static inline int getLinkValueAllowZero(const unsigned char* opcodePtr)
144{
145 return get2ByteValue(opcodePtr);
146}
147
148#define MAX_PATTERN_SIZE (1 << 16)
149
150static inline void putLinkValue(unsigned char* opcodePtr, int value)
151{
152 ASSERT(value);
153 putLinkValueAllowZero(opcodePtr, value);
154}
155
156static inline int getLinkValue(const unsigned char* opcodePtr)
157{
158 int value = getLinkValueAllowZero(opcodePtr);
159 ASSERT(value);
160 return value;
161}
162
163static inline void putLinkValueAndAdvance(unsigned char*& opcodePtr, int value)
164{
165 putLinkValue(opcodePtr, value);
166 opcodePtr += LINK_SIZE;
167}
168
169static inline void putLinkValueAllowZeroAndAdvance(unsigned char*& opcodePtr, int value)
170{
171 putLinkValueAllowZero(opcodePtr, value);
172 opcodePtr += LINK_SIZE;
173}
174
eric@webkit.org8a344672007-11-29 11:38:26 +0000175// FIXME: These are really more of a "compiled regexp state" than "regexp options"
176enum RegExpOptions {
darin@apple.com51d6b262008-06-15 06:23:50 +0000177 UseFirstByteOptimizationOption = 0x40000000, /* firstByte is set */
178 UseRequiredByteOptimizationOption = 0x20000000, /* reqByte is set */
eric@webkit.org8a344672007-11-29 11:38:26 +0000179 UseMultiLineFirstByteOptimizationOption = 0x10000000, /* start after \n for multiline */
180 IsAnchoredOption = 0x02000000, /* can't use partial with this regex */
181 IgnoreCaseOption = 0x00000001,
182 MatchAcrossMultipleLinesOption = 0x00000002
eric@webkit.orgc8d4c7d2007-11-29 11:09:42 +0000183};
darind7737ab2005-09-09 00:51:07 +0000184
darin@apple.com51d6b262008-06-15 06:23:50 +0000185/* Flags added to firstByte or reqByte; a "non-literal" item is either a
darind7737ab2005-09-09 00:51:07 +0000186variable-length repeat, or a anything other than literal characters. */
187
eric@webkit.org81716d82007-11-29 11:19:42 +0000188#define REQ_IGNORE_CASE 0x0100 /* indicates should ignore case */
darin@apple.com51d6b262008-06-15 06:23:50 +0000189#define REQ_VARY 0x0200 /* reqByte followed non-literal item */
darind7737ab2005-09-09 00:51:07 +0000190
191/* Miscellaneous definitions */
192
darinae790da2007-10-17 05:38:39 +0000193/* Flag bits and data types for the extended class (OP_XCLASS) for classes that
194contain UTF-8 characters with values greater than 255. */
195
196#define XCL_NOT 0x01 /* Flag: this is a negative class */
197#define XCL_MAP 0x02 /* Flag: a 32-byte map is present */
198
199#define XCL_END 0 /* Marks end of individual items */
200#define XCL_SINGLE 1 /* Single item (one multibyte char) follows */
201#define XCL_RANGE 2 /* A range (two multibyte chars) follows */
darinae790da2007-10-17 05:38:39 +0000202
darind7737ab2005-09-09 00:51:07 +0000203/* These are escaped items that aren't just an encoding of a particular data
204value such as \n. They must have non-zero values, as check_escape() returns
205their negation. Also, they must appear in the same order as in the opcode
darin@apple.com6b652e82007-11-30 18:54:34 +0000206definitions below, up to ESC_w. The final one must be
darind7737ab2005-09-09 00:51:07 +0000207ESC_REF as subsequent values are used for \1, \2, \3, etc. There is are two
darin@apple.com6b652e82007-11-30 18:54:34 +0000208tests in the code for an escape > ESC_b and <= ESC_w to
darind7737ab2005-09-09 00:51:07 +0000209detect the types that may be repeated. These are the types that consume
210characters. If any new escapes are put in between that don't consume a
211character, that code will have to change. */
212
darin@apple.com7ecf0c32007-11-04 06:18:31 +0000213enum { ESC_B = 1, ESC_b, ESC_D, ESC_d, ESC_S, ESC_s, ESC_W, ESC_w, ESC_REF };
darind7737ab2005-09-09 00:51:07 +0000214
darind7737ab2005-09-09 00:51:07 +0000215/* Opcode table: OP_BRA must be last, as all values >= it are used for brackets
216that extract substrings. Starting from 1 (i.e. after OP_END), the values up to
217OP_EOD must correspond in order to the list of escapes immediately above.
218Note that whenever this list is updated, the two macro definitions that follow
219must also be updated to match. */
220
darin@apple.comee752e72007-11-11 18:56:13 +0000221#define FOR_EACH_OPCODE(macro) \
222 macro(END) \
223 \
224 macro(NOT_WORD_BOUNDARY) \
225 macro(WORD_BOUNDARY) \
226 macro(NOT_DIGIT) \
227 macro(DIGIT) \
228 macro(NOT_WHITESPACE) \
229 macro(WHITESPACE) \
230 macro(NOT_WORDCHAR) \
231 macro(WORDCHAR) \
232 \
darin@apple.com6b652e82007-11-30 18:54:34 +0000233 macro(NOT_NEWLINE) \
darin@apple.comee752e72007-11-11 18:56:13 +0000234 \
235 macro(CIRC) \
236 macro(DOLL) \
eric@webkit.org7fbd8932008-03-31 08:01:09 +0000237 macro(BOL) \
238 macro(EOL) \
darin@apple.comee752e72007-11-11 18:56:13 +0000239 macro(CHAR) \
eric@webkit.org5bc5d412007-11-29 11:32:46 +0000240 macro(CHAR_IGNORING_CASE) \
darin@apple.comee752e72007-11-11 18:56:13 +0000241 macro(ASCII_CHAR) \
eric@webkit.org5bc5d412007-11-29 11:32:46 +0000242 macro(ASCII_LETTER_IGNORING_CASE) \
darin@apple.comee752e72007-11-11 18:56:13 +0000243 macro(NOT) \
244 \
245 macro(STAR) \
246 macro(MINSTAR) \
247 macro(PLUS) \
248 macro(MINPLUS) \
249 macro(QUERY) \
250 macro(MINQUERY) \
251 macro(UPTO) \
252 macro(MINUPTO) \
253 macro(EXACT) \
254 \
255 macro(NOTSTAR) \
256 macro(NOTMINSTAR) \
257 macro(NOTPLUS) \
258 macro(NOTMINPLUS) \
259 macro(NOTQUERY) \
260 macro(NOTMINQUERY) \
261 macro(NOTUPTO) \
262 macro(NOTMINUPTO) \
263 macro(NOTEXACT) \
264 \
265 macro(TYPESTAR) \
266 macro(TYPEMINSTAR) \
267 macro(TYPEPLUS) \
268 macro(TYPEMINPLUS) \
269 macro(TYPEQUERY) \
270 macro(TYPEMINQUERY) \
271 macro(TYPEUPTO) \
272 macro(TYPEMINUPTO) \
273 macro(TYPEEXACT) \
274 \
275 macro(CRSTAR) \
276 macro(CRMINSTAR) \
277 macro(CRPLUS) \
278 macro(CRMINPLUS) \
279 macro(CRQUERY) \
280 macro(CRMINQUERY) \
281 macro(CRRANGE) \
282 macro(CRMINRANGE) \
283 \
284 macro(CLASS) \
285 macro(NCLASS) \
286 macro(XCLASS) \
287 \
288 macro(REF) \
289 \
290 macro(ALT) \
291 macro(KET) \
292 macro(KETRMAX) \
293 macro(KETRMIN) \
294 \
295 macro(ASSERT) \
296 macro(ASSERT_NOT) \
297 \
darin@apple.comee752e72007-11-11 18:56:13 +0000298 macro(BRAZERO) \
299 macro(BRAMINZERO) \
300 macro(BRANUMBER) \
301 macro(BRA)
darind7737ab2005-09-09 00:51:07 +0000302
darin@apple.comee752e72007-11-11 18:56:13 +0000303#define OPCODE_ENUM_VALUE(opcode) OP_##opcode,
304enum { FOR_EACH_OPCODE(OPCODE_ENUM_VALUE) };
darind7737ab2005-09-09 00:51:07 +0000305
306/* WARNING WARNING WARNING: There is an implicit assumption in pcre.c and
307study.c that all opcodes are less than 128 in value. This makes handling UTF-8
308character sequences easier. */
309
310/* The highest extraction number before we have to start using additional
darin@apple.com28e8eb92007-12-17 04:19:25 +0000311bytes. (Originally PCRE didn't have support for extraction counts higher than
darind7737ab2005-09-09 00:51:07 +0000312this number.) The value is limited by the number of opcodes left after OP_BRA,
313i.e. 255 - OP_BRA. We actually set it a bit lower to leave room for additional
314opcodes. */
315
darin@apple.com28e8eb92007-12-17 04:19:25 +0000316/* FIXME: Note that OP_BRA + 100 is > 128, so the two comments above
317are in conflict! */
318
darind7737ab2005-09-09 00:51:07 +0000319#define EXTRACT_BASIC_MAX 100
320
darin@apple.comcff41592008-06-28 22:21:20 +0000321/* The code vector runs on as long as necessary after the end. */
darind7737ab2005-09-09 00:51:07 +0000322
eric@webkit.org386a6022007-11-29 11:04:06 +0000323struct JSRegExp {
darin@apple.com28e8eb92007-12-17 04:19:25 +0000324 unsigned options;
darind7737ab2005-09-09 00:51:07 +0000325
darin@apple.com51d6b262008-06-15 06:23:50 +0000326 unsigned short topBracket;
327 unsigned short topBackref;
eric@webkit.orgc2f9f302007-11-29 11:37:40 +0000328
darin@apple.com51d6b262008-06-15 06:23:50 +0000329 unsigned short firstByte;
330 unsigned short reqByte;
darin@apple.comcff41592008-06-28 22:21:20 +0000331
332#if REGEXP_HISTOGRAM
333 size_t stringOffset;
334 size_t stringLength;
335#endif
eric@webkit.org386a6022007-11-29 11:04:06 +0000336};
darind7737ab2005-09-09 00:51:07 +0000337
eric@webkit.org1dab6f52007-11-15 01:17:31 +0000338/* Internal shared data tables. These are tables that are used by more than one
339 of the exported public functions. They have to be "external" in the C sense,
340 but are not part of the PCRE public API. The data for these tables is in the
341 pcre_tables.c module. */
342
darin@apple.com28e8eb92007-12-17 04:19:25 +0000343#define kjs_pcre_utf8_table1_size 6
eric@webkit.org1dab6f52007-11-15 01:17:31 +0000344
darin@apple.com28e8eb92007-12-17 04:19:25 +0000345extern const int kjs_pcre_utf8_table1[6];
346extern const int kjs_pcre_utf8_table2[6];
347extern const int kjs_pcre_utf8_table3[6];
348extern const unsigned char kjs_pcre_utf8_table4[0x40];
eric@webkit.org1dab6f52007-11-15 01:17:31 +0000349
darin@apple.com28e8eb92007-12-17 04:19:25 +0000350extern const unsigned char kjs_pcre_default_tables[tables_length];
eric@webkit.org1dab6f52007-11-15 01:17:31 +0000351
darin@apple.com28e8eb92007-12-17 04:19:25 +0000352static inline unsigned char toLowerCase(unsigned char c)
eric@webkit.org4c023352007-11-29 11:22:30 +0000353{
darin@apple.com28e8eb92007-12-17 04:19:25 +0000354 static const unsigned char* lowerCaseChars = kjs_pcre_default_tables + lcc_offset;
eric@webkit.org4c023352007-11-29 11:22:30 +0000355 return lowerCaseChars[c];
356}
357
darin@apple.com28e8eb92007-12-17 04:19:25 +0000358static inline unsigned char flipCase(unsigned char c)
eric@webkit.org4c023352007-11-29 11:22:30 +0000359{
darin@apple.com28e8eb92007-12-17 04:19:25 +0000360 static const unsigned char* flippedCaseChars = kjs_pcre_default_tables + fcc_offset;
eric@webkit.org4c023352007-11-29 11:22:30 +0000361 return flippedCaseChars[c];
362}
363
darin@apple.com28e8eb92007-12-17 04:19:25 +0000364static inline unsigned char classBitmapForChar(unsigned char c)
eric@webkit.org4c023352007-11-29 11:22:30 +0000365{
darin@apple.com28e8eb92007-12-17 04:19:25 +0000366 static const unsigned char* charClassBitmaps = kjs_pcre_default_tables + cbits_offset;
eric@webkit.org4c023352007-11-29 11:22:30 +0000367 return charClassBitmaps[c];
368}
369
darin@apple.com28e8eb92007-12-17 04:19:25 +0000370static inline unsigned char charTypeForChar(unsigned char c)
eric@webkit.org4c023352007-11-29 11:22:30 +0000371{
darin@apple.com28e8eb92007-12-17 04:19:25 +0000372 const unsigned char* charTypeMap = kjs_pcre_default_tables + ctypes_offset;
eric@webkit.org4c023352007-11-29 11:22:30 +0000373 return charTypeMap[c];
374}
375
376static inline bool isWordChar(UChar c)
377{
darin@apple.com28e8eb92007-12-17 04:19:25 +0000378 return c < 128 && (charTypeForChar(c) & ctype_word);
eric@webkit.org4c023352007-11-29 11:22:30 +0000379}
380
381static inline bool isSpaceChar(UChar c)
382{
cwzwarich@webkit.org61e3e262008-09-18 09:35:52 +0000383 return (c < 128 && (charTypeForChar(c) & ctype_space)) || c == 0x00A0;
eric@webkit.org4c023352007-11-29 11:22:30 +0000384}
385
eric@webkit.org64029102007-11-29 11:06:57 +0000386static inline bool isNewline(UChar nl)
eric@webkit.org487902b2007-11-15 02:00:15 +0000387{
388 return (nl == 0xA || nl == 0xD || nl == 0x2028 || nl == 0x2029);
389}
darin@apple.com3fbfe082007-11-03 16:28:51 +0000390
darin@apple.com28e8eb92007-12-17 04:19:25 +0000391static inline bool isBracketStartOpcode(unsigned char opcode)
eric@webkit.orga818f3a2007-11-29 11:23:11 +0000392{
darin@apple.com28e8eb92007-12-17 04:19:25 +0000393 if (opcode >= OP_BRA)
394 return true;
395 switch (opcode) {
396 case OP_ASSERT:
397 case OP_ASSERT_NOT:
398 return true;
399 default:
400 return false;
401 }
eric@webkit.orga818f3a2007-11-29 11:23:11 +0000402}
403
darin@apple.com28e8eb92007-12-17 04:19:25 +0000404static inline void advanceToEndOfBracket(const unsigned char*& opcodePtr)
405{
406 ASSERT(isBracketStartOpcode(*opcodePtr) || *opcodePtr == OP_ALT);
407 do
408 opcodePtr += getLinkValue(opcodePtr + 1);
409 while (*opcodePtr == OP_ALT);
410}
411
412/* Internal shared functions. These are functions that are used in more
413that one of the source files. They have to have external linkage, but
414but are not part of the public API and so not exported from the library. */
415
416extern int kjs_pcre_ucp_othercase(unsigned);
417extern bool kjs_pcre_xclass(int, const unsigned char*);
418
ddkilzer60a7a802007-01-01 05:07:40 +0000419#endif
420
darin@apple.com68af1ff2007-11-11 19:24:37 +0000421#endif
422
darind7737ab2005-09-09 00:51:07 +0000423/* End of pcre_internal.h */