Blame glib/pcre/pcre_get.c

Packit ae235b
/*************************************************
Packit ae235b
*      Perl-Compatible Regular Expressions       *
Packit ae235b
*************************************************/
Packit ae235b
Packit ae235b
/* PCRE is a library of functions to support regular expressions whose syntax
Packit ae235b
and semantics are as close as possible to those of the Perl 5 language.
Packit ae235b
Packit ae235b
                       Written by Philip Hazel
Packit ae235b
           Copyright (c) 1997-2012 University of Cambridge
Packit ae235b
Packit ae235b
-----------------------------------------------------------------------------
Packit ae235b
Redistribution and use in source and binary forms, with or without
Packit ae235b
modification, are permitted provided that the following conditions are met:
Packit ae235b
Packit ae235b
    * Redistributions of source code must retain the above copyright notice,
Packit ae235b
      this list of conditions and the following disclaimer.
Packit ae235b
Packit ae235b
    * Redistributions in binary form must reproduce the above copyright
Packit ae235b
      notice, this list of conditions and the following disclaimer in the
Packit ae235b
      documentation and/or other materials provided with the distribution.
Packit ae235b
Packit ae235b
    * Neither the name of the University of Cambridge nor the names of its
Packit ae235b
      contributors may be used to endorse or promote products derived from
Packit ae235b
      this software without specific prior written permission.
Packit ae235b
Packit ae235b
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Packit ae235b
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit ae235b
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit ae235b
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
Packit ae235b
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
Packit ae235b
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
Packit ae235b
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Packit ae235b
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
Packit ae235b
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
Packit ae235b
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Packit ae235b
POSSIBILITY OF SUCH DAMAGE.
Packit ae235b
-----------------------------------------------------------------------------
Packit ae235b
*/
Packit ae235b
Packit ae235b
Packit ae235b
/* This module contains some convenience functions for extracting substrings
Packit ae235b
from the subject string after a regex match has succeeded. The original idea
Packit ae235b
for these functions came from Scott Wimer. */
Packit ae235b
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include "pcre_internal.h"
Packit ae235b
Packit ae235b
Packit ae235b
/*************************************************
Packit ae235b
*           Find number for named string         *
Packit ae235b
*************************************************/
Packit ae235b
Packit ae235b
/* This function is used by the get_first_set() function below, as well
Packit ae235b
as being generally available. It assumes that names are unique.
Packit ae235b
Packit ae235b
Arguments:
Packit ae235b
  code        the compiled regex
Packit ae235b
  stringname  the name whose number is required
Packit ae235b
Packit ae235b
Returns:      the number of the named parentheses, or a negative number
Packit ae235b
                (PCRE_ERROR_NOSUBSTRING) if not found
Packit ae235b
*/
Packit ae235b
Packit ae235b
#ifdef COMPILE_PCRE8
Packit ae235b
PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
Packit ae235b
pcre_get_stringnumber(const pcre *code, const char *stringname)
Packit ae235b
#else
Packit ae235b
PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
Packit ae235b
pcre16_get_stringnumber(const pcre16 *code, PCRE_SPTR16 stringname)
Packit ae235b
#endif
Packit ae235b
{
Packit ae235b
int rc;
Packit ae235b
int entrysize;
Packit ae235b
int top, bot;
Packit ae235b
pcre_uchar *nametable;
Packit ae235b
Packit ae235b
#ifdef COMPILE_PCRE8
Packit ae235b
if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMECOUNT, &top)) != 0)
Packit ae235b
  return rc;
Packit ae235b
if (top <= 0) return PCRE_ERROR_NOSUBSTRING;
Packit ae235b
Packit ae235b
if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMEENTRYSIZE, &entrysize)) != 0)
Packit ae235b
  return rc;
Packit ae235b
if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMETABLE, &nametable)) != 0)
Packit ae235b
  return rc;
Packit ae235b
#endif
Packit ae235b
#ifdef COMPILE_PCRE16
Packit ae235b
if ((rc = pcre16_fullinfo(code, NULL, PCRE_INFO_NAMECOUNT, &top)) != 0)
Packit ae235b
  return rc;
