ftypes: move get_value_sinteger64 into the union
[metze/wireshark/wip.git] / epan / ftypes / ftype-guid.c
1 /*
2  * Wireshark - Network traffic analyzer
3  * By Gerald Combs <gerald@wireshark.org>
4  * Copyright 2001 Gerald Combs
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20
21 #include "config.h"
22
23 #include <string.h>
24 #include <stdlib.h>
25
26 #include <ftypes-int.h>
27 #include <epan/guid-utils.h>
28 #include <epan/to_str.h>
29
30 static void
31 guid_fvalue_set_guid(fvalue_t *fv, const e_guid_t *value)
32 {
33     fv->value.guid = *value;
34 }
35
36 static gpointer
37 value_get(fvalue_t *fv)
38 {
39     return &(fv->value.guid);
40 }
41
42 static gboolean
43 get_guid(const char *s, e_guid_t *guid)
44 {
45     size_t i, n;
46     const char *p;
47     char digits[9];
48     static const char fmt[] = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX";
49
50     n = strlen(s);
51     if (n != strlen(fmt))
52         return FALSE;
53     for (i=0; i<n; i++) {
54         if (fmt[i] == 'X') {
55             if (!g_ascii_isxdigit(s[i]))
56                 return FALSE;
57         } else {
58             if (s[i] != fmt[i])
59                 return FALSE;
60         }
61     }
62
63     p = s;
64     g_strlcpy(digits, p, 9);
65     guid->data1 = (guint32)strtoul(digits, NULL, 16);
66     p += 9;
67     g_strlcpy(digits, p, 5);
68     guid->data2 = (guint16)strtoul(digits, NULL, 16);
69     p += 5;
70     g_strlcpy(digits, p, 5);
71     guid->data3 = (guint16)strtoul(digits, NULL, 16);
72     p += 5;
73     for (i=0; i < sizeof(guid->data4); i++) {
74         if (*p == '-') p++;
75         digits[0] = *(p++);
76         digits[1] = *(p++);
77         digits[2] = '\0';
78         guid->data4[i] = (guint8)strtoul(digits, NULL, 16);
79     }
80     return TRUE;
81 }
82
83 static gboolean
84 guid_from_unparsed(fvalue_t *fv, const char *s, gboolean allow_partial_value _U_, gchar **err_msg)
85 {
86      e_guid_t guid;
87
88     if (!get_guid(s, &guid)) {
89         if (err_msg != NULL)
90             *err_msg = g_strdup_printf("\"%s\" is not a valid GUID.", s);
91         return FALSE;
92     }
93
94     fv->value.guid = guid;
95     return TRUE;
96 }
97
98 static int
99 guid_repr_len(fvalue_t *fv _U_, ftrepr_t rtype _U_, int field_display _U_)
100 {
101     return GUID_STR_LEN;
102 }
103
104 static void
105 guid_to_repr(fvalue_t *fv, ftrepr_t rtype _U_, int field_display _U_, char *buf, unsigned int size)
106 {
107     guid_to_str_buf(&fv->value.guid, buf, size);
108 }
109
110 static gboolean
111 cmp_eq(const fvalue_t *a, const fvalue_t *b)
112 {
113     return memcmp(&a->value.guid, &b->value.guid, sizeof(e_guid_t)) == 0;
114 }
115
116 static gboolean
117 cmp_ne(const fvalue_t *a, const fvalue_t *b)
118 {
119     return memcmp(&a->value.guid, &b->value.guid, sizeof(e_guid_t)) != 0;
120 }
121
122 void
123 ftype_register_guid(void)
124 {
125
126     static ftype_t guid_type = {
127         FT_GUID,              /* ftype */
128         "FT_GUID",           /* name */
129         "Globally Unique Identifier",            /* pretty_name */
130         GUID_LEN,            /* wire_size */
131         NULL,                /* new_value */
132         NULL,                /* free_value */
133         guid_from_unparsed,  /* val_from_unparsed */
134         NULL,                /* val_from_string */
135         guid_to_repr,        /* val_to_string_repr */
136         guid_repr_len,       /* len_string_repr */
137
138         { .set_value_guid = guid_fvalue_set_guid }, /* union set_value */
139         { NULL },            /* union get_value */
140
141         value_get,           /* get_value_ptr */
142         NULL,                /* get_value_uinteger */
143         NULL,                /* get_value_sinteger */
144         NULL,                /* get_value_uinteger64 */
145
146         cmp_eq,
147         cmp_ne,
148         NULL,
149         NULL,
150         NULL,
151         NULL,
152         NULL,
153         NULL,
154         NULL,                /* cmp_matches */
155
156         NULL,
157         NULL,
158     };
159
160     ftype_register(FT_GUID, &guid_type);
161 }
162
163 /*
164  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
165  *
166  * Local variables:
167  * c-basic-offset: 4
168  * tab-width: 8
169  * indent-tabs-mode: nil
170  * End:
171  *
172  * vi: set shiftwidth=4 tabstop=8 expandtab:
173  * :indentSize=4:tabSize=8:noTabs=true:
174  */