blob: 5ef103caf79258fbe658168ecb6674d7af3fe4bd [file] [log] [blame]
Janis Danisevskis53e448c2016-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-2020 University of Cambridge
Janis Danisevskis53e448c2016-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#ifdef HAVE_CONFIG_H
42#include "config.h"
43#endif
44
45/* Save the configured link size, which is in bytes. In 16-bit and 32-bit modes
Elliott Hughes2dbd7d22020-06-03 14:32:37 -070046its value gets changed by pcre2_intmodedep.h (included by pcre2_internal.h) to
47be in code units. */
Janis Danisevskis53e448c2016-03-31 13:35:25 +010048
49static int configured_link_size = LINK_SIZE;
50
51#include "pcre2_internal.h"
52
53/* These macros are the standard way of turning unquoted text into C strings.
54They allow macros like PCRE2_MAJOR to be defined without quotes, which is
55convenient for user programs that want to test their values. */
56
57#define STRING(a) # a
58#define XSTRING(s) STRING(s)
59
60
61/*************************************************
62* Return info about what features are configured *
63*************************************************/
64
Janis Danisevskis8b979b22016-08-15 16:09:16 +010065/* If where is NULL, the length of memory required is returned.
66
Janis Danisevskis53e448c2016-03-31 13:35:25 +010067Arguments:
68 what what information is required
69 where where to put the information
70
Janis Danisevskis8b979b22016-08-15 16:09:16 +010071Returns: 0 if a numerical value is returned
72 >= 0 if a string value
Janis Danisevskis53e448c2016-03-31 13:35:25 +010073 PCRE2_ERROR_BADOPTION if "where" not recognized
Janis Danisevskis8b979b22016-08-15 16:09:16 +010074 or JIT target requested when JIT not enabled
Janis Danisevskis53e448c2016-03-31 13:35:25 +010075*/
76
77PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
78pcre2_config(uint32_t what, void *where)
79{
80if (where == NULL) /* Requests a length */
81 {
82 switch(what)
83 {
84 default:
85 return PCRE2_ERROR_BADOPTION;
86
87 case PCRE2_CONFIG_BSR:
Elliott Hughes9bc971b2018-07-27 13:23:14 -070088 case PCRE2_CONFIG_COMPILED_WIDTHS:
89 case PCRE2_CONFIG_DEPTHLIMIT:
90 case PCRE2_CONFIG_HEAPLIMIT:
Janis Danisevskis53e448c2016-03-31 13:35:25 +010091 case PCRE2_CONFIG_JIT:
92 case PCRE2_CONFIG_LINKSIZE:
93 case PCRE2_CONFIG_MATCHLIMIT:
Elliott Hughes9bc971b2018-07-27 13:23:14 -070094 case PCRE2_CONFIG_NEVER_BACKSLASH_C:
Janis Danisevskis53e448c2016-03-31 13:35:25 +010095 case PCRE2_CONFIG_NEWLINE:
96 case PCRE2_CONFIG_PARENSLIMIT:
Elliott Hughes9bc971b2018-07-27 13:23:14 -070097 case PCRE2_CONFIG_STACKRECURSE: /* Obsolete */
Elliott Hughes2dbd7d22020-06-03 14:32:37 -070098 case PCRE2_CONFIG_TABLES_LENGTH:
Janis Danisevskis53e448c2016-03-31 13:35:25 +010099 case PCRE2_CONFIG_UNICODE:
100 return sizeof(uint32_t);
101
102 /* These are handled below */
103
104 case PCRE2_CONFIG_JITTARGET:
105 case PCRE2_CONFIG_UNICODE_VERSION:
106 case PCRE2_CONFIG_VERSION:
107 break;
108 }
109 }
110
111switch (what)
112 {
113 default:
114 return PCRE2_ERROR_BADOPTION;
115
116 case PCRE2_CONFIG_BSR:
117#ifdef BSR_ANYCRLF
118 *((uint32_t *)where) = PCRE2_BSR_ANYCRLF;
119#else
120 *((uint32_t *)where) = PCRE2_BSR_UNICODE;
121#endif
122 break;
123
Elliott Hughes9bc971b2018-07-27 13:23:14 -0700124 case PCRE2_CONFIG_COMPILED_WIDTHS:
125 *((uint32_t *)where) = 0
126#ifdef SUPPORT_PCRE2_8
127 + 1
128#endif
129#ifdef SUPPORT_PCRE2_16
130 + 2
131#endif
132#ifdef SUPPORT_PCRE2_32
133 + 4
134#endif
135 ;
136 break;
137
138 case PCRE2_CONFIG_DEPTHLIMIT:
139 *((uint32_t *)where) = MATCH_LIMIT_DEPTH;
140 break;
141
142 case PCRE2_CONFIG_HEAPLIMIT:
143 *((uint32_t *)where) = HEAP_LIMIT;
144 break;
145
Janis Danisevskis53e448c2016-03-31 13:35:25 +0100146 case PCRE2_CONFIG_JIT:
147#ifdef SUPPORT_JIT
148 *((uint32_t *)where) = 1;
149#else
150 *((uint32_t *)where) = 0;
151#endif
152 break;
153
154 case PCRE2_CONFIG_JITTARGET:
155#ifdef SUPPORT_JIT
156 {
157 const char *v = PRIV(jit_get_target)();
Janis Danisevskis8b979b22016-08-15 16:09:16 +0100158 return (int)(1 + ((where == NULL)?
159 strlen(v) : PRIV(strcpy_c8)((PCRE2_UCHAR *)where, v)));
Janis Danisevskis53e448c2016-03-31 13:35:25 +0100160 }
161#else
162 return PCRE2_ERROR_BADOPTION;
163#endif
164
165 case PCRE2_CONFIG_LINKSIZE:
Janis Danisevskis8b979b22016-08-15 16:09:16 +0100166 *((uint32_t *)where) = (uint32_t)configured_link_size;
Janis Danisevskis53e448c2016-03-31 13:35:25 +0100167 break;
168
169 case PCRE2_CONFIG_MATCHLIMIT:
170 *((uint32_t *)where) = MATCH_LIMIT;
171 break;
172
173 case PCRE2_CONFIG_NEWLINE:
174 *((uint32_t *)where) = NEWLINE_DEFAULT;
175 break;
176
Elliott Hughes9bc971b2018-07-27 13:23:14 -0700177 case PCRE2_CONFIG_NEVER_BACKSLASH_C:
178#ifdef NEVER_BACKSLASH_C
179 *((uint32_t *)where) = 1;
180#else
181 *((uint32_t *)where) = 0;
182#endif
183 break;
184
Janis Danisevskis53e448c2016-03-31 13:35:25 +0100185 case PCRE2_CONFIG_PARENSLIMIT:
186 *((uint32_t *)where) = PARENS_NEST_LIMIT;
187 break;
188
Elliott Hughes9bc971b2018-07-27 13:23:14 -0700189 /* This is now obsolete. The stack is no longer used via recursion for
190 handling backtracking in pcre2_match(). */
Janis Danisevskis53e448c2016-03-31 13:35:25 +0100191
192 case PCRE2_CONFIG_STACKRECURSE:
Janis Danisevskis53e448c2016-03-31 13:35:25 +0100193 *((uint32_t *)where) = 0;
Janis Danisevskis53e448c2016-03-31 13:35:25 +0100194 break;
195
Elliott Hughes2dbd7d22020-06-03 14:32:37 -0700196 case PCRE2_CONFIG_TABLES_LENGTH:
197 *((uint32_t *)where) = TABLES_LENGTH;
198 break;
199
Janis Danisevskis53e448c2016-03-31 13:35:25 +0100200 case PCRE2_CONFIG_UNICODE_VERSION:
201 {
202#if defined SUPPORT_UNICODE
203 const char *v = PRIV(unicode_version);
204#else
205 const char *v = "Unicode not supported";
206#endif
Janis Danisevskis8b979b22016-08-15 16:09:16 +0100207 return (int)(1 + ((where == NULL)?
208 strlen(v) : PRIV(strcpy_c8)((PCRE2_UCHAR *)where, v)));
Janis Danisevskis53e448c2016-03-31 13:35:25 +0100209 }
210 break;
211
212 case PCRE2_CONFIG_UNICODE:
213#if defined SUPPORT_UNICODE
214 *((uint32_t *)where) = 1;
215#else
216 *((uint32_t *)where) = 0;
217#endif
218 break;
219
220 /* The hackery in setting "v" below is to cope with the case when
221 PCRE2_PRERELEASE is set to an empty string (which it is for real releases).
222 If the second alternative is used in this case, it does not leave a space
223 before the date. On the other hand, if all four macros are put into a single
224 XSTRING when PCRE2_PRERELEASE is not empty, an unwanted space is inserted.
225 There are problems using an "obvious" approach like this:
226
227 XSTRING(PCRE2_MAJOR) "." XSTRING(PCRE_MINOR)
228 XSTRING(PCRE2_PRERELEASE) " " XSTRING(PCRE_DATE)
229
230 because, when PCRE2_PRERELEASE is empty, this leads to an attempted expansion
231 of STRING(). The C standard states: "If (before argument substitution) any
232 argument consists of no preprocessing tokens, the behavior is undefined." It
233 turns out the gcc treats this case as a single empty string - which is what
234 we really want - but Visual C grumbles about the lack of an argument for the
235 macro. Unfortunately, both are within their rights. As there seems to be no
236 way to test for a macro's value being empty at compile time, we have to
237 resort to a runtime test. */
238
239 case PCRE2_CONFIG_VERSION:
240 {
241 const char *v = (XSTRING(Z PCRE2_PRERELEASE)[1] == 0)?
242 XSTRING(PCRE2_MAJOR.PCRE2_MINOR PCRE2_DATE) :
243 XSTRING(PCRE2_MAJOR.PCRE2_MINOR) XSTRING(PCRE2_PRERELEASE PCRE2_DATE);
Janis Danisevskis8b979b22016-08-15 16:09:16 +0100244 return (int)(1 + ((where == NULL)?
245 strlen(v) : PRIV(strcpy_c8)((PCRE2_UCHAR *)where, v)));
Janis Danisevskis53e448c2016-03-31 13:35:25 +0100246 }
247 }
248
249return 0;
250}
251
252/* End of pcre2_config.c */