The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
regex.h
Go to the documentation of this file.
1#pragma once
2/*
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
16 */
17#ifdef HAVE_REGEX
18/** Wrappers around various regular expression libraries
19 *
20 * @file src/lib/util/regex.h
21 *
22 * @copyright 2014 The FreeRADIUS server project
23 */
24RCSIDH(regex_h, "$Id: 830f93319248dc3ed8a37adc0ea271710ad812a9 $")
25
26# ifdef __cplusplus
27extern "C" {
28# endif
29
30#include <freeradius-devel/build.h>
31#include <freeradius-devel/missing.h>
32#include <freeradius-devel/util/sbuff.h>
33#include <freeradius-devel/util/talloc.h>
34#include <freeradius-devel/util/value.h>
35
36#include <unistd.h>
37
38/*
39 *######################################
40 *# STRUCTURES FOR LIBPCRE2 #
41 *######################################
42 */
43# ifdef HAVE_REGEX_PCRE2
44# define PCRE2_CODE_UNIT_WIDTH 8
45# include <pcre2.h>
46/*
47 * libpcre defines its matches as an array of ints which is a
48 * multiple of three.
49 */
50
51/** libpcre2 has its own matchdata struct, we wrap it so we can use talloc destructors
52 *
53 */
54typedef struct {
55 pcre2_match_data *match_data; //!< Match data containing the subject
56 ///< and various match offsets.
57 size_t used; //!< Number of slots filled with match data.
58#ifndef NDEBUG
59 char const *subject; //!< Here for debugging purposes if we explicitly duped the string.
60#endif
61} fr_regmatch_t;
62
63typedef struct {
64 pcre2_code *compiled; //!< Compiled regular expression.
65 uint32_t subcaptures; //!< Number of subcaptures contained within the expression.
66
67 bool precompiled; //!< Whether this regex was precompiled,
68 ///< or compiled for one off evaluation.
69 bool jitd; //!< Whether JIT data is available.
70} regex_t;
71/*
72 *######################################
73 *# STRUCTURES FOR POSIX-REGEX #
74 *######################################
75 */
76# else
77# include <regex.h>
78/*
79 * Allow REG_EXTENDED and REG_NOSUB to be or'd with flags
80 * if they're not defined.
81 */
82# ifndef REG_EXTENDED
83# define REG_EXTENDED (0)
84# endif
85
86# ifndef REG_NOSUB
87# define REG_NOSUB (0)
88# endif
89
90/** Emulates the functionality of the pcre2_match_data struct
91 *
92 */
93typedef struct {
94 regmatch_t *match_data; //!< Slots for matches.
95 size_t allocd; //!< Number of slots allocated for match data.
96 size_t used; //!< Number of slots filled with match data.
97 char const *subject; //!< A local copy of the subject.
98} fr_regmatch_t;
99
100# endif
101
102/*
103 *########################################
104 *# UNIVERSAL FUNCTIONS AND STRUCTURES #
105 *########################################
106 */
107
108/** The set of all flags implemented by the different regex libraries
109 *
110 * A specific library may not implement all these flags. If an unsupported flag is high
111 * then the library will produce an error.
112 */
113typedef struct {
114 unsigned int global : 1; //!< g - Perform global matching or substitution.
115 unsigned int ignore_case : 1; //!< i - Perform case insensitive matching.
116 unsigned int multiline : 1; //!< m - Multiline search.
117 unsigned int dot_all : 1; //!< s - Singleline - '.' matches everything, including newlines.
118 unsigned int unicode : 1; //!< u - Use unicode properties for character with code points
119 ///< greater than 127.
120 unsigned int extended : 1; //!< x - Permit whitespace and comments.
121} fr_regex_flags_t;
122
123#define REGEX_FLAG_BUFF_SIZE 7
124
125extern const fr_sbuff_escape_rules_t regex_escape_rules;
126
127/** Unique safefor value to prevent escaping for regexes
128 */
129#define FR_REGEX_SAFE_FOR ((uintptr_t)regex_exec)
130
131ssize_t regex_flags_parse(int *err, fr_regex_flags_t *out, fr_sbuff_t *in,
132 fr_sbuff_term_t const *terminals, bool err_on_dup);
133
134ssize_t regex_flags_print(fr_sbuff_t *sbuff, fr_regex_flags_t const *flags);
135
136 ssize_t regex_compile(TALLOC_CTX *ctx, regex_t **out, char const *pattern, size_t len,
137 fr_regex_flags_t const *flags, bool subcaptures, bool runtime);
138int regex_exec(regex_t *preg, char const *subject, size_t len, fr_regmatch_t *regmatch) CC_HINT(nonnull(1,2));
139#ifdef HAVE_REGEX_PCRE2
140int regex_substitute(TALLOC_CTX *ctx, char **out, size_t max_out, regex_t *preg, fr_regex_flags_t const *flags,
141 char const *subject, size_t subject_len,
142 char const *replacement, size_t replacement_len,
143 fr_regmatch_t *regmatch);
144#endif
145uint32_t regex_subcapture_count(regex_t const *preg);
146fr_regmatch_t *regex_match_data_alloc(TALLOC_CTX *ctx, uint32_t count);
147
148int fr_regex_cmp_op(fr_token_t op, fr_value_box_t const *a, fr_value_box_t const *b) CC_HINT(nonnull);
149
150# ifdef __cplusplus
151}
152# endif
153#endif
#define RCSIDH(h, id)
Definition build.h:507
static fr_slen_t err
Definition dict.h:882
static fr_slen_t in
Definition dict.h:882
unsigned int uint32_t
long int ssize_t
static size_t used
Set of terminal elements.
return count
Definition module.c:155
enum fr_token fr_token_t
int fr_regex_cmp_op(fr_token_t op, fr_value_box_t const *a, fr_value_box_t const *b)
Compare two boxes using an operator.
Definition regex.c:1024
int nonnull(2, 5))
static size_t char ** out
Definition value.h:1030