regguts.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /*
  2. * Internal interface definitions, etc., for the reg package
  3. *
  4. * Copyright (c) 1998, 1999 Henry Spencer. All rights reserved.
  5. *
  6. * Development of this software was funded, in part, by Cray Research Inc.,
  7. * UUNET Communications Services Inc., Sun Microsystems Inc., and Scriptics
  8. * Corporation, none of whom are responsible for the results. The author
  9. * thanks all of them.
  10. *
  11. * Redistribution and use in source and binary forms -- with or without
  12. * modification -- are permitted for any purpose, provided that
  13. * redistributions in source form retain this entire copyright notice and
  14. * indicate the origin and nature of any modifications.
  15. *
  16. * I'd appreciate being given credit for this package in the documentation
  17. * of software which uses it, but that is not a requirement.
  18. *
  19. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
  20. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  21. * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  22. * HENRY SPENCER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  25. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  27. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  28. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. * src/include/regex/regguts.h
  31. */
  32. /*
  33. * Environmental customization. It should not (I hope) be necessary to
  34. * alter the file you are now reading -- regcustom.h should handle it all,
  35. * given care here and elsewhere.
  36. */
  37. #include "regcustom.h"
  38. /*
  39. * Things that regcustom.h might override.
  40. */
  41. /* assertions */
  42. #ifndef assert
  43. #ifndef REG_DEBUG
  44. #define NDEBUG /* no assertions */
  45. #endif
  46. #include <assert.h>
  47. #endif
  48. /* voids */
  49. #ifndef DISCARD
  50. #define DISCARD void /* for throwing values away */
  51. #endif
  52. #ifndef VS
  53. #define VS(x) ((void *)(x)) /* cast something to generic ptr */
  54. #endif
  55. /* function-pointer declarator */
  56. #ifndef FUNCPTR
  57. #define FUNCPTR(name, args) (*(name)) args
  58. #endif
  59. /* memory allocation */
  60. #ifndef MALLOC
  61. #define MALLOC(n) malloc(n)
  62. #endif
  63. #ifndef REALLOC
  64. #define REALLOC(p, n) realloc(VS(p), n)
  65. #endif
  66. #ifndef FREE
  67. #define FREE(p) free(VS(p))
  68. #endif
  69. /* want size of a char in bits, and max value in bounded quantifiers */
  70. #ifndef _POSIX2_RE_DUP_MAX
  71. #define _POSIX2_RE_DUP_MAX 255 /* normally from <limits.h> */
  72. #endif
  73. /*
  74. * misc
  75. */
  76. #define NOTREACHED 0
  77. #define DUPMAX _POSIX2_RE_DUP_MAX
  78. #define DUPINF (DUPMAX+1)
  79. #define REMAGIC 0xfed7 /* magic number for main struct */
  80. /* Type codes for lookaround constraints */
  81. #define LATYPE_AHEAD_POS 03 /* positive lookahead */
  82. #define LATYPE_AHEAD_NEG 02 /* negative lookahead */
  83. #define LATYPE_BEHIND_POS 01 /* positive lookbehind */
  84. #define LATYPE_BEHIND_NEG 00 /* negative lookbehind */
  85. #define LATYPE_IS_POS(la) ((la) & 01)
  86. #define LATYPE_IS_AHEAD(la) ((la) & 02)
  87. /*
  88. * debugging facilities
  89. */
  90. #ifdef REG_DEBUG
  91. /* FDEBUG does finite-state tracing */
  92. #define FDEBUG(arglist) { if (v->eflags&REG_FTRACE) printf arglist; }
  93. /* MDEBUG does higher-level tracing */
  94. #define MDEBUG(arglist) { if (v->eflags&REG_MTRACE) printf arglist; }
  95. #else
  96. #define FDEBUG(arglist) {}
  97. #define MDEBUG(arglist) {}
  98. #endif
  99. /*
  100. * bitmap manipulation
  101. */
  102. #define UBITS (CHAR_BIT * sizeof(unsigned))
  103. #define BSET(uv, sn) ((uv)[(sn)/UBITS] |= (unsigned)1 << ((sn)%UBITS))
  104. #define ISBSET(uv, sn) ((uv)[(sn)/UBITS] & ((unsigned)1 << ((sn)%UBITS)))
  105. /*
  106. * We dissect a chr into byts for colormap table indexing. Here we define
  107. * a byt, which will be the same as a byte on most machines... The exact
  108. * size of a byt is not critical, but about 8 bits is good, and extraction
  109. * of 8-bit chunks is sometimes especially fast.
  110. */
  111. #ifndef BYTBITS
  112. #define BYTBITS 8 /* bits in a byt */
  113. #endif
  114. #define BYTTAB (1<<BYTBITS) /* size of table with one entry per byt value */
  115. #define BYTMASK (BYTTAB-1) /* bit mask for byt */
  116. #define NBYTS ((CHRBITS+BYTBITS-1)/BYTBITS)
  117. /* the definition of GETCOLOR(), below, assumes NBYTS <= 4 */
  118. /*
  119. * As soon as possible, we map chrs into equivalence classes -- "colors" --
  120. * which are of much more manageable number.
  121. */
  122. typedef short color; /* colors of characters */
  123. typedef int pcolor; /* what color promotes to */
  124. #define MAX_COLOR 32767 /* max color (must fit in 'color' datatype) */
  125. #define COLORLESS (-1) /* impossible color */
  126. #define WHITE 0 /* default color, parent of all others */
  127. /*
  128. * A colormap is a tree -- more precisely, a DAG -- indexed at each level
  129. * by a byt of the chr, to map the chr to a color efficiently. Because
  130. * lower sections of the tree can be shared, it can exploit the usual
  131. * sparseness of such a mapping table. The tree is always NBYTS levels
  132. * deep (in the past it was shallower during construction but was "filled"
  133. * to full depth at the end of that); areas that are unaltered as yet point
  134. * to "fill blocks" which are entirely WHITE in color.
  135. *
  136. * Leaf-level tree blocks are of type "struct colors", while upper-level
  137. * blocks are of type "struct ptrs". Pointers into the tree are generally
  138. * declared as "union tree *" to be agnostic about what level they point to.
  139. */
  140. /* the tree itself */
  141. struct colors
  142. {
  143. color ccolor[BYTTAB];
  144. };
  145. struct ptrs
  146. {
  147. union tree *pptr[BYTTAB];
  148. };
  149. union tree
  150. {
  151. struct colors colors;
  152. struct ptrs ptrs;
  153. };
  154. /* use these pseudo-field names when dereferencing a "union tree" pointer */
  155. #define tcolor colors.ccolor
  156. #define tptr ptrs.pptr
  157. /*
  158. * Per-color data structure for the compile-time color machinery
  159. *
  160. * If "sub" is not NOSUB then it is the number of the color's current
  161. * subcolor, i.e. we are in process of dividing this color (character
  162. * equivalence class) into two colors. See src/backend/regex/README for
  163. * discussion of subcolors.
  164. *
  165. * Currently-unused colors have the FREECOL bit set and are linked into a
  166. * freelist using their "sub" fields, but only if their color numbers are
  167. * less than colormap.max. Any array entries beyond "max" are just garbage.
  168. */
  169. struct colordesc
  170. {
  171. uchr nchrs; /* number of chars of this color */
  172. color sub; /* open subcolor, if any; or free-chain ptr */
  173. #define NOSUB COLORLESS /* value of "sub" when no open subcolor */
  174. struct arc *arcs; /* chain of all arcs of this color */
  175. chr firstchr; /* char first assigned to this color */
  176. int flags; /* bit values defined next */
  177. #define FREECOL 01 /* currently free */
  178. #define PSEUDO 02 /* pseudocolor, no real chars */
  179. #define UNUSEDCOLOR(cd) ((cd)->flags & FREECOL)
  180. union tree *block; /* block of solid color, if any */
  181. };
  182. /*
  183. * The color map itself
  184. *
  185. * Much of the data in the colormap struct is only used at compile time.
  186. * However, the bulk of the space usage is in the "tree" structure, so it's
  187. * not clear that there's much point in converting the rest to a more compact
  188. * form when compilation is finished.
  189. */
  190. struct colormap
  191. {
  192. int magic;
  193. #define CMMAGIC 0x876
  194. struct vars *v; /* for compile error reporting */
  195. size_t ncds; /* allocated length of colordescs array */
  196. size_t max; /* highest color number currently in use */
  197. color free; /* beginning of free chain (if non-0) */
  198. struct colordesc *cd; /* pointer to array of colordescs */
  199. #define CDEND(cm) (&(cm)->cd[(cm)->max + 1])
  200. /* If we need up to NINLINECDS, we store them here to save a malloc */
  201. #define NINLINECDS ((size_t)10)
  202. struct colordesc cdspace[NINLINECDS];
  203. union tree tree[NBYTS]; /* tree top, plus lower-level fill blocks */
  204. };
  205. /* optimization magic to do fast chr->color mapping */
  206. #define B0(c) ((c) & BYTMASK)
  207. #define B1(c) (((c)>>BYTBITS) & BYTMASK)
  208. #define B2(c) (((c)>>(2*BYTBITS)) & BYTMASK)
  209. #define B3(c) (((c)>>(3*BYTBITS)) & BYTMASK)
  210. #if NBYTS == 1
  211. #define GETCOLOR(cm, c) ((cm)->tree->tcolor[B0(c)])
  212. #endif
  213. /* beware, for NBYTS>1, GETCOLOR() is unsafe -- 2nd arg used repeatedly */
  214. #if NBYTS == 2
  215. #define GETCOLOR(cm, c) ((cm)->tree->tptr[B1(c)]->tcolor[B0(c)])
  216. #endif
  217. #if NBYTS == 4
  218. #define GETCOLOR(cm, c) ((cm)->tree->tptr[B3(c)]->tptr[B2(c)]->tptr[B1(c)]->tcolor[B0(c)])
  219. #endif
  220. /*
  221. * Interface definitions for locale-interface functions in regc_locale.c.
  222. */
  223. /*
  224. * Representation of a set of characters. chrs[] represents individual
  225. * code points, ranges[] represents ranges in the form min..max inclusive.
  226. *
  227. * Note that in cvecs gotten from newcvec() and intended to be freed by
  228. * freecvec(), both arrays of chrs are after the end of the struct, not
  229. * separately malloc'd; so chrspace and rangespace are effectively immutable.
  230. */
  231. struct cvec
  232. {
  233. int nchrs; /* number of chrs */
  234. int chrspace; /* number of chrs allocated in chrs[] */
  235. chr *chrs; /* pointer to vector of chrs */
  236. int nranges; /* number of ranges (chr pairs) */
  237. int rangespace; /* number of ranges allocated in ranges[] */
  238. chr *ranges; /* pointer to vector of chr pairs */
  239. };
  240. /*
  241. * definitions for NFA internal representation
  242. *
  243. * Having a "from" pointer within each arc may seem redundant, but it
  244. * saves a lot of hassle.
  245. */
  246. struct state;
  247. struct arc
  248. {
  249. int type; /* 0 if free, else an NFA arc type code */
  250. color co;
  251. struct state *from; /* where it's from (and contained within) */
  252. struct state *to; /* where it's to */
  253. struct arc *outchain; /* link in *from's outs chain or free chain */
  254. struct arc *outchainRev; /* back-link in *from's outs chain */
  255. #define freechain outchain /* we do not maintain "freechainRev" */
  256. struct arc *inchain; /* link in *to's ins chain */
  257. struct arc *inchainRev; /* back-link in *to's ins chain */
  258. struct arc *colorchain; /* link in color's arc chain */
  259. struct arc *colorchainRev; /* back-link in color's arc chain */
  260. };
  261. struct arcbatch
  262. { /* for bulk allocation of arcs */
  263. struct arcbatch *next;
  264. #define ABSIZE 10
  265. struct arc a[ABSIZE];
  266. };
  267. struct state
  268. {
  269. int no;
  270. #define FREESTATE (-1)
  271. char flag; /* marks special states */
  272. int nins; /* number of inarcs */
  273. struct arc *ins; /* chain of inarcs */
  274. int nouts; /* number of outarcs */
  275. struct arc *outs; /* chain of outarcs */
  276. struct arc *free; /* chain of free arcs */
  277. struct state *tmp; /* temporary for traversal algorithms */
  278. struct state *next; /* chain for traversing all */
  279. struct state *prev; /* back chain */
  280. struct arcbatch oas; /* first arcbatch, avoid malloc in easy case */
  281. int noas; /* number of arcs used in first arcbatch */
  282. };
  283. struct nfa
  284. {
  285. struct state *pre; /* pre-initial state */
  286. struct state *init; /* initial state */
  287. struct state *final; /* final state */
  288. struct state *post; /* post-final state */
  289. int nstates; /* for numbering states */
  290. struct state *states; /* state-chain header */
  291. struct state *slast; /* tail of the chain */
  292. struct state *free; /* free list */
  293. struct colormap *cm; /* the color map */
  294. color bos[2]; /* colors, if any, assigned to BOS and BOL */
  295. color eos[2]; /* colors, if any, assigned to EOS and EOL */
  296. struct vars *v; /* simplifies compile error reporting */
  297. struct nfa *parent; /* parent NFA, if any */
  298. };
  299. /*
  300. * definitions for compacted NFA
  301. *
  302. * The main space savings in a compacted NFA is from making the arcs as small
  303. * as possible. We store only the transition color and next-state number for
  304. * each arc. The list of out arcs for each state is an array beginning at
  305. * cnfa.states[statenumber], and terminated by a dummy carc struct with
  306. * co == COLORLESS.
  307. *
  308. * The non-dummy carc structs are of two types: plain arcs and LACON arcs.
  309. * Plain arcs just store the transition color number as "co". LACON arcs
  310. * store the lookaround constraint number plus cnfa.ncolors as "co". LACON
  311. * arcs can be distinguished from plain by testing for co >= cnfa.ncolors.
  312. */
  313. struct carc
  314. {
  315. color co; /* COLORLESS is list terminator */
  316. int to; /* next-state number */
  317. };
  318. struct cnfa
  319. {
  320. int nstates; /* number of states */
  321. int ncolors; /* number of colors (max color in use + 1) */
  322. int flags;
  323. #define HASLACONS 01 /* uses lookaround constraints */
  324. int pre; /* setup state number */
  325. int post; /* teardown state number */
  326. color bos[2]; /* colors, if any, assigned to BOS and BOL */
  327. color eos[2]; /* colors, if any, assigned to EOS and EOL */
  328. char *stflags; /* vector of per-state flags bytes */
  329. #define CNFA_NOPROGRESS 01 /* flag bit for a no-progress state */
  330. struct carc **states; /* vector of pointers to outarc lists */
  331. /* states[n] are pointers into a single malloc'd array of arcs */
  332. struct carc *arcs; /* the area for the lists */
  333. };
  334. #define ZAPCNFA(cnfa) ((cnfa).nstates = 0)
  335. #define NULLCNFA(cnfa) ((cnfa).nstates == 0)
  336. /*
  337. * This symbol limits the transient heap space used by the regex compiler,
  338. * and thereby also the maximum complexity of NFAs that we'll deal with.
  339. * Currently we only count NFA states and arcs against this; the other
  340. * transient data is generally not large enough to notice compared to those.
  341. * Note that we do not charge anything for the final output data structures
  342. * (the compacted NFA and the colormap).
  343. */
  344. #ifndef REG_MAX_COMPILE_SPACE
  345. #define REG_MAX_COMPILE_SPACE \
  346. (100000 * sizeof(struct state) + 100000 * sizeof(struct arcbatch))
  347. #endif
  348. /*
  349. * subexpression tree
  350. *
  351. * "op" is one of:
  352. * '=' plain regex without interesting substructure (implemented as DFA)
  353. * 'b' back-reference (has no substructure either)
  354. * '(' capture node: captures the match of its single child
  355. * '.' concatenation: matches a match for left, then a match for right
  356. * '|' alternation: matches a match for left or a match for right
  357. * '*' iteration: matches some number of matches of its single child
  358. *
  359. * Note: the right child of an alternation must be another alternation or
  360. * NULL; hence, an N-way branch requires N alternation nodes, not N-1 as you
  361. * might expect. This could stand to be changed. Actually I'd rather see
  362. * a single alternation node with N children, but that will take revising
  363. * the representation of struct subre.
  364. *
  365. * Note: when a backref is directly quantified, we stick the min/max counts
  366. * into the backref rather than plastering an iteration node on top. This is
  367. * for efficiency: there is no need to search for possible division points.
  368. */
  369. struct subre
  370. {
  371. char op; /* see type codes above */
  372. char flags;
  373. #define LONGER 01 /* prefers longer match */
  374. #define SHORTER 02 /* prefers shorter match */
  375. #define MIXED 04 /* mixed preference below */
  376. #define CAP 010 /* capturing parens below */
  377. #define BACKR 020 /* back reference below */
  378. #define INUSE 0100 /* in use in final tree */
  379. #define NOPROP 03 /* bits which may not propagate up */
  380. #define LMIX(f) ((f)<<2) /* LONGER -> MIXED */
  381. #define SMIX(f) ((f)<<1) /* SHORTER -> MIXED */
  382. #define UP(f) (((f)&~NOPROP) | (LMIX(f) & SMIX(f) & MIXED))
  383. #define MESSY(f) ((f)&(MIXED|CAP|BACKR))
  384. #define PREF(f) ((f)&NOPROP)
  385. #define PREF2(f1, f2) ((PREF(f1) != 0) ? PREF(f1) : PREF(f2))
  386. #define COMBINE(f1, f2) (UP((f1)|(f2)) | PREF2(f1, f2))
  387. short id; /* ID of subre (1..ntree-1) */
  388. int subno; /* subexpression number for 'b' and '(', or
  389. * LATYPE code for lookaround constraint */
  390. short min; /* min repetitions for iteration or backref */
  391. short max; /* max repetitions for iteration or backref */
  392. struct subre *left; /* left child, if any (also freelist chain) */
  393. struct subre *right; /* right child, if any */
  394. struct state *begin; /* outarcs from here... */
  395. struct state *end; /* ...ending in inarcs here */
  396. struct cnfa cnfa; /* compacted NFA, if any */
  397. struct subre *chain; /* for bookkeeping and error cleanup */
  398. };
  399. /*
  400. * table of function pointers for generic manipulation functions
  401. * A regex_t's re_fns points to one of these.
  402. */
  403. struct fns
  404. {
  405. void FUNCPTR(free, (regex_t *));
  406. int FUNCPTR(cancel_requested, (void));
  407. int FUNCPTR(stack_too_deep, (void));
  408. };
  409. #define CANCEL_REQUESTED(re) \
  410. ((*((struct fns *) (re)->re_fns)->cancel_requested) ())
  411. #define STACK_TOO_DEEP(re) \
  412. ((*((struct fns *) (re)->re_fns)->stack_too_deep) ())
  413. /*
  414. * the insides of a regex_t, hidden behind a void *
  415. */
  416. struct guts
  417. {
  418. int magic;
  419. #define GUTSMAGIC 0xfed9
  420. int cflags; /* copy of compile flags */
  421. long info; /* copy of re_info */
  422. size_t nsub; /* copy of re_nsub */
  423. struct subre *tree;
  424. struct cnfa search; /* for fast preliminary search */
  425. int ntree; /* number of subre's, plus one */
  426. struct colormap cmap;
  427. int FUNCPTR(compare, (const chr *, const chr *, size_t));
  428. struct subre *lacons; /* lookaround-constraint vector */
  429. int nlacons; /* size of lacons[]; note that only slots
  430. * numbered 1 .. nlacons-1 are used */
  431. };