/src/pcre2/src/pcre2_match_data.c
Line | Count | Source (jump to first uncovered line) |
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 | | Original API code Copyright (c) 1997-2012 University of Cambridge |
10 | | New API code Copyright (c) 2016-2024 University of Cambridge |
11 | | |
12 | | ----------------------------------------------------------------------------- |
13 | | Redistribution and use in source and binary forms, with or without |
14 | | modification, are permitted provided that the following conditions are met: |
15 | | |
16 | | * Redistributions of source code must retain the above copyright notice, |
17 | | this list of conditions and the following disclaimer. |
18 | | |
19 | | * Redistributions in binary form must reproduce the above copyright |
20 | | notice, this list of conditions and the following disclaimer in the |
21 | | documentation and/or other materials provided with the distribution. |
22 | | |
23 | | * Neither the name of the University of Cambridge nor the names of its |
24 | | contributors may be used to endorse or promote products derived from |
25 | | this software without specific prior written permission. |
26 | | |
27 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
28 | | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
29 | | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
30 | | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
31 | | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
32 | | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
33 | | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
34 | | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
35 | | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
36 | | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
37 | | POSSIBILITY OF SUCH DAMAGE. |
38 | | ----------------------------------------------------------------------------- |
39 | | */ |
40 | | |
41 | | |
42 | | #include "pcre2_internal.h" |
43 | | |
44 | | |
45 | | |
46 | | /************************************************* |
47 | | * Create a match data block given ovector size * |
48 | | *************************************************/ |
49 | | |
50 | | /* A minimum of 1 is imposed on the number of ovector pairs. A maximum is also |
51 | | imposed because the oveccount field in a match data block is uintt6_t. */ |
52 | | |
53 | | PCRE2_EXP_DEFN pcre2_match_data * PCRE2_CALL_CONVENTION |
54 | | pcre2_match_data_create(uint32_t oveccount, pcre2_general_context *gcontext) |
55 | 81.1k | { |
56 | 81.1k | pcre2_match_data *yield; |
57 | 81.1k | if (oveccount < 1) oveccount = 1; |
58 | 81.1k | if (oveccount > UINT16_MAX) oveccount = UINT16_MAX; |
59 | 81.1k | yield = PRIV(memctl_malloc)( |
60 | 81.1k | offsetof(pcre2_match_data, ovector) + 2*oveccount*sizeof(PCRE2_SIZE), |
61 | 81.1k | (pcre2_memctl *)gcontext); |
62 | 81.1k | if (yield == NULL) return NULL; |
63 | 81.1k | yield->oveccount = oveccount; |
64 | 81.1k | yield->flags = 0; |
65 | 81.1k | yield->heapframes = NULL; |
66 | 81.1k | yield->heapframes_size = 0; |
67 | 81.1k | return yield; |
68 | 81.1k | } |
69 | | |
70 | | |
71 | | |
72 | | /************************************************* |
73 | | * Create a match data block using pattern data * |
74 | | *************************************************/ |
75 | | |
76 | | /* If no context is supplied, use the memory allocator from the code. This code |
77 | | assumes that a general context contains nothing other than a memory allocator. |
78 | | If that ever changes, this code will need fixing. */ |
79 | | |
80 | | PCRE2_EXP_DEFN pcre2_match_data * PCRE2_CALL_CONVENTION |
81 | | pcre2_match_data_create_from_pattern(const pcre2_code *code, |
82 | | pcre2_general_context *gcontext) |
83 | 0 | { |
84 | 0 | if (gcontext == NULL) gcontext = (pcre2_general_context *)code; |
85 | 0 | return pcre2_match_data_create(((const pcre2_real_code *)code)->top_bracket + 1, |
86 | 0 | gcontext); |
87 | 0 | } |
88 | | |
89 | | |
90 | | |
91 | | /************************************************* |
92 | | * Free a match data block * |
93 | | *************************************************/ |
94 | | |
95 | | PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION |
96 | | pcre2_match_data_free(pcre2_match_data *match_data) |
97 | 81.1k | { |
98 | 81.1k | if (match_data != NULL) |
99 | 81.1k | { |
100 | 81.1k | if (match_data->heapframes != NULL) |
101 | 79.5k | match_data->memctl.free(match_data->heapframes, |
102 | 79.5k | match_data->memctl.memory_data); |
103 | 81.1k | if ((match_data->flags & PCRE2_MD_COPIED_SUBJECT) != 0) |
104 | 0 | match_data->memctl.free((void *)match_data->subject, |
105 | 0 | match_data->memctl.memory_data); |
106 | 81.1k | match_data->memctl.free(match_data, match_data->memctl.memory_data); |
107 | 81.1k | } |
108 | 81.1k | } |
109 | | |
110 | | |
111 | | |
112 | | /************************************************* |
113 | | * Get last mark in match * |
114 | | *************************************************/ |
115 | | |
116 | | PCRE2_EXP_DEFN PCRE2_SPTR PCRE2_CALL_CONVENTION |
117 | | pcre2_get_mark(pcre2_match_data *match_data) |
118 | 0 | { |
119 | 0 | return match_data->mark; |
120 | 0 | } |
121 | | |
122 | | |
123 | | |
124 | | /************************************************* |
125 | | * Get pointer to ovector * |
126 | | *************************************************/ |
127 | | |
128 | | PCRE2_EXP_DEFN PCRE2_SIZE * PCRE2_CALL_CONVENTION |
129 | | pcre2_get_ovector_pointer(pcre2_match_data *match_data) |
130 | 0 | { |
131 | 0 | return match_data->ovector; |
132 | 0 | } |
133 | | |
134 | | |
135 | | |
136 | | /************************************************* |
137 | | * Get number of ovector slots * |
138 | | *************************************************/ |
139 | | |
140 | | PCRE2_EXP_DEFN uint32_t PCRE2_CALL_CONVENTION |
141 | | pcre2_get_ovector_count(pcre2_match_data *match_data) |
142 | 0 | { |
143 | 0 | return match_data->oveccount; |
144 | 0 | } |
145 | | |
146 | | |
147 | | |
148 | | /************************************************* |
149 | | * Get starting code unit in match * |
150 | | *************************************************/ |
151 | | |
152 | | PCRE2_EXP_DEFN PCRE2_SIZE PCRE2_CALL_CONVENTION |
153 | | pcre2_get_startchar(pcre2_match_data *match_data) |
154 | 0 | { |
155 | 0 | return match_data->startchar; |
156 | 0 | } |
157 | | |
158 | | |
159 | | |
160 | | /************************************************* |
161 | | * Get size of match data block * |
162 | | *************************************************/ |
163 | | |
164 | | PCRE2_EXP_DEFN PCRE2_SIZE PCRE2_CALL_CONVENTION |
165 | | pcre2_get_match_data_size(pcre2_match_data *match_data) |
166 | 0 | { |
167 | 0 | return offsetof(pcre2_match_data, ovector) + |
168 | 0 | 2 * (match_data->oveccount) * sizeof(PCRE2_SIZE); |
169 | 0 | } |
170 | | |
171 | | |
172 | | |
173 | | /************************************************* |
174 | | * Get heapframes size * |
175 | | *************************************************/ |
176 | | |
177 | | PCRE2_EXP_DEFN PCRE2_SIZE PCRE2_CALL_CONVENTION |
178 | | pcre2_get_match_data_heapframes_size(pcre2_match_data *match_data) |
179 | 0 | { |
180 | 0 | return match_data->heapframes_size; |
181 | 0 | } |
182 | | |
183 | | /* End of pcre2_match_data.c */ |