1 #ifndef REGINT_H
2 #define REGINT_H
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
58
59 #define USE_NAMED_GROUP
60 #define USE_SUBEXP_CALL
61 #define USE_BACKREF_WITH_LEVEL
62 #define USE_MONOMANIAC_CHECK_CAPTURES_IN_ENDLESS_REPEAT
63 #define USE_NEWLINE_AT_END_OF_STRING_HAS_EMPTY_LINE
64 #define USE_WARNING_REDUNDANT_NESTED_REPEAT_OPERATOR
65
66
67
68
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
77
78 #if defined(__GNUC__)
79 # define ARG_UNUSED __attribute__ ((unused))
80 #else
81 # define ARG_UNUSED
82 #endif
83
84
85
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
97
98
99 #define THREAD_SYSTEM_INIT
100 #define THREAD_SYSTEM_END
101 #define THREAD_ATOMIC_START
102 #define THREAD_ATOMIC_END
103 #define THREAD_PASS
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)
164 #define ONIG_STATE_DEC(reg)
165 #define ONIG_STATE_INC_THREAD(reg)
166 #define ONIG_STATE_DEC_THREAD(reg)
167 #endif
168
169 #ifdef HAVE_STDLIB_H
170 #include <stdlib.h>
171 #endif
172
173 #if defined(HAVE_ALLOCA_H) && !defined(__GNUC__)
174 #include <alloca.h>
175 #endif
176
177 #ifdef HAVE_STRING_H
178 # include <string.h>
179 #else
180 # include <strings.h>
181 #endif
182
183 #include <ctype.h>
184 #ifdef HAVE_SYS_TYPES_H
185 #ifndef __BORLANDC__
186 #include <sys/types.h>
187 #endif
188 #endif
189
190 #ifdef __BORLANDC__
191 #include <malloc.h>
192 #endif
193
194 #ifdef ONIG_DEBUG
195 # include <stdio.h>
196 #endif
197
198 #include "regenc.h"
199
200 #ifdef MIN
201 #undef MIN
202 #endif
203 #ifdef MAX
204 #undef MAX
205 #endif
206 #define MIN(a,b) (((a)>(b))?(b):(a))
207 #define MAX(a,b) (((a)<(b))?(b):(a))
208
209 #define IS_NULL(p) (((void*)(p)) == (void*)0)
210 #define IS_NOT_NULL(p) (((void*)(p)) != (void*)0)
211 #define CHECK_NULL_RETURN(p) if (IS_NULL(p)) return NULL
212 #define CHECK_NULL_RETURN_MEMERR(p) if (IS_NULL(p)) return ONIGERR_MEMORY
213 #define NULL_UCHARP ((UChar* )0)
214
215 #ifdef PLATFORM_UNALIGNED_WORD_ACCESS
216
217 #define PLATFORM_GET_INC(val,p,type) do{\
218 val = *(type* )p;\
219 (p) += sizeof(type);\
220 } while(0)
221
222 #else
223
224 #define PLATFORM_GET_INC(val,p,type) do{\
225 xmemcpy(&val, (p), sizeof(type));\
226 (p) += sizeof(type);\
227 } while(0)
228
229
230 #define WORD_ALIGNMENT_SIZE SIZEOF_LONG
231
232 #define GET_ALIGNMENT_PAD_SIZE(addr,pad_size) do {\
233 (pad_size) = WORD_ALIGNMENT_SIZE \
234 - ((unsigned int )(addr) % WORD_ALIGNMENT_SIZE);\
235 if ((pad_size) == WORD_ALIGNMENT_SIZE) (pad_size) = 0;\
236 } while (0)
237
238 #define ALIGNMENT_RIGHT(addr) do {\
239 (addr) += (WORD_ALIGNMENT_SIZE - 1);\
240 (addr) -= ((unsigned int )(addr) % WORD_ALIGNMENT_SIZE);\
241 } while (0)
242
243 #endif
244
245
246 #define STACK_POP_LEVEL_FREE 0
247 #define STACK_POP_LEVEL_MEM_START 1
248 #define STACK_POP_LEVEL_ALL 2
249
250
251 #define ONIG_OPTIMIZE_NONE 0
252 #define ONIG_OPTIMIZE_EXACT 1
253 #define ONIG_OPTIMIZE_EXACT_BM 2
254 #define ONIG_OPTIMIZE_EXACT_BM_NOT_REV 3
255 #define ONIG_OPTIMIZE_EXACT_IC 4
256 #define ONIG_OPTIMIZE_MAP 5
257
258
259 typedef unsigned int BitStatusType;
260
261 #define BIT_STATUS_BITS_NUM (sizeof(BitStatusType) * 8)
262 #define BIT_STATUS_CLEAR(stats) (stats) = 0
263 #define BIT_STATUS_ON_ALL(stats) (stats) = ~((BitStatusType )0)
264 #define BIT_STATUS_AT(stats,n) \
265 ((n) < (int )BIT_STATUS_BITS_NUM ? ((stats) & (1 << n)) : ((stats) & 1))
266
267 #define BIT_STATUS_ON_AT(stats,n) do {\
268 if ((n) < (int )BIT_STATUS_BITS_NUM) \
269 (stats) |= (1 << (n));\
270 else\
271 (stats) |= 1;\
272 } while (0)
273
274 #define BIT_STATUS_ON_AT_SIMPLE(stats,n) do {\
275 if ((n) < (int )BIT_STATUS_BITS_NUM)\
276 (stats) |= (1 << (n));\
277 } while (0)
278
279
280 #define INT_MAX_LIMIT ((1UL << (SIZEOF_INT * 8 - 1)) - 1)
281
282 #define DIGITVAL(code) ((code) - '0')
283 #define ODIGITVAL(code) DIGITVAL(code)
284 #define XDIGITVAL(enc,code) \
285 (ONIGENC_IS_CODE_DIGIT(enc,code) ? DIGITVAL(code) \
286 : (ONIGENC_IS_CODE_UPPER(enc,code) ? (code) - 'A' + 10 : (code) - 'a' + 10))
287
288 #define IS_SINGLELINE(option) ((option) & ONIG_OPTION_SINGLELINE)
289 #define IS_MULTILINE(option) ((option) & ONIG_OPTION_MULTILINE)
290 #define IS_IGNORECASE(option) ((option) & ONIG_OPTION_IGNORECASE)
291 #define IS_EXTEND(option) ((option) & ONIG_OPTION_EXTEND)
292 #define IS_FIND_LONGEST(option) ((option) & ONIG_OPTION_FIND_LONGEST)
293 #define IS_FIND_NOT_EMPTY(option) ((option) & ONIG_OPTION_FIND_NOT_EMPTY)
294 #define IS_FIND_CONDITION(option) ((option) & \
295 (ONIG_OPTION_FIND_LONGEST | ONIG_OPTION_FIND_NOT_EMPTY))
296 #define IS_NOTBOL(option) ((option) & ONIG_OPTION_NOTBOL)
297 #define IS_NOTEOL(option) ((option) & ONIG_OPTION_NOTEOL)
298 #define IS_POSIX_REGION(option) ((option) & ONIG_OPTION_POSIX_REGION)
299
300
301
302
303
304
305 #define IS_DYNAMIC_OPTION(option) 0
306
307 #define DISABLE_CASE_FOLD_MULTI_CHAR(case_fold_flag) \
308 ((case_fold_flag) & ~INTERNAL_ONIGENC_CASE_FOLD_MULTI_CHAR)
309
310 #define REPEAT_INFINITE -1
311 #define IS_REPEAT_INFINITE(n) ((n) == REPEAT_INFINITE)
312
313
314 #define BITS_PER_BYTE 8
315 #define SINGLE_BYTE_SIZE (1 << BITS_PER_BYTE)
316 #define BITS_IN_ROOM (sizeof(Bits) * BITS_PER_BYTE)
317 #define BITSET_SIZE (SINGLE_BYTE_SIZE / BITS_IN_ROOM)
318
319 #ifdef PLATFORM_UNALIGNED_WORD_ACCESS
320 typedef unsigned int Bits;
321 #else
322 typedef unsigned char Bits;
323 #endif
324 typedef Bits BitSet[BITSET_SIZE];
325 typedef Bits* BitSetRef;
326
327 #define SIZE_BITSET sizeof(BitSet)
328
329 #define BITSET_CLEAR(bs) do {\
330 int i;\
331 for (i = 0; i < (int )BITSET_SIZE; i++) { (bs)[i] = 0; } \
332 } while (0)
333
334 #define BS_ROOM(bs,pos) (bs)[pos / BITS_IN_ROOM]
335 #define BS_BIT(pos) (1 << (pos % BITS_IN_ROOM))
336
337 #define BITSET_AT(bs, pos) (BS_ROOM(bs,pos) & BS_BIT(pos))
338 #define BITSET_SET_BIT(bs, pos) BS_ROOM(bs,pos) |= BS_BIT(pos)
339 #define BITSET_CLEAR_BIT(bs, pos) BS_ROOM(bs,pos) &= ~(BS_BIT(pos))
340 #define BITSET_INVERT_BIT(bs, pos) BS_ROOM(bs,pos) ^= BS_BIT(pos)
341
342
343 typedef struct _BBuf {
344 UChar* p;
345 unsigned int used;
346 unsigned int alloc;
347 } BBuf;
348
349 #define BBUF_INIT(buf,size) onig_bbuf_init((BBuf* )(buf), (size))
350
351 #define BBUF_SIZE_INC(buf,inc) do{\
352 (buf)->alloc += (inc);\
353 (buf)->p = (UChar* )xrealloc((buf)->p, (buf)->alloc);\
354 if (IS_NULL((buf)->p)) return(ONIGERR_MEMORY);\
355 } while (0)
356
357 #define BBUF_EXPAND(buf,low) do{\
358 do { (buf)->alloc *= 2; } while ((buf)->alloc < (unsigned int )low);\
359 (buf)->p = (UChar* )xrealloc((buf)->p, (buf)->alloc);\
360 if (IS_NULL((buf)->p)) return(ONIGERR_MEMORY);\
361 } while (0)
362
363 #define BBUF_ENSURE_SIZE(buf,size) do{\
364 unsigned int new_alloc = (buf)->alloc;\
365 while (new_alloc < (unsigned int )(size)) { new_alloc *= 2; }\
366 if ((buf)->alloc != new_alloc) {\
367 (buf)->p = (UChar* )xrealloc((buf)->p, new_alloc);\
368 if (IS_NULL((buf)->p)) return(ONIGERR_MEMORY);\
369 (buf)->alloc = new_alloc;\
370 }\
371 } while (0)
372
373 #define BBUF_WRITE(buf,pos,bytes,n) do{\
374 int used = (pos) + (n);\
375 if ((buf)->alloc < (unsigned int )used) BBUF_EXPAND((buf),used);\
376 xmemcpy((buf)->p + (pos), (bytes), (n));\
377 if ((buf)->used < (unsigned int )used) (buf)->used = used;\
378 } while (0)
379
380 #define BBUF_WRITE1(buf,pos,byte) do{\
381 int used = (pos) + 1;\
382 if ((buf)->alloc < (unsigned int )used) BBUF_EXPAND((buf),used);\
383 (buf)->p[(pos)] = (byte);\
384 if ((buf)->used < (unsigned int )used) (buf)->used = used;\
385 } while (0)
386
387 #define BBUF_ADD(buf,bytes,n) BBUF_WRITE((buf),(buf)->used,(bytes),(n))
388 #define BBUF_ADD1(buf,byte) BBUF_WRITE1((buf),(buf)->used,(byte))
389 #define BBUF_GET_ADD_ADDRESS(buf) ((buf)->p + (buf)->used)
390 #define BBUF_GET_OFFSET_POS(buf) ((buf)->used)
391
392
393 #define BBUF_MOVE_RIGHT(buf,from,to,n) do {\
394 if ((unsigned int )((to)+(n)) > (buf)->alloc) BBUF_EXPAND((buf),(to) + (n));\
395 xmemmove((buf)->p + (to), (buf)->p + (from), (n));\
396 if ((unsigned int )((to)+(n)) > (buf)->used) (buf)->used = (to) + (n);\
397 } while (0)
398
399
400 #define BBUF_MOVE_LEFT(buf,from,to,n) do {\
401 xmemmove((buf)->p + (to), (buf)->p + (from), (n));\
402 } while (0)
403
404
405 #define BBUF_MOVE_LEFT_REDUCE(buf,from,to) do {\
406 xmemmove((buf)->p + (to), (buf)->p + (from), (buf)->used - (from));\
407 (buf)->used -= (from - to);\
408 } while (0)
409
410 #define BBUF_INSERT(buf,pos,bytes,n) do {\
411 if (pos >= (buf)->used) {\
412 BBUF_WRITE(buf,pos,bytes,n);\
413 }\
414 else {\
415 BBUF_MOVE_RIGHT((buf),(pos),(pos) + (n),((buf)->used - (pos)));\
416 xmemcpy((buf)->p + (pos), (bytes), (n));\
417 }\
418 } while (0)
419
420 #define BBUF_GET_BYTE(buf, pos) (buf)->p[(pos)]
421
422
423 #define ANCHOR_BEGIN_BUF (1<<0)
424 #define ANCHOR_BEGIN_LINE (1<<1)
425 #define ANCHOR_BEGIN_POSITION (1<<2)
426 #define ANCHOR_END_BUF (1<<3)
427 #define ANCHOR_SEMI_END_BUF (1<<4)
428 #define ANCHOR_END_LINE (1<<5)
429
430 #define ANCHOR_WORD_BOUND (1<<6)
431 #define ANCHOR_NOT_WORD_BOUND (1<<7)
432 #define ANCHOR_WORD_BEGIN (1<<8)
433 #define ANCHOR_WORD_END (1<<9)
434 #define ANCHOR_PREC_READ (1<<10)
435 #define ANCHOR_PREC_READ_NOT (1<<11)
436 #define ANCHOR_LOOK_BEHIND (1<<12)
437 #define ANCHOR_LOOK_BEHIND_NOT (1<<13)
438
439 #define ANCHOR_ANYCHAR_STAR (1<<14)
440 #define ANCHOR_ANYCHAR_STAR_ML (1<<15)
441
442
443 enum OpCode {
444 OP_FINISH = 0,
445 OP_END = 1,
446
447 OP_EXACT1 = 2,
448 OP_EXACT2,
449 OP_EXACT3,
450 OP_EXACT4,
451 OP_EXACT5,
452 OP_EXACTN,
453 OP_EXACTMB2N1,
454 OP_EXACTMB2N2,
455 OP_EXACTMB2N3,
456 OP_EXACTMB2N,
457 OP_EXACTMB3N,
458 OP_EXACTMBN,
459
460 OP_EXACT1_IC,
461 OP_EXACTN_IC,
462
463 OP_CCLASS,
464 OP_CCLASS_MB,
465 OP_CCLASS_MIX,
466 OP_CCLASS_NOT,
467 OP_CCLASS_MB_NOT,
468 OP_CCLASS_MIX_NOT,
469 OP_CCLASS_NODE,
470
471 OP_ANYCHAR,
472 OP_ANYCHAR_ML,
473 OP_ANYCHAR_STAR,
474 OP_ANYCHAR_ML_STAR,
475 OP_ANYCHAR_STAR_PEEK_NEXT,
476 OP_ANYCHAR_ML_STAR_PEEK_NEXT,
477
478 OP_WORD,
479 OP_NOT_WORD,
480 OP_WORD_BOUND,
481 OP_NOT_WORD_BOUND,
482 OP_WORD_BEGIN,
483 OP_WORD_END,
484
485 OP_BEGIN_BUF,
486 OP_END_BUF,
487 OP_BEGIN_LINE,
488 OP_END_LINE,
489 OP_SEMI_END_BUF,
490 OP_BEGIN_POSITION,
491
492 OP_BACKREF1,
493 OP_BACKREF2,
494 OP_BACKREFN,
495 OP_BACKREFN_IC,
496 OP_BACKREF_MULTI,
497 OP_BACKREF_MULTI_IC,
498 OP_BACKREF_WITH_LEVEL,
499
500 OP_MEMORY_START,
501 OP_MEMORY_START_PUSH,
502 OP_MEMORY_END_PUSH,
503 OP_MEMORY_END_PUSH_REC,
504 OP_MEMORY_END,
505 OP_MEMORY_END_REC,
506
507 OP_FAIL,
508 OP_JUMP,
509 OP_PUSH,
510 OP_POP,
511 OP_PUSH_OR_JUMP_EXACT1,
512 OP_PUSH_IF_PEEK_NEXT,
513 OP_REPEAT,
514 OP_REPEAT_NG,
515 OP_REPEAT_INC,
516 OP_REPEAT_INC_NG,
517 OP_REPEAT_INC_SG,
518 OP_REPEAT_INC_NG_SG,
519 OP_NULL_CHECK_START,
520 OP_NULL_CHECK_END,
521 OP_NULL_CHECK_END_MEMST,
522 OP_NULL_CHECK_END_MEMST_PUSH,
523
524 OP_PUSH_POS,
525 OP_POP_POS,
526 OP_PUSH_POS_NOT,
527 OP_FAIL_POS,
528 OP_PUSH_STOP_BT,
529 OP_POP_STOP_BT,
530 OP_LOOK_BEHIND,
531 OP_PUSH_LOOK_BEHIND_NOT,
532 OP_FAIL_LOOK_BEHIND_NOT,
533
534 OP_CALL,
535 OP_RETURN,
536
537 OP_STATE_CHECK_PUSH,
538 OP_STATE_CHECK_PUSH_OR_JUMP,
539 OP_STATE_CHECK,
540 OP_STATE_CHECK_ANYCHAR_STAR,
541 OP_STATE_CHECK_ANYCHAR_ML_STAR,
542
543
544 OP_SET_OPTION_PUSH,
545 OP_SET_OPTION
546 };
547
548 typedef int RelAddrType;
549 typedef int AbsAddrType;
550 typedef int LengthType;
551 typedef int RepeatNumType;
552 typedef short int MemNumType;
553 typedef short int StateCheckNumType;
554 typedef void* PointerType;
555
556 #define SIZE_OPCODE 1
557 #define SIZE_RELADDR sizeof(RelAddrType)
558 #define SIZE_ABSADDR sizeof(AbsAddrType)
559 #define SIZE_LENGTH sizeof(LengthType)
560 #define SIZE_MEMNUM sizeof(MemNumType)
561 #define SIZE_STATE_CHECK_NUM sizeof(StateCheckNumType)
562 #define SIZE_REPEATNUM sizeof(RepeatNumType)
563 #define SIZE_OPTION sizeof(OnigOptionType)
564 #define SIZE_CODE_POINT sizeof(OnigCodePoint)
565 #define SIZE_POINTER sizeof(PointerType)
566
567
568 #define GET_RELADDR_INC(addr,p) PLATFORM_GET_INC(addr, p, RelAddrType)
569 #define GET_ABSADDR_INC(addr,p) PLATFORM_GET_INC(addr, p, AbsAddrType)
570 #define GET_LENGTH_INC(len,p) PLATFORM_GET_INC(len, p, LengthType)
571 #define GET_MEMNUM_INC(num,p) PLATFORM_GET_INC(num, p, MemNumType)
572 #define GET_REPEATNUM_INC(num,p) PLATFORM_GET_INC(num, p, RepeatNumType)
573 #define GET_OPTION_INC(option,p) PLATFORM_GET_INC(option, p, OnigOptionType)
574 #define GET_POINTER_INC(ptr,p) PLATFORM_GET_INC(ptr, p, PointerType)
575 #define GET_STATE_CHECK_NUM_INC(num,p) PLATFORM_GET_INC(num, p, StateCheckNumType)
576
577
578 #define GET_CODE_POINT(code,p) code = *((OnigCodePoint* )(p))
579 #define GET_BYTE_INC(byte,p) do{\
580 byte = *(p);\
581 (p)++;\
582 } while(0)
583
584
585
586 #define SIZE_OP_ANYCHAR_STAR SIZE_OPCODE
587 #define SIZE_OP_ANYCHAR_STAR_PEEK_NEXT (SIZE_OPCODE + 1)
588 #define SIZE_OP_JUMP (SIZE_OPCODE + SIZE_RELADDR)
589 #define SIZE_OP_PUSH (SIZE_OPCODE + SIZE_RELADDR)
590 #define SIZE_OP_POP SIZE_OPCODE
591 #define SIZE_OP_PUSH_OR_JUMP_EXACT1 (SIZE_OPCODE + SIZE_RELADDR + 1)
592 #define SIZE_OP_PUSH_IF_PEEK_NEXT (SIZE_OPCODE + SIZE_RELADDR + 1)
593 #define SIZE_OP_REPEAT_INC (SIZE_OPCODE + SIZE_MEMNUM)
594 #define SIZE_OP_REPEAT_INC_NG (SIZE_OPCODE + SIZE_MEMNUM)
595 #define SIZE_OP_PUSH_POS SIZE_OPCODE
596 #define SIZE_OP_PUSH_POS_NOT (SIZE_OPCODE + SIZE_RELADDR)
597 #define SIZE_OP_POP_POS SIZE_OPCODE
598 #define SIZE_OP_FAIL_POS SIZE_OPCODE
599 #define SIZE_OP_SET_OPTION (SIZE_OPCODE + SIZE_OPTION)
600 #define SIZE_OP_SET_OPTION_PUSH (SIZE_OPCODE + SIZE_OPTION)
601 #define SIZE_OP_FAIL SIZE_OPCODE
602 #define SIZE_OP_MEMORY_START (SIZE_OPCODE + SIZE_MEMNUM)
603 #define SIZE_OP_MEMORY_START_PUSH (SIZE_OPCODE + SIZE_MEMNUM)
604 #define SIZE_OP_MEMORY_END_PUSH (SIZE_OPCODE + SIZE_MEMNUM)
605 #define SIZE_OP_MEMORY_END_PUSH_REC (SIZE_OPCODE + SIZE_MEMNUM)
606 #define SIZE_OP_MEMORY_END (SIZE_OPCODE + SIZE_MEMNUM)
607 #define SIZE_OP_MEMORY_END_REC (SIZE_OPCODE + SIZE_MEMNUM)
608 #define SIZE_OP_PUSH_STOP_BT SIZE_OPCODE
609 #define SIZE_OP_POP_STOP_BT SIZE_OPCODE
610 #define SIZE_OP_NULL_CHECK_START (SIZE_OPCODE + SIZE_MEMNUM)
611 #define SIZE_OP_NULL_CHECK_END (SIZE_OPCODE + SIZE_MEMNUM)
612 #define SIZE_OP_LOOK_BEHIND (SIZE_OPCODE + SIZE_LENGTH)
613 #define SIZE_OP_PUSH_LOOK_BEHIND_NOT (SIZE_OPCODE + SIZE_RELADDR + SIZE_LENGTH)
614 #define SIZE_OP_FAIL_LOOK_BEHIND_NOT SIZE_OPCODE
615 #define SIZE_OP_CALL (SIZE_OPCODE + SIZE_ABSADDR)
616 #define SIZE_OP_RETURN SIZE_OPCODE
617
618 #ifdef USE_COMBINATION_EXPLOSION_CHECK
619 #define SIZE_OP_STATE_CHECK (SIZE_OPCODE + SIZE_STATE_CHECK_NUM)
620 #define SIZE_OP_STATE_CHECK_PUSH (SIZE_OPCODE + SIZE_STATE_CHECK_NUM + SIZE_RELADDR)
621 #define SIZE_OP_STATE_CHECK_PUSH_OR_JUMP (SIZE_OPCODE + SIZE_STATE_CHECK_NUM + SIZE_RELADDR)
622 #define SIZE_OP_STATE_CHECK_ANYCHAR_STAR (SIZE_OPCODE + SIZE_STATE_CHECK_NUM)
623 #endif
624
625 #define MC_ESC(syn) (syn)->meta_char_table.esc
626 #define MC_ANYCHAR(syn) (syn)->meta_char_table.anychar
627 #define MC_ANYTIME(syn) (syn)->meta_char_table.anytime
628 #define MC_ZERO_OR_ONE_TIME(syn) (syn)->meta_char_table.zero_or_one_time
629 #define MC_ONE_OR_MORE_TIME(syn) (syn)->meta_char_table.one_or_more_time
630 #define MC_ANYCHAR_ANYTIME(syn) (syn)->meta_char_table.anychar_anytime
631
632 #define IS_MC_ESC_CODE(code, syn) \
633 ((code) == MC_ESC(syn) && \
634 !IS_SYNTAX_OP2((syn), ONIG_SYN_OP2_INEFFECTIVE_ESCAPE))
635
636
637 #define SYN_POSIX_COMMON_OP \
638 ( ONIG_SYN_OP_DOT_ANYCHAR | ONIG_SYN_OP_POSIX_BRACKET | \
639 ONIG_SYN_OP_DECIMAL_BACKREF | \
640 ONIG_SYN_OP_BRACKET_CC | ONIG_SYN_OP_ASTERISK_ZERO_INF | \
641 ONIG_SYN_OP_LINE_ANCHOR | \
642 ONIG_SYN_OP_ESC_CONTROL_CHARS )
643
644 #define SYN_GNU_REGEX_OP \
645 ( ONIG_SYN_OP_DOT_ANYCHAR | ONIG_SYN_OP_BRACKET_CC | \
646 ONIG_SYN_OP_POSIX_BRACKET | ONIG_SYN_OP_DECIMAL_BACKREF | \
647 ONIG_SYN_OP_BRACE_INTERVAL | ONIG_SYN_OP_LPAREN_SUBEXP | \
648 ONIG_SYN_OP_VBAR_ALT | \
649 ONIG_SYN_OP_ASTERISK_ZERO_INF | ONIG_SYN_OP_PLUS_ONE_INF | \
650 ONIG_SYN_OP_QMARK_ZERO_ONE | \
651 ONIG_SYN_OP_ESC_AZ_BUF_ANCHOR | ONIG_SYN_OP_ESC_CAPITAL_G_BEGIN_ANCHOR | \
652 ONIG_SYN_OP_ESC_W_WORD | \
653 ONIG_SYN_OP_ESC_B_WORD_BOUND | ONIG_SYN_OP_ESC_LTGT_WORD_BEGIN_END | \
654 ONIG_SYN_OP_ESC_S_WHITE_SPACE | ONIG_SYN_OP_ESC_D_DIGIT | \
655 ONIG_SYN_OP_LINE_ANCHOR )
656
657 #define SYN_GNU_REGEX_BV \
658 ( ONIG_SYN_CONTEXT_INDEP_ANCHORS | ONIG_SYN_CONTEXT_INDEP_REPEAT_OPS | \
659 ONIG_SYN_CONTEXT_INVALID_REPEAT_OPS | ONIG_SYN_ALLOW_INVALID_INTERVAL | \
660 ONIG_SYN_BACKSLASH_ESCAPE_IN_CC | ONIG_SYN_ALLOW_DOUBLE_RANGE_OP_IN_CC )
661
662
663 #define NCCLASS_FLAGS(cc) ((cc)->flags)
664 #define NCCLASS_FLAG_SET(cc,flag) (NCCLASS_FLAGS(cc) |= (flag))
665 #define NCCLASS_FLAG_CLEAR(cc,flag) (NCCLASS_FLAGS(cc) &= ~(flag))
666 #define IS_NCCLASS_FLAG_ON(cc,flag) ((NCCLASS_FLAGS(cc) & (flag)) != 0)
667
668
669 #define FLAG_NCCLASS_NOT (1<<0)
670 #define FLAG_NCCLASS_SHARE (1<<1)
671
672 #define NCCLASS_SET_NOT(nd) NCCLASS_FLAG_SET(nd, FLAG_NCCLASS_NOT)
673 #define NCCLASS_SET_SHARE(nd) NCCLASS_FLAG_SET(nd, FLAG_NCCLASS_SHARE)
674 #define NCCLASS_CLEAR_NOT(nd) NCCLASS_FLAG_CLEAR(nd, FLAG_NCCLASS_NOT)
675 #define IS_NCCLASS_NOT(nd) IS_NCCLASS_FLAG_ON(nd, FLAG_NCCLASS_NOT)
676 #define IS_NCCLASS_SHARE(nd) IS_NCCLASS_FLAG_ON(nd, FLAG_NCCLASS_SHARE)
677
678 typedef struct {
679 int type;
680
681
682 } NodeBase;
683
684 typedef struct {
685 NodeBase base;
686 unsigned int flags;
687 BitSet bs;
688 BBuf* mbuf;
689 } CClassNode;
690
691 typedef long OnigStackIndex;
692
693 typedef struct _OnigStackType {
694 unsigned int type;
695 union {
696 struct {
697 UChar *pcode;
698 UChar *pstr;
699 UChar *pstr_prev;
700 #ifdef USE_COMBINATION_EXPLOSION_CHECK
701 unsigned int state_check;
702 #endif
703 } state;
704 struct {
705 int count;
706 UChar *pcode;
707 int num;
708 } repeat;
709 struct {
710 OnigStackIndex si;
711 } repeat_inc;
712 struct {
713 int num;
714 UChar *pstr;
715
716 OnigStackIndex start;
717 OnigStackIndex end;
718 } mem;
719 struct {
720 int num;
721 UChar *pstr;
722 } null_check;
723 #ifdef USE_SUBEXP_CALL
724 struct {
725 UChar *ret_addr;
726 int num;
727 UChar *pstr;
728 } call_frame;
729 #endif
730 } u;
731 } OnigStackType;
732
733 typedef struct {
734 void* stack_p;
735 int stack_n;
736 OnigOptionType options;
737 OnigRegion* region;
738 const UChar* start;
739 #ifdef USE_FIND_LONGEST_SEARCH_ALL_OF_RANGE
740 int best_len;
741 UChar* best_s;
742 #endif
743 #ifdef USE_COMBINATION_EXPLOSION_CHECK
744 void* state_check_buff;
745 int state_check_buff_size;
746 #endif
747 } OnigMatchArg;
748
749
750 #define IS_CODE_SB_WORD(enc,code) \
751 (ONIGENC_IS_CODE_ASCII(code) && ONIGENC_IS_CODE_WORD(enc,code))
752
753 #ifdef ONIG_DEBUG
754
755 typedef struct {
756 short int opcode;
757 char* name;
758 short int arg_type;
759 } OnigOpInfoType;
760
761 extern OnigOpInfoType OnigOpInfo[];
762
763 extern void onig_print_compiled_byte_code P_((FILE* f, UChar* bp, UChar** nextp, OnigEncoding enc));
764
765 #ifdef ONIG_DEBUG_STATISTICS
766 extern void onig_statistics_init P_((void));
767 extern void onig_print_statistics P_((FILE* f));
768 #endif
769 #endif
770
771 extern UChar* onig_error_code_to_format P_((int code));
772 extern void onig_snprintf_with_pattern PV_((UChar buf[], int bufsize, OnigEncoding enc, UChar* pat, UChar* pat_end, const UChar *fmt, ...));
773 extern int onig_bbuf_init P_((BBuf* buf, int size));
774 extern int onig_compile P_((regex_t* reg, const UChar* pattern, const UChar* pattern_end, OnigErrorInfo* einfo));
775 extern void onig_chain_reduce P_((regex_t* reg));
776 extern void onig_chain_link_add P_((regex_t* to, regex_t* add));
777 extern void onig_transfer P_((regex_t* to, regex_t* from));
778 extern int onig_is_code_in_cc P_((OnigEncoding enc, OnigCodePoint code, CClassNode* cc));
779 extern int onig_is_code_in_cc_len P_((int enclen, OnigCodePoint code, CClassNode* cc));
780
781
782 typedef void hash_table_type;
783 typedef unsigned long hash_data_type;
784
785 extern hash_table_type* onig_st_init_strend_table_with_size P_((int size));
786 extern int onig_st_lookup_strend P_((hash_table_type* table, const UChar* str_key, const UChar* end_key, hash_data_type *value));
787 extern int onig_st_insert_strend P_((hash_table_type* table, const UChar* str_key, const UChar* end_key, hash_data_type value));
788
789
790 #define PROPERTY_LIST_ADD_PROP(Name, CR) \
791 r = onigenc_property_list_add_property((UChar* )Name, CR,\
792 &PropertyNameTable, &PropertyList, &PropertyListNum,\
793 &PropertyListSize);\
794 if (r != 0) goto end
795
796 #define PROPERTY_LIST_INIT_CHECK \
797 if (PropertyInited == 0) {\
798 int r = onigenc_property_list_init(init_property_list);\
799 if (r != 0) return r;\
800 }
801
802 extern int onigenc_property_list_add_property P_((UChar* name, const OnigCodePoint* prop, hash_table_type **table, const OnigCodePoint*** plist, int *pnum, int *psize));
803
804 typedef int (*ONIGENC_INIT_PROPERTY_LIST_FUNC_TYPE)(void);
805
806 extern int onigenc_property_list_init P_((ONIGENC_INIT_PROPERTY_LIST_FUNC_TYPE));
807
808 #endif