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 if (tuple->string) g_free(tuple->string);
90 if (tuple->re) g_free(tuple->re);
91 if (tuple->ex) g_free(tuple->ex);
92 if (tuple->error) g_free(tuple->error);
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 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_integer */
193 NULL, /* set_value_integer64 */
194 NULL, /* set_value_floating */
196 pcre_fvalue_get, /* get_value */
197 NULL, /* get_value_integer */
198 NULL, /* get_value_integer64 */
199 NULL, /* get_value_floating */
207 NULL, /* cmp_bitwise_and */
208 NULL, /* cmp_contains */
209 NULL, /* cmp_matches */
214 ftype_register(FT_PCRE, &pcre_type);
217 #else /* HAVE_LIBPCRE */
220 ftype_register_pcre(void)
222 static ftype_t pcre_type = {
224 "FT_PCRE", /* name */
225 "Compiled Perl-Compatible Regular Expression object", /* pretty_name */
227 NULL, /* new_value */
228 NULL, /* free_value */
229 NULL, /* val_from_unparsed */
230 NULL, /* val_from_string */
231 NULL, /* val_to_string_repr */
232 NULL, /* len_string_repr */
234 NULL, /* set_value */
235 NULL, /* set_value_integer */
236 NULL, /* set_value_integer64 */
237 NULL, /* set_value_floating */
239 NULL, /* get_value */
240 NULL, /* get_value_integer */
241 NULL, /* get_value_integer64 */
242 NULL, /* get_value_floating */
250 NULL, /* cmp_bitwise_and */
251 NULL, /* cmp_contains */
252 NULL, /* cmp_matches */
257 ftype_register(FT_PCRE, &pcre_type);
260 #endif /* HAVE_LIBPCRE */