Add infrastructure for display filter functions.
[metze/wireshark/wip.git] / epan / ftypes / ftype-string.c
index 1af5c38184c95aa0472c839c6d78ff2677f49bb8..d4525fc27951d8c05ff35a4c2d5d59d8de25b9ed 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: ftype-string.c,v 1.17 2003/12/09 23:02:39 obiot Exp $
+ * $Id$
  *
  * Ethereal - Network traffic analyzer
  * By Gerald Combs <gerald@ethereal.com>
@@ -26,6 +26,7 @@
 
 #include <ftypes-int.h>
 #include <string.h>
+#include <epan/emem.h>
 
 #ifdef HAVE_LIBPCRE
 #include <pcre.h>
@@ -34,6 +35,8 @@
 #define CMP_MATCHES NULL
 #endif
 
+#include <ctype.h>
+
 static void
 string_fvalue_new(fvalue_t *fv)
 {
@@ -51,7 +54,7 @@ string_fvalue_free(fvalue_t *fv)
 static void
 string_fvalue_set(fvalue_t *fv, gpointer value, gboolean already_copied)
 {
-       g_assert(value != NULL);
+    DISSECTOR_ASSERT(value != NULL);
 
        /* Free up the old value, if we have one */
        string_fvalue_free(fv);
@@ -76,12 +79,21 @@ string_repr_len(fvalue_t *fv, ftrepr_t rtype)
                case FTREPR_DFILTER:
                        repr_len = 0;
                        for (p = fv->value.string; (c = *p) != '\0'; p++) {
+                               /* Backslashes and double-quotes must
+                                * be escaped */
                                if (c == '\\' || c == '"') {
-                                       /* Backslashes and double-quotes
-                                          must be escaped. */
+                                       repr_len += 2;
+                               }
+                               /* Values that can't nicely be represented
+                                * in ASCII need to be escaped. */
+                               else if (!isprint((unsigned char)c)) {
+                                       /* c --> \xNN */
+                                       repr_len += 4;
+                               }
+                               /* Other characters are just passed through. */
+                               else {
                                        repr_len++;
                                }
-                               repr_len++;
                        }
                        return repr_len + 2;    /* string plus leading and trailing quotes */
        }
@@ -94,17 +106,32 @@ string_to_repr(fvalue_t *fv, ftrepr_t rtype, char *buf)
 {
        gchar *p, c;
        char *bufp;
+       char hex[3];
 
        if (rtype == FTREPR_DFILTER) {
                bufp = buf;
                *bufp++ = '"';
                for (p = fv->value.string; (c = *p) != '\0'; p++) {
+                       /* Backslashes and double-quotes must
+                        * be escaped. */
                        if (c == '\\' || c == '"') {
-                               /* Backslashes and double-quotes
-                                  must be escaped. */
                                *bufp++ = '\\';
+                               *bufp++ = c;
+                       }
+                       /* Values that can't nicely be represented
+                        * in ASCII need to be escaped. */
+                       else if (!isprint((unsigned char)c)) {
+                               /* c --> \xNN */
+                               sprintf(hex, "%02x", (unsigned char) c);
+                               *bufp++ = '\\';
+                               *bufp++ = 'x';
+                               *bufp++ = hex[0];
+                               *bufp++ = hex[1];
+                       }
+                       /* Other characters are just passed through. */
+                       else {
+                               *bufp++ = c;
                        }
-                       *bufp++ = c;
                }
                *bufp++ = '"';
                *bufp = '\0';
@@ -148,7 +175,7 @@ val_from_unparsed(fvalue_t *fv, char *s, gboolean allow_partial_value _U_, LogFu
                int num_bytes = fv_bytes->value.bytes->len;
 
                fv->value.string = g_malloc(num_bytes + 1);
-               memcpy(fv->value.string, fv->value.bytes->data, num_bytes);
+               memcpy(fv->value.string, fv_bytes->value.bytes->data, num_bytes);
                fv->value.string[num_bytes] = '\0';
 
                FVALUE_FREE(fv_bytes);
@@ -172,7 +199,7 @@ slice(fvalue_t *fv, GByteArray *bytes, guint offset, guint length)
 {
        guint8* data;
 
-       data = fv->value.string + offset;
+       data = fv->value.ustring + offset;
 
        g_byte_array_append(bytes, data, length);
 }
@@ -237,6 +264,8 @@ cmp_contains(fvalue_t *fv_a, fvalue_t *fv_b)
 static gboolean
 cmp_matches(fvalue_t *fv_a, fvalue_t *fv_b)
 {
+       char *str = fv_a->value.string;
+       pcre_tuple_t *pcre = fv_b->value.re;
        int options = 0;
        int rc;
 
@@ -247,14 +276,14 @@ cmp_matches(fvalue_t *fv_a, fvalue_t *fv_b)
        if (strcmp(fv_b->ftype->name, "FT_PCRE") != 0) {
                return FALSE;
        }
-       if (! fv_b->value.re) {
+       if (! pcre) {
                return FALSE;
        }
        rc = pcre_exec(
-                       (fv_b->value.re)->re,   /* Compiled PCRE */
-                       (fv_b->value.re)->ex,   /* PCRE extra from pcre_study() */
-                       fv_a->value.string,             /* The data to check for the pattern... */
-                       (int)strlen(fv_a->value.string),        /* ... and its length */
+                       pcre->re,       /* Compiled PCRE */
+                       pcre->ex,       /* PCRE extra from pcre_study() */
+                       str,                            /* The data to check for the pattern... */
+                       (int)strlen(str),       /* ... and its length */
                        0,                      /* Start offset within data */
                        options,        /* PCRE options */
                        NULL,           /* We are not interested in the matched string */
@@ -272,6 +301,7 @@ ftype_register_string(void)
 {
 
        static ftype_t string_type = {
+               FT_STRING,                      /* ftype */
                "FT_STRING",                    /* name */
                "character string",             /* pretty_name */
                0,                              /* wire_size */
@@ -284,10 +314,12 @@ ftype_register_string(void)
 
                string_fvalue_set,              /* set_value */
                NULL,                           /* set_value_integer */
+               NULL,                           /* set_value_integer64 */
                NULL,                           /* set_value_floating */
 
                value_get,                      /* get_value */
                NULL,                           /* get_value_integer */
+               NULL,                           /* get_value_integer64 */
                NULL,                           /* get_value_floating */
 
                cmp_eq,
@@ -296,6 +328,7 @@ ftype_register_string(void)
                cmp_ge,
                cmp_lt,
                cmp_le,
+               NULL,                           /* cmp_bitwise_and */
                cmp_contains,
                CMP_MATCHES,
 
@@ -303,23 +336,26 @@ ftype_register_string(void)
                slice,
        };
        static ftype_t stringz_type = {
-               "FT_STRINGZ",
-               "character string",
-               0,
-               string_fvalue_new,
-               string_fvalue_free,
+               FT_STRINGZ,                     /* ftype */
+               "FT_STRINGZ",                   /* name */
+               "character string",             /* pretty name */
+               0,                              /* wire_size */
+               string_fvalue_new,              /* new_value */
+               string_fvalue_free,             /* free_value */
                val_from_unparsed,              /* val_from_unparsed */
                val_from_string,                /* val_from_string */
-               NULL,                           /* val_to_string_repr */
-               NULL,                           /* len_string_repr */
+               string_to_repr,                 /* val_to_string_repr */
+               string_repr_len,                /* len_string_repr */
 
-               string_fvalue_set,
-               NULL,
-               NULL,
+               string_fvalue_set,              /* set_value */
+               NULL,                           /* set_value_integer */
+               NULL,                           /* set_value_integer64 */
+               NULL,                           /* set_value_floating */
 
-               value_get,
-               NULL,
-               NULL,
+               value_get,                      /* get_value */
+               NULL,                           /* get_value_integer */
+               NULL,                           /* get_value_integer64 */
+               NULL,                           /* get_value_floating */
 
                cmp_eq,
                cmp_ne,
@@ -327,6 +363,7 @@ ftype_register_string(void)
                cmp_ge,
                cmp_lt,
                cmp_le,
+               NULL,                           /* cmp_bitwise_and */
                cmp_contains,                   /* cmp_contains */
                CMP_MATCHES,
 
@@ -334,23 +371,26 @@ ftype_register_string(void)
                slice,
        };
        static ftype_t uint_string_type = {
-               "FT_UINT_STRING",
-               "character string",
-               0,
-               string_fvalue_new,
-               string_fvalue_free,
+               FT_UINT_STRING,         /* ftype */
+               "FT_UINT_STRING",               /* name */
+               "character string",             /* pretty_name */
+               0,                              /* wire_size */
+               string_fvalue_new,              /* new_value */
+               string_fvalue_free,             /* free_value */
                val_from_unparsed,              /* val_from_unparsed */
                val_from_string,                /* val_from_string */
-               NULL,                           /* val_to_string_repr */
-               NULL,                           /* len_string_repr */
+               string_to_repr,                 /* val_to_string_repr */
+               string_repr_len,                /* len_string_repr */
 
-               string_fvalue_set,
-               NULL,
-               NULL,
+               string_fvalue_set,              /* set_value */
+               NULL,                           /* set_value_integer */
+               NULL,                           /* set_value_integer64 */
+               NULL,                           /* set_value_floating */
 
-               value_get,
-               NULL,
-               NULL,
+               value_get,                      /* get_value */
+               NULL,                           /* get_value_integer */
+               NULL,                           /* get_value_integer64 */
+               NULL,                           /* get_value_floating */
 
                cmp_eq,
                cmp_ne,
@@ -358,6 +398,7 @@ ftype_register_string(void)
                cmp_ge,
                cmp_lt,
                cmp_le,
+               NULL,                           /* cmp_bitwise_and */
                cmp_contains,                   /* cmp_contains */
                CMP_MATCHES,