ap_regex.inc 11 KB

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