PageRenderTime 28ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/tags/harbour-2.0.0/external/pcre/pcrecomp.c

#
C | 2069 lines | 1294 code | 286 blank | 489 comment | 415 complexity | 62f41abc94313478ea28a386d792a55c 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-2009 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 DEBUG is defined, we need the pcre_printint() function, which is also
  42. used by pcretest. DEBUG is not defined when building a production library. */
  43. #ifdef DEBUG
  44. #include "pcreprni.h"
  45. #endif
  46. /* Macro for setting individual bits in class bitmaps. */
  47. #define SETBIT(a,b) a[b/8] |= (1 << (b%8))
  48. /* Maximum length value to check against when making sure that the integer that
  49. holds the compiled pattern length does not overflow. We make it a bit less than
  50. INT_MAX to allow for adding in group terminating bytes, so that we don't have
  51. to check them every time. */
  52. #define OFLOW_MAX (INT_MAX - 20)
  53. /*************************************************
  54. * Code parameters and static tables *
  55. *************************************************/
  56. /* This value specifies the size of stack workspace that is used during the
  57. first pre-compile phase that determines how much memory is required. The regex
  58. is partly compiled into this space, but the compiled parts are discarded as
  59. soon as they can be, so that hopefully there will never be an overrun. The code
  60. does, however, check for an overrun. The largest amount I've seen used is 218,
  61. so this number is very generous.
  62. The same workspace is used during the second, actual compile phase for
  63. remembering forward references to groups so that they can be filled in at the
  64. end. Each entry in this list occupies LINK_SIZE bytes, so even when LINK_SIZE
  65. is 4 there is plenty of room. */
  66. #define COMPILE_WORK_SIZE (4096)
  67. /* Table for handling escaped characters in the range '0'-'z'. Positive returns
  68. are simple data values; negative values are for special things like \d and so
  69. on. Zero means further processing is needed (for things like \x), or the escape
  70. is invalid. */
  71. #ifndef EBCDIC
  72. /* This is the "normal" table for ASCII systems or for EBCDIC systems running
  73. in UTF-8 mode. */
  74. static const short int escapes[] = {
  75. 0, 0,
  76. 0, 0,
  77. 0, 0,
  78. 0, 0,
  79. 0, 0,
  80. CHAR_COLON, CHAR_SEMICOLON,
  81. CHAR_LESS_THAN_SIGN, CHAR_EQUALS_SIGN,
  82. CHAR_GREATER_THAN_SIGN, CHAR_QUESTION_MARK,
  83. CHAR_COMMERCIAL_AT, -ESC_A,
  84. -ESC_B, -ESC_C,
  85. -ESC_D, -ESC_E,
  86. 0, -ESC_G,
  87. -ESC_H, 0,
  88. 0, -ESC_K,
  89. 0, 0,
  90. 0, 0,
  91. -ESC_P, -ESC_Q,
  92. -ESC_R, -ESC_S,
  93. 0, 0,
  94. -ESC_V, -ESC_W,
  95. -ESC_X, 0,
  96. -ESC_Z, CHAR_LEFT_SQUARE_BRACKET,
  97. CHAR_BACKSLASH, CHAR_RIGHT_SQUARE_BRACKET,
  98. CHAR_CIRCUMFLEX_ACCENT, CHAR_UNDERSCORE,
  99. CHAR_GRAVE_ACCENT, 7,
  100. -ESC_b, 0,
  101. -ESC_d, ESC_e,
  102. ESC_f, 0,
  103. -ESC_h, 0,
  104. 0, -ESC_k,
  105. 0, 0,
  106. ESC_n, 0,
  107. -ESC_p, 0,
  108. ESC_r, -ESC_s,
  109. ESC_tee, 0,
  110. -ESC_v, -ESC_w,
  111. 0, 0,
  112. -ESC_z
  113. };
  114. #else
  115. /* This is the "abnormal" table for EBCDIC systems without UTF-8 support. */
  116. static const short int escapes[] = {
  117. /* 48 */ 0, 0, 0, '.', '<', '(', '+', '|',
  118. /* 50 */ '&', 0, 0, 0, 0, 0, 0, 0,
  119. /* 58 */ 0, 0, '!', '$', '*', ')', ';', '~',
  120. /* 60 */ '-', '/', 0, 0, 0, 0, 0, 0,
  121. /* 68 */ 0, 0, '|', ',', '%', '_', '>', '?',
  122. /* 70 */ 0, 0, 0, 0, 0, 0, 0, 0,
  123. /* 78 */ 0, '`', ':', '#', '@', '\'', '=', '"',
  124. /* 80 */ 0, 7, -ESC_b, 0, -ESC_d, ESC_e, ESC_f, 0,
  125. /* 88 */-ESC_h, 0, 0, '{', 0, 0, 0, 0,
  126. /* 90 */ 0, 0, -ESC_k, 'l', 0, ESC_n, 0, -ESC_p,
  127. /* 98 */ 0, ESC_r, 0, '}', 0, 0, 0, 0,
  128. /* A0 */ 0, '~', -ESC_s, ESC_tee, 0,-ESC_v, -ESC_w, 0,
  129. /* A8 */ 0,-ESC_z, 0, 0, 0, '[', 0, 0,
  130. /* B0 */ 0, 0, 0, 0, 0, 0, 0, 0,
  131. /* B8 */ 0, 0, 0, 0, 0, ']', '=', '-',
  132. /* C0 */ '{',-ESC_A, -ESC_B, -ESC_C, -ESC_D,-ESC_E, 0, -ESC_G,
  133. /* C8 */-ESC_H, 0, 0, 0, 0, 0, 0, 0,
  134. /* D0 */ '}', 0, -ESC_K, 0, 0, 0, 0, -ESC_P,
  135. /* D8 */-ESC_Q,-ESC_R, 0, 0, 0, 0, 0, 0,
  136. /* E0 */ '\\', 0, -ESC_S, 0, 0,-ESC_V, -ESC_W, -ESC_X,
  137. /* E8 */ 0,-ESC_Z, 0, 0, 0, 0, 0, 0,
  138. /* F0 */ 0, 0, 0, 0, 0, 0, 0, 0,
  139. /* F8 */ 0, 0, 0, 0, 0, 0, 0, 0
  140. };
  141. #endif
  142. /* Table of special "verbs" like (*PRUNE). This is a short table, so it is
  143. searched linearly. Put all the names into a single string, in order to reduce
  144. the number of relocations when a shared library is dynamically linked. The
  145. string is built from string macros so that it works in UTF-8 mode on EBCDIC
  146. platforms. */
  147. typedef struct verbitem {
  148. int len;
  149. int op;
  150. } verbitem;
  151. static const char verbnames[] =
  152. STRING_ACCEPT0
  153. STRING_COMMIT0
  154. STRING_F0
  155. STRING_FAIL0
  156. STRING_PRUNE0
  157. STRING_SKIP0
  158. STRING_THEN;
  159. static const verbitem verbs[] = {
  160. { 6, OP_ACCEPT },
  161. { 6, OP_COMMIT },
  162. { 1, OP_FAIL },
  163. { 4, OP_FAIL },
  164. { 5, OP_PRUNE },
  165. { 4, OP_SKIP },
  166. { 4, OP_THEN }
  167. };
  168. static const int verbcount = sizeof(verbs)/sizeof(verbitem);
  169. /* Tables of names of POSIX character classes and their lengths. The names are
  170. now all in a single string, to reduce the number of relocations when a shared
  171. library is dynamically loaded. The list of lengths is terminated by a zero
  172. length entry. The first three must be alpha, lower, upper, as this is assumed
  173. for handling case independence. */
  174. static const char posix_names[] =
  175. STRING_alpha0 STRING_lower0 STRING_upper0 STRING_alnum0
  176. STRING_ascii0 STRING_blank0 STRING_cntrl0 STRING_digit0
  177. STRING_graph0 STRING_print0 STRING_punct0 STRING_space0
  178. STRING_word0 STRING_xdigit;
  179. static const uschar posix_name_lengths[] = {
  180. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 6, 0 };
  181. /* Table of class bit maps for each POSIX class. Each class is formed from a
  182. base map, with an optional addition or removal of another map. Then, for some
  183. classes, there is some additional tweaking: for [:blank:] the vertical space
  184. characters are removed, and for [:alpha:] and [:alnum:] the underscore
  185. character is removed. The triples in the table consist of the base map offset,
  186. second map offset or -1 if no second map, and a non-negative value for map
  187. addition or a negative value for map subtraction (if there are two maps). The
  188. absolute value of the third field has these meanings: 0 => no tweaking, 1 =>
  189. remove vertical space characters, 2 => remove underscore. */
  190. static const int posix_class_maps[] = {
  191. cbit_word, cbit_digit, -2, /* alpha */
  192. cbit_lower, -1, 0, /* lower */
  193. cbit_upper, -1, 0, /* upper */
  194. cbit_word, -1, 2, /* alnum - word without underscore */
  195. cbit_print, cbit_cntrl, 0, /* ascii */
  196. cbit_space, -1, 1, /* blank - a GNU extension */
  197. cbit_cntrl, -1, 0, /* cntrl */
  198. cbit_digit, -1, 0, /* digit */
  199. cbit_graph, -1, 0, /* graph */
  200. cbit_print, -1, 0, /* print */
  201. cbit_punct, -1, 0, /* punct */
  202. cbit_space, -1, 0, /* space */
  203. cbit_word, -1, 0, /* word - a Perl extension */
  204. cbit_xdigit,-1, 0 /* xdigit */
  205. };
  206. #define STRING(a) # a
  207. #define XSTRING(s) STRING(s)
  208. /* The texts of compile-time error messages. These are "char *" because they
  209. are passed to the outside world. Do not ever re-use any error number, because
  210. they are documented. Always add a new error instead. Messages marked DEAD below
  211. are no longer used. This used to be a table of strings, but in order to reduce
  212. the number of relocations needed when a shared library is loaded dynamically,
  213. it is now one long string. We cannot use a table of offsets, because the
  214. lengths of inserts such as XSTRING(MAX_NAME_SIZE) are not known. Instead, we
  215. simply count through to the one we want - this isn't a performance issue
  216. because these strings are used only when there is a compilation error. */
  217. static const char error_texts[] =
  218. "no error\0"
  219. "\\ at end of pattern\0"
  220. "\\c at end of pattern\0"
  221. "unrecognized character follows \\\0"
  222. "numbers out of order in {} quantifier\0"
  223. /* 5 */
  224. "number too big in {} quantifier\0"
  225. "missing terminating ] for character class\0"
  226. "invalid escape sequence in character class\0"
  227. "range out of order in character class\0"
  228. "nothing to repeat\0"
  229. /* 10 */
  230. "operand of unlimited repeat could match the empty string\0" /** DEAD **/
  231. "internal error: unexpected repeat\0"
  232. "unrecognized character after (? or (?-\0"
  233. "POSIX named classes are supported only within a class\0"
  234. "missing )\0"
  235. /* 15 */
  236. "reference to non-existent subpattern\0"
  237. "erroffset passed as NULL\0"
  238. "unknown option bit(s) set\0"
  239. "missing ) after comment\0"
  240. "parentheses nested too deeply\0" /** DEAD **/
  241. /* 20 */
  242. "regular expression is too large\0"
  243. "failed to get memory\0"
  244. "unmatched parentheses\0"
  245. "internal error: code overflow\0"
  246. "unrecognized character after (?<\0"
  247. /* 25 */
  248. "lookbehind assertion is not fixed length\0"
  249. "malformed number or name after (?(\0"
  250. "conditional group contains more than two branches\0"
  251. "assertion expected after (?(\0"
  252. "(?R or (?[+-]digits must be followed by )\0"
  253. /* 30 */
  254. "unknown POSIX class name\0"
  255. "POSIX collating elements are not supported\0"
  256. "this version of PCRE is not compiled with PCRE_UTF8 support\0"
  257. "spare error\0" /** DEAD **/
  258. "character value in \\x{...} sequence is too large\0"
  259. /* 35 */
  260. "invalid condition (?(0)\0"
  261. "\\C not allowed in lookbehind assertion\0"
  262. "PCRE does not support \\L, \\l, \\N, \\U, or \\u\0"
  263. "number after (?C is > 255\0"
  264. "closing ) for (?C expected\0"
  265. /* 40 */
  266. "recursive call could loop indefinitely\0"
  267. "unrecognized character after (?P\0"
  268. "syntax error in subpattern name (missing terminator)\0"
  269. "two named subpatterns have the same name\0"
  270. "invalid UTF-8 string\0"
  271. /* 45 */
  272. "support for \\P, \\p, and \\X has not been compiled\0"
  273. "malformed \\P or \\p sequence\0"
  274. "unknown property name after \\P or \\p\0"
  275. "subpattern name is too long (maximum " XSTRING(MAX_NAME_SIZE) " characters)\0"
  276. "too many named subpatterns (maximum " XSTRING(MAX_NAME_COUNT) ")\0"
  277. /* 50 */
  278. "repeated subpattern is too long\0" /** DEAD **/
  279. "octal value is greater than \\377 (not in UTF-8 mode)\0"
  280. "internal error: overran compiling workspace\0"
  281. "internal error: previously-checked referenced subpattern not found\0"
  282. "DEFINE group contains more than one branch\0"
  283. /* 55 */
  284. "repeating a DEFINE group is not allowed\0"
  285. "inconsistent NEWLINE options\0"
  286. "\\g is not followed by a braced, angle-bracketed, or quoted name/number or by a plain number\0"
  287. "a numbered reference must not be zero\0"
  288. "(*VERB) with an argument is not supported\0"
  289. /* 60 */
  290. "(*VERB) not recognized\0"
  291. "number is too big\0"
  292. "subpattern name expected\0"
  293. "digit expected after (?+\0"
  294. "] is an invalid data character in JavaScript compatibility mode\0"
  295. /* 65 */
  296. "different names for subpatterns of the same number are not allowed";
  297. /* Table to identify digits and hex digits. This is used when compiling
  298. patterns. Note that the tables in chartables are dependent on the locale, and
  299. may mark arbitrary characters as digits - but the PCRE compiling code expects
  300. to handle only 0-9, a-z, and A-Z as digits when compiling. That is why we have
  301. a private table here. It costs 256 bytes, but it is a lot faster than doing
  302. character value tests (at least in some simple cases I timed), and in some
  303. applications one wants PCRE to compile efficiently as well as match
  304. efficiently.
  305. For convenience, we use the same bit definitions as in chartables:
  306. 0x04 decimal digit
  307. 0x08 hexadecimal digit
  308. Then we can use ctype_digit and ctype_xdigit in the code. */
  309. #ifndef EBCDIC
  310. /* This is the "normal" case, for ASCII systems, and EBCDIC systems running in
  311. UTF-8 mode. */
  312. static const unsigned char digitab[] =
  313. {
  314. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 0- 7 */
  315. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 8- 15 */
  316. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 16- 23 */
  317. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 24- 31 */
  318. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* - ' */
  319. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* ( - / */
  320. 0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c, /* 0 - 7 */
  321. 0x0c,0x0c,0x00,0x00,0x00,0x00,0x00,0x00, /* 8 - ? */
  322. 0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x00, /* @ - G */
  323. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* H - O */
  324. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* P - W */
  325. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* X - _ */
  326. 0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x00, /* ` - g */
  327. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* h - o */
  328. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* p - w */
  329. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* x -127 */
  330. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 128-135 */
  331. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 136-143 */
  332. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 144-151 */
  333. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 152-159 */
  334. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 160-167 */
  335. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 168-175 */
  336. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 176-183 */
  337. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 184-191 */
  338. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 192-199 */
  339. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 200-207 */
  340. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 208-215 */
  341. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 216-223 */
  342. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 224-231 */
  343. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 232-239 */
  344. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 240-247 */
  345. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};/* 248-255 */
  346. #else
  347. /* This is the "abnormal" case, for EBCDIC systems not running in UTF-8 mode. */
  348. static const unsigned char digitab[] =
  349. {
  350. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 0- 7 0 */
  351. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 8- 15 */
  352. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 16- 23 10 */
  353. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 24- 31 */
  354. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 32- 39 20 */
  355. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 40- 47 */
  356. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 48- 55 30 */
  357. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 56- 63 */
  358. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* - 71 40 */
  359. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 72- | */
  360. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* & - 87 50 */
  361. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 88- 95 */
  362. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* - -103 60 */
  363. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 104- ? */
  364. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 112-119 70 */
  365. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 120- " */
  366. 0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x00, /* 128- g 80 */
  367. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* h -143 */
  368. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 144- p 90 */
  369. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* q -159 */
  370. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 160- x A0 */
  371. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* y -175 */
  372. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* ^ -183 B0 */
  373. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 184-191 */
  374. 0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x00, /* { - G C0 */
  375. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* H -207 */
  376. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* } - P D0 */
  377. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* Q -223 */
  378. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* \ - X E0 */
  379. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* Y -239 */
  380. 0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c, /* 0 - 7 F0 */
  381. 0x0c,0x0c,0x00,0x00,0x00,0x00,0x00,0x00};/* 8 -255 */
  382. static const unsigned char ebcdic_chartab[] = { /* chartable partial dup */
  383. 0x80,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /* 0- 7 */
  384. 0x00,0x00,0x00,0x00,0x01,0x01,0x00,0x00, /* 8- 15 */
  385. 0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /* 16- 23 */
  386. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 24- 31 */
  387. 0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /* 32- 39 */
  388. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 40- 47 */
  389. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 48- 55 */
  390. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 56- 63 */
  391. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* - 71 */
  392. 0x00,0x00,0x00,0x80,0x00,0x80,0x80,0x80, /* 72- | */
  393. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* & - 87 */
  394. 0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00, /* 88- 95 */
  395. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* - -103 */
  396. 0x00,0x00,0x00,0x00,0x00,0x10,0x00,0x80, /* 104- ? */
  397. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 112-119 */
  398. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 120- " */
  399. 0x00,0x1a,0x1a,0x1a,0x1a,0x1a,0x1a,0x12, /* 128- g */
  400. 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* h -143 */
  401. 0x00,0x12,0x12,0x12,0x12,0x12,0x12,0x12, /* 144- p */
  402. 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* q -159 */
  403. 0x00,0x00,0x12,0x12,0x12,0x12,0x12,0x12, /* 160- x */
  404. 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* y -175 */
  405. 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* ^ -183 */
  406. 0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x00, /* 184-191 */
  407. 0x80,0x1a,0x1a,0x1a,0x1a,0x1a,0x1a,0x12, /* { - G */
  408. 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* H -207 */
  409. 0x00,0x12,0x12,0x12,0x12,0x12,0x12,0x12, /* } - P */
  410. 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* Q -223 */
  411. 0x00,0x00,0x12,0x12,0x12,0x12,0x12,0x12, /* \ - X */
  412. 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* Y -239 */
  413. 0x1c,0x1c,0x1c,0x1c,0x1c,0x1c,0x1c,0x1c, /* 0 - 7 */
  414. 0x1c,0x1c,0x00,0x00,0x00,0x00,0x00,0x00};/* 8 -255 */
  415. #endif
  416. /* Definition to allow mutual recursion */
  417. static BOOL
  418. compile_regex(int, int, uschar **, const uschar **, int *, BOOL, BOOL, int,
  419. int *, int *, branch_chain *, compile_data *, int *);
  420. /*************************************************
  421. * Find an error text *
  422. *************************************************/
  423. /* The error texts are now all in one long string, to save on relocations. As
  424. some of the text is of unknown length, we can't use a table of offsets.
  425. Instead, just count through the strings. This is not a performance issue
  426. because it happens only when there has been a compilation error.
  427. Argument: the error number
  428. Returns: pointer to the error string
  429. */
  430. static const char *
  431. find_error_text(int n)
  432. {
  433. const char *s = error_texts;
  434. for (; n > 0; n--) while (*s++ != 0) {};
  435. return s;
  436. }
  437. /*************************************************
  438. * Handle escapes *
  439. *************************************************/
  440. /* This function is called when a \ has been encountered. It either returns a
  441. positive value for a simple escape such as \n, or a negative value which
  442. encodes one of the more complicated things such as \d. A backreference to group
  443. n is returned as -(ESC_REF + n); ESC_REF is the highest ESC_xxx macro. When
  444. UTF-8 is enabled, a positive value greater than 255 may be returned. On entry,
  445. ptr is pointing at the \. On exit, it is on the final character of the escape
  446. sequence.
  447. Arguments:
  448. ptrptr points to the pattern position pointer
  449. errorcodeptr points to the errorcode variable
  450. bracount number of previous extracting brackets
  451. options the options bits
  452. isclass TRUE if inside a character class
  453. Returns: zero or positive => a data character
  454. negative => a special escape sequence
  455. on error, errorcodeptr is set
  456. */
  457. static int
  458. check_escape(const uschar **ptrptr, int *errorcodeptr, int bracount,
  459. int options, BOOL isclass)
  460. {
  461. BOOL utf8 = (options & PCRE_UTF8) != 0;
  462. const uschar *ptr = *ptrptr + 1;
  463. int c, i;
  464. GETCHARINCTEST(c, ptr); /* Get character value, increment pointer */
  465. ptr--; /* Set pointer back to the last byte */
  466. /* If backslash is at the end of the pattern, it's an error. */
  467. if (c == 0) *errorcodeptr = ERR1;
  468. /* Non-alphanumerics are literals. For digits or letters, do an initial lookup
  469. in a table. A non-zero result is something that can be returned immediately.
  470. Otherwise further processing may be required. */
  471. #ifndef EBCDIC /* ASCII/UTF-8 coding */
  472. else if (c < CHAR_0 || c > CHAR_z) {} /* Not alphanumeric */
  473. else if ((i = escapes[c - CHAR_0]) != 0) c = i;
  474. #else /* EBCDIC coding */
  475. else if (c < 'a' || (ebcdic_chartab[c] & 0x0E) == 0) {} /* Not alphanumeric */
  476. else if ((i = escapes[c - 0x48]) != 0) c = i;
  477. #endif
  478. /* Escapes that need further processing, or are illegal. */
  479. else
  480. {
  481. const uschar *oldptr;
  482. BOOL braced, negated;
  483. switch (c)
  484. {
  485. /* A number of Perl escapes are not handled by PCRE. We give an explicit
  486. error. */
  487. case CHAR_l:
  488. case CHAR_L:
  489. case CHAR_N:
  490. case CHAR_u:
  491. case CHAR_U:
  492. *errorcodeptr = ERR37;
  493. break;
  494. /* \g must be followed by one of a number of specific things:
  495. (1) A number, either plain or braced. If positive, it is an absolute
  496. backreference. If negative, it is a relative backreference. This is a Perl
  497. 5.10 feature.
  498. (2) Perl 5.10 also supports \g{name} as a reference to a named group. This
  499. is part of Perl's movement towards a unified syntax for back references. As
  500. this is synonymous with \k{name}, we fudge it up by pretending it really
  501. was \k.
  502. (3) For Oniguruma compatibility we also support \g followed by a name or a
  503. number either in angle brackets or in single quotes. However, these are
  504. (possibly recursive) subroutine calls, _not_ backreferences. Just return
  505. the -ESC_g code (cf \k). */
  506. case CHAR_g:
  507. if (ptr[1] == CHAR_LESS_THAN_SIGN || ptr[1] == CHAR_APOSTROPHE)
  508. {
  509. c = -ESC_g;
  510. break;
  511. }
  512. /* Handle the Perl-compatible cases */
  513. if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)
  514. {
  515. const uschar *p;
  516. for (p = ptr+2; *p != 0 && *p != CHAR_RIGHT_CURLY_BRACKET; p++)
  517. if (*p != CHAR_MINUS && (digitab[*p] & ctype_digit) == 0) break;
  518. if (*p != 0 && *p != CHAR_RIGHT_CURLY_BRACKET)
  519. {
  520. c = -ESC_k;
  521. break;
  522. }
  523. braced = TRUE;
  524. ptr++;
  525. }
  526. else braced = FALSE;
  527. if (ptr[1] == CHAR_MINUS)
  528. {
  529. negated = TRUE;
  530. ptr++;
  531. }
  532. else negated = FALSE;
  533. c = 0;
  534. while ((digitab[ptr[1]] & ctype_digit) != 0)
  535. c = c * 10 + *(++ptr) - CHAR_0;
  536. if (c < 0) /* Integer overflow */
  537. {
  538. *errorcodeptr = ERR61;
  539. break;
  540. }
  541. if (braced && *(++ptr) != CHAR_RIGHT_CURLY_BRACKET)
  542. {
  543. *errorcodeptr = ERR57;
  544. break;
  545. }
  546. if (c == 0)
  547. {
  548. *errorcodeptr = ERR58;
  549. break;
  550. }
  551. if (negated)
  552. {
  553. if (c > bracount)
  554. {
  555. *errorcodeptr = ERR15;
  556. break;
  557. }
  558. c = bracount - (c - 1);
  559. }
  560. c = -(ESC_REF + c);
  561. break;
  562. /* The handling of escape sequences consisting of a string of digits
  563. starting with one that is not zero is not straightforward. By experiment,
  564. the way Perl works seems to be as follows:
  565. Outside a character class, the digits are read as a decimal number. If the
  566. number is less than 10, or if there are that many previous extracting
  567. left brackets, then it is a back reference. Otherwise, up to three octal
  568. digits are read to form an escaped byte. Thus \123 is likely to be octal
  569. 123 (cf \0123, which is octal 012 followed by the literal 3). If the octal
  570. value is greater than 377, the least significant 8 bits are taken. Inside a
  571. character class, \ followed by a digit is always an octal number. */
  572. case CHAR_1: case CHAR_2: case CHAR_3: case CHAR_4: case CHAR_5:
  573. case CHAR_6: case CHAR_7: case CHAR_8: case CHAR_9:
  574. if (!isclass)
  575. {
  576. oldptr = ptr;
  577. c -= CHAR_0;
  578. while ((digitab[ptr[1]] & ctype_digit) != 0)
  579. c = c * 10 + *(++ptr) - CHAR_0;
  580. if (c < 0) /* Integer overflow */
  581. {
  582. *errorcodeptr = ERR61;
  583. break;
  584. }
  585. if (c < 10 || c <= bracount)
  586. {
  587. c = -(ESC_REF + c);
  588. break;
  589. }
  590. ptr = oldptr; /* Put the pointer back and fall through */
  591. }
  592. /* Handle an octal number following \. If the first digit is 8 or 9, Perl
  593. generates a binary zero byte and treats the digit as a following literal.
  594. Thus we have to pull back the pointer by one. */
  595. if ((c = *ptr) >= CHAR_8)
  596. {
  597. ptr--;
  598. c = 0;
  599. break;
  600. }
  601. /* \0 always starts an octal number, but we may drop through to here with a
  602. larger first octal digit. The original code used just to take the least
  603. significant 8 bits of octal numbers (I think this is what early Perls used
  604. to do). Nowadays we allow for larger numbers in UTF-8 mode, but no more
  605. than 3 octal digits. */
  606. case CHAR_0:
  607. c -= CHAR_0;
  608. while(i++ < 2 && ptr[1] >= CHAR_0 && ptr[1] <= CHAR_7)
  609. c = c * 8 + *(++ptr) - CHAR_0;
  610. if (!utf8 && c > 255) *errorcodeptr = ERR51;
  611. break;
  612. /* \x is complicated. \x{ddd} is a character number which can be greater
  613. than 0xff in utf8 mode, but only if the ddd are hex digits. If not, { is
  614. treated as a data character. */
  615. case CHAR_x:
  616. if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)
  617. {
  618. const uschar *pt = ptr + 2;
  619. int count = 0;
  620. c = 0;
  621. while ((digitab[*pt] & ctype_xdigit) != 0)
  622. {
  623. register int cc = *pt++;
  624. if (c == 0 && cc == CHAR_0) continue; /* Leading zeroes */
  625. count++;
  626. #ifndef EBCDIC /* ASCII/UTF-8 coding */
  627. if (cc >= CHAR_a) cc -= 32; /* Convert to upper case */
  628. c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));
  629. #else /* EBCDIC coding */
  630. if (cc >= CHAR_a && cc <= CHAR_z) cc += 64; /* Convert to upper case */
  631. c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
  632. #endif
  633. }
  634. if (*pt == CHAR_RIGHT_CURLY_BRACKET)
  635. {
  636. if (c < 0 || count > (utf8? 8 : 2)) *errorcodeptr = ERR34;
  637. ptr = pt;
  638. break;
  639. }
  640. /* If the sequence of hex digits does not end with '}', then we don't
  641. recognize this construct; fall through to the normal \x handling. */
  642. }
  643. /* Read just a single-byte hex-defined char */
  644. c = 0;
  645. while (i++ < 2 && (digitab[ptr[1]] & ctype_xdigit) != 0)
  646. {
  647. int cc; /* Some compilers don't like */
  648. cc = *(++ptr); /* ++ in initializers */
  649. #ifndef EBCDIC /* ASCII/UTF-8 coding */
  650. if (cc >= CHAR_a) cc -= 32; /* Convert to upper case */
  651. c = c * 16 + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));
  652. #else /* EBCDIC coding */
  653. if (cc <= CHAR_z) cc += 64; /* Convert to upper case */
  654. c = c * 16 + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
  655. #endif
  656. }
  657. break;
  658. /* For \c, a following letter is upper-cased; then the 0x40 bit is flipped.
  659. This coding is ASCII-specific, but then the whole concept of \cx is
  660. ASCII-specific. (However, an EBCDIC equivalent has now been added.) */
  661. case CHAR_c:
  662. c = *(++ptr);
  663. if (c == 0)
  664. {
  665. *errorcodeptr = ERR2;
  666. break;
  667. }
  668. #ifndef EBCDIC /* ASCII/UTF-8 coding */
  669. if (c >= CHAR_a && c <= CHAR_z) c -= 32;
  670. c ^= 0x40;
  671. #else /* EBCDIC coding */
  672. if (c >= CHAR_a && c <= CHAR_z) c += 64;
  673. c ^= 0xC0;
  674. #endif
  675. break;
  676. /* PCRE_EXTRA enables extensions to Perl in the matter of escapes. Any
  677. other alphanumeric following \ is an error if PCRE_EXTRA was set;
  678. otherwise, for Perl compatibility, it is a literal. This code looks a bit
  679. odd, but there used to be some cases other than the default, and there may
  680. be again in future, so I haven't "optimized" it. */
  681. default:
  682. if ((options & PCRE_EXTRA) != 0) switch(c)
  683. {
  684. default:
  685. *errorcodeptr = ERR3;
  686. break;
  687. }
  688. break;
  689. }
  690. }
  691. *ptrptr = ptr;
  692. return c;
  693. }
  694. #ifdef SUPPORT_UCP
  695. /*************************************************
  696. * Handle \P and \p *
  697. *************************************************/
  698. /* This function is called after \P or \p has been encountered, provided that
  699. PCRE is compiled with support for Unicode properties. On entry, ptrptr is
  700. pointing at the P or p. On exit, it is pointing at the final character of the
  701. escape sequence.
  702. Argument:
  703. ptrptr points to the pattern position pointer
  704. negptr points to a boolean that is set TRUE for negation else FALSE
  705. dptr points to an int that is set to the detailed property value
  706. errorcodeptr points to the error code variable
  707. Returns: type value from ucp_type_table, or -1 for an invalid type
  708. */
  709. static int
  710. get_ucp(const uschar **ptrptr, BOOL *negptr, int *dptr, int *errorcodeptr)
  711. {
  712. int c, i, bot, top;
  713. const uschar *ptr = *ptrptr;
  714. char name[32];
  715. c = *(++ptr);
  716. if (c == 0) goto ERROR_RETURN;
  717. *negptr = FALSE;
  718. /* \P or \p can be followed by a name in {}, optionally preceded by ^ for
  719. negation. */
  720. if (c == CHAR_LEFT_CURLY_BRACKET)
  721. {
  722. if (ptr[1] == CHAR_CIRCUMFLEX_ACCENT)
  723. {
  724. *negptr = TRUE;
  725. ptr++;
  726. }
  727. for (i = 0; i < (int)sizeof(name) - 1; i++)
  728. {
  729. c = *(++ptr);
  730. if (c == 0) goto ERROR_RETURN;
  731. if (c == CHAR_RIGHT_CURLY_BRACKET) break;
  732. name[i] = c;
  733. }
  734. if (c != CHAR_RIGHT_CURLY_BRACKET) goto ERROR_RETURN;
  735. name[i] = 0;
  736. }
  737. /* Otherwise there is just one following character */
  738. else
  739. {
  740. name[0] = c;
  741. name[1] = 0;
  742. }
  743. *ptrptr = ptr;
  744. /* Search for a recognized property name using binary chop */
  745. bot = 0;
  746. top = _pcre_utt_size;
  747. while (bot < top)
  748. {
  749. i = (bot + top) >> 1;
  750. c = strcmp(name, _pcre_utt_names + _pcre_utt[i].name_offset);
  751. if (c == 0)
  752. {
  753. *dptr = _pcre_utt[i].value;
  754. return _pcre_utt[i].type;
  755. }
  756. if (c > 0) bot = i + 1; else top = i;
  757. }
  758. *errorcodeptr = ERR47;
  759. *ptrptr = ptr;
  760. return -1;
  761. ERROR_RETURN:
  762. *errorcodeptr = ERR46;
  763. *ptrptr = ptr;
  764. return -1;
  765. }
  766. #endif
  767. /*************************************************
  768. * Check for counted repeat *
  769. *************************************************/
  770. /* This function is called when a '{' is encountered in a place where it might
  771. start a quantifier. It looks ahead to see if it really is a quantifier or not.
  772. It is only a quantifier if it is one of the forms {ddd} {ddd,} or {ddd,ddd}
  773. where the ddds are digits.
  774. Arguments:
  775. p pointer to the first char after '{'
  776. Returns: TRUE or FALSE
  777. */
  778. static BOOL
  779. is_counted_repeat(const uschar *p)
  780. {
  781. if ((digitab[*p++] & ctype_digit) == 0) return FALSE;
  782. while ((digitab[*p] & ctype_digit) != 0) p++;
  783. if (*p == CHAR_RIGHT_CURLY_BRACKET) return TRUE;
  784. if (*p++ != CHAR_COMMA) return FALSE;
  785. if (*p == CHAR_RIGHT_CURLY_BRACKET) return TRUE;
  786. if ((digitab[*p++] & ctype_digit) == 0) return FALSE;
  787. while ((digitab[*p] & ctype_digit) != 0) p++;
  788. return (*p == CHAR_RIGHT_CURLY_BRACKET);
  789. }
  790. /*************************************************
  791. * Read repeat counts *
  792. *************************************************/
  793. /* Read an item of the form {n,m} and return the values. This is called only
  794. after is_counted_repeat() has confirmed that a repeat-count quantifier exists,
  795. so the syntax is guaranteed to be correct, but we need to check the values.
  796. Arguments:
  797. p pointer to first char after '{'
  798. minp pointer to int for min
  799. maxp pointer to int for max
  800. returned as -1 if no max
  801. errorcodeptr points to error code variable
  802. Returns: pointer to '}' on success;
  803. current ptr on error, with errorcodeptr set non-zero
  804. */
  805. static const uschar *
  806. read_repeat_counts(const uschar *p, int *minp, int *maxp, int *errorcodeptr)
  807. {
  808. int min = 0;
  809. int max = -1;
  810. /* Read the minimum value and do a paranoid check: a negative value indicates
  811. an integer overflow. */
  812. while ((digitab[*p] & ctype_digit) != 0) min = min * 10 + *p++ - CHAR_0;
  813. if (min < 0 || min > 65535)
  814. {
  815. *errorcodeptr = ERR5;
  816. return p;
  817. }
  818. /* Read the maximum value if there is one, and again do a paranoid on its size.
  819. Also, max must not be less than min. */
  820. if (*p == CHAR_RIGHT_CURLY_BRACKET) max = min; else
  821. {
  822. if (*(++p) != CHAR_RIGHT_CURLY_BRACKET)
  823. {
  824. max = 0;
  825. while((digitab[*p] & ctype_digit) != 0) max = max * 10 + *p++ - CHAR_0;
  826. if (max < 0 || max > 65535)
  827. {
  828. *errorcodeptr = ERR5;
  829. return p;
  830. }
  831. if (max < min)
  832. {
  833. *errorcodeptr = ERR4;
  834. return p;
  835. }
  836. }
  837. }
  838. /* Fill in the required variables, and pass back the pointer to the terminating
  839. '}'. */
  840. *minp = min;
  841. *maxp = max;
  842. return p;
  843. }
  844. /*************************************************
  845. * Subroutine for finding forward reference *
  846. *************************************************/
  847. /* This recursive function is called only from find_parens() below. The
  848. top-level call starts at the beginning of the pattern. All other calls must
  849. start at a parenthesis. It scans along a pattern's text looking for capturing
  850. subpatterns, and counting them. If it finds a named pattern that matches the
  851. name it is given, it returns its number. Alternatively, if the name is NULL, it
  852. returns when it reaches a given numbered subpattern. We know that if (?P< is
  853. encountered, the name will be terminated by '>' because that is checked in the
  854. first pass. Recursion is used to keep track of subpatterns that reset the
  855. capturing group numbers - the (?| feature.
  856. Arguments:
  857. ptrptr address of the current character pointer (updated)
  858. cd compile background data
  859. name name to seek, or NULL if seeking a numbered subpattern
  860. lorn name length, or subpattern number if name is NULL
  861. xmode TRUE if we are in /x mode
  862. count pointer to the current capturing subpattern number (updated)
  863. Returns: the number of the named subpattern, or -1 if not found
  864. */
  865. static int
  866. find_parens_sub(uschar **ptrptr, compile_data *cd, const uschar *name, int lorn,
  867. BOOL xmode, int *count)
  868. {
  869. uschar *ptr = *ptrptr;
  870. int start_count = *count;
  871. int hwm_count = start_count;
  872. BOOL dup_parens = FALSE;
  873. /* If the first character is a parenthesis, check on the type of group we are
  874. dealing with. The very first call may not start with a parenthesis. */
  875. if (ptr[0] == CHAR_LEFT_PARENTHESIS)
  876. {
  877. if (ptr[1] == CHAR_QUESTION_MARK &&
  878. ptr[2] == CHAR_VERTICAL_LINE)
  879. {
  880. ptr += 3;
  881. dup_parens = TRUE;
  882. }
  883. /* Handle a normal, unnamed capturing parenthesis */
  884. else if (ptr[1] != CHAR_QUESTION_MARK && ptr[1] != CHAR_ASTERISK)
  885. {
  886. *count += 1;
  887. if (name == NULL && *count == lorn) return *count;
  888. ptr++;
  889. }
  890. /* Handle a condition. If it is an assertion, just carry on so that it
  891. is processed as normal. If not, skip to the closing parenthesis of the
  892. condition (there can't be any nested parens. */
  893. else if (ptr[2] == CHAR_LEFT_PARENTHESIS)
  894. {
  895. ptr += 2;
  896. if (ptr[1] != CHAR_QUESTION_MARK)
  897. {
  898. while (*ptr != 0 && *ptr != CHAR_RIGHT_PARENTHESIS) ptr++;
  899. if (*ptr != 0) ptr++;
  900. }
  901. }
  902. /* We have either (? or (* and not a condition */
  903. else
  904. {
  905. ptr += 2;
  906. if (*ptr == CHAR_P) ptr++; /* Allow optional P */
  907. /* We have to disambiguate (?<! and (?<= from (?<name> for named groups */
  908. if ((*ptr == CHAR_LESS_THAN_SIGN && ptr[1] != CHAR_EXCLAMATION_MARK &&
  909. ptr[1] != CHAR_EQUALS_SIGN) || *ptr == CHAR_APOSTROPHE)
  910. {
  911. int term;
  912. const uschar *thisname;
  913. *count += 1;
  914. if (name == NULL && *count == lorn) return *count;
  915. term = *ptr++;
  916. if (term == CHAR_LESS_THAN_SIGN) term = CHAR_GREATER_THAN_SIGN;
  917. thisname = ptr;
  918. while (*ptr != term) ptr++;
  919. if (name != NULL && lorn == ptr - thisname &&
  920. strncmp((const char *)name, (const char *)thisname, lorn) == 0)
  921. return *count;
  922. term++;
  923. }
  924. }
  925. }
  926. /* Past any initial parenthesis handling, scan for parentheses or vertical
  927. bars. */
  928. for (; *ptr != 0; ptr++)
  929. {
  930. /* Skip over backslashed characters and also entire \Q...\E */
  931. if (*ptr == CHAR_BACKSLASH)
  932. {
  933. if (*(++ptr) == 0) goto FAIL_EXIT;
  934. if (*ptr == CHAR_Q) for (;;)
  935. {
  936. while (*(++ptr) != 0 && *ptr != CHAR_BACKSLASH) {};
  937. if (*ptr == 0) goto FAIL_EXIT;
  938. if (*(++ptr) == CHAR_E) break;
  939. }
  940. continue;
  941. }
  942. /* Skip over character classes; this logic must be similar to the way they
  943. are handled for real. If the first character is '^', skip it. Also, if the
  944. first few characters (either before or after ^) are \Q\E or \E we skip them
  945. too. This makes for compatibility with Perl. Note the use of STR macros to
  946. encode "Q\\E" so that it works in UTF-8 on EBCDIC platforms. */
  947. if (*ptr == CHAR_LEFT_SQUARE_BRACKET)
  948. {
  949. BOOL negate_class = FALSE;
  950. for (;;)
  951. {
  952. if (ptr[1] == CHAR_BACKSLASH)
  953. {
  954. if (ptr[2] == CHAR_E)
  955. ptr+= 2;
  956. else if (strncmp((const char *)ptr+2,
  957. STR_Q STR_BACKSLASH STR_E, 3) == 0)
  958. ptr += 4;
  959. else
  960. break;
  961. }
  962. else if (!negate_class && ptr[1] == CHAR_CIRCUMFLEX_ACCENT)
  963. {
  964. negate_class = TRUE;
  965. ptr++;
  966. }
  967. else break;
  968. }
  969. /* If the next character is ']', it is a data character that must be
  970. skipped, except in JavaScript compatibility mode. */
  971. if (ptr[1] == CHAR_RIGHT_SQUARE_BRACKET &&
  972. (cd->external_options & PCRE_JAVASCRIPT_COMPAT) == 0)
  973. ptr++;
  974. while (*(++ptr) != CHAR_RIGHT_SQUARE_BRACKET)
  975. {
  976. if (*ptr == 0) return -1;
  977. if (*ptr == CHAR_BACKSLASH)
  978. {
  979. if (*(++ptr) == 0) goto FAIL_EXIT;
  980. if (*ptr == CHAR_Q) for (;;)
  981. {
  982. while (*(++ptr) != 0 && *ptr != CHAR_BACKSLASH) {};
  983. if (*ptr == 0) goto FAIL_EXIT;
  984. if (*(++ptr) == CHAR_E) break;
  985. }
  986. continue;
  987. }
  988. }
  989. continue;
  990. }
  991. /* Skip comments in /x mode */
  992. if (xmode && *ptr == CHAR_NUMBER_SIGN)
  993. {
  994. while (*(++ptr) != 0 && *ptr != CHAR_NL) {};
  995. if (*ptr == 0) goto FAIL_EXIT;
  996. continue;
  997. }
  998. /* Check for the special metacharacters */
  999. if (*ptr == CHAR_LEFT_PARENTHESIS)
  1000. {
  1001. int rc = find_parens_sub(&ptr, cd, name, lorn, xmode, count);
  1002. if (rc > 0) return rc;
  1003. if (*ptr == 0) goto FAIL_EXIT;
  1004. }
  1005. else if (*ptr == CHAR_RIGHT_PARENTHESIS)
  1006. {
  1007. if (dup_parens && *count < hwm_count) *count = hwm_count;
  1008. *ptrptr = ptr;
  1009. return -1;
  1010. }
  1011. else if (*ptr == CHAR_VERTICAL_LINE && dup_parens)
  1012. {
  1013. if (*count > hwm_count) hwm_count = *count;
  1014. *count = start_count;
  1015. }
  1016. }
  1017. FAIL_EXIT:
  1018. *ptrptr = ptr;
  1019. return -1;
  1020. }
  1021. /*************************************************
  1022. * Find forward referenced subpattern *
  1023. *************************************************/
  1024. /* This function scans along a pattern's text looking for capturing
  1025. subpatterns, and counting them. If it finds a named pattern that matches the
  1026. name it is given, it returns its number. Alternatively, if the name is NULL, it
  1027. returns when it reaches a given numbered subpattern. This is used for forward
  1028. references to subpatterns. We used to be able to start this scan from the
  1029. current compiling point, using the current count value from cd->bracount, and
  1030. do it all in a single loop, but the addition of the possibility of duplicate
  1031. subpattern numbers means that we have to scan from the very start, in order to
  1032. take account of such duplicates, and to use a recursive function to keep track
  1033. of the different types of group.
  1034. Arguments:
  1035. cd compile background data
  1036. name name to seek, or NULL if seeking a numbered subpattern
  1037. lorn name length, or subpattern number if name is NULL
  1038. xmode TRUE if we are in /x mode
  1039. Returns: the number of the found subpattern, or -1 if not found
  1040. */
  1041. static int
  1042. find_parens(compile_data *cd, const uschar *name, int lorn, BOOL xmode)
  1043. {
  1044. uschar *ptr = (uschar *)cd->start_pattern;
  1045. int count = 0;
  1046. int rc;
  1047. /* If the pattern does not start with an opening parenthesis, the first call
  1048. to find_parens_sub() will scan right to the end (if necessary). However, if it
  1049. does start with a parenthesis, find_parens_sub() will return when it hits the
  1050. matching closing parens. That is why we have to have a loop. */
  1051. for (;;)
  1052. {
  1053. rc = find_parens_sub(&ptr, cd, name, lorn, xmode, &count);
  1054. if (rc > 0 || *ptr++ == 0) break;
  1055. }
  1056. return rc;
  1057. }
  1058. /*************************************************
  1059. * Find first significant op code *
  1060. *************************************************/
  1061. /* This is called by several functions that scan a compiled expression looking
  1062. for a fixed first character, or an anchoring op code etc. It skips over things
  1063. that do not influence this. For some calls, a change of option is important.
  1064. For some calls, it makes sense to skip negative forward and all backward
  1065. assertions, and also the \b assertion; for others it does not.
  1066. Arguments:
  1067. code pointer to the start of the group
  1068. options pointer to external options
  1069. optbit the option bit whose changing is significant, or
  1070. zero if none are
  1071. skipassert TRUE if certain assertions are to be skipped
  1072. Returns: pointer to the first significant opcode
  1073. */
  1074. static const uschar*
  1075. first_significant_code(const uschar *code, int *options, int optbit,
  1076. BOOL skipassert)
  1077. {
  1078. for (;;)
  1079. {
  1080. switch ((int)*code)
  1081. {
  1082. case OP_OPT:
  1083. if (optbit > 0 && ((int)code[1] & optbit) != (*options & optbit))
  1084. *options = (int)code[1];
  1085. code += 2;
  1086. break;
  1087. case OP_ASSERT_NOT:
  1088. case OP_ASSERTBACK:
  1089. case OP_ASSERTBACK_NOT:
  1090. if (!skipassert) return code;
  1091. do code += GET(code, 1); while (*code == OP_ALT);
  1092. code += _pcre_OP_lengths[*code];
  1093. break;
  1094. case OP_WORD_BOUNDARY:
  1095. case OP_NOT_WORD_BOUNDARY:
  1096. if (!skipassert) return code;
  1097. /* Fall through */
  1098. case OP_CALLOUT:
  1099. case OP_CREF:
  1100. case OP_NCREF:
  1101. case OP_RREF:
  1102. case OP_NRREF:
  1103. case OP_DEF:
  1104. code += _pcre_OP_lengths[*code];
  1105. break;
  1106. default:
  1107. return code;
  1108. }
  1109. }
  1110. /* Control never reaches here */
  1111. }
  1112. /*************************************************
  1113. * Find the fixed length of a branch *
  1114. *************************************************/
  1115. /* Scan a branch and compute the fixed length of subject that will match it,
  1116. if the length is fixed. This is needed for dealing with backward assertions.
  1117. In UTF8 mode, the result is in characters rather than bytes. The branch is
  1118. temporarily terminated with OP_END when this function is called.
  1119. This function is called when a backward assertion is encountered, so that if it
  1120. fails, the error message can point to the correct place in the pattern.
  1121. However, we cannot do this when the assertion contains subroutine calls,
  1122. because they can be forward references. We solve this by remembering this case
  1123. and doing the check at the end; a flag specifies which mode we are running in.
  1124. Arguments:
  1125. code points to the start of the pattern (the bracket)
  1126. options the compiling options
  1127. atend TRUE if called when the pattern is complete
  1128. cd the "compile data" structure
  1129. Returns: the fixed length,
  1130. or -1 if there is no fixed length,
  1131. or -2 if \C was encountered
  1132. or -3 if an OP_RECURSE item was encountered and atend is FALSE
  1133. */
  1134. static int
  1135. find_fixedlength(uschar *code, int options, BOOL atend, compile_data *cd)
  1136. {
  1137. int length = -1;
  1138. register int branchlength = 0;
  1139. register uschar *cc = code + 1 + LINK_SIZE;
  1140. /* Scan along the opcodes for this branch. If we get to the end of the
  1141. branch, check the length against that of the other branches. */
  1142. for (;;)
  1143. {
  1144. int d;
  1145. uschar *ce, *cs;
  1146. register int op = *cc;
  1147. switch (op)
  1148. {
  1149. case OP_CBRA:
  1150. case OP_BRA:
  1151. case OP_ONCE:
  1152. case OP_COND:
  1153. d = find_fixedlength(cc + ((op == OP_CBRA)? 2:0), options, atend, cd);
  1154. if (d < 0) return d;
  1155. branchlength += d;
  1156. do cc += GET(cc, 1); while (*cc == OP_ALT);
  1157. cc += 1 + LINK_SIZE;
  1158. break;
  1159. /* Reached end of a branch; if it's a ket it is the end of a nested
  1160. call. If it's ALT it is an alternation in a nested call. If it is
  1161. END it's the end of the outer call. All can be handled by the same code. */
  1162. case OP_ALT:
  1163. case OP_KET:
  1164. case OP_KETRMAX:
  1165. case OP_KETRMIN:
  1166. case OP_END:
  1167. if (length < 0) length = branchlength;
  1168. else if (length != branchlength) return -1;
  1169. if (*cc != OP_ALT) return length;
  1170. cc += 1 + LINK_SIZE;
  1171. branchlength = 0;
  1172. break;
  1173. /* A true recursion implies not fixed length, but a subroutine call may
  1174. be OK. If the subroutine is a forward reference, we can't deal with
  1175. it until the end of the pattern, so return -3. */
  1176. case OP_RECURSE:
  1177. if (!atend) return -3;
  1178. cs = ce = (uschar *)cd->start_code + GET(cc, 1); /* Start subpattern */
  1179. do ce += GET(ce, 1); while (*ce == OP_ALT); /* End subpattern */
  1180. if (cc > cs && cc < ce) return -1; /* Recursion */
  1181. d = find_fixedlength(cs + 2, options, atend, cd);
  1182. if (d < 0) return d;
  1183. branchlength += d;
  1184. cc += 1 + LINK_SIZE;
  1185. break;
  1186. /* Skip over assertive subpatterns */
  1187. case OP_ASSERT:
  1188. case OP_ASSERT_NOT:
  1189. case OP_ASSERTBACK:
  1190. case OP_ASSERTBACK_NOT:
  1191. do cc += GET(cc, 1); while (*cc == OP_ALT);
  1192. /* Fall through */
  1193. /* Skip over things that don't match chars */
  1194. case OP_REVERSE:
  1195. case OP_CREF:
  1196. case OP_NCREF:
  1197. case OP_RREF:
  1198. case OP_NRREF:
  1199. case OP_DEF:
  1200. case OP_OPT:
  1201. case OP_CALLOUT:
  1202. case OP_SOD:
  1203. case OP_SOM:
  1204. case OP_EOD:
  1205. case OP_EODN:
  1206. case OP_CIRC:
  1207. case OP_DOLL:
  1208. case OP_NOT_WORD_BOUNDARY:
  1209. case OP_WORD_BOUNDARY:
  1210. cc += _pcre_OP_lengths[*cc];
  1211. break;
  1212. /* Handle literal characters */
  1213. case OP_CHAR:
  1214. case OP_CHARNC:
  1215. case OP_NOT:
  1216. branchlength++;
  1217. cc += 2;
  1218. #ifdef SUPPORT_UTF8
  1219. if ((options & PCRE_UTF8) != 0 && cc[-1] >= 0xc0)
  1220. cc += _pcre_utf8_table4[cc[-1] & 0x3f];
  1221. #endif
  1222. break;
  1223. /* Handle exact repetitions. The count is already in characters, but we
  1224. need to skip over a multibyte character in UTF8 mode. */
  1225. case OP_EXACT:
  1226. branchlength += GET2(cc,1);
  1227. cc += 4;
  1228. #ifdef SUPPORT_UTF8
  1229. if ((options & PCRE_UTF8) != 0 && cc[-1] >= 0xc0)
  1230. cc += _pcre_utf8_table4[cc[-1] & 0x3f];
  1231. #endif
  1232. break;
  1233. case OP_TYPEEXACT:
  1234. branchlength += GET2(cc,1);
  1235. if (cc[3] == OP_PROP || cc[3] == OP_NOTPROP) cc += 2;
  1236. cc += 4;
  1237. break;
  1238. /* Handle single-char matchers */
  1239. case OP_PROP:
  1240. case OP_NOTPROP:
  1241. cc += 2;
  1242. /* Fall through */
  1243. case OP_NOT_DIGIT:
  1244. case OP_DIGIT:
  1245. case OP_NOT_WHITESPACE:
  1246. case OP_WHITESPACE:
  1247. case OP_NOT_WORDCHAR:
  1248. case OP_WORDCHAR:
  1249. case OP_ANY:
  1250. case OP_ALLANY:
  1251. branchlength++;
  1252. cc++;
  1253. break;
  1254. /* The single-byte matcher isn't allowed */
  1255. case OP_ANYBYTE:
  1256. return -2;
  1257. /* Check a class for variable quantification */
  1258. #ifdef SUPPORT_UTF8
  1259. case OP_XCLASS:
  1260. cc += GET(cc, 1) - 33;
  1261. /* Fall through */
  1262. #endif
  1263. case OP_CLASS:
  1264. case OP_NCLASS:
  1265. cc += 33;
  1266. switch (*cc)
  1267. {
  1268. case OP_CRSTAR:
  1269. case OP_CRMINSTAR:
  1270. case OP_CRQUERY:
  1271. case OP_CRMINQUERY:
  1272. return -1;
  1273. case OP_CRRANGE:
  1274. case OP_CRMINRANGE:
  1275. if (GET2(cc,1) != GET2(cc,3)) return -1;
  1276. branchlength += GET2(cc,1);
  1277. cc += 5;
  1278. break;
  1279. default:
  1280. branchlength++;
  1281. }
  1282. break;
  1283. /* Anything else is variable length */
  1284. default:
  1285. return -1;
  1286. }
  1287. }
  1288. /* Control never gets here */
  1289. }
  1290. /*************************************************
  1291. * Scan compiled regex for specific bracket *
  1292. *************************************************/
  1293. /* This little function scans through a compiled pattern until it finds a
  1294. capturing bracket with the given number, or, if the number is negative, an
  1295. instance of OP_REVERSE for a lookbehind. The function is global in the C sense
  1296. so that it can be called from pcre_study() when finding the minimum matching
  1297. length.
  1298. Arguments:
  1299. code points to start of expression
  1300. utf8 TRUE in UTF-8 mode
  1301. number the required bracket number or negative to find a lookbehind
  1302. Returns: pointer to the opcode for the bracket, or NULL if not found
  1303. */
  1304. const uschar *
  1305. _pcre_find_bracket(const uschar *code, BOOL utf8, int number)
  1306. {
  1307. for (;;)
  1308. {
  1309. register int c = *code;
  1310. if (c == OP_END) return NULL;
  1311. /* XCLASS is used for classes that cannot be represented just by a bit
  1312. map. This includes negated single high-valued characters. The length in
  1313. the table is zero; the actual length is stored in the compiled code. */
  1314. if (c == OP_XCLASS) code += GET(code, 1);
  1315. /* Handle recursion */
  1316. else if (c == OP_REVERSE)
  1317. {
  1318. if (number < 0) return (uschar *)code;
  1319. code += _pcre_OP_lengths[c];
  1320. }
  1321. /* Handle capturing bracket */
  1322. else if (c == OP_CBRA)
  1323. {
  1324. int n = GET2(code, 1+LINK_SIZE);
  1325. if (n == number) return (uschar *)code;
  1326. code += _pcre_OP_lengths[c];
  1327. }
  1328. /* Otherwise, we can get the item's length from the table, except that for
  1329. repeated character types, we have to test for \p and \P, which have an extra
  1330. two bytes of parameters. */
  1331. else
  1332. {
  1333. switch(c)
  1334. {
  1335. case OP_TYPESTAR:
  1336. case OP_TYPEMINSTAR:
  1337. case OP_TYPEPLUS:
  1338. case OP_TYPEMINPLUS:
  1339. case OP_TYPEQUERY:
  1340. case OP_TYPEMINQUERY:
  1341. case OP_TYPEPOSSTAR:
  1342. case OP_TYPEPOSPLUS:
  1343. case OP_TYPEPOSQUERY:
  1344. if (code[1] == OP_PROP || code[1] == OP_NOTPROP) code += 2;
  1345. break;
  1346. case OP_TYPEUPTO:
  1347. case OP_TYPEMINUPTO:
  1348. case OP_TYPEEXACT:
  1349. case OP_TYPEPOSUPTO:
  1350. if (code[3] == OP_PROP || code[3] == OP_NOTPROP) code += 2;
  1351. break;
  1352. }
  1353. /* Add in the fixed length from the table */
  1354. code += _pcre_OP_lengths[c];
  1355. /* In UTF-8 mode, opcodes that are followed by a character may be followed by
  1356. a multi-byte character. The length in the table is a minimum, so we have to
  1357. arrange to skip the extra bytes. */
  1358. #ifdef SUPPORT_UTF8
  1359. if (utf8) switch(c)
  1360. {
  1361. case OP_CHAR:
  1362. case OP_CHARNC:
  1363. case OP_EXACT:
  1364. case OP_UPTO:
  1365. case OP_MINUPTO:
  1366. case OP_POSUPTO:
  1367. case OP_STAR:
  1368. case OP_MINSTAR:
  1369. case OP_POSSTAR:
  1370. case OP_PLUS:
  1371. case OP_MINPLUS:
  1372. case OP_POSPLUS:
  1373. case OP_QUERY:
  1374. case OP_MINQUERY:
  1375. case OP_POSQUERY:
  1376. if (code[-1] >= 0xc0) code += _pcre_utf8_table4[code[-1] & 0x3f];
  1377. break;
  1378. }
  1379. #else
  1380. (void)(utf8); /* Keep compiler happy by referencing function argument */
  1381. #endif
  1382. }
  1383. }
  1384. }
  1385. /*************************************************
  1386. * Scan compiled regex for recursion reference *
  1387. *************************************************/
  1388. /* This little function scans through a compiled pattern until it finds an
  1389. instance of OP_RECURSE.
  1390. Arguments:
  1391. code points to start of expression
  1392. utf8 TRUE in UTF-8 mode
  1393. Returns: pointer to the opcode for OP_RECURSE, or NULL if not found
  1394. */
  1395. static const uschar *
  1396. find_recurse(const uschar *code, BOOL utf8)
  1397. {
  1398. for (;;)
  1399. {
  1400. register int c = *code;
  1401. if (c == OP_END) return NULL;
  1402. if (c == OP_RECURSE) return code;
  1403. /* XCLASS is used for classes that cannot be represented just by a bit
  1404. map. This includes negated single high-valued characters. The length in
  1405. the table is zero; the actual length is stored in the compiled code. */
  1406. if (c == OP_XCLASS) code += GET(code, 1);
  1407. /* Otherwise, we can get the item's length from the table, except that for
  1408. repeated character types, we have to test for \p and \P, which have an extra
  1409. two bytes of parameters. */
  1410. else
  1411. {
  1412. switch(c)
  1413. {
  1414. case OP_TYPESTAR:
  1415. case OP_TYPEMINSTAR:
  1416. case OP_TYPEPLUS:
  1417. case OP_TYPEMINPLUS:
  1418. case OP_TYPEQUERY:
  1419. case OP_TYPEMINQUERY:
  1420. case OP_TYPEPOSSTAR:
  1421. case OP_TYPEPOSPLUS:
  1422. case OP_TYPEPOSQUERY:
  1423. if (code[1] == OP_PROP || code[1] == OP_NOTPROP) code += 2;
  1424. break;
  1425. case OP_TYPEPOSUPTO:
  1426. case OP_TYPEUPTO:
  1427. case OP_TYPEMINUPTO:
  1428. case OP_TYPEEXACT:
  1429. if (code[3] == OP_PROP || code[3] == OP_NOTPROP) code += 2;
  1430. break;
  1431. }
  1432. /* Add in the fixed length from the table */
  1433. code += _pcre_OP_lengths[c];
  1434. /* In UTF-8 mode, opcodes that are followed by a character may be followed
  1435. by a multi-byte character. The length in the table is a minimum, so we have
  1436. to arrange to skip the extra bytes. */
  1437. #ifdef SUPPORT_UTF8
  1438. if (utf8) switch(c)
  1439. {
  1440. case OP_CHAR:
  1441. case OP_CHARNC:
  1442. case OP_EXACT:
  1443. case OP_UPTO:
  1444. case OP_MINUPTO:
  1445. case OP_POSUPTO:
  1446. case OP_STAR:
  1447. case OP_MINSTAR:
  1448. case OP_POSSTAR:
  1449. case OP_PLUS:
  1450. case OP_MINPLUS:
  1451. case OP_POSPLUS:
  1452. case OP_QUERY:
  1453. case OP_MINQUERY:
  1454. case OP_POSQUERY:
  1455. if (code[-1] >= 0xc0) code += _pcre_utf8_table4[code[-1] & 0x3f];
  1456. break;
  1457. }
  1458. #else
  1459. (void)(utf8); /* Keep compiler happy by referencing function argument */
  1460. #endif
  1461. }
  1462. }
  1463. }
  1464. /*************************************************
  1465. * Scan compiled branch for non-emptiness *
  1466. *************************************************/
  1467. /* This function scans through a branch of a compiled pattern to see whether it
  1468. can match the empty string or not. It is called from could_be_empty()
  1469. below and from compile_branch() when checking for an unlimited repeat of a
  1470. group that can match nothing. Note that first_significant_code() skips over
  1471. backward and negative forward assertions when its final argument is TRUE. If we
  1472. hit an unclosed bracket, we return "empty" - this means we've struck an inner
  1473. bracket whose current branch will already have been scanned.
  1474. Arguments:
  1475. code points to start of search
  1476. endcode points to where to stop
  1477. utf8 TRUE if in UTF8 mode
  1478. Returns: TRUE if what is matched could be empty
  1479. */
  1480. static BOOL
  1481. could_be_empty_branch(const uschar *code, const uschar *endcode, BOOL utf8)
  1482. {
  1483. register int c;
  1484. for (code = first_significant_code(code + _pcre_OP_lengths[*code], NULL, 0, TRUE);
  1485. code < endcode;
  1486. code = first_significant_code(code + _pcre_OP_lengths[c], NULL, 0, TRUE))
  1487. {
  1488. const uschar *ccode;
  1489. c = *code;
  1490. /* Skip over forward assertions; the other assertions are skipped by
  1491. first_significant_code() with a TRUE final argument. */
  1492. if (c == OP_ASSERT)
  1493. {
  1494. do code += GET(code, 1); while (*code == OP_ALT);
  1495. c = *code;
  1496. continue;
  1497. }
  1498. /* Groups with zero repeats can of course be empty; skip them. */
  1499. if (c == OP_BRAZERO || c == OP_BRAMINZERO || c == OP_SKIPZERO)
  1500. {
  1501. code += _pcre_OP_lengths[c];
  1502. do code += GET(code, 1); while (*code == OP_ALT);
  1503. c = *code;
  1504. continue;
  1505. }
  1506. /* For other groups, scan the branches. */
  1507. if (c == OP_BRA || c == OP_CBRA || c == OP_ONCE || c == OP_COND)
  1508. {
  1509. BOOL empty_branch;
  1510. if (GET(code, 1) == 0) return TRUE; /* Hit unclosed bracket */
  1511. /* If a conditional group has only one branch, there is a second, implied,
  1512. empty branch, so just skip over the conditional, because it could be empty.
  1513. Otherwise, scan the individual branches of the group. */
  1514. if (c == OP_COND && code[GET(code, 1)] != OP_ALT)
  1515. code += GET(code, 1);
  1516. else
  1517. {
  1518. empty_branch = FALSE;
  1519. do
  1520. {
  1521. if (!empty_branch && could_be_empty_branch(code, endcode, utf8))
  1522. empty_branch = TRUE;
  1523. code += GET(code, 1);
  1524. }
  1525. while (*code == OP_ALT);
  1526. if (!empty_branch) return FALSE; /* All branches are non-empty */
  1527. }
  1528. c = *code;
  1529. continue;
  1530. }
  1531. /* Handle the other opcodes */
  1532. switch (c)
  1533. {
  1534. /* Check for quantifiers after a class. XCLASS is used for classes that
  1535. cannot be represented just by a bit map. This includes negated single
  1536. high-valued characters. The length in _pcre_OP_lengths[] is zero; the
  1537. actual length is stored in the compiled code, so we must update "code"
  1538. here. */
  1539. #ifdef SUPPORT_UTF8
  1540. case OP_XCLASS:
  1541. ccode = code += GET(code, 1);
  1542. goto CHECK_CLASS_REPEAT;
  1543. #endif
  1544. case OP_CLASS:
  1545. case OP_NCLASS:
  1546. ccode = code + 33;
  1547. #ifdef SUPPORT_UTF8
  1548. CHECK_CLASS_REPEAT:
  1549. #endif
  1550. switch (*ccode)
  1551. {
  1552. case OP_CRSTAR: /* These could be empty; continue */
  1553. case OP_CRMINSTAR:
  1554. case OP_CRQUERY:
  1555. case OP_CRMINQUERY:
  1556. break;
  1557. default: /* Non-repeat => class must match */
  1558. case OP_CRPLUS: /* These repeats aren't empty */
  1559. case OP_CRMINPLUS:
  1560. return FALSE;
  1561. case OP_CRRANGE:
  1562. case OP_CRMINRANGE:
  1563. if (GET2(ccode, 1) > 0) return FALSE; /* Minimum > 0 */
  1564. break;
  1565. }
  1566. break;
  1567. /* Opcodes that must match a character */
  1568. case OP_PROP:
  1569. case OP_NOTPROP:
  1570. case OP_EXTUNI:
  1571. case OP_NOT_DIGIT:
  1572. case OP_DIGIT:
  1573. case OP_NOT_WHITESPACE:
  1574. case OP_WHITESPACE:
  1575. case OP_NOT_WORDCHAR:
  1576. case OP_WORDCHAR:
  1577. case OP_ANY:
  1578. case OP_ALLANY:
  1579. case OP_ANYBYTE:
  1580. case OP_CHAR:
  1581. case OP_CHARNC:
  1582. case OP_NOT:
  1583. case OP_PLUS:
  1584. case OP_MINPLUS:
  1585. case OP_POSPLUS:
  1586. case OP_EXACT:
  1587. case OP_NOTPLUS:
  1588. case OP_NOTMINPLUS:
  1589. case OP_NOTPOSPLUS:
  1590. case OP_NOTEXACT:
  1591. case OP_TYPEPLUS:
  1592. case OP_TYPEMINPLUS:
  1593. case OP_TYPEPOSPLUS:
  1594. case OP_TYPEEXACT:
  1595. return FALSE;
  1596. /* These are going to continue, as they may be empty, but we have to
  1597. fudge the length for the \p and \P cases. */
  1598. case OP_TYPESTAR:
  1599. case OP_TYPEMINSTAR:
  1600. case OP_TYPEPOSSTAR:
  1601. case OP_TYPEQUERY:
  1602. case OP_TYPEMINQUERY:
  1603. case OP_TYPEPOSQUERY:
  1604. if (code[1] == OP_PROP || code[1] == OP_NOTPROP) code += 2;
  1605. break;
  1606. /* Same for these */
  1607. case OP_TYPEUPTO:
  1608. case OP_TYPEMINUPTO:
  1609. case OP_TYPEPOSUPTO:
  1610. if (code[3] == OP_PROP || code[3] == OP_NOTPROP) code += 2;
  1611. break;
  1612. /* End of branch */
  1613. case OP_KET:
  1614. case OP_KETRMAX:
  1615. case OP_KETRMIN:
  1616. case OP_ALT:
  1617. return TRUE;
  1618. /* In UTF-8 mode, STAR, MINSTAR, POSSTAR, QUERY, MINQUERY, POSQUERY, UPTO,
  1619. MINUPTO, and POSUPTO may be followed by a multibyte character */
  1620. #ifdef SUPPORT_UTF8
  1621. case OP_STAR:
  1622. case OP_MINSTAR:
  1623. case OP_POSSTAR:
  1624. case OP_QUERY:
  1625. case OP_MINQUERY:
  1626. case OP_POSQUERY:
  1627. if (utf8 && code[1] >= 0xc0) code += _pcre_utf8_table4[code[1] & 0x3f];
  1628. break;
  1629. case OP_UPTO:
  1630. case OP_MINUPTO:
  1631. case OP_POSUPTO:
  1632. if (utf8 && code[3] >= 0xc0) code += _pcre_utf8_table4[code[3] & 0x3f];
  1633. break;
  1634. #endif
  1635. }
  1636. }
  1637. return TRUE;
  1638. }
  1639. /*************************************************
  1640. * Scan compiled regex for non-emptiness *
  1641. *************************************************/
  1642. /* This function is called to check for left recursive calls. We want to check
  1643. the current branch of the current pattern to see if it could match the empty
  1644. string. If it could, we must look outwards for branches at other levels,
  1645. stopping when we pass beyond the bracket which is the subject of the recursion.
  1646. Arguments:
  1647. code points to start of the recursion
  1648. endcode points to where to stop (current RECURSE item)
  1649. bcptr points to the chain of current (unclosed) branch starts
  1650. utf8 TRUE if in UTF-8 mode
  1651. Returns: TRUE if what is matched could be empty
  1652. */
  1653. static BOOL
  1654. could_be_empty(const uschar *code, const uschar *endcode, branch_chain *bcptr,
  1655. BOOL utf8)
  1656. {
  1657. while (bcptr != NULL && bcptr->current >= code)
  1658. {
  1659. if (!could_be_empty_branch(bcptr->current, endcode, utf8)) return FALSE;
  1660. bcptr = bcptr->outer;
  1661. }
  1662. return TRUE;
  1663. }
  1664. /*************************************************
  1665. * Check for POSIX class syntax *
  1666. *************************************************/
  1667. /* This function is called when the sequence "[:" or "[." or "[=" is
  1668. encountered in a character class. It checks whether this is followed by a
  1669. sequence of characters terminated by a matching ":]" or ".]" or "=]". If we
  1670. reach an unescaped ']' without the special preceding character, return FALSE.
  1671. Originally, this function only recognized a sequence of letters between the
  1672. terminators, but it seems that Perl recognizes any sequence of characters,
  1673. though of course unknown POSIX names are subsequently rejected. Perl gives an
  1674. "Unknown POSIX class" error for [:f\oo:] for example, where previously PCRE
  1675. didn't consider this to be a POSIX class. Likewise for [:1234:].
  1676. The problem in trying to be exactly like Perl is in the handling of escapes. We
  1677. have to be sure that [abc[:x\]pqr] is *not* treated as containing a POSIX
  1678. class, but [abc[:x\]pqr:]] is (so that an error can be generated). The code
  1679. below handles the special case of \], but does not try to do any other escape
  1680. processing. This makes it different from Perl for cases such as [:l\ower:]
  1681. where Perl recognizes it as the POSIX class "lower" but PCRE does not recognize
  1682. "l\ower". This is a lesser evil that not diagnosing bad classes when Perl does,
  1683. I think.
  1684. Arguments:
  1685. ptr pointer to the initial [
  1686. endptr where to return the end pointer
  1687. Returns: TRUE or FALSE
  1688. */
  1689. static BOOL
  1690. check_posix_syntax(const uschar *ptr, const uschar **endptr)
  1691. {
  1692. int terminator; /* Don't combine these lines; the Solaris cc */
  1693. terminator = *(++ptr); /* compiler warns about "non-constant" initializer. */
  1694. for (++ptr; *ptr != 0; ptr++)
  1695. {
  1696. if (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET) ptr++; else
  1697. {
  1698. if (*ptr == CHAR_RIGHT_SQUARE_BRACKET) return FALSE;
  1699. if (*ptr == terminator && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
  1700. {
  1701. *endptr = ptr;
  1702. return TRUE;
  1703. }
  1704. }
  1705. }
  1706. return FALSE;
  1707. }
  1708. /*************************************************
  1709. * Check POSIX class name *
  1710. *************************************************/
  1711. /* This function is called to check the name given in a POSIX-style class entry
  1712. such as [:alnum:].
  1713. Arguments:
  1714. ptr