• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*************************************************
2 *      Perl-Compatible Regular Expressions       *
3 *************************************************/
4 
5 /* PCRE2 is a library of functions to support regular expressions whose syntax
6 and semantics are as close as possible to those of the Perl 5 language. This is
7 the public header file to be #included by applications that call PCRE2 via the
8 POSIX wrapper interface.
9 
10                        Written by Philip Hazel
11      Original API code Copyright (c) 1997-2012 University of Cambridge
12           New API code Copyright (c) 2016-2022 University of Cambridge
13 
14 -----------------------------------------------------------------------------
15 Redistribution and use in source and binary forms, with or without
16 modification, are permitted provided that the following conditions are met:
17 
18     * Redistributions of source code must retain the above copyright notice,
19       this list of conditions and the following disclaimer.
20 
21     * Redistributions in binary form must reproduce the above copyright
22       notice, this list of conditions and the following disclaimer in the
23       documentation and/or other materials provided with the distribution.
24 
25     * Neither the name of the University of Cambridge nor the names of its
26       contributors may be used to endorse or promote products derived from
27       this software without specific prior written permission.
28 
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 
44 /* Have to include stdlib.h in order to ensure that size_t is defined. */
45 
46 #include <stdlib.h>
47 
48 /* Allow for C++ users */
49 
50 #ifdef __cplusplus
51 extern "C" {
52 #endif
53 
54 /* Options, mostly defined by POSIX, but with some extras. */
55 
56 #define REG_ICASE     0x0001  /* Maps to PCRE2_CASELESS */
57 #define REG_NEWLINE   0x0002  /* Maps to PCRE2_MULTILINE */
58 #define REG_NOTBOL    0x0004  /* Maps to PCRE2_NOTBOL */
59 #define REG_NOTEOL    0x0008  /* Maps to PCRE2_NOTEOL */
60 #define REG_DOTALL    0x0010  /* NOT defined by POSIX; maps to PCRE2_DOTALL */
61 #define REG_NOSUB     0x0020  /* Do not report what was matched */
62 #define REG_UTF       0x0040  /* NOT defined by POSIX; maps to PCRE2_UTF */
63 #define REG_STARTEND  0x0080  /* BSD feature: pass subject string by so,eo */
64 #define REG_NOTEMPTY  0x0100  /* NOT defined by POSIX; maps to PCRE2_NOTEMPTY */
65 #define REG_UNGREEDY  0x0200  /* NOT defined by POSIX; maps to PCRE2_UNGREEDY */
66 #define REG_UCP       0x0400  /* NOT defined by POSIX; maps to PCRE2_UCP */
67 #define REG_PEND      0x0800  /* GNU feature: pass end pattern by re_endp */
68 #define REG_NOSPEC    0x1000  /* Maps to PCRE2_LITERAL */
69 
70 /* This is not used by PCRE2, but by defining it we make it easier
71 to slot PCRE2 into existing programs that make POSIX calls. */
72 
73 #define REG_EXTENDED  0
74 
75 /* Error values. Not all these are relevant or used by the wrapper. */
76 
77 enum {
78   REG_ASSERT = 1,  /* internal error ? */
79   REG_BADBR,       /* invalid repeat counts in {} */
80   REG_BADPAT,      /* pattern error */
81   REG_BADRPT,      /* ? * + invalid */
82   REG_EBRACE,      /* unbalanced {} */
83   REG_EBRACK,      /* unbalanced [] */
84   REG_ECOLLATE,    /* collation error - not relevant */
85   REG_ECTYPE,      /* bad class */
86   REG_EESCAPE,     /* bad escape sequence */
87   REG_EMPTY,       /* empty expression */
88   REG_EPAREN,      /* unbalanced () */
89   REG_ERANGE,      /* bad range inside [] */
90   REG_ESIZE,       /* expression too big */
91   REG_ESPACE,      /* failed to get memory */
92   REG_ESUBREG,     /* bad back reference */
93   REG_INVARG,      /* bad argument */
94   REG_NOMATCH      /* match failed */
95 };
96 
97 
98 /* The structure representing a compiled regular expression. It is also used
99 for passing the pattern end pointer when REG_PEND is set. */
100 
101 typedef struct {
102   void *re_pcre2_code;
103   void *re_match_data;
104   const char *re_endp;
105   size_t re_nsub;
106   size_t re_erroffset;
107   int re_cflags;
108 } regex_t;
109 
110 /* The structure in which a captured offset is returned. */
111 
112 typedef int regoff_t;
113 
114 typedef struct {
115   regoff_t rm_so;
116   regoff_t rm_eo;
117 } regmatch_t;
118 
119 /* When compiling with the MSVC compiler, it is sometimes necessary to include
120 a "calling convention" before exported function names. (This is secondhand
121 information; I know nothing about MSVC myself). For example, something like
122 
123   void __cdecl function(....)
124 
125 might be needed. In order to make this easy, all the exported functions have
126 PCRE2_CALL_CONVENTION just before their names. It is rarely needed; if not
127 set, we ensure here that it has no effect. */
128 
129 #ifndef PCRE2_CALL_CONVENTION
130 #define PCRE2_CALL_CONVENTION
131 #endif
132 
133 /* When an application links to a PCRE2 DLL in Windows, the symbols that are
134 imported have to be identified as such. When building PCRE2, the appropriate
135 export settings are needed, and are set in pcre2posix.c before including this
136 file. */
137 
138 #if defined(_WIN32) && !defined(PCRE2_STATIC) && !defined(PCRE2POSIX_EXP_DECL)
139 #  define PCRE2POSIX_EXP_DECL  extern __declspec(dllimport)
140 #  define PCRE2POSIX_EXP_DEFN  __declspec(dllimport)
141 #endif
142 
143 /* By default, we use the standard "extern" declarations. */
144 
145 #ifndef PCRE2POSIX_EXP_DECL
146 #  ifdef __cplusplus
147 #    define PCRE2POSIX_EXP_DECL  extern "C"
148 #    define PCRE2POSIX_EXP_DEFN  extern "C"
149 #  else
150 #    define PCRE2POSIX_EXP_DECL  extern
151 #    define PCRE2POSIX_EXP_DEFN  extern
152 #  endif
153 #endif
154 
155 /* The functions. The actual code is in functions with pcre2_xxx names for
156 uniqueness. POSIX names are provided as macros for API compatibility with POSIX
157 regex functions. It's done this way to ensure to they are always linked from
158 the PCRE2 library and not by accident from elsewhere (regex_t differs in size
159 elsewhere). */
160 
161 PCRE2POSIX_EXP_DECL int PCRE2_CALL_CONVENTION pcre2_regcomp(regex_t *, const char *, int);
162 PCRE2POSIX_EXP_DECL int PCRE2_CALL_CONVENTION pcre2_regexec(const regex_t *, const char *, size_t,
163                      regmatch_t *, int);
164 PCRE2POSIX_EXP_DECL size_t PCRE2_CALL_CONVENTION pcre2_regerror(int, const regex_t *, char *, size_t);
165 PCRE2POSIX_EXP_DECL void PCRE2_CALL_CONVENTION pcre2_regfree(regex_t *);
166 
167 #define regcomp  pcre2_regcomp
168 #define regexec  pcre2_regexec
169 #define regerror pcre2_regerror
170 #define regfree  pcre2_regfree
171 
172 /* Debian had a patch that used different names. These are now here to save
173 them having to maintain their own patch, but are not documented by PCRE2. */
174 
175 #define PCRE2regcomp  pcre2_regcomp
176 #define PCRE2regexec  pcre2_regexec
177 #define PCRE2regerror pcre2_regerror
178 #define PCRE2regfree  pcre2_regfree
179 
180 #ifdef __cplusplus
181 }   /* extern "C" */
182 #endif
183 
184 /* End of pcre2posix.h */
185