ap_regex.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* This code is based on pcreposix.h from the PCRE Library distribution,
  17. * as originally written by Philip Hazel <ph10@cam.ac.uk>, and forked by
  18. * the Apache HTTP Server project to provide POSIX-style regex function
  19. * wrappers around underlying PCRE library functions for httpd.
  20. *
  21. * The original source file pcreposix.h is copyright and licensed as follows;
  22. Copyright (c) 1997-2004 University of Cambridge
  23. -----------------------------------------------------------------------------
  24. Redistribution and use in source and binary forms, with or without
  25. modification, are permitted provided that the following conditions are met:
  26. * Redistributions of source code must retain the above copyright notice,
  27. this list of conditions and the following disclaimer.
  28. * Redistributions in binary form must reproduce the above copyright
  29. notice, this list of conditions and the following disclaimer in the
  30. documentation and/or other materials provided with the distribution.
  31. * Neither the name of the University of Cambridge nor the names of its
  32. contributors may be used to endorse or promote products derived from
  33. this software without specific prior written permission.
  34. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  35. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  36. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  37. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  38. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  39. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  40. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  41. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  42. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  43. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  44. POSSIBILITY OF SUCH DAMAGE.
  45. -----------------------------------------------------------------------------
  46. */
  47. /**
  48. * @file ap_regex.h
  49. * @brief Apache Regex defines
  50. */
  51. #ifndef AP_REGEX_H
  52. #define AP_REGEX_H
  53. #include "apr.h"
  54. /* Allow for C++ users */
  55. #ifdef __cplusplus
  56. extern "C" {
  57. #endif
  58. /* Options for ap_regcomp, ap_regexec, and ap_rxplus versions: */
  59. #define AP_REG_ICASE 0x01 /** use a case-insensitive match */
  60. #define AP_REG_NEWLINE 0x02 /** don't match newlines against '.' etc */
  61. #define AP_REG_NOTBOL 0x04 /** ^ will not match against start-of-string */
  62. #define AP_REG_NOTEOL 0x08 /** $ will not match against end-of-string */
  63. #define AP_REG_EXTENDED (0) /** unused */
  64. #define AP_REG_NOSUB (0) /** unused */
  65. #define AP_REG_MULTI 0x10 /* perl's /g (needs fixing) */
  66. #define AP_REG_NOMEM 0x20 /* nomem in our code */
  67. #define AP_REG_DOTALL 0x40 /* perl's /s flag */
  68. #define AP_REG_DOLLAR_ENDONLY 0x200 /* '$' matches at end of subject string only */
  69. #define AP_REG_MATCH "MATCH_" /** suggested prefix for ap_regname */
  70. /* Error values: */
  71. enum {
  72. AP_REG_ASSERT = 1, /** internal error ? */
  73. AP_REG_ESPACE, /** failed to get memory */
  74. AP_REG_INVARG, /** invalid argument */
  75. AP_REG_NOMATCH /** match failed */
  76. };
  77. /* The structure representing a compiled regular expression. */
  78. typedef struct {
  79. void *re_pcre;
  80. int re_nsub;
  81. apr_size_t re_erroffset;
  82. } ap_regex_t;
  83. /* The structure in which a captured offset is returned. */
  84. typedef struct {
  85. int rm_so;
  86. int rm_eo;
  87. } ap_regmatch_t;
  88. /* The functions */
  89. /**
  90. * Get default compile flags
  91. * @return Bitwise OR of AP_REG_* flags
  92. */
  93. AP_DECLARE(int) ap_regcomp_get_default_cflags(void);
  94. /**
  95. * Set default compile flags
  96. * @param cflags Bitwise OR of AP_REG_* flags
  97. */
  98. AP_DECLARE(void) ap_regcomp_set_default_cflags(int cflags);
  99. /**
  100. * Get the AP_REG_* corresponding to the string.
  101. * @param name The name (i.e. AP_REG_<name>)
  102. * @return The AP_REG_*, or zero if the string is unknown
  103. *
  104. */
  105. AP_DECLARE(int) ap_regcomp_default_cflag_by_name(const char *name);
  106. /**
  107. * Compile a regular expression.
  108. * @param preg Returned compiled regex
  109. * @param regex The regular expression string
  110. * @param cflags Bitwise OR of AP_REG_* flags (ICASE and NEWLINE supported,
  111. * other flags are ignored)
  112. * @return Zero on success or non-zero on error
  113. */
  114. AP_DECLARE(int) ap_regcomp(ap_regex_t *preg, const char *regex, int cflags);
  115. /**
  116. * Match a NUL-terminated string against a pre-compiled regex.
  117. * @param preg The pre-compiled regex
  118. * @param string The string to match
  119. * @param nmatch Provide information regarding the location of any matches
  120. * @param pmatch Provide information regarding the location of any matches
  121. * @param eflags Bitwise OR of AP_REG_* flags (NOTBOL and NOTEOL supported,
  122. * other flags are ignored)
  123. * @return 0 for successful match, \p AP_REG_NOMATCH otherwise
  124. */
  125. AP_DECLARE(int) ap_regexec(const ap_regex_t *preg, const char *string,
  126. apr_size_t nmatch, ap_regmatch_t *pmatch, int eflags);
  127. /**
  128. * Match a string with given length against a pre-compiled regex. The string
  129. * does not need to be NUL-terminated.
  130. * @param preg The pre-compiled regex
  131. * @param buff The string to match
  132. * @param len Length of the string to match
  133. * @param nmatch Provide information regarding the location of any matches
  134. * @param pmatch Provide information regarding the location of any matches
  135. * @param eflags Bitwise OR of AP_REG_* flags (NOTBOL and NOTEOL supported,
  136. * other flags are ignored)
  137. * @return 0 for successful match, AP_REG_NOMATCH otherwise
  138. */
  139. AP_DECLARE(int) ap_regexec_len(const ap_regex_t *preg, const char *buff,
  140. apr_size_t len, apr_size_t nmatch,
  141. ap_regmatch_t *pmatch, int eflags);
  142. /**
  143. * Return the error code returned by regcomp or regexec into error messages
  144. * @param errcode the error code returned by regexec or regcomp
  145. * @param preg The precompiled regex
  146. * @param errbuf A buffer to store the error in
  147. * @param errbuf_size The size of the buffer
  148. */
  149. AP_DECLARE(apr_size_t) ap_regerror(int errcode, const ap_regex_t *preg,
  150. char *errbuf, apr_size_t errbuf_size);
  151. /**
  152. * Return an array of named regex backreferences
  153. * @param preg The precompiled regex
  154. * @param names The array to which the names will be added
  155. * @param upper If non zero, uppercase the names
  156. */
  157. AP_DECLARE(int) ap_regname(const ap_regex_t *preg,
  158. apr_array_header_t *names, const char *prefix,
  159. int upper);
  160. /** Destroy a pre-compiled regex.
  161. * @param preg The pre-compiled regex to free.
  162. */
  163. AP_DECLARE(void) ap_regfree(ap_regex_t *preg);
  164. /* ap_rxplus: higher-level regexps */
  165. typedef struct {
  166. ap_regex_t rx;
  167. apr_uint32_t flags;
  168. const char *subs;
  169. const char *match;
  170. apr_size_t nmatch;
  171. ap_regmatch_t *pmatch;
  172. } ap_rxplus_t;
  173. /**
  174. * Compile a pattern into a regexp.
  175. * supports perl-like formats
  176. * match-string
  177. * /match-string/flags
  178. * s/match-string/replacement-string/flags
  179. * Intended to support more perl-like stuff as and when round tuits happen
  180. * match-string is anything supported by ap_regcomp
  181. * replacement-string is a substitution string as supported in ap_pregsub
  182. * flags should correspond with perl syntax: treat failure to do so as a bug
  183. * (documentation TBD)
  184. * @param pool Pool to allocate from
  185. * @param pattern Pattern to compile
  186. * @return Compiled regexp, or NULL in case of compile/syntax error
  187. */
  188. AP_DECLARE(ap_rxplus_t*) ap_rxplus_compile(apr_pool_t *pool, const char *pattern);
  189. /**
  190. * Apply a regexp operation to a string.
  191. * @param pool Pool to allocate from
  192. * @param rx The regex match to apply
  193. * @param pattern The string to apply it to
  194. * NOTE: This MUST be kept in scope to use regexp memory
  195. * @param newpattern The modified string (ignored if the operation doesn't
  196. * modify the string)
  197. * @return Number of times a match happens. Normally 0 (no match) or 1
  198. * (match found), but may be greater if a transforming pattern
  199. * is applied with the 'g' flag.
  200. */
  201. AP_DECLARE(int) ap_rxplus_exec(apr_pool_t *pool, ap_rxplus_t *rx,
  202. const char *pattern, char **newpattern);
  203. #ifdef DOXYGEN
  204. /**
  205. * Number of matches in the regexp operation's memory
  206. * This may be 0 if no match is in memory, or up to nmatch from compilation
  207. * @param rx The regexp
  208. * @return Number of matches in memory
  209. */
  210. AP_DECLARE(int) ap_rxplus_nmatch(ap_rxplus_t *rx);
  211. #else
  212. #define ap_rxplus_nmatch(rx) (((rx)->match != NULL) ? (rx)->nmatch : 0)
  213. #endif
  214. /**
  215. * Get a pointer to a match from regex memory
  216. * NOTE: this relies on the match pattern from the last call to
  217. * ap_rxplus_exec still being valid (i.e. not freed or out-of-scope)
  218. * @param rx The regexp
  219. * @param n The match number to retrieve (must be between 0 and nmatch)
  220. * @param len Returns the length of the match.
  221. * @param match Returns the match pattern
  222. */
  223. AP_DECLARE(void) ap_rxplus_match(ap_rxplus_t *rx, int n, int *len,
  224. const char **match);
  225. /**
  226. * Get a match from regex memory in a string copy
  227. * NOTE: this relies on the match pattern from the last call to
  228. * ap_rxplus_exec still being valid (i.e. not freed or out-of-scope)
  229. * @param pool Pool to allocate from
  230. * @param rx The regexp
  231. * @param n The match number to retrieve (must be between 0 and nmatch)
  232. * @return The matched string
  233. */
  234. AP_DECLARE(char*) ap_rxplus_pmatch(apr_pool_t *pool, ap_rxplus_t *rx, int n);
  235. #ifdef __cplusplus
  236. } /* extern "C" */
  237. #endif
  238. #endif /* AP_REGEX_T */