xref: /PHP-7.2/ext/mbstring/oniguruma/src/regint.h (revision c2c60fca)
1 #ifndef REGINT_H
2 #define REGINT_H
3 /**********************************************************************
4   regint.h -  Oniguruma (regular expression library)
5 **********************************************************************/
6 /*-
7  * Copyright (c) 2002-2013  K.Kosako  <sndgk393 AT ybb DOT ne DOT jp>
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 /* for debug */
33 /* #define ONIG_DEBUG_PARSE_TREE */
34 /* #define ONIG_DEBUG_COMPILE */
35 /* #define ONIG_DEBUG_SEARCH */
36 /* #define ONIG_DEBUG_MATCH */
37 /* #define ONIG_DONT_OPTIMIZE */
38 
39 /* for byte-code statistical data. */
40 /* #define ONIG_DEBUG_STATISTICS */
41 
42 #if defined(ONIG_DEBUG_PARSE_TREE) || defined(ONIG_DEBUG_MATCH) || \
43     defined(ONIG_DEBUG_SEARCH) || defined(ONIG_DEBUG_COMPILE) || \
44     defined(ONIG_DEBUG_STATISTICS)
45 #ifndef ONIG_DEBUG
46 #define ONIG_DEBUG
47 #endif
48 #endif
49 
50 #if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \
51     (defined(__ppc__) && defined(__APPLE__)) || \
52     defined(__x86_64) || defined(__x86_64__) || \
53     defined(__mc68020__)
54 #define PLATFORM_UNALIGNED_WORD_ACCESS
55 #endif
56 
57 /* config */
58 /* spec. config */
59 #define USE_NAMED_GROUP
60 #define USE_SUBEXP_CALL
61 #define USE_BACKREF_WITH_LEVEL        /* \k<name+n>, \k<name-n> */
62 #define USE_MONOMANIAC_CHECK_CAPTURES_IN_ENDLESS_REPEAT  /* /(?:()|())*\2/ */
63 #define USE_NEWLINE_AT_END_OF_STRING_HAS_EMPTY_LINE     /* /\n$/ =~ "\n" */
64 #define USE_WARNING_REDUNDANT_NESTED_REPEAT_OPERATOR
65 /* !!! moved to regenc.h. */ /* #define USE_CRNL_AS_LINE_TERMINATOR */
66 
67 /* internal config */
68 #define USE_OP_PUSH_OR_JUMP_EXACT
69 #define USE_QTFR_PEEK_NEXT
70 #define USE_ST_LIBRARY
71 
72 #define INIT_MATCH_STACK_SIZE                     160
73 #define DEFAULT_MATCH_STACK_LIMIT_SIZE              0 /* unlimited */
74 #define DEFAULT_PARSE_DEPTH_LIMIT                4096
75 
76 #if defined(__GNUC__)
77 #  define ARG_UNUSED  __attribute__ ((unused))
78 #else
79 #  define ARG_UNUSED
80 #endif
81 
82 /* */
83 /* escape other system UChar definition */
84 #include "config.h"
85 #ifdef ONIG_ESCAPE_UCHAR_COLLISION
86 #undef ONIG_ESCAPE_UCHAR_COLLISION
87 #endif
88 
89 #define USE_WORD_BEGIN_END        /* "\<", "\>" */
90 #define USE_CAPTURE_HISTORY
91 #define USE_VARIABLE_META_CHARS
92 #define USE_POSIX_API_REGION_OPTION
93 #define USE_FIND_LONGEST_SEARCH_ALL_OF_RANGE
94 /* #define USE_COMBINATION_EXPLOSION_CHECK */     /* (X*)* */
95 
96 #define xmalloc     malloc
97 #define xrealloc    realloc
98 #define xcalloc     calloc
99 #define xfree       free
100 
101 #define CHECK_INTERRUPT_IN_MATCH_AT
102 
103 #define st_init_table                  onig_st_init_table
104 #define st_init_table_with_size        onig_st_init_table_with_size
105 #define st_init_numtable               onig_st_init_numtable
106 #define st_init_numtable_with_size     onig_st_init_numtable_with_size
107 #define st_init_strtable               onig_st_init_strtable
108 #define st_init_strtable_with_size     onig_st_init_strtable_with_size
109 #define st_delete                      onig_st_delete
110 #define st_delete_safe                 onig_st_delete_safe
111 #define st_insert                      onig_st_insert
112 #define st_lookup                      onig_st_lookup
113 #define st_foreach                     onig_st_foreach
114 #define st_add_direct                  onig_st_add_direct
115 #define st_free_table                  onig_st_free_table
116 #define st_cleanup_safe                onig_st_cleanup_safe
117 #define st_copy                        onig_st_copy
118 #define st_nothing_key_clone           onig_st_nothing_key_clone
119 #define st_nothing_key_free            onig_st_nothing_key_free
120 /* */
121 #define onig_st_is_member              st_is_member
122 
123 #define STATE_CHECK_STRING_THRESHOLD_LEN             7
124 #define STATE_CHECK_BUFF_MAX_SIZE               0x4000
125 
126 #define xmemset     memset
127 #define xmemcpy     memcpy
128 #define xmemmove    memmove
129 
130 #if defined(_WIN32) && !defined(__GNUC__)
131 #define xalloca     _alloca
132 #define xvsnprintf(buf,size,fmt,args)  _vsnprintf_s(buf,size,_TRUNCATE,fmt,args)
133 #define xsnprintf   sprintf_s
134 #define xstrcat(dest,src,size)   strcat_s(dest,size,src)
135 #else
136 #define xalloca     alloca
137 #define xvsnprintf  vsnprintf
138 #define xsnprintf   snprintf
139 #define xstrcat(dest,src,size)   strcat(dest,src)
140 #endif
141 
142 
143 #ifdef HAVE_STDLIB_H
144 #include <stdlib.h>
145 #endif
146 
147 #if defined(HAVE_ALLOCA_H) && !defined(__GNUC__)
148 #include <alloca.h>
149 #endif
150 
151 #ifdef HAVE_STRING_H
152 # include <string.h>
153 #else
154 # include <strings.h>
155 #endif
156 
157 #include <ctype.h>
158 #ifdef HAVE_SYS_TYPES_H
159 #ifndef __BORLANDC__
160 #include <sys/types.h>
161 #endif
162 #endif
163 
164 #ifdef __BORLANDC__
165 #include <malloc.h>
166 #endif
167 
168 #ifdef ONIG_DEBUG
169 # include <stdio.h>
170 #endif
171 
172 #include "regenc.h"
173 
174 #ifdef MIN
175 #undef MIN
176 #endif
177 #ifdef MAX
178 #undef MAX
179 #endif
180 #define MIN(a,b) (((a)>(b))?(b):(a))
181 #define MAX(a,b) (((a)<(b))?(b):(a))
182 
183 #define IS_NULL(p)                    (((void*)(p)) == (void*)0)
184 #define IS_NOT_NULL(p)                (((void*)(p)) != (void*)0)
185 #define CHECK_NULL_RETURN(p)          if (IS_NULL(p)) return NULL
186 #define CHECK_NULL_RETURN_MEMERR(p)   if (IS_NULL(p)) return ONIGERR_MEMORY
187 #define NULL_UCHARP                   ((UChar* )0)
188 
189 #ifdef PLATFORM_UNALIGNED_WORD_ACCESS
190 
191 #define PLATFORM_GET_INC(val,p,type) do{\
192   val  = *(type* )p;\
193   (p) += sizeof(type);\
194 } while(0)
195 
196 #else
197 
198 #define PLATFORM_GET_INC(val,p,type) do{\
199   xmemcpy(&val, (p), sizeof(type));\
200   (p) += sizeof(type);\
201 } while(0)
202 
203 /* sizeof(OnigCodePoint) */
204 #ifdef SIZEOF_SIZE_T
205 # define WORD_ALIGNMENT_SIZE     SIZEOF_SIZE_T
206 #else
207 # define WORD_ALIGNMENT_SIZE     SIZEOF_LONG
208 #endif
209 
210 #define GET_ALIGNMENT_PAD_SIZE(addr,pad_size) do {\
211   (pad_size) = WORD_ALIGNMENT_SIZE \
212                - ((size_t)(addr) % WORD_ALIGNMENT_SIZE);\
213   if ((pad_size) == WORD_ALIGNMENT_SIZE) (pad_size) = 0;\
214 } while (0)
215 
216 #define ALIGNMENT_RIGHT(addr) do {\
217   (addr) += (WORD_ALIGNMENT_SIZE - 1);\
218   (addr) -= ((size_t)(addr) % WORD_ALIGNMENT_SIZE);\
219 } while (0)
220 
221 #endif /* PLATFORM_UNALIGNED_WORD_ACCESS */
222 
223 /* stack pop level */
224 #define STACK_POP_LEVEL_FREE        0
225 #define STACK_POP_LEVEL_MEM_START   1
226 #define STACK_POP_LEVEL_ALL         2
227 
228 /* optimize flags */
229 #define ONIG_OPTIMIZE_NONE              0
230 #define ONIG_OPTIMIZE_EXACT             1   /* Slow Search */
231 #define ONIG_OPTIMIZE_EXACT_BM          2   /* Boyer Moore Search */
232 #define ONIG_OPTIMIZE_EXACT_BM_NOT_REV  3   /* BM   (but not simple match) */
233 #define ONIG_OPTIMIZE_EXACT_IC          4   /* Slow Search (ignore case) */
234 #define ONIG_OPTIMIZE_MAP               5   /* char map */
235 
236 /* bit status */
237 typedef unsigned int  BitStatusType;
238 
239 #define BIT_STATUS_BITS_NUM          (sizeof(BitStatusType) * 8)
240 #define BIT_STATUS_CLEAR(stats)      (stats) = 0
241 #define BIT_STATUS_ON_ALL(stats)     (stats) = ~((BitStatusType )0)
242 #define BIT_STATUS_AT(stats,n) \
243   ((n) < (int )BIT_STATUS_BITS_NUM  ?  ((stats) & (1 << n)) : ((stats) & 1))
244 
245 #define BIT_STATUS_ON_AT(stats,n) do {\
246     if ((n) < (int )BIT_STATUS_BITS_NUM)	\
247     (stats) |= (1 << (n));\
248   else\
249     (stats) |= 1;\
250 } while (0)
251 
252 #define BIT_STATUS_ON_AT_SIMPLE(stats,n) do {\
253     if ((n) < (int )BIT_STATUS_BITS_NUM)\
254     (stats) |= (1 << (n));\
255 } while (0)
256 
257 
258 #define INT_MAX_LIMIT           ((1UL << (SIZEOF_INT * 8 - 1)) - 1)
259 
260 #define DIGITVAL(code)    ((code) - '0')
261 #define ODIGITVAL(code)   DIGITVAL(code)
262 #define XDIGITVAL(enc,code) \
263   (ONIGENC_IS_CODE_DIGIT(enc,code) ? DIGITVAL(code) \
264    : (ONIGENC_IS_CODE_UPPER(enc,code) ? (code) - 'A' + 10 : (code) - 'a' + 10))
265 
266 #define IS_SINGLELINE(option)     ((option) & ONIG_OPTION_SINGLELINE)
267 #define IS_MULTILINE(option)      ((option) & ONIG_OPTION_MULTILINE)
268 #define IS_IGNORECASE(option)     ((option) & ONIG_OPTION_IGNORECASE)
269 #define IS_EXTEND(option)         ((option) & ONIG_OPTION_EXTEND)
270 #define IS_FIND_LONGEST(option)   ((option) & ONIG_OPTION_FIND_LONGEST)
271 #define IS_FIND_NOT_EMPTY(option) ((option) & ONIG_OPTION_FIND_NOT_EMPTY)
272 #define IS_FIND_CONDITION(option) ((option) & \
273           (ONIG_OPTION_FIND_LONGEST | ONIG_OPTION_FIND_NOT_EMPTY))
274 #define IS_NOTBOL(option)         ((option) & ONIG_OPTION_NOTBOL)
275 #define IS_NOTEOL(option)         ((option) & ONIG_OPTION_NOTEOL)
276 #define IS_POSIX_REGION(option)   ((option) & ONIG_OPTION_POSIX_REGION)
277 
278 /* OP_SET_OPTION is required for these options.
279 #define IS_DYNAMIC_OPTION(option) \
280   (((option) & (ONIG_OPTION_MULTILINE | ONIG_OPTION_IGNORECASE)) != 0)
281 */
282 /* ignore-case and multibyte status are included in compiled code. */
283 #define IS_DYNAMIC_OPTION(option)  0
284 
285 #define DISABLE_CASE_FOLD_MULTI_CHAR(case_fold_flag) \
286   ((case_fold_flag) & ~INTERNAL_ONIGENC_CASE_FOLD_MULTI_CHAR)
287 
288 #define REPEAT_INFINITE         -1
289 #define IS_REPEAT_INFINITE(n)   ((n) == REPEAT_INFINITE)
290 
291 /* bitset */
292 #define BITS_PER_BYTE      8
293 #define SINGLE_BYTE_SIZE   (1 << BITS_PER_BYTE)
294 #define BITS_IN_ROOM       (sizeof(Bits) * BITS_PER_BYTE)
295 #define BITSET_SIZE        (SINGLE_BYTE_SIZE / BITS_IN_ROOM)
296 
297 #ifdef PLATFORM_UNALIGNED_WORD_ACCESS
298 typedef unsigned int   Bits;
299 #else
300 typedef unsigned char  Bits;
301 #endif
302 typedef Bits           BitSet[BITSET_SIZE];
303 typedef Bits*          BitSetRef;
304 
305 #define SIZE_BITSET        sizeof(BitSet)
306 
307 #define BITSET_CLEAR(bs) do {\
308   int i;\
309   for (i = 0; i < (int )BITSET_SIZE; i++) { (bs)[i] = 0; }	\
310 } while (0)
311 
312 #define BS_ROOM(bs,pos)            (bs)[pos / BITS_IN_ROOM]
313 #define BS_BIT(pos)                (1 << (pos % BITS_IN_ROOM))
314 
315 #define BITSET_AT(bs, pos)         (BS_ROOM(bs,pos) & BS_BIT(pos))
316 #define BITSET_SET_BIT(bs, pos)     BS_ROOM(bs,pos) |= BS_BIT(pos)
317 #define BITSET_CLEAR_BIT(bs, pos)   BS_ROOM(bs,pos) &= ~(BS_BIT(pos))
318 #define BITSET_INVERT_BIT(bs, pos)  BS_ROOM(bs,pos) ^= BS_BIT(pos)
319 
320 /* bytes buffer */
321 typedef struct _BBuf {
322   UChar* p;
323   unsigned int used;
324   unsigned int alloc;
325 } BBuf;
326 
327 #define BBUF_INIT(buf,size)    onig_bbuf_init((BBuf* )(buf), (size))
328 
329 #define BBUF_SIZE_INC(buf,inc) do{\
330   (buf)->alloc += (inc);\
331   (buf)->p = (UChar* )xrealloc((buf)->p, (buf)->alloc);\
332   if (IS_NULL((buf)->p)) return(ONIGERR_MEMORY);\
333 } while (0)
334 
335 #define BBUF_EXPAND(buf,low) do{\
336   do { (buf)->alloc *= 2; } while ((buf)->alloc < (unsigned int )low);\
337   (buf)->p = (UChar* )xrealloc((buf)->p, (buf)->alloc);\
338   if (IS_NULL((buf)->p)) return(ONIGERR_MEMORY);\
339 } while (0)
340 
341 #define BBUF_ENSURE_SIZE(buf,size) do{\
342   unsigned int new_alloc = (buf)->alloc;\
343   while (new_alloc < (unsigned int )(size)) { new_alloc *= 2; }\
344   if ((buf)->alloc != new_alloc) {\
345     (buf)->p = (UChar* )xrealloc((buf)->p, new_alloc);\
346     if (IS_NULL((buf)->p)) return(ONIGERR_MEMORY);\
347     (buf)->alloc = new_alloc;\
348   }\
349 } while (0)
350 
351 #define BBUF_WRITE(buf,pos,bytes,n) do{\
352   int used = (pos) + (n);\
353   if ((buf)->alloc < (unsigned int )used) BBUF_EXPAND((buf),used);\
354   xmemcpy((buf)->p + (pos), (bytes), (n));\
355   if ((buf)->used < (unsigned int )used) (buf)->used = used;\
356 } while (0)
357 
358 #define BBUF_WRITE1(buf,pos,byte) do{\
359   int used = (pos) + 1;\
360   if ((buf)->alloc < (unsigned int )used) BBUF_EXPAND((buf),used);\
361   (buf)->p[(pos)] = (byte);\
362   if ((buf)->used < (unsigned int )used) (buf)->used = used;\
363 } while (0)
364 
365 #define BBUF_ADD(buf,bytes,n)       BBUF_WRITE((buf),(buf)->used,(bytes),(n))
366 #define BBUF_ADD1(buf,byte)         BBUF_WRITE1((buf),(buf)->used,(byte))
367 #define BBUF_GET_ADD_ADDRESS(buf)   ((buf)->p + (buf)->used)
368 #define BBUF_GET_OFFSET_POS(buf)    ((buf)->used)
369 
370 /* from < to */
371 #define BBUF_MOVE_RIGHT(buf,from,to,n) do {\
372   if ((unsigned int )((to)+(n)) > (buf)->alloc) BBUF_EXPAND((buf),(to) + (n));\
373   xmemmove((buf)->p + (to), (buf)->p + (from), (n));\
374   if ((unsigned int )((to)+(n)) > (buf)->used) (buf)->used = (to) + (n);\
375 } while (0)
376 
377 /* from > to */
378 #define BBUF_MOVE_LEFT(buf,from,to,n) do {\
379   xmemmove((buf)->p + (to), (buf)->p + (from), (n));\
380 } while (0)
381 
382 /* from > to */
383 #define BBUF_MOVE_LEFT_REDUCE(buf,from,to) do {\
384   xmemmove((buf)->p + (to), (buf)->p + (from), (buf)->used - (from));\
385   (buf)->used -= (from - to);\
386 } while (0)
387 
388 #define BBUF_INSERT(buf,pos,bytes,n) do {\
389   if (pos >= (buf)->used) {\
390     BBUF_WRITE(buf,pos,bytes,n);\
391   }\
392   else {\
393     BBUF_MOVE_RIGHT((buf),(pos),(pos) + (n),((buf)->used - (pos)));\
394     xmemcpy((buf)->p + (pos), (bytes), (n));\
395   }\
396 } while (0)
397 
398 #define BBUF_GET_BYTE(buf, pos) (buf)->p[(pos)]
399 
400 
401 #define ANCHOR_BEGIN_BUF        (1<<0)
402 #define ANCHOR_BEGIN_LINE       (1<<1)
403 #define ANCHOR_BEGIN_POSITION   (1<<2)
404 #define ANCHOR_END_BUF          (1<<3)
405 #define ANCHOR_SEMI_END_BUF     (1<<4)
406 #define ANCHOR_END_LINE         (1<<5)
407 
408 #define ANCHOR_WORD_BOUND       (1<<6)
409 #define ANCHOR_NOT_WORD_BOUND   (1<<7)
410 #define ANCHOR_WORD_BEGIN       (1<<8)
411 #define ANCHOR_WORD_END         (1<<9)
412 #define ANCHOR_PREC_READ        (1<<10)
413 #define ANCHOR_PREC_READ_NOT    (1<<11)
414 #define ANCHOR_LOOK_BEHIND      (1<<12)
415 #define ANCHOR_LOOK_BEHIND_NOT  (1<<13)
416 
417 #define ANCHOR_ANYCHAR_STAR     (1<<14)   /* ".*" optimize info */
418 #define ANCHOR_ANYCHAR_STAR_ML  (1<<15)   /* ".*" optimize info (multi-line) */
419 
420 /* operation code */
421 enum OpCode {
422   OP_FINISH = 0,        /* matching process terminator (no more alternative) */
423   OP_END    = 1,        /* pattern code terminator (success end) */
424 
425   OP_EXACT1 = 2,        /* single byte, N = 1 */
426   OP_EXACT2,            /* single byte, N = 2 */
427   OP_EXACT3,            /* single byte, N = 3 */
428   OP_EXACT4,            /* single byte, N = 4 */
429   OP_EXACT5,            /* single byte, N = 5 */
430   OP_EXACTN,            /* single byte */
431   OP_EXACTMB2N1,        /* mb-length = 2 N = 1 */
432   OP_EXACTMB2N2,        /* mb-length = 2 N = 2 */
433   OP_EXACTMB2N3,        /* mb-length = 2 N = 3 */
434   OP_EXACTMB2N,         /* mb-length = 2 */
435   OP_EXACTMB3N,         /* mb-length = 3 */
436   OP_EXACTMBN,          /* other length */
437 
438   OP_EXACT1_IC,         /* single byte, N = 1, ignore case */
439   OP_EXACTN_IC,         /* single byte,        ignore case */
440 
441   OP_CCLASS,
442   OP_CCLASS_MB,
443   OP_CCLASS_MIX,
444   OP_CCLASS_NOT,
445   OP_CCLASS_MB_NOT,
446   OP_CCLASS_MIX_NOT,
447   OP_CCLASS_NODE,       /* pointer to CClassNode node */
448 
449   OP_ANYCHAR,                 /* "."  */
450   OP_ANYCHAR_ML,              /* "."  multi-line */
451   OP_ANYCHAR_STAR,            /* ".*" */
452   OP_ANYCHAR_ML_STAR,         /* ".*" multi-line */
453   OP_ANYCHAR_STAR_PEEK_NEXT,
454   OP_ANYCHAR_ML_STAR_PEEK_NEXT,
455 
456   OP_WORD,
457   OP_NOT_WORD,
458   OP_WORD_BOUND,
459   OP_NOT_WORD_BOUND,
460   OP_WORD_BEGIN,
461   OP_WORD_END,
462 
463   OP_BEGIN_BUF,
464   OP_END_BUF,
465   OP_BEGIN_LINE,
466   OP_END_LINE,
467   OP_SEMI_END_BUF,
468   OP_BEGIN_POSITION,
469 
470   OP_BACKREF1,
471   OP_BACKREF2,
472   OP_BACKREFN,
473   OP_BACKREFN_IC,
474   OP_BACKREF_MULTI,
475   OP_BACKREF_MULTI_IC,
476   OP_BACKREF_WITH_LEVEL,    /* \k<xxx+n>, \k<xxx-n> */
477 
478   OP_MEMORY_START,
479   OP_MEMORY_START_PUSH,   /* push back-tracker to stack */
480   OP_MEMORY_END_PUSH,     /* push back-tracker to stack */
481   OP_MEMORY_END_PUSH_REC, /* push back-tracker to stack */
482   OP_MEMORY_END,
483   OP_MEMORY_END_REC,      /* push marker to stack */
484 
485   OP_FAIL,               /* pop stack and move */
486   OP_JUMP,
487   OP_PUSH,
488   OP_POP,
489   OP_PUSH_OR_JUMP_EXACT1,  /* if match exact then push, else jump. */
490   OP_PUSH_IF_PEEK_NEXT,    /* if match exact then push, else none. */
491   OP_REPEAT,               /* {n,m} */
492   OP_REPEAT_NG,            /* {n,m}? (non greedy) */
493   OP_REPEAT_INC,
494   OP_REPEAT_INC_NG,        /* non greedy */
495   OP_REPEAT_INC_SG,        /* search and get in stack */
496   OP_REPEAT_INC_NG_SG,     /* search and get in stack (non greedy) */
497   OP_NULL_CHECK_START,     /* null loop checker start */
498   OP_NULL_CHECK_END,       /* null loop checker end   */
499   OP_NULL_CHECK_END_MEMST, /* null loop checker end (with capture status) */
500   OP_NULL_CHECK_END_MEMST_PUSH, /* with capture status and push check-end */
501 
502   OP_PUSH_POS,             /* (?=...)  start */
503   OP_POP_POS,              /* (?=...)  end   */
504   OP_PUSH_POS_NOT,         /* (?!...)  start */
505   OP_FAIL_POS,             /* (?!...)  end   */
506   OP_PUSH_STOP_BT,         /* (?>...)  start */
507   OP_POP_STOP_BT,          /* (?>...)  end   */
508   OP_LOOK_BEHIND,          /* (?<=...) start (no needs end opcode) */
509   OP_PUSH_LOOK_BEHIND_NOT, /* (?<!...) start */
510   OP_FAIL_LOOK_BEHIND_NOT, /* (?<!...) end   */
511 
512   OP_CALL,                 /* \g<name> */
513   OP_RETURN,
514 
515   OP_STATE_CHECK_PUSH,         /* combination explosion check and push */
516   OP_STATE_CHECK_PUSH_OR_JUMP, /* check ok -> push, else jump  */
517   OP_STATE_CHECK,              /* check only */
518   OP_STATE_CHECK_ANYCHAR_STAR,
519   OP_STATE_CHECK_ANYCHAR_ML_STAR,
520 
521   /* no need: IS_DYNAMIC_OPTION() == 0 */
522   OP_SET_OPTION_PUSH,    /* set option and push recover option */
523   OP_SET_OPTION          /* set option */
524 };
525 
526 typedef int RelAddrType;
527 typedef int AbsAddrType;
528 typedef int LengthType;
529 typedef int RepeatNumType;
530 typedef int MemNumType;
531 typedef short int StateCheckNumType;
532 typedef void* PointerType;
533 
534 #define SIZE_OPCODE           1
535 #define SIZE_RELADDR          sizeof(RelAddrType)
536 #define SIZE_ABSADDR          sizeof(AbsAddrType)
537 #define SIZE_LENGTH           sizeof(LengthType)
538 #define SIZE_MEMNUM           sizeof(MemNumType)
539 #define SIZE_STATE_CHECK_NUM  sizeof(StateCheckNumType)
540 #define SIZE_REPEATNUM        sizeof(RepeatNumType)
541 #define SIZE_OPTION           sizeof(OnigOptionType)
542 #define SIZE_CODE_POINT       sizeof(OnigCodePoint)
543 #define SIZE_POINTER          sizeof(PointerType)
544 
545 
546 #define GET_RELADDR_INC(addr,p)    PLATFORM_GET_INC(addr,   p, RelAddrType)
547 #define GET_ABSADDR_INC(addr,p)    PLATFORM_GET_INC(addr,   p, AbsAddrType)
548 #define GET_LENGTH_INC(len,p)      PLATFORM_GET_INC(len,    p, LengthType)
549 #define GET_MEMNUM_INC(num,p)      PLATFORM_GET_INC(num,    p, MemNumType)
550 #define GET_REPEATNUM_INC(num,p)   PLATFORM_GET_INC(num,    p, RepeatNumType)
551 #define GET_OPTION_INC(option,p)   PLATFORM_GET_INC(option, p, OnigOptionType)
552 #define GET_POINTER_INC(ptr,p)     PLATFORM_GET_INC(ptr,    p, PointerType)
553 #define GET_STATE_CHECK_NUM_INC(num,p)  PLATFORM_GET_INC(num, p, StateCheckNumType)
554 
555 /* code point's address must be aligned address. */
556 #define GET_CODE_POINT(code,p)   code = *((OnigCodePoint* )(p))
557 #define GET_BYTE_INC(byte,p) do{\
558   byte = *(p);\
559   (p)++;\
560 } while(0)
561 
562 
563 /* op-code + arg size */
564 #define SIZE_OP_ANYCHAR_STAR            SIZE_OPCODE
565 #define SIZE_OP_ANYCHAR_STAR_PEEK_NEXT (SIZE_OPCODE + 1)
566 #define SIZE_OP_JUMP                   (SIZE_OPCODE + SIZE_RELADDR)
567 #define SIZE_OP_PUSH                   (SIZE_OPCODE + SIZE_RELADDR)
568 #define SIZE_OP_POP                     SIZE_OPCODE
569 #define SIZE_OP_PUSH_OR_JUMP_EXACT1    (SIZE_OPCODE + SIZE_RELADDR + 1)
570 #define SIZE_OP_PUSH_IF_PEEK_NEXT      (SIZE_OPCODE + SIZE_RELADDR + 1)
571 #define SIZE_OP_REPEAT_INC             (SIZE_OPCODE + SIZE_MEMNUM)
572 #define SIZE_OP_REPEAT_INC_NG          (SIZE_OPCODE + SIZE_MEMNUM)
573 #define SIZE_OP_PUSH_POS                SIZE_OPCODE
574 #define SIZE_OP_PUSH_POS_NOT           (SIZE_OPCODE + SIZE_RELADDR)
575 #define SIZE_OP_POP_POS                 SIZE_OPCODE
576 #define SIZE_OP_FAIL_POS                SIZE_OPCODE
577 #define SIZE_OP_SET_OPTION             (SIZE_OPCODE + SIZE_OPTION)
578 #define SIZE_OP_SET_OPTION_PUSH        (SIZE_OPCODE + SIZE_OPTION)
579 #define SIZE_OP_FAIL                    SIZE_OPCODE
580 #define SIZE_OP_MEMORY_START           (SIZE_OPCODE + SIZE_MEMNUM)
581 #define SIZE_OP_MEMORY_START_PUSH      (SIZE_OPCODE + SIZE_MEMNUM)
582 #define SIZE_OP_MEMORY_END_PUSH        (SIZE_OPCODE + SIZE_MEMNUM)
583 #define SIZE_OP_MEMORY_END_PUSH_REC    (SIZE_OPCODE + SIZE_MEMNUM)
584 #define SIZE_OP_MEMORY_END             (SIZE_OPCODE + SIZE_MEMNUM)
585 #define SIZE_OP_MEMORY_END_REC         (SIZE_OPCODE + SIZE_MEMNUM)
586 #define SIZE_OP_PUSH_STOP_BT            SIZE_OPCODE
587 #define SIZE_OP_POP_STOP_BT             SIZE_OPCODE
588 #define SIZE_OP_NULL_CHECK_START       (SIZE_OPCODE + SIZE_MEMNUM)
589 #define SIZE_OP_NULL_CHECK_END         (SIZE_OPCODE + SIZE_MEMNUM)
590 #define SIZE_OP_LOOK_BEHIND            (SIZE_OPCODE + SIZE_LENGTH)
591 #define SIZE_OP_PUSH_LOOK_BEHIND_NOT   (SIZE_OPCODE + SIZE_RELADDR + SIZE_LENGTH)
592 #define SIZE_OP_FAIL_LOOK_BEHIND_NOT    SIZE_OPCODE
593 #define SIZE_OP_CALL                   (SIZE_OPCODE + SIZE_ABSADDR)
594 #define SIZE_OP_RETURN                  SIZE_OPCODE
595 
596 #ifdef USE_COMBINATION_EXPLOSION_CHECK
597 #define SIZE_OP_STATE_CHECK            (SIZE_OPCODE + SIZE_STATE_CHECK_NUM)
598 #define SIZE_OP_STATE_CHECK_PUSH       (SIZE_OPCODE + SIZE_STATE_CHECK_NUM + SIZE_RELADDR)
599 #define SIZE_OP_STATE_CHECK_PUSH_OR_JUMP (SIZE_OPCODE + SIZE_STATE_CHECK_NUM + SIZE_RELADDR)
600 #define SIZE_OP_STATE_CHECK_ANYCHAR_STAR (SIZE_OPCODE + SIZE_STATE_CHECK_NUM)
601 #endif
602 
603 #define MC_ESC(syn)               (syn)->meta_char_table.esc
604 #define MC_ANYCHAR(syn)           (syn)->meta_char_table.anychar
605 #define MC_ANYTIME(syn)           (syn)->meta_char_table.anytime
606 #define MC_ZERO_OR_ONE_TIME(syn)  (syn)->meta_char_table.zero_or_one_time
607 #define MC_ONE_OR_MORE_TIME(syn)  (syn)->meta_char_table.one_or_more_time
608 #define MC_ANYCHAR_ANYTIME(syn)   (syn)->meta_char_table.anychar_anytime
609 
610 #define IS_MC_ESC_CODE(code, syn) \
611   ((code) == MC_ESC(syn) && \
612    !IS_SYNTAX_OP2((syn), ONIG_SYN_OP2_INEFFECTIVE_ESCAPE))
613 
614 
615 #define SYN_POSIX_COMMON_OP \
616  ( ONIG_SYN_OP_DOT_ANYCHAR | ONIG_SYN_OP_POSIX_BRACKET | \
617    ONIG_SYN_OP_DECIMAL_BACKREF | \
618    ONIG_SYN_OP_BRACKET_CC | ONIG_SYN_OP_ASTERISK_ZERO_INF | \
619    ONIG_SYN_OP_LINE_ANCHOR | \
620    ONIG_SYN_OP_ESC_CONTROL_CHARS )
621 
622 #define SYN_GNU_REGEX_OP \
623   ( ONIG_SYN_OP_DOT_ANYCHAR | ONIG_SYN_OP_BRACKET_CC | \
624     ONIG_SYN_OP_POSIX_BRACKET | ONIG_SYN_OP_DECIMAL_BACKREF | \
625     ONIG_SYN_OP_BRACE_INTERVAL | ONIG_SYN_OP_LPAREN_SUBEXP | \
626     ONIG_SYN_OP_VBAR_ALT | \
627     ONIG_SYN_OP_ASTERISK_ZERO_INF | ONIG_SYN_OP_PLUS_ONE_INF | \
628     ONIG_SYN_OP_QMARK_ZERO_ONE | \
629     ONIG_SYN_OP_ESC_AZ_BUF_ANCHOR | ONIG_SYN_OP_ESC_CAPITAL_G_BEGIN_ANCHOR | \
630     ONIG_SYN_OP_ESC_W_WORD | \
631     ONIG_SYN_OP_ESC_B_WORD_BOUND | ONIG_SYN_OP_ESC_LTGT_WORD_BEGIN_END | \
632     ONIG_SYN_OP_ESC_S_WHITE_SPACE | ONIG_SYN_OP_ESC_D_DIGIT | \
633     ONIG_SYN_OP_LINE_ANCHOR )
634 
635 #define SYN_GNU_REGEX_BV \
636   ( ONIG_SYN_CONTEXT_INDEP_ANCHORS | ONIG_SYN_CONTEXT_INDEP_REPEAT_OPS | \
637     ONIG_SYN_CONTEXT_INVALID_REPEAT_OPS | ONIG_SYN_ALLOW_INVALID_INTERVAL | \
638     ONIG_SYN_BACKSLASH_ESCAPE_IN_CC | ONIG_SYN_ALLOW_DOUBLE_RANGE_OP_IN_CC )
639 
640 
641 #define NCCLASS_FLAGS(cc)           ((cc)->flags)
642 #define NCCLASS_FLAG_SET(cc,flag)    (NCCLASS_FLAGS(cc) |= (flag))
643 #define NCCLASS_FLAG_CLEAR(cc,flag)  (NCCLASS_FLAGS(cc) &= ~(flag))
644 #define IS_NCCLASS_FLAG_ON(cc,flag) ((NCCLASS_FLAGS(cc) & (flag)) != 0)
645 
646 /* cclass node */
647 #define FLAG_NCCLASS_NOT           (1<<0)
648 #define FLAG_NCCLASS_SHARE         (1<<1)
649 
650 #define NCCLASS_SET_NOT(nd)     NCCLASS_FLAG_SET(nd, FLAG_NCCLASS_NOT)
651 #define NCCLASS_SET_SHARE(nd)   NCCLASS_FLAG_SET(nd, FLAG_NCCLASS_SHARE)
652 #define NCCLASS_CLEAR_NOT(nd)   NCCLASS_FLAG_CLEAR(nd, FLAG_NCCLASS_NOT)
653 #define IS_NCCLASS_NOT(nd)      IS_NCCLASS_FLAG_ON(nd, FLAG_NCCLASS_NOT)
654 #define IS_NCCLASS_SHARE(nd)    IS_NCCLASS_FLAG_ON(nd, FLAG_NCCLASS_SHARE)
655 
656 typedef struct {
657   int type;
658   /* struct _Node* next; */
659   /* unsigned int flags; */
660 } NodeBase;
661 
662 typedef struct {
663   NodeBase base;
664   unsigned int flags;
665   BitSet bs;
666   BBuf*  mbuf;   /* multi-byte info or NULL */
667 } CClassNode;
668 
669 #ifdef _WIN64
670 typedef __int64 OnigStackIndex;
671 #else
672 typedef long OnigStackIndex;
673 #endif
674 
675 typedef struct _OnigStackType {
676   unsigned int type;
677   union {
678     struct {
679       UChar *pcode;      /* byte code position */
680       UChar *pstr;       /* string position */
681       UChar *pstr_prev;  /* previous char position of pstr */
682 #ifdef USE_COMBINATION_EXPLOSION_CHECK
683       unsigned int state_check;
684 #endif
685     } state;
686     struct {
687       int   count;       /* for OP_REPEAT_INC, OP_REPEAT_INC_NG */
688       UChar *pcode;      /* byte code position (head of repeated target) */
689       int   num;         /* repeat id */
690     } repeat;
691     struct {
692       OnigStackIndex si;     /* index of stack */
693     } repeat_inc;
694     struct {
695       int num;           /* memory num */
696       UChar *pstr;       /* start/end position */
697       /* Following information is set, if this stack type is MEM-START */
698       OnigStackIndex start;  /* prev. info (for backtrack  "(...)*" ) */
699       OnigStackIndex end;    /* prev. info (for backtrack  "(...)*" ) */
700     } mem;
701     struct {
702       int num;           /* null check id */
703       UChar *pstr;       /* start position */
704     } null_check;
705 #ifdef USE_SUBEXP_CALL
706     struct {
707       UChar *ret_addr;   /* byte code position */
708       int    num;        /* null check id */
709       UChar *pstr;       /* string position */
710     } call_frame;
711 #endif
712   } u;
713 } OnigStackType;
714 
715 typedef struct {
716   void* stack_p;
717   int   stack_n;
718   OnigOptionType options;
719   OnigRegion*    region;
720   int   ptr_num;
721   const UChar* start;   /* search start position (for \G: BEGIN_POSITION) */
722 #ifdef USE_FIND_LONGEST_SEARCH_ALL_OF_RANGE
723   int    best_len;      /* for ONIG_OPTION_FIND_LONGEST */
724   UChar* best_s;
725 #endif
726 #ifdef USE_COMBINATION_EXPLOSION_CHECK
727   void* state_check_buff;
728   int   state_check_buff_size;
729 #endif
730 } OnigMatchArg;
731 
732 
733 #define IS_CODE_SB_WORD(enc,code) \
734   (ONIGENC_IS_CODE_ASCII(code) && ONIGENC_IS_CODE_WORD(enc,code))
735 
736 typedef struct OnigEndCallListItem {
737   struct OnigEndCallListItem* next;
738   void (*func)(void);
739 } OnigEndCallListItemType;
740 
741 extern void onig_add_end_call(void (*func)(void));
742 
743 
744 #ifdef ONIG_DEBUG
745 
746 typedef struct {
747   short int opcode;
748   char*     name;
749   short int arg_type;
750 } OnigOpInfoType;
751 
752 extern OnigOpInfoType OnigOpInfo[];
753 
754 
755 extern void onig_print_compiled_byte_code P_((FILE* f, UChar* bp, UChar** nextp, OnigEncoding enc));
756 
757 #ifdef ONIG_DEBUG_STATISTICS
758 extern void onig_statistics_init P_((void));
759 extern int  onig_print_statistics P_((FILE* f));
760 #endif
761 #endif
762 
763 extern void onig_warning(const char* s);
764 extern UChar* onig_error_code_to_format P_((int code));
765 extern void  onig_snprintf_with_pattern PV_((UChar buf[], int bufsize, OnigEncoding enc, UChar* pat, UChar* pat_end, const UChar *fmt, ...));
766 extern int  onig_bbuf_init P_((BBuf* buf, int size));
767 extern int  onig_compile P_((regex_t* reg, const UChar* pattern, const UChar* pattern_end, OnigErrorInfo* einfo));
768 extern void onig_transfer P_((regex_t* to, regex_t* from));
769 extern int  onig_is_code_in_cc P_((OnigEncoding enc, OnigCodePoint code, CClassNode* cc));
770 extern int  onig_is_code_in_cc_len P_((int enclen, OnigCodePoint code, CClassNode* cc));
771 
772 /* strend hash */
773 typedef void hash_table_type;
774 #ifdef _WIN32
775 # include <windows.h>
776 typedef ULONG_PTR hash_data_type;
777 #else
778 typedef unsigned long hash_data_type;
779 #endif
780 
781 extern hash_table_type* onig_st_init_strend_table_with_size P_((int size));
782 extern int onig_st_lookup_strend P_((hash_table_type* table, const UChar* str_key, const UChar* end_key, hash_data_type *value));
783 extern int onig_st_insert_strend P_((hash_table_type* table, const UChar* str_key, const UChar* end_key, hash_data_type value));
784 
785 typedef int (*ONIGENC_INIT_PROPERTY_LIST_FUNC_TYPE)(void);
786 
787 #endif /* REGINT_H */
788