Debian Codegraph @

lib/regex_internal.h

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