xref: /aosp_15_r20/external/selinux/libselinux/src/regex.h (revision 2d543d20722ada2425b5bdab9d0d1d29470e7bba)
1*2d543d20SAndroid Build Coastguard Worker #ifndef SRC_REGEX_H_
2*2d543d20SAndroid Build Coastguard Worker #define SRC_REGEX_H_
3*2d543d20SAndroid Build Coastguard Worker 
4*2d543d20SAndroid Build Coastguard Worker #include <stdbool.h>
5*2d543d20SAndroid Build Coastguard Worker #include <stdio.h>
6*2d543d20SAndroid Build Coastguard Worker 
7*2d543d20SAndroid Build Coastguard Worker #ifdef USE_PCRE2
8*2d543d20SAndroid Build Coastguard Worker #include <pcre2.h>
9*2d543d20SAndroid Build Coastguard Worker #else
10*2d543d20SAndroid Build Coastguard Worker #include <pcre.h>
11*2d543d20SAndroid Build Coastguard Worker #endif
12*2d543d20SAndroid Build Coastguard Worker 
13*2d543d20SAndroid Build Coastguard Worker 
14*2d543d20SAndroid Build Coastguard Worker enum { REGEX_MATCH,
15*2d543d20SAndroid Build Coastguard Worker        REGEX_MATCH_PARTIAL,
16*2d543d20SAndroid Build Coastguard Worker        REGEX_NO_MATCH,
17*2d543d20SAndroid Build Coastguard Worker        REGEX_ERROR = -1,
18*2d543d20SAndroid Build Coastguard Worker };
19*2d543d20SAndroid Build Coastguard Worker 
20*2d543d20SAndroid Build Coastguard Worker struct regex_data;
21*2d543d20SAndroid Build Coastguard Worker 
22*2d543d20SAndroid Build Coastguard Worker #ifdef USE_PCRE2
23*2d543d20SAndroid Build Coastguard Worker struct regex_error_data {
24*2d543d20SAndroid Build Coastguard Worker 	int error_code;
25*2d543d20SAndroid Build Coastguard Worker 	PCRE2_SIZE error_offset;
26*2d543d20SAndroid Build Coastguard Worker };
27*2d543d20SAndroid Build Coastguard Worker #else
28*2d543d20SAndroid Build Coastguard Worker struct regex_error_data {
29*2d543d20SAndroid Build Coastguard Worker 	char const *error_buffer;
30*2d543d20SAndroid Build Coastguard Worker 	int error_offset;
31*2d543d20SAndroid Build Coastguard Worker };
32*2d543d20SAndroid Build Coastguard Worker #endif
33*2d543d20SAndroid Build Coastguard Worker 
34*2d543d20SAndroid Build Coastguard Worker struct mmap_area;
35*2d543d20SAndroid Build Coastguard Worker 
36*2d543d20SAndroid Build Coastguard Worker /**
37*2d543d20SAndroid Build Coastguard Worker  * regex_arch_string return a string that represents the pointer width, the
38*2d543d20SAndroid Build Coastguard Worker  * width of what the backend considers a size type, and the endianness of the
39*2d543d20SAndroid Build Coastguard Worker  * system that this library was build for. (e.g. for x86_64: "8-8-el").
40*2d543d20SAndroid Build Coastguard Worker  * This is required when loading stored regular espressions. PCRE2 regular
41*2d543d20SAndroid Build Coastguard Worker  * expressions are not portable across architectures that do not have a
42*2d543d20SAndroid Build Coastguard Worker  * matching arch-string.
43*2d543d20SAndroid Build Coastguard Worker  */
44*2d543d20SAndroid Build Coastguard Worker char const *regex_arch_string(void) ;
45*2d543d20SAndroid Build Coastguard Worker 
46*2d543d20SAndroid Build Coastguard Worker /**
47*2d543d20SAndroid Build Coastguard Worker  * regex_version returns the version string of the underlying regular
48*2d543d20SAndroid Build Coastguard Worker  * regular expressions library. In the case of PCRE it just returns the
49*2d543d20SAndroid Build Coastguard Worker  * result of pcre_version(). In the case of PCRE2, the very first time this
50*2d543d20SAndroid Build Coastguard Worker  * function is called it allocates a buffer large enough to hold the version
51*2d543d20SAndroid Build Coastguard Worker  * string and reads the PCRE2_CONFIG_VERSION option to fill the buffer.
52*2d543d20SAndroid Build Coastguard Worker  * The allocated buffer will linger in memory until the calling process is being
53*2d543d20SAndroid Build Coastguard Worker  * reaped.
54*2d543d20SAndroid Build Coastguard Worker  *
55*2d543d20SAndroid Build Coastguard Worker  * It may return NULL on error.
56*2d543d20SAndroid Build Coastguard Worker  */
57*2d543d20SAndroid Build Coastguard Worker char const *regex_version(void) ;
58*2d543d20SAndroid Build Coastguard Worker /**
59*2d543d20SAndroid Build Coastguard Worker  * This constructor function allocates a buffer for a regex_data structure.
60*2d543d20SAndroid Build Coastguard Worker  * The buffer is being initialized with zeroes.
61*2d543d20SAndroid Build Coastguard Worker  */
62*2d543d20SAndroid Build Coastguard Worker struct regex_data *regex_data_create(void) ;
63*2d543d20SAndroid Build Coastguard Worker /**
64*2d543d20SAndroid Build Coastguard Worker  * This complementary destructor function frees the a given regex_data buffer.
65*2d543d20SAndroid Build Coastguard Worker  * It also frees any non NULL member pointers with the appropriate pcreX_X_free
66*2d543d20SAndroid Build Coastguard Worker  * function. For PCRE this function respects the extra_owned field and frees
67*2d543d20SAndroid Build Coastguard Worker  * the pcre_extra data conditionally. Calling this function on a NULL pointer is
68*2d543d20SAndroid Build Coastguard Worker  * save.
69*2d543d20SAndroid Build Coastguard Worker  */
70*2d543d20SAndroid Build Coastguard Worker void regex_data_free(struct regex_data *regex) ;
71*2d543d20SAndroid Build Coastguard Worker /**
72*2d543d20SAndroid Build Coastguard Worker  * This function compiles the regular expression. Additionally, it prepares
73*2d543d20SAndroid Build Coastguard Worker  * data structures required by the different underlying engines. For PCRE
74*2d543d20SAndroid Build Coastguard Worker  * it calls pcre_study to generate optional data required for optimized
75*2d543d20SAndroid Build Coastguard Worker  * execution of the compiled pattern. In the case of PCRE2, it allocates
76*2d543d20SAndroid Build Coastguard Worker  * a pcre2_match_data structure of appropriate size to hold all possible
77*2d543d20SAndroid Build Coastguard Worker  * matches created by the pattern.
78*2d543d20SAndroid Build Coastguard Worker  *
79*2d543d20SAndroid Build Coastguard Worker  * @arg regex If successful, the structure returned through *regex was allocated
80*2d543d20SAndroid Build Coastguard Worker  *            with regex_data_create and must be freed with regex_data_free.
81*2d543d20SAndroid Build Coastguard Worker  * @arg pattern_string The pattern string that is to be compiled.
82*2d543d20SAndroid Build Coastguard Worker  * @arg errordata A pointer to a regex_error_data structure must be passed
83*2d543d20SAndroid Build Coastguard Worker  *                to this function. This structure depends on the underlying
84*2d543d20SAndroid Build Coastguard Worker  *                implementation. It can be passed to regex_format_error
85*2d543d20SAndroid Build Coastguard Worker  *                to generate a human readable error message.
86*2d543d20SAndroid Build Coastguard Worker  * @retval 0 on success
87*2d543d20SAndroid Build Coastguard Worker  * @retval -1 on error
88*2d543d20SAndroid Build Coastguard Worker  */
89*2d543d20SAndroid Build Coastguard Worker int regex_prepare_data(struct regex_data **regex, char const *pattern_string,
90*2d543d20SAndroid Build Coastguard Worker 		       struct regex_error_data *errordata) ;
91*2d543d20SAndroid Build Coastguard Worker /**
92*2d543d20SAndroid Build Coastguard Worker  * This function loads a serialized precompiled pattern from a contiguous
93*2d543d20SAndroid Build Coastguard Worker  * data region given by map_area.
94*2d543d20SAndroid Build Coastguard Worker  *
95*2d543d20SAndroid Build Coastguard Worker  * @arg map_area Description of the memory region holding a serialized
96*2d543d20SAndroid Build Coastguard Worker  *               representation of the precompiled pattern.
97*2d543d20SAndroid Build Coastguard Worker  * @arg regex If successful, the structure returned through *regex was allocated
98*2d543d20SAndroid Build Coastguard Worker  *            with regex_data_create and must be freed with regex_data_free.
99*2d543d20SAndroid Build Coastguard Worker  * @arg do_load_precompregex If non-zero precompiled patterns get loaded from
100*2d543d20SAndroid Build Coastguard Worker  *			     the mmap region (ignored by PCRE1 back-end).
101*2d543d20SAndroid Build Coastguard Worker  * @arg regex_compiled Set to true if a precompiled pattern was loaded
102*2d543d20SAndroid Build Coastguard Worker  * 		       into regex, otherwise set to false to indicate later
103*2d543d20SAndroid Build Coastguard Worker  *		       compilation must occur
104*2d543d20SAndroid Build Coastguard Worker  *
105*2d543d20SAndroid Build Coastguard Worker  * @retval 0 on success
106*2d543d20SAndroid Build Coastguard Worker  * @retval -1 on error
107*2d543d20SAndroid Build Coastguard Worker  */
108*2d543d20SAndroid Build Coastguard Worker int regex_load_mmap(struct mmap_area *map_area,
109*2d543d20SAndroid Build Coastguard Worker 		    struct regex_data **regex,
110*2d543d20SAndroid Build Coastguard Worker 		    int do_load_precompregex,
111*2d543d20SAndroid Build Coastguard Worker 		    bool *regex_compiled) ;
112*2d543d20SAndroid Build Coastguard Worker /**
113*2d543d20SAndroid Build Coastguard Worker  * This function stores a precompiled regular expression to a file.
114*2d543d20SAndroid Build Coastguard Worker  * In the case of PCRE, it just dumps the binary representation of the
115*2d543d20SAndroid Build Coastguard Worker  * precomplied pattern into a file. In the case of PCRE2, it uses the
116*2d543d20SAndroid Build Coastguard Worker  * serialization function provided by the library.
117*2d543d20SAndroid Build Coastguard Worker  *
118*2d543d20SAndroid Build Coastguard Worker  * @arg regex The precomplied regular expression data.
119*2d543d20SAndroid Build Coastguard Worker  * @arg fp A file stream specifying the output file.
120*2d543d20SAndroid Build Coastguard Worker  * @arg do_write_precompregex If non-zero precompiled patterns are written to
121*2d543d20SAndroid Build Coastguard Worker  *			      the output file (ignored by PCRE1 back-end).
122*2d543d20SAndroid Build Coastguard Worker  */
123*2d543d20SAndroid Build Coastguard Worker int regex_writef(struct regex_data *regex, FILE *fp,
124*2d543d20SAndroid Build Coastguard Worker 		 int do_write_precompregex) ;
125*2d543d20SAndroid Build Coastguard Worker /**
126*2d543d20SAndroid Build Coastguard Worker  * This function applies a precompiled pattern to a subject string and
127*2d543d20SAndroid Build Coastguard Worker  * returns whether or not a match was found.
128*2d543d20SAndroid Build Coastguard Worker  *
129*2d543d20SAndroid Build Coastguard Worker  * @arg regex The precompiled pattern.
130*2d543d20SAndroid Build Coastguard Worker  * @arg subject The subject string.
131*2d543d20SAndroid Build Coastguard Worker  * @arg partial Boolean indicating if partial matches are wanted. A nonzero
132*2d543d20SAndroid Build Coastguard Worker  *              value is equivalent to specifying PCRE[2]_PARTIAL_SOFT as
133*2d543d20SAndroid Build Coastguard Worker  *              option to pcre_exec of pcre2_match.
134*2d543d20SAndroid Build Coastguard Worker  * @retval REGEX_MATCH if a match was found
135*2d543d20SAndroid Build Coastguard Worker  * @retval REGEX_MATCH_PARTIAL if a partial match was found
136*2d543d20SAndroid Build Coastguard Worker  * @retval REGEX_NO_MATCH if no match was found
137*2d543d20SAndroid Build Coastguard Worker  * @retval REGEX_ERROR if an error was encountered during the execution of the
138*2d543d20SAndroid Build Coastguard Worker  *                     regular expression
139*2d543d20SAndroid Build Coastguard Worker  */
140*2d543d20SAndroid Build Coastguard Worker int regex_match(struct regex_data *regex, char const *subject,
141*2d543d20SAndroid Build Coastguard Worker 		int partial) ;
142*2d543d20SAndroid Build Coastguard Worker /**
143*2d543d20SAndroid Build Coastguard Worker  * This function compares two compiled regular expressions (regex1 and regex2).
144*2d543d20SAndroid Build Coastguard Worker  * It compares the binary representations of the compiled patterns. It is a very
145*2d543d20SAndroid Build Coastguard Worker  * crude approximation because the binary representation holds data like
146*2d543d20SAndroid Build Coastguard Worker  * reference counters, that has nothing to do with the actual state machine.
147*2d543d20SAndroid Build Coastguard Worker  *
148*2d543d20SAndroid Build Coastguard Worker  * @retval SELABEL_EQUAL if the pattern's binary representations are exactly
149*2d543d20SAndroid Build Coastguard Worker  *                       the same
150*2d543d20SAndroid Build Coastguard Worker  * @retval SELABEL_INCOMPARABLE otherwise
151*2d543d20SAndroid Build Coastguard Worker  */
152*2d543d20SAndroid Build Coastguard Worker int regex_cmp(struct regex_data *regex1, struct regex_data *regex2) ;
153*2d543d20SAndroid Build Coastguard Worker /**
154*2d543d20SAndroid Build Coastguard Worker  * This function takes the error data returned by regex_prepare_data and turns
155*2d543d20SAndroid Build Coastguard Worker  * it in to a human readable error message.
156*2d543d20SAndroid Build Coastguard Worker  * If the buffer given to hold the error message is to small it truncates the
157*2d543d20SAndroid Build Coastguard Worker  * message and indicates the truncation with an ellipsis ("...") at the end of
158*2d543d20SAndroid Build Coastguard Worker  * the buffer.
159*2d543d20SAndroid Build Coastguard Worker  *
160*2d543d20SAndroid Build Coastguard Worker  * @arg error_data Error data as returned by regex_prepare_data.
161*2d543d20SAndroid Build Coastguard Worker  * @arg buffer String buffer to hold the formatted error string.
162*2d543d20SAndroid Build Coastguard Worker  * @arg buf_size Total size of the given buffer in bytes.
163*2d543d20SAndroid Build Coastguard Worker  */
164*2d543d20SAndroid Build Coastguard Worker void regex_format_error(struct regex_error_data const *error_data, char *buffer,
165*2d543d20SAndroid Build Coastguard Worker 			size_t buf_size) ;
166*2d543d20SAndroid Build Coastguard Worker #endif /* SRC_REGEX_H_ */
167