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