Ruby 4.1.0dev (2026-09-26 revision 227006291d35b2255b3f7a888d75102c129b0472)
regint.h (227006291d35b2255b3f7a888d75102c129b0472)
1#ifndef ONIGMO_REGINT_H
2#define ONIGMO_REGINT_H
3/**********************************************************************
4 regint.h - Onigmo (Oniguruma-mod) (regular expression library)
5**********************************************************************/
6/*-
7 * Copyright (c) 2002-2013 K.Kosako <sndgk393 AT ybb DOT ne DOT jp>
8 * Copyright (c) 2011-2019 K.Takata <kentkt AT csc DOT jp>
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33/* for debug */
34/* #define ONIG_DEBUG_PARSE_TREE */
35/* #define ONIG_DEBUG_COMPILE */
36/* #define ONIG_DEBUG_SEARCH */
37/* #define ONIG_DEBUG_MATCH */
38/* #define ONIG_DEBUG_MATCH_CACHE */
39/* #define ONIG_DEBUG_MEMLEAK */
40/* #define ONIG_DONT_OPTIMIZE */
41
42/* for byte-code statistical data. */
43/* #define ONIG_DEBUG_STATISTICS */
44
45/* enable the match optimization by using a cache. */
46#define USE_MATCH_CACHE
47
48#if defined(ONIG_DEBUG_PARSE_TREE) || defined(ONIG_DEBUG_MATCH) || \
49 defined(ONIG_DEBUG_SEARCH) || defined(ONIG_DEBUG_COMPILE) || \
50 defined(ONIG_DEBUG_STATISTICS) || defined(ONIG_DEBUG_MEMLEAK)
51# ifndef ONIG_DEBUG
52# define ONIG_DEBUG
53# endif
54#endif
55
56/* __POWERPC__ added to accommodate Darwin case. */
57#ifndef UNALIGNED_WORD_ACCESS
58# if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \
59 defined(__x86_64) || defined(__x86_64__) || defined(_M_AMD64) || \
60 defined(__powerpc64__) || defined(__POWERPC__) || defined(__aarch64__) || \
61 defined(__mc68020__)
62# define UNALIGNED_WORD_ACCESS 1
63# else
64# define UNALIGNED_WORD_ACCESS 0
65# endif
66#endif
67
68#if UNALIGNED_WORD_ACCESS
69# define PLATFORM_UNALIGNED_WORD_ACCESS
70#endif
71
72/* config */
73/* spec. config */
74#define USE_NAMED_GROUP
75#define USE_SUBEXP_CALL
76#define USE_PERL_SUBEXP_CALL
77#define USE_CAPITAL_P_NAMED_GROUP
78#define USE_BACKREF_WITH_LEVEL /* \k<name+n>, \k<name-n> */
79#define USE_MONOMANIAC_CHECK_CAPTURES_IN_ENDLESS_REPEAT /* /(?:()|())*\2/ */
80#define USE_NEWLINE_AT_END_OF_STRING_HAS_EMPTY_LINE /* /\n$/ =~ "\n" */
81#define USE_WARNING_REDUNDANT_NESTED_REPEAT_OPERATOR
82/* !!! moved to regenc.h. */ /* #define USE_CRNL_AS_LINE_TERMINATOR */
83#define USE_NO_INVALID_QUANTIFIER
84
85/* internal config */
86/* #define USE_OP_PUSH_OR_JUMP_EXACT */
87#define USE_QTFR_PEEK_NEXT
88#define USE_ST_LIBRARY
89
90#define INIT_MATCH_STACK_SIZE 160
91#define DEFAULT_MATCH_STACK_LIMIT_SIZE 0 /* unlimited */
92#define DEFAULT_PARSE_DEPTH_LIMIT 4096
93
94#define OPT_EXACT_MAXLEN 24 /* This must be smaller than ONIG_CHAR_TABLE_SIZE. */
95
96/* check config */
97#if defined(USE_PERL_SUBEXP_CALL) || defined(USE_CAPITAL_P_NAMED_GROUP)
98# if !defined(USE_NAMED_GROUP) || !defined(USE_SUBEXP_CALL)
99# error USE_NAMED_GROUP and USE_SUBEXP_CALL must be defined.
100# endif
101#endif
102
103#if defined(__GNUC__)
104# define ARG_UNUSED __attribute__ ((unused))
105#else
106# define ARG_UNUSED
107#endif
108
109#if !defined(RUBY) && defined(RUBY_EXPORT)
110# define RUBY
111#endif
112#ifdef RUBY
113# ifndef RUBY_DEFINES_H
114# include "ruby/ruby.h"
115# undef xmalloc
116# undef xrealloc
117# undef xcalloc
118# undef xfree
119# endif
120#else /* RUBY */
121# include "config.h"
122# if SIZEOF_LONG_LONG > 0
123# define LONG_LONG long long
124# endif
125#endif /* RUBY */
126
127#include <stdarg.h>
128
129/* */
130/* escape other system UChar definition */
131#ifdef ONIG_ESCAPE_UCHAR_COLLISION
132# undef ONIG_ESCAPE_UCHAR_COLLISION
133#endif
134
135#define USE_WORD_BEGIN_END /* "<": word-begin, ">": word-end */
136#ifdef RUBY
137# undef USE_CAPTURE_HISTORY
138#else
139# define USE_CAPTURE_HISTORY
140#endif
141#define USE_VARIABLE_META_CHARS
142#define USE_FIND_LONGEST_SEARCH_ALL_OF_RANGE
143/* #define USE_COMBINATION_EXPLOSION_CHECK */ /* (X*)* */
144
145
146#ifndef xmalloc
147# define xmalloc malloc
148# define xrealloc realloc
149# define xcalloc calloc
150# define xfree free
151#endif
152
153#ifdef RUBY
154
155# define CHECK_INTERRUPT_IN_MATCH_AT do { \
156 msa->counter++; \
157 if (msa->counter >= 128) { \
158 msa->counter = 0; \
159 if (rb_reg_timeout_p(reg, &msa->end_time)) { \
160 goto timeout; \
161 } \
162 rb_thread_check_ints(); \
163 } \
164} while(0)
165# define onig_st_init_table st_init_table
166# define onig_st_init_table_with_size st_init_table_with_size
167# define onig_st_init_numtable st_init_numtable
168# define onig_st_init_numtable_with_size st_init_numtable_with_size
169# define onig_st_init_strtable st_init_strtable
170# define onig_st_init_strtable_with_size st_init_strtable_with_size
171# define onig_st_delete st_delete
172# define onig_st_delete_safe st_delete_safe
173# define onig_st_insert st_insert
174# define onig_st_lookup st_lookup
175# define onig_st_foreach st_foreach
176# define onig_st_add_direct st_add_direct
177# define onig_st_free_table st_free_table
178# define onig_st_cleanup_safe st_cleanup_safe
179# define onig_st_copy st_copy
180# define onig_st_nothing_key_clone st_nothing_key_clone
181# define onig_st_nothing_key_free st_nothing_key_free
182# define onig_st_is_member st_is_member
183
184# define USE_UPPER_CASE_TABLE
185#else /* RUBY */
186
187# define CHECK_INTERRUPT_IN_MATCH_AT
188
189# define st_init_table onig_st_init_table
190# define st_init_table_with_size onig_st_init_table_with_size
191# define st_init_numtable onig_st_init_numtable
192# define st_init_numtable_with_size onig_st_init_numtable_with_size
193# define st_init_strtable onig_st_init_strtable
194# define st_init_strtable_with_size onig_st_init_strtable_with_size
195# define st_delete onig_st_delete
196# define st_delete_safe onig_st_delete_safe
197# define st_insert onig_st_insert
198# define st_lookup onig_st_lookup
199# define st_foreach onig_st_foreach
200# define st_add_direct onig_st_add_direct
201# define st_free_table onig_st_free_table
202# define st_cleanup_safe onig_st_cleanup_safe
203# define st_copy onig_st_copy
204# define st_nothing_key_clone onig_st_nothing_key_clone
205# define st_nothing_key_free onig_st_nothing_key_free
206/* */
207# define onig_st_is_member st_is_member
208
209#endif /* RUBY */
210
211#define STATE_CHECK_STRING_THRESHOLD_LEN 7
212#define STATE_CHECK_BUFF_MAX_SIZE 0x4000
213
214#define xmemset memset
215#define xmemcpy memcpy
216#define xmemmove memmove
217
218#if defined(_WIN32) && !defined(__GNUC__)
219# define xalloca _alloca
220# define xvsnprintf(buf,size,fmt,args) _vsnprintf_s(buf,size,_TRUNCATE,fmt,args)
221# define xsnprintf sprintf_s
222# define xstrcat(dest,src,size) strcat_s(dest,size,src)
223#else
224# define xalloca alloca
225# define xvsnprintf vsnprintf
226# define xsnprintf snprintf
227# define xstrcat(dest,src,size) strcat(dest,src)
228#endif
229
230#if defined(ONIG_DEBUG_MEMLEAK) && defined(_MSC_VER)
231# define _CRTDBG_MAP_ALLOC
232# include <malloc.h>
233# include <crtdbg.h>
234#endif
235
236#include <stdlib.h>
237
238#if defined(HAVE_ALLOCA_H) && (defined(_AIX) || !defined(__GNUC__))
239# include <alloca.h>
240#endif
241
242#include <string.h>
243
244#include <ctype.h>
245#ifdef HAVE_SYS_TYPES_H
246# include <sys/types.h>
247#endif
248
249#ifdef HAVE_STDINT_H
250# include <stdint.h>
251#endif
252
253#ifdef HAVE_INTTYPES_H
254# include <inttypes.h>
255#endif
256
257#include <stddef.h>
258
259#ifdef _WIN32
260# include <malloc.h> /* for alloca() */
261#endif
262
263#ifdef ONIG_DEBUG
264# include <stdio.h>
265#endif
266
267#ifndef PRIdPTRDIFF
268# define PRIdPTRDIFF PRIdPTR
269#endif
270
271#include "regenc.h"
272
273RUBY_SYMBOL_EXPORT_BEGIN
274
275#ifdef MIN
276# undef MIN
277#endif
278#ifdef MAX
279# undef MAX
280#endif
281#define MIN(a,b) (((a)>(b))?(b):(a))
282#define MAX(a,b) (((a)<(b))?(b):(a))
283
284#define IS_NULL(p) (((void*)(p)) == (void*)0)
285#define IS_NOT_NULL(p) (((void*)(p)) != (void*)0)
286#define CHECK_NULL_RETURN(p) if (IS_NULL(p)) return NULL
287#define CHECK_NULL_RETURN_MEMERR(p) if (IS_NULL(p)) return ONIGERR_MEMORY
288#define NULL_UCHARP ((UChar* )0)
289
290#define ONIG_LAST_CODE_POINT (~((OnigCodePoint )0))
291
292#define PLATFORM_GET_INC_ARGUMENTS_ASSERT(val, type) \
293 ((void)sizeof(char[2 * (sizeof(val) == sizeof(type)) - 1]))
294
295#ifdef PLATFORM_UNALIGNED_WORD_ACCESS
296
297# define PLATFORM_GET_INC(val,p,type) do{\
298 PLATFORM_GET_INC_ARGUMENTS_ASSERT(val, type);\
299 val = *(type* )p;\
300 (p) += sizeof(type);\
301} while(0)
302
303#else
304
305# define PLATFORM_GET_INC(val,p,type) do{\
306 PLATFORM_GET_INC_ARGUMENTS_ASSERT(val, type);\
307 type platform_get_value;\
308 xmemcpy(&platform_get_value, (p), sizeof(type));\
309 val = platform_get_value;\
310 (p) += sizeof(type);\
311} while(0)
312
313/* sizeof(OnigCodePoint) */
314# define WORD_ALIGNMENT_SIZE SIZEOF_LONG
315
316# define GET_ALIGNMENT_PAD_SIZE(addr,pad_size) do {\
317 (pad_size) = WORD_ALIGNMENT_SIZE \
318 - ((uintptr_t )(addr) % WORD_ALIGNMENT_SIZE);\
319 if ((pad_size) == WORD_ALIGNMENT_SIZE) (pad_size) = 0;\
320} while (0)
321
322# define ALIGNMENT_RIGHT(addr) do {\
323 (addr) += (WORD_ALIGNMENT_SIZE - 1);\
324 (addr) -= ((uintptr_t )(addr) % WORD_ALIGNMENT_SIZE);\
325} while (0)
326
327#endif /* PLATFORM_UNALIGNED_WORD_ACCESS */
328
329/* stack pop level */
330#define STACK_POP_LEVEL_FREE 0
331#define STACK_POP_LEVEL_MEM_START 1
332#define STACK_POP_LEVEL_ALL 2
333
334/* optimize flags */
335#define ONIG_OPTIMIZE_NONE 0
336#define ONIG_OPTIMIZE_EXACT 1 /* Slow Search */
337#define ONIG_OPTIMIZE_EXACT_BM 2 /* Boyer Moore Search */
338#define ONIG_OPTIMIZE_EXACT_BM_NOT_REV 3 /* BM (applied to a multibyte string) */
339#define ONIG_OPTIMIZE_EXACT_IC 4 /* Slow Search (ignore case) */
340#define ONIG_OPTIMIZE_MAP 5 /* char map */
341#define ONIG_OPTIMIZE_EXACT_BM_IC 6 /* BM (ignore case) */
342#define ONIG_OPTIMIZE_EXACT_BM_NOT_REV_IC 7 /* BM (applied to a multibyte string) (ignore case) */
343
344/* bit status */
345typedef unsigned int BitStatusType;
346
347#define BIT_STATUS_BITS_NUM (sizeof(BitStatusType) * 8)
348#define BIT_STATUS_CLEAR(stats) (stats) = 0
349#define BIT_STATUS_ON_ALL(stats) (stats) = ~((BitStatusType )0)
350#define BIT_STATUS_AT(stats,n) \
351 ((n) < (int )BIT_STATUS_BITS_NUM ? ((stats) & ((BitStatusType )1 << n)) : ((stats) & 1))
352
353#define BIT_STATUS_ON_AT(stats,n) do {\
354 if ((n) < (int )BIT_STATUS_BITS_NUM)\
355 (stats) |= (1 << (n));\
356 else\
357 (stats) |= 1;\
358} while (0)
359
360#define BIT_STATUS_ON_AT_SIMPLE(stats,n) do {\
361 if ((n) < (int )BIT_STATUS_BITS_NUM)\
362 (stats) |= (1 << (n));\
363} while (0)
364
365
366#define INT_MAX_LIMIT ((1UL << (SIZEOF_INT * 8 - 1)) - 1)
367#define LONG_MAX_LIMIT ((1UL << (SIZEOF_LONG * 8 - 1)) - 1)
368
369/* Program offsets are RelAddrType, and the compile_length_*() functions sum
370 into int, so a larger program would wrap both. The quarter leaves room
371 for the sums the offset arithmetic makes out of those lengths. */
372#define MAX_COMPILED_PROGRAM_SIZE ((unsigned int )(INT_MAX_LIMIT / 4))
373
374#define DIGITVAL(code) ((code) - '0')
375#define ODIGITVAL(code) DIGITVAL(code)
376#define XDIGITVAL(enc,code) \
377 (ONIGENC_IS_CODE_DIGIT(enc,code) ? DIGITVAL(code) \
378 : (ONIGENC_IS_CODE_UPPER(enc,code) ? (code) - 'A' + 10 : (code) - 'a' + 10))
379
380#define IS_SINGLELINE(option) ((option) & ONIG_OPTION_SINGLELINE)
381#define IS_MULTILINE(option) ((option) & ONIG_OPTION_MULTILINE)
382#define IS_IGNORECASE(option) ((option) & ONIG_OPTION_IGNORECASE)
383#define IS_EXTEND(option) ((option) & ONIG_OPTION_EXTEND)
384#define IS_FIND_LONGEST(option) ((option) & ONIG_OPTION_FIND_LONGEST)
385#define IS_FIND_NOT_EMPTY(option) ((option) & ONIG_OPTION_FIND_NOT_EMPTY)
386#define IS_FIND_CONDITION(option) ((option) & \
387 (ONIG_OPTION_FIND_LONGEST | ONIG_OPTION_FIND_NOT_EMPTY))
388#define IS_NOTBOL(option) ((option) & ONIG_OPTION_NOTBOL)
389#define IS_NOTEOL(option) ((option) & ONIG_OPTION_NOTEOL)
390#define IS_NOTBOS(option) ((option) & ONIG_OPTION_NOTBOS)
391#define IS_NOTEOS(option) ((option) & ONIG_OPTION_NOTEOS)
392#define IS_ASCII_RANGE(option) ((option) & ONIG_OPTION_ASCII_RANGE)
393#define IS_POSIX_BRACKET_ALL_RANGE(option) ((option) & ONIG_OPTION_POSIX_BRACKET_ALL_RANGE)
394#define IS_WORD_BOUND_ALL_RANGE(option) ((option) & ONIG_OPTION_WORD_BOUND_ALL_RANGE)
395#define IS_NEWLINE_CRLF(option) ((option) & ONIG_OPTION_NEWLINE_CRLF)
396
397/* OP_SET_OPTION is required for these options.
398#define IS_DYNAMIC_OPTION(option) \
399 (((option) & (ONIG_OPTION_MULTILINE | ONIG_OPTION_IGNORECASE)) != 0)
400*/
401/* ignore-case and multibyte status are included in compiled code. */
402#define IS_DYNAMIC_OPTION(option) 0
403
404#define DISABLE_CASE_FOLD_MULTI_CHAR(case_fold_flag) \
405 ((case_fold_flag) & ~INTERNAL_ONIGENC_CASE_FOLD_MULTI_CHAR)
406
407#define REPEAT_INFINITE -1
408#define IS_REPEAT_INFINITE(n) ((n) == REPEAT_INFINITE)
409
410/* bitset */
411#define BITS_PER_BYTE 8
412#define SINGLE_BYTE_SIZE (1 << BITS_PER_BYTE)
413#define BITS_IN_ROOM ((int )sizeof(Bits) * BITS_PER_BYTE)
414#define BITSET_SIZE (SINGLE_BYTE_SIZE / BITS_IN_ROOM)
415
416#ifdef PLATFORM_UNALIGNED_WORD_ACCESS
417typedef unsigned int Bits;
418#else
419typedef unsigned char Bits;
420#endif
421typedef Bits BitSet[BITSET_SIZE];
422typedef Bits* BitSetRef;
423
424#define SIZE_BITSET (int )sizeof(BitSet)
425
426#define BITSET_CLEAR(bs) do {\
427 int i;\
428 for (i = 0; i < BITSET_SIZE; i++) { (bs)[i] = 0; } \
429} while (0)
430
431#define BS_ROOM(bs,pos) (bs)[(int )(pos) / BITS_IN_ROOM]
432#define BS_BIT(pos) (1U << ((int )(pos) % BITS_IN_ROOM))
433
434#define BITSET_AT(bs, pos) (BS_ROOM(bs,pos) & BS_BIT(pos))
435#define BITSET_SET_BIT(bs, pos) BS_ROOM(bs,pos) |= BS_BIT(pos)
436#define BITSET_CLEAR_BIT(bs, pos) BS_ROOM(bs,pos) &= ~(BS_BIT(pos))
437#define BITSET_INVERT_BIT(bs, pos) BS_ROOM(bs,pos) ^= BS_BIT(pos)
438
439/* bytes buffer */
440typedef struct _BBuf {
441 UChar* p;
442 unsigned int used;
443 unsigned int alloc;
444} BBuf;
445
446#define BBUF_INIT(buf,size) onig_bbuf_init((BBuf* )(buf), (size))
447
448#define BBUF_SIZE_INC(buf,inc) do{\
449 UChar *tmp;\
450 (buf)->alloc += (inc);\
451 tmp = (UChar* )xrealloc((buf)->p, (buf)->alloc);\
452 if (IS_NULL(tmp)) return(ONIGERR_MEMORY);\
453 (buf)->p = tmp;\
454} while (0)
455
456#define BBUF_EXPAND(buf,low) do{\
457 UChar *tmp;\
458 do { (buf)->alloc *= 2; } while ((buf)->alloc < (unsigned int )low);\
459 tmp = (UChar* )xrealloc((buf)->p, (buf)->alloc);\
460 if (IS_NULL(tmp)) return(ONIGERR_MEMORY);\
461 (buf)->p = tmp;\
462} while (0)
463
464#define BBUF_ENSURE_SIZE(buf,size) do{\
465 unsigned int new_alloc = (buf)->alloc;\
466 while (new_alloc < (unsigned int )(size)) { new_alloc *= 2; }\
467 if ((buf)->alloc != new_alloc) {\
468 UChar *tmp;\
469 tmp = (UChar* )xrealloc((buf)->p, new_alloc);\
470 if (IS_NULL(tmp)) return(ONIGERR_MEMORY);\
471 (buf)->p = tmp;\
472 (buf)->alloc = new_alloc;\
473 }\
474} while (0)
475
476#define BBUF_WRITE(buf,pos,bytes,n) do{\
477 int used = (pos) + (int )(n);\
478 if ((buf)->alloc < (unsigned int )used) BBUF_EXPAND((buf),used);\
479 xmemcpy((buf)->p + (pos), (bytes), (n));\
480 if ((buf)->used < (unsigned int )used) (buf)->used = used;\
481} while (0)
482
483#define BBUF_WRITE1(buf,pos,byte) do{\
484 int used = (pos) + 1;\
485 if ((buf)->alloc < (unsigned int )used) BBUF_EXPAND((buf),used);\
486 (buf)->p[(pos)] = (UChar )(byte);\
487 if ((buf)->used < (unsigned int )used) (buf)->used = used;\
488} while (0)
489
490#define BBUF_ADD(buf,bytes,n) BBUF_WRITE((buf),(buf)->used,(bytes),(n))
491#define BBUF_ADD1(buf,byte) BBUF_WRITE1((buf),(buf)->used,(byte))
492#define BBUF_GET_ADD_ADDRESS(buf) ((buf)->p + (buf)->used)
493#define BBUF_GET_OFFSET_POS(buf) ((buf)->used)
494
495/* from < to */
496#define BBUF_MOVE_RIGHT(buf,from,to,n) do {\
497 if ((unsigned int )((to)+(n)) > (buf)->alloc) BBUF_EXPAND((buf),(to) + (n));\
498 xmemmove((buf)->p + (to), (buf)->p + (from), (n));\
499 if ((unsigned int )((to)+(n)) > (buf)->used) (buf)->used = (to) + (n);\
500} while (0)
501
502/* from > to */
503#define BBUF_MOVE_LEFT(buf,from,to,n) do {\
504 xmemmove((buf)->p + (to), (buf)->p + (from), (n));\
505} while (0)
506
507/* from > to */
508#define BBUF_MOVE_LEFT_REDUCE(buf,from,to) do {\
509 xmemmove((buf)->p + (to), (buf)->p + (from), (buf)->used - (from));\
510 (buf)->used -= (from - to);\
511} while (0)
512
513#define BBUF_INSERT(buf,pos,bytes,n) do {\
514 if (pos >= (buf)->used) {\
515 BBUF_WRITE(buf,pos,bytes,n);\
516 }\
517 else {\
518 BBUF_MOVE_RIGHT((buf),(pos),(pos) + (n),((buf)->used - (pos)));\
519 xmemcpy((buf)->p + (pos), (bytes), (n));\
520 }\
521} while (0)
522
523#define BBUF_GET_BYTE(buf, pos) (buf)->p[(pos)]
524
525
526#define ANCHOR_BEGIN_BUF (1<<0)
527#define ANCHOR_BEGIN_LINE (1<<1)
528#define ANCHOR_BEGIN_POSITION (1<<2)
529#define ANCHOR_END_BUF (1<<3)
530#define ANCHOR_SEMI_END_BUF (1<<4)
531#define ANCHOR_END_LINE (1<<5)
532
533#define ANCHOR_WORD_BOUND (1<<6)
534#define ANCHOR_NOT_WORD_BOUND (1<<7)
535#define ANCHOR_WORD_BEGIN (1<<8)
536#define ANCHOR_WORD_END (1<<9)
537#define ANCHOR_PREC_READ (1<<10)
538#define ANCHOR_PREC_READ_NOT (1<<11)
539#define ANCHOR_LOOK_BEHIND (1<<12)
540#define ANCHOR_LOOK_BEHIND_NOT (1<<13)
541
542#define ANCHOR_ANYCHAR_STAR (1<<14) /* ".*" optimize info */
543#define ANCHOR_ANYCHAR_STAR_ML (1<<15) /* ".*" optimize info (multi-line) */
544
545#define ANCHOR_KEEP (1<<16)
546
547/* operation code */
548enum OpCode {
549 OP_FINISH = 0, /* matching process terminator (no more alternative) */
550 OP_END = 1, /* pattern code terminator (success end) */
551
552 OP_EXACT1 = 2, /* single byte, N = 1 */
553 OP_EXACT2, /* single byte, N = 2 */
554 OP_EXACT3, /* single byte, N = 3 */
555 OP_EXACT4, /* single byte, N = 4 */
556 OP_EXACT5, /* single byte, N = 5 */
557 OP_EXACTN, /* single byte */
558 OP_EXACTMB2N1, /* mb-length = 2 N = 1 */
559 OP_EXACTMB2N2, /* mb-length = 2 N = 2 */
560 OP_EXACTMB2N3, /* mb-length = 2 N = 3 */
561 OP_EXACTMB2N, /* mb-length = 2 */
562 OP_EXACTMB3N, /* mb-length = 3 */
563 OP_EXACTMBN, /* other length */
564
565 OP_EXACT1_IC, /* single byte, N = 1, ignore case */
566 OP_EXACTN_IC, /* single byte, ignore case */
567
568 OP_CCLASS,
569 OP_CCLASS_MB,
570 OP_CCLASS_MIX,
571 OP_CCLASS_NOT,
572 OP_CCLASS_MB_NOT,
573 OP_CCLASS_MIX_NOT,
574
575 OP_ANYCHAR, /* "." */
576 OP_ANYCHAR_ML, /* "." multi-line */
577 OP_ANYCHAR_STAR, /* ".*" */
578 OP_ANYCHAR_ML_STAR, /* ".*" multi-line */
579 OP_ANYCHAR_STAR_PEEK_NEXT,
580 OP_ANYCHAR_ML_STAR_PEEK_NEXT,
581
582 OP_WORD,
583 OP_NOT_WORD,
584 OP_WORD_BOUND,
585 OP_NOT_WORD_BOUND,
586 OP_WORD_BEGIN,
587 OP_WORD_END,
588
589 OP_ASCII_WORD,
590 OP_NOT_ASCII_WORD,
591 OP_ASCII_WORD_BOUND,
592 OP_NOT_ASCII_WORD_BOUND,
593 OP_ASCII_WORD_BEGIN,
594 OP_ASCII_WORD_END,
595
596 OP_BEGIN_BUF,
597 OP_END_BUF,
598 OP_BEGIN_LINE,
599 OP_END_LINE,
600 OP_SEMI_END_BUF,
601 OP_BEGIN_POSITION,
602
603 OP_BACKREF1,
604 OP_BACKREF2,
605 OP_BACKREFN,
606 OP_BACKREFN_IC,
607 OP_BACKREF_MULTI,
608 OP_BACKREF_MULTI_IC,
609 OP_BACKREF_WITH_LEVEL, /* \k<xxx+n>, \k<xxx-n> */
610
611 OP_MEMORY_START,
612 OP_MEMORY_START_PUSH, /* push back-tracker to stack */
613 OP_MEMORY_END_PUSH, /* push back-tracker to stack */
614 OP_MEMORY_END_PUSH_REC, /* push back-tracker to stack */
615 OP_MEMORY_END,
616 OP_MEMORY_END_REC, /* push marker to stack */
617
618 OP_KEEP,
619
620 OP_FAIL, /* pop stack and move */
621 OP_JUMP,
622 OP_PUSH,
623 OP_POP,
624 OP_PUSH_OR_JUMP_EXACT1, /* if match exact then push, else jump. */
625 OP_PUSH_IF_PEEK_NEXT, /* if match exact then push, else none. */
626 OP_REPEAT, /* {n,m} */
627 OP_REPEAT_NG, /* {n,m}? (non greedy) */
628 OP_REPEAT_INC,
629 OP_REPEAT_INC_NG, /* non greedy */
630 OP_REPEAT_INC_SG, /* search and get in stack */
631 OP_REPEAT_INC_NG_SG, /* search and get in stack (non greedy) */
632 OP_NULL_CHECK_START, /* null loop checker start */
633 OP_NULL_CHECK_END, /* null loop checker end */
634 OP_NULL_CHECK_END_MEMST, /* null loop checker end (with capture status) */
635 OP_NULL_CHECK_END_MEMST_PUSH, /* with capture status and push check-end */
636
637 OP_PUSH_POS, /* (?=...) start */
638 OP_POP_POS, /* (?=...) end */
639 OP_PUSH_POS_NOT, /* (?!...) start */
640 OP_FAIL_POS, /* (?!...) end */
641 OP_PUSH_STOP_BT, /* (?>...) start */
642 OP_POP_STOP_BT, /* (?>...) end */
643 OP_LOOK_BEHIND, /* (?<=...) start (no needs end opcode) */
644 OP_PUSH_LOOK_BEHIND_NOT, /* (?<!...) start */
645 OP_FAIL_LOOK_BEHIND_NOT, /* (?<!...) end */
646 OP_PUSH_ABSENT_POS, /* (?~...) start */
647 OP_ABSENT, /* (?~...) start of inner loop */
648 OP_ABSENT_END, /* (?~...) end */
649
650 OP_CALL, /* \g<name> */
651 OP_RETURN,
652
653 OP_CONDITION,
654
655 OP_STATE_CHECK_PUSH, /* combination explosion check and push */
656 OP_STATE_CHECK_PUSH_OR_JUMP, /* check ok -> push, else jump */
657 OP_STATE_CHECK, /* check only */
658 OP_STATE_CHECK_ANYCHAR_STAR,
659 OP_STATE_CHECK_ANYCHAR_ML_STAR,
660
661 /* no need: IS_DYNAMIC_OPTION() == 0 */
662 OP_SET_OPTION_PUSH, /* set option and push recover option */
663 OP_SET_OPTION /* set option */
664};
665
666typedef int RelAddrType;
667typedef int AbsAddrType;
668typedef int LengthType;
669typedef int RepeatNumType;
670typedef short int MemNumType;
671typedef short int StateCheckNumType;
672typedef void* PointerType;
673
674#define SIZE_OPCODE 1
675#define SIZE_RELADDR (int )sizeof(RelAddrType)
676#define SIZE_ABSADDR (int )sizeof(AbsAddrType)
677#define SIZE_LENGTH (int )sizeof(LengthType)
678#define SIZE_MEMNUM (int )sizeof(MemNumType)
679#define SIZE_STATE_CHECK_NUM (int )sizeof(StateCheckNumType)
680#define SIZE_REPEATNUM (int )sizeof(RepeatNumType)
681#define SIZE_OPTION (int )sizeof(OnigOptionType)
682#define SIZE_CODE_POINT (int )sizeof(OnigCodePoint)
683#define SIZE_POINTER (int )sizeof(PointerType)
684
685
686#define GET_RELADDR_INC(addr,p) PLATFORM_GET_INC(addr, p, RelAddrType)
687#define GET_ABSADDR_INC(addr,p) PLATFORM_GET_INC(addr, p, AbsAddrType)
688#define GET_LENGTH_INC(len,p) PLATFORM_GET_INC(len, p, LengthType)
689#define GET_MEMNUM_INC(num,p) PLATFORM_GET_INC(num, p, MemNumType)
690#define GET_REPEATNUM_INC(num,p) PLATFORM_GET_INC(num, p, RepeatNumType)
691#define GET_OPTION_INC(option,p) PLATFORM_GET_INC(option, p, OnigOptionType)
692#define GET_POINTER_INC(ptr,p) PLATFORM_GET_INC(ptr, p, PointerType)
693#define GET_STATE_CHECK_NUM_INC(num,p) PLATFORM_GET_INC(num, p, StateCheckNumType)
694
695/* code point's address must be aligned address. */
696#define GET_CODE_POINT(code,p) code = *((OnigCodePoint* )(p))
697#define GET_BYTE_INC(byte,p) do{\
698 byte = *(p);\
699 (p)++;\
700} while(0)
701
702
703/* op-code + arg size */
704#define SIZE_OP_ANYCHAR_STAR SIZE_OPCODE
705#define SIZE_OP_ANYCHAR_STAR_PEEK_NEXT (SIZE_OPCODE + 1)
706#define SIZE_OP_JUMP (SIZE_OPCODE + SIZE_RELADDR)
707#define SIZE_OP_PUSH (SIZE_OPCODE + SIZE_RELADDR)
708#define SIZE_OP_POP SIZE_OPCODE
709#define SIZE_OP_PUSH_OR_JUMP_EXACT1 (SIZE_OPCODE + SIZE_RELADDR + 1)
710#define SIZE_OP_PUSH_IF_PEEK_NEXT (SIZE_OPCODE + SIZE_RELADDR + 1)
711#define SIZE_OP_REPEAT_INC (SIZE_OPCODE + SIZE_MEMNUM)
712#define SIZE_OP_REPEAT_INC_NG (SIZE_OPCODE + SIZE_MEMNUM)
713#define SIZE_OP_PUSH_POS SIZE_OPCODE
714#define SIZE_OP_PUSH_POS_NOT (SIZE_OPCODE + SIZE_RELADDR)
715#define SIZE_OP_POP_POS SIZE_OPCODE
716#define SIZE_OP_FAIL_POS SIZE_OPCODE
717#define SIZE_OP_SET_OPTION (SIZE_OPCODE + SIZE_OPTION)
718#define SIZE_OP_SET_OPTION_PUSH (SIZE_OPCODE + SIZE_OPTION)
719#define SIZE_OP_FAIL SIZE_OPCODE
720#define SIZE_OP_MEMORY_START (SIZE_OPCODE + SIZE_MEMNUM)
721#define SIZE_OP_MEMORY_START_PUSH (SIZE_OPCODE + SIZE_MEMNUM)
722#define SIZE_OP_MEMORY_END_PUSH (SIZE_OPCODE + SIZE_MEMNUM)
723#define SIZE_OP_MEMORY_END_PUSH_REC (SIZE_OPCODE + SIZE_MEMNUM)
724#define SIZE_OP_MEMORY_END (SIZE_OPCODE + SIZE_MEMNUM)
725#define SIZE_OP_MEMORY_END_REC (SIZE_OPCODE + SIZE_MEMNUM)
726#define SIZE_OP_PUSH_STOP_BT SIZE_OPCODE
727#define SIZE_OP_POP_STOP_BT SIZE_OPCODE
728#define SIZE_OP_NULL_CHECK_START (SIZE_OPCODE + SIZE_MEMNUM)
729#define SIZE_OP_NULL_CHECK_END (SIZE_OPCODE + SIZE_MEMNUM)
730#define SIZE_OP_LOOK_BEHIND (SIZE_OPCODE + SIZE_LENGTH)
731#define SIZE_OP_PUSH_LOOK_BEHIND_NOT (SIZE_OPCODE + SIZE_RELADDR + SIZE_LENGTH)
732#define SIZE_OP_FAIL_LOOK_BEHIND_NOT SIZE_OPCODE
733#define SIZE_OP_CALL (SIZE_OPCODE + SIZE_ABSADDR)
734#define SIZE_OP_RETURN SIZE_OPCODE
735#define SIZE_OP_CONDITION (SIZE_OPCODE + SIZE_MEMNUM + SIZE_RELADDR)
736#define SIZE_OP_PUSH_ABSENT_POS SIZE_OPCODE
737#define SIZE_OP_ABSENT (SIZE_OPCODE + SIZE_RELADDR)
738#define SIZE_OP_ABSENT_END SIZE_OPCODE
739
740#ifdef USE_COMBINATION_EXPLOSION_CHECK
741# define SIZE_OP_STATE_CHECK (SIZE_OPCODE + SIZE_STATE_CHECK_NUM)
742# define SIZE_OP_STATE_CHECK_PUSH (SIZE_OPCODE + SIZE_STATE_CHECK_NUM + SIZE_RELADDR)
743# define SIZE_OP_STATE_CHECK_PUSH_OR_JUMP (SIZE_OPCODE + SIZE_STATE_CHECK_NUM + SIZE_RELADDR)
744# define SIZE_OP_STATE_CHECK_ANYCHAR_STAR (SIZE_OPCODE + SIZE_STATE_CHECK_NUM)
745#endif
746
747#define MC_ESC(syn) (syn)->meta_char_table.esc
748#define MC_ANYCHAR(syn) (syn)->meta_char_table.anychar
749#define MC_ANYTIME(syn) (syn)->meta_char_table.anytime
750#define MC_ZERO_OR_ONE_TIME(syn) (syn)->meta_char_table.zero_or_one_time
751#define MC_ONE_OR_MORE_TIME(syn) (syn)->meta_char_table.one_or_more_time
752#define MC_ANYCHAR_ANYTIME(syn) (syn)->meta_char_table.anychar_anytime
753
754#define IS_MC_ESC_CODE(code, syn) \
755 ((code) == MC_ESC(syn) && \
756 !IS_SYNTAX_OP2((syn), ONIG_SYN_OP2_INEFFECTIVE_ESCAPE))
757
758
759#define SYN_POSIX_COMMON_OP \
760 ( ONIG_SYN_OP_DOT_ANYCHAR | ONIG_SYN_OP_POSIX_BRACKET | \
761 ONIG_SYN_OP_DECIMAL_BACKREF | \
762 ONIG_SYN_OP_BRACKET_CC | ONIG_SYN_OP_ASTERISK_ZERO_INF | \
763 ONIG_SYN_OP_LINE_ANCHOR | \
764 ONIG_SYN_OP_ESC_CONTROL_CHARS )
765
766#define SYN_GNU_REGEX_OP \
767 ( ONIG_SYN_OP_DOT_ANYCHAR | ONIG_SYN_OP_BRACKET_CC | \
768 ONIG_SYN_OP_POSIX_BRACKET | ONIG_SYN_OP_DECIMAL_BACKREF | \
769 ONIG_SYN_OP_BRACE_INTERVAL | ONIG_SYN_OP_LPAREN_SUBEXP | \
770 ONIG_SYN_OP_VBAR_ALT | \
771 ONIG_SYN_OP_ASTERISK_ZERO_INF | ONIG_SYN_OP_PLUS_ONE_INF | \
772 ONIG_SYN_OP_QMARK_ZERO_ONE | \
773 ONIG_SYN_OP_ESC_AZ_BUF_ANCHOR | ONIG_SYN_OP_ESC_CAPITAL_G_BEGIN_ANCHOR | \
774 ONIG_SYN_OP_ESC_W_WORD | \
775 ONIG_SYN_OP_ESC_B_WORD_BOUND | ONIG_SYN_OP_ESC_LTGT_WORD_BEGIN_END | \
776 ONIG_SYN_OP_ESC_S_WHITE_SPACE | ONIG_SYN_OP_ESC_D_DIGIT | \
777 ONIG_SYN_OP_LINE_ANCHOR )
778
779#define SYN_GNU_REGEX_BV \
780 ( ONIG_SYN_CONTEXT_INDEP_ANCHORS | ONIG_SYN_CONTEXT_INDEP_REPEAT_OPS | \
781 ONIG_SYN_CONTEXT_INVALID_REPEAT_OPS | ONIG_SYN_ALLOW_INVALID_INTERVAL | \
782 ONIG_SYN_BACKSLASH_ESCAPE_IN_CC | ONIG_SYN_ALLOW_DOUBLE_RANGE_OP_IN_CC )
783
784
785#define NCCLASS_FLAGS(cc) ((cc)->flags)
786#define NCCLASS_FLAG_SET(cc,flag) (NCCLASS_FLAGS(cc) |= (flag))
787#define NCCLASS_FLAG_CLEAR(cc,flag) (NCCLASS_FLAGS(cc) &= ~(flag))
788#define IS_NCCLASS_FLAG_ON(cc,flag) ((NCCLASS_FLAGS(cc) & (flag)) != 0)
789
790/* cclass node */
791#define FLAG_NCCLASS_NOT (1<<0)
792
793#define NCCLASS_SET_NOT(nd) NCCLASS_FLAG_SET(nd, FLAG_NCCLASS_NOT)
794#define NCCLASS_CLEAR_NOT(nd) NCCLASS_FLAG_CLEAR(nd, FLAG_NCCLASS_NOT)
795#define IS_NCCLASS_NOT(nd) IS_NCCLASS_FLAG_ON(nd, FLAG_NCCLASS_NOT)
796
797typedef struct {
798 int type;
799 /* struct _Node* next; */
800 /* unsigned int flags; */
801} NodeBase;
802
803typedef struct {
804 NodeBase base;
805 unsigned int flags;
806 BitSet bs;
807 BBuf* mbuf; /* multi-byte info or NULL */
808} CClassNode;
809
810typedef intptr_t OnigStackIndex;
811
812typedef struct _OnigStackType {
813 unsigned int type;
814 OnigStackIndex null_check;
815 union {
816 struct {
817 UChar *pcode; /* byte code position */
818 UChar *pstr; /* string position */
819 UChar *pstr_prev; /* previous char position of pstr */
820#ifdef USE_COMBINATION_EXPLOSION_CHECK
821 unsigned int state_check;
822#endif
823 UChar *pkeep; /* keep pattern position */
824 } state;
825 struct {
826 int count; /* for OP_REPEAT_INC, OP_REPEAT_INC_NG */
827 UChar *pcode; /* byte code position (head of repeated target) */
828 int num; /* repeat id */
829 } repeat;
830 struct {
831 OnigStackIndex si; /* index of stack */
832 } repeat_inc;
833 struct {
834 int num; /* memory num */
835 UChar *pstr; /* start/end position */
836 /* Following information is set, if this stack type is MEM-START */
837 OnigStackIndex start; /* prev. info (for backtrack "(...)*" ) */
838 OnigStackIndex end; /* prev. info (for backtrack "(...)*" ) */
839 } mem;
840 struct {
841 int num; /* null check id */
842 UChar *pstr; /* start position */
843 } null_check;
844#ifdef USE_SUBEXP_CALL
845 struct {
846 UChar *ret_addr; /* byte code position */
847 int num; /* null check id */
848 UChar *pstr; /* string position */
849 } call_frame;
850#endif
851 struct {
852 UChar *abs_pstr; /* absent start position */
853 const UChar *end_pstr; /* end position */
854 } absent_pos;
855#ifdef USE_MATCH_CACHE
856 struct {
857 long index; /* index of the match cache buffer */
858 uint8_t mask; /* bit-mask for the match cache buffer */
859 } match_cache_point;
860#endif
861 } u;
863
864#ifdef USE_MATCH_CACHE
865typedef struct {
866 UChar *addr;
867 long cache_point;
868 int outer_repeat_mem;
869 long num_cache_points_at_outer_repeat;
870 long num_cache_points_in_outer_repeat;
871 int lookaround_nesting;
872 UChar *match_addr;
874#endif
875
876typedef struct {
877 void* stack_p;
878 size_t stack_n;
879 OnigOptionType options;
880 OnigRegion* region;
881 const UChar* start; /* search start position */
882 const UChar* gpos; /* global position (for \G: BEGIN_POSITION) */
883#ifdef USE_FIND_LONGEST_SEARCH_ALL_OF_RANGE
884 OnigPosition best_len; /* for ONIG_OPTION_FIND_LONGEST */
885 UChar* best_s;
886#endif
887#ifdef USE_COMBINATION_EXPLOSION_CHECK
888 void* state_check_buff;
889 int state_check_buff_size;
890#endif
891 int counter;
892 /* rb_hrtime_t from hrtime.h */
893#ifdef MY_RUBY_BUILD_MAY_TIME_TRAVEL
894 int128_t end_time;
895#else
896 uint64_t end_time;
897#endif
898#ifdef USE_MATCH_CACHE
899 int match_cache_status;
900 long num_fails;
901 long num_cache_opcodes;
902 OnigCacheOpcode* cache_opcodes;
903 long num_cache_points;
904 uint8_t* match_cache_buf;
905#endif
907
908#define NUM_CACHE_OPCODES_UNINIT 1
909#define NUM_CACHE_OPCODES_IMPOSSIBLE -1
910
911#define MATCH_CACHE_STATUS_UNINIT 1
912#define MATCH_CACHE_STATUS_INIT 2
913#define MATCH_CACHE_STATUS_DISABLED -1
914#define MATCH_CACHE_STATUS_ENABLED 0
915
916#define IS_CODE_SB_WORD(enc,code) \
917 (ONIGENC_IS_CODE_ASCII(code) && ONIGENC_IS_CODE_WORD(enc,code))
918
919typedef struct OnigEndCallListItem {
920 struct OnigEndCallListItem* next;
921 void (*func)(void);
923
924extern void onig_add_end_call(void (*func)(void));
925
926
927#ifdef ONIG_DEBUG
928
929typedef struct {
930 short int opcode;
931 const char* name;
932 short int arg_type;
933} OnigOpInfoType;
934
935extern OnigOpInfoType OnigOpInfo[];
936
937
938extern void onig_print_compiled_byte_code(FILE* f, UChar* bp, UChar* bpend, UChar** nextp, OnigEncoding enc);
939
940# ifdef ONIG_DEBUG_STATISTICS
941extern void onig_statistics_init(void);
942extern void onig_print_statistics(FILE* f);
943# endif
944#endif
945
946#ifndef PRINTF_ARGS
947#define PRINTF_ARGS(func, fmt, vargs) func
948#endif
949
950extern UChar* onig_error_code_to_format(OnigPosition code);
951PRINTF_ARGS(extern void onig_vsnprintf_with_pattern(UChar buf[], int bufsize, OnigEncoding enc, UChar* pat, UChar* pat_end, const char *fmt, va_list args), 6, 0);
952PRINTF_ARGS(extern void onig_snprintf_with_pattern(UChar buf[], int bufsize, OnigEncoding enc, UChar* pat, UChar* pat_end, const char *fmt, ...), 6, 7);
953extern int onig_bbuf_init(BBuf* buf, OnigDistance size);
954extern int onig_compile(regex_t* reg, const UChar* pattern, const UChar* pattern_end, OnigErrorInfo* einfo);
955#ifdef RUBY
956extern int onig_compile_ruby(regex_t* reg, const UChar* pattern, const UChar* pattern_end, OnigErrorInfo* einfo, const char *sourcefile, int sourceline);
957#endif
958extern void onig_transfer(regex_t* to, regex_t* from);
959extern int onig_is_code_in_cc(OnigEncoding enc, OnigCodePoint code, CClassNode* cc);
960extern int onig_is_code_in_cc_len(int enclen, OnigCodePoint code, CClassNode* cc);
961
962/* strend hash */
963typedef void hash_table_type;
964#ifdef RUBY
965# include "ruby/st.h"
966#else
967# include "st.h"
968#endif
969typedef st_data_t hash_data_type;
970
971extern hash_table_type* onig_st_init_strend_table_with_size(st_index_t size);
972extern int onig_st_lookup_strend(hash_table_type* table, const UChar* str_key, const UChar* end_key, hash_data_type *value);
973extern int onig_st_insert_strend(hash_table_type* table, const UChar* str_key, const UChar* end_key, hash_data_type value);
974
975#ifdef RUBY
976extern size_t onig_memsize(const regex_t *reg);
977extern size_t onig_region_memsize(const struct re_registers *regs);
978bool rb_reg_timeout_p(regex_t *reg, void *end_time);
979#endif
980
981RUBY_SYMBOL_EXPORT_END
982
983#endif /* ONIGMO_REGINT_H */
Defines old _.