Annotation of embedaddon/pcre/pcre_compile.c, revision 1.1.1.4
1.1 misho 1: /*************************************************
2: * Perl-Compatible Regular Expressions *
3: *************************************************/
4:
5: /* PCRE is a library of functions to support regular expressions whose syntax
6: and semantics are as close as possible to those of the Perl 5 language.
7:
8: Written by Philip Hazel
1.1.1.4 ! misho 9: Copyright (c) 1997-2013 University of Cambridge
1.1 misho 10:
11: -----------------------------------------------------------------------------
12: Redistribution and use in source and binary forms, with or without
13: modification, 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:
26: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27: AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29: ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30: LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31: CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32: SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33: INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34: CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35: ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36: POSSIBILITY OF SUCH DAMAGE.
37: -----------------------------------------------------------------------------
38: */
39:
40:
41: /* This module contains the external function pcre_compile(), along with
42: supporting internal functions that are not used by other modules. */
43:
44:
45: #ifdef HAVE_CONFIG_H
46: #include "config.h"
47: #endif
48:
49: #define NLBLOCK cd /* Block containing newline information */
50: #define PSSTART start_pattern /* Field containing processed string start */
51: #define PSEND end_pattern /* Field containing processed string end */
52:
53: #include "pcre_internal.h"
54:
55:
1.1.1.4 ! misho 56: /* When PCRE_DEBUG is defined, we need the pcre(16|32)_printint() function, which
1.1.1.2 misho 57: is also used by pcretest. PCRE_DEBUG is not defined when building a production
58: library. We do not need to select pcre16_printint.c specially, because the
59: COMPILE_PCREx macro will already be appropriately set. */
1.1 misho 60:
61: #ifdef PCRE_DEBUG
1.1.1.2 misho 62: /* pcre_printint.c should not include any headers */
63: #define PCRE_INCLUDED
64: #include "pcre_printint.c"
65: #undef PCRE_INCLUDED
1.1 misho 66: #endif
67:
68:
69: /* Macro for setting individual bits in class bitmaps. */
70:
1.1.1.4 ! misho 71: #define SETBIT(a,b) a[(b)/8] |= (1 << ((b)&7))
1.1 misho 72:
73: /* Maximum length value to check against when making sure that the integer that
74: holds the compiled pattern length does not overflow. We make it a bit less than
75: INT_MAX to allow for adding in group terminating bytes, so that we don't have
76: to check them every time. */
77:
78: #define OFLOW_MAX (INT_MAX - 20)
79:
1.1.1.4 ! misho 80: /* Definitions to allow mutual recursion */
! 81:
! 82: static int
! 83: add_list_to_class(pcre_uint8 *, pcre_uchar **, int, compile_data *,
! 84: const pcre_uint32 *, unsigned int);
! 85:
! 86: static BOOL
! 87: compile_regex(int, pcre_uchar **, const pcre_uchar **, int *, BOOL, BOOL, int, int,
! 88: pcre_uint32 *, pcre_int32 *, pcre_uint32 *, pcre_int32 *, branch_chain *,
! 89: compile_data *, int *);
! 90:
! 91:
1.1 misho 92:
93: /*************************************************
94: * Code parameters and static tables *
95: *************************************************/
96:
97: /* This value specifies the size of stack workspace that is used during the
98: first pre-compile phase that determines how much memory is required. The regex
99: is partly compiled into this space, but the compiled parts are discarded as
100: soon as they can be, so that hopefully there will never be an overrun. The code
101: does, however, check for an overrun. The largest amount I've seen used is 218,
102: so this number is very generous.
103:
104: The same workspace is used during the second, actual compile phase for
105: remembering forward references to groups so that they can be filled in at the
106: end. Each entry in this list occupies LINK_SIZE bytes, so even when LINK_SIZE
107: is 4 there is plenty of room for most patterns. However, the memory can get
108: filled up by repetitions of forward references, for example patterns like
109: /(?1){0,1999}(b)/, and one user did hit the limit. The code has been changed so
110: that the workspace is expanded using malloc() in this situation. The value
111: below is therefore a minimum, and we put a maximum on it for safety. The
112: minimum is now also defined in terms of LINK_SIZE so that the use of malloc()
113: kicks in at the same number of forward references in all cases. */
114:
115: #define COMPILE_WORK_SIZE (2048*LINK_SIZE)
116: #define COMPILE_WORK_SIZE_MAX (100*COMPILE_WORK_SIZE)
117:
118: /* The overrun tests check for a slightly smaller size so that they detect the
119: overrun before it actually does run off the end of the data block. */
120:
121: #define WORK_SIZE_SAFETY_MARGIN (100)
122:
1.1.1.2 misho 123: /* Private flags added to firstchar and reqchar. */
124:
1.1.1.4 ! misho 125: #define REQ_CASELESS (1 << 0) /* Indicates caselessness */
! 126: #define REQ_VARY (1 << 1) /* Reqchar followed non-literal item */
! 127: /* Negative values for the firstchar and reqchar flags */
! 128: #define REQ_UNSET (-2)
! 129: #define REQ_NONE (-1)
1.1.1.2 misho 130:
131: /* Repeated character flags. */
132:
133: #define UTF_LENGTH 0x10000000l /* The char contains its length. */
1.1 misho 134:
135: /* Table for handling escaped characters in the range '0'-'z'. Positive returns
136: are simple data values; negative values are for special things like \d and so
137: on. Zero means further processing is needed (for things like \x), or the escape
138: is invalid. */
139:
140: #ifndef EBCDIC
141:
142: /* This is the "normal" table for ASCII systems or for EBCDIC systems running
143: in UTF-8 mode. */
144:
145: static const short int escapes[] = {
146: 0, 0,
147: 0, 0,
148: 0, 0,
149: 0, 0,
150: 0, 0,
151: CHAR_COLON, CHAR_SEMICOLON,
152: CHAR_LESS_THAN_SIGN, CHAR_EQUALS_SIGN,
153: CHAR_GREATER_THAN_SIGN, CHAR_QUESTION_MARK,
154: CHAR_COMMERCIAL_AT, -ESC_A,
155: -ESC_B, -ESC_C,
156: -ESC_D, -ESC_E,
157: 0, -ESC_G,
158: -ESC_H, 0,
159: 0, -ESC_K,
160: 0, 0,
161: -ESC_N, 0,
162: -ESC_P, -ESC_Q,
163: -ESC_R, -ESC_S,
164: 0, 0,
165: -ESC_V, -ESC_W,
166: -ESC_X, 0,
167: -ESC_Z, CHAR_LEFT_SQUARE_BRACKET,
168: CHAR_BACKSLASH, CHAR_RIGHT_SQUARE_BRACKET,
169: CHAR_CIRCUMFLEX_ACCENT, CHAR_UNDERSCORE,
170: CHAR_GRAVE_ACCENT, 7,
171: -ESC_b, 0,
172: -ESC_d, ESC_e,
173: ESC_f, 0,
174: -ESC_h, 0,
175: 0, -ESC_k,
176: 0, 0,
177: ESC_n, 0,
178: -ESC_p, 0,
179: ESC_r, -ESC_s,
180: ESC_tee, 0,
181: -ESC_v, -ESC_w,
182: 0, 0,
183: -ESC_z
184: };
185:
186: #else
187:
188: /* This is the "abnormal" table for EBCDIC systems without UTF-8 support. */
189:
190: static const short int escapes[] = {
191: /* 48 */ 0, 0, 0, '.', '<', '(', '+', '|',
192: /* 50 */ '&', 0, 0, 0, 0, 0, 0, 0,
193: /* 58 */ 0, 0, '!', '$', '*', ')', ';', '~',
194: /* 60 */ '-', '/', 0, 0, 0, 0, 0, 0,
195: /* 68 */ 0, 0, '|', ',', '%', '_', '>', '?',
196: /* 70 */ 0, 0, 0, 0, 0, 0, 0, 0,
197: /* 78 */ 0, '`', ':', '#', '@', '\'', '=', '"',
198: /* 80 */ 0, 7, -ESC_b, 0, -ESC_d, ESC_e, ESC_f, 0,
199: /* 88 */-ESC_h, 0, 0, '{', 0, 0, 0, 0,
200: /* 90 */ 0, 0, -ESC_k, 'l', 0, ESC_n, 0, -ESC_p,
201: /* 98 */ 0, ESC_r, 0, '}', 0, 0, 0, 0,
202: /* A0 */ 0, '~', -ESC_s, ESC_tee, 0,-ESC_v, -ESC_w, 0,
203: /* A8 */ 0,-ESC_z, 0, 0, 0, '[', 0, 0,
204: /* B0 */ 0, 0, 0, 0, 0, 0, 0, 0,
205: /* B8 */ 0, 0, 0, 0, 0, ']', '=', '-',
206: /* C0 */ '{',-ESC_A, -ESC_B, -ESC_C, -ESC_D,-ESC_E, 0, -ESC_G,
207: /* C8 */-ESC_H, 0, 0, 0, 0, 0, 0, 0,
208: /* D0 */ '}', 0, -ESC_K, 0, 0,-ESC_N, 0, -ESC_P,
209: /* D8 */-ESC_Q,-ESC_R, 0, 0, 0, 0, 0, 0,
210: /* E0 */ '\\', 0, -ESC_S, 0, 0,-ESC_V, -ESC_W, -ESC_X,
211: /* E8 */ 0,-ESC_Z, 0, 0, 0, 0, 0, 0,
212: /* F0 */ 0, 0, 0, 0, 0, 0, 0, 0,
213: /* F8 */ 0, 0, 0, 0, 0, 0, 0, 0
214: };
215: #endif
216:
217:
218: /* Table of special "verbs" like (*PRUNE). This is a short table, so it is
219: searched linearly. Put all the names into a single string, in order to reduce
220: the number of relocations when a shared library is dynamically linked. The
221: string is built from string macros so that it works in UTF-8 mode on EBCDIC
222: platforms. */
223:
224: typedef struct verbitem {
225: int len; /* Length of verb name */
226: int op; /* Op when no arg, or -1 if arg mandatory */
227: int op_arg; /* Op when arg present, or -1 if not allowed */
228: } verbitem;
229:
230: static const char verbnames[] =
231: "\0" /* Empty name is a shorthand for MARK */
232: STRING_MARK0
233: STRING_ACCEPT0
234: STRING_COMMIT0
235: STRING_F0
236: STRING_FAIL0
237: STRING_PRUNE0
238: STRING_SKIP0
239: STRING_THEN;
240:
241: static const verbitem verbs[] = {
242: { 0, -1, OP_MARK },
243: { 4, -1, OP_MARK },
244: { 6, OP_ACCEPT, -1 },
245: { 6, OP_COMMIT, -1 },
246: { 1, OP_FAIL, -1 },
247: { 4, OP_FAIL, -1 },
248: { 5, OP_PRUNE, OP_PRUNE_ARG },
249: { 4, OP_SKIP, OP_SKIP_ARG },
250: { 4, OP_THEN, OP_THEN_ARG }
251: };
252:
253: static const int verbcount = sizeof(verbs)/sizeof(verbitem);
254:
255:
256: /* Tables of names of POSIX character classes and their lengths. The names are
257: now all in a single string, to reduce the number of relocations when a shared
258: library is dynamically loaded. The list of lengths is terminated by a zero
259: length entry. The first three must be alpha, lower, upper, as this is assumed
260: for handling case independence. */
261:
262: static const char posix_names[] =
263: STRING_alpha0 STRING_lower0 STRING_upper0 STRING_alnum0
264: STRING_ascii0 STRING_blank0 STRING_cntrl0 STRING_digit0
265: STRING_graph0 STRING_print0 STRING_punct0 STRING_space0
266: STRING_word0 STRING_xdigit;
267:
1.1.1.2 misho 268: static const pcre_uint8 posix_name_lengths[] = {
1.1 misho 269: 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 6, 0 };
270:
271: /* Table of class bit maps for each POSIX class. Each class is formed from a
272: base map, with an optional addition or removal of another map. Then, for some
273: classes, there is some additional tweaking: for [:blank:] the vertical space
274: characters are removed, and for [:alpha:] and [:alnum:] the underscore
275: character is removed. The triples in the table consist of the base map offset,
276: second map offset or -1 if no second map, and a non-negative value for map
277: addition or a negative value for map subtraction (if there are two maps). The
278: absolute value of the third field has these meanings: 0 => no tweaking, 1 =>
279: remove vertical space characters, 2 => remove underscore. */
280:
281: static const int posix_class_maps[] = {
282: cbit_word, cbit_digit, -2, /* alpha */
283: cbit_lower, -1, 0, /* lower */
284: cbit_upper, -1, 0, /* upper */
285: cbit_word, -1, 2, /* alnum - word without underscore */
286: cbit_print, cbit_cntrl, 0, /* ascii */
287: cbit_space, -1, 1, /* blank - a GNU extension */
288: cbit_cntrl, -1, 0, /* cntrl */
289: cbit_digit, -1, 0, /* digit */
290: cbit_graph, -1, 0, /* graph */
291: cbit_print, -1, 0, /* print */
292: cbit_punct, -1, 0, /* punct */
293: cbit_space, -1, 0, /* space */
294: cbit_word, -1, 0, /* word - a Perl extension */
295: cbit_xdigit,-1, 0 /* xdigit */
296: };
297:
298: /* Table of substitutes for \d etc when PCRE_UCP is set. The POSIX class
299: substitutes must be in the order of the names, defined above, and there are
300: both positive and negative cases. NULL means no substitute. */
301:
302: #ifdef SUPPORT_UCP
1.1.1.2 misho 303: static const pcre_uchar string_PNd[] = {
304: CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
305: CHAR_N, CHAR_d, CHAR_RIGHT_CURLY_BRACKET, '\0' };
306: static const pcre_uchar string_pNd[] = {
307: CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
308: CHAR_N, CHAR_d, CHAR_RIGHT_CURLY_BRACKET, '\0' };
309: static const pcre_uchar string_PXsp[] = {
310: CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
311: CHAR_X, CHAR_s, CHAR_p, CHAR_RIGHT_CURLY_BRACKET, '\0' };
312: static const pcre_uchar string_pXsp[] = {
313: CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
314: CHAR_X, CHAR_s, CHAR_p, CHAR_RIGHT_CURLY_BRACKET, '\0' };
315: static const pcre_uchar string_PXwd[] = {
316: CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
317: CHAR_X, CHAR_w, CHAR_d, CHAR_RIGHT_CURLY_BRACKET, '\0' };
318: static const pcre_uchar string_pXwd[] = {
319: CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
320: CHAR_X, CHAR_w, CHAR_d, CHAR_RIGHT_CURLY_BRACKET, '\0' };
321:
322: static const pcre_uchar *substitutes[] = {
323: string_PNd, /* \D */
324: string_pNd, /* \d */
325: string_PXsp, /* \S */ /* NOTE: Xsp is Perl space */
326: string_pXsp, /* \s */
327: string_PXwd, /* \W */
328: string_pXwd /* \w */
1.1 misho 329: };
330:
1.1.1.2 misho 331: static const pcre_uchar string_pL[] = {
332: CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
333: CHAR_L, CHAR_RIGHT_CURLY_BRACKET, '\0' };
334: static const pcre_uchar string_pLl[] = {
335: CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
336: CHAR_L, CHAR_l, CHAR_RIGHT_CURLY_BRACKET, '\0' };
337: static const pcre_uchar string_pLu[] = {
338: CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
339: CHAR_L, CHAR_u, CHAR_RIGHT_CURLY_BRACKET, '\0' };
340: static const pcre_uchar string_pXan[] = {
341: CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
342: CHAR_X, CHAR_a, CHAR_n, CHAR_RIGHT_CURLY_BRACKET, '\0' };
343: static const pcre_uchar string_h[] = {
344: CHAR_BACKSLASH, CHAR_h, '\0' };
345: static const pcre_uchar string_pXps[] = {
346: CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
347: CHAR_X, CHAR_p, CHAR_s, CHAR_RIGHT_CURLY_BRACKET, '\0' };
348: static const pcre_uchar string_PL[] = {
349: CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
350: CHAR_L, CHAR_RIGHT_CURLY_BRACKET, '\0' };
351: static const pcre_uchar string_PLl[] = {
352: CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
353: CHAR_L, CHAR_l, CHAR_RIGHT_CURLY_BRACKET, '\0' };
354: static const pcre_uchar string_PLu[] = {
355: CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
356: CHAR_L, CHAR_u, CHAR_RIGHT_CURLY_BRACKET, '\0' };
357: static const pcre_uchar string_PXan[] = {
358: CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
359: CHAR_X, CHAR_a, CHAR_n, CHAR_RIGHT_CURLY_BRACKET, '\0' };
360: static const pcre_uchar string_H[] = {
361: CHAR_BACKSLASH, CHAR_H, '\0' };
362: static const pcre_uchar string_PXps[] = {
363: CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
364: CHAR_X, CHAR_p, CHAR_s, CHAR_RIGHT_CURLY_BRACKET, '\0' };
365:
366: static const pcre_uchar *posix_substitutes[] = {
367: string_pL, /* alpha */
368: string_pLl, /* lower */
369: string_pLu, /* upper */
370: string_pXan, /* alnum */
371: NULL, /* ascii */
372: string_h, /* blank */
373: NULL, /* cntrl */
374: string_pNd, /* digit */
375: NULL, /* graph */
376: NULL, /* print */
377: NULL, /* punct */
378: string_pXps, /* space */ /* NOTE: Xps is POSIX space */
379: string_pXwd, /* word */
380: NULL, /* xdigit */
1.1 misho 381: /* Negated cases */
1.1.1.2 misho 382: string_PL, /* ^alpha */
383: string_PLl, /* ^lower */
384: string_PLu, /* ^upper */
385: string_PXan, /* ^alnum */
386: NULL, /* ^ascii */
387: string_H, /* ^blank */
388: NULL, /* ^cntrl */
389: string_PNd, /* ^digit */
390: NULL, /* ^graph */
391: NULL, /* ^print */
392: NULL, /* ^punct */
393: string_PXps, /* ^space */ /* NOTE: Xps is POSIX space */
394: string_PXwd, /* ^word */
395: NULL /* ^xdigit */
1.1 misho 396: };
1.1.1.2 misho 397: #define POSIX_SUBSIZE (sizeof(posix_substitutes) / sizeof(pcre_uchar *))
1.1 misho 398: #endif
399:
400: #define STRING(a) # a
401: #define XSTRING(s) STRING(s)
402:
403: /* The texts of compile-time error messages. These are "char *" because they
404: are passed to the outside world. Do not ever re-use any error number, because
405: they are documented. Always add a new error instead. Messages marked DEAD below
406: are no longer used. This used to be a table of strings, but in order to reduce
407: the number of relocations needed when a shared library is loaded dynamically,
408: it is now one long string. We cannot use a table of offsets, because the
409: lengths of inserts such as XSTRING(MAX_NAME_SIZE) are not known. Instead, we
410: simply count through to the one we want - this isn't a performance issue
411: because these strings are used only when there is a compilation error.
412:
413: Each substring ends with \0 to insert a null character. This includes the final
414: substring, so that the whole string ends with \0\0, which can be detected when
415: counting through. */
416:
417: static const char error_texts[] =
418: "no error\0"
419: "\\ at end of pattern\0"
420: "\\c at end of pattern\0"
421: "unrecognized character follows \\\0"
422: "numbers out of order in {} quantifier\0"
423: /* 5 */
424: "number too big in {} quantifier\0"
425: "missing terminating ] for character class\0"
426: "invalid escape sequence in character class\0"
427: "range out of order in character class\0"
428: "nothing to repeat\0"
429: /* 10 */
430: "operand of unlimited repeat could match the empty string\0" /** DEAD **/
431: "internal error: unexpected repeat\0"
432: "unrecognized character after (? or (?-\0"
433: "POSIX named classes are supported only within a class\0"
434: "missing )\0"
435: /* 15 */
436: "reference to non-existent subpattern\0"
437: "erroffset passed as NULL\0"
438: "unknown option bit(s) set\0"
439: "missing ) after comment\0"
440: "parentheses nested too deeply\0" /** DEAD **/
441: /* 20 */
442: "regular expression is too large\0"
443: "failed to get memory\0"
444: "unmatched parentheses\0"
445: "internal error: code overflow\0"
446: "unrecognized character after (?<\0"
447: /* 25 */
448: "lookbehind assertion is not fixed length\0"
449: "malformed number or name after (?(\0"
450: "conditional group contains more than two branches\0"
451: "assertion expected after (?(\0"
452: "(?R or (?[+-]digits must be followed by )\0"
453: /* 30 */
454: "unknown POSIX class name\0"
455: "POSIX collating elements are not supported\0"
1.1.1.2 misho 456: "this version of PCRE is compiled without UTF support\0"
1.1 misho 457: "spare error\0" /** DEAD **/
458: "character value in \\x{...} sequence is too large\0"
459: /* 35 */
460: "invalid condition (?(0)\0"
461: "\\C not allowed in lookbehind assertion\0"
462: "PCRE does not support \\L, \\l, \\N{name}, \\U, or \\u\0"
463: "number after (?C is > 255\0"
464: "closing ) for (?C expected\0"
465: /* 40 */
466: "recursive call could loop indefinitely\0"
467: "unrecognized character after (?P\0"
468: "syntax error in subpattern name (missing terminator)\0"
469: "two named subpatterns have the same name\0"
470: "invalid UTF-8 string\0"
471: /* 45 */
472: "support for \\P, \\p, and \\X has not been compiled\0"
473: "malformed \\P or \\p sequence\0"
474: "unknown property name after \\P or \\p\0"
475: "subpattern name is too long (maximum " XSTRING(MAX_NAME_SIZE) " characters)\0"
476: "too many named subpatterns (maximum " XSTRING(MAX_NAME_COUNT) ")\0"
477: /* 50 */
478: "repeated subpattern is too long\0" /** DEAD **/
1.1.1.2 misho 479: "octal value is greater than \\377 in 8-bit non-UTF-8 mode\0"
1.1 misho 480: "internal error: overran compiling workspace\0"
481: "internal error: previously-checked referenced subpattern not found\0"
482: "DEFINE group contains more than one branch\0"
483: /* 55 */
484: "repeating a DEFINE group is not allowed\0" /** DEAD **/
485: "inconsistent NEWLINE options\0"
486: "\\g is not followed by a braced, angle-bracketed, or quoted name/number or by a plain number\0"
487: "a numbered reference must not be zero\0"
488: "an argument is not allowed for (*ACCEPT), (*FAIL), or (*COMMIT)\0"
489: /* 60 */
1.1.1.4 ! misho 490: "(*VERB) not recognized or malformed\0"
1.1 misho 491: "number is too big\0"
492: "subpattern name expected\0"
493: "digit expected after (?+\0"
494: "] is an invalid data character in JavaScript compatibility mode\0"
495: /* 65 */
496: "different names for subpatterns of the same number are not allowed\0"
497: "(*MARK) must have an argument\0"
1.1.1.2 misho 498: "this version of PCRE is not compiled with Unicode property support\0"
1.1 misho 499: "\\c must be followed by an ASCII character\0"
500: "\\k is not followed by a braced, angle-bracketed, or quoted name\0"
501: /* 70 */
502: "internal error: unknown opcode in find_fixedlength()\0"
503: "\\N is not supported in a class\0"
504: "too many forward references\0"
1.1.1.2 misho 505: "disallowed Unicode code point (>= 0xd800 && <= 0xdfff)\0"
506: "invalid UTF-16 string\0"
1.1.1.3 misho 507: /* 75 */
508: "name is too long in (*MARK), (*PRUNE), (*SKIP), or (*THEN)\0"
509: "character value in \\u.... sequence is too large\0"
1.1.1.4 ! misho 510: "invalid UTF-32 string\0"
! 511: "setting UTF is disabled by the application\0"
1.1 misho 512: ;
513:
514: /* Table to identify digits and hex digits. This is used when compiling
515: patterns. Note that the tables in chartables are dependent on the locale, and
516: may mark arbitrary characters as digits - but the PCRE compiling code expects
517: to handle only 0-9, a-z, and A-Z as digits when compiling. That is why we have
518: a private table here. It costs 256 bytes, but it is a lot faster than doing
519: character value tests (at least in some simple cases I timed), and in some
520: applications one wants PCRE to compile efficiently as well as match
521: efficiently.
522:
523: For convenience, we use the same bit definitions as in chartables:
524:
525: 0x04 decimal digit
526: 0x08 hexadecimal digit
527:
528: Then we can use ctype_digit and ctype_xdigit in the code. */
529:
1.1.1.2 misho 530: /* Using a simple comparison for decimal numbers rather than a memory read
531: is much faster, and the resulting code is simpler (the compiler turns it
532: into a subtraction and unsigned comparison). */
533:
534: #define IS_DIGIT(x) ((x) >= CHAR_0 && (x) <= CHAR_9)
535:
1.1 misho 536: #ifndef EBCDIC
537:
538: /* This is the "normal" case, for ASCII systems, and EBCDIC systems running in
539: UTF-8 mode. */
540:
1.1.1.2 misho 541: static const pcre_uint8 digitab[] =
1.1 misho 542: {
543: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 0- 7 */
544: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 8- 15 */
545: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 16- 23 */
546: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 24- 31 */
547: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* - ' */
548: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* ( - / */
549: 0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c, /* 0 - 7 */
550: 0x0c,0x0c,0x00,0x00,0x00,0x00,0x00,0x00, /* 8 - ? */
551: 0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x00, /* @ - G */
552: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* H - O */
553: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* P - W */
554: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* X - _ */
555: 0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x00, /* ` - g */
556: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* h - o */
557: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* p - w */
558: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* x -127 */
559: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 128-135 */
560: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 136-143 */
561: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 144-151 */
562: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 152-159 */
563: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 160-167 */
564: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 168-175 */
565: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 176-183 */
566: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 184-191 */
567: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 192-199 */
568: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 200-207 */
569: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 208-215 */
570: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 216-223 */
571: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 224-231 */
572: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 232-239 */
573: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 240-247 */
574: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};/* 248-255 */
575:
576: #else
577:
578: /* This is the "abnormal" case, for EBCDIC systems not running in UTF-8 mode. */
579:
1.1.1.2 misho 580: static const pcre_uint8 digitab[] =
1.1 misho 581: {
582: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 0- 7 0 */
583: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 8- 15 */
584: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 16- 23 10 */
585: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 24- 31 */
586: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 32- 39 20 */
587: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 40- 47 */
588: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 48- 55 30 */
589: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 56- 63 */
590: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* - 71 40 */
591: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 72- | */
592: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* & - 87 50 */
593: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 88- 95 */
594: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* - -103 60 */
595: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 104- ? */
596: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 112-119 70 */
597: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 120- " */
598: 0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x00, /* 128- g 80 */
599: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* h -143 */
600: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 144- p 90 */
601: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* q -159 */
602: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 160- x A0 */
603: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* y -175 */
604: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* ^ -183 B0 */
605: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 184-191 */
606: 0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x00, /* { - G C0 */
607: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* H -207 */
608: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* } - P D0 */
609: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* Q -223 */
610: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* \ - X E0 */
611: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* Y -239 */
612: 0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c, /* 0 - 7 F0 */
613: 0x0c,0x0c,0x00,0x00,0x00,0x00,0x00,0x00};/* 8 -255 */
614:
1.1.1.2 misho 615: static const pcre_uint8 ebcdic_chartab[] = { /* chartable partial dup */
1.1 misho 616: 0x80,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /* 0- 7 */
617: 0x00,0x00,0x00,0x00,0x01,0x01,0x00,0x00, /* 8- 15 */
618: 0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /* 16- 23 */
619: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 24- 31 */
620: 0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /* 32- 39 */
621: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 40- 47 */
622: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 48- 55 */
623: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 56- 63 */
624: 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* - 71 */
625: 0x00,0x00,0x00,0x80,0x00,0x80,0x80,0x80, /* 72- | */
626: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* & - 87 */
627: 0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00, /* 88- 95 */
628: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* - -103 */
629: 0x00,0x00,0x00,0x00,0x00,0x10,0x00,0x80, /* 104- ? */
630: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 112-119 */
631: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 120- " */
632: 0x00,0x1a,0x1a,0x1a,0x1a,0x1a,0x1a,0x12, /* 128- g */
633: 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* h -143 */
634: 0x00,0x12,0x12,0x12,0x12,0x12,0x12,0x12, /* 144- p */
635: 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* q -159 */
636: 0x00,0x00,0x12,0x12,0x12,0x12,0x12,0x12, /* 160- x */
637: 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* y -175 */
638: 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* ^ -183 */
639: 0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x00, /* 184-191 */
640: 0x80,0x1a,0x1a,0x1a,0x1a,0x1a,0x1a,0x12, /* { - G */
641: 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* H -207 */
642: 0x00,0x12,0x12,0x12,0x12,0x12,0x12,0x12, /* } - P */
643: 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* Q -223 */
644: 0x00,0x00,0x12,0x12,0x12,0x12,0x12,0x12, /* \ - X */
645: 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* Y -239 */
646: 0x1c,0x1c,0x1c,0x1c,0x1c,0x1c,0x1c,0x1c, /* 0 - 7 */
647: 0x1c,0x1c,0x00,0x00,0x00,0x00,0x00,0x00};/* 8 -255 */
648: #endif
649:
650:
651:
652: /*************************************************
653: * Find an error text *
654: *************************************************/
655:
656: /* The error texts are now all in one long string, to save on relocations. As
657: some of the text is of unknown length, we can't use a table of offsets.
658: Instead, just count through the strings. This is not a performance issue
659: because it happens only when there has been a compilation error.
660:
661: Argument: the error number
662: Returns: pointer to the error string
663: */
664:
665: static const char *
666: find_error_text(int n)
667: {
668: const char *s = error_texts;
669: for (; n > 0; n--)
670: {
1.1.1.4 ! misho 671: while (*s++ != CHAR_NULL) {};
! 672: if (*s == CHAR_NULL) return "Error text not found (please report)";
1.1 misho 673: }
674: return s;
675: }
676:
677:
678: /*************************************************
679: * Expand the workspace *
680: *************************************************/
681:
682: /* This function is called during the second compiling phase, if the number of
683: forward references fills the existing workspace, which is originally a block on
684: the stack. A larger block is obtained from malloc() unless the ultimate limit
685: has been reached or the increase will be rather small.
686:
687: Argument: pointer to the compile data block
688: Returns: 0 if all went well, else an error number
689: */
690:
691: static int
692: expand_workspace(compile_data *cd)
693: {
1.1.1.2 misho 694: pcre_uchar *newspace;
1.1 misho 695: int newsize = cd->workspace_size * 2;
696:
697: if (newsize > COMPILE_WORK_SIZE_MAX) newsize = COMPILE_WORK_SIZE_MAX;
698: if (cd->workspace_size >= COMPILE_WORK_SIZE_MAX ||
699: newsize - cd->workspace_size < WORK_SIZE_SAFETY_MARGIN)
700: return ERR72;
701:
1.1.1.2 misho 702: newspace = (PUBL(malloc))(IN_UCHARS(newsize));
1.1 misho 703: if (newspace == NULL) return ERR21;
1.1.1.2 misho 704: memcpy(newspace, cd->start_workspace, cd->workspace_size * sizeof(pcre_uchar));
705: cd->hwm = (pcre_uchar *)newspace + (cd->hwm - cd->start_workspace);
1.1 misho 706: if (cd->workspace_size > COMPILE_WORK_SIZE)
1.1.1.2 misho 707: (PUBL(free))((void *)cd->start_workspace);
1.1 misho 708: cd->start_workspace = newspace;
709: cd->workspace_size = newsize;
710: return 0;
711: }
712:
713:
714:
715: /*************************************************
716: * Check for counted repeat *
717: *************************************************/
718:
719: /* This function is called when a '{' is encountered in a place where it might
720: start a quantifier. It looks ahead to see if it really is a quantifier or not.
721: It is only a quantifier if it is one of the forms {ddd} {ddd,} or {ddd,ddd}
722: where the ddds are digits.
723:
724: Arguments:
725: p pointer to the first char after '{'
726:
727: Returns: TRUE or FALSE
728: */
729:
730: static BOOL
1.1.1.2 misho 731: is_counted_repeat(const pcre_uchar *p)
1.1 misho 732: {
1.1.1.2 misho 733: if (!IS_DIGIT(*p)) return FALSE;
734: p++;
735: while (IS_DIGIT(*p)) p++;
1.1 misho 736: if (*p == CHAR_RIGHT_CURLY_BRACKET) return TRUE;
737:
738: if (*p++ != CHAR_COMMA) return FALSE;
739: if (*p == CHAR_RIGHT_CURLY_BRACKET) return TRUE;
740:
1.1.1.2 misho 741: if (!IS_DIGIT(*p)) return FALSE;
742: p++;
743: while (IS_DIGIT(*p)) p++;
1.1 misho 744:
745: return (*p == CHAR_RIGHT_CURLY_BRACKET);
746: }
747:
748:
749:
750: /*************************************************
751: * Handle escapes *
752: *************************************************/
753:
754: /* This function is called when a \ has been encountered. It either returns a
1.1.1.4 ! misho 755: positive value for a simple escape such as \n, or 0 for a data character
! 756: which will be placed in chptr. A backreference to group n is returned as
! 757: negative n. When UTF-8 is enabled, a positive value greater than 255 may
! 758: be returned in chptr.
! 759: On entry,ptr is pointing at the \. On exit, it is on the final character of the
! 760: escape sequence.
1.1 misho 761:
762: Arguments:
763: ptrptr points to the pattern position pointer
1.1.1.4 ! misho 764: chptr points to the data character
1.1 misho 765: errorcodeptr points to the errorcode variable
766: bracount number of previous extracting brackets
767: options the options bits
768: isclass TRUE if inside a character class
769:
1.1.1.4 ! misho 770: Returns: zero => a data character
! 771: positive => a special escape sequence
! 772: negative => a back reference
1.1 misho 773: on error, errorcodeptr is set
774: */
775:
776: static int
1.1.1.4 ! misho 777: check_escape(const pcre_uchar **ptrptr, pcre_uint32 *chptr, int *errorcodeptr,
! 778: int bracount, int options, BOOL isclass)
1.1 misho 779: {
1.1.1.2 misho 780: /* PCRE_UTF16 has the same value as PCRE_UTF8. */
781: BOOL utf = (options & PCRE_UTF8) != 0;
782: const pcre_uchar *ptr = *ptrptr + 1;
1.1.1.4 ! misho 783: pcre_uint32 c;
! 784: int escape = 0;
1.1.1.2 misho 785: int i;
1.1 misho 786:
787: GETCHARINCTEST(c, ptr); /* Get character value, increment pointer */
788: ptr--; /* Set pointer back to the last byte */
789:
790: /* If backslash is at the end of the pattern, it's an error. */
791:
1.1.1.4 ! misho 792: if (c == CHAR_NULL) *errorcodeptr = ERR1;
1.1 misho 793:
794: /* Non-alphanumerics are literals. For digits or letters, do an initial lookup
795: in a table. A non-zero result is something that can be returned immediately.
796: Otherwise further processing may be required. */
797:
798: #ifndef EBCDIC /* ASCII/UTF-8 coding */
1.1.1.2 misho 799: /* Not alphanumeric */
800: else if (c < CHAR_0 || c > CHAR_z) {}
1.1.1.4 ! misho 801: else if ((i = escapes[c - CHAR_0]) != 0)
! 802: { if (i > 0) c = (pcre_uint32)i; else escape = -i; }
1.1 misho 803:
804: #else /* EBCDIC coding */
1.1.1.2 misho 805: /* Not alphanumeric */
1.1.1.4 ! misho 806: else if (c < CHAR_a || (!MAX_255(c) || (ebcdic_chartab[c] & 0x0E) == 0)) {}
! 807: else if ((i = escapes[c - 0x48]) != 0) { if (i > 0) c = (pcre_uint32)i; else escape = -i; }
1.1 misho 808: #endif
809:
810: /* Escapes that need further processing, or are illegal. */
811:
812: else
813: {
1.1.1.2 misho 814: const pcre_uchar *oldptr;
1.1.1.4 ! misho 815: BOOL braced, negated, overflow;
! 816: int s;
1.1 misho 817:
818: switch (c)
819: {
820: /* A number of Perl escapes are not handled by PCRE. We give an explicit
821: error. */
822:
823: case CHAR_l:
824: case CHAR_L:
825: *errorcodeptr = ERR37;
826: break;
827:
828: case CHAR_u:
829: if ((options & PCRE_JAVASCRIPT_COMPAT) != 0)
830: {
831: /* In JavaScript, \u must be followed by four hexadecimal numbers.
832: Otherwise it is a lowercase u letter. */
1.1.1.2 misho 833: if (MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0
834: && MAX_255(ptr[2]) && (digitab[ptr[2]] & ctype_xdigit) != 0
835: && MAX_255(ptr[3]) && (digitab[ptr[3]] & ctype_xdigit) != 0
836: && MAX_255(ptr[4]) && (digitab[ptr[4]] & ctype_xdigit) != 0)
1.1 misho 837: {
838: c = 0;
839: for (i = 0; i < 4; ++i)
840: {
1.1.1.4 ! misho 841: register pcre_uint32 cc = *(++ptr);
1.1 misho 842: #ifndef EBCDIC /* ASCII/UTF-8 coding */
843: if (cc >= CHAR_a) cc -= 32; /* Convert to upper case */
844: c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));
845: #else /* EBCDIC coding */
846: if (cc >= CHAR_a && cc <= CHAR_z) cc += 64; /* Convert to upper case */
847: c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
848: #endif
849: }
1.1.1.3 misho 850:
1.1.1.4 ! misho 851: #if defined COMPILE_PCRE8
! 852: if (c > (utf ? 0x10ffffU : 0xffU))
! 853: #elif defined COMPILE_PCRE16
! 854: if (c > (utf ? 0x10ffffU : 0xffffU))
! 855: #elif defined COMPILE_PCRE32
! 856: if (utf && c > 0x10ffffU)
1.1.1.3 misho 857: #endif
858: {
859: *errorcodeptr = ERR76;
860: }
861: else if (utf && c >= 0xd800 && c <= 0xdfff) *errorcodeptr = ERR73;
1.1 misho 862: }
863: }
864: else
865: *errorcodeptr = ERR37;
866: break;
867:
868: case CHAR_U:
869: /* In JavaScript, \U is an uppercase U letter. */
870: if ((options & PCRE_JAVASCRIPT_COMPAT) == 0) *errorcodeptr = ERR37;
871: break;
872:
873: /* In a character class, \g is just a literal "g". Outside a character
874: class, \g must be followed by one of a number of specific things:
875:
876: (1) A number, either plain or braced. If positive, it is an absolute
877: backreference. If negative, it is a relative backreference. This is a Perl
878: 5.10 feature.
879:
880: (2) Perl 5.10 also supports \g{name} as a reference to a named group. This
881: is part of Perl's movement towards a unified syntax for back references. As
882: this is synonymous with \k{name}, we fudge it up by pretending it really
883: was \k.
884:
885: (3) For Oniguruma compatibility we also support \g followed by a name or a
886: number either in angle brackets or in single quotes. However, these are
887: (possibly recursive) subroutine calls, _not_ backreferences. Just return
1.1.1.4 ! misho 888: the ESC_g code (cf \k). */
1.1 misho 889:
890: case CHAR_g:
891: if (isclass) break;
892: if (ptr[1] == CHAR_LESS_THAN_SIGN || ptr[1] == CHAR_APOSTROPHE)
893: {
1.1.1.4 ! misho 894: escape = ESC_g;
1.1 misho 895: break;
896: }
897:
898: /* Handle the Perl-compatible cases */
899:
900: if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)
901: {
1.1.1.2 misho 902: const pcre_uchar *p;
1.1.1.4 ! misho 903: for (p = ptr+2; *p != CHAR_NULL && *p != CHAR_RIGHT_CURLY_BRACKET; p++)
1.1.1.2 misho 904: if (*p != CHAR_MINUS && !IS_DIGIT(*p)) break;
1.1.1.4 ! misho 905: if (*p != CHAR_NULL && *p != CHAR_RIGHT_CURLY_BRACKET)
1.1 misho 906: {
1.1.1.4 ! misho 907: escape = ESC_k;
1.1 misho 908: break;
909: }
910: braced = TRUE;
911: ptr++;
912: }
913: else braced = FALSE;
914:
915: if (ptr[1] == CHAR_MINUS)
916: {
917: negated = TRUE;
918: ptr++;
919: }
920: else negated = FALSE;
921:
1.1.1.2 misho 922: /* The integer range is limited by the machine's int representation. */
1.1.1.4 ! misho 923: s = 0;
! 924: overflow = FALSE;
1.1.1.2 misho 925: while (IS_DIGIT(ptr[1]))
926: {
1.1.1.4 ! misho 927: if (s > INT_MAX / 10 - 1) /* Integer overflow */
1.1.1.2 misho 928: {
1.1.1.4 ! misho 929: overflow = TRUE;
1.1.1.2 misho 930: break;
931: }
1.1.1.4 ! misho 932: s = s * 10 + (int)(*(++ptr) - CHAR_0);
1.1.1.2 misho 933: }
1.1.1.4 ! misho 934: if (overflow) /* Integer overflow */
1.1 misho 935: {
1.1.1.2 misho 936: while (IS_DIGIT(ptr[1]))
937: ptr++;
1.1 misho 938: *errorcodeptr = ERR61;
939: break;
940: }
941:
942: if (braced && *(++ptr) != CHAR_RIGHT_CURLY_BRACKET)
943: {
944: *errorcodeptr = ERR57;
945: break;
946: }
947:
1.1.1.4 ! misho 948: if (s == 0)
1.1 misho 949: {
950: *errorcodeptr = ERR58;
951: break;
952: }
953:
954: if (negated)
955: {
1.1.1.4 ! misho 956: if (s > bracount)
1.1 misho 957: {
958: *errorcodeptr = ERR15;
959: break;
960: }
1.1.1.4 ! misho 961: s = bracount - (s - 1);
1.1 misho 962: }
963:
1.1.1.4 ! misho 964: escape = -s;
1.1 misho 965: break;
966:
967: /* The handling of escape sequences consisting of a string of digits
968: starting with one that is not zero is not straightforward. By experiment,
969: the way Perl works seems to be as follows:
970:
971: Outside a character class, the digits are read as a decimal number. If the
972: number is less than 10, or if there are that many previous extracting
973: left brackets, then it is a back reference. Otherwise, up to three octal
974: digits are read to form an escaped byte. Thus \123 is likely to be octal
975: 123 (cf \0123, which is octal 012 followed by the literal 3). If the octal
976: value is greater than 377, the least significant 8 bits are taken. Inside a
977: character class, \ followed by a digit is always an octal number. */
978:
979: case CHAR_1: case CHAR_2: case CHAR_3: case CHAR_4: case CHAR_5:
980: case CHAR_6: case CHAR_7: case CHAR_8: case CHAR_9:
981:
982: if (!isclass)
983: {
984: oldptr = ptr;
1.1.1.2 misho 985: /* The integer range is limited by the machine's int representation. */
1.1.1.4 ! misho 986: s = (int)(c -CHAR_0);
! 987: overflow = FALSE;
1.1.1.2 misho 988: while (IS_DIGIT(ptr[1]))
989: {
1.1.1.4 ! misho 990: if (s > INT_MAX / 10 - 1) /* Integer overflow */
1.1.1.2 misho 991: {
1.1.1.4 ! misho 992: overflow = TRUE;
1.1.1.2 misho 993: break;
994: }
1.1.1.4 ! misho 995: s = s * 10 + (int)(*(++ptr) - CHAR_0);
1.1.1.2 misho 996: }
1.1.1.4 ! misho 997: if (overflow) /* Integer overflow */
1.1 misho 998: {
1.1.1.2 misho 999: while (IS_DIGIT(ptr[1]))
1000: ptr++;
1.1 misho 1001: *errorcodeptr = ERR61;
1002: break;
1003: }
1.1.1.4 ! misho 1004: if (s < 10 || s <= bracount)
1.1 misho 1005: {
1.1.1.4 ! misho 1006: escape = -s;
1.1 misho 1007: break;
1008: }
1009: ptr = oldptr; /* Put the pointer back and fall through */
1010: }
1011:
1012: /* Handle an octal number following \. If the first digit is 8 or 9, Perl
1013: generates a binary zero byte and treats the digit as a following literal.
1014: Thus we have to pull back the pointer by one. */
1015:
1016: if ((c = *ptr) >= CHAR_8)
1017: {
1018: ptr--;
1019: c = 0;
1020: break;
1021: }
1022:
1023: /* \0 always starts an octal number, but we may drop through to here with a
1024: larger first octal digit. The original code used just to take the least
1025: significant 8 bits of octal numbers (I think this is what early Perls used
1.1.1.2 misho 1026: to do). Nowadays we allow for larger numbers in UTF-8 mode and 16-bit mode,
1027: but no more than 3 octal digits. */
1.1 misho 1028:
1029: case CHAR_0:
1030: c -= CHAR_0;
1031: while(i++ < 2 && ptr[1] >= CHAR_0 && ptr[1] <= CHAR_7)
1032: c = c * 8 + *(++ptr) - CHAR_0;
1.1.1.2 misho 1033: #ifdef COMPILE_PCRE8
1034: if (!utf && c > 0xff) *errorcodeptr = ERR51;
1035: #endif
1.1 misho 1036: break;
1037:
1038: /* \x is complicated. \x{ddd} is a character number which can be greater
1.1.1.2 misho 1039: than 0xff in utf or non-8bit mode, but only if the ddd are hex digits.
1040: If not, { is treated as a data character. */
1.1 misho 1041:
1042: case CHAR_x:
1043: if ((options & PCRE_JAVASCRIPT_COMPAT) != 0)
1044: {
1045: /* In JavaScript, \x must be followed by two hexadecimal numbers.
1046: Otherwise it is a lowercase x letter. */
1.1.1.2 misho 1047: if (MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0
1048: && MAX_255(ptr[2]) && (digitab[ptr[2]] & ctype_xdigit) != 0)
1.1 misho 1049: {
1050: c = 0;
1051: for (i = 0; i < 2; ++i)
1052: {
1.1.1.4 ! misho 1053: register pcre_uint32 cc = *(++ptr);
1.1 misho 1054: #ifndef EBCDIC /* ASCII/UTF-8 coding */
1055: if (cc >= CHAR_a) cc -= 32; /* Convert to upper case */
1056: c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));
1057: #else /* EBCDIC coding */
1058: if (cc >= CHAR_a && cc <= CHAR_z) cc += 64; /* Convert to upper case */
1059: c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
1060: #endif
1061: }
1062: }
1063: break;
1064: }
1065:
1066: if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)
1067: {
1.1.1.2 misho 1068: const pcre_uchar *pt = ptr + 2;
1.1 misho 1069:
1070: c = 0;
1.1.1.4 ! misho 1071: overflow = FALSE;
1.1.1.2 misho 1072: while (MAX_255(*pt) && (digitab[*pt] & ctype_xdigit) != 0)
1.1 misho 1073: {
1.1.1.4 ! misho 1074: register pcre_uint32 cc = *pt++;
1.1 misho 1075: if (c == 0 && cc == CHAR_0) continue; /* Leading zeroes */
1076:
1.1.1.4 ! misho 1077: #ifdef COMPILE_PCRE32
! 1078: if (c >= 0x10000000l) { overflow = TRUE; break; }
! 1079: #endif
! 1080:
1.1 misho 1081: #ifndef EBCDIC /* ASCII/UTF-8 coding */
1082: if (cc >= CHAR_a) cc -= 32; /* Convert to upper case */
1083: c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));
1084: #else /* EBCDIC coding */
1085: if (cc >= CHAR_a && cc <= CHAR_z) cc += 64; /* Convert to upper case */
1086: c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
1087: #endif
1.1.1.2 misho 1088:
1.1.1.4 ! misho 1089: #if defined COMPILE_PCRE8
! 1090: if (c > (utf ? 0x10ffffU : 0xffU)) { overflow = TRUE; break; }
! 1091: #elif defined COMPILE_PCRE16
! 1092: if (c > (utf ? 0x10ffffU : 0xffffU)) { overflow = TRUE; break; }
! 1093: #elif defined COMPILE_PCRE32
! 1094: if (utf && c > 0x10ffffU) { overflow = TRUE; break; }
1.1.1.2 misho 1095: #endif
1096: }
1097:
1.1.1.4 ! misho 1098: if (overflow)
1.1.1.2 misho 1099: {
1100: while (MAX_255(*pt) && (digitab[*pt] & ctype_xdigit) != 0) pt++;
1101: *errorcodeptr = ERR34;
1.1 misho 1102: }
1103:
1104: if (*pt == CHAR_RIGHT_CURLY_BRACKET)
1105: {
1.1.1.2 misho 1106: if (utf && c >= 0xd800 && c <= 0xdfff) *errorcodeptr = ERR73;
1.1 misho 1107: ptr = pt;
1108: break;
1109: }
1110:
1111: /* If the sequence of hex digits does not end with '}', then we don't
1112: recognize this construct; fall through to the normal \x handling. */
1113: }
1114:
1115: /* Read just a single-byte hex-defined char */
1116:
1117: c = 0;
1.1.1.2 misho 1118: while (i++ < 2 && MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0)
1.1 misho 1119: {
1.1.1.4 ! misho 1120: pcre_uint32 cc; /* Some compilers don't like */
1.1 misho 1121: cc = *(++ptr); /* ++ in initializers */
1122: #ifndef EBCDIC /* ASCII/UTF-8 coding */
1123: if (cc >= CHAR_a) cc -= 32; /* Convert to upper case */
1124: c = c * 16 + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));
1125: #else /* EBCDIC coding */
1126: if (cc <= CHAR_z) cc += 64; /* Convert to upper case */
1127: c = c * 16 + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
1128: #endif
1129: }
1130: break;
1131:
1132: /* For \c, a following letter is upper-cased; then the 0x40 bit is flipped.
1133: An error is given if the byte following \c is not an ASCII character. This
1134: coding is ASCII-specific, but then the whole concept of \cx is
1135: ASCII-specific. (However, an EBCDIC equivalent has now been added.) */
1136:
1137: case CHAR_c:
1138: c = *(++ptr);
1.1.1.4 ! misho 1139: if (c == CHAR_NULL)
1.1 misho 1140: {
1141: *errorcodeptr = ERR2;
1142: break;
1143: }
1144: #ifndef EBCDIC /* ASCII/UTF-8 coding */
1145: if (c > 127) /* Excludes all non-ASCII in either mode */
1146: {
1147: *errorcodeptr = ERR68;
1148: break;
1149: }
1150: if (c >= CHAR_a && c <= CHAR_z) c -= 32;
1151: c ^= 0x40;
1152: #else /* EBCDIC coding */
1153: if (c >= CHAR_a && c <= CHAR_z) c += 64;
1154: c ^= 0xC0;
1155: #endif
1156: break;
1157:
1158: /* PCRE_EXTRA enables extensions to Perl in the matter of escapes. Any
1159: other alphanumeric following \ is an error if PCRE_EXTRA was set;
1160: otherwise, for Perl compatibility, it is a literal. This code looks a bit
1161: odd, but there used to be some cases other than the default, and there may
1162: be again in future, so I haven't "optimized" it. */
1163:
1164: default:
1165: if ((options & PCRE_EXTRA) != 0) switch(c)
1166: {
1167: default:
1168: *errorcodeptr = ERR3;
1169: break;
1170: }
1171: break;
1172: }
1173: }
1174:
1175: /* Perl supports \N{name} for character names, as well as plain \N for "not
1176: newline". PCRE does not support \N{name}. However, it does support
1177: quantification such as \N{2,3}. */
1178:
1.1.1.4 ! misho 1179: if (escape == ESC_N && ptr[1] == CHAR_LEFT_CURLY_BRACKET &&
1.1 misho 1180: !is_counted_repeat(ptr+2))
1181: *errorcodeptr = ERR37;
1182:
1183: /* If PCRE_UCP is set, we change the values for \d etc. */
1184:
1.1.1.4 ! misho 1185: if ((options & PCRE_UCP) != 0 && escape >= ESC_D && escape <= ESC_w)
! 1186: escape += (ESC_DU - ESC_D);
1.1 misho 1187:
1188: /* Set the pointer to the final character before returning. */
1189:
1190: *ptrptr = ptr;
1.1.1.4 ! misho 1191: *chptr = c;
! 1192: return escape;
1.1 misho 1193: }
1194:
1195: #ifdef SUPPORT_UCP
1196: /*************************************************
1197: * Handle \P and \p *
1198: *************************************************/
1199:
1200: /* This function is called after \P or \p has been encountered, provided that
1201: PCRE is compiled with support for Unicode properties. On entry, ptrptr is
1202: pointing at the P or p. On exit, it is pointing at the final character of the
1203: escape sequence.
1204:
1205: Argument:
1206: ptrptr points to the pattern position pointer
1207: negptr points to a boolean that is set TRUE for negation else FALSE
1.1.1.4 ! misho 1208: ptypeptr points to an unsigned int that is set to the type value
! 1209: pdataptr points to an unsigned int that is set to the detailed property value
1.1 misho 1210: errorcodeptr points to the error code variable
1211:
1.1.1.4 ! misho 1212: Returns: TRUE if the type value was found, or FALSE for an invalid type
1.1 misho 1213: */
1214:
1.1.1.4 ! misho 1215: static BOOL
! 1216: get_ucp(const pcre_uchar **ptrptr, BOOL *negptr, unsigned int *ptypeptr,
! 1217: unsigned int *pdataptr, int *errorcodeptr)
1.1 misho 1218: {
1.1.1.4 ! misho 1219: pcre_uchar c;
! 1220: int i, bot, top;
1.1.1.2 misho 1221: const pcre_uchar *ptr = *ptrptr;
1222: pcre_uchar name[32];
1.1 misho 1223:
1224: c = *(++ptr);
1.1.1.4 ! misho 1225: if (c == CHAR_NULL) goto ERROR_RETURN;
1.1 misho 1226:
1227: *negptr = FALSE;
1228:
1229: /* \P or \p can be followed by a name in {}, optionally preceded by ^ for
1230: negation. */
1231:
1232: if (c == CHAR_LEFT_CURLY_BRACKET)
1233: {
1234: if (ptr[1] == CHAR_CIRCUMFLEX_ACCENT)
1235: {
1236: *negptr = TRUE;
1237: ptr++;
1238: }
1.1.1.2 misho 1239: for (i = 0; i < (int)(sizeof(name) / sizeof(pcre_uchar)) - 1; i++)
1.1 misho 1240: {
1241: c = *(++ptr);
1.1.1.4 ! misho 1242: if (c == CHAR_NULL) goto ERROR_RETURN;
1.1 misho 1243: if (c == CHAR_RIGHT_CURLY_BRACKET) break;
1244: name[i] = c;
1245: }
1246: if (c != CHAR_RIGHT_CURLY_BRACKET) goto ERROR_RETURN;
1247: name[i] = 0;
1248: }
1249:
1250: /* Otherwise there is just one following character */
1251:
1252: else
1253: {
1254: name[0] = c;
1255: name[1] = 0;
1256: }
1257:
1258: *ptrptr = ptr;
1259:
1260: /* Search for a recognized property name using binary chop */
1261:
1262: bot = 0;
1.1.1.2 misho 1263: top = PRIV(utt_size);
1.1 misho 1264:
1265: while (bot < top)
1266: {
1.1.1.4 ! misho 1267: int r;
1.1 misho 1268: i = (bot + top) >> 1;
1.1.1.4 ! misho 1269: r = STRCMP_UC_C8(name, PRIV(utt_names) + PRIV(utt)[i].name_offset);
! 1270: if (r == 0)
1.1 misho 1271: {
1.1.1.4 ! misho 1272: *ptypeptr = PRIV(utt)[i].type;
! 1273: *pdataptr = PRIV(utt)[i].value;
! 1274: return TRUE;
1.1 misho 1275: }
1.1.1.4 ! misho 1276: if (r > 0) bot = i + 1; else top = i;
1.1 misho 1277: }
1278:
1279: *errorcodeptr = ERR47;
1280: *ptrptr = ptr;
1.1.1.4 ! misho 1281: return FALSE;
1.1 misho 1282:
1283: ERROR_RETURN:
1284: *errorcodeptr = ERR46;
1285: *ptrptr = ptr;
1.1.1.4 ! misho 1286: return FALSE;
1.1 misho 1287: }
1288: #endif
1289:
1290:
1291:
1292:
1293: /*************************************************
1294: * Read repeat counts *
1295: *************************************************/
1296:
1297: /* Read an item of the form {n,m} and return the values. This is called only
1298: after is_counted_repeat() has confirmed that a repeat-count quantifier exists,
1299: so the syntax is guaranteed to be correct, but we need to check the values.
1300:
1301: Arguments:
1302: p pointer to first char after '{'
1303: minp pointer to int for min
1304: maxp pointer to int for max
1305: returned as -1 if no max
1306: errorcodeptr points to error code variable
1307:
1308: Returns: pointer to '}' on success;
1309: current ptr on error, with errorcodeptr set non-zero
1310: */
1311:
1.1.1.2 misho 1312: static const pcre_uchar *
1313: read_repeat_counts(const pcre_uchar *p, int *minp, int *maxp, int *errorcodeptr)
1.1 misho 1314: {
1315: int min = 0;
1316: int max = -1;
1317:
1318: /* Read the minimum value and do a paranoid check: a negative value indicates
1319: an integer overflow. */
1320:
1.1.1.4 ! misho 1321: while (IS_DIGIT(*p)) min = min * 10 + (int)(*p++ - CHAR_0);
1.1 misho 1322: if (min < 0 || min > 65535)
1323: {
1324: *errorcodeptr = ERR5;
1325: return p;
1326: }
1327:
1328: /* Read the maximum value if there is one, and again do a paranoid on its size.
1329: Also, max must not be less than min. */
1330:
1331: if (*p == CHAR_RIGHT_CURLY_BRACKET) max = min; else
1332: {
1333: if (*(++p) != CHAR_RIGHT_CURLY_BRACKET)
1334: {
1335: max = 0;
1.1.1.4 ! misho 1336: while(IS_DIGIT(*p)) max = max * 10 + (int)(*p++ - CHAR_0);
1.1 misho 1337: if (max < 0 || max > 65535)
1338: {
1339: *errorcodeptr = ERR5;
1340: return p;
1341: }
1342: if (max < min)
1343: {
1344: *errorcodeptr = ERR4;
1345: return p;
1346: }
1347: }
1348: }
1349:
1350: /* Fill in the required variables, and pass back the pointer to the terminating
1351: '}'. */
1352:
1353: *minp = min;
1354: *maxp = max;
1355: return p;
1356: }
1357:
1358:
1359:
1360: /*************************************************
1361: * Subroutine for finding forward reference *
1362: *************************************************/
1363:
1364: /* This recursive function is called only from find_parens() below. The
1365: top-level call starts at the beginning of the pattern. All other calls must
1366: start at a parenthesis. It scans along a pattern's text looking for capturing
1367: subpatterns, and counting them. If it finds a named pattern that matches the
1368: name it is given, it returns its number. Alternatively, if the name is NULL, it
1369: returns when it reaches a given numbered subpattern. Recursion is used to keep
1370: track of subpatterns that reset the capturing group numbers - the (?| feature.
1371:
1372: This function was originally called only from the second pass, in which we know
1373: that if (?< or (?' or (?P< is encountered, the name will be correctly
1374: terminated because that is checked in the first pass. There is now one call to
1375: this function in the first pass, to check for a recursive back reference by
1376: name (so that we can make the whole group atomic). In this case, we need check
1377: only up to the current position in the pattern, and that is still OK because
1378: and previous occurrences will have been checked. To make this work, the test
1379: for "end of pattern" is a check against cd->end_pattern in the main loop,
1380: instead of looking for a binary zero. This means that the special first-pass
1381: call can adjust cd->end_pattern temporarily. (Checks for binary zero while
1382: processing items within the loop are OK, because afterwards the main loop will
1383: terminate.)
1384:
1385: Arguments:
1386: ptrptr address of the current character pointer (updated)
1387: cd compile background data
1388: name name to seek, or NULL if seeking a numbered subpattern
1389: lorn name length, or subpattern number if name is NULL
1390: xmode TRUE if we are in /x mode
1.1.1.4 ! misho 1391: utf TRUE if we are in UTF-8 / UTF-16 / UTF-32 mode
1.1 misho 1392: count pointer to the current capturing subpattern number (updated)
1393:
1394: Returns: the number of the named subpattern, or -1 if not found
1395: */
1396:
1397: static int
1.1.1.2 misho 1398: find_parens_sub(pcre_uchar **ptrptr, compile_data *cd, const pcre_uchar *name, int lorn,
1399: BOOL xmode, BOOL utf, int *count)
1.1 misho 1400: {
1.1.1.2 misho 1401: pcre_uchar *ptr = *ptrptr;
1.1 misho 1402: int start_count = *count;
1403: int hwm_count = start_count;
1404: BOOL dup_parens = FALSE;
1405:
1406: /* If the first character is a parenthesis, check on the type of group we are
1407: dealing with. The very first call may not start with a parenthesis. */
1408:
1409: if (ptr[0] == CHAR_LEFT_PARENTHESIS)
1410: {
1411: /* Handle specials such as (*SKIP) or (*UTF8) etc. */
1412:
1.1.1.4 ! misho 1413: if (ptr[1] == CHAR_ASTERISK)
! 1414: {
! 1415: ptr += 2;
! 1416: while (ptr < cd->end_pattern && *ptr != CHAR_RIGHT_PARENTHESIS) ptr++;
! 1417: }
1.1 misho 1418:
1419: /* Handle a normal, unnamed capturing parenthesis. */
1420:
1421: else if (ptr[1] != CHAR_QUESTION_MARK)
1422: {
1423: *count += 1;
1424: if (name == NULL && *count == lorn) return *count;
1425: ptr++;
1426: }
1427:
1428: /* All cases now have (? at the start. Remember when we are in a group
1429: where the parenthesis numbers are duplicated. */
1430:
1431: else if (ptr[2] == CHAR_VERTICAL_LINE)
1432: {
1433: ptr += 3;
1434: dup_parens = TRUE;
1435: }
1436:
1437: /* Handle comments; all characters are allowed until a ket is reached. */
1438:
1439: else if (ptr[2] == CHAR_NUMBER_SIGN)
1440: {
1.1.1.4 ! misho 1441: for (ptr += 3; *ptr != CHAR_NULL; ptr++)
! 1442: if (*ptr == CHAR_RIGHT_PARENTHESIS) break;
1.1 misho 1443: goto FAIL_EXIT;
1444: }
1445:
1446: /* Handle a condition. If it is an assertion, just carry on so that it
1447: is processed as normal. If not, skip to the closing parenthesis of the
1448: condition (there can't be any nested parens). */
1449:
1450: else if (ptr[2] == CHAR_LEFT_PARENTHESIS)
1451: {
1452: ptr += 2;
1453: if (ptr[1] != CHAR_QUESTION_MARK)
1454: {
1.1.1.4 ! misho 1455: while (*ptr != CHAR_NULL && *ptr != CHAR_RIGHT_PARENTHESIS) ptr++;
! 1456: if (*ptr != CHAR_NULL) ptr++;
1.1 misho 1457: }
1458: }
1459:
1460: /* Start with (? but not a condition. */
1461:
1462: else
1463: {
1464: ptr += 2;
1465: if (*ptr == CHAR_P) ptr++; /* Allow optional P */
1466:
1467: /* We have to disambiguate (?<! and (?<= from (?<name> for named groups */
1468:
1469: if ((*ptr == CHAR_LESS_THAN_SIGN && ptr[1] != CHAR_EXCLAMATION_MARK &&
1470: ptr[1] != CHAR_EQUALS_SIGN) || *ptr == CHAR_APOSTROPHE)
1471: {
1.1.1.4 ! misho 1472: pcre_uchar term;
1.1.1.2 misho 1473: const pcre_uchar *thisname;
1.1 misho 1474: *count += 1;
1475: if (name == NULL && *count == lorn) return *count;
1476: term = *ptr++;
1477: if (term == CHAR_LESS_THAN_SIGN) term = CHAR_GREATER_THAN_SIGN;
1478: thisname = ptr;
1479: while (*ptr != term) ptr++;
1.1.1.4 ! misho 1480: if (name != NULL && lorn == (int)(ptr - thisname) &&
! 1481: STRNCMP_UC_UC(name, thisname, (unsigned int)lorn) == 0)
1.1 misho 1482: return *count;
1483: term++;
1484: }
1485: }
1486: }
1487:
1488: /* Past any initial parenthesis handling, scan for parentheses or vertical
1489: bars. Stop if we get to cd->end_pattern. Note that this is important for the
1490: first-pass call when this value is temporarily adjusted to stop at the current
1491: position. So DO NOT change this to a test for binary zero. */
1492:
1493: for (; ptr < cd->end_pattern; ptr++)
1494: {
1495: /* Skip over backslashed characters and also entire \Q...\E */
1496:
1497: if (*ptr == CHAR_BACKSLASH)
1498: {
1.1.1.4 ! misho 1499: if (*(++ptr) == CHAR_NULL) goto FAIL_EXIT;
1.1 misho 1500: if (*ptr == CHAR_Q) for (;;)
1501: {
1.1.1.4 ! misho 1502: while (*(++ptr) != CHAR_NULL && *ptr != CHAR_BACKSLASH) {};
! 1503: if (*ptr == CHAR_NULL) goto FAIL_EXIT;
1.1 misho 1504: if (*(++ptr) == CHAR_E) break;
1505: }
1506: continue;
1507: }
1508:
1509: /* Skip over character classes; this logic must be similar to the way they
1510: are handled for real. If the first character is '^', skip it. Also, if the
1511: first few characters (either before or after ^) are \Q\E or \E we skip them
1512: too. This makes for compatibility with Perl. Note the use of STR macros to
1513: encode "Q\\E" so that it works in UTF-8 on EBCDIC platforms. */
1514:
1515: if (*ptr == CHAR_LEFT_SQUARE_BRACKET)
1516: {
1517: BOOL negate_class = FALSE;
1518: for (;;)
1519: {
1520: if (ptr[1] == CHAR_BACKSLASH)
1521: {
1522: if (ptr[2] == CHAR_E)
1523: ptr+= 2;
1.1.1.2 misho 1524: else if (STRNCMP_UC_C8(ptr + 2,
1.1 misho 1525: STR_Q STR_BACKSLASH STR_E, 3) == 0)
1526: ptr += 4;
1527: else
1528: break;
1529: }
1530: else if (!negate_class && ptr[1] == CHAR_CIRCUMFLEX_ACCENT)
1531: {
1532: negate_class = TRUE;
1533: ptr++;
1534: }
1535: else break;
1536: }
1537:
1538: /* If the next character is ']', it is a data character that must be
1539: skipped, except in JavaScript compatibility mode. */
1540:
1541: if (ptr[1] == CHAR_RIGHT_SQUARE_BRACKET &&
1542: (cd->external_options & PCRE_JAVASCRIPT_COMPAT) == 0)
1543: ptr++;
1544:
1545: while (*(++ptr) != CHAR_RIGHT_SQUARE_BRACKET)
1546: {
1.1.1.4 ! misho 1547: if (*ptr == CHAR_NULL) return -1;
1.1 misho 1548: if (*ptr == CHAR_BACKSLASH)
1549: {
1.1.1.4 ! misho 1550: if (*(++ptr) == CHAR_NULL) goto FAIL_EXIT;
1.1 misho 1551: if (*ptr == CHAR_Q) for (;;)
1552: {
1.1.1.4 ! misho 1553: while (*(++ptr) != CHAR_NULL && *ptr != CHAR_BACKSLASH) {};
! 1554: if (*ptr == CHAR_NULL) goto FAIL_EXIT;
1.1 misho 1555: if (*(++ptr) == CHAR_E) break;
1556: }
1557: continue;
1558: }
1559: }
1560: continue;
1561: }
1562:
1563: /* Skip comments in /x mode */
1564:
1565: if (xmode && *ptr == CHAR_NUMBER_SIGN)
1566: {
1567: ptr++;
1.1.1.4 ! misho 1568: while (*ptr != CHAR_NULL)
1.1 misho 1569: {
1570: if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }
1571: ptr++;
1.1.1.2 misho 1572: #ifdef SUPPORT_UTF
1573: if (utf) FORWARDCHAR(ptr);
1.1 misho 1574: #endif
1575: }
1.1.1.4 ! misho 1576: if (*ptr == CHAR_NULL) goto FAIL_EXIT;
1.1 misho 1577: continue;
1578: }
1579:
1580: /* Check for the special metacharacters */
1581:
1582: if (*ptr == CHAR_LEFT_PARENTHESIS)
1583: {
1.1.1.2 misho 1584: int rc = find_parens_sub(&ptr, cd, name, lorn, xmode, utf, count);
1.1 misho 1585: if (rc > 0) return rc;
1.1.1.4 ! misho 1586: if (*ptr == CHAR_NULL) goto FAIL_EXIT;
1.1 misho 1587: }
1588:
1589: else if (*ptr == CHAR_RIGHT_PARENTHESIS)
1590: {
1591: if (dup_parens && *count < hwm_count) *count = hwm_count;
1592: goto FAIL_EXIT;
1593: }
1594:
1595: else if (*ptr == CHAR_VERTICAL_LINE && dup_parens)
1596: {
1597: if (*count > hwm_count) hwm_count = *count;
1598: *count = start_count;
1599: }
1600: }
1601:
1602: FAIL_EXIT:
1603: *ptrptr = ptr;
1604: return -1;
1605: }
1606:
1607:
1608:
1609:
1610: /*************************************************
1611: * Find forward referenced subpattern *
1612: *************************************************/
1613:
1614: /* This function scans along a pattern's text looking for capturing
1615: subpatterns, and counting them. If it finds a named pattern that matches the
1616: name it is given, it returns its number. Alternatively, if the name is NULL, it
1617: returns when it reaches a given numbered subpattern. This is used for forward
1618: references to subpatterns. We used to be able to start this scan from the
1619: current compiling point, using the current count value from cd->bracount, and
1620: do it all in a single loop, but the addition of the possibility of duplicate
1621: subpattern numbers means that we have to scan from the very start, in order to
1622: take account of such duplicates, and to use a recursive function to keep track
1623: of the different types of group.
1624:
1625: Arguments:
1626: cd compile background data
1627: name name to seek, or NULL if seeking a numbered subpattern
1628: lorn name length, or subpattern number if name is NULL
1629: xmode TRUE if we are in /x mode
1.1.1.4 ! misho 1630: utf TRUE if we are in UTF-8 / UTF-16 / UTF-32 mode
1.1 misho 1631:
1632: Returns: the number of the found subpattern, or -1 if not found
1633: */
1634:
1635: static int
1.1.1.2 misho 1636: find_parens(compile_data *cd, const pcre_uchar *name, int lorn, BOOL xmode,
1637: BOOL utf)
1.1 misho 1638: {
1.1.1.2 misho 1639: pcre_uchar *ptr = (pcre_uchar *)cd->start_pattern;
1.1 misho 1640: int count = 0;
1641: int rc;
1642:
1643: /* If the pattern does not start with an opening parenthesis, the first call
1644: to find_parens_sub() will scan right to the end (if necessary). However, if it
1645: does start with a parenthesis, find_parens_sub() will return when it hits the
1646: matching closing parens. That is why we have to have a loop. */
1647:
1648: for (;;)
1649: {
1.1.1.2 misho 1650: rc = find_parens_sub(&ptr, cd, name, lorn, xmode, utf, &count);
1.1.1.4 ! misho 1651: if (rc > 0 || *ptr++ == CHAR_NULL) break;
1.1 misho 1652: }
1653:
1654: return rc;
1655: }
1656:
1657:
1658:
1659:
1660: /*************************************************
1661: * Find first significant op code *
1662: *************************************************/
1663:
1664: /* This is called by several functions that scan a compiled expression looking
1665: for a fixed first character, or an anchoring op code etc. It skips over things
1666: that do not influence this. For some calls, it makes sense to skip negative
1667: forward and all backward assertions, and also the \b assertion; for others it
1668: does not.
1669:
1670: Arguments:
1671: code pointer to the start of the group
1672: skipassert TRUE if certain assertions are to be skipped
1673:
1674: Returns: pointer to the first significant opcode
1675: */
1676:
1.1.1.2 misho 1677: static const pcre_uchar*
1678: first_significant_code(const pcre_uchar *code, BOOL skipassert)
1.1 misho 1679: {
1680: for (;;)
1681: {
1682: switch ((int)*code)
1683: {
1684: case OP_ASSERT_NOT:
1685: case OP_ASSERTBACK:
1686: case OP_ASSERTBACK_NOT:
1687: if (!skipassert) return code;
1688: do code += GET(code, 1); while (*code == OP_ALT);
1.1.1.2 misho 1689: code += PRIV(OP_lengths)[*code];
1.1 misho 1690: break;
1691:
1692: case OP_WORD_BOUNDARY:
1693: case OP_NOT_WORD_BOUNDARY:
1694: if (!skipassert) return code;
1695: /* Fall through */
1696:
1697: case OP_CALLOUT:
1698: case OP_CREF:
1699: case OP_NCREF:
1700: case OP_RREF:
1701: case OP_NRREF:
1702: case OP_DEF:
1.1.1.2 misho 1703: code += PRIV(OP_lengths)[*code];
1.1 misho 1704: break;
1705:
1706: default:
1707: return code;
1708: }
1709: }
1710: /* Control never reaches here */
1711: }
1712:
1713:
1714:
1715:
1716: /*************************************************
1717: * Find the fixed length of a branch *
1718: *************************************************/
1719:
1720: /* Scan a branch and compute the fixed length of subject that will match it,
1721: if the length is fixed. This is needed for dealing with backward assertions.
1722: In UTF8 mode, the result is in characters rather than bytes. The branch is
1723: temporarily terminated with OP_END when this function is called.
1724:
1725: This function is called when a backward assertion is encountered, so that if it
1726: fails, the error message can point to the correct place in the pattern.
1727: However, we cannot do this when the assertion contains subroutine calls,
1728: because they can be forward references. We solve this by remembering this case
1729: and doing the check at the end; a flag specifies which mode we are running in.
1730:
1731: Arguments:
1732: code points to the start of the pattern (the bracket)
1.1.1.4 ! misho 1733: utf TRUE in UTF-8 / UTF-16 / UTF-32 mode
1.1 misho 1734: atend TRUE if called when the pattern is complete
1735: cd the "compile data" structure
1736:
1737: Returns: the fixed length,
1738: or -1 if there is no fixed length,
1739: or -2 if \C was encountered (in UTF-8 mode only)
1740: or -3 if an OP_RECURSE item was encountered and atend is FALSE
1741: or -4 if an unknown opcode was encountered (internal error)
1742: */
1743:
1744: static int
1.1.1.2 misho 1745: find_fixedlength(pcre_uchar *code, BOOL utf, BOOL atend, compile_data *cd)
1.1 misho 1746: {
1747: int length = -1;
1748:
1749: register int branchlength = 0;
1.1.1.2 misho 1750: register pcre_uchar *cc = code + 1 + LINK_SIZE;
1.1 misho 1751:
1752: /* Scan along the opcodes for this branch. If we get to the end of the
1753: branch, check the length against that of the other branches. */
1754:
1755: for (;;)
1756: {
1757: int d;
1.1.1.2 misho 1758: pcre_uchar *ce, *cs;
1.1.1.4 ! misho 1759: register pcre_uchar op = *cc;
1.1.1.2 misho 1760:
1.1 misho 1761: switch (op)
1762: {
1763: /* We only need to continue for OP_CBRA (normal capturing bracket) and
1764: OP_BRA (normal non-capturing bracket) because the other variants of these
1765: opcodes are all concerned with unlimited repeated groups, which of course
1766: are not of fixed length. */
1767:
1768: case OP_CBRA:
1769: case OP_BRA:
1770: case OP_ONCE:
1771: case OP_ONCE_NC:
1772: case OP_COND:
1.1.1.2 misho 1773: d = find_fixedlength(cc + ((op == OP_CBRA)? IMM2_SIZE : 0), utf, atend, cd);
1.1 misho 1774: if (d < 0) return d;
1775: branchlength += d;
1776: do cc += GET(cc, 1); while (*cc == OP_ALT);
1777: cc += 1 + LINK_SIZE;
1778: break;
1779:
1780: /* Reached end of a branch; if it's a ket it is the end of a nested call.
1781: If it's ALT it is an alternation in a nested call. An ACCEPT is effectively
1782: an ALT. If it is END it's the end of the outer call. All can be handled by
1783: the same code. Note that we must not include the OP_KETRxxx opcodes here,
1784: because they all imply an unlimited repeat. */
1785:
1786: case OP_ALT:
1787: case OP_KET:
1788: case OP_END:
1789: case OP_ACCEPT:
1790: case OP_ASSERT_ACCEPT:
1791: if (length < 0) length = branchlength;
1792: else if (length != branchlength) return -1;
1793: if (*cc != OP_ALT) return length;
1794: cc += 1 + LINK_SIZE;
1795: branchlength = 0;
1796: break;
1797:
1798: /* A true recursion implies not fixed length, but a subroutine call may
1799: be OK. If the subroutine is a forward reference, we can't deal with
1800: it until the end of the pattern, so return -3. */
1801:
1802: case OP_RECURSE:
1803: if (!atend) return -3;
1.1.1.2 misho 1804: cs = ce = (pcre_uchar *)cd->start_code + GET(cc, 1); /* Start subpattern */
1805: do ce += GET(ce, 1); while (*ce == OP_ALT); /* End subpattern */
1806: if (cc > cs && cc < ce) return -1; /* Recursion */
1807: d = find_fixedlength(cs + IMM2_SIZE, utf, atend, cd);
1.1 misho 1808: if (d < 0) return d;
1809: branchlength += d;
1810: cc += 1 + LINK_SIZE;
1811: break;
1812:
1813: /* Skip over assertive subpatterns */
1814:
1815: case OP_ASSERT:
1816: case OP_ASSERT_NOT:
1817: case OP_ASSERTBACK:
1818: case OP_ASSERTBACK_NOT:
1819: do cc += GET(cc, 1); while (*cc == OP_ALT);
1.1.1.2 misho 1820: cc += PRIV(OP_lengths)[*cc];
1821: break;
1.1 misho 1822:
1823: /* Skip over things that don't match chars */
1824:
1825: case OP_MARK:
1826: case OP_PRUNE_ARG:
1827: case OP_SKIP_ARG:
1828: case OP_THEN_ARG:
1.1.1.2 misho 1829: cc += cc[1] + PRIV(OP_lengths)[*cc];
1.1 misho 1830: break;
1831:
1832: case OP_CALLOUT:
1833: case OP_CIRC:
1834: case OP_CIRCM:
1835: case OP_CLOSE:
1836: case OP_COMMIT:
1837: case OP_CREF:
1838: case OP_DEF:
1839: case OP_DOLL:
1840: case OP_DOLLM:
1841: case OP_EOD:
1842: case OP_EODN:
1843: case OP_FAIL:
1844: case OP_NCREF:
1845: case OP_NRREF:
1846: case OP_NOT_WORD_BOUNDARY:
1847: case OP_PRUNE:
1848: case OP_REVERSE:
1849: case OP_RREF:
1850: case OP_SET_SOM:
1851: case OP_SKIP:
1852: case OP_SOD:
1853: case OP_SOM:
1854: case OP_THEN:
1855: case OP_WORD_BOUNDARY:
1.1.1.2 misho 1856: cc += PRIV(OP_lengths)[*cc];
1.1 misho 1857: break;
1858:
1859: /* Handle literal characters */
1860:
1861: case OP_CHAR:
1862: case OP_CHARI:
1863: case OP_NOT:
1864: case OP_NOTI:
1865: branchlength++;
1866: cc += 2;
1.1.1.2 misho 1867: #ifdef SUPPORT_UTF
1868: if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
1.1 misho 1869: #endif
1870: break;
1871:
1872: /* Handle exact repetitions. The count is already in characters, but we
1873: need to skip over a multibyte character in UTF8 mode. */
1874:
1875: case OP_EXACT:
1876: case OP_EXACTI:
1877: case OP_NOTEXACT:
1878: case OP_NOTEXACTI:
1.1.1.4 ! misho 1879: branchlength += (int)GET2(cc,1);
1.1.1.2 misho 1880: cc += 2 + IMM2_SIZE;
1881: #ifdef SUPPORT_UTF
1882: if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
1.1 misho 1883: #endif
1884: break;
1885:
1886: case OP_TYPEEXACT:
1887: branchlength += GET2(cc,1);
1.1.1.4 ! misho 1888: if (cc[1 + IMM2_SIZE] == OP_PROP || cc[1 + IMM2_SIZE] == OP_NOTPROP)
! 1889: cc += 2;
1.1.1.2 misho 1890: cc += 1 + IMM2_SIZE + 1;
1.1 misho 1891: break;
1892:
1893: /* Handle single-char matchers */
1894:
1895: case OP_PROP:
1896: case OP_NOTPROP:
1897: cc += 2;
1898: /* Fall through */
1899:
1900: case OP_HSPACE:
1901: case OP_VSPACE:
1902: case OP_NOT_HSPACE:
1903: case OP_NOT_VSPACE:
1904: case OP_NOT_DIGIT:
1905: case OP_DIGIT:
1906: case OP_NOT_WHITESPACE:
1907: case OP_WHITESPACE:
1908: case OP_NOT_WORDCHAR:
1909: case OP_WORDCHAR:
1910: case OP_ANY:
1911: case OP_ALLANY:
1912: branchlength++;
1913: cc++;
1914: break;
1915:
1916: /* The single-byte matcher isn't allowed. This only happens in UTF-8 mode;
1917: otherwise \C is coded as OP_ALLANY. */
1918:
1919: case OP_ANYBYTE:
1920: return -2;
1921:
1922: /* Check a class for variable quantification */
1923:
1924: case OP_CLASS:
1925: case OP_NCLASS:
1.1.1.4 ! misho 1926: #if defined SUPPORT_UTF || defined COMPILE_PCRE16 || defined COMPILE_PCRE32
! 1927: case OP_XCLASS:
! 1928: /* The original code caused an unsigned overflow in 64 bit systems,
! 1929: so now we use a conditional statement. */
! 1930: if (op == OP_XCLASS)
! 1931: cc += GET(cc, 1);
! 1932: else
! 1933: cc += PRIV(OP_lengths)[OP_CLASS];
! 1934: #else
1.1.1.2 misho 1935: cc += PRIV(OP_lengths)[OP_CLASS];
1.1.1.4 ! misho 1936: #endif
1.1 misho 1937:
1938: switch (*cc)
1939: {
1940: case OP_CRPLUS:
1941: case OP_CRMINPLUS:
1942: case OP_CRSTAR:
1943: case OP_CRMINSTAR:
1944: case OP_CRQUERY:
1945: case OP_CRMINQUERY:
1946: return -1;
1947:
1948: case OP_CRRANGE:
1949: case OP_CRMINRANGE:
1.1.1.2 misho 1950: if (GET2(cc,1) != GET2(cc,1+IMM2_SIZE)) return -1;
1.1.1.4 ! misho 1951: branchlength += (int)GET2(cc,1);
1.1.1.2 misho 1952: cc += 1 + 2 * IMM2_SIZE;
1.1 misho 1953: break;
1954:
1955: default:
1956: branchlength++;
1957: }
1958: break;
1959:
1960: /* Anything else is variable length */
1961:
1962: case OP_ANYNL:
1963: case OP_BRAMINZERO:
1964: case OP_BRAPOS:
1965: case OP_BRAPOSZERO:
1966: case OP_BRAZERO:
1967: case OP_CBRAPOS:
1968: case OP_EXTUNI:
1969: case OP_KETRMAX:
1970: case OP_KETRMIN:
1971: case OP_KETRPOS:
1972: case OP_MINPLUS:
1973: case OP_MINPLUSI:
1974: case OP_MINQUERY:
1975: case OP_MINQUERYI:
1976: case OP_MINSTAR:
1977: case OP_MINSTARI:
1978: case OP_MINUPTO:
1979: case OP_MINUPTOI:
1980: case OP_NOTMINPLUS:
1981: case OP_NOTMINPLUSI:
1982: case OP_NOTMINQUERY:
1983: case OP_NOTMINQUERYI:
1984: case OP_NOTMINSTAR:
1985: case OP_NOTMINSTARI:
1986: case OP_NOTMINUPTO:
1987: case OP_NOTMINUPTOI:
1988: case OP_NOTPLUS:
1989: case OP_NOTPLUSI:
1990: case OP_NOTPOSPLUS:
1991: case OP_NOTPOSPLUSI:
1992: case OP_NOTPOSQUERY:
1993: case OP_NOTPOSQUERYI:
1994: case OP_NOTPOSSTAR:
1995: case OP_NOTPOSSTARI:
1996: case OP_NOTPOSUPTO:
1997: case OP_NOTPOSUPTOI:
1998: case OP_NOTQUERY:
1999: case OP_NOTQUERYI:
2000: case OP_NOTSTAR:
2001: case OP_NOTSTARI:
2002: case OP_NOTUPTO:
2003: case OP_NOTUPTOI:
2004: case OP_PLUS:
2005: case OP_PLUSI:
2006: case OP_POSPLUS:
2007: case OP_POSPLUSI:
2008: case OP_POSQUERY:
2009: case OP_POSQUERYI:
2010: case OP_POSSTAR:
2011: case OP_POSSTARI:
2012: case OP_POSUPTO:
2013: case OP_POSUPTOI:
2014: case OP_QUERY:
2015: case OP_QUERYI:
2016: case OP_REF:
2017: case OP_REFI:
2018: case OP_SBRA:
2019: case OP_SBRAPOS:
2020: case OP_SCBRA:
2021: case OP_SCBRAPOS:
2022: case OP_SCOND:
2023: case OP_SKIPZERO:
2024: case OP_STAR:
2025: case OP_STARI:
2026: case OP_TYPEMINPLUS:
2027: case OP_TYPEMINQUERY:
2028: case OP_TYPEMINSTAR:
2029: case OP_TYPEMINUPTO:
2030: case OP_TYPEPLUS:
2031: case OP_TYPEPOSPLUS:
2032: case OP_TYPEPOSQUERY:
2033: case OP_TYPEPOSSTAR:
2034: case OP_TYPEPOSUPTO:
2035: case OP_TYPEQUERY:
2036: case OP_TYPESTAR:
2037: case OP_TYPEUPTO:
2038: case OP_UPTO:
2039: case OP_UPTOI:
2040: return -1;
2041:
2042: /* Catch unrecognized opcodes so that when new ones are added they
2043: are not forgotten, as has happened in the past. */
2044:
2045: default:
2046: return -4;
2047: }
2048: }
2049: /* Control never gets here */
2050: }
2051:
2052:
2053:
2054:
2055: /*************************************************
2056: * Scan compiled regex for specific bracket *
2057: *************************************************/
2058:
2059: /* This little function scans through a compiled pattern until it finds a
2060: capturing bracket with the given number, or, if the number is negative, an
2061: instance of OP_REVERSE for a lookbehind. The function is global in the C sense
2062: so that it can be called from pcre_study() when finding the minimum matching
2063: length.
2064:
2065: Arguments:
2066: code points to start of expression
1.1.1.4 ! misho 2067: utf TRUE in UTF-8 / UTF-16 / UTF-32 mode
1.1 misho 2068: number the required bracket number or negative to find a lookbehind
2069:
2070: Returns: pointer to the opcode for the bracket, or NULL if not found
2071: */
2072:
1.1.1.2 misho 2073: const pcre_uchar *
2074: PRIV(find_bracket)(const pcre_uchar *code, BOOL utf, int number)
1.1 misho 2075: {
2076: for (;;)
2077: {
1.1.1.4 ! misho 2078: register pcre_uchar c = *code;
1.1 misho 2079:
2080: if (c == OP_END) return NULL;
2081:
2082: /* XCLASS is used for classes that cannot be represented just by a bit
2083: map. This includes negated single high-valued characters. The length in
2084: the table is zero; the actual length is stored in the compiled code. */
2085:
2086: if (c == OP_XCLASS) code += GET(code, 1);
2087:
2088: /* Handle recursion */
2089:
2090: else if (c == OP_REVERSE)
2091: {
1.1.1.2 misho 2092: if (number < 0) return (pcre_uchar *)code;
2093: code += PRIV(OP_lengths)[c];
1.1 misho 2094: }
2095:
2096: /* Handle capturing bracket */
2097:
2098: else if (c == OP_CBRA || c == OP_SCBRA ||
2099: c == OP_CBRAPOS || c == OP_SCBRAPOS)
2100: {
1.1.1.4 ! misho 2101: int n = (int)GET2(code, 1+LINK_SIZE);
1.1.1.2 misho 2102: if (n == number) return (pcre_uchar *)code;
2103: code += PRIV(OP_lengths)[c];
1.1 misho 2104: }
2105:
2106: /* Otherwise, we can get the item's length from the table, except that for
2107: repeated character types, we have to test for \p and \P, which have an extra
2108: two bytes of parameters, and for MARK/PRUNE/SKIP/THEN with an argument, we
2109: must add in its length. */
2110:
2111: else
2112: {
2113: switch(c)
2114: {
2115: case OP_TYPESTAR:
2116: case OP_TYPEMINSTAR:
2117: case OP_TYPEPLUS:
2118: case OP_TYPEMINPLUS:
2119: case OP_TYPEQUERY:
2120: case OP_TYPEMINQUERY:
2121: case OP_TYPEPOSSTAR:
2122: case OP_TYPEPOSPLUS:
2123: case OP_TYPEPOSQUERY:
2124: if (code[1] == OP_PROP || code[1] == OP_NOTPROP) code += 2;
2125: break;
2126:
2127: case OP_TYPEUPTO:
2128: case OP_TYPEMINUPTO:
2129: case OP_TYPEEXACT:
2130: case OP_TYPEPOSUPTO:
1.1.1.4 ! misho 2131: if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
! 2132: code += 2;
1.1 misho 2133: break;
2134:
2135: case OP_MARK:
2136: case OP_PRUNE_ARG:
2137: case OP_SKIP_ARG:
2138: case OP_THEN_ARG:
2139: code += code[1];
2140: break;
2141: }
2142:
2143: /* Add in the fixed length from the table */
2144:
1.1.1.2 misho 2145: code += PRIV(OP_lengths)[c];
1.1 misho 2146:
2147: /* In UTF-8 mode, opcodes that are followed by a character may be followed by
2148: a multi-byte character. The length in the table is a minimum, so we have to
2149: arrange to skip the extra bytes. */
2150:
1.1.1.4 ! misho 2151: #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
1.1.1.2 misho 2152: if (utf) switch(c)
1.1 misho 2153: {
2154: case OP_CHAR:
2155: case OP_CHARI:
2156: case OP_EXACT:
2157: case OP_EXACTI:
2158: case OP_UPTO:
2159: case OP_UPTOI:
2160: case OP_MINUPTO:
2161: case OP_MINUPTOI:
2162: case OP_POSUPTO:
2163: case OP_POSUPTOI:
2164: case OP_STAR:
2165: case OP_STARI:
2166: case OP_MINSTAR:
2167: case OP_MINSTARI:
2168: case OP_POSSTAR:
2169: case OP_POSSTARI:
2170: case OP_PLUS:
2171: case OP_PLUSI:
2172: case OP_MINPLUS:
2173: case OP_MINPLUSI:
2174: case OP_POSPLUS:
2175: case OP_POSPLUSI:
2176: case OP_QUERY:
2177: case OP_QUERYI:
2178: case OP_MINQUERY:
2179: case OP_MINQUERYI:
2180: case OP_POSQUERY:
2181: case OP_POSQUERYI:
1.1.1.2 misho 2182: if (HAS_EXTRALEN(code[-1])) code += GET_EXTRALEN(code[-1]);
1.1 misho 2183: break;
2184: }
2185: #else
1.1.1.2 misho 2186: (void)(utf); /* Keep compiler happy by referencing function argument */
1.1 misho 2187: #endif
2188: }
2189: }
2190: }
2191:
2192:
2193:
2194: /*************************************************
2195: * Scan compiled regex for recursion reference *
2196: *************************************************/
2197:
2198: /* This little function scans through a compiled pattern until it finds an
2199: instance of OP_RECURSE.
2200:
2201: Arguments:
2202: code points to start of expression
1.1.1.4 ! misho 2203: utf TRUE in UTF-8 / UTF-16 / UTF-32 mode
1.1 misho 2204:
2205: Returns: pointer to the opcode for OP_RECURSE, or NULL if not found
2206: */
2207:
1.1.1.2 misho 2208: static const pcre_uchar *
2209: find_recurse(const pcre_uchar *code, BOOL utf)
1.1 misho 2210: {
2211: for (;;)
2212: {
1.1.1.4 ! misho 2213: register pcre_uchar c = *code;
1.1 misho 2214: if (c == OP_END) return NULL;
2215: if (c == OP_RECURSE) return code;
2216:
2217: /* XCLASS is used for classes that cannot be represented just by a bit
2218: map. This includes negated single high-valued characters. The length in
2219: the table is zero; the actual length is stored in the compiled code. */
2220:
2221: if (c == OP_XCLASS) code += GET(code, 1);
2222:
2223: /* Otherwise, we can get the item's length from the table, except that for
2224: repeated character types, we have to test for \p and \P, which have an extra
2225: two bytes of parameters, and for MARK/PRUNE/SKIP/THEN with an argument, we
2226: must add in its length. */
2227:
2228: else
2229: {
2230: switch(c)
2231: {
2232: case OP_TYPESTAR:
2233: case OP_TYPEMINSTAR:
2234: case OP_TYPEPLUS:
2235: case OP_TYPEMINPLUS:
2236: case OP_TYPEQUERY:
2237: case OP_TYPEMINQUERY:
2238: case OP_TYPEPOSSTAR:
2239: case OP_TYPEPOSPLUS:
2240: case OP_TYPEPOSQUERY:
2241: if (code[1] == OP_PROP || code[1] == OP_NOTPROP) code += 2;
2242: break;
2243:
2244: case OP_TYPEPOSUPTO:
2245: case OP_TYPEUPTO:
2246: case OP_TYPEMINUPTO:
2247: case OP_TYPEEXACT:
1.1.1.4 ! misho 2248: if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
! 2249: code += 2;
1.1 misho 2250: break;
2251:
2252: case OP_MARK:
2253: case OP_PRUNE_ARG:
2254: case OP_SKIP_ARG:
2255: case OP_THEN_ARG:
2256: code += code[1];
2257: break;
2258: }
2259:
2260: /* Add in the fixed length from the table */
2261:
1.1.1.2 misho 2262: code += PRIV(OP_lengths)[c];
1.1 misho 2263:
2264: /* In UTF-8 mode, opcodes that are followed by a character may be followed
2265: by a multi-byte character. The length in the table is a minimum, so we have
2266: to arrange to skip the extra bytes. */
2267:
1.1.1.4 ! misho 2268: #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
1.1.1.2 misho 2269: if (utf) switch(c)
1.1 misho 2270: {
2271: case OP_CHAR:
2272: case OP_CHARI:
1.1.1.3 misho 2273: case OP_NOT:
2274: case OP_NOTI:
1.1 misho 2275: case OP_EXACT:
2276: case OP_EXACTI:
1.1.1.3 misho 2277: case OP_NOTEXACT:
2278: case OP_NOTEXACTI:
1.1 misho 2279: case OP_UPTO:
2280: case OP_UPTOI:
1.1.1.3 misho 2281: case OP_NOTUPTO:
2282: case OP_NOTUPTOI:
1.1 misho 2283: case OP_MINUPTO:
2284: case OP_MINUPTOI:
1.1.1.3 misho 2285: case OP_NOTMINUPTO:
2286: case OP_NOTMINUPTOI:
1.1 misho 2287: case OP_POSUPTO:
2288: case OP_POSUPTOI:
1.1.1.3 misho 2289: case OP_NOTPOSUPTO:
2290: case OP_NOTPOSUPTOI:
1.1 misho 2291: case OP_STAR:
2292: case OP_STARI:
1.1.1.3 misho 2293: case OP_NOTSTAR:
2294: case OP_NOTSTARI:
1.1 misho 2295: case OP_MINSTAR:
2296: case OP_MINSTARI:
1.1.1.3 misho 2297: case OP_NOTMINSTAR:
2298: case OP_NOTMINSTARI:
1.1 misho 2299: case OP_POSSTAR:
2300: case OP_POSSTARI:
1.1.1.3 misho 2301: case OP_NOTPOSSTAR:
2302: case OP_NOTPOSSTARI:
1.1 misho 2303: case OP_PLUS:
2304: case OP_PLUSI:
1.1.1.3 misho 2305: case OP_NOTPLUS:
2306: case OP_NOTPLUSI:
1.1 misho 2307: case OP_MINPLUS:
2308: case OP_MINPLUSI:
1.1.1.3 misho 2309: case OP_NOTMINPLUS:
2310: case OP_NOTMINPLUSI:
1.1 misho 2311: case OP_POSPLUS:
2312: case OP_POSPLUSI:
1.1.1.3 misho 2313: case OP_NOTPOSPLUS:
2314: case OP_NOTPOSPLUSI:
1.1 misho 2315: case OP_QUERY:
2316: case OP_QUERYI:
1.1.1.3 misho 2317: case OP_NOTQUERY:
2318: case OP_NOTQUERYI:
1.1 misho 2319: case OP_MINQUERY:
2320: case OP_MINQUERYI:
1.1.1.3 misho 2321: case OP_NOTMINQUERY:
2322: case OP_NOTMINQUERYI:
1.1 misho 2323: case OP_POSQUERY:
2324: case OP_POSQUERYI:
1.1.1.3 misho 2325: case OP_NOTPOSQUERY:
2326: case OP_NOTPOSQUERYI:
1.1.1.2 misho 2327: if (HAS_EXTRALEN(code[-1])) code += GET_EXTRALEN(code[-1]);
1.1 misho 2328: break;
2329: }
2330: #else
1.1.1.2 misho 2331: (void)(utf); /* Keep compiler happy by referencing function argument */
1.1 misho 2332: #endif
2333: }
2334: }
2335: }
2336:
2337:
2338:
2339: /*************************************************
2340: * Scan compiled branch for non-emptiness *
2341: *************************************************/
2342:
2343: /* This function scans through a branch of a compiled pattern to see whether it
2344: can match the empty string or not. It is called from could_be_empty()
2345: below and from compile_branch() when checking for an unlimited repeat of a
2346: group that can match nothing. Note that first_significant_code() skips over
2347: backward and negative forward assertions when its final argument is TRUE. If we
2348: hit an unclosed bracket, we return "empty" - this means we've struck an inner
2349: bracket whose current branch will already have been scanned.
2350:
2351: Arguments:
2352: code points to start of search
2353: endcode points to where to stop
1.1.1.4 ! misho 2354: utf TRUE if in UTF-8 / UTF-16 / UTF-32 mode
1.1 misho 2355: cd contains pointers to tables etc.
2356:
2357: Returns: TRUE if what is matched could be empty
2358: */
2359:
2360: static BOOL
1.1.1.2 misho 2361: could_be_empty_branch(const pcre_uchar *code, const pcre_uchar *endcode,
2362: BOOL utf, compile_data *cd)
1.1 misho 2363: {
1.1.1.4 ! misho 2364: register pcre_uchar c;
1.1.1.2 misho 2365: for (code = first_significant_code(code + PRIV(OP_lengths)[*code], TRUE);
1.1 misho 2366: code < endcode;
1.1.1.2 misho 2367: code = first_significant_code(code + PRIV(OP_lengths)[c], TRUE))
1.1 misho 2368: {
1.1.1.2 misho 2369: const pcre_uchar *ccode;
1.1 misho 2370:
2371: c = *code;
2372:
2373: /* Skip over forward assertions; the other assertions are skipped by
2374: first_significant_code() with a TRUE final argument. */
2375:
2376: if (c == OP_ASSERT)
2377: {
2378: do code += GET(code, 1); while (*code == OP_ALT);
2379: c = *code;
2380: continue;
2381: }
2382:
2383: /* For a recursion/subroutine call, if its end has been reached, which
2384: implies a backward reference subroutine call, we can scan it. If it's a
2385: forward reference subroutine call, we can't. To detect forward reference
2386: we have to scan up the list that is kept in the workspace. This function is
2387: called only when doing the real compile, not during the pre-compile that
2388: measures the size of the compiled pattern. */
2389:
2390: if (c == OP_RECURSE)
2391: {
1.1.1.2 misho 2392: const pcre_uchar *scode;
1.1 misho 2393: BOOL empty_branch;
2394:
2395: /* Test for forward reference */
2396:
2397: for (scode = cd->start_workspace; scode < cd->hwm; scode += LINK_SIZE)
1.1.1.4 ! misho 2398: if ((int)GET(scode, 0) == (int)(code + 1 - cd->start_code)) return TRUE;
1.1 misho 2399:
2400: /* Not a forward reference, test for completed backward reference */
2401:
2402: empty_branch = FALSE;
2403: scode = cd->start_code + GET(code, 1);
2404: if (GET(scode, 1) == 0) return TRUE; /* Unclosed */
2405:
2406: /* Completed backwards reference */
2407:
2408: do
2409: {
1.1.1.2 misho 2410: if (could_be_empty_branch(scode, endcode, utf, cd))
1.1 misho 2411: {
2412: empty_branch = TRUE;
2413: break;
2414: }
2415: scode += GET(scode, 1);
2416: }
2417: while (*scode == OP_ALT);
2418:
2419: if (!empty_branch) return FALSE; /* All branches are non-empty */
2420: continue;
2421: }
2422:
2423: /* Groups with zero repeats can of course be empty; skip them. */
2424:
2425: if (c == OP_BRAZERO || c == OP_BRAMINZERO || c == OP_SKIPZERO ||
2426: c == OP_BRAPOSZERO)
2427: {
1.1.1.2 misho 2428: code += PRIV(OP_lengths)[c];
1.1 misho 2429: do code += GET(code, 1); while (*code == OP_ALT);
2430: c = *code;
2431: continue;
2432: }
2433:
2434: /* A nested group that is already marked as "could be empty" can just be
2435: skipped. */
2436:
2437: if (c == OP_SBRA || c == OP_SBRAPOS ||
2438: c == OP_SCBRA || c == OP_SCBRAPOS)
2439: {
2440: do code += GET(code, 1); while (*code == OP_ALT);
2441: c = *code;
2442: continue;
2443: }
2444:
2445: /* For other groups, scan the branches. */
2446:
2447: if (c == OP_BRA || c == OP_BRAPOS ||
2448: c == OP_CBRA || c == OP_CBRAPOS ||
2449: c == OP_ONCE || c == OP_ONCE_NC ||
2450: c == OP_COND)
2451: {
2452: BOOL empty_branch;
2453: if (GET(code, 1) == 0) return TRUE; /* Hit unclosed bracket */
2454:
2455: /* If a conditional group has only one branch, there is a second, implied,
2456: empty branch, so just skip over the conditional, because it could be empty.
2457: Otherwise, scan the individual branches of the group. */
2458:
2459: if (c == OP_COND && code[GET(code, 1)] != OP_ALT)
2460: code += GET(code, 1);
2461: else
2462: {
2463: empty_branch = FALSE;
2464: do
2465: {
1.1.1.2 misho 2466: if (!empty_branch && could_be_empty_branch(code, endcode, utf, cd))
1.1 misho 2467: empty_branch = TRUE;
2468: code += GET(code, 1);
2469: }
2470: while (*code == OP_ALT);
2471: if (!empty_branch) return FALSE; /* All branches are non-empty */
2472: }
2473:
2474: c = *code;
2475: continue;
2476: }
2477:
2478: /* Handle the other opcodes */
2479:
2480: switch (c)
2481: {
2482: /* Check for quantifiers after a class. XCLASS is used for classes that
2483: cannot be represented just by a bit map. This includes negated single
1.1.1.2 misho 2484: high-valued characters. The length in PRIV(OP_lengths)[] is zero; the
1.1 misho 2485: actual length is stored in the compiled code, so we must update "code"
2486: here. */
2487:
1.1.1.2 misho 2488: #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
1.1 misho 2489: case OP_XCLASS:
2490: ccode = code += GET(code, 1);
2491: goto CHECK_CLASS_REPEAT;
2492: #endif
2493:
2494: case OP_CLASS:
2495: case OP_NCLASS:
1.1.1.2 misho 2496: ccode = code + PRIV(OP_lengths)[OP_CLASS];
1.1 misho 2497:
1.1.1.2 misho 2498: #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
1.1 misho 2499: CHECK_CLASS_REPEAT:
2500: #endif
2501:
2502: switch (*ccode)
2503: {
2504: case OP_CRSTAR: /* These could be empty; continue */
2505: case OP_CRMINSTAR:
2506: case OP_CRQUERY:
2507: case OP_CRMINQUERY:
2508: break;
2509:
2510: default: /* Non-repeat => class must match */
2511: case OP_CRPLUS: /* These repeats aren't empty */
2512: case OP_CRMINPLUS:
2513: return FALSE;
2514:
2515: case OP_CRRANGE:
2516: case OP_CRMINRANGE:
2517: if (GET2(ccode, 1) > 0) return FALSE; /* Minimum > 0 */
2518: break;
2519: }
2520: break;
2521:
2522: /* Opcodes that must match a character */
2523:
2524: case OP_PROP:
2525: case OP_NOTPROP:
2526: case OP_EXTUNI:
2527: case OP_NOT_DIGIT:
2528: case OP_DIGIT:
2529: case OP_NOT_WHITESPACE:
2530: case OP_WHITESPACE:
2531: case OP_NOT_WORDCHAR:
2532: case OP_WORDCHAR:
2533: case OP_ANY:
2534: case OP_ALLANY:
2535: case OP_ANYBYTE:
2536: case OP_CHAR:
2537: case OP_CHARI:
2538: case OP_NOT:
2539: case OP_NOTI:
2540: case OP_PLUS:
2541: case OP_MINPLUS:
2542: case OP_POSPLUS:
2543: case OP_EXACT:
2544: case OP_NOTPLUS:
2545: case OP_NOTMINPLUS:
2546: case OP_NOTPOSPLUS:
2547: case OP_NOTEXACT:
2548: case OP_TYPEPLUS:
2549: case OP_TYPEMINPLUS:
2550: case OP_TYPEPOSPLUS:
2551: case OP_TYPEEXACT:
2552: return FALSE;
2553:
2554: /* These are going to continue, as they may be empty, but we have to
2555: fudge the length for the \p and \P cases. */
2556:
2557: case OP_TYPESTAR:
2558: case OP_TYPEMINSTAR:
2559: case OP_TYPEPOSSTAR:
2560: case OP_TYPEQUERY:
2561: case OP_TYPEMINQUERY:
2562: case OP_TYPEPOSQUERY:
2563: if (code[1] == OP_PROP || code[1] == OP_NOTPROP) code += 2;
2564: break;
2565:
2566: /* Same for these */
2567:
2568: case OP_TYPEUPTO:
2569: case OP_TYPEMINUPTO:
2570: case OP_TYPEPOSUPTO:
1.1.1.4 ! misho 2571: if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
! 2572: code += 2;
1.1 misho 2573: break;
2574:
2575: /* End of branch */
2576:
2577: case OP_KET:
2578: case OP_KETRMAX:
2579: case OP_KETRMIN:
2580: case OP_KETRPOS:
2581: case OP_ALT:
2582: return TRUE;
2583:
2584: /* In UTF-8 mode, STAR, MINSTAR, POSSTAR, QUERY, MINQUERY, POSQUERY, UPTO,
2585: MINUPTO, and POSUPTO may be followed by a multibyte character */
2586:
1.1.1.4 ! misho 2587: #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
1.1 misho 2588: case OP_STAR:
2589: case OP_STARI:
2590: case OP_MINSTAR:
2591: case OP_MINSTARI:
2592: case OP_POSSTAR:
2593: case OP_POSSTARI:
2594: case OP_QUERY:
2595: case OP_QUERYI:
2596: case OP_MINQUERY:
2597: case OP_MINQUERYI:
2598: case OP_POSQUERY:
2599: case OP_POSQUERYI:
1.1.1.2 misho 2600: if (utf && HAS_EXTRALEN(code[1])) code += GET_EXTRALEN(code[1]);
1.1 misho 2601: break;
2602:
2603: case OP_UPTO:
2604: case OP_UPTOI:
2605: case OP_MINUPTO:
2606: case OP_MINUPTOI:
2607: case OP_POSUPTO:
2608: case OP_POSUPTOI:
1.1.1.2 misho 2609: if (utf && HAS_EXTRALEN(code[1 + IMM2_SIZE])) code += GET_EXTRALEN(code[1 + IMM2_SIZE]);
1.1 misho 2610: break;
2611: #endif
2612:
2613: /* MARK, and PRUNE/SKIP/THEN with an argument must skip over the argument
2614: string. */
2615:
2616: case OP_MARK:
2617: case OP_PRUNE_ARG:
2618: case OP_SKIP_ARG:
2619: case OP_THEN_ARG:
2620: code += code[1];
2621: break;
2622:
2623: /* None of the remaining opcodes are required to match a character. */
2624:
2625: default:
2626: break;
2627: }
2628: }
2629:
2630: return TRUE;
2631: }
2632:
2633:
2634:
2635: /*************************************************
2636: * Scan compiled regex for non-emptiness *
2637: *************************************************/
2638:
2639: /* This function is called to check for left recursive calls. We want to check
2640: the current branch of the current pattern to see if it could match the empty
2641: string. If it could, we must look outwards for branches at other levels,
2642: stopping when we pass beyond the bracket which is the subject of the recursion.
2643: This function is called only during the real compile, not during the
2644: pre-compile.
2645:
2646: Arguments:
2647: code points to start of the recursion
2648: endcode points to where to stop (current RECURSE item)
2649: bcptr points to the chain of current (unclosed) branch starts
1.1.1.4 ! misho 2650: utf TRUE if in UTF-8 / UTF-16 / UTF-32 mode
1.1 misho 2651: cd pointers to tables etc
2652:
2653: Returns: TRUE if what is matched could be empty
2654: */
2655:
2656: static BOOL
1.1.1.2 misho 2657: could_be_empty(const pcre_uchar *code, const pcre_uchar *endcode,
2658: branch_chain *bcptr, BOOL utf, compile_data *cd)
1.1 misho 2659: {
2660: while (bcptr != NULL && bcptr->current_branch >= code)
2661: {
1.1.1.2 misho 2662: if (!could_be_empty_branch(bcptr->current_branch, endcode, utf, cd))
1.1 misho 2663: return FALSE;
2664: bcptr = bcptr->outer;
2665: }
2666: return TRUE;
2667: }
2668:
2669:
2670:
2671: /*************************************************
2672: * Check for POSIX class syntax *
2673: *************************************************/
2674:
2675: /* This function is called when the sequence "[:" or "[." or "[=" is
2676: encountered in a character class. It checks whether this is followed by a
2677: sequence of characters terminated by a matching ":]" or ".]" or "=]". If we
2678: reach an unescaped ']' without the special preceding character, return FALSE.
2679:
2680: Originally, this function only recognized a sequence of letters between the
2681: terminators, but it seems that Perl recognizes any sequence of characters,
2682: though of course unknown POSIX names are subsequently rejected. Perl gives an
2683: "Unknown POSIX class" error for [:f\oo:] for example, where previously PCRE
2684: didn't consider this to be a POSIX class. Likewise for [:1234:].
2685:
2686: The problem in trying to be exactly like Perl is in the handling of escapes. We
2687: have to be sure that [abc[:x\]pqr] is *not* treated as containing a POSIX
2688: class, but [abc[:x\]pqr:]] is (so that an error can be generated). The code
2689: below handles the special case of \], but does not try to do any other escape
2690: processing. This makes it different from Perl for cases such as [:l\ower:]
2691: where Perl recognizes it as the POSIX class "lower" but PCRE does not recognize
2692: "l\ower". This is a lesser evil that not diagnosing bad classes when Perl does,
2693: I think.
2694:
2695: A user pointed out that PCRE was rejecting [:a[:digit:]] whereas Perl was not.
2696: It seems that the appearance of a nested POSIX class supersedes an apparent
2697: external class. For example, [:a[:digit:]b:] matches "a", "b", ":", or
2698: a digit.
2699:
2700: In Perl, unescaped square brackets may also appear as part of class names. For
2701: example, [:a[:abc]b:] gives unknown POSIX class "[:abc]b:]". However, for
2702: [:a[:abc]b][b:] it gives unknown POSIX class "[:abc]b][b:]", which does not
2703: seem right at all. PCRE does not allow closing square brackets in POSIX class
2704: names.
2705:
2706: Arguments:
2707: ptr pointer to the initial [
2708: endptr where to return the end pointer
2709:
2710: Returns: TRUE or FALSE
2711: */
2712:
2713: static BOOL
1.1.1.2 misho 2714: check_posix_syntax(const pcre_uchar *ptr, const pcre_uchar **endptr)
1.1 misho 2715: {
1.1.1.4 ! misho 2716: pcre_uchar terminator; /* Don't combine these lines; the Solaris cc */
1.1 misho 2717: terminator = *(++ptr); /* compiler warns about "non-constant" initializer. */
1.1.1.4 ! misho 2718: for (++ptr; *ptr != CHAR_NULL; ptr++)
1.1 misho 2719: {
2720: if (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
2721: ptr++;
2722: else if (*ptr == CHAR_RIGHT_SQUARE_BRACKET) return FALSE;
2723: else
2724: {
2725: if (*ptr == terminator && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
2726: {
2727: *endptr = ptr;
2728: return TRUE;
2729: }
2730: if (*ptr == CHAR_LEFT_SQUARE_BRACKET &&
2731: (ptr[1] == CHAR_COLON || ptr[1] == CHAR_DOT ||
2732: ptr[1] == CHAR_EQUALS_SIGN) &&
2733: check_posix_syntax(ptr, endptr))
2734: return FALSE;
2735: }
2736: }
2737: return FALSE;
2738: }
2739:
2740:
2741:
2742:
2743: /*************************************************
2744: * Check POSIX class name *
2745: *************************************************/
2746:
2747: /* This function is called to check the name given in a POSIX-style class entry
2748: such as [:alnum:].
2749:
2750: Arguments:
2751: ptr points to the first letter
2752: len the length of the name
2753:
2754: Returns: a value representing the name, or -1 if unknown
2755: */
2756:
2757: static int
1.1.1.2 misho 2758: check_posix_name(const pcre_uchar *ptr, int len)
1.1 misho 2759: {
2760: const char *pn = posix_names;
2761: register int yield = 0;
2762: while (posix_name_lengths[yield] != 0)
2763: {
2764: if (len == posix_name_lengths[yield] &&
1.1.1.4 ! misho 2765: STRNCMP_UC_C8(ptr, pn, (unsigned int)len) == 0) return yield;
1.1 misho 2766: pn += posix_name_lengths[yield] + 1;
2767: yield++;
2768: }
2769: return -1;
2770: }
2771:
2772:
2773: /*************************************************
2774: * Adjust OP_RECURSE items in repeated group *
2775: *************************************************/
2776:
2777: /* OP_RECURSE items contain an offset from the start of the regex to the group
2778: that is referenced. This means that groups can be replicated for fixed
2779: repetition simply by copying (because the recursion is allowed to refer to
2780: earlier groups that are outside the current group). However, when a group is
2781: optional (i.e. the minimum quantifier is zero), OP_BRAZERO or OP_SKIPZERO is
2782: inserted before it, after it has been compiled. This means that any OP_RECURSE
2783: items within it that refer to the group itself or any contained groups have to
2784: have their offsets adjusted. That one of the jobs of this function. Before it
2785: is called, the partially compiled regex must be temporarily terminated with
2786: OP_END.
2787:
2788: This function has been extended with the possibility of forward references for
2789: recursions and subroutine calls. It must also check the list of such references
2790: for the group we are dealing with. If it finds that one of the recursions in
2791: the current group is on this list, it adjusts the offset in the list, not the
2792: value in the reference (which is a group number).
2793:
2794: Arguments:
2795: group points to the start of the group
2796: adjust the amount by which the group is to be moved
1.1.1.4 ! misho 2797: utf TRUE in UTF-8 / UTF-16 / UTF-32 mode
1.1 misho 2798: cd contains pointers to tables etc.
2799: save_hwm the hwm forward reference pointer at the start of the group
2800:
2801: Returns: nothing
2802: */
2803:
2804: static void
1.1.1.2 misho 2805: adjust_recurse(pcre_uchar *group, int adjust, BOOL utf, compile_data *cd,
2806: pcre_uchar *save_hwm)
1.1 misho 2807: {
1.1.1.2 misho 2808: pcre_uchar *ptr = group;
1.1 misho 2809:
1.1.1.2 misho 2810: while ((ptr = (pcre_uchar *)find_recurse(ptr, utf)) != NULL)
1.1 misho 2811: {
2812: int offset;
1.1.1.2 misho 2813: pcre_uchar *hc;
1.1 misho 2814:
2815: /* See if this recursion is on the forward reference list. If so, adjust the
2816: reference. */
2817:
2818: for (hc = save_hwm; hc < cd->hwm; hc += LINK_SIZE)
2819: {
1.1.1.4 ! misho 2820: offset = (int)GET(hc, 0);
1.1 misho 2821: if (cd->start_code + offset == ptr + 1)
2822: {
2823: PUT(hc, 0, offset + adjust);
2824: break;
2825: }
2826: }
2827:
2828: /* Otherwise, adjust the recursion offset if it's after the start of this
2829: group. */
2830:
2831: if (hc >= cd->hwm)
2832: {
1.1.1.4 ! misho 2833: offset = (int)GET(ptr, 1);
1.1 misho 2834: if (cd->start_code + offset >= group) PUT(ptr, 1, offset + adjust);
2835: }
2836:
2837: ptr += 1 + LINK_SIZE;
2838: }
2839: }
2840:
2841:
2842:
2843: /*************************************************
2844: * Insert an automatic callout point *
2845: *************************************************/
2846:
2847: /* This function is called when the PCRE_AUTO_CALLOUT option is set, to insert
2848: callout points before each pattern item.
2849:
2850: Arguments:
2851: code current code pointer
2852: ptr current pattern pointer
2853: cd pointers to tables etc
2854:
2855: Returns: new code pointer
2856: */
2857:
1.1.1.2 misho 2858: static pcre_uchar *
2859: auto_callout(pcre_uchar *code, const pcre_uchar *ptr, compile_data *cd)
1.1 misho 2860: {
2861: *code++ = OP_CALLOUT;
2862: *code++ = 255;
2863: PUT(code, 0, (int)(ptr - cd->start_pattern)); /* Pattern offset */
2864: PUT(code, LINK_SIZE, 0); /* Default length */
1.1.1.2 misho 2865: return code + 2 * LINK_SIZE;
1.1 misho 2866: }
2867:
2868:
2869:
2870: /*************************************************
2871: * Complete a callout item *
2872: *************************************************/
2873:
2874: /* A callout item contains the length of the next item in the pattern, which
2875: we can't fill in till after we have reached the relevant point. This is used
2876: for both automatic and manual callouts.
2877:
2878: Arguments:
2879: previous_callout points to previous callout item
2880: ptr current pattern pointer
2881: cd pointers to tables etc
2882:
2883: Returns: nothing
2884: */
2885:
2886: static void
1.1.1.2 misho 2887: complete_callout(pcre_uchar *previous_callout, const pcre_uchar *ptr, compile_data *cd)
1.1 misho 2888: {
2889: int length = (int)(ptr - cd->start_pattern - GET(previous_callout, 2));
2890: PUT(previous_callout, 2 + LINK_SIZE, length);
2891: }
2892:
2893:
2894:
2895: #ifdef SUPPORT_UCP
2896: /*************************************************
2897: * Get othercase range *
2898: *************************************************/
2899:
2900: /* This function is passed the start and end of a class range, in UTF-8 mode
1.1.1.4 ! misho 2901: with UCP support. It searches up the characters, looking for ranges of
1.1 misho 2902: characters in the "other" case. Each call returns the next one, updating the
1.1.1.4 ! misho 2903: start address. A character with multiple other cases is returned on its own
! 2904: with a special return value.
1.1 misho 2905:
2906: Arguments:
2907: cptr points to starting character value; updated
2908: d end value
2909: ocptr where to put start of othercase range
2910: odptr where to put end of othercase range
2911:
1.1.1.4 ! misho 2912: Yield: -1 when no more
! 2913: 0 when a range is returned
! 2914: >0 the CASESET offset for char with multiple other cases
! 2915: in this case, ocptr contains the original
1.1 misho 2916: */
2917:
1.1.1.4 ! misho 2918: static int
! 2919: get_othercase_range(pcre_uint32 *cptr, pcre_uint32 d, pcre_uint32 *ocptr,
! 2920: pcre_uint32 *odptr)
1.1 misho 2921: {
1.1.1.4 ! misho 2922: pcre_uint32 c, othercase, next;
! 2923: unsigned int co;
! 2924:
! 2925: /* Find the first character that has an other case. If it has multiple other
! 2926: cases, return its case offset value. */
1.1 misho 2927:
2928: for (c = *cptr; c <= d; c++)
1.1.1.4 ! misho 2929: {
! 2930: if ((co = UCD_CASESET(c)) != 0)
! 2931: {
! 2932: *ocptr = c++; /* Character that has the set */
! 2933: *cptr = c; /* Rest of input range */
! 2934: return (int)co;
! 2935: }
! 2936: if ((othercase = UCD_OTHERCASE(c)) != c) break;
! 2937: }
1.1 misho 2938:
1.1.1.4 ! misho 2939: if (c > d) return -1; /* Reached end of range */
1.1 misho 2940:
2941: *ocptr = othercase;
2942: next = othercase + 1;
2943:
2944: for (++c; c <= d; c++)
2945: {
2946: if (UCD_OTHERCASE(c) != next) break;
2947: next++;
2948: }
2949:
1.1.1.4 ! misho 2950: *odptr = next - 1; /* End of othercase range */
! 2951: *cptr = c; /* Rest of input range */
! 2952: return 0;
1.1 misho 2953: }
2954:
2955:
2956:
2957: /*************************************************
2958: * Check a character and a property *
2959: *************************************************/
2960:
2961: /* This function is called by check_auto_possessive() when a property item
2962: is adjacent to a fixed character.
2963:
2964: Arguments:
2965: c the character
2966: ptype the property type
2967: pdata the data for the type
2968: negated TRUE if it's a negated property (\P or \p{^)
2969:
2970: Returns: TRUE if auto-possessifying is OK
2971: */
2972:
2973: static BOOL
1.1.1.4 ! misho 2974: check_char_prop(pcre_uint32 c, unsigned int ptype, unsigned int pdata, BOOL negated)
1.1 misho 2975: {
1.1.1.4 ! misho 2976: #ifdef SUPPORT_UCP
! 2977: const pcre_uint32 *p;
! 2978: #endif
! 2979:
1.1 misho 2980: const ucd_record *prop = GET_UCD(c);
1.1.1.4 ! misho 2981:
1.1 misho 2982: switch(ptype)
2983: {
2984: case PT_LAMP:
2985: return (prop->chartype == ucp_Lu ||
2986: prop->chartype == ucp_Ll ||
2987: prop->chartype == ucp_Lt) == negated;
2988:
2989: case PT_GC:
1.1.1.2 misho 2990: return (pdata == PRIV(ucp_gentype)[prop->chartype]) == negated;
1.1 misho 2991:
2992: case PT_PC:
2993: return (pdata == prop->chartype) == negated;
2994:
2995: case PT_SC:
2996: return (pdata == prop->script) == negated;
2997:
2998: /* These are specials */
2999:
3000: case PT_ALNUM:
1.1.1.2 misho 3001: return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
3002: PRIV(ucp_gentype)[prop->chartype] == ucp_N) == negated;
1.1 misho 3003:
3004: case PT_SPACE: /* Perl space */
1.1.1.2 misho 3005: return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z ||
1.1 misho 3006: c == CHAR_HT || c == CHAR_NL || c == CHAR_FF || c == CHAR_CR)
3007: == negated;
3008:
3009: case PT_PXSPACE: /* POSIX space */
1.1.1.2 misho 3010: return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z ||
1.1 misho 3011: c == CHAR_HT || c == CHAR_NL || c == CHAR_VT ||
3012: c == CHAR_FF || c == CHAR_CR)
3013: == negated;
3014:
3015: case PT_WORD:
1.1.1.2 misho 3016: return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
3017: PRIV(ucp_gentype)[prop->chartype] == ucp_N ||
1.1 misho 3018: c == CHAR_UNDERSCORE) == negated;
1.1.1.4 ! misho 3019:
! 3020: #ifdef SUPPORT_UCP
! 3021: case PT_CLIST:
! 3022: p = PRIV(ucd_caseless_sets) + prop->caseset;
! 3023: for (;;)
! 3024: {
! 3025: if (c < *p) return !negated;
! 3026: if (c == *p++) return negated;
! 3027: }
! 3028: break; /* Control never reaches here */
! 3029: #endif
1.1 misho 3030: }
1.1.1.4 ! misho 3031:
1.1 misho 3032: return FALSE;
3033: }
3034: #endif /* SUPPORT_UCP */
3035:
3036:
3037:
3038: /*************************************************
3039: * Check if auto-possessifying is possible *
3040: *************************************************/
3041:
3042: /* This function is called for unlimited repeats of certain items, to see
3043: whether the next thing could possibly match the repeated item. If not, it makes
3044: sense to automatically possessify the repeated item.
3045:
3046: Arguments:
3047: previous pointer to the repeated opcode
1.1.1.4 ! misho 3048: utf TRUE in UTF-8 / UTF-16 / UTF-32 mode
1.1 misho 3049: ptr next character in pattern
3050: options options bits
3051: cd contains pointers to tables etc.
3052:
3053: Returns: TRUE if possessifying is wanted
3054: */
3055:
3056: static BOOL
1.1.1.2 misho 3057: check_auto_possessive(const pcre_uchar *previous, BOOL utf,
3058: const pcre_uchar *ptr, int options, compile_data *cd)
1.1 misho 3059: {
1.1.1.4 ! misho 3060: pcre_uint32 c = NOTACHAR;
! 3061: pcre_uint32 next;
! 3062: int escape;
! 3063: pcre_uchar op_code = *previous++;
1.1 misho 3064:
3065: /* Skip whitespace and comments in extended mode */
3066:
3067: if ((options & PCRE_EXTENDED) != 0)
3068: {
3069: for (;;)
3070: {
1.1.1.2 misho 3071: while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_space) != 0) ptr++;
1.1 misho 3072: if (*ptr == CHAR_NUMBER_SIGN)
3073: {
3074: ptr++;
1.1.1.4 ! misho 3075: while (*ptr != CHAR_NULL)
1.1 misho 3076: {
3077: if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }
3078: ptr++;
1.1.1.2 misho 3079: #ifdef SUPPORT_UTF
3080: if (utf) FORWARDCHAR(ptr);
1.1 misho 3081: #endif
3082: }
3083: }
3084: else break;
3085: }
3086: }
3087:
3088: /* If the next item is one that we can handle, get its value. A non-negative
3089: value is a character, a negative value is an escape value. */
3090:
3091: if (*ptr == CHAR_BACKSLASH)
3092: {
3093: int temperrorcode = 0;
1.1.1.4 ! misho 3094: escape = check_escape(&ptr, &next, &temperrorcode, cd->bracount, options,
! 3095: FALSE);
1.1 misho 3096: if (temperrorcode != 0) return FALSE;
3097: ptr++; /* Point after the escape sequence */
3098: }
1.1.1.2 misho 3099: else if (!MAX_255(*ptr) || (cd->ctypes[*ptr] & ctype_meta) == 0)
1.1 misho 3100: {
1.1.1.4 ! misho 3101: escape = 0;
1.1.1.2 misho 3102: #ifdef SUPPORT_UTF
3103: if (utf) { GETCHARINC(next, ptr); } else
1.1 misho 3104: #endif
3105: next = *ptr++;
3106: }
3107: else return FALSE;
3108:
3109: /* Skip whitespace and comments in extended mode */
3110:
3111: if ((options & PCRE_EXTENDED) != 0)
3112: {
3113: for (;;)
3114: {
1.1.1.2 misho 3115: while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_space) != 0) ptr++;
1.1 misho 3116: if (*ptr == CHAR_NUMBER_SIGN)
3117: {
3118: ptr++;
1.1.1.4 ! misho 3119: while (*ptr != CHAR_NULL)
1.1 misho 3120: {
3121: if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }
3122: ptr++;
1.1.1.2 misho 3123: #ifdef SUPPORT_UTF
3124: if (utf) FORWARDCHAR(ptr);
1.1 misho 3125: #endif
3126: }
3127: }
3128: else break;
3129: }
3130: }
3131:
3132: /* If the next thing is itself optional, we have to give up. */
3133:
3134: if (*ptr == CHAR_ASTERISK || *ptr == CHAR_QUESTION_MARK ||
1.1.1.2 misho 3135: STRNCMP_UC_C8(ptr, STR_LEFT_CURLY_BRACKET STR_0 STR_COMMA, 3) == 0)
1.1 misho 3136: return FALSE;
3137:
1.1.1.4 ! misho 3138: /* If the previous item is a character, get its value. */
1.1 misho 3139:
1.1.1.4 ! misho 3140: if (op_code == OP_CHAR || op_code == OP_CHARI ||
! 3141: op_code == OP_NOT || op_code == OP_NOTI)
1.1 misho 3142: {
1.1.1.2 misho 3143: #ifdef SUPPORT_UTF
1.1 misho 3144: GETCHARTEST(c, previous);
3145: #else
3146: c = *previous;
3147: #endif
1.1.1.4 ! misho 3148: }
1.1 misho 3149:
1.1.1.4 ! misho 3150: /* Now compare the next item with the previous opcode. First, handle cases when
! 3151: the next item is a character. */
1.1 misho 3152:
1.1.1.4 ! misho 3153: if (escape == 0)
! 3154: {
! 3155: /* For a caseless UTF match, the next character may have more than one other
! 3156: case, which maps to the special PT_CLIST property. Check this first. */
! 3157:
! 3158: #ifdef SUPPORT_UCP
! 3159: if (utf && c != NOTACHAR && (options & PCRE_CASELESS) != 0)
! 3160: {
! 3161: unsigned int ocs = UCD_CASESET(next);
! 3162: if (ocs > 0) return check_char_prop(c, PT_CLIST, ocs, op_code >= OP_NOT);
! 3163: }
1.1 misho 3164: #endif
1.1.1.4 ! misho 3165:
! 3166: switch(op_code)
1.1 misho 3167: {
1.1.1.4 ! misho 3168: case OP_CHAR:
! 3169: return c != next;
! 3170:
! 3171: /* For CHARI (caseless character) we must check the other case. If we have
! 3172: Unicode property support, we can use it to test the other case of
! 3173: high-valued characters. We know that next can have only one other case,
! 3174: because multi-other-case characters are dealt with above. */
! 3175:
! 3176: case OP_CHARI:
! 3177: if (c == next) return FALSE;
! 3178: #ifdef SUPPORT_UTF
! 3179: if (utf)
! 3180: {
! 3181: pcre_uint32 othercase;
! 3182: if (next < 128) othercase = cd->fcc[next]; else
1.1 misho 3183: #ifdef SUPPORT_UCP
1.1.1.4 ! misho 3184: othercase = UCD_OTHERCASE(next);
1.1 misho 3185: #else
1.1.1.4 ! misho 3186: othercase = NOTACHAR;
1.1 misho 3187: #endif
1.1.1.4 ! misho 3188: return c != othercase;
! 3189: }
! 3190: else
1.1.1.2 misho 3191: #endif /* SUPPORT_UTF */
1.1.1.4 ! misho 3192: return (c != TABLE_GET(next, cd->fcc, next)); /* Not UTF */
1.1 misho 3193:
1.1.1.4 ! misho 3194: case OP_NOT:
! 3195: return c == next;
1.1 misho 3196:
1.1.1.4 ! misho 3197: case OP_NOTI:
! 3198: if (c == next) return TRUE;
1.1.1.2 misho 3199: #ifdef SUPPORT_UTF
1.1.1.4 ! misho 3200: if (utf)
! 3201: {
! 3202: pcre_uint32 othercase;
! 3203: if (next < 128) othercase = cd->fcc[next]; else
1.1 misho 3204: #ifdef SUPPORT_UCP
1.1.1.4 ! misho 3205: othercase = UCD_OTHERCASE(next);
1.1 misho 3206: #else
1.1.1.4 ! misho 3207: othercase = NOTACHAR;
1.1 misho 3208: #endif
1.1.1.4 ! misho 3209: return c == othercase;
! 3210: }
! 3211: else
1.1.1.2 misho 3212: #endif /* SUPPORT_UTF */
1.1.1.4 ! misho 3213: return (c == TABLE_GET(next, cd->fcc, next)); /* Not UTF */
1.1 misho 3214:
1.1.1.4 ! misho 3215: /* Note that OP_DIGIT etc. are generated only when PCRE_UCP is *not* set.
! 3216: When it is set, \d etc. are converted into OP_(NOT_)PROP codes. */
1.1 misho 3217:
1.1.1.4 ! misho 3218: case OP_DIGIT:
! 3219: return next > 255 || (cd->ctypes[next] & ctype_digit) == 0;
1.1 misho 3220:
1.1.1.4 ! misho 3221: case OP_NOT_DIGIT:
! 3222: return next <= 255 && (cd->ctypes[next] & ctype_digit) != 0;
1.1 misho 3223:
1.1.1.4 ! misho 3224: case OP_WHITESPACE:
! 3225: return next > 255 || (cd->ctypes[next] & ctype_space) == 0;
1.1 misho 3226:
1.1.1.4 ! misho 3227: case OP_NOT_WHITESPACE:
! 3228: return next <= 255 && (cd->ctypes[next] & ctype_space) != 0;
1.1 misho 3229:
1.1.1.4 ! misho 3230: case OP_WORDCHAR:
! 3231: return next > 255 || (cd->ctypes[next] & ctype_word) == 0;
1.1 misho 3232:
1.1.1.4 ! misho 3233: case OP_NOT_WORDCHAR:
! 3234: return next <= 255 && (cd->ctypes[next] & ctype_word) != 0;
1.1 misho 3235:
1.1.1.4 ! misho 3236: case OP_HSPACE:
! 3237: case OP_NOT_HSPACE:
! 3238: switch(next)
! 3239: {
! 3240: HSPACE_CASES:
! 3241: return op_code == OP_NOT_HSPACE;
1.1 misho 3242:
1.1.1.4 ! misho 3243: default:
! 3244: return op_code != OP_NOT_HSPACE;
! 3245: }
! 3246:
! 3247: case OP_ANYNL:
! 3248: case OP_VSPACE:
! 3249: case OP_NOT_VSPACE:
! 3250: switch(next)
! 3251: {
! 3252: VSPACE_CASES:
! 3253: return op_code == OP_NOT_VSPACE;
! 3254:
! 3255: default:
! 3256: return op_code != OP_NOT_VSPACE;
! 3257: }
1.1 misho 3258:
3259: #ifdef SUPPORT_UCP
1.1.1.4 ! misho 3260: case OP_PROP:
! 3261: return check_char_prop(next, previous[0], previous[1], FALSE);
1.1 misho 3262:
1.1.1.4 ! misho 3263: case OP_NOTPROP:
! 3264: return check_char_prop(next, previous[0], previous[1], TRUE);
1.1 misho 3265: #endif
3266:
1.1.1.4 ! misho 3267: default:
! 3268: return FALSE;
! 3269: }
1.1 misho 3270: }
3271:
3272: /* Handle the case when the next item is \d, \s, etc. Note that when PCRE_UCP
3273: is set, \d turns into ESC_du rather than ESC_d, etc., so ESC_d etc. are
3274: generated only when PCRE_UCP is *not* set, that is, when only ASCII
3275: characteristics are recognized. Similarly, the opcodes OP_DIGIT etc. are
3276: replaced by OP_PROP codes when PCRE_UCP is set. */
3277:
3278: switch(op_code)
3279: {
3280: case OP_CHAR:
3281: case OP_CHARI:
1.1.1.4 ! misho 3282: switch(escape)
1.1 misho 3283: {
3284: case ESC_d:
1.1.1.3 misho 3285: return c > 255 || (cd->ctypes[c] & ctype_digit) == 0;
1.1 misho 3286:
3287: case ESC_D:
1.1.1.3 misho 3288: return c <= 255 && (cd->ctypes[c] & ctype_digit) != 0;
1.1 misho 3289:
3290: case ESC_s:
1.1.1.3 misho 3291: return c > 255 || (cd->ctypes[c] & ctype_space) == 0;
1.1 misho 3292:
3293: case ESC_S:
1.1.1.3 misho 3294: return c <= 255 && (cd->ctypes[c] & ctype_space) != 0;
1.1 misho 3295:
3296: case ESC_w:
1.1.1.3 misho 3297: return c > 255 || (cd->ctypes[c] & ctype_word) == 0;
1.1 misho 3298:
3299: case ESC_W:
1.1.1.3 misho 3300: return c <= 255 && (cd->ctypes[c] & ctype_word) != 0;
1.1 misho 3301:
3302: case ESC_h:
3303: case ESC_H:
3304: switch(c)
3305: {
1.1.1.4 ! misho 3306: HSPACE_CASES:
! 3307: return escape != ESC_h;
! 3308:
1.1 misho 3309: default:
1.1.1.4 ! misho 3310: return escape == ESC_h;
1.1 misho 3311: }
3312:
3313: case ESC_v:
3314: case ESC_V:
3315: switch(c)
3316: {
1.1.1.4 ! misho 3317: VSPACE_CASES:
! 3318: return escape != ESC_v;
! 3319:
1.1 misho 3320: default:
1.1.1.4 ! misho 3321: return escape == ESC_v;
1.1 misho 3322: }
3323:
3324: /* When PCRE_UCP is set, these values get generated for \d etc. Find
3325: their substitutions and process them. The result will always be either
1.1.1.4 ! misho 3326: ESC_p or ESC_P. Then fall through to process those values. */
1.1 misho 3327:
3328: #ifdef SUPPORT_UCP
3329: case ESC_du:
3330: case ESC_DU:
3331: case ESC_wu:
3332: case ESC_WU:
3333: case ESC_su:
3334: case ESC_SU:
3335: {
3336: int temperrorcode = 0;
1.1.1.4 ! misho 3337: ptr = substitutes[escape - ESC_DU];
! 3338: escape = check_escape(&ptr, &next, &temperrorcode, 0, options, FALSE);
1.1 misho 3339: if (temperrorcode != 0) return FALSE;
3340: ptr++; /* For compatibility */
3341: }
3342: /* Fall through */
3343:
3344: case ESC_p:
3345: case ESC_P:
3346: {
1.1.1.4 ! misho 3347: unsigned int ptype = 0, pdata = 0;
! 3348: int errorcodeptr;
1.1 misho 3349: BOOL negated;
3350:
3351: ptr--; /* Make ptr point at the p or P */
1.1.1.4 ! misho 3352: if (!get_ucp(&ptr, &negated, &ptype, &pdata, &errorcodeptr))
! 3353: return FALSE;
1.1 misho 3354: ptr++; /* Point past the final curly ket */
3355:
3356: /* If the property item is optional, we have to give up. (When generated
3357: from \d etc by PCRE_UCP, this test will have been applied much earlier,
3358: to the original \d etc. At this point, ptr will point to a zero byte. */
3359:
3360: if (*ptr == CHAR_ASTERISK || *ptr == CHAR_QUESTION_MARK ||
1.1.1.2 misho 3361: STRNCMP_UC_C8(ptr, STR_LEFT_CURLY_BRACKET STR_0 STR_COMMA, 3) == 0)
1.1 misho 3362: return FALSE;
3363:
3364: /* Do the property check. */
3365:
1.1.1.4 ! misho 3366: return check_char_prop(c, ptype, pdata, (escape == ESC_P) != negated);
1.1 misho 3367: }
3368: #endif
3369:
3370: default:
3371: return FALSE;
3372: }
3373:
3374: /* In principle, support for Unicode properties should be integrated here as
3375: well. It means re-organizing the above code so as to get hold of the property
3376: values before switching on the op-code. However, I wonder how many patterns
3377: combine ASCII \d etc with Unicode properties? (Note that if PCRE_UCP is set,
3378: these op-codes are never generated.) */
3379:
3380: case OP_DIGIT:
1.1.1.4 ! misho 3381: return escape == ESC_D || escape == ESC_s || escape == ESC_W ||
! 3382: escape == ESC_h || escape == ESC_v || escape == ESC_R;
1.1 misho 3383:
3384: case OP_NOT_DIGIT:
1.1.1.4 ! misho 3385: return escape == ESC_d;
1.1 misho 3386:
3387: case OP_WHITESPACE:
1.1.1.4 ! misho 3388: return escape == ESC_S || escape == ESC_d || escape == ESC_w;
1.1 misho 3389:
3390: case OP_NOT_WHITESPACE:
1.1.1.4 ! misho 3391: return escape == ESC_s || escape == ESC_h || escape == ESC_v || escape == ESC_R;
1.1 misho 3392:
3393: case OP_HSPACE:
1.1.1.4 ! misho 3394: return escape == ESC_S || escape == ESC_H || escape == ESC_d ||
! 3395: escape == ESC_w || escape == ESC_v || escape == ESC_R;
1.1 misho 3396:
3397: case OP_NOT_HSPACE:
1.1.1.4 ! misho 3398: return escape == ESC_h;
1.1 misho 3399:
3400: /* Can't have \S in here because VT matches \S (Perl anomaly) */
3401: case OP_ANYNL:
3402: case OP_VSPACE:
1.1.1.4 ! misho 3403: return escape == ESC_V || escape == ESC_d || escape == ESC_w;
1.1 misho 3404:
3405: case OP_NOT_VSPACE:
1.1.1.4 ! misho 3406: return escape == ESC_v || escape == ESC_R;
1.1 misho 3407:
3408: case OP_WORDCHAR:
1.1.1.4 ! misho 3409: return escape == ESC_W || escape == ESC_s || escape == ESC_h ||
! 3410: escape == ESC_v || escape == ESC_R;
1.1 misho 3411:
3412: case OP_NOT_WORDCHAR:
1.1.1.4 ! misho 3413: return escape == ESC_w || escape == ESC_d;
1.1 misho 3414:
3415: default:
3416: return FALSE;
3417: }
3418:
3419: /* Control does not reach here */
3420: }
3421:
3422:
3423:
3424: /*************************************************
1.1.1.4 ! misho 3425: * Add a character or range to a class *
! 3426: *************************************************/
! 3427:
! 3428: /* This function packages up the logic of adding a character or range of
! 3429: characters to a class. The character values in the arguments will be within the
! 3430: valid values for the current mode (8-bit, 16-bit, UTF, etc). This function is
! 3431: mutually recursive with the function immediately below.
! 3432:
! 3433: Arguments:
! 3434: classbits the bit map for characters < 256
! 3435: uchardptr points to the pointer for extra data
! 3436: options the options word
! 3437: cd contains pointers to tables etc.
! 3438: start start of range character
! 3439: end end of range character
! 3440:
! 3441: Returns: the number of < 256 characters added
! 3442: the pointer to extra data is updated
! 3443: */
! 3444:
! 3445: static int
! 3446: add_to_class(pcre_uint8 *classbits, pcre_uchar **uchardptr, int options,
! 3447: compile_data *cd, pcre_uint32 start, pcre_uint32 end)
! 3448: {
! 3449: pcre_uint32 c;
! 3450: int n8 = 0;
! 3451:
! 3452: /* If caseless matching is required, scan the range and process alternate
! 3453: cases. In Unicode, there are 8-bit characters that have alternate cases that
! 3454: are greater than 255 and vice-versa. Sometimes we can just extend the original
! 3455: range. */
! 3456:
! 3457: if ((options & PCRE_CASELESS) != 0)
! 3458: {
! 3459: #ifdef SUPPORT_UCP
! 3460: if ((options & PCRE_UTF8) != 0)
! 3461: {
! 3462: int rc;
! 3463: pcre_uint32 oc, od;
! 3464:
! 3465: options &= ~PCRE_CASELESS; /* Remove for recursive calls */
! 3466: c = start;
! 3467:
! 3468: while ((rc = get_othercase_range(&c, end, &oc, &od)) >= 0)
! 3469: {
! 3470: /* Handle a single character that has more than one other case. */
! 3471:
! 3472: if (rc > 0) n8 += add_list_to_class(classbits, uchardptr, options, cd,
! 3473: PRIV(ucd_caseless_sets) + rc, oc);
! 3474:
! 3475: /* Do nothing if the other case range is within the original range. */
! 3476:
! 3477: else if (oc >= start && od <= end) continue;
! 3478:
! 3479: /* Extend the original range if there is overlap, noting that if oc < c, we
! 3480: can't have od > end because a subrange is always shorter than the basic
! 3481: range. Otherwise, use a recursive call to add the additional range. */
! 3482:
! 3483: else if (oc < start && od >= start - 1) start = oc; /* Extend downwards */
! 3484: else if (od > end && oc <= end + 1) end = od; /* Extend upwards */
! 3485: else n8 += add_to_class(classbits, uchardptr, options, cd, oc, od);
! 3486: }
! 3487: }
! 3488: else
! 3489: #endif /* SUPPORT_UCP */
! 3490:
! 3491: /* Not UTF-mode, or no UCP */
! 3492:
! 3493: for (c = start; c <= end && c < 256; c++)
! 3494: {
! 3495: SETBIT(classbits, cd->fcc[c]);
! 3496: n8++;
! 3497: }
! 3498: }
! 3499:
! 3500: /* Now handle the original range. Adjust the final value according to the bit
! 3501: length - this means that the same lists of (e.g.) horizontal spaces can be used
! 3502: in all cases. */
! 3503:
! 3504: #if defined COMPILE_PCRE8
! 3505: #ifdef SUPPORT_UTF
! 3506: if ((options & PCRE_UTF8) == 0)
! 3507: #endif
! 3508: if (end > 0xff) end = 0xff;
! 3509:
! 3510: #elif defined COMPILE_PCRE16
! 3511: #ifdef SUPPORT_UTF
! 3512: if ((options & PCRE_UTF16) == 0)
! 3513: #endif
! 3514: if (end > 0xffff) end = 0xffff;
! 3515:
! 3516: #endif /* COMPILE_PCRE[8|16] */
! 3517:
! 3518: /* If all characters are less than 256, use the bit map. Otherwise use extra
! 3519: data. */
! 3520:
! 3521: if (end < 0x100)
! 3522: {
! 3523: for (c = start; c <= end; c++)
! 3524: {
! 3525: n8++;
! 3526: SETBIT(classbits, c);
! 3527: }
! 3528: }
! 3529:
! 3530: else
! 3531: {
! 3532: pcre_uchar *uchardata = *uchardptr;
! 3533:
! 3534: #ifdef SUPPORT_UTF
! 3535: if ((options & PCRE_UTF8) != 0) /* All UTFs use the same flag bit */
! 3536: {
! 3537: if (start < end)
! 3538: {
! 3539: *uchardata++ = XCL_RANGE;
! 3540: uchardata += PRIV(ord2utf)(start, uchardata);
! 3541: uchardata += PRIV(ord2utf)(end, uchardata);
! 3542: }
! 3543: else if (start == end)
! 3544: {
! 3545: *uchardata++ = XCL_SINGLE;
! 3546: uchardata += PRIV(ord2utf)(start, uchardata);
! 3547: }
! 3548: }
! 3549: else
! 3550: #endif /* SUPPORT_UTF */
! 3551:
! 3552: /* Without UTF support, character values are constrained by the bit length,
! 3553: and can only be > 256 for 16-bit and 32-bit libraries. */
! 3554:
! 3555: #ifdef COMPILE_PCRE8
! 3556: {}
! 3557: #else
! 3558: if (start < end)
! 3559: {
! 3560: *uchardata++ = XCL_RANGE;
! 3561: *uchardata++ = start;
! 3562: *uchardata++ = end;
! 3563: }
! 3564: else if (start == end)
! 3565: {
! 3566: *uchardata++ = XCL_SINGLE;
! 3567: *uchardata++ = start;
! 3568: }
! 3569: #endif
! 3570:
! 3571: *uchardptr = uchardata; /* Updata extra data pointer */
! 3572: }
! 3573:
! 3574: return n8; /* Number of 8-bit characters */
! 3575: }
! 3576:
! 3577:
! 3578:
! 3579:
! 3580: /*************************************************
! 3581: * Add a list of characters to a class *
! 3582: *************************************************/
! 3583:
! 3584: /* This function is used for adding a list of case-equivalent characters to a
! 3585: class, and also for adding a list of horizontal or vertical whitespace. If the
! 3586: list is in order (which it should be), ranges of characters are detected and
! 3587: handled appropriately. This function is mutually recursive with the function
! 3588: above.
! 3589:
! 3590: Arguments:
! 3591: classbits the bit map for characters < 256
! 3592: uchardptr points to the pointer for extra data
! 3593: options the options word
! 3594: cd contains pointers to tables etc.
! 3595: p points to row of 32-bit values, terminated by NOTACHAR
! 3596: except character to omit; this is used when adding lists of
! 3597: case-equivalent characters to avoid including the one we
! 3598: already know about
! 3599:
! 3600: Returns: the number of < 256 characters added
! 3601: the pointer to extra data is updated
! 3602: */
! 3603:
! 3604: static int
! 3605: add_list_to_class(pcre_uint8 *classbits, pcre_uchar **uchardptr, int options,
! 3606: compile_data *cd, const pcre_uint32 *p, unsigned int except)
! 3607: {
! 3608: int n8 = 0;
! 3609: while (p[0] < NOTACHAR)
! 3610: {
! 3611: int n = 0;
! 3612: if (p[0] != except)
! 3613: {
! 3614: while(p[n+1] == p[0] + n + 1) n++;
! 3615: n8 += add_to_class(classbits, uchardptr, options, cd, p[0], p[n]);
! 3616: }
! 3617: p += n + 1;
! 3618: }
! 3619: return n8;
! 3620: }
! 3621:
! 3622:
! 3623:
! 3624: /*************************************************
! 3625: * Add characters not in a list to a class *
! 3626: *************************************************/
! 3627:
! 3628: /* This function is used for adding the complement of a list of horizontal or
! 3629: vertical whitespace to a class. The list must be in order.
! 3630:
! 3631: Arguments:
! 3632: classbits the bit map for characters < 256
! 3633: uchardptr points to the pointer for extra data
! 3634: options the options word
! 3635: cd contains pointers to tables etc.
! 3636: p points to row of 32-bit values, terminated by NOTACHAR
! 3637:
! 3638: Returns: the number of < 256 characters added
! 3639: the pointer to extra data is updated
! 3640: */
! 3641:
! 3642: static int
! 3643: add_not_list_to_class(pcre_uint8 *classbits, pcre_uchar **uchardptr,
! 3644: int options, compile_data *cd, const pcre_uint32 *p)
! 3645: {
! 3646: BOOL utf = (options & PCRE_UTF8) != 0;
! 3647: int n8 = 0;
! 3648: if (p[0] > 0)
! 3649: n8 += add_to_class(classbits, uchardptr, options, cd, 0, p[0] - 1);
! 3650: while (p[0] < NOTACHAR)
! 3651: {
! 3652: while (p[1] == p[0] + 1) p++;
! 3653: n8 += add_to_class(classbits, uchardptr, options, cd, p[0] + 1,
! 3654: (p[1] == NOTACHAR) ? (utf ? 0x10ffffu : 0xffffffffu) : p[1] - 1);
! 3655: p++;
! 3656: }
! 3657: return n8;
! 3658: }
! 3659:
! 3660:
! 3661:
! 3662: /*************************************************
1.1 misho 3663: * Compile one branch *
3664: *************************************************/
3665:
3666: /* Scan the pattern, compiling it into the a vector. If the options are
3667: changed during the branch, the pointer is used to change the external options
3668: bits. This function is used during the pre-compile phase when we are trying
3669: to find out the amount of memory needed, as well as during the real compile
3670: phase. The value of lengthptr distinguishes the two phases.
3671:
3672: Arguments:
3673: optionsptr pointer to the option bits
3674: codeptr points to the pointer to the current code point
3675: ptrptr points to the current pattern pointer
3676: errorcodeptr points to error code variable
1.1.1.4 ! misho 3677: firstcharptr place to put the first required character
! 3678: firstcharflagsptr place to put the first character flags, or a negative number
! 3679: reqcharptr place to put the last required character
! 3680: reqcharflagsptr place to put the last required character flags, or a negative number
1.1 misho 3681: bcptr points to current branch chain
3682: cond_depth conditional nesting depth
3683: cd contains pointers to tables etc.
3684: lengthptr NULL during the real compile phase
3685: points to length accumulator during pre-compile phase
3686:
3687: Returns: TRUE on success
3688: FALSE, with *errorcodeptr set non-zero on error
3689: */
3690:
3691: static BOOL
1.1.1.2 misho 3692: compile_branch(int *optionsptr, pcre_uchar **codeptr,
1.1.1.4 ! misho 3693: const pcre_uchar **ptrptr, int *errorcodeptr,
! 3694: pcre_uint32 *firstcharptr, pcre_int32 *firstcharflagsptr,
! 3695: pcre_uint32 *reqcharptr, pcre_int32 *reqcharflagsptr,
! 3696: branch_chain *bcptr, int cond_depth,
1.1.1.2 misho 3697: compile_data *cd, int *lengthptr)
1.1 misho 3698: {
3699: int repeat_type, op_type;
3700: int repeat_min = 0, repeat_max = 0; /* To please picky compilers */
3701: int bravalue = 0;
3702: int greedy_default, greedy_non_default;
1.1.1.4 ! misho 3703: pcre_uint32 firstchar, reqchar;
! 3704: pcre_int32 firstcharflags, reqcharflags;
! 3705: pcre_uint32 zeroreqchar, zerofirstchar;
! 3706: pcre_int32 zeroreqcharflags, zerofirstcharflags;
1.1.1.2 misho 3707: pcre_int32 req_caseopt, reqvary, tempreqvary;
1.1 misho 3708: int options = *optionsptr; /* May change dynamically */
3709: int after_manual_callout = 0;
3710: int length_prevgroup = 0;
1.1.1.4 ! misho 3711: register pcre_uint32 c;
! 3712: int escape;
1.1.1.2 misho 3713: register pcre_uchar *code = *codeptr;
3714: pcre_uchar *last_code = code;
3715: pcre_uchar *orig_code = code;
3716: pcre_uchar *tempcode;
1.1 misho 3717: BOOL inescq = FALSE;
1.1.1.2 misho 3718: BOOL groupsetfirstchar = FALSE;
3719: const pcre_uchar *ptr = *ptrptr;
3720: const pcre_uchar *tempptr;
3721: const pcre_uchar *nestptr = NULL;
3722: pcre_uchar *previous = NULL;
3723: pcre_uchar *previous_callout = NULL;
3724: pcre_uchar *save_hwm = NULL;
3725: pcre_uint8 classbits[32];
1.1 misho 3726:
3727: /* We can fish out the UTF-8 setting once and for all into a BOOL, but we
3728: must not do this for other options (e.g. PCRE_EXTENDED) because they may change
3729: dynamically as we process the pattern. */
3730:
1.1.1.2 misho 3731: #ifdef SUPPORT_UTF
1.1.1.4 ! misho 3732: /* PCRE_UTF[16|32] have the same value as PCRE_UTF8. */
1.1.1.2 misho 3733: BOOL utf = (options & PCRE_UTF8) != 0;
1.1.1.4 ! misho 3734: #ifndef COMPILE_PCRE32
1.1.1.2 misho 3735: pcre_uchar utf_chars[6];
1.1.1.4 ! misho 3736: #endif
1.1 misho 3737: #else
1.1.1.2 misho 3738: BOOL utf = FALSE;
3739: #endif
3740:
1.1.1.4 ! misho 3741: /* Helper variables for OP_XCLASS opcode (for characters > 255). We define
! 3742: class_uchardata always so that it can be passed to add_to_class() always,
! 3743: though it will not be used in non-UTF 8-bit cases. This avoids having to supply
! 3744: alternative calls for the different cases. */
1.1.1.2 misho 3745:
1.1.1.4 ! misho 3746: pcre_uchar *class_uchardata;
1.1.1.2 misho 3747: #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3748: BOOL xclass;
3749: pcre_uchar *class_uchardata_base;
1.1 misho 3750: #endif
3751:
3752: #ifdef PCRE_DEBUG
3753: if (lengthptr != NULL) DPRINTF((">> start branch\n"));
3754: #endif
3755:
3756: /* Set up the default and non-default settings for greediness */
3757:
3758: greedy_default = ((options & PCRE_UNGREEDY) != 0);
3759: greedy_non_default = greedy_default ^ 1;
3760:
3761: /* Initialize no first byte, no required byte. REQ_UNSET means "no char
3762: matching encountered yet". It gets changed to REQ_NONE if we hit something that
1.1.1.2 misho 3763: matches a non-fixed char first char; reqchar just remains unset if we never
1.1 misho 3764: find one.
3765:
3766: When we hit a repeat whose minimum is zero, we may have to adjust these values
3767: to take the zero repeat into account. This is implemented by setting them to
1.1.1.2 misho 3768: zerofirstbyte and zeroreqchar when such a repeat is encountered. The individual
1.1 misho 3769: item types that can be repeated set these backoff variables appropriately. */
3770:
1.1.1.4 ! misho 3771: firstchar = reqchar = zerofirstchar = zeroreqchar = 0;
! 3772: firstcharflags = reqcharflags = zerofirstcharflags = zeroreqcharflags = REQ_UNSET;
1.1 misho 3773:
1.1.1.2 misho 3774: /* The variable req_caseopt contains either the REQ_CASELESS value
3775: or zero, according to the current setting of the caseless flag. The
3776: REQ_CASELESS leaves the lower 28 bit empty. It is added into the
3777: firstchar or reqchar variables to record the case status of the
3778: value. This is used only for ASCII characters. */
1.1 misho 3779:
1.1.1.2 misho 3780: req_caseopt = ((options & PCRE_CASELESS) != 0)? REQ_CASELESS:0;
1.1 misho 3781:
3782: /* Switch on next character until the end of the branch */
3783:
3784: for (;; ptr++)
3785: {
3786: BOOL negate_class;
3787: BOOL should_flip_negation;
3788: BOOL possessive_quantifier;
3789: BOOL is_quantifier;
3790: BOOL is_recurse;
3791: BOOL reset_bracount;
1.1.1.2 misho 3792: int class_has_8bitchar;
1.1.1.4 ! misho 3793: int class_one_char;
1.1 misho 3794: int newoptions;
3795: int recno;
3796: int refsign;
3797: int skipbytes;
1.1.1.4 ! misho 3798: pcre_uint32 subreqchar, subfirstchar;
! 3799: pcre_int32 subreqcharflags, subfirstcharflags;
1.1 misho 3800: int terminator;
1.1.1.4 ! misho 3801: unsigned int mclength;
! 3802: unsigned int tempbracount;
! 3803: pcre_uint32 ec;
1.1.1.2 misho 3804: pcre_uchar mcbuffer[8];
1.1 misho 3805:
1.1.1.2 misho 3806: /* Get next character in the pattern */
1.1 misho 3807:
3808: c = *ptr;
3809:
3810: /* If we are at the end of a nested substitution, revert to the outer level
3811: string. Nesting only happens one level deep. */
3812:
1.1.1.4 ! misho 3813: if (c == CHAR_NULL && nestptr != NULL)
1.1 misho 3814: {
3815: ptr = nestptr;
3816: nestptr = NULL;
3817: c = *ptr;
3818: }
3819:
3820: /* If we are in the pre-compile phase, accumulate the length used for the
3821: previous cycle of this loop. */
3822:
3823: if (lengthptr != NULL)
3824: {
3825: #ifdef PCRE_DEBUG
3826: if (code > cd->hwm) cd->hwm = code; /* High water info */
3827: #endif
3828: if (code > cd->start_workspace + cd->workspace_size -
3829: WORK_SIZE_SAFETY_MARGIN) /* Check for overrun */
3830: {
3831: *errorcodeptr = ERR52;
3832: goto FAILED;
3833: }
3834:
3835: /* There is at least one situation where code goes backwards: this is the
3836: case of a zero quantifier after a class (e.g. [ab]{0}). At compile time,
3837: the class is simply eliminated. However, it is created first, so we have to
3838: allow memory for it. Therefore, don't ever reduce the length at this point.
3839: */
3840:
3841: if (code < last_code) code = last_code;
3842:
3843: /* Paranoid check for integer overflow */
3844:
3845: if (OFLOW_MAX - *lengthptr < code - last_code)
3846: {
3847: *errorcodeptr = ERR20;
3848: goto FAILED;
3849: }
3850:
3851: *lengthptr += (int)(code - last_code);
1.1.1.2 misho 3852: DPRINTF(("length=%d added %d c=%c (0x%x)\n", *lengthptr,
3853: (int)(code - last_code), c, c));
1.1 misho 3854:
3855: /* If "previous" is set and it is not at the start of the work space, move
3856: it back to there, in order to avoid filling up the work space. Otherwise,
3857: if "previous" is NULL, reset the current code pointer to the start. */
3858:
3859: if (previous != NULL)
3860: {
3861: if (previous > orig_code)
3862: {
1.1.1.2 misho 3863: memmove(orig_code, previous, IN_UCHARS(code - previous));
1.1 misho 3864: code -= previous - orig_code;
3865: previous = orig_code;
3866: }
3867: }
3868: else code = orig_code;
3869:
3870: /* Remember where this code item starts so we can pick up the length
3871: next time round. */
3872:
3873: last_code = code;
3874: }
3875:
3876: /* In the real compile phase, just check the workspace used by the forward
3877: reference list. */
3878:
3879: else if (cd->hwm > cd->start_workspace + cd->workspace_size -
3880: WORK_SIZE_SAFETY_MARGIN)
3881: {
3882: *errorcodeptr = ERR52;
3883: goto FAILED;
3884: }
3885:
3886: /* If in \Q...\E, check for the end; if not, we have a literal */
3887:
1.1.1.4 ! misho 3888: if (inescq && c != CHAR_NULL)
1.1 misho 3889: {
3890: if (c == CHAR_BACKSLASH && ptr[1] == CHAR_E)
3891: {
3892: inescq = FALSE;
3893: ptr++;
3894: continue;
3895: }
3896: else
3897: {
3898: if (previous_callout != NULL)
3899: {
3900: if (lengthptr == NULL) /* Don't attempt in pre-compile phase */
3901: complete_callout(previous_callout, ptr, cd);
3902: previous_callout = NULL;
3903: }
3904: if ((options & PCRE_AUTO_CALLOUT) != 0)
3905: {
3906: previous_callout = code;
3907: code = auto_callout(code, ptr, cd);
3908: }
3909: goto NORMAL_CHAR;
3910: }
3911: }
3912:
3913: /* Fill in length of a previous callout, except when the next thing is
3914: a quantifier. */
3915:
3916: is_quantifier =
3917: c == CHAR_ASTERISK || c == CHAR_PLUS || c == CHAR_QUESTION_MARK ||
3918: (c == CHAR_LEFT_CURLY_BRACKET && is_counted_repeat(ptr+1));
3919:
3920: if (!is_quantifier && previous_callout != NULL &&
3921: after_manual_callout-- <= 0)
3922: {
3923: if (lengthptr == NULL) /* Don't attempt in pre-compile phase */
3924: complete_callout(previous_callout, ptr, cd);
3925: previous_callout = NULL;
3926: }
3927:
3928: /* In extended mode, skip white space and comments. */
3929:
3930: if ((options & PCRE_EXTENDED) != 0)
3931: {
1.1.1.2 misho 3932: if (MAX_255(*ptr) && (cd->ctypes[c] & ctype_space) != 0) continue;
1.1 misho 3933: if (c == CHAR_NUMBER_SIGN)
3934: {
3935: ptr++;
1.1.1.4 ! misho 3936: while (*ptr != CHAR_NULL)
1.1 misho 3937: {
3938: if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }
3939: ptr++;
1.1.1.2 misho 3940: #ifdef SUPPORT_UTF
3941: if (utf) FORWARDCHAR(ptr);
1.1 misho 3942: #endif
3943: }
1.1.1.4 ! misho 3944: if (*ptr != CHAR_NULL) continue;
1.1 misho 3945:
3946: /* Else fall through to handle end of string */
3947: c = 0;
3948: }
3949: }
3950:
3951: /* No auto callout for quantifiers. */
3952:
3953: if ((options & PCRE_AUTO_CALLOUT) != 0 && !is_quantifier)
3954: {
3955: previous_callout = code;
3956: code = auto_callout(code, ptr, cd);
3957: }
3958:
3959: switch(c)
3960: {
3961: /* ===================================================================*/
3962: case 0: /* The branch terminates at string end */
3963: case CHAR_VERTICAL_LINE: /* or | or ) */
3964: case CHAR_RIGHT_PARENTHESIS:
1.1.1.2 misho 3965: *firstcharptr = firstchar;
1.1.1.4 ! misho 3966: *firstcharflagsptr = firstcharflags;
1.1.1.2 misho 3967: *reqcharptr = reqchar;
1.1.1.4 ! misho 3968: *reqcharflagsptr = reqcharflags;
1.1 misho 3969: *codeptr = code;
3970: *ptrptr = ptr;
3971: if (lengthptr != NULL)
3972: {
3973: if (OFLOW_MAX - *lengthptr < code - last_code)
3974: {
3975: *errorcodeptr = ERR20;
3976: goto FAILED;
3977: }
3978: *lengthptr += (int)(code - last_code); /* To include callout length */
3979: DPRINTF((">> end branch\n"));
3980: }
3981: return TRUE;
3982:
3983:
3984: /* ===================================================================*/
3985: /* Handle single-character metacharacters. In multiline mode, ^ disables
3986: the setting of any following char as a first character. */
3987:
3988: case CHAR_CIRCUMFLEX_ACCENT:
3989: previous = NULL;
3990: if ((options & PCRE_MULTILINE) != 0)
3991: {
1.1.1.4 ! misho 3992: if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
1.1 misho 3993: *code++ = OP_CIRCM;
3994: }
3995: else *code++ = OP_CIRC;
3996: break;
3997:
3998: case CHAR_DOLLAR_SIGN:
3999: previous = NULL;
4000: *code++ = ((options & PCRE_MULTILINE) != 0)? OP_DOLLM : OP_DOLL;
4001: break;
4002:
4003: /* There can never be a first char if '.' is first, whatever happens about
1.1.1.2 misho 4004: repeats. The value of reqchar doesn't change either. */
1.1 misho 4005:
4006: case CHAR_DOT:
1.1.1.4 ! misho 4007: if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
1.1.1.2 misho 4008: zerofirstchar = firstchar;
1.1.1.4 ! misho 4009: zerofirstcharflags = firstcharflags;
1.1.1.2 misho 4010: zeroreqchar = reqchar;
1.1.1.4 ! misho 4011: zeroreqcharflags = reqcharflags;
1.1 misho 4012: previous = code;
4013: *code++ = ((options & PCRE_DOTALL) != 0)? OP_ALLANY: OP_ANY;
4014: break;
4015:
4016:
4017: /* ===================================================================*/
4018: /* Character classes. If the included characters are all < 256, we build a
4019: 32-byte bitmap of the permitted characters, except in the special case
4020: where there is only one such character. For negated classes, we build the
4021: map as usual, then invert it at the end. However, we use a different opcode
4022: so that data characters > 255 can be handled correctly.
4023:
4024: If the class contains characters outside the 0-255 range, a different
4025: opcode is compiled. It may optionally have a bit map for characters < 256,
4026: but those above are are explicitly listed afterwards. A flag byte tells
4027: whether the bitmap is present, and whether this is a negated class or not.
4028:
4029: In JavaScript compatibility mode, an isolated ']' causes an error. In
4030: default (Perl) mode, it is treated as a data character. */
4031:
4032: case CHAR_RIGHT_SQUARE_BRACKET:
4033: if ((cd->external_options & PCRE_JAVASCRIPT_COMPAT) != 0)
4034: {
4035: *errorcodeptr = ERR64;
4036: goto FAILED;
4037: }
4038: goto NORMAL_CHAR;
4039:
4040: case CHAR_LEFT_SQUARE_BRACKET:
4041: previous = code;
4042:
4043: /* PCRE supports POSIX class stuff inside a class. Perl gives an error if
4044: they are encountered at the top level, so we'll do that too. */
4045:
4046: if ((ptr[1] == CHAR_COLON || ptr[1] == CHAR_DOT ||
4047: ptr[1] == CHAR_EQUALS_SIGN) &&
4048: check_posix_syntax(ptr, &tempptr))
4049: {
4050: *errorcodeptr = (ptr[1] == CHAR_COLON)? ERR13 : ERR31;
4051: goto FAILED;
4052: }
4053:
4054: /* If the first character is '^', set the negation flag and skip it. Also,
4055: if the first few characters (either before or after ^) are \Q\E or \E we
4056: skip them too. This makes for compatibility with Perl. */
4057:
4058: negate_class = FALSE;
4059: for (;;)
4060: {
4061: c = *(++ptr);
4062: if (c == CHAR_BACKSLASH)
4063: {
4064: if (ptr[1] == CHAR_E)
4065: ptr++;
1.1.1.2 misho 4066: else if (STRNCMP_UC_C8(ptr + 1, STR_Q STR_BACKSLASH STR_E, 3) == 0)
1.1 misho 4067: ptr += 3;
4068: else
4069: break;
4070: }
4071: else if (!negate_class && c == CHAR_CIRCUMFLEX_ACCENT)
4072: negate_class = TRUE;
4073: else break;
4074: }
4075:
4076: /* Empty classes are allowed in JavaScript compatibility mode. Otherwise,
4077: an initial ']' is taken as a data character -- the code below handles
4078: that. In JS mode, [] must always fail, so generate OP_FAIL, whereas
4079: [^] must match any character, so generate OP_ALLANY. */
4080:
4081: if (c == CHAR_RIGHT_SQUARE_BRACKET &&
4082: (cd->external_options & PCRE_JAVASCRIPT_COMPAT) != 0)
4083: {
4084: *code++ = negate_class? OP_ALLANY : OP_FAIL;
1.1.1.4 ! misho 4085: if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
1.1.1.2 misho 4086: zerofirstchar = firstchar;
1.1.1.4 ! misho 4087: zerofirstcharflags = firstcharflags;
1.1 misho 4088: break;
4089: }
4090:
4091: /* If a class contains a negative special such as \S, we need to flip the
4092: negation flag at the end, so that support for characters > 255 works
4093: correctly (they are all included in the class). */
4094:
4095: should_flip_negation = FALSE;
4096:
1.1.1.4 ! misho 4097: /* For optimization purposes, we track some properties of the class:
! 4098: class_has_8bitchar will be non-zero if the class contains at least one <
! 4099: 256 character; class_one_char will be 1 if the class contains just one
! 4100: character. */
1.1 misho 4101:
1.1.1.2 misho 4102: class_has_8bitchar = 0;
1.1.1.4 ! misho 4103: class_one_char = 0;
1.1 misho 4104:
4105: /* Initialize the 32-char bit map to all zeros. We build the map in a
1.1.1.4 ! misho 4106: temporary bit of memory, in case the class contains fewer than two
! 4107: 8-bit characters because in that case the compiled code doesn't use the bit
! 4108: map. */
1.1 misho 4109:
1.1.1.2 misho 4110: memset(classbits, 0, 32 * sizeof(pcre_uint8));
1.1 misho 4111:
1.1.1.2 misho 4112: #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
1.1.1.4 ! misho 4113: xclass = FALSE;
! 4114: class_uchardata = code + LINK_SIZE + 2; /* For XCLASS items */
! 4115: class_uchardata_base = class_uchardata; /* Save the start */
1.1 misho 4116: #endif
4117:
4118: /* Process characters until ] is reached. By writing this as a "do" it
4119: means that an initial ] is taken as a data character. At the start of the
4120: loop, c contains the first byte of the character. */
4121:
1.1.1.4 ! misho 4122: if (c != CHAR_NULL) do
1.1 misho 4123: {
1.1.1.2 misho 4124: const pcre_uchar *oldptr;
1.1 misho 4125:
1.1.1.2 misho 4126: #ifdef SUPPORT_UTF
4127: if (utf && HAS_EXTRALEN(c))
1.1 misho 4128: { /* Braces are required because the */
4129: GETCHARLEN(c, ptr, ptr); /* macro generates multiple statements */
4130: }
1.1.1.2 misho 4131: #endif
1.1 misho 4132:
1.1.1.2 misho 4133: #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
4134: /* In the pre-compile phase, accumulate the length of any extra
1.1 misho 4135: data and reset the pointer. This is so that very large classes that
1.1.1.2 misho 4136: contain a zillion > 255 characters no longer overwrite the work space
1.1.1.4 ! misho 4137: (which is on the stack). We have to remember that there was XCLASS data,
! 4138: however. */
1.1 misho 4139:
1.1.1.4 ! misho 4140: if (lengthptr != NULL && class_uchardata > class_uchardata_base)
1.1 misho 4141: {
1.1.1.4 ! misho 4142: xclass = TRUE;
1.1.1.2 misho 4143: *lengthptr += class_uchardata - class_uchardata_base;
4144: class_uchardata = class_uchardata_base;
1.1 misho 4145: }
4146: #endif
4147:
4148: /* Inside \Q...\E everything is literal except \E */
4149:
4150: if (inescq)
4151: {
4152: if (c == CHAR_BACKSLASH && ptr[1] == CHAR_E) /* If we are at \E */
4153: {
4154: inescq = FALSE; /* Reset literal state */
4155: ptr++; /* Skip the 'E' */
4156: continue; /* Carry on with next */
4157: }
4158: goto CHECK_RANGE; /* Could be range if \E follows */
4159: }
4160:
4161: /* Handle POSIX class names. Perl allows a negation extension of the
4162: form [:^name:]. A square bracket that doesn't match the syntax is
4163: treated as a literal. We also recognize the POSIX constructions
4164: [.ch.] and [=ch=] ("collating elements") and fault them, as Perl
4165: 5.6 and 5.8 do. */
4166:
4167: if (c == CHAR_LEFT_SQUARE_BRACKET &&
4168: (ptr[1] == CHAR_COLON || ptr[1] == CHAR_DOT ||
4169: ptr[1] == CHAR_EQUALS_SIGN) && check_posix_syntax(ptr, &tempptr))
4170: {
4171: BOOL local_negate = FALSE;
4172: int posix_class, taboffset, tabopt;
1.1.1.2 misho 4173: register const pcre_uint8 *cbits = cd->cbits;
4174: pcre_uint8 pbits[32];
1.1 misho 4175:
4176: if (ptr[1] != CHAR_COLON)
4177: {
4178: *errorcodeptr = ERR31;
4179: goto FAILED;
4180: }
4181:
4182: ptr += 2;
4183: if (*ptr == CHAR_CIRCUMFLEX_ACCENT)
4184: {
4185: local_negate = TRUE;
4186: should_flip_negation = TRUE; /* Note negative special */
4187: ptr++;
4188: }
4189:
4190: posix_class = check_posix_name(ptr, (int)(tempptr - ptr));
4191: if (posix_class < 0)
4192: {
4193: *errorcodeptr = ERR30;
4194: goto FAILED;
4195: }
4196:
4197: /* If matching is caseless, upper and lower are converted to
4198: alpha. This relies on the fact that the class table starts with
4199: alpha, lower, upper as the first 3 entries. */
4200:
4201: if ((options & PCRE_CASELESS) != 0 && posix_class <= 2)
4202: posix_class = 0;
4203:
4204: /* When PCRE_UCP is set, some of the POSIX classes are converted to
4205: different escape sequences that use Unicode properties. */
4206:
4207: #ifdef SUPPORT_UCP
4208: if ((options & PCRE_UCP) != 0)
4209: {
4210: int pc = posix_class + ((local_negate)? POSIX_SUBSIZE/2 : 0);
4211: if (posix_substitutes[pc] != NULL)
4212: {
4213: nestptr = tempptr + 1;
4214: ptr = posix_substitutes[pc] - 1;
4215: continue;
4216: }
4217: }
4218: #endif
4219: /* In the non-UCP case, we build the bit map for the POSIX class in a
4220: chunk of local store because we may be adding and subtracting from it,
4221: and we don't want to subtract bits that may be in the main map already.
4222: At the end we or the result into the bit map that is being built. */
4223:
4224: posix_class *= 3;
4225:
4226: /* Copy in the first table (always present) */
4227:
4228: memcpy(pbits, cbits + posix_class_maps[posix_class],
1.1.1.2 misho 4229: 32 * sizeof(pcre_uint8));
1.1 misho 4230:
4231: /* If there is a second table, add or remove it as required. */
4232:
4233: taboffset = posix_class_maps[posix_class + 1];
4234: tabopt = posix_class_maps[posix_class + 2];
4235:
4236: if (taboffset >= 0)
4237: {
4238: if (tabopt >= 0)
4239: for (c = 0; c < 32; c++) pbits[c] |= cbits[c + taboffset];
4240: else
4241: for (c = 0; c < 32; c++) pbits[c] &= ~cbits[c + taboffset];
4242: }
4243:
1.1.1.4 ! misho 4244: /* Now see if we need to remove any special characters. An option
1.1 misho 4245: value of 1 removes vertical space and 2 removes underscore. */
4246:
4247: if (tabopt < 0) tabopt = -tabopt;
4248: if (tabopt == 1) pbits[1] &= ~0x3c;
4249: else if (tabopt == 2) pbits[11] &= 0x7f;
4250:
4251: /* Add the POSIX table or its complement into the main table that is
4252: being built and we are done. */
4253:
4254: if (local_negate)
4255: for (c = 0; c < 32; c++) classbits[c] |= ~pbits[c];
4256: else
4257: for (c = 0; c < 32; c++) classbits[c] |= pbits[c];
4258:
4259: ptr = tempptr + 1;
1.1.1.4 ! misho 4260: /* Every class contains at least one < 256 character. */
1.1.1.2 misho 4261: class_has_8bitchar = 1;
4262: /* Every class contains at least two characters. */
1.1.1.4 ! misho 4263: class_one_char = 2;
1.1 misho 4264: continue; /* End of POSIX syntax handling */
4265: }
4266:
4267: /* Backslash may introduce a single character, or it may introduce one
4268: of the specials, which just set a flag. The sequence \b is a special
4269: case. Inside a class (and only there) it is treated as backspace. We
1.1.1.2 misho 4270: assume that other escapes have more than one character in them, so
1.1.1.4 ! misho 4271: speculatively set both class_has_8bitchar and class_one_char bigger
1.1.1.2 misho 4272: than one. Unrecognized escapes fall through and are either treated
4273: as literal characters (by default), or are faulted if
1.1 misho 4274: PCRE_EXTRA is set. */
4275:
4276: if (c == CHAR_BACKSLASH)
4277: {
1.1.1.4 ! misho 4278: escape = check_escape(&ptr, &ec, errorcodeptr, cd->bracount, options,
! 4279: TRUE);
1.1 misho 4280: if (*errorcodeptr != 0) goto FAILED;
1.1.1.4 ! misho 4281: if (escape == 0) c = ec;
! 4282: else if (escape == ESC_b) c = CHAR_BS; /* \b is backspace in a class */
! 4283: else if (escape == ESC_N) /* \N is not supported in a class */
1.1 misho 4284: {
4285: *errorcodeptr = ERR71;
4286: goto FAILED;
4287: }
1.1.1.4 ! misho 4288: else if (escape == ESC_Q) /* Handle start of quoted string */
1.1 misho 4289: {
4290: if (ptr[1] == CHAR_BACKSLASH && ptr[2] == CHAR_E)
4291: {
4292: ptr += 2; /* avoid empty string */
4293: }
4294: else inescq = TRUE;
4295: continue;
4296: }
1.1.1.4 ! misho 4297: else if (escape == ESC_E) continue; /* Ignore orphan \E */
1.1 misho 4298:
1.1.1.4 ! misho 4299: else
1.1 misho 4300: {
1.1.1.2 misho 4301: register const pcre_uint8 *cbits = cd->cbits;
4302: /* Every class contains at least two < 256 characters. */
4303: class_has_8bitchar++;
4304: /* Every class contains at least two characters. */
1.1.1.4 ! misho 4305: class_one_char += 2;
1.1 misho 4306:
1.1.1.4 ! misho 4307: switch (escape)
1.1 misho 4308: {
4309: #ifdef SUPPORT_UCP
4310: case ESC_du: /* These are the values given for \d etc */
4311: case ESC_DU: /* when PCRE_UCP is set. We replace the */
4312: case ESC_wu: /* escape sequence with an appropriate \p */
4313: case ESC_WU: /* or \P to test Unicode properties instead */
4314: case ESC_su: /* of the default ASCII testing. */
4315: case ESC_SU:
4316: nestptr = ptr;
1.1.1.4 ! misho 4317: ptr = substitutes[escape - ESC_DU] - 1; /* Just before substitute */
1.1.1.2 misho 4318: class_has_8bitchar--; /* Undo! */
1.1 misho 4319: continue;
4320: #endif
4321: case ESC_d:
4322: for (c = 0; c < 32; c++) classbits[c] |= cbits[c+cbit_digit];
4323: continue;
4324:
4325: case ESC_D:
4326: should_flip_negation = TRUE;
4327: for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_digit];
4328: continue;
4329:
4330: case ESC_w:
4331: for (c = 0; c < 32; c++) classbits[c] |= cbits[c+cbit_word];
4332: continue;
4333:
4334: case ESC_W:
4335: should_flip_negation = TRUE;
4336: for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_word];
4337: continue;
4338:
4339: /* Perl 5.004 onwards omits VT from \s, but we must preserve it
4340: if it was previously set by something earlier in the character
1.1.1.4 ! misho 4341: class. Luckily, the value of CHAR_VT is 0x0b in both ASCII and
! 4342: EBCDIC, so we lazily just adjust the appropriate bit. */
1.1 misho 4343:
4344: case ESC_s:
4345: classbits[0] |= cbits[cbit_space];
4346: classbits[1] |= cbits[cbit_space+1] & ~0x08;
4347: for (c = 2; c < 32; c++) classbits[c] |= cbits[c+cbit_space];
4348: continue;
4349:
4350: case ESC_S:
4351: should_flip_negation = TRUE;
4352: for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_space];
4353: classbits[1] |= 0x08; /* Perl 5.004 onwards omits VT from \s */
4354: continue;
4355:
1.1.1.4 ! misho 4356: /* The rest apply in both UCP and non-UCP cases. */
! 4357:
! 4358: case ESC_h:
! 4359: (void)add_list_to_class(classbits, &class_uchardata, options, cd,
! 4360: PRIV(hspace_list), NOTACHAR);
1.1 misho 4361: continue;
4362:
4363: case ESC_H:
1.1.1.4 ! misho 4364: (void)add_not_list_to_class(classbits, &class_uchardata, options,
! 4365: cd, PRIV(hspace_list));
1.1 misho 4366: continue;
4367:
4368: case ESC_v:
1.1.1.4 ! misho 4369: (void)add_list_to_class(classbits, &class_uchardata, options, cd,
! 4370: PRIV(vspace_list), NOTACHAR);
1.1 misho 4371: continue;
4372:
4373: case ESC_V:
1.1.1.4 ! misho 4374: (void)add_not_list_to_class(classbits, &class_uchardata, options,
! 4375: cd, PRIV(vspace_list));
1.1 misho 4376: continue;
4377:
4378: #ifdef SUPPORT_UCP
4379: case ESC_p:
4380: case ESC_P:
4381: {
4382: BOOL negated;
1.1.1.4 ! misho 4383: unsigned int ptype = 0, pdata = 0;
! 4384: if (!get_ucp(&ptr, &negated, &ptype, &pdata, errorcodeptr))
! 4385: goto FAILED;
! 4386: *class_uchardata++ = ((escape == ESC_p) != negated)?
1.1 misho 4387: XCL_PROP : XCL_NOTPROP;
1.1.1.2 misho 4388: *class_uchardata++ = ptype;
4389: *class_uchardata++ = pdata;
4390: class_has_8bitchar--; /* Undo! */
1.1 misho 4391: continue;
4392: }
4393: #endif
4394: /* Unrecognized escapes are faulted if PCRE is running in its
4395: strict mode. By default, for compatibility with Perl, they are
4396: treated as literals. */
4397:
4398: default:
4399: if ((options & PCRE_EXTRA) != 0)
4400: {
4401: *errorcodeptr = ERR7;
4402: goto FAILED;
4403: }
1.1.1.2 misho 4404: class_has_8bitchar--; /* Undo the speculative increase. */
1.1.1.4 ! misho 4405: class_one_char -= 2; /* Undo the speculative increase. */
1.1.1.2 misho 4406: c = *ptr; /* Get the final character and fall through */
1.1 misho 4407: break;
4408: }
4409: }
4410:
1.1.1.4 ! misho 4411: /* Fall through if the escape just defined a single character (c >= 0).
! 4412: This may be greater than 256. */
! 4413:
! 4414: escape = 0;
1.1 misho 4415:
4416: } /* End of backslash handling */
4417:
1.1.1.4 ! misho 4418: /* A character may be followed by '-' to form a range. However, Perl does
! 4419: not permit ']' to be the end of the range. A '-' character at the end is
! 4420: treated as a literal. Perl ignores orphaned \E sequences entirely. The
! 4421: code for handling \Q and \E is messy. */
1.1 misho 4422:
4423: CHECK_RANGE:
4424: while (ptr[1] == CHAR_BACKSLASH && ptr[2] == CHAR_E)
4425: {
4426: inescq = FALSE;
4427: ptr += 2;
4428: }
4429: oldptr = ptr;
4430:
1.1.1.4 ! misho 4431: /* Remember if \r or \n were explicitly used */
1.1 misho 4432:
4433: if (c == CHAR_CR || c == CHAR_NL) cd->external_flags |= PCRE_HASCRORLF;
4434:
4435: /* Check for range */
4436:
4437: if (!inescq && ptr[1] == CHAR_MINUS)
4438: {
1.1.1.4 ! misho 4439: pcre_uint32 d;
1.1 misho 4440: ptr += 2;
4441: while (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_E) ptr += 2;
4442:
4443: /* If we hit \Q (not followed by \E) at this point, go into escaped
4444: mode. */
4445:
4446: while (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_Q)
4447: {
4448: ptr += 2;
4449: if (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_E)
4450: { ptr += 2; continue; }
4451: inescq = TRUE;
4452: break;
4453: }
4454:
1.1.1.4 ! misho 4455: /* Minus (hyphen) at the end of a class is treated as a literal, so put
! 4456: back the pointer and jump to handle the character that preceded it. */
! 4457:
! 4458: if (*ptr == CHAR_NULL || (!inescq && *ptr == CHAR_RIGHT_SQUARE_BRACKET))
1.1 misho 4459: {
4460: ptr = oldptr;
1.1.1.4 ! misho 4461: goto CLASS_SINGLE_CHARACTER;
1.1 misho 4462: }
4463:
1.1.1.4 ! misho 4464: /* Otherwise, we have a potential range; pick up the next character */
! 4465:
1.1.1.2 misho 4466: #ifdef SUPPORT_UTF
4467: if (utf)
1.1 misho 4468: { /* Braces are required because the */
4469: GETCHARLEN(d, ptr, ptr); /* macro generates multiple statements */
4470: }
4471: else
4472: #endif
4473: d = *ptr; /* Not UTF-8 mode */
4474:
4475: /* The second part of a range can be a single-character escape, but
4476: not any of the other escapes. Perl 5.6 treats a hyphen as a literal
4477: in such circumstances. */
4478:
4479: if (!inescq && d == CHAR_BACKSLASH)
4480: {
1.1.1.4 ! misho 4481: int descape;
! 4482: descape = check_escape(&ptr, &d, errorcodeptr, cd->bracount, options, TRUE);
1.1 misho 4483: if (*errorcodeptr != 0) goto FAILED;
4484:
1.1.1.4 ! misho 4485: /* \b is backspace; any other special means the '-' was literal. */
1.1 misho 4486:
1.1.1.4 ! misho 4487: if (descape != 0)
1.1 misho 4488: {
1.1.1.4 ! misho 4489: if (descape == ESC_b) d = CHAR_BS; else
1.1 misho 4490: {
4491: ptr = oldptr;
1.1.1.4 ! misho 4492: goto CLASS_SINGLE_CHARACTER; /* A few lines below */
1.1 misho 4493: }
4494: }
4495: }
4496:
4497: /* Check that the two values are in the correct order. Optimize
1.1.1.4 ! misho 4498: one-character ranges. */
1.1 misho 4499:
4500: if (d < c)
4501: {
4502: *errorcodeptr = ERR8;
4503: goto FAILED;
4504: }
1.1.1.4 ! misho 4505: if (d == c) goto CLASS_SINGLE_CHARACTER; /* A few lines below */
1.1 misho 4506:
1.1.1.4 ! misho 4507: /* We have found a character range, so single character optimizations
! 4508: cannot be done anymore. Any value greater than 1 indicates that there
! 4509: is more than one character. */
1.1 misho 4510:
1.1.1.4 ! misho 4511: class_one_char = 2;
! 4512:
! 4513: /* Remember an explicit \r or \n, and add the range to the class. */
1.1 misho 4514:
4515: if (d == CHAR_CR || d == CHAR_NL) cd->external_flags |= PCRE_HASCRORLF;
4516:
1.1.1.4 ! misho 4517: class_has_8bitchar +=
! 4518: add_to_class(classbits, &class_uchardata, options, cd, c, d);
1.1 misho 4519:
1.1.1.4 ! misho 4520: continue; /* Go get the next char in the class */
! 4521: }
1.1 misho 4522:
1.1.1.4 ! misho 4523: /* Handle a single character - we can get here for a normal non-escape
! 4524: char, or after \ that introduces a single character or for an apparent
! 4525: range that isn't. Only the value 1 matters for class_one_char, so don't
! 4526: increase it if it is already 2 or more ... just in case there's a class
! 4527: with a zillion characters in it. */
! 4528:
! 4529: CLASS_SINGLE_CHARACTER:
! 4530: if (class_one_char < 2) class_one_char++;
! 4531:
! 4532: /* If class_one_char is 1, we have the first single character in the
! 4533: class, and there have been no prior ranges, or XCLASS items generated by
! 4534: escapes. If this is the final character in the class, we can optimize by
! 4535: turning the item into a 1-character OP_CHAR[I] if it's positive, or
! 4536: OP_NOT[I] if it's negative. In the positive case, it can cause firstchar
! 4537: to be set. Otherwise, there can be no first char if this item is first,
! 4538: whatever repeat count may follow. In the case of reqchar, save the
! 4539: previous value for reinstating. */
1.1 misho 4540:
1.1.1.4 ! misho 4541: if (class_one_char == 1 && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
! 4542: {
! 4543: ptr++;
! 4544: zeroreqchar = reqchar;
! 4545: zeroreqcharflags = reqcharflags;
1.1 misho 4546:
1.1.1.4 ! misho 4547: if (negate_class)
! 4548: {
! 4549: #ifdef SUPPORT_UCP
! 4550: int d;
1.1.1.2 misho 4551: #endif
1.1.1.4 ! misho 4552: if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
! 4553: zerofirstchar = firstchar;
! 4554: zerofirstcharflags = firstcharflags;
1.1 misho 4555:
1.1.1.4 ! misho 4556: /* For caseless UTF-8 mode when UCP support is available, check
! 4557: whether this character has more than one other case. If so, generate
! 4558: a special OP_NOTPROP item instead of OP_NOTI. */
1.1 misho 4559:
4560: #ifdef SUPPORT_UCP
1.1.1.4 ! misho 4561: if (utf && (options & PCRE_CASELESS) != 0 &&
! 4562: (d = UCD_CASESET(c)) != 0)
1.1.1.2 misho 4563: {
1.1.1.4 ! misho 4564: *code++ = OP_NOTPROP;
! 4565: *code++ = PT_CLIST;
! 4566: *code++ = d;
1.1.1.2 misho 4567: }
4568: else
1.1.1.4 ! misho 4569: #endif
! 4570: /* Char has only one other case, or UCP not available */
1.1 misho 4571:
4572: {
1.1.1.4 ! misho 4573: *code++ = ((options & PCRE_CASELESS) != 0)? OP_NOTI: OP_NOT;
! 4574: #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
! 4575: if (utf && c > MAX_VALUE_FOR_SINGLE_CHAR)
! 4576: code += PRIV(ord2utf)(c, code);
! 4577: else
! 4578: #endif
! 4579: *code++ = c;
1.1 misho 4580: }
4581:
1.1.1.4 ! misho 4582: /* We are finished with this character class */
1.1 misho 4583:
1.1.1.4 ! misho 4584: goto END_CLASS;
1.1.1.2 misho 4585: }
4586:
4587: /* For a single, positive character, get the value into mcbuffer, and
4588: then we can handle this with the normal one-character code. */
4589:
1.1.1.4 ! misho 4590: #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
1.1.1.2 misho 4591: if (utf && c > MAX_VALUE_FOR_SINGLE_CHAR)
4592: mclength = PRIV(ord2utf)(c, mcbuffer);
4593: else
4594: #endif
4595: {
4596: mcbuffer[0] = c;
4597: mclength = 1;
4598: }
4599: goto ONE_CHAR;
4600: } /* End of 1-char optimization */
4601:
1.1.1.4 ! misho 4602: /* There is more than one character in the class, or an XCLASS item
! 4603: has been generated. Add this character to the class. */
1.1.1.2 misho 4604:
1.1.1.4 ! misho 4605: class_has_8bitchar +=
! 4606: add_to_class(classbits, &class_uchardata, options, cd, c, c);
1.1 misho 4607: }
4608:
4609: /* Loop until ']' reached. This "while" is the end of the "do" far above.
4610: If we are at the end of an internal nested string, revert to the outer
4611: string. */
4612:
1.1.1.4 ! misho 4613: while (((c = *(++ptr)) != CHAR_NULL ||
1.1 misho 4614: (nestptr != NULL &&
1.1.1.4 ! misho 4615: (ptr = nestptr, nestptr = NULL, c = *(++ptr)) != CHAR_NULL)) &&
1.1 misho 4616: (c != CHAR_RIGHT_SQUARE_BRACKET || inescq));
4617:
4618: /* Check for missing terminating ']' */
4619:
1.1.1.4 ! misho 4620: if (c == CHAR_NULL)
1.1 misho 4621: {
4622: *errorcodeptr = ERR6;
4623: goto FAILED;
4624: }
4625:
1.1.1.4 ! misho 4626: /* We will need an XCLASS if data has been placed in class_uchardata. In
! 4627: the second phase this is a sufficient test. However, in the pre-compile
! 4628: phase, class_uchardata gets emptied to prevent workspace overflow, so it
! 4629: only if the very last character in the class needs XCLASS will it contain
! 4630: anything at this point. For this reason, xclass gets set TRUE above when
! 4631: uchar_classdata is emptied, and that's why this code is the way it is here
! 4632: instead of just doing a test on class_uchardata below. */
! 4633:
! 4634: #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
! 4635: if (class_uchardata > class_uchardata_base) xclass = TRUE;
! 4636: #endif
! 4637:
1.1.1.2 misho 4638: /* If this is the first thing in the branch, there can be no first char
4639: setting, whatever the repeat count. Any reqchar setting must remain
4640: unchanged after any kind of repeat. */
4641:
1.1.1.4 ! misho 4642: if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
1.1.1.2 misho 4643: zerofirstchar = firstchar;
1.1.1.4 ! misho 4644: zerofirstcharflags = firstcharflags;
1.1.1.2 misho 4645: zeroreqchar = reqchar;
1.1.1.4 ! misho 4646: zeroreqcharflags = reqcharflags;
1.1 misho 4647:
4648: /* If there are characters with values > 255, we have to compile an
4649: extended class, with its own opcode, unless there was a negated special
4650: such as \S in the class, and PCRE_UCP is not set, because in that case all
4651: characters > 255 are in the class, so any that were explicitly given as
4652: well can be ignored. If (when there are explicit characters > 255 that must
4653: be listed) there are no characters < 256, we can omit the bitmap in the
4654: actual compiled code. */
4655:
1.1.1.2 misho 4656: #ifdef SUPPORT_UTF
4657: if (xclass && (!should_flip_negation || (options & PCRE_UCP) != 0))
4658: #elif !defined COMPILE_PCRE8
4659: if (xclass && !should_flip_negation)
4660: #endif
4661: #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
1.1 misho 4662: {
1.1.1.2 misho 4663: *class_uchardata++ = XCL_END; /* Marks the end of extra data */
1.1 misho 4664: *code++ = OP_XCLASS;
4665: code += LINK_SIZE;
1.1.1.2 misho 4666: *code = negate_class? XCL_NOT:0;
1.1 misho 4667:
4668: /* If the map is required, move up the extra data to make room for it;
4669: otherwise just move the code pointer to the end of the extra data. */
4670:
1.1.1.2 misho 4671: if (class_has_8bitchar > 0)
1.1 misho 4672: {
4673: *code++ |= XCL_MAP;
1.1.1.2 misho 4674: memmove(code + (32 / sizeof(pcre_uchar)), code,
4675: IN_UCHARS(class_uchardata - code));
1.1 misho 4676: memcpy(code, classbits, 32);
1.1.1.2 misho 4677: code = class_uchardata + (32 / sizeof(pcre_uchar));
1.1 misho 4678: }
1.1.1.2 misho 4679: else code = class_uchardata;
1.1 misho 4680:
4681: /* Now fill in the complete length of the item */
4682:
4683: PUT(previous, 1, (int)(code - previous));
4684: break; /* End of class handling */
4685: }
4686: #endif
4687:
4688: /* If there are no characters > 255, or they are all to be included or
4689: excluded, set the opcode to OP_CLASS or OP_NCLASS, depending on whether the
4690: whole class was negated and whether there were negative specials such as \S
4691: (non-UCP) in the class. Then copy the 32-byte map into the code vector,
4692: negating it if necessary. */
4693:
4694: *code++ = (negate_class == should_flip_negation) ? OP_CLASS : OP_NCLASS;
1.1.1.2 misho 4695: if (lengthptr == NULL) /* Save time in the pre-compile phase */
1.1 misho 4696: {
1.1.1.2 misho 4697: if (negate_class)
4698: for (c = 0; c < 32; c++) classbits[c] = ~classbits[c];
1.1 misho 4699: memcpy(code, classbits, 32);
4700: }
1.1.1.2 misho 4701: code += 32 / sizeof(pcre_uchar);
1.1.1.4 ! misho 4702:
! 4703: END_CLASS:
1.1 misho 4704: break;
4705:
4706:
4707: /* ===================================================================*/
4708: /* Various kinds of repeat; '{' is not necessarily a quantifier, but this
4709: has been tested above. */
4710:
4711: case CHAR_LEFT_CURLY_BRACKET:
4712: if (!is_quantifier) goto NORMAL_CHAR;
4713: ptr = read_repeat_counts(ptr+1, &repeat_min, &repeat_max, errorcodeptr);
4714: if (*errorcodeptr != 0) goto FAILED;
4715: goto REPEAT;
4716:
4717: case CHAR_ASTERISK:
4718: repeat_min = 0;
4719: repeat_max = -1;
4720: goto REPEAT;
4721:
4722: case CHAR_PLUS:
4723: repeat_min = 1;
4724: repeat_max = -1;
4725: goto REPEAT;
4726:
4727: case CHAR_QUESTION_MARK:
4728: repeat_min = 0;
4729: repeat_max = 1;
4730:
4731: REPEAT:
4732: if (previous == NULL)
4733: {
4734: *errorcodeptr = ERR9;
4735: goto FAILED;
4736: }
4737:
4738: if (repeat_min == 0)
4739: {
1.1.1.2 misho 4740: firstchar = zerofirstchar; /* Adjust for zero repeat */
1.1.1.4 ! misho 4741: firstcharflags = zerofirstcharflags;
1.1.1.2 misho 4742: reqchar = zeroreqchar; /* Ditto */
1.1.1.4 ! misho 4743: reqcharflags = zeroreqcharflags;
1.1 misho 4744: }
4745:
4746: /* Remember whether this is a variable length repeat */
4747:
4748: reqvary = (repeat_min == repeat_max)? 0 : REQ_VARY;
4749:
4750: op_type = 0; /* Default single-char op codes */
4751: possessive_quantifier = FALSE; /* Default not possessive quantifier */
4752:
4753: /* Save start of previous item, in case we have to move it up in order to
4754: insert something before it. */
4755:
4756: tempcode = previous;
4757:
4758: /* If the next character is '+', we have a possessive quantifier. This
4759: implies greediness, whatever the setting of the PCRE_UNGREEDY option.
4760: If the next character is '?' this is a minimizing repeat, by default,
4761: but if PCRE_UNGREEDY is set, it works the other way round. We change the
4762: repeat type to the non-default. */
4763:
4764: if (ptr[1] == CHAR_PLUS)
4765: {
4766: repeat_type = 0; /* Force greedy */
4767: possessive_quantifier = TRUE;
4768: ptr++;
4769: }
4770: else if (ptr[1] == CHAR_QUESTION_MARK)
4771: {
4772: repeat_type = greedy_non_default;
4773: ptr++;
4774: }
4775: else repeat_type = greedy_default;
4776:
4777: /* If previous was a recursion call, wrap it in atomic brackets so that
4778: previous becomes the atomic group. All recursions were so wrapped in the
4779: past, but it no longer happens for non-repeated recursions. In fact, the
4780: repeated ones could be re-implemented independently so as not to need this,
4781: but for the moment we rely on the code for repeating groups. */
4782:
4783: if (*previous == OP_RECURSE)
4784: {
1.1.1.2 misho 4785: memmove(previous + 1 + LINK_SIZE, previous, IN_UCHARS(1 + LINK_SIZE));
1.1 misho 4786: *previous = OP_ONCE;
4787: PUT(previous, 1, 2 + 2*LINK_SIZE);
4788: previous[2 + 2*LINK_SIZE] = OP_KET;
4789: PUT(previous, 3 + 2*LINK_SIZE, 2 + 2*LINK_SIZE);
4790: code += 2 + 2 * LINK_SIZE;
4791: length_prevgroup = 3 + 3*LINK_SIZE;
4792:
4793: /* When actually compiling, we need to check whether this was a forward
4794: reference, and if so, adjust the offset. */
4795:
4796: if (lengthptr == NULL && cd->hwm >= cd->start_workspace + LINK_SIZE)
4797: {
4798: int offset = GET(cd->hwm, -LINK_SIZE);
4799: if (offset == previous + 1 - cd->start_code)
4800: PUT(cd->hwm, -LINK_SIZE, offset + 1 + LINK_SIZE);
4801: }
4802: }
4803:
4804: /* Now handle repetition for the different types of item. */
4805:
1.1.1.3 misho 4806: /* If previous was a character or negated character match, abolish the item
4807: and generate a repeat item instead. If a char item has a minimum of more
4808: than one, ensure that it is set in reqchar - it might not be if a sequence
4809: such as x{3} is the first thing in a branch because the x will have gone
4810: into firstchar instead. */
4811:
4812: if (*previous == OP_CHAR || *previous == OP_CHARI
4813: || *previous == OP_NOT || *previous == OP_NOTI)
4814: {
4815: switch (*previous)
4816: {
4817: default: /* Make compiler happy. */
4818: case OP_CHAR: op_type = OP_STAR - OP_STAR; break;
4819: case OP_CHARI: op_type = OP_STARI - OP_STAR; break;
4820: case OP_NOT: op_type = OP_NOTSTAR - OP_STAR; break;
4821: case OP_NOTI: op_type = OP_NOTSTARI - OP_STAR; break;
4822: }
1.1 misho 4823:
1.1.1.2 misho 4824: /* Deal with UTF characters that take up more than one character. It's
1.1 misho 4825: easier to write this out separately than try to macrify it. Use c to
1.1.1.2 misho 4826: hold the length of the character in bytes, plus UTF_LENGTH to flag that
4827: it's a length rather than a small character. */
1.1 misho 4828:
1.1.1.4 ! misho 4829: #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
1.1.1.2 misho 4830: if (utf && NOT_FIRSTCHAR(code[-1]))
1.1 misho 4831: {
1.1.1.2 misho 4832: pcre_uchar *lastchar = code - 1;
4833: BACKCHAR(lastchar);
1.1 misho 4834: c = (int)(code - lastchar); /* Length of UTF-8 character */
1.1.1.2 misho 4835: memcpy(utf_chars, lastchar, IN_UCHARS(c)); /* Save the char */
4836: c |= UTF_LENGTH; /* Flag c as a length */
1.1 misho 4837: }
4838: else
1.1.1.2 misho 4839: #endif /* SUPPORT_UTF */
1.1 misho 4840:
1.1.1.2 misho 4841: /* Handle the case of a single charater - either with no UTF support, or
4842: with UTF disabled, or for a single character UTF character. */
1.1 misho 4843: {
4844: c = code[-1];
1.1.1.3 misho 4845: if (*previous <= OP_CHARI && repeat_min > 1)
1.1.1.4 ! misho 4846: {
! 4847: reqchar = c;
! 4848: reqcharflags = req_caseopt | cd->req_varyopt;
! 4849: }
1.1 misho 4850: }
4851:
4852: /* If the repetition is unlimited, it pays to see if the next thing on
4853: the line is something that cannot possibly match this character. If so,
4854: automatically possessifying this item gains some performance in the case
4855: where the match fails. */
4856:
4857: if (!possessive_quantifier &&
4858: repeat_max < 0 &&
1.1.1.2 misho 4859: check_auto_possessive(previous, utf, ptr + 1, options, cd))
1.1 misho 4860: {
4861: repeat_type = 0; /* Force greedy */
4862: possessive_quantifier = TRUE;
4863: }
4864:
4865: goto OUTPUT_SINGLE_REPEAT; /* Code shared with single character types */
4866: }
4867:
4868: /* If previous was a character type match (\d or similar), abolish it and
4869: create a suitable repeat item. The code is shared with single-character
4870: repeats by setting op_type to add a suitable offset into repeat_type. Note
4871: the the Unicode property types will be present only when SUPPORT_UCP is
4872: defined, but we don't wrap the little bits of code here because it just
4873: makes it horribly messy. */
4874:
4875: else if (*previous < OP_EODN)
4876: {
1.1.1.2 misho 4877: pcre_uchar *oldcode;
1.1 misho 4878: int prop_type, prop_value;
4879: op_type = OP_TYPESTAR - OP_STAR; /* Use type opcodes */
4880: c = *previous;
4881:
4882: if (!possessive_quantifier &&
4883: repeat_max < 0 &&
1.1.1.2 misho 4884: check_auto_possessive(previous, utf, ptr + 1, options, cd))
1.1 misho 4885: {
4886: repeat_type = 0; /* Force greedy */
4887: possessive_quantifier = TRUE;
4888: }
4889:
4890: OUTPUT_SINGLE_REPEAT:
4891: if (*previous == OP_PROP || *previous == OP_NOTPROP)
4892: {
4893: prop_type = previous[1];
4894: prop_value = previous[2];
4895: }
4896: else prop_type = prop_value = -1;
4897:
4898: oldcode = code;
4899: code = previous; /* Usually overwrite previous item */
4900:
4901: /* If the maximum is zero then the minimum must also be zero; Perl allows
4902: this case, so we do too - by simply omitting the item altogether. */
4903:
4904: if (repeat_max == 0) goto END_REPEAT;
4905:
4906: /* Combine the op_type with the repeat_type */
4907:
4908: repeat_type += op_type;
4909:
4910: /* A minimum of zero is handled either as the special case * or ?, or as
4911: an UPTO, with the maximum given. */
4912:
4913: if (repeat_min == 0)
4914: {
4915: if (repeat_max == -1) *code++ = OP_STAR + repeat_type;
4916: else if (repeat_max == 1) *code++ = OP_QUERY + repeat_type;
4917: else
4918: {
4919: *code++ = OP_UPTO + repeat_type;
4920: PUT2INC(code, 0, repeat_max);
4921: }
4922: }
4923:
4924: /* A repeat minimum of 1 is optimized into some special cases. If the
4925: maximum is unlimited, we use OP_PLUS. Otherwise, the original item is
4926: left in place and, if the maximum is greater than 1, we use OP_UPTO with
4927: one less than the maximum. */
4928:
4929: else if (repeat_min == 1)
4930: {
4931: if (repeat_max == -1)
4932: *code++ = OP_PLUS + repeat_type;
4933: else
4934: {
4935: code = oldcode; /* leave previous item in place */
4936: if (repeat_max == 1) goto END_REPEAT;
4937: *code++ = OP_UPTO + repeat_type;
4938: PUT2INC(code, 0, repeat_max - 1);
4939: }
4940: }
4941:
4942: /* The case {n,n} is just an EXACT, while the general case {n,m} is
4943: handled as an EXACT followed by an UPTO. */
4944:
4945: else
4946: {
4947: *code++ = OP_EXACT + op_type; /* NB EXACT doesn't have repeat_type */
4948: PUT2INC(code, 0, repeat_min);
4949:
4950: /* If the maximum is unlimited, insert an OP_STAR. Before doing so,
4951: we have to insert the character for the previous code. For a repeated
4952: Unicode property match, there are two extra bytes that define the
4953: required property. In UTF-8 mode, long characters have their length in
1.1.1.2 misho 4954: c, with the UTF_LENGTH bit as a flag. */
1.1 misho 4955:
4956: if (repeat_max < 0)
4957: {
1.1.1.4 ! misho 4958: #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
1.1.1.2 misho 4959: if (utf && (c & UTF_LENGTH) != 0)
1.1 misho 4960: {
1.1.1.2 misho 4961: memcpy(code, utf_chars, IN_UCHARS(c & 7));
1.1 misho 4962: code += c & 7;
4963: }
4964: else
4965: #endif
4966: {
4967: *code++ = c;
4968: if (prop_type >= 0)
4969: {
4970: *code++ = prop_type;
4971: *code++ = prop_value;
4972: }
4973: }
4974: *code++ = OP_STAR + repeat_type;
4975: }
4976:
4977: /* Else insert an UPTO if the max is greater than the min, again
4978: preceded by the character, for the previously inserted code. If the
4979: UPTO is just for 1 instance, we can use QUERY instead. */
4980:
4981: else if (repeat_max != repeat_min)
4982: {
1.1.1.4 ! misho 4983: #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
1.1.1.2 misho 4984: if (utf && (c & UTF_LENGTH) != 0)
1.1 misho 4985: {
1.1.1.2 misho 4986: memcpy(code, utf_chars, IN_UCHARS(c & 7));
1.1 misho 4987: code += c & 7;
4988: }
4989: else
4990: #endif
4991: *code++ = c;
4992: if (prop_type >= 0)
4993: {
4994: *code++ = prop_type;
4995: *code++ = prop_value;
4996: }
4997: repeat_max -= repeat_min;
4998:
4999: if (repeat_max == 1)
5000: {
5001: *code++ = OP_QUERY + repeat_type;
5002: }
5003: else
5004: {
5005: *code++ = OP_UPTO + repeat_type;
5006: PUT2INC(code, 0, repeat_max);
5007: }
5008: }
5009: }
5010:
5011: /* The character or character type itself comes last in all cases. */
5012:
1.1.1.4 ! misho 5013: #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
1.1.1.2 misho 5014: if (utf && (c & UTF_LENGTH) != 0)
1.1 misho 5015: {
1.1.1.2 misho 5016: memcpy(code, utf_chars, IN_UCHARS(c & 7));
1.1 misho 5017: code += c & 7;
5018: }
5019: else
5020: #endif
5021: *code++ = c;
5022:
5023: /* For a repeated Unicode property match, there are two extra bytes that
5024: define the required property. */
5025:
5026: #ifdef SUPPORT_UCP
5027: if (prop_type >= 0)
5028: {
5029: *code++ = prop_type;
5030: *code++ = prop_value;
5031: }
5032: #endif
5033: }
5034:
5035: /* If previous was a character class or a back reference, we put the repeat
5036: stuff after it, but just skip the item if the repeat was {0,0}. */
5037:
5038: else if (*previous == OP_CLASS ||
5039: *previous == OP_NCLASS ||
1.1.1.2 misho 5040: #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
1.1 misho 5041: *previous == OP_XCLASS ||
5042: #endif
5043: *previous == OP_REF ||
5044: *previous == OP_REFI)
5045: {
5046: if (repeat_max == 0)
5047: {
5048: code = previous;
5049: goto END_REPEAT;
5050: }
5051:
5052: if (repeat_min == 0 && repeat_max == -1)
5053: *code++ = OP_CRSTAR + repeat_type;
5054: else if (repeat_min == 1 && repeat_max == -1)
5055: *code++ = OP_CRPLUS + repeat_type;
5056: else if (repeat_min == 0 && repeat_max == 1)
5057: *code++ = OP_CRQUERY + repeat_type;
5058: else
5059: {
5060: *code++ = OP_CRRANGE + repeat_type;
5061: PUT2INC(code, 0, repeat_min);
5062: if (repeat_max == -1) repeat_max = 0; /* 2-byte encoding for max */
5063: PUT2INC(code, 0, repeat_max);
5064: }
5065: }
5066:
5067: /* If previous was a bracket group, we may have to replicate it in certain
5068: cases. Note that at this point we can encounter only the "basic" bracket
5069: opcodes such as BRA and CBRA, as this is the place where they get converted
5070: into the more special varieties such as BRAPOS and SBRA. A test for >=
5071: OP_ASSERT and <= OP_COND includes ASSERT, ASSERT_NOT, ASSERTBACK,
5072: ASSERTBACK_NOT, ONCE, BRA, CBRA, and COND. Originally, PCRE did not allow
5073: repetition of assertions, but now it does, for Perl compatibility. */
5074:
5075: else if (*previous >= OP_ASSERT && *previous <= OP_COND)
5076: {
5077: register int i;
5078: int len = (int)(code - previous);
1.1.1.2 misho 5079: pcre_uchar *bralink = NULL;
5080: pcre_uchar *brazeroptr = NULL;
1.1 misho 5081:
5082: /* Repeating a DEFINE group is pointless, but Perl allows the syntax, so
5083: we just ignore the repeat. */
5084:
5085: if (*previous == OP_COND && previous[LINK_SIZE+1] == OP_DEF)
5086: goto END_REPEAT;
5087:
5088: /* There is no sense in actually repeating assertions. The only potential
5089: use of repetition is in cases when the assertion is optional. Therefore,
5090: if the minimum is greater than zero, just ignore the repeat. If the
5091: maximum is not not zero or one, set it to 1. */
5092:
5093: if (*previous < OP_ONCE) /* Assertion */
5094: {
5095: if (repeat_min > 0) goto END_REPEAT;
5096: if (repeat_max < 0 || repeat_max > 1) repeat_max = 1;
5097: }
5098:
5099: /* The case of a zero minimum is special because of the need to stick
5100: OP_BRAZERO in front of it, and because the group appears once in the
5101: data, whereas in other cases it appears the minimum number of times. For
5102: this reason, it is simplest to treat this case separately, as otherwise
5103: the code gets far too messy. There are several special subcases when the
5104: minimum is zero. */
5105:
5106: if (repeat_min == 0)
5107: {
5108: /* If the maximum is also zero, we used to just omit the group from the
5109: output altogether, like this:
5110:
5111: ** if (repeat_max == 0)
5112: ** {
5113: ** code = previous;
5114: ** goto END_REPEAT;
5115: ** }
5116:
5117: However, that fails when a group or a subgroup within it is referenced
5118: as a subroutine from elsewhere in the pattern, so now we stick in
5119: OP_SKIPZERO in front of it so that it is skipped on execution. As we
5120: don't have a list of which groups are referenced, we cannot do this
5121: selectively.
5122:
5123: If the maximum is 1 or unlimited, we just have to stick in the BRAZERO
5124: and do no more at this point. However, we do need to adjust any
5125: OP_RECURSE calls inside the group that refer to the group itself or any
5126: internal or forward referenced group, because the offset is from the
5127: start of the whole regex. Temporarily terminate the pattern while doing
5128: this. */
5129:
5130: if (repeat_max <= 1) /* Covers 0, 1, and unlimited */
5131: {
5132: *code = OP_END;
1.1.1.2 misho 5133: adjust_recurse(previous, 1, utf, cd, save_hwm);
5134: memmove(previous + 1, previous, IN_UCHARS(len));
1.1 misho 5135: code++;
5136: if (repeat_max == 0)
5137: {
5138: *previous++ = OP_SKIPZERO;
5139: goto END_REPEAT;
5140: }
5141: brazeroptr = previous; /* Save for possessive optimizing */
5142: *previous++ = OP_BRAZERO + repeat_type;
5143: }
5144:
5145: /* If the maximum is greater than 1 and limited, we have to replicate
5146: in a nested fashion, sticking OP_BRAZERO before each set of brackets.
5147: The first one has to be handled carefully because it's the original
5148: copy, which has to be moved up. The remainder can be handled by code
5149: that is common with the non-zero minimum case below. We have to
5150: adjust the value or repeat_max, since one less copy is required. Once
5151: again, we may have to adjust any OP_RECURSE calls inside the group. */
5152:
5153: else
5154: {
5155: int offset;
5156: *code = OP_END;
1.1.1.2 misho 5157: adjust_recurse(previous, 2 + LINK_SIZE, utf, cd, save_hwm);
5158: memmove(previous + 2 + LINK_SIZE, previous, IN_UCHARS(len));
1.1 misho 5159: code += 2 + LINK_SIZE;
5160: *previous++ = OP_BRAZERO + repeat_type;
5161: *previous++ = OP_BRA;
5162:
5163: /* We chain together the bracket offset fields that have to be
5164: filled in later when the ends of the brackets are reached. */
5165:
5166: offset = (bralink == NULL)? 0 : (int)(previous - bralink);
5167: bralink = previous;
5168: PUTINC(previous, 0, offset);
5169: }
5170:
5171: repeat_max--;
5172: }
5173:
5174: /* If the minimum is greater than zero, replicate the group as many
5175: times as necessary, and adjust the maximum to the number of subsequent
5176: copies that we need. If we set a first char from the group, and didn't
5177: set a required char, copy the latter from the former. If there are any
5178: forward reference subroutine calls in the group, there will be entries on
5179: the workspace list; replicate these with an appropriate increment. */
5180:
5181: else
5182: {
5183: if (repeat_min > 1)
5184: {
5185: /* In the pre-compile phase, we don't actually do the replication. We
5186: just adjust the length as if we had. Do some paranoid checks for
5187: potential integer overflow. The INT64_OR_DOUBLE type is a 64-bit
5188: integer type when available, otherwise double. */
5189:
5190: if (lengthptr != NULL)
5191: {
5192: int delta = (repeat_min - 1)*length_prevgroup;
5193: if ((INT64_OR_DOUBLE)(repeat_min - 1)*
5194: (INT64_OR_DOUBLE)length_prevgroup >
5195: (INT64_OR_DOUBLE)INT_MAX ||
5196: OFLOW_MAX - *lengthptr < delta)
5197: {
5198: *errorcodeptr = ERR20;
5199: goto FAILED;
5200: }
5201: *lengthptr += delta;
5202: }
5203:
5204: /* This is compiling for real. If there is a set first byte for
5205: the group, and we have not yet set a "required byte", set it. Make
5206: sure there is enough workspace for copying forward references before
5207: doing the copy. */
5208:
5209: else
5210: {
1.1.1.4 ! misho 5211: if (groupsetfirstchar && reqcharflags < 0)
! 5212: {
! 5213: reqchar = firstchar;
! 5214: reqcharflags = firstcharflags;
! 5215: }
1.1 misho 5216:
5217: for (i = 1; i < repeat_min; i++)
5218: {
1.1.1.2 misho 5219: pcre_uchar *hc;
5220: pcre_uchar *this_hwm = cd->hwm;
5221: memcpy(code, previous, IN_UCHARS(len));
1.1 misho 5222:
5223: while (cd->hwm > cd->start_workspace + cd->workspace_size -
5224: WORK_SIZE_SAFETY_MARGIN - (this_hwm - save_hwm))
5225: {
5226: int save_offset = save_hwm - cd->start_workspace;
5227: int this_offset = this_hwm - cd->start_workspace;
5228: *errorcodeptr = expand_workspace(cd);
5229: if (*errorcodeptr != 0) goto FAILED;
1.1.1.2 misho 5230: save_hwm = (pcre_uchar *)cd->start_workspace + save_offset;
5231: this_hwm = (pcre_uchar *)cd->start_workspace + this_offset;
1.1 misho 5232: }
5233:
5234: for (hc = save_hwm; hc < this_hwm; hc += LINK_SIZE)
5235: {
5236: PUT(cd->hwm, 0, GET(hc, 0) + len);
5237: cd->hwm += LINK_SIZE;
5238: }
5239: save_hwm = this_hwm;
5240: code += len;
5241: }
5242: }
5243: }
5244:
5245: if (repeat_max > 0) repeat_max -= repeat_min;
5246: }
5247:
5248: /* This code is common to both the zero and non-zero minimum cases. If
5249: the maximum is limited, it replicates the group in a nested fashion,
5250: remembering the bracket starts on a stack. In the case of a zero minimum,
5251: the first one was set up above. In all cases the repeat_max now specifies
5252: the number of additional copies needed. Again, we must remember to
5253: replicate entries on the forward reference list. */
5254:
5255: if (repeat_max >= 0)
5256: {
5257: /* In the pre-compile phase, we don't actually do the replication. We
5258: just adjust the length as if we had. For each repetition we must add 1
5259: to the length for BRAZERO and for all but the last repetition we must
5260: add 2 + 2*LINKSIZE to allow for the nesting that occurs. Do some
5261: paranoid checks to avoid integer overflow. The INT64_OR_DOUBLE type is
5262: a 64-bit integer type when available, otherwise double. */
5263:
5264: if (lengthptr != NULL && repeat_max > 0)
5265: {
5266: int delta = repeat_max * (length_prevgroup + 1 + 2 + 2*LINK_SIZE) -
5267: 2 - 2*LINK_SIZE; /* Last one doesn't nest */
5268: if ((INT64_OR_DOUBLE)repeat_max *
5269: (INT64_OR_DOUBLE)(length_prevgroup + 1 + 2 + 2*LINK_SIZE)
5270: > (INT64_OR_DOUBLE)INT_MAX ||
5271: OFLOW_MAX - *lengthptr < delta)
5272: {
5273: *errorcodeptr = ERR20;
5274: goto FAILED;
5275: }
5276: *lengthptr += delta;
5277: }
5278:
5279: /* This is compiling for real */
5280:
5281: else for (i = repeat_max - 1; i >= 0; i--)
5282: {
1.1.1.2 misho 5283: pcre_uchar *hc;
5284: pcre_uchar *this_hwm = cd->hwm;
1.1 misho 5285:
5286: *code++ = OP_BRAZERO + repeat_type;
5287:
5288: /* All but the final copy start a new nesting, maintaining the
5289: chain of brackets outstanding. */
5290:
5291: if (i != 0)
5292: {
5293: int offset;
5294: *code++ = OP_BRA;
5295: offset = (bralink == NULL)? 0 : (int)(code - bralink);
5296: bralink = code;
5297: PUTINC(code, 0, offset);
5298: }
5299:
1.1.1.2 misho 5300: memcpy(code, previous, IN_UCHARS(len));
1.1 misho 5301:
5302: /* Ensure there is enough workspace for forward references before
5303: copying them. */
5304:
5305: while (cd->hwm > cd->start_workspace + cd->workspace_size -
5306: WORK_SIZE_SAFETY_MARGIN - (this_hwm - save_hwm))
5307: {
5308: int save_offset = save_hwm - cd->start_workspace;
5309: int this_offset = this_hwm - cd->start_workspace;
5310: *errorcodeptr = expand_workspace(cd);
5311: if (*errorcodeptr != 0) goto FAILED;
1.1.1.2 misho 5312: save_hwm = (pcre_uchar *)cd->start_workspace + save_offset;
5313: this_hwm = (pcre_uchar *)cd->start_workspace + this_offset;
1.1 misho 5314: }
5315:
5316: for (hc = save_hwm; hc < this_hwm; hc += LINK_SIZE)
5317: {
5318: PUT(cd->hwm, 0, GET(hc, 0) + len + ((i != 0)? 2+LINK_SIZE : 1));
5319: cd->hwm += LINK_SIZE;
5320: }
5321: save_hwm = this_hwm;
5322: code += len;
5323: }
5324:
5325: /* Now chain through the pending brackets, and fill in their length
5326: fields (which are holding the chain links pro tem). */
5327:
5328: while (bralink != NULL)
5329: {
5330: int oldlinkoffset;
5331: int offset = (int)(code - bralink + 1);
1.1.1.2 misho 5332: pcre_uchar *bra = code - offset;
1.1 misho 5333: oldlinkoffset = GET(bra, 1);
5334: bralink = (oldlinkoffset == 0)? NULL : bralink - oldlinkoffset;
5335: *code++ = OP_KET;
5336: PUTINC(code, 0, offset);
5337: PUT(bra, 1, offset);
5338: }
5339: }
5340:
5341: /* If the maximum is unlimited, set a repeater in the final copy. For
5342: ONCE brackets, that's all we need to do. However, possessively repeated
5343: ONCE brackets can be converted into non-capturing brackets, as the
5344: behaviour of (?:xx)++ is the same as (?>xx)++ and this saves having to
5345: deal with possessive ONCEs specially.
5346:
5347: Otherwise, when we are doing the actual compile phase, check to see
5348: whether this group is one that could match an empty string. If so,
5349: convert the initial operator to the S form (e.g. OP_BRA -> OP_SBRA) so
5350: that runtime checking can be done. [This check is also applied to ONCE
5351: groups at runtime, but in a different way.]
5352:
5353: Then, if the quantifier was possessive and the bracket is not a
5354: conditional, we convert the BRA code to the POS form, and the KET code to
5355: KETRPOS. (It turns out to be convenient at runtime to detect this kind of
5356: subpattern at both the start and at the end.) The use of special opcodes
5357: makes it possible to reduce greatly the stack usage in pcre_exec(). If
5358: the group is preceded by OP_BRAZERO, convert this to OP_BRAPOSZERO.
5359:
5360: Then, if the minimum number of matches is 1 or 0, cancel the possessive
5361: flag so that the default action below, of wrapping everything inside
5362: atomic brackets, does not happen. When the minimum is greater than 1,
5363: there will be earlier copies of the group, and so we still have to wrap
5364: the whole thing. */
5365:
5366: else
5367: {
1.1.1.2 misho 5368: pcre_uchar *ketcode = code - 1 - LINK_SIZE;
5369: pcre_uchar *bracode = ketcode - GET(ketcode, 1);
1.1 misho 5370:
5371: /* Convert possessive ONCE brackets to non-capturing */
5372:
5373: if ((*bracode == OP_ONCE || *bracode == OP_ONCE_NC) &&
5374: possessive_quantifier) *bracode = OP_BRA;
5375:
5376: /* For non-possessive ONCE brackets, all we need to do is to
5377: set the KET. */
5378:
5379: if (*bracode == OP_ONCE || *bracode == OP_ONCE_NC)
5380: *ketcode = OP_KETRMAX + repeat_type;
5381:
5382: /* Handle non-ONCE brackets and possessive ONCEs (which have been
5383: converted to non-capturing above). */
5384:
5385: else
5386: {
5387: /* In the compile phase, check for empty string matching. */
5388:
5389: if (lengthptr == NULL)
5390: {
1.1.1.2 misho 5391: pcre_uchar *scode = bracode;
1.1 misho 5392: do
5393: {
1.1.1.2 misho 5394: if (could_be_empty_branch(scode, ketcode, utf, cd))
1.1 misho 5395: {
5396: *bracode += OP_SBRA - OP_BRA;
5397: break;
5398: }
5399: scode += GET(scode, 1);
5400: }
5401: while (*scode == OP_ALT);
5402: }
5403:
5404: /* Handle possessive quantifiers. */
5405:
5406: if (possessive_quantifier)
5407: {
5408: /* For COND brackets, we wrap the whole thing in a possessively
5409: repeated non-capturing bracket, because we have not invented POS
5410: versions of the COND opcodes. Because we are moving code along, we
5411: must ensure that any pending recursive references are updated. */
5412:
5413: if (*bracode == OP_COND || *bracode == OP_SCOND)
5414: {
5415: int nlen = (int)(code - bracode);
5416: *code = OP_END;
1.1.1.2 misho 5417: adjust_recurse(bracode, 1 + LINK_SIZE, utf, cd, save_hwm);
5418: memmove(bracode + 1 + LINK_SIZE, bracode, IN_UCHARS(nlen));
1.1 misho 5419: code += 1 + LINK_SIZE;
5420: nlen += 1 + LINK_SIZE;
5421: *bracode = OP_BRAPOS;
5422: *code++ = OP_KETRPOS;
5423: PUTINC(code, 0, nlen);
5424: PUT(bracode, 1, nlen);
5425: }
5426:
5427: /* For non-COND brackets, we modify the BRA code and use KETRPOS. */
5428:
5429: else
5430: {
5431: *bracode += 1; /* Switch to xxxPOS opcodes */
5432: *ketcode = OP_KETRPOS;
5433: }
5434:
5435: /* If the minimum is zero, mark it as possessive, then unset the
5436: possessive flag when the minimum is 0 or 1. */
5437:
5438: if (brazeroptr != NULL) *brazeroptr = OP_BRAPOSZERO;
5439: if (repeat_min < 2) possessive_quantifier = FALSE;
5440: }
5441:
5442: /* Non-possessive quantifier */
5443:
5444: else *ketcode = OP_KETRMAX + repeat_type;
5445: }
5446: }
5447: }
5448:
5449: /* If previous is OP_FAIL, it was generated by an empty class [] in
5450: JavaScript mode. The other ways in which OP_FAIL can be generated, that is
5451: by (*FAIL) or (?!) set previous to NULL, which gives a "nothing to repeat"
5452: error above. We can just ignore the repeat in JS case. */
5453:
5454: else if (*previous == OP_FAIL) goto END_REPEAT;
5455:
5456: /* Else there's some kind of shambles */
5457:
5458: else
5459: {
5460: *errorcodeptr = ERR11;
5461: goto FAILED;
5462: }
5463:
5464: /* If the character following a repeat is '+', or if certain optimization
5465: tests above succeeded, possessive_quantifier is TRUE. For some opcodes,
5466: there are special alternative opcodes for this case. For anything else, we
5467: wrap the entire repeated item inside OP_ONCE brackets. Logically, the '+'
5468: notation is just syntactic sugar, taken from Sun's Java package, but the
5469: special opcodes can optimize it.
5470:
5471: Some (but not all) possessively repeated subpatterns have already been
5472: completely handled in the code just above. For them, possessive_quantifier
5473: is always FALSE at this stage.
5474:
5475: Note that the repeated item starts at tempcode, not at previous, which
5476: might be the first part of a string whose (former) last char we repeated.
5477:
5478: Possessifying an 'exact' quantifier has no effect, so we can ignore it. But
5479: an 'upto' may follow. We skip over an 'exact' item, and then test the
5480: length of what remains before proceeding. */
5481:
5482: if (possessive_quantifier)
5483: {
5484: int len;
5485:
5486: if (*tempcode == OP_TYPEEXACT)
1.1.1.2 misho 5487: tempcode += PRIV(OP_lengths)[*tempcode] +
5488: ((tempcode[1 + IMM2_SIZE] == OP_PROP
5489: || tempcode[1 + IMM2_SIZE] == OP_NOTPROP)? 2 : 0);
1.1 misho 5490:
5491: else if (*tempcode == OP_EXACT || *tempcode == OP_NOTEXACT)
5492: {
1.1.1.2 misho 5493: tempcode += PRIV(OP_lengths)[*tempcode];
5494: #ifdef SUPPORT_UTF
5495: if (utf && HAS_EXTRALEN(tempcode[-1]))
5496: tempcode += GET_EXTRALEN(tempcode[-1]);
1.1 misho 5497: #endif
5498: }
5499:
5500: len = (int)(code - tempcode);
5501: if (len > 0) switch (*tempcode)
5502: {
5503: case OP_STAR: *tempcode = OP_POSSTAR; break;
5504: case OP_PLUS: *tempcode = OP_POSPLUS; break;
5505: case OP_QUERY: *tempcode = OP_POSQUERY; break;
5506: case OP_UPTO: *tempcode = OP_POSUPTO; break;
5507:
5508: case OP_STARI: *tempcode = OP_POSSTARI; break;
5509: case OP_PLUSI: *tempcode = OP_POSPLUSI; break;
5510: case OP_QUERYI: *tempcode = OP_POSQUERYI; break;
5511: case OP_UPTOI: *tempcode = OP_POSUPTOI; break;
5512:
5513: case OP_NOTSTAR: *tempcode = OP_NOTPOSSTAR; break;
5514: case OP_NOTPLUS: *tempcode = OP_NOTPOSPLUS; break;
5515: case OP_NOTQUERY: *tempcode = OP_NOTPOSQUERY; break;
5516: case OP_NOTUPTO: *tempcode = OP_NOTPOSUPTO; break;
5517:
5518: case OP_NOTSTARI: *tempcode = OP_NOTPOSSTARI; break;
5519: case OP_NOTPLUSI: *tempcode = OP_NOTPOSPLUSI; break;
5520: case OP_NOTQUERYI: *tempcode = OP_NOTPOSQUERYI; break;
5521: case OP_NOTUPTOI: *tempcode = OP_NOTPOSUPTOI; break;
5522:
5523: case OP_TYPESTAR: *tempcode = OP_TYPEPOSSTAR; break;
5524: case OP_TYPEPLUS: *tempcode = OP_TYPEPOSPLUS; break;
5525: case OP_TYPEQUERY: *tempcode = OP_TYPEPOSQUERY; break;
5526: case OP_TYPEUPTO: *tempcode = OP_TYPEPOSUPTO; break;
5527:
5528: /* Because we are moving code along, we must ensure that any
5529: pending recursive references are updated. */
5530:
5531: default:
5532: *code = OP_END;
1.1.1.2 misho 5533: adjust_recurse(tempcode, 1 + LINK_SIZE, utf, cd, save_hwm);
5534: memmove(tempcode + 1 + LINK_SIZE, tempcode, IN_UCHARS(len));
1.1 misho 5535: code += 1 + LINK_SIZE;
5536: len += 1 + LINK_SIZE;
5537: tempcode[0] = OP_ONCE;
5538: *code++ = OP_KET;
5539: PUTINC(code, 0, len);
5540: PUT(tempcode, 1, len);
5541: break;
5542: }
5543: }
5544:
5545: /* In all case we no longer have a previous item. We also set the
1.1.1.2 misho 5546: "follows varying string" flag for subsequently encountered reqchars if
1.1 misho 5547: it isn't already set and we have just passed a varying length item. */
5548:
5549: END_REPEAT:
5550: previous = NULL;
5551: cd->req_varyopt |= reqvary;
5552: break;
5553:
5554:
5555: /* ===================================================================*/
5556: /* Start of nested parenthesized sub-expression, or comment or lookahead or
5557: lookbehind or option setting or condition or all the other extended
5558: parenthesis forms. */
5559:
5560: case CHAR_LEFT_PARENTHESIS:
5561: newoptions = options;
5562: skipbytes = 0;
5563: bravalue = OP_CBRA;
5564: save_hwm = cd->hwm;
5565: reset_bracount = FALSE;
5566:
5567: /* First deal with various "verbs" that can be introduced by '*'. */
5568:
1.1.1.2 misho 5569: ptr++;
5570: if (ptr[0] == CHAR_ASTERISK && (ptr[1] == ':'
5571: || (MAX_255(ptr[1]) && ((cd->ctypes[ptr[1]] & ctype_letter) != 0))))
1.1 misho 5572: {
5573: int i, namelen;
5574: int arglen = 0;
5575: const char *vn = verbnames;
1.1.1.2 misho 5576: const pcre_uchar *name = ptr + 1;
5577: const pcre_uchar *arg = NULL;
1.1 misho 5578: previous = NULL;
1.1.1.2 misho 5579: ptr++;
5580: while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_letter) != 0) ptr++;
1.1 misho 5581: namelen = (int)(ptr - name);
5582:
5583: /* It appears that Perl allows any characters whatsoever, other than
5584: a closing parenthesis, to appear in arguments, so we no longer insist on
5585: letters, digits, and underscores. */
5586:
5587: if (*ptr == CHAR_COLON)
5588: {
5589: arg = ++ptr;
1.1.1.4 ! misho 5590: while (*ptr != CHAR_NULL && *ptr != CHAR_RIGHT_PARENTHESIS) ptr++;
1.1 misho 5591: arglen = (int)(ptr - arg);
1.1.1.4 ! misho 5592: if ((unsigned int)arglen > MAX_MARK)
1.1.1.3 misho 5593: {
5594: *errorcodeptr = ERR75;
5595: goto FAILED;
5596: }
1.1 misho 5597: }
5598:
5599: if (*ptr != CHAR_RIGHT_PARENTHESIS)
5600: {
5601: *errorcodeptr = ERR60;
5602: goto FAILED;
5603: }
5604:
5605: /* Scan the table of verb names */
5606:
5607: for (i = 0; i < verbcount; i++)
5608: {
5609: if (namelen == verbs[i].len &&
1.1.1.2 misho 5610: STRNCMP_UC_C8(name, vn, namelen) == 0)
1.1 misho 5611: {
1.1.1.4 ! misho 5612: int setverb;
! 5613:
1.1 misho 5614: /* Check for open captures before ACCEPT and convert it to
5615: ASSERT_ACCEPT if in an assertion. */
5616:
5617: if (verbs[i].op == OP_ACCEPT)
5618: {
5619: open_capitem *oc;
5620: if (arglen != 0)
5621: {
5622: *errorcodeptr = ERR59;
5623: goto FAILED;
5624: }
5625: cd->had_accept = TRUE;
5626: for (oc = cd->open_caps; oc != NULL; oc = oc->next)
5627: {
5628: *code++ = OP_CLOSE;
5629: PUT2INC(code, 0, oc->number);
5630: }
1.1.1.4 ! misho 5631: setverb = *code++ =
! 5632: (cd->assert_depth > 0)? OP_ASSERT_ACCEPT : OP_ACCEPT;
1.1 misho 5633:
1.1.1.2 misho 5634: /* Do not set firstchar after *ACCEPT */
1.1.1.4 ! misho 5635: if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
1.1 misho 5636: }
5637:
5638: /* Handle other cases with/without an argument */
5639:
5640: else if (arglen == 0)
5641: {
5642: if (verbs[i].op < 0) /* Argument is mandatory */
5643: {
5644: *errorcodeptr = ERR66;
5645: goto FAILED;
5646: }
1.1.1.4 ! misho 5647: setverb = *code++ = verbs[i].op;
1.1 misho 5648: }
5649:
5650: else
5651: {
5652: if (verbs[i].op_arg < 0) /* Argument is forbidden */
5653: {
5654: *errorcodeptr = ERR59;
5655: goto FAILED;
5656: }
1.1.1.4 ! misho 5657: setverb = *code++ = verbs[i].op_arg;
1.1 misho 5658: *code++ = arglen;
1.1.1.2 misho 5659: memcpy(code, arg, IN_UCHARS(arglen));
1.1 misho 5660: code += arglen;
5661: *code++ = 0;
5662: }
5663:
1.1.1.4 ! misho 5664: switch (setverb)
! 5665: {
! 5666: case OP_THEN:
! 5667: case OP_THEN_ARG:
! 5668: cd->external_flags |= PCRE_HASTHEN;
! 5669: break;
! 5670:
! 5671: case OP_PRUNE:
! 5672: case OP_PRUNE_ARG:
! 5673: case OP_SKIP:
! 5674: case OP_SKIP_ARG:
! 5675: cd->had_pruneorskip = TRUE;
! 5676: break;
! 5677: }
! 5678:
1.1 misho 5679: break; /* Found verb, exit loop */
5680: }
5681:
5682: vn += verbs[i].len + 1;
5683: }
5684:
5685: if (i < verbcount) continue; /* Successfully handled a verb */
5686: *errorcodeptr = ERR60; /* Verb not recognized */
5687: goto FAILED;
5688: }
5689:
5690: /* Deal with the extended parentheses; all are introduced by '?', and the
5691: appearance of any of them means that this is not a capturing group. */
5692:
5693: else if (*ptr == CHAR_QUESTION_MARK)
5694: {
5695: int i, set, unset, namelen;
5696: int *optset;
1.1.1.2 misho 5697: const pcre_uchar *name;
5698: pcre_uchar *slot;
1.1 misho 5699:
5700: switch (*(++ptr))
5701: {
5702: case CHAR_NUMBER_SIGN: /* Comment; skip to ket */
5703: ptr++;
1.1.1.4 ! misho 5704: while (*ptr != CHAR_NULL && *ptr != CHAR_RIGHT_PARENTHESIS) ptr++;
! 5705: if (*ptr == CHAR_NULL)
1.1 misho 5706: {
5707: *errorcodeptr = ERR18;
5708: goto FAILED;
5709: }
5710: continue;
5711:
5712:
5713: /* ------------------------------------------------------------ */
5714: case CHAR_VERTICAL_LINE: /* Reset capture count for each branch */
5715: reset_bracount = TRUE;
5716: /* Fall through */
5717:
5718: /* ------------------------------------------------------------ */
5719: case CHAR_COLON: /* Non-capturing bracket */
5720: bravalue = OP_BRA;
5721: ptr++;
5722: break;
5723:
5724:
5725: /* ------------------------------------------------------------ */
5726: case CHAR_LEFT_PARENTHESIS:
5727: bravalue = OP_COND; /* Conditional group */
1.1.1.4 ! misho 5728: tempptr = ptr;
1.1 misho 5729:
5730: /* A condition can be an assertion, a number (referring to a numbered
5731: group), a name (referring to a named group), or 'R', referring to
5732: recursion. R<digits> and R&name are also permitted for recursion tests.
5733:
5734: There are several syntaxes for testing a named group: (?(name)) is used
5735: by Python; Perl 5.10 onwards uses (?(<name>) or (?('name')).
5736:
5737: There are two unfortunate ambiguities, caused by history. (a) 'R' can
5738: be the recursive thing or the name 'R' (and similarly for 'R' followed
5739: by digits), and (b) a number could be a name that consists of digits.
5740: In both cases, we look for a name first; if not found, we try the other
1.1.1.4 ! misho 5741: cases.
! 5742:
! 5743: For compatibility with auto-callouts, we allow a callout to be
! 5744: specified before a condition that is an assertion. First, check for the
! 5745: syntax of a callout; if found, adjust the temporary pointer that is
! 5746: used to check for an assertion condition. That's all that is needed! */
! 5747:
! 5748: if (ptr[1] == CHAR_QUESTION_MARK && ptr[2] == CHAR_C)
! 5749: {
! 5750: for (i = 3;; i++) if (!IS_DIGIT(ptr[i])) break;
! 5751: if (ptr[i] == CHAR_RIGHT_PARENTHESIS)
! 5752: tempptr += i + 1;
! 5753: }
1.1 misho 5754:
5755: /* For conditions that are assertions, check the syntax, and then exit
5756: the switch. This will take control down to where bracketed groups,
5757: including assertions, are processed. */
5758:
1.1.1.4 ! misho 5759: if (tempptr[1] == CHAR_QUESTION_MARK &&
! 5760: (tempptr[2] == CHAR_EQUALS_SIGN ||
! 5761: tempptr[2] == CHAR_EXCLAMATION_MARK ||
! 5762: tempptr[2] == CHAR_LESS_THAN_SIGN))
1.1 misho 5763: break;
5764:
5765: /* Most other conditions use OP_CREF (a couple change to OP_RREF
1.1.1.2 misho 5766: below), and all need to skip 1+IMM2_SIZE bytes at the start of the group. */
1.1 misho 5767:
5768: code[1+LINK_SIZE] = OP_CREF;
1.1.1.2 misho 5769: skipbytes = 1+IMM2_SIZE;
1.1 misho 5770: refsign = -1;
5771:
5772: /* Check for a test for recursion in a named group. */
5773:
5774: if (ptr[1] == CHAR_R && ptr[2] == CHAR_AMPERSAND)
5775: {
5776: terminator = -1;
5777: ptr += 2;
5778: code[1+LINK_SIZE] = OP_RREF; /* Change the type of test */
5779: }
5780:
5781: /* Check for a test for a named group's having been set, using the Perl
5782: syntax (?(<name>) or (?('name') */
5783:
5784: else if (ptr[1] == CHAR_LESS_THAN_SIGN)
5785: {
5786: terminator = CHAR_GREATER_THAN_SIGN;
5787: ptr++;
5788: }
5789: else if (ptr[1] == CHAR_APOSTROPHE)
5790: {
5791: terminator = CHAR_APOSTROPHE;
5792: ptr++;
5793: }
5794: else
5795: {
1.1.1.4 ! misho 5796: terminator = CHAR_NULL;
1.1 misho 5797: if (ptr[1] == CHAR_MINUS || ptr[1] == CHAR_PLUS) refsign = *(++ptr);
5798: }
5799:
5800: /* We now expect to read a name; any thing else is an error */
5801:
1.1.1.2 misho 5802: if (!MAX_255(ptr[1]) || (cd->ctypes[ptr[1]] & ctype_word) == 0)
1.1 misho 5803: {
5804: ptr += 1; /* To get the right offset */
5805: *errorcodeptr = ERR28;
5806: goto FAILED;
5807: }
5808:
5809: /* Read the name, but also get it as a number if it's all digits */
5810:
5811: recno = 0;
5812: name = ++ptr;
1.1.1.2 misho 5813: while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_word) != 0)
1.1 misho 5814: {
5815: if (recno >= 0)
1.1.1.4 ! misho 5816: recno = (IS_DIGIT(*ptr))? recno * 10 + (int)(*ptr - CHAR_0) : -1;
1.1 misho 5817: ptr++;
5818: }
5819: namelen = (int)(ptr - name);
5820:
1.1.1.4 ! misho 5821: if ((terminator > 0 && *ptr++ != (pcre_uchar)terminator) ||
1.1 misho 5822: *ptr++ != CHAR_RIGHT_PARENTHESIS)
5823: {
5824: ptr--; /* Error offset */
5825: *errorcodeptr = ERR26;
5826: goto FAILED;
5827: }
5828:
5829: /* Do no further checking in the pre-compile phase. */
5830:
5831: if (lengthptr != NULL) break;
5832:
5833: /* In the real compile we do the work of looking for the actual
5834: reference. If the string started with "+" or "-" we require the rest to
5835: be digits, in which case recno will be set. */
5836:
5837: if (refsign > 0)
5838: {
5839: if (recno <= 0)
5840: {
5841: *errorcodeptr = ERR58;
5842: goto FAILED;
5843: }
5844: recno = (refsign == CHAR_MINUS)?
5845: cd->bracount - recno + 1 : recno +cd->bracount;
5846: if (recno <= 0 || recno > cd->final_bracount)
5847: {
5848: *errorcodeptr = ERR15;
5849: goto FAILED;
5850: }
5851: PUT2(code, 2+LINK_SIZE, recno);
5852: break;
5853: }
5854:
5855: /* Otherwise (did not start with "+" or "-"), start by looking for the
5856: name. If we find a name, add one to the opcode to change OP_CREF or
5857: OP_RREF into OP_NCREF or OP_NRREF. These behave exactly the same,
5858: except they record that the reference was originally to a name. The
5859: information is used to check duplicate names. */
5860:
5861: slot = cd->name_table;
5862: for (i = 0; i < cd->names_found; i++)
5863: {
1.1.1.2 misho 5864: if (STRNCMP_UC_UC(name, slot+IMM2_SIZE, namelen) == 0) break;
1.1 misho 5865: slot += cd->name_entry_size;
5866: }
5867:
5868: /* Found a previous named subpattern */
5869:
5870: if (i < cd->names_found)
5871: {
5872: recno = GET2(slot, 0);
5873: PUT2(code, 2+LINK_SIZE, recno);
5874: code[1+LINK_SIZE]++;
5875: }
5876:
5877: /* Search the pattern for a forward reference */
5878:
5879: else if ((i = find_parens(cd, name, namelen,
1.1.1.2 misho 5880: (options & PCRE_EXTENDED) != 0, utf)) > 0)
1.1 misho 5881: {
5882: PUT2(code, 2+LINK_SIZE, i);
5883: code[1+LINK_SIZE]++;
5884: }
5885:
1.1.1.4 ! misho 5886: /* If terminator == CHAR_NULL it means that the name followed directly
! 5887: after the opening parenthesis [e.g. (?(abc)...] and in this case there
! 5888: are some further alternatives to try. For the cases where terminator !=
! 5889: 0 [things like (?(<name>... or (?('name')... or (?(R&name)... ] we have
1.1 misho 5890: now checked all the possibilities, so give an error. */
5891:
1.1.1.4 ! misho 5892: else if (terminator != CHAR_NULL)
1.1 misho 5893: {
5894: *errorcodeptr = ERR15;
5895: goto FAILED;
5896: }
5897:
5898: /* Check for (?(R) for recursion. Allow digits after R to specify a
5899: specific group number. */
5900:
5901: else if (*name == CHAR_R)
5902: {
5903: recno = 0;
5904: for (i = 1; i < namelen; i++)
5905: {
1.1.1.2 misho 5906: if (!IS_DIGIT(name[i]))
1.1 misho 5907: {
5908: *errorcodeptr = ERR15;
5909: goto FAILED;
5910: }
5911: recno = recno * 10 + name[i] - CHAR_0;
5912: }
5913: if (recno == 0) recno = RREF_ANY;
5914: code[1+LINK_SIZE] = OP_RREF; /* Change test type */
5915: PUT2(code, 2+LINK_SIZE, recno);
5916: }
5917:
5918: /* Similarly, check for the (?(DEFINE) "condition", which is always
5919: false. */
5920:
1.1.1.2 misho 5921: else if (namelen == 6 && STRNCMP_UC_C8(name, STRING_DEFINE, 6) == 0)
1.1 misho 5922: {
5923: code[1+LINK_SIZE] = OP_DEF;
5924: skipbytes = 1;
5925: }
5926:
5927: /* Check for the "name" actually being a subpattern number. We are
5928: in the second pass here, so final_bracount is set. */
5929:
5930: else if (recno > 0 && recno <= cd->final_bracount)
5931: {
5932: PUT2(code, 2+LINK_SIZE, recno);
5933: }
5934:
5935: /* Either an unidentified subpattern, or a reference to (?(0) */
5936:
5937: else
5938: {
5939: *errorcodeptr = (recno == 0)? ERR35: ERR15;
5940: goto FAILED;
5941: }
5942: break;
5943:
5944:
5945: /* ------------------------------------------------------------ */
5946: case CHAR_EQUALS_SIGN: /* Positive lookahead */
5947: bravalue = OP_ASSERT;
5948: cd->assert_depth += 1;
5949: ptr++;
5950: break;
5951:
5952:
5953: /* ------------------------------------------------------------ */
5954: case CHAR_EXCLAMATION_MARK: /* Negative lookahead */
5955: ptr++;
5956: if (*ptr == CHAR_RIGHT_PARENTHESIS) /* Optimize (?!) */
5957: {
5958: *code++ = OP_FAIL;
5959: previous = NULL;
5960: continue;
5961: }
5962: bravalue = OP_ASSERT_NOT;
5963: cd->assert_depth += 1;
5964: break;
5965:
5966:
5967: /* ------------------------------------------------------------ */
5968: case CHAR_LESS_THAN_SIGN: /* Lookbehind or named define */
5969: switch (ptr[1])
5970: {
5971: case CHAR_EQUALS_SIGN: /* Positive lookbehind */
5972: bravalue = OP_ASSERTBACK;
5973: cd->assert_depth += 1;
5974: ptr += 2;
5975: break;
5976:
5977: case CHAR_EXCLAMATION_MARK: /* Negative lookbehind */
5978: bravalue = OP_ASSERTBACK_NOT;
5979: cd->assert_depth += 1;
5980: ptr += 2;
5981: break;
5982:
5983: default: /* Could be name define, else bad */
1.1.1.2 misho 5984: if (MAX_255(ptr[1]) && (cd->ctypes[ptr[1]] & ctype_word) != 0)
5985: goto DEFINE_NAME;
1.1 misho 5986: ptr++; /* Correct offset for error */
5987: *errorcodeptr = ERR24;
5988: goto FAILED;
5989: }
5990: break;
5991:
5992:
5993: /* ------------------------------------------------------------ */
5994: case CHAR_GREATER_THAN_SIGN: /* One-time brackets */
5995: bravalue = OP_ONCE;
5996: ptr++;
5997: break;
5998:
5999:
6000: /* ------------------------------------------------------------ */
6001: case CHAR_C: /* Callout - may be followed by digits; */
6002: previous_callout = code; /* Save for later completion */
6003: after_manual_callout = 1; /* Skip one item before completing */
6004: *code++ = OP_CALLOUT;
6005: {
6006: int n = 0;
1.1.1.2 misho 6007: ptr++;
6008: while(IS_DIGIT(*ptr))
6009: n = n * 10 + *ptr++ - CHAR_0;
1.1 misho 6010: if (*ptr != CHAR_RIGHT_PARENTHESIS)
6011: {
6012: *errorcodeptr = ERR39;
6013: goto FAILED;
6014: }
6015: if (n > 255)
6016: {
6017: *errorcodeptr = ERR38;
6018: goto FAILED;
6019: }
6020: *code++ = n;
6021: PUT(code, 0, (int)(ptr - cd->start_pattern + 1)); /* Pattern offset */
6022: PUT(code, LINK_SIZE, 0); /* Default length */
6023: code += 2 * LINK_SIZE;
6024: }
6025: previous = NULL;
6026: continue;
6027:
6028:
6029: /* ------------------------------------------------------------ */
6030: case CHAR_P: /* Python-style named subpattern handling */
6031: if (*(++ptr) == CHAR_EQUALS_SIGN ||
6032: *ptr == CHAR_GREATER_THAN_SIGN) /* Reference or recursion */
6033: {
6034: is_recurse = *ptr == CHAR_GREATER_THAN_SIGN;
6035: terminator = CHAR_RIGHT_PARENTHESIS;
6036: goto NAMED_REF_OR_RECURSE;
6037: }
6038: else if (*ptr != CHAR_LESS_THAN_SIGN) /* Test for Python-style defn */
6039: {
6040: *errorcodeptr = ERR41;
6041: goto FAILED;
6042: }
6043: /* Fall through to handle (?P< as (?< is handled */
6044:
6045:
6046: /* ------------------------------------------------------------ */
6047: DEFINE_NAME: /* Come here from (?< handling */
6048: case CHAR_APOSTROPHE:
6049: {
6050: terminator = (*ptr == CHAR_LESS_THAN_SIGN)?
6051: CHAR_GREATER_THAN_SIGN : CHAR_APOSTROPHE;
6052: name = ++ptr;
6053:
1.1.1.2 misho 6054: while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_word) != 0) ptr++;
1.1 misho 6055: namelen = (int)(ptr - name);
6056:
6057: /* In the pre-compile phase, just do a syntax check. */
6058:
6059: if (lengthptr != NULL)
6060: {
1.1.1.4 ! misho 6061: if (*ptr != (pcre_uchar)terminator)
1.1 misho 6062: {
6063: *errorcodeptr = ERR42;
6064: goto FAILED;
6065: }
6066: if (cd->names_found >= MAX_NAME_COUNT)
6067: {
6068: *errorcodeptr = ERR49;
6069: goto FAILED;
6070: }
1.1.1.2 misho 6071: if (namelen + IMM2_SIZE + 1 > cd->name_entry_size)
1.1 misho 6072: {
1.1.1.2 misho 6073: cd->name_entry_size = namelen + IMM2_SIZE + 1;
1.1 misho 6074: if (namelen > MAX_NAME_SIZE)
6075: {
6076: *errorcodeptr = ERR48;
6077: goto FAILED;
6078: }
6079: }
6080: }
6081:
6082: /* In the real compile, create the entry in the table, maintaining
6083: alphabetical order. Duplicate names for different numbers are
6084: permitted only if PCRE_DUPNAMES is set. Duplicate names for the same
6085: number are always OK. (An existing number can be re-used if (?|
6086: appears in the pattern.) In either event, a duplicate name results in
6087: a duplicate entry in the table, even if the number is the same. This
6088: is because the number of names, and hence the table size, is computed
6089: in the pre-compile, and it affects various numbers and pointers which
6090: would all have to be modified, and the compiled code moved down, if
6091: duplicates with the same number were omitted from the table. This
6092: doesn't seem worth the hassle. However, *different* names for the
6093: same number are not permitted. */
6094:
6095: else
6096: {
6097: BOOL dupname = FALSE;
6098: slot = cd->name_table;
6099:
6100: for (i = 0; i < cd->names_found; i++)
6101: {
1.1.1.2 misho 6102: int crc = memcmp(name, slot+IMM2_SIZE, IN_UCHARS(namelen));
1.1 misho 6103: if (crc == 0)
6104: {
1.1.1.2 misho 6105: if (slot[IMM2_SIZE+namelen] == 0)
1.1 misho 6106: {
6107: if (GET2(slot, 0) != cd->bracount + 1 &&
6108: (options & PCRE_DUPNAMES) == 0)
6109: {
6110: *errorcodeptr = ERR43;
6111: goto FAILED;
6112: }
6113: else dupname = TRUE;
6114: }
6115: else crc = -1; /* Current name is a substring */
6116: }
6117:
6118: /* Make space in the table and break the loop for an earlier
6119: name. For a duplicate or later name, carry on. We do this for
6120: duplicates so that in the simple case (when ?(| is not used) they
6121: are in order of their numbers. */
6122:
6123: if (crc < 0)
6124: {
6125: memmove(slot + cd->name_entry_size, slot,
1.1.1.2 misho 6126: IN_UCHARS((cd->names_found - i) * cd->name_entry_size));
1.1 misho 6127: break;
6128: }
6129:
6130: /* Continue the loop for a later or duplicate name */
6131:
6132: slot += cd->name_entry_size;
6133: }
6134:
6135: /* For non-duplicate names, check for a duplicate number before
6136: adding the new name. */
6137:
6138: if (!dupname)
6139: {
1.1.1.2 misho 6140: pcre_uchar *cslot = cd->name_table;
1.1 misho 6141: for (i = 0; i < cd->names_found; i++)
6142: {
6143: if (cslot != slot)
6144: {
6145: if (GET2(cslot, 0) == cd->bracount + 1)
6146: {
6147: *errorcodeptr = ERR65;
6148: goto FAILED;
6149: }
6150: }
6151: else i--;
6152: cslot += cd->name_entry_size;
6153: }
6154: }
6155:
6156: PUT2(slot, 0, cd->bracount + 1);
1.1.1.2 misho 6157: memcpy(slot + IMM2_SIZE, name, IN_UCHARS(namelen));
6158: slot[IMM2_SIZE + namelen] = 0;
1.1 misho 6159: }
6160: }
6161:
6162: /* In both pre-compile and compile, count the number of names we've
6163: encountered. */
6164:
6165: cd->names_found++;
6166: ptr++; /* Move past > or ' */
6167: goto NUMBERED_GROUP;
6168:
6169:
6170: /* ------------------------------------------------------------ */
6171: case CHAR_AMPERSAND: /* Perl recursion/subroutine syntax */
6172: terminator = CHAR_RIGHT_PARENTHESIS;
6173: is_recurse = TRUE;
6174: /* Fall through */
6175:
6176: /* We come here from the Python syntax above that handles both
6177: references (?P=name) and recursion (?P>name), as well as falling
6178: through from the Perl recursion syntax (?&name). We also come here from
6179: the Perl \k<name> or \k'name' back reference syntax and the \k{name}
6180: .NET syntax, and the Oniguruma \g<...> and \g'...' subroutine syntax. */
6181:
6182: NAMED_REF_OR_RECURSE:
6183: name = ++ptr;
1.1.1.2 misho 6184: while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_word) != 0) ptr++;
1.1 misho 6185: namelen = (int)(ptr - name);
6186:
6187: /* In the pre-compile phase, do a syntax check. We used to just set
6188: a dummy reference number, because it was not used in the first pass.
6189: However, with the change of recursive back references to be atomic,
6190: we have to look for the number so that this state can be identified, as
6191: otherwise the incorrect length is computed. If it's not a backwards
6192: reference, the dummy number will do. */
6193:
6194: if (lengthptr != NULL)
6195: {
1.1.1.2 misho 6196: const pcre_uchar *temp;
1.1 misho 6197:
6198: if (namelen == 0)
6199: {
6200: *errorcodeptr = ERR62;
6201: goto FAILED;
6202: }
1.1.1.4 ! misho 6203: if (*ptr != (pcre_uchar)terminator)
1.1 misho 6204: {
6205: *errorcodeptr = ERR42;
6206: goto FAILED;
6207: }
6208: if (namelen > MAX_NAME_SIZE)
6209: {
6210: *errorcodeptr = ERR48;
6211: goto FAILED;
6212: }
6213:
6214: /* The name table does not exist in the first pass, so we cannot
6215: do a simple search as in the code below. Instead, we have to scan the
6216: pattern to find the number. It is important that we scan it only as
6217: far as we have got because the syntax of named subpatterns has not
6218: been checked for the rest of the pattern, and find_parens() assumes
6219: correct syntax. In any case, it's a waste of resources to scan
6220: further. We stop the scan at the current point by temporarily
6221: adjusting the value of cd->endpattern. */
6222:
6223: temp = cd->end_pattern;
6224: cd->end_pattern = ptr;
6225: recno = find_parens(cd, name, namelen,
1.1.1.2 misho 6226: (options & PCRE_EXTENDED) != 0, utf);
1.1 misho 6227: cd->end_pattern = temp;
6228: if (recno < 0) recno = 0; /* Forward ref; set dummy number */
6229: }
6230:
6231: /* In the real compile, seek the name in the table. We check the name
6232: first, and then check that we have reached the end of the name in the
6233: table. That way, if the name that is longer than any in the table,
6234: the comparison will fail without reading beyond the table entry. */
6235:
6236: else
6237: {
6238: slot = cd->name_table;
6239: for (i = 0; i < cd->names_found; i++)
6240: {
1.1.1.2 misho 6241: if (STRNCMP_UC_UC(name, slot+IMM2_SIZE, namelen) == 0 &&
6242: slot[IMM2_SIZE+namelen] == 0)
1.1 misho 6243: break;
6244: slot += cd->name_entry_size;
6245: }
6246:
6247: if (i < cd->names_found) /* Back reference */
6248: {
6249: recno = GET2(slot, 0);
6250: }
6251: else if ((recno = /* Forward back reference */
6252: find_parens(cd, name, namelen,
1.1.1.2 misho 6253: (options & PCRE_EXTENDED) != 0, utf)) <= 0)
1.1 misho 6254: {
6255: *errorcodeptr = ERR15;
6256: goto FAILED;
6257: }
6258: }
6259:
6260: /* In both phases, we can now go to the code than handles numerical
6261: recursion or backreferences. */
6262:
6263: if (is_recurse) goto HANDLE_RECURSION;
6264: else goto HANDLE_REFERENCE;
6265:
6266:
6267: /* ------------------------------------------------------------ */
6268: case CHAR_R: /* Recursion */
6269: ptr++; /* Same as (?0) */
6270: /* Fall through */
6271:
6272:
6273: /* ------------------------------------------------------------ */
6274: case CHAR_MINUS: case CHAR_PLUS: /* Recursion or subroutine */
6275: case CHAR_0: case CHAR_1: case CHAR_2: case CHAR_3: case CHAR_4:
6276: case CHAR_5: case CHAR_6: case CHAR_7: case CHAR_8: case CHAR_9:
6277: {
1.1.1.2 misho 6278: const pcre_uchar *called;
1.1 misho 6279: terminator = CHAR_RIGHT_PARENTHESIS;
6280:
6281: /* Come here from the \g<...> and \g'...' code (Oniguruma
6282: compatibility). However, the syntax has been checked to ensure that
6283: the ... are a (signed) number, so that neither ERR63 nor ERR29 will
6284: be called on this path, nor with the jump to OTHER_CHAR_AFTER_QUERY
6285: ever be taken. */
6286:
6287: HANDLE_NUMERICAL_RECURSION:
6288:
6289: if ((refsign = *ptr) == CHAR_PLUS)
6290: {
6291: ptr++;
1.1.1.2 misho 6292: if (!IS_DIGIT(*ptr))
1.1 misho 6293: {
6294: *errorcodeptr = ERR63;
6295: goto FAILED;
6296: }
6297: }
6298: else if (refsign == CHAR_MINUS)
6299: {
1.1.1.2 misho 6300: if (!IS_DIGIT(ptr[1]))
1.1 misho 6301: goto OTHER_CHAR_AFTER_QUERY;
6302: ptr++;
6303: }
6304:
6305: recno = 0;
1.1.1.2 misho 6306: while(IS_DIGIT(*ptr))
1.1 misho 6307: recno = recno * 10 + *ptr++ - CHAR_0;
6308:
1.1.1.4 ! misho 6309: if (*ptr != (pcre_uchar)terminator)
1.1 misho 6310: {
6311: *errorcodeptr = ERR29;
6312: goto FAILED;
6313: }
6314:
6315: if (refsign == CHAR_MINUS)
6316: {
6317: if (recno == 0)
6318: {
6319: *errorcodeptr = ERR58;
6320: goto FAILED;
6321: }
6322: recno = cd->bracount - recno + 1;
6323: if (recno <= 0)
6324: {
6325: *errorcodeptr = ERR15;
6326: goto FAILED;
6327: }
6328: }
6329: else if (refsign == CHAR_PLUS)
6330: {
6331: if (recno == 0)
6332: {
6333: *errorcodeptr = ERR58;
6334: goto FAILED;
6335: }
6336: recno += cd->bracount;
6337: }
6338:
6339: /* Come here from code above that handles a named recursion */
6340:
6341: HANDLE_RECURSION:
6342:
6343: previous = code;
6344: called = cd->start_code;
6345:
6346: /* When we are actually compiling, find the bracket that is being
6347: referenced. Temporarily end the regex in case it doesn't exist before
6348: this point. If we end up with a forward reference, first check that
6349: the bracket does occur later so we can give the error (and position)
6350: now. Then remember this forward reference in the workspace so it can
6351: be filled in at the end. */
6352:
6353: if (lengthptr == NULL)
6354: {
6355: *code = OP_END;
6356: if (recno != 0)
1.1.1.2 misho 6357: called = PRIV(find_bracket)(cd->start_code, utf, recno);
1.1 misho 6358:
6359: /* Forward reference */
6360:
6361: if (called == NULL)
6362: {
6363: if (find_parens(cd, NULL, recno,
1.1.1.2 misho 6364: (options & PCRE_EXTENDED) != 0, utf) < 0)
1.1 misho 6365: {
6366: *errorcodeptr = ERR15;
6367: goto FAILED;
6368: }
6369:
6370: /* Fudge the value of "called" so that when it is inserted as an
6371: offset below, what it actually inserted is the reference number
6372: of the group. Then remember the forward reference. */
6373:
6374: called = cd->start_code + recno;
6375: if (cd->hwm >= cd->start_workspace + cd->workspace_size -
6376: WORK_SIZE_SAFETY_MARGIN)
6377: {
6378: *errorcodeptr = expand_workspace(cd);
6379: if (*errorcodeptr != 0) goto FAILED;
6380: }
6381: PUTINC(cd->hwm, 0, (int)(code + 1 - cd->start_code));
6382: }
6383:
6384: /* If not a forward reference, and the subpattern is still open,
6385: this is a recursive call. We check to see if this is a left
6386: recursion that could loop for ever, and diagnose that case. We
6387: must not, however, do this check if we are in a conditional
6388: subpattern because the condition might be testing for recursion in
6389: a pattern such as /(?(R)a+|(?R)b)/, which is perfectly valid.
6390: Forever loops are also detected at runtime, so those that occur in
6391: conditional subpatterns will be picked up then. */
6392:
6393: else if (GET(called, 1) == 0 && cond_depth <= 0 &&
1.1.1.2 misho 6394: could_be_empty(called, code, bcptr, utf, cd))
1.1 misho 6395: {
6396: *errorcodeptr = ERR40;
6397: goto FAILED;
6398: }
6399: }
6400:
6401: /* Insert the recursion/subroutine item. It does not have a set first
1.1.1.2 misho 6402: character (relevant if it is repeated, because it will then be
6403: wrapped with ONCE brackets). */
1.1 misho 6404:
6405: *code = OP_RECURSE;
6406: PUT(code, 1, (int)(called - cd->start_code));
6407: code += 1 + LINK_SIZE;
1.1.1.2 misho 6408: groupsetfirstchar = FALSE;
1.1 misho 6409: }
6410:
6411: /* Can't determine a first byte now */
6412:
1.1.1.4 ! misho 6413: if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
1.1 misho 6414: continue;
6415:
6416:
6417: /* ------------------------------------------------------------ */
6418: default: /* Other characters: check option setting */
6419: OTHER_CHAR_AFTER_QUERY:
6420: set = unset = 0;
6421: optset = &set;
6422:
6423: while (*ptr != CHAR_RIGHT_PARENTHESIS && *ptr != CHAR_COLON)
6424: {
6425: switch (*ptr++)
6426: {
6427: case CHAR_MINUS: optset = &unset; break;
6428:
6429: case CHAR_J: /* Record that it changed in the external options */
6430: *optset |= PCRE_DUPNAMES;
6431: cd->external_flags |= PCRE_JCHANGED;
6432: break;
6433:
6434: case CHAR_i: *optset |= PCRE_CASELESS; break;
6435: case CHAR_m: *optset |= PCRE_MULTILINE; break;
6436: case CHAR_s: *optset |= PCRE_DOTALL; break;
6437: case CHAR_x: *optset |= PCRE_EXTENDED; break;
6438: case CHAR_U: *optset |= PCRE_UNGREEDY; break;
6439: case CHAR_X: *optset |= PCRE_EXTRA; break;
6440:
6441: default: *errorcodeptr = ERR12;
6442: ptr--; /* Correct the offset */
6443: goto FAILED;
6444: }
6445: }
6446:
6447: /* Set up the changed option bits, but don't change anything yet. */
6448:
6449: newoptions = (options | set) & (~unset);
6450:
6451: /* If the options ended with ')' this is not the start of a nested
6452: group with option changes, so the options change at this level. If this
6453: item is right at the start of the pattern, the options can be
6454: abstracted and made external in the pre-compile phase, and ignored in
6455: the compile phase. This can be helpful when matching -- for instance in
6456: caseless checking of required bytes.
6457:
6458: If the code pointer is not (cd->start_code + 1 + LINK_SIZE), we are
6459: definitely *not* at the start of the pattern because something has been
6460: compiled. In the pre-compile phase, however, the code pointer can have
6461: that value after the start, because it gets reset as code is discarded
6462: during the pre-compile. However, this can happen only at top level - if
6463: we are within parentheses, the starting BRA will still be present. At
6464: any parenthesis level, the length value can be used to test if anything
6465: has been compiled at that level. Thus, a test for both these conditions
6466: is necessary to ensure we correctly detect the start of the pattern in
6467: both phases.
6468:
6469: If we are not at the pattern start, reset the greedy defaults and the
1.1.1.2 misho 6470: case value for firstchar and reqchar. */
1.1 misho 6471:
6472: if (*ptr == CHAR_RIGHT_PARENTHESIS)
6473: {
6474: if (code == cd->start_code + 1 + LINK_SIZE &&
6475: (lengthptr == NULL || *lengthptr == 2 + 2*LINK_SIZE))
6476: {
6477: cd->external_options = newoptions;
6478: }
6479: else
6480: {
6481: greedy_default = ((newoptions & PCRE_UNGREEDY) != 0);
6482: greedy_non_default = greedy_default ^ 1;
1.1.1.2 misho 6483: req_caseopt = ((newoptions & PCRE_CASELESS) != 0)? REQ_CASELESS:0;
1.1 misho 6484: }
6485:
6486: /* Change options at this level, and pass them back for use
6487: in subsequent branches. */
6488:
6489: *optionsptr = options = newoptions;
6490: previous = NULL; /* This item can't be repeated */
6491: continue; /* It is complete */
6492: }
6493:
6494: /* If the options ended with ':' we are heading into a nested group
6495: with possible change of options. Such groups are non-capturing and are
6496: not assertions of any kind. All we need to do is skip over the ':';
6497: the newoptions value is handled below. */
6498:
6499: bravalue = OP_BRA;
6500: ptr++;
6501: } /* End of switch for character following (? */
6502: } /* End of (? handling */
6503:
6504: /* Opening parenthesis not followed by '*' or '?'. If PCRE_NO_AUTO_CAPTURE
6505: is set, all unadorned brackets become non-capturing and behave like (?:...)
6506: brackets. */
6507:
6508: else if ((options & PCRE_NO_AUTO_CAPTURE) != 0)
6509: {
6510: bravalue = OP_BRA;
6511: }
6512:
6513: /* Else we have a capturing group. */
6514:
6515: else
6516: {
6517: NUMBERED_GROUP:
6518: cd->bracount += 1;
6519: PUT2(code, 1+LINK_SIZE, cd->bracount);
1.1.1.2 misho 6520: skipbytes = IMM2_SIZE;
1.1 misho 6521: }
6522:
6523: /* Process nested bracketed regex. Assertions used not to be repeatable,
6524: but this was changed for Perl compatibility, so all kinds can now be
6525: repeated. We copy code into a non-register variable (tempcode) in order to
6526: be able to pass its address because some compilers complain otherwise. */
6527:
6528: previous = code; /* For handling repetition */
6529: *code = bravalue;
6530: tempcode = code;
6531: tempreqvary = cd->req_varyopt; /* Save value before bracket */
6532: tempbracount = cd->bracount; /* Save value before bracket */
6533: length_prevgroup = 0; /* Initialize for pre-compile phase */
6534:
6535: if (!compile_regex(
6536: newoptions, /* The complete new option state */
6537: &tempcode, /* Where to put code (updated) */
6538: &ptr, /* Input pointer (updated) */
6539: errorcodeptr, /* Where to put an error message */
6540: (bravalue == OP_ASSERTBACK ||
6541: bravalue == OP_ASSERTBACK_NOT), /* TRUE if back assert */
6542: reset_bracount, /* True if (?| group */
6543: skipbytes, /* Skip over bracket number */
6544: cond_depth +
6545: ((bravalue == OP_COND)?1:0), /* Depth of condition subpatterns */
1.1.1.2 misho 6546: &subfirstchar, /* For possible first char */
1.1.1.4 ! misho 6547: &subfirstcharflags,
1.1.1.2 misho 6548: &subreqchar, /* For possible last char */
1.1.1.4 ! misho 6549: &subreqcharflags,
1.1 misho 6550: bcptr, /* Current branch chain */
6551: cd, /* Tables block */
6552: (lengthptr == NULL)? NULL : /* Actual compile phase */
6553: &length_prevgroup /* Pre-compile phase */
6554: ))
6555: goto FAILED;
6556:
6557: /* If this was an atomic group and there are no capturing groups within it,
6558: generate OP_ONCE_NC instead of OP_ONCE. */
6559:
6560: if (bravalue == OP_ONCE && cd->bracount <= tempbracount)
6561: *code = OP_ONCE_NC;
6562:
6563: if (bravalue >= OP_ASSERT && bravalue <= OP_ASSERTBACK_NOT)
6564: cd->assert_depth -= 1;
6565:
6566: /* At the end of compiling, code is still pointing to the start of the
6567: group, while tempcode has been updated to point past the end of the group.
6568: The pattern pointer (ptr) is on the bracket.
6569:
6570: If this is a conditional bracket, check that there are no more than
6571: two branches in the group, or just one if it's a DEFINE group. We do this
6572: in the real compile phase, not in the pre-pass, where the whole group may
6573: not be available. */
6574:
6575: if (bravalue == OP_COND && lengthptr == NULL)
6576: {
1.1.1.2 misho 6577: pcre_uchar *tc = code;
1.1 misho 6578: int condcount = 0;
6579:
6580: do {
6581: condcount++;
6582: tc += GET(tc,1);
6583: }
6584: while (*tc != OP_KET);
6585:
6586: /* A DEFINE group is never obeyed inline (the "condition" is always
6587: false). It must have only one branch. */
6588:
6589: if (code[LINK_SIZE+1] == OP_DEF)
6590: {
6591: if (condcount > 1)
6592: {
6593: *errorcodeptr = ERR54;
6594: goto FAILED;
6595: }
6596: bravalue = OP_DEF; /* Just a flag to suppress char handling below */
6597: }
6598:
6599: /* A "normal" conditional group. If there is just one branch, we must not
1.1.1.2 misho 6600: make use of its firstchar or reqchar, because this is equivalent to an
1.1 misho 6601: empty second branch. */
6602:
6603: else
6604: {
6605: if (condcount > 2)
6606: {
6607: *errorcodeptr = ERR27;
6608: goto FAILED;
6609: }
1.1.1.4 ! misho 6610: if (condcount == 1) subfirstcharflags = subreqcharflags = REQ_NONE;
1.1 misho 6611: }
6612: }
6613:
6614: /* Error if hit end of pattern */
6615:
6616: if (*ptr != CHAR_RIGHT_PARENTHESIS)
6617: {
6618: *errorcodeptr = ERR14;
6619: goto FAILED;
6620: }
6621:
6622: /* In the pre-compile phase, update the length by the length of the group,
6623: less the brackets at either end. Then reduce the compiled code to just a
6624: set of non-capturing brackets so that it doesn't use much memory if it is
6625: duplicated by a quantifier.*/
6626:
6627: if (lengthptr != NULL)
6628: {
6629: if (OFLOW_MAX - *lengthptr < length_prevgroup - 2 - 2*LINK_SIZE)
6630: {
6631: *errorcodeptr = ERR20;
6632: goto FAILED;
6633: }
6634: *lengthptr += length_prevgroup - 2 - 2*LINK_SIZE;
6635: code++; /* This already contains bravalue */
6636: PUTINC(code, 0, 1 + LINK_SIZE);
6637: *code++ = OP_KET;
6638: PUTINC(code, 0, 1 + LINK_SIZE);
6639: break; /* No need to waste time with special character handling */
6640: }
6641:
6642: /* Otherwise update the main code pointer to the end of the group. */
6643:
6644: code = tempcode;
6645:
6646: /* For a DEFINE group, required and first character settings are not
6647: relevant. */
6648:
6649: if (bravalue == OP_DEF) break;
6650:
6651: /* Handle updating of the required and first characters for other types of
6652: group. Update for normal brackets of all kinds, and conditions with two
6653: branches (see code above). If the bracket is followed by a quantifier with
1.1.1.2 misho 6654: zero repeat, we have to back off. Hence the definition of zeroreqchar and
6655: zerofirstchar outside the main loop so that they can be accessed for the
1.1 misho 6656: back off. */
6657:
1.1.1.2 misho 6658: zeroreqchar = reqchar;
1.1.1.4 ! misho 6659: zeroreqcharflags = reqcharflags;
1.1.1.2 misho 6660: zerofirstchar = firstchar;
1.1.1.4 ! misho 6661: zerofirstcharflags = firstcharflags;
1.1.1.2 misho 6662: groupsetfirstchar = FALSE;
1.1 misho 6663:
6664: if (bravalue >= OP_ONCE)
6665: {
1.1.1.2 misho 6666: /* If we have not yet set a firstchar in this branch, take it from the
1.1 misho 6667: subpattern, remembering that it was set here so that a repeat of more
1.1.1.2 misho 6668: than one can replicate it as reqchar if necessary. If the subpattern has
6669: no firstchar, set "none" for the whole branch. In both cases, a zero
6670: repeat forces firstchar to "none". */
1.1 misho 6671:
1.1.1.4 ! misho 6672: if (firstcharflags == REQ_UNSET)
1.1 misho 6673: {
1.1.1.4 ! misho 6674: if (subfirstcharflags >= 0)
1.1 misho 6675: {
1.1.1.2 misho 6676: firstchar = subfirstchar;
1.1.1.4 ! misho 6677: firstcharflags = subfirstcharflags;
1.1.1.2 misho 6678: groupsetfirstchar = TRUE;
1.1 misho 6679: }
1.1.1.4 ! misho 6680: else firstcharflags = REQ_NONE;
! 6681: zerofirstcharflags = REQ_NONE;
1.1 misho 6682: }
6683:
1.1.1.2 misho 6684: /* If firstchar was previously set, convert the subpattern's firstchar
6685: into reqchar if there wasn't one, using the vary flag that was in
1.1 misho 6686: existence beforehand. */
6687:
1.1.1.4 ! misho 6688: else if (subfirstcharflags >= 0 && subreqcharflags < 0)
! 6689: {
! 6690: subreqchar = subfirstchar;
! 6691: subreqcharflags = subfirstcharflags | tempreqvary;
! 6692: }
1.1 misho 6693:
6694: /* If the subpattern set a required byte (or set a first byte that isn't
6695: really the first byte - see above), set it. */
6696:
1.1.1.4 ! misho 6697: if (subreqcharflags >= 0)
! 6698: {
! 6699: reqchar = subreqchar;
! 6700: reqcharflags = subreqcharflags;
! 6701: }
1.1 misho 6702: }
6703:
1.1.1.2 misho 6704: /* For a forward assertion, we take the reqchar, if set. This can be
1.1 misho 6705: helpful if the pattern that follows the assertion doesn't set a different
1.1.1.2 misho 6706: char. For example, it's useful for /(?=abcde).+/. We can't set firstchar
1.1 misho 6707: for an assertion, however because it leads to incorrect effect for patterns
1.1.1.2 misho 6708: such as /(?=a)a.+/ when the "real" "a" would then become a reqchar instead
6709: of a firstchar. This is overcome by a scan at the end if there's no
6710: firstchar, looking for an asserted first char. */
1.1 misho 6711:
1.1.1.4 ! misho 6712: else if (bravalue == OP_ASSERT && subreqcharflags >= 0)
! 6713: {
! 6714: reqchar = subreqchar;
! 6715: reqcharflags = subreqcharflags;
! 6716: }
1.1 misho 6717: break; /* End of processing '(' */
6718:
6719:
6720: /* ===================================================================*/
6721: /* Handle metasequences introduced by \. For ones like \d, the ESC_ values
6722: are arranged to be the negation of the corresponding OP_values in the
6723: default case when PCRE_UCP is not set. For the back references, the values
1.1.1.4 ! misho 6724: are negative the reference number. Only back references and those types
1.1 misho 6725: that consume a character may be repeated. We can test for values between
6726: ESC_b and ESC_Z for the latter; this may have to change if any new ones are
6727: ever created. */
6728:
6729: case CHAR_BACKSLASH:
6730: tempptr = ptr;
1.1.1.4 ! misho 6731: escape = check_escape(&ptr, &ec, errorcodeptr, cd->bracount, options, FALSE);
1.1 misho 6732: if (*errorcodeptr != 0) goto FAILED;
6733:
1.1.1.4 ! misho 6734: if (escape == 0) /* The escape coded a single character */
! 6735: c = ec;
! 6736: else
1.1 misho 6737: {
1.1.1.4 ! misho 6738: if (escape == ESC_Q) /* Handle start of quoted string */
1.1 misho 6739: {
6740: if (ptr[1] == CHAR_BACKSLASH && ptr[2] == CHAR_E)
6741: ptr += 2; /* avoid empty string */
6742: else inescq = TRUE;
6743: continue;
6744: }
6745:
1.1.1.4 ! misho 6746: if (escape == ESC_E) continue; /* Perl ignores an orphan \E */
1.1 misho 6747:
6748: /* For metasequences that actually match a character, we disable the
6749: setting of a first character if it hasn't already been set. */
6750:
1.1.1.4 ! misho 6751: if (firstcharflags == REQ_UNSET && escape > ESC_b && escape < ESC_Z)
! 6752: firstcharflags = REQ_NONE;
1.1 misho 6753:
6754: /* Set values to reset to if this is followed by a zero repeat. */
6755:
1.1.1.2 misho 6756: zerofirstchar = firstchar;
1.1.1.4 ! misho 6757: zerofirstcharflags = firstcharflags;
1.1.1.2 misho 6758: zeroreqchar = reqchar;
1.1.1.4 ! misho 6759: zeroreqcharflags = reqcharflags;
1.1 misho 6760:
6761: /* \g<name> or \g'name' is a subroutine call by name and \g<n> or \g'n'
6762: is a subroutine call by number (Oniguruma syntax). In fact, the value
1.1.1.4 ! misho 6763: ESC_g is returned only for these cases. So we don't need to check for <
! 6764: or ' if the value is ESC_g. For the Perl syntax \g{n} the value is
! 6765: -n, and for the Perl syntax \g{name} the result is ESC_k (as
1.1 misho 6766: that is a synonym for a named back reference). */
6767:
1.1.1.4 ! misho 6768: if (escape == ESC_g)
1.1 misho 6769: {
1.1.1.2 misho 6770: const pcre_uchar *p;
1.1 misho 6771: save_hwm = cd->hwm; /* Normally this is set when '(' is read */
6772: terminator = (*(++ptr) == CHAR_LESS_THAN_SIGN)?
6773: CHAR_GREATER_THAN_SIGN : CHAR_APOSTROPHE;
6774:
6775: /* These two statements stop the compiler for warning about possibly
6776: unset variables caused by the jump to HANDLE_NUMERICAL_RECURSION. In
6777: fact, because we actually check for a number below, the paths that
6778: would actually be in error are never taken. */
6779:
6780: skipbytes = 0;
6781: reset_bracount = FALSE;
6782:
6783: /* Test for a name */
6784:
6785: if (ptr[1] != CHAR_PLUS && ptr[1] != CHAR_MINUS)
6786: {
1.1.1.2 misho 6787: BOOL is_a_number = TRUE;
1.1.1.4 ! misho 6788: for (p = ptr + 1; *p != CHAR_NULL && *p != (pcre_uchar)terminator; p++)
1.1 misho 6789: {
1.1.1.2 misho 6790: if (!MAX_255(*p)) { is_a_number = FALSE; break; }
6791: if ((cd->ctypes[*p] & ctype_digit) == 0) is_a_number = FALSE;
1.1 misho 6792: if ((cd->ctypes[*p] & ctype_word) == 0) break;
6793: }
1.1.1.4 ! misho 6794: if (*p != (pcre_uchar)terminator)
1.1 misho 6795: {
6796: *errorcodeptr = ERR57;
6797: break;
6798: }
1.1.1.2 misho 6799: if (is_a_number)
1.1 misho 6800: {
6801: ptr++;
6802: goto HANDLE_NUMERICAL_RECURSION;
6803: }
6804: is_recurse = TRUE;
6805: goto NAMED_REF_OR_RECURSE;
6806: }
6807:
6808: /* Test a signed number in angle brackets or quotes. */
6809:
6810: p = ptr + 2;
1.1.1.2 misho 6811: while (IS_DIGIT(*p)) p++;
1.1.1.4 ! misho 6812: if (*p != (pcre_uchar)terminator)
1.1 misho 6813: {
6814: *errorcodeptr = ERR57;
6815: break;
6816: }
6817: ptr++;
6818: goto HANDLE_NUMERICAL_RECURSION;
6819: }
6820:
6821: /* \k<name> or \k'name' is a back reference by name (Perl syntax).
6822: We also support \k{name} (.NET syntax). */
6823:
1.1.1.4 ! misho 6824: if (escape == ESC_k)
1.1 misho 6825: {
6826: if ((ptr[1] != CHAR_LESS_THAN_SIGN &&
6827: ptr[1] != CHAR_APOSTROPHE && ptr[1] != CHAR_LEFT_CURLY_BRACKET))
6828: {
6829: *errorcodeptr = ERR69;
6830: break;
6831: }
6832: is_recurse = FALSE;
6833: terminator = (*(++ptr) == CHAR_LESS_THAN_SIGN)?
6834: CHAR_GREATER_THAN_SIGN : (*ptr == CHAR_APOSTROPHE)?
6835: CHAR_APOSTROPHE : CHAR_RIGHT_CURLY_BRACKET;
6836: goto NAMED_REF_OR_RECURSE;
6837: }
6838:
1.1.1.2 misho 6839: /* Back references are handled specially; must disable firstchar if
1.1 misho 6840: not set to cope with cases like (?=(\w+))\1: which would otherwise set
6841: ':' later. */
6842:
1.1.1.4 ! misho 6843: if (escape < 0)
1.1 misho 6844: {
6845: open_capitem *oc;
1.1.1.4 ! misho 6846: recno = -escape;
1.1 misho 6847:
6848: HANDLE_REFERENCE: /* Come here from named backref handling */
1.1.1.4 ! misho 6849: if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
1.1 misho 6850: previous = code;
6851: *code++ = ((options & PCRE_CASELESS) != 0)? OP_REFI : OP_REF;
6852: PUT2INC(code, 0, recno);
6853: cd->backref_map |= (recno < 32)? (1 << recno) : 1;
6854: if (recno > cd->top_backref) cd->top_backref = recno;
6855:
6856: /* Check to see if this back reference is recursive, that it, it
6857: is inside the group that it references. A flag is set so that the
6858: group can be made atomic. */
6859:
6860: for (oc = cd->open_caps; oc != NULL; oc = oc->next)
6861: {
6862: if (oc->number == recno)
6863: {
6864: oc->flag = TRUE;
6865: break;
6866: }
6867: }
6868: }
6869:
6870: /* So are Unicode property matches, if supported. */
6871:
6872: #ifdef SUPPORT_UCP
1.1.1.4 ! misho 6873: else if (escape == ESC_P || escape == ESC_p)
1.1 misho 6874: {
6875: BOOL negated;
1.1.1.4 ! misho 6876: unsigned int ptype = 0, pdata = 0;
! 6877: if (!get_ucp(&ptr, &negated, &ptype, &pdata, errorcodeptr))
! 6878: goto FAILED;
1.1 misho 6879: previous = code;
1.1.1.4 ! misho 6880: *code++ = ((escape == ESC_p) != negated)? OP_PROP : OP_NOTPROP;
1.1 misho 6881: *code++ = ptype;
6882: *code++ = pdata;
6883: }
6884: #else
6885:
6886: /* If Unicode properties are not supported, \X, \P, and \p are not
6887: allowed. */
6888:
1.1.1.4 ! misho 6889: else if (escape == ESC_X || escape == ESC_P || escape == ESC_p)
1.1 misho 6890: {
6891: *errorcodeptr = ERR45;
6892: goto FAILED;
6893: }
6894: #endif
6895:
6896: /* For the rest (including \X when Unicode properties are supported), we
6897: can obtain the OP value by negating the escape value in the default
6898: situation when PCRE_UCP is not set. When it *is* set, we substitute
1.1.1.3 misho 6899: Unicode property tests. Note that \b and \B do a one-character
1.1.1.4 ! misho 6900: lookbehind, and \A also behaves as if it does. */
1.1 misho 6901:
6902: else
6903: {
1.1.1.4 ! misho 6904: if ((escape == ESC_b || escape == ESC_B || escape == ESC_A) &&
! 6905: cd->max_lookbehind == 0)
1.1.1.3 misho 6906: cd->max_lookbehind = 1;
1.1 misho 6907: #ifdef SUPPORT_UCP
1.1.1.4 ! misho 6908: if (escape >= ESC_DU && escape <= ESC_wu)
1.1 misho 6909: {
6910: nestptr = ptr + 1; /* Where to resume */
1.1.1.4 ! misho 6911: ptr = substitutes[escape - ESC_DU] - 1; /* Just before substitute */
1.1 misho 6912: }
6913: else
6914: #endif
6915: /* In non-UTF-8 mode, we turn \C into OP_ALLANY instead of OP_ANYBYTE
6916: so that it works in DFA mode and in lookbehinds. */
6917:
6918: {
1.1.1.4 ! misho 6919: previous = (escape > ESC_b && escape < ESC_Z)? code : NULL;
! 6920: *code++ = (!utf && escape == ESC_C)? OP_ALLANY : escape;
1.1 misho 6921: }
6922: }
6923: continue;
6924: }
6925:
6926: /* We have a data character whose value is in c. In UTF-8 mode it may have
6927: a value > 127. We set its representation in the length/buffer, and then
6928: handle it as a data character. */
6929:
1.1.1.4 ! misho 6930: #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
1.1.1.2 misho 6931: if (utf && c > MAX_VALUE_FOR_SINGLE_CHAR)
6932: mclength = PRIV(ord2utf)(c, mcbuffer);
1.1 misho 6933: else
6934: #endif
6935:
6936: {
6937: mcbuffer[0] = c;
6938: mclength = 1;
6939: }
6940: goto ONE_CHAR;
6941:
6942:
6943: /* ===================================================================*/
6944: /* Handle a literal character. It is guaranteed not to be whitespace or #
6945: when the extended flag is set. If we are in UTF-8 mode, it may be a
6946: multi-byte literal character. */
6947:
6948: default:
6949: NORMAL_CHAR:
6950: mclength = 1;
6951: mcbuffer[0] = c;
6952:
1.1.1.2 misho 6953: #ifdef SUPPORT_UTF
6954: if (utf && HAS_EXTRALEN(c))
6955: ACROSSCHAR(TRUE, ptr[1], mcbuffer[mclength++] = *(++ptr));
1.1 misho 6956: #endif
6957:
6958: /* At this point we have the character's bytes in mcbuffer, and the length
6959: in mclength. When not in UTF-8 mode, the length is always 1. */
6960:
6961: ONE_CHAR:
6962: previous = code;
1.1.1.4 ! misho 6963:
! 6964: /* For caseless UTF-8 mode when UCP support is available, check whether
! 6965: this character has more than one other case. If so, generate a special
! 6966: OP_PROP item instead of OP_CHARI. */
! 6967:
! 6968: #ifdef SUPPORT_UCP
! 6969: if (utf && (options & PCRE_CASELESS) != 0)
! 6970: {
! 6971: GETCHAR(c, mcbuffer);
! 6972: if ((c = UCD_CASESET(c)) != 0)
! 6973: {
! 6974: *code++ = OP_PROP;
! 6975: *code++ = PT_CLIST;
! 6976: *code++ = c;
! 6977: if (firstcharflags == REQ_UNSET) firstcharflags = zerofirstcharflags = REQ_NONE;
! 6978: break;
! 6979: }
! 6980: }
! 6981: #endif
! 6982:
! 6983: /* Caseful matches, or not one of the multicase characters. */
! 6984:
1.1 misho 6985: *code++ = ((options & PCRE_CASELESS) != 0)? OP_CHARI : OP_CHAR;
6986: for (c = 0; c < mclength; c++) *code++ = mcbuffer[c];
6987:
6988: /* Remember if \r or \n were seen */
6989:
6990: if (mcbuffer[0] == CHAR_CR || mcbuffer[0] == CHAR_NL)
6991: cd->external_flags |= PCRE_HASCRORLF;
6992:
6993: /* Set the first and required bytes appropriately. If no previous first
6994: byte, set it from this character, but revert to none on a zero repeat.
1.1.1.2 misho 6995: Otherwise, leave the firstchar value alone, and don't change it on a zero
1.1 misho 6996: repeat. */
6997:
1.1.1.4 ! misho 6998: if (firstcharflags == REQ_UNSET)
1.1 misho 6999: {
1.1.1.4 ! misho 7000: zerofirstcharflags = REQ_NONE;
1.1.1.2 misho 7001: zeroreqchar = reqchar;
1.1.1.4 ! misho 7002: zeroreqcharflags = reqcharflags;
1.1 misho 7003:
1.1.1.2 misho 7004: /* If the character is more than one byte long, we can set firstchar
1.1 misho 7005: only if it is not to be matched caselessly. */
7006:
7007: if (mclength == 1 || req_caseopt == 0)
7008: {
1.1.1.2 misho 7009: firstchar = mcbuffer[0] | req_caseopt;
1.1.1.4 ! misho 7010: firstchar = mcbuffer[0];
! 7011: firstcharflags = req_caseopt;
! 7012:
! 7013: if (mclength != 1)
! 7014: {
! 7015: reqchar = code[-1];
! 7016: reqcharflags = cd->req_varyopt;
! 7017: }
1.1 misho 7018: }
1.1.1.4 ! misho 7019: else firstcharflags = reqcharflags = REQ_NONE;
1.1 misho 7020: }
7021:
1.1.1.2 misho 7022: /* firstchar was previously set; we can set reqchar only if the length is
1.1 misho 7023: 1 or the matching is caseful. */
7024:
7025: else
7026: {
1.1.1.2 misho 7027: zerofirstchar = firstchar;
1.1.1.4 ! misho 7028: zerofirstcharflags = firstcharflags;
1.1.1.2 misho 7029: zeroreqchar = reqchar;
1.1.1.4 ! misho 7030: zeroreqcharflags = reqcharflags;
1.1 misho 7031: if (mclength == 1 || req_caseopt == 0)
1.1.1.4 ! misho 7032: {
! 7033: reqchar = code[-1];
! 7034: reqcharflags = req_caseopt | cd->req_varyopt;
! 7035: }
1.1 misho 7036: }
7037:
7038: break; /* End of literal character handling */
7039: }
7040: } /* end of big loop */
7041:
7042:
7043: /* Control never reaches here by falling through, only by a goto for all the
7044: error states. Pass back the position in the pattern so that it can be displayed
7045: to the user for diagnosing the error. */
7046:
7047: FAILED:
7048: *ptrptr = ptr;
7049: return FALSE;
7050: }
7051:
7052:
7053:
7054: /*************************************************
7055: * Compile sequence of alternatives *
7056: *************************************************/
7057:
7058: /* On entry, ptr is pointing past the bracket character, but on return it
7059: points to the closing bracket, or vertical bar, or end of string. The code
7060: variable is pointing at the byte into which the BRA operator has been stored.
7061: This function is used during the pre-compile phase when we are trying to find
7062: out the amount of memory needed, as well as during the real compile phase. The
7063: value of lengthptr distinguishes the two phases.
7064:
7065: Arguments:
7066: options option bits, including any changes for this subpattern
7067: codeptr -> the address of the current code pointer
7068: ptrptr -> the address of the current pattern pointer
7069: errorcodeptr -> pointer to error code variable
7070: lookbehind TRUE if this is a lookbehind assertion
7071: reset_bracount TRUE to reset the count for each branch
7072: skipbytes skip this many bytes at start (for brackets and OP_COND)
7073: cond_depth depth of nesting for conditional subpatterns
1.1.1.4 ! misho 7074: firstcharptr place to put the first required character
! 7075: firstcharflagsptr place to put the first character flags, or a negative number
! 7076: reqcharptr place to put the last required character
! 7077: reqcharflagsptr place to put the last required character flags, or a negative number
1.1 misho 7078: bcptr pointer to the chain of currently open branches
7079: cd points to the data block with tables pointers etc.
7080: lengthptr NULL during the real compile phase
7081: points to length accumulator during pre-compile phase
7082:
7083: Returns: TRUE on success
7084: */
7085:
7086: static BOOL
1.1.1.2 misho 7087: compile_regex(int options, pcre_uchar **codeptr, const pcre_uchar **ptrptr,
1.1 misho 7088: int *errorcodeptr, BOOL lookbehind, BOOL reset_bracount, int skipbytes,
1.1.1.4 ! misho 7089: int cond_depth,
! 7090: pcre_uint32 *firstcharptr, pcre_int32 *firstcharflagsptr,
! 7091: pcre_uint32 *reqcharptr, pcre_int32 *reqcharflagsptr,
1.1.1.2 misho 7092: branch_chain *bcptr, compile_data *cd, int *lengthptr)
1.1 misho 7093: {
1.1.1.2 misho 7094: const pcre_uchar *ptr = *ptrptr;
7095: pcre_uchar *code = *codeptr;
7096: pcre_uchar *last_branch = code;
7097: pcre_uchar *start_bracket = code;
7098: pcre_uchar *reverse_count = NULL;
1.1 misho 7099: open_capitem capitem;
7100: int capnumber = 0;
1.1.1.4 ! misho 7101: pcre_uint32 firstchar, reqchar;
! 7102: pcre_int32 firstcharflags, reqcharflags;
! 7103: pcre_uint32 branchfirstchar, branchreqchar;
! 7104: pcre_int32 branchfirstcharflags, branchreqcharflags;
1.1 misho 7105: int length;
1.1.1.4 ! misho 7106: unsigned int orig_bracount;
! 7107: unsigned int max_bracount;
1.1 misho 7108: branch_chain bc;
7109:
7110: bc.outer = bcptr;
7111: bc.current_branch = code;
7112:
1.1.1.4 ! misho 7113: firstchar = reqchar = 0;
! 7114: firstcharflags = reqcharflags = REQ_UNSET;
1.1 misho 7115:
7116: /* Accumulate the length for use in the pre-compile phase. Start with the
7117: length of the BRA and KET and any extra bytes that are required at the
7118: beginning. We accumulate in a local variable to save frequent testing of
7119: lenthptr for NULL. We cannot do this by looking at the value of code at the
7120: start and end of each alternative, because compiled items are discarded during
7121: the pre-compile phase so that the work space is not exceeded. */
7122:
7123: length = 2 + 2*LINK_SIZE + skipbytes;
7124:
7125: /* WARNING: If the above line is changed for any reason, you must also change
7126: the code that abstracts option settings at the start of the pattern and makes
7127: them global. It tests the value of length for (2 + 2*LINK_SIZE) in the
7128: pre-compile phase to find out whether anything has yet been compiled or not. */
7129:
7130: /* If this is a capturing subpattern, add to the chain of open capturing items
7131: so that we can detect them if (*ACCEPT) is encountered. This is also used to
7132: detect groups that contain recursive back references to themselves. Note that
7133: only OP_CBRA need be tested here; changing this opcode to one of its variants,
7134: e.g. OP_SCBRAPOS, happens later, after the group has been compiled. */
7135:
7136: if (*code == OP_CBRA)
7137: {
7138: capnumber = GET2(code, 1 + LINK_SIZE);
7139: capitem.number = capnumber;
7140: capitem.next = cd->open_caps;
7141: capitem.flag = FALSE;
7142: cd->open_caps = &capitem;
7143: }
7144:
7145: /* Offset is set zero to mark that this bracket is still open */
7146:
7147: PUT(code, 1, 0);
7148: code += 1 + LINK_SIZE + skipbytes;
7149:
7150: /* Loop for each alternative branch */
7151:
7152: orig_bracount = max_bracount = cd->bracount;
7153: for (;;)
7154: {
7155: /* For a (?| group, reset the capturing bracket count so that each branch
7156: uses the same numbers. */
7157:
7158: if (reset_bracount) cd->bracount = orig_bracount;
7159:
7160: /* Set up dummy OP_REVERSE if lookbehind assertion */
7161:
7162: if (lookbehind)
7163: {
7164: *code++ = OP_REVERSE;
7165: reverse_count = code;
7166: PUTINC(code, 0, 0);
7167: length += 1 + LINK_SIZE;
7168: }
7169:
7170: /* Now compile the branch; in the pre-compile phase its length gets added
7171: into the length. */
7172:
1.1.1.2 misho 7173: if (!compile_branch(&options, &code, &ptr, errorcodeptr, &branchfirstchar,
1.1.1.4 ! misho 7174: &branchfirstcharflags, &branchreqchar, &branchreqcharflags, &bc,
! 7175: cond_depth, cd, (lengthptr == NULL)? NULL : &length))
1.1 misho 7176: {
7177: *ptrptr = ptr;
7178: return FALSE;
7179: }
7180:
7181: /* Keep the highest bracket count in case (?| was used and some branch
7182: has fewer than the rest. */
7183:
7184: if (cd->bracount > max_bracount) max_bracount = cd->bracount;
7185:
7186: /* In the real compile phase, there is some post-processing to be done. */
7187:
7188: if (lengthptr == NULL)
7189: {
1.1.1.2 misho 7190: /* If this is the first branch, the firstchar and reqchar values for the
1.1 misho 7191: branch become the values for the regex. */
7192:
7193: if (*last_branch != OP_ALT)
7194: {
1.1.1.2 misho 7195: firstchar = branchfirstchar;
1.1.1.4 ! misho 7196: firstcharflags = branchfirstcharflags;
1.1.1.2 misho 7197: reqchar = branchreqchar;
1.1.1.4 ! misho 7198: reqcharflags = branchreqcharflags;
1.1 misho 7199: }
7200:
1.1.1.2 misho 7201: /* If this is not the first branch, the first char and reqchar have to
1.1 misho 7202: match the values from all the previous branches, except that if the
1.1.1.2 misho 7203: previous value for reqchar didn't have REQ_VARY set, it can still match,
1.1 misho 7204: and we set REQ_VARY for the regex. */
7205:
7206: else
7207: {
1.1.1.2 misho 7208: /* If we previously had a firstchar, but it doesn't match the new branch,
7209: we have to abandon the firstchar for the regex, but if there was
7210: previously no reqchar, it takes on the value of the old firstchar. */
1.1 misho 7211:
1.1.1.4 ! misho 7212: if (firstcharflags >= 0 &&
! 7213: (firstcharflags != branchfirstcharflags || firstchar != branchfirstchar))
1.1 misho 7214: {
1.1.1.4 ! misho 7215: if (reqcharflags < 0)
! 7216: {
! 7217: reqchar = firstchar;
! 7218: reqcharflags = firstcharflags;
! 7219: }
! 7220: firstcharflags = REQ_NONE;
1.1 misho 7221: }
7222:
1.1.1.2 misho 7223: /* If we (now or from before) have no firstchar, a firstchar from the
7224: branch becomes a reqchar if there isn't a branch reqchar. */
1.1 misho 7225:
1.1.1.4 ! misho 7226: if (firstcharflags < 0 && branchfirstcharflags >= 0 && branchreqcharflags < 0)
! 7227: {
! 7228: branchreqchar = branchfirstchar;
! 7229: branchreqcharflags = branchfirstcharflags;
! 7230: }
1.1 misho 7231:
1.1.1.2 misho 7232: /* Now ensure that the reqchars match */
1.1 misho 7233:
1.1.1.4 ! misho 7234: if (((reqcharflags & ~REQ_VARY) != (branchreqcharflags & ~REQ_VARY)) ||
! 7235: reqchar != branchreqchar)
! 7236: reqcharflags = REQ_NONE;
! 7237: else
! 7238: {
! 7239: reqchar = branchreqchar;
! 7240: reqcharflags |= branchreqcharflags; /* To "or" REQ_VARY */
! 7241: }
1.1 misho 7242: }
7243:
7244: /* If lookbehind, check that this branch matches a fixed-length string, and
7245: put the length into the OP_REVERSE item. Temporarily mark the end of the
7246: branch with OP_END. If the branch contains OP_RECURSE, the result is -3
7247: because there may be forward references that we can't check here. Set a
7248: flag to cause another lookbehind check at the end. Why not do it all at the
7249: end? Because common, erroneous checks are picked up here and the offset of
7250: the problem can be shown. */
7251:
7252: if (lookbehind)
7253: {
7254: int fixed_length;
7255: *code = OP_END;
7256: fixed_length = find_fixedlength(last_branch, (options & PCRE_UTF8) != 0,
7257: FALSE, cd);
7258: DPRINTF(("fixed length = %d\n", fixed_length));
7259: if (fixed_length == -3)
7260: {
7261: cd->check_lookbehind = TRUE;
7262: }
7263: else if (fixed_length < 0)
7264: {
7265: *errorcodeptr = (fixed_length == -2)? ERR36 :
7266: (fixed_length == -4)? ERR70: ERR25;
7267: *ptrptr = ptr;
7268: return FALSE;
7269: }
1.1.1.3 misho 7270: else
7271: {
7272: if (fixed_length > cd->max_lookbehind)
7273: cd->max_lookbehind = fixed_length;
7274: PUT(reverse_count, 0, fixed_length);
7275: }
1.1 misho 7276: }
7277: }
7278:
7279: /* Reached end of expression, either ')' or end of pattern. In the real
7280: compile phase, go back through the alternative branches and reverse the chain
7281: of offsets, with the field in the BRA item now becoming an offset to the
7282: first alternative. If there are no alternatives, it points to the end of the
7283: group. The length in the terminating ket is always the length of the whole
7284: bracketed item. Return leaving the pointer at the terminating char. */
7285:
7286: if (*ptr != CHAR_VERTICAL_LINE)
7287: {
7288: if (lengthptr == NULL)
7289: {
7290: int branch_length = (int)(code - last_branch);
7291: do
7292: {
7293: int prev_length = GET(last_branch, 1);
7294: PUT(last_branch, 1, branch_length);
7295: branch_length = prev_length;
7296: last_branch -= branch_length;
7297: }
7298: while (branch_length > 0);
7299: }
7300:
7301: /* Fill in the ket */
7302:
7303: *code = OP_KET;
7304: PUT(code, 1, (int)(code - start_bracket));
7305: code += 1 + LINK_SIZE;
7306:
7307: /* If it was a capturing subpattern, check to see if it contained any
7308: recursive back references. If so, we must wrap it in atomic brackets.
7309: In any event, remove the block from the chain. */
7310:
7311: if (capnumber > 0)
7312: {
7313: if (cd->open_caps->flag)
7314: {
7315: memmove(start_bracket + 1 + LINK_SIZE, start_bracket,
1.1.1.2 misho 7316: IN_UCHARS(code - start_bracket));
1.1 misho 7317: *start_bracket = OP_ONCE;
7318: code += 1 + LINK_SIZE;
7319: PUT(start_bracket, 1, (int)(code - start_bracket));
7320: *code = OP_KET;
7321: PUT(code, 1, (int)(code - start_bracket));
7322: code += 1 + LINK_SIZE;
7323: length += 2 + 2*LINK_SIZE;
7324: }
7325: cd->open_caps = cd->open_caps->next;
7326: }
7327:
7328: /* Retain the highest bracket number, in case resetting was used. */
7329:
7330: cd->bracount = max_bracount;
7331:
7332: /* Set values to pass back */
7333:
7334: *codeptr = code;
7335: *ptrptr = ptr;
1.1.1.2 misho 7336: *firstcharptr = firstchar;
1.1.1.4 ! misho 7337: *firstcharflagsptr = firstcharflags;
1.1.1.2 misho 7338: *reqcharptr = reqchar;
1.1.1.4 ! misho 7339: *reqcharflagsptr = reqcharflags;
1.1 misho 7340: if (lengthptr != NULL)
7341: {
7342: if (OFLOW_MAX - *lengthptr < length)
7343: {
7344: *errorcodeptr = ERR20;
7345: return FALSE;
7346: }
7347: *lengthptr += length;
7348: }
7349: return TRUE;
7350: }
7351:
7352: /* Another branch follows. In the pre-compile phase, we can move the code
7353: pointer back to where it was for the start of the first branch. (That is,
7354: pretend that each branch is the only one.)
7355:
7356: In the real compile phase, insert an ALT node. Its length field points back
7357: to the previous branch while the bracket remains open. At the end the chain
7358: is reversed. It's done like this so that the start of the bracket has a
7359: zero offset until it is closed, making it possible to detect recursion. */
7360:
7361: if (lengthptr != NULL)
7362: {
7363: code = *codeptr + 1 + LINK_SIZE + skipbytes;
7364: length += 1 + LINK_SIZE;
7365: }
7366: else
7367: {
7368: *code = OP_ALT;
7369: PUT(code, 1, (int)(code - last_branch));
7370: bc.current_branch = last_branch = code;
7371: code += 1 + LINK_SIZE;
7372: }
7373:
7374: ptr++;
7375: }
7376: /* Control never reaches here */
7377: }
7378:
7379:
7380:
7381:
7382: /*************************************************
7383: * Check for anchored expression *
7384: *************************************************/
7385:
7386: /* Try to find out if this is an anchored regular expression. Consider each
7387: alternative branch. If they all start with OP_SOD or OP_CIRC, or with a bracket
7388: all of whose alternatives start with OP_SOD or OP_CIRC (recurse ad lib), then
7389: it's anchored. However, if this is a multiline pattern, then only OP_SOD will
7390: be found, because ^ generates OP_CIRCM in that mode.
7391:
7392: We can also consider a regex to be anchored if OP_SOM starts all its branches.
7393: This is the code for \G, which means "match at start of match position, taking
7394: into account the match offset".
7395:
7396: A branch is also implicitly anchored if it starts with .* and DOTALL is set,
7397: because that will try the rest of the pattern at all possible matching points,
7398: so there is no point trying again.... er ....
7399:
7400: .... except when the .* appears inside capturing parentheses, and there is a
7401: subsequent back reference to those parentheses. We haven't enough information
7402: to catch that case precisely.
7403:
7404: At first, the best we could do was to detect when .* was in capturing brackets
7405: and the highest back reference was greater than or equal to that level.
7406: However, by keeping a bitmap of the first 31 back references, we can catch some
7407: of the more common cases more precisely.
7408:
1.1.1.4 ! misho 7409: ... A second exception is when the .* appears inside an atomic group, because
! 7410: this prevents the number of characters it matches from being adjusted.
! 7411:
1.1 misho 7412: Arguments:
7413: code points to start of expression (the bracket)
7414: bracket_map a bitmap of which brackets we are inside while testing; this
7415: handles up to substring 31; after that we just have to take
7416: the less precise approach
1.1.1.4 ! misho 7417: cd points to the compile data block
! 7418: atomcount atomic group level
1.1 misho 7419:
7420: Returns: TRUE or FALSE
7421: */
7422:
7423: static BOOL
1.1.1.2 misho 7424: is_anchored(register const pcre_uchar *code, unsigned int bracket_map,
1.1.1.4 ! misho 7425: compile_data *cd, int atomcount)
1.1 misho 7426: {
7427: do {
1.1.1.2 misho 7428: const pcre_uchar *scode = first_significant_code(
7429: code + PRIV(OP_lengths)[*code], FALSE);
1.1 misho 7430: register int op = *scode;
7431:
7432: /* Non-capturing brackets */
7433:
7434: if (op == OP_BRA || op == OP_BRAPOS ||
7435: op == OP_SBRA || op == OP_SBRAPOS)
7436: {
1.1.1.4 ! misho 7437: if (!is_anchored(scode, bracket_map, cd, atomcount)) return FALSE;
1.1 misho 7438: }
7439:
7440: /* Capturing brackets */
7441:
7442: else if (op == OP_CBRA || op == OP_CBRAPOS ||
7443: op == OP_SCBRA || op == OP_SCBRAPOS)
7444: {
7445: int n = GET2(scode, 1+LINK_SIZE);
7446: int new_map = bracket_map | ((n < 32)? (1 << n) : 1);
1.1.1.4 ! misho 7447: if (!is_anchored(scode, new_map, cd, atomcount)) return FALSE;
! 7448: }
! 7449:
! 7450: /* Positive forward assertions and conditions */
! 7451:
! 7452: else if (op == OP_ASSERT || op == OP_COND)
! 7453: {
! 7454: if (!is_anchored(scode, bracket_map, cd, atomcount)) return FALSE;
1.1 misho 7455: }
7456:
1.1.1.4 ! misho 7457: /* Atomic groups */
1.1 misho 7458:
1.1.1.4 ! misho 7459: else if (op == OP_ONCE || op == OP_ONCE_NC)
1.1 misho 7460: {
1.1.1.4 ! misho 7461: if (!is_anchored(scode, bracket_map, cd, atomcount + 1))
! 7462: return FALSE;
1.1 misho 7463: }
7464:
7465: /* .* is not anchored unless DOTALL is set (which generates OP_ALLANY) and
1.1.1.4 ! misho 7466: it isn't in brackets that are or may be referenced or inside an atomic
! 7467: group. */
1.1 misho 7468:
7469: else if ((op == OP_TYPESTAR || op == OP_TYPEMINSTAR ||
7470: op == OP_TYPEPOSSTAR))
7471: {
1.1.1.4 ! misho 7472: if (scode[1] != OP_ALLANY || (bracket_map & cd->backref_map) != 0 ||
! 7473: atomcount > 0 || cd->had_pruneorskip)
1.1 misho 7474: return FALSE;
7475: }
7476:
7477: /* Check for explicit anchoring */
7478:
7479: else if (op != OP_SOD && op != OP_SOM && op != OP_CIRC) return FALSE;
1.1.1.4 ! misho 7480:
1.1 misho 7481: code += GET(code, 1);
7482: }
7483: while (*code == OP_ALT); /* Loop for each alternative */
7484: return TRUE;
7485: }
7486:
7487:
7488:
7489: /*************************************************
7490: * Check for starting with ^ or .* *
7491: *************************************************/
7492:
7493: /* This is called to find out if every branch starts with ^ or .* so that
7494: "first char" processing can be done to speed things up in multiline
7495: matching and for non-DOTALL patterns that start with .* (which must start at
7496: the beginning or after \n). As in the case of is_anchored() (see above), we
7497: have to take account of back references to capturing brackets that contain .*
1.1.1.4 ! misho 7498: because in that case we can't make the assumption. Also, the appearance of .*
! 7499: inside atomic brackets or in a pattern that contains *PRUNE or *SKIP does not
! 7500: count, because once again the assumption no longer holds.
1.1 misho 7501:
7502: Arguments:
7503: code points to start of expression (the bracket)
7504: bracket_map a bitmap of which brackets we are inside while testing; this
7505: handles up to substring 31; after that we just have to take
7506: the less precise approach
1.1.1.4 ! misho 7507: cd points to the compile data
! 7508: atomcount atomic group level
1.1 misho 7509:
7510: Returns: TRUE or FALSE
7511: */
7512:
7513: static BOOL
1.1.1.2 misho 7514: is_startline(const pcre_uchar *code, unsigned int bracket_map,
1.1.1.4 ! misho 7515: compile_data *cd, int atomcount)
1.1 misho 7516: {
7517: do {
1.1.1.2 misho 7518: const pcre_uchar *scode = first_significant_code(
7519: code + PRIV(OP_lengths)[*code], FALSE);
1.1 misho 7520: register int op = *scode;
7521:
7522: /* If we are at the start of a conditional assertion group, *both* the
7523: conditional assertion *and* what follows the condition must satisfy the test
7524: for start of line. Other kinds of condition fail. Note that there may be an
7525: auto-callout at the start of a condition. */
7526:
7527: if (op == OP_COND)
7528: {
7529: scode += 1 + LINK_SIZE;
1.1.1.2 misho 7530: if (*scode == OP_CALLOUT) scode += PRIV(OP_lengths)[OP_CALLOUT];
1.1 misho 7531: switch (*scode)
7532: {
7533: case OP_CREF:
7534: case OP_NCREF:
7535: case OP_RREF:
7536: case OP_NRREF:
7537: case OP_DEF:
7538: return FALSE;
7539:
7540: default: /* Assertion */
1.1.1.4 ! misho 7541: if (!is_startline(scode, bracket_map, cd, atomcount)) return FALSE;
1.1 misho 7542: do scode += GET(scode, 1); while (*scode == OP_ALT);
7543: scode += 1 + LINK_SIZE;
7544: break;
7545: }
7546: scode = first_significant_code(scode, FALSE);
7547: op = *scode;
7548: }
7549:
7550: /* Non-capturing brackets */
7551:
7552: if (op == OP_BRA || op == OP_BRAPOS ||
7553: op == OP_SBRA || op == OP_SBRAPOS)
7554: {
1.1.1.4 ! misho 7555: if (!is_startline(scode, bracket_map, cd, atomcount)) return FALSE;
1.1 misho 7556: }
7557:
7558: /* Capturing brackets */
7559:
7560: else if (op == OP_CBRA || op == OP_CBRAPOS ||
7561: op == OP_SCBRA || op == OP_SCBRAPOS)
7562: {
7563: int n = GET2(scode, 1+LINK_SIZE);
7564: int new_map = bracket_map | ((n < 32)? (1 << n) : 1);
1.1.1.4 ! misho 7565: if (!is_startline(scode, new_map, cd, atomcount)) return FALSE;
! 7566: }
! 7567:
! 7568: /* Positive forward assertions */
! 7569:
! 7570: else if (op == OP_ASSERT)
! 7571: {
! 7572: if (!is_startline(scode, bracket_map, cd, atomcount)) return FALSE;
1.1 misho 7573: }
7574:
1.1.1.4 ! misho 7575: /* Atomic brackets */
1.1 misho 7576:
1.1.1.4 ! misho 7577: else if (op == OP_ONCE || op == OP_ONCE_NC)
1.1 misho 7578: {
1.1.1.4 ! misho 7579: if (!is_startline(scode, bracket_map, cd, atomcount + 1)) return FALSE;
1.1 misho 7580: }
7581:
1.1.1.4 ! misho 7582: /* .* means "start at start or after \n" if it isn't in atomic brackets or
! 7583: brackets that may be referenced, as long as the pattern does not contain
! 7584: *PRUNE or *SKIP, because these break the feature. Consider, for example,
! 7585: /.*?a(*PRUNE)b/ with the subject "aab", which matches "ab", i.e. not at the
! 7586: start of a line. */
1.1 misho 7587:
7588: else if (op == OP_TYPESTAR || op == OP_TYPEMINSTAR || op == OP_TYPEPOSSTAR)
7589: {
1.1.1.4 ! misho 7590: if (scode[1] != OP_ANY || (bracket_map & cd->backref_map) != 0 ||
! 7591: atomcount > 0 || cd->had_pruneorskip)
! 7592: return FALSE;
1.1 misho 7593: }
7594:
1.1.1.4 ! misho 7595: /* Check for explicit circumflex; anything else gives a FALSE result. Note
! 7596: in particular that this includes atomic brackets OP_ONCE and OP_ONCE_NC
! 7597: because the number of characters matched by .* cannot be adjusted inside
! 7598: them. */
1.1 misho 7599:
7600: else if (op != OP_CIRC && op != OP_CIRCM) return FALSE;
7601:
7602: /* Move on to the next alternative */
7603:
7604: code += GET(code, 1);
7605: }
7606: while (*code == OP_ALT); /* Loop for each alternative */
7607: return TRUE;
7608: }
7609:
7610:
7611:
7612: /*************************************************
7613: * Check for asserted fixed first char *
7614: *************************************************/
7615:
7616: /* During compilation, the "first char" settings from forward assertions are
7617: discarded, because they can cause conflicts with actual literals that follow.
7618: However, if we end up without a first char setting for an unanchored pattern,
7619: it is worth scanning the regex to see if there is an initial asserted first
7620: char. If all branches start with the same asserted char, or with a bracket all
7621: of whose alternatives start with the same asserted char (recurse ad lib), then
7622: we return that char, otherwise -1.
7623:
7624: Arguments:
7625: code points to start of expression (the bracket)
1.1.1.4 ! misho 7626: flags points to the first char flags, or to REQ_NONE
1.1 misho 7627: inassert TRUE if in an assertion
7628:
1.1.1.4 ! misho 7629: Returns: the fixed first char, or 0 with REQ_NONE in flags
1.1 misho 7630: */
7631:
1.1.1.4 ! misho 7632: static pcre_uint32
! 7633: find_firstassertedchar(const pcre_uchar *code, pcre_int32 *flags,
! 7634: BOOL inassert)
1.1 misho 7635: {
1.1.1.4 ! misho 7636: register pcre_uint32 c = 0;
! 7637: int cflags = REQ_NONE;
! 7638:
! 7639: *flags = REQ_NONE;
1.1 misho 7640: do {
1.1.1.4 ! misho 7641: pcre_uint32 d;
! 7642: int dflags;
1.1 misho 7643: int xl = (*code == OP_CBRA || *code == OP_SCBRA ||
1.1.1.2 misho 7644: *code == OP_CBRAPOS || *code == OP_SCBRAPOS)? IMM2_SIZE:0;
7645: const pcre_uchar *scode = first_significant_code(code + 1+LINK_SIZE + xl,
7646: TRUE);
1.1.1.4 ! misho 7647: register pcre_uchar op = *scode;
1.1 misho 7648:
7649: switch(op)
7650: {
7651: default:
1.1.1.4 ! misho 7652: return 0;
1.1 misho 7653:
7654: case OP_BRA:
7655: case OP_BRAPOS:
7656: case OP_CBRA:
7657: case OP_SCBRA:
7658: case OP_CBRAPOS:
7659: case OP_SCBRAPOS:
7660: case OP_ASSERT:
7661: case OP_ONCE:
7662: case OP_ONCE_NC:
7663: case OP_COND:
1.1.1.4 ! misho 7664: d = find_firstassertedchar(scode, &dflags, op == OP_ASSERT);
! 7665: if (dflags < 0)
! 7666: return 0;
! 7667: if (cflags < 0) { c = d; cflags = dflags; } else if (c != d || cflags != dflags) return 0;
1.1 misho 7668: break;
7669:
7670: case OP_EXACT:
1.1.1.2 misho 7671: scode += IMM2_SIZE;
1.1 misho 7672: /* Fall through */
7673:
7674: case OP_CHAR:
7675: case OP_PLUS:
7676: case OP_MINPLUS:
7677: case OP_POSPLUS:
1.1.1.4 ! misho 7678: if (!inassert) return 0;
! 7679: if (cflags < 0) { c = scode[1]; cflags = 0; }
! 7680: else if (c != scode[1]) return 0;
1.1 misho 7681: break;
7682:
7683: case OP_EXACTI:
1.1.1.2 misho 7684: scode += IMM2_SIZE;
1.1 misho 7685: /* Fall through */
7686:
7687: case OP_CHARI:
7688: case OP_PLUSI:
7689: case OP_MINPLUSI:
7690: case OP_POSPLUSI:
1.1.1.4 ! misho 7691: if (!inassert) return 0;
! 7692: if (cflags < 0) { c = scode[1]; cflags = REQ_CASELESS; }
! 7693: else if (c != scode[1]) return 0;
1.1 misho 7694: break;
7695: }
7696:
7697: code += GET(code, 1);
7698: }
7699: while (*code == OP_ALT);
1.1.1.4 ! misho 7700:
! 7701: *flags = cflags;
1.1 misho 7702: return c;
7703: }
7704:
7705:
7706:
7707: /*************************************************
7708: * Compile a Regular Expression *
7709: *************************************************/
7710:
7711: /* This function takes a string and returns a pointer to a block of store
7712: holding a compiled version of the expression. The original API for this
7713: function had no error code return variable; it is retained for backwards
7714: compatibility. The new function is given a new name.
7715:
7716: Arguments:
7717: pattern the regular expression
7718: options various option bits
7719: errorcodeptr pointer to error code variable (pcre_compile2() only)
7720: can be NULL if you don't want a code value
7721: errorptr pointer to pointer to error text
7722: erroroffset ptr offset in pattern where error was detected
7723: tables pointer to character tables or NULL
7724:
7725: Returns: pointer to compiled data block, or NULL on error,
7726: with errorptr and erroroffset set
7727: */
7728:
1.1.1.4 ! misho 7729: #if defined COMPILE_PCRE8
1.1 misho 7730: PCRE_EXP_DEFN pcre * PCRE_CALL_CONVENTION
7731: pcre_compile(const char *pattern, int options, const char **errorptr,
7732: int *erroroffset, const unsigned char *tables)
1.1.1.4 ! misho 7733: #elif defined COMPILE_PCRE16
1.1.1.2 misho 7734: PCRE_EXP_DEFN pcre16 * PCRE_CALL_CONVENTION
7735: pcre16_compile(PCRE_SPTR16 pattern, int options, const char **errorptr,
7736: int *erroroffset, const unsigned char *tables)
1.1.1.4 ! misho 7737: #elif defined COMPILE_PCRE32
! 7738: PCRE_EXP_DEFN pcre32 * PCRE_CALL_CONVENTION
! 7739: pcre32_compile(PCRE_SPTR32 pattern, int options, const char **errorptr,
! 7740: int *erroroffset, const unsigned char *tables)
1.1.1.2 misho 7741: #endif
1.1 misho 7742: {
1.1.1.4 ! misho 7743: #if defined COMPILE_PCRE8
1.1 misho 7744: return pcre_compile2(pattern, options, NULL, errorptr, erroroffset, tables);
1.1.1.4 ! misho 7745: #elif defined COMPILE_PCRE16
1.1.1.2 misho 7746: return pcre16_compile2(pattern, options, NULL, errorptr, erroroffset, tables);
1.1.1.4 ! misho 7747: #elif defined COMPILE_PCRE32
! 7748: return pcre32_compile2(pattern, options, NULL, errorptr, erroroffset, tables);
1.1.1.2 misho 7749: #endif
1.1 misho 7750: }
7751:
7752:
1.1.1.4 ! misho 7753: #if defined COMPILE_PCRE8
1.1 misho 7754: PCRE_EXP_DEFN pcre * PCRE_CALL_CONVENTION
7755: pcre_compile2(const char *pattern, int options, int *errorcodeptr,
7756: const char **errorptr, int *erroroffset, const unsigned char *tables)
1.1.1.4 ! misho 7757: #elif defined COMPILE_PCRE16
1.1.1.2 misho 7758: PCRE_EXP_DEFN pcre16 * PCRE_CALL_CONVENTION
7759: pcre16_compile2(PCRE_SPTR16 pattern, int options, int *errorcodeptr,
7760: const char **errorptr, int *erroroffset, const unsigned char *tables)
1.1.1.4 ! misho 7761: #elif defined COMPILE_PCRE32
! 7762: PCRE_EXP_DEFN pcre32 * PCRE_CALL_CONVENTION
! 7763: pcre32_compile2(PCRE_SPTR32 pattern, int options, int *errorcodeptr,
! 7764: const char **errorptr, int *erroroffset, const unsigned char *tables)
1.1.1.2 misho 7765: #endif
1.1 misho 7766: {
1.1.1.2 misho 7767: REAL_PCRE *re;
1.1 misho 7768: int length = 1; /* For final END opcode */
1.1.1.4 ! misho 7769: pcre_int32 firstcharflags, reqcharflags;
! 7770: pcre_uint32 firstchar, reqchar;
! 7771: pcre_uint32 limit_match = PCRE_UINT32_MAX;
! 7772: pcre_uint32 limit_recursion = PCRE_UINT32_MAX;
1.1.1.2 misho 7773: int newline;
1.1 misho 7774: int errorcode = 0;
7775: int skipatstart = 0;
1.1.1.2 misho 7776: BOOL utf;
1.1.1.4 ! misho 7777: BOOL never_utf = FALSE;
1.1 misho 7778: size_t size;
1.1.1.2 misho 7779: pcre_uchar *code;
7780: const pcre_uchar *codestart;
7781: const pcre_uchar *ptr;
1.1 misho 7782: compile_data compile_block;
7783: compile_data *cd = &compile_block;
7784:
7785: /* This space is used for "compiling" into during the first phase, when we are
7786: computing the amount of memory that is needed. Compiled items are thrown away
7787: as soon as possible, so that a fairly large buffer should be sufficient for
7788: this purpose. The same space is used in the second phase for remembering where
7789: to fill in forward references to subpatterns. That may overflow, in which case
7790: new memory is obtained from malloc(). */
7791:
1.1.1.2 misho 7792: pcre_uchar cworkspace[COMPILE_WORK_SIZE];
1.1 misho 7793:
7794: /* Set this early so that early errors get offset 0. */
7795:
1.1.1.2 misho 7796: ptr = (const pcre_uchar *)pattern;
1.1 misho 7797:
7798: /* We can't pass back an error message if errorptr is NULL; I guess the best we
7799: can do is just return NULL, but we can set a code value if there is a code
7800: pointer. */
7801:
7802: if (errorptr == NULL)
7803: {
7804: if (errorcodeptr != NULL) *errorcodeptr = 99;
7805: return NULL;
7806: }
7807:
7808: *errorptr = NULL;
7809: if (errorcodeptr != NULL) *errorcodeptr = ERR0;
7810:
7811: /* However, we can give a message for this error */
7812:
7813: if (erroroffset == NULL)
7814: {
7815: errorcode = ERR16;
7816: goto PCRE_EARLY_ERROR_RETURN2;
7817: }
7818:
7819: *erroroffset = 0;
7820:
7821: /* Set up pointers to the individual character tables */
7822:
1.1.1.2 misho 7823: if (tables == NULL) tables = PRIV(default_tables);
1.1 misho 7824: cd->lcc = tables + lcc_offset;
7825: cd->fcc = tables + fcc_offset;
7826: cd->cbits = tables + cbits_offset;
7827: cd->ctypes = tables + ctypes_offset;
7828:
7829: /* Check that all undefined public option bits are zero */
7830:
7831: if ((options & ~PUBLIC_COMPILE_OPTIONS) != 0)
7832: {
7833: errorcode = ERR17;
7834: goto PCRE_EARLY_ERROR_RETURN;
7835: }
7836:
1.1.1.4 ! misho 7837: /* If PCRE_NEVER_UTF is set, remember it. */
! 7838:
! 7839: if ((options & PCRE_NEVER_UTF) != 0) never_utf = TRUE;
! 7840:
1.1 misho 7841: /* Check for global one-time settings at the start of the pattern, and remember
7842: the offset for later. */
7843:
1.1.1.4 ! misho 7844: cd->external_flags = 0; /* Initialize here for LIMIT_MATCH/RECURSION */
! 7845:
1.1 misho 7846: while (ptr[skipatstart] == CHAR_LEFT_PARENTHESIS &&
7847: ptr[skipatstart+1] == CHAR_ASTERISK)
7848: {
7849: int newnl = 0;
7850: int newbsr = 0;
7851:
1.1.1.4 ! misho 7852: /* For completeness and backward compatibility, (*UTFn) is supported in the
! 7853: relevant libraries, but (*UTF) is generic and always supported. Note that
! 7854: PCRE_UTF8 == PCRE_UTF16 == PCRE_UTF32. */
! 7855:
1.1.1.2 misho 7856: #ifdef COMPILE_PCRE8
1.1.1.4 ! misho 7857: if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_UTF8_RIGHTPAR, 5) == 0)
1.1 misho 7858: { skipatstart += 7; options |= PCRE_UTF8; continue; }
1.1.1.2 misho 7859: #endif
7860: #ifdef COMPILE_PCRE16
1.1.1.4 ! misho 7861: if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_UTF16_RIGHTPAR, 6) == 0)
1.1.1.2 misho 7862: { skipatstart += 8; options |= PCRE_UTF16; continue; }
7863: #endif
1.1.1.4 ! misho 7864: #ifdef COMPILE_PCRE32
! 7865: if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_UTF32_RIGHTPAR, 6) == 0)
! 7866: { skipatstart += 8; options |= PCRE_UTF32; continue; }
! 7867: #endif
! 7868:
! 7869: else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_UTF_RIGHTPAR, 4) == 0)
! 7870: { skipatstart += 6; options |= PCRE_UTF8; continue; }
1.1.1.2 misho 7871: else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_UCP_RIGHTPAR, 4) == 0)
1.1 misho 7872: { skipatstart += 6; options |= PCRE_UCP; continue; }
1.1.1.2 misho 7873: else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_NO_START_OPT_RIGHTPAR, 13) == 0)
1.1 misho 7874: { skipatstart += 15; options |= PCRE_NO_START_OPTIMIZE; continue; }
7875:
1.1.1.4 ! misho 7876: else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_LIMIT_MATCH_EQ, 12) == 0)
! 7877: {
! 7878: pcre_uint32 c = 0;
! 7879: int p = skipatstart + 14;
! 7880: while (isdigit(ptr[p]))
! 7881: {
! 7882: if (c > PCRE_UINT32_MAX / 10 - 1) break; /* Integer overflow */
! 7883: c = c*10 + ptr[p++] - CHAR_0;
! 7884: }
! 7885: if (ptr[p++] != CHAR_RIGHT_PARENTHESIS) break;
! 7886: if (c < limit_match)
! 7887: {
! 7888: limit_match = c;
! 7889: cd->external_flags |= PCRE_MLSET;
! 7890: }
! 7891: skipatstart = p;
! 7892: continue;
! 7893: }
! 7894:
! 7895: else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_LIMIT_RECURSION_EQ, 16) == 0)
! 7896: {
! 7897: pcre_uint32 c = 0;
! 7898: int p = skipatstart + 18;
! 7899: while (isdigit(ptr[p]))
! 7900: {
! 7901: if (c > PCRE_UINT32_MAX / 10 - 1) break; /* Integer overflow check */
! 7902: c = c*10 + ptr[p++] - CHAR_0;
! 7903: }
! 7904: if (ptr[p++] != CHAR_RIGHT_PARENTHESIS) break;
! 7905: if (c < limit_recursion)
! 7906: {
! 7907: limit_recursion = c;
! 7908: cd->external_flags |= PCRE_RLSET;
! 7909: }
! 7910: skipatstart = p;
! 7911: continue;
! 7912: }
! 7913:
1.1.1.2 misho 7914: if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_CR_RIGHTPAR, 3) == 0)
1.1 misho 7915: { skipatstart += 5; newnl = PCRE_NEWLINE_CR; }
1.1.1.2 misho 7916: else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_LF_RIGHTPAR, 3) == 0)
1.1 misho 7917: { skipatstart += 5; newnl = PCRE_NEWLINE_LF; }
1.1.1.2 misho 7918: else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_CRLF_RIGHTPAR, 5) == 0)
1.1 misho 7919: { skipatstart += 7; newnl = PCRE_NEWLINE_CR + PCRE_NEWLINE_LF; }
1.1.1.2 misho 7920: else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_ANY_RIGHTPAR, 4) == 0)
1.1 misho 7921: { skipatstart += 6; newnl = PCRE_NEWLINE_ANY; }
1.1.1.2 misho 7922: else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_ANYCRLF_RIGHTPAR, 8) == 0)
1.1 misho 7923: { skipatstart += 10; newnl = PCRE_NEWLINE_ANYCRLF; }
7924:
1.1.1.2 misho 7925: else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_BSR_ANYCRLF_RIGHTPAR, 12) == 0)
1.1 misho 7926: { skipatstart += 14; newbsr = PCRE_BSR_ANYCRLF; }
1.1.1.2 misho 7927: else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_BSR_UNICODE_RIGHTPAR, 12) == 0)
1.1 misho 7928: { skipatstart += 14; newbsr = PCRE_BSR_UNICODE; }
7929:
7930: if (newnl != 0)
7931: options = (options & ~PCRE_NEWLINE_BITS) | newnl;
7932: else if (newbsr != 0)
7933: options = (options & ~(PCRE_BSR_ANYCRLF|PCRE_BSR_UNICODE)) | newbsr;
7934: else break;
7935: }
7936:
1.1.1.4 ! misho 7937: /* PCRE_UTF(16|32) have the same value as PCRE_UTF8. */
1.1.1.2 misho 7938: utf = (options & PCRE_UTF8) != 0;
1.1.1.4 ! misho 7939: if (utf && never_utf)
! 7940: {
! 7941: errorcode = ERR78;
! 7942: goto PCRE_EARLY_ERROR_RETURN2;
! 7943: }
1.1 misho 7944:
1.1.1.2 misho 7945: /* Can't support UTF unless PCRE has been compiled to include the code. The
7946: return of an error code from PRIV(valid_utf)() is a new feature, introduced in
1.1 misho 7947: release 8.13. It is passed back from pcre_[dfa_]exec(), but at the moment is
7948: not used here. */
7949:
1.1.1.2 misho 7950: #ifdef SUPPORT_UTF
7951: if (utf && (options & PCRE_NO_UTF8_CHECK) == 0 &&
7952: (errorcode = PRIV(valid_utf)((PCRE_PUCHAR)pattern, -1, erroroffset)) != 0)
1.1 misho 7953: {
1.1.1.4 ! misho 7954: #if defined COMPILE_PCRE8
1.1 misho 7955: errorcode = ERR44;
1.1.1.4 ! misho 7956: #elif defined COMPILE_PCRE16
1.1.1.2 misho 7957: errorcode = ERR74;
1.1.1.4 ! misho 7958: #elif defined COMPILE_PCRE32
! 7959: errorcode = ERR77;
1.1.1.2 misho 7960: #endif
1.1 misho 7961: goto PCRE_EARLY_ERROR_RETURN2;
7962: }
7963: #else
1.1.1.2 misho 7964: if (utf)
1.1 misho 7965: {
7966: errorcode = ERR32;
7967: goto PCRE_EARLY_ERROR_RETURN;
7968: }
7969: #endif
7970:
7971: /* Can't support UCP unless PCRE has been compiled to include the code. */
7972:
7973: #ifndef SUPPORT_UCP
7974: if ((options & PCRE_UCP) != 0)
7975: {
7976: errorcode = ERR67;
7977: goto PCRE_EARLY_ERROR_RETURN;
7978: }
7979: #endif
7980:
7981: /* Check validity of \R options. */
7982:
7983: if ((options & (PCRE_BSR_ANYCRLF|PCRE_BSR_UNICODE)) ==
7984: (PCRE_BSR_ANYCRLF|PCRE_BSR_UNICODE))
7985: {
7986: errorcode = ERR56;
7987: goto PCRE_EARLY_ERROR_RETURN;
7988: }
7989:
7990: /* Handle different types of newline. The three bits give seven cases. The
7991: current code allows for fixed one- or two-byte sequences, plus "any" and
7992: "anycrlf". */
7993:
7994: switch (options & PCRE_NEWLINE_BITS)
7995: {
7996: case 0: newline = NEWLINE; break; /* Build-time default */
7997: case PCRE_NEWLINE_CR: newline = CHAR_CR; break;
7998: case PCRE_NEWLINE_LF: newline = CHAR_NL; break;
7999: case PCRE_NEWLINE_CR+
8000: PCRE_NEWLINE_LF: newline = (CHAR_CR << 8) | CHAR_NL; break;
8001: case PCRE_NEWLINE_ANY: newline = -1; break;
8002: case PCRE_NEWLINE_ANYCRLF: newline = -2; break;
8003: default: errorcode = ERR56; goto PCRE_EARLY_ERROR_RETURN;
8004: }
8005:
8006: if (newline == -2)
8007: {
8008: cd->nltype = NLTYPE_ANYCRLF;
8009: }
8010: else if (newline < 0)
8011: {
8012: cd->nltype = NLTYPE_ANY;
8013: }
8014: else
8015: {
8016: cd->nltype = NLTYPE_FIXED;
8017: if (newline > 255)
8018: {
8019: cd->nllen = 2;
8020: cd->nl[0] = (newline >> 8) & 255;
8021: cd->nl[1] = newline & 255;
8022: }
8023: else
8024: {
8025: cd->nllen = 1;
8026: cd->nl[0] = newline;
8027: }
8028: }
8029:
8030: /* Maximum back reference and backref bitmap. The bitmap records up to 31 back
8031: references to help in deciding whether (.*) can be treated as anchored or not.
8032: */
8033:
8034: cd->top_backref = 0;
8035: cd->backref_map = 0;
8036:
8037: /* Reflect pattern for debugging output */
8038:
8039: DPRINTF(("------------------------------------------------------------------\n"));
1.1.1.2 misho 8040: #ifdef PCRE_DEBUG
8041: print_puchar(stdout, (PCRE_PUCHAR)pattern);
8042: #endif
8043: DPRINTF(("\n"));
1.1 misho 8044:
8045: /* Pretend to compile the pattern while actually just accumulating the length
8046: of memory required. This behaviour is triggered by passing a non-NULL final
8047: argument to compile_regex(). We pass a block of workspace (cworkspace) for it
8048: to compile parts of the pattern into; the compiled code is discarded when it is
8049: no longer needed, so hopefully this workspace will never overflow, though there
8050: is a test for its doing so. */
8051:
8052: cd->bracount = cd->final_bracount = 0;
8053: cd->names_found = 0;
8054: cd->name_entry_size = 0;
8055: cd->name_table = NULL;
8056: cd->start_code = cworkspace;
8057: cd->hwm = cworkspace;
8058: cd->start_workspace = cworkspace;
8059: cd->workspace_size = COMPILE_WORK_SIZE;
1.1.1.2 misho 8060: cd->start_pattern = (const pcre_uchar *)pattern;
8061: cd->end_pattern = (const pcre_uchar *)(pattern + STRLEN_UC((const pcre_uchar *)pattern));
1.1 misho 8062: cd->req_varyopt = 0;
1.1.1.2 misho 8063: cd->assert_depth = 0;
1.1.1.3 misho 8064: cd->max_lookbehind = 0;
1.1 misho 8065: cd->external_options = options;
8066: cd->open_caps = NULL;
8067:
8068: /* Now do the pre-compile. On error, errorcode will be set non-zero, so we
8069: don't need to look at the result of the function here. The initial options have
8070: been put into the cd block so that they can be changed if an option setting is
8071: found within the regex right at the beginning. Bringing initial option settings
8072: outside can help speed up starting point checks. */
8073:
8074: ptr += skipatstart;
8075: code = cworkspace;
8076: *code = OP_BRA;
8077: (void)compile_regex(cd->external_options, &code, &ptr, &errorcode, FALSE,
1.1.1.4 ! misho 8078: FALSE, 0, 0, &firstchar, &firstcharflags, &reqchar, &reqcharflags, NULL,
! 8079: cd, &length);
1.1 misho 8080: if (errorcode != 0) goto PCRE_EARLY_ERROR_RETURN;
8081:
8082: DPRINTF(("end pre-compile: length=%d workspace=%d\n", length,
1.1.1.2 misho 8083: (int)(cd->hwm - cworkspace)));
1.1 misho 8084:
8085: if (length > MAX_PATTERN_SIZE)
8086: {
8087: errorcode = ERR20;
8088: goto PCRE_EARLY_ERROR_RETURN;
8089: }
8090:
8091: /* Compute the size of data block needed and get it, either from malloc or
8092: externally provided function. Integer overflow should no longer be possible
8093: because nowadays we limit the maximum value of cd->names_found and
8094: cd->name_entry_size. */
8095:
1.1.1.2 misho 8096: size = sizeof(REAL_PCRE) + (length + cd->names_found * cd->name_entry_size) * sizeof(pcre_uchar);
8097: re = (REAL_PCRE *)(PUBL(malloc))(size);
1.1 misho 8098:
8099: if (re == NULL)
8100: {
8101: errorcode = ERR21;
8102: goto PCRE_EARLY_ERROR_RETURN;
8103: }
8104:
8105: /* Put in the magic number, and save the sizes, initial options, internal
8106: flags, and character table pointer. NULL is used for the default character
8107: tables. The nullpad field is at the end; it's there to help in the case when a
8108: regex compiled on a system with 4-byte pointers is run on another with 8-byte
8109: pointers. */
8110:
8111: re->magic_number = MAGIC_NUMBER;
8112: re->size = (int)size;
8113: re->options = cd->external_options;
8114: re->flags = cd->external_flags;
1.1.1.4 ! misho 8115: re->limit_match = limit_match;
! 8116: re->limit_recursion = limit_recursion;
1.1.1.2 misho 8117: re->first_char = 0;
8118: re->req_char = 0;
8119: re->name_table_offset = sizeof(REAL_PCRE) / sizeof(pcre_uchar);
1.1 misho 8120: re->name_entry_size = cd->name_entry_size;
8121: re->name_count = cd->names_found;
8122: re->ref_count = 0;
1.1.1.2 misho 8123: re->tables = (tables == PRIV(default_tables))? NULL : tables;
1.1 misho 8124: re->nullpad = NULL;
1.1.1.4 ! misho 8125: #ifdef COMPILE_PCRE32
! 8126: re->dummy = 0;
! 8127: #else
! 8128: re->dummy1 = re->dummy2 = re->dummy3 = 0;
! 8129: #endif
1.1 misho 8130:
8131: /* The starting points of the name/number translation table and of the code are
8132: passed around in the compile data block. The start/end pattern and initial
8133: options are already set from the pre-compile phase, as is the name_entry_size
8134: field. Reset the bracket count and the names_found field. Also reset the hwm
8135: field; this time it's used for remembering forward references to subpatterns.
8136: */
8137:
8138: cd->final_bracount = cd->bracount; /* Save for checking forward references */
8139: cd->assert_depth = 0;
8140: cd->bracount = 0;
1.1.1.3 misho 8141: cd->max_lookbehind = 0;
1.1 misho 8142: cd->names_found = 0;
1.1.1.2 misho 8143: cd->name_table = (pcre_uchar *)re + re->name_table_offset;
1.1 misho 8144: codestart = cd->name_table + re->name_entry_size * re->name_count;
8145: cd->start_code = codestart;
1.1.1.2 misho 8146: cd->hwm = (pcre_uchar *)(cd->start_workspace);
1.1 misho 8147: cd->req_varyopt = 0;
8148: cd->had_accept = FALSE;
1.1.1.4 ! misho 8149: cd->had_pruneorskip = FALSE;
1.1 misho 8150: cd->check_lookbehind = FALSE;
8151: cd->open_caps = NULL;
8152:
8153: /* Set up a starting, non-extracting bracket, then compile the expression. On
8154: error, errorcode will be set non-zero, so we don't need to look at the result
8155: of the function here. */
8156:
1.1.1.2 misho 8157: ptr = (const pcre_uchar *)pattern + skipatstart;
8158: code = (pcre_uchar *)codestart;
1.1 misho 8159: *code = OP_BRA;
8160: (void)compile_regex(re->options, &code, &ptr, &errorcode, FALSE, FALSE, 0, 0,
1.1.1.4 ! misho 8161: &firstchar, &firstcharflags, &reqchar, &reqcharflags, NULL, cd, NULL);
1.1 misho 8162: re->top_bracket = cd->bracount;
8163: re->top_backref = cd->top_backref;
1.1.1.3 misho 8164: re->max_lookbehind = cd->max_lookbehind;
1.1.1.2 misho 8165: re->flags = cd->external_flags | PCRE_MODE;
1.1 misho 8166:
1.1.1.4 ! misho 8167: if (cd->had_accept)
! 8168: {
! 8169: reqchar = 0; /* Must disable after (*ACCEPT) */
! 8170: reqcharflags = REQ_NONE;
! 8171: }
1.1 misho 8172:
8173: /* If not reached end of pattern on success, there's an excess bracket. */
8174:
1.1.1.4 ! misho 8175: if (errorcode == 0 && *ptr != CHAR_NULL) errorcode = ERR22;
1.1 misho 8176:
8177: /* Fill in the terminating state and check for disastrous overflow, but
8178: if debugging, leave the test till after things are printed out. */
8179:
8180: *code++ = OP_END;
8181:
8182: #ifndef PCRE_DEBUG
8183: if (code - codestart > length) errorcode = ERR23;
8184: #endif
8185:
1.1.1.4 ! misho 8186: #ifdef SUPPORT_VALGRIND
! 8187: /* If the estimated length exceeds the really used length, mark the extra
! 8188: allocated memory as unaddressable, so that any out-of-bound reads can be
! 8189: detected. */
! 8190: VALGRIND_MAKE_MEM_NOACCESS(code, (length - (code - codestart)) * sizeof(pcre_uchar));
! 8191: #endif
! 8192:
1.1 misho 8193: /* Fill in any forward references that are required. There may be repeated
8194: references; optimize for them, as searching a large regex takes time. */
8195:
8196: if (cd->hwm > cd->start_workspace)
8197: {
8198: int prev_recno = -1;
1.1.1.2 misho 8199: const pcre_uchar *groupptr = NULL;
1.1 misho 8200: while (errorcode == 0 && cd->hwm > cd->start_workspace)
8201: {
8202: int offset, recno;
8203: cd->hwm -= LINK_SIZE;
8204: offset = GET(cd->hwm, 0);
8205: recno = GET(codestart, offset);
8206: if (recno != prev_recno)
8207: {
1.1.1.2 misho 8208: groupptr = PRIV(find_bracket)(codestart, utf, recno);
1.1 misho 8209: prev_recno = recno;
8210: }
8211: if (groupptr == NULL) errorcode = ERR53;
1.1.1.2 misho 8212: else PUT(((pcre_uchar *)codestart), offset, (int)(groupptr - codestart));
1.1 misho 8213: }
8214: }
8215:
8216: /* If the workspace had to be expanded, free the new memory. */
8217:
8218: if (cd->workspace_size > COMPILE_WORK_SIZE)
1.1.1.2 misho 8219: (PUBL(free))((void *)cd->start_workspace);
1.1 misho 8220:
8221: /* Give an error if there's back reference to a non-existent capturing
8222: subpattern. */
8223:
8224: if (errorcode == 0 && re->top_backref > re->top_bracket) errorcode = ERR15;
8225:
8226: /* If there were any lookbehind assertions that contained OP_RECURSE
8227: (recursions or subroutine calls), a flag is set for them to be checked here,
1.1.1.4 ! misho 8228: because they may contain forward references. Actual recursions cannot be fixed
1.1 misho 8229: length, but subroutine calls can. It is done like this so that those without
8230: OP_RECURSE that are not fixed length get a diagnosic with a useful offset. The
8231: exceptional ones forgo this. We scan the pattern to check that they are fixed
8232: length, and set their lengths. */
8233:
8234: if (cd->check_lookbehind)
8235: {
1.1.1.2 misho 8236: pcre_uchar *cc = (pcre_uchar *)codestart;
1.1 misho 8237:
8238: /* Loop, searching for OP_REVERSE items, and process those that do not have
8239: their length set. (Actually, it will also re-process any that have a length
8240: of zero, but that is a pathological case, and it does no harm.) When we find
8241: one, we temporarily terminate the branch it is in while we scan it. */
8242:
1.1.1.2 misho 8243: for (cc = (pcre_uchar *)PRIV(find_bracket)(codestart, utf, -1);
1.1 misho 8244: cc != NULL;
1.1.1.2 misho 8245: cc = (pcre_uchar *)PRIV(find_bracket)(cc, utf, -1))
1.1 misho 8246: {
8247: if (GET(cc, 1) == 0)
8248: {
8249: int fixed_length;
1.1.1.2 misho 8250: pcre_uchar *be = cc - 1 - LINK_SIZE + GET(cc, -LINK_SIZE);
1.1 misho 8251: int end_op = *be;
8252: *be = OP_END;
8253: fixed_length = find_fixedlength(cc, (re->options & PCRE_UTF8) != 0, TRUE,
8254: cd);
8255: *be = end_op;
8256: DPRINTF(("fixed length = %d\n", fixed_length));
8257: if (fixed_length < 0)
8258: {
8259: errorcode = (fixed_length == -2)? ERR36 :
8260: (fixed_length == -4)? ERR70 : ERR25;
8261: break;
8262: }
1.1.1.3 misho 8263: if (fixed_length > cd->max_lookbehind) cd->max_lookbehind = fixed_length;
1.1 misho 8264: PUT(cc, 1, fixed_length);
8265: }
8266: cc += 1 + LINK_SIZE;
8267: }
8268: }
8269:
8270: /* Failed to compile, or error while post-processing */
8271:
8272: if (errorcode != 0)
8273: {
1.1.1.2 misho 8274: (PUBL(free))(re);
1.1 misho 8275: PCRE_EARLY_ERROR_RETURN:
1.1.1.2 misho 8276: *erroroffset = (int)(ptr - (const pcre_uchar *)pattern);
1.1 misho 8277: PCRE_EARLY_ERROR_RETURN2:
8278: *errorptr = find_error_text(errorcode);
8279: if (errorcodeptr != NULL) *errorcodeptr = errorcode;
8280: return NULL;
8281: }
8282:
8283: /* If the anchored option was not passed, set the flag if we can determine that
1.1.1.4 ! misho 8284: the pattern is anchored by virtue of ^ characters or \A or anything else, such
! 8285: as starting with non-atomic .* when DOTALL is set and there are no occurrences
! 8286: of *PRUNE or *SKIP.
1.1 misho 8287:
8288: Otherwise, if we know what the first byte has to be, save it, because that
8289: speeds up unanchored matches no end. If not, see if we can set the
8290: PCRE_STARTLINE flag. This is helpful for multiline matches when all branches
1.1.1.4 ! misho 8291: start with ^. and also when all branches start with non-atomic .* for
! 8292: non-DOTALL matches when *PRUNE and SKIP are not present. */
1.1 misho 8293:
8294: if ((re->options & PCRE_ANCHORED) == 0)
8295: {
1.1.1.4 ! misho 8296: if (is_anchored(codestart, 0, cd, 0)) re->options |= PCRE_ANCHORED;
1.1 misho 8297: else
8298: {
1.1.1.4 ! misho 8299: if (firstcharflags < 0)
! 8300: firstchar = find_firstassertedchar(codestart, &firstcharflags, FALSE);
! 8301: if (firstcharflags >= 0) /* Remove caseless flag for non-caseable chars */
1.1.1.2 misho 8302: {
1.1.1.4 ! misho 8303: #if defined COMPILE_PCRE8
1.1.1.2 misho 8304: re->first_char = firstchar & 0xff;
1.1.1.4 ! misho 8305: #elif defined COMPILE_PCRE16
1.1.1.2 misho 8306: re->first_char = firstchar & 0xffff;
1.1.1.4 ! misho 8307: #elif defined COMPILE_PCRE32
! 8308: re->first_char = firstchar;
1.1.1.2 misho 8309: #endif
1.1.1.4 ! misho 8310: if ((firstcharflags & REQ_CASELESS) != 0)
1.1.1.2 misho 8311: {
8312: #if defined SUPPORT_UCP && !(defined COMPILE_PCRE8)
8313: /* We ignore non-ASCII first chars in 8 bit mode. */
8314: if (utf)
8315: {
8316: if (re->first_char < 128)
8317: {
8318: if (cd->fcc[re->first_char] != re->first_char)
8319: re->flags |= PCRE_FCH_CASELESS;
8320: }
8321: else if (UCD_OTHERCASE(re->first_char) != re->first_char)
8322: re->flags |= PCRE_FCH_CASELESS;
8323: }
8324: else
8325: #endif
8326: if (MAX_255(re->first_char)
8327: && cd->fcc[re->first_char] != re->first_char)
8328: re->flags |= PCRE_FCH_CASELESS;
8329: }
8330:
1.1 misho 8331: re->flags |= PCRE_FIRSTSET;
8332: }
1.1.1.4 ! misho 8333:
! 8334: else if (is_startline(codestart, 0, cd, 0)) re->flags |= PCRE_STARTLINE;
1.1 misho 8335: }
8336: }
8337:
8338: /* For an anchored pattern, we use the "required byte" only if it follows a
8339: variable length item in the regex. Remove the caseless flag for non-caseable
8340: bytes. */
8341:
1.1.1.4 ! misho 8342: if (reqcharflags >= 0 &&
! 8343: ((re->options & PCRE_ANCHORED) == 0 || (reqcharflags & REQ_VARY) != 0))
1.1 misho 8344: {
1.1.1.4 ! misho 8345: #if defined COMPILE_PCRE8
1.1.1.2 misho 8346: re->req_char = reqchar & 0xff;
1.1.1.4 ! misho 8347: #elif defined COMPILE_PCRE16
1.1.1.2 misho 8348: re->req_char = reqchar & 0xffff;
1.1.1.4 ! misho 8349: #elif defined COMPILE_PCRE32
! 8350: re->req_char = reqchar;
1.1.1.2 misho 8351: #endif
1.1.1.4 ! misho 8352: if ((reqcharflags & REQ_CASELESS) != 0)
1.1.1.2 misho 8353: {
8354: #if defined SUPPORT_UCP && !(defined COMPILE_PCRE8)
8355: /* We ignore non-ASCII first chars in 8 bit mode. */
8356: if (utf)
8357: {
8358: if (re->req_char < 128)
8359: {
8360: if (cd->fcc[re->req_char] != re->req_char)
8361: re->flags |= PCRE_RCH_CASELESS;
8362: }
8363: else if (UCD_OTHERCASE(re->req_char) != re->req_char)
8364: re->flags |= PCRE_RCH_CASELESS;
8365: }
8366: else
8367: #endif
8368: if (MAX_255(re->req_char) && cd->fcc[re->req_char] != re->req_char)
8369: re->flags |= PCRE_RCH_CASELESS;
8370: }
8371:
1.1 misho 8372: re->flags |= PCRE_REQCHSET;
8373: }
8374:
8375: /* Print out the compiled data if debugging is enabled. This is never the
8376: case when building a production library. */
8377:
8378: #ifdef PCRE_DEBUG
8379: printf("Length = %d top_bracket = %d top_backref = %d\n",
8380: length, re->top_bracket, re->top_backref);
8381:
8382: printf("Options=%08x\n", re->options);
8383:
8384: if ((re->flags & PCRE_FIRSTSET) != 0)
8385: {
1.1.1.2 misho 8386: pcre_uchar ch = re->first_char;
8387: const char *caseless =
8388: ((re->flags & PCRE_FCH_CASELESS) == 0)? "" : " (caseless)";
8389: if (PRINTABLE(ch)) printf("First char = %c%s\n", ch, caseless);
1.1 misho 8390: else printf("First char = \\x%02x%s\n", ch, caseless);
8391: }
8392:
8393: if ((re->flags & PCRE_REQCHSET) != 0)
8394: {
1.1.1.2 misho 8395: pcre_uchar ch = re->req_char;
8396: const char *caseless =
8397: ((re->flags & PCRE_RCH_CASELESS) == 0)? "" : " (caseless)";
8398: if (PRINTABLE(ch)) printf("Req char = %c%s\n", ch, caseless);
1.1 misho 8399: else printf("Req char = \\x%02x%s\n", ch, caseless);
8400: }
8401:
1.1.1.4 ! misho 8402: #if defined COMPILE_PCRE8
1.1.1.2 misho 8403: pcre_printint((pcre *)re, stdout, TRUE);
1.1.1.4 ! misho 8404: #elif defined COMPILE_PCRE16
1.1.1.2 misho 8405: pcre16_printint((pcre *)re, stdout, TRUE);
1.1.1.4 ! misho 8406: #elif defined COMPILE_PCRE32
! 8407: pcre32_printint((pcre *)re, stdout, TRUE);
1.1.1.2 misho 8408: #endif
1.1 misho 8409:
8410: /* This check is done here in the debugging case so that the code that
8411: was compiled can be seen. */
8412:
8413: if (code - codestart > length)
8414: {
1.1.1.2 misho 8415: (PUBL(free))(re);
1.1 misho 8416: *errorptr = find_error_text(ERR23);
1.1.1.2 misho 8417: *erroroffset = ptr - (pcre_uchar *)pattern;
1.1 misho 8418: if (errorcodeptr != NULL) *errorcodeptr = ERR23;
8419: return NULL;
8420: }
8421: #endif /* PCRE_DEBUG */
8422:
1.1.1.4 ! misho 8423: #if defined COMPILE_PCRE8
1.1 misho 8424: return (pcre *)re;
1.1.1.4 ! misho 8425: #elif defined COMPILE_PCRE16
1.1.1.2 misho 8426: return (pcre16 *)re;
1.1.1.4 ! misho 8427: #elif defined COMPILE_PCRE32
! 8428: return (pcre32 *)re;
1.1.1.2 misho 8429: #endif
1.1 misho 8430: }
8431:
8432: /* End of pcre_compile.c */
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>