This source file includes following definitions.
- pcre_get_stringnumber
- pcre_get_stringtable_entries
- get_first_set
- pcre_copy_substring
- pcre_copy_named_substring
- pcre_get_substring_list
- pcre_free_substring_list
- pcre_get_substring
- pcre_get_named_substring
- pcre_free_substring
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 #include "config.h"
47
48 #include "pcre_internal.h"
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
131
132
133
134
135
136
137
138
139
140
141
142
143
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
242
243
244
245
246
247
248
249
250
251
252
253
254
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
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
489
490
491
492
493
494
495
496
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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
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
636
637
638
639
640
641
642
643
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