blob: 06c3e9dfc34a6bc606ddfd215f24fb09c10b3a81 [file] [log] [blame]
The Android Open Source Project8e35f3c2009-03-03 19:30:52 -08001/* 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.
6
7 Originally written by Philip Hazel
8 Copyright (c) 1997-2006 University of Cambridge
9 Copyright (C) 2002, 2004, 2006, 2007 Apple Inc. All rights reserved.
10
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
40/* 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
47/* 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
70#ifndef DFTABLES
71
72// Change the following to 1 to dump used regular expressions at process exit time.
73#define REGEXP_HISTOGRAM 0
74
75#include "Assertions.h"
76
77#if COMPILER(MSVC)
78#pragma warning(disable: 4232)
79#pragma warning(disable: 4244)
80#endif
81
82#include "pcre.h"
83
84/* 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
86patterns up to 64K long. */
87
88#define LINK_SIZE 2
89
90/* Define DEBUG to get debugging output on stdout. */
91
92#if 0
93#define DEBUG
94#endif
95
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
107/* 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
113loading of offsets from the byte string is now handled by the functions that are
114defined here. */
115
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
120static inline void put2ByteValue(unsigned char* opcodePtr, int value)
121{
122 ASSERT(value >= 0 && value <= 0xFFFF);
123 opcodePtr[0] = value >> 8;
124 opcodePtr[1] = value;
125}
126
127static inline int get2ByteValue(const unsigned char* opcodePtr)
128{
129 return (opcodePtr[0] << 8) | opcodePtr[1];
130}
131
132static inline void put2ByteValueAndAdvance(unsigned char*& opcodePtr, int value)
133{
134 put2ByteValue(opcodePtr, value);
135 opcodePtr += 2;
136}
137
138static 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
175// FIXME: These are really more of a "compiled regexp state" than "regexp options"
176enum RegExpOptions {
177 UseFirstByteOptimizationOption = 0x40000000, /* firstByte is set */
178 UseRequiredByteOptimizationOption = 0x20000000, /* reqByte is set */
179 UseMultiLineFirstByteOptimizationOption = 0x10000000, /* start after \n for multiline */
180 IsAnchoredOption = 0x02000000, /* can't use partial with this regex */
181 IgnoreCaseOption = 0x00000001,
182 MatchAcrossMultipleLinesOption = 0x00000002
183};
184
185/* Flags added to firstByte or reqByte; a "non-literal" item is either a
186variable-length repeat, or a anything other than literal characters. */
187
188#define REQ_IGNORE_CASE 0x0100 /* indicates should ignore case */
189#define REQ_VARY 0x0200 /* reqByte followed non-literal item */
190
191/* Miscellaneous definitions */
192
193/* 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 */
202
203/* 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
206definitions below, up to ESC_w. The final one must be
207ESC_REF as subsequent values are used for \1, \2, \3, etc. There is are two
208tests in the code for an escape > ESC_b and <= ESC_w to
209detect 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
213enum { ESC_B = 1, ESC_b, ESC_D, ESC_d, ESC_S, ESC_s, ESC_W, ESC_w, ESC_REF };
214
215/* 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
221#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 \
233 macro(NOT_NEWLINE) \
234 \
235 macro(CIRC) \
236 macro(DOLL) \
237 macro(BOL) \
238 macro(EOL) \
239 macro(CHAR) \
240 macro(CHAR_IGNORING_CASE) \
241 macro(ASCII_CHAR) \
242 macro(ASCII_LETTER_IGNORING_CASE) \
243 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 \
298 macro(BRAZERO) \
299 macro(BRAMINZERO) \
300 macro(BRANUMBER) \
301 macro(BRA)
302
303#define OPCODE_ENUM_VALUE(opcode) OP_##opcode,
304enum { FOR_EACH_OPCODE(OPCODE_ENUM_VALUE) };
305
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
311bytes. (Originally PCRE didn't have support for extraction counts higher than
312this 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
316/* FIXME: Note that OP_BRA + 100 is > 128, so the two comments above
317are in conflict! */
318
319#define EXTRACT_BASIC_MAX 100
320
321/* The code vector runs on as long as necessary after the end. */
322
323struct JSRegExp {
324 unsigned options;
325
326 unsigned short topBracket;
327 unsigned short topBackref;
328
329 unsigned short firstByte;
330 unsigned short reqByte;
331
332#if REGEXP_HISTOGRAM
333 size_t stringOffset;
334 size_t stringLength;
335#endif
336};
337
338/* 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
343#define kjs_pcre_utf8_table1_size 6
344
345extern 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];
349
350extern const unsigned char kjs_pcre_default_tables[tables_length];
351
352static inline unsigned char toLowerCase(unsigned char c)
353{
354 static const unsigned char* lowerCaseChars = kjs_pcre_default_tables + lcc_offset;
355 return lowerCaseChars[c];
356}
357
358static inline unsigned char flipCase(unsigned char c)
359{
360 static const unsigned char* flippedCaseChars = kjs_pcre_default_tables + fcc_offset;
361 return flippedCaseChars[c];
362}
363
364static inline unsigned char classBitmapForChar(unsigned char c)
365{
366 static const unsigned char* charClassBitmaps = kjs_pcre_default_tables + cbits_offset;
367 return charClassBitmaps[c];
368}
369
370static inline unsigned char charTypeForChar(unsigned char c)
371{
372 const unsigned char* charTypeMap = kjs_pcre_default_tables + ctypes_offset;
373 return charTypeMap[c];
374}
375
376static inline bool isWordChar(UChar c)
377{
378 return c < 128 && (charTypeForChar(c) & ctype_word);
379}
380
381static inline bool isSpaceChar(UChar c)
382{
383 return (c < 128 && (charTypeForChar(c) & ctype_space)) || c == 0x00A0;
384}
385
386static inline bool isNewline(UChar nl)
387{
388 return (nl == 0xA || nl == 0xD || nl == 0x2028 || nl == 0x2029);
389}
390
391static inline bool isBracketStartOpcode(unsigned char opcode)
392{
393 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 }
402}
403
404static 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
419#endif
420
421#endif
422
423/* End of pcre_internal.h */