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