1 /* $Cambridge: exim/src/src/pcre/pcre_get.c,v 1.4 2007/01/23 15:08:45 ph10 Exp $ */
3 /*************************************************
4 * Perl-Compatible Regular Expressions *
5 *************************************************/
7 /* PCRE is a library of functions to support regular expressions whose syntax
8 and semantics are as close as possible to those of the Perl 5 language.
10 Written by Philip Hazel
11 Copyright (c) 1997-2006 University of Cambridge
13 -----------------------------------------------------------------------------
14 Redistribution and use in source and binary forms, with or without
15 modification, are permitted provided that the following conditions are met:
17 * Redistributions of source code must retain the above copyright notice,
18 this list of conditions and the following disclaimer.
20 * Redistributions in binary form must reproduce the above copyright
21 notice, this list of conditions and the following disclaimer in the
22 documentation and/or other materials provided with the distribution.
24 * Neither the name of the University of Cambridge nor the names of its
25 contributors may be used to endorse or promote products derived from
26 this software without specific prior written permission.
28 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
29 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
32 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 POSSIBILITY OF SUCH DAMAGE.
39 -----------------------------------------------------------------------------
43 /* This module contains some convenience functions for extracting substrings
44 from the subject string after a regex match has succeeded. The original idea
45 for these functions came from Scott Wimer. */
48 #include "pcre_internal.h"
51 /*************************************************
52 * Find number for named string *
53 *************************************************/
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.
59 code the compiled regex
60 stringname the name whose number is required
62 Returns: the number of the named parentheses, or a negative number
63 (PCRE_ERROR_NOSUBSTRING) if not found
67 pcre_get_stringnumber(const pcre *code, const char *stringname)
74 if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMECOUNT, &top)) != 0)
76 if (top <= 0) return PCRE_ERROR_NOSUBSTRING;
78 if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMEENTRYSIZE, &entrysize)) != 0)
80 if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMETABLE, &nametable)) != 0)
86 int mid = (top + bot) / 2;
87 uschar *entry = nametable + entrysize*mid;
88 int c = strcmp(stringname, (char *)(entry + 2));
89 if (c == 0) return (entry[0] << 8) + entry[1];
90 if (c > 0) bot = mid + 1; else top = mid;
93 return PCRE_ERROR_NOSUBSTRING;
98 /*************************************************
99 * Find (multiple) entries for named string *
100 *************************************************/
102 /* This is used by the get_first_set() function below, as well as being
103 generally available. It is used when duplicated names are permitted.
106 code the compiled regex
107 stringname the name whose entries required
108 firstptr where to put the pointer to the first entry
109 lastptr where to put the pointer to the last entry
111 Returns: the length of each entry, or a negative number
112 (PCRE_ERROR_NOSUBSTRING) if not found
116 pcre_get_stringtable_entries(const pcre *code, const char *stringname,
117 char **firstptr, char **lastptr)
122 uschar *nametable, *lastentry;
124 if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMECOUNT, &top)) != 0)
126 if (top <= 0) return PCRE_ERROR_NOSUBSTRING;
128 if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMEENTRYSIZE, &entrysize)) != 0)
130 if ((rc = pcre_fullinfo(code, NULL, PCRE_INFO_NAMETABLE, &nametable)) != 0)
133 lastentry = nametable + entrysize * (top - 1);
137 int mid = (top + bot) / 2;
138 uschar *entry = nametable + entrysize*mid;
139 int c = strcmp(stringname, (char *)(entry + 2));
142 uschar *first = entry;
143 uschar *last = entry;
144 while (first > nametable)
146 if (strcmp(stringname, (char *)(first - entrysize + 2)) != 0) break;
149 while (last < lastentry)
151 if (strcmp(stringname, (char *)(last + entrysize + 2)) != 0) break;
154 *firstptr = (char *)first;
155 *lastptr = (char *)last;
158 if (c > 0) bot = mid + 1; else top = mid;
161 return PCRE_ERROR_NOSUBSTRING;
166 /*************************************************
167 * Find first set of multiple named strings *
168 *************************************************/
170 /* This function allows for duplicate names in the table of named substrings.
171 It returns the number of the first one that was set in a pattern match.
174 code the compiled regex
175 stringname the name of the capturing substring
176 ovector the vector of matched substrings
178 Returns: the number of the first that is set,
179 or the number of the last one if none are set,
180 or a negative number on error
184 get_first_set(const pcre *code, const char *stringname, int *ovector)
186 const real_pcre *re = (const real_pcre *)code;
190 if ((re->options & (PCRE_DUPNAMES | PCRE_JCHANGED)) == 0)
191 return pcre_get_stringnumber(code, stringname);
192 entrysize = pcre_get_stringtable_entries(code, stringname, &first, &last);
193 if (entrysize <= 0) return entrysize;
194 for (entry = (uschar *)first; entry <= (uschar *)last; entry += entrysize)
196 int n = (entry[0] << 8) + entry[1];
197 if (ovector[n*2] >= 0) return n;
199 return (first[0] << 8) + first[1];
205 /*************************************************
206 * Copy captured string to given buffer *
207 *************************************************/
209 /* This function copies a single captured substring into a given buffer.
210 Note that we use memcpy() rather than strncpy() in case there are binary zeros
214 subject the subject string that was matched
215 ovector pointer to the offsets table
216 stringcount the number of substrings that were captured
217 (i.e. the yield of the pcre_exec call, unless
218 that was zero, in which case it should be 1/3
219 of the offset table size)
220 stringnumber the number of the required substring
221 buffer where to put the substring
222 size the size of the buffer
224 Returns: if successful:
225 the length of the copied string, not including the zero
226 that is put on the end; can be zero
228 PCRE_ERROR_NOMEMORY (-6) buffer too small
229 PCRE_ERROR_NOSUBSTRING (-7) no such captured substring
233 pcre_copy_substring(const char *subject, int *ovector, int stringcount,
234 int stringnumber, char *buffer, int size)
237 if (stringnumber < 0 || stringnumber >= stringcount)
238 return PCRE_ERROR_NOSUBSTRING;
240 yield = ovector[stringnumber+1] - ovector[stringnumber];
241 if (size < yield + 1) return PCRE_ERROR_NOMEMORY;
242 memcpy(buffer, subject + ovector[stringnumber], yield);
249 /*************************************************
250 * Copy named captured string to given buffer *
251 *************************************************/
253 /* This function copies a single captured substring into a given buffer,
254 identifying it by name. If the regex permits duplicate names, the first
255 substring that is set is chosen.
258 code the compiled regex
259 subject the subject string that was matched
260 ovector pointer to the offsets table
261 stringcount the number of substrings that were captured
262 (i.e. the yield of the pcre_exec call, unless
263 that was zero, in which case it should be 1/3
264 of the offset table size)
265 stringname the name of the required substring
266 buffer where to put the substring
267 size the size of the buffer
269 Returns: if successful:
270 the length of the copied string, not including the zero
271 that is put on the end; can be zero
273 PCRE_ERROR_NOMEMORY (-6) buffer too small
274 PCRE_ERROR_NOSUBSTRING (-7) no such captured substring
278 pcre_copy_named_substring(const pcre *code, const char *subject, int *ovector,
279 int stringcount, const char *stringname, char *buffer, int size)
281 int n = get_first_set(code, stringname, ovector);
282 if (n <= 0) return n;
283 return pcre_copy_substring(subject, ovector, stringcount, n, buffer, size);
288 /*************************************************
289 * Copy all captured strings to new store *
290 *************************************************/
292 /* This function gets one chunk of store and builds a list of pointers and all
293 of the captured substrings in it. A NULL pointer is put on the end of the list.
296 subject the subject string that was matched
297 ovector pointer to the offsets table
298 stringcount the number of substrings that were captured
299 (i.e. the yield of the pcre_exec call, unless
300 that was zero, in which case it should be 1/3
301 of the offset table size)
302 listptr set to point to the list of pointers
304 Returns: if successful: 0
306 PCRE_ERROR_NOMEMORY (-6) failed to get store
310 pcre_get_substring_list(const char *subject, int *ovector, int stringcount,
311 const char ***listptr)
314 int size = sizeof(char *);
315 int double_count = stringcount * 2;
319 for (i = 0; i < double_count; i += 2)
320 size += sizeof(char *) + ovector[i+1] - ovector[i] + 1;
322 stringlist = (char **)(pcre_malloc)(size);
323 if (stringlist == NULL) return PCRE_ERROR_NOMEMORY;
325 *listptr = (const char **)stringlist;
326 p = (char *)(stringlist + stringcount + 1);
328 for (i = 0; i < double_count; i += 2)
330 int len = ovector[i+1] - ovector[i];
331 memcpy(p, subject + ovector[i], len);
343 /*************************************************
344 * Free store obtained by get_substring_list *
345 *************************************************/
347 /* This function exists for the benefit of people calling PCRE from non-C
348 programs that can call its functions, but not free() or (pcre_free)() directly.
350 Argument: the result of a previous pcre_get_substring_list()
355 pcre_free_substring_list(const char **pointer)
357 (pcre_free)((void *)pointer);
362 /*************************************************
363 * Copy captured string to new store *
364 *************************************************/
366 /* This function copies a single captured substring into a piece of new
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 stringnumber the number of the required substring
377 stringptr where to put a pointer to the substring
379 Returns: if successful:
380 the length of the string, not including the zero that
381 is put on the end; can be zero
383 PCRE_ERROR_NOMEMORY (-6) failed to get store
384 PCRE_ERROR_NOSUBSTRING (-7) substring not present
388 pcre_get_substring(const char *subject, int *ovector, int stringcount,
389 int stringnumber, const char **stringptr)
393 if (stringnumber < 0 || stringnumber >= stringcount)
394 return PCRE_ERROR_NOSUBSTRING;
396 yield = ovector[stringnumber+1] - ovector[stringnumber];
397 substring = (char *)(pcre_malloc)(yield + 1);
398 if (substring == NULL) return PCRE_ERROR_NOMEMORY;
399 memcpy(substring, subject + ovector[stringnumber], yield);
400 substring[yield] = 0;
401 *stringptr = substring;
407 /*************************************************
408 * Copy named captured string to new store *
409 *************************************************/
411 /* This function copies a single captured substring, identified by name, into
412 new store. If the regex permits duplicate names, the first substring that is
416 code the compiled regex
417 subject the subject string that was matched
418 ovector pointer to the offsets table
419 stringcount the number of substrings that were captured
420 (i.e. the yield of the pcre_exec call, unless
421 that was zero, in which case it should be 1/3
422 of the offset table size)
423 stringname the name of the required substring
424 stringptr where to put the pointer
426 Returns: if successful:
427 the length of the copied string, not including the zero
428 that is put on the end; can be zero
430 PCRE_ERROR_NOMEMORY (-6) couldn't get memory
431 PCRE_ERROR_NOSUBSTRING (-7) no such captured substring
435 pcre_get_named_substring(const pcre *code, const char *subject, int *ovector,
436 int stringcount, const char *stringname, const char **stringptr)
438 int n = get_first_set(code, stringname, ovector);
439 if (n <= 0) return n;
440 return pcre_get_substring(subject, ovector, stringcount, n, stringptr);
446 /*************************************************
447 * Free store obtained by get_substring *
448 *************************************************/
450 /* This function exists for the benefit of people calling PCRE from non-C
451 programs that can call its functions, but not free() or (pcre_free)() directly.
453 Argument: the result of a previous pcre_get_substring()
458 pcre_free_substring(const char *pointer)
460 (pcre_free)((void *)pointer);
463 /* End of pcre_get.c */