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-2017 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_internal.h to be in code units. */ 47 48 static int configured_link_size = LINK_SIZE; 49 50 #include "pcre2_internal.h" 51 52 /* These macros are the standard way of turning unquoted text into C strings. 53 They allow macros like PCRE2_MAJOR to be defined without quotes, which is 54 convenient for user programs that want to test their values. */ 55 56 #define STRING(a) # a 57 #define XSTRING(s) STRING(s) 58 59 60 /************************************************* 61 * Return info about what features are configured * 62 *************************************************/ 63 64 /* If where is NULL, the length of memory required is returned. 65 66 Arguments: 67 what what information is required 68 where where to put the information 69 70 Returns: 0 if a numerical value is returned 71 >= 0 if a string value 72 PCRE2_ERROR_BADOPTION if "where" not recognized 73 or JIT target requested when JIT not enabled 74 */ 75 76 PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION 77 pcre2_config(uint32_t what, void *where) 78 { 79 if (where == NULL) /* Requests a length */ 80 { 81 switch(what) 82 { 83 default: 84 return PCRE2_ERROR_BADOPTION; 85 86 case PCRE2_CONFIG_BSR: 87 case PCRE2_CONFIG_COMPILED_WIDTHS: 88 case PCRE2_CONFIG_DEPTHLIMIT: 89 case PCRE2_CONFIG_HEAPLIMIT: 90 case PCRE2_CONFIG_JIT: 91 case PCRE2_CONFIG_LINKSIZE: 92 case PCRE2_CONFIG_MATCHLIMIT: 93 case PCRE2_CONFIG_NEVER_BACKSLASH_C: 94 case PCRE2_CONFIG_NEWLINE: 95 case PCRE2_CONFIG_PARENSLIMIT: 96 case PCRE2_CONFIG_STACKRECURSE: /* Obsolete */ 97 case PCRE2_CONFIG_UNICODE: 98 return sizeof(uint32_t); 99 100 /* These are handled below */ 101 102 case PCRE2_CONFIG_JITTARGET: 103 case PCRE2_CONFIG_UNICODE_VERSION: 104 case PCRE2_CONFIG_VERSION: 105 break; 106 } 107 } 108 109 switch (what) 110 { 111 default: 112 return PCRE2_ERROR_BADOPTION; 113 114 case PCRE2_CONFIG_BSR: 115 #ifdef BSR_ANYCRLF 116 *((uint32_t *)where) = PCRE2_BSR_ANYCRLF; 117 #else 118 *((uint32_t *)where) = PCRE2_BSR_UNICODE; 119 #endif 120 break; 121 122 case PCRE2_CONFIG_COMPILED_WIDTHS: 123 *((uint32_t *)where) = 0 124 #ifdef SUPPORT_PCRE2_8 125 + 1 126 #endif 127 #ifdef SUPPORT_PCRE2_16 128 + 2 129 #endif 130 #ifdef SUPPORT_PCRE2_32 131 + 4 132 #endif 133 ; 134 break; 135 136 case PCRE2_CONFIG_DEPTHLIMIT: 137 *((uint32_t *)where) = MATCH_LIMIT_DEPTH; 138 break; 139 140 case PCRE2_CONFIG_HEAPLIMIT: 141 *((uint32_t *)where) = HEAP_LIMIT; 142 break; 143 144 case PCRE2_CONFIG_JIT: 145 #ifdef SUPPORT_JIT 146 *((uint32_t *)where) = 1; 147 #else 148 *((uint32_t *)where) = 0; 149 #endif 150 break; 151 152 case PCRE2_CONFIG_JITTARGET: 153 #ifdef SUPPORT_JIT 154 { 155 const char *v = PRIV(jit_get_target)(); 156 return (int)(1 + ((where == NULL)? 157 strlen(v) : PRIV(strcpy_c8)((PCRE2_UCHAR *)where, v))); 158 } 159 #else 160 return PCRE2_ERROR_BADOPTION; 161 #endif 162 163 case PCRE2_CONFIG_LINKSIZE: 164 *((uint32_t *)where) = (uint32_t)configured_link_size; 165 break; 166 167 case PCRE2_CONFIG_MATCHLIMIT: 168 *((uint32_t *)where) = MATCH_LIMIT; 169 break; 170 171 case PCRE2_CONFIG_NEWLINE: 172 *((uint32_t *)where) = NEWLINE_DEFAULT; 173 break; 174 175 case PCRE2_CONFIG_NEVER_BACKSLASH_C: 176 #ifdef NEVER_BACKSLASH_C 177 *((uint32_t *)where) = 1; 178 #else 179 *((uint32_t *)where) = 0; 180 #endif 181 break; 182 183 case PCRE2_CONFIG_PARENSLIMIT: 184 *((uint32_t *)where) = PARENS_NEST_LIMIT; 185 break; 186 187 /* This is now obsolete. The stack is no longer used via recursion for 188 handling backtracking in pcre2_match(). */ 189 190 case PCRE2_CONFIG_STACKRECURSE: 191 *((uint32_t *)where) = 0; 192 break; 193 194 case PCRE2_CONFIG_UNICODE_VERSION: 195 { 196 #if defined SUPPORT_UNICODE 197 const char *v = PRIV(unicode_version); 198 #else 199 const char *v = "Unicode not supported"; 200 #endif 201 return (int)(1 + ((where == NULL)? 202 strlen(v) : PRIV(strcpy_c8)((PCRE2_UCHAR *)where, v))); 203 } 204 break; 205 206 case PCRE2_CONFIG_UNICODE: 207 #if defined SUPPORT_UNICODE 208 *((uint32_t *)where) = 1; 209 #else 210 *((uint32_t *)where) = 0; 211 #endif 212 break; 213 214 /* The hackery in setting "v" below is to cope with the case when 215 PCRE2_PRERELEASE is set to an empty string (which it is for real releases). 216 If the second alternative is used in this case, it does not leave a space 217 before the date. On the other hand, if all four macros are put into a single 218 XSTRING when PCRE2_PRERELEASE is not empty, an unwanted space is inserted. 219 There are problems using an "obvious" approach like this: 220 221 XSTRING(PCRE2_MAJOR) "." XSTRING(PCRE_MINOR) 222 XSTRING(PCRE2_PRERELEASE) " " XSTRING(PCRE_DATE) 223 224 because, when PCRE2_PRERELEASE is empty, this leads to an attempted expansion 225 of STRING(). The C standard states: "If (before argument substitution) any 226 argument consists of no preprocessing tokens, the behavior is undefined." It 227 turns out the gcc treats this case as a single empty string - which is what 228 we really want - but Visual C grumbles about the lack of an argument for the 229 macro. Unfortunately, both are within their rights. As there seems to be no 230 way to test for a macro's value being empty at compile time, we have to 231 resort to a runtime test. */ 232 233 case PCRE2_CONFIG_VERSION: 234 { 235 const char *v = (XSTRING(Z PCRE2_PRERELEASE)[1] == 0)? 236 XSTRING(PCRE2_MAJOR.PCRE2_MINOR PCRE2_DATE) : 237 XSTRING(PCRE2_MAJOR.PCRE2_MINOR) XSTRING(PCRE2_PRERELEASE PCRE2_DATE); 238 return (int)(1 + ((where == NULL)? 239 strlen(v) : PRIV(strcpy_c8)((PCRE2_UCHAR *)where, v))); 240 } 241 } 242 243 return 0; 244 } 245 246 /* End of pcre2_config.c */ 247