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: 7f3ec7b5ec05f8865c103aa4b204b7a706360105 $")
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 <stdbool.h>
37#include <stdint.h>
38#include <unistd.h>
39
40/*
41 *######################################
42 *# STRUCTURES FOR LIBPCRE2 #
43 *######################################
44 */
45# ifdef HAVE_REGEX_PCRE2
46# define PCRE2_CODE_UNIT_WIDTH 8
47# include <pcre2.h>
48/*
49 * libpcre defines its matches as an array of ints which is a
50 * multiple of three.
51 */
52
53/** libpcre2 has its own matchdata struct, we wrap it so we can use talloc destructors
54 *
55 */
56typedef struct {
57 pcre2_match_data *match_data; //!< Match data containing the subject
58 ///< and various match offsets.
59 size_t used; //!< Number of slots filled with match data.
60#ifndef NDEBUG
61 char const *subject; //!< Here for debugging purposes if we explicitly duped the string.
62#endif
63} fr_regmatch_t;
64
65typedef struct {
66 pcre2_code *compiled; //!< Compiled regular expression.
67 uint32_t subcaptures; //!< Number of subcaptures contained within the expression.
68
69 bool precompiled; //!< Whether this regex was precompiled,
70 ///< or compiled for one off evaluation.
71 bool jitd; //!< Whether JIT data is available.
72} regex_t;
73/*
74 *######################################
75 *# STRUCTURES FOR POSIX-REGEX #
76 *######################################
77 */
78# else
79# include <regex.h>
80/*
81 * Allow REG_EXTENDED and REG_NOSUB to be or'd with flags
82 * if they're not defined.
83 */
84# ifndef REG_EXTENDED
85# define REG_EXTENDED (0)
86# endif
87
88# ifndef REG_NOSUB
89# define REG_NOSUB (0)
90# endif
91
92/** Emulates the functionality of the pcre2_match_data struct
93 *
94 */
95typedef struct {
96 regmatch_t *match_data; //!< Slots for matches.
97 size_t allocd; //!< Number of slots allocated for match data.
98 size_t used; //!< Number of slots filled with match data.
99 char const *subject; //!< A local copy of the subject.
100} fr_regmatch_t;
101
102# endif
103
104/*
105 *########################################
106 *# UNIVERSAL FUNCTIONS AND STRUCTURES #
107 *########################################
108 */
109
110/** The set of all flags implemented by the different regex libraries
111 *
112 * A specific library may not implement all these flags. If an unsupported flag is high
113 * then the library will produce an error.
114 */
115typedef struct {
116 uint8_t global:1; //!< g - Perform global matching or substitution.
117 uint8_t ignore_case:1; //!< i - Perform case insensitive matching.
118 uint8_t multiline:1; //!< m - Multiline search.
119 uint8_t dot_all:1; //!< s - Singleline - '.' matches everything, including newlines.
120 uint8_t unicode:1; //!< u - Use unicode properties for character with code points
121 ///< greater than 127.
122 uint8_t extended:1; //!< x - Permit whitespace and comments.
123} fr_regex_flags_t;
124
125#define REGEX_FLAG_BUFF_SIZE 7
126
127extern const fr_sbuff_escape_rules_t regex_escape_rules;
128
129/** Unique safefor value to prevent escaping for regexes
130 */
131#define FR_REGEX_SAFE_FOR ((uintptr_t)regex_exec)
132
133ssize_t regex_flags_parse(int *err, fr_regex_flags_t *out, fr_sbuff_t *in,
134 fr_sbuff_term_t const *terminals, bool err_on_dup);
135
136ssize_t regex_flags_print(fr_sbuff_t *sbuff, fr_regex_flags_t const *flags);
137
138 ssize_t regex_compile(TALLOC_CTX *ctx, regex_t **out, char const *pattern, size_t len,
139 fr_regex_flags_t const *flags, bool subcaptures, bool runtime);
140int regex_exec(regex_t *preg, char const *subject, size_t len, fr_regmatch_t *regmatch) CC_HINT(nonnull(1,2));
141#ifdef HAVE_REGEX_PCRE2
142int regex_substitute(TALLOC_CTX *ctx, char **out, size_t max_out, regex_t *preg, fr_regex_flags_t const *flags,
143 char const *subject, size_t subject_len,
144 char const *replacement, size_t replacement_len,
145 fr_regmatch_t *regmatch);
146#endif
147uint32_t regex_subcapture_count(regex_t const *preg);
148fr_regmatch_t *regex_match_data_alloc(TALLOC_CTX *ctx, uint32_t count);
149
150int fr_regex_cmp_op(fr_token_t op, fr_value_box_t const *a, fr_value_box_t const *b) CC_HINT(nonnull);
151
152# ifdef __cplusplus
153}
154# endif
155#endif
#define RCSIDH(h, id)
Definition build.h:486
static fr_slen_t err
Definition dict.h:848
static fr_slen_t in
Definition dict.h:848
unsigned int uint32_t
long int ssize_t
unsigned char uint8_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:1025
int nonnull(2, 5))
static size_t char ** out
Definition value.h:1023