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