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