PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/openvas-libraries-4.0.7/nasl/nasl_regex.h

#
C Header | 505 lines | 188 code | 99 blank | 218 comment | 0 complexity | 4ab5ec894361124738bf9241c9f49e83 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0, LGPL-2.0
  1. /* Definitions for data structures and routines for the regular
  2. expression library, version 0.12.
  3. Copyright (C) 1985, 1989, 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, version 2.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
  14. #ifdef HAVE_REGEX_SUPPORT
  15. #include <regex.h>
  16. #else
  17. #ifndef __REGEXP_LIBRARY_H__
  18. #define __REGEXP_LIBRARY_H__
  19. /* POSIX says that <sys/types.h> must be included (by the caller) before
  20. <regex.h>. */
  21. #include <sys/types.h> /* for size_t */
  22. #ifdef VMS
  23. /* VMS doesn't have `size_t' in <sys/types.h>, even though POSIX says it
  24. should be there. */
  25. #include <stddef.h>
  26. #endif
  27. /* The following bits are used to determine the regexp syntax we
  28. recognize. The set/not-set meanings are chosen so that Emacs syntax
  29. remains the value 0. The bits are given in alphabetical order, and
  30. the definitions shifted by one from the previous bit; thus, when we
  31. add or remove a bit, only one other definition need change. */
  32. typedef unsigned reg_syntax_t;
  33. /* If this bit is not set, then \ inside a bracket expression is literal.
  34. If set, then such a \ quotes the following character. */
  35. #define RE_BACKSLASH_ESCAPE_IN_LISTS (1)
  36. /* If this bit is not set, then + and ? are operators, and \+ and \? are
  37. literals.
  38. If set, then \+ and \? are operators and + and ? are literals. */
  39. #define RE_BK_PLUS_QM (RE_BACKSLASH_ESCAPE_IN_LISTS << 1)
  40. /* If this bit is set, then character classes are supported. They are:
  41. [:alpha:], [:upper:], [:lower:], [:digit:], [:alnum:], [:xdigit:],
  42. [:space:], [:print:], [:punct:], [:graph:], and [:cntrl:].
  43. If not set, then character classes are not supported. */
  44. #define RE_CHAR_CLASSES (RE_BK_PLUS_QM << 1)
  45. /* If this bit is set, then ^ and $ are always anchors (outside bracket
  46. expressions, of course).
  47. If this bit is not set, then it depends:
  48. ^ is an anchor if it is at the beginning of a regular
  49. expression or after an open-group or an alternation operator;
  50. $ is an anchor if it is at the end of a regular expression, or
  51. before a close-group or an alternation operator.
  52. This bit could be (re)combined with RE_CONTEXT_INDEP_OPS, because
  53. POSIX draft 11.2 says that * etc. in leading positions is undefined.
  54. We already implemented a previous draft which made those constructs
  55. invalid, though, so we haven't changed the code back. */
  56. #define RE_CONTEXT_INDEP_ANCHORS (RE_CHAR_CLASSES << 1)
  57. /* If this bit is set, then special characters are always special
  58. regardless of where they are in the pattern.
  59. If this bit is not set, then special characters are special only in
  60. some contexts; otherwise they are ordinary. Specifically,
  61. * + ? and intervals are only special when not after the beginning,
  62. open-group, or alternation operator. */
  63. #define RE_CONTEXT_INDEP_OPS (RE_CONTEXT_INDEP_ANCHORS << 1)
  64. /* If this bit is set, then *, +, ?, and { cannot be first in an re or
  65. immediately after an alternation or begin-group operator. */
  66. #define RE_CONTEXT_INVALID_OPS (RE_CONTEXT_INDEP_OPS << 1)
  67. /* If this bit is set, then . matches newline.
  68. If not set, then it doesn't. */
  69. #define RE_DOT_NEWLINE (RE_CONTEXT_INVALID_OPS << 1)
  70. /* If this bit is set, then . doesn't match NUL.
  71. If not set, then it does. */
  72. #define RE_DOT_NOT_NULL (RE_DOT_NEWLINE << 1)
  73. /* If this bit is set, nonmatching lists [^...] do not match newline.
  74. If not set, they do. */
  75. #define RE_HAT_LISTS_NOT_NEWLINE (RE_DOT_NOT_NULL << 1)
  76. /* If this bit is set, either \{...\} or {...} defines an
  77. interval, depending on RE_NO_BK_BRACES.
  78. If not set, \{, \}, {, and } are literals. */
  79. #define RE_INTERVALS (RE_HAT_LISTS_NOT_NEWLINE << 1)
  80. /* If this bit is set, +, ? and | aren't recognized as operators.
  81. If not set, they are. */
  82. #define RE_LIMITED_OPS (RE_INTERVALS << 1)
  83. /* If this bit is set, newline is an alternation operator.
  84. If not set, newline is literal. */
  85. #define RE_NEWLINE_ALT (RE_LIMITED_OPS << 1)
  86. /* If this bit is set, then `{...}' defines an interval, and \{ and \}
  87. are literals.
  88. If not set, then `\{...\}' defines an interval. */
  89. #define RE_NO_BK_BRACES (RE_NEWLINE_ALT << 1)
  90. /* If this bit is set, (...) defines a group, and \( and \) are literals.
  91. If not set, \(...\) defines a group, and ( and ) are literals. */
  92. #define RE_NO_BK_PARENS (RE_NO_BK_BRACES << 1)
  93. /* If this bit is set, then \<digit> matches <digit>.
  94. If not set, then \<digit> is a back-reference. */
  95. #define RE_NO_BK_REFS (RE_NO_BK_PARENS << 1)
  96. /* If this bit is set, then | is an alternation operator, and \| is literal.
  97. If not set, then \| is an alternation operator, and | is literal. */
  98. #define RE_NO_BK_VBAR (RE_NO_BK_REFS << 1)
  99. /* If this bit is set, then an ending range point collating higher
  100. than the starting range point, as in [z-a], is invalid.
  101. If not set, then when ending range point collates higher than the
  102. starting range point, the range is ignored. */
  103. #define RE_NO_EMPTY_RANGES (RE_NO_BK_VBAR << 1)
  104. /* If this bit is set, then an unmatched ) is ordinary.
  105. If not set, then an unmatched ) is invalid. */
  106. #define RE_UNMATCHED_RIGHT_PAREN_ORD (RE_NO_EMPTY_RANGES << 1)
  107. /* This global variable defines the particular regexp syntax to use (for
  108. some interfaces). When a regexp is compiled, the syntax used is
  109. stored in the pattern buffer, so changing this does not affect
  110. already-compiled regexps. */
  111. extern reg_syntax_t re_syntax_options;
  112. /* Define combinations of the above bits for the standard possibilities.
  113. (The [[[ comments delimit what gets put into the Texinfo file, so
  114. don't delete them!) */
  115. /* [[[begin syntaxes]]] */
  116. #define RE_SYNTAX_EMACS 0
  117. #define RE_SYNTAX_AWK \
  118. (RE_BACKSLASH_ESCAPE_IN_LISTS | RE_DOT_NOT_NULL \
  119. | RE_NO_BK_PARENS | RE_NO_BK_REFS \
  120. | RE_NO_BK_VBAR | RE_NO_EMPTY_RANGES \
  121. | RE_UNMATCHED_RIGHT_PAREN_ORD)
  122. #define RE_SYNTAX_POSIX_AWK \
  123. (RE_SYNTAX_POSIX_EXTENDED | RE_BACKSLASH_ESCAPE_IN_LISTS)
  124. #define RE_SYNTAX_GREP \
  125. (RE_BK_PLUS_QM | RE_CHAR_CLASSES \
  126. | RE_HAT_LISTS_NOT_NEWLINE | RE_INTERVALS \
  127. | RE_NEWLINE_ALT)
  128. #define RE_SYNTAX_EGREP \
  129. (RE_CHAR_CLASSES | RE_CONTEXT_INDEP_ANCHORS \
  130. | RE_CONTEXT_INDEP_OPS | RE_HAT_LISTS_NOT_NEWLINE \
  131. | RE_NEWLINE_ALT | RE_NO_BK_PARENS \
  132. | RE_NO_BK_VBAR)
  133. #define RE_SYNTAX_POSIX_EGREP \
  134. (RE_SYNTAX_EGREP | RE_INTERVALS | RE_NO_BK_BRACES)
  135. /* P1003.2/D11.2, section 4.20.7.1, lines 5078ff. */
  136. #define RE_SYNTAX_ED RE_SYNTAX_POSIX_BASIC
  137. #define RE_SYNTAX_SED RE_SYNTAX_POSIX_BASIC
  138. /* Syntax bits common to both basic and extended POSIX regex syntax. */
  139. #define _RE_SYNTAX_POSIX_COMMON \
  140. (RE_CHAR_CLASSES | RE_DOT_NEWLINE | RE_DOT_NOT_NULL \
  141. | RE_INTERVALS | RE_NO_EMPTY_RANGES)
  142. #define RE_SYNTAX_POSIX_BASIC \
  143. (_RE_SYNTAX_POSIX_COMMON | RE_BK_PLUS_QM)
  144. /* Differs from ..._POSIX_BASIC only in that RE_BK_PLUS_QM becomes
  145. RE_LIMITED_OPS, i.e., \? \+ \| are not recognized. Actually, this
  146. isn't minimal, since other operators, such as \`, aren't disabled. */
  147. #define RE_SYNTAX_POSIX_MINIMAL_BASIC \
  148. (_RE_SYNTAX_POSIX_COMMON | RE_LIMITED_OPS)
  149. #define RE_SYNTAX_POSIX_EXTENDED \
  150. (_RE_SYNTAX_POSIX_COMMON | RE_CONTEXT_INDEP_ANCHORS \
  151. | RE_CONTEXT_INDEP_OPS | RE_NO_BK_BRACES \
  152. | RE_NO_BK_PARENS | RE_NO_BK_VBAR \
  153. | RE_UNMATCHED_RIGHT_PAREN_ORD)
  154. /* Differs from ..._POSIX_EXTENDED in that RE_CONTEXT_INVALID_OPS
  155. replaces RE_CONTEXT_INDEP_OPS and RE_NO_BK_REFS is added. */
  156. #define RE_SYNTAX_POSIX_MINIMAL_EXTENDED \
  157. (_RE_SYNTAX_POSIX_COMMON | RE_CONTEXT_INDEP_ANCHORS \
  158. | RE_CONTEXT_INVALID_OPS | RE_NO_BK_BRACES \
  159. | RE_NO_BK_PARENS | RE_NO_BK_REFS \
  160. | RE_NO_BK_VBAR | RE_UNMATCHED_RIGHT_PAREN_ORD)
  161. /* [[[end syntaxes]]] */
  162. /* Maximum number of duplicates an interval can allow. Some systems
  163. (erroneously) define this in other header files, but we want our
  164. value, so remove any previous define. */
  165. #ifdef RE_DUP_MAX
  166. #undef RE_DUP_MAX
  167. #endif
  168. #define RE_DUP_MAX ((1 << 15) - 1)
  169. /* POSIX `cflags' bits (i.e., information for `regcomp'). */
  170. /* If this bit is set, then use extended regular expression syntax.
  171. If not set, then use basic regular expression syntax. */
  172. #define REG_EXTENDED 1
  173. /* If this bit is set, then ignore case when matching.
  174. If not set, then case is significant. */
  175. #define REG_ICASE (REG_EXTENDED << 1)
  176. /* If this bit is set, then anchors do not match at newline
  177. characters in the string.
  178. If not set, then anchors do match at newlines. */
  179. #define REG_NEWLINE (REG_ICASE << 1)
  180. /* If this bit is set, then report only success or fail in regexec.
  181. If not set, then returns differ between not matching and errors. */
  182. #define REG_NOSUB (REG_NEWLINE << 1)
  183. /* POSIX `eflags' bits (i.e., information for regexec). */
  184. /* If this bit is set, then the beginning-of-line operator doesn't match
  185. the beginning of the string (presumably because it's not the
  186. beginning of a line).
  187. If not set, then the beginning-of-line operator does match the
  188. beginning of the string. */
  189. #define REG_NOTBOL 1
  190. /* Like REG_NOTBOL, except for the end-of-line. */
  191. #define REG_NOTEOL (1 << 1)
  192. /* If any error codes are removed, changed, or added, update the
  193. `re_error_msg' table in regex.c. */
  194. typedef enum
  195. {
  196. REG_NOERROR = 0, /* Success. */
  197. REG_NOMATCH, /* Didn't find a match (for regexec). */
  198. /* POSIX regcomp return error codes. (In the order listed in the
  199. standard.) */
  200. REG_BADPAT, /* Invalid pattern. */
  201. REG_ECOLLATE, /* Not implemented. */
  202. REG_ECTYPE, /* Invalid character class name. */
  203. REG_EESCAPE, /* Trailing backslash. */
  204. REG_ESUBREG, /* Invalid back reference. */
  205. REG_EBRACK, /* Unmatched left bracket. */
  206. REG_EPAREN, /* Parenthesis imbalance. */
  207. REG_EBRACE, /* Unmatched \{. */
  208. REG_BADBR, /* Invalid contents of \{\}. */
  209. REG_ERANGE, /* Invalid range end. */
  210. REG_ESPACE, /* Ran out of memory. */
  211. REG_BADRPT, /* No preceding re for repetition op. */
  212. /* Error codes we've added. */
  213. REG_EEND, /* Premature end. */
  214. REG_ESIZE, /* Compiled pattern bigger than 2^16 bytes. */
  215. REG_ERPAREN /* Unmatched ) or \); not returned from regcomp. */
  216. } reg_errcode_t;
  217. /* This data structure represents a compiled pattern. Before calling
  218. the pattern compiler, the fields `buffer', `allocated', `fastmap',
  219. `translate', and `no_sub' can be set. After the pattern has been
  220. compiled, the `re_nsub' field is available. All other fields are
  221. private to the regex routines. */
  222. struct re_pattern_buffer
  223. {
  224. /* [[[begin pattern_buffer]]] */
  225. /* Space that holds the compiled pattern. It is declared as
  226. `unsigned char *' because its elements are
  227. sometimes used as array indexes. */
  228. unsigned char *buffer;
  229. /* Number of bytes to which `buffer' points. */
  230. unsigned long allocated;
  231. /* Number of bytes actually used in `buffer'. */
  232. unsigned long used;
  233. /* Syntax setting with which the pattern was compiled. */
  234. reg_syntax_t syntax;
  235. /* Pointer to a fastmap, if any, otherwise zero. re_search uses
  236. the fastmap, if there is one, to skip over impossible
  237. starting points for matches. */
  238. char *fastmap;
  239. /* Either a translate table to apply to all characters before
  240. comparing them, or zero for no translation. The translation
  241. is applied to a pattern when it is compiled and to a string
  242. when it is matched. */
  243. char *translate;
  244. /* Number of subexpressions found by the compiler. */
  245. size_t re_nsub;
  246. /* Zero if this pattern cannot match the empty string, one else.
  247. Well, in truth it's used only in `re_search_2', to see
  248. whether or not we should use the fastmap, so we don't set
  249. this absolutely perfectly; see `re_compile_fastmap' (the
  250. `duplicate' case). */
  251. unsigned can_be_null : 1;
  252. /* If REGS_UNALLOCATED, allocate space in the `regs' structure
  253. for `max (RE_NREGS, re_nsub + 1)' groups.
  254. If REGS_REALLOCATE, reallocate space if necessary.
  255. If REGS_FIXED, use what's there. */
  256. #define REGS_UNALLOCATED 0
  257. #define REGS_REALLOCATE 1
  258. #define REGS_FIXED 2
  259. unsigned regs_allocated : 2;
  260. /* Set to zero when `regex_compile' compiles a pattern; set to one
  261. by `re_compile_fastmap' if it updates the fastmap. */
  262. unsigned fastmap_accurate : 1;
  263. /* If set, `re_match_2' does not return information about
  264. subexpressions. */
  265. unsigned no_sub : 1;
  266. /* If set, a beginning-of-line anchor doesn't match at the
  267. beginning of the string. */
  268. unsigned not_bol : 1;
  269. /* Similarly for an end-of-line anchor. */
  270. unsigned not_eol : 1;
  271. /* If true, an anchor at a newline matches. */
  272. unsigned newline_anchor : 1;
  273. char * orig;
  274. /* [[[end pattern_buffer]]] */
  275. };
  276. typedef struct re_pattern_buffer regex_t;
  277. /* search.c (search_buffer) in Emacs needs this one opcode value. It is
  278. defined both in `regex.c' and here. */
  279. #define RE_EXACTN_VALUE 1
  280. /* Type for byte offsets within the string. POSIX mandates this. */
  281. typedef int regoff_t;
  282. /* This is the structure we store register match data in. See
  283. regex.texinfo for a full description of what registers match. */
  284. struct re_registers
  285. {
  286. unsigned num_regs;
  287. regoff_t *start;
  288. regoff_t *end;
  289. };
  290. /* If `regs_allocated' is REGS_UNALLOCATED in the pattern buffer,
  291. `re_match_2' returns information about at least this many registers
  292. the first time a `regs' structure is passed. */
  293. #ifndef RE_NREGS
  294. #define RE_NREGS 30
  295. #endif
  296. /* POSIX specification for registers. Aside from the different names than
  297. `re_registers', POSIX uses an array of structures, instead of a
  298. structure of arrays. */
  299. typedef struct
  300. {
  301. regoff_t rm_so; /* Byte offset from string's start to substring's start. */
  302. regoff_t rm_eo; /* Byte offset from string's start to substring's end. */
  303. } regmatch_t;
  304. /* Declarations for routines. */
  305. /* To avoid duplicating every routine declaration -- once with a
  306. prototype (if we are ANSI), and once without (if we aren't) -- we
  307. use the following macro to declare argument types. This
  308. unfortunately clutters up the declarations a bit, but I think it's
  309. worth it. */
  310. #if __STDC__
  311. #define _RE_ARGS(args) args
  312. #else /* not __STDC__ */
  313. #define _RE_ARGS(args) ()
  314. #endif /* not __STDC__ */
  315. /* Sets the current default syntax to SYNTAX, and return the old syntax.
  316. You can also simply assign to the `re_syntax_options' variable. */
  317. extern reg_syntax_t re_set_syntax _RE_ARGS ((reg_syntax_t syntax));
  318. /* Compile the regular expression PATTERN, with length LENGTH
  319. and syntax given by the global `re_syntax_options', into the buffer
  320. BUFFER. Return NULL if successful, and an error string if not. */
  321. extern const char *re_compile_pattern
  322. _RE_ARGS ((const char *pattern, int length,
  323. struct re_pattern_buffer *buffer));
  324. /* Compile a fastmap for the compiled pattern in BUFFER; used to
  325. accelerate searches. Return 0 if successful and -2 if was an
  326. internal error. */
  327. extern int re_compile_fastmap _RE_ARGS ((struct re_pattern_buffer *buffer));
  328. /* Search in the string STRING (with length LENGTH) for the pattern
  329. compiled into BUFFER. Start searching at position START, for RANGE
  330. characters. Return the starting position of the match, -1 for no
  331. match, or -2 for an internal error. Also return register
  332. information in REGS (if REGS and BUFFER->no_sub are nonzero). */
  333. extern int re_search
  334. _RE_ARGS ((struct re_pattern_buffer *buffer, const char *string,
  335. int length, int start, int range, struct re_registers *regs));
  336. /* Like `re_search', but search in the concatenation of STRING1 and
  337. STRING2. Also, stop searching at index START + STOP. */
  338. extern int re_search_2
  339. _RE_ARGS ((struct re_pattern_buffer *buffer, const char *string1,
  340. int length1, const char *string2, int length2,
  341. int start, int range, struct re_registers *regs, int stop));
  342. /* Like `re_search', but return how many characters in STRING the regexp
  343. in BUFFER matched, starting at position START. */
  344. extern int re_match
  345. _RE_ARGS ((struct re_pattern_buffer *buffer, const char *string,
  346. int length, int start, struct re_registers *regs));
  347. /* Relates to `re_match' as `re_search_2' relates to `re_search'. */
  348. extern int re_match_2
  349. _RE_ARGS ((struct re_pattern_buffer *buffer, const char *string1,
  350. int length1, const char *string2, int length2,
  351. int start, struct re_registers *regs, int stop));
  352. /* Set REGS to hold NUM_REGS registers, storing them in STARTS and
  353. ENDS. Subsequent matches using BUFFER and REGS will use this memory
  354. for recording register information. STARTS and ENDS must be
  355. allocated with malloc, and must each be at least `NUM_REGS * sizeof
  356. (regoff_t)' bytes long.
  357. If NUM_REGS == 0, then subsequent matches should allocate their own
  358. register data.
  359. Unless this function is called, the first search or match using
  360. PATTERN_BUFFER will allocate its own register data, without
  361. freeing the old data. */
  362. extern void re_set_registers
  363. _RE_ARGS ((struct re_pattern_buffer *buffer, struct re_registers *regs,
  364. unsigned num_regs, regoff_t *starts, regoff_t *ends));
  365. /* 4.2 bsd compatibility. */
  366. extern char *re_comp _RE_ARGS ((const char *));
  367. extern int re_exec _RE_ARGS ((const char *));
  368. /* POSIX compatibility. */
  369. extern int regcomp _RE_ARGS ((regex_t *preg, const char *pattern, int cflags));
  370. extern int regexec
  371. _RE_ARGS ((const regex_t *preg, const char *string, size_t nmatch,
  372. regmatch_t pmatch[], int eflags));
  373. extern size_t regerror
  374. _RE_ARGS ((int errcode, const regex_t *preg, char *errbuf,
  375. size_t errbuf_size));
  376. extern void regfree _RE_ARGS ((regex_t *preg));
  377. #endif /* not __REGEXP_LIBRARY_H__ */
  378. /*
  379. Local variables:
  380. make-backup-files: t
  381. version-control: t
  382. trim-versions-without-asking: nil
  383. End:
  384. */
  385. #endif
  386. extern int nasl_regcomp (regex_t *preg, const char *pattern, int cflags);
  387. extern int nasl_regexec (const regex_t *preg, const char *string, size_t nmatch,
  388. regmatch_t pmatch[], int eflags);
  389. extern size_t nasl_regerror (int errcode, const regex_t *preg, char *errbuf,
  390. size_t errbuf_size);
  391. extern reg_syntax_t nasl_re_set_syntax (reg_syntax_t syntax);
  392. extern char * nasl_regorig(regex_t * preg);
  393. extern void nasl_regfree (regex_t *preg);