blob: 53e469870796445843786654a07322f155067cea [file] [log] [blame]
Janis Danisevskis112c9cc2016-03-31 13:35:25 +01001/*************************************************
2* Perl-Compatible Regular Expressions *
3*************************************************/
4
5/* PCRE is a library of functions to support regular expressions whose syntax
6and 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
Elliott Hughes2dbd7d22020-06-03 14:32:37 -070010 New API code Copyright (c) 2016-2019 University of Cambridge
Janis Danisevskis112c9cc2016-03-31 13:35:25 +010011
12-----------------------------------------------------------------------------
13Redistribution and use in source and binary forms, with or without
14modification, 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
27THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
31LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37POSSIBILITY OF SUCH DAMAGE.
38-----------------------------------------------------------------------------
39*/
40
41
42#ifdef HAVE_CONFIG_H
43#include "config.h"
44#endif
45
46#include "pcre2_internal.h"
47
48
49
50/*************************************************
51* Create a match data block given ovector size *
52*************************************************/
53
Elliott Hughes9bc971b2018-07-27 13:23:14 -070054/* A minimum of 1 is imposed on the number of ovector pairs. */
Janis Danisevskis112c9cc2016-03-31 13:35:25 +010055
56PCRE2_EXP_DEFN pcre2_match_data * PCRE2_CALL_CONVENTION
57pcre2_match_data_create(uint32_t oveccount, pcre2_general_context *gcontext)
58{
59pcre2_match_data *yield;
60if (oveccount < 1) oveccount = 1;
61yield = PRIV(memctl_malloc)(
Elliott Hughes9bc971b2018-07-27 13:23:14 -070062 offsetof(pcre2_match_data, ovector) + 2*oveccount*sizeof(PCRE2_SIZE),
Janis Danisevskis112c9cc2016-03-31 13:35:25 +010063 (pcre2_memctl *)gcontext);
64if (yield == NULL) return NULL;
65yield->oveccount = oveccount;
Elliott Hughes0c26e192019-08-07 12:24:46 -070066yield->flags = 0;
Janis Danisevskis112c9cc2016-03-31 13:35:25 +010067return yield;
68}
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. */
77
78PCRE2_EXP_DEFN pcre2_match_data * PCRE2_CALL_CONVENTION
79pcre2_match_data_create_from_pattern(const pcre2_code *code,
80 pcre2_general_context *gcontext)
81{
82if (gcontext == NULL) gcontext = (pcre2_general_context *)code;
83return pcre2_match_data_create(((pcre2_real_code *)code)->top_bracket + 1,
84 gcontext);
85}
86
87
88
89/*************************************************
90* Free a match data block *
91*************************************************/
92
93PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
94pcre2_match_data_free(pcre2_match_data *match_data)
95{
96if (match_data != NULL)
Elliott Hughes0c26e192019-08-07 12:24:46 -070097 {
98 if ((match_data->flags & PCRE2_MD_COPIED_SUBJECT) != 0)
99 match_data->memctl.free((void *)match_data->subject,
100 match_data->memctl.memory_data);
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100101 match_data->memctl.free(match_data, match_data->memctl.memory_data);
Elliott Hughes0c26e192019-08-07 12:24:46 -0700102 }
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100103}
104
105
106
107/*************************************************
108* Get last mark in match *
109*************************************************/
110
111PCRE2_EXP_DEFN PCRE2_SPTR PCRE2_CALL_CONVENTION
112pcre2_get_mark(pcre2_match_data *match_data)
113{
114return match_data->mark;
115}
116
117
118
119/*************************************************
120* Get pointer to ovector *
121*************************************************/
122
123PCRE2_EXP_DEFN PCRE2_SIZE * PCRE2_CALL_CONVENTION
124pcre2_get_ovector_pointer(pcre2_match_data *match_data)
125{
126return match_data->ovector;
127}
128
129
130
131/*************************************************
132* Get number of ovector slots *
133*************************************************/
134
135PCRE2_EXP_DEFN uint32_t PCRE2_CALL_CONVENTION
136pcre2_get_ovector_count(pcre2_match_data *match_data)
137{
138return match_data->oveccount;
139}
140
141
142
143/*************************************************
144* Get starting code unit in match *
145*************************************************/
146
147PCRE2_EXP_DEFN PCRE2_SIZE PCRE2_CALL_CONVENTION
148pcre2_get_startchar(pcre2_match_data *match_data)
149{
150return match_data->startchar;
151}
152
Elliott Hughes2dbd7d22020-06-03 14:32:37 -0700153
154
155/*************************************************
156* Get size of match data block *
157*************************************************/
158
159PCRE2_EXP_DEFN PCRE2_SIZE PCRE2_CALL_CONVENTION
160pcre2_get_match_data_size(pcre2_match_data *match_data)
161{
162return offsetof(pcre2_match_data, ovector) +
163 2 * (match_data->oveccount) * sizeof(PCRE2_SIZE);
164}
165
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100166/* End of pcre2_match_data.c */