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