regint.h 28 KB

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