PageRenderTime 59ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/beta3/harbour/source/hbpcre/pcrecomp.c

#
C | 2101 lines | 1254 code | 313 blank | 534 comment | 356 complexity | 176f1ce6df975a239bdb7a1ba855d8c9 MD5 | raw file
Possible License(s): AGPL-1.0, BSD-3-Clause, CC-BY-SA-3.0, LGPL-3.0, GPL-2.0, LGPL-2.0, LGPL-2.1
  1. /*************************************************
  2. * Perl-Compatible Regular Expressions *
  3. *************************************************/
  4. /* PCRE is a library of functions to support regular expressions whose syntax
  5. and semantics are as close as possible to those of the Perl 5 language.
  6. Written by Philip Hazel
  7. Copyright (c) 1997-2005 University of Cambridge
  8. -----------------------------------------------------------------------------
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions are met:
  11. * Redistributions of source code must retain the above copyright notice,
  12. this list of conditions and the following disclaimer.
  13. * Redistributions in binary form must reproduce the above copyright
  14. notice, this list of conditions and the following disclaimer in the
  15. documentation and/or other materials provided with the distribution.
  16. * Neither the name of the University of Cambridge nor the names of its
  17. contributors may be used to endorse or promote products derived from
  18. this software without specific prior written permission.
  19. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  23. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. POSSIBILITY OF SUCH DAMAGE.
  30. -----------------------------------------------------------------------------
  31. */
  32. /* This module contains the external function pcre_compile(), along with
  33. supporting internal functions that are not used by other modules. */
  34. #include "pcreinal.h"
  35. /*************************************************
  36. * Code parameters and static tables *
  37. *************************************************/
  38. /* Maximum number of items on the nested bracket stacks at compile time. This
  39. applies to the nesting of all kinds of parentheses. It does not limit
  40. un-nested, non-capturing parentheses. This number can be made bigger if
  41. necessary - it is used to dimension one int and one unsigned char vector at
  42. compile time. */
  43. #define BRASTACK_SIZE 200
  44. /* Table for handling escaped characters in the range '0'-'z'. Positive returns
  45. are simple data values; negative values are for special things like \d and so
  46. on. Zero means further processing is needed (for things like \x), or the escape
  47. is invalid. */
  48. #if !EBCDIC /* This is the "normal" table for ASCII systems */
  49. static const short int escapes[] = {
  50. 0, 0, 0, 0, 0, 0, 0, 0, /* 0 - 7 */
  51. 0, 0, ':', ';', '<', '=', '>', '?', /* 8 - ? */
  52. '@', -ESC_A, -ESC_B, -ESC_C, -ESC_D, -ESC_E, 0, -ESC_G, /* @ - G */
  53. 0, 0, 0, 0, 0, 0, 0, 0, /* H - O */
  54. -ESC_P, -ESC_Q, 0, -ESC_S, 0, 0, 0, -ESC_W, /* P - W */
  55. -ESC_X, 0, -ESC_Z, '[', '\\', ']', '^', '_', /* X - _ */
  56. '`', 7, -ESC_b, 0, -ESC_d, ESC_e, ESC_f, 0, /* ` - g */
  57. 0, 0, 0, 0, 0, 0, ESC_n, 0, /* h - o */
  58. -ESC_p, 0, ESC_r, -ESC_s, ESC_tee, 0, 0, -ESC_w, /* p - w */
  59. 0, 0, -ESC_z /* x - z */
  60. };
  61. #else /* This is the "abnormal" table for EBCDIC systems */
  62. static const short int escapes[] = {
  63. /* 48 */ 0, 0, 0, '.', '<', '(', '+', '|',
  64. /* 50 */ '&', 0, 0, 0, 0, 0, 0, 0,
  65. /* 58 */ 0, 0, '!', '$', '*', ')', ';', '~',
  66. /* 60 */ '-', '/', 0, 0, 0, 0, 0, 0,
  67. /* 68 */ 0, 0, '|', ',', '%', '_', '>', '?',
  68. /* 70 */ 0, 0, 0, 0, 0, 0, 0, 0,
  69. /* 78 */ 0, '`', ':', '#', '@', '\'', '=', '"',
  70. /* 80 */ 0, 7, -ESC_b, 0, -ESC_d, ESC_e, ESC_f, 0,
  71. /* 88 */ 0, 0, 0, '{', 0, 0, 0, 0,
  72. /* 90 */ 0, 0, 0, 'l', 0, ESC_n, 0, -ESC_p,
  73. /* 98 */ 0, ESC_r, 0, '}', 0, 0, 0, 0,
  74. /* A0 */ 0, '~', -ESC_s, ESC_tee, 0, 0, -ESC_w, 0,
  75. /* A8 */ 0,-ESC_z, 0, 0, 0, '[', 0, 0,
  76. /* B0 */ 0, 0, 0, 0, 0, 0, 0, 0,
  77. /* B8 */ 0, 0, 0, 0, 0, ']', '=', '-',
  78. /* C0 */ '{',-ESC_A, -ESC_B, -ESC_C, -ESC_D,-ESC_E, 0, -ESC_G,
  79. /* C8 */ 0, 0, 0, 0, 0, 0, 0, 0,
  80. /* D0 */ '}', 0, 0, 0, 0, 0, 0, -ESC_P,
  81. /* D8 */-ESC_Q, 0, 0, 0, 0, 0, 0, 0,
  82. /* E0 */ '\\', 0, -ESC_S, 0, 0, 0, -ESC_W, -ESC_X,
  83. /* E8 */ 0,-ESC_Z, 0, 0, 0, 0, 0, 0,
  84. /* F0 */ 0, 0, 0, 0, 0, 0, 0, 0,
  85. /* F8 */ 0, 0, 0, 0, 0, 0, 0, 0
  86. };
  87. #endif
  88. /* Tables of names of POSIX character classes and their lengths. The list is
  89. terminated by a zero length entry. The first three must be alpha, upper, lower,
  90. as this is assumed for handling case independence. */
  91. static const char *const posix_names[] = {
  92. "alpha", "lower", "upper",
  93. "alnum", "ascii", "blank", "cntrl", "digit", "graph",
  94. "print", "punct", "space", "word", "xdigit" };
  95. static const uschar posix_name_lengths[] = {
  96. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 6, 0 };
  97. /* Table of class bit maps for each POSIX class; up to three may be combined
  98. to form the class. The table for [:blank:] is dynamically modified to remove
  99. the vertical space characters. */
  100. static const int posix_class_maps[] = {
  101. cbit_lower, cbit_upper, -1, /* alpha */
  102. cbit_lower, -1, -1, /* lower */
  103. cbit_upper, -1, -1, /* upper */
  104. cbit_digit, cbit_lower, cbit_upper, /* alnum */
  105. cbit_print, cbit_cntrl, -1, /* ascii */
  106. cbit_space, -1, -1, /* blank - a GNU extension */
  107. cbit_cntrl, -1, -1, /* cntrl */
  108. cbit_digit, -1, -1, /* digit */
  109. cbit_graph, -1, -1, /* graph */
  110. cbit_print, -1, -1, /* print */
  111. cbit_punct, -1, -1, /* punct */
  112. cbit_space, -1, -1, /* space */
  113. cbit_word, -1, -1, /* word - a Perl extension */
  114. cbit_xdigit,-1, -1 /* xdigit */
  115. };
  116. /* The texts of compile-time error messages. These are "char *" because they
  117. are passed to the outside world. */
  118. static const char *error_texts[] = {
  119. "no error",
  120. "\\ at end of pattern",
  121. "\\c at end of pattern",
  122. "unrecognized character follows \\",
  123. "numbers out of order in {} quantifier",
  124. /* 5 */
  125. "number too big in {} quantifier",
  126. "missing terminating ] for character class",
  127. "invalid escape sequence in character class",
  128. "range out of order in character class",
  129. "nothing to repeat",
  130. /* 10 */
  131. "operand of unlimited repeat could match the empty string",
  132. "internal error: unexpected repeat",
  133. "unrecognized character after (?",
  134. "POSIX named classes are supported only within a class",
  135. "missing )",
  136. /* 15 */
  137. "reference to non-existent subpattern",
  138. "erroffset passed as NULL",
  139. "unknown option bit(s) set",
  140. "missing ) after comment",
  141. "parentheses nested too deeply",
  142. /* 20 */
  143. "regular expression too large",
  144. "failed to get memory",
  145. "unmatched parentheses",
  146. "internal error: code overflow",
  147. "unrecognized character after (?<",
  148. /* 25 */
  149. "lookbehind assertion is not fixed length",
  150. "malformed number after (?(",
  151. "conditional group contains more than two branches",
  152. "assertion expected after (?(",
  153. "(?R or (?digits must be followed by )",
  154. /* 30 */
  155. "unknown POSIX class name",
  156. "POSIX collating elements are not supported",
  157. "this version of PCRE is not compiled with PCRE_UTF8 support",
  158. "spare error",
  159. "character value in \\x{...} sequence is too large",
  160. /* 35 */
  161. "invalid condition (?(0)",
  162. "\\C not allowed in lookbehind assertion",
  163. "PCRE does not support \\L, \\l, \\N, \\U, or \\u",
  164. "number after (?C is > 255",
  165. "closing ) for (?C expected",
  166. /* 40 */
  167. "recursive call could loop indefinitely",
  168. "unrecognized character after (?P",
  169. "syntax error after (?P",
  170. "two named groups have the same name",
  171. "invalid UTF-8 string",
  172. /* 45 */
  173. "support for \\P, \\p, and \\X has not been compiled",
  174. "malformed \\P or \\p sequence",
  175. "unknown property name after \\P or \\p"
  176. };
  177. /* Table to identify digits and hex digits. This is used when compiling
  178. patterns. Note that the tables in chartables are dependent on the locale, and
  179. may mark arbitrary characters as digits - but the PCRE compiling code expects
  180. to handle only 0-9, a-z, and A-Z as digits when compiling. That is why we have
  181. a private table here. It costs 256 bytes, but it is a lot faster than doing
  182. character value tests (at least in some simple cases I timed), and in some
  183. applications one wants PCRE to compile efficiently as well as match
  184. efficiently.
  185. For convenience, we use the same bit definitions as in chartables:
  186. 0x04 decimal digit
  187. 0x08 hexadecimal digit
  188. Then we can use ctype_digit and ctype_xdigit in the code. */
  189. #if !EBCDIC /* This is the "normal" case, for ASCII systems */
  190. static const unsigned char digitab[] =
  191. {
  192. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 0- 7 */
  193. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 8- 15 */
  194. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 16- 23 */
  195. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 24- 31 */
  196. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* - ' */
  197. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* ( - / */
  198. 0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c, /* 0 - 7 */
  199. 0x0c,0x0c,0x00,0x00,0x00,0x00,0x00,0x00, /* 8 - ? */
  200. 0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x00, /* @ - G */
  201. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* H - O */
  202. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* P - W */
  203. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* X - _ */
  204. 0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x00, /* ` - g */
  205. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* h - o */
  206. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* p - w */
  207. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* x -127 */
  208. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 128-135 */
  209. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 136-143 */
  210. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 144-151 */
  211. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 152-159 */
  212. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 160-167 */
  213. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 168-175 */
  214. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 176-183 */
  215. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 184-191 */
  216. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 192-199 */
  217. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 200-207 */
  218. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 208-215 */
  219. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 216-223 */
  220. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 224-231 */
  221. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 232-239 */
  222. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 240-247 */
  223. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};/* 248-255 */
  224. #else /* This is the "abnormal" case, for EBCDIC systems */
  225. static const unsigned char digitab[] =
  226. {
  227. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 0- 7 0 */
  228. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 8- 15 */
  229. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 16- 23 10 */
  230. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 24- 31 */
  231. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 32- 39 20 */
  232. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 40- 47 */
  233. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 48- 55 30 */
  234. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 56- 63 */
  235. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* - 71 40 */
  236. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 72- | */
  237. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* & - 87 50 */
  238. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 88- ? */
  239. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* - -103 60 */
  240. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 104- ? */
  241. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 112-119 70 */
  242. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 120- " */
  243. 0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x00, /* 128- g 80 */
  244. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* h -143 */
  245. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 144- p 90 */
  246. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* q -159 */
  247. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 160- x A0 */
  248. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* y -175 */
  249. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* ^ -183 B0 */
  250. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 184-191 */
  251. 0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x00, /* { - G C0 */
  252. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* H -207 */
  253. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* } - P D0 */
  254. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* Q -223 */
  255. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* \ - X E0 */
  256. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* Y -239 */
  257. 0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c, /* 0 - 7 F0 */
  258. 0x0c,0x0c,0x00,0x00,0x00,0x00,0x00,0x00};/* 8 -255 */
  259. static const unsigned char ebcdic_chartab[] = { /* chartable partial dup */
  260. 0x80,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /* 0- 7 */
  261. 0x00,0x00,0x00,0x00,0x01,0x01,0x00,0x00, /* 8- 15 */
  262. 0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /* 16- 23 */
  263. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 24- 31 */
  264. 0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /* 32- 39 */
  265. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 40- 47 */
  266. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 48- 55 */
  267. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 56- 63 */
  268. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* - 71 */
  269. 0x00,0x00,0x00,0x80,0x00,0x80,0x80,0x80, /* 72- | */
  270. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* & - 87 */
  271. 0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00, /* 88- ? */
  272. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* - -103 */
  273. 0x00,0x00,0x00,0x00,0x00,0x10,0x00,0x80, /* 104- ? */
  274. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 112-119 */
  275. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 120- " */
  276. 0x00,0x1a,0x1a,0x1a,0x1a,0x1a,0x1a,0x12, /* 128- g */
  277. 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* h -143 */
  278. 0x00,0x12,0x12,0x12,0x12,0x12,0x12,0x12, /* 144- p */
  279. 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* q -159 */
  280. 0x00,0x00,0x12,0x12,0x12,0x12,0x12,0x12, /* 160- x */
  281. 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* y -175 */
  282. 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* ^ -183 */
  283. 0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x00, /* 184-191 */
  284. 0x80,0x1a,0x1a,0x1a,0x1a,0x1a,0x1a,0x12, /* { - G */
  285. 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* H -207 */
  286. 0x00,0x12,0x12,0x12,0x12,0x12,0x12,0x12, /* } - P */
  287. 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* Q -223 */
  288. 0x00,0x00,0x12,0x12,0x12,0x12,0x12,0x12, /* \ - X */
  289. 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* Y -239 */
  290. 0x1c,0x1c,0x1c,0x1c,0x1c,0x1c,0x1c,0x1c, /* 0 - 7 */
  291. 0x1c,0x1c,0x00,0x00,0x00,0x00,0x00,0x00};/* 8 -255 */
  292. #endif
  293. /* Definition to allow mutual recursion */
  294. static BOOL
  295. compile_regex(int, int, int *, uschar **, const uschar **, int *, BOOL, int,
  296. int *, int *, branch_chain *, compile_data *);
  297. /*************************************************
  298. * Handle escapes *
  299. *************************************************/
  300. /* This function is called when a \ has been encountered. It either returns a
  301. positive value for a simple escape such as \n, or a negative value which
  302. encodes one of the more complicated things such as \d. When UTF-8 is enabled,
  303. a positive value greater than 255 may be returned. On entry, ptr is pointing at
  304. the \. On exit, it is on the final character of the escape sequence.
  305. Arguments:
  306. ptrptr points to the pattern position pointer
  307. errorcodeptr points to the errorcode variable
  308. bracount number of previous extracting brackets
  309. options the options bits
  310. isclass TRUE if inside a character class
  311. Returns: zero or positive => a data character
  312. negative => a special escape sequence
  313. on error, errorptr is set
  314. */
  315. static int
  316. check_escape(const uschar **ptrptr, int *errorcodeptr, int bracount,
  317. int options, BOOL isclass)
  318. {
  319. const uschar *ptr = *ptrptr;
  320. int c, i;
  321. /* If backslash is at the end of the pattern, it's an error. */
  322. c = *(++ptr);
  323. if (c == 0) *errorcodeptr = ERR1;
  324. /* Non-alphamerics are literals. For digits or letters, do an initial lookup in
  325. a table. A non-zero result is something that can be returned immediately.
  326. Otherwise further processing may be required. */
  327. #if !EBCDIC /* ASCII coding */
  328. else if (c < '0' || c > 'z') {} /* Not alphameric */
  329. else if ((i = escapes[c - '0']) != 0) c = i;
  330. #else /* EBCDIC coding */
  331. else if (c < 'a' || (ebcdic_chartab[c] & 0x0E) == 0) {} /* Not alphameric */
  332. else if ((i = escapes[c - 0x48]) != 0) c = i;
  333. #endif
  334. /* Escapes that need further processing, or are illegal. */
  335. else
  336. {
  337. const uschar *oldptr;
  338. switch (c)
  339. {
  340. /* A number of Perl escapes are not handled by PCRE. We give an explicit
  341. error. */
  342. case 'l':
  343. case 'L':
  344. case 'N':
  345. case 'u':
  346. case 'U':
  347. *errorcodeptr = ERR37;
  348. break;
  349. /* The handling of escape sequences consisting of a string of digits
  350. starting with one that is not zero is not straightforward. By experiment,
  351. the way Perl works seems to be as follows:
  352. Outside a character class, the digits are read as a decimal number. If the
  353. number is less than 10, or if there are that many previous extracting
  354. left brackets, then it is a back reference. Otherwise, up to three octal
  355. digits are read to form an escaped byte. Thus \123 is likely to be octal
  356. 123 (cf \0123, which is octal 012 followed by the literal 3). If the octal
  357. value is greater than 377, the least significant 8 bits are taken. Inside a
  358. character class, \ followed by a digit is always an octal number. */
  359. case '1': case '2': case '3': case '4': case '5':
  360. case '6': case '7': case '8': case '9':
  361. if (!isclass)
  362. {
  363. oldptr = ptr;
  364. c -= '0';
  365. while ((digitab[ptr[1]] & ctype_digit) != 0)
  366. c = c * 10 + *(++ptr) - '0';
  367. if (c < 10 || c <= bracount)
  368. {
  369. c = -(ESC_REF + c);
  370. break;
  371. }
  372. ptr = oldptr; /* Put the pointer back and fall through */
  373. }
  374. /* Handle an octal number following \. If the first digit is 8 or 9, Perl
  375. generates a binary zero byte and treats the digit as a following literal.
  376. Thus we have to pull back the pointer by one. */
  377. if ((c = *ptr) >= '8')
  378. {
  379. ptr--;
  380. c = 0;
  381. break;
  382. }
  383. /* \0 always starts an octal number, but we may drop through to here with a
  384. larger first octal digit. */
  385. case '0':
  386. c -= '0';
  387. while(i++ < 2 && ptr[1] >= '0' && ptr[1] <= '7')
  388. c = c * 8 + *(++ptr) - '0';
  389. c &= 255; /* Take least significant 8 bits */
  390. break;
  391. /* \x is complicated when UTF-8 is enabled. \x{ddd} is a character number
  392. which can be greater than 0xff, but only if the ddd are hex digits. */
  393. case 'x':
  394. #ifdef SUPPORT_UTF8
  395. if (ptr[1] == '{' && (options & PCRE_UTF8) != 0)
  396. {
  397. const uschar *pt = ptr + 2;
  398. register int count = 0;
  399. c = 0;
  400. while ((digitab[*pt] & ctype_xdigit) != 0)
  401. {
  402. int cc = *pt++;
  403. count++;
  404. #if !EBCDIC /* ASCII coding */
  405. if (cc >= 'a') cc -= 32; /* Convert to upper case */
  406. c = c * 16 + cc - ((cc < 'A')? '0' : ('A' - 10));
  407. #else /* EBCDIC coding */
  408. if (cc >= 'a' && cc <= 'z') cc += 64; /* Convert to upper case */
  409. c = c * 16 + cc - ((cc >= '0')? '0' : ('A' - 10));
  410. #endif
  411. }
  412. if (*pt == '}')
  413. {
  414. if (c < 0 || count > 8) *errorcodeptr = ERR34;
  415. ptr = pt;
  416. break;
  417. }
  418. /* If the sequence of hex digits does not end with '}', then we don't
  419. recognize this construct; fall through to the normal \x handling. */
  420. }
  421. #endif
  422. /* Read just a single hex char */
  423. c = 0;
  424. while (i++ < 2 && (digitab[ptr[1]] & ctype_xdigit) != 0)
  425. {
  426. int cc; /* Some compilers don't like ++ */
  427. cc = *(++ptr); /* in initializers */
  428. #if !EBCDIC /* ASCII coding */
  429. if (cc >= 'a') cc -= 32; /* Convert to upper case */
  430. c = c * 16 + cc - ((cc < 'A')? '0' : ('A' - 10));
  431. #else /* EBCDIC coding */
  432. if (cc <= 'z') cc += 64; /* Convert to upper case */
  433. c = c * 16 + cc - ((cc >= '0')? '0' : ('A' - 10));
  434. #endif
  435. }
  436. break;
  437. /* Other special escapes not starting with a digit are straightforward */
  438. case 'c':
  439. c = *(++ptr);
  440. if (c == 0)
  441. {
  442. *errorcodeptr = ERR2;
  443. return 0;
  444. }
  445. /* A letter is upper-cased; then the 0x40 bit is flipped. This coding
  446. is ASCII-specific, but then the whole concept of \cx is ASCII-specific.
  447. (However, an EBCDIC equivalent has now been added.) */
  448. #if !EBCDIC /* ASCII coding */
  449. if (c >= 'a' && c <= 'z') c -= 32;
  450. c ^= 0x40;
  451. #else /* EBCDIC coding */
  452. if (c >= 'a' && c <= 'z') c += 64;
  453. c ^= 0xC0;
  454. #endif
  455. break;
  456. /* PCRE_EXTRA enables extensions to Perl in the matter of escapes. Any
  457. other alphameric following \ is an error if PCRE_EXTRA was set; otherwise,
  458. for Perl compatibility, it is a literal. This code looks a bit odd, but
  459. there used to be some cases other than the default, and there may be again
  460. in future, so I haven't "optimized" it. */
  461. default:
  462. if ((options & PCRE_EXTRA) != 0) switch(c)
  463. {
  464. default:
  465. *errorcodeptr = ERR3;
  466. break;
  467. }
  468. break;
  469. }
  470. }
  471. *ptrptr = ptr;
  472. return c;
  473. }
  474. #ifdef SUPPORT_UCP
  475. /*************************************************
  476. * Handle \P and \p *
  477. *************************************************/
  478. /* This function is called after \P or \p has been encountered, provided that
  479. PCRE is compiled with support for Unicode properties. On entry, ptrptr is
  480. pointing at the P or p. On exit, it is pointing at the final character of the
  481. escape sequence.
  482. Argument:
  483. ptrptr points to the pattern position pointer
  484. negptr points to a boolean that is set TRUE for negation else FALSE
  485. errorcodeptr points to the error code variable
  486. Returns: value from ucp_type_table, or -1 for an invalid type
  487. */
  488. static int
  489. get_ucp(const uschar **ptrptr, BOOL *negptr, int *errorcodeptr)
  490. {
  491. int c, i, bot, top;
  492. const uschar *ptr = *ptrptr;
  493. char name[4];
  494. *negptr = FALSE;
  495. c = *(++ptr);
  496. if (c == 0) goto ERROR_RETURN;
  497. /* \P or \p can be followed by a one- or two-character name in {}, optionally
  498. preceded by ^ for negation. */
  499. if (c == '{')
  500. {
  501. if (ptr[1] == '^')
  502. {
  503. *negptr = TRUE;
  504. ptr++;
  505. }
  506. for (i = 0; i <= 2; i++)
  507. {
  508. c = *(++ptr);
  509. if (c == 0) goto ERROR_RETURN;
  510. if (c == '}') break;
  511. name[i] = c;
  512. }
  513. if (c !='}') /* Try to distinguish error cases */
  514. {
  515. while (*(++ptr) != 0 && *ptr != '}');
  516. if (*ptr == '}') goto UNKNOWN_RETURN; else goto ERROR_RETURN;
  517. }
  518. name[i] = 0;
  519. }
  520. /* Otherwise there is just one following character */
  521. else
  522. {
  523. name[0] = c;
  524. name[1] = 0;
  525. }
  526. *ptrptr = ptr;
  527. /* Search for a recognized property name using binary chop */
  528. bot = 0;
  529. top = _pcre_utt_size;
  530. while (bot < top)
  531. {
  532. i = (bot + top)/2;
  533. c = strcmp(name, _pcre_utt[i].name);
  534. if (c == 0) return _pcre_utt[i].value;
  535. if (c > 0) bot = i + 1; else top = i;
  536. }
  537. UNKNOWN_RETURN:
  538. *errorcodeptr = ERR47;
  539. *ptrptr = ptr;
  540. return -1;
  541. ERROR_RETURN:
  542. *errorcodeptr = ERR46;
  543. *ptrptr = ptr;
  544. return -1;
  545. }
  546. #endif
  547. /*************************************************
  548. * Check for counted repeat *
  549. *************************************************/
  550. /* This function is called when a '{' is encountered in a place where it might
  551. start a quantifier. It looks ahead to see if it really is a quantifier or not.
  552. It is only a quantifier if it is one of the forms {ddd} {ddd,} or {ddd,ddd}
  553. where the ddds are digits.
  554. Arguments:
  555. p pointer to the first char after '{'
  556. Returns: TRUE or FALSE
  557. */
  558. static BOOL
  559. is_counted_repeat(const uschar *p)
  560. {
  561. if ((digitab[*p++] & ctype_digit) == 0) return FALSE;
  562. while ((digitab[*p] & ctype_digit) != 0) p++;
  563. if (*p == '}') return TRUE;
  564. if (*p++ != ',') return FALSE;
  565. if (*p == '}') return TRUE;
  566. if ((digitab[*p++] & ctype_digit) == 0) return FALSE;
  567. while ((digitab[*p] & ctype_digit) != 0) p++;
  568. return (*p == '}');
  569. }
  570. /*************************************************
  571. * Read repeat counts *
  572. *************************************************/
  573. /* Read an item of the form {n,m} and return the values. This is called only
  574. after is_counted_repeat() has confirmed that a repeat-count quantifier exists,
  575. so the syntax is guaranteed to be correct, but we need to check the values.
  576. Arguments:
  577. p pointer to first char after '{'
  578. minp pointer to int for min
  579. maxp pointer to int for max
  580. returned as -1 if no max
  581. errorcodeptr points to error code variable
  582. Returns: pointer to '}' on success;
  583. current ptr on error, with errorcodeptr set non-zero
  584. */
  585. static const uschar *
  586. read_repeat_counts(const uschar *p, int *minp, int *maxp, int *errorcodeptr)
  587. {
  588. int min = 0;
  589. int max = -1;
  590. /* Read the minimum value and do a paranoid check: a negative value indicates
  591. an integer overflow. */
  592. while ((digitab[*p] & ctype_digit) != 0) min = min * 10 + *p++ - '0';
  593. if (min < 0 || min > 65535)
  594. {
  595. *errorcodeptr = ERR5;
  596. return p;
  597. }
  598. /* Read the maximum value if there is one, and again do a paranoid on its size.
  599. Also, max must not be less than min. */
  600. if (*p == '}') max = min; else
  601. {
  602. if (*(++p) != '}')
  603. {
  604. max = 0;
  605. while((digitab[*p] & ctype_digit) != 0) max = max * 10 + *p++ - '0';
  606. if (max < 0 || max > 65535)
  607. {
  608. *errorcodeptr = ERR5;
  609. return p;
  610. }
  611. if (max < min)
  612. {
  613. *errorcodeptr = ERR4;
  614. return p;
  615. }
  616. }
  617. }
  618. /* Fill in the required variables, and pass back the pointer to the terminating
  619. '}'. */
  620. *minp = min;
  621. *maxp = max;
  622. return p;
  623. }
  624. /*************************************************
  625. * Find first significant op code *
  626. *************************************************/
  627. /* This is called by several functions that scan a compiled expression looking
  628. for a fixed first character, or an anchoring op code etc. It skips over things
  629. that do not influence this. For some calls, a change of option is important.
  630. For some calls, it makes sense to skip negative forward and all backward
  631. assertions, and also the \b assertion; for others it does not.
  632. Arguments:
  633. code pointer to the start of the group
  634. options pointer to external options
  635. optbit the option bit whose changing is significant, or
  636. zero if none are
  637. skipassert TRUE if certain assertions are to be skipped
  638. Returns: pointer to the first significant opcode
  639. */
  640. static const uschar*
  641. first_significant_code(const uschar *code, int *options, int optbit,
  642. BOOL skipassert)
  643. {
  644. for (;;)
  645. {
  646. switch ((int)*code)
  647. {
  648. case OP_OPT:
  649. if (optbit > 0 && ((int)code[1] & optbit) != (*options & optbit))
  650. *options = (int)code[1];
  651. code += 2;
  652. break;
  653. case OP_ASSERT_NOT:
  654. case OP_ASSERTBACK:
  655. case OP_ASSERTBACK_NOT:
  656. if (!skipassert) return code;
  657. do code += GET(code, 1); while (*code == OP_ALT);
  658. code += _pcre_OP_lengths[*code];
  659. break;
  660. case OP_WORD_BOUNDARY:
  661. case OP_NOT_WORD_BOUNDARY:
  662. if (!skipassert) return code;
  663. /* Fall through */
  664. case OP_CALLOUT:
  665. case OP_CREF:
  666. case OP_BRANUMBER:
  667. code += _pcre_OP_lengths[*code];
  668. break;
  669. default:
  670. return code;
  671. }
  672. }
  673. /* Control never reaches here */
  674. }
  675. /*************************************************
  676. * Find the fixed length of a pattern *
  677. *************************************************/
  678. /* Scan a pattern and compute the fixed length of subject that will match it,
  679. if the length is fixed. This is needed for dealing with backward assertions.
  680. In UTF8 mode, the result is in characters rather than bytes.
  681. Arguments:
  682. code points to the start of the pattern (the bracket)
  683. options the compiling options
  684. Returns: the fixed length, or -1 if there is no fixed length,
  685. or -2 if \C was encountered
  686. */
  687. static int
  688. find_fixedlength(uschar *code, int options)
  689. {
  690. int length = -1;
  691. register int branchlength = 0;
  692. register uschar *cc = code + 1 + LINK_SIZE;
  693. /* Scan along the opcodes for this branch. If we get to the end of the
  694. branch, check the length against that of the other branches. */
  695. for (;;)
  696. {
  697. int d;
  698. register int op = *cc;
  699. if (op >= OP_BRA) op = OP_BRA;
  700. switch (op)
  701. {
  702. case OP_BRA:
  703. case OP_ONCE:
  704. case OP_COND:
  705. d = find_fixedlength(cc, options);
  706. if (d < 0) return d;
  707. branchlength += d;
  708. do cc += GET(cc, 1); while (*cc == OP_ALT);
  709. cc += 1 + LINK_SIZE;
  710. break;
  711. /* Reached end of a branch; if it's a ket it is the end of a nested
  712. call. If it's ALT it is an alternation in a nested call. If it is
  713. END it's the end of the outer call. All can be handled by the same code. */
  714. case OP_ALT:
  715. case OP_KET:
  716. case OP_KETRMAX:
  717. case OP_KETRMIN:
  718. case OP_END:
  719. if (length < 0) length = branchlength;
  720. else if (length != branchlength) return -1;
  721. if (*cc != OP_ALT) return length;
  722. cc += 1 + LINK_SIZE;
  723. branchlength = 0;
  724. break;
  725. /* Skip over assertive subpatterns */
  726. case OP_ASSERT:
  727. case OP_ASSERT_NOT:
  728. case OP_ASSERTBACK:
  729. case OP_ASSERTBACK_NOT:
  730. do cc += GET(cc, 1); while (*cc == OP_ALT);
  731. /* Fall through */
  732. /* Skip over things that don't match chars */
  733. case OP_REVERSE:
  734. case OP_BRANUMBER:
  735. case OP_CREF:
  736. case OP_OPT:
  737. case OP_CALLOUT:
  738. case OP_SOD:
  739. case OP_SOM:
  740. case OP_EOD:
  741. case OP_EODN:
  742. case OP_CIRC:
  743. case OP_DOLL:
  744. case OP_NOT_WORD_BOUNDARY:
  745. case OP_WORD_BOUNDARY:
  746. cc += _pcre_OP_lengths[*cc];
  747. break;
  748. /* Handle literal characters */
  749. case OP_CHAR:
  750. case OP_CHARNC:
  751. branchlength++;
  752. cc += 2;
  753. #ifdef SUPPORT_UTF8
  754. if ((options & PCRE_UTF8) != 0)
  755. {
  756. while ((*cc & 0xc0) == 0x80) cc++;
  757. }
  758. #endif
  759. break;
  760. /* Handle exact repetitions. The count is already in characters, but we
  761. need to skip over a multibyte character in UTF8 mode. */
  762. case OP_EXACT:
  763. branchlength += GET2(cc,1);
  764. cc += 4;
  765. #ifdef SUPPORT_UTF8
  766. if ((options & PCRE_UTF8) != 0)
  767. {
  768. while((*cc & 0x80) == 0x80) cc++;
  769. }
  770. #endif
  771. break;
  772. case OP_TYPEEXACT:
  773. branchlength += GET2(cc,1);
  774. cc += 4;
  775. break;
  776. /* Handle single-char matchers */
  777. case OP_PROP:
  778. case OP_NOTPROP:
  779. cc++;
  780. /* Fall through */
  781. case OP_NOT_DIGIT:
  782. case OP_DIGIT:
  783. case OP_NOT_WHITESPACE:
  784. case OP_WHITESPACE:
  785. case OP_NOT_WORDCHAR:
  786. case OP_WORDCHAR:
  787. case OP_ANY:
  788. branchlength++;
  789. cc++;
  790. break;
  791. /* The single-byte matcher isn't allowed */
  792. case OP_ANYBYTE:
  793. return -2;
  794. /* Check a class for variable quantification */
  795. #ifdef SUPPORT_UTF8
  796. case OP_XCLASS:
  797. cc += GET(cc, 1) - 33;
  798. /* Fall through */
  799. #endif
  800. case OP_CLASS:
  801. case OP_NCLASS:
  802. cc += 33;
  803. switch (*cc)
  804. {
  805. case OP_CRSTAR:
  806. case OP_CRMINSTAR:
  807. case OP_CRQUERY:
  808. case OP_CRMINQUERY:
  809. return -1;
  810. case OP_CRRANGE:
  811. case OP_CRMINRANGE:
  812. if (GET2(cc,1) != GET2(cc,3)) return -1;
  813. branchlength += GET2(cc,1);
  814. cc += 5;
  815. break;
  816. default:
  817. branchlength++;
  818. }
  819. break;
  820. /* Anything else is variable length */
  821. default:
  822. return -1;
  823. }
  824. }
  825. /* Control never gets here */
  826. }
  827. /*************************************************
  828. * Scan compiled regex for numbered bracket *
  829. *************************************************/
  830. /* This little function scans through a compiled pattern until it finds a
  831. capturing bracket with the given number.
  832. Arguments:
  833. code points to start of expression
  834. utf8 TRUE in UTF-8 mode
  835. number the required bracket number
  836. Returns: pointer to the opcode for the bracket, or NULL if not found
  837. */
  838. static const uschar *
  839. find_bracket(const uschar *code, BOOL utf8, int number)
  840. {
  841. #ifndef SUPPORT_UTF8
  842. utf8 = utf8; /* Stop pedantic compilers complaining */
  843. #endif
  844. for (;;)
  845. {
  846. register int c = *code;
  847. if (c == OP_END) return NULL;
  848. else if (c > OP_BRA)
  849. {
  850. int n = c - OP_BRA;
  851. if (n > EXTRACT_BASIC_MAX) n = GET2(code, 2+LINK_SIZE);
  852. if (n == number) return (uschar *)code;
  853. code += _pcre_OP_lengths[OP_BRA];
  854. }
  855. else
  856. {
  857. code += _pcre_OP_lengths[c];
  858. #ifdef SUPPORT_UTF8
  859. /* In UTF-8 mode, opcodes that are followed by a character may be followed
  860. by a multi-byte character. The length in the table is a minimum, so we have
  861. to scan along to skip the extra bytes. All opcodes are less than 128, so we
  862. can use relatively efficient code. */
  863. if (utf8) switch(c)
  864. {
  865. case OP_CHAR:
  866. case OP_CHARNC:
  867. case OP_EXACT:
  868. case OP_UPTO:
  869. case OP_MINUPTO:
  870. case OP_STAR:
  871. case OP_MINSTAR:
  872. case OP_PLUS:
  873. case OP_MINPLUS:
  874. case OP_QUERY:
  875. case OP_MINQUERY:
  876. while ((*code & 0xc0) == 0x80) code++;
  877. break;
  878. /* XCLASS is used for classes that cannot be represented just by a bit
  879. map. This includes negated single high-valued characters. The length in
  880. the table is zero; the actual length is stored in the compiled code. */
  881. case OP_XCLASS:
  882. code += GET(code, 1) + 1;
  883. break;
  884. }
  885. #endif
  886. }
  887. }
  888. }
  889. /*************************************************
  890. * Scan compiled regex for recursion reference *
  891. *************************************************/
  892. /* This little function scans through a compiled pattern until it finds an
  893. instance of OP_RECURSE.
  894. Arguments:
  895. code points to start of expression
  896. utf8 TRUE in UTF-8 mode
  897. Returns: pointer to the opcode for OP_RECURSE, or NULL if not found
  898. */
  899. static const uschar *
  900. find_recurse(const uschar *code, BOOL utf8)
  901. {
  902. #ifndef SUPPORT_UTF8
  903. utf8 = utf8; /* Stop pedantic compilers complaining */
  904. #endif
  905. for (;;)
  906. {
  907. register int c = *code;
  908. if (c == OP_END) return NULL;
  909. else if (c == OP_RECURSE) return code;
  910. else if (c > OP_BRA)
  911. {
  912. code += _pcre_OP_lengths[OP_BRA];
  913. }
  914. else
  915. {
  916. code += _pcre_OP_lengths[c];
  917. #ifdef SUPPORT_UTF8
  918. /* In UTF-8 mode, opcodes that are followed by a character may be followed
  919. by a multi-byte character. The length in the table is a minimum, so we have
  920. to scan along to skip the extra bytes. All opcodes are less than 128, so we
  921. can use relatively efficient code. */
  922. if (utf8) switch(c)
  923. {
  924. case OP_CHAR:
  925. case OP_CHARNC:
  926. case OP_EXACT:
  927. case OP_UPTO:
  928. case OP_MINUPTO:
  929. case OP_STAR:
  930. case OP_MINSTAR:
  931. case OP_PLUS:
  932. case OP_MINPLUS:
  933. case OP_QUERY:
  934. case OP_MINQUERY:
  935. while ((*code & 0xc0) == 0x80) code++;
  936. break;
  937. /* XCLASS is used for classes that cannot be represented just by a bit
  938. map. This includes negated single high-valued characters. The length in
  939. the table is zero; the actual length is stored in the compiled code. */
  940. case OP_XCLASS:
  941. code += GET(code, 1) + 1;
  942. break;
  943. }
  944. #endif
  945. }
  946. }
  947. }
  948. /*************************************************
  949. * Scan compiled branch for non-emptiness *
  950. *************************************************/
  951. /* This function scans through a branch of a compiled pattern to see whether it
  952. can match the empty string or not. It is called only from could_be_empty()
  953. below. Note that first_significant_code() skips over assertions. If we hit an
  954. unclosed bracket, we return "empty" - this means we've struck an inner bracket
  955. whose current branch will already have been scanned.
  956. Arguments:
  957. code points to start of search
  958. endcode points to where to stop
  959. utf8 TRUE if in UTF8 mode
  960. Returns: TRUE if what is matched could be empty
  961. */
  962. static BOOL
  963. could_be_empty_branch(const uschar *code, const uschar *endcode, BOOL utf8)
  964. {
  965. register int c;
  966. for (code = first_significant_code(code + 1 + LINK_SIZE, NULL, 0, TRUE);
  967. code < endcode;
  968. code = first_significant_code(code + _pcre_OP_lengths[c], NULL, 0, TRUE))
  969. {
  970. const uschar *ccode;
  971. c = *code;
  972. if (c >= OP_BRA)
  973. {
  974. BOOL empty_branch;
  975. if (GET(code, 1) == 0) return TRUE; /* Hit unclosed bracket */
  976. /* Scan a closed bracket */
  977. empty_branch = FALSE;
  978. do
  979. {
  980. if (!empty_branch && could_be_empty_branch(code, endcode, utf8))
  981. empty_branch = TRUE;
  982. code += GET(code, 1);
  983. }
  984. while (*code == OP_ALT);
  985. if (!empty_branch) return FALSE; /* All branches are non-empty */
  986. code += 1 + LINK_SIZE;
  987. c = *code;
  988. }
  989. else switch (c)
  990. {
  991. /* Check for quantifiers after a class */
  992. #ifdef SUPPORT_UTF8
  993. case OP_XCLASS:
  994. ccode = code + GET(code, 1);
  995. goto CHECK_CLASS_REPEAT;
  996. #endif
  997. case OP_CLASS:
  998. case OP_NCLASS:
  999. ccode = code + 33;
  1000. #ifdef SUPPORT_UTF8
  1001. CHECK_CLASS_REPEAT:
  1002. #endif
  1003. switch (*ccode)
  1004. {
  1005. case OP_CRSTAR: /* These could be empty; continue */
  1006. case OP_CRMINSTAR:
  1007. case OP_CRQUERY:
  1008. case OP_CRMINQUERY:
  1009. break;
  1010. default: /* Non-repeat => class must match */
  1011. case OP_CRPLUS: /* These repeats aren't empty */
  1012. case OP_CRMINPLUS:
  1013. return FALSE;
  1014. case OP_CRRANGE:
  1015. case OP_CRMINRANGE:
  1016. if (GET2(ccode, 1) > 0) return FALSE; /* Minimum > 0 */
  1017. break;
  1018. }
  1019. break;
  1020. /* Opcodes that must match a character */
  1021. case OP_PROP:
  1022. case OP_NOTPROP:
  1023. case OP_EXTUNI:
  1024. case OP_NOT_DIGIT:
  1025. case OP_DIGIT:
  1026. case OP_NOT_WHITESPACE:
  1027. case OP_WHITESPACE:
  1028. case OP_NOT_WORDCHAR:
  1029. case OP_WORDCHAR:
  1030. case OP_ANY:
  1031. case OP_ANYBYTE:
  1032. case OP_CHAR:
  1033. case OP_CHARNC:
  1034. case OP_NOT:
  1035. case OP_PLUS:
  1036. case OP_MINPLUS:
  1037. case OP_EXACT:
  1038. case OP_NOTPLUS:
  1039. case OP_NOTMINPLUS:
  1040. case OP_NOTEXACT:
  1041. case OP_TYPEPLUS:
  1042. case OP_TYPEMINPLUS:
  1043. case OP_TYPEEXACT:
  1044. return FALSE;
  1045. /* End of branch */
  1046. case OP_KET:
  1047. case OP_KETRMAX:
  1048. case OP_KETRMIN:
  1049. case OP_ALT:
  1050. return TRUE;
  1051. /* In UTF-8 mode, STAR, MINSTAR, QUERY, MINQUERY, UPTO, and MINUPTO may be
  1052. followed by a multibyte character */
  1053. #ifdef SUPPORT_UTF8
  1054. case OP_STAR:
  1055. case OP_MINSTAR:
  1056. case OP_QUERY:
  1057. case OP_MINQUERY:
  1058. case OP_UPTO:
  1059. case OP_MINUPTO:
  1060. if (utf8) while ((code[2] & 0xc0) == 0x80) code++;
  1061. break;
  1062. #endif
  1063. }
  1064. }
  1065. return TRUE;
  1066. }
  1067. /*************************************************
  1068. * Scan compiled regex for non-emptiness *
  1069. *************************************************/
  1070. /* This function is called to check for left recursive calls. We want to check
  1071. the current branch of the current pattern to see if it could match the empty
  1072. string. If it could, we must look outwards for branches at other levels,
  1073. stopping when we pass beyond the bracket which is the subject of the recursion.
  1074. Arguments:
  1075. code points to start of the recursion
  1076. endcode points to where to stop (current RECURSE item)
  1077. bcptr points to the chain of current (unclosed) branch starts
  1078. utf8 TRUE if in UTF-8 mode
  1079. Returns: TRUE if what is matched could be empty
  1080. */
  1081. static BOOL
  1082. could_be_empty(const uschar *code, const uschar *endcode, branch_chain *bcptr,
  1083. BOOL utf8)
  1084. {
  1085. while (bcptr != NULL && bcptr->current >= code)
  1086. {
  1087. if (!could_be_empty_branch(bcptr->current, endcode, utf8)) return FALSE;
  1088. bcptr = bcptr->outer;
  1089. }
  1090. return TRUE;
  1091. }
  1092. /*************************************************
  1093. * Check for POSIX class syntax *
  1094. *************************************************/
  1095. /* This function is called when the sequence "[:" or "[." or "[=" is
  1096. encountered in a character class. It checks whether this is followed by an
  1097. optional ^ and then a sequence of letters, terminated by a matching ":]" or
  1098. ".]" or "=]".
  1099. Argument:
  1100. ptr pointer to the initial [
  1101. endptr where to return the end pointer
  1102. cd pointer to compile data
  1103. Returns: TRUE or FALSE
  1104. */
  1105. static BOOL
  1106. check_posix_syntax(const uschar *ptr, const uschar **endptr, compile_data *cd)
  1107. {
  1108. int terminator; /* Don't combine these lines; the Solaris cc */
  1109. terminator = *(++ptr); /* compiler warns about "non-constant" initializer. */
  1110. if (*(++ptr) == '^') ptr++;
  1111. while ((cd->ctypes[*ptr] & ctype_letter) != 0) ptr++;
  1112. if (*ptr == terminator && ptr[1] == ']')
  1113. {
  1114. *endptr = ptr;
  1115. return TRUE;
  1116. }
  1117. return FALSE;
  1118. }
  1119. /*************************************************
  1120. * Check POSIX class name *
  1121. *************************************************/
  1122. /* This function is called to check the name given in a POSIX-style class entry
  1123. such as [:alnum:].
  1124. Arguments:
  1125. ptr points to the first letter
  1126. len the length of the name
  1127. Returns: a value representing the name, or -1 if unknown
  1128. */
  1129. static int
  1130. check_posix_name(const uschar *ptr, int len)
  1131. {
  1132. register int yield = 0;
  1133. while (posix_name_lengths[yield] != 0)
  1134. {
  1135. if (len == posix_name_lengths[yield] &&
  1136. strncmp((const char *)ptr, posix_names[yield], len) == 0) return yield;
  1137. yield++;
  1138. }
  1139. return -1;
  1140. }
  1141. /*************************************************
  1142. * Adjust OP_RECURSE items in repeated group *
  1143. *************************************************/
  1144. /* OP_RECURSE items contain an offset from the start of the regex to the group
  1145. that is referenced. This means that groups can be replicated for fixed
  1146. repetition simply by copying (because the recursion is allowed to refer to
  1147. earlier groups that are outside the current group). However, when a group is
  1148. optional (i.e. the minimum quantifier is zero), OP_BRAZERO is inserted before
  1149. it, after it has been compiled. This means that any OP_RECURSE items within it
  1150. that refer to the group itself or any contained groups have to have their
  1151. offsets adjusted. That is the job of this function. Before it is called, the
  1152. partially compiled regex must be temporarily terminated with OP_END.
  1153. Arguments:
  1154. group points to the start of the group
  1155. adjust the amount by which the group is to be moved
  1156. utf8 TRUE in UTF-8 mode
  1157. cd contains pointers to tables etc.
  1158. Returns: nothing
  1159. */
  1160. static void
  1161. adjust_recurse(uschar *group, int adjust, BOOL utf8, compile_data *cd)
  1162. {
  1163. uschar *ptr = group;
  1164. while ((ptr = (uschar *)find_recurse(ptr, utf8)) != NULL)
  1165. {
  1166. int offset = GET(ptr, 1);
  1167. if (cd->start_code + offset >= group) PUT(ptr, 1, offset + adjust);
  1168. ptr += 1 + LINK_SIZE;
  1169. }
  1170. }
  1171. /*************************************************
  1172. * Insert an automatic callout point *
  1173. *************************************************/
  1174. /* This function is called when the PCRE_AUTO_CALLOUT option is set, to insert
  1175. callout points before each pattern item.
  1176. Arguments:
  1177. code current code pointer
  1178. ptr current pattern pointer
  1179. cd pointers to tables etc
  1180. Returns: new code pointer
  1181. */
  1182. static uschar *
  1183. auto_callout(uschar *code, const uschar *ptr, compile_data *cd)
  1184. {
  1185. *code++ = OP_CALLOUT;
  1186. *code++ = 255;
  1187. PUT(code, 0, ptr - cd->start_pattern); /* Pattern offset */
  1188. PUT(code, LINK_SIZE, 0); /* Default length */
  1189. return code + 2*LINK_SIZE;
  1190. }
  1191. /*************************************************
  1192. * Complete a callout item *
  1193. *************************************************/
  1194. /* A callout item contains the length of the next item in the pattern, which
  1195. we can't fill in till after we have reached the relevant point. This is used
  1196. for both automatic and manual callouts.
  1197. Arguments:
  1198. previous_callout points to previous callout item
  1199. ptr current pattern pointer
  1200. cd pointers to tables etc
  1201. Returns: nothing
  1202. */
  1203. static void
  1204. complete_callout(uschar *previous_callout, const uschar *ptr, compile_data *cd)
  1205. {
  1206. int length = ptr - cd->start_pattern - GET(previous_callout, 2);
  1207. PUT(previous_callout, 2 + LINK_SIZE, length);
  1208. }
  1209. #ifdef SUPPORT_UCP
  1210. /*************************************************
  1211. * Get othercase range *
  1212. *************************************************/
  1213. /* This function is passed the start and end of a class range, in UTF-8 mode
  1214. with UCP support. It searches up the characters, looking for internal ranges of
  1215. characters in the "other" case. Each call returns the next one, updating the
  1216. start address.
  1217. Arguments:
  1218. cptr points to starting character value; updated
  1219. d end value
  1220. ocptr where to put start of othercase range
  1221. odptr where to put end of othercase range
  1222. Yield: TRUE when range returned; FALSE when no more
  1223. */
  1224. static BOOL
  1225. get_othercase_range(int *cptr, int d, int *ocptr, int *odptr)
  1226. {
  1227. int c, chartype, othercase, next;
  1228. for (c = *cptr; c <= d; c++)
  1229. {
  1230. if (_pcre_ucp_findchar(c, &chartype, &othercase) == ucp_L && othercase != 0)
  1231. break;
  1232. }
  1233. if (c > d) return FALSE;
  1234. *ocptr = othercase;
  1235. next = othercase + 1;
  1236. for (++c; c <= d; c++)
  1237. {
  1238. if (_pcre_ucp_findchar(c, &chartype, &othercase) != ucp_L ||
  1239. othercase != next)
  1240. break;
  1241. next++;
  1242. }
  1243. *odptr = next - 1;
  1244. *cptr = c;
  1245. return TRUE;
  1246. }
  1247. #endif /* SUPPORT_UCP */
  1248. /*************************************************
  1249. * Compile one branch *
  1250. *************************************************/
  1251. /* Scan the pattern, compiling it into the code vector. If the options are
  1252. changed during the branch, the pointer is used to change the external options
  1253. bits.
  1254. Arguments:
  1255. optionsptr pointer to the option bits
  1256. brackets points to number of extracting brackets used
  1257. codeptr points to the pointer to the current code point
  1258. ptrptr points to the current pattern pointer
  1259. errorcodeptr points to error code variable
  1260. firstbyteptr set to initial literal character, or < 0 (REQ_UNSET, REQ_NONE)
  1261. reqbyteptr set to the last literal character required, else < 0
  1262. bcptr points to current branch chain
  1263. cd contains pointers to tables etc.
  1264. Returns: TRUE on success
  1265. FALSE, with *errorcodeptr set non-zero on error
  1266. */
  1267. static BOOL
  1268. compile_branch(int *optionsptr, int *brackets, uschar **codeptr,
  1269. const uschar **ptrptr, int *errorcodeptr, int *firstbyteptr,
  1270. int *reqbyteptr, branch_chain *bcptr, compile_data *cd)
  1271. {
  1272. int repeat_type, op_type;
  1273. int repeat_min = 0, repeat_max = 0; /* To please picky compilers */
  1274. int bravalue = 0;
  1275. int greedy_default, greedy_non_default;
  1276. int firstbyte, reqbyte;
  1277. int zeroreqbyte, zerofirstbyte;
  1278. int req_caseopt, reqvary, tempreqvary;
  1279. int condcount = 0;
  1280. int options = *optionsptr;
  1281. int after_manual_callout = 0;
  1282. register int c;
  1283. register uschar *code = *codeptr;
  1284. uschar *tempcode;
  1285. BOOL inescq = FALSE;
  1286. BOOL groupsetfirstbyte = FALSE;
  1287. const uschar *ptr = *ptrptr;
  1288. const uschar *tempptr;
  1289. uschar *previous = NULL;
  1290. uschar *previous_callout = NULL;
  1291. uschar classbits[32];
  1292. #ifdef SUPPORT_UTF8
  1293. BOOL class_utf8;
  1294. BOOL utf8 = (options & PCRE_UTF8) != 0;
  1295. uschar *class_utf8data;
  1296. uschar utf8_char[6];
  1297. #else
  1298. BOOL utf8 = FALSE;
  1299. #endif
  1300. /* Set up the default and non-default settings for greediness */
  1301. greedy_default = ((options & PCRE_UNGREEDY) != 0);
  1302. greedy_non_default = greedy_default ^ 1;
  1303. /* Initialize no first byte, no required byte. REQ_UNSET means "no char
  1304. matching encountered yet". It gets changed to REQ_NONE if we hit something that
  1305. matches a non-fixed char first char; reqbyte just remains unset if we never
  1306. find one.
  1307. When we hit a repeat whose minimum is zero, we may have to adjust these values
  1308. to take the zero repeat into account. This is implemented by setting them to
  1309. zerofirstbyte and zeroreqbyte when such a repeat is encountered. The individual
  1310. item types that can be repeated set these backoff variables appropriately. */
  1311. firstbyte = reqbyte = zerofirstbyte = zeroreqbyte = REQ_UNSET;
  1312. /* The variable req_caseopt contains either the REQ_CASELESS value or zero,
  1313. according to the current setting of the caseless flag. REQ_CASELESS is a bit
  1314. value > 255. It is added into the firstbyte or reqbyte variables to record the
  1315. case status of the value. This is used only for ASCII characters. */
  1316. req_caseopt = ((options & PCRE_CASELESS) != 0)? REQ_CASELESS : 0;
  1317. /* Switch on next character until the end of the branch */
  1318. for (;; ptr++)
  1319. {
  1320. BOOL negate_class;
  1321. BOOL possessive_quantifier;
  1322. BOOL is_quantifier;
  1323. int class_charcount;
  1324. int class_lastchar;
  1325. int newoptions;
  1326. int recno;
  1327. int skipbytes;
  1328. int subreqbyte;
  1329. int subfirstbyte;
  1330. int mclength;
  1331. uschar mcbuffer[8];
  1332. /* Next byte in the pattern */
  1333. c = *ptr;
  1334. /* If in \Q...\E, check for the end; if not, we have a literal */
  1335. if (inescq && c != 0)
  1336. {
  1337. if (c == '\\' && ptr[1] == 'E')
  1338. {
  1339. inescq = FALSE;
  1340. ptr++;
  1341. continue;
  1342. }
  1343. else
  1344. {
  1345. if (previous_callout != NULL)
  1346. {
  1347. complete_callout(previous_callout, ptr, cd);
  1348. previous_callout = NULL;
  1349. }
  1350. if ((options & PCRE_AUTO_CALLOUT) != 0)
  1351. {
  1352. previous_callout = code;
  1353. code = auto_callout(code, ptr, cd);
  1354. }
  1355. goto NORMAL_CHAR;
  1356. }
  1357. }
  1358. /* Fill in length of a previous callout, except when the next thing is
  1359. a quantifier. */
  1360. is_quantifier = c == '*' || c == '+' || c == '?' ||
  1361. (c == '{' && is_counted_repeat(ptr+1));
  1362. if (!is_quantifier && previous_callout != NULL &&
  1363. after_manual_callout-- <= 0)
  1364. {
  1365. complete_callout(previous_callout, ptr, cd);
  1366. previous_callout = NULL;
  1367. }
  1368. /* In extended mode, skip white space and comments */
  1369. if ((options & PCRE_EXTENDED) != 0)
  1370. {
  1371. if ((cd->ctypes[c] & ctype_space) != 0) continue;
  1372. if (c == '#')
  1373. {
  1374. /* The space before the ; is to avoid a warning on a silly compiler
  1375. on the Macintosh. */
  1376. while ((c = *(++ptr)) != 0 && c != NEWLINE) ;
  1377. if (c != 0) continue; /* Else fall through to handle end of string */
  1378. }
  1379. }
  1380. /* No auto callout for quantifiers. */
  1381. if ((options & PCRE_AUTO_CALLOUT) != 0 && !is_quantifier)
  1382. {
  1383. previous_callout = code;
  1384. code = auto_callout(code, ptr, cd);
  1385. }
  1386. switch(c)
  1387. {
  1388. /* The branch terminates at end of string, |, or ). */
  1389. case 0:
  1390. case '|':
  1391. case ')':
  1392. *firstbyteptr = firstbyte;
  1393. *reqbyteptr = reqbyte;
  1394. *codeptr = code;
  1395. *ptrptr = ptr;
  1396. return TRUE;
  1397. /* Handle single-character metacharacters. In multiline mode, ^ disables
  1398. the setting of any following char as a first character. */
  1399. case '^':
  1400. if ((options & PCRE_MULTILINE) != 0)
  1401. {
  1402. if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;
  1403. }
  1404. previous = NULL;
  1405. *code++ = OP_CIRC;
  1406. break;
  1407. case '$':
  1408. previous = NULL;
  1409. *code++ = OP_DOLL;
  1410. break;
  1411. /* There can never be a first char if '.' is first, whatever happens about
  1412. repeats. The value of reqbyte doesn't change either. */
  1413. case '.':
  1414. if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;
  1415. zerofirstbyte = firstbyte;
  1416. zeroreqbyte = reqbyte;
  1417. previous = code;
  1418. *code++ = OP_ANY;
  1419. break;
  1420. /* Character classes. If the included characters are all < 255 in value, we
  1421. build a 32-byte bitmap of the permitted characters, except in the special
  1422. case where there is only one such character. For negated classes, we build
  1423. the map as usual, then invert it at the end. However, we use a different
  1424. opcode so that data characters > 255 can be handled correctly.
  1425. If the class contains characters outside the 0-255 range, a different
  1426. opcode is compiled. It may optionally have a bit map for characters < 256,
  1427. but those above are are explicitly listed afterwards. A flag byte tells
  1428. whether the bitmap is present, and whether this is a negated class or not.
  1429. */
  1430. case '[':
  1431. previous = code;
  1432. /* PCRE supports POSIX class stuff inside a class. Perl gives an error if
  1433. they are encountered at the top level, so we'll do that too. */
  1434. if ((ptr[1] == ':' || ptr[1] == '.' || ptr[1] == '=') &&
  1435. check_posix_syntax(ptr, &tempptr, cd))
  1436. {
  1437. *errorcodeptr = (ptr[1] == ':')? ERR13 : ERR31;
  1438. goto FAILED;
  1439. }
  1440. /* If the first character is '^', set the negation flag and skip it. */
  1441. if ((c = *(++ptr)) == '^')
  1442. {
  1443. negate_class = TRUE;
  1444. c = *(++ptr);
  1445. }
  1446. else
  1447. {
  1448. negate_class = FALSE;
  1449. }
  1450. /* Keep a count of chars with values < 256 so that we can optimize the case
  1451. of just a single character (as long as it's < 256). For higher valued UTF-8
  1452. characters, we don't yet do any optimization. */
  1453. class_charcount = 0;
  1454. class_lastchar = -1;
  1455. #ifdef SUPPORT_UTF8
  1456. class_utf8 = FALSE; /* No chars >= 256 */
  1457. class_utf8data = code + LINK_SIZE + 34; /* For UTF-8 items */
  1458. #endif
  1459. /* Initialize the 32-char bit map to all zeros. We have to build the
  1460. map in a temporary bit of store, in case the class contains only 1
  1461. character (< 256), because in that case the compiled code doesn't use the
  1462. bit map. */
  1463. memset(classbits, 0, 32 * sizeof(uschar));
  1464. /* Process characters until ] is reached. By writing this as a "do" it
  1465. means that an initial ] is taken as a data character. The first pass
  1466. through the regex checked the overall syntax, so we don't need to be very
  1467. strict here. At the start of the loop, c contains the first byte of the
  1468. character. */
  1469. do
  1470. {
  1471. #ifdef SUPPORT_UTF8
  1472. if (utf8 && c > 127)
  1473. { /* Braces are required because the */
  1474. GETCHARLEN(c, ptr, ptr); /* macro generates multiple statements */
  1475. }
  1476. #endif
  1477. /* Inside \Q...\E everything is literal except \E */
  1478. if (inescq)
  1479. {
  1480. if (c == '\\' && ptr[1] == 'E')
  1481. {
  1482. inescq = FALSE;
  1483. ptr++;
  1484. continue;
  1485. }
  1486. else goto LONE_SINGLE_CHARACTER;
  1487. }
  1488. /* Handle POSIX class names. Perl allows a negation extension of the
  1489. form [:^name:]. A square bracket that doesn't match the syntax is
  1490. treated as a literal. We also recognize the POSIX constructions
  1491. [.ch.] and [=ch=] ("collating elements") and fault them, as Perl
  1492. 5.6 and 5.8 do. */
  1493. if (c == '[' &&
  1494. (ptr[1] == ':' || ptr[1] == '.' || ptr[1] == '=') &&
  1495. check_posix_syntax(ptr, &tempptr, cd))
  1496. {
  1497. BOOL local_negate = FALSE;
  1498. int posix_class, i;
  1499. register const uschar *cbits = cd->cbits;
  1500. if (ptr[1] != ':')
  1501. {
  1502. *errorcodeptr = ERR31;
  1503. goto FAILED;
  1504. }
  1505. ptr += 2;
  1506. if (*ptr == '^')
  1507. {
  1508. local_negate = TRUE;
  1509. ptr++;
  1510. }
  1511. posix_class = check_posix_name(ptr, tempptr - ptr);
  1512. if (posix_class < 0)
  1513. {
  1514. *errorcodeptr = ERR30;
  1515. goto FAILED;
  1516. }
  1517. /* If matching is caseless, upper and lower are converted to
  1518. alpha. This relies on the fact that the class table starts with
  1519. alpha, lower, upper as the first 3 entries. */
  1520. if ((options & PCRE_CASELESS) != 0 && posix_class <= 2)
  1521. posix_class = 0;
  1522. /* Or into the map we are building up to 3 of the static class
  1523. tables, or their negations. The [:blank:] class sets up the same
  1524. chars as the [:space:] class (all white space). We remove the vertical
  1525. white space chars afterwards. */
  1526. posix_class *= 3;
  1527. for (i = 0; i < 3; i++)
  1528. {
  1529. BOOL blankclass = strncmp((char *)ptr, "blank", 5) == 0;
  1530. int taboffset = posix_class_maps[posix_class + i];
  1531. if (taboffset < 0) break;
  1532. if (local_negate)
  1533. {
  1534. if (i == 0)
  1535. for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+taboffset];
  1536. else
  1537. for (c = 0; c < 32; c++) classbits[c] &= ~cbits[c+taboffset];
  1538. if (blankclass) classbits[1] |= 0x3c;
  1539. }
  1540. else
  1541. {
  1542. for (c = 0; c < 32; c++) classbits[c] |= cbits[c+taboffset];
  1543. if (blankclass) classbits[1] &= ~0x3c;
  1544. }
  1545. }
  1546. ptr = tempptr + 1;
  1547. class_charcount = 10; /* Set > 1; assumes more than 1 per class */
  1548. continue; /* End of POSIX syntax handling */
  1549. }
  1550. /* Backslash may introduce a single character, or it may introduce one
  1551. of the specials, which just set a flag. Escaped items are checked for
  1552. validity in the pre-compiling pass. The sequence \b is a special case.
  1553. Inside a class (and only there) it is treated as backspace. Elsewhere
  1554. it marks a word boundary. Other escapes have preset maps ready to
  1555. or into the one we are building. We assume they have more than one
  1556. character in them, so set class_charcount bigger than one. */
  1557. if (c == '\\')
  1558. {
  1559. c = check_escape(&ptr, errorcodeptr, *brackets, options, TRUE);
  1560. if (-c == ESC_b) c = '\b'; /* \b is backslash in a class */
  1561. else if (-c == ESC_X) c = 'X'; /* \X is literal X in a class */
  1562. else if (-c == ESC_Q) /* Handle start of quoted string */
  1563. {
  1564. if (ptr[1] == '\\' && ptr[2] == 'E')
  1565. {
  1566. ptr += 2; /* avoid empty string */
  1567. }
  1568. else inescq = TRUE;
  1569. continue;
  1570. }
  1571. if (c < 0)
  1572. {
  1573. register const uschar *cbits = cd->cbits;
  1574. class_charcount += 2; /* Greater than 1 is what matters */
  1575. switch (-c)
  1576. {
  1577. case ESC_d:
  1578. for (c = 0; c < 32; c++) classbits[c] |= cbits[c+cbit_digit];
  1579. continue;
  1580. case ESC_D:
  1581. for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_digit];
  1582. continue;
  1583. case ESC_w:
  1584. for (c = 0; c < 32; c++) classbits[c] |= cbits[c+cbit_word];
  1585. continue;
  1586. case ESC_W:
  1587. for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_word];
  1588. continue;
  1589. case ESC_s:
  1590. for (c = 0; c < 32; c++) classbits[c] |= cbits[c+cbit_space];
  1591. classbits[1] &= ~0x08; /* Perl 5.004 onwards omits VT from \s */
  1592. continue;
  1593. case ESC_S:
  1594. for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_space];
  1595. classbits[1] |= 0x08; /* Perl 5.004 onwards omits VT from \s */
  1596. continue;
  1597. #ifdef SUPPORT_UCP
  1598. case ESC_p:
  1599. case ESC_P:
  1600. {
  1601. BOOL negated;
  1602. int property = get_ucp(&ptr, &negated, errorcodeptr);
  1603. if (property < 0) goto FAILED;
  1604. class_utf8 = TRUE;
  1605. *class_utf8data++ = ((-c == ESC_p) != negated)?
  1606. XCL_PROP : XCL_NOTPROP;
  1607. *class_utf8data++ = property;
  1608. class_charcount -= 2; /* Not a < 256 character */
  1609. }
  1610. continue;
  1611. #endif
  1612. /* Unrecognized escapes are faulted if PCRE is running in its
  1613. strict mode. By default, for compatibility with Perl, they are
  1614. treated as literals. */
  1615. default:
  1616. if ((options & PCRE_EXTRA) != 0)
  1617. {
  1618. *errorcodeptr = ERR7;
  1619. goto FAILED;
  1620. }
  1621. c = *ptr; /* The final character */
  1622. class_charcount -= 2; /* Undo the default count from above */
  1623. }
  1624. }
  1625. /* Fall through if we have a single character (c >= 0). This may be
  1626. > 256 in UTF-8 mode. */
  1627. } /* End of backslash handling */
  1628. /* A single character may be followed by '-' to form a range. However,
  1629. Perl does not permit ']' to be the end of the range. A '-' character
  1630. here is treated as a literal. */
  1631. if (ptr[1] == '-' && ptr[2] != ']')
  1632. {
  1633. int d;
  1634. ptr += 2;
  1635. #ifdef SUPPORT_UTF8
  1636. if (utf8)
  1637. { /* Braces are required because the */
  1638. GETCHARLEN(d, ptr, ptr); /* macro generates multiple statements */
  1639. }
  1640. else
  1641. #endif
  1642. d = *ptr; /* Not UTF-8 mode */
  1643. /* The second part of a range can be a single-character escape, but
  1644. not any of the other escapes. Perl 5.6 treats a hyphen as a literal
  1645. in such circumstances. */
  1646. if (d == '\\')
  1647. {
  1648. const uschar *oldptr = ptr;
  1649. d = check_escape(&ptr, errorcodeptr, *brackets, options, TRUE);
  1650. /* \b is backslash; \X is literal X; any other special means the '-'
  1651. was literal */
  1652. if (d < 0)
  1653. {
  1654. if (d == -ESC_b) d = '\b';
  1655. else if (d == -ESC_X) d = 'X'; else
  1656. {
  1657. ptr = oldptr - 2;
  1658. goto LONE_SINGLE_CHARACTER; /* A few lines below */
  1659. }
  1660. }
  1661. }
  1662. /* The check that the two values are in the correct order happens in
  1663. the pre-pass. Optimize one-character ranges */
  1664. if (d == c) goto LONE_SINGLE_CHARACTER; /* A few lines below */
  1665. /* In UTF-8 mode, if the upper limit is > 255, or > 127 for caseless
  1666. matching, we have to use an XCLASS with extra data items. Caseless
  1667. matching for characters > 127 is available only if UCP support is
  1668. available. */
  1669. #ifdef SUPPORT_UTF8
  1670. if (utf8 && (d > 255 || ((options & PCRE_CASELESS) != 0 && d > 127)))
  1671. {
  1672. class_utf8 = TRUE;
  1673. /* With UCP support, we can find the other case equivalents of
  1674. the relevant characters. There may be several ranges. Optimize how
  1675. they fit with the basic range. */
  1676. #ifdef SUPPORT_UCP
  1677. if ((options & PCRE_CASELESS) != 0)
  1678. {
  1679. int occ, ocd;
  1680. int cc = c;
  1681. int origd = d;
  1682. while (get_othercase_range(&cc, origd, &occ, &ocd))
  1683. {
  1684. if (occ >= c && ocd <= d) continue; /* Skip embedded ranges */
  1685. if (occ < c && ocd >= c - 1) /* Extend the basic range */
  1686. { /* if there is overlap, */
  1687. c = occ; /* noting that if occ < c */
  1688. continue; /* we can't have ocd > d */
  1689. } /* because a subrange is */
  1690. if (ocd > d && occ <= d + 1) /* always shorter than */
  1691. { /* the basic range. */
  1692. d = ocd;
  1693. continue;
  1694. }
  1695. if (occ == ocd)
  1696. {
  1697. *class_utf8data++ = XCL_SINGLE;
  1698. }
  1699. else
  1700. {
  1701. *class_utf8data++ = XCL_RANGE;
  1702. class_utf8data += _pcre_ord2utf8(occ, class_utf8data);
  1703. }
  1704. class_utf8data += _pcre_ord2utf8(ocd, class_utf8data);
  1705. }
  1706. }
  1707. #endif /* SUPPORT_UCP */
  1708. /* Now record the original range, possibly modified for UCP caseless
  1709. overlapping ranges. */
  1710. *class_utf8data++ = XCL_RANGE;
  1711. class_utf8data += _pcre_ord2utf8(c, class_utf8data);
  1712. class_utf8data += _pcre_ord2utf8(d, class_utf8data);
  1713. /* With UCP support, we are done. Without UCP support, there is no
  1714. caseless matching for UTF-8 characters > 127; we can use the bit map
  1715. for the smaller ones. */
  1716. #ifdef SUPPORT_UCP
  1717. continue; /* With next character in the class */
  1718. #else
  1719. if ((options & PCRE_CASELESS) == 0 || c > 127) continue;
  1720. /* Adjust upper limit and fall through to set up the map */
  1721. d = 127;
  1722. #endif /* SUPPORT_UCP */
  1723. }
  1724. #endif /* SUPPORT_UTF8 */
  1725. /* We use the bit map for all cases when not in UTF-8 mode; else
  1726. ranges that lie entirely within 0-127 when there is UCP support; else
  1727. for partial ranges without UCP support. */
  1728. for (; c <= d; c++)
  1729. {
  1730. classbits[c/8] |= (1 << (c&7));
  1731. if ((options & PCRE_CASELESS) != 0)
  1732. {
  1733. int