diff options
Diffstat (limited to 'regint.h')
| -rw-r--r-- | regint.h | 750 |
1 files changed, 514 insertions, 236 deletions
@@ -1,10 +1,11 @@ -#ifndef REGINT_H -#define REGINT_H +#ifndef ONIGMO_REGINT_H +#define ONIGMO_REGINT_H /********************************************************************** - regint.h - Oniguruma (regular expression library) + regint.h - Onigmo (Oniguruma-mod) (regular expression library) **********************************************************************/ /*- - * Copyright (c) 2002-2004 K.Kosako <kosako AT sofnec DOT co DOT jp> + * Copyright (c) 2002-2013 K.Kosako <sndgk393 AT ybb DOT ne DOT jp> + * Copyright (c) 2011-2019 K.Takata <kentkt AT csc DOT jp> * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -34,127 +35,260 @@ /* #define ONIG_DEBUG_COMPILE */ /* #define ONIG_DEBUG_SEARCH */ /* #define ONIG_DEBUG_MATCH */ +/* #define ONIG_DEBUG_MATCH_CACHE */ +/* #define ONIG_DEBUG_MEMLEAK */ /* #define ONIG_DONT_OPTIMIZE */ /* for byte-code statistical data. */ /* #define ONIG_DEBUG_STATISTICS */ +/* enable the match optimization by using a cache. */ +#define USE_MATCH_CACHE + #if defined(ONIG_DEBUG_PARSE_TREE) || defined(ONIG_DEBUG_MATCH) || \ defined(ONIG_DEBUG_SEARCH) || defined(ONIG_DEBUG_COMPILE) || \ - defined(ONIG_DEBUG_STATISTICS) -#ifndef ONIG_DEBUG -#define ONIG_DEBUG + defined(ONIG_DEBUG_STATISTICS) || defined(ONIG_DEBUG_MEMLEAK) +# ifndef ONIG_DEBUG +# define ONIG_DEBUG +# endif #endif + +/* __POWERPC__ added to accommodate Darwin case. */ +#ifndef UNALIGNED_WORD_ACCESS +# if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \ + defined(__x86_64) || defined(__x86_64__) || defined(_M_AMD64) || \ + defined(__powerpc64__) || defined(__POWERPC__) || defined(__aarch64__) || \ + defined(__mc68020__) +# define UNALIGNED_WORD_ACCESS 1 +# else +# define UNALIGNED_WORD_ACCESS 0 +# endif #endif -#if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \ - (defined(__ppc__) && defined(__APPLE__)) || \ - defined(__x86_64) || defined(__x86_64__) || \ - defined(__mc68020__) -#define PLATFORM_UNALIGNED_WORD_ACCESS +#if UNALIGNED_WORD_ACCESS +# define PLATFORM_UNALIGNED_WORD_ACCESS #endif /* config */ /* spec. config */ #define USE_NAMED_GROUP #define USE_SUBEXP_CALL -#define USE_INFINITE_REPEAT_MONOMANIAC_MEM_STATUS_CHECK /* /(?:()|())*\2/ */ +#define USE_PERL_SUBEXP_CALL +#define USE_CAPITAL_P_NAMED_GROUP +#define USE_BACKREF_WITH_LEVEL /* \k<name+n>, \k<name-n> */ +#define USE_MONOMANIAC_CHECK_CAPTURES_IN_ENDLESS_REPEAT /* /(?:()|())*\2/ */ #define USE_NEWLINE_AT_END_OF_STRING_HAS_EMPTY_LINE /* /\n$/ =~ "\n" */ #define USE_WARNING_REDUNDANT_NESTED_REPEAT_OPERATOR +/* !!! moved to regenc.h. */ /* #define USE_CRNL_AS_LINE_TERMINATOR */ +#define USE_NO_INVALID_QUANTIFIER + /* internal config */ -#define USE_RECYCLE_NODE -#define USE_OP_PUSH_OR_JUMP_EXACT -#define USE_QUALIFIER_PEEK_NEXT +/* #define USE_OP_PUSH_OR_JUMP_EXACT */ +#define USE_QTFR_PEEK_NEXT +#define USE_ST_LIBRARY #define INIT_MATCH_STACK_SIZE 160 #define DEFAULT_MATCH_STACK_LIMIT_SIZE 0 /* unlimited */ +#define DEFAULT_PARSE_DEPTH_LIMIT 4096 -/* interface to external system */ -#ifdef NOT_RUBY /* gived from Makefile */ -#include "config.h" -#define USE_CAPTURE_HISTORY -#define USE_VARIABLE_META_CHARS -#define USE_WORD_BEGIN_END /* "\<": word-begin, "\>": word-end */ -#define USE_POSIX_REGION_OPTION /* needed for POSIX API support */ -#define THREAD_ATOMIC_START /* depend on thread system */ -#define THREAD_ATOMIC_END /* depend on thread system */ -#define THREAD_PASS /* depend on thread system */ -#define CHECK_INTERRUPT /* depend on application */ -#define xmalloc malloc -#define xrealloc realloc -#define xfree free +#define OPT_EXACT_MAXLEN 24 /* This must be smaller than ONIG_CHAR_TABLE_SIZE. */ + +/* check config */ +#if defined(USE_PERL_SUBEXP_CALL) || defined(USE_CAPITAL_P_NAMED_GROUP) +# if !defined(USE_NAMED_GROUP) || !defined(USE_SUBEXP_CALL) +# error USE_NAMED_GROUP and USE_SUBEXP_CALL must be defined. +# endif +#endif + +#if defined(__GNUC__) +# define ARG_UNUSED __attribute__ ((unused)) #else -#include "ruby.h" -#include "version.h" -#include "rubysig.h" /* for DEFER_INTS, ENABLE_INTS */ -#define THREAD_ATOMIC_START DEFER_INTS -#define THREAD_ATOMIC_END ENABLE_INTS -#define THREAD_PASS rb_thread_schedule() -#define CHECK_INTERRUPT do {\ - if (rb_trap_pending) {\ - if (! rb_prohibit_interrupt) {\ - rb_trap_exec();\ - }\ - }\ -} while (0) +# define ARG_UNUSED +#endif -#define DEFAULT_WARN_FUNCTION rb_warn -#define DEFAULT_VERB_WARN_FUNCTION rb_warning +#if !defined(RUBY) && defined(RUBY_EXPORT) +# define RUBY +#endif +#ifdef RUBY +# ifndef RUBY_DEFINES_H +# include "ruby/ruby.h" +# undef xmalloc +# undef xrealloc +# undef xcalloc +# undef xfree +# endif +#else /* RUBY */ +# include "config.h" +# if SIZEOF_LONG_LONG > 0 +# define LONG_LONG long long +# endif +#endif /* RUBY */ + +#include <stdarg.h> + +/* */ +/* escape other system UChar definition */ +#ifdef ONIG_ESCAPE_UCHAR_COLLISION +# undef ONIG_ESCAPE_UCHAR_COLLISION +#endif -#if defined(RUBY_VERSION_MAJOR) -#if RUBY_VERSION_MAJOR > 1 || \ -(RUBY_VERSION_MAJOR == 1 && \ - defined(RUBY_VERSION_MINOR) && RUBY_VERSION_MINOR >= 8) -#define USE_ST_HASH_TABLE +#define USE_WORD_BEGIN_END /* "\<": word-begin, "\>": word-end */ +#ifdef RUBY +# undef USE_CAPTURE_HISTORY +#else +# define USE_CAPTURE_HISTORY #endif +#define USE_VARIABLE_META_CHARS +#define USE_FIND_LONGEST_SEARCH_ALL_OF_RANGE +/* #define USE_COMBINATION_EXPLOSION_CHECK */ /* (X*)* */ + + +#ifndef xmalloc +# define xmalloc malloc +# define xrealloc realloc +# define xcalloc calloc +# define xfree free #endif -#endif /* else NOT_RUBY */ +#ifdef RUBY + +# define CHECK_INTERRUPT_IN_MATCH_AT do { \ + msa->counter++; \ + if (msa->counter >= 128) { \ + msa->counter = 0; \ + if (rb_reg_timeout_p(reg, &msa->end_time)) { \ + goto timeout; \ + } \ + rb_thread_check_ints(); \ + } \ +} while(0) +# define onig_st_init_table st_init_table +# define onig_st_init_table_with_size st_init_table_with_size +# define onig_st_init_numtable st_init_numtable +# define onig_st_init_numtable_with_size st_init_numtable_with_size +# define onig_st_init_strtable st_init_strtable +# define onig_st_init_strtable_with_size st_init_strtable_with_size +# define onig_st_delete st_delete +# define onig_st_delete_safe st_delete_safe +# define onig_st_insert st_insert +# define onig_st_lookup st_lookup +# define onig_st_foreach st_foreach +# define onig_st_add_direct st_add_direct +# define onig_st_free_table st_free_table +# define onig_st_cleanup_safe st_cleanup_safe +# define onig_st_copy st_copy +# define onig_st_nothing_key_clone st_nothing_key_clone +# define onig_st_nothing_key_free st_nothing_key_free +# define onig_st_is_member st_is_member + +# define USE_UPPER_CASE_TABLE +#else /* RUBY */ + +# define CHECK_INTERRUPT_IN_MATCH_AT + +# define st_init_table onig_st_init_table +# define st_init_table_with_size onig_st_init_table_with_size +# define st_init_numtable onig_st_init_numtable +# define st_init_numtable_with_size onig_st_init_numtable_with_size +# define st_init_strtable onig_st_init_strtable +# define st_init_strtable_with_size onig_st_init_strtable_with_size +# define st_delete onig_st_delete +# define st_delete_safe onig_st_delete_safe +# define st_insert onig_st_insert +# define st_lookup onig_st_lookup +# define st_foreach onig_st_foreach +# define st_add_direct onig_st_add_direct +# define st_free_table onig_st_free_table +# define st_cleanup_safe onig_st_cleanup_safe +# define st_copy onig_st_copy +# define st_nothing_key_clone onig_st_nothing_key_clone +# define st_nothing_key_free onig_st_nothing_key_free +/* */ +# define onig_st_is_member st_is_member + +#endif /* RUBY */ + +#define STATE_CHECK_STRING_THRESHOLD_LEN 7 +#define STATE_CHECK_BUFF_MAX_SIZE 0x4000 -#define THREAD_PASS_LIMIT_COUNT 10 #define xmemset memset #define xmemcpy memcpy #define xmemmove memmove + #if defined(_WIN32) && !defined(__GNUC__) -#define xalloca _alloca -#ifdef NOT_RUBY -#define vsnprintf _vsnprintf -#endif +# define xalloca _alloca +# define xvsnprintf(buf,size,fmt,args) _vsnprintf_s(buf,size,_TRUNCATE,fmt,args) +# define xsnprintf sprintf_s +# define xstrcat(dest,src,size) strcat_s(dest,size,src) #else -#define xalloca alloca +# define xalloca alloca +# define xvsnprintf vsnprintf +# define xsnprintf snprintf +# define xstrcat(dest,src,size) strcat(dest,src) +#endif + +#if defined(ONIG_DEBUG_MEMLEAK) && defined(_MSC_VER) +# define _CRTDBG_MAP_ALLOC +# include <malloc.h> +# include <crtdbg.h> #endif -#ifdef HAVE_STDLIB_H #include <stdlib.h> + +#if defined(HAVE_ALLOCA_H) && (defined(_AIX) || !defined(__GNUC__)) +# include <alloca.h> #endif -#if defined(HAVE_ALLOCA_H) && !defined(__GNUC__) -#include <alloca.h> +#include <string.h> + +#include <ctype.h> +#ifdef HAVE_SYS_TYPES_H +# include <sys/types.h> #endif -#ifdef HAVE_STRING_H -# include <string.h> -#else -# include <strings.h> +#ifdef HAVE_STDINT_H +# include <stdint.h> #endif -#include <ctype.h> -#ifndef __BORLANDC__ -#include <sys/types.h> +#ifdef HAVE_INTTYPES_H +# include <inttypes.h> +#endif + +#include <stddef.h> + +#ifdef _WIN32 +# include <malloc.h> /* for alloca() */ #endif #ifdef ONIG_DEBUG # include <stdio.h> #endif +#ifndef PRIdPTR +# ifdef _WIN64 +# define PRIdPTR "I64d" +# define PRIuPTR "I64u" +# define PRIxPTR "I64x" +# else +# define PRIdPTR "ld" +# define PRIuPTR "lu" +# define PRIxPTR "lx" +# endif +#endif + +#ifndef PRIdPTRDIFF +# define PRIdPTRDIFF PRIdPTR +#endif + #include "regenc.h" -#include "oniguruma.h" + +RUBY_SYMBOL_EXPORT_BEGIN #ifdef MIN -#undef MIN +# undef MIN #endif #ifdef MAX -#undef MAX +# undef MAX #endif #define MIN(a,b) (((a)>(b))?(b):(a)) #define MAX(a,b) (((a)<(b))?(b):(a)) @@ -162,103 +296,46 @@ #define IS_NULL(p) (((void*)(p)) == (void*)0) #define IS_NOT_NULL(p) (((void*)(p)) != (void*)0) #define CHECK_NULL_RETURN(p) if (IS_NULL(p)) return NULL -#define CHECK_NULL_RETURN_VAL(p,val) if (IS_NULL(p)) return (val) +#define CHECK_NULL_RETURN_MEMERR(p) if (IS_NULL(p)) return ONIGERR_MEMORY #define NULL_UCHARP ((UChar* )0) -#ifndef PLATFORM_UNALIGNED_WORD_ACCESS -#define WORD_ALIGNMENT_SIZE SIZEOF_INT +#define ONIG_LAST_CODE_POINT (~((OnigCodePoint )0)) -#define GET_ALIGNMENT_PAD_SIZE(addr,pad_size) do {\ - (pad_size) = WORD_ALIGNMENT_SIZE \ - - ((unsigned int )(addr) % WORD_ALIGNMENT_SIZE);\ - if ((pad_size) == WORD_ALIGNMENT_SIZE) (pad_size) = 0;\ -} while (0) - -#define ALIGNMENT_RIGHT(addr) do {\ - (addr) += (WORD_ALIGNMENT_SIZE - 1);\ - (addr) -= ((unsigned int )(addr) % WORD_ALIGNMENT_SIZE);\ -} while (0) +#define PLATFORM_GET_INC_ARGUMENTS_ASSERT(val, type) \ + ((void)sizeof(char[2 * (sizeof(val) == sizeof(type)) - 1])) +#ifdef PLATFORM_UNALIGNED_WORD_ACCESS -#define B_SHIFT 8 -#define B_MASK 0xff - -#define SERIALIZE_2BYTE_INT(i,p) do {\ - *(p) = ((i) >> B_SHIFT) & B_MASK;\ - *((p)+1) = (i) & B_MASK;\ -} while (0) +# define PLATFORM_GET_INC(val,p,type) do{\ + PLATFORM_GET_INC_ARGUMENTS_ASSERT(val, type);\ + val = *(type* )p;\ + (p) += sizeof(type);\ +} while(0) -#define SERIALIZE_4BYTE_INT(i,p) do {\ - *(p) = ((i) >> B_SHIFT*3) & B_MASK;\ - *((p)+1) = ((i) >> B_SHIFT*2) & B_MASK;\ - *((p)+2) = ((i) >> B_SHIFT ) & B_MASK;\ - *((p)+3) = (i) & B_MASK;\ -} while (0) +#else -#define SERIALIZE_8BYTE_INT(i,p) do {\ - *(p) = ((i) >> B_SHIFT*7) & B_MASK;\ - *((p)+1) = ((i) >> B_SHIFT*6) & B_MASK;\ - *((p)+2) = ((i) >> B_SHIFT*5) & B_MASK;\ - *((p)+3) = ((i) >> B_SHIFT*4) & B_MASK;\ - *((p)+4) = ((i) >> B_SHIFT*3) & B_MASK;\ - *((p)+5) = ((i) >> B_SHIFT*2) & B_MASK;\ - *((p)+6) = ((i) >> B_SHIFT ) & B_MASK;\ - *((p)+7) = (i) & B_MASK;\ -} while (0) +# define PLATFORM_GET_INC(val,p,type) do{\ + PLATFORM_GET_INC_ARGUMENTS_ASSERT(val, type);\ + type platform_get_value;\ + xmemcpy(&platform_get_value, (p), sizeof(type));\ + val = platform_get_value;\ + (p) += sizeof(type);\ +} while(0) -#define GET_2BYTE_INT_INC(type,i,p) do {\ - (i) = (type )(((unsigned int )(*(p)) << B_SHIFT) | (unsigned int )((p)[1]));\ - (p) += 2;\ -} while (0) +/* sizeof(OnigCodePoint) */ +# define WORD_ALIGNMENT_SIZE SIZEOF_LONG -#define GET_4BYTE_INT_INC(type,i,p) do {\ - (i) = (type )(((unsigned int )((p)[0]) << B_SHIFT*3) | \ - ((unsigned int )((p)[1]) << B_SHIFT*2) | \ - ((unsigned int )((p)[2]) << B_SHIFT ) | \ - ((unsigned int )((p)[3]) )); \ - (p) += 4;\ +# define GET_ALIGNMENT_PAD_SIZE(addr,pad_size) do {\ + (pad_size) = WORD_ALIGNMENT_SIZE \ + - ((uintptr_t )(addr) % WORD_ALIGNMENT_SIZE);\ + if ((pad_size) == WORD_ALIGNMENT_SIZE) (pad_size) = 0;\ } while (0) -#define GET_8BYTE_INT_INC(type,i,p) do {\ - (i) = (type )(((unsigned long )((p)[0]) << B_SHIFT*7) | \ - ((unsigned long )((p)[1]) << B_SHIFT*6) | \ - ((unsigned long )((p)[2]) << B_SHIFT*5) | \ - ((unsigned long )((p)[3]) << B_SHIFT*4) | \ - ((unsigned long )((p)[4]) << B_SHIFT*3) | \ - ((unsigned long )((p)[5]) << B_SHIFT*2) | \ - ((unsigned long )((p)[6]) << B_SHIFT ) | \ - ((unsigned long )((p)[7]) )); \ - (p) += 8;\ +# define ALIGNMENT_RIGHT(addr) do {\ + (addr) += (WORD_ALIGNMENT_SIZE - 1);\ + (addr) -= ((uintptr_t )(addr) % WORD_ALIGNMENT_SIZE);\ } while (0) -#if SIZEOF_SHORT == 2 -#define GET_SHORT_INC(i,p) GET_2BYTE_INT_INC(short,i,p) -#define SERIALIZE_SHORT(i,p) SERIALIZE_2BYTE_INT(i,p) -#elif SIZEOF_SHORT == 4 -#define GET_SHORT_INC(i,p) GET_4BYTE_INT_INC(short,i,p) -#define SERIALIZE_SHORT(i,p) SERIALIZE_4BYTE_INT(i,p) -#elif SIZEOF_SHORT == 8 -#define GET_SHORT_INC(i,p) GET_8BYTE_INT_INC(short,i,p) -#define SERIALIZE_SHORT(i,p) SERIALIZE_8BYTE_INT(i,p) -#endif - -#if SIZEOF_INT == 2 -#define GET_INT_INC(i,p) GET_2BYTE_INT_INC(int,i,p) -#define GET_UINT_INC(i,p) GET_2BYTE_INT_INC(unsigned,i,p) -#define SERIALIZE_INT(i,p) SERIALIZE_2BYTE_INT(i,p) -#define SERIALIZE_UINT(i,p) SERIALIZE_2BYTE_INT(i,p) -#elif SIZEOF_INT == 4 -#define GET_INT_INC(i,p) GET_4BYTE_INT_INC(int,i,p) -#define GET_UINT_INC(i,p) GET_4BYTE_INT_INC(unsigned,i,p) -#define SERIALIZE_INT(i,p) SERIALIZE_4BYTE_INT(i,p) -#define SERIALIZE_UINT(i,p) SERIALIZE_4BYTE_INT(i,p) -#elif SIZEOF_INT == 8 -#define GET_INT_INC(i,p) GET_8BYTE_INT_INC(int,i,p) -#define GET_UINT_INC(i,p) GET_8BYTE_INT_INC(unsigned,i,p) -#define SERIALIZE_INT(i,p) SERIALIZE_8BYTE_INT(i,p) -#define SERIALIZE_UINT(i,p) SERIALIZE_8BYTE_INT(i,p) -#endif - #endif /* PLATFORM_UNALIGNED_WORD_ACCESS */ /* stack pop level */ @@ -270,9 +347,11 @@ #define ONIG_OPTIMIZE_NONE 0 #define ONIG_OPTIMIZE_EXACT 1 /* Slow Search */ #define ONIG_OPTIMIZE_EXACT_BM 2 /* Boyer Moore Search */ -#define ONIG_OPTIMIZE_EXACT_BM_NOT_REV 3 /* BM (but not simple match) */ +#define ONIG_OPTIMIZE_EXACT_BM_NOT_REV 3 /* BM (applied to a multibyte string) */ #define ONIG_OPTIMIZE_EXACT_IC 4 /* Slow Search (ignore case) */ #define ONIG_OPTIMIZE_MAP 5 /* char map */ +#define ONIG_OPTIMIZE_EXACT_BM_IC 6 /* BM (ignore case) */ +#define ONIG_OPTIMIZE_EXACT_BM_NOT_REV_IC 7 /* BM (applied to a multibyte string) (ignore case) */ /* bit status */ typedef unsigned int BitStatusType; @@ -281,22 +360,23 @@ typedef unsigned int BitStatusType; #define BIT_STATUS_CLEAR(stats) (stats) = 0 #define BIT_STATUS_ON_ALL(stats) (stats) = ~((BitStatusType )0) #define BIT_STATUS_AT(stats,n) \ - ((n) < BIT_STATUS_BITS_NUM ? ((stats) & (1 << n)) : ((stats) & 1)) + ((n) < (int )BIT_STATUS_BITS_NUM ? ((stats) & ((BitStatusType )1 << n)) : ((stats) & 1)) #define BIT_STATUS_ON_AT(stats,n) do {\ - if ((n) < BIT_STATUS_BITS_NUM)\ + if ((n) < (int )BIT_STATUS_BITS_NUM)\ (stats) |= (1 << (n));\ else\ (stats) |= 1;\ } while (0) #define BIT_STATUS_ON_AT_SIMPLE(stats,n) do {\ - if ((n) < BIT_STATUS_BITS_NUM)\ + if ((n) < (int )BIT_STATUS_BITS_NUM)\ (stats) |= (1 << (n));\ } while (0) #define INT_MAX_LIMIT ((1UL << (SIZEOF_INT * 8 - 1)) - 1) +#define LONG_MAX_LIMIT ((1UL << (SIZEOF_LONG * 8 - 1)) - 1) #define DIGITVAL(code) ((code) - '0') #define ODIGITVAL(code) DIGITVAL(code) @@ -310,12 +390,16 @@ typedef unsigned int BitStatusType; #define IS_EXTEND(option) ((option) & ONIG_OPTION_EXTEND) #define IS_FIND_LONGEST(option) ((option) & ONIG_OPTION_FIND_LONGEST) #define IS_FIND_NOT_EMPTY(option) ((option) & ONIG_OPTION_FIND_NOT_EMPTY) -#define IS_POSIXLINE(option) (IS_SINGLELINE(option) && IS_MULTILINE(option)) #define IS_FIND_CONDITION(option) ((option) & \ (ONIG_OPTION_FIND_LONGEST | ONIG_OPTION_FIND_NOT_EMPTY)) #define IS_NOTBOL(option) ((option) & ONIG_OPTION_NOTBOL) #define IS_NOTEOL(option) ((option) & ONIG_OPTION_NOTEOL) -#define IS_POSIX_REGION(option) ((option) & ONIG_OPTION_POSIX_REGION) +#define IS_NOTBOS(option) ((option) & ONIG_OPTION_NOTBOS) +#define IS_NOTEOS(option) ((option) & ONIG_OPTION_NOTEOS) +#define IS_ASCII_RANGE(option) ((option) & ONIG_OPTION_ASCII_RANGE) +#define IS_POSIX_BRACKET_ALL_RANGE(option) ((option) & ONIG_OPTION_POSIX_BRACKET_ALL_RANGE) +#define IS_WORD_BOUND_ALL_RANGE(option) ((option) & ONIG_OPTION_WORD_BOUND_ALL_RANGE) +#define IS_NEWLINE_CRLF(option) ((option) & ONIG_OPTION_NEWLINE_CRLF) /* OP_SET_OPTION is required for these options. #define IS_DYNAMIC_OPTION(option) \ @@ -324,13 +408,16 @@ typedef unsigned int BitStatusType; /* ignore-case and multibyte status are included in compiled code. */ #define IS_DYNAMIC_OPTION(option) 0 +#define DISABLE_CASE_FOLD_MULTI_CHAR(case_fold_flag) \ + ((case_fold_flag) & ~INTERNAL_ONIGENC_CASE_FOLD_MULTI_CHAR) + #define REPEAT_INFINITE -1 #define IS_REPEAT_INFINITE(n) ((n) == REPEAT_INFINITE) /* bitset */ #define BITS_PER_BYTE 8 #define SINGLE_BYTE_SIZE (1 << BITS_PER_BYTE) -#define BITS_IN_ROOM (sizeof(Bits) * BITS_PER_BYTE) +#define BITS_IN_ROOM ((int )sizeof(Bits) * BITS_PER_BYTE) #define BITSET_SIZE (SINGLE_BYTE_SIZE / BITS_IN_ROOM) #ifdef PLATFORM_UNALIGNED_WORD_ACCESS @@ -341,15 +428,15 @@ typedef unsigned char Bits; typedef Bits BitSet[BITSET_SIZE]; typedef Bits* BitSetRef; -#define SIZE_BITSET sizeof(BitSet) +#define SIZE_BITSET (int )sizeof(BitSet) #define BITSET_CLEAR(bs) do {\ int i;\ - for (i = 0; i < BITSET_SIZE; i++) { (bs)[i] = 0; }\ + for (i = 0; i < BITSET_SIZE; i++) { (bs)[i] = 0; } \ } while (0) -#define BS_ROOM(bs,pos) (bs)[pos / BITS_IN_ROOM] -#define BS_BIT(pos) (1 << (pos % BITS_IN_ROOM)) +#define BS_ROOM(bs,pos) (bs)[(int )(pos) / BITS_IN_ROOM] +#define BS_BIT(pos) (1U << ((int )(pos) % BITS_IN_ROOM)) #define BITSET_AT(bs, pos) (BS_ROOM(bs,pos) & BS_BIT(pos)) #define BITSET_SET_BIT(bs, pos) BS_ROOM(bs,pos) |= BS_BIT(pos) @@ -366,29 +453,35 @@ typedef struct _BBuf { #define BBUF_INIT(buf,size) onig_bbuf_init((BBuf* )(buf), (size)) #define BBUF_SIZE_INC(buf,inc) do{\ + UChar *tmp;\ (buf)->alloc += (inc);\ - (buf)->p = (UChar* )xrealloc((buf)->p, (buf)->alloc);\ - if (IS_NULL((buf)->p)) return(ONIGERR_MEMORY);\ + tmp = (UChar* )xrealloc((buf)->p, (buf)->alloc);\ + if (IS_NULL(tmp)) return(ONIGERR_MEMORY);\ + (buf)->p = tmp;\ } while (0) #define BBUF_EXPAND(buf,low) do{\ + UChar *tmp;\ do { (buf)->alloc *= 2; } while ((buf)->alloc < (unsigned int )low);\ - (buf)->p = (UChar* )xrealloc((buf)->p, (buf)->alloc);\ - if (IS_NULL((buf)->p)) return(ONIGERR_MEMORY);\ + tmp = (UChar* )xrealloc((buf)->p, (buf)->alloc);\ + if (IS_NULL(tmp)) return(ONIGERR_MEMORY);\ + (buf)->p = tmp;\ } while (0) #define BBUF_ENSURE_SIZE(buf,size) do{\ unsigned int new_alloc = (buf)->alloc;\ while (new_alloc < (unsigned int )(size)) { new_alloc *= 2; }\ if ((buf)->alloc != new_alloc) {\ - (buf)->p = (UChar* )xrealloc((buf)->p, new_alloc);\ - if (IS_NULL((buf)->p)) return(ONIGERR_MEMORY);\ + UChar *tmp;\ + tmp = (UChar* )xrealloc((buf)->p, new_alloc);\ + if (IS_NULL(tmp)) return(ONIGERR_MEMORY);\ + (buf)->p = tmp;\ (buf)->alloc = new_alloc;\ }\ } while (0) #define BBUF_WRITE(buf,pos,bytes,n) do{\ - int used = (pos) + (n);\ + int used = (pos) + (int )(n);\ if ((buf)->alloc < (unsigned int )used) BBUF_EXPAND((buf),used);\ xmemcpy((buf)->p + (pos), (bytes), (n));\ if ((buf)->used < (unsigned int )used) (buf)->used = used;\ @@ -397,7 +490,7 @@ typedef struct _BBuf { #define BBUF_WRITE1(buf,pos,byte) do{\ int used = (pos) + 1;\ if ((buf)->alloc < (unsigned int )used) BBUF_EXPAND((buf),used);\ - (buf)->p[(pos)] = (byte);\ + (buf)->p[(pos)] = (UChar )(byte);\ if ((buf)->used < (unsigned int )used) (buf)->used = used;\ } while (0) @@ -454,7 +547,9 @@ typedef struct _BBuf { #define ANCHOR_LOOK_BEHIND_NOT (1<<13) #define ANCHOR_ANYCHAR_STAR (1<<14) /* ".*" optimize info */ -#define ANCHOR_ANYCHAR_STAR_PL (1<<15) /* ".*" optimize info (posix-line) */ +#define ANCHOR_ANYCHAR_STAR_ML (1<<15) /* ".*" optimize info (multi-line) */ + +#define ANCHOR_KEEP (1<<16) /* operation code */ enum OpCode { @@ -493,13 +588,18 @@ enum OpCode { OP_WORD, OP_NOT_WORD, - OP_WORD_SB, - OP_WORD_MB, OP_WORD_BOUND, OP_NOT_WORD_BOUND, OP_WORD_BEGIN, OP_WORD_END, + OP_ASCII_WORD, + OP_NOT_ASCII_WORD, + OP_ASCII_WORD_BOUND, + OP_NOT_ASCII_WORD_BOUND, + OP_ASCII_WORD_BEGIN, + OP_ASCII_WORD_END, + OP_BEGIN_BUF, OP_END_BUF, OP_BEGIN_LINE, @@ -509,11 +609,11 @@ enum OpCode { OP_BACKREF1, OP_BACKREF2, - OP_BACKREF3, OP_BACKREFN, OP_BACKREFN_IC, OP_BACKREF_MULTI, OP_BACKREF_MULTI_IC, + OP_BACKREF_WITH_LEVEL, /* \k<xxx+n>, \k<xxx-n> */ OP_MEMORY_START, OP_MEMORY_START_PUSH, /* push back-tracker to stack */ @@ -522,8 +622,7 @@ enum OpCode { OP_MEMORY_END, OP_MEMORY_END_REC, /* push marker to stack */ - OP_SET_OPTION_PUSH, /* set option and push recover option */ - OP_SET_OPTION, /* set option */ + OP_KEEP, OP_FAIL, /* pop stack and move */ OP_JUMP, @@ -551,52 +650,45 @@ enum OpCode { OP_LOOK_BEHIND, /* (?<=...) start (no needs end opcode) */ OP_PUSH_LOOK_BEHIND_NOT, /* (?<!...) start */ OP_FAIL_LOOK_BEHIND_NOT, /* (?<!...) end */ + OP_PUSH_ABSENT_POS, /* (?~...) start */ + OP_ABSENT, /* (?~...) start of inner loop */ + OP_ABSENT_END, /* (?~...) end */ OP_CALL, /* \g<name> */ - OP_RETURN -}; + OP_RETURN, + + OP_CONDITION, -/* arguments type */ -#define ARG_SPECIAL -1 -#define ARG_NON 0 -#define ARG_RELADDR 1 -#define ARG_ABSADDR 2 -#define ARG_LENGTH 3 -#define ARG_MEMNUM 4 -#define ARG_OPTION 5 + OP_STATE_CHECK_PUSH, /* combination explosion check and push */ + OP_STATE_CHECK_PUSH_OR_JUMP, /* check ok -> push, else jump */ + OP_STATE_CHECK, /* check only */ + OP_STATE_CHECK_ANYCHAR_STAR, + OP_STATE_CHECK_ANYCHAR_ML_STAR, + + /* no need: IS_DYNAMIC_OPTION() == 0 */ + OP_SET_OPTION_PUSH, /* set option and push recover option */ + OP_SET_OPTION /* set option */ +}; typedef int RelAddrType; typedef int AbsAddrType; typedef int LengthType; typedef int RepeatNumType; typedef short int MemNumType; +typedef short int StateCheckNumType; +typedef void* PointerType; -#define SIZE_OPCODE 1 -#define SIZE_RELADDR sizeof(RelAddrType) -#define SIZE_ABSADDR sizeof(AbsAddrType) -#define SIZE_LENGTH sizeof(LengthType) -#define SIZE_MEMNUM sizeof(MemNumType) -#define SIZE_REPEATNUM sizeof(RepeatNumType) -#define SIZE_OPTION sizeof(OnigOptionType) -#define SIZE_CODE_POINT sizeof(OnigCodePoint) - - - -#ifdef PLATFORM_UNALIGNED_WORD_ACCESS - -#define PLATFORM_GET_INC(val,p,type) do{\ - val = *(type* )p;\ - (p) += sizeof(type);\ -} while(0) - -#else - -#define PLATFORM_GET_INC(val,p,type) do{\ - xmemcpy(&val, (p), sizeof(type));\ - (p) += sizeof(type);\ -} while(0) +#define SIZE_OPCODE 1 +#define SIZE_RELADDR (int )sizeof(RelAddrType) +#define SIZE_ABSADDR (int )sizeof(AbsAddrType) +#define SIZE_LENGTH (int )sizeof(LengthType) +#define SIZE_MEMNUM (int )sizeof(MemNumType) +#define SIZE_STATE_CHECK_NUM (int )sizeof(StateCheckNumType) +#define SIZE_REPEATNUM (int )sizeof(RepeatNumType) +#define SIZE_OPTION (int )sizeof(OnigOptionType) +#define SIZE_CODE_POINT (int )sizeof(OnigCodePoint) +#define SIZE_POINTER (int )sizeof(PointerType) -#endif /* PLATFORM_UNALIGNED_WORD_ACCESS */ #define GET_RELADDR_INC(addr,p) PLATFORM_GET_INC(addr, p, RelAddrType) #define GET_ABSADDR_INC(addr,p) PLATFORM_GET_INC(addr, p, AbsAddrType) @@ -604,6 +696,8 @@ typedef short int MemNumType; #define GET_MEMNUM_INC(num,p) PLATFORM_GET_INC(num, p, MemNumType) #define GET_REPEATNUM_INC(num,p) PLATFORM_GET_INC(num, p, RepeatNumType) #define GET_OPTION_INC(option,p) PLATFORM_GET_INC(option, p, OnigOptionType) +#define GET_POINTER_INC(ptr,p) PLATFORM_GET_INC(ptr, p, PointerType) +#define GET_STATE_CHECK_NUM_INC(num,p) PLATFORM_GET_INC(num, p, StateCheckNumType) /* code point's address must be aligned address. */ #define GET_CODE_POINT(code,p) code = *((OnigCodePoint* )(p)) @@ -645,14 +739,29 @@ typedef short int MemNumType; #define SIZE_OP_FAIL_LOOK_BEHIND_NOT SIZE_OPCODE #define SIZE_OP_CALL (SIZE_OPCODE + SIZE_ABSADDR) #define SIZE_OP_RETURN SIZE_OPCODE +#define SIZE_OP_CONDITION (SIZE_OPCODE + SIZE_MEMNUM + SIZE_RELADDR) +#define SIZE_OP_PUSH_ABSENT_POS SIZE_OPCODE +#define SIZE_OP_ABSENT (SIZE_OPCODE + SIZE_RELADDR) +#define SIZE_OP_ABSENT_END SIZE_OPCODE + +#ifdef USE_COMBINATION_EXPLOSION_CHECK +# define SIZE_OP_STATE_CHECK (SIZE_OPCODE + SIZE_STATE_CHECK_NUM) +# define SIZE_OP_STATE_CHECK_PUSH (SIZE_OPCODE + SIZE_STATE_CHECK_NUM + SIZE_RELADDR) +# define SIZE_OP_STATE_CHECK_PUSH_OR_JUMP (SIZE_OPCODE + SIZE_STATE_CHECK_NUM + SIZE_RELADDR) +# define SIZE_OP_STATE_CHECK_ANYCHAR_STAR (SIZE_OPCODE + SIZE_STATE_CHECK_NUM) +#endif +#define MC_ESC(syn) (syn)->meta_char_table.esc +#define MC_ANYCHAR(syn) (syn)->meta_char_table.anychar +#define MC_ANYTIME(syn) (syn)->meta_char_table.anytime +#define MC_ZERO_OR_ONE_TIME(syn) (syn)->meta_char_table.zero_or_one_time +#define MC_ONE_OR_MORE_TIME(syn) (syn)->meta_char_table.one_or_more_time +#define MC_ANYCHAR_ANYTIME(syn) (syn)->meta_char_table.anychar_anytime + +#define IS_MC_ESC_CODE(code, syn) \ + ((code) == MC_ESC(syn) && \ + !IS_SYNTAX_OP2((syn), ONIG_SYN_OP2_INEFFECTIVE_ESCAPE)) -#define MC_ESC(enc) (enc)->meta_char_table.esc -#define MC_ANYCHAR(enc) (enc)->meta_char_table.anychar -#define MC_ANYTIME(enc) (enc)->meta_char_table.anytime -#define MC_ZERO_OR_ONE_TIME(enc) (enc)->meta_char_table.zero_or_one_time -#define MC_ONE_OR_MORE_TIME(enc) (enc)->meta_char_table.one_or_more_time -#define MC_ANYCHAR_ANYTIME(enc) (enc)->meta_char_table.anychar_anytime #define SYN_POSIX_COMMON_OP \ ( ONIG_SYN_OP_DOT_ANYCHAR | ONIG_SYN_OP_POSIX_BRACKET | \ @@ -680,33 +789,202 @@ typedef short int MemNumType; ONIG_SYN_BACKSLASH_ESCAPE_IN_CC | ONIG_SYN_ALLOW_DOUBLE_RANGE_OP_IN_CC ) +#define NCCLASS_FLAGS(cc) ((cc)->flags) +#define NCCLASS_FLAG_SET(cc,flag) (NCCLASS_FLAGS(cc) |= (flag)) +#define NCCLASS_FLAG_CLEAR(cc,flag) (NCCLASS_FLAGS(cc) &= ~(flag)) +#define IS_NCCLASS_FLAG_ON(cc,flag) ((NCCLASS_FLAGS(cc) & (flag)) != 0) + +/* cclass node */ +#define FLAG_NCCLASS_NOT (1<<0) + +#define NCCLASS_SET_NOT(nd) NCCLASS_FLAG_SET(nd, FLAG_NCCLASS_NOT) +#define NCCLASS_CLEAR_NOT(nd) NCCLASS_FLAG_CLEAR(nd, FLAG_NCCLASS_NOT) +#define IS_NCCLASS_NOT(nd) IS_NCCLASS_FLAG_ON(nd, FLAG_NCCLASS_NOT) + +typedef struct { + int type; + /* struct _Node* next; */ + /* unsigned int flags; */ +} NodeBase; + +typedef struct { + NodeBase base; + unsigned int flags; + BitSet bs; + BBuf* mbuf; /* multi-byte info or NULL */ +} CClassNode; + +typedef intptr_t OnigStackIndex; + +typedef struct _OnigStackType { + unsigned int type; + OnigStackIndex null_check; + union { + struct { + UChar *pcode; /* byte code position */ + UChar *pstr; /* string position */ + UChar *pstr_prev; /* previous char position of pstr */ +#ifdef USE_COMBINATION_EXPLOSION_CHECK + unsigned int state_check; +#endif + UChar *pkeep; /* keep pattern position */ + } state; + struct { + int count; /* for OP_REPEAT_INC, OP_REPEAT_INC_NG */ + UChar *pcode; /* byte code position (head of repeated target) */ + int num; /* repeat id */ + } repeat; + struct { + OnigStackIndex si; /* index of stack */ + } repeat_inc; + struct { + int num; /* memory num */ + UChar *pstr; /* start/end position */ + /* Following information is set, if this stack type is MEM-START */ + OnigStackIndex start; /* prev. info (for backtrack "(...)*" ) */ + OnigStackIndex end; /* prev. info (for backtrack "(...)*" ) */ + } mem; + struct { + int num; /* null check id */ + UChar *pstr; /* start position */ + } null_check; +#ifdef USE_SUBEXP_CALL + struct { + UChar *ret_addr; /* byte code position */ + int num; /* null check id */ + UChar *pstr; /* string position */ + } call_frame; +#endif + struct { + UChar *abs_pstr; /* absent start position */ + const UChar *end_pstr; /* end position */ + } absent_pos; +#ifdef USE_MATCH_CACHE + struct { + long index; /* index of the match cache buffer */ + uint8_t mask; /* bit-mask for the match cache buffer */ + } match_cache_point; +#endif + } u; +} OnigStackType; + +#ifdef USE_MATCH_CACHE +typedef struct { + UChar *addr; + long cache_point; + int outer_repeat_mem; + long num_cache_points_at_outer_repeat; + long num_cache_points_in_outer_repeat; + int lookaround_nesting; + UChar *match_addr; +} OnigCacheOpcode; +#endif + +typedef struct { + void* stack_p; + size_t stack_n; + OnigOptionType options; + OnigRegion* region; + const UChar* start; /* search start position */ + const UChar* gpos; /* global position (for \G: BEGIN_POSITION) */ +#ifdef USE_FIND_LONGEST_SEARCH_ALL_OF_RANGE + OnigPosition best_len; /* for ONIG_OPTION_FIND_LONGEST */ + UChar* best_s; +#endif +#ifdef USE_COMBINATION_EXPLOSION_CHECK + void* state_check_buff; + int state_check_buff_size; +#endif + int counter; + /* rb_hrtime_t from hrtime.h */ +#ifdef MY_RUBY_BUILD_MAY_TIME_TRAVEL + int128_t end_time; +#else + uint64_t end_time; +#endif +#ifdef USE_MATCH_CACHE + int match_cache_status; + long num_fails; + long num_cache_opcodes; + OnigCacheOpcode* cache_opcodes; + long num_cache_points; + uint8_t* match_cache_buf; +#endif +} OnigMatchArg; + +#define NUM_CACHE_OPCODES_UNINIT 1 +#define NUM_CACHE_OPCODES_IMPOSSIBLE -1 + +#define MATCH_CACHE_STATUS_UNINIT 1 +#define MATCH_CACHE_STATUS_INIT 2 +#define MATCH_CACHE_STATUS_DISABLED -1 +#define MATCH_CACHE_STATUS_ENABLED 0 + +#define IS_CODE_SB_WORD(enc,code) \ + (ONIGENC_IS_CODE_ASCII(code) && ONIGENC_IS_CODE_WORD(enc,code)) + +typedef struct OnigEndCallListItem { + struct OnigEndCallListItem* next; + void (*func)(void); +} OnigEndCallListItemType; + +extern void onig_add_end_call(void (*func)(void)); + + #ifdef ONIG_DEBUG typedef struct { short int opcode; - char* name; + const char* name; short int arg_type; } OnigOpInfoType; extern OnigOpInfoType OnigOpInfo[]; -extern void onig_print_compiled_byte_code P_((FILE* f, UChar* bp, UChar** nextp, OnigEncoding enc)); -#ifdef ONIG_DEBUG_STATISTICS -extern void onig_statistics_init P_((void)); -extern void onig_print_statistics P_((FILE* f)); +extern void onig_print_compiled_byte_code(FILE* f, UChar* bp, UChar* bpend, UChar** nextp, OnigEncoding enc); + +# ifdef ONIG_DEBUG_STATISTICS +extern void onig_statistics_init(void); +extern void onig_print_statistics(FILE* f); +# endif +#endif + +#ifndef PRINTF_ARGS +#define PRINTF_ARGS(func, fmt, vargs) func +#endif + +extern UChar* onig_error_code_to_format(OnigPosition code); +PRINTF_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); +PRINTF_ARGS(extern void onig_snprintf_with_pattern(UChar buf[], int bufsize, OnigEncoding enc, UChar* pat, UChar* pat_end, const char *fmt, ...), 6, 7); +extern int onig_bbuf_init(BBuf* buf, OnigDistance size); +extern int onig_compile(regex_t* reg, const UChar* pattern, const UChar* pattern_end, OnigErrorInfo* einfo); +#ifdef RUBY +extern int onig_compile_ruby(regex_t* reg, const UChar* pattern, const UChar* pattern_end, OnigErrorInfo* einfo, const char *sourcefile, int sourceline); #endif +extern void onig_transfer(regex_t* to, regex_t* from); +extern int onig_is_code_in_cc(OnigEncoding enc, OnigCodePoint code, CClassNode* cc); +extern int onig_is_code_in_cc_len(int enclen, OnigCodePoint code, CClassNode* cc); + +/* strend hash */ +typedef void hash_table_type; +#ifdef RUBY +# include "ruby/st.h" +#else +# include "st.h" +#endif +typedef st_data_t hash_data_type; + +extern hash_table_type* onig_st_init_strend_table_with_size(st_index_t size); +extern int onig_st_lookup_strend(hash_table_type* table, const UChar* str_key, const UChar* end_key, hash_data_type *value); +extern int onig_st_insert_strend(hash_table_type* table, const UChar* str_key, const UChar* end_key, hash_data_type value); + +#ifdef RUBY +extern size_t onig_memsize(const regex_t *reg); +extern size_t onig_region_memsize(const struct re_registers *regs); +bool rb_reg_timeout_p(regex_t *reg, void *end_time); #endif -extern char* onig_error_code_to_format P_((int code)); -extern void onig_snprintf_with_pattern PV_((char buf[], int bufsize, OnigEncoding enc, char* pat, char* pat_end, char *fmt, ...)); -extern UChar* onig_strdup P_((UChar* s, UChar* end)); -extern int onig_bbuf_init P_((BBuf* buf, int size)); -extern int onig_alloc_init P_((regex_t** reg, OnigOptionType option, OnigAmbigType ambig_flag, OnigEncoding enc, OnigSyntaxType* syntax)); -extern int onig_compile P_((regex_t* reg, UChar* pattern, UChar* pattern_end, OnigErrorInfo* einfo)); -extern void onig_chain_reduce P_((regex_t* reg)); -extern void onig_chain_link_add P_((regex_t* to, regex_t* add)); -extern void onig_transfer P_((regex_t* to, regex_t* from)); -extern int onig_is_in_code_range P_((UChar* p, OnigCodePoint code)); +RUBY_SYMBOL_EXPORT_END -#endif /* REGINT_H */ +#endif /* ONIGMO_REGINT_H */ |
