4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 2001 Gerald Combs
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 /* Perl-Compatible Regular Expression (PCRE) internal field type.
24 * Used with the "matches" dfilter operator, allowing efficient
25 * compilation and studying of a PCRE pattern in dfilters.
27 * PCRE is provided with libpcre (http://www.pcre.org/).
34 #include <ftypes-int.h>
42 /* Create a pcre_tuple_t object based on the given string pattern */
44 pcre_tuple_new(const char *value)
47 const char *pcre_error_text;
48 int pcre_error_offset;
50 tuple = g_malloc(sizeof(pcre_tuple_t));
51 tuple->string = g_strdup(value); /* The RE as string */
54 tuple->re = pcre_compile(
57 &pcre_error_text, /* PCRE constant error string */
58 &pcre_error_offset, /* Start offset of error in pattern */
59 NULL /* Default char tables (C locale) */
61 if (pcre_error_text) {
62 tuple->error = g_strdup_printf("In regular expression \"%s\":\n"
63 "%s (character position %d)",
64 value, pcre_error_text, pcre_error_offset);
70 tuple->ex = pcre_study(tuple->re, 0, &pcre_error_text);
71 if (pcre_error_text) {
73 tuple->error = g_strdup_printf("In regular expression \"%s\":\n"
75 value, tuple->error, pcre_error_text);
77 tuple->error = g_strdup_printf("In regular expression \"%s\":\n"
79 value, pcre_error_text);
86 pcre_tuple_free(pcre_tuple_t *tuple)
89 g_free(tuple->string);
98 pcre_fvalue_new(fvalue_t *fv)
104 pcre_fvalue_free(fvalue_t *fv)
107 pcre_tuple_free(fv->value.re);
111 /* Generate a FT_PCRE from a parsed string pattern.
112 * Uses the specified logfunc() to report errors. */
114 val_from_string(fvalue_t *fv, char *pattern, LogFunc logfunc)
116 /* Free up the old value, if we have one */
117 pcre_fvalue_free(fv);
119 fv->value.re = pcre_tuple_new(pattern);
120 if (fv->value.re->error) {
121 logfunc(fv->value.re->error);
127 /* Generate a FT_PCRE from an unparsed string pattern.
128 * Uses the specified logfunc() to report errors. */
130 val_from_unparsed(fvalue_t *fv, char *pattern, gboolean allow_partial_value _U_, LogFunc logfunc)
132 /* Free up the old value, if we have one */
133 pcre_fvalue_free(fv);
134 g_assert(! allow_partial_value);
136 fv->value.re = pcre_tuple_new(pattern);
137 if (fv->value.re->error) {
138 logfunc(fv->value.re->error);
145 pcre_repr_len(fvalue_t *fv, ftrepr_t rtype)
147 g_assert(rtype == FTREPR_DFILTER);
148 return (int)strlen(fv->value.re->string);
152 pcre_to_repr(fvalue_t *fv, ftrepr_t rtype, char *buf)
154 g_assert(rtype == FTREPR_DFILTER);
155 strcpy(buf, fv->value.re->string);
158 /* BEHOLD - value contains the string representation of the regular expression,
159 * and we want to store the compiled PCRE RE object into the value. */
161 pcre_fvalue_set(fvalue_t *fv, gpointer value, gboolean already_copied)
163 g_assert(value != NULL);
164 /* Free up the old value, if we have one */
165 pcre_fvalue_free(fv);
166 g_assert(! already_copied);
167 fv->value.re = pcre_tuple_new(value);
171 pcre_fvalue_get(fvalue_t *fv)
177 ftype_register_pcre(void)
179 static ftype_t pcre_type = {
181 "FT_PCRE", /* name */
182 "Compiled Perl-Compatible Regular Expression object", /* pretty_name */
184 pcre_fvalue_new, /* new_value */
185 pcre_fvalue_free, /* free_value */
186 val_from_unparsed, /* val_from_unparsed */
187 val_from_string, /* val_from_string */
188 pcre_to_repr, /* val_to_string_repr */
189 pcre_repr_len, /* len_string_repr */
191 pcre_fvalue_set, /* set_value */
192 NULL, /* set_value_uinteger */
193 NULL, /* set_value_sinteger */
194 NULL, /* set_value_integer64 */
195 NULL, /* set_value_floating */
197 pcre_fvalue_get, /* get_value */
198 NULL, /* get_value_uinteger */
199 NULL, /* get_value_sinteger */
200 NULL, /* get_value_integer64 */
201 NULL, /* get_value_floating */
209 NULL, /* cmp_bitwise_and */
210 NULL, /* cmp_contains */
211 NULL, /* cmp_matches */
216 ftype_register(FT_PCRE, &pcre_type);
219 #else /* No HAVE_LIBPCRE. Try falling back to GRegex. */
226 gregex_fvalue_new(fvalue_t *fv)
232 gregex_fvalue_free(fvalue_t *fv)
235 g_regex_unref(fv->value.re);
240 /* Generate a FT_PCRE from a parsed string pattern.
241 * Uses the specified logfunc() to report errors. */
243 val_from_string(fvalue_t *fv, char *pattern, LogFunc logfunc)
245 GError *regex_error = NULL;
246 /* Free up the old value, if we have one */
247 gregex_fvalue_free(fv);
249 fv->value.re = g_regex_new(
250 pattern, /* pattern */
251 G_REGEX_OPTIMIZE, /* Compile options (G_REGEX_OPTIMIZE = pcre_study) */
252 0, /* Match options */
253 ®ex_error /* Compile / study errors */
258 logfunc(regex_error->message);
260 g_error_free(regex_error);
262 g_regex_unref(fv->value.re);
269 /* Generate a FT_PCRE from an unparsed string pattern.
270 * Uses the specified logfunc() to report errors. */
272 val_from_unparsed(fvalue_t *fv, char *pattern, gboolean allow_partial_value _U_, LogFunc logfunc)
274 g_assert(! allow_partial_value);
276 return val_from_string(fv, pattern, logfunc);
280 gregex_repr_len(fvalue_t *fv, ftrepr_t rtype)
282 g_assert(rtype == FTREPR_DFILTER);
283 return (int)strlen(g_regex_get_pattern(fv->value.re));
287 gregex_to_repr(fvalue_t *fv, ftrepr_t rtype, char *buf)
289 g_assert(rtype == FTREPR_DFILTER);
290 strcpy(buf, g_regex_get_pattern(fv->value.re));
293 /* BEHOLD - value contains the string representation of the regular expression,
294 * and we want to store the compiled PCRE RE object into the value. */
296 gregex_fvalue_set(fvalue_t *fv, gpointer value, gboolean already_copied)
298 g_assert(value != NULL);
299 /* Free up the old value, if we have one */
300 gregex_fvalue_free(fv);
301 g_assert(! already_copied);
302 val_from_unparsed(fv, value, FALSE, NULL);
306 gregex_fvalue_get(fvalue_t *fv)
312 ftype_register_pcre(void)
314 static ftype_t pcre_type = {
316 "FT_PCRE", /* name */
317 "Compiled Perl-Compatible Regular Expression (GRegex) object", /* pretty_name */
319 gregex_fvalue_new, /* new_value */
320 gregex_fvalue_free, /* free_value */
321 val_from_unparsed, /* val_from_unparsed */
322 val_from_string, /* val_from_string */
323 gregex_to_repr, /* val_to_string_repr */
324 gregex_repr_len, /* len_string_repr */
326 gregex_fvalue_set, /* set_value */
327 NULL, /* set_value_uinteger */
328 NULL, /* set_value_sinteger */
329 NULL, /* set_value_integer64 */
330 NULL, /* set_value_floating */
332 gregex_fvalue_get, /* get_value */
333 NULL, /* get_value_uinteger */
334 NULL, /* get_value_sinteger */
335 NULL, /* get_value_integer64 */
336 NULL, /* get_value_floating */
344 NULL, /* cmp_bitwise_and */
345 NULL, /* cmp_contains */
346 NULL, /* cmp_matches */
351 ftype_register(FT_PCRE, &pcre_type);
354 #endif /* HAVE_LIBPCRE */
357 * Editor modelines - http://www.wireshark.org/tools/modelines.html
362 * indent-tabs-mode: t
365 * vi: set shiftwidth=4 tabstop=4 noexpandtab
366 * :indentSize=4:tabSize=4:noTabs=false: