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