pcre2_substitute.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  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. Original API code Copyright (c) 1997-2012 University of Cambridge
  8. New API code Copyright (c) 2016-2020 University of Cambridge
  9. -----------------------------------------------------------------------------
  10. Redistribution and use in source and binary forms, with or without
  11. modification, are permitted provided that the following conditions are met:
  12. * Redistributions of source code must retain the above copyright notice,
  13. this list of conditions and the following disclaimer.
  14. * Redistributions in binary form must reproduce the above copyright
  15. notice, this list of conditions and the following disclaimer in the
  16. documentation and/or other materials provided with the distribution.
  17. * Neither the name of the University of Cambridge nor the names of its
  18. contributors may be used to endorse or promote products derived from
  19. this software without specific prior written permission.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  24. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. POSSIBILITY OF SUCH DAMAGE.
  31. -----------------------------------------------------------------------------
  32. */
  33. #ifdef HAVE_CONFIG_H
  34. #include "config.h"
  35. #endif
  36. #include "pcre2_internal.h"
  37. #define PTR_STACK_SIZE 20
  38. #define SUBSTITUTE_OPTIONS \
  39. (PCRE2_SUBSTITUTE_EXTENDED|PCRE2_SUBSTITUTE_GLOBAL| \
  40. PCRE2_SUBSTITUTE_LITERAL|PCRE2_SUBSTITUTE_MATCHED| \
  41. PCRE2_SUBSTITUTE_OVERFLOW_LENGTH|PCRE2_SUBSTITUTE_REPLACEMENT_ONLY| \
  42. PCRE2_SUBSTITUTE_UNKNOWN_UNSET|PCRE2_SUBSTITUTE_UNSET_EMPTY)
  43. /*************************************************
  44. * Find end of substitute text *
  45. *************************************************/
  46. /* In extended mode, we recognize ${name:+set text:unset text} and similar
  47. constructions. This requires the identification of unescaped : and }
  48. characters. This function scans for such. It must deal with nested ${
  49. constructions. The pointer to the text is updated, either to the required end
  50. character, or to where an error was detected.
  51. Arguments:
  52. code points to the compiled expression (for options)
  53. ptrptr points to the pointer to the start of the text (updated)
  54. ptrend end of the whole string
  55. last TRUE if the last expected string (only } recognized)
  56. Returns: 0 on success
  57. negative error code on failure
  58. */
  59. static int
  60. find_text_end(const pcre2_code *code, PCRE2_SPTR *ptrptr, PCRE2_SPTR ptrend,
  61. BOOL last)
  62. {
  63. int rc = 0;
  64. uint32_t nestlevel = 0;
  65. BOOL literal = FALSE;
  66. PCRE2_SPTR ptr = *ptrptr;
  67. for (; ptr < ptrend; ptr++)
  68. {
  69. if (literal)
  70. {
  71. if (ptr[0] == CHAR_BACKSLASH && ptr < ptrend - 1 && ptr[1] == CHAR_E)
  72. {
  73. literal = FALSE;
  74. ptr += 1;
  75. }
  76. }
  77. else if (*ptr == CHAR_RIGHT_CURLY_BRACKET)
  78. {
  79. if (nestlevel == 0) goto EXIT;
  80. nestlevel--;
  81. }
  82. else if (*ptr == CHAR_COLON && !last && nestlevel == 0) goto EXIT;
  83. else if (*ptr == CHAR_DOLLAR_SIGN)
  84. {
  85. if (ptr < ptrend - 1 && ptr[1] == CHAR_LEFT_CURLY_BRACKET)
  86. {
  87. nestlevel++;
  88. ptr += 1;
  89. }
  90. }
  91. else if (*ptr == CHAR_BACKSLASH)
  92. {
  93. int erc;
  94. int errorcode;
  95. uint32_t ch;
  96. if (ptr < ptrend - 1) switch (ptr[1])
  97. {
  98. case CHAR_L:
  99. case CHAR_l:
  100. case CHAR_U:
  101. case CHAR_u:
  102. ptr += 1;
  103. continue;
  104. }
  105. ptr += 1; /* Must point after \ */
  106. erc = PRIV(check_escape)(&ptr, ptrend, &ch, &errorcode,
  107. code->overall_options, code->extra_options, FALSE, NULL);
  108. ptr -= 1; /* Back to last code unit of escape */
  109. if (errorcode != 0)
  110. {
  111. rc = errorcode;
  112. goto EXIT;
  113. }
  114. switch(erc)
  115. {
  116. case 0: /* Data character */
  117. case ESC_E: /* Isolated \E is ignored */
  118. break;
  119. case ESC_Q:
  120. literal = TRUE;
  121. break;
  122. default:
  123. rc = PCRE2_ERROR_BADREPESCAPE;
  124. goto EXIT;
  125. }
  126. }
  127. }
  128. rc = PCRE2_ERROR_REPMISSINGBRACE; /* Terminator not found */
  129. EXIT:
  130. *ptrptr = ptr;
  131. return rc;
  132. }
  133. /*************************************************
  134. * Match and substitute *
  135. *************************************************/
  136. /* This function applies a compiled re to a subject string and creates a new
  137. string with substitutions. The first 7 arguments are the same as for
  138. pcre2_match(). Either string length may be PCRE2_ZERO_TERMINATED.
  139. Arguments:
  140. code points to the compiled expression
  141. subject points to the subject string
  142. length length of subject string (may contain binary zeros)
  143. start_offset where to start in the subject string
  144. options option bits
  145. match_data points to a match_data block, or is NULL
  146. context points a PCRE2 context
  147. replacement points to the replacement string
  148. rlength length of replacement string
  149. buffer where to put the substituted string
  150. blength points to length of buffer; updated to length of string
  151. Returns: >= 0 number of substitutions made
  152. < 0 an error code
  153. PCRE2_ERROR_BADREPLACEMENT means invalid use of $
  154. */
  155. /* This macro checks for space in the buffer before copying into it. On
  156. overflow, either give an error immediately, or keep on, accumulating the
  157. length. */
  158. #define CHECKMEMCPY(from,length) \
  159. { \
  160. if (!overflowed && lengthleft < length) \
  161. { \
  162. if ((suboptions & PCRE2_SUBSTITUTE_OVERFLOW_LENGTH) == 0) goto NOROOM; \
  163. overflowed = TRUE; \
  164. extra_needed = length - lengthleft; \
  165. } \
  166. else if (overflowed) \
  167. { \
  168. extra_needed += length; \
  169. } \
  170. else \
  171. { \
  172. memcpy(buffer + buff_offset, from, CU2BYTES(length)); \
  173. buff_offset += length; \
  174. lengthleft -= length; \
  175. } \
  176. }
  177. /* Here's the function */
  178. PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
  179. pcre2_substitute(const pcre2_code *code, PCRE2_SPTR subject, PCRE2_SIZE length,
  180. PCRE2_SIZE start_offset, uint32_t options, pcre2_match_data *match_data,
  181. pcre2_match_context *mcontext, PCRE2_SPTR replacement, PCRE2_SIZE rlength,
  182. PCRE2_UCHAR *buffer, PCRE2_SIZE *blength)
  183. {
  184. int rc;
  185. int subs;
  186. int forcecase = 0;
  187. int forcecasereset = 0;
  188. uint32_t ovector_count;
  189. uint32_t goptions = 0;
  190. uint32_t suboptions;
  191. pcre2_match_data *internal_match_data = NULL;
  192. BOOL escaped_literal = FALSE;
  193. BOOL overflowed = FALSE;
  194. BOOL use_existing_match;
  195. BOOL replacement_only;
  196. #ifdef SUPPORT_UNICODE
  197. BOOL utf = (code->overall_options & PCRE2_UTF) != 0;
  198. BOOL ucp = (code->overall_options & PCRE2_UCP) != 0;
  199. #endif
  200. PCRE2_UCHAR temp[6];
  201. PCRE2_SPTR ptr;
  202. PCRE2_SPTR repend;
  203. PCRE2_SIZE extra_needed = 0;
  204. PCRE2_SIZE buff_offset, buff_length, lengthleft, fraglength;
  205. PCRE2_SIZE *ovector;
  206. PCRE2_SIZE ovecsave[3];
  207. pcre2_substitute_callout_block scb;
  208. /* General initialization */
  209. buff_offset = 0;
  210. lengthleft = buff_length = *blength;
  211. *blength = PCRE2_UNSET;
  212. ovecsave[0] = ovecsave[1] = ovecsave[2] = PCRE2_UNSET;
  213. /* Partial matching is not valid. This must come after setting *blength to
  214. PCRE2_UNSET, so as not to imply an offset in the replacement. */
  215. if ((options & (PCRE2_PARTIAL_HARD|PCRE2_PARTIAL_SOFT)) != 0)
  216. return PCRE2_ERROR_BADOPTION;
  217. /* Check for using a match that has already happened. Note that the subject
  218. pointer in the match data may be NULL after a no-match. */
  219. use_existing_match = ((options & PCRE2_SUBSTITUTE_MATCHED) != 0);
  220. replacement_only = ((options & PCRE2_SUBSTITUTE_REPLACEMENT_ONLY) != 0);
  221. /* If starting from an existing match, there must be an externally provided
  222. match data block. We create an internal match_data block in two cases: (a) an
  223. external one is not supplied (and we are not starting from an existing match);
  224. (b) an existing match is to be used for the first substitution. In the latter
  225. case, we copy the existing match into the internal block. This ensures that no
  226. changes are made to the existing match data block. */
  227. if (match_data == NULL)
  228. {
  229. pcre2_general_context *gcontext;
  230. if (use_existing_match) return PCRE2_ERROR_NULL;
  231. gcontext = (mcontext == NULL)?
  232. (pcre2_general_context *)code :
  233. (pcre2_general_context *)mcontext;
  234. match_data = internal_match_data =
  235. pcre2_match_data_create_from_pattern(code, gcontext);
  236. if (internal_match_data == NULL) return PCRE2_ERROR_NOMEMORY;
  237. }
  238. else if (use_existing_match)
  239. {
  240. pcre2_general_context *gcontext = (mcontext == NULL)?
  241. (pcre2_general_context *)code :
  242. (pcre2_general_context *)mcontext;
  243. int pairs = (code->top_bracket + 1 < match_data->oveccount)?
  244. code->top_bracket + 1 : match_data->oveccount;
  245. internal_match_data = pcre2_match_data_create(match_data->oveccount,
  246. gcontext);
  247. if (internal_match_data == NULL) return PCRE2_ERROR_NOMEMORY;
  248. memcpy(internal_match_data, match_data, offsetof(pcre2_match_data, ovector)
  249. + 2*pairs*sizeof(PCRE2_SIZE));
  250. match_data = internal_match_data;
  251. }
  252. /* Remember ovector details */
  253. ovector = pcre2_get_ovector_pointer(match_data);
  254. ovector_count = pcre2_get_ovector_count(match_data);
  255. /* Fixed things in the callout block */
  256. scb.version = 0;
  257. scb.input = subject;
  258. scb.output = (PCRE2_SPTR)buffer;
  259. scb.ovector = ovector;
  260. /* Find lengths of zero-terminated strings and the end of the replacement. */
  261. if (length == PCRE2_ZERO_TERMINATED) length = PRIV(strlen)(subject);
  262. if (rlength == PCRE2_ZERO_TERMINATED) rlength = PRIV(strlen)(replacement);
  263. repend = replacement + rlength;
  264. /* Check UTF replacement string if necessary. */
  265. #ifdef SUPPORT_UNICODE
  266. if (utf && (options & PCRE2_NO_UTF_CHECK) == 0)
  267. {
  268. rc = PRIV(valid_utf)(replacement, rlength, &(match_data->startchar));
  269. if (rc != 0)
  270. {
  271. match_data->leftchar = 0;
  272. goto EXIT;
  273. }
  274. }
  275. #endif /* SUPPORT_UNICODE */
  276. /* Save the substitute options and remove them from the match options. */
  277. suboptions = options & SUBSTITUTE_OPTIONS;
  278. options &= ~SUBSTITUTE_OPTIONS;
  279. /* Error if the start match offset is greater than the length of the subject. */
  280. if (start_offset > length)
  281. {
  282. match_data->leftchar = 0;
  283. rc = PCRE2_ERROR_BADOFFSET;
  284. goto EXIT;
  285. }
  286. /* Copy up to the start offset, unless only the replacement is required. */
  287. if (!replacement_only) CHECKMEMCPY(subject, start_offset);
  288. /* Loop for global substituting. If PCRE2_SUBSTITUTE_MATCHED is set, the first
  289. match is taken from the match_data that was passed in. */
  290. subs = 0;
  291. do
  292. {
  293. PCRE2_SPTR ptrstack[PTR_STACK_SIZE];
  294. uint32_t ptrstackptr = 0;
  295. if (use_existing_match)
  296. {
  297. rc = match_data->rc;
  298. use_existing_match = FALSE;
  299. }
  300. else rc = pcre2_match(code, subject, length, start_offset, options|goptions,
  301. match_data, mcontext);
  302. #ifdef SUPPORT_UNICODE
  303. if (utf) options |= PCRE2_NO_UTF_CHECK; /* Only need to check once */
  304. #endif
  305. /* Any error other than no match returns the error code. No match when not
  306. doing the special after-empty-match global rematch, or when at the end of the
  307. subject, breaks the global loop. Otherwise, advance the starting point by one
  308. character, copying it to the output, and try again. */
  309. if (rc < 0)
  310. {
  311. PCRE2_SIZE save_start;
  312. if (rc != PCRE2_ERROR_NOMATCH) goto EXIT;
  313. if (goptions == 0 || start_offset >= length) break;
  314. /* Advance by one code point. Then, if CRLF is a valid newline sequence and
  315. we have advanced into the middle of it, advance one more code point. In
  316. other words, do not start in the middle of CRLF, even if CR and LF on their
  317. own are valid newlines. */
  318. save_start = start_offset++;
  319. if (subject[start_offset-1] == CHAR_CR &&
  320. code->newline_convention != PCRE2_NEWLINE_CR &&
  321. code->newline_convention != PCRE2_NEWLINE_LF &&
  322. start_offset < length &&
  323. subject[start_offset] == CHAR_LF)
  324. start_offset++;
  325. /* Otherwise, in UTF mode, advance past any secondary code points. */
  326. else if ((code->overall_options & PCRE2_UTF) != 0)
  327. {
  328. #if PCRE2_CODE_UNIT_WIDTH == 8
  329. while (start_offset < length && (subject[start_offset] & 0xc0) == 0x80)
  330. start_offset++;
  331. #elif PCRE2_CODE_UNIT_WIDTH == 16
  332. while (start_offset < length &&
  333. (subject[start_offset] & 0xfc00) == 0xdc00)
  334. start_offset++;
  335. #endif
  336. }
  337. /* Copy what we have advanced past (unless not required), reset the special
  338. global options, and continue to the next match. */
  339. fraglength = start_offset - save_start;
  340. if (!replacement_only) CHECKMEMCPY(subject + save_start, fraglength);
  341. goptions = 0;
  342. continue;
  343. }
  344. /* Handle a successful match. Matches that use \K to end before they start
  345. or start before the current point in the subject are not supported. */
  346. if (ovector[1] < ovector[0] || ovector[0] < start_offset)
  347. {
  348. rc = PCRE2_ERROR_BADSUBSPATTERN;
  349. goto EXIT;
  350. }
  351. /* Check for the same match as previous. This is legitimate after matching an
  352. empty string that starts after the initial match offset. We have tried again
  353. at the match point in case the pattern is one like /(?<=\G.)/ which can never
  354. match at its starting point, so running the match achieves the bumpalong. If
  355. we do get the same (null) match at the original match point, it isn't such a
  356. pattern, so we now do the empty string magic. In all other cases, a repeat
  357. match should never occur. */
  358. if (ovecsave[0] == ovector[0] && ovecsave[1] == ovector[1])
  359. {
  360. if (ovector[0] == ovector[1] && ovecsave[2] != start_offset)
  361. {
  362. goptions = PCRE2_NOTEMPTY_ATSTART | PCRE2_ANCHORED;
  363. ovecsave[2] = start_offset;
  364. continue; /* Back to the top of the loop */
  365. }
  366. rc = PCRE2_ERROR_INTERNAL_DUPMATCH;
  367. goto EXIT;
  368. }
  369. /* Count substitutions with a paranoid check for integer overflow; surely no
  370. real call to this function would ever hit this! */
  371. if (subs == INT_MAX)
  372. {
  373. rc = PCRE2_ERROR_TOOMANYREPLACE;
  374. goto EXIT;
  375. }
  376. subs++;
  377. /* Copy the text leading up to the match (unless not required), and remember
  378. where the insert begins and how many ovector pairs are set. */
  379. if (rc == 0) rc = ovector_count;
  380. fraglength = ovector[0] - start_offset;
  381. if (!replacement_only) CHECKMEMCPY(subject + start_offset, fraglength);
  382. scb.output_offsets[0] = buff_offset;
  383. scb.oveccount = rc;
  384. /* Process the replacement string. If the entire replacement is literal, just
  385. copy it with length check. */
  386. ptr = replacement;
  387. if ((suboptions & PCRE2_SUBSTITUTE_LITERAL) != 0)
  388. {
  389. CHECKMEMCPY(ptr, rlength);
  390. }
  391. /* Within a non-literal replacement, which must be scanned character by
  392. character, local literal mode can be set by \Q, but only in extended mode
  393. when backslashes are being interpreted. In extended mode we must handle
  394. nested substrings that are to be reprocessed. */
  395. else for (;;)
  396. {
  397. uint32_t ch;
  398. unsigned int chlen;
  399. /* If at the end of a nested substring, pop the stack. */
  400. if (ptr >= repend)
  401. {
  402. if (ptrstackptr == 0) break; /* End of replacement string */
  403. repend = ptrstack[--ptrstackptr];
  404. ptr = ptrstack[--ptrstackptr];
  405. continue;
  406. }
  407. /* Handle the next character */
  408. if (escaped_literal)
  409. {
  410. if (ptr[0] == CHAR_BACKSLASH && ptr < repend - 1 && ptr[1] == CHAR_E)
  411. {
  412. escaped_literal = FALSE;
  413. ptr += 2;
  414. continue;
  415. }
  416. goto LOADLITERAL;
  417. }
  418. /* Not in literal mode. */
  419. if (*ptr == CHAR_DOLLAR_SIGN)
  420. {
  421. int group, n;
  422. uint32_t special = 0;
  423. BOOL inparens;
  424. BOOL star;
  425. PCRE2_SIZE sublength;
  426. PCRE2_SPTR text1_start = NULL;
  427. PCRE2_SPTR text1_end = NULL;
  428. PCRE2_SPTR text2_start = NULL;
  429. PCRE2_SPTR text2_end = NULL;
  430. PCRE2_UCHAR next;
  431. PCRE2_UCHAR name[33];
  432. if (++ptr >= repend) goto BAD;
  433. if ((next = *ptr) == CHAR_DOLLAR_SIGN) goto LOADLITERAL;
  434. group = -1;
  435. n = 0;
  436. inparens = FALSE;
  437. star = FALSE;
  438. if (next == CHAR_LEFT_CURLY_BRACKET)
  439. {
  440. if (++ptr >= repend) goto BAD;
  441. next = *ptr;
  442. inparens = TRUE;
  443. }
  444. if (next == CHAR_ASTERISK)
  445. {
  446. if (++ptr >= repend) goto BAD;
  447. next = *ptr;
  448. star = TRUE;
  449. }
  450. if (!star && next >= CHAR_0 && next <= CHAR_9)
  451. {
  452. group = next - CHAR_0;
  453. while (++ptr < repend)
  454. {
  455. next = *ptr;
  456. if (next < CHAR_0 || next > CHAR_9) break;
  457. group = group * 10 + next - CHAR_0;
  458. /* A check for a number greater than the hightest captured group
  459. is sufficient here; no need for a separate overflow check. If unknown
  460. groups are to be treated as unset, just skip over any remaining
  461. digits and carry on. */
  462. if (group > code->top_bracket)
  463. {
  464. if ((suboptions & PCRE2_SUBSTITUTE_UNKNOWN_UNSET) != 0)
  465. {
  466. while (++ptr < repend && *ptr >= CHAR_0 && *ptr <= CHAR_9);
  467. break;
  468. }
  469. else
  470. {
  471. rc = PCRE2_ERROR_NOSUBSTRING;
  472. goto PTREXIT;
  473. }
  474. }
  475. }
  476. }
  477. else
  478. {
  479. const uint8_t *ctypes = code->tables + ctypes_offset;
  480. while (MAX_255(next) && (ctypes[next] & ctype_word) != 0)
  481. {
  482. name[n++] = next;
  483. if (n > 32) goto BAD;
  484. if (++ptr >= repend) break;
  485. next = *ptr;
  486. }
  487. if (n == 0) goto BAD;
  488. name[n] = 0;
  489. }
  490. /* In extended mode we recognize ${name:+set text:unset text} and
  491. ${name:-default text}. */
  492. if (inparens)
  493. {
  494. if ((suboptions & PCRE2_SUBSTITUTE_EXTENDED) != 0 &&
  495. !star && ptr < repend - 2 && next == CHAR_COLON)
  496. {
  497. special = *(++ptr);
  498. if (special != CHAR_PLUS && special != CHAR_MINUS)
  499. {
  500. rc = PCRE2_ERROR_BADSUBSTITUTION;
  501. goto PTREXIT;
  502. }
  503. text1_start = ++ptr;
  504. rc = find_text_end(code, &ptr, repend, special == CHAR_MINUS);
  505. if (rc != 0) goto PTREXIT;
  506. text1_end = ptr;
  507. if (special == CHAR_PLUS && *ptr == CHAR_COLON)
  508. {
  509. text2_start = ++ptr;
  510. rc = find_text_end(code, &ptr, repend, TRUE);
  511. if (rc != 0) goto PTREXIT;
  512. text2_end = ptr;
  513. }
  514. }
  515. else
  516. {
  517. if (ptr >= repend || *ptr != CHAR_RIGHT_CURLY_BRACKET)
  518. {
  519. rc = PCRE2_ERROR_REPMISSINGBRACE;
  520. goto PTREXIT;
  521. }
  522. }
  523. ptr++;
  524. }
  525. /* Have found a syntactically correct group number or name, or *name.
  526. Only *MARK is currently recognized. */
  527. if (star)
  528. {
  529. if (PRIV(strcmp_c8)(name, STRING_MARK) == 0)
  530. {
  531. PCRE2_SPTR mark = pcre2_get_mark(match_data);
  532. if (mark != NULL)
  533. {
  534. PCRE2_SPTR mark_start = mark;
  535. while (*mark != 0) mark++;
  536. fraglength = mark - mark_start;
  537. CHECKMEMCPY(mark_start, fraglength);
  538. }
  539. }
  540. else goto BAD;
  541. }
  542. /* Substitute the contents of a group. We don't use substring_copy
  543. functions any more, in order to support case forcing. */
  544. else
  545. {
  546. PCRE2_SPTR subptr, subptrend;
  547. /* Find a number for a named group. In case there are duplicate names,
  548. search for the first one that is set. If the name is not found when
  549. PCRE2_SUBSTITUTE_UNKNOWN_EMPTY is set, set the group number to a
  550. non-existent group. */
  551. if (group < 0)
  552. {
  553. PCRE2_SPTR first, last, entry;
  554. rc = pcre2_substring_nametable_scan(code, name, &first, &last);
  555. if (rc == PCRE2_ERROR_NOSUBSTRING &&
  556. (suboptions & PCRE2_SUBSTITUTE_UNKNOWN_UNSET) != 0)
  557. {
  558. group = code->top_bracket + 1;
  559. }
  560. else
  561. {
  562. if (rc < 0) goto PTREXIT;
  563. for (entry = first; entry <= last; entry += rc)
  564. {
  565. uint32_t ng = GET2(entry, 0);
  566. if (ng < ovector_count)
  567. {
  568. if (group < 0) group = ng; /* First in ovector */
  569. if (ovector[ng*2] != PCRE2_UNSET)
  570. {
  571. group = ng; /* First that is set */
  572. break;
  573. }
  574. }
  575. }
  576. /* If group is still negative, it means we did not find a group
  577. that is in the ovector. Just set the first group. */
  578. if (group < 0) group = GET2(first, 0);
  579. }
  580. }
  581. /* We now have a group that is identified by number. Find the length of
  582. the captured string. If a group in a non-special substitution is unset
  583. when PCRE2_SUBSTITUTE_UNSET_EMPTY is set, substitute nothing. */
  584. rc = pcre2_substring_length_bynumber(match_data, group, &sublength);
  585. if (rc < 0)
  586. {
  587. if (rc == PCRE2_ERROR_NOSUBSTRING &&
  588. (suboptions & PCRE2_SUBSTITUTE_UNKNOWN_UNSET) != 0)
  589. {
  590. rc = PCRE2_ERROR_UNSET;
  591. }
  592. if (rc != PCRE2_ERROR_UNSET) goto PTREXIT; /* Non-unset errors */
  593. if (special == 0) /* Plain substitution */
  594. {
  595. if ((suboptions & PCRE2_SUBSTITUTE_UNSET_EMPTY) != 0) continue;
  596. goto PTREXIT; /* Else error */
  597. }
  598. }
  599. /* If special is '+' we have a 'set' and possibly an 'unset' text,
  600. both of which are reprocessed when used. If special is '-' we have a
  601. default text for when the group is unset; it must be reprocessed. */
  602. if (special != 0)
  603. {
  604. if (special == CHAR_MINUS)
  605. {
  606. if (rc == 0) goto LITERAL_SUBSTITUTE;
  607. text2_start = text1_start;
  608. text2_end = text1_end;
  609. }
  610. if (ptrstackptr >= PTR_STACK_SIZE) goto BAD;
  611. ptrstack[ptrstackptr++] = ptr;
  612. ptrstack[ptrstackptr++] = repend;
  613. if (rc == 0)
  614. {
  615. ptr = text1_start;
  616. repend = text1_end;
  617. }
  618. else
  619. {
  620. ptr = text2_start;
  621. repend = text2_end;
  622. }
  623. continue;
  624. }
  625. /* Otherwise we have a literal substitution of a group's contents. */
  626. LITERAL_SUBSTITUTE:
  627. subptr = subject + ovector[group*2];
  628. subptrend = subject + ovector[group*2 + 1];
  629. /* Substitute a literal string, possibly forcing alphabetic case. */
  630. while (subptr < subptrend)
  631. {
  632. GETCHARINCTEST(ch, subptr);
  633. if (forcecase != 0)
  634. {
  635. #ifdef SUPPORT_UNICODE
  636. if (utf || ucp)
  637. {
  638. uint32_t type = UCD_CHARTYPE(ch);
  639. if (PRIV(ucp_gentype)[type] == ucp_L &&
  640. type != ((forcecase > 0)? ucp_Lu : ucp_Ll))
  641. ch = UCD_OTHERCASE(ch);
  642. }
  643. else
  644. #endif
  645. {
  646. if (((code->tables + cbits_offset +
  647. ((forcecase > 0)? cbit_upper:cbit_lower)
  648. )[ch/8] & (1u << (ch%8))) == 0)
  649. ch = (code->tables + fcc_offset)[ch];
  650. }
  651. forcecase = forcecasereset;
  652. }
  653. #ifdef SUPPORT_UNICODE
  654. if (utf) chlen = PRIV(ord2utf)(ch, temp); else
  655. #endif
  656. {
  657. temp[0] = ch;
  658. chlen = 1;
  659. }
  660. CHECKMEMCPY(temp, chlen);
  661. }
  662. }
  663. }
  664. /* Handle an escape sequence in extended mode. We can use check_escape()
  665. to process \Q, \E, \c, \o, \x and \ followed by non-alphanumerics, but
  666. the case-forcing escapes are not supported in pcre2_compile() so must be
  667. recognized here. */
  668. else if ((suboptions & PCRE2_SUBSTITUTE_EXTENDED) != 0 &&
  669. *ptr == CHAR_BACKSLASH)
  670. {
  671. int errorcode;
  672. if (ptr < repend - 1) switch (ptr[1])
  673. {
  674. case CHAR_L:
  675. forcecase = forcecasereset = -1;
  676. ptr += 2;
  677. continue;
  678. case CHAR_l:
  679. forcecase = -1;
  680. forcecasereset = 0;
  681. ptr += 2;
  682. continue;
  683. case CHAR_U:
  684. forcecase = forcecasereset = 1;
  685. ptr += 2;
  686. continue;
  687. case CHAR_u:
  688. forcecase = 1;
  689. forcecasereset = 0;
  690. ptr += 2;
  691. continue;
  692. default:
  693. break;
  694. }
  695. ptr++; /* Point after \ */
  696. rc = PRIV(check_escape)(&ptr, repend, &ch, &errorcode,
  697. code->overall_options, code->extra_options, FALSE, NULL);
  698. if (errorcode != 0) goto BADESCAPE;
  699. switch(rc)
  700. {
  701. case ESC_E:
  702. forcecase = forcecasereset = 0;
  703. continue;
  704. case ESC_Q:
  705. escaped_literal = TRUE;
  706. continue;
  707. case 0: /* Data character */
  708. goto LITERAL;
  709. default:
  710. goto BADESCAPE;
  711. }
  712. }
  713. /* Handle a literal code unit */
  714. else
  715. {
  716. LOADLITERAL:
  717. GETCHARINCTEST(ch, ptr); /* Get character value, increment pointer */
  718. LITERAL:
  719. if (forcecase != 0)
  720. {
  721. #ifdef SUPPORT_UNICODE
  722. if (utf || ucp)
  723. {
  724. uint32_t type = UCD_CHARTYPE(ch);
  725. if (PRIV(ucp_gentype)[type] == ucp_L &&
  726. type != ((forcecase > 0)? ucp_Lu : ucp_Ll))
  727. ch = UCD_OTHERCASE(ch);
  728. }
  729. else
  730. #endif
  731. {
  732. if (((code->tables + cbits_offset +
  733. ((forcecase > 0)? cbit_upper:cbit_lower)
  734. )[ch/8] & (1u << (ch%8))) == 0)
  735. ch = (code->tables + fcc_offset)[ch];
  736. }
  737. forcecase = forcecasereset;
  738. }
  739. #ifdef SUPPORT_UNICODE
  740. if (utf) chlen = PRIV(ord2utf)(ch, temp); else
  741. #endif
  742. {
  743. temp[0] = ch;
  744. chlen = 1;
  745. }
  746. CHECKMEMCPY(temp, chlen);
  747. } /* End handling a literal code unit */
  748. } /* End of loop for scanning the replacement. */
  749. /* The replacement has been copied to the output, or its size has been
  750. remembered. Do the callout if there is one and we have done an actual
  751. replacement. */
  752. if (!overflowed && mcontext != NULL && mcontext->substitute_callout != NULL)
  753. {
  754. scb.subscount = subs;
  755. scb.output_offsets[1] = buff_offset;
  756. rc = mcontext->substitute_callout(&scb, mcontext->substitute_callout_data);
  757. /* A non-zero return means cancel this substitution. Instead, copy the
  758. matched string fragment. */
  759. if (rc != 0)
  760. {
  761. PCRE2_SIZE newlength = scb.output_offsets[1] - scb.output_offsets[0];
  762. PCRE2_SIZE oldlength = ovector[1] - ovector[0];
  763. buff_offset -= newlength;
  764. lengthleft += newlength;
  765. if (!replacement_only) CHECKMEMCPY(subject + ovector[0], oldlength);
  766. /* A negative return means do not do any more. */
  767. if (rc < 0) suboptions &= (~PCRE2_SUBSTITUTE_GLOBAL);
  768. }
  769. }
  770. /* Save the details of this match. See above for how this data is used. If we
  771. matched an empty string, do the magic for global matches. Update the start
  772. offset to point to the rest of the subject string. If we re-used an existing
  773. match for the first match, switch to the internal match data block. */
  774. ovecsave[0] = ovector[0];
  775. ovecsave[1] = ovector[1];
  776. ovecsave[2] = start_offset;
  777. goptions = (ovector[0] != ovector[1] || ovector[0] > start_offset)? 0 :
  778. PCRE2_ANCHORED|PCRE2_NOTEMPTY_ATSTART;
  779. start_offset = ovector[1];
  780. } while ((suboptions & PCRE2_SUBSTITUTE_GLOBAL) != 0); /* Repeat "do" loop */
  781. /* Copy the rest of the subject unless not required, and terminate the output
  782. with a binary zero. */
  783. if (!replacement_only)
  784. {
  785. fraglength = length - start_offset;
  786. CHECKMEMCPY(subject + start_offset, fraglength);
  787. }
  788. temp[0] = 0;
  789. CHECKMEMCPY(temp, 1);
  790. /* If overflowed is set it means the PCRE2_SUBSTITUTE_OVERFLOW_LENGTH is set,
  791. and matching has carried on after a full buffer, in order to compute the length
  792. needed. Otherwise, an overflow generates an immediate error return. */
  793. if (overflowed)
  794. {
  795. rc = PCRE2_ERROR_NOMEMORY;
  796. *blength = buff_length + extra_needed;
  797. }
  798. /* After a successful execution, return the number of substitutions and set the
  799. length of buffer used, excluding the trailing zero. */
  800. else
  801. {
  802. rc = subs;
  803. *blength = buff_offset - 1;
  804. }
  805. EXIT:
  806. if (internal_match_data != NULL) pcre2_match_data_free(internal_match_data);
  807. else match_data->rc = rc;
  808. return rc;
  809. NOROOM:
  810. rc = PCRE2_ERROR_NOMEMORY;
  811. goto EXIT;
  812. BAD:
  813. rc = PCRE2_ERROR_BADREPLACEMENT;
  814. goto PTREXIT;
  815. BADESCAPE:
  816. rc = PCRE2_ERROR_BADREPESCAPE;
  817. PTREXIT:
  818. *blength = (PCRE2_SIZE)(ptr - replacement);
  819. goto EXIT;
  820. }
  821. /* End of pcre2_substitute.c */