Diff for /embedaddon/pcre/pcre_get.c between versions 1.1.1.1 and 1.1.1.2

version 1.1.1.1, 2012/02/21 23:05:51 version 1.1.1.2, 2012/02/21 23:50:25
Line 6 Line 6
 and semantics are as close as possible to those of the Perl 5 language.  and semantics are as close as possible to those of the Perl 5 language.
   
                        Written by Philip Hazel                         Written by Philip Hazel
           Copyright (c) 1997-2008 University of Cambridge           Copyright (c) 1997-2012 University of Cambridge
   
 -----------------------------------------------------------------------------  -----------------------------------------------------------------------------
 Redistribution and use in source and binary forms, with or without  Redistribution and use in source and binary forms, with or without
Line 65  Returns:      the number of the named parentheses, or  Line 65  Returns:      the number of the named parentheses, or 
                 (PCRE_ERROR_NOSUBSTRING) if not found                  (PCRE_ERROR_NOSUBSTRING) if not found
 */  */
   
   #ifdef COMPILE_PCRE8
 PCRE_EXP_DEFN int PCRE_CALL_CONVENTION  PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
 pcre_get_stringnumber(const pcre *code, const char *stringname)  pcre_get_stringnumber(const pcre *code, const char *stringname)
   #else
   PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
   pcre16_get_stringnumber(const pcre16 *code, PCRE_SPTR16 stringname)
   #endif
 {  {
 int rc;  int rc;
 int entrysize;  int entrysize;
 int top, bot;  int top, bot;
uschar *nametable;pcre_uchar *nametable;
   
   #ifdef COMPILE_PCRE8
 if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMECOUNT, &top)) != 0)  if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMECOUNT, &top)) != 0)
   return rc;    return rc;
 if (top <= 0) return PCRE_ERROR_NOSUBSTRING;  if (top <= 0) return PCRE_ERROR_NOSUBSTRING;
Line 81  if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMEENTR Line 87  if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMEENTR
   return rc;    return rc;
 if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMETABLE, &nametable)) != 0)  if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMETABLE, &nametable)) != 0)
   return rc;    return rc;
   #endif
   #ifdef COMPILE_PCRE16
   if ((rc = pcre16_fullinfo(code, NULL, PCRE_INFO_NAMECOUNT, &top)) != 0)
     return rc;
   if (top <= 0) return PCRE_ERROR_NOSUBSTRING;
   
   if ((rc = pcre16_fullinfo(code, NULL, PCRE_INFO_NAMEENTRYSIZE, &entrysize)) != 0)
     return rc;
   if ((rc = pcre16_fullinfo(code, NULL, PCRE_INFO_NAMETABLE, &nametable)) != 0)
     return rc;
   #endif
   
 bot = 0;  bot = 0;
 while (top > bot)  while (top > bot)
   {    {
   int mid = (top + bot) / 2;    int mid = (top + bot) / 2;
  uschar *entry = nametable + entrysize*mid;  pcre_uchar *entry = nametable + entrysize*mid;
  int c = strcmp(stringname, (char *)(entry + 2));  int c = STRCMP_UC_UC((pcre_uchar *)stringname,
  if (c == 0) return (entry[0] << 8) + entry[1];    (pcre_uchar *)(entry + IMM2_SIZE));
   if (c == 0) return GET2(entry, 0);
   if (c > 0) bot = mid + 1; else top = mid;    if (c > 0) bot = mid + 1; else top = mid;
   }    }
   
Line 114  Returns:      the length of each entry, or a negative  Line 132  Returns:      the length of each entry, or a negative 
                 (PCRE_ERROR_NOSUBSTRING) if not found                  (PCRE_ERROR_NOSUBSTRING) if not found
 */  */
   
   #ifdef COMPILE_PCRE8
 PCRE_EXP_DEFN int PCRE_CALL_CONVENTION  PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
 pcre_get_stringtable_entries(const pcre *code, const char *stringname,  pcre_get_stringtable_entries(const pcre *code, const char *stringname,
   char **firstptr, char **lastptr)    char **firstptr, char **lastptr)
   #else
   PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
   pcre16_get_stringtable_entries(const pcre16 *code, PCRE_SPTR16 stringname,
     PCRE_UCHAR16 **firstptr, PCRE_UCHAR16 **lastptr)
   #endif
 {  {
 int rc;  int rc;
 int entrysize;  int entrysize;
 int top, bot;  int top, bot;
uschar *nametable, *lastentry;pcre_uchar *nametable, *lastentry;
   
   #ifdef COMPILE_PCRE8
 if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMECOUNT, &top)) != 0)  if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMECOUNT, &top)) != 0)
   return rc;    return rc;
 if (top <= 0) return PCRE_ERROR_NOSUBSTRING;  if (top <= 0) return PCRE_ERROR_NOSUBSTRING;
