2 * $Id: ftype-pcre.c,v 1.2 2004/01/25 17:33:20 jmayer Exp $
4 * Ethereal - Network traffic analyzer
5 * By Gerald Combs <gerald@ethereal.com>
6 * Copyright 2001 Gerald Combs
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 /* Perl-Compatible Regular Expression (PCRE) internal field type.
25 * Used with the "matches" dfilter operator, allowing efficient
26 * compilation and studying of a PCRE pattern in dfilters.
28 * PCRE is provided with libpcre (http://www.pcre.org/).
35 #include <ftypes-int.h>
40 /* Create a pcre_tuple_t object based on the given string pattern */
42 pcre_tuple_new(const char *value)
45 const char *pcre_error_text;
46 int pcre_error_offset;
48 tuple = g_malloc(sizeof(pcre_tuple_t));
49 tuple->string = g_strdup(value); /* The RE as string */
51 tuple->re = pcre_compile(
54 &pcre_error_text, /* PCRE constant error string */
55 &pcre_error_offset, /* Start offset of error in pattern */
56 NULL /* Default char tables (C locale) */
58 if (pcre_error_text) {
59 tuple->error = g_strdup_printf("In regular expression \"%s\":\n"
60 "%s (character position %d)",
61 value, pcre_error_text, pcre_error_offset);
67 tuple->ex = pcre_study(tuple->re, 0, &pcre_error_text);
68 if (pcre_error_text) {
70 tuple->error = g_strdup_printf("In regular expression \"%s\":\n"
72 value, tuple->error, pcre_error_text);
74 tuple->error = g_strdup_printf("In regular expression \"%s\":\n"
76 value, pcre_error_text);
83 pcre_tuple_free(pcre_tuple_t *tuple)
86 if (tuple->string) g_free(tuple->string);
87 if (tuple->re) g_free(tuple->re);
88 if (tuple->ex) g_free(tuple->ex);
89 if (tuple->error) g_free(tuple->error);
95 pcre_fvalue_new(fvalue_t *fv)
101 pcre_fvalue_free(fvalue_t *fv)
104 pcre_tuple_free(fv->value.re);
108 /* Generate a FT_PCRE from a parsed string pattern.
109 * Uses the specified logfunc() to report errors. */
111 val_from_string(fvalue_t *fv, char *pattern, LogFunc logfunc)
113 /* Free up the old value, if we have one */
114 pcre_fvalue_free(fv);
116 fv->value.re = pcre_tuple_new(pattern);
117 if (fv->value.re->error) {
118 logfunc(fv->value.re->error);
124 /* Generate a FT_PCRE from an unparsed string pattern.
125 * Uses the specified logfunc() to report errors. */
127 val_from_unparsed(fvalue_t *fv, char *pattern, gboolean allow_partial_value _U_, LogFunc logfunc)
129 /* Free up the old value, if we have one */
130 pcre_fvalue_free(fv);
131 g_assert(! allow_partial_value);
133 fv->value.re = pcre_tuple_new(pattern);
134 if (fv->value.re->error) {
135 logfunc(fv->value.re->error);
141 /* BEHOLD - value contains the string representation of the regular expression,
142 * and we want to store the compiled PCRE RE object into the value. */
144 pcre_fvalue_set(fvalue_t *fv, gpointer value, gboolean already_copied)
146 g_assert(value != NULL);
147 /* Free up the old value, if we have one */
148 pcre_fvalue_free(fv);
149 g_assert(! already_copied);
150 fv->value.re = pcre_tuple_new(value);
154 pcre_fvalue_get(fvalue_t *fv)
160 ftype_register_pcre(void)
162 static ftype_t pcre_type = {
164 "Compiled Perl-Compatible Regular Expression object",
166 pcre_fvalue_new, /* new_value */
167 pcre_fvalue_free, /* free_value */
168 val_from_unparsed, /* val_from_unparsed */
169 val_from_string, /* val_from_string */
170 NULL, /* val_to_string_repr */
171 NULL, /* len_string_repr */
173 pcre_fvalue_set, /* set_value */
174 NULL, /* set_value_integer */
175 NULL, /* set_value_floating */
177 pcre_fvalue_get, /* get_value */
178 NULL, /* get_value_integer */
179 NULL, /* get_value_floating */
187 NULL, /* cmp_contains */
188 NULL, /* cmp_matches */
193 ftype_register(FT_PCRE, &pcre_type);
196 #else /* HAVE_LIBPCRE */
199 ftype_register_pcre(void)
201 static ftype_t pcre_type = {
203 "Compiled Perl-Compatible Regular Expression object",
205 NULL, /* new_value */
206 NULL, /* free_value */
207 NULL, /* val_from_unparsed */
208 NULL, /* val_from_string */
209 NULL, /* val_to_string_repr */
210 NULL, /* len_string_repr */
212 NULL, /* set_value */
213 NULL, /* set_value_integer */
214 NULL, /* set_value_floating */
216 NULL, /* get_value */
217 NULL, /* get_value_integer */
218 NULL, /* get_value_floating */
226 NULL, /* cmp_contains */
227 NULL, /* cmp_matches */
232 ftype_register(FT_PCRE, &pcre_type);
235 #endif /* HAVE_LIBPCRE */