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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include <ftypes-int.h>
27 #include <epan/emem.h>
30 #define CMP_MATCHES cmp_matches
36 string_fvalue_new(fvalue_t
*fv
)
38 fv
->value
.string
= NULL
;
42 string_fvalue_free(fvalue_t
*fv
)
44 g_free(fv
->value
.string
);
48 string_fvalue_set(fvalue_t
*fv
, gpointer value
, gboolean already_copied
)
50 DISSECTOR_ASSERT(value
!= NULL
);
52 /* Free up the old value, if we have one */
53 string_fvalue_free(fv
);
56 fv
->value
.string
= (gchar
*)value
; /* must be g_ allocated */
58 fv
->value
.string
= (gchar
*)g_strdup((const gchar
*)value
);
62 string_repr_len(fvalue_t
*fv
, ftrepr_t rtype
)
66 return (int)strlen(fv
->value
.string
);
69 return escape_string_len(fv
->value
.string
);
71 g_assert_not_reached();
76 string_to_repr(fvalue_t
*fv
, ftrepr_t rtype
, char *buf
)
80 strcpy(buf
, fv
->value
.string
);
84 escape_string(buf
, fv
->value
.string
);
87 g_assert_not_reached();
92 value_get(fvalue_t
*fv
)
94 return fv
->value
.string
;
98 val_from_string(fvalue_t
*fv
, char *s
, LogFunc logfunc _U_
)
100 /* Free up the old value, if we have one */
101 string_fvalue_free(fv
);
103 fv
->value
.string
= g_strdup(s
);
108 val_from_unparsed(fvalue_t
*fv
, char *s
, gboolean allow_partial_value _U_
, LogFunc logfunc
)
112 /* Free up the old value, if we have one */
113 string_fvalue_free(fv
);
115 /* Does this look like a byte-string? */
116 fv_bytes
= fvalue_from_unparsed(FT_BYTES
, s
, TRUE
, NULL
);
118 /* Copy the bytes over to a string and terminate it
119 * with a NUL. XXX - what if the user embeds a NUL
120 * in the middle of the byte string? */
121 int num_bytes
= fv_bytes
->value
.bytes
->len
;
123 fv
->value
.string
= (gchar
*)g_malloc(num_bytes
+ 1);
124 memcpy(fv
->value
.string
, fv_bytes
->value
.bytes
->data
, num_bytes
);
125 fv
->value
.string
[num_bytes
] = '\0';
127 FVALUE_FREE(fv_bytes
);
131 /* Just turn it into a string */
132 return val_from_string(fv
, s
, logfunc
);
138 return (guint
)strlen(fv
->value
.string
);
142 slice(fvalue_t
*fv
, GByteArray
*bytes
, guint offset
, guint length
)
146 data
= fv
->value
.ustring
+ offset
;
148 g_byte_array_append(bytes
, data
, length
);
153 cmp_eq(const fvalue_t
*a
, const fvalue_t
*b
)
155 return (strcmp(a
->value
.string
, b
->value
.string
) == 0);
159 cmp_ne(const fvalue_t
*a
, const fvalue_t
*b
)
161 return (strcmp(a
->value
.string
, b
->value
.string
) != 0);
165 cmp_gt(const fvalue_t
*a
, const fvalue_t
*b
)
167 return (strcmp(a
->value
.string
, b
->value
.string
) > 0);
171 cmp_ge(const fvalue_t
*a
, const fvalue_t
*b
)
173 return (strcmp(a
->value
.string
, b
->value
.string
) >= 0);
177 cmp_lt(const fvalue_t
*a
, const fvalue_t
*b
)
179 return (strcmp(a
->value
.string
, b
->value
.string
) < 0);
183 cmp_le(const fvalue_t
*a
, const fvalue_t
*b
)
185 return (strcmp(a
->value
.string
, b
->value
.string
) <= 0);
189 cmp_contains(const fvalue_t
*fv_a
, const fvalue_t
*fv_b
)
192 * http://www.introl.com/introl-demo/Libraries/C/ANSI_C/string/strstr.html
193 * strstr() returns a non-NULL value if needle is an empty
194 * string. We don't that behavior for cmp_contains. */
195 if (strlen(fv_b
->value
.string
) == 0) {
199 if (strstr(fv_a
->value
.string
, fv_b
->value
.string
)) {
208 cmp_matches(const fvalue_t
*fv_a
, const fvalue_t
*fv_b
)
210 char *str
= fv_a
->value
.string
;
211 GRegex
*regex
= fv_b
->value
.re
;
213 /* fv_b is always a FT_PCRE, otherwise the dfilter semcheck() would have
214 * warned us. For the same reason (and because we're using g_malloc()),
215 * fv_b->value.re is not NULL.
217 if (strcmp(fv_b
->ftype
->name
, "FT_PCRE") != 0) {
223 return g_regex_match_full(
224 regex
, /* Compiled PCRE */
225 str
, /* The data to check for the pattern... */
226 (int)strlen(str
), /* ... and its length */
227 0, /* Start offset within data */
228 (GRegexMatchFlags
)0, /* GRegexMatchFlags */
229 NULL
, /* We are not interested in the match information */
230 NULL
/* We don't want error information */
235 ftype_register_string(void)
238 static ftype_t string_type
= {
239 FT_STRING
, /* ftype */
240 "FT_STRING", /* name */
241 "Character string", /* pretty_name */
243 string_fvalue_new
, /* new_value */
244 string_fvalue_free
, /* free_value */
245 val_from_unparsed
, /* val_from_unparsed */
246 val_from_string
, /* val_from_string */
247 string_to_repr
, /* val_to_string_repr */
248 string_repr_len
, /* len_string_repr */
250 string_fvalue_set
, /* set_value */
251 NULL
, /* set_value_uinteger */
252 NULL
, /* set_value_sinteger */
253 NULL
, /* set_value_integer64 */
254 NULL
, /* set_value_floating */
256 value_get
, /* get_value */
257 NULL
, /* get_value_uinteger */
258 NULL
, /* get_value_sinteger */
259 NULL
, /* get_value_integer64 */
260 NULL
, /* get_value_floating */
268 NULL
, /* cmp_bitwise_and */
275 static ftype_t stringz_type
= {
276 FT_STRINGZ
, /* ftype */
277 "FT_STRINGZ", /* name */
278 "Character string", /* pretty name */
280 string_fvalue_new
, /* new_value */
281 string_fvalue_free
, /* free_value */
282 val_from_unparsed
, /* val_from_unparsed */
283 val_from_string
, /* val_from_string */
284 string_to_repr
, /* val_to_string_repr */
285 string_repr_len
, /* len_string_repr */
287 string_fvalue_set
, /* set_value */
288 NULL
, /* set_value_uinteger */
289 NULL
, /* set_value_sinteger */
290 NULL
, /* set_value_integer64 */
291 NULL
, /* set_value_floating */
293 value_get
, /* get_value */
294 NULL
, /* get_value_uinteger */
295 NULL
, /* get_value_sinteger */
296 NULL
, /* get_value_integer64 */
297 NULL
, /* get_value_floating */
305 NULL
, /* cmp_bitwise_and */
306 cmp_contains
, /* cmp_contains */
312 static ftype_t uint_string_type
= {
313 FT_UINT_STRING
, /* ftype */
314 "FT_UINT_STRING", /* name */
315 "Character string", /* pretty_name */
317 string_fvalue_new
, /* new_value */
318 string_fvalue_free
, /* free_value */
319 val_from_unparsed
, /* val_from_unparsed */
320 val_from_string
, /* val_from_string */
321 string_to_repr
, /* val_to_string_repr */
322 string_repr_len
, /* len_string_repr */
324 string_fvalue_set
, /* set_value */
325 NULL
, /* set_value_uinteger */
326 NULL
, /* set_value_sinteger */
327 NULL
, /* set_value_integer64 */
328 NULL
, /* set_value_floating */
330 value_get
, /* get_value */
331 NULL
, /* get_value_uinteger */
332 NULL
, /* get_value_sinteger */
333 NULL
, /* get_value_integer64 */
334 NULL
, /* get_value_floating */
342 NULL
, /* cmp_bitwise_and */
343 cmp_contains
, /* cmp_contains */
350 ftype_register(FT_STRING
, &string_type
);
351 ftype_register(FT_STRINGZ
, &stringz_type
);
352 ftype_register(FT_UINT_STRING
, &uint_string_type
);