/[pcre]/code/trunk/pcre_get.c
ViewVC logotype

Contents of /code/trunk/pcre_get.c

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

Name Value
svn:eol-style native
svn:keywords "Author Date Id Revision Url"

  ViewVC Help
Powered by ViewVC 1.1.5