Fix 'warning: cast discards qualifiers from pointer target type' by removing cast
[metze/wireshark/wip.git] / epan / ftypes / ftype-pcre.c
1 /*
2  * $Id: ftype-pcre.c,v 1.2 2004/01/25 17:33:20 jmayer Exp $
3  *
4  * Ethereal - Network traffic analyzer
5  * By Gerald Combs <gerald@ethereal.com>
6  * Copyright 2001 Gerald Combs
7  *
8  *
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.
13  *
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.
18  *
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.
22  */
23
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.
27  *
28  * PCRE is provided with libpcre (http://www.pcre.org/).
29  */
30
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34
35 #include <ftypes-int.h>
36
37 #ifdef HAVE_LIBPCRE
38 #include <pcre.h>
39
40 /* Create a pcre_tuple_t object based on the given string pattern */ 
41 static pcre_tuple_t *
42 pcre_tuple_new(const char *value)
43 {
44         pcre_tuple_t *tuple;
45         const char *pcre_error_text;
46         int pcre_error_offset;
47
48         tuple = g_malloc(sizeof(pcre_tuple_t));
49         tuple->string = g_strdup(value); /* The RE as string */
50         /* Compile the RE */
51         tuple->re = pcre_compile(
52                         value,                          /* pattern */
53                         0,                                      /* PCRE options */
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) */
57                         );
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);
62                 return tuple;
63         } else {
64                 tuple->error = NULL;
65         }
66         /* Study the RE */
67         tuple->ex = pcre_study(tuple->re, 0, &pcre_error_text);
68         if (pcre_error_text) {
69                 if (tuple->error) {
70                         tuple->error = g_strdup_printf("In regular expression \"%s\":\n"
71                                         "%s. %s",
72                                         value, tuple->error, pcre_error_text);
73                 } else {
74                         tuple->error = g_strdup_printf("In regular expression \"%s\":\n"
75                                         "%s",
76                                         value, pcre_error_text);
77                 }
78         }
79         return tuple;
80 }
81
82 static void
83 pcre_tuple_free(pcre_tuple_t *tuple)
84 {
85         if (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);
90                 g_free(tuple);
91         }
92 }
93
94 static void
95 pcre_fvalue_new(fvalue_t *fv)
96 {
97         fv->value.re = NULL;
98 }
99
100 static void
101 pcre_fvalue_free(fvalue_t *fv)
102 {
103         if (fv->value.re) {
104                 pcre_tuple_free(fv->value.re);
105         }
106 }
107
108 /* Generate a FT_PCRE from a parsed string pattern.
109  * Uses the specified logfunc() to report errors. */
110 static gboolean
111 val_from_string(fvalue_t *fv, char *pattern, LogFunc logfunc)
112 {
113         /* Free up the old value, if we have one */
114         pcre_fvalue_free(fv);
115
116         fv->value.re = pcre_tuple_new(pattern);
117         if (fv->value.re->error) {
118                 logfunc(fv->value.re->error);
119                 return FALSE;
120         }
121         return TRUE;
122 }
123
124 /* Generate a FT_PCRE from an unparsed string pattern.
125  * Uses the specified logfunc() to report errors. */
126 static gboolean
127 val_from_unparsed(fvalue_t *fv, char *pattern, gboolean allow_partial_value _U_, LogFunc logfunc)
128 {
129         /* Free up the old value, if we have one */
130         pcre_fvalue_free(fv);
131         g_assert(! allow_partial_value);
132
133         fv->value.re = pcre_tuple_new(pattern);
134         if (fv->value.re->error) {
135                 logfunc(fv->value.re->error);
136                 return FALSE;
137         }
138         return TRUE;
139 }
140
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. */
143 static void
144 pcre_fvalue_set(fvalue_t *fv, gpointer value, gboolean already_copied)
145 {
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);
151 }
152
153 static gpointer
154 pcre_fvalue_get(fvalue_t *fv)
155 {
156         return fv->value.re;
157 }
158
159 void
160 ftype_register_pcre(void)
161 {
162         static ftype_t pcre_type = {
163                 "FT_PCRE",
164                 "Compiled Perl-Compatible Regular Expression object",
165                 0,                                      /* wire_size */
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 */
172
173                 pcre_fvalue_set,        /* set_value */
174                 NULL,                           /* set_value_integer */
175                 NULL,                           /* set_value_floating */
176
177                 pcre_fvalue_get,        /* get_value */
178                 NULL,                           /* get_value_integer */
179                 NULL,                           /* get_value_floating */
180
181                 NULL,                           /* cmp_eq */
182                 NULL,                           /* cmp_ne */
183                 NULL,                           /* cmp_gt */
184                 NULL,                           /* cmp_ge */
185                 NULL,                           /* cmp_lt */
186                 NULL,                           /* cmp_le */
187                 NULL,                           /* cmp_contains */
188                 NULL,                           /* cmp_matches */
189
190                 NULL,                           /* len */
191                 NULL,                           /* slice */
192         };
193         ftype_register(FT_PCRE, &pcre_type);
194 }
195
196 #else /* HAVE_LIBPCRE */
197
198 void
199 ftype_register_pcre(void)
200 {
201         static ftype_t pcre_type = {
202                 "FT_PCRE",
203                 "Compiled Perl-Compatible Regular Expression object",
204                 0,                                      /* wire_size */
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 */
211
212                 NULL,                           /* set_value */
213                 NULL,                           /* set_value_integer */
214                 NULL,                           /* set_value_floating */
215
216                 NULL,                           /* get_value */
217                 NULL,                           /* get_value_integer */
218                 NULL,                           /* get_value_floating */ 
219
220                 NULL,                           /* cmp_eq */
221                 NULL,                           /* cmp_ne */
222                 NULL,                           /* cmp_gt */
223                 NULL,                           /* cmp_ge */
224                 NULL,                           /* cmp_lt */
225                 NULL,                           /* cmp_le */
226                 NULL,                           /* cmp_contains */
227                 NULL,                           /* cmp_matches */
228
229                 NULL,                           /* len */
230                 NULL,                           /* slice */
231         };
232         ftype_register(FT_PCRE, &pcre_type);
233 }
234
235 #endif /* HAVE_LIBPCRE */