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