2014-11-08 17:09:24 +01:00
|
|
|
/*************************************************
|
|
|
|
* Perl-Compatible Regular Expressions *
|
|
|
|
*************************************************/
|
|
|
|
|
|
|
|
/* PCRE is a library of functions to support regular expressions whose syntax
|
|
|
|
and semantics are as close as possible to those of the Perl 5 language.
|
|
|
|
|
|
|
|
Written by Philip Hazel
|
|
|
|
Original API code Copyright (c) 1997-2012 University of Cambridge
|
2020-01-22 18:50:12 +01:00
|
|
|
New API code Copyright (c) 2016-2020 University of Cambridge
|
2014-11-08 17:09:24 +01:00
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright notice,
|
|
|
|
this list of conditions and the following disclaimer.
|
|
|
|
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
|
|
|
documentation and/or other materials provided with the distribution.
|
|
|
|
|
|
|
|
* Neither the name of the University of Cambridge nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived from
|
|
|
|
this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "pcre2_internal.h"
|
|
|
|
|
2015-10-07 19:32:48 +02:00
|
|
|
#define PTR_STACK_SIZE 20
|
|
|
|
|
2015-12-12 19:45:40 +01:00
|
|
|
#define SUBSTITUTE_OPTIONS \
|
|
|
|
(PCRE2_SUBSTITUTE_EXTENDED|PCRE2_SUBSTITUTE_GLOBAL| \
|
2019-12-27 14:35:17 +01:00
|
|
|
PCRE2_SUBSTITUTE_LITERAL|PCRE2_SUBSTITUTE_MATCHED| \
|
2020-01-22 18:50:12 +01:00
|
|
|
PCRE2_SUBSTITUTE_OVERFLOW_LENGTH|PCRE2_SUBSTITUTE_REPLACEMENT_ONLY| \
|
|
|
|
PCRE2_SUBSTITUTE_UNKNOWN_UNSET|PCRE2_SUBSTITUTE_UNSET_EMPTY)
|
2015-12-12 19:45:40 +01:00
|
|
|
|
|
|
|
|
2015-10-07 19:32:48 +02:00
|
|
|
|
|
|
|
/*************************************************
|
|
|
|
* Find end of substitute text *
|
|
|
|
*************************************************/
|
|
|
|
|
|
|
|
/* In extended mode, we recognize ${name:+set text:unset text} and similar
|
|
|
|
constructions. This requires the identification of unescaped : and }
|
|
|
|
characters. This function scans for such. It must deal with nested ${
|
2015-11-03 18:38:00 +01:00
|
|
|
constructions. The pointer to the text is updated, either to the required end
|
2015-10-07 19:32:48 +02:00
|
|
|
character, or to where an error was detected.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
code points to the compiled expression (for options)
|
|
|
|
ptrptr points to the pointer to the start of the text (updated)
|
|
|
|
ptrend end of the whole string
|
|
|
|
last TRUE if the last expected string (only } recognized)
|
|
|
|
|
|
|
|
Returns: 0 on success
|
|
|
|
negative error code on failure
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int
|
|
|
|
find_text_end(const pcre2_code *code, PCRE2_SPTR *ptrptr, PCRE2_SPTR ptrend,
|
|
|
|
BOOL last)
|
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
uint32_t nestlevel = 0;
|
|
|
|
BOOL literal = FALSE;
|
|
|
|
PCRE2_SPTR ptr = *ptrptr;
|
|
|
|
|
|
|
|
for (; ptr < ptrend; ptr++)
|
|
|
|
{
|
|
|
|
if (literal)
|
|
|
|
{
|
|
|
|
if (ptr[0] == CHAR_BACKSLASH && ptr < ptrend - 1 && ptr[1] == CHAR_E)
|
|
|
|
{
|
|
|
|
literal = FALSE;
|
|
|
|
ptr += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (*ptr == CHAR_RIGHT_CURLY_BRACKET)
|
|
|
|
{
|
|
|
|
if (nestlevel == 0) goto EXIT;
|
|
|
|
nestlevel--;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (*ptr == CHAR_COLON && !last && nestlevel == 0) goto EXIT;
|
|
|
|
|
|
|
|
else if (*ptr == CHAR_DOLLAR_SIGN)
|
|
|
|
{
|
|
|
|
if (ptr < ptrend - 1 && ptr[1] == CHAR_LEFT_CURLY_BRACKET)
|
|
|
|
{
|
|
|
|
nestlevel++;
|
|
|
|
ptr += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (*ptr == CHAR_BACKSLASH)
|
|
|
|
{
|
2015-11-03 18:38:00 +01:00
|
|
|
int erc;
|
2016-10-02 18:01:01 +02:00
|
|
|
int errorcode;
|
2015-10-07 19:32:48 +02:00
|
|
|
uint32_t ch;
|
|
|
|
|
|
|
|
if (ptr < ptrend - 1) switch (ptr[1])
|
|
|
|
{
|
|
|
|
case CHAR_L:
|
|
|
|
case CHAR_l:
|
|
|
|
case CHAR_U:
|
|
|
|
case CHAR_u:
|
|
|
|
ptr += 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-10-02 18:01:01 +02:00
|
|
|
ptr += 1; /* Must point after \ */
|
2015-10-07 19:32:48 +02:00
|
|
|
erc = PRIV(check_escape)(&ptr, ptrend, &ch, &errorcode,
|
2019-02-12 18:50:19 +01:00
|
|
|
code->overall_options, code->extra_options, FALSE, NULL);
|
2016-12-09 19:19:38 +01:00
|
|
|
ptr -= 1; /* Back to last code unit of escape */
|
2015-10-07 19:32:48 +02:00
|
|
|
if (errorcode != 0)
|
|
|
|
{
|
|
|
|
rc = errorcode;
|
|
|
|
goto EXIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(erc)
|
|
|
|
{
|
|
|
|
case 0: /* Data character */
|
|
|
|
case ESC_E: /* Isolated \E is ignored */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ESC_Q:
|
|
|
|
literal = TRUE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
rc = PCRE2_ERROR_BADREPESCAPE;
|
|
|
|
goto EXIT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = PCRE2_ERROR_REPMISSINGBRACE; /* Terminator not found */
|
|
|
|
|
|
|
|
EXIT:
|
|
|
|
*ptrptr = ptr;
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-08 17:09:24 +01:00
|
|
|
|
|
|
|
/*************************************************
|
|
|
|
* Match and substitute *
|
|
|
|
*************************************************/
|
|
|
|
|
|
|
|
/* This function applies a compiled re to a subject string and creates a new
|
2014-11-11 11:19:23 +01:00
|
|
|
string with substitutions. The first 7 arguments are the same as for
|
2014-11-08 17:09:24 +01:00
|
|
|
pcre2_match(). Either string length may be PCRE2_ZERO_TERMINATED.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
code points to the compiled expression
|
|
|
|
subject points to the subject string
|
|
|
|
length length of subject string (may contain binary zeros)
|
|
|
|
start_offset where to start in the subject string
|
|
|
|
options option bits
|
|
|
|
match_data points to a match_data block, or is NULL
|
|
|
|
context points a PCRE2 context
|
|
|
|
replacement points to the replacement string
|
|
|
|
rlength length of replacement string
|
|
|
|
buffer where to put the substituted string
|
|
|
|
blength points to length of buffer; updated to length of string
|
|
|
|
|
2014-11-11 17:51:07 +01:00
|
|
|
Returns: >= 0 number of substitutions made
|
|
|
|
< 0 an error code
|
2014-11-14 19:41:20 +01:00
|
|
|
PCRE2_ERROR_BADREPLACEMENT means invalid use of $
|
2014-11-08 17:09:24 +01:00
|
|
|
*/
|
|
|
|
|
2015-12-12 19:45:40 +01:00
|
|
|
/* This macro checks for space in the buffer before copying into it. On
|
|
|
|
overflow, either give an error immediately, or keep on, accumulating the
|
|
|
|
length. */
|
|
|
|
|
|
|
|
#define CHECKMEMCPY(from,length) \
|
2020-01-22 18:50:12 +01:00
|
|
|
{ \
|
2015-12-12 19:45:40 +01:00
|
|
|
if (!overflowed && lengthleft < length) \
|
|
|
|
{ \
|
|
|
|
if ((suboptions & PCRE2_SUBSTITUTE_OVERFLOW_LENGTH) == 0) goto NOROOM; \
|
|
|
|
overflowed = TRUE; \
|
|
|
|
extra_needed = length - lengthleft; \
|
|
|
|
} \
|
|
|
|
else if (overflowed) \
|
|
|
|
{ \
|
|
|
|
extra_needed += length; \
|
|
|
|
} \
|
|
|
|
else \
|
|
|
|
{ \
|
|
|
|
memcpy(buffer + buff_offset, from, CU2BYTES(length)); \
|
|
|
|
buff_offset += length; \
|
|
|
|
lengthleft -= length; \
|
2020-01-22 18:50:12 +01:00
|
|
|
} \
|
|
|
|
}
|
2015-12-12 19:45:40 +01:00
|
|
|
|
|
|
|
/* Here's the function */
|
|
|
|
|
2014-11-08 17:09:24 +01:00
|
|
|
PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
|
|
|
|
pcre2_substitute(const pcre2_code *code, PCRE2_SPTR subject, PCRE2_SIZE length,
|
|
|
|
PCRE2_SIZE start_offset, uint32_t options, pcre2_match_data *match_data,
|
|
|
|
pcre2_match_context *mcontext, PCRE2_SPTR replacement, PCRE2_SIZE rlength,
|
|
|
|
PCRE2_UCHAR *buffer, PCRE2_SIZE *blength)
|
|
|
|
{
|
2014-11-11 17:51:07 +01:00
|
|
|
int rc;
|
|
|
|
int subs;
|
2015-10-07 19:32:48 +02:00
|
|
|
int forcecase = 0;
|
|
|
|
int forcecasereset = 0;
|
2014-11-08 17:09:24 +01:00
|
|
|
uint32_t ovector_count;
|
|
|
|
uint32_t goptions = 0;
|
2015-12-12 19:45:40 +01:00
|
|
|
uint32_t suboptions;
|
2020-02-16 18:46:40 +01:00
|
|
|
pcre2_match_data *internal_match_data = NULL;
|
2019-12-26 15:53:24 +01:00
|
|
|
BOOL escaped_literal = FALSE;
|
2015-12-12 19:45:40 +01:00
|
|
|
BOOL overflowed = FALSE;
|
2019-12-27 14:35:17 +01:00
|
|
|
BOOL use_existing_match;
|
2020-01-22 18:50:12 +01:00
|
|
|
BOOL replacement_only;
|
2015-10-17 20:29:01 +02:00
|
|
|
#ifdef SUPPORT_UNICODE
|
2015-10-07 19:32:48 +02:00
|
|
|
BOOL utf = (code->overall_options & PCRE2_UTF) != 0;
|
2020-02-23 17:40:05 +01:00
|
|
|
BOOL ucp = (code->overall_options & PCRE2_UCP) != 0;
|
2015-10-17 20:29:01 +02:00
|
|
|
#endif
|
2015-12-12 19:45:40 +01:00
|
|
|
PCRE2_UCHAR temp[6];
|
2015-10-07 19:32:48 +02:00
|
|
|
PCRE2_SPTR ptr;
|
|
|
|
PCRE2_SPTR repend;
|
2015-12-12 19:45:40 +01:00
|
|
|
PCRE2_SIZE extra_needed = 0;
|
2015-10-07 19:32:48 +02:00
|
|
|
PCRE2_SIZE buff_offset, buff_length, lengthleft, fraglength;
|
2014-11-08 17:09:24 +01:00
|
|
|
PCRE2_SIZE *ovector;
|
2018-07-02 12:54:03 +02:00
|
|
|
PCRE2_SIZE ovecsave[3];
|
2018-09-18 18:31:30 +02:00
|
|
|
pcre2_substitute_callout_block scb;
|
2014-11-08 17:09:24 +01:00
|
|
|
|
2018-11-12 17:02:01 +01:00
|
|
|
/* General initialization */
|
|
|
|
|
2015-12-12 19:45:40 +01:00
|
|
|
buff_offset = 0;
|
|
|
|
lengthleft = buff_length = *blength;
|
2015-10-07 19:32:48 +02:00
|
|
|
*blength = PCRE2_UNSET;
|
2018-07-02 12:54:03 +02:00
|
|
|
ovecsave[0] = ovecsave[1] = ovecsave[2] = PCRE2_UNSET;
|
2015-10-07 19:32:48 +02:00
|
|
|
|
2019-12-27 14:35:17 +01:00
|
|
|
/* Partial matching is not valid. This must come after setting *blength to
|
2018-11-12 17:02:01 +01:00
|
|
|
PCRE2_UNSET, so as not to imply an offset in the replacement. */
|
2014-11-11 11:19:23 +01:00
|
|
|
|
|
|
|
if ((options & (PCRE2_PARTIAL_HARD|PCRE2_PARTIAL_SOFT)) != 0)
|
|
|
|
return PCRE2_ERROR_BADOPTION;
|
2014-11-14 19:41:20 +01:00
|
|
|
|
2020-01-22 18:50:12 +01:00
|
|
|
/* Check for using a match that has already happened. Note that the subject
|
2019-12-27 14:35:17 +01:00
|
|
|
pointer in the match data may be NULL after a no-match. */
|
2014-11-08 17:09:24 +01:00
|
|
|
|
2019-12-27 14:35:17 +01:00
|
|
|
use_existing_match = ((options & PCRE2_SUBSTITUTE_MATCHED) != 0);
|
2020-01-22 18:50:12 +01:00
|
|
|
replacement_only = ((options & PCRE2_SUBSTITUTE_REPLACEMENT_ONLY) != 0);
|
2019-12-27 14:35:17 +01:00
|
|
|
|
2020-02-16 18:46:40 +01:00
|
|
|
/* If starting from an existing match, there must be an externally provided
|
|
|
|
match data block. We create an internal match_data block in two cases: (a) an
|
|
|
|
external one is not supplied (and we are not starting from an existing match);
|
|
|
|
(b) an existing match is to be used for the first substitution. In the latter
|
|
|
|
case, we copy the existing match into the internal block. This ensures that no
|
|
|
|
changes are made to the existing match data block. */
|
|
|
|
|
|
|
|
if (match_data == NULL)
|
2019-12-27 14:35:17 +01:00
|
|
|
{
|
2020-02-16 18:46:40 +01:00
|
|
|
pcre2_general_context *gcontext;
|
|
|
|
if (use_existing_match) return PCRE2_ERROR_NULL;
|
|
|
|
gcontext = (mcontext == NULL)?
|
|
|
|
(pcre2_general_context *)code :
|
|
|
|
(pcre2_general_context *)mcontext;
|
|
|
|
match_data = internal_match_data =
|
|
|
|
pcre2_match_data_create_from_pattern(code, gcontext);
|
|
|
|
if (internal_match_data == NULL) return PCRE2_ERROR_NOMEMORY;
|
2019-12-27 14:35:17 +01:00
|
|
|
}
|
|
|
|
|
2020-02-16 18:46:40 +01:00
|
|
|
else if (use_existing_match)
|
2014-11-08 17:09:24 +01:00
|
|
|
{
|
|
|
|
pcre2_general_context *gcontext = (mcontext == NULL)?
|
|
|
|
(pcre2_general_context *)code :
|
|
|
|
(pcre2_general_context *)mcontext;
|
2020-02-16 18:46:40 +01:00
|
|
|
int pairs = (code->top_bracket + 1 < match_data->oveccount)?
|
|
|
|
code->top_bracket + 1 : match_data->oveccount;
|
|
|
|
internal_match_data = pcre2_match_data_create(match_data->oveccount,
|
|
|
|
gcontext);
|
|
|
|
if (internal_match_data == NULL) return PCRE2_ERROR_NOMEMORY;
|
|
|
|
memcpy(internal_match_data, match_data, offsetof(pcre2_match_data, ovector)
|
|
|
|
+ 2*pairs*sizeof(PCRE2_SIZE));
|
|
|
|
match_data = internal_match_data;
|
2014-11-08 17:09:24 +01:00
|
|
|
}
|
2020-02-16 18:46:40 +01:00
|
|
|
|
|
|
|
/* Remember ovector details */
|
|
|
|
|
2014-11-08 17:09:24 +01:00
|
|
|
ovector = pcre2_get_ovector_pointer(match_data);
|
|
|
|
ovector_count = pcre2_get_ovector_count(match_data);
|
|
|
|
|
2018-11-12 17:02:01 +01:00
|
|
|
/* Fixed things in the callout block */
|
|
|
|
|
|
|
|
scb.version = 0;
|
|
|
|
scb.input = subject;
|
|
|
|
scb.output = (PCRE2_SPTR)buffer;
|
|
|
|
scb.ovector = ovector;
|
|
|
|
|
2015-10-30 18:30:03 +01:00
|
|
|
/* Find lengths of zero-terminated strings and the end of the replacement. */
|
|
|
|
|
|
|
|
if (length == PCRE2_ZERO_TERMINATED) length = PRIV(strlen)(subject);
|
|
|
|
if (rlength == PCRE2_ZERO_TERMINATED) rlength = PRIV(strlen)(replacement);
|
|
|
|
repend = replacement + rlength;
|
|
|
|
|
2014-11-11 17:51:07 +01:00
|
|
|
/* Check UTF replacement string if necessary. */
|
|
|
|
|
|
|
|
#ifdef SUPPORT_UNICODE
|
2015-10-07 19:32:48 +02:00
|
|
|
if (utf && (options & PCRE2_NO_UTF_CHECK) == 0)
|
2014-11-11 17:51:07 +01:00
|
|
|
{
|
2020-02-16 18:46:40 +01:00
|
|
|
rc = PRIV(valid_utf)(replacement, rlength, &(match_data->startchar));
|
2014-11-11 17:51:07 +01:00
|
|
|
if (rc != 0)
|
|
|
|
{
|
|
|
|
match_data->leftchar = 0;
|
|
|
|
goto EXIT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* SUPPORT_UNICODE */
|
2014-11-14 19:41:20 +01:00
|
|
|
|
2015-12-12 19:45:40 +01:00
|
|
|
/* Save the substitute options and remove them from the match options. */
|
2015-10-07 19:32:48 +02:00
|
|
|
|
2015-12-12 19:45:40 +01:00
|
|
|
suboptions = options & SUBSTITUTE_OPTIONS;
|
|
|
|
options &= ~SUBSTITUTE_OPTIONS;
|
2015-12-04 19:39:08 +01:00
|
|
|
|
2020-02-16 18:46:40 +01:00
|
|
|
/* Error if the start match offset is greater than the length of the subject. */
|
2014-11-08 17:09:24 +01:00
|
|
|
|
2016-12-09 19:19:38 +01:00
|
|
|
if (start_offset > length)
|
|
|
|
{
|
|
|
|
match_data->leftchar = 0;
|
|
|
|
rc = PCRE2_ERROR_BADOFFSET;
|
|
|
|
goto EXIT;
|
|
|
|
}
|
2020-01-22 18:50:12 +01:00
|
|
|
|
|
|
|
/* Copy up to the start offset, unless only the replacement is required. */
|
|
|
|
|
|
|
|
if (!replacement_only) CHECKMEMCPY(subject, start_offset);
|
2014-11-08 17:09:24 +01:00
|
|
|
|
2019-12-27 14:35:17 +01:00
|
|
|
/* Loop for global substituting. If PCRE2_SUBSTITUTE_MATCHED is set, the first
|
|
|
|
match is taken from the match_data that was passed in. */
|
2014-11-08 17:09:24 +01:00
|
|
|
|
2014-11-11 17:51:07 +01:00
|
|
|
subs = 0;
|
2014-11-08 17:09:24 +01:00
|
|
|
do
|
|
|
|
{
|
2015-10-07 19:32:48 +02:00
|
|
|
PCRE2_SPTR ptrstack[PTR_STACK_SIZE];
|
|
|
|
uint32_t ptrstackptr = 0;
|
2014-11-08 17:09:24 +01:00
|
|
|
|
2019-12-27 14:35:17 +01:00
|
|
|
if (use_existing_match)
|
|
|
|
{
|
|
|
|
rc = match_data->rc;
|
|
|
|
use_existing_match = FALSE;
|
|
|
|
}
|
|
|
|
else rc = pcre2_match(code, subject, length, start_offset, options|goptions,
|
2020-02-16 18:46:40 +01:00
|
|
|
match_data, mcontext);
|
2015-11-03 18:38:00 +01:00
|
|
|
|
2015-10-30 18:41:56 +01:00
|
|
|
#ifdef SUPPORT_UNICODE
|
|
|
|
if (utf) options |= PCRE2_NO_UTF_CHECK; /* Only need to check once */
|
2015-11-03 18:38:00 +01:00
|
|
|
#endif
|
2014-11-14 19:41:20 +01:00
|
|
|
|
|
|
|
/* Any error other than no match returns the error code. No match when not
|
|
|
|
doing the special after-empty-match global rematch, or when at the end of the
|
|
|
|
subject, breaks the global loop. Otherwise, advance the starting point by one
|
|
|
|
character, copying it to the output, and try again. */
|
2014-11-08 17:09:24 +01:00
|
|
|
|
|
|
|
if (rc < 0)
|
|
|
|
{
|
2014-11-14 19:41:20 +01:00
|
|
|
PCRE2_SIZE save_start;
|
|
|
|
|
2014-11-11 11:19:23 +01:00
|
|
|
if (rc != PCRE2_ERROR_NOMATCH) goto EXIT;
|
|
|
|
if (goptions == 0 || start_offset >= length) break;
|
2014-11-14 19:41:20 +01:00
|
|
|
|
2015-11-13 17:52:26 +01:00
|
|
|
/* Advance by one code point. Then, if CRLF is a valid newline sequence and
|
|
|
|
we have advanced into the middle of it, advance one more code point. In
|
|
|
|
other words, do not start in the middle of CRLF, even if CR and LF on their
|
|
|
|
own are valid newlines. */
|
|
|
|
|
2014-11-14 19:41:20 +01:00
|
|
|
save_start = start_offset++;
|
2015-11-13 17:52:26 +01:00
|
|
|
if (subject[start_offset-1] == CHAR_CR &&
|
|
|
|
code->newline_convention != PCRE2_NEWLINE_CR &&
|
|
|
|
code->newline_convention != PCRE2_NEWLINE_LF &&
|
|
|
|
start_offset < length &&
|
|
|
|
subject[start_offset] == CHAR_LF)
|
|
|
|
start_offset++;
|
|
|
|
|
|
|
|
/* Otherwise, in UTF mode, advance past any secondary code points. */
|
|
|
|
|
|
|
|
else if ((code->overall_options & PCRE2_UTF) != 0)
|
2014-11-08 17:09:24 +01:00
|
|
|
{
|
|
|
|
#if PCRE2_CODE_UNIT_WIDTH == 8
|
|
|
|
while (start_offset < length && (subject[start_offset] & 0xc0) == 0x80)
|
|
|
|
start_offset++;
|
|
|
|
#elif PCRE2_CODE_UNIT_WIDTH == 16
|
|
|
|
while (start_offset < length &&
|
|
|
|
(subject[start_offset] & 0xfc00) == 0xdc00)
|
|
|
|
start_offset++;
|
|
|
|
#endif
|
|
|
|
}
|
2014-11-14 19:41:20 +01:00
|
|
|
|
2020-01-22 18:50:12 +01:00
|
|
|
/* Copy what we have advanced past (unless not required), reset the special
|
|
|
|
global options, and continue to the next match. */
|
2014-11-14 19:41:20 +01:00
|
|
|
|
2015-12-12 19:45:40 +01:00
|
|
|
fraglength = start_offset - save_start;
|
2020-01-22 18:50:12 +01:00
|
|
|
if (!replacement_only) CHECKMEMCPY(subject + save_start, fraglength);
|
2014-11-08 17:09:24 +01:00
|
|
|
goptions = 0;
|
|
|
|
continue;
|
|
|
|
}
|
2014-11-14 19:41:20 +01:00
|
|
|
|
2015-11-03 18:38:00 +01:00
|
|
|
/* Handle a successful match. Matches that use \K to end before they start
|
2018-06-22 18:29:56 +02:00
|
|
|
or start before the current point in the subject are not supported. */
|
2019-12-27 14:35:17 +01:00
|
|
|
|
2018-06-22 18:29:56 +02:00
|
|
|
if (ovector[1] < ovector[0] || ovector[0] < start_offset)
|
2015-11-03 18:38:00 +01:00
|
|
|
{
|
|
|
|
rc = PCRE2_ERROR_BADSUBSPATTERN;
|
|
|
|
goto EXIT;
|
|
|
|
}
|
2019-12-27 14:35:17 +01:00
|
|
|
|
|
|
|
/* Check for the same match as previous. This is legitimate after matching an
|
2018-07-02 12:54:03 +02:00
|
|
|
empty string that starts after the initial match offset. We have tried again
|
|
|
|
at the match point in case the pattern is one like /(?<=\G.)/ which can never
|
|
|
|
match at its starting point, so running the match achieves the bumpalong. If
|
|
|
|
we do get the same (null) match at the original match point, it isn't such a
|
|
|
|
pattern, so we now do the empty string magic. In all other cases, a repeat
|
|
|
|
match should never occur. */
|
2019-12-27 14:35:17 +01:00
|
|
|
|
2018-07-02 12:54:03 +02:00
|
|
|
if (ovecsave[0] == ovector[0] && ovecsave[1] == ovector[1])
|
2019-12-27 14:35:17 +01:00
|
|
|
{
|
|
|
|
if (ovector[0] == ovector[1] && ovecsave[2] != start_offset)
|
|
|
|
{
|
|
|
|
goptions = PCRE2_NOTEMPTY_ATSTART | PCRE2_ANCHORED;
|
|
|
|
ovecsave[2] = start_offset;
|
|
|
|
continue; /* Back to the top of the loop */
|
2018-07-02 12:54:03 +02:00
|
|
|
}
|
|
|
|
rc = PCRE2_ERROR_INTERNAL_DUPMATCH;
|
2019-12-27 14:35:17 +01:00
|
|
|
goto EXIT;
|
|
|
|
}
|
|
|
|
|
2015-12-12 19:45:40 +01:00
|
|
|
/* Count substitutions with a paranoid check for integer overflow; surely no
|
|
|
|
real call to this function would ever hit this! */
|
2015-11-11 19:35:14 +01:00
|
|
|
|
|
|
|
if (subs == INT_MAX)
|
|
|
|
{
|
|
|
|
rc = PCRE2_ERROR_TOOMANYREPLACE;
|
|
|
|
goto EXIT;
|
|
|
|
}
|
2015-12-12 19:45:40 +01:00
|
|
|
subs++;
|
2015-11-11 19:35:14 +01:00
|
|
|
|
2020-01-22 18:50:12 +01:00
|
|
|
/* Copy the text leading up to the match (unless not required), and remember
|
|
|
|
where the insert begins and how many ovector pairs are set. */
|
2015-11-11 19:35:14 +01:00
|
|
|
|
2014-11-08 17:09:24 +01:00
|
|
|
if (rc == 0) rc = ovector_count;
|
2014-11-14 19:41:20 +01:00
|
|
|
fraglength = ovector[0] - start_offset;
|
2020-01-22 18:50:12 +01:00
|
|
|
if (!replacement_only) CHECKMEMCPY(subject + start_offset, fraglength);
|
2018-09-18 18:31:30 +02:00
|
|
|
scb.output_offsets[0] = buff_offset;
|
2018-11-12 17:02:01 +01:00
|
|
|
scb.oveccount = rc;
|
2014-11-08 17:09:24 +01:00
|
|
|
|
2019-12-27 14:35:17 +01:00
|
|
|
/* Process the replacement string. If the entire replacement is literal, just
|
2019-12-26 15:53:24 +01:00
|
|
|
copy it with length check. */
|
2019-12-27 14:35:17 +01:00
|
|
|
|
2015-10-07 19:32:48 +02:00
|
|
|
ptr = replacement;
|
2019-12-26 15:53:24 +01:00
|
|
|
if ((suboptions & PCRE2_SUBSTITUTE_LITERAL) != 0)
|
|
|
|
{
|
2019-12-27 14:35:17 +01:00
|
|
|
CHECKMEMCPY(ptr, rlength);
|
2019-12-26 15:53:24 +01:00
|
|
|
}
|
|
|
|
|
2019-12-27 14:35:17 +01:00
|
|
|
/* Within a non-literal replacement, which must be scanned character by
|
2019-12-26 15:53:24 +01:00
|
|
|
character, local literal mode can be set by \Q, but only in extended mode
|
|
|
|
when backslashes are being interpreted. In extended mode we must handle
|
|
|
|
nested substrings that are to be reprocessed. */
|
2019-12-27 14:35:17 +01:00
|
|
|
|
2019-12-26 15:53:24 +01:00
|
|
|
else for (;;)
|
2014-11-08 17:09:24 +01:00
|
|
|
{
|
2015-10-07 19:32:48 +02:00
|
|
|
uint32_t ch;
|
2015-12-12 19:45:40 +01:00
|
|
|
unsigned int chlen;
|
2015-10-07 19:32:48 +02:00
|
|
|
|
|
|
|
/* If at the end of a nested substring, pop the stack. */
|
|
|
|
|
|
|
|
if (ptr >= repend)
|
|
|
|
{
|
2018-11-17 17:59:39 +01:00
|
|
|
if (ptrstackptr == 0) break; /* End of replacement string */
|
2015-10-07 19:32:48 +02:00
|
|
|
repend = ptrstack[--ptrstackptr];
|
|
|
|
ptr = ptrstack[--ptrstackptr];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle the next character */
|
|
|
|
|
2019-12-26 15:53:24 +01:00
|
|
|
if (escaped_literal)
|
2015-10-07 19:32:48 +02:00
|
|
|
{
|
|
|
|
if (ptr[0] == CHAR_BACKSLASH && ptr < repend - 1 && ptr[1] == CHAR_E)
|
|
|
|
{
|
2019-12-26 15:53:24 +01:00
|
|
|
escaped_literal = FALSE;
|
2015-10-07 19:32:48 +02:00
|
|
|
ptr += 2;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
goto LOADLITERAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Not in literal mode. */
|
|
|
|
|
|
|
|
if (*ptr == CHAR_DOLLAR_SIGN)
|
2014-11-08 17:09:24 +01:00
|
|
|
{
|
2014-11-11 11:19:23 +01:00
|
|
|
int group, n;
|
2015-10-07 19:32:48 +02:00
|
|
|
uint32_t special = 0;
|
2014-11-11 11:19:23 +01:00
|
|
|
BOOL inparens;
|
2015-08-29 19:13:09 +02:00
|
|
|
BOOL star;
|
2014-11-11 11:19:23 +01:00
|
|
|
PCRE2_SIZE sublength;
|
2015-10-07 19:32:48 +02:00
|
|
|
PCRE2_SPTR text1_start = NULL;
|
|
|
|
PCRE2_SPTR text1_end = NULL;
|
|
|
|
PCRE2_SPTR text2_start = NULL;
|
|
|
|
PCRE2_SPTR text2_end = NULL;
|
2014-11-11 11:19:23 +01:00
|
|
|
PCRE2_UCHAR next;
|
2014-11-14 19:41:20 +01:00
|
|
|
PCRE2_UCHAR name[33];
|
|
|
|
|
2015-10-07 19:32:48 +02:00
|
|
|
if (++ptr >= repend) goto BAD;
|
|
|
|
if ((next = *ptr) == CHAR_DOLLAR_SIGN) goto LOADLITERAL;
|
2014-11-14 19:41:20 +01:00
|
|
|
|
2014-11-11 11:19:23 +01:00
|
|
|
group = -1;
|
|
|
|
n = 0;
|
|
|
|
inparens = FALSE;
|
2015-08-29 19:13:09 +02:00
|
|
|
star = FALSE;
|
2014-11-08 17:09:24 +01:00
|
|
|
|
|
|
|
if (next == CHAR_LEFT_CURLY_BRACKET)
|
|
|
|
{
|
2015-10-07 19:32:48 +02:00
|
|
|
if (++ptr >= repend) goto BAD;
|
|
|
|
next = *ptr;
|
2014-11-08 17:09:24 +01:00
|
|
|
inparens = TRUE;
|
|
|
|
}
|
|
|
|
|
2015-08-29 19:13:09 +02:00
|
|
|
if (next == CHAR_ASTERISK)
|
|
|
|
{
|
2015-10-07 19:32:48 +02:00
|
|
|
if (++ptr >= repend) goto BAD;
|
|
|
|
next = *ptr;
|
2015-08-29 19:13:09 +02:00
|
|
|
star = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!star && next >= CHAR_0 && next <= CHAR_9)
|
2014-11-08 17:09:24 +01:00
|
|
|
{
|
|
|
|
group = next - CHAR_0;
|
2015-10-07 19:32:48 +02:00
|
|
|
while (++ptr < repend)
|
2014-11-08 17:09:24 +01:00
|
|
|
{
|
2015-10-07 19:32:48 +02:00
|
|
|
next = *ptr;
|
2014-11-08 17:09:24 +01:00
|
|
|
if (next < CHAR_0 || next > CHAR_9) break;
|
|
|
|
group = group * 10 + next - CHAR_0;
|
2015-11-03 18:38:00 +01:00
|
|
|
|
2015-10-30 19:25:19 +01:00
|
|
|
/* A check for a number greater than the hightest captured group
|
2015-12-12 19:45:40 +01:00
|
|
|
is sufficient here; no need for a separate overflow check. If unknown
|
|
|
|
groups are to be treated as unset, just skip over any remaining
|
|
|
|
digits and carry on. */
|
2015-11-03 18:38:00 +01:00
|
|
|
|
2015-10-30 19:25:19 +01:00
|
|
|
if (group > code->top_bracket)
|
|
|
|
{
|
2015-12-12 19:45:40 +01:00
|
|
|
if ((suboptions & PCRE2_SUBSTITUTE_UNKNOWN_UNSET) != 0)
|
|
|
|
{
|
|
|
|
while (++ptr < repend && *ptr >= CHAR_0 && *ptr <= CHAR_9);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rc = PCRE2_ERROR_NOSUBSTRING;
|
|
|
|
goto PTREXIT;
|
|
|
|
}
|
2015-10-30 19:25:19 +01:00
|
|
|
}
|
2014-11-08 17:09:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const uint8_t *ctypes = code->tables + ctypes_offset;
|
|
|
|
while (MAX_255(next) && (ctypes[next] & ctype_word) != 0)
|
|
|
|
{
|
|
|
|
name[n++] = next;
|
2014-11-11 11:19:23 +01:00
|
|
|
if (n > 32) goto BAD;
|
2015-11-01 17:36:20 +01:00
|
|
|
if (++ptr >= repend) break;
|
|
|
|
next = *ptr;
|
2014-11-08 17:09:24 +01:00
|
|
|
}
|
2014-11-14 19:41:20 +01:00
|
|
|
if (n == 0) goto BAD;
|
2014-11-08 17:09:24 +01:00
|
|
|
name[n] = 0;
|
|
|
|
}
|
|
|
|
|
2015-10-07 19:32:48 +02:00
|
|
|
/* In extended mode we recognize ${name:+set text:unset text} and
|
|
|
|
${name:-default text}. */
|
|
|
|
|
2014-11-08 17:09:24 +01:00
|
|
|
if (inparens)
|
|
|
|
{
|
2015-12-12 19:45:40 +01:00
|
|
|
if ((suboptions & PCRE2_SUBSTITUTE_EXTENDED) != 0 &&
|
|
|
|
!star && ptr < repend - 2 && next == CHAR_COLON)
|
2015-10-07 19:32:48 +02:00
|
|
|
{
|
|
|
|
special = *(++ptr);
|
|
|
|
if (special != CHAR_PLUS && special != CHAR_MINUS)
|
|
|
|
{
|
|
|
|
rc = PCRE2_ERROR_BADSUBSTITUTION;
|
|
|
|
goto PTREXIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
text1_start = ++ptr;
|
|
|
|
rc = find_text_end(code, &ptr, repend, special == CHAR_MINUS);
|
|
|
|
if (rc != 0) goto PTREXIT;
|
|
|
|
text1_end = ptr;
|
|
|
|
|
|
|
|
if (special == CHAR_PLUS && *ptr == CHAR_COLON)
|
|
|
|
{
|
|
|
|
text2_start = ++ptr;
|
|
|
|
rc = find_text_end(code, &ptr, repend, TRUE);
|
|
|
|
if (rc != 0) goto PTREXIT;
|
|
|
|
text2_end = ptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (ptr >= repend || *ptr != CHAR_RIGHT_CURLY_BRACKET)
|
|
|
|
{
|
|
|
|
rc = PCRE2_ERROR_REPMISSINGBRACE;
|
|
|
|
goto PTREXIT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr++;
|
2014-11-08 17:09:24 +01:00
|
|
|
}
|
2014-11-14 19:41:20 +01:00
|
|
|
|
2015-12-12 19:45:40 +01:00
|
|
|
/* Have found a syntactically correct group number or name, or *name.
|
|
|
|
Only *MARK is currently recognized. */
|
2014-11-08 17:09:24 +01:00
|
|
|
|
2015-08-29 19:13:09 +02:00
|
|
|
if (star)
|
|
|
|
{
|
|
|
|
if (PRIV(strcmp_c8)(name, STRING_MARK) == 0)
|
|
|
|
{
|
|
|
|
PCRE2_SPTR mark = pcre2_get_mark(match_data);
|
|
|
|
if (mark != NULL)
|
|
|
|
{
|
2015-12-12 19:45:40 +01:00
|
|
|
PCRE2_SPTR mark_start = mark;
|
|
|
|
while (*mark != 0) mark++;
|
|
|
|
fraglength = mark - mark_start;
|
|
|
|
CHECKMEMCPY(mark_start, fraglength);
|
2015-08-29 19:13:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else goto BAD;
|
|
|
|
}
|
|
|
|
|
2015-10-07 19:32:48 +02:00
|
|
|
/* Substitute the contents of a group. We don't use substring_copy
|
|
|
|
functions any more, in order to support case forcing. */
|
2014-11-14 19:41:20 +01:00
|
|
|
|
2015-08-29 19:13:09 +02:00
|
|
|
else
|
|
|
|
{
|
2015-10-07 19:32:48 +02:00
|
|
|
PCRE2_SPTR subptr, subptrend;
|
2015-11-03 18:38:00 +01:00
|
|
|
|
|
|
|
/* Find a number for a named group. In case there are duplicate names,
|
2015-12-12 19:45:40 +01:00
|
|
|
search for the first one that is set. If the name is not found when
|
|
|
|
PCRE2_SUBSTITUTE_UNKNOWN_EMPTY is set, set the group number to a
|
|
|
|
non-existent group. */
|
2015-10-07 19:32:48 +02:00
|
|
|
|
2015-08-29 19:13:09 +02:00
|
|
|
if (group < 0)
|
2015-10-07 19:32:48 +02:00
|
|
|
{
|
|
|
|
PCRE2_SPTR first, last, entry;
|
|
|
|
rc = pcre2_substring_nametable_scan(code, name, &first, &last);
|
2015-12-12 19:45:40 +01:00
|
|
|
if (rc == PCRE2_ERROR_NOSUBSTRING &&
|
|
|
|
(suboptions & PCRE2_SUBSTITUTE_UNKNOWN_UNSET) != 0)
|
|
|
|
{
|
|
|
|
group = code->top_bracket + 1;
|
|
|
|
}
|
|
|
|
else
|
2015-10-07 19:32:48 +02:00
|
|
|
{
|
2015-12-12 19:45:40 +01:00
|
|
|
if (rc < 0) goto PTREXIT;
|
|
|
|
for (entry = first; entry <= last; entry += rc)
|
2015-10-07 19:32:48 +02:00
|
|
|
{
|
2015-12-12 19:45:40 +01:00
|
|
|
uint32_t ng = GET2(entry, 0);
|
|
|
|
if (ng < ovector_count)
|
2015-10-07 19:32:48 +02:00
|
|
|
{
|
2015-12-12 19:45:40 +01:00
|
|
|
if (group < 0) group = ng; /* First in ovector */
|
|
|
|
if (ovector[ng*2] != PCRE2_UNSET)
|
|
|
|
{
|
|
|
|
group = ng; /* First that is set */
|
|
|
|
break;
|
|
|
|
}
|
2015-11-03 18:38:00 +01:00
|
|
|
}
|
2015-10-07 19:32:48 +02:00
|
|
|
}
|
2015-11-03 18:38:00 +01:00
|
|
|
|
2015-12-12 19:45:40 +01:00
|
|
|
/* If group is still negative, it means we did not find a group
|
|
|
|
that is in the ovector. Just set the first group. */
|
2015-11-03 18:38:00 +01:00
|
|
|
|
2015-12-12 19:45:40 +01:00
|
|
|
if (group < 0) group = GET2(first, 0);
|
|
|
|
}
|
2015-10-07 19:32:48 +02:00
|
|
|
}
|
|
|
|
|
2015-12-04 19:39:08 +01:00
|
|
|
/* We now have a group that is identified by number. Find the length of
|
|
|
|
the captured string. If a group in a non-special substitution is unset
|
|
|
|
when PCRE2_SUBSTITUTE_UNSET_EMPTY is set, substitute nothing. */
|
|
|
|
|
2015-10-07 19:32:48 +02:00
|
|
|
rc = pcre2_substring_length_bynumber(match_data, group, &sublength);
|
2015-12-04 19:39:08 +01:00
|
|
|
if (rc < 0)
|
|
|
|
{
|
2015-12-12 19:45:40 +01:00
|
|
|
if (rc == PCRE2_ERROR_NOSUBSTRING &&
|
|
|
|
(suboptions & PCRE2_SUBSTITUTE_UNKNOWN_UNSET) != 0)
|
|
|
|
{
|
|
|
|
rc = PCRE2_ERROR_UNSET;
|
|
|
|
}
|
2015-12-04 19:39:08 +01:00
|
|
|
if (rc != PCRE2_ERROR_UNSET) goto PTREXIT; /* Non-unset errors */
|
|
|
|
if (special == 0) /* Plain substitution */
|
|
|
|
{
|
2015-12-12 19:45:40 +01:00
|
|
|
if ((suboptions & PCRE2_SUBSTITUTE_UNSET_EMPTY) != 0) continue;
|
2015-12-04 19:39:08 +01:00
|
|
|
goto PTREXIT; /* Else error */
|
|
|
|
}
|
|
|
|
}
|
2015-10-07 19:32:48 +02:00
|
|
|
|
|
|
|
/* If special is '+' we have a 'set' and possibly an 'unset' text,
|
|
|
|
both of which are reprocessed when used. If special is '-' we have a
|
|
|
|
default text for when the group is unset; it must be reprocessed. */
|
|
|
|
|
|
|
|
if (special != 0)
|
|
|
|
{
|
|
|
|
if (special == CHAR_MINUS)
|
|
|
|
{
|
|
|
|
if (rc == 0) goto LITERAL_SUBSTITUTE;
|
|
|
|
text2_start = text1_start;
|
|
|
|
text2_end = text1_end;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ptrstackptr >= PTR_STACK_SIZE) goto BAD;
|
|
|
|
ptrstack[ptrstackptr++] = ptr;
|
|
|
|
ptrstack[ptrstackptr++] = repend;
|
|
|
|
|
|
|
|
if (rc == 0)
|
|
|
|
{
|
|
|
|
ptr = text1_start;
|
|
|
|
repend = text1_end;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ptr = text2_start;
|
|
|
|
repend = text2_end;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise we have a literal substitution of a group's contents. */
|
2015-08-29 19:13:09 +02:00
|
|
|
|
2015-10-07 19:32:48 +02:00
|
|
|
LITERAL_SUBSTITUTE:
|
|
|
|
subptr = subject + ovector[group*2];
|
|
|
|
subptrend = subject + ovector[group*2 + 1];
|
|
|
|
|
|
|
|
/* Substitute a literal string, possibly forcing alphabetic case. */
|
|
|
|
|
|
|
|
while (subptr < subptrend)
|
|
|
|
{
|
|
|
|
GETCHARINCTEST(ch, subptr);
|
|
|
|
if (forcecase != 0)
|
|
|
|
{
|
|
|
|
#ifdef SUPPORT_UNICODE
|
2020-02-23 17:40:05 +01:00
|
|
|
if (utf || ucp)
|
2015-10-07 19:32:48 +02:00
|
|
|
{
|
|
|
|
uint32_t type = UCD_CHARTYPE(ch);
|
|
|
|
if (PRIV(ucp_gentype)[type] == ucp_L &&
|
|
|
|
type != ((forcecase > 0)? ucp_Lu : ucp_Ll))
|
|
|
|
ch = UCD_OTHERCASE(ch);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
if (((code->tables + cbits_offset +
|
|
|
|
((forcecase > 0)? cbit_upper:cbit_lower)
|
2019-04-12 16:40:27 +02:00
|
|
|
)[ch/8] & (1u << (ch%8))) == 0)
|
2015-10-07 19:32:48 +02:00
|
|
|
ch = (code->tables + fcc_offset)[ch];
|
|
|
|
}
|
|
|
|
forcecase = forcecasereset;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef SUPPORT_UNICODE
|
2015-12-12 19:45:40 +01:00
|
|
|
if (utf) chlen = PRIV(ord2utf)(ch, temp); else
|
2015-10-07 19:32:48 +02:00
|
|
|
#endif
|
|
|
|
{
|
2015-12-12 19:45:40 +01:00
|
|
|
temp[0] = ch;
|
|
|
|
chlen = 1;
|
2015-10-07 19:32:48 +02:00
|
|
|
}
|
2015-12-12 19:45:40 +01:00
|
|
|
CHECKMEMCPY(temp, chlen);
|
2015-10-07 19:32:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle an escape sequence in extended mode. We can use check_escape()
|
|
|
|
to process \Q, \E, \c, \o, \x and \ followed by non-alphanumerics, but
|
|
|
|
the case-forcing escapes are not supported in pcre2_compile() so must be
|
|
|
|
recognized here. */
|
|
|
|
|
2015-12-12 19:45:40 +01:00
|
|
|
else if ((suboptions & PCRE2_SUBSTITUTE_EXTENDED) != 0 &&
|
|
|
|
*ptr == CHAR_BACKSLASH)
|
2015-10-07 19:32:48 +02:00
|
|
|
{
|
2016-10-02 18:01:01 +02:00
|
|
|
int errorcode;
|
2015-10-07 19:32:48 +02:00
|
|
|
|
|
|
|
if (ptr < repend - 1) switch (ptr[1])
|
|
|
|
{
|
|
|
|
case CHAR_L:
|
|
|
|
forcecase = forcecasereset = -1;
|
|
|
|
ptr += 2;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case CHAR_l:
|
|
|
|
forcecase = -1;
|
|
|
|
forcecasereset = 0;
|
|
|
|
ptr += 2;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case CHAR_U:
|
|
|
|
forcecase = forcecasereset = 1;
|
|
|
|
ptr += 2;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case CHAR_u:
|
|
|
|
forcecase = 1;
|
|
|
|
forcecasereset = 0;
|
|
|
|
ptr += 2;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-10-02 18:01:01 +02:00
|
|
|
ptr++; /* Point after \ */
|
2015-10-07 19:32:48 +02:00
|
|
|
rc = PRIV(check_escape)(&ptr, repend, &ch, &errorcode,
|
2019-02-12 18:50:19 +01:00
|
|
|
code->overall_options, code->extra_options, FALSE, NULL);
|
2015-10-07 19:32:48 +02:00
|
|
|
if (errorcode != 0) goto BADESCAPE;
|
|
|
|
|
|
|
|
switch(rc)
|
|
|
|
{
|
|
|
|
case ESC_E:
|
|
|
|
forcecase = forcecasereset = 0;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case ESC_Q:
|
2019-12-26 15:53:24 +01:00
|
|
|
escaped_literal = TRUE;
|
2015-10-07 19:32:48 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
case 0: /* Data character */
|
|
|
|
goto LITERAL;
|
|
|
|
|
|
|
|
default:
|
|
|
|
goto BADESCAPE;
|
2015-08-29 19:13:09 +02:00
|
|
|
}
|
2014-11-08 17:09:24 +01:00
|
|
|
}
|
|
|
|
|
2015-10-07 19:32:48 +02:00
|
|
|
/* Handle a literal code unit */
|
2014-11-08 17:09:24 +01:00
|
|
|
|
2015-10-07 19:32:48 +02:00
|
|
|
else
|
2014-11-08 17:09:24 +01:00
|
|
|
{
|
2015-10-07 19:32:48 +02:00
|
|
|
LOADLITERAL:
|
|
|
|
GETCHARINCTEST(ch, ptr); /* Get character value, increment pointer */
|
|
|
|
|
2014-11-08 17:09:24 +01:00
|
|
|
LITERAL:
|
2015-10-07 19:32:48 +02:00
|
|
|
if (forcecase != 0)
|
|
|
|
{
|
|
|
|
#ifdef SUPPORT_UNICODE
|
2020-02-23 17:40:05 +01:00
|
|
|
if (utf || ucp)
|
2015-10-07 19:32:48 +02:00
|
|
|
{
|
|
|
|
uint32_t type = UCD_CHARTYPE(ch);
|
|
|
|
if (PRIV(ucp_gentype)[type] == ucp_L &&
|
|
|
|
type != ((forcecase > 0)? ucp_Lu : ucp_Ll))
|
|
|
|
ch = UCD_OTHERCASE(ch);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
if (((code->tables + cbits_offset +
|
|
|
|
((forcecase > 0)? cbit_upper:cbit_lower)
|
2019-04-12 16:40:27 +02:00
|
|
|
)[ch/8] & (1u << (ch%8))) == 0)
|
2015-10-07 19:32:48 +02:00
|
|
|
ch = (code->tables + fcc_offset)[ch];
|
|
|
|
}
|
|
|
|
forcecase = forcecasereset;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef SUPPORT_UNICODE
|
2015-12-12 19:45:40 +01:00
|
|
|
if (utf) chlen = PRIV(ord2utf)(ch, temp); else
|
2015-10-07 19:32:48 +02:00
|
|
|
#endif
|
|
|
|
{
|
2015-12-12 19:45:40 +01:00
|
|
|
temp[0] = ch;
|
|
|
|
chlen = 1;
|
2015-10-07 19:32:48 +02:00
|
|
|
}
|
2015-12-12 19:45:40 +01:00
|
|
|
CHECKMEMCPY(temp, chlen);
|
|
|
|
} /* End handling a literal code unit */
|
|
|
|
} /* End of loop for scanning the replacement. */
|
2014-11-08 17:09:24 +01:00
|
|
|
|
2019-12-27 14:35:17 +01:00
|
|
|
/* The replacement has been copied to the output, or its size has been
|
|
|
|
remembered. Do the callout if there is one and we have done an actual
|
2018-09-18 18:31:30 +02:00
|
|
|
replacement. */
|
2019-12-27 14:35:17 +01:00
|
|
|
|
2019-03-11 18:29:08 +01:00
|
|
|
if (!overflowed && mcontext != NULL && mcontext->substitute_callout != NULL)
|
2018-09-18 18:31:30 +02:00
|
|
|
{
|
2019-12-27 14:35:17 +01:00
|
|
|
scb.subscount = subs;
|
2018-09-18 18:31:30 +02:00
|
|
|
scb.output_offsets[1] = buff_offset;
|
2019-12-27 14:35:17 +01:00
|
|
|
rc = mcontext->substitute_callout(&scb, mcontext->substitute_callout_data);
|
2018-11-12 17:02:01 +01:00
|
|
|
|
2019-12-27 14:35:17 +01:00
|
|
|
/* A non-zero return means cancel this substitution. Instead, copy the
|
2018-11-12 17:02:01 +01:00
|
|
|
matched string fragment. */
|
|
|
|
|
|
|
|
if (rc != 0)
|
|
|
|
{
|
|
|
|
PCRE2_SIZE newlength = scb.output_offsets[1] - scb.output_offsets[0];
|
|
|
|
PCRE2_SIZE oldlength = ovector[1] - ovector[0];
|
2019-12-27 14:35:17 +01:00
|
|
|
|
2018-11-12 17:02:01 +01:00
|
|
|
buff_offset -= newlength;
|
|
|
|
lengthleft += newlength;
|
2020-01-22 18:50:12 +01:00
|
|
|
if (!replacement_only) CHECKMEMCPY(subject + ovector[0], oldlength);
|
2019-12-27 14:35:17 +01:00
|
|
|
|
2018-11-12 17:02:01 +01:00
|
|
|
/* A negative return means do not do any more. */
|
2019-12-27 14:35:17 +01:00
|
|
|
|
2018-11-12 17:02:01 +01:00
|
|
|
if (rc < 0) suboptions &= (~PCRE2_SUBSTITUTE_GLOBAL);
|
|
|
|
}
|
2019-12-27 14:35:17 +01:00
|
|
|
}
|
|
|
|
|
2018-09-18 18:31:30 +02:00
|
|
|
/* Save the details of this match. See above for how this data is used. If we
|
2020-02-16 18:46:40 +01:00
|
|
|
matched an empty string, do the magic for global matches. Update the start
|
|
|
|
offset to point to the rest of the subject string. If we re-used an existing
|
|
|
|
match for the first match, switch to the internal match data block. */
|
2019-12-27 14:35:17 +01:00
|
|
|
|
|
|
|
ovecsave[0] = ovector[0];
|
|
|
|
ovecsave[1] = ovector[1];
|
2018-07-02 12:54:03 +02:00
|
|
|
ovecsave[2] = start_offset;
|
2019-12-27 14:35:17 +01:00
|
|
|
|
2018-07-02 12:54:03 +02:00
|
|
|
goptions = (ovector[0] != ovector[1] || ovector[0] > start_offset)? 0 :
|
2014-11-08 17:09:24 +01:00
|
|
|
PCRE2_ANCHORED|PCRE2_NOTEMPTY_ATSTART;
|
2018-07-02 12:54:03 +02:00
|
|
|
start_offset = ovector[1];
|
2015-12-12 19:45:40 +01:00
|
|
|
} while ((suboptions & PCRE2_SUBSTITUTE_GLOBAL) != 0); /* Repeat "do" loop */
|
2014-11-08 17:09:24 +01:00
|
|
|
|
2020-01-22 18:50:12 +01:00
|
|
|
/* Copy the rest of the subject unless not required, and terminate the output
|
|
|
|
with a binary zero. */
|
|
|
|
|
|
|
|
if (!replacement_only)
|
|
|
|
{
|
|
|
|
fraglength = length - start_offset;
|
|
|
|
CHECKMEMCPY(subject + start_offset, fraglength);
|
|
|
|
}
|
2014-11-08 17:09:24 +01:00
|
|
|
|
2015-12-12 19:45:40 +01:00
|
|
|
temp[0] = 0;
|
2020-01-22 18:50:12 +01:00
|
|
|
CHECKMEMCPY(temp, 1);
|
2015-12-12 19:45:40 +01:00
|
|
|
|
|
|
|
/* If overflowed is set it means the PCRE2_SUBSTITUTE_OVERFLOW_LENGTH is set,
|
|
|
|
and matching has carried on after a full buffer, in order to compute the length
|
|
|
|
needed. Otherwise, an overflow generates an immediate error return. */
|
|
|
|
|
|
|
|
if (overflowed)
|
|
|
|
{
|
|
|
|
rc = PCRE2_ERROR_NOMEMORY;
|
|
|
|
*blength = buff_length + extra_needed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* After a successful execution, return the number of substitutions and set the
|
|
|
|
length of buffer used, excluding the trailing zero. */
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rc = subs;
|
|
|
|
*blength = buff_offset - 1;
|
|
|
|
}
|
2014-11-08 17:09:24 +01:00
|
|
|
|
|
|
|
EXIT:
|
2020-02-16 18:46:40 +01:00
|
|
|
if (internal_match_data != NULL) pcre2_match_data_free(internal_match_data);
|
2014-11-14 19:41:20 +01:00
|
|
|
else match_data->rc = rc;
|
2014-11-08 17:09:24 +01:00
|
|
|
return rc;
|
|
|
|
|
|
|
|
NOROOM:
|
|
|
|
rc = PCRE2_ERROR_NOMEMORY;
|
|
|
|
goto EXIT;
|
2014-11-11 11:19:23 +01:00
|
|
|
|
|
|
|
BAD:
|
|
|
|
rc = PCRE2_ERROR_BADREPLACEMENT;
|
2015-10-07 19:32:48 +02:00
|
|
|
goto PTREXIT;
|
|
|
|
|
|
|
|
BADESCAPE:
|
|
|
|
rc = PCRE2_ERROR_BADREPESCAPE;
|
|
|
|
|
|
|
|
PTREXIT:
|
|
|
|
*blength = (PCRE2_SIZE)(ptr - replacement);
|
2014-11-11 11:19:23 +01:00
|
|
|
goto EXIT;
|
2014-11-08 17:09:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* End of pcre2_substitute.c */
|