Packit ae235b
if (top <= 0) return PCRE_ERROR_NOSUBSTRING;
Packit ae235b
Packit ae235b
if ((rc = pcre16_fullinfo(code, NULL, PCRE_INFO_NAMEENTRYSIZE, &entrysize)) != 0)
Packit ae235b
  return rc;
Packit ae235b
if ((rc = pcre16_fullinfo(code, NULL, PCRE_INFO_NAMETABLE, &nametable)) != 0)
Packit ae235b
  return rc;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
bot = 0;
Packit ae235b
while (top > bot)
Packit ae235b
  {
Packit ae235b
  int mid = (top + bot) / 2;
Packit ae235b
  pcre_uchar *entry = nametable + entrysize*mid;
Packit ae235b
  int c = STRCMP_UC_UC((pcre_uchar *)stringname,
Packit ae235b
    (pcre_uchar *)(entry + IMM2_SIZE));
Packit ae235b
  if (c == 0) return GET2(entry, 0);
Packit ae235b
  if (c > 0) bot = mid + 1; else top = mid;
Packit ae235b
  }
Packit ae235b
Packit ae235b
return PCRE_ERROR_NOSUBSTRING;
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
/*************************************************
Packit ae235b
*     Find (multiple) entries for named string   *
Packit ae235b
*************************************************/
Packit ae235b
Packit ae235b
/* This is used by the get_first_set() function below, as well as being
Packit ae235b
generally available. It is used when duplicated names are permitted.
Packit ae235b
Packit ae235b
Arguments:
Packit ae235b
  code        the compiled regex
Packit ae235b
  stringname  the name whose entries required
Packit ae235b
  firstptr    where to put the pointer to the first entry
Packit ae235b
  lastptr     where to put the pointer to the last entry
Packit ae235b
Packit ae235b
Returns:      the length of each entry, or a negative number
Packit ae235b
                (PCRE_ERROR_NOSUBSTRING) if not found
Packit ae235b
*/
Packit ae235b
Packit ae235b
#ifdef COMPILE_PCRE8
Packit ae235b
PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
Packit ae235b
pcre_get_stringtable_entries(const pcre *code, const char *stringname,
Packit ae235b
  char **firstptr, char **lastptr)
Packit ae235b
#else
Packit ae235b
PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
Packit ae235b
pcre16_get_stringtable_entries(const pcre16 *code, PCRE_SPTR16 stringname,
Packit ae235b
  PCRE_UCHAR16 **firstptr, PCRE_UCHAR16 **lastptr)
Packit ae235b
#endif
Packit ae235b
{
Packit ae235b
int rc;
Packit ae235b
int entrysize;
Packit ae235b
int top, bot;
Packit ae235b
pcre_uchar *nametable, *lastentry;
Packit ae235b
Packit ae235b
#ifdef COMPILE_PCRE8
Packit ae235b
if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMECOUNT, &top)) != 0)
Packit ae235b
  return rc;
Packit ae235b
if (top <= 0) return PCRE_ERROR_NOSUBSTRING;
Packit ae235b
Packit ae235b
if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMEENTRYSIZE, &entrysize)) != 0)
Packit ae235b
  return rc;
Packit ae235b
if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMETABLE, &nametable)) != 0)
Packit ae235b
  return rc;
Packit ae235b
#endif
Packit ae235b
#ifdef COMPILE_PCRE16
Packit ae235b
if ((rc = pcre16_fullinfo(code, NULL, PCRE_INFO_NAMECOUNT, &top)) != 0)
Packit ae235b
  return rc;
Packit ae235b
if (top <= 0) return PCRE_ERROR_NOSUBSTRING;
Packit ae235b
Packit ae235b
if ((rc = pcre16_fullinfo(code, NULL, PCRE_INFO_NAMEENTRYSIZE, &entrysize)) != 0)
Packit ae235b
  return rc;
Packit ae235b
if ((rc = pcre16_fullinfo(code, NULL, PCRE_INFO_NAMETABLE, &nametable)) != 0)
Packit ae235b
  return rc;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
