PageRenderTime 32ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/harbour/src/3rd/pcre/pcrecomp.c

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