pcreposix.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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-2020 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 is a wrapper that provides a POSIX API to the underlying PCRE
  33. functions. */
  34. #ifdef HAVE_CONFIG_H
  35. #include "pcre_config.h"
  36. #endif
  37. /* Ensure that the PCREPOSIX_EXP_xxx macros are set appropriately for
  38. compiling these functions. This must come before including pcreposix.h, where
  39. they are set for an application (using these functions) if they have not
  40. previously been set. */
  41. #if defined(_WIN32) && !defined(PCRE_STATIC)
  42. # define PCREPOSIX_EXP_DECL extern __declspec(dllexport)
  43. # define PCREPOSIX_EXP_DEFN __declspec(dllexport)
  44. #endif
  45. /* We include pcre.h before pcre_internal.h so that the PCRE library functions
  46. are declared as "import" for Windows by defining PCRE_EXP_DECL as "import".
  47. This is needed even though pcre_internal.h itself includes pcre.h, because it
  48. does so after it has set PCRE_EXP_DECL to "export" if it is not already set. */
  49. #include "pcre.h"
  50. #include "pcre_internal.h"
  51. #include "pcreposix.h"
  52. /* Table to translate PCRE compile time error codes into POSIX error codes. */
  53. static const int eint[] = {
  54. 0, /* no error */
  55. REG_EESCAPE, /* \ at end of pattern */
  56. REG_EESCAPE, /* \c at end of pattern */
  57. REG_EESCAPE, /* unrecognized character follows \ */
  58. REG_BADBR, /* numbers out of order in {} quantifier */
  59. /* 5 */
  60. REG_BADBR, /* number too big in {} quantifier */
  61. REG_EBRACK, /* missing terminating ] for character class */
  62. REG_ECTYPE, /* invalid escape sequence in character class */
  63. REG_ERANGE, /* range out of order in character class */
  64. REG_BADRPT, /* nothing to repeat */
  65. /* 10 */
  66. REG_BADRPT, /* operand of unlimited repeat could match the empty string */
  67. REG_ASSERT, /* internal error: unexpected repeat */
  68. REG_BADPAT, /* unrecognized character after (? */
  69. REG_BADPAT, /* POSIX named classes are supported only within a class */
  70. REG_EPAREN, /* missing ) */
  71. /* 15 */
  72. REG_ESUBREG, /* reference to non-existent subpattern */
  73. REG_INVARG, /* erroffset passed as NULL */
  74. REG_INVARG, /* unknown option bit(s) set */
  75. REG_EPAREN, /* missing ) after comment */
  76. REG_ESIZE, /* parentheses nested too deeply */
  77. /* 20 */
  78. REG_ESIZE, /* regular expression too large */
  79. REG_ESPACE, /* failed to get memory */
  80. REG_EPAREN, /* unmatched parentheses */
  81. REG_ASSERT, /* internal error: code overflow */
  82. REG_BADPAT, /* unrecognized character after (?< */
  83. /* 25 */
  84. REG_BADPAT, /* lookbehind assertion is not fixed length */
  85. REG_BADPAT, /* malformed number or name after (?( */
  86. REG_BADPAT, /* conditional group contains more than two branches */
  87. REG_BADPAT, /* assertion expected after (?( */
  88. REG_BADPAT, /* (?R or (?[+-]digits must be followed by ) */
  89. /* 30 */
  90. REG_ECTYPE, /* unknown POSIX class name */
  91. REG_BADPAT, /* POSIX collating elements are not supported */
  92. REG_INVARG, /* this version of PCRE is not compiled with PCRE_UTF8 support */
  93. REG_BADPAT, /* spare error */
  94. REG_BADPAT, /* character value in \x{} or \o{} is too large */
  95. /* 35 */
  96. REG_BADPAT, /* invalid condition (?(0) */
  97. REG_BADPAT, /* \C not allowed in lookbehind assertion */
  98. REG_EESCAPE, /* PCRE does not support \L, \l, \N, \U, or \u */
  99. REG_BADPAT, /* number after (?C is > 255 */
  100. REG_BADPAT, /* closing ) for (?C expected */
  101. /* 40 */
  102. REG_BADPAT, /* recursive call could loop indefinitely */
  103. REG_BADPAT, /* unrecognized character after (?P */
  104. REG_BADPAT, /* syntax error in subpattern name (missing terminator) */
  105. REG_BADPAT, /* two named subpatterns have the same name */
  106. REG_BADPAT, /* invalid UTF-8 string */
  107. /* 45 */
  108. REG_BADPAT, /* support for \P, \p, and \X has not been compiled */
  109. REG_BADPAT, /* malformed \P or \p sequence */
  110. REG_BADPAT, /* unknown property name after \P or \p */
  111. REG_BADPAT, /* subpattern name is too long (maximum 32 characters) */
  112. REG_BADPAT, /* too many named subpatterns (maximum 10,000) */
  113. /* 50 */
  114. REG_BADPAT, /* repeated subpattern is too long */
  115. REG_BADPAT, /* octal value is greater than \377 (not in UTF-8 mode) */
  116. REG_BADPAT, /* internal error: overran compiling workspace */
  117. REG_BADPAT, /* internal error: previously-checked referenced subpattern not found */
  118. REG_BADPAT, /* DEFINE group contains more than one branch */
  119. /* 55 */
  120. REG_BADPAT, /* repeating a DEFINE group is not allowed */
  121. REG_INVARG, /* inconsistent NEWLINE options */
  122. REG_BADPAT, /* \g is not followed followed by an (optionally braced) non-zero number */
  123. REG_BADPAT, /* a numbered reference must not be zero */
  124. REG_BADPAT, /* an argument is not allowed for (*ACCEPT), (*FAIL), or (*COMMIT) */
  125. /* 60 */
  126. REG_BADPAT, /* (*VERB) not recognized */
  127. REG_BADPAT, /* number is too big */
  128. REG_BADPAT, /* subpattern name expected */
  129. REG_BADPAT, /* digit expected after (?+ */
  130. REG_BADPAT, /* ] is an invalid data character in JavaScript compatibility mode */
  131. /* 65 */
  132. REG_BADPAT, /* different names for subpatterns of the same number are not allowed */
  133. REG_BADPAT, /* (*MARK) must have an argument */
  134. REG_INVARG, /* this version of PCRE is not compiled with PCRE_UCP support */
  135. REG_BADPAT, /* \c must be followed by an ASCII character */
  136. REG_BADPAT, /* \k is not followed by a braced, angle-bracketed, or quoted name */
  137. /* 70 */
  138. REG_BADPAT, /* internal error: unknown opcode in find_fixedlength() */
  139. REG_BADPAT, /* \N is not supported in a class */
  140. REG_BADPAT, /* too many forward references */
  141. REG_BADPAT, /* disallowed UTF-8/16/32 code point (>= 0xd800 && <= 0xdfff) */
  142. REG_BADPAT, /* invalid UTF-16 string (should not occur) */
  143. /* 75 */
  144. REG_BADPAT, /* overlong MARK name */
  145. REG_BADPAT, /* character value in \u.... sequence is too large */
  146. REG_BADPAT, /* invalid UTF-32 string (should not occur) */
  147. REG_BADPAT, /* setting UTF is disabled by the application */
  148. REG_BADPAT, /* non-hex character in \\x{} (closing brace missing?) */
  149. /* 80 */
  150. REG_BADPAT, /* non-octal character in \o{} (closing brace missing?) */
  151. REG_BADPAT, /* missing opening brace after \o */
  152. REG_BADPAT, /* parentheses too deeply nested */
  153. REG_BADPAT, /* invalid range in character class */
  154. REG_BADPAT, /* group name must start with a non-digit */
  155. /* 85 */
  156. REG_BADPAT, /* parentheses too deeply nested (stack check) */
  157. REG_BADPAT, /* missing digits in \x{} or \o{} */
  158. REG_BADPAT /* pattern too complicated */
  159. };
  160. /* Table of texts corresponding to POSIX error codes */
  161. static const char *const pstring[] = {
  162. "", /* Dummy for value 0 */
  163. "internal error", /* REG_ASSERT */
  164. "invalid repeat counts in {}", /* BADBR */
  165. "pattern error", /* BADPAT */
  166. "? * + invalid", /* BADRPT */
  167. "unbalanced {}", /* EBRACE */
  168. "unbalanced []", /* EBRACK */
  169. "collation error - not relevant", /* ECOLLATE */
  170. "bad class", /* ECTYPE */
  171. "bad escape sequence", /* EESCAPE */
  172. "empty expression", /* EMPTY */
  173. "unbalanced ()", /* EPAREN */
  174. "bad range inside []", /* ERANGE */
  175. "expression too big", /* ESIZE */
  176. "failed to get memory", /* ESPACE */
  177. "bad back reference", /* ESUBREG */
  178. "bad argument", /* INVARG */
  179. "match failed" /* NOMATCH */
  180. };
  181. /*************************************************
  182. * Translate error code to string *
  183. *************************************************/
  184. PCREPOSIX_EXP_DEFN size_t PCRE_CALL_CONVENTION
  185. regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size)
  186. {
  187. const char *message, *addmessage;
  188. size_t length, addlength;
  189. message = (errcode >= (int)(sizeof(pstring)/sizeof(char *)))?
  190. "unknown error code" : pstring[errcode];
  191. length = strlen(message) + 1;
  192. addmessage = " at offset ";
  193. addlength = (preg != NULL && (int)preg->re_erroffset != -1)?
  194. strlen(addmessage) + 6 : 0;
  195. if (errbuf_size > 0)
  196. {
  197. if (addlength > 0 && errbuf_size >= length + addlength)
  198. sprintf(errbuf, "%s%s%-6d", message, addmessage, (int)preg->re_erroffset);
  199. else
  200. {
  201. strncpy(errbuf, message, errbuf_size - 1);
  202. errbuf[errbuf_size-1] = 0;
  203. }
  204. }
  205. return length + addlength;
  206. }
  207. /*************************************************
  208. * Free store held by a regex *
  209. *************************************************/
  210. PCREPOSIX_EXP_DEFN void PCRE_CALL_CONVENTION
  211. regfree(regex_t *preg)
  212. {
  213. (PUBL(free))(preg->re_pcre);
  214. }
  215. /*************************************************
  216. * Compile a regular expression *
  217. *************************************************/
  218. /*
  219. Arguments:
  220. preg points to a structure for recording the compiled expression
  221. pattern the pattern to compile
  222. cflags compilation flags
  223. Returns: 0 on success
  224. various non-zero codes on failure
  225. */
  226. PCREPOSIX_EXP_DEFN int PCRE_CALL_CONVENTION
  227. regcomp(regex_t *preg, const char *pattern, int cflags)
  228. {
  229. const char *errorptr;
  230. int erroffset;
  231. int errorcode;
  232. int options = 0;
  233. int re_nsub = 0;
  234. if ((cflags & REG_ICASE) != 0) options |= PCRE_CASELESS;
  235. if ((cflags & REG_NEWLINE) != 0) options |= PCRE_MULTILINE;
  236. if ((cflags & REG_DOTALL) != 0) options |= PCRE_DOTALL;
  237. if ((cflags & REG_NOSUB) != 0) options |= PCRE_NO_AUTO_CAPTURE;
  238. if ((cflags & REG_UTF8) != 0) options |= PCRE_UTF8;
  239. if ((cflags & REG_UCP) != 0) options |= PCRE_UCP;
  240. if ((cflags & REG_UNGREEDY) != 0) options |= PCRE_UNGREEDY;
  241. preg->re_pcre = pcre_compile2(pattern, options, &errorcode, &errorptr,
  242. &erroffset, NULL);
  243. preg->re_erroffset = erroffset;
  244. /* Safety: if the error code is too big for the translation vector (which
  245. should not happen, but we all make mistakes), return REG_BADPAT. */
  246. if (preg->re_pcre == NULL)
  247. {
  248. return (errorcode < (int)(sizeof(eint)/sizeof(const int)))?
  249. eint[errorcode] : REG_BADPAT;
  250. }
  251. (void)pcre_fullinfo((const pcre *)preg->re_pcre, NULL, PCRE_INFO_CAPTURECOUNT,
  252. &re_nsub);
  253. preg->re_nsub = (size_t)re_nsub;
  254. preg->re_erroffset = (size_t)(-1); /* No meaning after successful compile */
  255. return 0;
  256. }
  257. /*************************************************
  258. * Match a regular expression *
  259. *************************************************/
  260. /* Unfortunately, PCRE requires 3 ints of working space for each captured
  261. substring, so we have to get and release working store instead of just using
  262. the POSIX structures as was done in earlier releases when PCRE needed only 2
  263. ints. However, if the number of possible capturing brackets is small, use a
  264. block of store on the stack, to reduce the use of malloc/free. The threshold is
  265. in a macro that can be changed at configure time.
  266. If REG_NOSUB was specified at compile time, the PCRE_NO_AUTO_CAPTURE flag will
  267. be set. When this is the case, the nmatch and pmatch arguments are ignored, and
  268. the only result is yes/no/error. */
  269. PCREPOSIX_EXP_DEFN int PCRE_CALL_CONVENTION
  270. regexec(const regex_t *preg, const char *string, size_t nmatch,
  271. regmatch_t pmatch[], int eflags)
  272. {
  273. int rc, so, eo;
  274. int options = 0;
  275. int *ovector = NULL;
  276. int small_ovector[POSIX_MALLOC_THRESHOLD * 3];
  277. BOOL allocated_ovector = FALSE;
  278. BOOL nosub =
  279. (REAL_PCRE_OPTIONS((const pcre *)preg->re_pcre) & PCRE_NO_AUTO_CAPTURE) != 0;
  280. if ((eflags & REG_NOTBOL) != 0) options |= PCRE_NOTBOL;
  281. if ((eflags & REG_NOTEOL) != 0) options |= PCRE_NOTEOL;
  282. if ((eflags & REG_NOTEMPTY) != 0) options |= PCRE_NOTEMPTY;
  283. /* When no string data is being returned, or no vector has been passed in which
  284. to put it, ensure that nmatch is zero. Otherwise, ensure the vector for holding
  285. the return data is large enough. */
  286. if (nosub || pmatch == NULL) nmatch = 0;
  287. else if (nmatch > 0)
  288. {
  289. if (nmatch <= POSIX_MALLOC_THRESHOLD)
  290. {
  291. ovector = &(small_ovector[0]);
  292. }
  293. else
  294. {
  295. if (nmatch > INT_MAX/(sizeof(int) * 3)) return REG_ESPACE;
  296. ovector = (int *)malloc(sizeof(int) * nmatch * 3);
  297. if (ovector == NULL) return REG_ESPACE;
  298. allocated_ovector = TRUE;
  299. }
  300. }
  301. /* REG_STARTEND is a BSD extension, to allow for non-NUL-terminated strings.
  302. The man page from OS X says "REG_STARTEND affects only the location of the
  303. string, not how it is matched". That is why the "so" value is used to bump the
  304. start location rather than being passed as a PCRE "starting offset". */
  305. if ((eflags & REG_STARTEND) != 0)
  306. {
  307. if (pmatch == NULL) return REG_INVARG;
  308. so = pmatch[0].rm_so;
  309. eo = pmatch[0].rm_eo;
  310. }
  311. else
  312. {
  313. so = 0;
  314. eo = (int)strlen(string);
  315. }
  316. rc = pcre_exec((const pcre *)preg->re_pcre, NULL, string + so, (eo - so),
  317. 0, options, ovector, (int)(nmatch * 3));
  318. if (rc == 0) rc = (int)nmatch; /* All captured slots were filled in */
  319. /* Successful match */
  320. if (rc >= 0)
  321. {
  322. size_t i;
  323. if (!nosub)
  324. {
  325. for (i = 0; i < (size_t)rc; i++)
  326. {
  327. pmatch[i].rm_so = (ovector[i*2] < 0)? -1 : ovector[i*2] + so;
  328. pmatch[i].rm_eo = (ovector[i*2+1] < 0)? -1: ovector[i*2+1] + so;
  329. }
  330. if (allocated_ovector) free(ovector);
  331. for (; i < nmatch; i++) pmatch[i].rm_so = pmatch[i].rm_eo = -1;
  332. }
  333. return 0;
  334. }
  335. /* Unsuccessful match */
  336. if (allocated_ovector) free(ovector);
  337. switch(rc)
  338. {
  339. /* ========================================================================== */
  340. /* These cases are never obeyed. This is a fudge that causes a compile-time
  341. error if the vector eint, which is indexed by compile-time error number, is
  342. not the correct length. It seems to be the only way to do such a check at
  343. compile time, as the sizeof() operator does not work in the C preprocessor.
  344. As all the PCRE_ERROR_xxx values are negative, we can use 0 and 1. */
  345. case 0:
  346. case (sizeof(eint)/sizeof(int) == ERRCOUNT):
  347. return REG_ASSERT;
  348. /* ========================================================================== */
  349. case PCRE_ERROR_NOMATCH: return REG_NOMATCH;
  350. case PCRE_ERROR_NULL: return REG_INVARG;
  351. case PCRE_ERROR_BADOPTION: return REG_INVARG;
  352. case PCRE_ERROR_BADMAGIC: return REG_INVARG;
  353. case PCRE_ERROR_UNKNOWN_NODE: return REG_ASSERT;
  354. case PCRE_ERROR_NOMEMORY: return REG_ESPACE;
  355. case PCRE_ERROR_MATCHLIMIT: return REG_ESPACE;
  356. case PCRE_ERROR_BADUTF8: return REG_INVARG;
  357. case PCRE_ERROR_BADUTF8_OFFSET: return REG_INVARG;
  358. case PCRE_ERROR_BADMODE: return REG_INVARG;
  359. default: return REG_ASSERT;
  360. }
  361. }
  362. /* End of pcreposix.c */