PageRenderTime 61ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/beta3/harbour/source/hbpcre/pcreinal.h

#
C Header | 912 lines | 502 code | 181 blank | 229 comment | 28 complexity | 3ab546ee02d1aa260ac31b808aa9ab45 MD5 | raw file
Possible License(s): AGPL-1.0, BSD-3-Clause, CC-BY-SA-3.0, LGPL-3.0, GPL-2.0, LGPL-2.0, LGPL-2.1
  1. /*************************************************
  2. * Perl-Compatible Regular Expressions *
  3. *************************************************/
  4. /* PCRE is a library of functions to support regular expressions whose syntax
  5. and semantics are as close as possible to those of the Perl 5 language.
  6. Written by Philip Hazel
  7. Copyright (c) 1997-2005 University of Cambridge
  8. -----------------------------------------------------------------------------
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions are met:
  11. * Redistributions of source code must retain the above copyright notice,
  12. this list of conditions and the following disclaimer.
  13. * Redistributions in binary form must reproduce the above copyright
  14. notice, this list of conditions and the following disclaimer in the
  15. documentation and/or other materials provided with the distribution.
  16. * Neither the name of the University of Cambridge nor the names of its
  17. contributors may be used to endorse or promote products derived from
  18. this software without specific prior written permission.
  19. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  23. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. POSSIBILITY OF SUCH DAMAGE.
  30. -----------------------------------------------------------------------------
  31. */
  32. /* This header contains definitions that are shared between the different
  33. modules, but which are not relevant to the exported API. This includes some
  34. functions whose names all begin with "_pcre_". */
  35. /* Define DEBUG to get debugging output on stdout. */
  36. /****
  37. #define DEBUG
  38. ****/
  39. /* Use a macro for debugging printing, 'cause that eliminates the use of #ifdef
  40. inline, and there are *still* stupid compilers about that don't like indented
  41. pre-processor statements, or at least there were when I first wrote this. After
  42. all, it had only been about 10 years then... */
  43. #ifdef DEBUG
  44. #define DPRINTF(p) printf p
  45. #else
  46. #define DPRINTF(p) /*nothing*/
  47. #endif
  48. /* Get the definitions provided by running "configure" */
  49. #include "config.h"
  50. /* Standard C headers plus the external interface definition. The only time
  51. setjmp and stdarg are used is when NO_RECURSE is set. */
  52. #include <ctype.h>
  53. #include <limits.h>
  54. #include <setjmp.h>
  55. #include <stdarg.h>
  56. #include <stddef.h>
  57. #include <stdio.h>
  58. #include <stdlib.h>
  59. #include <string.h>
  60. #ifndef PCRE_SPY
  61. #define PCRE_DEFINITION /* Win32 __declspec(export) trigger for .dll */
  62. #endif
  63. /* We need to have types that specify unsigned 16-bit and 32-bit integers. We
  64. cannot determine these outside the compilation (e.g. by running a program as
  65. part of "configure") because PCRE is often cross-compiled for use on other
  66. systems. Instead we make use of the maximum sizes that are available at
  67. preprocessor time in standard C environments. */
  68. #if USHRT_MAX == 65535
  69. typedef unsigned short pcre_uint16;
  70. #elif UINT_MAX == 65535
  71. typedef unsigned int pcre_uint16;
  72. #else
  73. #error Cannot determine a type for 16-bit unsigned integers
  74. #endif
  75. #if UINT_MAX == 4294967295
  76. typedef unsigned int pcre_uint32;
  77. #elif ULONG_MAX == 4294967295
  78. typedef unsigned long int pcre_uint32;
  79. #else
  80. #error Cannot determine a type for 32-bit unsigned integers
  81. #endif
  82. /* All character handling must be done as unsigned characters. Otherwise there
  83. are problems with top-bit-set characters and functions such as isspace().
  84. However, we leave the interface to the outside world as char *, because that
  85. should make things easier for callers. We define a short type for unsigned char
  86. to save lots of typing. I tried "uchar", but it causes problems on Digital
  87. Unix, where it is defined in sys/types, so use "uschar" instead. */
  88. typedef unsigned char uschar;
  89. /* Include the public PCRE header */
  90. #include "pcre.h"
  91. /* Include the (copy of) the public ucp header, changing the external name into
  92. a private one. This does no harm, even if we aren't compiling UCP support. */
  93. #define ucp_findchar _pcre_ucp_findchar
  94. #include "ucp.h"
  95. /* When compiling for use with the Virtual Pascal compiler, these functions
  96. need to have their names changed. PCRE must be compiled with the -DVPCOMPAT
  97. option on the command line. */
  98. #ifdef VPCOMPAT
  99. #define strncmp(s1,s2,m) _strncmp(s1,s2,m)
  100. #define memcpy(d,s,n) _memcpy(d,s,n)
  101. #define memmove(d,s,n) _memmove(d,s,n)
  102. #define memset(s,c,n) _memset(s,c,n)
  103. #else /* VPCOMPAT */
  104. /* To cope with SunOS4 and other systems that lack memmove() but have bcopy(),
  105. define a macro for memmove() if HAVE_MEMMOVE is false, provided that HAVE_BCOPY
  106. is set. Otherwise, include an emulating function for those systems that have
  107. neither (there some non-Unix environments where this is the case). This assumes
  108. that all calls to memmove are moving strings upwards in store, which is the
  109. case in PCRE. */
  110. #if ! HAVE_MEMMOVE
  111. #undef memmove /* some systems may have a macro */
  112. #if HAVE_BCOPY
  113. #define memmove(a, b, c) bcopy(b, a, c)
  114. #else /* HAVE_BCOPY */
  115. void *
  116. pcre_memmove(unsigned char *dest, const unsigned char *src, size_t n)
  117. {
  118. int i;
  119. dest += n;
  120. src += n;
  121. for (i = 0; i < n; ++i) *(--dest) = *(--src);
  122. }
  123. #define memmove(a, b, c) pcre_memmove(a, b, c)
  124. #endif /* not HAVE_BCOPY */
  125. #endif /* not HAVE_MEMMOVE */
  126. #endif /* not VPCOMPAT */
  127. /* PCRE keeps offsets in its compiled code as 2-byte quantities (always stored
  128. in big-endian order) by default. These are used, for example, to link from the
  129. start of a subpattern to its alternatives and its end. The use of 2 bytes per
  130. offset limits the size of the compiled regex to around 64K, which is big enough
  131. for almost everybody. However, I received a request for an even bigger limit.
  132. For this reason, and also to make the code easier to maintain, the storing and
  133. loading of offsets from the byte string is now handled by the macros that are
  134. defined here.
  135. The macros are controlled by the value of LINK_SIZE. This defaults to 2 in
  136. the config.h file, but can be overridden by using -D on the command line. This
  137. is automated on Unix systems via the "configure" command. */
  138. #if LINK_SIZE == 2
  139. #define PUT(a,n,d) \
  140. (a[n] = (d) >> 8), \
  141. (a[(n)+1] = (d) & 255)
  142. #define GET(a,n) \
  143. (((a)[n] << 8) | (a)[(n)+1])
  144. #define MAX_PATTERN_SIZE (1 << 16)
  145. #elif LINK_SIZE == 3
  146. #define PUT(a,n,d) \
  147. (a[n] = (d) >> 16), \
  148. (a[(n)+1] = (d) >> 8), \
  149. (a[(n)+2] = (d) & 255)
  150. #define GET(a,n) \
  151. (((a)[n] << 16) | ((a)[(n)+1] << 8) | (a)[(n)+2])
  152. #define MAX_PATTERN_SIZE (1 << 24)
  153. #elif LINK_SIZE == 4
  154. #define PUT(a,n,d) \
  155. (a[n] = (d) >> 24), \
  156. (a[(n)+1] = (d) >> 16), \
  157. (a[(n)+2] = (d) >> 8), \
  158. (a[(n)+3] = (d) & 255)
  159. #define GET(a,n) \
  160. (((a)[n] << 24) | ((a)[(n)+1] << 16) | ((a)[(n)+2] << 8) | (a)[(n)+3])
  161. #define MAX_PATTERN_SIZE (1 << 30) /* Keep it positive */
  162. #else
  163. #error LINK_SIZE must be either 2, 3, or 4
  164. #endif
  165. /* Convenience macro defined in terms of the others */
  166. #define PUTINC(a,n,d) PUT(a,n,d), a += LINK_SIZE
  167. /* PCRE uses some other 2-byte quantities that do not change when the size of
  168. offsets changes. There are used for repeat counts and for other things such as
  169. capturing parenthesis numbers in back references. */
  170. #define PUT2(a,n,d) \
  171. a[n] = (d) >> 8; \
  172. a[(n)+1] = (d) & 255
  173. #define GET2(a,n) \
  174. (((a)[n] << 8) | (a)[(n)+1])
  175. #define PUT2INC(a,n,d) PUT2(a,n,d), a += 2
  176. /* When UTF-8 encoding is being used, a character is no longer just a single
  177. byte. The macros for character handling generate simple sequences when used in
  178. byte-mode, and more complicated ones for UTF-8 characters. */
  179. #ifndef SUPPORT_UTF8
  180. #define GETCHAR(c, eptr) c = *eptr;
  181. #define GETCHARTEST(c, eptr) c = *eptr;
  182. #define GETCHARINC(c, eptr) c = *eptr++;
  183. #define GETCHARINCTEST(c, eptr) c = *eptr++;
  184. #define GETCHARLEN(c, eptr, len) c = *eptr;
  185. #define BACKCHAR(eptr)
  186. #else /* SUPPORT_UTF8 */
  187. /* Get the next UTF-8 character, not advancing the pointer. This is called when
  188. we know we are in UTF-8 mode. */
  189. #define GETCHAR(c, eptr) \
  190. c = *eptr; \
  191. if ((c & 0xc0) == 0xc0) \
  192. { \
  193. int gcii; \
  194. int gcaa = _pcre_utf8_table4[c & 0x3f]; /* Number of additional bytes */ \
  195. int gcss = 6*gcaa; \
  196. c = (c & _pcre_utf8_table3[gcaa]) << gcss; \
  197. for (gcii = 1; gcii <= gcaa; gcii++) \
  198. { \
  199. gcss -= 6; \
  200. c |= (eptr[gcii] & 0x3f) << gcss; \
  201. } \
  202. }
  203. /* Get the next UTF-8 character, testing for UTF-8 mode, and not advancing the
  204. pointer. */
  205. #define GETCHARTEST(c, eptr) \
  206. c = *eptr; \
  207. if (utf8 && (c & 0xc0) == 0xc0) \
  208. { \
  209. int gcii; \
  210. int gcaa = _pcre_utf8_table4[c & 0x3f]; /* Number of additional bytes */ \
  211. int gcss = 6*gcaa; \
  212. c = (c & _pcre_utf8_table3[gcaa]) << gcss; \
  213. for (gcii = 1; gcii <= gcaa; gcii++) \
  214. { \
  215. gcss -= 6; \
  216. c |= (eptr[gcii] & 0x3f) << gcss; \
  217. } \
  218. }
  219. /* Get the next UTF-8 character, advancing the pointer. This is called when we
  220. know we are in UTF-8 mode. */
  221. #define GETCHARINC(c, eptr) \
  222. c = *eptr++; \
  223. if ((c & 0xc0) == 0xc0) \
  224. { \
  225. int gcaa = _pcre_utf8_table4[c & 0x3f]; /* Number of additional bytes */ \
  226. int gcss = 6*gcaa; \
  227. c = (c & _pcre_utf8_table3[gcaa]) << gcss; \
  228. while (gcaa-- > 0) \
  229. { \
  230. gcss -= 6; \
  231. c |= (*eptr++ & 0x3f) << gcss; \
  232. } \
  233. }
  234. /* Get the next character, testing for UTF-8 mode, and advancing the pointer */
  235. #define GETCHARINCTEST(c, eptr) \
  236. c = *eptr++; \
  237. if (utf8 && (c & 0xc0) == 0xc0) \
  238. { \
  239. int gcaa = _pcre_utf8_table4[c & 0x3f]; /* Number of additional bytes */ \
  240. int gcss = 6*gcaa; \
  241. c = (c & _pcre_utf8_table3[gcaa]) << gcss; \
  242. while (gcaa-- > 0) \
  243. { \
  244. gcss -= 6; \
  245. c |= (*eptr++ & 0x3f) << gcss; \
  246. } \
  247. }
  248. /* Get the next UTF-8 character, not advancing the pointer, incrementing length
  249. if there are extra bytes. This is called when we know we are in UTF-8 mode. */
  250. #define GETCHARLEN(c, eptr, len) \
  251. c = *eptr; \
  252. if ((c & 0xc0) == 0xc0) \
  253. { \
  254. int gcii; \
  255. int gcaa = _pcre_utf8_table4[c & 0x3f]; /* Number of additional bytes */ \
  256. int gcss = 6*gcaa; \
  257. c = (c & _pcre_utf8_table3[gcaa]) << gcss; \
  258. for (gcii = 1; gcii <= gcaa; gcii++) \
  259. { \
  260. gcss -= 6; \
  261. c |= (eptr[gcii] & 0x3f) << gcss; \
  262. } \
  263. len += gcaa; \
  264. }
  265. /* If the pointer is not at the start of a character, move it back until
  266. it is. Called only in UTF-8 mode. */
  267. #define BACKCHAR(eptr) while((*eptr & 0xc0) == 0x80) eptr--;
  268. #endif
  269. /* In case there is no definition of offsetof() provided - though any proper
  270. Standard C system should have one. */
  271. #ifndef offsetof
  272. #define offsetof(p_type,field) ((size_t)&(((p_type *)0)->field))
  273. #endif
  274. /* These are the public options that can change during matching. */
  275. #define PCRE_IMS (PCRE_CASELESS|PCRE_MULTILINE|PCRE_DOTALL)
  276. /* Private options flags start at the most significant end of the four bytes,
  277. but skip the top bit so we can use ints for convenience without getting tangled
  278. with negative values. The public options defined in pcre.h start at the least
  279. significant end. Make sure they don't overlap! */
  280. #define PCRE_FIRSTSET 0x40000000 /* first_byte is set */
  281. #define PCRE_REQCHSET 0x20000000 /* req_byte is set */
  282. #define PCRE_STARTLINE 0x10000000 /* start after \n for multiline */
  283. #define PCRE_ICHANGED 0x08000000 /* i option changes within regex */
  284. #define PCRE_NOPARTIAL 0x04000000 /* can't use partial with this regex */
  285. /* Options for the "extra" block produced by pcre_study(). */
  286. #define PCRE_STUDY_MAPPED 0x01 /* a map of starting chars exists */
  287. /* Masks for identifying the public options that are permitted at compile
  288. time, run time, or study time, respectively. */
  289. #define PUBLIC_OPTIONS \
  290. (PCRE_CASELESS|PCRE_EXTENDED|PCRE_ANCHORED|PCRE_MULTILINE| \
  291. PCRE_DOTALL|PCRE_DOLLAR_ENDONLY|PCRE_EXTRA|PCRE_UNGREEDY|PCRE_UTF8| \
  292. PCRE_NO_AUTO_CAPTURE|PCRE_NO_UTF8_CHECK|PCRE_AUTO_CALLOUT|PCRE_FIRSTLINE)
  293. #define PUBLIC_EXEC_OPTIONS \
  294. (PCRE_ANCHORED|PCRE_NOTBOL|PCRE_NOTEOL|PCRE_NOTEMPTY|PCRE_NO_UTF8_CHECK| \
  295. PCRE_PARTIAL)
  296. #define PUBLIC_DFA_EXEC_OPTIONS \
  297. (PCRE_ANCHORED|PCRE_NOTBOL|PCRE_NOTEOL|PCRE_NOTEMPTY|PCRE_NO_UTF8_CHECK| \
  298. PCRE_PARTIAL|PCRE_DFA_SHORTEST|PCRE_DFA_RESTART)
  299. #define PUBLIC_STUDY_OPTIONS 0 /* None defined */
  300. /* Magic number to provide a small check against being handed junk. Also used
  301. to detect whether a pattern was compiled on a host of different endianness. */
  302. #define MAGIC_NUMBER 0x50435245UL /* 'PCRE' */
  303. /* Negative values for the firstchar and reqchar variables */
  304. #define REQ_UNSET (-2)
  305. #define REQ_NONE (-1)
  306. /* The maximum remaining length of subject we are prepared to search for a
  307. req_byte match. */
  308. #define REQ_BYTE_MAX 1000
  309. /* Flags added to firstbyte or reqbyte; a "non-literal" item is either a
  310. variable-length repeat, or a anything other than literal characters. */
  311. #define REQ_CASELESS 0x0100 /* indicates caselessness */
  312. #define REQ_VARY 0x0200 /* reqbyte followed non-literal item */
  313. /* Miscellaneous definitions */
  314. /* 30/08/2005 - <maurilio.longo@libero.it>
  315. Use xharbour typedefs if they exist, ie, this file
  316. has been included after xharbour ones
  317. */
  318. #ifndef FALSE
  319. typedef int BOOL;
  320. #define FALSE 0
  321. #define TRUE 1
  322. #endif
  323. /* Escape items that are just an encoding of a particular data value. Note that
  324. ESC_n is defined as yet another macro, which is set in config.h to either \n
  325. (the default) or \r (which some people want). */
  326. #ifndef ESC_e
  327. #define ESC_e 27
  328. #endif
  329. #ifndef ESC_f
  330. #define ESC_f '\f'
  331. #endif
  332. #ifndef ESC_n
  333. #define ESC_n NEWLINE
  334. #endif
  335. #ifndef ESC_r
  336. #define ESC_r '\r'
  337. #endif
  338. /* We can't officially use ESC_t because it is a POSIX reserved identifier
  339. (presumably because of all the others like size_t). */
  340. #ifndef ESC_tee
  341. #define ESC_tee '\t'
  342. #endif
  343. /* These are escaped items that aren't just an encoding of a particular data
  344. value such as \n. They must have non-zero values, as check_escape() returns
  345. their negation. Also, they must appear in the same order as in the opcode
  346. definitions below, up to ESC_z. There's a dummy for OP_ANY because it
  347. corresponds to "." rather than an escape sequence. The final one must be
  348. ESC_REF as subsequent values are used for \1, \2, \3, etc. There is are two
  349. tests in the code for an escape greater than ESC_b and less than ESC_Z to
  350. detect the types that may be repeated. These are the types that consume
  351. characters. If any new escapes are put in between that don't consume a
  352. character, that code will have to change. */
  353. enum { ESC_A = 1, ESC_G, ESC_B, ESC_b, ESC_D, ESC_d, ESC_S, ESC_s, ESC_W,
  354. ESC_w, ESC_dum1, ESC_C, ESC_P, ESC_p, ESC_X, ESC_Z, ESC_z, ESC_E,
  355. ESC_Q, ESC_REF };
  356. /* Flag bits and data types for the extended class (OP_XCLASS) for classes that
  357. contain UTF-8 characters with values greater than 255. */
  358. #define XCL_NOT 0x01 /* Flag: this is a negative class */
  359. #define XCL_MAP 0x02 /* Flag: a 32-byte map is present */
  360. #define XCL_END 0 /* Marks end of individual items */
  361. #define XCL_SINGLE 1 /* Single item (one multibyte char) follows */
  362. #define XCL_RANGE 2 /* A range (two multibyte chars) follows */
  363. #define XCL_PROP 3 /* Unicode property (one property code) follows */
  364. #define XCL_NOTPROP 4 /* Unicode inverted property (ditto) */
  365. /* Opcode table: OP_BRA must be last, as all values >= it are used for brackets
  366. that extract substrings. Starting from 1 (i.e. after OP_END), the values up to
  367. OP_EOD must correspond in order to the list of escapes immediately above.
  368. Note that whenever this list is updated, the two macro definitions that follow
  369. must also be updated to match. */
  370. enum {
  371. OP_END, /* 0 End of pattern */
  372. /* Values corresponding to backslashed metacharacters */
  373. OP_SOD, /* 1 Start of data: \A */
  374. OP_SOM, /* 2 Start of match (subject + offset): \G */
  375. OP_NOT_WORD_BOUNDARY, /* 3 \B */
  376. OP_WORD_BOUNDARY, /* 4 \b */
  377. OP_NOT_DIGIT, /* 5 \D */
  378. OP_DIGIT, /* 6 \d */
  379. OP_NOT_WHITESPACE, /* 7 \S */
  380. OP_WHITESPACE, /* 8 \s */
  381. OP_NOT_WORDCHAR, /* 9 \W */
  382. OP_WORDCHAR, /* 10 \w */
  383. OP_ANY, /* 11 Match any character */
  384. OP_ANYBYTE, /* 12 Match any byte (\C); different to OP_ANY for UTF-8 */
  385. OP_NOTPROP, /* 13 \P (not Unicode property) */
  386. OP_PROP, /* 14 \p (Unicode property) */
  387. OP_EXTUNI, /* 15 \X (extended Unicode sequence */
  388. OP_EODN, /* 16 End of data or \n at end of data: \Z. */
  389. OP_EOD, /* 17 End of data: \z */
  390. OP_OPT, /* 18 Set runtime options */
  391. OP_CIRC, /* 19 Start of line - varies with multiline switch */
  392. OP_DOLL, /* 20 End of line - varies with multiline switch */
  393. OP_CHAR, /* 21 Match one character, casefully */
  394. OP_CHARNC, /* 22 Match one character, caselessly */
  395. OP_NOT, /* 23 Match anything but the following char */
  396. OP_STAR, /* 24 The maximizing and minimizing versions of */
  397. OP_MINSTAR, /* 25 all these opcodes must come in pairs, with */
  398. OP_PLUS, /* 26 the minimizing one second. */
  399. OP_MINPLUS, /* 27 This first set applies to single characters */
  400. OP_QUERY, /* 28 */
  401. OP_MINQUERY, /* 29 */
  402. OP_UPTO, /* 30 From 0 to n matches */
  403. OP_MINUPTO, /* 31 */
  404. OP_EXACT, /* 32 Exactly n matches */
  405. OP_NOTSTAR, /* 33 The maximizing and minimizing versions of */
  406. OP_NOTMINSTAR, /* 34 all these opcodes must come in pairs, with */
  407. OP_NOTPLUS, /* 35 the minimizing one second. */
  408. OP_NOTMINPLUS, /* 36 This set applies to "not" single characters */
  409. OP_NOTQUERY, /* 37 */
  410. OP_NOTMINQUERY, /* 38 */
  411. OP_NOTUPTO, /* 39 From 0 to n matches */
  412. OP_NOTMINUPTO, /* 40 */
  413. OP_NOTEXACT, /* 41 Exactly n matches */
  414. OP_TYPESTAR, /* 42 The maximizing and minimizing versions of */
  415. OP_TYPEMINSTAR, /* 43 all these opcodes must come in pairs, with */
  416. OP_TYPEPLUS, /* 44 the minimizing one second. These codes must */
  417. OP_TYPEMINPLUS, /* 45 be in exactly the same order as those above. */
  418. OP_TYPEQUERY, /* 46 This set applies to character types such as \d */
  419. OP_TYPEMINQUERY, /* 47 */
  420. OP_TYPEUPTO, /* 48 From 0 to n matches */
  421. OP_TYPEMINUPTO, /* 49 */
  422. OP_TYPEEXACT, /* 50 Exactly n matches */
  423. OP_CRSTAR, /* 51 The maximizing and minimizing versions of */
  424. OP_CRMINSTAR, /* 52 all these opcodes must come in pairs, with */
  425. OP_CRPLUS, /* 53 the minimizing one second. These codes must */
  426. OP_CRMINPLUS, /* 54 be in exactly the same order as those above. */
  427. OP_CRQUERY, /* 55 These are for character classes and back refs */
  428. OP_CRMINQUERY, /* 56 */
  429. OP_CRRANGE, /* 57 These are different to the three sets above. */
  430. OP_CRMINRANGE, /* 58 */
  431. OP_CLASS, /* 59 Match a character class, chars < 256 only */
  432. OP_NCLASS, /* 60 Same, but the bitmap was created from a negative
  433. class - the difference is relevant only when a UTF-8
  434. character > 255 is encountered. */
  435. OP_XCLASS, /* 61 Extended class for handling UTF-8 chars within the
  436. class. This does both positive and negative. */
  437. OP_REF, /* 62 Match a back reference */
  438. OP_RECURSE, /* 63 Match a numbered subpattern (possibly recursive) */
  439. OP_CALLOUT, /* 64 Call out to external function if provided */
  440. OP_ALT, /* 65 Start of alternation */
  441. OP_KET, /* 66 End of group that doesn't have an unbounded repeat */
  442. OP_KETRMAX, /* 67 These two must remain together and in this */
  443. OP_KETRMIN, /* 68 order. They are for groups the repeat for ever. */
  444. /* The assertions must come before ONCE and COND */
  445. OP_ASSERT, /* 69 Positive lookahead */
  446. OP_ASSERT_NOT, /* 70 Negative lookahead */
  447. OP_ASSERTBACK, /* 71 Positive lookbehind */
  448. OP_ASSERTBACK_NOT, /* 72 Negative lookbehind */
  449. OP_REVERSE, /* 73 Move pointer back - used in lookbehind assertions */
  450. /* ONCE and COND must come after the assertions, with ONCE first, as there's
  451. a test for >= ONCE for a subpattern that isn't an assertion. */
  452. OP_ONCE, /* 74 Once matched, don't back up into the subpattern */
  453. OP_COND, /* 75 Conditional group */
  454. OP_CREF, /* 76 Used to hold an extraction string number (cond ref) */
  455. OP_BRAZERO, /* 77 These two must remain together and in this */
  456. OP_BRAMINZERO, /* 78 order. */
  457. OP_BRANUMBER, /* 79 Used for extracting brackets whose number is greater
  458. than can fit into an opcode. */
  459. OP_BRA /* 80 This and greater values are used for brackets that
  460. extract substrings up to EXTRACT_BASIC_MAX. After
  461. that, use is made of OP_BRANUMBER. */
  462. };
  463. /* WARNING WARNING WARNING: There is an implicit assumption in pcre.c and
  464. study.c that all opcodes are less than 128 in value. This makes handling UTF-8
  465. character sequences easier. */
  466. /* The highest extraction number before we have to start using additional
  467. bytes. (Originally PCRE didn't have support for extraction counts highter than
  468. this number.) The value is limited by the number of opcodes left after OP_BRA,
  469. i.e. 255 - OP_BRA. We actually set it a bit lower to leave room for additional
  470. opcodes. */
  471. #define EXTRACT_BASIC_MAX 100
  472. /* This macro defines textual names for all the opcodes. These are used only
  473. for debugging. The macro is referenced only in pcreprni.c. */
  474. #define OP_NAME_LIST \
  475. "End", "\\A", "\\G", "\\B", "\\b", "\\D", "\\d", \
  476. "\\S", "\\s", "\\W", "\\w", "Any", "Anybyte", \
  477. "notprop", "prop", "extuni", \
  478. "\\Z", "\\z", \
  479. "Opt", "^", "$", "char", "charnc", "not", \
  480. "*", "*?", "+", "+?", "?", "??", "{", "{", "{", \
  481. "*", "*?", "+", "+?", "?", "??", "{", "{", "{", \
  482. "*", "*?", "+", "+?", "?", "??", "{", "{", "{", \
  483. "*", "*?", "+", "+?", "?", "??", "{", "{", \
  484. "class", "nclass", "xclass", "Ref", "Recurse", "Callout", \
  485. "Alt", "Ket", "KetRmax", "KetRmin", "Assert", "Assert not", \
  486. "AssertB", "AssertB not", "Reverse", "Once", "Cond", "Cond ref",\
  487. "Brazero", "Braminzero", "Branumber", "Bra"
  488. /* This macro defines the length of fixed length operations in the compiled
  489. regex. The lengths are used when searching for specific things, and also in the
  490. debugging printing of a compiled regex. We use a macro so that it can be
  491. defined close to the definitions of the opcodes themselves.
  492. As things have been extended, some of these are no longer fixed lenths, but are
  493. minima instead. For example, the length of a single-character repeat may vary
  494. in UTF-8 mode. The code that uses this table must know about such things. */
  495. #define OP_LENGTHS \
  496. 1, /* End */ \
  497. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* \A, \G, \B, \B, \D, \d, \S, \s, \W, \w */ \
  498. 1, 1, /* Any, Anybyte */ \
  499. 2, 2, 1, /* NOTPROP, PROP, EXTUNI */ \
  500. 1, 1, 2, 1, 1, /* \Z, \z, Opt, ^, $ */ \
  501. 2, /* Char - the minimum length */ \
  502. 2, /* Charnc - the minimum length */ \
  503. 2, /* not */ \
  504. /* Positive single-char repeats ** These are */ \
  505. 2, 2, 2, 2, 2, 2, /* *, *?, +, +?, ?, ?? ** minima in */ \
  506. 4, 4, 4, /* upto, minupto, exact ** UTF-8 mode */ \
  507. /* Negative single-char repeats - only for chars < 256 */ \
  508. 2, 2, 2, 2, 2, 2, /* NOT *, *?, +, +?, ?, ?? */ \
  509. 4, 4, 4, /* NOT upto, minupto, exact */ \
  510. /* Positive type repeats */ \
  511. 2, 2, 2, 2, 2, 2, /* Type *, *?, +, +?, ?, ?? */ \
  512. 4, 4, 4, /* Type upto, minupto, exact */ \
  513. /* Character class & ref repeats */ \
  514. 1, 1, 1, 1, 1, 1, /* *, *?, +, +?, ?, ?? */ \
  515. 5, 5, /* CRRANGE, CRMINRANGE */ \
  516. 33, /* CLASS */ \
  517. 33, /* NCLASS */ \
  518. 0, /* XCLASS - variable length */ \
  519. 3, /* REF */ \
  520. 1+LINK_SIZE, /* RECURSE */ \
  521. 2+2*LINK_SIZE, /* CALLOUT */ \
  522. 1+LINK_SIZE, /* Alt */ \
  523. 1+LINK_SIZE, /* Ket */ \
  524. 1+LINK_SIZE, /* KetRmax */ \
  525. 1+LINK_SIZE, /* KetRmin */ \
  526. 1+LINK_SIZE, /* Assert */ \
  527. 1+LINK_SIZE, /* Assert not */ \
  528. 1+LINK_SIZE, /* Assert behind */ \
  529. 1+LINK_SIZE, /* Assert behind not */ \
  530. 1+LINK_SIZE, /* Reverse */ \
  531. 1+LINK_SIZE, /* Once */ \
  532. 1+LINK_SIZE, /* COND */ \
  533. 3, /* CREF */ \
  534. 1, 1, /* BRAZERO, BRAMINZERO */ \
  535. 3, /* BRANUMBER */ \
  536. 1+LINK_SIZE /* BRA */ \
  537. /* A magic value for OP_CREF to indicate the "in recursion" condition. */
  538. #define CREF_RECURSE 0xffff
  539. /* Error code numbers. They are given names so that they can more easily be
  540. tracked. */
  541. enum { ERR0, ERR1, ERR2, ERR3, ERR4, ERR5, ERR6, ERR7, ERR8, ERR9,
  542. ERR10, ERR11, ERR12, ERR13, ERR14, ERR15, ERR16, ERR17, ERR18, ERR19,
  543. ERR20, ERR21, ERR22, ERR23, ERR24, ERR25, ERR26, ERR27, ERR28, ERR29,
  544. ERR30, ERR31, ERR32, ERR33, ERR34, ERR35, ERR36, ERR37, ERR38, ERR39,
  545. ERR40, ERR41, ERR42, ERR43, ERR44, ERR45, ERR46, ERR47 };
  546. /* The real format of the start of the pcre block; the index of names and the
  547. code vector run on as long as necessary after the end. We store an explicit
  548. offset to the name table so that if a regex is compiled on one host, saved, and
  549. then run on another where the size of pointers is different, all might still
  550. be well. For the case of compiled-on-4 and run-on-8, we include an extra
  551. pointer that is always NULL. For future-proofing, a few dummy fields were
  552. originally included - even though you can never get this planning right - but
  553. there is only one left now.
  554. NOTE NOTE NOTE:
  555. Because people can now save and re-use compiled patterns, any additions to this
  556. structure should be made at the end, and something earlier (e.g. a new
  557. flag in the options or one of the dummy fields) should indicate that the new
  558. fields are present. Currently PCRE always sets the dummy fields to zero.
  559. NOTE NOTE NOTE:
  560. */
  561. typedef struct real_pcre {
  562. pcre_uint32 magic_number;
  563. pcre_uint32 size; /* Total that was malloced */
  564. pcre_uint32 options;
  565. pcre_uint32 dummy1; /* For future use, maybe */
  566. pcre_uint16 top_bracket;
  567. pcre_uint16 top_backref;
  568. pcre_uint16 first_byte;
  569. pcre_uint16 req_byte;
  570. pcre_uint16 name_table_offset; /* Offset to name table that follows */
  571. pcre_uint16 name_entry_size; /* Size of any name items */
  572. pcre_uint16 name_count; /* Number of name items */
  573. pcre_uint16 ref_count; /* Reference count */
  574. const unsigned char *tables; /* Pointer to tables or NULL for std */
  575. const unsigned char *nullpad; /* NULL padding */
  576. } real_pcre;
  577. /* The format of the block used to store data from pcre_study(). The same
  578. remark (see NOTE above) about extending this structure applies. */
  579. typedef struct pcre_study_data {
  580. pcre_uint32 size; /* Total that was malloced */
  581. pcre_uint32 options;
  582. uschar start_bits[32];
  583. } pcre_study_data;
  584. /* Structure for passing "static" information around between the functions
  585. doing the compiling, so that they are thread-safe. */
  586. typedef struct compile_data {
  587. const uschar *lcc; /* Points to lower casing table */
  588. const uschar *fcc; /* Points to case-flipping table */
  589. const uschar *cbits; /* Points to character type table */
  590. const uschar *ctypes; /* Points to table of type maps */
  591. const uschar *start_code; /* The start of the compiled code */
  592. const uschar *start_pattern; /* The start of the pattern */
  593. uschar *name_table; /* The name/number table */
  594. int names_found; /* Number of entries so far */
  595. int name_entry_size; /* Size of each entry */
  596. int top_backref; /* Maximum back reference */
  597. unsigned int backref_map; /* Bitmap of low back refs */
  598. int req_varyopt; /* "After variable item" flag for reqbyte */
  599. BOOL nopartial; /* Set TRUE if partial won't work */
  600. } compile_data;
  601. /* Structure for maintaining a chain of pointers to the currently incomplete
  602. branches, for testing for left recursion. */
  603. typedef struct branch_chain {
  604. struct branch_chain *outer;
  605. uschar *current;
  606. } branch_chain;
  607. /* Structure for items in a linked list that represents an explicit recursive
  608. call within the pattern. */
  609. typedef struct recursion_info {
  610. struct recursion_info *prevrec; /* Previous recursion record (or NULL) */
  611. int group_num; /* Number of group that was called */
  612. const uschar *after_call; /* "Return value": points after the call in the expr */
  613. const uschar *save_start; /* Old value of md->start_match */
  614. int *offset_save; /* Pointer to start of saved offsets */
  615. int saved_max; /* Number of saved offsets */
  616. } recursion_info;
  617. /* When compiling in a mode that doesn't use recursive calls to match(),
  618. a structure is used to remember local variables on the heap. It is defined in
  619. pcre.c, close to the match() function, so that it is easy to keep it in step
  620. with any changes of local variable. However, the pointer to the current frame
  621. must be saved in some "static" place over a longjmp(). We declare the
  622. structure here so that we can put a pointer in the match_data structure.
  623. NOTE: This isn't used for a "normal" compilation of pcre. */
  624. struct heapframe;
  625. /* Structure for passing "static" information around between the functions
  626. doing traditional NFA matching, so that they are thread-safe. */
  627. typedef struct match_data {
  628. unsigned long int match_call_count; /* As it says */
  629. unsigned long int match_limit;/* As it says */
  630. int *offset_vector; /* Offset vector */
  631. int offset_end; /* One past the end */
  632. int offset_max; /* The maximum usable for return data */
  633. const uschar *lcc; /* Points to lower casing table */
  634. const uschar *ctypes; /* Points to table of type maps */
  635. BOOL offset_overflow; /* Set if too many extractions */
  636. BOOL notbol; /* NOTBOL flag */
  637. BOOL noteol; /* NOTEOL flag */
  638. BOOL utf8; /* UTF8 flag */
  639. BOOL endonly; /* Dollar not before final \n */
  640. BOOL notempty; /* Empty string match not wanted */
  641. BOOL partial; /* PARTIAL flag */
  642. BOOL hitend; /* Hit the end of the subject at some point */
  643. const uschar *start_code; /* For use when recursing */
  644. const uschar *start_subject; /* Start of the subject string */
  645. const uschar *end_subject; /* End of the subject string */
  646. const uschar *start_match; /* Start of this match attempt */
  647. const uschar *end_match_ptr; /* Subject position at end match */
  648. int end_offset_top; /* Highwater mark at end of match */
  649. int capture_last; /* Most recent capture number */
  650. int start_offset; /* The start offset value */
  651. recursion_info *recursive; /* Linked list of recursion data */
  652. void *callout_data; /* To pass back to callouts */
  653. struct heapframe *thisframe; /* Used only when compiling for no recursion */
  654. } match_data;
  655. /* A similar structure is used for the same purpose by the DFA matching
  656. functions. */
  657. typedef struct dfa_match_data {
  658. const uschar *start_code; /* Start of the compiled pattern */
  659. const uschar *start_subject; /* Start of the subject string */
  660. const uschar *end_subject; /* End of subject string */
  661. const uschar *tables; /* Character tables */
  662. int moptions; /* Match options */
  663. int poptions; /* Pattern options */
  664. void *callout_data; /* To pass back to callouts */
  665. } dfa_match_data;
  666. /* Bit definitions for entries in the pcre_ctypes table. */
  667. #define ctype_space 0x01
  668. #define ctype_letter 0x02
  669. #define ctype_digit 0x04
  670. #define ctype_xdigit 0x08
  671. #define ctype_word 0x10 /* alphameric or '_' */
  672. #define ctype_meta 0x80 /* regexp meta char or zero (end pattern) */
  673. /* Offsets for the bitmap tables in pcre_cbits. Each table contains a set
  674. of bits for a class map. Some classes are built by combining these tables. */
  675. #define cbit_space 0 /* [:space:] or \s */
  676. #define cbit_xdigit 32 /* [:xdigit:] */
  677. #define cbit_digit 64 /* [:digit:] or \d */
  678. #define cbit_upper 96 /* [:upper:] */
  679. #define cbit_lower 128 /* [:lower:] */
  680. #define cbit_word 160 /* [:word:] or \w */
  681. #define cbit_graph 192 /* [:graph:] */
  682. #define cbit_print 224 /* [:print:] */
  683. #define cbit_punct 256 /* [:punct:] */
  684. #define cbit_cntrl 288 /* [:cntrl:] */
  685. #define cbit_length 320 /* Length of the cbits table */
  686. /* Offsets of the various tables from the base tables pointer, and
  687. total length. */
  688. #define lcc_offset 0
  689. #define fcc_offset 256
  690. #define cbits_offset 512
  691. #define ctypes_offset (cbits_offset + cbit_length)
  692. #define tables_length (ctypes_offset + 256)
  693. /* Layout of the UCP type table that translates property names into codes for
  694. ucp_findchar(). */
  695. typedef struct {
  696. const char *name;
  697. int value;
  698. } ucp_type_table;
  699. /* Internal shared data tables. These are tables that are used by more than one
  700. of the exported public functions. They have to be "external" in the C sense,
  701. but are not part of the PCRE public API. The data for these tables is in the
  702. pcretabs.c module. */
  703. extern const int _pcre_utf8_table1[];
  704. extern const int _pcre_utf8_table2[];
  705. extern const int _pcre_utf8_table3[];
  706. extern const uschar _pcre_utf8_table4[];
  707. extern const int _pcre_utf8_table1_size;
  708. extern const ucp_type_table _pcre_utt[];
  709. extern const int _pcre_utt_size;
  710. extern const uschar _pcre_default_tables[];
  711. extern const uschar _pcre_OP_lengths[];
  712. /* Internal shared functions. These are functions that are used by more than
  713. one of the exported public functions. They have to be "external" in the C
  714. sense, but are not part of the PCRE public API. */
  715. extern int _pcre_ord2utf8(int, uschar *);
  716. extern void _pcre_printint(pcre *, FILE *);
  717. extern real_pcre * _pcre_try_flipped(const real_pcre *, real_pcre *,
  718. const pcre_study_data *, pcre_study_data *);
  719. extern int _pcre_ucp_findchar(const int, int *, int *);
  720. extern int _pcre_valid_utf8(const uschar *, int);
  721. extern BOOL _pcre_xclass(int, const uschar *);
  722. /* End of pcreinal.h */