Merge "Apply layouts in Qt Creator. Fix a tooltip."
[metze/wireshark/wip.git] / epan / ftypes / ftype-guid.c
1 /*
2  * $Id$
3  *
4  * Wireshark - Network traffic analyzer
5  * By Gerald Combs <gerald@wireshark.org>
6  * Copyright 2001 Gerald Combs
7  *
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.
12  *
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.
17  *
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.
21  */
22
23 #include "config.h"
24
25 #include <string.h>
26 #include <stdlib.h>
27 #include <ctype.h>
28
29 #include <ftypes-int.h>
30 #include <epan/guid-utils.h>
31 #include <epan/to_str.h>
32
33 static void
34 guid_fvalue_set_guid(fvalue_t *fv, const e_guid_t *value)
35 {
36     fv->value.guid = *value;
37 }
38
39 static gpointer
40 value_get(fvalue_t *fv)
41 {
42     return &(fv->value.guid);
43 }
44
45 static gboolean
46 get_guid(const char *s, e_guid_t *guid)
47 {
48     size_t i, n;
49     const char *p;
50     char digits[9];
51     static const char fmt[] = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX";
52
53     n = strlen(s);
54     if (n != strlen(fmt))
55         return FALSE;
56     for (i=0; i<n; i++) {
57         if (fmt[i] == 'X') {
58             if (!isxdigit((guchar)s[i]))
59                 return FALSE;
60         } else {
61             if (s[i] != fmt[i])
62                 return FALSE;
63         }
64     }
65     
66     p = s;
67     strncpy(digits, p, 8); 
68     digits[8] = '\0';
69     guid->data1 = (guint32)strtoul(digits, NULL, 16);
70     p += 9;
71     strncpy(digits, p, 4); 
72     digits[4] = '\0';
73     guid->data2 = (guint16)strtoul(digits, NULL, 16);
74     p += 5;
75     strncpy(digits, p, 4); 
76     digits[4] = '\0';
77     guid->data3 = (guint16)strtoul(digits, NULL, 16);
78     p += 5;
79     for (i=0; i < sizeof(guid->data4); i++) {
80         if (*p == '-') p++;
81         digits[0] = *(p++);
82         digits[1] = *(p++);
83         digits[2] = '\0';
84         guid->data4[i] = (guint8)strtoul(digits, NULL, 16);
85     }
86     return TRUE;
87 }
88
89 static gboolean
90 guid_from_unparsed(fvalue_t *fv, const char *s, gboolean allow_partial_value _U_, LogFunc logfunc)
91 {
92      e_guid_t guid;
93
94     if (!get_guid(s, &guid)) {
95         logfunc("\"%s\" is not a valid GUID.", s);
96         return FALSE;
97     }
98
99     fv->value.guid = guid;
100     return TRUE;
101 }
102
103 static int
104 guid_repr_len(fvalue_t *fv _U_, ftrepr_t rtype _U_)
105 {
106     return GUID_STR_LEN;
107 }
108
109 static void
110 guid_to_repr(fvalue_t *fv, ftrepr_t rtype _U_, char *buf)
111 {
112     guid_to_str_buf(&fv->value.guid, buf, GUID_STR_LEN);
113 }
114
115 static gboolean
116 cmp_eq(const fvalue_t *a, const fvalue_t *b)
117 {
118     return memcmp(&a->value.guid, &b->value.guid, sizeof(e_guid_t)) == 0;
119 }
120
121 static gboolean
122 cmp_ne(const fvalue_t *a, const fvalue_t *b)
123 {
124     return memcmp(&a->value.guid, &b->value.guid, sizeof(e_guid_t)) != 0;
125 }
126
127 void
128 ftype_register_guid(void)
129 {
130
131     static ftype_t guid_type = {
132         FT_GUID,              /* ftype */
133         "FT_GUID",           /* name */
134         "Globally Unique Identifier",            /* pretty_name */
135         GUID_LEN,            /* wire_size */
136         NULL,                /* new_value */
137         NULL,                /* free_value */
138         guid_from_unparsed,  /* val_from_unparsed */
139         NULL,                /* val_from_string */
140         guid_to_repr,        /* val_to_string_repr */
141         guid_repr_len,       /* len_string_repr */
142
143         NULL,                /* set_value_byte_array */
144         NULL,                /* set_value_bytes */
145         guid_fvalue_set_guid, /* set_value_guid */
146         NULL,                /* set_value_time */
147         NULL,                /* set_value_string */
148         NULL,                /* set_value_tvbuff */
149         NULL,                /* set_value_uinteger */
150         NULL,                /* set_value_sinteger */
151         NULL,                /* set_value_integer64 */
152         NULL,                /* set_value_floating */
153
154         value_get,           /* get_value */
155         NULL,                /* get_value_uinteger */
156         NULL,                /* get_value_sinteger */
157         NULL,                /* get_value_integer64 */
158         NULL,                /* get_value_floating */
159
160         cmp_eq,
161         cmp_ne,
162         NULL,
163         NULL,
164         NULL,
165         NULL,
166         NULL,
167         NULL,
168         NULL,                /* cmp_matches */
169
170         NULL,
171         NULL,
172     };
173
174     ftype_register(FT_GUID, &guid_type);
175 }