lastentry = nametable + entrysize * (top - 1);
Packit ae235b
bot = 0;
Packit ae235b
while (top > bot)
Packit ae235b
  {
Packit ae235b
  int mid = (top + bot) / 2;
Packit ae235b
  pcre_uchar *entry = nametable + entrysize*mid;
Packit ae235b
  int c = STRCMP_UC_UC((pcre_uchar *)stringname,
Packit ae235b
    (pcre_uchar *)(entry + IMM2_SIZE));
Packit ae235b
  if (c == 0)
Packit ae235b
    {
Packit ae235b
    pcre_uchar *first = entry;
Packit ae235b
    pcre_uchar *last = entry;
Packit ae235b
    while (first > nametable)
Packit ae235b
      {
Packit ae235b
      if (STRCMP_UC_UC((pcre_uchar *)stringname,
Packit ae235b
        (pcre_uchar *)(first - entrysize + IMM2_SIZE)) != 0) break;
Packit ae235b
      first -= entrysize;
Packit ae235b
      }
Packit ae235b
    while (last < lastentry)
Packit ae235b
      {
Packit ae235b
      if (STRCMP_UC_UC((pcre_uchar *)stringname,
Packit ae235b
        (pcre_uchar *)(last + entrysize + IMM2_SIZE)) != 0) break;
Packit ae235b
      last += entrysize;
Packit ae235b
      }
Packit ae235b
#ifdef COMPILE_PCRE8
Packit ae235b
    *firstptr = (char *)first;
Packit ae235b
    *lastptr = (char *)last;
Packit ae235b
#else
Packit ae235b
    *firstptr = (PCRE_UCHAR16 *)first;
Packit ae235b
    *lastptr = (PCRE_UCHAR16 *)last;
Packit ae235b
#endif
Packit ae235b
    return entrysize;
Packit ae235b
    }
Packit ae235b
  if (c > 0) bot = mid + 1; else top = mid;
Packit ae235b
  }
