xref: /PHP-7.4/ext/pcre/pcre2lib/pcre2_config.c (revision 9f2d0395)
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-2020 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 #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
46 its value gets changed by pcre2_intmodedep.h (included by pcre2_internal.h) to
47 be in code units. */
48 
49 static 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.
54 They allow macros like PCRE2_MAJOR to be defined without quotes, which is
55 convenient 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 
65 /* If where is NULL, the length of memory required is returned.
66 
67 Arguments:
68   what             what information is required
69   where            where to put the information
70 
71 Returns:           0 if a numerical value is returned
72                    >= 0 if a string value
73                    PCRE2_ERROR_BADOPTION if "where" not recognized
74                      or JIT target requested when JIT not enabled
75 */
76 
77 PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
pcre2_config(uint32_t what,void * where)78 pcre2_config(uint32_t what, void *where)
79 {
80 if (where == NULL)  /* Requests a length */
81   {
82   switch(what)
83     {
84     default:
85     return PCRE2_ERROR_BADOPTION;
86 
87     case PCRE2_CONFIG_BSR:
88     case PCRE2_CONFIG_COMPILED_WIDTHS:
89     case PCRE2_CONFIG_DEPTHLIMIT:
90     case PCRE2_CONFIG_HEAPLIMIT:
91     case PCRE2_CONFIG_JIT:
92     case PCRE2_CONFIG_LINKSIZE:
93     case PCRE2_CONFIG_MATCHLIMIT:
94     case PCRE2_CONFIG_NEVER_BACKSLASH_C:
95     case PCRE2_CONFIG_NEWLINE:
96     case PCRE2_CONFIG_PARENSLIMIT:
97     case PCRE2_CONFIG_STACKRECURSE:    /* Obsolete */
98     case PCRE2_CONFIG_TABLES_LENGTH:
99     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 
111 switch (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 
124   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 
146   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)();
158     return (int)(1 + ((where == NULL)?
159       strlen(v) : PRIV(strcpy_c8)((PCRE2_UCHAR *)where, v)));
160     }
161 #else
162   return PCRE2_ERROR_BADOPTION;
163 #endif
164 
165   case PCRE2_CONFIG_LINKSIZE:
166   *((uint32_t *)where) = (uint32_t)configured_link_size;
167   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 
177   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 
185   case PCRE2_CONFIG_PARENSLIMIT:
186   *((uint32_t *)where) = PARENS_NEST_LIMIT;
187   break;
188 
189   /* This is now obsolete. The stack is no longer used via recursion for
190   handling backtracking in pcre2_match(). */
191 
192   case PCRE2_CONFIG_STACKRECURSE:
193   *((uint32_t *)where) = 0;
194   break;
195 
196   case PCRE2_CONFIG_TABLES_LENGTH:
197   *((uint32_t *)where) = TABLES_LENGTH;
198   break;
199 
200   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
207     return (int)(1 + ((where == NULL)?
208       strlen(v) : PRIV(strcpy_c8)((PCRE2_UCHAR *)where, v)));
209    }
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);
244     return (int)(1 + ((where == NULL)?
245       strlen(v) : PRIV(strcpy_c8)((PCRE2_UCHAR *)where, v)));
246     }
247   }
248 
249 return 0;
250 }
251 
252 /* End of pcre2_config.c */
253