MSWSP: add two more Property Sets
[wireshark-wip.git] / epan / ftypes / ftype-string.c
blobdb0c62796013d70e30b5fbfd58d23f1e13431838
1 /*
2 * $Id$
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.
23 #include "config.h"
25 #include <stdio.h>
26 #include <ftypes-int.h>
27 #include <epan/emem.h>
28 #include <string.h>
30 #define CMP_MATCHES cmp_matches
32 #include <ctype.h>
33 #include <strutil.h>
35 static void
36 string_fvalue_new(fvalue_t *fv)
38 fv->value.string = NULL;
41 static void
42 string_fvalue_free(fvalue_t *fv)
44 g_free(fv->value.string);
47 static void
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);
55 if (already_copied)
56 fv->value.string = (gchar *)value; /* must be g_ allocated */
57 else
58 fv->value.string = (gchar *)g_strdup((const gchar *)value);
61 static int
62 string_repr_len(fvalue_t *fv, ftrepr_t rtype)
64 switch (rtype) {
65 case FTREPR_DISPLAY:
66 return (int)strlen(fv->value.string);
68 case FTREPR_DFILTER:
69 return escape_string_len(fv->value.string);
71 g_assert_not_reached();
72 return -1;
75 static void
76 string_to_repr(fvalue_t *fv, ftrepr_t rtype, char *buf)
78 switch (rtype) {
79 case FTREPR_DISPLAY:
80 strcpy(buf, fv->value.string);
81 return;
83 case FTREPR_DFILTER:
84 escape_string(buf, fv->value.string);
85 return;
87 g_assert_not_reached();
91 static gpointer
92 value_get(fvalue_t *fv)
94 return fv->value.string;
97 static gboolean
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);
104 return TRUE;
107 static gboolean
108 val_from_unparsed(fvalue_t *fv, char *s, gboolean allow_partial_value _U_, LogFunc logfunc)
110 fvalue_t *fv_bytes;
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);
117 if (fv_bytes) {
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);
128 return TRUE;
131 /* Just turn it into a string */
132 return val_from_string(fv, s, logfunc);
135 static guint
136 len(fvalue_t *fv)
138 return (guint)strlen(fv->value.string);
141 static void
142 slice(fvalue_t *fv, GByteArray *bytes, guint offset, guint length)
144 guint8* data;
146 data = fv->value.ustring + offset;
148 g_byte_array_append(bytes, data, length);
152 static gboolean
153 cmp_eq(const fvalue_t *a, const fvalue_t *b)
155 return (strcmp(a->value.string, b->value.string) == 0);
158 static gboolean
159 cmp_ne(const fvalue_t *a, const fvalue_t *b)
161 return (strcmp(a->value.string, b->value.string) != 0);
164 static gboolean
165 cmp_gt(const fvalue_t *a, const fvalue_t *b)
167 return (strcmp(a->value.string, b->value.string) > 0);
170 static gboolean
171 cmp_ge(const fvalue_t *a, const fvalue_t *b)
173 return (strcmp(a->value.string, b->value.string) >= 0);
176 static gboolean
177 cmp_lt(const fvalue_t *a, const fvalue_t *b)
179 return (strcmp(a->value.string, b->value.string) < 0);
182 static gboolean
183 cmp_le(const fvalue_t *a, const fvalue_t *b)
185 return (strcmp(a->value.string, b->value.string) <= 0);
188 static gboolean
189 cmp_contains(const fvalue_t *fv_a, const fvalue_t *fv_b)
191 /* According to
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) {
196 return FALSE;
199 if (strstr(fv_a->value.string, fv_b->value.string)) {
200 return TRUE;
202 else {
203 return FALSE;
207 static gboolean
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) {
218 return FALSE;
220 if (! regex) {
221 return FALSE;
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 */
234 void
235 ftype_register_string(void)
238 static ftype_t string_type = {
239 FT_STRING, /* ftype */
240 "FT_STRING", /* name */
241 "Character string", /* pretty_name */
242 0, /* wire_size */
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 */
262 cmp_eq,
263 cmp_ne,
264 cmp_gt,
265 cmp_ge,
266 cmp_lt,
267 cmp_le,
268 NULL, /* cmp_bitwise_and */
269 cmp_contains,
270 CMP_MATCHES,
272 len,
273 slice,
275 static ftype_t stringz_type = {
276 FT_STRINGZ, /* ftype */
277 "FT_STRINGZ", /* name */
278 "Character string", /* pretty name */
279 0, /* wire_size */
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 */
299 cmp_eq,
300 cmp_ne,
301 cmp_gt,
302 cmp_ge,
303 cmp_lt,
304 cmp_le,
305 NULL, /* cmp_bitwise_and */
306 cmp_contains, /* cmp_contains */
307 CMP_MATCHES,
309 len,
310 slice,
312 static ftype_t uint_string_type = {
313 FT_UINT_STRING, /* ftype */
314 "FT_UINT_STRING", /* name */
315 "Character string", /* pretty_name */
316 0, /* wire_size */
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 */
336 cmp_eq,
337 cmp_ne,
338 cmp_gt,
339 cmp_ge,
340 cmp_lt,
341 cmp_le,
342 NULL, /* cmp_bitwise_and */
343 cmp_contains, /* cmp_contains */
344 CMP_MATCHES,
346 len,
347 slice,
350 ftype_register(FT_STRING, &string_type);
351 ftype_register(FT_STRINGZ, &stringz_type);
352 ftype_register(FT_UINT_STRING, &uint_string_type);