Packit ae235b
Packit ae235b
return PCRE_ERROR_NOSUBSTRING;
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
/*************************************************
Packit ae235b
*    Find first set of multiple named strings    *
Packit ae235b
*************************************************/
Packit ae235b
Packit ae235b
/* This function allows for duplicate names in the table of named substrings.
Packit ae235b
It returns the number of the first one that was set in a pattern match.
Packit ae235b
Packit ae235b
Arguments:
Packit ae235b
  code         the compiled regex
Packit ae235b
  stringname   the name of the capturing substring
Packit ae235b
  ovector      the vector of matched substrings
Packit ae235b
Packit ae235b
Returns:       the number of the first that is set,
Packit ae235b
               or the number of the last one if none are set,
Packit ae235b
               or a negative number on error
Packit ae235b
*/
Packit ae235b
Packit ae235b
#ifdef COMPILE_PCRE8
Packit ae235b
static int
Packit ae235b
get_first_set(const pcre *code, const char *stringname, int *ovector)
Packit ae235b
#else
Packit ae235b
static int
Packit ae235b
get_first_set(const pcre16 *code, PCRE_SPTR16 stringname, int *ovector)
Packit ae235b
#endif
Packit ae235b
{
Packit ae235b
const REAL_PCRE *re = (const REAL_PCRE *)code;
Packit ae235b
int entrysize;
Packit ae235b
pcre_uchar *entry;
Packit ae235b
#ifdef COMPILE_PCRE8
Packit ae235b
char *first, *last;
Packit ae235b
#else
Packit ae235b
PCRE_UCHAR16 *first, *last;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef COMPILE_PCRE8
Packit ae235b
if ((re->options & PCRE_DUPNAMES) == 0 && (re->flags & PCRE_JCHANGED) == 0)
Packit ae235b
  return pcre_get_stringnumber(code, stringname);
Packit ae235b
entrysize = pcre_get_stringtable_entries(code, stringname, &first, &last);
Packit ae235b
#else
Packit ae235b
if ((re->options & PCRE_DUPNAMES) == 0 && (re->flags & PCRE_JCHANGED) == 0)
Packit ae235b
  return pcre16_get_stringnumber(code, stringname);
Packit ae235b
entrysize = pcre16_get_stringtable_entries(code, stringname, &first, &last);
Packit ae235b
#endif
Packit ae235b
if (entrysize <= 0) return entrysize;
Packit ae235b
for (entry = (pcre_uchar *)first; entry <= (pcre_uchar *)last; entry += entrysize)
Packit ae235b
  {
Packit ae235b
  int n = GET2(entry, 0);
Packit ae235b
  if (ovector[n*2] >= 0) return n;
Packit ae235b
  }
Packit ae235b
return GET2(entry, 0);
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
/*************************************************
Packit ae235b
*      Copy captured string to given buffer      *
Packit ae235b
*************************************************/
Packit ae235b
Packit ae235b
/* This function copies a single captured substring into a given buffer.
Packit ae235b
Note that we use memcpy() rather than strncpy() in case there are binary zeros
Packit ae235b
in the string.
Packit ae235b
Packit ae235b
Arguments:
Packit ae235b
  subject        the subject string that was matched
Packit ae235b
  ovector        pointer to the offsets table
Packit ae235b
  stringcount    the number of substrings that were captured
Packit ae235b
                   (i.e. the yield of the pcre_exec call, unless
Packit ae235b
                   that was zero, in which case it should be 1/3
Packit ae235b
                   of the offset table size)
Packit ae235b
  stringnumber   the number of the required substring
Packit ae235b
  buffer         where to put the substring
Packit ae235b
  size           the size of the buffer
Packit ae235b
Packit ae235b
Returns:         if successful:
Packit ae235b
                   the length of the copied string, not including the zero
Packit ae235b
                   that is put on the end; can be zero
Packit ae235b
                 if not successful:
Packit ae235b
                   PCRE_ERROR_NOMEMORY (-6) buffer too small
Packit ae235b
                   PCRE_ERROR_NOSUBSTRING (-7) no such captured substring
Packit ae235b
*/
Packit ae235b
Packit ae235b
#ifdef COMPILE_PCRE8
Packit ae235b
PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
Packit ae235b
pcre_copy_substring(const char *subject, int *ovector, int stringcount,
Packit ae235b
  int stringnumber, char *buffer, int size)
Packit ae235b
#else
Packit ae235b
PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
Packit ae235b
pcre16_copy_substring(PCRE_SPTR16 subject, int *ovector, int stringcount,
Packit ae235b
  int stringnumber, PCRE_UCHAR16 *buffer, int size)
Packit ae235b
#endif
Packit ae235b
{
Packit ae235b
int yield;
Packit ae235b
if (stringnumber < 0 || stringnumber >= stringcount)
Packit ae235b
  return PCRE_ERROR_NOSUBSTRING;
Packit ae235b
stringnumber *= 2;
Packit ae235b
yield = ovector[stringnumber+1] - ovector[stringnumber];
Packit ae235b
if (size < yield + 1) return PCRE_ERROR_NOMEMORY;
Packit ae235b
memcpy(buffer, subject + ovector[stringnumber], IN_UCHARS(yield));
Packit ae235b
buffer[yield] = 0;
Packit ae235b
return yield;
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
/*************************************************
Packit ae235b
*   Copy named captured string to given buffer   *
Packit ae235b
*************************************************/
Packit ae235b
Packit ae235b
/* This function copies a single captured substring into a given buffer,
Packit ae235b
identifying it by name. If the regex permits duplicate names, the first
Packit ae235b
substring that is set is chosen.
Packit ae235b
Packit ae235b
Arguments:
Packit ae235b
  code           the compiled regex
Packit ae235b
  subject        the subject string that was matched
Packit ae235b
  ovector        pointer to the offsets table
Packit ae235b
  stringcount    the number of substrings that were captured
Packit ae235b
                   (i.e. the yield of the pcre_exec call, unless
Packit ae235b
                   that was zero, in which case it should be 1/3
Packit ae235b
                   of the offset table size)
Packit ae235b
  stringname     the name of the required substring
Packit ae235b
  buffer         where to put the substring
Packit ae235b
  size           the size of the buffer
Packit ae235b
Packit ae235b
Returns:         if successful:
Packit ae235b
                   the length of the copied string, not including the zero
Packit ae235b
                   that is put on the end; can be zero
Packit ae235b
                 if not successful:
Packit ae235b
                   PCRE_ERROR_NOMEMORY (-6) buffer too small
Packit ae235b
                   PCRE_ERROR_NOSUBSTRING (-7) no such captured substring
Packit ae235b
*/
Packit ae235b
Packit ae235b
#ifdef COMPILE_PCRE8
Packit ae235b
PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
Packit ae235b
pcre_copy_named_substring(const pcre *code, const char *subject,
Packit ae235b
  int *ovector, int stringcount, const char *stringname,
Packit ae235b
  char *buffer, int size)
Packit ae235b
#else
Packit ae235b
PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
Packit ae235b
pcre16_copy_named_substring(const pcre16 *code, PCRE_SPTR16 subject,
Packit ae235b
  int *ovector, int stringcount, PCRE_SPTR16 stringname,
Packit ae235b
  PCRE_UCHAR16 *buffer, int size)
Packit ae235b
#endif
Packit ae235b
{
Packit ae235b
int n = get_first_set(code, stringname, ovector);
Packit ae235b
if (n <= 0) return n;
Packit ae235b
#ifdef COMPILE_PCRE8
Packit ae235b
return pcre_copy_substring(subject, ovector, stringcount, n, buffer, size);
Packit ae235b
#else
Packit ae235b
return pcre16_copy_substring(subject, ovector, stringcount, n, buffer, size);
Packit ae235b
#endif
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
/*************************************************
Packit ae235b
*      Copy all captured strings to new store    *
Packit ae235b
*************************************************/
Packit ae235b
Packit ae235b
/* This function gets one chunk of store and builds a list of pointers and all
Packit ae235b
of the captured substrings in it. A NULL pointer is put on the end of the list.
Packit ae235b
Packit ae235b
Arguments:
Packit ae235b
  subject        the subject string that was matched
Packit ae235b
  ovector        pointer to the offsets table
Packit ae235b
  stringcount    the number of substrings that were captured
Packit ae235b
                   (i.e. the yield of the pcre_exec call, unless
Packit ae235b
                   that was zero, in which case it should be 1/3
Packit ae235b
                   of the offset table size)
Packit ae235b
  listptr        set to point to the list of pointers
Packit ae235b
Packit ae235b
Returns:         if successful: 0
Packit ae235b
                 if not successful:
Packit ae235b
                   PCRE_ERROR_NOMEMORY (-6) failed to get store
Packit ae235b
*/
Packit ae235b
Packit ae235b
#ifdef COMPILE_PCRE8
Packit ae235b
PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
Packit ae235b
pcre_get_substring_list(const char *subject, int *ovector, int stringcount,
Packit ae235b
  const char ***listptr)
Packit ae235b
#else
Packit ae235b
PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
Packit ae235b
pcre16_get_substring_list(PCRE_SPTR16 subject, int *ovector, int stringcount,
Packit ae235b
  PCRE_SPTR16 **listptr)
Packit ae235b
#endif
Packit ae235b
{
Packit ae235b
int i;
Packit ae235b
int size = sizeof(pcre_uchar *);
Packit ae235b
int double_count = stringcount * 2;
Packit ae235b
pcre_uchar **stringlist;
Packit ae235b
pcre_uchar *p;
Packit ae235b
Packit ae235b
for (i = 0; i < double_count; i += 2)
Packit ae235b
  size += sizeof(pcre_uchar *) + IN_UCHARS(ovector[i+1] - ovector[i] + 1);
Packit ae235b
Packit ae235b
stringlist = (pcre_uchar **)(PUBL(malloc))(size);
Packit ae235b
if (stringlist == NULL) return PCRE_ERROR_NOMEMORY;
Packit ae235b
Packit ae235b
#ifdef COMPILE_PCRE8
Packit ae235b
*listptr = (const char **)stringlist;
Packit ae235b
#else
Packit ae235b
*listptr = (PCRE_SPTR16 *)stringlist;
Packit ae235b
#endif
Packit ae235b
p = (pcre_uchar *)(stringlist + stringcount + 1);
Packit ae235b
Packit ae235b
for (i = 0; i < double_count; i += 2)
Packit ae235b
  {
Packit ae235b
  int len = ovector[i+1] - ovector[i];
Packit ae235b
  memcpy(p, subject + ovector[i], IN_UCHARS(len));
Packit ae235b
  *stringlist++ = p;
Packit ae235b
  p += len;
Packit ae235b
  *p++ = 0;
Packit ae235b
  }
Packit ae235b
Packit ae235b
*stringlist = NULL;
Packit ae235b
return 0;
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
/*************************************************
Packit ae235b
*   Free store obtained by get_substring_list    *
Packit ae235b
*************************************************/
Packit ae235b
Packit ae235b
/* This function exists for the benefit of people calling PCRE from non-C
Packit ae235b
programs that can call its functions, but not free() or (PUBL(free))()
Packit ae235b
directly.
Packit ae235b
Packit ae235b
Argument:   the result of a previous pcre_get_substring_list()
Packit ae235b
Returns:    nothing
Packit ae235b
*/
Packit ae235b
Packit ae235b
#ifdef COMPILE_PCRE8
Packit ae235b
PCRE_EXP_DEFN void PCRE_CALL_CONVENTION
Packit ae235b
pcre_free_substring_list(const char **pointer)
Packit ae235b
#else
Packit ae235b
PCRE_EXP_DEFN void PCRE_CALL_CONVENTION
Packit ae235b
pcre16_free_substring_list(PCRE_SPTR16 *pointer)
Packit ae235b
#endif
Packit ae235b
{
Packit ae235b
(PUBL(free))((void *)pointer);
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
/*************************************************
Packit ae235b
*      Copy captured string to new store         *
Packit ae235b
*************************************************/
Packit ae235b
Packit ae235b
/* This function copies a single captured substring into a piece of new
Packit ae235b
store
Packit ae235b
Packit ae235b
Arguments:
Packit ae235b
  subject        the subject string that was matched
Packit ae235b
  ovector        pointer to the offsets table
Packit ae235b
  stringcount    the number of substrings that were captured
Packit ae235b
                   (i.e. the yield of the pcre_exec call, unless
Packit ae235b
                   that was zero, in which case it should be 1/3
Packit ae235b
                   of the offset table size)
Packit ae235b
  stringnumber   the number of the required substring
Packit ae235b
  stringptr      where to put a pointer to the substring
Packit ae235b
Packit ae235b
Returns:         if successful:
Packit ae235b
                   the length of the string, not including the zero that
Packit ae235b
                   is put on the end; can be zero
Packit ae235b
                 if not successful:
Packit ae235b
                   PCRE_ERROR_NOMEMORY (-6) failed to get store
Packit ae235b
                   PCRE_ERROR_NOSUBSTRING (-7) substring not present
Packit ae235b
*/
Packit ae235b
Packit ae235b
#ifdef COMPILE_PCRE8
Packit ae235b
PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
Packit ae235b
pcre_get_substring(const char *subject, int *ovector, int stringcount,
Packit ae235b
  int stringnumber, const char **stringptr)
Packit ae235b
#else
Packit ae235b
PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
Packit ae235b
pcre16_get_substring(PCRE_SPTR16 subject, int *ovector, int stringcount,
Packit ae235b
  int stringnumber, PCRE_SPTR16 *stringptr)
Packit ae235b
#endif
Packit ae235b
{
Packit ae235b
int yield;
Packit ae235b
pcre_uchar *substring;
Packit ae235b
if (stringnumber < 0 || stringnumber >= stringcount)
Packit ae235b
  return PCRE_ERROR_NOSUBSTRING;
Packit ae235b
stringnumber *= 2;
Packit ae235b
yield = ovector[stringnumber+1] - ovector[stringnumber];
Packit ae235b
substring = (pcre_uchar *)(PUBL(malloc))(IN_UCHARS(yield + 1));
Packit ae235b
if (substring == NULL) return PCRE_ERROR_NOMEMORY;
Packit ae235b
memcpy(substring, subject + ovector[stringnumber], IN_UCHARS(yield));
Packit ae235b
substring[yield] = 0;
Packit ae235b
#ifdef COMPILE_PCRE8
Packit ae235b
*stringptr = (const char *)substring;
Packit ae235b
#else
Packit ae235b
*stringptr = (PCRE_SPTR16)substring;
Packit ae235b
#endif
Packit ae235b
return yield;
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
/*************************************************
Packit ae235b
*   Copy named captured string to new store      *
Packit ae235b
*************************************************/
Packit ae235b
Packit ae235b
/* This function copies a single captured substring, identified by name, into
Packit ae235b
new store. If the regex permits duplicate names, the first substring that is
Packit ae235b
set is chosen.
Packit ae235b
Packit ae235b
Arguments:
Packit ae235b
  code           the compiled regex
Packit ae235b
  subject        the subject string that was matched
Packit ae235b
  ovector        pointer to the offsets table
Packit ae235b
  stringcount    the number of substrings that were captured
Packit ae235b
                   (i.e. the yield of the pcre_exec call, unless
Packit ae235b
                   that was zero, in which case it should be 1/3
Packit ae235b
                   of the offset table size)
Packit ae235b
  stringname     the name of the required substring
Packit ae235b
  stringptr      where to put the pointer
Packit ae235b
Packit ae235b
Returns:         if successful:
Packit ae235b
                   the length of the copied string, not including the zero
Packit ae235b
                   that is put on the end; can be zero
Packit ae235b
                 if not successful:
Packit ae235b
                   PCRE_ERROR_NOMEMORY (-6) couldn't get memory
Packit ae235b
                   PCRE_ERROR_NOSUBSTRING (-7) no such captured substring
Packit ae235b
*/
Packit ae235b
Packit ae235b
#ifdef COMPILE_PCRE8
Packit ae235b
PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
Packit ae235b
pcre_get_named_substring(const pcre *code, const char *subject,
Packit ae235b
  int *ovector, int stringcount, const char *stringname,
Packit ae235b
  const char **stringptr)
Packit ae235b
#else
Packit ae235b
PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
Packit ae235b
pcre16_get_named_substring(const pcre16 *code, PCRE_SPTR16 subject,
Packit ae235b
  int *ovector, int stringcount, PCRE_SPTR16 stringname,
Packit ae235b
  PCRE_SPTR16 *stringptr)
Packit ae235b
#endif
Packit ae235b
{
Packit ae235b
int n = get_first_set(code, stringname, ovector);
Packit ae235b
if (n <= 0) return n;
Packit ae235b
#ifdef COMPILE_PCRE8
Packit ae235b
return pcre_get_substring(subject, ovector, stringcount, n, stringptr);
Packit ae235b
#else
Packit ae235b
return pcre16_get_substring(subject, ovector, stringcount, n, stringptr);
Packit ae235b
#endif
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
/*************************************************
Packit ae235b
*       Free store obtained by get_substring     *
Packit ae235b
*************************************************/
Packit ae235b
Packit ae235b
/* This function exists for the benefit of people calling PCRE from non-C
Packit ae235b
programs that can call its functions, but not free() or (PUBL(free))()
Packit ae235b
directly.
Packit ae235b
Packit ae235b
Argument:   the result of a previous pcre_get_substring()
Packit ae235b
Returns:    nothing
Packit ae235b
*/
Packit ae235b
Packit ae235b
#ifdef COMPILE_PCRE8
Packit ae235b
PCRE_EXP_DEFN void PCRE_CALL_CONVENTION
Packit ae235b
pcre_free_substring(const char *pointer)
Packit ae235b
#else
Packit ae235b
PCRE_EXP_DEFN void PCRE_CALL_CONVENTION
Packit ae235b
pcre16_free_substring(PCRE_SPTR16 pointer)
Packit ae235b
#endif
Packit ae235b
{
Packit ae235b
(PUBL(free))((void *)pointer);
Packit ae235b
}
Packit ae235b
Packit ae235b
/* End of pcre_get.c */