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