root/ext/pcre/pcrelib/pcre_get.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. pcre_get_stringnumber
  2. pcre_get_stringtable_entries
  3. get_first_set
  4. pcre_copy_substring
  5. pcre_copy_named_substring
  6. pcre_get_substring_list
  7. pcre_free_substring_list
  8. pcre_get_substring
  9. pcre_get_named_substring
  10. pcre_free_substring

   1 /*************************************************
   2 *      Perl-Compatible Regular Expressions       *
   3 *************************************************/
   4 
   5 /* PCRE is a library of functions to support regular expressions whose syntax
   6 and semantics are as close as possible to those of the Perl 5 language.
   7 
   8                        Written by Philip Hazel
   9            Copyright (c) 1997-2012 University of Cambridge
  10 
  11 -----------------------------------------------------------------------------
  12 Redistribution and use in source and binary forms, with or without
  13 modification, are permitted provided that the following conditions are met:
  14 
  15     * Redistributions of source code must retain the above copyright notice,
  16       this list of conditions and the following disclaimer.
  17 
  18     * Redistributions in binary form must reproduce the above copyright
  19       notice, this list of conditions and the following disclaimer in the
  20       documentation and/or other materials provided with the distribution.
  21 
  22     * Neither the name of the University of Cambridge nor the names of its
  23       contributors may be used to endorse or promote products derived from
  24       this software without specific prior written permission.
  25 
  26 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  30 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  32 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  34 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36 POSSIBILITY OF SUCH DAMAGE.
  37 -----------------------------------------------------------------------------
  38 */
  39 
  40 
  41 /* This module contains some convenience functions for extracting substrings
  42 from the subject string after a regex match has succeeded. The original idea
  43 for these functions came from Scott Wimer. */
  44 
  45 
  46 #include "config.h"
  47 
  48 #include "pcre_internal.h"
  49 
  50 
  51 /*************************************************
  52 *           Find number for named string         *
  53 *************************************************/
  54 
  55 /* This function is used by the get_first_set() function below, as well
  56 as being generally available. It assumes that names are unique.
  57 
  58 Arguments:
  59   code        the compiled regex
  60   stringname  the name whose number is required
  61 
  62 Returns:      the number of the named parentheses, or a negative number
  63                 (PCRE_ERROR_NOSUBSTRING) if not found
  64 */
  65 
  66 #if defined COMPILE_PCRE8
  67 PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
  68 pcre_get_stringnumber(const pcre *code, const char *stringname)
  69 #elif defined COMPILE_PCRE16
  70 PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
  71 pcre16_get_stringnumber(const pcre16 *code, PCRE_SPTR16 stringname)
  72 #elif defined COMPILE_PCRE32
  73 PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
  74 pcre32_get_stringnumber(const pcre32 *code, PCRE_SPTR32 stringname)
  75 #endif
  76 {
  77 int rc;
  78 int entrysize;
  79 int top, bot;
  80 pcre_uchar *nametable;
  81 
  82 #ifdef COMPILE_PCRE8
  83 if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMECOUNT, &top)) != 0)
  84   return rc;
  85 if (top <= 0) return PCRE_ERROR_NOSUBSTRING;
  86 
  87 if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMEENTRYSIZE, &entrysize)) != 0)
  88   return rc;
  89 if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMETABLE, &nametable)) != 0)
  90   return rc;
  91 #endif
  92 #ifdef COMPILE_PCRE16
  93 if ((rc = pcre16_fullinfo(code, NULL, PCRE_INFO_NAMECOUNT, &top)) != 0)
  94   return rc;
  95 if (top <= 0) return PCRE_ERROR_NOSUBSTRING;
  96 
  97 if ((rc = pcre16_fullinfo(code, NULL, PCRE_INFO_NAMEENTRYSIZE, &entrysize)) != 0)
  98   return rc;
  99 if ((rc = pcre16_fullinfo(code, NULL, PCRE_INFO_NAMETABLE, &nametable)) != 0)
 100   return rc;
 101 #endif
 102 #ifdef COMPILE_PCRE32
 103 if ((rc = pcre32_fullinfo(code, NULL, PCRE_INFO_NAMECOUNT, &top)) != 0)
 104   return rc;
 105 if (top <= 0) return PCRE_ERROR_NOSUBSTRING;
 106 
 107 if ((rc = pcre32_fullinfo(code, NULL, PCRE_INFO_NAMEENTRYSIZE, &entrysize)) != 0)
 108   return rc;
 109 if ((rc = pcre32_fullinfo(code, NULL, PCRE_INFO_NAMETABLE, &nametable)) != 0)
 110   return rc;
 111 #endif
 112 
 113 bot = 0;
 114 while (top > bot)
 115   {
 116   int mid = (top + bot) / 2;
 117   pcre_uchar *entry = nametable + entrysize*mid;
 118   int c = STRCMP_UC_UC((pcre_uchar *)stringname,
 119     (pcre_uchar *)(entry + IMM2_SIZE));
 120   if (c == 0) return GET2(entry, 0);
 121   if (c > 0) bot = mid + 1; else top = mid;
 122   }
 123 
 124 return PCRE_ERROR_NOSUBSTRING;
 125 }
 126 
 127 
 128 
 129 /*************************************************
 130 *     Find (multiple) entries for named string   *
 131 *************************************************/
 132 
 133 /* This is used by the get_first_set() function below, as well as being
 134 generally available. It is used when duplicated names are permitted.
 135 
 136 Arguments:
 137   code        the compiled regex
 138   stringname  the name whose entries required
 139   firstptr    where to put the pointer to the first entry
 140   lastptr     where to put the pointer to the last entry
 141 
 142 Returns:      the length of each entry, or a negative number
 143                 (PCRE_ERROR_NOSUBSTRING) if not found
 144 */
 145 
 146 #if defined COMPILE_PCRE8
 147 PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
 148 pcre_get_stringtable_entries(const pcre *code, const char *stringname,
 149   char **firstptr, char **lastptr)
 150 #elif defined COMPILE_PCRE16
 151 PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
 152 pcre16_get_stringtable_entries(const pcre16 *code, PCRE_SPTR16 stringname,
 153   PCRE_UCHAR16 **firstptr, PCRE_UCHAR16 **lastptr)
 154 #elif defined COMPILE_PCRE32
 155 PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
 156 pcre32_get_stringtable_entries(const pcre32 *code, PCRE_SPTR32 stringname,
 157   PCRE_UCHAR32 **firstptr, PCRE_UCHAR32 **lastptr)
 158 #endif
 159 {
 160 int rc;
 161 int entrysize;
 162 int top, bot;
 163 pcre_uchar *nametable, *lastentry;
 164 
 165 #ifdef COMPILE_PCRE8
 166 if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMECOUNT, &top)) != 0)
 167   return rc;
 168 if (top <= 0) return PCRE_ERROR_NOSUBSTRING;
 169 
 170 if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMEENTRYSIZE, &entrysize)) != 0)
 171   return rc;
 172 if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMETABLE, &nametable)) != 0)
 173   return rc;
 174 #endif
 175 #ifdef COMPILE_PCRE16
 176 if ((rc = pcre16_fullinfo(code, NULL, PCRE_INFO_NAMECOUNT, &top)) != 0)
 177   return rc;
 178 if (top <= 0) return PCRE_ERROR_NOSUBSTRING;
 179 
 180 if ((rc = pcre16_fullinfo(code, NULL, PCRE_INFO_NAMEENTRYSIZE, &entrysize)) != 0)
 181   return rc;
 182 if ((rc = pcre16_fullinfo(code, NULL, PCRE_INFO_NAMETABLE, &nametable)) != 0)
 183   return rc;
 184 #endif
 185 #ifdef COMPILE_PCRE32
 186 if ((rc = pcre32_fullinfo(code, NULL, PCRE_INFO_NAMECOUNT, &top)) != 0)
 187   return rc;
 188 if (top <= 0) return PCRE_ERROR_NOSUBSTRING;
 189 
 190 if ((rc = pcre32_fullinfo(code, NULL, PCRE_INFO_NAMEENTRYSIZE, &entrysize)) != 0)
 191   return rc;
 192 if ((rc = pcre32_fullinfo(code, NULL, PCRE_INFO_NAMETABLE, &nametable)) != 0)
 193   return rc;
 194 #endif
 195 
 196 lastentry = nametable + entrysize * (top - 1);
 197 bot = 0;
 198 while (top > bot)
 199   {
 200   int mid = (top + bot) / 2;
 201   pcre_uchar *entry = nametable + entrysize*mid;
 202   int c = STRCMP_UC_UC((pcre_uchar *)stringname,
 203     (pcre_uchar *)(entry + IMM2_SIZE));
 204   if (c == 0)
 205     {
 206     pcre_uchar *first = entry;
 207     pcre_uchar *last = entry;
 208     while (first > nametable)
 209       {
 210       if (STRCMP_UC_UC((pcre_uchar *)stringname,
 211         (pcre_uchar *)(first - entrysize + IMM2_SIZE)) != 0) break;
 212       first -= entrysize;
 213       }
 214     while (last < lastentry)
 215       {
 216       if (STRCMP_UC_UC((pcre_uchar *)stringname,
 217         (pcre_uchar *)(last + entrysize + IMM2_SIZE)) != 0) break;
 218       last += entrysize;
 219       }
 220 #if defined COMPILE_PCRE8
 221     *firstptr = (char *)first;
 222     *lastptr = (char *)last;
 223 #elif defined COMPILE_PCRE16
 224     *firstptr = (PCRE_UCHAR16 *)first;
 225     *lastptr = (PCRE_UCHAR16 *)last;
 226 #elif defined COMPILE_PCRE32
 227     *firstptr = (PCRE_UCHAR32 *)first;
 228     *lastptr = (PCRE_UCHAR32 *)last;
 229 #endif
 230     return entrysize;
 231     }
 232   if (c > 0) bot = mid + 1; else top = mid;
 233   }
 234 
 235 return PCRE_ERROR_NOSUBSTRING;
 236 }
 237 
 238 
 239 
 240 /*************************************************
 241 *    Find first set of multiple named strings    *
 242 *************************************************/
 243 
 244 /* This function allows for duplicate names in the table of named substrings.
 245 It returns the number of the first one that was set in a pattern match.
 246 
 247 Arguments:
 248   code         the compiled regex
 249   stringname   the name of the capturing substring
 250   ovector      the vector of matched substrings
 251 
 252 Returns:       the number of the first that is set,
 253                or the number of the last one if none are set,
 254                or a negative number on error
 255 */
 256 
 257 #if defined COMPILE_PCRE8
 258 static int
 259 get_first_set(const pcre *code, const char *stringname, int *ovector)
 260 #elif defined COMPILE_PCRE16
 261 static int
 262 get_first_set(const pcre16 *code, PCRE_SPTR16 stringname, int *ovector)
 263 #elif defined COMPILE_PCRE32
 264 static int
 265 get_first_set(const pcre32 *code, PCRE_SPTR32 stringname, int *ovector)
 266 #endif
 267 {
 268 const REAL_PCRE *re = (const REAL_PCRE *)code;
 269 int entrysize;
 270 pcre_uchar *entry;
 271 #if defined COMPILE_PCRE8
 272 char *first, *last;
 273 #elif defined COMPILE_PCRE16
 274 PCRE_UCHAR16 *first, *last;
 275 #elif defined COMPILE_PCRE32
 276 PCRE_UCHAR32 *first, *last;
 277 #endif
 278 
 279 #if defined COMPILE_PCRE8
 280 if ((re->options & PCRE_DUPNAMES) == 0 && (re->flags & PCRE_JCHANGED) == 0)
 281   return pcre_get_stringnumber(code, stringname);
 282 entrysize = pcre_get_stringtable_entries(code, stringname, &first, &last);
 283 #elif defined COMPILE_PCRE16
 284 if ((re->options & PCRE_DUPNAMES) == 0 && (re->flags & PCRE_JCHANGED) == 0)
 285   return pcre16_get_stringnumber(code, stringname);
 286 entrysize = pcre16_get_stringtable_entries(code, stringname, &first, &last);
 287 #elif defined COMPILE_PCRE32
 288 if ((re->options & PCRE_DUPNAMES) == 0 && (re->flags & PCRE_JCHANGED) == 0)
 289   return pcre32_get_stringnumber(code, stringname);
 290 entrysize = pcre32_get_stringtable_entries(code, stringname, &first, &last);
 291 #endif
 292 if (entrysize <= 0) return entrysize;
 293 for (entry = (pcre_uchar *)first; entry <= (pcre_uchar *)last; entry += entrysize)
 294   {
 295   int n = GET2(entry, 0);
 296   if (ovector[n*2] >= 0) return n;
 297   }
 298 return GET2(entry, 0);
 299 }
 300 
 301 
 302 
 303 
 304 /*************************************************
 305 *      Copy captured string to given buffer      *
 306 *************************************************/
 307 
 308 /* This function copies a single captured substring into a given buffer.
 309 Note that we use memcpy() rather than strncpy() in case there are binary zeros
 310 in the string.
 311 
 312 Arguments:
 313   subject        the subject string that was matched
 314   ovector        pointer to the offsets table
 315   stringcount    the number of substrings that were captured
 316                    (i.e. the yield of the pcre_exec call, unless
 317                    that was zero, in which case it should be 1/3
 318                    of the offset table size)
 319   stringnumber   the number of the required substring
 320   buffer         where to put the substring
 321   size           the size of the buffer
 322 
 323 Returns:         if successful:
 324                    the length of the copied string, not including the zero
 325                    that is put on the end; can be zero
 326                  if not successful:
 327                    PCRE_ERROR_NOMEMORY (-6) buffer too small
 328                    PCRE_ERROR_NOSUBSTRING (-7) no such captured substring
 329 */
 330 
 331 #if defined COMPILE_PCRE8
 332 PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
 333 pcre_copy_substring(const char *subject, int *ovector, int stringcount,
 334   int stringnumber, char *buffer, int size)
 335 #elif defined COMPILE_PCRE16
 336 PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
 337 pcre16_copy_substring(PCRE_SPTR16 subject, int *ovector, int stringcount,
 338   int stringnumber, PCRE_UCHAR16 *buffer, int size)
 339 #elif defined COMPILE_PCRE32
 340 PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
 341 pcre32_copy_substring(PCRE_SPTR32 subject, int *ovector, int stringcount,
 342   int stringnumber, PCRE_UCHAR32 *buffer, int size)
 343 #endif
 344 {
 345 int yield;
 346 if (stringnumber < 0 || stringnumber >= stringcount)
 347   return PCRE_ERROR_NOSUBSTRING;
 348 stringnumber *= 2;
 349 yield = ovector[stringnumber+1] - ovector[stringnumber];
 350 if (size < yield + 1) return PCRE_ERROR_NOMEMORY;
 351 memcpy(buffer, subject + ovector[stringnumber], IN_UCHARS(yield));
 352 buffer[yield] = 0;
 353 return yield;
 354 }
 355 
 356 
 357 
 358 /*************************************************
 359 *   Copy named captured string to given buffer   *
 360 *************************************************/
 361 
 362 /* This function copies a single captured substring into a given buffer,
 363 identifying it by name. If the regex permits duplicate names, the first
 364 substring that is set is chosen.
 365 
 366 Arguments:
 367   code           the compiled regex
 368   subject        the subject string that was matched
 369   ovector        pointer to the offsets table
 370   stringcount    the number of substrings that were captured
 371                    (i.e. the yield of the pcre_exec call, unless
 372                    that was zero, in which case it should be 1/3
 373                    of the offset table size)
 374   stringname     the name of the required substring
 375   buffer         where to put the substring
 376   size           the size of the buffer
 377 
 378 Returns:         if successful:
 379                    the length of the copied string, not including the zero
 380                    that is put on the end; can be zero
 381                  if not successful:
 382                    PCRE_ERROR_NOMEMORY (-6) buffer too small
 383                    PCRE_ERROR_NOSUBSTRING (-7) no such captured substring
 384 */
 385 
 386 #if defined COMPILE_PCRE8
 387 PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
 388 pcre_copy_named_substring(const pcre *code, const char *subject,
 389   int *ovector, int stringcount, const char *stringname,
 390   char *buffer, int size)
 391 #elif defined COMPILE_PCRE16
 392 PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
 393 pcre16_copy_named_substring(const pcre16 *code, PCRE_SPTR16 subject,
 394   int *ovector, int stringcount, PCRE_SPTR16 stringname,
 395   PCRE_UCHAR16 *buffer, int size)
 396 #elif defined COMPILE_PCRE32
 397 PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
 398 pcre32_copy_named_substring(const pcre32 *code, PCRE_SPTR32 subject,
 399   int *ovector, int stringcount, PCRE_SPTR32 stringname,
 400   PCRE_UCHAR32 *buffer, int size)
 401 #endif
 402 {
 403 int n = get_first_set(code, stringname, ovector);
 404 if (n <= 0) return n;
 405 #if defined COMPILE_PCRE8
 406 return pcre_copy_substring(subject, ovector, stringcount, n, buffer, size);
 407 #elif defined COMPILE_PCRE16
 408 return pcre16_copy_substring(subject, ovector, stringcount, n, buffer, size);
 409 #elif defined COMPILE_PCRE32
 410 return pcre32_copy_substring(subject, ovector, stringcount, n, buffer, size);
 411 #endif
 412 }
 413 
 414 
 415 
 416 /*************************************************
 417 *      Copy all captured strings to new store    *
 418 *************************************************/
 419 
 420 /* This function gets one chunk of store and builds a list of pointers and all
 421 of the captured substrings in it. A NULL pointer is put on the end of the list.
 422 
 423 Arguments:
 424   subject        the subject string that was matched
 425   ovector        pointer to the offsets table
 426   stringcount    the number of substrings that were captured
 427                    (i.e. the yield of the pcre_exec call, unless
 428                    that was zero, in which case it should be 1/3
 429                    of the offset table size)
 430   listptr        set to point to the list of pointers
 431 
 432 Returns:         if successful: 0
 433                  if not successful:
 434                    PCRE_ERROR_NOMEMORY (-6) failed to get store
 435 */
 436 
 437 #if defined COMPILE_PCRE8
 438 PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
 439 pcre_get_substring_list(const char *subject, int *ovector, int stringcount,
 440   const char ***listptr)
 441 #elif defined COMPILE_PCRE16
 442 PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
 443 pcre16_get_substring_list(PCRE_SPTR16 subject, int *ovector, int stringcount,
 444   PCRE_SPTR16 **listptr)
 445 #elif defined COMPILE_PCRE32
 446 PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
 447 pcre32_get_substring_list(PCRE_SPTR32 subject, int *ovector, int stringcount,
 448   PCRE_SPTR32 **listptr)
 449 #endif
 450 {
 451 int i;
 452 int size = sizeof(pcre_uchar *);
 453 int double_count = stringcount * 2;
 454 pcre_uchar **stringlist;
 455 pcre_uchar *p;
 456 
 457 for (i = 0; i < double_count; i += 2)
 458   size += sizeof(pcre_uchar *) + IN_UCHARS(ovector[i+1] - ovector[i] + 1);
 459 
 460 stringlist = (pcre_uchar **)(PUBL(malloc))(size);
 461 if (stringlist == NULL) return PCRE_ERROR_NOMEMORY;
 462 
 463 #if defined COMPILE_PCRE8
 464 *listptr = (const char **)stringlist;
 465 #elif defined COMPILE_PCRE16
 466 *listptr = (PCRE_SPTR16 *)stringlist;
 467 #elif defined COMPILE_PCRE32
 468 *listptr = (PCRE_SPTR32 *)stringlist;
 469 #endif
 470 p = (pcre_uchar *)(stringlist + stringcount + 1);
 471 
 472 for (i = 0; i < double_count; i += 2)
 473   {
 474   int len = ovector[i+1] - ovector[i];
 475   memcpy(p, subject + ovector[i], IN_UCHARS(len));
 476   *stringlist++ = p;
 477   p += len;
 478   *p++ = 0;
 479   }
 480 
 481 *stringlist = NULL;
 482 return 0;
 483 }
 484 
 485 
 486 
 487 /*************************************************
 488 *   Free store obtained by get_substring_list    *
 489 *************************************************/
 490 
 491 /* This function exists for the benefit of people calling PCRE from non-C
 492 programs that can call its functions, but not free() or (PUBL(free))()
 493 directly.
 494 
 495 Argument:   the result of a previous pcre_get_substring_list()
 496 Returns:    nothing
 497 */
 498 
 499 #if defined COMPILE_PCRE8
 500 PCRE_EXP_DEFN void PCRE_CALL_CONVENTION
 501 pcre_free_substring_list(const char **pointer)
 502 #elif defined COMPILE_PCRE16
 503 PCRE_EXP_DEFN void PCRE_CALL_CONVENTION
 504 pcre16_free_substring_list(PCRE_SPTR16 *pointer)
 505 #elif defined COMPILE_PCRE32
 506 PCRE_EXP_DEFN void PCRE_CALL_CONVENTION
 507 pcre32_free_substring_list(PCRE_SPTR32 *pointer)
 508 #endif
 509 {
 510 (PUBL(free))((void *)pointer);
 511 }
 512 
 513 
 514 
 515 /*************************************************
 516 *      Copy captured string to new store         *
 517 *************************************************/
 518 
 519 /* This function copies a single captured substring into a piece of new
 520 store
 521 
 522 Arguments:
 523   subject        the subject string that was matched
 524   ovector        pointer to the offsets table
 525   stringcount    the number of substrings that were captured
 526                    (i.e. the yield of the pcre_exec call, unless
 527                    that was zero, in which case it should be 1/3
 528                    of the offset table size)
 529   stringnumber   the number of the required substring
 530   stringptr      where to put a pointer to the substring
 531 
 532 Returns:         if successful:
 533                    the length of the string, not including the zero that
 534                    is put on the end; can be zero
 535                  if not successful:
 536                    PCRE_ERROR_NOMEMORY (-6) failed to get store
 537                    PCRE_ERROR_NOSUBSTRING (-7) substring not present
 538 */
 539 
 540 #if defined COMPILE_PCRE8
 541 PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
 542 pcre_get_substring(const char *subject, int *ovector, int stringcount,
 543   int stringnumber, const char **stringptr)
 544 #elif defined COMPILE_PCRE16
 545 PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
 546 pcre16_get_substring(PCRE_SPTR16 subject, int *ovector, int stringcount,
 547   int stringnumber, PCRE_SPTR16 *stringptr)
 548 #elif defined COMPILE_PCRE32
 549 PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
 550 pcre32_get_substring(PCRE_SPTR32 subject, int *ovector, int stringcount,
 551   int stringnumber, PCRE_SPTR32 *stringptr)
 552 #endif
 553 {
 554 int yield;
 555 pcre_uchar *substring;
 556 if (stringnumber < 0 || stringnumber >= stringcount)
 557   return PCRE_ERROR_NOSUBSTRING;
 558 stringnumber *= 2;
 559 yield = ovector[stringnumber+1] - ovector[stringnumber];
 560 substring = (pcre_uchar *)(PUBL(malloc))(IN_UCHARS(yield + 1));
 561 if (substring == NULL) return PCRE_ERROR_NOMEMORY;
 562 memcpy(substring, subject + ovector[stringnumber], IN_UCHARS(yield));
 563 substring[yield] = 0;
 564 #if defined COMPILE_PCRE8
 565 *stringptr = (const char *)substring;
 566 #elif defined COMPILE_PCRE16
 567 *stringptr = (PCRE_SPTR16)substring;
 568 #elif defined COMPILE_PCRE32
 569 *stringptr = (PCRE_SPTR32)substring;
 570 #endif
 571 return yield;
 572 }
 573 
 574 
 575 
 576 /*************************************************
 577 *   Copy named captured string to new store      *
 578 *************************************************/
 579 
 580 /* This function copies a single captured substring, identified by name, into
 581 new store. If the regex permits duplicate names, the first substring that is
 582 set is chosen.
 583 
 584 Arguments:
 585   code           the compiled regex
 586   subject        the subject string that was matched
 587   ovector        pointer to the offsets table
 588   stringcount    the number of substrings that were captured
 589                    (i.e. the yield of the pcre_exec call, unless
 590                    that was zero, in which case it should be 1/3
 591                    of the offset table size)
 592   stringname     the name of the required substring
 593   stringptr      where to put the pointer
 594 
 595 Returns:         if successful:
 596                    the length of the copied string, not including the zero
 597                    that is put on the end; can be zero
 598                  if not successful:
 599                    PCRE_ERROR_NOMEMORY (-6) couldn't get memory
 600                    PCRE_ERROR_NOSUBSTRING (-7) no such captured substring
 601 */
 602 
 603 #if defined COMPILE_PCRE8
 604 PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
 605 pcre_get_named_substring(const pcre *code, const char *subject,
 606   int *ovector, int stringcount, const char *stringname,
 607   const char **stringptr)
 608 #elif defined COMPILE_PCRE16
 609 PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
 610 pcre16_get_named_substring(const pcre16 *code, PCRE_SPTR16 subject,
 611   int *ovector, int stringcount, PCRE_SPTR16 stringname,
 612   PCRE_SPTR16 *stringptr)
 613 #elif defined COMPILE_PCRE32
 614 PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
 615 pcre32_get_named_substring(const pcre32 *code, PCRE_SPTR32 subject,
 616   int *ovector, int stringcount, PCRE_SPTR32 stringname,
 617   PCRE_SPTR32 *stringptr)
 618 #endif
 619 {
 620 int n = get_first_set(code, stringname, ovector);
 621 if (n <= 0) return n;
 622 #if defined COMPILE_PCRE8
 623 return pcre_get_substring(subject, ovector, stringcount, n, stringptr);
 624 #elif defined COMPILE_PCRE16
 625 return pcre16_get_substring(subject, ovector, stringcount, n, stringptr);
 626 #elif defined COMPILE_PCRE32
 627 return pcre32_get_substring(subject, ovector, stringcount, n, stringptr);
 628 #endif
 629 }
 630 
 631 
 632 
 633 
 634 /*************************************************
 635 *       Free store obtained by get_substring     *
 636 *************************************************/
 637 
 638 /* This function exists for the benefit of people calling PCRE from non-C
 639 programs that can call its functions, but not free() or (PUBL(free))()
 640 directly.
 641 
 642 Argument:   the result of a previous pcre_get_substring()
 643 Returns:    nothing
 644 */
 645 
 646 #if defined COMPILE_PCRE8
 647 PCRE_EXP_DEFN void PCRE_CALL_CONVENTION
 648 pcre_free_substring(const char *pointer)
 649 #elif defined COMPILE_PCRE16
 650 PCRE_EXP_DEFN void PCRE_CALL_CONVENTION
 651 pcre16_free_substring(PCRE_SPTR16 pointer)
 652 #elif defined COMPILE_PCRE32
 653 PCRE_EXP_DEFN void PCRE_CALL_CONVENTION
 654 pcre32_free_substring(PCRE_SPTR32 pointer)
 655 #endif
 656 {
 657 (PUBL(free))((void *)pointer);
 658 }
 659 
 660 /* End of pcre_get.c */

/* [<][>][^][v][top][bottom][index][help] */