ze-filter  (ze-filter-0.8.0-develop-180218)
zeRegex.c
Go to the documentation of this file.
1 /*
2  *
3  * ze-filter - Mail Server Filter for sendmail
4  *
5  * Copyright (c) 2001-2018 - Jose-Marcio Martins da Cruz
6  *
7  * Auteur : Jose Marcio Martins da Cruz
8  * jose.marcio.mc@gmail.org
9  *
10  * Historique :
11  * Creation : janvier 2002
12  *
13  * This program is free software, but with restricted license :
14  *
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19  *
20  * More details about ze-filter license can be found at ze-filter
21  * web site : http://foss.jose-marcio.org
22  */
23 
24 #include <ze-sys.h>
25 #include <libze.h>
26 
27 #include <zeRegex.h>
28 /* ****************************************************************************
29  * *
30  * *
31  **************************************************************************** */
32 
33 #if USE_PCRE
34 #define J_PCRE_FLAGS (PCRE_CASELESS | PCRE_DOTALL)
35 #else
36 #define J_PCRE_FLAGS 0
37 #endif
38 
39 #define JREGCOMP_FLAGS (REG_ICASE | REG_EXTENDED)
40 #define JREGEXEC_FLAGS 0
41 
42 #if USE_PCRE
43 static bool use_pcre = TRUE;
44 #else
45 static bool use_pcre = FALSE;
46 #endif /* USE_PCRE */
47 
48 
49 /* ****************************************************************************
50  * *
51  * *
52  **************************************************************************** */
53 bool
54 zeRegexComp(re, expr, flags)
55  zeRegex_T *re;
56  char *expr;
57  int flags;
58 {
59  if ((re == NULL) || (expr == NULL))
60  return FALSE;
61 
62 #if USE_PCRE
63  use_pcre = TRUE;
64 #else
65  use_pcre = FALSE;
66 #endif /* USE_PCRE */
67 
68  ZE_MessageInfo(20, "Using PCRE : %s", use_pcre ? "YES" : "NO");
69 
70  if (re->signature == SIGNATURE)
71  zeRegexFree(re);
72 
73 #if USE_PCRE
74  if (use_pcre)
75  {
76 
77  if (re->signature != SIGNATURE)
78  {
79  const char *errptr = NULL;
80  int erroffset = 0;
81 
82  re->pcre_rebase = pcre_compile(expr, J_PCRE_FLAGS, &errptr, &erroffset, NULL);
83  if (re->pcre_rebase == NULL)
84  ZE_LogMsgError(0, "pcre_compile error : %s", errptr);
85  if (re->pcre_rebase != NULL)
86  {
87  re->pcre_rextra = pcre_study(re->pcre_rebase, 0, &errptr);
88  if (re->pcre_rextra == NULL)
89  ZE_LogMsgInfo(12, "pcre_study error : %s", errptr);
90  }
91  re->re_ok = (re->pcre_rebase != NULL);
92  }
93  } else
94 #endif
95  {
96  re->result = regcomp(&re->re, expr, flags);
97  }
98 
99  if (re->result == 0)
100  {
101  /*
102  ** See this latter if shall save expression
103  */
104 #if 1
105  re->expr = strdup(expr);
106 #endif
107  re->signature = SIGNATURE;
108  }
109  return (re->result == 0);
110 }
111 
112 /* ****************************************************************************
113  * *
114  * *
115  **************************************************************************** */
116 #define DIM_VECTOR (3 * 32)
117 
118 bool
119 zeRegexExec(re, s, pi, pf, flags)
120  zeRegex_T *re;
121  char *s;
122  long *pi;
123  long *pf;
124  int flags;
125 {
126  regmatch_t pm;
127 
128  if ((re == NULL) || (re->signature != SIGNATURE) || (s == NULL))
129  return FALSE;
130 
131  re->result = -1;
132 #if USE_PCRE
133  if (use_pcre)
134  {
135  int ovector[DIM_VECTOR];
136  int rc;
137 
138  rc = pcre_exec(re->pcre_rebase, re->pcre_rextra, s, strlen(s),
139  0, 0, ovector, DIM_VECTOR);
140  if (rc >= 0)
141  {
142  re->result = 0;
143  if (pi != NULL)
144  *pi = ovector[0];
145  if (pf != NULL)
146  *pf = ovector[1];
147  }
148  } else
149 #endif
150  {
151  re->result = regexec(&re->re, s, 1, &pm, flags);
152  if (re->result == 0)
153  {
154  if (pi != NULL)
155  *pi = pm.rm_so;
156  if (pf != NULL)
157  *pf = pm.rm_eo;
158  }
159  }
160 
161  return (re->result == 0);
162 }
163 
164 /* ****************************************************************************
165  * *
166  * *
167  **************************************************************************** */
168 void
170  zeRegex_T *re;
171 {
172  if ((re == NULL) || (re->signature != SIGNATURE))
173  return;
174 
175 #if USE_PCRE
176  if (use_pcre)
177  {
178  if (re->pcre_rebase != NULL)
179  pcre_free(re->pcre_rebase);
180  if (re->pcre_rextra != NULL)
181  pcre_free(re->pcre_rextra);
182  re->pcre_rebase = NULL;
183  re->pcre_rextra = NULL;
184  } else
185 #endif
186  {
187  regfree(&re->re);
188  }
189 
190  FREE(re->expr);
191  re->signature = 0;
192  memset(re, 0, sizeof (zeRegex_T));
193 }
194 
195 /* ****************************************************************************
196  * *
197  * *
198  **************************************************************************** */
199 int
200 zeRegexCount(s, expr)
201  char *s;
202  char *expr;
203 {
204  int nb = 0;
205  long pf = 0;
206  char *p;
207  zeRegex_T re;
208 
209  if ((s == NULL) || (expr == NULL))
210  return FALSE;
211 
212  memset(&re, 0, sizeof (re));
213 
214  if (zeRegexComp(&re, expr, JREGCOMP_FLAGS))
215  {
216  for (p = s; zeRegexExec(&re, p, NULL, &pf, 0); p += pf)
217  nb++;
218  zeRegexFree(&re);
219  }
220 
221  return nb;
222 }
223 
224 /* ****************************************************************************
225  * *
226  * *
227  **************************************************************************** */
228 bool
229 zeRegexLookup(s, expr, pi, pf)
230  char *s;
231  char *expr;
232  long *pi;
233  long *pf;
234 {
235  zeRegex_T re;
236  bool found = FALSE;
237  long xi, xf;
238 
239  if ((s == NULL) || (expr == NULL))
240  return FALSE;
241 
242  xi = xf = 0;
243  memset(&re, 0, sizeof (re));
244 
245  if (zeRegexComp(&re, expr, JREGCOMP_FLAGS))
246  {
247  found = zeRegexExec(&re, s, &xi, &xf, JREGEXEC_FLAGS);
248  zeRegexFree(&re);
249  if (found)
250  {
251  if (pi != NULL)
252  *pi = xi;
253  if (pf != NULL)
254  *pf = xf;
255  }
256  }
257  return found;
258 }
259 
260 /* ****************************************************************************
261  * *
262  * *
263  **************************************************************************** */
264 char *
266  zeRegex_T *re;
267 {
268 
269  return NULL;
270 }
bool re_ok
Definition: zeRegex.h:45
regex_t re
Definition: zeRegex.h:37
#define DIM_VECTOR
Definition: zeRegex.c:116
int zeRegexCount(char *s, char *expr)
Definition: zeRegex.c:200
int result
Definition: zeRegex.h:44
#define FREE(x)
Definition: macros.h:37
pcre_extra * pcre_rextra
Definition: zeRegex.h:41
bool zeRegexExec(zeRegex_T *re, char *s, long *pi, long *pf, int flags)
Definition: zeRegex.c:119
char * zeRegexError(zeRegex_T *re)
Definition: zeRegex.c:265
#define ZE_LogMsgInfo(level,...)
Definition: zeSyslog.h:110
#define FALSE
Definition: macros.h:160
#define ZE_LogMsgError(level,...)
Definition: zeSyslog.h:113
#define JREGEXEC_FLAGS
Definition: zeRegex.c:40
bool zeRegexLookup(char *s, char *expr, long *pi, long *pf)
Definition: zeRegex.c:229
void zeRegexFree(zeRegex_T *re)
Definition: zeRegex.c:169
pcre * pcre_rebase
Definition: zeRegex.h:40
#define ZE_MessageInfo(level,...)
Definition: zeSyslog.h:90
int nb
Definition: ze-connopen.c:61
#define TRUE
Definition: macros.h:157
#define JREGCOMP_FLAGS
Definition: zeRegex.c:39
bool zeRegexComp(zeRegex_T *re, char *expr, int flags)
Definition: zeRegex.c:54
uint32_t signature
Definition: zeRegex.h:35
#define J_PCRE_FLAGS
Definition: zeRegex.c:34
#define SIGNATURE
Definition: ze-libjc.h:75
char * expr
Definition: zeRegex.h:36