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