source: EcnlProtoTool/trunk/onigmo-5.15.0/src/regint.h@ 279

Last change on this file since 279 was 279, checked in by coas-nagasima, 7 years ago

ファイルを追加、更新。

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