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 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 /* This module is a wrapper that provides a POSIX API to the underlying PCRE2 43 functions. */ 44 45 46 #ifdef HAVE_CONFIG_H 47 #include "config.h" 48 #endif 49 50 51 /* Ensure that the PCRE2POSIX_EXP_xxx macros are set appropriately for 52 compiling these functions. This must come before including pcre2posix.h, where 53 they are set for an application (using these functions) if they have not 54 previously been set. */ 55 56 #if defined(_WIN32) && !defined(PCRE2_STATIC) 57 # define PCRE2POSIX_EXP_DECL extern __declspec(dllexport) 58 # define PCRE2POSIX_EXP_DEFN __declspec(dllexport) 59 #endif 60 61 /* Older versions of MSVC lack snprintf(). This define allows for 62 warning/error-free compilation and testing with MSVC compilers back to at least 63 MSVC 10/2010. Except for VC6 (which is missing some fundamentals and fails). */ 64 65 #if defined(_MSC_VER) && (_MSC_VER < 1900) 66 #define snprintf _snprintf 67 #endif 68 69 70 /* Compile-time error numbers start at this value. It should probably never be 71 changed. This #define is a copy of the one in pcre2_internal.h. */ 72 73 #define COMPILE_ERROR_BASE 100 74 75 76 /* Standard C headers */ 77 78 #include <ctype.h> 79 #include <limits.h> 80 #include <stddef.h> 81 #include <stdio.h> 82 #include <stdlib.h> 83 #include <string.h> 84 85 /* PCRE2 headers */ 86 87 #include "pcre2.h" 88 #include "pcre2posix.h" 89 90 /* When compiling with the MSVC compiler, it is sometimes necessary to include 91 a "calling convention" before exported function names. (This is secondhand 92 information; I know nothing about MSVC myself). For example, something like 93 94 void __cdecl function(....) 95 96 might be needed. In order so make this easy, all the exported functions have 97 PCRE2_CALL_CONVENTION just before their names. It is rarely needed; if not 98 set, we ensure here that it has no effect. */ 99 100 #ifndef PCRE2_CALL_CONVENTION 101 #define PCRE2_CALL_CONVENTION 102 #endif 103 104 /* Table to translate PCRE2 compile time error codes into POSIX error codes. 105 Only a few PCRE2 errors with a value greater than 23 turn into special POSIX 106 codes: most go to REG_BADPAT. The second table lists, in pairs, those that 107 don't. */ 108 109 static const int eint1[] = { 110 0, /* No error */ 111 REG_EESCAPE, /* \ at end of pattern */ 112 REG_EESCAPE, /* \c at end of pattern */ 113 REG_EESCAPE, /* unrecognized character follows \ */ 114 REG_BADBR, /* numbers out of order in {} quantifier */ 115 /* 5 */ 116 REG_BADBR, /* number too big in {} quantifier */ 117 REG_EBRACK, /* missing terminating ] for character class */ 118 REG_ECTYPE, /* invalid escape sequence in character class */ 119 REG_ERANGE, /* range out of order in character class */ 120 REG_BADRPT, /* nothing to repeat */ 121 /* 10 */ 122 REG_ASSERT, /* internal error: unexpected repeat */ 123 REG_BADPAT, /* unrecognized character after (? or (?- */ 124 REG_BADPAT, /* POSIX named classes are supported only within a class */ 125 REG_BADPAT, /* POSIX collating elements are not supported */ 126 REG_EPAREN, /* missing ) */ 127 /* 15 */ 128 REG_ESUBREG, /* reference to non-existent subpattern */ 129 REG_INVARG, /* pattern passed as NULL */ 130 REG_INVARG, /* unknown compile-time option bit(s) */ 131 REG_EPAREN, /* missing ) after (?# comment */ 132 REG_ESIZE, /* parentheses nested too deeply */ 133 /* 20 */ 134 REG_ESIZE, /* regular expression too large */ 135 REG_ESPACE, /* failed to get memory */ 136 REG_EPAREN, /* unmatched closing parenthesis */ 137 REG_ASSERT /* internal error: code overflow */ 138 }; 139 140 static const int eint2[] = { 141 30, REG_ECTYPE, /* unknown POSIX class name */ 142 32, REG_INVARG, /* this version of PCRE2 does not have Unicode support */ 143 37, REG_EESCAPE, /* PCRE2 does not support \L, \l, \N{name}, \U, or \u */ 144 56, REG_INVARG, /* internal error: unknown newline setting */ 145 }; 146 147 /* Table of texts corresponding to POSIX error codes */ 148 149 static const char *const pstring[] = { 150 "", /* Dummy for value 0 */ 151 "internal error", /* REG_ASSERT */ 152 "invalid repeat counts in {}", /* BADBR */ 153 "pattern error", /* BADPAT */ 154 "? * + invalid", /* BADRPT */ 155 "unbalanced {}", /* EBRACE */ 156 "unbalanced []", /* EBRACK */ 157 "collation error - not relevant", /* ECOLLATE */ 158 "bad class", /* ECTYPE */ 159 "bad escape sequence", /* EESCAPE */ 160 "empty expression", /* EMPTY */ 161 "unbalanced ()", /* EPAREN */ 162 "bad range inside []", /* ERANGE */ 163 "expression too big", /* ESIZE */ 164 "failed to get memory", /* ESPACE */ 165 "bad back reference", /* ESUBREG */ 166 "bad argument", /* INVARG */ 167 "match failed" /* NOMATCH */ 168 }; 169 170 171 172 173 /************************************************* 174 * Translate error code to string * 175 *************************************************/ 176 177 PCRE2POSIX_EXP_DEFN size_t PCRE2_CALL_CONVENTION 178 regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size) 179 { 180 int used; 181 const char *message; 182 183 message = (errcode <= 0 || errcode >= (int)(sizeof(pstring)/sizeof(char *)))? 184 "unknown error code" : pstring[errcode]; 185 186 if (preg != NULL && (int)preg->re_erroffset != -1) 187 { 188 used = snprintf(errbuf, errbuf_size, "%s at offset %-6d", message, 189 (int)preg->re_erroffset); 190 } 191 else 192 { 193 used = snprintf(errbuf, errbuf_size, "%s", message); 194 } 195 196 return used + 1; 197 } 198 199 200 201 202 /************************************************* 203 * Free store held by a regex * 204 *************************************************/ 205 206 PCRE2POSIX_EXP_DEFN void PCRE2_CALL_CONVENTION 207 regfree(regex_t *preg) 208 { 209 pcre2_match_data_free(preg->re_match_data); 210 pcre2_code_free(preg->re_pcre2_code); 211 } 212 213 214 215 216 /************************************************* 217 * Compile a regular expression * 218 *************************************************/ 219 220 /* 221 Arguments: 222 preg points to a structure for recording the compiled expression 223 pattern the pattern to compile 224 cflags compilation flags 225 226 Returns: 0 on success 227 various non-zero codes on failure 228 */ 229 230 PCRE2POSIX_EXP_DEFN int PCRE2_CALL_CONVENTION 231 regcomp(regex_t *preg, const char *pattern, int cflags) 232 { 233 PCRE2_SIZE erroffset; 234 int errorcode; 235 int options = 0; 236 int re_nsub = 0; 237 238 if ((cflags & REG_ICASE) != 0) options |= PCRE2_CASELESS; 239 if ((cflags & REG_NEWLINE) != 0) options |= PCRE2_MULTILINE; 240 if ((cflags & REG_DOTALL) != 0) options |= PCRE2_DOTALL; 241 if ((cflags & REG_UTF) != 0) options |= PCRE2_UTF; 242 if ((cflags & REG_UCP) != 0) options |= PCRE2_UCP; 243 if ((cflags & REG_UNGREEDY) != 0) options |= PCRE2_UNGREEDY; 244 245 preg->re_cflags = cflags; 246 preg->re_pcre2_code = pcre2_compile((PCRE2_SPTR)pattern, PCRE2_ZERO_TERMINATED, 247 options, &errorcode, &erroffset, NULL); 248 preg->re_erroffset = erroffset; 249 250 if (preg->re_pcre2_code == NULL) 251 { 252 unsigned int i; 253 254 /* A negative value is a UTF error; otherwise all error codes are greater 255 than COMPILE_ERROR_BASE, but check, just in case. */ 256 257 if (errorcode < COMPILE_ERROR_BASE) return REG_BADPAT; 258 errorcode -= COMPILE_ERROR_BASE; 259 260 if (errorcode < (int)(sizeof(eint1)/sizeof(const int))) 261 return eint1[errorcode]; 262 for (i = 0; i < sizeof(eint2)/(2*sizeof(const int)); i += 2) 263 if (errorcode == eint2[i]) return eint2[i+1]; 264 return REG_BADPAT; 265 } 266 267 (void)pcre2_pattern_info((const pcre2_code *)preg->re_pcre2_code, 268 PCRE2_INFO_CAPTURECOUNT, &re_nsub); 269 preg->re_nsub = (size_t)re_nsub; 270 preg->re_match_data = pcre2_match_data_create(re_nsub + 1, NULL); 271 272 if (preg->re_match_data == NULL) 273 { 274 pcre2_code_free(preg->re_pcre2_code); 275 return REG_ESPACE; 276 } 277 278 return 0; 279 } 280 281 282 283 /************************************************* 284 * Match a regular expression * 285 *************************************************/ 286 287 /* A suitable match_data block, large enough to hold all possible captures, was 288 obtained when the pattern was compiled, to save having to allocate and free it 289 for each match. If REG_NOSUB was specified at compile time, the 290 PCRE_NO_AUTO_CAPTURE flag will be set. When this is the case, the nmatch and 291 pmatch arguments are ignored, and the only result is yes/no/error. */ 292 293 PCRE2POSIX_EXP_DEFN int PCRE2_CALL_CONVENTION 294 regexec(const regex_t *preg, const char *string, size_t nmatch, 295 regmatch_t pmatch[], int eflags) 296 { 297 int rc, so, eo; 298 int options = 0; 299 pcre2_match_data *md = (pcre2_match_data *)preg->re_match_data; 300 301 if ((eflags & REG_NOTBOL) != 0) options |= PCRE2_NOTBOL; 302 if ((eflags & REG_NOTEOL) != 0) options |= PCRE2_NOTEOL; 303 if ((eflags & REG_NOTEMPTY) != 0) options |= PCRE2_NOTEMPTY; 304 305 ((regex_t *)preg)->re_erroffset = (size_t)(-1); /* Only has meaning after compile */ 306 307 /* When REG_NOSUB was specified, or if no vector has been passed in which to 308 put captured strings, ensure that nmatch is zero. This will stop any attempt to 309 write to pmatch. */ 310 311 if ((preg->re_cflags & REG_NOSUB) != 0 || pmatch == NULL) nmatch = 0; 312 313 /* REG_STARTEND is a BSD extension, to allow for non-NUL-terminated strings. 314 The man page from OS X says "REG_STARTEND affects only the location of the 315 string, not how it is matched". That is why the "so" value is used to bump the 316 start location rather than being passed as a PCRE2 "starting offset". */ 317 318 if ((eflags & REG_STARTEND) != 0) 319 { 320 if (pmatch == NULL) return REG_INVARG; 321 so = pmatch[0].rm_so; 322 eo = pmatch[0].rm_eo; 323 } 324 else 325 { 326 so = 0; 327 eo = (int)strlen(string); 328 } 329 330 rc = pcre2_match((const pcre2_code *)preg->re_pcre2_code, 331 (PCRE2_SPTR)string + so, (eo - so), 0, options, md, NULL); 332 333 /* Successful match */ 334 335 if (rc >= 0) 336 { 337 size_t i; 338 PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(md); 339 if ((size_t)rc > nmatch) rc = (int)nmatch; 340 for (i = 0; i < (size_t)rc; i++) 341 { 342 pmatch[i].rm_so = ovector[i*2]; 343 pmatch[i].rm_eo = ovector[i*2+1]; 344 } 345 for (; i < nmatch; i++) pmatch[i].rm_so = pmatch[i].rm_eo = -1; 346 return 0; 347 } 348 349 /* Unsuccessful match */ 350 351 if (rc <= PCRE2_ERROR_UTF8_ERR1 && rc >= PCRE2_ERROR_UTF8_ERR21) 352 return REG_INVARG; 353 354 switch(rc) 355 { 356 default: return REG_ASSERT; 357 case PCRE2_ERROR_BADMODE: return REG_INVARG; 358 case PCRE2_ERROR_BADMAGIC: return REG_INVARG; 359 case PCRE2_ERROR_BADOPTION: return REG_INVARG; 360 case PCRE2_ERROR_BADUTFOFFSET: return REG_INVARG; 361 case PCRE2_ERROR_MATCHLIMIT: return REG_ESPACE; 362 case PCRE2_ERROR_NOMATCH: return REG_NOMATCH; 363 case PCRE2_ERROR_NOMEMORY: return REG_ESPACE; 364 case PCRE2_ERROR_NULL: return REG_INVARG; 365 } 366 } 367 368 /* End of pcre2posix.c */ 369