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