regexec.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*-
  2. * This code is derived from OpenBSD's libc/regex, original license follows:
  3. *
  4. * Copyright (c) 1992, 1993, 1994 Henry Spencer.
  5. * Copyright (c) 1992, 1993, 1994
  6. * The Regents of the University of California. All rights reserved.
  7. *
  8. * This code is derived from software contributed to Berkeley by
  9. * Henry Spencer.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * 3. Neither the name of the University nor the names of its contributors
  20. * may be used to endorse or promote products derived from this software
  21. * without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  24. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  27. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  29. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  30. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  32. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  33. * SUCH DAMAGE.
  34. *
  35. * @(#)regexec.c 8.3 (Berkeley) 3/20/94
  36. */
  37. /*
  38. * the outer shell of llvm_regexec()
  39. *
  40. * This file includes engine.inc *twice*, after muchos fiddling with the
  41. * macros that code uses. This lets the same code operate on two different
  42. * representations for state sets.
  43. */
  44. #include <sys/types.h>
  45. #include <stdio.h>
  46. #include <stdlib.h>
  47. #include <string.h>
  48. #include <limits.h>
  49. #include <ctype.h>
  50. #include "regex_impl.h"
  51. #include "regutils.h"
  52. #include "regex2.h"
  53. /* macros for manipulating states, small version */
  54. /* FIXME: 'states' is assumed as 'long' on small version. */
  55. #define states1 long /* for later use in llvm_regexec() decision */
  56. #define states states1
  57. #define CLEAR(v) ((v) = 0)
  58. #define SET0(v, n) ((v) &= ~((unsigned long)1 << (n)))
  59. #define SET1(v, n) ((v) |= (unsigned long)1 << (n))
  60. #define ISSET(v, n) (((v) & ((unsigned long)1 << (n))) != 0)
  61. #define ASSIGN(d, s) ((d) = (s))
  62. #define EQ(a, b) ((a) == (b))
  63. #define STATEVARS long dummy /* dummy version */
  64. #define STATESETUP(m, n) /* nothing */
  65. #define STATETEARDOWN(m) /* nothing */
  66. #define SETUP(v) ((v) = 0)
  67. #define onestate long
  68. #define INIT(o, n) ((o) = (unsigned long)1 << (n))
  69. #define INC(o) ((o) = (unsigned long)(o) << 1)
  70. #define ISSTATEIN(v, o) (((v) & (o)) != 0)
  71. /* some abbreviations; note that some of these know variable names! */
  72. /* do "if I'm here, I can also be there" etc without branches */
  73. #define FWD(dst, src, n) ((dst) |= ((unsigned long)(src)&(here)) << (n))
  74. #define BACK(dst, src, n) ((dst) |= ((unsigned long)(src)&(here)) >> (n))
  75. #define ISSETBACK(v, n) (((v) & ((unsigned long)here >> (n))) != 0)
  76. /* function names */
  77. #define SNAMES /* engine.inc looks after details */
  78. #define _states_param_ /* define to nothing when not a pointer */
  79. #include "regengine.inc"
  80. /* now undo things */
  81. #undef states
  82. #undef CLEAR
  83. #undef SET0
  84. #undef SET1
  85. #undef ISSET
  86. #undef ASSIGN
  87. #undef EQ
  88. #undef STATEVARS
  89. #undef STATESETUP
  90. #undef STATETEARDOWN
  91. #undef SETUP
  92. #undef onestate
  93. #undef INIT
  94. #undef INC
  95. #undef ISSTATEIN
  96. #undef FWD
  97. #undef BACK
  98. #undef ISSETBACK
  99. #undef SNAMES
  100. #undef _states_param_
  101. /* macros for manipulating states, large version */
  102. #define states char *
  103. #define CLEAR(v) memset(v, 0, m->g->nstates)
  104. #define SET0(v, n) ((v)[n] = 0)
  105. #define SET1(v, n) ((v)[n] = 1)
  106. #define ISSET(v, n) ((v)[n])
  107. #define ASSIGN(d, s) memmove(d, s, m->g->nstates)
  108. #define EQ(a, b) (memcmp(a, b, m->g->nstates) == 0)
  109. #define STATEVARS long vn; char *space
  110. // HLSL Change Begin: Use custom allocator
  111. #define STATESETUP(m, nv) { (m)->space = regex_malloc((nv)*(m)->g->nstates); \
  112. if ((m)->space == NULL) return(REG_ESPACE); \
  113. (m)->vn = 0; }
  114. #define STATETEARDOWN(m) { regex_free((m)->space); }
  115. // HLSL Change End
  116. #define SETUP(v) ((v) = &m->space[m->vn++ * m->g->nstates])
  117. #define onestate long
  118. #define INIT(o, n) ((o) = (n))
  119. #define INC(o) ((o)++)
  120. #define ISSTATEIN(v, o) ((v)[o])
  121. /* some abbreviations; note that some of these know variable names! */
  122. /* do "if I'm here, I can also be there" etc without branches */
  123. #define FWD(dst, src, n) ((dst)[here+(n)] |= (src)[here])
  124. #define BACK(dst, src, n) ((dst)[here-(n)] |= (src)[here])
  125. #define ISSETBACK(v, n) ((v)[here - (n)])
  126. /* function names */
  127. #define LNAMES /* flag */
  128. #define _states_param_ __inexpressible_readableTo(stopmarker)
  129. #include "regengine.inc"
  130. /*
  131. - llvm_regexec - interface for matching
  132. *
  133. * We put this here so we can exploit knowledge of the state representation
  134. * when choosing which matcher to call. Also, by this point the matchers
  135. * have been prototyped.
  136. */
  137. int /* 0 success, REG_NOMATCH failure */
  138. llvm_regexec(const llvm_regex_t *preg, const char *string, size_t nmatch,
  139. llvm_regmatch_t pmatch[], int eflags)
  140. {
  141. struct re_guts *g = preg->re_g;
  142. #ifdef REDEBUG
  143. # define GOODFLAGS(f) (f)
  144. #else
  145. # define GOODFLAGS(f) ((f)&(REG_NOTBOL|REG_NOTEOL|REG_STARTEND))
  146. #endif
  147. if (preg->re_magic != MAGIC1 || g->magic != MAGIC2)
  148. return(REG_BADPAT);
  149. assert(!(g->iflags&REGEX_BAD));
  150. if (g->iflags&REGEX_BAD) /* backstop for no-debug case */
  151. return(REG_BADPAT);
  152. eflags = GOODFLAGS(eflags);
  153. if (g->nstates <= (long)(CHAR_BIT*sizeof(states1)) && !(eflags&REG_LARGE))
  154. return(smatcher(g, string, nmatch, pmatch, eflags));
  155. else
  156. return(lmatcher(g, string, nmatch, pmatch, eflags));
  157. }