diff options
Diffstat (limited to 'utility/ap_pcre.h')
-rw-r--r-- | utility/ap_pcre.h | 176 |
1 files changed, 176 insertions, 0 deletions
diff --git a/utility/ap_pcre.h b/utility/ap_pcre.h new file mode 100644 index 0000000..a851d29 --- /dev/null +++ b/utility/ap_pcre.h | |||
@@ -0,0 +1,176 @@ | |||
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_regexec: */ | ||
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 | /* Error values: */ | ||
77 | enum { | ||
78 | AP_REG_ASSERT = 1, /** internal error ? */ | ||
79 | AP_REG_ESPACE, /** failed to get memory */ | ||
80 | AP_REG_INVARG, /** invalid argument */ | ||
81 | AP_REG_NOMATCH /** match failed */ | ||
82 | }; | ||
83 | |||
84 | /* The structure representing a compiled regular expression. */ | ||
85 | typedef struct { | ||
86 | void *re_pcre; | ||
87 | apr_size_t re_nsub; | ||
88 | apr_size_t re_erroffset; | ||
89 | } ap_regex_t; | ||
90 | |||
91 | /* The structure in which a captured offset is returned. */ | ||
92 | typedef struct { | ||
93 | int rm_so; | ||
94 | int rm_eo; | ||
95 | } ap_regmatch_t; | ||
96 | |||
97 | /* The functions */ | ||
98 | |||
99 | /** | ||
100 | * Compile a regular expression. | ||
101 | * @param preg Returned compiled regex | ||
102 | * @param regex The regular expression string | ||
103 | * @param cflags Must be zero (currently). | ||
104 | * @return Zero on success or non-zero on error | ||
105 | */ | ||
106 | int ap_regcomp(ap_regex_t *preg, const char *regex, int cflags); | ||
107 | |||
108 | /** | ||
109 | * Match a NUL-terminated string against a pre-compiled regex. | ||
110 | * @param preg The pre-compiled regex | ||
111 | * @param string The string to match | ||
112 | * @param nmatch Provide information regarding the location of any matches | ||
113 | * @param pmatch Provide information regarding the location of any matches | ||
114 | * @param eflags Bitwise OR of any of AP_REG_* flags | ||
115 | * @return 0 for successful match, #REG_NOMATCH otherwise | ||
116 | */ | ||
117 | int ap_regexec(const ap_regex_t *preg, const char *string, | ||
118 | apr_size_t nmatch, ap_regmatch_t *pmatch, int eflags); | ||
119 | |||
120 | /** | ||
121 | * Return the error code returned by regcomp or regexec into error messages | ||
122 | * @param errcode the error code returned by regexec or regcomp | ||
123 | * @param preg The precompiled regex | ||
124 | * @param errbuf A buffer to store the error in | ||
125 | * @param errbuf_size The size of the buffer | ||
126 | */ | ||
127 | apr_size_t ap_regerror(int errcode, const ap_regex_t *preg, | ||
128 | char *errbuf, apr_size_t errbuf_size); | ||
129 | |||
130 | /** Destroy a pre-compiled regex. | ||
131 | * @param preg The pre-compiled regex to free. | ||
132 | */ | ||
133 | void ap_regfree(ap_regex_t *preg); | ||
134 | |||
135 | /** | ||
136 | * Compile a regular expression to be used later | ||
137 | * @param p The pool to allocate from | ||
138 | * @param pattern the regular expression to compile | ||
139 | * @param cflags The bitwise or of one or more of the following: | ||
140 | * @li REG_EXTENDED - Use POSIX extended Regular Expressions | ||
141 | * @li REG_ICASE - Ignore case | ||
142 | * @li REG_NOSUB - Support for substring addressing of matches | ||
143 | * not required | ||
144 | * @li REG_NEWLINE - Match-any-character operators don't match new-line | ||
145 | * @return The compiled regular expression | ||
146 | */ | ||
147 | ap_regex_t * ap_pregcomp(apr_pool_t *p, const char *pattern, | ||
148 | int cflags); | ||
149 | |||
150 | /** | ||
151 | * Free the memory associated with a compiled regular expression | ||
152 | * @param p The pool the regex was allocated from | ||
153 | * @param reg The regular expression to free | ||
154 | */ | ||
155 | void ap_pregfree(apr_pool_t *p, ap_regex_t *reg); | ||
156 | |||
157 | /** | ||
158 | * After performing a successful regex match, you may use this function to | ||
159 | * perform a series of string substitutions based on subexpressions that were | ||
160 | * matched during the call to ap_regexec | ||
161 | * @param p The pool to allocate from | ||
162 | * @param input An arbitrary string containing $1 through $9. These are | ||
163 | * replaced with the corresponding matched sub-expressions | ||
164 | * @param source The string that was originally matched to the regex | ||
165 | * @param nmatch the nmatch returned from ap_pregex | ||
166 | * @param pmatch the pmatch array returned from ap_pregex | ||
167 | */ | ||
168 | char * ap_pregsub(apr_pool_t *p, const char *input, const char *source, | ||
169 | size_t nmatch, ap_regmatch_t pmatch[]); | ||
170 | |||
171 | #ifdef __cplusplus | ||
172 | } /* extern "C" */ | ||
173 | #endif | ||
174 | |||
175 | #endif /* AP_REGEX_T */ | ||
176 | |||