| 1 | /* Extended regular expression matching and search library. |
| 2 | Copyright (C) 2002-2018 Free Software Foundation, Inc. |
| 3 | This file is part of the GNU C Library. |
| 4 | Contributed by Isamu Hasegawa <isamu@yamato.ibm.com>. |
| 5 | |
| 6 | The GNU C Library is free software; you can redistribute it and/or |
| 7 | modify it under the terms of the GNU Lesser General Public |
| 8 | License as published by the Free Software Foundation; either |
| 9 | version 2.1 of the License, or (at your option) any later version. |
| 10 | |
| 11 | The GNU C Library is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | Lesser General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU Lesser General Public |
| 17 | License along with the GNU C Library; if not, see |
| 18 | <https://www.gnu.org/licenses/>. */ |
| 19 | |
| 20 | #ifndef _REGEX_INTERNAL_H |
| 21 | #define _REGEX_INTERNAL_H 1 |
| 22 | |
| 23 | #include <assert.h> |
| 24 | #include <ctype.h> |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
| 28 | |
| 29 | #include <langinfo.h> |
| 30 | #include <locale.h> |
| 31 | #include <wchar.h> |
| 32 | #include <wctype.h> |
| 33 | #include <stdbool.h> |
| 34 | #include <stdint.h> |
| 35 | |
| 36 | /* Properties of integers. Although Gnulib has intprops.h, glibc does |
| 37 | without for now. */ |
| 38 | #ifndef _LIBC |
| 39 | # include "intprops.h" |
| 40 | #else |
| 41 | /* True if the real type T is signed. */ |
| 42 | # define TYPE_SIGNED(t) (! ((t) 0 < (t) -1)) |
| 43 | |
| 44 | /* True if adding the nonnegative Idx values A and B would overflow. |
| 45 | If false, set *R to A + B. A, B, and R may be evaluated more than |
| 46 | once, or zero times. Although this is not a full implementation of |
| 47 | Gnulib INT_ADD_WRAPV, it is good enough for glibc regex code. |
| 48 | FIXME: This implementation is a fragile stopgap, and this file would |
| 49 | be simpler and more robust if intprops.h were migrated into glibc. */ |
| 50 | # define INT_ADD_WRAPV(a, b, r) \ |
| 51 | (IDX_MAX - (a) < (b) ? true : (*(r) = (a) + (b), false)) |
| 52 | #endif |
| 53 | |
| 54 | #ifdef _LIBC |
| 55 | # include <libc-lock.h> |
| 56 | # define lock_define(name) __libc_lock_define (, name) |
| 57 | # define lock_init(lock) (__libc_lock_init (lock), 0) |
| 58 | # define lock_fini(lock) ((void) 0) |
| 59 | # define lock_lock(lock) __libc_lock_lock (lock) |
| 60 | # define lock_unlock(lock) __libc_lock_unlock (lock) |
| 61 | #elif defined GNULIB_LOCK && !defined USE_UNLOCKED_IO |
| 62 | # include "glthread/lock.h" |
| 63 | /* Use gl_lock_define if empty macro arguments are known to work. |
| 64 | Otherwise, fall back on less-portable substitutes. */ |
| 65 | # if ((defined __GNUC__ && !defined __STRICT_ANSI__) \ |
| 66 | || (defined __STDC_VERSION__ && 199901L <= __STDC_VERSION__)) |
| 67 | # define lock_define(name) gl_lock_define (, name) |
| 68 | # elif USE_POSIX_THREADS |
| 69 | # define lock_define(name) pthread_mutex_t name; |
| 70 | # elif USE_PTH_THREADS |
| 71 | # define lock_define(name) pth_mutex_t name; |
| 72 | # elif USE_SOLARIS_THREADS |
| 73 | # define lock_define(name) mutex_t name; |
| 74 | # elif USE_WINDOWS_THREADS |
| 75 | # define lock_define(name) gl_lock_t name; |
| 76 | # else |
| 77 | # define lock_define(name) |
| 78 | # endif |
| 79 | # define lock_init(lock) glthread_lock_init (&(lock)) |
| 80 | # define lock_fini(lock) glthread_lock_destroy (&(lock)) |
| 81 | # define lock_lock(lock) glthread_lock_lock (&(lock)) |
| 82 | # define lock_unlock(lock) glthread_lock_unlock (&(lock)) |
| 83 | #elif defined GNULIB_PTHREAD && !defined USE_UNLOCKED_IO |
| 84 | # include <pthread.h> |
| 85 | # define lock_define(name) pthread_mutex_t name; |
| 86 | # define lock_init(lock) pthread_mutex_init (&(lock), 0) |
| 87 | # define lock_fini(lock) pthread_mutex_destroy (&(lock)) |
| 88 | # define lock_lock(lock) pthread_mutex_lock (&(lock)) |
| 89 | # define lock_unlock(lock) pthread_mutex_unlock (&(lock)) |
| 90 | #else |
| 91 | # define lock_define(name) |
| 92 | # define lock_init(lock) 0 |
| 93 | # define lock_fini(lock) ((void) 0) |
| 94 | /* The 'dfa' avoids an "unused variable 'dfa'" warning from GCC. */ |
| 95 | # define lock_lock(lock) ((void) dfa) |
| 96 | # define lock_unlock(lock) ((void) 0) |
| 97 | #endif |
| 98 | |
| 99 | /* In case that the system doesn't have isblank(). */ |
| 100 | #if !defined _LIBC && ! (defined isblank || (HAVE_ISBLANK && HAVE_DECL_ISBLANK)) |
| 101 | # define isblank(ch) ((ch) == ' ' || (ch) == '\t') |
| 102 | #endif |
| 103 | |
| 104 | #ifdef _LIBC |
| 105 | # ifndef _RE_DEFINE_LOCALE_FUNCTIONS |
| 106 | # define _RE_DEFINE_LOCALE_FUNCTIONS 1 |
| 107 | # include <locale/localeinfo.h> |
| 108 | # include <locale/coll-lookup.h> |
| 109 | # endif |
| 110 | #endif |
| 111 | |
| 112 | /* This is for other GNU distributions with internationalized messages. */ |
| 113 | #if (HAVE_LIBINTL_H && ENABLE_NLS) || defined _LIBC |
| 114 | # include <libintl.h> |
| 115 | # ifdef _LIBC |
| 116 | # undef gettext |
| 117 | # define gettext(msgid) \ |
| 118 | __dcgettext (_libc_intl_domainname, msgid, LC_MESSAGES) |
| 119 | # endif |
| 120 | #else |
| 121 | # undef gettext |
| 122 | # define gettext(msgid) (msgid) |
| 123 | #endif |
| 124 | |
| 125 | #ifndef gettext_noop |
| 126 | /* This define is so xgettext can find the internationalizable |
| 127 | strings. */ |
| 128 | # define gettext_noop(String) String |
| 129 | #endif |
| 130 | |
| 131 | #if (defined MB_CUR_MAX && HAVE_WCTYPE_H && HAVE_ISWCTYPE) || _LIBC |
| 132 | # define RE_ENABLE_I18N |
| 133 | #endif |
| 134 | |
| 135 | #define BE(expr, val) __builtin_expect (expr, val) |
| 136 | |
| 137 | /* Number of ASCII characters. */ |
| 138 | #define ASCII_CHARS 0x80 |
| 139 | |
| 140 | /* Number of single byte characters. */ |
| 141 | #define SBC_MAX (UCHAR_MAX + 1) |
| 142 | |
| 143 | #define COLL_ELEM_LEN_MAX 8 |
| 144 | |
| 145 | /* The character which represents newline. */ |
| 146 | #define NEWLINE_CHAR '\n' |
| 147 | #define WIDE_NEWLINE_CHAR L'\n' |
| 148 | |
| 149 | /* Rename to standard API for using out of glibc. */ |
| 150 | #ifndef _LIBC |
| 151 | # undef __wctype |
| 152 | # undef __iswctype |
| 153 | # define __wctype wctype |
| 154 | # define __iswalnum iswalnum |
| 155 | # define __iswctype iswctype |
| 156 | # define __towlower towlower |
| 157 | # define __towupper towupper |
| 158 | # define __btowc btowc |
| 159 | # define __mbrtowc mbrtowc |
| 160 | # define __wcrtomb wcrtomb |
| 161 | # define __regfree regfree |
| 162 | # define attribute_hidden |
| 163 | #endif /* not _LIBC */ |
| 164 | |
| 165 | #if __GNUC__ < 3 + (__GNUC_MINOR__ < 1) |
| 166 | # define __attribute__(arg) |
| 167 | #endif |
| 168 | |
| 169 | #ifndef SSIZE_MAX |
| 170 | # define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2)) |
| 171 | #endif |
| 172 | |
| 173 | /* The type of indexes into strings. This is signed, not size_t, |
| 174 | since the API requires indexes to fit in regoff_t anyway, and using |
| 175 | signed integers makes the code a bit smaller and presumably faster. |
| 176 | The traditional GNU regex implementation uses int for indexes. |
| 177 | The POSIX-compatible implementation uses a possibly-wider type. |
| 178 | The name 'Idx' is three letters to minimize the hassle of |
| 179 | reindenting a lot of regex code that formerly used 'int'. */ |
| 180 | typedef regoff_t Idx; |
| 181 | #ifdef _REGEX_LARGE_OFFSETS |
| 182 | # define IDX_MAX SSIZE_MAX |
| 183 | #else |
| 184 | # define IDX_MAX INT_MAX |
| 185 | #endif |
| 186 | |
| 187 | /* A hash value, suitable for computing hash tables. */ |
| 188 | typedef __re_size_t re_hashval_t; |
| 189 | |
| 190 | /* An integer used to represent a set of bits. It must be unsigned, |
| 191 | and must be at least as wide as unsigned int. */ |
| 192 | typedef unsigned long int bitset_word_t; |
| 193 | /* All bits set in a bitset_word_t. */ |
| 194 | #define BITSET_WORD_MAX ULONG_MAX |
| 195 | |
| 196 | /* Number of bits in a bitset_word_t. For portability to hosts with |
| 197 | padding bits, do not use '(sizeof (bitset_word_t) * CHAR_BIT)'; |
| 198 | instead, deduce it directly from BITSET_WORD_MAX. Avoid |
| 199 | greater-than-32-bit integers and unconditional shifts by more than |
| 200 | 31 bits, as they're not portable. */ |
| 201 | #if BITSET_WORD_MAX == 0xffffffffUL |
| 202 | # define BITSET_WORD_BITS 32 |
| 203 | #elif BITSET_WORD_MAX >> 31 >> 4 == 1 |
| 204 | # define BITSET_WORD_BITS 36 |
| 205 | #elif BITSET_WORD_MAX >> 31 >> 16 == 1 |
| 206 | # define BITSET_WORD_BITS 48 |
| 207 | #elif BITSET_WORD_MAX >> 31 >> 28 == 1 |
| 208 | # define BITSET_WORD_BITS 60 |
| 209 | #elif BITSET_WORD_MAX >> 31 >> 31 >> 1 == 1 |
| 210 | # define BITSET_WORD_BITS 64 |
| 211 | #elif BITSET_WORD_MAX >> 31 >> 31 >> 9 == 1 |
| 212 | # define BITSET_WORD_BITS 72 |
| 213 | #elif BITSET_WORD_MAX >> 31 >> 31 >> 31 >> 31 >> 3 == 1 |
| 214 | # define BITSET_WORD_BITS 128 |
| 215 | #elif BITSET_WORD_MAX >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 7 == 1 |
| 216 | # define BITSET_WORD_BITS 256 |
| 217 | #elif BITSET_WORD_MAX >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 7 > 1 |
| 218 | # define BITSET_WORD_BITS 257 /* any value > SBC_MAX will do here */ |
| 219 | # if BITSET_WORD_BITS <= SBC_MAX |
| 220 | # error "Invalid SBC_MAX" |
| 221 | # endif |
| 222 | #else |
| 223 | # error "Add case for new bitset_word_t size" |
| 224 | #endif |
| 225 | |
| 226 | /* Number of bitset_word_t values in a bitset_t. */ |
| 227 | #define BITSET_WORDS ((SBC_MAX + BITSET_WORD_BITS - 1) / BITSET_WORD_BITS) |
| 228 | |
| 229 | typedef bitset_word_t bitset_t[BITSET_WORDS]; |
| 230 | typedef bitset_word_t *re_bitset_ptr_t; |
| 231 | typedef const bitset_word_t *re_const_bitset_ptr_t; |
| 232 | |
| 233 | #define PREV_WORD_CONSTRAINT 0x0001 |
| 234 | #define PREV_NOTWORD_CONSTRAINT 0x0002 |
| 235 | #define NEXT_WORD_CONSTRAINT 0x0004 |
| 236 | #define NEXT_NOTWORD_CONSTRAINT 0x0008 |
| 237 | #define PREV_NEWLINE_CONSTRAINT 0x0010 |
| 238 | #define NEXT_NEWLINE_CONSTRAINT 0x0020 |
| 239 | #define PREV_BEGBUF_CONSTRAINT 0x0040 |
| 240 | #define NEXT_ENDBUF_CONSTRAINT 0x0080 |
| 241 | #define WORD_DELIM_CONSTRAINT 0x0100 |
| 242 | #define NOT_WORD_DELIM_CONSTRAINT 0x0200 |
| 243 | |
| 244 | typedef enum |
| 245 | { |
| 246 | INSIDE_WORD = PREV_WORD_CONSTRAINT | NEXT_WORD_CONSTRAINT, |
| 247 | WORD_FIRST = PREV_NOTWORD_CONSTRAINT | NEXT_WORD_CONSTRAINT, |
| 248 | WORD_LAST = PREV_WORD_CONSTRAINT | NEXT_NOTWORD_CONSTRAINT, |
| 249 | INSIDE_NOTWORD = PREV_NOTWORD_CONSTRAINT | NEXT_NOTWORD_CONSTRAINT, |
| 250 | LINE_FIRST = PREV_NEWLINE_CONSTRAINT, |
| 251 | LINE_LAST = NEXT_NEWLINE_CONSTRAINT, |
| 252 | BUF_FIRST = PREV_BEGBUF_CONSTRAINT, |
| 253 | BUF_LAST = NEXT_ENDBUF_CONSTRAINT, |
| 254 | WORD_DELIM = WORD_DELIM_CONSTRAINT, |
| 255 | NOT_WORD_DELIM = NOT_WORD_DELIM_CONSTRAINT |
| 256 | } re_context_type; |
| 257 | |
| 258 | typedef struct |
| 259 | { |
| 260 | Idx alloc; |
| 261 | Idx nelem; |
| 262 | Idx *elems; |
| 263 | } re_node_set; |
| 264 | |
| 265 | typedef enum |
| 266 | { |
| 267 | NON_TYPE = 0, |
| 268 | |
| 269 | /* Node type, These are used by token, node, tree. */ |
| 270 | CHARACTER = 1, |
| 271 | END_OF_RE = 2, |
| 272 | SIMPLE_BRACKET = 3, |
| 273 | OP_BACK_REF = 4, |
| 274 | OP_PERIOD = 5, |
| 275 | #ifdef RE_ENABLE_I18N |
| 276 | COMPLEX_BRACKET = 6, |
| 277 | OP_UTF8_PERIOD = 7, |
| 278 | #endif /* RE_ENABLE_I18N */ |
| 279 | |
| 280 | /* We define EPSILON_BIT as a macro so that OP_OPEN_SUBEXP is used |
| 281 | when the debugger shows values of this enum type. */ |
| 282 | #define EPSILON_BIT 8 |
| 283 | OP_OPEN_SUBEXP = EPSILON_BIT | 0, |
| 284 | OP_CLOSE_SUBEXP = EPSILON_BIT | 1, |
| 285 | OP_ALT = EPSILON_BIT | 2, |
| 286 | OP_DUP_ASTERISK = EPSILON_BIT | 3, |
| 287 | ANCHOR = EPSILON_BIT | 4, |
| 288 | |
| 289 | /* Tree type, these are used only by tree. */ |
| 290 | CONCAT = 16, |
| 291 | SUBEXP = 17, |
| 292 | |
| 293 | /* Token type, these are used only by token. */ |
| 294 | OP_DUP_PLUS = 18, |
| 295 | OP_DUP_QUESTION, |
| 296 | OP_OPEN_BRACKET, |
| 297 | OP_CLOSE_BRACKET, |
| 298 | OP_CHARSET_RANGE, |
| 299 | OP_OPEN_DUP_NUM, |
| 300 | OP_CLOSE_DUP_NUM, |
| 301 | OP_NON_MATCH_LIST, |
| 302 | OP_OPEN_COLL_ELEM, |
| 303 | OP_CLOSE_COLL_ELEM, |
| 304 | OP_OPEN_EQUIV_CLASS, |
| 305 | OP_CLOSE_EQUIV_CLASS, |
| 306 | OP_OPEN_CHAR_CLASS, |
| 307 | OP_CLOSE_CHAR_CLASS, |
| 308 | OP_WORD, |
| 309 | OP_NOTWORD, |
| 310 | OP_SPACE, |
| 311 | OP_NOTSPACE, |
| 312 | BACK_SLASH |
| 313 | |
| 314 | } re_token_type_t; |
| 315 | |
| 316 | #ifdef RE_ENABLE_I18N |
| 317 | typedef struct |
| 318 | { |
| 319 | /* Multibyte characters. */ |
| 320 | wchar_t *mbchars; |
| 321 | |
| 322 | /* Collating symbols. */ |
| 323 | # ifdef _LIBC |
| 324 | int32_t *coll_syms; |
| 325 | # endif |
| 326 | |
| 327 | /* Equivalence classes. */ |
| 328 | # ifdef _LIBC |
| 329 | int32_t *equiv_classes; |
| 330 | # endif |
| 331 | |
| 332 | /* Range expressions. */ |
| 333 | # ifdef _LIBC |
| 334 | uint32_t *range_starts; |
| 335 | uint32_t *range_ends; |
| 336 | # else /* not _LIBC */ |
| 337 | wchar_t *range_starts; |
| 338 | wchar_t *range_ends; |
| 339 | # endif /* not _LIBC */ |
| 340 | |
| 341 | /* Character classes. */ |
| 342 | wctype_t *char_classes; |
| 343 | |
| 344 | /* If this character set is the non-matching list. */ |
| 345 | unsigned int non_match : 1; |
| 346 | |
| 347 | /* # of multibyte characters. */ |
| 348 | Idx nmbchars; |
| 349 | |
| 350 | /* # of collating symbols. */ |
| 351 | Idx ncoll_syms; |
| 352 | |
| 353 | /* # of equivalence classes. */ |
| 354 | Idx nequiv_classes; |
| 355 | |
| 356 | /* # of range expressions. */ |
| 357 | Idx nranges; |
| 358 | |
| 359 | /* # of character classes. */ |
| 360 | Idx nchar_classes; |
| 361 | } re_charset_t; |
| 362 | #endif /* RE_ENABLE_I18N */ |
| 363 | |
| 364 | typedef struct |
| 365 | { |
| 366 | union |
| 367 | { |
| 368 | unsigned char c; /* for CHARACTER */ |
| 369 | re_bitset_ptr_t sbcset; /* for SIMPLE_BRACKET */ |
| 370 | #ifdef RE_ENABLE_I18N |
| 371 | re_charset_t *mbcset; /* for COMPLEX_BRACKET */ |
| 372 | #endif /* RE_ENABLE_I18N */ |
| 373 | Idx idx; /* for BACK_REF */ |
| 374 | re_context_type ctx_type; /* for ANCHOR */ |
| 375 | } opr; |
| 376 | #if __GNUC__ >= 2 && !defined __STRICT_ANSI__ |
| 377 | re_token_type_t type : 8; |
| 378 | #else |
| 379 | re_token_type_t type; |
| 380 | #endif |
| 381 | unsigned int constraint : 10; /* context constraint */ |
| 382 | unsigned int duplicated : 1; |
| 383 | unsigned int opt_subexp : 1; |
| 384 | #ifdef RE_ENABLE_I18N |
| 385 | unsigned int accept_mb : 1; |
| 386 | /* These 2 bits can be moved into the union if needed (e.g. if running out |
| 387 | of bits; move opr.c to opr.c.c and move the flags to opr.c.flags). */ |
| 388 | unsigned int mb_partial : 1; |
| 389 | #endif |
| 390 | unsigned int word_char : 1; |
| 391 | } re_token_t; |
| 392 | |
| 393 | #define IS_EPSILON_NODE(type) ((type) & EPSILON_BIT) |
| 394 | |
| 395 | struct re_string_t |
| 396 | { |
| 397 | /* Indicate the raw buffer which is the original string passed as an |
| 398 | argument of regexec(), re_search(), etc.. */ |
| 399 | const unsigned char *raw_mbs; |
| 400 | /* Store the multibyte string. In case of "case insensitive mode" like |
| 401 | REG_ICASE, upper cases of the string are stored, otherwise MBS points |
| 402 | the same address that RAW_MBS points. */ |
| 403 | unsigned char *mbs; |
| 404 | #ifdef RE_ENABLE_I18N |
| 405 | /* Store the wide character string which is corresponding to MBS. */ |
| 406 | wint_t *wcs; |
| 407 | Idx *offsets; |
| 408 | mbstate_t cur_state; |
| 409 | #endif |
| 410 | /* Index in RAW_MBS. Each character mbs[i] corresponds to |
| 411 | raw_mbs[raw_mbs_idx + i]. */ |
| 412 | Idx raw_mbs_idx; |
| 413 | /* The length of the valid characters in the buffers. */ |
| 414 | Idx valid_len; |
| 415 | /* The corresponding number of bytes in raw_mbs array. */ |
| 416 | Idx valid_raw_len; |
| 417 | /* The length of the buffers MBS and WCS. */ |
| 418 | Idx bufs_len; |
| 419 | /* The index in MBS, which is updated by re_string_fetch_byte. */ |
| 420 | Idx cur_idx; |
| 421 | /* length of RAW_MBS array. */ |
| 422 | Idx raw_len; |
| 423 | /* This is RAW_LEN - RAW_MBS_IDX + VALID_LEN - VALID_RAW_LEN. */ |
| 424 | Idx len; |
| 425 | /* End of the buffer may be shorter than its length in the cases such |
| 426 | as re_match_2, re_search_2. Then, we use STOP for end of the buffer |
| 427 | instead of LEN. */ |
| 428 | Idx raw_stop; |
| 429 | /* This is RAW_STOP - RAW_MBS_IDX adjusted through OFFSETS. */ |
| 430 | Idx stop; |
| 431 | |
| 432 | /* The context of mbs[0]. We store the context independently, since |
| 433 | the context of mbs[0] may be different from raw_mbs[0], which is |
| 434 | the beginning of the input string. */ |
| 435 | unsigned int tip_context; |
| 436 | /* The translation passed as a part of an argument of re_compile_pattern. */ |
| 437 | RE_TRANSLATE_TYPE trans; |
| 438 | /* Copy of re_dfa_t's word_char. */ |
| 439 | re_const_bitset_ptr_t word_char; |
| 440 | /* true if REG_ICASE. */ |
| 441 | unsigned char icase; |
| 442 | unsigned char is_utf8; |
| 443 | unsigned char map_notascii; |
| 444 | unsigned char mbs_allocated; |
| 445 | unsigned char offsets_needed; |
| 446 | unsigned char newline_anchor; |
| 447 | unsigned char word_ops_used; |
| 448 | int mb_cur_max; |
| 449 | }; |
| 450 | typedef struct re_string_t re_string_t; |
| 451 | |
| 452 | |
| 453 | struct re_dfa_t; |
| 454 | typedef struct re_dfa_t re_dfa_t; |
| 455 | |
| 456 | #ifndef _LIBC |
| 457 | # define IS_IN(libc) false |
| 458 | #endif |
| 459 | |
| 460 | #define re_string_peek_byte(pstr, offset) \ |
| 461 | ((pstr)->mbs[(pstr)->cur_idx + offset]) |
| 462 | #define re_string_fetch_byte(pstr) \ |
| 463 | ((pstr)->mbs[(pstr)->cur_idx++]) |
| 464 | #define re_string_first_byte(pstr, idx) \ |
| 465 | ((idx) == (pstr)->valid_len || (pstr)->wcs[idx] != WEOF) |
| 466 | #define re_string_is_single_byte_char(pstr, idx) \ |
| 467 | ((pstr)->wcs[idx] != WEOF && ((pstr)->valid_len == (idx) + 1 \ |
| 468 | || (pstr)->wcs[(idx) + 1] != WEOF)) |
| 469 | #define re_string_eoi(pstr) ((pstr)->stop <= (pstr)->cur_idx) |
| 470 | #define re_string_cur_idx(pstr) ((pstr)->cur_idx) |
| 471 | #define re_string_get_buffer(pstr) ((pstr)->mbs) |
| 472 | #define re_string_length(pstr) ((pstr)->len) |
| 473 | #define re_string_byte_at(pstr,idx) ((pstr)->mbs[idx]) |
| 474 | #define re_string_skip_bytes(pstr,idx) ((pstr)->cur_idx += (idx)) |
| 475 | #define re_string_set_index(pstr,idx) ((pstr)->cur_idx = (idx)) |
| 476 | |
| 477 | #if defined _LIBC || HAVE_ALLOCA |
| 478 | # include <alloca.h> |
| 479 | #endif |
| 480 | |
| 481 | #ifndef _LIBC |
| 482 | # if HAVE_ALLOCA |
| 483 | /* The OS usually guarantees only one guard page at the bottom of the stack, |
| 484 | and a page size can be as small as 4096 bytes. So we cannot safely |
| 485 | allocate anything larger than 4096 bytes. Also care for the possibility |
| 486 | of a few compiler-allocated temporary stack slots. */ |
| 487 | # define __libc_use_alloca(n) ((n) < 4032) |
| 488 | # else |
| 489 | /* alloca is implemented with malloc, so just use malloc. */ |
| 490 | # define __libc_use_alloca(n) 0 |
| 491 | # undef alloca |
| 492 | # define alloca(n) malloc (n) |
| 493 | # endif |
| 494 | #endif |
| 495 | |
| 496 | #ifdef _LIBC |
| 497 | # define MALLOC_0_IS_NONNULL 1 |
| 498 | #elif !defined MALLOC_0_IS_NONNULL |
| 499 | # define MALLOC_0_IS_NONNULL 0 |
| 500 | #endif |
| 501 | |
| 502 | #ifndef MAX |
| 503 | # define MAX(a,b) ((a) < (b) ? (b) : (a)) |
| 504 | #endif |
| 505 | #ifndef MIN |
| 506 | # define MIN(a,b) ((a) < (b) ? (a) : (b)) |
| 507 | #endif |
| 508 | |
| 509 | #define re_malloc(t,n) ((t *) malloc ((n) * sizeof (t))) |
| 510 | #define re_realloc(p,t,n) ((t *) realloc (p, (n) * sizeof (t))) |
| 511 | #define re_free(p) free (p) |
| 512 | |
| 513 | struct bin_tree_t |
| 514 | { |
| 515 | struct bin_tree_t *parent; |
| 516 | struct bin_tree_t *left; |
| 517 | struct bin_tree_t *right; |
| 518 | struct bin_tree_t *first; |
| 519 | struct bin_tree_t *next; |
| 520 | |
| 521 | re_token_t token; |
| 522 | |
| 523 | /* 'node_idx' is the index in dfa->nodes, if 'type' == 0. |
| 524 | Otherwise 'type' indicate the type of this node. */ |
| 525 | Idx node_idx; |
| 526 | }; |
| 527 | typedef struct bin_tree_t bin_tree_t; |
| 528 | |
| 529 | #define BIN_TREE_STORAGE_SIZE \ |
| 530 | ((1024 - sizeof (void *)) / sizeof (bin_tree_t)) |
| 531 | |
| 532 | struct bin_tree_storage_t |
| 533 | { |
| 534 | struct bin_tree_storage_t *next; |
| 535 | bin_tree_t data[BIN_TREE_STORAGE_SIZE]; |
| 536 | }; |
| 537 | typedef struct bin_tree_storage_t bin_tree_storage_t; |
| 538 | |
| 539 | #define CONTEXT_WORD 1 |
| 540 | #define CONTEXT_NEWLINE (CONTEXT_WORD << 1) |
| 541 | #define CONTEXT_BEGBUF (CONTEXT_NEWLINE << 1) |
| 542 | #define CONTEXT_ENDBUF (CONTEXT_BEGBUF << 1) |
| 543 | |
| 544 | #define IS_WORD_CONTEXT(c) ((c) & CONTEXT_WORD) |
| 545 | #define IS_NEWLINE_CONTEXT(c) ((c) & CONTEXT_NEWLINE) |
| 546 | #define IS_BEGBUF_CONTEXT(c) ((c) & CONTEXT_BEGBUF) |
| 547 | #define IS_ENDBUF_CONTEXT(c) ((c) & CONTEXT_ENDBUF) |
| 548 | #define IS_ORDINARY_CONTEXT(c) ((c) == 0) |
| 549 | |
| 550 | #define IS_WORD_CHAR(ch) (isalnum (ch) || (ch) == '_') |
| 551 | #define IS_NEWLINE(ch) ((ch) == NEWLINE_CHAR) |
| 552 | #define IS_WIDE_WORD_CHAR(ch) (__iswalnum (ch) || (ch) == L'_') |
| 553 | #define IS_WIDE_NEWLINE(ch) ((ch) == WIDE_NEWLINE_CHAR) |
| 554 | |
| 555 | #define NOT_SATISFY_PREV_CONSTRAINT(constraint,context) \ |
| 556 | ((((constraint) & PREV_WORD_CONSTRAINT) && !IS_WORD_CONTEXT (context)) \ |
| 557 | || ((constraint & PREV_NOTWORD_CONSTRAINT) && IS_WORD_CONTEXT (context)) \ |
| 558 | || ((constraint & PREV_NEWLINE_CONSTRAINT) && !IS_NEWLINE_CONTEXT (context))\ |
| 559 | || ((constraint & PREV_BEGBUF_CONSTRAINT) && !IS_BEGBUF_CONTEXT (context))) |
| 560 | |
| 561 | #define NOT_SATISFY_NEXT_CONSTRAINT(constraint,context) \ |
| 562 | ((((constraint) & NEXT_WORD_CONSTRAINT) && !IS_WORD_CONTEXT (context)) \ |
| 563 | || (((constraint) & NEXT_NOTWORD_CONSTRAINT) && IS_WORD_CONTEXT (context)) \ |
| 564 | || (((constraint) & NEXT_NEWLINE_CONSTRAINT) && !IS_NEWLINE_CONTEXT (context)) \ |
| 565 | || (((constraint) & NEXT_ENDBUF_CONSTRAINT) && !IS_ENDBUF_CONTEXT (context))) |
| 566 | |
| 567 | struct re_dfastate_t |
| 568 | { |
| 569 | re_hashval_t hash; |
| 570 | re_node_set nodes; |
| 571 | re_node_set non_eps_nodes; |
| 572 | re_node_set inveclosure; |
| 573 | re_node_set *entrance_nodes; |
| 574 | struct re_dfastate_t **trtable, **word_trtable; |
| 575 | unsigned int context : 4; |
| 576 | unsigned int halt : 1; |
| 577 | /* If this state can accept "multi byte". |
| 578 | Note that we refer to multibyte characters, and multi character |
| 579 | collating elements as "multi byte". */ |
| 580 | unsigned int accept_mb : 1; |
| 581 | /* If this state has backreference node(s). */ |
| 582 | unsigned int has_backref : 1; |
| 583 | unsigned int has_constraint : 1; |
| 584 | }; |
| 585 | typedef struct re_dfastate_t re_dfastate_t; |
| 586 | |
| 587 | struct re_state_table_entry |
| 588 | { |
| 589 | Idx num; |
| 590 | Idx alloc; |
| 591 | re_dfastate_t **array; |
| 592 | }; |
| 593 | |
| 594 | /* Array type used in re_sub_match_last_t and re_sub_match_top_t. */ |
| 595 | |
| 596 | typedef struct |
| 597 | { |
| 598 | Idx next_idx; |
| 599 | Idx alloc; |
| 600 | re_dfastate_t **array; |
| 601 | } state_array_t; |
| 602 | |
| 603 | /* Store information about the node NODE whose type is OP_CLOSE_SUBEXP. */ |
| 604 | |
| 605 | typedef struct |
| 606 | { |
| 607 | Idx node; |
| 608 | Idx str_idx; /* The position NODE match at. */ |
| 609 | state_array_t path; |
| 610 | } re_sub_match_last_t; |
| 611 | |
| 612 | /* Store information about the node NODE whose type is OP_OPEN_SUBEXP. |
| 613 | And information about the node, whose type is OP_CLOSE_SUBEXP, |
| 614 | corresponding to NODE is stored in LASTS. */ |
| 615 | |
| 616 | typedef struct |
| 617 | { |
| 618 | Idx str_idx; |
| 619 | Idx node; |
| 620 | state_array_t *path; |
| 621 | Idx alasts; /* Allocation size of LASTS. */ |
| 622 | Idx nlasts; /* The number of LASTS. */ |
| 623 | re_sub_match_last_t **lasts; |
| 624 | } re_sub_match_top_t; |
| 625 | |
| 626 | struct re_backref_cache_entry |
| 627 | { |
| 628 | Idx node; |
| 629 | Idx str_idx; |
| 630 | Idx subexp_from; |
| 631 | Idx subexp_to; |
| 632 | char more; |
| 633 | char unused; |
| 634 | unsigned short int eps_reachable_subexps_map; |
| 635 | }; |
| 636 | |
| 637 | typedef struct |
| 638 | { |
| 639 | /* The string object corresponding to the input string. */ |
| 640 | re_string_t input; |
| 641 | #if defined _LIBC || (defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L) |
| 642 | const re_dfa_t *const dfa; |
| 643 | #else |
| 644 | const re_dfa_t *dfa; |
| 645 | #endif |
| 646 | /* EFLAGS of the argument of regexec. */ |
| 647 | int eflags; |
| 648 | /* Where the matching ends. */ |
| 649 | Idx match_last; |
| 650 | Idx last_node; |
| 651 | /* The state log used by the matcher. */ |
| 652 | re_dfastate_t **state_log; |
| 653 | Idx state_log_top; |
| 654 | /* Back reference cache. */ |
| 655 | Idx nbkref_ents; |
| 656 | Idx abkref_ents; |
| 657 | struct re_backref_cache_entry *bkref_ents; |
| 658 | int max_mb_elem_len; |
| 659 | Idx nsub_tops; |
| 660 | Idx asub_tops; |
| 661 | re_sub_match_top_t **sub_tops; |
| 662 | } re_match_context_t; |
| 663 | |
| 664 | typedef struct |
| 665 | { |
| 666 | re_dfastate_t **sifted_states; |
| 667 | re_dfastate_t **limited_states; |
| 668 | Idx last_node; |
| 669 | Idx last_str_idx; |
| 670 | re_node_set limits; |
| 671 | } re_sift_context_t; |
| 672 | |
| 673 | struct re_fail_stack_ent_t |
| 674 | { |
| 675 | Idx idx; |
| 676 | Idx node; |
| 677 | regmatch_t *regs; |
| 678 | re_node_set eps_via_nodes; |
| 679 | }; |
| 680 | |
| 681 | struct re_fail_stack_t |
| 682 | { |
| 683 | Idx num; |
| 684 | Idx alloc; |
| 685 | struct re_fail_stack_ent_t *stack; |
| 686 | }; |
| 687 | |
| 688 | struct re_dfa_t |
| 689 | { |
| 690 | re_token_t *nodes; |
| 691 | size_t nodes_alloc; |
| 692 | size_t nodes_len; |
| 693 | Idx *nexts; |
| 694 | Idx *org_indices; |
| 695 | re_node_set *edests; |
| 696 | re_node_set *eclosures; |
| 697 | re_node_set *inveclosures; |
| 698 | struct re_state_table_entry *state_table; |
| 699 | re_dfastate_t *init_state; |
| 700 | re_dfastate_t *init_state_word; |
| 701 | re_dfastate_t *init_state_nl; |
| 702 | re_dfastate_t *init_state_begbuf; |
| 703 | bin_tree_t *str_tree; |
| 704 | bin_tree_storage_t *str_tree_storage; |
| 705 | re_bitset_ptr_t sb_char; |
| 706 | int str_tree_storage_idx; |
| 707 | |
| 708 | /* number of subexpressions 're_nsub' is in regex_t. */ |
| 709 | re_hashval_t state_hash_mask; |
| 710 | Idx init_node; |
| 711 | Idx nbackref; /* The number of backreference in this dfa. */ |
| 712 | |
| 713 | /* Bitmap expressing which backreference is used. */ |
| 714 | bitset_word_t used_bkref_map; |
| 715 | bitset_word_t completed_bkref_map; |
| 716 | |
| 717 | unsigned int has_plural_match : 1; |
| 718 | /* If this dfa has "multibyte node", which is a backreference or |
| 719 | a node which can accept multibyte character or multi character |
| 720 | collating element. */ |
| 721 | unsigned int has_mb_node : 1; |
| 722 | unsigned int is_utf8 : 1; |
| 723 | unsigned int map_notascii : 1; |
| 724 | unsigned int word_ops_used : 1; |
| 725 | int mb_cur_max; |
| 726 | bitset_t word_char; |
| 727 | reg_syntax_t syntax; |
| 728 | Idx *subexp_map; |
| 729 | #ifdef DEBUG |
| 730 | char* re_str; |
| 731 | #endif |
| 732 | lock_define (lock) |
| 733 | }; |
| 734 | |
| 735 | #define re_node_set_init_empty(set) memset (set, '\0', sizeof (re_node_set)) |
| 736 | #define re_node_set_remove(set,id) \ |
| 737 | (re_node_set_remove_at (set, re_node_set_contains (set, id) - 1)) |
| 738 | #define re_node_set_empty(p) ((p)->nelem = 0) |
| 739 | #define re_node_set_free(set) re_free ((set)->elems) |
| 740 | |
| 741 | |
| 742 | typedef enum |
| 743 | { |
| 744 | SB_CHAR, |
| 745 | MB_CHAR, |
| 746 | EQUIV_CLASS, |
| 747 | COLL_SYM, |
| 748 | CHAR_CLASS |
| 749 | } bracket_elem_type; |
| 750 | |
| 751 | typedef struct |
| 752 | { |
| 753 | bracket_elem_type type; |
| 754 | union |
| 755 | { |
| 756 | unsigned char ch; |
| 757 | unsigned char *name; |
| 758 | wchar_t wch; |
| 759 | } opr; |
| 760 | } bracket_elem_t; |
| 761 | |
| 762 | |
| 763 | /* Functions for bitset_t operation. */ |
| 764 | |
| 765 | static inline void |
| 766 | bitset_set (bitset_t set, Idx i) |
| 767 | { |
| 768 | set[i / BITSET_WORD_BITS] |= (bitset_word_t) 1 << i % BITSET_WORD_BITS; |
| 769 | } |
| 770 | |
| 771 | static inline void |
| 772 | bitset_clear (bitset_t set, Idx i) |
| 773 | { |
| 774 | set[i / BITSET_WORD_BITS] &= ~ ((bitset_word_t) 1 << i % BITSET_WORD_BITS); |
| 775 | } |
| 776 | |
| 777 | static inline bool |
| 778 | bitset_contain (const bitset_t set, Idx i) |
| 779 | { |
| 780 | return (set[i / BITSET_WORD_BITS] >> i % BITSET_WORD_BITS) & 1; |
| 781 | } |
| 782 | |
| 783 | static inline void |
| 784 | bitset_empty (bitset_t set) |
| 785 | { |
| 786 | memset (set, '\0', sizeof (bitset_t)); |
| 787 | } |
| 788 | |
| 789 | static inline void |
| 790 | bitset_set_all (bitset_t set) |
| 791 | { |
| 792 | memset (set, -1, sizeof (bitset_word_t) * (SBC_MAX / BITSET_WORD_BITS)); |
| 793 | if (SBC_MAX % BITSET_WORD_BITS != 0) |
| 794 | set[BITSET_WORDS - 1] = |
| 795 | ((bitset_word_t) 1 << SBC_MAX % BITSET_WORD_BITS) - 1; |
| 796 | } |
| 797 | |
| 798 | static inline void |
| 799 | bitset_copy (bitset_t dest, const bitset_t src) |
| 800 | { |
| 801 | memcpy (dest, src, sizeof (bitset_t)); |
| 802 | } |
| 803 | |
| 804 | static inline void |
| 805 | bitset_not (bitset_t set) |
| 806 | { |
| 807 | int bitset_i; |
| 808 | for (bitset_i = 0; bitset_i < SBC_MAX / BITSET_WORD_BITS; ++bitset_i) |
| 809 | set[bitset_i] = ~set[bitset_i]; |
| 810 | if (SBC_MAX % BITSET_WORD_BITS != 0) |
| 811 | set[BITSET_WORDS - 1] = |
| 812 | ((((bitset_word_t) 1 << SBC_MAX % BITSET_WORD_BITS) - 1) |
| 813 | & ~set[BITSET_WORDS - 1]); |
| 814 | } |
| 815 | |
| 816 | static inline void |
| 817 | bitset_merge (bitset_t dest, const bitset_t src) |
| 818 | { |
| 819 | int bitset_i; |
| 820 | for (bitset_i = 0; bitset_i < BITSET_WORDS; ++bitset_i) |
| 821 | dest[bitset_i] |= src[bitset_i]; |
| 822 | } |
| 823 | |
| 824 | static inline void |
| 825 | bitset_mask (bitset_t dest, const bitset_t src) |
| 826 | { |
| 827 | int bitset_i; |
| 828 | for (bitset_i = 0; bitset_i < BITSET_WORDS; ++bitset_i) |
| 829 | dest[bitset_i] &= src[bitset_i]; |
| 830 | } |
| 831 | |
| 832 | #ifdef RE_ENABLE_I18N |
| 833 | /* Functions for re_string. */ |
| 834 | static int |
| 835 | __attribute__ ((pure, unused)) |
| 836 | re_string_char_size_at (const re_string_t *pstr, Idx idx) |
| 837 | { |
| 838 | int byte_idx; |
| 839 | if (pstr->mb_cur_max == 1) |
| 840 | return 1; |
| 841 | for (byte_idx = 1; idx + byte_idx < pstr->valid_len; ++byte_idx) |
| 842 | if (pstr->wcs[idx + byte_idx] != WEOF) |
| 843 | break; |
| 844 | return byte_idx; |
| 845 | } |
| 846 | |
| 847 | static wint_t |
| 848 | __attribute__ ((pure, unused)) |
| 849 | re_string_wchar_at (const re_string_t *pstr, Idx idx) |
| 850 | { |
| 851 | if (pstr->mb_cur_max == 1) |
| 852 | return (wint_t) pstr->mbs[idx]; |
| 853 | return (wint_t) pstr->wcs[idx]; |
| 854 | } |
| 855 | |
| 856 | # ifdef _LIBC |
| 857 | # include <locale/weight.h> |
| 858 | # endif |
| 859 | |
| 860 | static int |
| 861 | __attribute__ ((pure, unused)) |
| 862 | re_string_elem_size_at (const re_string_t *pstr, Idx idx) |
| 863 | { |
| 864 | # ifdef _LIBC |
| 865 | const unsigned char *p, *; |
| 866 | const int32_t *table, *indirect; |
| 867 | uint_fast32_t nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES); |
| 868 | |
| 869 | if (nrules != 0) |
| 870 | { |
| 871 | table = (const int32_t *) _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEMB); |
| 872 | extra = (const unsigned char *) |
| 873 | _NL_CURRENT (LC_COLLATE, _NL_COLLATE_EXTRAMB); |
| 874 | indirect = (const int32_t *) _NL_CURRENT (LC_COLLATE, |
| 875 | _NL_COLLATE_INDIRECTMB); |
| 876 | p = pstr->mbs + idx; |
| 877 | findidx (table, indirect, extra, &p, pstr->len - idx); |
| 878 | return p - pstr->mbs - idx; |
| 879 | } |
| 880 | else |
| 881 | # endif /* _LIBC */ |
| 882 | return 1; |
| 883 | } |
| 884 | #endif /* RE_ENABLE_I18N */ |
| 885 | |
| 886 | #ifndef __GNUC_PREREQ |
| 887 | # if defined __GNUC__ && defined __GNUC_MINOR__ |
| 888 | # define __GNUC_PREREQ(maj, min) \ |
| 889 | ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min)) |
| 890 | # else |
| 891 | # define __GNUC_PREREQ(maj, min) 0 |
| 892 | # endif |
| 893 | #endif |
| 894 | |
| 895 | #if __GNUC_PREREQ (3,4) |
| 896 | # undef __attribute_warn_unused_result__ |
| 897 | # define __attribute_warn_unused_result__ \ |
| 898 | __attribute__ ((__warn_unused_result__)) |
| 899 | #else |
| 900 | # define __attribute_warn_unused_result__ /* empty */ |
| 901 | #endif |
| 902 | |
| 903 | #ifndef FALLTHROUGH |
| 904 | # if __GNUC__ < 7 |
| 905 | # define FALLTHROUGH ((void) 0) |
| 906 | # else |
| 907 | # define FALLTHROUGH __attribute__ ((__fallthrough__)) |
| 908 | # endif |
| 909 | #endif |
| 910 | |
| 911 | #endif /* _REGEX_INTERNAL_H */ |
| 912 | |