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