4 * Ethereal - Network traffic analyzer
5 * By Gerald Combs <gerald@ethereal.com>
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.
27 #include <ftypes-int.h>
29 #include <epan/gdebug.h>
33 #define CMP_MATCHES cmp_matches
35 #define CMP_MATCHES NULL
38 #define tvb_is_private fvalue_gboolean1
41 value_new(fvalue_t *fv)
44 fv->tvb_is_private = FALSE;
48 value_free(fvalue_t *fv)
50 if (fv->value.tvb && fv->tvb_is_private) {
51 tvb_free_chain(fv->value.tvb);
56 value_set(fvalue_t *fv, gpointer value, gboolean already_copied)
58 g_assert(already_copied);
60 /* Free up the old value, if we have one */
63 fv->value.tvb = value;
67 free_tvb_data(void *data)
73 val_from_string(fvalue_t *fv, char *s, LogFunc logfunc _U_)
78 /* Free up the old value, if we have one */
81 /* Make a tvbuff from the string. We can drop the
83 private_data = g_memdup(s, strlen(s));
84 new_tvb = tvb_new_real_data(private_data,
85 strlen(s), strlen(s));
87 /* Let the tvbuff know how to delete the data. */
88 tvb_set_free_cb(new_tvb, free_tvb_data);
90 /* And let us know that we need to free the tvbuff */
91 fv->tvb_is_private = TRUE;
92 fv->value.tvb = new_tvb;
97 val_from_unparsed(fvalue_t *fv, char *s, gboolean allow_partial_value _U_, LogFunc logfunc)
101 guint8 *private_data;
103 /* Free up the old value, if we have one */
106 /* Does this look like a byte string? */
107 fv_bytes = fvalue_from_unparsed(FT_BYTES, s, TRUE, NULL);
109 /* Make a tvbuff from the bytes */
110 private_data = g_memdup(fv_bytes->value.bytes->data,
111 fv_bytes->value.bytes->len);
112 new_tvb = tvb_new_real_data(private_data,
113 fv_bytes->value.bytes->len,
114 fv_bytes->value.bytes->len);
116 /* Let the tvbuff know how to delete the data. */
117 tvb_set_free_cb(new_tvb, free_tvb_data);
119 /* And let us know that we need to free the tvbuff */
120 fv->tvb_is_private = TRUE;
121 fv->value.tvb = new_tvb;
125 /* Treat it as a string. */
126 return val_from_string(fv, s, logfunc);
128 g_assert_not_reached();
132 value_get(fvalue_t *fv)
134 return fv->value.tvb;
141 return tvb_length(fv->value.tvb);
147 slice(fvalue_t *fv, GByteArray *bytes, guint offset, guint length)
153 data = tvb_get_ptr(fv->value.tvb, offset, length);
154 g_byte_array_append(bytes, data, length);
165 cmp_contains(fvalue_t *fv_a, fvalue_t *fv_b)
167 if (tvb_find_tvb(fv_a->value.tvb, fv_b->value.tvb, 0) > -1) {
177 cmp_matches(fvalue_t *fv_a, fvalue_t *fv_b)
179 tvbuff_t *tvb = fv_a->value.tvb;
180 pcre_tuple_t *pcre = fv_b->value.re;
183 const char *data = NULL; /* tvb data */
184 guint32 tvb_len; /* tvb length */
186 /* fv_b is always a FT_PCRE, otherwise the dfilter semcheck() would have
187 * warned us. For the same reason (and because we're using g_malloc()),
188 * fv_b->value.re is not NULL.
190 if (strcmp(fv_b->ftype->name, "FT_PCRE") != 0) {
197 tvb_len = tvb_length(tvb);
198 data = (const char *)tvb_get_ptr(tvb, 0, tvb_len);
200 pcre->re, /* Compiled PCRE */
201 pcre->ex, /* PCRE extra from pcre_study() */
202 data, /* The data to check for the pattern... */
203 tvb_len, /* ... and its length */
204 0, /* Start offset within data */
205 options, /* PCRE options */
206 NULL, /* We are not interested in the matched string */
207 0 /* of the pattern; only in success or failure. */
209 /* NOTE - DO NOT g_free(data) */
223 ftype_register_tvbuff(void)
226 static ftype_t protocol_type = {
227 "FT_PROTOCOL", /* name */
228 "protocol", /* pretty_name */
230 value_new, /* new_value */
231 value_free, /* free_value */
232 val_from_unparsed, /* val_from_unparsed */
233 val_from_string, /* val_from_string */
234 NULL, /* val_to_string_repr */
235 NULL, /* len_string_repr */
246 /* TODO - tvb's *can* do 'eq', etc. */
254 NULL, /* cmp_bitwise_and */
264 ftype_register(FT_PROTOCOL, &protocol_type);