Debian Codegraph @

texk/web2c/pdftexdir/regex/regex_internal.h

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