PageRenderTime 62ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/harbour-1.0.0/source/hbpcre/pcrecomp.c

#
C | 2147 lines | 1300 code | 310 blank | 537 comment | 426 complexity | 77a2ead75113a065678eef12a603e3c3 MD5 | raw file
Possible License(s): AGPL-1.0, BSD-3-Clause, CC-BY-SA-3.0, LGPL-3.0, GPL-2.0, LGPL-2.0, LGPL-2.1

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

  1. /*************************************************
  2. * Perl-Compatible Regular Expressions *
  3. *************************************************/
  4. /* PCRE is a library of functions to support regular expressions whose syntax
  5. and semantics are as close as possible to those of the Perl 5 language.
  6. Written by Philip Hazel
  7. Copyright (c) 1997-2008 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. #if 1
  35. #include "_hbconf.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 /* This is the "normal" table for ASCII systems */
  72. static const short int escapes[] = {
  73. 0, 0, 0, 0, 0, 0, 0, 0, /* 0 - 7 */
  74. 0, 0, ':', ';', '<', '=', '>', '?', /* 8 - ? */
  75. '@', -ESC_A, -ESC_B, -ESC_C, -ESC_D, -ESC_E, 0, -ESC_G, /* @ - G */
  76. -ESC_H, 0, 0, -ESC_K, 0, 0, 0, 0, /* H - O */
  77. -ESC_P, -ESC_Q, -ESC_R, -ESC_S, 0, 0, -ESC_V, -ESC_W, /* P - W */
  78. -ESC_X, 0, -ESC_Z, '[', '\\', ']', '^', '_', /* X - _ */
  79. '`', 7, -ESC_b, 0, -ESC_d, ESC_e, ESC_f, 0, /* ` - g */
  80. -ESC_h, 0, 0, -ESC_k, 0, 0, ESC_n, 0, /* h - o */
  81. -ESC_p, 0, ESC_r, -ESC_s, ESC_tee, 0, -ESC_v, -ESC_w, /* p - w */
  82. 0, 0, -ESC_z /* x - z */
  83. };
  84. #else /* This is the "abnormal" table for EBCDIC systems */
  85. static const short int escapes[] = {
  86. /* 48 */ 0, 0, 0, '.', '<', '(', '+', '|',
  87. /* 50 */ '&', 0, 0, 0, 0, 0, 0, 0,
  88. /* 58 */ 0, 0, '!', '$', '*', ')', ';', '~',
  89. /* 60 */ '-', '/', 0, 0, 0, 0, 0, 0,
  90. /* 68 */ 0, 0, '|', ',', '%', '_', '>', '?',
  91. /* 70 */ 0, 0, 0, 0, 0, 0, 0, 0,
  92. /* 78 */ 0, '`', ':', '#', '@', '\'', '=', '"',
  93. /* 80 */ 0, 7, -ESC_b, 0, -ESC_d, ESC_e, ESC_f, 0,
  94. /* 88 */-ESC_h, 0, 0, '{', 0, 0, 0, 0,
  95. /* 90 */ 0, 0, -ESC_k, 'l', 0, ESC_n, 0, -ESC_p,
  96. /* 98 */ 0, ESC_r, 0, '}', 0, 0, 0, 0,
  97. /* A0 */ 0, '~', -ESC_s, ESC_tee, 0,-ESC_v, -ESC_w, 0,
  98. /* A8 */ 0,-ESC_z, 0, 0, 0, '[', 0, 0,
  99. /* B0 */ 0, 0, 0, 0, 0, 0, 0, 0,
  100. /* B8 */ 0, 0, 0, 0, 0, ']', '=', '-',
  101. /* C0 */ '{',-ESC_A, -ESC_B, -ESC_C, -ESC_D,-ESC_E, 0, -ESC_G,
  102. /* C8 */-ESC_H, 0, 0, 0, 0, 0, 0, 0,
  103. /* D0 */ '}', 0, -ESC_K, 0, 0, 0, 0, -ESC_P,
  104. /* D8 */-ESC_Q,-ESC_R, 0, 0, 0, 0, 0, 0,
  105. /* E0 */ '\\', 0, -ESC_S, 0, 0,-ESC_V, -ESC_W, -ESC_X,
  106. /* E8 */ 0,-ESC_Z, 0, 0, 0, 0, 0, 0,
  107. /* F0 */ 0, 0, 0, 0, 0, 0, 0, 0,
  108. /* F8 */ 0, 0, 0, 0, 0, 0, 0, 0
  109. };
  110. #endif
  111. /* Table of special "verbs" like (*PRUNE). This is a short table, so it is
  112. searched linearly. Put all the names into a single string, in order to reduce
  113. the number of relocations when a shared library is dynamically linked. */
  114. typedef struct verbitem {
  115. int len;
  116. int op;
  117. } verbitem;
  118. static const char verbnames[] =
  119. "ACCEPT\0"
  120. "COMMIT\0"
  121. "F\0"
  122. "FAIL\0"
  123. "PRUNE\0"
  124. "SKIP\0"
  125. "THEN";
  126. static const verbitem verbs[] = {
  127. { 6, OP_ACCEPT },
  128. { 6, OP_COMMIT },
  129. { 1, OP_FAIL },
  130. { 4, OP_FAIL },
  131. { 5, OP_PRUNE },
  132. { 4, OP_SKIP },
  133. { 4, OP_THEN }
  134. };
  135. static const int verbcount = sizeof(verbs)/sizeof(verbitem);
  136. /* Tables of names of POSIX character classes and their lengths. The names are
  137. now all in a single string, to reduce the number of relocations when a shared
  138. library is dynamically loaded. The list of lengths is terminated by a zero
  139. length entry. The first three must be alpha, lower, upper, as this is assumed
  140. for handling case independence. */
  141. static const char posix_names[] =
  142. "alpha\0" "lower\0" "upper\0" "alnum\0" "ascii\0" "blank\0"
  143. "cntrl\0" "digit\0" "graph\0" "print\0" "punct\0" "space\0"
  144. "word\0" "xdigit";
  145. static const uschar posix_name_lengths[] = {
  146. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 6, 0 };
  147. /* Table of class bit maps for each POSIX class. Each class is formed from a
  148. base map, with an optional addition or removal of another map. Then, for some
  149. classes, there is some additional tweaking: for [:blank:] the vertical space
  150. characters are removed, and for [:alpha:] and [:alnum:] the underscore
  151. character is removed. The triples in the table consist of the base map offset,
  152. second map offset or -1 if no second map, and a non-negative value for map
  153. addition or a negative value for map subtraction (if there are two maps). The
  154. absolute value of the third field has these meanings: 0 => no tweaking, 1 =>
  155. remove vertical space characters, 2 => remove underscore. */
  156. static const int posix_class_maps[] = {
  157. cbit_word, cbit_digit, -2, /* alpha */
  158. cbit_lower, -1, 0, /* lower */
  159. cbit_upper, -1, 0, /* upper */
  160. cbit_word, -1, 2, /* alnum - word without underscore */
  161. cbit_print, cbit_cntrl, 0, /* ascii */
  162. cbit_space, -1, 1, /* blank - a GNU extension */
  163. cbit_cntrl, -1, 0, /* cntrl */
  164. cbit_digit, -1, 0, /* digit */
  165. cbit_graph, -1, 0, /* graph */
  166. cbit_print, -1, 0, /* print */
  167. cbit_punct, -1, 0, /* punct */
  168. cbit_space, -1, 0, /* space */
  169. cbit_word, -1, 0, /* word - a Perl extension */
  170. cbit_xdigit,-1, 0 /* xdigit */
  171. };
  172. #define STRING(a) # a
  173. #define XSTRING(s) STRING(s)
  174. /* The texts of compile-time error messages. These are "char *" because they
  175. are passed to the outside world. Do not ever re-use any error number, because
  176. they are documented. Always add a new error instead. Messages marked DEAD below
  177. are no longer used. This used to be a table of strings, but in order to reduce
  178. the number of relocations needed when a shared library is loaded dynamically,
  179. it is now one long string. We cannot use a table of offsets, because the
  180. lengths of inserts such as XSTRING(MAX_NAME_SIZE) are not known. Instead, we
  181. simply count through to the one we want - this isn't a performance issue
  182. because these strings are used only when there is a compilation error. */
  183. static const char error_texts[] =
  184. "no error\0"
  185. "\\ at end of pattern\0"
  186. "\\c at end of pattern\0"
  187. "unrecognized character follows \\\0"
  188. "numbers out of order in {} quantifier\0"
  189. /* 5 */
  190. "number too big in {} quantifier\0"
  191. "missing terminating ] for character class\0"
  192. "invalid escape sequence in character class\0"
  193. "range out of order in character class\0"
  194. "nothing to repeat\0"
  195. /* 10 */
  196. "operand of unlimited repeat could match the empty string\0" /** DEAD **/
  197. "internal error: unexpected repeat\0"
  198. "unrecognized character after (? or (?-\0"
  199. "POSIX named classes are supported only within a class\0"
  200. "missing )\0"
  201. /* 15 */
  202. "reference to non-existent subpattern\0"
  203. "erroffset passed as NULL\0"
  204. "unknown option bit(s) set\0"
  205. "missing ) after comment\0"
  206. "parentheses nested too deeply\0" /** DEAD **/
  207. /* 20 */
  208. "regular expression is too large\0"
  209. "failed to get memory\0"
  210. "unmatched parentheses\0"
  211. "internal error: code overflow\0"
  212. "unrecognized character after (?<\0"
  213. /* 25 */
  214. "lookbehind assertion is not fixed length\0"
  215. "malformed number or name after (?(\0"
  216. "conditional group contains more than two branches\0"
  217. "assertion expected after (?(\0"
  218. "(?R or (?[+-]digits must be followed by )\0"
  219. /* 30 */
  220. "unknown POSIX class name\0"
  221. "POSIX collating elements are not supported\0"
  222. "this version of PCRE is not compiled with PCRE_UTF8 support\0"
  223. "spare error\0" /** DEAD **/
  224. "character value in \\x{...} sequence is too large\0"
  225. /* 35 */
  226. "invalid condition (?(0)\0"
  227. "\\C not allowed in lookbehind assertion\0"
  228. "PCRE does not support \\L, \\l, \\N, \\U, or \\u\0"
  229. "number after (?C is > 255\0"
  230. "closing ) for (?C expected\0"
  231. /* 40 */
  232. "recursive call could loop indefinitely\0"
  233. "unrecognized character after (?P\0"
  234. "syntax error in subpattern name (missing terminator)\0"
  235. "two named subpatterns have the same name\0"
  236. "invalid UTF-8 string\0"
  237. /* 45 */
  238. "support for \\P, \\p, and \\X has not been compiled\0"
  239. "malformed \\P or \\p sequence\0"
  240. "unknown property name after \\P or \\p\0"
  241. "subpattern name is too long (maximum " XSTRING(MAX_NAME_SIZE) " characters)\0"
  242. "too many named subpatterns (maximum " XSTRING(MAX_NAME_COUNT) ")\0"
  243. /* 50 */
  244. "repeated subpattern is too long\0" /** DEAD **/
  245. "octal value is greater than \\377 (not in UTF-8 mode)\0"
  246. "internal error: overran compiling workspace\0"
  247. "internal error: previously-checked referenced subpattern not found\0"
  248. "DEFINE group contains more than one branch\0"
  249. /* 55 */
  250. "repeating a DEFINE group is not allowed\0"
  251. "inconsistent NEWLINE options\0"
  252. "\\g is not followed by a braced, angle-bracketed, or quoted name/number or by a plain number\0"
  253. "a numbered reference must not be zero\0"
  254. "(*VERB) with an argument is not supported\0"
  255. /* 60 */
  256. "(*VERB) not recognized\0"
  257. "number is too big\0"
  258. "subpattern name expected\0"
  259. "digit expected after (?+\0"
  260. "] is an invalid data character in JavaScript compatibility mode";
  261. /* Table to identify digits and hex digits. This is used when compiling
  262. patterns. Note that the tables in chartables are dependent on the locale, and
  263. may mark arbitrary characters as digits - but the PCRE compiling code expects
  264. to handle only 0-9, a-z, and A-Z as digits when compiling. That is why we have
  265. a private table here. It costs 256 bytes, but it is a lot faster than doing
  266. character value tests (at least in some simple cases I timed), and in some
  267. applications one wants PCRE to compile efficiently as well as match
  268. efficiently.
  269. For convenience, we use the same bit definitions as in chartables:
  270. 0x04 decimal digit
  271. 0x08 hexadecimal digit
  272. Then we can use ctype_digit and ctype_xdigit in the code. */
  273. #ifndef EBCDIC /* This is the "normal" case, for ASCII systems */
  274. static const unsigned char digitab[] =
  275. {
  276. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 0- 7 */
  277. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 8- 15 */
  278. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 16- 23 */
  279. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 24- 31 */
  280. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* - ' */
  281. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* ( - / */
  282. 0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c, /* 0 - 7 */
  283. 0x0c,0x0c,0x00,0x00,0x00,0x00,0x00,0x00, /* 8 - ? */
  284. 0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x00, /* @ - G */
  285. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* H - O */
  286. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* P - W */
  287. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* X - _ */
  288. 0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x00, /* ` - g */
  289. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* h - o */
  290. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* p - w */
  291. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* x -127 */
  292. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 128-135 */
  293. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 136-143 */
  294. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 144-151 */
  295. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 152-159 */
  296. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 160-167 */
  297. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 168-175 */
  298. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 176-183 */
  299. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 184-191 */
  300. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 192-199 */
  301. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 200-207 */
  302. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 208-215 */
  303. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 216-223 */
  304. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 224-231 */
  305. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 232-239 */
  306. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 240-247 */
  307. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};/* 248-255 */
  308. #else /* This is the "abnormal" case, for EBCDIC systems */
  309. static const unsigned char digitab[] =
  310. {
  311. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 0- 7 0 */
  312. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 8- 15 */
  313. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 16- 23 10 */
  314. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 24- 31 */
  315. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 32- 39 20 */
  316. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 40- 47 */
  317. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 48- 55 30 */
  318. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 56- 63 */
  319. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* - 71 40 */
  320. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 72- | */
  321. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* & - 87 50 */
  322. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 88- 95 */
  323. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* - -103 60 */
  324. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 104- ? */
  325. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 112-119 70 */
  326. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 120- " */
  327. 0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x00, /* 128- g 80 */
  328. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* h -143 */
  329. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 144- p 90 */
  330. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* q -159 */
  331. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 160- x A0 */
  332. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* y -175 */
  333. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* ^ -183 B0 */
  334. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 184-191 */
  335. 0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x00, /* { - G C0 */
  336. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* H -207 */
  337. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* } - P D0 */
  338. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* Q -223 */
  339. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* \ - X E0 */
  340. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* Y -239 */
  341. 0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c, /* 0 - 7 F0 */
  342. 0x0c,0x0c,0x00,0x00,0x00,0x00,0x00,0x00};/* 8 -255 */
  343. static const unsigned char ebcdic_chartab[] = { /* chartable partial dup */
  344. 0x80,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /* 0- 7 */
  345. 0x00,0x00,0x00,0x00,0x01,0x01,0x00,0x00, /* 8- 15 */
  346. 0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /* 16- 23 */
  347. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 24- 31 */
  348. 0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /* 32- 39 */
  349. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 40- 47 */
  350. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 48- 55 */
  351. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 56- 63 */
  352. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* - 71 */
  353. 0x00,0x00,0x00,0x80,0x00,0x80,0x80,0x80, /* 72- | */
  354. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* & - 87 */
  355. 0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00, /* 88- 95 */
  356. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* - -103 */
  357. 0x00,0x00,0x00,0x00,0x00,0x10,0x00,0x80, /* 104- ? */
  358. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 112-119 */
  359. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 120- " */
  360. 0x00,0x1a,0x1a,0x1a,0x1a,0x1a,0x1a,0x12, /* 128- g */
  361. 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* h -143 */
  362. 0x00,0x12,0x12,0x12,0x12,0x12,0x12,0x12, /* 144- p */
  363. 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* q -159 */
  364. 0x00,0x00,0x12,0x12,0x12,0x12,0x12,0x12, /* 160- x */
  365. 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* y -175 */
  366. 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* ^ -183 */
  367. 0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x00, /* 184-191 */
  368. 0x80,0x1a,0x1a,0x1a,0x1a,0x1a,0x1a,0x12, /* { - G */
  369. 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* H -207 */
  370. 0x00,0x12,0x12,0x12,0x12,0x12,0x12,0x12, /* } - P */
  371. 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* Q -223 */
  372. 0x00,0x00,0x12,0x12,0x12,0x12,0x12,0x12, /* \ - X */
  373. 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* Y -239 */
  374. 0x1c,0x1c,0x1c,0x1c,0x1c,0x1c,0x1c,0x1c, /* 0 - 7 */
  375. 0x1c,0x1c,0x00,0x00,0x00,0x00,0x00,0x00};/* 8 -255 */
  376. #endif
  377. /* Definition to allow mutual recursion */
  378. static BOOL
  379. compile_regex(int, int, uschar **, const uschar **, int *, BOOL, BOOL, int,
  380. int *, int *, branch_chain *, compile_data *, int *);
  381. /*************************************************
  382. * Find an error text *
  383. *************************************************/
  384. /* The error texts are now all in one long string, to save on relocations. As
  385. some of the text is of unknown length, we can't use a table of offsets.
  386. Instead, just count through the strings. This is not a performance issue
  387. because it happens only when there has been a compilation error.
  388. Argument: the error number
  389. Returns: pointer to the error string
  390. */
  391. static const char *
  392. find_error_text(int n)
  393. {
  394. const char *s = error_texts;
  395. for (; n > 0; n--) while (*s++ != 0) {};
  396. return s;
  397. }
  398. /*************************************************
  399. * Handle escapes *
  400. *************************************************/
  401. /* This function is called when a \ has been encountered. It either returns a
  402. positive value for a simple escape such as \n, or a negative value which
  403. encodes one of the more complicated things such as \d. A backreference to group
  404. n is returned as -(ESC_REF + n); ESC_REF is the highest ESC_xxx macro. When
  405. UTF-8 is enabled, a positive value greater than 255 may be returned. On entry,
  406. ptr is pointing at the \. On exit, it is on the final character of the escape
  407. sequence.
  408. Arguments:
  409. ptrptr points to the pattern position pointer
  410. errorcodeptr points to the errorcode variable
  411. bracount number of previous extracting brackets
  412. options the options bits
  413. isclass TRUE if inside a character class
  414. Returns: zero or positive => a data character
  415. negative => a special escape sequence
  416. on error, errorcodeptr is set
  417. */
  418. static int
  419. check_escape(const uschar **ptrptr, int *errorcodeptr, int bracount,
  420. int options, BOOL isclass)
  421. {
  422. BOOL utf8 = (options & PCRE_UTF8) != 0;
  423. const uschar *ptr = *ptrptr + 1;
  424. int c, i;
  425. GETCHARINCTEST(c, ptr); /* Get character value, increment pointer */
  426. ptr--; /* Set pointer back to the last byte */
  427. /* If backslash is at the end of the pattern, it's an error. */
  428. if (c == 0) *errorcodeptr = ERR1;
  429. /* Non-alphanumerics are literals. For digits or letters, do an initial lookup
  430. in a table. A non-zero result is something that can be returned immediately.
  431. Otherwise further processing may be required. */
  432. #ifndef EBCDIC /* ASCII coding */
  433. else if (c < '0' || c > 'z') {} /* Not alphanumeric */
  434. else if ((i = escapes[c - '0']) != 0) c = i;
  435. #else /* EBCDIC coding */
  436. else if (c < 'a' || (ebcdic_chartab[c] & 0x0E) == 0) {} /* Not alphanumeric */
  437. else if ((i = escapes[c - 0x48]) != 0) c = i;
  438. #endif
  439. /* Escapes that need further processing, or are illegal. */
  440. else
  441. {
  442. const uschar *oldptr;
  443. BOOL braced, negated;
  444. switch (c)
  445. {
  446. /* A number of Perl escapes are not handled by PCRE. We give an explicit
  447. error. */
  448. case 'l':
  449. case 'L':
  450. case 'N':
  451. case 'u':
  452. case 'U':
  453. *errorcodeptr = ERR37;
  454. break;
  455. /* \g must be followed by one of a number of specific things:
  456. (1) A number, either plain or braced. If positive, it is an absolute
  457. backreference. If negative, it is a relative backreference. This is a Perl
  458. 5.10 feature.
  459. (2) Perl 5.10 also supports \g{name} as a reference to a named group. This
  460. is part of Perl's movement towards a unified syntax for back references. As
  461. this is synonymous with \k{name}, we fudge it up by pretending it really
  462. was \k.
  463. (3) For Oniguruma compatibility we also support \g followed by a name or a
  464. number either in angle brackets or in single quotes. However, these are
  465. (possibly recursive) subroutine calls, _not_ backreferences. Just return
  466. the -ESC_g code (cf \k). */
  467. case 'g':
  468. if (ptr[1] == '<' || ptr[1] == '\'')
  469. {
  470. c = -ESC_g;
  471. break;
  472. }
  473. /* Handle the Perl-compatible cases */
  474. if (ptr[1] == '{')
  475. {
  476. const uschar *p;
  477. for (p = ptr+2; *p != 0 && *p != '}'; p++)
  478. if (*p != '-' && (digitab[*p] & ctype_digit) == 0) break;
  479. if (*p != 0 && *p != '}')
  480. {
  481. c = -ESC_k;
  482. break;
  483. }
  484. braced = TRUE;
  485. ptr++;
  486. }
  487. else braced = FALSE;
  488. if (ptr[1] == '-')
  489. {
  490. negated = TRUE;
  491. ptr++;
  492. }
  493. else negated = FALSE;
  494. c = 0;
  495. while ((digitab[ptr[1]] & ctype_digit) != 0)
  496. c = c * 10 + *(++ptr) - '0';
  497. if (c < 0) /* Integer overflow */
  498. {
  499. *errorcodeptr = ERR61;
  500. break;
  501. }
  502. if (braced && *(++ptr) != '}')
  503. {
  504. *errorcodeptr = ERR57;
  505. break;
  506. }
  507. if (c == 0)
  508. {
  509. *errorcodeptr = ERR58;
  510. break;
  511. }
  512. if (negated)
  513. {
  514. if (c > bracount)
  515. {
  516. *errorcodeptr = ERR15;
  517. break;
  518. }
  519. c = bracount - (c - 1);
  520. }
  521. c = -(ESC_REF + c);
  522. break;
  523. /* The handling of escape sequences consisting of a string of digits
  524. starting with one that is not zero is not straightforward. By experiment,
  525. the way Perl works seems to be as follows:
  526. Outside a character class, the digits are read as a decimal number. If the
  527. number is less than 10, or if there are that many previous extracting
  528. left brackets, then it is a back reference. Otherwise, up to three octal
  529. digits are read to form an escaped byte. Thus \123 is likely to be octal
  530. 123 (cf \0123, which is octal 012 followed by the literal 3). If the octal
  531. value is greater than 377, the least significant 8 bits are taken. Inside a
  532. character class, \ followed by a digit is always an octal number. */
  533. case '1': case '2': case '3': case '4': case '5':
  534. case '6': case '7': case '8': case '9':
  535. if (!isclass)
  536. {
  537. oldptr = ptr;
  538. c -= '0';
  539. while ((digitab[ptr[1]] & ctype_digit) != 0)
  540. c = c * 10 + *(++ptr) - '0';
  541. if (c < 0) /* Integer overflow */
  542. {
  543. *errorcodeptr = ERR61;
  544. break;
  545. }
  546. if (c < 10 || c <= bracount)
  547. {
  548. c = -(ESC_REF + c);
  549. break;
  550. }
  551. ptr = oldptr; /* Put the pointer back and fall through */
  552. }
  553. /* Handle an octal number following \. If the first digit is 8 or 9, Perl
  554. generates a binary zero byte and treats the digit as a following literal.
  555. Thus we have to pull back the pointer by one. */
  556. if ((c = *ptr) >= '8')
  557. {
  558. ptr--;
  559. c = 0;
  560. break;
  561. }
  562. /* \0 always starts an octal number, but we may drop through to here with a
  563. larger first octal digit. The original code used just to take the least
  564. significant 8 bits of octal numbers (I think this is what early Perls used
  565. to do). Nowadays we allow for larger numbers in UTF-8 mode, but no more
  566. than 3 octal digits. */
  567. case '0':
  568. c -= '0';
  569. while(i++ < 2 && ptr[1] >= '0' && ptr[1] <= '7')
  570. c = c * 8 + *(++ptr) - '0';
  571. if (!utf8 && c > 255) *errorcodeptr = ERR51;
  572. break;
  573. /* \x is complicated. \x{ddd} is a character number which can be greater
  574. than 0xff in utf8 mode, but only if the ddd are hex digits. If not, { is
  575. treated as a data character. */
  576. case 'x':
  577. if (ptr[1] == '{')
  578. {
  579. const uschar *pt = ptr + 2;
  580. int count = 0;
  581. c = 0;
  582. while ((digitab[*pt] & ctype_xdigit) != 0)
  583. {
  584. register int cc = *pt++;
  585. if (c == 0 && cc == '0') continue; /* Leading zeroes */
  586. count++;
  587. #ifndef EBCDIC /* ASCII coding */
  588. if (cc >= 'a') cc -= 32; /* Convert to upper case */
  589. c = (c << 4) + cc - ((cc < 'A')? '0' : ('A' - 10));
  590. #else /* EBCDIC coding */
  591. if (cc >= 'a' && cc <= 'z') cc += 64; /* Convert to upper case */
  592. c = (c << 4) + cc - ((cc >= '0')? '0' : ('A' - 10));
  593. #endif
  594. }
  595. if (*pt == '}')
  596. {
  597. if (c < 0 || count > (utf8? 8 : 2)) *errorcodeptr = ERR34;
  598. ptr = pt;
  599. break;
  600. }
  601. /* If the sequence of hex digits does not end with '}', then we don't
  602. recognize this construct; fall through to the normal \x handling. */
  603. }
  604. /* Read just a single-byte hex-defined char */
  605. c = 0;
  606. while (i++ < 2 && (digitab[ptr[1]] & ctype_xdigit) != 0)
  607. {
  608. int cc; /* Some compilers don't like ++ */
  609. cc = *(++ptr); /* in initializers */
  610. #ifndef EBCDIC /* ASCII coding */
  611. if (cc >= 'a') cc -= 32; /* Convert to upper case */
  612. c = c * 16 + cc - ((cc < 'A')? '0' : ('A' - 10));
  613. #else /* EBCDIC coding */
  614. if (cc <= 'z') cc += 64; /* Convert to upper case */
  615. c = c * 16 + cc - ((cc >= '0')? '0' : ('A' - 10));
  616. #endif
  617. }
  618. break;
  619. /* For \c, a following letter is upper-cased; then the 0x40 bit is flipped.
  620. This coding is ASCII-specific, but then the whole concept of \cx is
  621. ASCII-specific. (However, an EBCDIC equivalent has now been added.) */
  622. case 'c':
  623. c = *(++ptr);
  624. if (c == 0)
  625. {
  626. *errorcodeptr = ERR2;
  627. break;
  628. }
  629. #ifndef EBCDIC /* ASCII coding */
  630. if (c >= 'a' && c <= 'z') c -= 32;
  631. c ^= 0x40;
  632. #else /* EBCDIC coding */
  633. if (c >= 'a' && c <= 'z') c += 64;
  634. c ^= 0xC0;
  635. #endif
  636. break;
  637. /* PCRE_EXTRA enables extensions to Perl in the matter of escapes. Any
  638. other alphanumeric following \ is an error if PCRE_EXTRA was set;
  639. otherwise, for Perl compatibility, it is a literal. This code looks a bit
  640. odd, but there used to be some cases other than the default, and there may
  641. be again in future, so I haven't "optimized" it. */
  642. default:
  643. if ((options & PCRE_EXTRA) != 0) switch(c)
  644. {
  645. default:
  646. *errorcodeptr = ERR3;
  647. break;
  648. }
  649. break;
  650. }
  651. }
  652. *ptrptr = ptr;
  653. return c;
  654. }
  655. #ifdef SUPPORT_UCP
  656. /*************************************************
  657. * Handle \P and \p *
  658. *************************************************/
  659. /* This function is called after \P or \p has been encountered, provided that
  660. PCRE is compiled with support for Unicode properties. On entry, ptrptr is
  661. pointing at the P or p. On exit, it is pointing at the final character of the
  662. escape sequence.
  663. Argument:
  664. ptrptr points to the pattern position pointer
  665. negptr points to a boolean that is set TRUE for negation else FALSE
  666. dptr points to an int that is set to the detailed property value
  667. errorcodeptr points to the error code variable
  668. Returns: type value from ucp_type_table, or -1 for an invalid type
  669. */
  670. static int
  671. get_ucp(const uschar **ptrptr, BOOL *negptr, int *dptr, int *errorcodeptr)
  672. {
  673. int c, i, bot, top;
  674. const uschar *ptr = *ptrptr;
  675. char name[32];
  676. c = *(++ptr);
  677. if (c == 0) goto ERROR_RETURN;
  678. *negptr = FALSE;
  679. /* \P or \p can be followed by a name in {}, optionally preceded by ^ for
  680. negation. */
  681. if (c == '{')
  682. {
  683. if (ptr[1] == '^')
  684. {
  685. *negptr = TRUE;
  686. ptr++;
  687. }
  688. for (i = 0; i < (int)sizeof(name) - 1; i++)
  689. {
  690. c = *(++ptr);
  691. if (c == 0) goto ERROR_RETURN;
  692. if (c == '}') break;
  693. name[i] = c;
  694. }
  695. if (c !='}') goto ERROR_RETURN;
  696. name[i] = 0;
  697. }
  698. /* Otherwise there is just one following character */
  699. else
  700. {
  701. name[0] = c;
  702. name[1] = 0;
  703. }
  704. *ptrptr = ptr;
  705. /* Search for a recognized property name using binary chop */
  706. bot = 0;
  707. top = _pcre_utt_size;
  708. while (bot < top)
  709. {
  710. i = (bot + top) >> 1;
  711. c = strcmp(name, _pcre_utt_names + _pcre_utt[i].name_offset);
  712. if (c == 0)
  713. {
  714. *dptr = _pcre_utt[i].value;
  715. return _pcre_utt[i].type;
  716. }
  717. if (c > 0) bot = i + 1; else top = i;
  718. }
  719. *errorcodeptr = ERR47;
  720. *ptrptr = ptr;
  721. return -1;
  722. ERROR_RETURN:
  723. *errorcodeptr = ERR46;
  724. *ptrptr = ptr;
  725. return -1;
  726. }
  727. #endif
  728. /*************************************************
  729. * Check for counted repeat *
  730. *************************************************/
  731. /* This function is called when a '{' is encountered in a place where it might
  732. start a quantifier. It looks ahead to see if it really is a quantifier or not.
  733. It is only a quantifier if it is one of the forms {ddd} {ddd,} or {ddd,ddd}
  734. where the ddds are digits.
  735. Arguments:
  736. p pointer to the first char after '{'
  737. Returns: TRUE or FALSE
  738. */
  739. static BOOL
  740. is_counted_repeat(const uschar *p)
  741. {
  742. if ((digitab[*p++] & ctype_digit) == 0) return FALSE;
  743. while ((digitab[*p] & ctype_digit) != 0) p++;
  744. if (*p == '}') return TRUE;
  745. if (*p++ != ',') return FALSE;
  746. if (*p == '}') return TRUE;
  747. if ((digitab[*p++] & ctype_digit) == 0) return FALSE;
  748. while ((digitab[*p] & ctype_digit) != 0) p++;
  749. return (*p == '}');
  750. }
  751. /*************************************************
  752. * Read repeat counts *
  753. *************************************************/
  754. /* Read an item of the form {n,m} and return the values. This is called only
  755. after is_counted_repeat() has confirmed that a repeat-count quantifier exists,
  756. so the syntax is guaranteed to be correct, but we need to check the values.
  757. Arguments:
  758. p pointer to first char after '{'
  759. minp pointer to int for min
  760. maxp pointer to int for max
  761. returned as -1 if no max
  762. errorcodeptr points to error code variable
  763. Returns: pointer to '}' on success;
  764. current ptr on error, with errorcodeptr set non-zero
  765. */
  766. static const uschar *
  767. read_repeat_counts(const uschar *p, int *minp, int *maxp, int *errorcodeptr)
  768. {
  769. int min = 0;
  770. int max = -1;
  771. /* Read the minimum value and do a paranoid check: a negative value indicates
  772. an integer overflow. */
  773. while ((digitab[*p] & ctype_digit) != 0) min = min * 10 + *p++ - '0';
  774. if (min < 0 || min > 65535)
  775. {
  776. *errorcodeptr = ERR5;
  777. return p;
  778. }
  779. /* Read the maximum value if there is one, and again do a paranoid on its size.
  780. Also, max must not be less than min. */
  781. if (*p == '}') max = min; else
  782. {
  783. if (*(++p) != '}')
  784. {
  785. max = 0;
  786. while((digitab[*p] & ctype_digit) != 0) max = max * 10 + *p++ - '0';
  787. if (max < 0 || max > 65535)
  788. {
  789. *errorcodeptr = ERR5;
  790. return p;
  791. }
  792. if (max < min)
  793. {
  794. *errorcodeptr = ERR4;
  795. return p;
  796. }
  797. }
  798. }
  799. /* Fill in the required variables, and pass back the pointer to the terminating
  800. '}'. */
  801. *minp = min;
  802. *maxp = max;
  803. return p;
  804. }
  805. /*************************************************
  806. * Find forward referenced subpattern *
  807. *************************************************/
  808. /* This function scans along a pattern's text looking for capturing
  809. subpatterns, and counting them. If it finds a named pattern that matches the
  810. name it is given, it returns its number. Alternatively, if the name is NULL, it
  811. returns when it reaches a given numbered subpattern. This is used for forward
  812. references to subpatterns. We know that if (?P< is encountered, the name will
  813. be terminated by '>' because that is checked in the first pass.
  814. Arguments:
  815. ptr current position in the pattern
  816. cd compile background data
  817. name name to seek, or NULL if seeking a numbered subpattern
  818. lorn name length, or subpattern number if name is NULL
  819. xmode TRUE if we are in /x mode
  820. Returns: the number of the named subpattern, or -1 if not found
  821. */
  822. static int
  823. find_parens(const uschar *ptr, compile_data *cd, const uschar *name, int lorn,
  824. BOOL xmode)
  825. {
  826. const uschar *thisname;
  827. int count = cd->bracount;
  828. for (; *ptr != 0; ptr++)
  829. {
  830. int term;
  831. /* Skip over backslashed characters and also entire \Q...\E */
  832. if (*ptr == '\\')
  833. {
  834. if (*(++ptr) == 0) return -1;
  835. if (*ptr == 'Q') for (;;)
  836. {
  837. while (*(++ptr) != 0 && *ptr != '\\') {};
  838. if (*ptr == 0) return -1;
  839. if (*(++ptr) == 'E') break;
  840. }
  841. continue;
  842. }
  843. /* Skip over character classes; this logic must be similar to the way they
  844. are handled for real. If the first character is '^', skip it. Also, if the
  845. first few characters (either before or after ^) are \Q\E or \E we skip them
  846. too. This makes for compatibility with Perl. */
  847. if (*ptr == '[')
  848. {
  849. BOOL negate_class = FALSE;
  850. for (;;)
  851. {
  852. int c = *(++ptr);
  853. if (c == '\\')
  854. {
  855. if (ptr[1] == 'E') ptr++;
  856. else if (strncmp((const char *)ptr+1, "Q\\E", 3) == 0) ptr += 3;
  857. else break;
  858. }
  859. else if (!negate_class && c == '^')
  860. negate_class = TRUE;
  861. else break;
  862. }
  863. /* If the next character is ']', it is a data character that must be
  864. skipped, except in JavaScript compatibility mode. */
  865. if (ptr[1] == ']' && (cd->external_options & PCRE_JAVASCRIPT_COMPAT) == 0)
  866. ptr++;
  867. while (*(++ptr) != ']')
  868. {
  869. if (*ptr == 0) return -1;
  870. if (*ptr == '\\')
  871. {
  872. if (*(++ptr) == 0) return -1;
  873. if (*ptr == 'Q') for (;;)
  874. {
  875. while (*(++ptr) != 0 && *ptr != '\\') {};
  876. if (*ptr == 0) return -1;
  877. if (*(++ptr) == 'E') break;
  878. }
  879. continue;
  880. }
  881. }
  882. continue;
  883. }
  884. /* Skip comments in /x mode */
  885. if (xmode && *ptr == '#')
  886. {
  887. while (*(++ptr) != 0 && *ptr != '\n') {};
  888. if (*ptr == 0) return -1;
  889. continue;
  890. }
  891. /* An opening parens must now be a real metacharacter */
  892. if (*ptr != '(') continue;
  893. if (ptr[1] != '?' && ptr[1] != '*')
  894. {
  895. count++;
  896. if (name == NULL && count == lorn) return count;
  897. continue;
  898. }
  899. ptr += 2;
  900. if (*ptr == 'P') ptr++; /* Allow optional P */
  901. /* We have to disambiguate (?<! and (?<= from (?<name> */
  902. if ((*ptr != '<' || ptr[1] == '!' || ptr[1] == '=') &&
  903. *ptr != '\'')
  904. continue;
  905. count++;
  906. if (name == NULL && count == lorn) return count;
  907. term = *ptr++;
  908. if (term == '<') term = '>';
  909. thisname = ptr;
  910. while (*ptr != term) ptr++;
  911. if (name != NULL && lorn == ptr - thisname &&
  912. strncmp((const char *)name, (const char *)thisname, lorn) == 0)
  913. return count;
  914. }
  915. return -1;
  916. }
  917. /*************************************************
  918. * Find first significant op code *
  919. *************************************************/
  920. /* This is called by several functions that scan a compiled expression looking
  921. for a fixed first character, or an anchoring op code etc. It skips over things
  922. that do not influence this. For some calls, a change of option is important.
  923. For some calls, it makes sense to skip negative forward and all backward
  924. assertions, and also the \b assertion; for others it does not.
  925. Arguments:
  926. code pointer to the start of the group
  927. options pointer to external options
  928. optbit the option bit whose changing is significant, or
  929. zero if none are
  930. skipassert TRUE if certain assertions are to be skipped
  931. Returns: pointer to the first significant opcode
  932. */
  933. static const uschar*
  934. first_significant_code(const uschar *code, int *options, int optbit,
  935. BOOL skipassert)
  936. {
  937. for (;;)
  938. {
  939. switch ((int)*code)
  940. {
  941. case OP_OPT:
  942. if (optbit > 0 && ((int)code[1] & optbit) != (*options & optbit))
  943. *options = (int)code[1];
  944. code += 2;
  945. break;
  946. case OP_ASSERT_NOT:
  947. case OP_ASSERTBACK:
  948. case OP_ASSERTBACK_NOT:
  949. if (!skipassert) return code;
  950. do code += GET(code, 1); while (*code == OP_ALT);
  951. code += _pcre_OP_lengths[*code];
  952. break;
  953. case OP_WORD_BOUNDARY:
  954. case OP_NOT_WORD_BOUNDARY:
  955. if (!skipassert) return code;
  956. /* Fall through */
  957. case OP_CALLOUT:
  958. case OP_CREF:
  959. case OP_RREF:
  960. case OP_DEF:
  961. code += _pcre_OP_lengths[*code];
  962. break;
  963. default:
  964. return code;
  965. }
  966. }
  967. /* Control never reaches here */
  968. }
  969. /*************************************************
  970. * Find the fixed length of a pattern *
  971. *************************************************/
  972. /* Scan a pattern and compute the fixed length of subject that will match it,
  973. if the length is fixed. This is needed for dealing with backward assertions.
  974. In UTF8 mode, the result is in characters rather than bytes.
  975. Arguments:
  976. code points to the start of the pattern (the bracket)
  977. options the compiling options
  978. Returns: the fixed length, or -1 if there is no fixed length,
  979. or -2 if \C was encountered
  980. */
  981. static int
  982. find_fixedlength(uschar *code, int options)
  983. {
  984. int length = -1;
  985. register int branchlength = 0;
  986. register uschar *cc = code + 1 + LINK_SIZE;
  987. /* Scan along the opcodes for this branch. If we get to the end of the
  988. branch, check the length against that of the other branches. */
  989. for (;;)
  990. {
  991. int d;
  992. register int op = *cc;
  993. switch (op)
  994. {
  995. case OP_CBRA:
  996. case OP_BRA:
  997. case OP_ONCE:
  998. case OP_COND:
  999. d = find_fixedlength(cc + ((op == OP_CBRA)? 2:0), options);
  1000. if (d < 0) return d;
  1001. branchlength += d;
  1002. do cc += GET(cc, 1); while (*cc == OP_ALT);
  1003. cc += 1 + LINK_SIZE;
  1004. break;
  1005. /* Reached end of a branch; if it's a ket it is the end of a nested
  1006. call. If it's ALT it is an alternation in a nested call. If it is
  1007. END it's the end of the outer call. All can be handled by the same code. */
  1008. case OP_ALT:
  1009. case OP_KET:
  1010. case OP_KETRMAX:
  1011. case OP_KETRMIN:
  1012. case OP_END:
  1013. if (length < 0) length = branchlength;
  1014. else if (length != branchlength) return -1;
  1015. if (*cc != OP_ALT) return length;
  1016. cc += 1 + LINK_SIZE;
  1017. branchlength = 0;
  1018. break;
  1019. /* Skip over assertive subpatterns */
  1020. case OP_ASSERT:
  1021. case OP_ASSERT_NOT:
  1022. case OP_ASSERTBACK:
  1023. case OP_ASSERTBACK_NOT:
  1024. do cc += GET(cc, 1); while (*cc == OP_ALT);
  1025. /* Fall through */
  1026. /* Skip over things that don't match chars */
  1027. case OP_REVERSE:
  1028. case OP_CREF:
  1029. case OP_RREF:
  1030. case OP_DEF:
  1031. case OP_OPT:
  1032. case OP_CALLOUT:
  1033. case OP_SOD:
  1034. case OP_SOM:
  1035. case OP_EOD:
  1036. case OP_EODN:
  1037. case OP_CIRC:
  1038. case OP_DOLL:
  1039. case OP_NOT_WORD_BOUNDARY:
  1040. case OP_WORD_BOUNDARY:
  1041. cc += _pcre_OP_lengths[*cc];
  1042. break;
  1043. /* Handle literal characters */
  1044. case OP_CHAR:
  1045. case OP_CHARNC:
  1046. case OP_NOT:
  1047. branchlength++;
  1048. cc += 2;
  1049. #ifdef SUPPORT_UTF8
  1050. if ((options & PCRE_UTF8) != 0)
  1051. {
  1052. while ((*cc & 0xc0) == 0x80) cc++;
  1053. }
  1054. #endif
  1055. break;
  1056. /* Handle exact repetitions. The count is already in characters, but we
  1057. need to skip over a multibyte character in UTF8 mode. */
  1058. case OP_EXACT:
  1059. branchlength += GET2(cc,1);
  1060. cc += 4;
  1061. #ifdef SUPPORT_UTF8
  1062. if ((options & PCRE_UTF8) != 0)
  1063. {
  1064. while((*cc & 0x80) == 0x80) cc++;
  1065. }
  1066. #endif
  1067. break;
  1068. case OP_TYPEEXACT:
  1069. branchlength += GET2(cc,1);
  1070. if (cc[3] == OP_PROP || cc[3] == OP_NOTPROP) cc += 2;
  1071. cc += 4;
  1072. break;
  1073. /* Handle single-char matchers */
  1074. case OP_PROP:
  1075. case OP_NOTPROP:
  1076. cc += 2;
  1077. /* Fall through */
  1078. case OP_NOT_DIGIT:
  1079. case OP_DIGIT:
  1080. case OP_NOT_WHITESPACE:
  1081. case OP_WHITESPACE:
  1082. case OP_NOT_WORDCHAR:
  1083. case OP_WORDCHAR:
  1084. case OP_ANY:
  1085. case OP_ALLANY:
  1086. branchlength++;
  1087. cc++;
  1088. break;
  1089. /* The single-byte matcher isn't allowed */
  1090. case OP_ANYBYTE:
  1091. return -2;
  1092. /* Check a class for variable quantification */
  1093. #ifdef SUPPORT_UTF8
  1094. case OP_XCLASS:
  1095. cc += GET(cc, 1) - 33;
  1096. /* Fall through */
  1097. #endif
  1098. case OP_CLASS:
  1099. case OP_NCLASS:
  1100. cc += 33;
  1101. switch (*cc)
  1102. {
  1103. case OP_CRSTAR:
  1104. case OP_CRMINSTAR:
  1105. case OP_CRQUERY:
  1106. case OP_CRMINQUERY:
  1107. return -1;
  1108. case OP_CRRANGE:
  1109. case OP_CRMINRANGE:
  1110. if (GET2(cc,1) != GET2(cc,3)) return -1;
  1111. branchlength += GET2(cc,1);
  1112. cc += 5;
  1113. break;
  1114. default:
  1115. branchlength++;
  1116. }
  1117. break;
  1118. /* Anything else is variable length */
  1119. default:
  1120. return -1;
  1121. }
  1122. }
  1123. /* Control never gets here */
  1124. }
  1125. /*************************************************
  1126. * Scan compiled regex for numbered bracket *
  1127. *************************************************/
  1128. /* This little function scans through a compiled pattern until it finds a
  1129. capturing bracket with the given number.
  1130. Arguments:
  1131. code points to start of expression
  1132. utf8 TRUE in UTF-8 mode
  1133. number the required bracket number
  1134. Returns: pointer to the opcode for the bracket, or NULL if not found
  1135. */
  1136. static const uschar *
  1137. find_bracket(const uschar *code, BOOL utf8, int number)
  1138. {
  1139. for (;;)
  1140. {
  1141. register int c = *code;
  1142. if (c == OP_END) return NULL;
  1143. /* XCLASS is used for classes that cannot be represented just by a bit
  1144. map. This includes negated single high-valued characters. The length in
  1145. the table is zero; the actual length is stored in the compiled code. */
  1146. if (c == OP_XCLASS) code += GET(code, 1);
  1147. /* Handle capturing bracket */
  1148. else if (c == OP_CBRA)
  1149. {
  1150. int n = GET2(code, 1+LINK_SIZE);
  1151. if (n == number) return (uschar *)code;
  1152. code += _pcre_OP_lengths[c];
  1153. }
  1154. /* Otherwise, we can get the item's length from the table, except that for
  1155. repeated character types, we have to test for \p and \P, which have an extra
  1156. two bytes of parameters. */
  1157. else
  1158. {
  1159. switch(c)
  1160. {
  1161. case OP_TYPESTAR:
  1162. case OP_TYPEMINSTAR:
  1163. case OP_TYPEPLUS:
  1164. case OP_TYPEMINPLUS:
  1165. case OP_TYPEQUERY:
  1166. case OP_TYPEMINQUERY:
  1167. case OP_TYPEPOSSTAR:
  1168. case OP_TYPEPOSPLUS:
  1169. case OP_TYPEPOSQUERY:
  1170. if (code[1] == OP_PROP || code[1] == OP_NOTPROP) code += 2;
  1171. break;
  1172. case OP_TYPEUPTO:
  1173. case OP_TYPEMINUPTO:
  1174. case OP_TYPEEXACT:
  1175. case OP_TYPEPOSUPTO:
  1176. if (code[3] == OP_PROP || code[3] == OP_NOTPROP) code += 2;
  1177. break;
  1178. }
  1179. /* Add in the fixed length from the table */
  1180. code += _pcre_OP_lengths[c];
  1181. /* In UTF-8 mode, opcodes that are followed by a character may be followed by
  1182. a multi-byte character. The length in the table is a minimum, so we have to
  1183. arrange to skip the extra bytes. */
  1184. #ifdef SUPPORT_UTF8
  1185. if (utf8) switch(c)
  1186. {
  1187. case OP_CHAR:
  1188. case OP_CHARNC:
  1189. case OP_EXACT:
  1190. case OP_UPTO:
  1191. case OP_MINUPTO:
  1192. case OP_POSUPTO:
  1193. case OP_STAR:
  1194. case OP_MINSTAR:
  1195. case OP_POSSTAR:
  1196. case OP_PLUS:
  1197. case OP_MINPLUS:
  1198. case OP_POSPLUS:
  1199. case OP_QUERY:
  1200. case OP_MINQUERY:
  1201. case OP_POSQUERY:
  1202. if (code[-1] >= 0xc0) code += _pcre_utf8_table4[code[-1] & 0x3f];
  1203. break;
  1204. }
  1205. #else
  1206. /* pacify warnings */
  1207. (void)(utf8);
  1208. #endif
  1209. }
  1210. }
  1211. }
  1212. /*************************************************
  1213. * Scan compiled regex for recursion reference *
  1214. *************************************************/
  1215. /* This little function scans through a compiled pattern until it finds an
  1216. instance of OP_RECURSE.
  1217. Arguments:
  1218. code points to start of expression
  1219. utf8 TRUE in UTF-8 mode
  1220. Returns: pointer to the opcode for OP_RECURSE, or NULL if not found
  1221. */
  1222. static const uschar *
  1223. find_recurse(const uschar *code, BOOL utf8)
  1224. {
  1225. for (;;)
  1226. {
  1227. register int c = *code;
  1228. if (c == OP_END) return NULL;
  1229. if (c == OP_RECURSE) return code;
  1230. /* XCLASS is used for classes that cannot be represented just by a bit
  1231. map. This includes negated single high-valued characters. The length in
  1232. the table is zero; the actual length is stored in the compiled code. */
  1233. if (c == OP_XCLASS) code += GET(code, 1);
  1234. /* Otherwise, we can get the item's length from the table, except that for
  1235. repeated character types, we have to test for \p and \P, which have an extra
  1236. two bytes of parameters. */
  1237. else
  1238. {
  1239. switch(c)
  1240. {
  1241. case OP_TYPESTAR:
  1242. case OP_TYPEMINSTAR:
  1243. case OP_TYPEPLUS:
  1244. case OP_TYPEMINPLUS:
  1245. case OP_TYPEQUERY:
  1246. case OP_TYPEMINQUERY:
  1247. case OP_TYPEPOSSTAR:
  1248. case OP_TYPEPOSPLUS:
  1249. case OP_TYPEPOSQUERY:
  1250. if (code[1] == OP_PROP || code[1] == OP_NOTPROP) code += 2;
  1251. break;
  1252. case OP_TYPEPOSUPTO:
  1253. case OP_TYPEUPTO:
  1254. case OP_TYPEMINUPTO:
  1255. case OP_TYPEEXACT:
  1256. if (code[3] == OP_PROP || code[3] == OP_NOTPROP) code += 2;
  1257. break;
  1258. }
  1259. /* Add in the fixed length from the table */
  1260. code += _pcre_OP_lengths[c];
  1261. /* In UTF-8 mode, opcodes that are followed by a character may be followed
  1262. by a multi-byte character. The length in the table is a minimum, so we have
  1263. to arrange to skip the extra bytes. */
  1264. #ifdef SUPPORT_UTF8
  1265. if (utf8) switch(c)
  1266. {
  1267. case OP_CHAR:
  1268. case OP_CHARNC:
  1269. case OP_EXACT:
  1270. case OP_UPTO:
  1271. case OP_MINUPTO:
  1272. case OP_POSUPTO:
  1273. case OP_STAR:
  1274. case OP_MINSTAR:
  1275. case OP_POSSTAR:
  1276. case OP_PLUS:
  1277. case OP_MINPLUS:
  1278. case OP_POSPLUS:
  1279. case OP_QUERY:
  1280. case OP_MINQUERY:
  1281. case OP_POSQUERY:
  1282. if (code[-1] >= 0xc0) code += _pcre_utf8_table4[code[-1] & 0x3f];
  1283. break;
  1284. }
  1285. #else
  1286. /* pacify warnings */
  1287. (void)(utf8);
  1288. #endif
  1289. }
  1290. }
  1291. }
  1292. /*************************************************
  1293. * Scan compiled branch for non-emptiness *
  1294. *************************************************/
  1295. /* This function scans through a branch of a compiled pattern to see whether it
  1296. can match the empty string or not. It is called from could_be_empty()
  1297. below and from compile_branch() when checking for an unlimited repeat of a
  1298. group that can match nothing. Note that first_significant_code() skips over
  1299. backward and negative forward assertions when its final argument is TRUE. If we
  1300. hit an unclosed bracket, we return "empty" - this means we've struck an inner
  1301. bracket whose current branch will already have been scanned.
  1302. Arguments:
  1303. code points to start of search
  1304. endcode points to where to stop
  1305. utf8 TRUE if in UTF8 mode
  1306. Returns: TRUE if what is matched could be empty
  1307. */
  1308. static BOOL
  1309. could_be_empty_branch(const

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