00001 /************************************************* 00002 * Perl-Compatible Regular Expressions * 00003 *************************************************/ 00004 00005 00006 /* This is a library of functions to support regular expressions whose syntax 00007 and semantics are as close as possible to those of the Perl 5 language. See 00008 the file doc/Tech.Notes for some information on the internals. 00009 00010 Written by: Philip Hazel <ph10@cam.ac.uk> 00011 00012 Copyright (c) 1997-2004 University of Cambridge 00013 00014 ----------------------------------------------------------------------------- 00015 Redistribution and use in source and binary forms, with or without 00016 modification, are permitted provided that the following conditions are met: 00017 00018 * Redistributions of source code must retain the above copyright notice, 00019 this list of conditions and the following disclaimer. 00020 00021 * Redistributions in binary form must reproduce the above copyright 00022 notice, this list of conditions and the following disclaimer in the 00023 documentation and/or other materials provided with the distribution. 00024 00025 * Neither the name of the University of Cambridge nor the names of its 00026 contributors may be used to endorse or promote products derived from 00027 this software without specific prior written permission. 00028 00029 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00030 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00031 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00032 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00033 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00034 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00035 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00036 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00037 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00038 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00039 POSSIBILITY OF SUCH DAMAGE. 00040 ----------------------------------------------------------------------------- 00041 */ 00042 00043 /* This header contains definitions that are shared between the different 00044 modules, but which are not relevant to the outside. */ 00045 00046 /* Get the definitions provided by running "configure" */ 00047 00048 #include "config.h" 00049 00050 /* Standard C headers plus the external interface definition. The only time 00051 setjmp and stdarg are used is when NO_RECURSE is set. */ 00052 00053 #include <ctype.h> 00054 #include <limits.h> 00055 #include <setjmp.h> 00056 #include <stdarg.h> 00057 #include <stddef.h> 00058 #include <stdio.h> 00059 #include <stdlib.h> 00060 #include <string.h> 00061 00062 #ifndef PCRE_SPY 00063 #define PCRE_DEFINITION /* Win32 __declspec(export) trigger for .dll */ 00064 #endif 00065 00066 /* We need to have types that specify unsigned 16-bit and 32-bit integers. We 00067 cannot determine these outside the compilation (e.g. by running a program as 00068 part of "configure") because PCRE is often cross-compiled for use on other 00069 systems. Instead we make use of the maximum sizes that are available at 00070 preprocessor time in standard C environments. */ 00071 00072 #if USHRT_MAX == 65535 00073 typedef unsigned short pcre_uint16; 00074 #elif UINT_MAX == 65535 00075 typedef unsigned int pcre_uint16; 00076 #else 00077 #error Cannot determine a type for 16-bit unsigned integers 00078 #endif 00079 00080 #if UINT_MAX == 4294967295 00081 typedef unsigned int pcre_uint32; 00082 #elif ULONG_MAX == 4294967295 00083 typedef unsigned long int pcre_uint32; 00084 #else 00085 #error Cannot determine a type for 32-bit unsigned integers 00086 #endif 00087 00088 /* All character handling must be done as unsigned characters. Otherwise there 00089 are problems with top-bit-set characters and functions such as isspace(). 00090 However, we leave the interface to the outside world as char *, because that 00091 should make things easier for callers. We define a short type for unsigned char 00092 to save lots of typing. I tried "uchar", but it causes problems on Digital 00093 Unix, where it is defined in sys/types, so use "uschar" instead. */ 00094 00095 typedef unsigned char uschar; 00096 00097 /* Include the public PCRE header */ 00098 00099 #include "pcre.h" 00100 00101 /* When compiling for use with the Virtual Pascal compiler, these functions 00102 need to have their names changed. PCRE must be compiled with the -DVPCOMPAT 00103 option on the command line. */ 00104 00105 #ifdef VPCOMPAT 00106 #define strncmp(s1,s2,m) _strncmp(s1,s2,m) 00107 #define memcpy(d,s,n) _memcpy(d,s,n) 00108 #define memmove(d,s,n) _memmove(d,s,n) 00109 #define memset(s,c,n) _memset(s,c,n) 00110 #else /* VPCOMPAT */ 00111 00112 /* To cope with SunOS4 and other systems that lack memmove() but have bcopy(), 00113 define a macro for memmove() if HAVE_MEMMOVE is false, provided that HAVE_BCOPY 00114 is set. Otherwise, include an emulating function for those systems that have 00115 neither (there some non-Unix environments where this is the case). This assumes 00116 that all calls to memmove are moving strings upwards in store, which is the 00117 case in PCRE. */ 00118 00119 #if ! HAVE_MEMMOVE 00120 #undef memmove /* some systems may have a macro */ 00121 #if HAVE_BCOPY 00122 #define memmove(a, b, c) bcopy(b, a, c) 00123 #else /* HAVE_BCOPY */ 00124 void * 00125 pcre_memmove(unsigned char *dest, const unsigned char *src, size_t n) 00126 { 00127 int i; 00128 dest += n; 00129 src += n; 00130 for (i = 0; i < n; ++i) *(--dest) = *(--src); 00131 } 00132 #define memmove(a, b, c) pcre_memmove(a, b, c) 00133 #endif /* not HAVE_BCOPY */ 00134 #endif /* not HAVE_MEMMOVE */ 00135 #endif /* not VPCOMPAT */ 00136 00137 00138 /* PCRE keeps offsets in its compiled code as 2-byte quantities (always stored 00139 in big-endian order) by default. These are used, for example, to link from the 00140 start of a subpattern to its alternatives and its end. The use of 2 bytes per 00141 offset limits the size of the compiled regex to around 64K, which is big enough 00142 for almost everybody. However, I received a request for an even bigger limit. 00143 For this reason, and also to make the code easier to maintain, the storing and 00144 loading of offsets from the byte string is now handled by the macros that are 00145 defined here. 00146 00147 The macros are controlled by the value of LINK_SIZE. This defaults to 2 in 00148 the config.h file, but can be overridden by using -D on the command line. This 00149 is automated on Unix systems via the "configure" command. */ 00150 00151 #if LINK_SIZE == 2 00152 00153 #define PUT(a,n,d) \ 00154 (a[n] = (d) >> 8), \ 00155 (a[(n)+1] = (d) & 255) 00156 00157 #define GET(a,n) \ 00158 (((a)[n] << 8) | (a)[(n)+1]) 00159 00160 #define MAX_PATTERN_SIZE (1 << 16) 00161 00162 00163 #elif LINK_SIZE == 3 00164 00165 #define PUT(a,n,d) \ 00166 (a[n] = (d) >> 16), \ 00167 (a[(n)+1] = (d) >> 8), \ 00168 (a[(n)+2] = (d) & 255) 00169 00170 #define GET(a,n) \ 00171 (((a)[n] << 16) | ((a)[(n)+1] << 8) | (a)[(n)+2]) 00172 00173 #define MAX_PATTERN_SIZE (1 << 24) 00174 00175 00176 #elif LINK_SIZE == 4 00177 00178 #define PUT(a,n,d) \ 00179 (a[n] = (d) >> 24), \ 00180 (a[(n)+1] = (d) >> 16), \ 00181 (a[(n)+2] = (d) >> 8), \ 00182 (a[(n)+3] = (d) & 255) 00183 00184 #define GET(a,n) \ 00185 (((a)[n] << 24) | ((a)[(n)+1] << 16) | ((a)[(n)+2] << 8) | (a)[(n)+3]) 00186 00187 #define MAX_PATTERN_SIZE (1 << 30) /* Keep it positive */ 00188 00189 00190 #else 00191 #error LINK_SIZE must be either 2, 3, or 4 00192 #endif 00193 00194 00195 /* Convenience macro defined in terms of the others */ 00196 00197 #define PUTINC(a,n,d) PUT(a,n,d), a += LINK_SIZE 00198 00199 00200 /* PCRE uses some other 2-byte quantities that do not change when the size of 00201 offsets changes. There are used for repeat counts and for other things such as 00202 capturing parenthesis numbers in back references. */ 00203 00204 #define PUT2(a,n,d) \ 00205 a[n] = (d) >> 8; \ 00206 a[(n)+1] = (d) & 255 00207 00208 #define GET2(a,n) \ 00209 (((a)[n] << 8) | (a)[(n)+1]) 00210 00211 #define PUT2INC(a,n,d) PUT2(a,n,d), a += 2 00212 00213 00214 /* In case there is no definition of offsetof() provided - though any proper 00215 Standard C system should have one. */ 00216 00217 #ifndef offsetof 00218 #define offsetof(p_type,field) ((size_t)&(((p_type *)0)->field)) 00219 #endif 00220 00221 00222 /* These are the public options that can change during matching. */ 00223 00224 #define PCRE_IMS (PCRE_CASELESS|PCRE_MULTILINE|PCRE_DOTALL) 00225 00226 /* Private options flags start at the most significant end of the four bytes, 00227 but skip the top bit so we can use ints for convenience without getting tangled 00228 with negative values. The public options defined in pcre.h start at the least 00229 significant end. Make sure they don't overlap, though now that we have expanded 00230 to four bytes, there is plenty of space. */ 00231 00232 #define PCRE_FIRSTSET 0x40000000 /* first_byte is set */ 00233 #define PCRE_REQCHSET 0x20000000 /* req_byte is set */ 00234 #define PCRE_STARTLINE 0x10000000 /* start after \n for multiline */ 00235 #define PCRE_ICHANGED 0x08000000 /* i option changes within regex */ 00236 #define PCRE_NOPARTIAL 0x04000000 /* can't use partial with this regex */ 00237 00238 /* Options for the "extra" block produced by pcre_study(). */ 00239 00240 #define PCRE_STUDY_MAPPED 0x01 /* a map of starting chars exists */ 00241 00242 /* Masks for identifying the public options which are permitted at compile 00243 time, run time or study time, respectively. */ 00244 00245 #define PUBLIC_OPTIONS \ 00246 (PCRE_CASELESS|PCRE_EXTENDED|PCRE_ANCHORED|PCRE_MULTILINE| \ 00247 PCRE_DOTALL|PCRE_DOLLAR_ENDONLY|PCRE_EXTRA|PCRE_UNGREEDY|PCRE_UTF8| \ 00248 PCRE_NO_AUTO_CAPTURE|PCRE_NO_UTF8_CHECK|PCRE_AUTO_CALLOUT) 00249 00250 #define PUBLIC_EXEC_OPTIONS \ 00251 (PCRE_ANCHORED|PCRE_NOTBOL|PCRE_NOTEOL|PCRE_NOTEMPTY|PCRE_NO_UTF8_CHECK| \ 00252 PCRE_PARTIAL) 00253 00254 #define PUBLIC_STUDY_OPTIONS 0 /* None defined */ 00255 00256 /* Magic number to provide a small check against being handed junk. */ 00257 00258 #define MAGIC_NUMBER 0x50435245UL /* 'PCRE' */ 00259 00260 /* Negative values for the firstchar and reqchar variables */ 00261 00262 #define REQ_UNSET (-2) 00263 #define REQ_NONE (-1) 00264 00265 /* Flags added to firstbyte or reqbyte; a "non-literal" item is either a 00266 variable-length repeat, or a anything other than literal characters. */ 00267 00268 #define REQ_CASELESS 0x0100 /* indicates caselessness */ 00269 #define REQ_VARY 0x0200 /* reqbyte followed non-literal item */ 00270 00271 /* Miscellaneous definitions */ 00272 00273 typedef int BOOL; 00274 00275 #define FALSE 0 00276 #define TRUE 1 00277 00278 /* Escape items that are just an encoding of a particular data value. Note that 00279 ESC_n is defined as yet another macro, which is set in config.h to either \n 00280 (the default) or \r (which some people want). */ 00281 00282 #ifndef ESC_e 00283 #define ESC_e 27 00284 #endif 00285 00286 #ifndef ESC_f 00287 #define ESC_f '\f' 00288 #endif 00289 00290 #ifndef ESC_n 00291 #define ESC_n NEWLINE 00292 #endif 00293 00294 #ifndef ESC_r 00295 #define ESC_r '\r' 00296 #endif 00297 00298 /* We can't officially use ESC_t because it is a POSIX reserved identifier 00299 (presumably because of all the others like size_t). */ 00300 00301 #ifndef ESC_tee 00302 #define ESC_tee '\t' 00303 #endif 00304 00305 /* These are escaped items that aren't just an encoding of a particular data 00306 value such as \n. They must have non-zero values, as check_escape() returns 00307 their negation. Also, they must appear in the same order as in the opcode 00308 definitions below, up to ESC_z. There's a dummy for OP_ANY because it 00309 corresponds to "." rather than an escape sequence. The final one must be 00310 ESC_REF as subsequent values are used for \1, \2, \3, etc. There is are two 00311 tests in the code for an escape greater than ESC_b and less than ESC_Z to 00312 detect the types that may be repeated. These are the types that consume 00313 characters. If any new escapes are put in between that don't consume a 00314 character, that code will have to change. */ 00315 00316 enum { ESC_A = 1, ESC_G, ESC_B, ESC_b, ESC_D, ESC_d, ESC_S, ESC_s, ESC_W, 00317 ESC_w, ESC_dum1, ESC_C, ESC_P, ESC_p, ESC_X, ESC_Z, ESC_z, ESC_E, 00318 ESC_Q, ESC_REF }; 00319 00320 /* Flag bits and data types for the extended class (OP_XCLASS) for classes that 00321 contain UTF-8 characters with values greater than 255. */ 00322 00323 #define XCL_NOT 0x01 /* Flag: this is a negative class */ 00324 #define XCL_MAP 0x02 /* Flag: a 32-byte map is present */ 00325 00326 #define XCL_END 0 /* Marks end of individual items */ 00327 #define XCL_SINGLE 1 /* Single item (one multibyte char) follows */ 00328 #define XCL_RANGE 2 /* A range (two multibyte chars) follows */ 00329 #define XCL_PROP 3 /* Unicode property (one property code) follows */ 00330 #define XCL_NOTPROP 4 /* Unicode inverted property (ditto) */ 00331 00332 00333 /* Opcode table: OP_BRA must be last, as all values >= it are used for brackets 00334 that extract substrings. Starting from 1 (i.e. after OP_END), the values up to 00335 OP_EOD must correspond in order to the list of escapes immediately above. 00336 Note that whenever this list is updated, the two macro definitions that follow 00337 must also be updated to match. */ 00338 00339 enum { 00340 OP_END, /* 0 End of pattern */ 00341 00342 /* Values corresponding to backslashed metacharacters */ 00343 00344 OP_SOD, /* 1 Start of data: \A */ 00345 OP_SOM, /* 2 Start of match (subject + offset): \G */ 00346 OP_NOT_WORD_BOUNDARY, /* 3 \B */ 00347 OP_WORD_BOUNDARY, /* 4 \b */ 00348 OP_NOT_DIGIT, /* 5 \D */ 00349 OP_DIGIT, /* 6 \d */ 00350 OP_NOT_WHITESPACE, /* 7 \S */ 00351 OP_WHITESPACE, /* 8 \s */ 00352 OP_NOT_WORDCHAR, /* 9 \W */ 00353 OP_WORDCHAR, /* 10 \w */ 00354 OP_ANY, /* 11 Match any character */ 00355 OP_ANYBYTE, /* 12 Match any byte (\C); different to OP_ANY for UTF-8 */ 00356 OP_NOTPROP, /* 13 \P (not Unicode property) */ 00357 OP_PROP, /* 14 \p (Unicode property) */ 00358 OP_EXTUNI, /* 15 \X (extended Unicode sequence */ 00359 OP_EODN, /* 16 End of data or \n at end of data: \Z. */ 00360 OP_EOD, /* 17 End of data: \z */ 00361 00362 OP_OPT, /* 18 Set runtime options */ 00363 OP_CIRC, /* 19 Start of line - varies with multiline switch */ 00364 OP_DOLL, /* 20 End of line - varies with multiline switch */ 00365 OP_CHAR, /* 21 Match one character, casefully */ 00366 OP_CHARNC, /* 22 Match one character, caselessly */ 00367 OP_NOT, /* 23 Match anything but the following char */ 00368 00369 OP_STAR, /* 24 The maximizing and minimizing versions of */ 00370 OP_MINSTAR, /* 25 all these opcodes must come in pairs, with */ 00371 OP_PLUS, /* 26 the minimizing one second. */ 00372 OP_MINPLUS, /* 27 This first set applies to single characters */ 00373 OP_QUERY, /* 28 */ 00374 OP_MINQUERY, /* 29 */ 00375 OP_UPTO, /* 30 From 0 to n matches */ 00376 OP_MINUPTO, /* 31 */ 00377 OP_EXACT, /* 32 Exactly n matches */ 00378 00379 OP_NOTSTAR, /* 33 The maximizing and minimizing versions of */ 00380 OP_NOTMINSTAR, /* 34 all these opcodes must come in pairs, with */ 00381 OP_NOTPLUS, /* 35 the minimizing one second. */ 00382 OP_NOTMINPLUS, /* 36 This set applies to "not" single characters */ 00383 OP_NOTQUERY, /* 37 */ 00384 OP_NOTMINQUERY, /* 38 */ 00385 OP_NOTUPTO, /* 39 From 0 to n matches */ 00386 OP_NOTMINUPTO, /* 40 */ 00387 OP_NOTEXACT, /* 41 Exactly n matches */ 00388 00389 OP_TYPESTAR, /* 42 The maximizing and minimizing versions of */ 00390 OP_TYPEMINSTAR, /* 43 all these opcodes must come in pairs, with */ 00391 OP_TYPEPLUS, /* 44 the minimizing one second. These codes must */ 00392 OP_TYPEMINPLUS, /* 45 be in exactly the same order as those above. */ 00393 OP_TYPEQUERY, /* 46 This set applies to character types such as \d */ 00394 OP_TYPEMINQUERY, /* 47 */ 00395 OP_TYPEUPTO, /* 48 From 0 to n matches */ 00396 OP_TYPEMINUPTO, /* 49 */ 00397 OP_TYPEEXACT, /* 50 Exactly n matches */ 00398 00399 OP_CRSTAR, /* 51 The maximizing and minimizing versions of */ 00400 OP_CRMINSTAR, /* 52 all these opcodes must come in pairs, with */ 00401 OP_CRPLUS, /* 53 the minimizing one second. These codes must */ 00402 OP_CRMINPLUS, /* 54 be in exactly the same order as those above. */ 00403 OP_CRQUERY, /* 55 These are for character classes and back refs */ 00404 OP_CRMINQUERY, /* 56 */ 00405 OP_CRRANGE, /* 57 These are different to the three sets above. */ 00406 OP_CRMINRANGE, /* 58 */ 00407 00408 OP_CLASS, /* 59 Match a character class, chars < 256 only */ 00409 OP_NCLASS, /* 60 Same, but the bitmap was created from a negative 00410 class - the difference is relevant only when a UTF-8 00411 character > 255 is encountered. */ 00412 00413 OP_XCLASS, /* 61 Extended class for handling UTF-8 chars within the 00414 class. This does both positive and negative. */ 00415 00416 OP_REF, /* 62 Match a back reference */ 00417 OP_RECURSE, /* 63 Match a numbered subpattern (possibly recursive) */ 00418 OP_CALLOUT, /* 64 Call out to external function if provided */ 00419 00420 OP_ALT, /* 65 Start of alternation */ 00421 OP_KET, /* 66 End of group that doesn't have an unbounded repeat */ 00422 OP_KETRMAX, /* 67 These two must remain together and in this */ 00423 OP_KETRMIN, /* 68 order. They are for groups the repeat for ever. */ 00424 00425 /* The assertions must come before ONCE and COND */ 00426 00427 OP_ASSERT, /* 69 Positive lookahead */ 00428 OP_ASSERT_NOT, /* 70 Negative lookahead */ 00429 OP_ASSERTBACK, /* 71 Positive lookbehind */ 00430 OP_ASSERTBACK_NOT, /* 72 Negative lookbehind */ 00431 OP_REVERSE, /* 73 Move pointer back - used in lookbehind assertions */ 00432 00433 /* ONCE and COND must come after the assertions, with ONCE first, as there's 00434 a test for >= ONCE for a subpattern that isn't an assertion. */ 00435 00436 OP_ONCE, /* 74 Once matched, don't back up into the subpattern */ 00437 OP_COND, /* 75 Conditional group */ 00438 OP_CREF, /* 76 Used to hold an extraction string number (cond ref) */ 00439 00440 OP_BRAZERO, /* 77 These two must remain together and in this */ 00441 OP_BRAMINZERO, /* 78 order. */ 00442 00443 OP_BRANUMBER, /* 79 Used for extracting brackets whose number is greater 00444 than can fit into an opcode. */ 00445 00446 OP_BRA /* 80 This and greater values are used for brackets that 00447 extract substrings up to EXTRACT_BASIC_MAX. After 00448 that, use is made of OP_BRANUMBER. */ 00449 }; 00450 00451 /* WARNING WARNING WARNING: There is an implicit assumption in pcre.c and 00452 study.c that all opcodes are less than 128 in value. This makes handling UTF-8 00453 character sequences easier. */ 00454 00455 /* The highest extraction number before we have to start using additional 00456 bytes. (Originally PCRE didn't have support for extraction counts highter than 00457 this number.) The value is limited by the number of opcodes left after OP_BRA, 00458 i.e. 255 - OP_BRA. We actually set it a bit lower to leave room for additional 00459 opcodes. */ 00460 00461 #define EXTRACT_BASIC_MAX 100 00462 00463 00464 /* This macro defines textual names for all the opcodes. There are used only 00465 for debugging, in pcre.c when DEBUG is defined, and also in pcretest.c. The 00466 macro is referenced only in printint.c. */ 00467 00468 #define OP_NAME_LIST \ 00469 "End", "\\A", "\\G", "\\B", "\\b", "\\D", "\\d", \ 00470 "\\S", "\\s", "\\W", "\\w", "Any", "Anybyte", \ 00471 "notprop", "prop", "extuni", \ 00472 "\\Z", "\\z", \ 00473 "Opt", "^", "$", "char", "charnc", "not", \ 00474 "*", "*?", "+", "+?", "?", "??", "{", "{", "{", \ 00475 "*", "*?", "+", "+?", "?", "??", "{", "{", "{", \ 00476 "*", "*?", "+", "+?", "?", "??", "{", "{", "{", \ 00477 "*", "*?", "+", "+?", "?", "??", "{", "{", \ 00478 "class", "nclass", "xclass", "Ref", "Recurse", "Callout", \ 00479 "Alt", "Ket", "KetRmax", "KetRmin", "Assert", "Assert not", \ 00480 "AssertB", "AssertB not", "Reverse", "Once", "Cond", "Cond ref",\ 00481 "Brazero", "Braminzero", "Branumber", "Bra" 00482 00483 00484 /* This macro defines the length of fixed length operations in the compiled 00485 regex. The lengths are used when searching for specific things, and also in the 00486 debugging printing of a compiled regex. We use a macro so that it can be 00487 incorporated both into pcre.c and pcretest.c without being publicly exposed. 00488 00489 As things have been extended, some of these are no longer fixed lenths, but are 00490 minima instead. For example, the length of a single-character repeat may vary 00491 in UTF-8 mode. The code that uses this table must know about such things. */ 00492 00493 #define OP_LENGTHS \ 00494 1, /* End */ \ 00495 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* \A, \G, \B, \B, \D, \d, \S, \s, \W, \w */ \ 00496 1, 1, /* Any, Anybyte */ \ 00497 2, 2, 1, /* NOTPROP, PROP, EXTUNI */ \ 00498 1, 1, 2, 1, 1, /* \Z, \z, Opt, ^, $ */ \ 00499 2, /* Char - the minimum length */ \ 00500 2, /* Charnc - the minimum length */ \ 00501 2, /* not */ \ 00502 /* Positive single-char repeats ** These are */ \ 00503 2, 2, 2, 2, 2, 2, /* *, *?, +, +?, ?, ?? ** minima in */ \ 00504 4, 4, 4, /* upto, minupto, exact ** UTF-8 mode */ \ 00505 /* Negative single-char repeats - only for chars < 256 */ \ 00506 2, 2, 2, 2, 2, 2, /* NOT *, *?, +, +?, ?, ?? */ \ 00507 4, 4, 4, /* NOT upto, minupto, exact */ \ 00508 /* Positive type repeats */ \ 00509 2, 2, 2, 2, 2, 2, /* Type *, *?, +, +?, ?, ?? */ \ 00510 4, 4, 4, /* Type upto, minupto, exact */ \ 00511 /* Character class & ref repeats */ \ 00512 1, 1, 1, 1, 1, 1, /* *, *?, +, +?, ?, ?? */ \ 00513 5, 5, /* CRRANGE, CRMINRANGE */ \ 00514 33, /* CLASS */ \ 00515 33, /* NCLASS */ \ 00516 0, /* XCLASS - variable length */ \ 00517 3, /* REF */ \ 00518 1+LINK_SIZE, /* RECURSE */ \ 00519 2+2*LINK_SIZE, /* CALLOUT */ \ 00520 1+LINK_SIZE, /* Alt */ \ 00521 1+LINK_SIZE, /* Ket */ \ 00522 1+LINK_SIZE, /* KetRmax */ \ 00523 1+LINK_SIZE, /* KetRmin */ \ 00524 1+LINK_SIZE, /* Assert */ \ 00525 1+LINK_SIZE, /* Assert not */ \ 00526 1+LINK_SIZE, /* Assert behind */ \ 00527 1+LINK_SIZE, /* Assert behind not */ \ 00528 1+LINK_SIZE, /* Reverse */ \ 00529 1+LINK_SIZE, /* Once */ \ 00530 1+LINK_SIZE, /* COND */ \ 00531 3, /* CREF */ \ 00532 1, 1, /* BRAZERO, BRAMINZERO */ \ 00533 3, /* BRANUMBER */ \ 00534 1+LINK_SIZE /* BRA */ \ 00535 00536 00537 /* A magic value for OP_CREF to indicate the "in recursion" condition. */ 00538 00539 #define CREF_RECURSE 0xffff 00540 00541 /* The texts of compile-time error messages are defined as macros here so that 00542 they can be accessed by the POSIX wrapper and converted into error codes. Yes, 00543 I could have used error codes in the first place, but didn't feel like changing 00544 just to accommodate the POSIX wrapper. */ 00545 00546 #define ERR1 "\\ at end of pattern" 00547 #define ERR2 "\\c at end of pattern" 00548 #define ERR3 "unrecognized character follows \\" 00549 #define ERR4 "numbers out of order in {} quantifier" 00550 #define ERR5 "number too big in {} quantifier" 00551 #define ERR6 "missing terminating ] for character class" 00552 #define ERR7 "invalid escape sequence in character class" 00553 #define ERR8 "range out of order in character class" 00554 #define ERR9 "nothing to repeat" 00555 #define ERR10 "operand of unlimited repeat could match the empty string" 00556 #define ERR11 "internal error: unexpected repeat" 00557 #define ERR12 "unrecognized character after (?" 00558 #define ERR13 "POSIX named classes are supported only within a class" 00559 #define ERR14 "missing )" 00560 #define ERR15 "reference to non-existent subpattern" 00561 #define ERR16 "erroffset passed as NULL" 00562 #define ERR17 "unknown option bit(s) set" 00563 #define ERR18 "missing ) after comment" 00564 #define ERR19 "parentheses nested too deeply" 00565 #define ERR20 "regular expression too large" 00566 #define ERR21 "failed to get memory" 00567 #define ERR22 "unmatched parentheses" 00568 #define ERR23 "internal error: code overflow" 00569 #define ERR24 "unrecognized character after (?<" 00570 #define ERR25 "lookbehind assertion is not fixed length" 00571 #define ERR26 "malformed number after (?(" 00572 #define ERR27 "conditional group contains more than two branches" 00573 #define ERR28 "assertion expected after (?(" 00574 #define ERR29 "(?R or (?digits must be followed by )" 00575 #define ERR30 "unknown POSIX class name" 00576 #define ERR31 "POSIX collating elements are not supported" 00577 #define ERR32 "this version of PCRE is not compiled with PCRE_UTF8 support" 00578 #define ERR33 "spare error" 00579 #define ERR34 "character value in \\x{...} sequence is too large" 00580 #define ERR35 "invalid condition (?(0)" 00581 #define ERR36 "\\C not allowed in lookbehind assertion" 00582 #define ERR37 "PCRE does not support \\L, \\l, \\N, \\U, or \\u" 00583 #define ERR38 "number after (?C is > 255" 00584 #define ERR39 "closing ) for (?C expected" 00585 #define ERR40 "recursive call could loop indefinitely" 00586 #define ERR41 "unrecognized character after (?P" 00587 #define ERR42 "syntax error after (?P" 00588 #define ERR43 "two named groups have the same name" 00589 #define ERR44 "invalid UTF-8 string" 00590 #define ERR45 "support for \\P, \\p, and \\X has not been compiled" 00591 #define ERR46 "malformed \\P or \\p sequence" 00592 #define ERR47 "unknown property name after \\P or \\p" 00593 00594 /* The real format of the start of the pcre block; the index of names and the 00595 code vector run on as long as necessary after the end. We store an explicit 00596 offset to the name table so that if a regex is compiled on one host, saved, and 00597 then run on another where the size of pointers is different, all might still 00598 be well. For the case of compiled-on-4 and run-on-8, we include an extra 00599 pointer that is always NULL. For future-proofing, we also include a few dummy 00600 fields - even though you can never get this planning right! 00601 00602 NOTE NOTE NOTE: 00603 Because people can now save and re-use compiled patterns, any additions to this 00604 structure should be made at the end, and something earlier (e.g. a new 00605 flag in the options or one of the dummy fields) should indicate that the new 00606 fields are present. Currently PCRE always sets the dummy fields to zero. 00607 NOTE NOTE NOTE: 00608 */ 00609 00610 typedef struct real_pcre { 00611 pcre_uint32 magic_number; 00612 pcre_uint32 size; /* Total that was malloced */ 00613 pcre_uint32 options; 00614 pcre_uint32 dummy1; /* For future use, maybe */ 00615 00616 pcre_uint16 top_bracket; 00617 pcre_uint16 top_backref; 00618 pcre_uint16 first_byte; 00619 pcre_uint16 req_byte; 00620 pcre_uint16 name_table_offset; /* Offset to name table that follows */ 00621 pcre_uint16 name_entry_size; /* Size of any name items */ 00622 pcre_uint16 name_count; /* Number of name items */ 00623 pcre_uint16 dummy2; /* For future use, maybe */ 00624 00625 const unsigned char *tables; /* Pointer to tables or NULL for std */ 00626 const unsigned char *nullpad; /* NULL padding */ 00627 } real_pcre; 00628 00629 /* The format of the block used to store data from pcre_study(). The same 00630 remark (see NOTE above) about extending this structure applies. */ 00631 00632 typedef struct pcre_study_data { 00633 pcre_uint32 size; /* Total that was malloced */ 00634 pcre_uint32 options; 00635 uschar start_bits[32]; 00636 } pcre_study_data; 00637 00638 /* Structure for passing "static" information around between the functions 00639 doing the compiling, so that they are thread-safe. */ 00640 00641 typedef struct compile_data { 00642 const uschar *lcc; /* Points to lower casing table */ 00643 const uschar *fcc; /* Points to case-flipping table */ 00644 const uschar *cbits; /* Points to character type table */ 00645 const uschar *ctypes; /* Points to table of type maps */ 00646 const uschar *start_code; /* The start of the compiled code */ 00647 const uschar *start_pattern; /* The start of the pattern */ 00648 uschar *name_table; /* The name/number table */ 00649 int names_found; /* Number of entries so far */ 00650 int name_entry_size; /* Size of each entry */ 00651 int top_backref; /* Maximum back reference */ 00652 unsigned int backref_map; /* Bitmap of low back refs */ 00653 int req_varyopt; /* "After variable item" flag for reqbyte */ 00654 BOOL nopartial; /* Set TRUE if partial won't work */ 00655 } compile_data; 00656 00657 /* Structure for maintaining a chain of pointers to the currently incomplete 00658 branches, for testing for left recursion. */ 00659 00660 typedef struct branch_chain { 00661 struct branch_chain *outer; 00662 uschar *current; 00663 } branch_chain; 00664 00665 /* Structure for items in a linked list that represents an explicit recursive 00666 call within the pattern. */ 00667 00668 typedef struct recursion_info { 00669 struct recursion_info *prevrec; /* Previous recursion record (or NULL) */ 00670 int group_num; /* Number of group that was called */ 00671 const uschar *after_call; /* "Return value": points after the call in the expr */ 00672 const uschar *save_start; /* Old value of md->start_match */ 00673 int *offset_save; /* Pointer to start of saved offsets */ 00674 int saved_max; /* Number of saved offsets */ 00675 } recursion_info; 00676 00677 /* When compiling in a mode that doesn't use recursive calls to match(), 00678 a structure is used to remember local variables on the heap. It is defined in 00679 pcre.c, close to the match() function, so that it is easy to keep it in step 00680 with any changes of local variable. However, the pointer to the current frame 00681 must be saved in some "static" place over a longjmp(). We declare the 00682 structure here so that we can put a pointer in the match_data structure. 00683 NOTE: This isn't used for a "normal" compilation of pcre. */ 00684 00685 struct heapframe; 00686 00687 /* Structure for passing "static" information around between the functions 00688 doing the matching, so that they are thread-safe. */ 00689 00690 typedef struct match_data { 00691 unsigned long int match_call_count; /* As it says */ 00692 unsigned long int match_limit;/* As it says */ 00693 int *offset_vector; /* Offset vector */ 00694 int offset_end; /* One past the end */ 00695 int offset_max; /* The maximum usable for return data */ 00696 const uschar *lcc; /* Points to lower casing table */ 00697 const uschar *ctypes; /* Points to table of type maps */ 00698 BOOL offset_overflow; /* Set if too many extractions */ 00699 BOOL notbol; /* NOTBOL flag */ 00700 BOOL noteol; /* NOTEOL flag */ 00701 BOOL utf8; /* UTF8 flag */ 00702 BOOL endonly; /* Dollar not before final \n */ 00703 BOOL notempty; /* Empty string match not wanted */ 00704 BOOL partial; /* PARTIAL flag */ 00705 BOOL hitend; /* Hit the end of the subject at some point */ 00706 const uschar *start_code; /* For use when recursing */ 00707 const uschar *start_subject; /* Start of the subject string */ 00708 const uschar *end_subject; /* End of the subject string */ 00709 const uschar *start_match; /* Start of this match attempt */ 00710 const uschar *end_match_ptr; /* Subject position at end match */ 00711 int end_offset_top; /* Highwater mark at end of match */ 00712 int capture_last; /* Most recent capture number */ 00713 int start_offset; /* The start offset value */ 00714 recursion_info *recursive; /* Linked list of recursion data */ 00715 void *callout_data; /* To pass back to callouts */ 00716 struct heapframe *thisframe; /* Used only when compiling for no recursion */ 00717 } match_data; 00718 00719 /* Bit definitions for entries in the pcre_ctypes table. */ 00720 00721 #define ctype_space 0x01 00722 #define ctype_letter 0x02 00723 #define ctype_digit 0x04 00724 #define ctype_xdigit 0x08 00725 #define ctype_word 0x10 /* alphameric or '_' */ 00726 #define ctype_meta 0x80 /* regexp meta char or zero (end pattern) */ 00727 00728 /* Offsets for the bitmap tables in pcre_cbits. Each table contains a set 00729 of bits for a class map. Some classes are built by combining these tables. */ 00730 00731 #define cbit_space 0 /* [:space:] or \s */ 00732 #define cbit_xdigit 32 /* [:xdigit:] */ 00733 #define cbit_digit 64 /* [:digit:] or \d */ 00734 #define cbit_upper 96 /* [:upper:] */ 00735 #define cbit_lower 128 /* [:lower:] */ 00736 #define cbit_word 160 /* [:word:] or \w */ 00737 #define cbit_graph 192 /* [:graph:] */ 00738 #define cbit_print 224 /* [:print:] */ 00739 #define cbit_punct 256 /* [:punct:] */ 00740 #define cbit_cntrl 288 /* [:cntrl:] */ 00741 #define cbit_length 320 /* Length of the cbits table */ 00742 00743 /* Offsets of the various tables from the base tables pointer, and 00744 total length. */ 00745 00746 #define lcc_offset 0 00747 #define fcc_offset 256 00748 #define cbits_offset 512 00749 #define ctypes_offset (cbits_offset + cbit_length) 00750 #define tables_length (ctypes_offset + 256) 00751 00752 /* End of internal.h */