PageRenderTime 58ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/harbour-3.0.0/src/3rd/pcre/pcrecomp.c

#
C | 2019 lines | 1293 code | 270 blank | 456 comment | 413 complexity | bef5b5cfa890b2a910e1f8ccad45c8a2 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

Large files files are truncated, but you can click here to view the full file

  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-2010 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 module contains the external function pcre_compile(), along with
  33. supporting internal functions that are not used by other modules. */
  34. #ifdef HAVE_CONFIG_H
  35. #include "config.h"
  36. #endif
  37. #define NLBLOCK cd /* Block containing newline information */
  38. #define PSSTART start_pattern /* Field containing processed string start */
  39. #define PSEND end_pattern /* Field containing processed string end */
  40. #include "pcreinal.h"
  41. /* When PCRE_DEBUG is defined, we need the pcre_printint() function, which is
  42. also used by pcretest. PCRE_DEBUG is not defined when building a production
  43. library. */
  44. #ifdef PCRE_DEBUG
  45. #include "pcreprni.h"
  46. #endif
  47. /* Macro for setting individual bits in class bitmaps. */
  48. #define SETBIT(a,b) a[b/8] |= (1 << (b%8))
  49. /* Maximum length value to check against when making sure that the integer that
  50. holds the compiled pattern length does not overflow. We make it a bit less than
  51. INT_MAX to allow for adding in group terminating bytes, so that we don't have
  52. to check them every time. */
  53. #define OFLOW_MAX (INT_MAX - 20)
  54. /*************************************************
  55. * Code parameters and static tables *
  56. *************************************************/
  57. /* This value specifies the size of stack workspace that is used during the
  58. first pre-compile phase that determines how much memory is required. The regex
  59. is partly compiled into this space, but the compiled parts are discarded as
  60. soon as they can be, so that hopefully there will never be an overrun. The code
  61. does, however, check for an overrun. The largest amount I've seen used is 218,
  62. so this number is very generous.
  63. The same workspace is used during the second, actual compile phase for
  64. remembering forward references to groups so that they can be filled in at the
  65. end. Each entry in this list occupies LINK_SIZE bytes, so even when LINK_SIZE
  66. is 4 there is plenty of room. */
  67. #define COMPILE_WORK_SIZE (4096)
  68. /* The overrun tests check for a slightly smaller size so that they detect the
  69. overrun before it actually does run off the end of the data block. */
  70. #define WORK_SIZE_CHECK (COMPILE_WORK_SIZE - 100)
  71. /* Table for handling escaped characters in the range '0'-'z'. Positive returns
  72. are simple data values; negative values are for special things like \d and so
  73. on. Zero means further processing is needed (for things like \x), or the escape
  74. is invalid. */
  75. #ifndef EBCDIC
  76. /* This is the "normal" table for ASCII systems or for EBCDIC systems running
  77. in UTF-8 mode. */
  78. static const short int escapes[] = {
  79. 0, 0,
  80. 0, 0,
  81. 0, 0,
  82. 0, 0,
  83. 0, 0,
  84. CHAR_COLON, CHAR_SEMICOLON,
  85. CHAR_LESS_THAN_SIGN, CHAR_EQUALS_SIGN,
  86. CHAR_GREATER_THAN_SIGN, CHAR_QUESTION_MARK,
  87. CHAR_COMMERCIAL_AT, -ESC_A,
  88. -ESC_B, -ESC_C,
  89. -ESC_D, -ESC_E,
  90. 0, -ESC_G,
  91. -ESC_H, 0,
  92. 0, -ESC_K,
  93. 0, 0,
  94. -ESC_N, 0,
  95. -ESC_P, -ESC_Q,
  96. -ESC_R, -ESC_S,
  97. 0, 0,
  98. -ESC_V, -ESC_W,
  99. -ESC_X, 0,
  100. -ESC_Z, CHAR_LEFT_SQUARE_BRACKET,
  101. CHAR_BACKSLASH, CHAR_RIGHT_SQUARE_BRACKET,
  102. CHAR_CIRCUMFLEX_ACCENT, CHAR_UNDERSCORE,
  103. CHAR_GRAVE_ACCENT, 7,
  104. -ESC_b, 0,
  105. -ESC_d, ESC_e,
  106. ESC_f, 0,
  107. -ESC_h, 0,
  108. 0, -ESC_k,
  109. 0, 0,
  110. ESC_n, 0,
  111. -ESC_p, 0,
  112. ESC_r, -ESC_s,
  113. ESC_tee, 0,
  114. -ESC_v, -ESC_w,
  115. 0, 0,
  116. -ESC_z
  117. };
  118. #else
  119. /* This is the "abnormal" table for EBCDIC systems without UTF-8 support. */
  120. static const short int escapes[] = {
  121. /* 48 */ 0, 0, 0, '.', '<', '(', '+', '|',
  122. /* 50 */ '&', 0, 0, 0, 0, 0, 0, 0,
  123. /* 58 */ 0, 0, '!', '$', '*', ')', ';', '~',
  124. /* 60 */ '-', '/', 0, 0, 0, 0, 0, 0,
  125. /* 68 */ 0, 0, '|', ',', '%', '_', '>', '?',
  126. /* 70 */ 0, 0, 0, 0, 0, 0, 0, 0,
  127. /* 78 */ 0, '`', ':', '#', '@', '\'', '=', '"',
  128. /* 80 */ 0, 7, -ESC_b, 0, -ESC_d, ESC_e, ESC_f, 0,
  129. /* 88 */-ESC_h, 0, 0, '{', 0, 0, 0, 0,
  130. /* 90 */ 0, 0, -ESC_k, 'l', 0, ESC_n, 0, -ESC_p,
  131. /* 98 */ 0, ESC_r, 0, '}', 0, 0, 0, 0,
  132. /* A0 */ 0, '~', -ESC_s, ESC_tee, 0,-ESC_v, -ESC_w, 0,
  133. /* A8 */ 0,-ESC_z, 0, 0, 0, '[', 0, 0,
  134. /* B0 */ 0, 0, 0, 0, 0, 0, 0, 0,
  135. /* B8 */ 0, 0, 0, 0, 0, ']', '=', '-',
  136. /* C0 */ '{',-ESC_A, -ESC_B, -ESC_C, -ESC_D,-ESC_E, 0, -ESC_G,
  137. /* C8 */-ESC_H, 0, 0, 0, 0, 0, 0, 0,
  138. /* D0 */ '}', 0, -ESC_K, 0, 0,-ESC_N, 0, -ESC_P,
  139. /* D8 */-ESC_Q,-ESC_R, 0, 0, 0, 0, 0, 0,
  140. /* E0 */ '\\', 0, -ESC_S, 0, 0,-ESC_V, -ESC_W, -ESC_X,
  141. /* E8 */ 0,-ESC_Z, 0, 0, 0, 0, 0, 0,
  142. /* F0 */ 0, 0, 0, 0, 0, 0, 0, 0,
  143. /* F8 */ 0, 0, 0, 0, 0, 0, 0, 0
  144. };
  145. #endif
  146. /* Table of special "verbs" like (*PRUNE). This is a short table, so it is
  147. searched linearly. Put all the names into a single string, in order to reduce
  148. the number of relocations when a shared library is dynamically linked. The
  149. string is built from string macros so that it works in UTF-8 mode on EBCDIC
  150. platforms. */
  151. typedef struct verbitem {
  152. int len; /* Length of verb name */
  153. int op; /* Op when no arg, or -1 if arg mandatory */
  154. int op_arg; /* Op when arg present, or -1 if not allowed */
  155. } verbitem;
  156. static const char verbnames[] =
  157. "\0" /* Empty name is a shorthand for MARK */
  158. STRING_MARK0
  159. STRING_ACCEPT0
  160. STRING_COMMIT0
  161. STRING_F0
  162. STRING_FAIL0
  163. STRING_PRUNE0
  164. STRING_SKIP0
  165. STRING_THEN;
  166. static const verbitem verbs[] = {
  167. { 0, -1, OP_MARK },
  168. { 4, -1, OP_MARK },
  169. { 6, OP_ACCEPT, -1 },
  170. { 6, OP_COMMIT, -1 },
  171. { 1, OP_FAIL, -1 },
  172. { 4, OP_FAIL, -1 },
  173. { 5, OP_PRUNE, OP_PRUNE_ARG },
  174. { 4, OP_SKIP, OP_SKIP_ARG },
  175. { 4, OP_THEN, OP_THEN_ARG }
  176. };
  177. static const int verbcount = sizeof(verbs)/sizeof(verbitem);
  178. /* Tables of names of POSIX character classes and their lengths. The names are
  179. now all in a single string, to reduce the number of relocations when a shared
  180. library is dynamically loaded. The list of lengths is terminated by a zero
  181. length entry. The first three must be alpha, lower, upper, as this is assumed
  182. for handling case independence. */
  183. static const char posix_names[] =
  184. STRING_alpha0 STRING_lower0 STRING_upper0 STRING_alnum0
  185. STRING_ascii0 STRING_blank0 STRING_cntrl0 STRING_digit0
  186. STRING_graph0 STRING_print0 STRING_punct0 STRING_space0
  187. STRING_word0 STRING_xdigit;
  188. static const uschar posix_name_lengths[] = {
  189. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 6, 0 };
  190. /* Table of class bit maps for each POSIX class. Each class is formed from a
  191. base map, with an optional addition or removal of another map. Then, for some
  192. classes, there is some additional tweaking: for [:blank:] the vertical space
  193. characters are removed, and for [:alpha:] and [:alnum:] the underscore
  194. character is removed. The triples in the table consist of the base map offset,
  195. second map offset or -1 if no second map, and a non-negative value for map
  196. addition or a negative value for map subtraction (if there are two maps). The
  197. absolute value of the third field has these meanings: 0 => no tweaking, 1 =>
  198. remove vertical space characters, 2 => remove underscore. */
  199. static const int posix_class_maps[] = {
  200. cbit_word, cbit_digit, -2, /* alpha */
  201. cbit_lower, -1, 0, /* lower */
  202. cbit_upper, -1, 0, /* upper */
  203. cbit_word, -1, 2, /* alnum - word without underscore */
  204. cbit_print, cbit_cntrl, 0, /* ascii */
  205. cbit_space, -1, 1, /* blank - a GNU extension */
  206. cbit_cntrl, -1, 0, /* cntrl */
  207. cbit_digit, -1, 0, /* digit */
  208. cbit_graph, -1, 0, /* graph */
  209. cbit_print, -1, 0, /* print */
  210. cbit_punct, -1, 0, /* punct */
  211. cbit_space, -1, 0, /* space */
  212. cbit_word, -1, 0, /* word - a Perl extension */
  213. cbit_xdigit,-1, 0 /* xdigit */
  214. };
  215. /* Table of substitutes for \d etc when PCRE_UCP is set. The POSIX class
  216. substitutes must be in the order of the names, defined above, and there are
  217. both positive and negative cases. NULL means no substitute. */
  218. #ifdef SUPPORT_UCP
  219. static const uschar *substitutes[] = {
  220. (uschar *)"\\P{Nd}", /* \D */
  221. (uschar *)"\\p{Nd}", /* \d */
  222. (uschar *)"\\P{Xsp}", /* \S */ /* NOTE: Xsp is Perl space */
  223. (uschar *)"\\p{Xsp}", /* \s */
  224. (uschar *)"\\P{Xwd}", /* \W */
  225. (uschar *)"\\p{Xwd}" /* \w */
  226. };
  227. static const uschar *posix_substitutes[] = {
  228. (uschar *)"\\p{L}", /* alpha */
  229. (uschar *)"\\p{Ll}", /* lower */
  230. (uschar *)"\\p{Lu}", /* upper */
  231. (uschar *)"\\p{Xan}", /* alnum */
  232. NULL, /* ascii */
  233. (uschar *)"\\h", /* blank */
  234. NULL, /* cntrl */
  235. (uschar *)"\\p{Nd}", /* digit */
  236. NULL, /* graph */
  237. NULL, /* print */
  238. NULL, /* punct */
  239. (uschar *)"\\p{Xps}", /* space */ /* NOTE: Xps is POSIX space */
  240. (uschar *)"\\p{Xwd}", /* word */
  241. NULL, /* xdigit */
  242. /* Negated cases */
  243. (uschar *)"\\P{L}", /* ^alpha */
  244. (uschar *)"\\P{Ll}", /* ^lower */
  245. (uschar *)"\\P{Lu}", /* ^upper */
  246. (uschar *)"\\P{Xan}", /* ^alnum */
  247. NULL, /* ^ascii */
  248. (uschar *)"\\H", /* ^blank */
  249. NULL, /* ^cntrl */
  250. (uschar *)"\\P{Nd}", /* ^digit */
  251. NULL, /* ^graph */
  252. NULL, /* ^print */
  253. NULL, /* ^punct */
  254. (uschar *)"\\P{Xps}", /* ^space */ /* NOTE: Xps is POSIX space */
  255. (uschar *)"\\P{Xwd}", /* ^word */
  256. NULL /* ^xdigit */
  257. };
  258. #define POSIX_SUBSIZE (sizeof(posix_substitutes)/sizeof(uschar *))
  259. #endif
  260. #define STRING(a) # a
  261. #define XSTRING(s) STRING(s)
  262. /* The texts of compile-time error messages. These are "char *" because they
  263. are passed to the outside world. Do not ever re-use any error number, because
  264. they are documented. Always add a new error instead. Messages marked DEAD below
  265. are no longer used. This used to be a table of strings, but in order to reduce
  266. the number of relocations needed when a shared library is loaded dynamically,
  267. it is now one long string. We cannot use a table of offsets, because the
  268. lengths of inserts such as XSTRING(MAX_NAME_SIZE) are not known. Instead, we
  269. simply count through to the one we want - this isn't a performance issue
  270. because these strings are used only when there is a compilation error.
  271. Each substring ends with \0 to insert a null character. This includes the final
  272. substring, so that the whole string ends with \0\0, which can be detected when
  273. counting through. */
  274. static const char error_texts[] =
  275. "no error\0"
  276. "\\ at end of pattern\0"
  277. "\\c at end of pattern\0"
  278. "unrecognized character follows \\\0"
  279. "numbers out of order in {} quantifier\0"
  280. /* 5 */
  281. "number too big in {} quantifier\0"
  282. "missing terminating ] for character class\0"
  283. "invalid escape sequence in character class\0"
  284. "range out of order in character class\0"
  285. "nothing to repeat\0"
  286. /* 10 */
  287. "operand of unlimited repeat could match the empty string\0" /** DEAD **/
  288. "internal error: unexpected repeat\0"
  289. "unrecognized character after (? or (?-\0"
  290. "POSIX named classes are supported only within a class\0"
  291. "missing )\0"
  292. /* 15 */
  293. "reference to non-existent subpattern\0"
  294. "erroffset passed as NULL\0"
  295. "unknown option bit(s) set\0"
  296. "missing ) after comment\0"
  297. "parentheses nested too deeply\0" /** DEAD **/
  298. /* 20 */
  299. "regular expression is too large\0"
  300. "failed to get memory\0"
  301. "unmatched parentheses\0"
  302. "internal error: code overflow\0"
  303. "unrecognized character after (?<\0"
  304. /* 25 */
  305. "lookbehind assertion is not fixed length\0"
  306. "malformed number or name after (?(\0"
  307. "conditional group contains more than two branches\0"
  308. "assertion expected after (?(\0"
  309. "(?R or (?[+-]digits must be followed by )\0"
  310. /* 30 */
  311. "unknown POSIX class name\0"
  312. "POSIX collating elements are not supported\0"
  313. "this version of PCRE is not compiled with PCRE_UTF8 support\0"
  314. "spare error\0" /** DEAD **/
  315. "character value in \\x{...} sequence is too large\0"
  316. /* 35 */
  317. "invalid condition (?(0)\0"
  318. "\\C not allowed in lookbehind assertion\0"
  319. "PCRE does not support \\L, \\l, \\N{name}, \\U, or \\u\0"
  320. "number after (?C is > 255\0"
  321. "closing ) for (?C expected\0"
  322. /* 40 */
  323. "recursive call could loop indefinitely\0"
  324. "unrecognized character after (?P\0"
  325. "syntax error in subpattern name (missing terminator)\0"
  326. "two named subpatterns have the same name\0"
  327. "invalid UTF-8 string\0"
  328. /* 45 */
  329. "support for \\P, \\p, and \\X has not been compiled\0"
  330. "malformed \\P or \\p sequence\0"
  331. "unknown property name after \\P or \\p\0"
  332. "subpattern name is too long (maximum " XSTRING(MAX_NAME_SIZE) " characters)\0"
  333. "too many named subpatterns (maximum " XSTRING(MAX_NAME_COUNT) ")\0"
  334. /* 50 */
  335. "repeated subpattern is too long\0" /** DEAD **/
  336. "octal value is greater than \\377 (not in UTF-8 mode)\0"
  337. "internal error: overran compiling workspace\0"
  338. "internal error: previously-checked referenced subpattern not found\0"
  339. "DEFINE group contains more than one branch\0"
  340. /* 55 */
  341. "repeating a DEFINE group is not allowed\0"
  342. "inconsistent NEWLINE options\0"
  343. "\\g is not followed by a braced, angle-bracketed, or quoted name/number or by a plain number\0"
  344. "a numbered reference must not be zero\0"
  345. "an argument is not allowed for (*ACCEPT), (*FAIL), or (*COMMIT)\0"
  346. /* 60 */
  347. "(*VERB) not recognized\0"
  348. "number is too big\0"
  349. "subpattern name expected\0"
  350. "digit expected after (?+\0"
  351. "] is an invalid data character in JavaScript compatibility mode\0"
  352. /* 65 */
  353. "different names for subpatterns of the same number are not allowed\0"
  354. "(*MARK) must have an argument\0"
  355. "this version of PCRE is not compiled with PCRE_UCP support\0"
  356. "\\c must be followed by an ASCII character\0"
  357. ;
  358. /* Table to identify digits and hex digits. This is used when compiling
  359. patterns. Note that the tables in chartables are dependent on the locale, and
  360. may mark arbitrary characters as digits - but the PCRE compiling code expects
  361. to handle only 0-9, a-z, and A-Z as digits when compiling. That is why we have
  362. a private table here. It costs 256 bytes, but it is a lot faster than doing
  363. character value tests (at least in some simple cases I timed), and in some
  364. applications one wants PCRE to compile efficiently as well as match
  365. efficiently.
  366. For convenience, we use the same bit definitions as in chartables:
  367. 0x04 decimal digit
  368. 0x08 hexadecimal digit
  369. Then we can use ctype_digit and ctype_xdigit in the code. */
  370. #ifndef EBCDIC
  371. /* This is the "normal" case, for ASCII systems, and EBCDIC systems running in
  372. UTF-8 mode. */
  373. static const unsigned char digitab[] =
  374. {
  375. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 0- 7 */
  376. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 8- 15 */
  377. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 16- 23 */
  378. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 24- 31 */
  379. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* - ' */
  380. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* ( - / */
  381. 0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c, /* 0 - 7 */
  382. 0x0c,0x0c,0x00,0x00,0x00,0x00,0x00,0x00, /* 8 - ? */
  383. 0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x00, /* @ - G */
  384. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* H - O */
  385. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* P - W */
  386. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* X - _ */
  387. 0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x00, /* ` - g */
  388. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* h - o */
  389. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* p - w */
  390. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* x -127 */
  391. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 128-135 */
  392. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 136-143 */
  393. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 144-151 */
  394. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 152-159 */
  395. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 160-167 */
  396. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 168-175 */
  397. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 176-183 */
  398. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 184-191 */
  399. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 192-199 */
  400. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 200-207 */
  401. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 208-215 */
  402. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 216-223 */
  403. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 224-231 */
  404. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 232-239 */
  405. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 240-247 */
  406. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};/* 248-255 */
  407. #else
  408. /* This is the "abnormal" case, for EBCDIC systems not running in UTF-8 mode. */
  409. static const unsigned char digitab[] =
  410. {
  411. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 0- 7 0 */
  412. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 8- 15 */
  413. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 16- 23 10 */
  414. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 24- 31 */
  415. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 32- 39 20 */
  416. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 40- 47 */
  417. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 48- 55 30 */
  418. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 56- 63 */
  419. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* - 71 40 */
  420. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 72- | */
  421. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* & - 87 50 */
  422. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 88- 95 */
  423. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* - -103 60 */
  424. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 104- ? */
  425. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 112-119 70 */
  426. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 120- " */
  427. 0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x00, /* 128- g 80 */
  428. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* h -143 */
  429. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 144- p 90 */
  430. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* q -159 */
  431. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 160- x A0 */
  432. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* y -175 */
  433. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* ^ -183 B0 */
  434. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 184-191 */
  435. 0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x00, /* { - G C0 */
  436. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* H -207 */
  437. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* } - P D0 */
  438. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* Q -223 */
  439. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* \ - X E0 */
  440. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* Y -239 */
  441. 0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c, /* 0 - 7 F0 */
  442. 0x0c,0x0c,0x00,0x00,0x00,0x00,0x00,0x00};/* 8 -255 */
  443. static const unsigned char ebcdic_chartab[] = { /* chartable partial dup */
  444. 0x80,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /* 0- 7 */
  445. 0x00,0x00,0x00,0x00,0x01,0x01,0x00,0x00, /* 8- 15 */
  446. 0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /* 16- 23 */
  447. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 24- 31 */
  448. 0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /* 32- 39 */
  449. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 40- 47 */
  450. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 48- 55 */
  451. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 56- 63 */
  452. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* - 71 */
  453. 0x00,0x00,0x00,0x80,0x00,0x80,0x80,0x80, /* 72- | */
  454. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* & - 87 */
  455. 0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00, /* 88- 95 */
  456. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* - -103 */
  457. 0x00,0x00,0x00,0x00,0x00,0x10,0x00,0x80, /* 104- ? */
  458. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 112-119 */
  459. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 120- " */
  460. 0x00,0x1a,0x1a,0x1a,0x1a,0x1a,0x1a,0x12, /* 128- g */
  461. 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* h -143 */
  462. 0x00,0x12,0x12,0x12,0x12,0x12,0x12,0x12, /* 144- p */
  463. 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* q -159 */
  464. 0x00,0x00,0x12,0x12,0x12,0x12,0x12,0x12, /* 160- x */
  465. 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* y -175 */
  466. 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* ^ -183 */
  467. 0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x00, /* 184-191 */
  468. 0x80,0x1a,0x1a,0x1a,0x1a,0x1a,0x1a,0x12, /* { - G */
  469. 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* H -207 */
  470. 0x00,0x12,0x12,0x12,0x12,0x12,0x12,0x12, /* } - P */
  471. 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* Q -223 */
  472. 0x00,0x00,0x12,0x12,0x12,0x12,0x12,0x12, /* \ - X */
  473. 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* Y -239 */
  474. 0x1c,0x1c,0x1c,0x1c,0x1c,0x1c,0x1c,0x1c, /* 0 - 7 */
  475. 0x1c,0x1c,0x00,0x00,0x00,0x00,0x00,0x00};/* 8 -255 */
  476. #endif
  477. /* Definition to allow mutual recursion */
  478. static BOOL
  479. compile_regex(int, int, uschar **, const uschar **, int *, BOOL, BOOL, int,
  480. int *, int *, branch_chain *, compile_data *, int *);
  481. /*************************************************
  482. * Find an error text *
  483. *************************************************/
  484. /* The error texts are now all in one long string, to save on relocations. As
  485. some of the text is of unknown length, we can't use a table of offsets.
  486. Instead, just count through the strings. This is not a performance issue
  487. because it happens only when there has been a compilation error.
  488. Argument: the error number
  489. Returns: pointer to the error string
  490. */
  491. static const char *
  492. find_error_text(int n)
  493. {
  494. const char *s = error_texts;
  495. for (; n > 0; n--)
  496. {
  497. while (*s++ != 0) {};
  498. if (*s == 0) return "Error text not found (please report)";
  499. }
  500. return s;
  501. }
  502. /*************************************************
  503. * Handle escapes *
  504. *************************************************/
  505. /* This function is called when a \ has been encountered. It either returns a
  506. positive value for a simple escape such as \n, or a negative value which
  507. encodes one of the more complicated things such as \d. A backreference to group
  508. n is returned as -(ESC_REF + n); ESC_REF is the highest ESC_xxx macro. When
  509. UTF-8 is enabled, a positive value greater than 255 may be returned. On entry,
  510. ptr is pointing at the \. On exit, it is on the final character of the escape
  511. sequence.
  512. Arguments:
  513. ptrptr points to the pattern position pointer
  514. errorcodeptr points to the errorcode variable
  515. bracount number of previous extracting brackets
  516. options the options bits
  517. isclass TRUE if inside a character class
  518. Returns: zero or positive => a data character
  519. negative => a special escape sequence
  520. on error, errorcodeptr is set
  521. */
  522. static int
  523. check_escape(const uschar **ptrptr, int *errorcodeptr, int bracount,
  524. int options, BOOL isclass)
  525. {
  526. BOOL utf8 = (options & PCRE_UTF8) != 0;
  527. const uschar *ptr = *ptrptr + 1;
  528. int c, i;
  529. GETCHARINCTEST(c, ptr); /* Get character value, increment pointer */
  530. ptr--; /* Set pointer back to the last byte */
  531. /* If backslash is at the end of the pattern, it's an error. */
  532. if (c == 0) *errorcodeptr = ERR1;
  533. /* Non-alphanumerics are literals. For digits or letters, do an initial lookup
  534. in a table. A non-zero result is something that can be returned immediately.
  535. Otherwise further processing may be required. */
  536. #ifndef EBCDIC /* ASCII/UTF-8 coding */
  537. else if (c < CHAR_0 || c > CHAR_z) {} /* Not alphanumeric */
  538. else if ((i = escapes[c - CHAR_0]) != 0) c = i;
  539. #else /* EBCDIC coding */
  540. else if (c < 'a' || (ebcdic_chartab[c] & 0x0E) == 0) {} /* Not alphanumeric */
  541. else if ((i = escapes[c - 0x48]) != 0) c = i;
  542. #endif
  543. /* Escapes that need further processing, or are illegal. */
  544. else
  545. {
  546. const uschar *oldptr;
  547. BOOL braced, negated;
  548. switch (c)
  549. {
  550. /* A number of Perl escapes are not handled by PCRE. We give an explicit
  551. error. */
  552. case CHAR_l:
  553. case CHAR_L:
  554. case CHAR_u:
  555. case CHAR_U:
  556. *errorcodeptr = ERR37;
  557. break;
  558. /* \g must be followed by one of a number of specific things:
  559. (1) A number, either plain or braced. If positive, it is an absolute
  560. backreference. If negative, it is a relative backreference. This is a Perl
  561. 5.10 feature.
  562. (2) Perl 5.10 also supports \g{name} as a reference to a named group. This
  563. is part of Perl's movement towards a unified syntax for back references. As
  564. this is synonymous with \k{name}, we fudge it up by pretending it really
  565. was \k.
  566. (3) For Oniguruma compatibility we also support \g followed by a name or a
  567. number either in angle brackets or in single quotes. However, these are
  568. (possibly recursive) subroutine calls, _not_ backreferences. Just return
  569. the -ESC_g code (cf \k). */
  570. case CHAR_g:
  571. if (ptr[1] == CHAR_LESS_THAN_SIGN || ptr[1] == CHAR_APOSTROPHE)
  572. {
  573. c = -ESC_g;
  574. break;
  575. }
  576. /* Handle the Perl-compatible cases */
  577. if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)
  578. {
  579. const uschar *p;
  580. for (p = ptr+2; *p != 0 && *p != CHAR_RIGHT_CURLY_BRACKET; p++)
  581. if (*p != CHAR_MINUS && (digitab[*p] & ctype_digit) == 0) break;
  582. if (*p != 0 && *p != CHAR_RIGHT_CURLY_BRACKET)
  583. {
  584. c = -ESC_k;
  585. break;
  586. }
  587. braced = TRUE;
  588. ptr++;
  589. }
  590. else braced = FALSE;
  591. if (ptr[1] == CHAR_MINUS)
  592. {
  593. negated = TRUE;
  594. ptr++;
  595. }
  596. else negated = FALSE;
  597. c = 0;
  598. while ((digitab[ptr[1]] & ctype_digit) != 0)
  599. c = c * 10 + *(++ptr) - CHAR_0;
  600. if (c < 0) /* Integer overflow */
  601. {
  602. *errorcodeptr = ERR61;
  603. break;
  604. }
  605. if (braced && *(++ptr) != CHAR_RIGHT_CURLY_BRACKET)
  606. {
  607. *errorcodeptr = ERR57;
  608. break;
  609. }
  610. if (c == 0)
  611. {
  612. *errorcodeptr = ERR58;
  613. break;
  614. }
  615. if (negated)
  616. {
  617. if (c > bracount)
  618. {
  619. *errorcodeptr = ERR15;
  620. break;
  621. }
  622. c = bracount - (c - 1);
  623. }
  624. c = -(ESC_REF + c);
  625. break;
  626. /* The handling of escape sequences consisting of a string of digits
  627. starting with one that is not zero is not straightforward. By experiment,
  628. the way Perl works seems to be as follows:
  629. Outside a character class, the digits are read as a decimal number. If the
  630. number is less than 10, or if there are that many previous extracting
  631. left brackets, then it is a back reference. Otherwise, up to three octal
  632. digits are read to form an escaped byte. Thus \123 is likely to be octal
  633. 123 (cf \0123, which is octal 012 followed by the literal 3). If the octal
  634. value is greater than 377, the least significant 8 bits are taken. Inside a
  635. character class, \ followed by a digit is always an octal number. */
  636. case CHAR_1: case CHAR_2: case CHAR_3: case CHAR_4: case CHAR_5:
  637. case CHAR_6: case CHAR_7: case CHAR_8: case CHAR_9:
  638. if (!isclass)
  639. {
  640. oldptr = ptr;
  641. c -= CHAR_0;
  642. while ((digitab[ptr[1]] & ctype_digit) != 0)
  643. c = c * 10 + *(++ptr) - CHAR_0;
  644. if (c < 0) /* Integer overflow */
  645. {
  646. *errorcodeptr = ERR61;
  647. break;
  648. }
  649. if (c < 10 || c <= bracount)
  650. {
  651. c = -(ESC_REF + c);
  652. break;
  653. }
  654. ptr = oldptr; /* Put the pointer back and fall through */
  655. }
  656. /* Handle an octal number following \. If the first digit is 8 or 9, Perl
  657. generates a binary zero byte and treats the digit as a following literal.
  658. Thus we have to pull back the pointer by one. */
  659. if ((c = *ptr) >= CHAR_8)
  660. {
  661. ptr--;
  662. c = 0;
  663. break;
  664. }
  665. /* \0 always starts an octal number, but we may drop through to here with a
  666. larger first octal digit. The original code used just to take the least
  667. significant 8 bits of octal numbers (I think this is what early Perls used
  668. to do). Nowadays we allow for larger numbers in UTF-8 mode, but no more
  669. than 3 octal digits. */
  670. case CHAR_0:
  671. c -= CHAR_0;
  672. while(i++ < 2 && ptr[1] >= CHAR_0 && ptr[1] <= CHAR_7)
  673. c = c * 8 + *(++ptr) - CHAR_0;
  674. if (!utf8 && c > 255) *errorcodeptr = ERR51;
  675. break;
  676. /* \x is complicated. \x{ddd} is a character number which can be greater
  677. than 0xff in utf8 mode, but only if the ddd are hex digits. If not, { is
  678. treated as a data character. */
  679. case CHAR_x:
  680. if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)
  681. {
  682. const uschar *pt = ptr + 2;
  683. int count = 0;
  684. c = 0;
  685. while ((digitab[*pt] & ctype_xdigit) != 0)
  686. {
  687. register int cc = *pt++;
  688. if (c == 0 && cc == CHAR_0) continue; /* Leading zeroes */
  689. count++;
  690. #ifndef EBCDIC /* ASCII/UTF-8 coding */
  691. if (cc >= CHAR_a) cc -= 32; /* Convert to upper case */
  692. c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));
  693. #else /* EBCDIC coding */
  694. if (cc >= CHAR_a && cc <= CHAR_z) cc += 64; /* Convert to upper case */
  695. c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
  696. #endif
  697. }
  698. if (*pt == CHAR_RIGHT_CURLY_BRACKET)
  699. {
  700. if (c < 0 || count > (utf8? 8 : 2)) *errorcodeptr = ERR34;
  701. ptr = pt;
  702. break;
  703. }
  704. /* If the sequence of hex digits does not end with '}', then we don't
  705. recognize this construct; fall through to the normal \x handling. */
  706. }
  707. /* Read just a single-byte hex-defined char */
  708. c = 0;
  709. while (i++ < 2 && (digitab[ptr[1]] & ctype_xdigit) != 0)
  710. {
  711. int cc; /* Some compilers don't like */
  712. cc = *(++ptr); /* ++ in initializers */
  713. #ifndef EBCDIC /* ASCII/UTF-8 coding */
  714. if (cc >= CHAR_a) cc -= 32; /* Convert to upper case */
  715. c = c * 16 + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));
  716. #else /* EBCDIC coding */
  717. if (cc <= CHAR_z) cc += 64; /* Convert to upper case */
  718. c = c * 16 + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
  719. #endif
  720. }
  721. break;
  722. /* For \c, a following letter is upper-cased; then the 0x40 bit is flipped.
  723. An error is given if the byte following \c is not an ASCII character. This
  724. coding is ASCII-specific, but then the whole concept of \cx is
  725. ASCII-specific. (However, an EBCDIC equivalent has now been added.) */
  726. case CHAR_c:
  727. c = *(++ptr);
  728. if (c == 0)
  729. {
  730. *errorcodeptr = ERR2;
  731. break;
  732. }
  733. #ifndef EBCDIC /* ASCII/UTF-8 coding */
  734. if (c > 127) /* Excludes all non-ASCII in either mode */
  735. {
  736. *errorcodeptr = ERR68;
  737. break;
  738. }
  739. if (c >= CHAR_a && c <= CHAR_z) c -= 32;
  740. c ^= 0x40;
  741. #else /* EBCDIC coding */
  742. if (c >= CHAR_a && c <= CHAR_z) c += 64;
  743. c ^= 0xC0;
  744. #endif
  745. break;
  746. /* PCRE_EXTRA enables extensions to Perl in the matter of escapes. Any
  747. other alphanumeric following \ is an error if PCRE_EXTRA was set;
  748. otherwise, for Perl compatibility, it is a literal. This code looks a bit
  749. odd, but there used to be some cases other than the default, and there may
  750. be again in future, so I haven't "optimized" it. */
  751. default:
  752. if ((options & PCRE_EXTRA) != 0) switch(c)
  753. {
  754. default:
  755. *errorcodeptr = ERR3;
  756. break;
  757. }
  758. break;
  759. }
  760. }
  761. /* Perl supports \N{name} for character names, as well as plain \N for "not
  762. newline". PCRE does not support \N{name}. */
  763. if (c == -ESC_N && ptr[1] == CHAR_LEFT_CURLY_BRACKET)
  764. *errorcodeptr = ERR37;
  765. /* If PCRE_UCP is set, we change the values for \d etc. */
  766. if ((options & PCRE_UCP) != 0 && c <= -ESC_D && c >= -ESC_w)
  767. c -= (ESC_DU - ESC_D);
  768. /* Set the pointer to the final character before returning. */
  769. *ptrptr = ptr;
  770. return c;
  771. }
  772. #ifdef SUPPORT_UCP
  773. /*************************************************
  774. * Handle \P and \p *
  775. *************************************************/
  776. /* This function is called after \P or \p has been encountered, provided that
  777. PCRE is compiled with support for Unicode properties. On entry, ptrptr is
  778. pointing at the P or p. On exit, it is pointing at the final character of the
  779. escape sequence.
  780. Argument:
  781. ptrptr points to the pattern position pointer
  782. negptr points to a boolean that is set TRUE for negation else FALSE
  783. dptr points to an int that is set to the detailed property value
  784. errorcodeptr points to the error code variable
  785. Returns: type value from ucp_type_table, or -1 for an invalid type
  786. */
  787. static int
  788. get_ucp(const uschar **ptrptr, BOOL *negptr, int *dptr, int *errorcodeptr)
  789. {
  790. int c, i, bot, top;
  791. const uschar *ptr = *ptrptr;
  792. char name[32];
  793. c = *(++ptr);
  794. if (c == 0) goto ERROR_RETURN;
  795. *negptr = FALSE;
  796. /* \P or \p can be followed by a name in {}, optionally preceded by ^ for
  797. negation. */
  798. if (c == CHAR_LEFT_CURLY_BRACKET)
  799. {
  800. if (ptr[1] == CHAR_CIRCUMFLEX_ACCENT)
  801. {
  802. *negptr = TRUE;
  803. ptr++;
  804. }
  805. for (i = 0; i < (int)sizeof(name) - 1; i++)
  806. {
  807. c = *(++ptr);
  808. if (c == 0) goto ERROR_RETURN;
  809. if (c == CHAR_RIGHT_CURLY_BRACKET) break;
  810. name[i] = c;
  811. }
  812. if (c != CHAR_RIGHT_CURLY_BRACKET) goto ERROR_RETURN;
  813. name[i] = 0;
  814. }
  815. /* Otherwise there is just one following character */
  816. else
  817. {
  818. name[0] = c;
  819. name[1] = 0;
  820. }
  821. *ptrptr = ptr;
  822. /* Search for a recognized property name using binary chop */
  823. bot = 0;
  824. top = _pcre_utt_size;
  825. while (bot < top)
  826. {
  827. i = (bot + top) >> 1;
  828. c = strcmp(name, _pcre_utt_names + _pcre_utt[i].name_offset);
  829. if (c == 0)
  830. {
  831. *dptr = _pcre_utt[i].value;
  832. return _pcre_utt[i].type;
  833. }
  834. if (c > 0) bot = i + 1; else top = i;
  835. }
  836. *errorcodeptr = ERR47;
  837. *ptrptr = ptr;
  838. return -1;
  839. ERROR_RETURN:
  840. *errorcodeptr = ERR46;
  841. *ptrptr = ptr;
  842. return -1;
  843. }
  844. #endif
  845. /*************************************************
  846. * Check for counted repeat *
  847. *************************************************/
  848. /* This function is called when a '{' is encountered in a place where it might
  849. start a quantifier. It looks ahead to see if it really is a quantifier or not.
  850. It is only a quantifier if it is one of the forms {ddd} {ddd,} or {ddd,ddd}
  851. where the ddds are digits.
  852. Arguments:
  853. p pointer to the first char after '{'
  854. Returns: TRUE or FALSE
  855. */
  856. static BOOL
  857. is_counted_repeat(const uschar *p)
  858. {
  859. if ((digitab[*p++] & ctype_digit) == 0) return FALSE;
  860. while ((digitab[*p] & ctype_digit) != 0) p++;
  861. if (*p == CHAR_RIGHT_CURLY_BRACKET) return TRUE;
  862. if (*p++ != CHAR_COMMA) return FALSE;
  863. if (*p == CHAR_RIGHT_CURLY_BRACKET) return TRUE;
  864. if ((digitab[*p++] & ctype_digit) == 0) return FALSE;
  865. while ((digitab[*p] & ctype_digit) != 0) p++;
  866. return (*p == CHAR_RIGHT_CURLY_BRACKET);
  867. }
  868. /*************************************************
  869. * Read repeat counts *
  870. *************************************************/
  871. /* Read an item of the form {n,m} and return the values. This is called only
  872. after is_counted_repeat() has confirmed that a repeat-count quantifier exists,
  873. so the syntax is guaranteed to be correct, but we need to check the values.
  874. Arguments:
  875. p pointer to first char after '{'
  876. minp pointer to int for min
  877. maxp pointer to int for max
  878. returned as -1 if no max
  879. errorcodeptr points to error code variable
  880. Returns: pointer to '}' on success;
  881. current ptr on error, with errorcodeptr set non-zero
  882. */
  883. static const uschar *
  884. read_repeat_counts(const uschar *p, int *minp, int *maxp, int *errorcodeptr)
  885. {
  886. int min = 0;
  887. int max = -1;
  888. /* Read the minimum value and do a paranoid check: a negative value indicates
  889. an integer overflow. */
  890. while ((digitab[*p] & ctype_digit) != 0) min = min * 10 + *p++ - CHAR_0;
  891. if (min < 0 || min > 65535)
  892. {
  893. *errorcodeptr = ERR5;
  894. return p;
  895. }
  896. /* Read the maximum value if there is one, and again do a paranoid on its size.
  897. Also, max must not be less than min. */
  898. if (*p == CHAR_RIGHT_CURLY_BRACKET) max = min; else
  899. {
  900. if (*(++p) != CHAR_RIGHT_CURLY_BRACKET)
  901. {
  902. max = 0;
  903. while((digitab[*p] & ctype_digit) != 0) max = max * 10 + *p++ - CHAR_0;
  904. if (max < 0 || max > 65535)
  905. {
  906. *errorcodeptr = ERR5;
  907. return p;
  908. }
  909. if (max < min)
  910. {
  911. *errorcodeptr = ERR4;
  912. return p;
  913. }
  914. }
  915. }
  916. /* Fill in the required variables, and pass back the pointer to the terminating
  917. '}'. */
  918. *minp = min;
  919. *maxp = max;
  920. return p;
  921. }
  922. /*************************************************
  923. * Subroutine for finding forward reference *
  924. *************************************************/
  925. /* This recursive function is called only from find_parens() below. The
  926. top-level call starts at the beginning of the pattern. All other calls must
  927. start at a parenthesis. It scans along a pattern's text looking for capturing
  928. subpatterns, and counting them. If it finds a named pattern that matches the
  929. name it is given, it returns its number. Alternatively, if the name is NULL, it
  930. returns when it reaches a given numbered subpattern. Recursion is used to keep
  931. track of subpatterns that reset the capturing group numbers - the (?| feature.
  932. This function was originally called only from the second pass, in which we know
  933. that if (?< or (?' or (?P< is encountered, the name will be correctly
  934. terminated because that is checked in the first pass. There is now one call to
  935. this function in the first pass, to check for a recursive back reference by
  936. name (so that we can make the whole group atomic). In this case, we need check
  937. only up to the current position in the pattern, and that is still OK because
  938. and previous occurrences will have been checked. To make this work, the test
  939. for "end of pattern" is a check against cd->end_pattern in the main loop,
  940. instead of looking for a binary zero. This means that the special first-pass
  941. call can adjust cd->end_pattern temporarily. (Checks for binary zero while
  942. processing items within the loop are OK, because afterwards the main loop will
  943. terminate.)
  944. Arguments:
  945. ptrptr address of the current character pointer (updated)
  946. cd compile background data
  947. name name to seek, or NULL if seeking a numbered subpattern
  948. lorn name length, or subpattern number if name is NULL
  949. xmode TRUE if we are in /x mode
  950. utf8 TRUE if we are in UTF-8 mode
  951. count pointer to the current capturing subpattern number (updated)
  952. Returns: the number of the named subpattern, or -1 if not found
  953. */
  954. static int
  955. find_parens_sub(uschar **ptrptr, compile_data *cd, const uschar *name, int lorn,
  956. BOOL xmode, BOOL utf8, int *count)
  957. {
  958. uschar *ptr = *ptrptr;
  959. int start_count = *count;
  960. int hwm_count = start_count;
  961. BOOL dup_parens = FALSE;
  962. /* If the first character is a parenthesis, check on the type of group we are
  963. dealing with. The very first call may not start with a parenthesis. */
  964. if (ptr[0] == CHAR_LEFT_PARENTHESIS)
  965. {
  966. /* Handle specials such as (*SKIP) or (*UTF8) etc. */
  967. if (ptr[1] == CHAR_ASTERISK) ptr += 2;
  968. /* Handle a normal, unnamed capturing parenthesis. */
  969. else if (ptr[1] != CHAR_QUESTION_MARK)
  970. {
  971. *count += 1;
  972. if (name == NULL && *count == lorn) return *count;
  973. ptr++;
  974. }
  975. /* All cases now have (? at the start. Remember when we are in a group
  976. where the parenthesis numbers are duplicated. */
  977. else if (ptr[2] == CHAR_VERTICAL_LINE)
  978. {
  979. ptr += 3;
  980. dup_parens = TRUE;
  981. }
  982. /* Handle comments; all characters are allowed until a ket is reached. */
  983. else if (ptr[2] == CHAR_NUMBER_SIGN)
  984. {
  985. for (ptr += 3; *ptr != 0; ptr++) if (*ptr == CHAR_RIGHT_PARENTHESIS) break;
  986. goto FAIL_EXIT;
  987. }
  988. /* Handle a condition. If it is an assertion, just carry on so that it
  989. is processed as normal. If not, skip to the closing parenthesis of the
  990. condition (there can't be any nested parens). */
  991. else if (ptr[2] == CHAR_LEFT_PARENTHESIS)
  992. {
  993. ptr += 2;
  994. if (ptr[1] != CHAR_QUESTION_MARK)
  995. {
  996. while (*ptr != 0 && *ptr != CHAR_RIGHT_PARENTHESIS) ptr++;
  997. if (*ptr != 0) ptr++;
  998. }
  999. }
  1000. /* Start with (? but not a condition. */
  1001. else
  1002. {
  1003. ptr += 2;
  1004. if (*ptr == CHAR_P) ptr++; /* Allow optional P */
  1005. /* We have to disambiguate (?<! and (?<= from (?<name> for named groups */
  1006. if ((*ptr == CHAR_LESS_THAN_SIGN && ptr[1] != CHAR_EXCLAMATION_MARK &&
  1007. ptr[1] != CHAR_EQUALS_SIGN) || *ptr == CHAR_APOSTROPHE)
  1008. {
  1009. int term;
  1010. const uschar *thisname;
  1011. *count += 1;
  1012. if (name == NULL && *count == lorn) return *count;
  1013. term = *ptr++;
  1014. if (term == CHAR_LESS_THAN_SIGN) term = CHAR_GREATER_THAN_SIGN;
  1015. thisname = ptr;
  1016. while (*ptr != term) ptr++;
  1017. if (name != NULL && lorn == ptr - thisname &&
  1018. strncmp((const char *)name, (const char *)thisname, lorn) == 0)
  1019. return *count;
  1020. term++;
  1021. }
  1022. }
  1023. }
  1024. /* Past any initial parenthesis handling, scan for parentheses or vertical
  1025. bars. Stop if we get to cd->end_pattern. Note that this is important for the
  1026. first-pass call when this value is temporarily adjusted to stop at the current
  1027. position. So DO NOT change this to a test for binary zero. */
  1028. for (; ptr < cd->end_pattern; ptr++)
  1029. {
  1030. /* Skip over backslashed characters and also entire \Q...\E */
  1031. if (*ptr == CHAR_BACKSLASH)
  1032. {
  1033. if (*(++ptr) == 0) goto FAIL_EXIT;
  1034. if (*ptr == CHAR_Q) for (;;)
  1035. {
  1036. while (*(++ptr) != 0 && *ptr != CHAR_BACKSLASH) {};
  1037. if (*ptr == 0) goto FAIL_EXIT;
  1038. if (*(++ptr) == CHAR_E) break;
  1039. }
  1040. continue;
  1041. }
  1042. /* Skip over character classes; this logic must be similar to the way they
  1043. are handled for real. If the first character is '^', skip it. Also, if the
  1044. first few characters (either before or after ^) are \Q\E or \E we skip them
  1045. too. This makes for compatibility with Perl. Note the use of STR macros to
  1046. encode "Q\\E" so that it works in UTF-8 on EBCDIC platforms. */
  1047. if (*ptr == CHAR_LEFT_SQUARE_BRACKET)
  1048. {
  1049. BOOL negate_class = FALSE;
  1050. for (;;)
  1051. {
  1052. if (ptr[1] == CHAR_BACKSLASH)
  1053. {
  1054. if (ptr[2] == CHAR_E)
  1055. ptr+= 2;
  1056. else if (strncmp((const char *)ptr+2,
  1057. STR_Q STR_BACKSLASH STR_E, 3) == 0)
  1058. ptr += 4;
  1059. else
  1060. break;
  1061. }
  1062. else if (!negate_class && ptr[1] == CHAR_CIRCUMFLEX_ACCENT)
  1063. {
  1064. negate_class = TRUE;
  1065. ptr++;
  1066. }
  1067. else break;
  1068. }
  1069. /* If the next character is ']', it is a data character that must be
  1070. skipped, except in JavaScript compatibility mode. */
  1071. if (ptr[1] == CHAR_RIGHT_SQUARE_BRACKET &&
  1072. (cd->external_options & PCRE_JAVASCRIPT_COMPAT) == 0)
  1073. ptr++;
  1074. while (*(++ptr) != CHAR_RIGHT_SQUARE_BRACKET)
  1075. {
  1076. if (*ptr == 0) return -1;
  1077. if (*ptr == CHAR_BACKSLASH)
  1078. {
  1079. if (*(++ptr) == 0) goto FAIL_EXIT;
  1080. if (*ptr == CHAR_Q) for (;;)
  1081. {
  1082. while (*(++ptr) != 0 && *ptr != CHAR_BACKSLASH) {};
  1083. if (*ptr == 0) goto FAIL_EXIT;
  1084. if (*(++ptr) == CHAR_E) break;
  1085. }
  1086. continue;
  1087. }
  1088. }
  1089. continue;
  1090. }
  1091. /* Skip comments in /x mode */
  1092. if (xmode && *ptr == CHAR_NUMBER_SIGN)
  1093. {
  1094. ptr++;
  1095. while (*ptr != 0)
  1096. {
  1097. if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }
  1098. ptr++;
  1099. #ifdef SUPPORT_UTF8
  1100. if (utf8) while ((*ptr & 0xc0) == 0x80) ptr++;
  1101. #endif
  1102. }
  1103. if (*ptr == 0) goto FAIL_EXIT;
  1104. continue;
  1105. }
  1106. /* Check for the special metacharacters */
  1107. if (*ptr == CHAR_LEFT_PARENTHESIS)
  1108. {
  1109. int rc = find_parens_sub(&ptr, cd, name, lorn, xmode, utf8, count);
  1110. if (rc > 0) return rc;
  1111. if (*ptr == 0) goto FAIL_EXIT;
  1112. }
  1113. else if (*ptr == CHAR_RIGHT_PARENTHESIS)
  1114. {
  1115. if (dup_parens && *count < hwm_count) *count = hwm_count;
  1116. goto FAIL_EXIT;
  1117. }
  1118. else if (*ptr == CHAR_VERTICAL_LINE && dup_parens)
  1119. {
  1120. if (*count > hwm_count) hwm_count = *count;
  1121. *count = start_count;
  1122. }
  1123. }
  1124. FAIL_EXIT:
  1125. *ptrptr = ptr;
  1126. return -1;
  1127. }
  1128. /*************************************************
  1129. * Find forward referenced subpattern *
  1130. *************************************************/
  1131. /* This function scans along a pattern's text looking for capturing
  1132. subpatterns, and counting them. If it finds a named pattern that matches the
  1133. name it is given, it returns its number. Alternatively, if the name is NULL, it
  1134. returns when it reaches a given numbered subpattern. This is used for forward
  1135. references to subpatterns. We used to be able to start this scan from the
  1136. current compiling point, using the current count value from cd->bracount, and
  1137. do it all in a single loop, but the addition of the possibility of duplicate
  1138. subpattern numbers means that we have to scan from the very start, in order to
  1139. take account of such duplicates, and to use a recursive function to keep track
  1140. of the different types of group.
  1141. Arguments:
  1142. cd compile background data
  1143. name name to seek, or NULL if seeking a numbered subpattern
  1144. lorn name length, or subpattern number if name is NULL
  1145. xmode TRUE if we are in /x mode
  1146. utf8 TRUE if we are in UTF-8 mode
  1147. Returns: the number of the found subpattern, or -1 if not found
  1148. */
  1149. static int
  1150. find_parens(compile_data *cd, const uschar *name, int lorn, BOOL xmode,
  1151. BOOL utf8)
  1152. {
  1153. uschar *ptr = (uschar *)cd->start_pattern;
  1154. int count = 0;
  1155. int rc;
  1156. /* If the pattern does not start with an opening parenthesis, the first call
  1157. to find_parens_sub() will scan right to the end (if necessary). However, if it
  1158. does start with a parenthesis, find_parens_sub() will return when it hits the
  1159. matching closing parens. That is why we have to have a loop. */
  1160. for (;;)
  1161. {
  1162. rc = find_parens_sub(&ptr, cd, name, lorn, xmode, utf8, &count);
  1163. if (rc > 0 || *ptr++ == 0) break;
  1164. }
  1165. return rc;
  1166. }
  1167. /*************************************************
  1168. * Find first significant op code *
  1169. *************************************************/
  1170. /* This is called by several functions that scan a compiled expression looking
  1171. for a fixed first character, or an anchoring op code etc. It skips over things
  1172. that do not influence this. For some calls, a change of option is important.
  1173. For some calls, it makes sense to skip negative forward and all backward
  1174. assertions, and also the \b assertion; for others it does not.
  1175. Arguments:
  1176. code pointer to the start of the group
  1177. options pointer to external options
  1178. optbit the option bit whose changing is significant, or
  1179. zero if none are
  1180. skipassert TRUE if certain assertions are to be skipped
  1181. Returns: pointer to the first significant opcode
  1182. */
  1183. static const uschar*
  1184. first_significant_code(const uschar *code, int *options, int optbit,
  1185. BOOL skipassert)
  1186. {
  1187. for (;;)
  1188. {
  1189. switch ((int)*code)
  1190. {
  1191. case

Large files files are truncated, but you can click here to view the full file