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