Line 131  if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMEENTR Line 156  if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMEENTR
   return rc;    return rc;
 if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMETABLE, &nametable)) != 0)  if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMETABLE, &nametable)) != 0)
   return rc;    return rc;
   #endif
   #ifdef COMPILE_PCRE16
   if ((rc = pcre16_fullinfo(code, NULL, PCRE_INFO_NAMECOUNT, &top)) != 0)
     return rc;
   if (top <= 0) return PCRE_ERROR_NOSUBSTRING;
   
   if ((rc = pcre16_fullinfo(code, NULL, PCRE_INFO_NAMEENTRYSIZE, &entrysize)) != 0)
     return rc;
   if ((rc = pcre16_fullinfo(code, NULL, PCRE_INFO_NAMETABLE, &nametable)) != 0)
     return rc;
   #endif
   
 lastentry = nametable + entrysize * (top - 1);  lastentry = nametable + entrysize * (top - 1);
 bot = 0;  bot = 0;
 while (top > bot)  while (top > bot)
   {    {
   int mid = (top + bot) / 2;    int mid = (top + bot) / 2;
  uschar *entry = nametable + entrysize*mid;  pcre_uchar *entry = nametable + entrysize*mid;
  int c = strcmp(stringname, (char *)(entry + 2));  int c = STRCMP_UC_UC((pcre_uchar *)stringname,
     (pcre_uchar *)(entry + IMM2_SIZE));
   if (c == 0)    if (c == 0)
     {      {
    uschar *first = entry;    pcre_uchar *first = entry;
    uschar *last = entry;    pcre_uchar *last = entry;
     while (first > nametable)      while (first > nametable)
       {        {
      if (strcmp(stringname, (char *)(first - entrysize + 2)) != 0) break;      if (STRCMP_UC_UC((pcre_uchar *)stringname,
         (pcre_uchar *)(first - entrysize + IMM2_SIZE)) != 0) break;
       first -= entrysize;        first -= entrysize;
       }        }
     while (last < lastentry)      while (last < lastentry)
       {        {
      if (strcmp(stringname, (char *)(last + entrysize + 2)) != 0) break;      if (STRCMP_UC_UC((pcre_uchar *)stringname,
         (pcre_uchar *)(last + entrysize + IMM2_SIZE)) != 0) break;
       last += entrysize;        last += entrysize;
       }        }
   #ifdef COMPILE_PCRE8
     *firstptr = (char *)first;      *firstptr = (char *)first;
     *lastptr = (char *)last;      *lastptr = (char *)last;
   #else
       *firstptr = (PCRE_UCHAR16 *)first;
       *lastptr = (PCRE_UCHAR16 *)last;
   #endif
     return entrysize;      return entrysize;
     }      }
   if (c > 0) bot = mid + 1; else top = mid;    if (c > 0) bot = mid + 1; else top = mid;
Line 182  Returns:       the number of the first that is set, Line 226  Returns:       the number of the first that is set,
                or a negative number on error                 or a negative number on error
 */  */
   
   #ifdef COMPILE_PCRE8
 static int  static int
 get_first_set(const pcre *code, const char *stringname, int *ovector)  get_first_set(const pcre *code, const char *stringname, int *ovector)
   #else
   static int
   get_first_set(const pcre16 *code, PCRE_SPTR16 stringname, int *ovector)
   #endif
 {  {
const real_pcre *re = (const real_pcre *)code;const REAL_PCRE *re = (const REAL_PCRE *)code;
 int entrysize;  int entrysize;
   pcre_uchar *entry;
   #ifdef COMPILE_PCRE8
 char *first, *last;  char *first, *last;
uschar *entry;#else
 PCRE_UCHAR16 *first, *last;
 #endif
 
 #ifdef COMPILE_PCRE8
 if ((re->options & PCRE_DUPNAMES) == 0 && (re->flags & PCRE_JCHANGED) == 0)  if ((re->options & PCRE_DUPNAMES) == 0 && (re->flags & PCRE_JCHANGED) == 0)
   return pcre_get_stringnumber(code, stringname);    return pcre_get_stringnumber(code, stringname);
 entrysize = pcre_get_stringtable_entries(code, stringname, &first, &last);  entrysize = pcre_get_stringtable_entries(code, stringname, &first, &last);
   #else
   if ((re->options & PCRE_DUPNAMES) == 0 && (re->flags & PCRE_JCHANGED) == 0)
     return pcre16_get_stringnumber(code, stringname);
   entrysize = pcre16_get_stringtable_entries(code, stringname, &first, &last);
   #endif
 if (entrysize <= 0) return entrysize;  if (entrysize <= 0) return entrysize;
for (entry = (uschar *)first; entry <= (uschar *)last; entry += entrysize)for (entry = (pcre_uchar *)first; entry <= (pcre_uchar *)last; entry += entrysize)
   {    {
  int n = (entry[0] << 8) + entry[1];  int n = GET2(entry, 0);
   if (ovector[n*2] >= 0) return n;    if (ovector[n*2] >= 0) return n;
   }    }
return (first[0] << 8) + first[1];return GET2(entry, 0);
 }  }
   
   
Line 231  Returns:         if successful: Line 291  Returns:         if successful:
                    PCRE_ERROR_NOSUBSTRING (-7) no such captured substring                     PCRE_ERROR_NOSUBSTRING (-7) no such captured substring
 */  */
   
   #ifdef COMPILE_PCRE8
 PCRE_EXP_DEFN int PCRE_CALL_CONVENTION  PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
 pcre_copy_substring(const char *subject, int *ovector, int stringcount,  pcre_copy_substring(const char *subject, int *ovector, int stringcount,
   int stringnumber, char *buffer, int size)    int stringnumber, char *buffer, int size)
   #else
   PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
   pcre16_copy_substring(PCRE_SPTR16 subject, int *ovector, int stringcount,
     int stringnumber, PCRE_UCHAR16 *buffer, int size)
   #endif
 {  {
 int yield;  int yield;
 if (stringnumber < 0 || stringnumber >= stringcount)  if (stringnumber < 0 || stringnumber >= stringcount)
Line 241  if (stringnumber < 0 || stringnumber >= stringcount) Line 307  if (stringnumber < 0 || stringnumber >= stringcount)
 stringnumber *= 2;  stringnumber *= 2;
 yield = ovector[stringnumber+1] - ovector[stringnumber];  yield = ovector[stringnumber+1] - ovector[stringnumber];
 if (size < yield + 1) return PCRE_ERROR_NOMEMORY;  if (size < yield + 1) return PCRE_ERROR_NOMEMORY;
memcpy(buffer, subject + ovector[stringnumber], yield);memcpy(buffer, subject + ovector[stringnumber], IN_UCHARS(yield));
 buffer[yield] = 0;  buffer[yield] = 0;
 return yield;  return yield;
 }  }
Line 276  Returns:         if successful: Line 342  Returns:         if successful:
                    PCRE_ERROR_NOSUBSTRING (-7) no such captured substring                     PCRE_ERROR_NOSUBSTRING (-7) no such captured substring
 */  */
   
   #ifdef COMPILE_PCRE8
 PCRE_EXP_DEFN int PCRE_CALL_CONVENTION  PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
pcre_copy_named_substring(const pcre *code, const char *subject, int *ovector,pcre_copy_named_substring(const pcre *code, const char *subject,
  int stringcount, const char *stringname, char *buffer, int size)  int *ovector, int stringcount, const char *stringname,
   char *buffer, int size)
 #else
 PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
 pcre16_copy_named_substring(const pcre16 *code, PCRE_SPTR16 subject,
   int *ovector, int stringcount, PCRE_SPTR16 stringname,
   PCRE_UCHAR16 *buffer, int size)
 #endif
 {  {
 int n = get_first_set(code, stringname, ovector);  int n = get_first_set(code, stringname, ovector);
 if (n <= 0) return n;  if (n <= 0) return n;
   #ifdef COMPILE_PCRE8
 return pcre_copy_substring(subject, ovector, stringcount, n, buffer, size);  return pcre_copy_substring(subject, ovector, stringcount, n, buffer, size);
   #else
   return pcre16_copy_substring(subject, ovector, stringcount, n, buffer, size);
   #endif
 }  }
   
   
Line 308  Returns:         if successful: 0 Line 386  Returns:         if successful: 0
                    PCRE_ERROR_NOMEMORY (-6) failed to get store                     PCRE_ERROR_NOMEMORY (-6) failed to get store
 */  */
   
   #ifdef COMPILE_PCRE8
 PCRE_EXP_DEFN int PCRE_CALL_CONVENTION  PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
 pcre_get_substring_list(const char *subject, int *ovector, int stringcount,  pcre_get_substring_list(const char *subject, int *ovector, int stringcount,
   const char ***listptr)    const char ***listptr)
   #else
   PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
   pcre16_get_substring_list(PCRE_SPTR16 subject, int *ovector, int stringcount,
     PCRE_SPTR16 **listptr)
   #endif
 {  {
 int i;  int i;
int size = sizeof(char *);int size = sizeof(pcre_uchar *);
 int double_count = stringcount * 2;  int double_count = stringcount * 2;
char **stringlist;pcre_uchar **stringlist;
char *p;pcre_uchar *p;
   
 for (i = 0; i < double_count; i += 2)  for (i = 0; i < double_count; i += 2)
  size += sizeof(char *) + ovector[i+1] - ovector[i] + 1;  size += sizeof(pcre_uchar *) + IN_UCHARS(ovector[i+1] - ovector[i] + 1);
   
stringlist = (char **)(pcre_malloc)(size);stringlist = (pcre_uchar **)(PUBL(malloc))(size);
 if (stringlist == NULL) return PCRE_ERROR_NOMEMORY;  if (stringlist == NULL) return PCRE_ERROR_NOMEMORY;
   
   #ifdef COMPILE_PCRE8
 *listptr = (const char **)stringlist;  *listptr = (const char **)stringlist;
p = (char *)(stringlist + stringcount + 1);#else
 *listptr = (PCRE_SPTR16 *)stringlist;
 #endif
 p = (pcre_uchar *)(stringlist + stringcount + 1);
   
 for (i = 0; i < double_count; i += 2)  for (i = 0; i < double_count; i += 2)
   {    {
   int len = ovector[i+1] - ovector[i];    int len = ovector[i+1] - ovector[i];
  memcpy(p, subject + ovector[i], len);  memcpy(p, subject + ovector[i], IN_UCHARS(len));
   *stringlist++ = p;    *stringlist++ = p;
   p += len;    p += len;
   *p++ = 0;    *p++ = 0;
Line 347  return 0; Line 435  return 0;
 *************************************************/  *************************************************/
   
 /* This function exists for the benefit of people calling PCRE from non-C  /* This function exists for the benefit of people calling PCRE from non-C
programs that can call its functions, but not free() or (pcre_free)() directly.programs that can call its functions, but not free() or (PUBL(free))()
 directly.
   
 Argument:   the result of a previous pcre_get_substring_list()  Argument:   the result of a previous pcre_get_substring_list()
 Returns:    nothing  Returns:    nothing
 */  */
   
   #ifdef COMPILE_PCRE8
 PCRE_EXP_DEFN void PCRE_CALL_CONVENTION  PCRE_EXP_DEFN void PCRE_CALL_CONVENTION
 pcre_free_substring_list(const char **pointer)  pcre_free_substring_list(const char **pointer)
   #else
   PCRE_EXP_DEFN void PCRE_CALL_CONVENTION
   pcre16_free_substring_list(PCRE_SPTR16 *pointer)
   #endif
 {  {
(pcre_free)((void *)pointer);(PUBL(free))((void *)pointer);
 }  }
   
   
Line 386  Returns:         if successful: Line 480  Returns:         if successful:
                    PCRE_ERROR_NOSUBSTRING (-7) substring not present                     PCRE_ERROR_NOSUBSTRING (-7) substring not present
 */  */
   
   #ifdef COMPILE_PCRE8
 PCRE_EXP_DEFN int PCRE_CALL_CONVENTION  PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
 pcre_get_substring(const char *subject, int *ovector, int stringcount,  pcre_get_substring(const char *subject, int *ovector, int stringcount,
   int stringnumber, const char **stringptr)    int stringnumber, const char **stringptr)
   #else
   PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
   pcre16_get_substring(PCRE_SPTR16 subject, int *ovector, int stringcount,
     int stringnumber, PCRE_SPTR16 *stringptr)
   #endif
 {  {
 int yield;  int yield;
char *substring;pcre_uchar *substring;
 if (stringnumber < 0 || stringnumber >= stringcount)  if (stringnumber < 0 || stringnumber >= stringcount)
   return PCRE_ERROR_NOSUBSTRING;    return PCRE_ERROR_NOSUBSTRING;
 stringnumber *= 2;  stringnumber *= 2;
 yield = ovector[stringnumber+1] - ovector[stringnumber];  yield = ovector[stringnumber+1] - ovector[stringnumber];
substring = (char *)(pcre_malloc)(yield + 1);substring = (pcre_uchar *)(PUBL(malloc))(IN_UCHARS(yield + 1));
 if (substring == NULL) return PCRE_ERROR_NOMEMORY;  if (substring == NULL) return PCRE_ERROR_NOMEMORY;
memcpy(substring, subject + ovector[stringnumber], yield);memcpy(substring, subject + ovector[stringnumber], IN_UCHARS(yield));
 substring[yield] = 0;  substring[yield] = 0;
*stringptr = substring;#ifdef COMPILE_PCRE8
 *stringptr = (const char *)substring;
 #else
 *stringptr = (PCRE_SPTR16)substring;
 #endif
 return yield;  return yield;
 }  }
   
Line 433  Returns:         if successful: Line 537  Returns:         if successful:
                    PCRE_ERROR_NOSUBSTRING (-7) no such captured substring                     PCRE_ERROR_NOSUBSTRING (-7) no such captured substring
 */  */
   
   #ifdef COMPILE_PCRE8
 PCRE_EXP_DEFN int PCRE_CALL_CONVENTION  PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
pcre_get_named_substring(const pcre *code, const char *subject, int *ovector,pcre_get_named_substring(const pcre *code, const char *subject,
  int stringcount, const char *stringname, const char **stringptr)  int *ovector, int stringcount, const char *stringname,
   const char **stringptr)
 #else
 PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
 pcre16_get_named_substring(const pcre16 *code, PCRE_SPTR16 subject,
   int *ovector, int stringcount, PCRE_SPTR16 stringname,
   PCRE_SPTR16 *stringptr)
 #endif
 {  {
 int n = get_first_set(code, stringname, ovector);  int n = get_first_set(code, stringname, ovector);
 if (n <= 0) return n;  if (n <= 0) return n;
   #ifdef COMPILE_PCRE8
 return pcre_get_substring(subject, ovector, stringcount, n, stringptr);  return pcre_get_substring(subject, ovector, stringcount, n, stringptr);
   #else
   return pcre16_get_substring(subject, ovector, stringcount, n, stringptr);
   #endif
 }  }
   
   
Line 450  return pcre_get_substring(subject, ovector, stringcoun Line 566  return pcre_get_substring(subject, ovector, stringcoun
 *************************************************/  *************************************************/
   
 /* This function exists for the benefit of people calling PCRE from non-C  /* This function exists for the benefit of people calling PCRE from non-C
programs that can call its functions, but not free() or (pcre_free)() directly.programs that can call its functions, but not free() or (PUBL(free))()
 directly.
   
 Argument:   the result of a previous pcre_get_substring()  Argument:   the result of a previous pcre_get_substring()
 Returns:    nothing  Returns:    nothing
 */  */
   
   #ifdef COMPILE_PCRE8
 PCRE_EXP_DEFN void PCRE_CALL_CONVENTION  PCRE_EXP_DEFN void PCRE_CALL_CONVENTION
 pcre_free_substring(const char *pointer)  pcre_free_substring(const char *pointer)
   #else
   PCRE_EXP_DEFN void PCRE_CALL_CONVENTION
   pcre16_free_substring(PCRE_SPTR16 pointer)
   #endif
 {  {
(pcre_free)((void *)pointer);(PUBL(free))((void *)pointer);
 }  }
   
 /* End of pcre_get.c */  /* End of pcre_get.c */

Removed from v.1.1.1.1  
changed lines
  Added in v.1.1.1.2


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>