2 * $Id: ftype-string.c,v 1.17 2003/12/09 23:02:39 obiot Exp $
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>
32 #define CMP_MATCHES cmp_matches
34 #define CMP_MATCHES NULL
38 string_fvalue_new(fvalue_t *fv)
40 fv->value.string = NULL;
44 string_fvalue_free(fvalue_t *fv)
46 if (fv->value.string) {
47 g_free(fv->value.string);
52 string_fvalue_set(fvalue_t *fv, gpointer value, gboolean already_copied)
54 g_assert(value != NULL);
56 /* Free up the old value, if we have one */
57 string_fvalue_free(fv);
60 fv->value.string = value;
63 fv->value.string = g_strdup(value);
68 string_repr_len(fvalue_t *fv, ftrepr_t rtype)
75 return strlen(fv->value.string);
78 for (p = fv->value.string; (c = *p) != '\0'; p++) {
79 if (c == '\\' || c == '"') {
80 /* Backslashes and double-quotes
86 return repr_len + 2; /* string plus leading and trailing quotes */
88 g_assert_not_reached();
93 string_to_repr(fvalue_t *fv, ftrepr_t rtype, char *buf)
98 if (rtype == FTREPR_DFILTER) {
101 for (p = fv->value.string; (c = *p) != '\0'; p++) {
102 if (c == '\\' || c == '"') {
103 /* Backslashes and double-quotes
113 strcpy(buf, fv->value.string);
119 value_get(fvalue_t *fv)
121 return fv->value.string;
125 val_from_string(fvalue_t *fv, char *s, LogFunc logfunc _U_)
127 /* Free up the old value, if we have one */
128 string_fvalue_free(fv);
130 fv->value.string = g_strdup(s);
135 val_from_unparsed(fvalue_t *fv, char *s, gboolean allow_partial_value _U_, LogFunc logfunc)
139 /* Free up the old value, if we have one */
140 string_fvalue_free(fv);
142 /* Does this look like a byte-string? */
143 fv_bytes = fvalue_from_unparsed(FT_BYTES, s, TRUE, NULL);
145 /* Copy the bytes over to a string and terminate it
146 * with a NUL. XXX - what if the user embeds a NUL
147 * in the middle of the byte string? */
148 int num_bytes = fv_bytes->value.bytes->len;
150 fv->value.string = g_malloc(num_bytes + 1);
151 memcpy(fv->value.string, fv->value.bytes->data, num_bytes);
152 fv->value.string[num_bytes] = '\0';
154 FVALUE_FREE(fv_bytes);
158 /* Just turn it into a string */
159 return val_from_string(fv, s, logfunc);
161 g_assert_not_reached();
167 return strlen(fv->value.string);
171 slice(fvalue_t *fv, GByteArray *bytes, guint offset, guint length)
175 data = fv->value.string + offset;
177 g_byte_array_append(bytes, data, length);
182 cmp_eq(fvalue_t *a, fvalue_t *b)
184 return (strcmp(a->value.string, b->value.string) == 0);
188 cmp_ne(fvalue_t *a, fvalue_t *b)
190 return (strcmp(a->value.string, b->value.string) != 0);
194 cmp_gt(fvalue_t *a, fvalue_t *b)
196 return (strcmp(a->value.string, b->value.string) > 0);
200 cmp_ge(fvalue_t *a, fvalue_t *b)
202 return (strcmp(a->value.string, b->value.string) >= 0);
206 cmp_lt(fvalue_t *a, fvalue_t *b)
208 return (strcmp(a->value.string, b->value.string) < 0);
212 cmp_le(fvalue_t *a, fvalue_t *b)
214 return (strcmp(a->value.string, b->value.string) <= 0);
218 cmp_contains(fvalue_t *fv_a, fvalue_t *fv_b)
221 * http://www.introl.com/introl-demo/Libraries/C/ANSI_C/string/strstr.html
222 * strstr() returns a non-NULL value if needle is an empty
223 * string. We don't that behavior for cmp_contains. */
224 if (strlen(fv_b->value.string) == 0) {
228 if (strstr(fv_a->value.string, fv_b->value.string)) {
238 cmp_matches(fvalue_t *fv_a, fvalue_t *fv_b)
243 /* fv_b is always a FT_PCRE, otherwise the dfilter semcheck() would have
244 * warned us. For the same reason (and because we're using g_malloc()),
245 * fv_b->value.re is not NULL.
247 if (strcmp(fv_b->ftype->name, "FT_PCRE") != 0) {
250 if (! fv_b->value.re) {
254 (fv_b->value.re)->re, /* Compiled PCRE */
255 (fv_b->value.re)->ex, /* PCRE extra from pcre_study() */
256 fv_a->value.string, /* The data to check for the pattern... */
257 (int)strlen(fv_a->value.string), /* ... and its length */
258 0, /* Start offset within data */
259 options, /* PCRE options */
260 NULL, /* We are not interested in the matched string */
261 0 /* of the pattern; only in success or failure. */
271 ftype_register_string(void)
274 static ftype_t string_type = {
275 "FT_STRING", /* name */
276 "character string", /* pretty_name */
278 string_fvalue_new, /* new_value */
279 string_fvalue_free, /* free_value */
280 val_from_unparsed, /* val_from_unparsed */
281 val_from_string, /* val_from_string */
282 string_to_repr, /* val_to_string_repr */
283 string_repr_len, /* len_string_repr */
285 string_fvalue_set, /* set_value */
286 NULL, /* set_value_integer */
287 NULL, /* set_value_floating */
289 value_get, /* get_value */
290 NULL, /* get_value_integer */
291 NULL, /* get_value_floating */
305 static ftype_t stringz_type = {
311 val_from_unparsed, /* val_from_unparsed */
312 val_from_string, /* val_from_string */
313 NULL, /* val_to_string_repr */
314 NULL, /* len_string_repr */
330 cmp_contains, /* cmp_contains */
336 static ftype_t uint_string_type = {
342 val_from_unparsed, /* val_from_unparsed */
343 val_from_string, /* val_from_string */
344 NULL, /* val_to_string_repr */
345 NULL, /* len_string_repr */
361 cmp_contains, /* cmp_contains */
368 ftype_register(FT_STRING, &string_type);
369 ftype_register(FT_STRINGZ, &stringz_type);
370 ftype_register(FT_UINT_STRING, &uint_string_type);