epan/dissectors/pidl/samr/samr.cnf cnf_dissect_lsa_BinaryString => lsarpc_dissect_str...
[wireshark-sm.git] / epan / ftypes / ftype-guid.c
blobd8ffa1f03b21b494cddfe134eb37ad48076d6346
1 /*
2 * Wireshark - Network traffic analyzer
3 * By Gerald Combs <gerald@wireshark.org>
4 * Copyright 2001 Gerald Combs
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
9 #include "config.h"
11 #include <string.h>
12 #include <stdlib.h>
14 #include <ftypes-int.h>
15 #include <epan/guid-utils.h>
16 #include <epan/to_str.h>
17 #include <wsutil/array.h>
19 static void
20 guid_fvalue_set_guid(fvalue_t *fv, const e_guid_t *value)
22 fv->value.guid = *value;
25 static const e_guid_t *
26 value_get(fvalue_t *fv)
28 return &(fv->value.guid);
31 static bool
32 get_guid(const char *s, e_guid_t *guid)
34 size_t i, n;
35 const char *p;
36 char digits[3];
37 static const char fmt[] = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX";
38 const size_t fmtchars = sizeof(fmt) - 1;
40 n = strnlen(s, fmtchars);
41 if (n != fmtchars)
42 return false;
43 for (i=0; i<n; i++) {
44 if (fmt[i] == 'X') {
45 if (!g_ascii_isxdigit(s[i]))
46 return false;
47 } else {
48 if (s[i] != fmt[i])
49 return false;
53 p = s;
54 guid->data1 = (uint32_t)strtoul(p, NULL, 16);
55 p += 9;
56 guid->data2 = (uint16_t)strtoul(p, NULL, 16);
57 p += 5;
58 guid->data3 = (uint16_t)strtoul(p, NULL, 16);
59 p += 5;
60 for (i=0; i < sizeof(guid->data4); i++) {
61 if (*p == '-') p++;
62 digits[0] = *(p++);
63 digits[1] = *(p++);
64 digits[2] = '\0';
65 guid->data4[i] = (uint8_t)strtoul(digits, NULL, 16);
67 return true;
70 static bool
71 guid_from_literal(fvalue_t *fv, const char *s, bool allow_partial_value _U_, char **err_msg)
73 e_guid_t guid;
75 if (!get_guid(s, &guid)) {
76 if (err_msg != NULL)
77 *err_msg = ws_strdup_printf("\"%s\" is not a valid GUID.", s);
78 return false;
81 fv->value.guid = guid;
82 return true;
85 static char *
86 guid_to_repr(wmem_allocator_t *scope, const fvalue_t *fv, ftrepr_t rtype _U_, int field_display _U_)
88 return guid_to_str(scope, &fv->value.guid);
91 static enum ft_result
92 cmp_order(const fvalue_t *a, const fvalue_t *b, int *cmp)
94 *cmp = memcmp(&a->value.guid, &b->value.guid, sizeof(e_guid_t));
95 return FT_OK;
98 static unsigned
99 value_hash(const fvalue_t *fv)
101 return guid_hash(&fv->value.guid);
104 void
105 ftype_register_guid(void)
108 static const ftype_t guid_type = {
109 FT_GUID, /* ftype */
110 GUID_LEN, /* wire_size */
111 NULL, /* new_value */
112 NULL, /* copy_value */
113 NULL, /* free_value */
114 guid_from_literal, /* val_from_literal */
115 NULL, /* val_from_string */
116 NULL, /* val_from_charconst */
117 NULL, /* val_from_uinteger64 */
118 NULL, /* val_from_sinteger64 */
119 NULL, /* val_from_double */
120 guid_to_repr, /* val_to_string_repr */
122 NULL, /* val_to_uinteger64 */
123 NULL, /* val_to_sinteger64 */
124 NULL, /* val_to_double */
126 { .set_value_guid = guid_fvalue_set_guid }, /* union set_value */
127 { .get_value_guid = value_get }, /* union get_value */
129 cmp_order,
130 NULL,
131 NULL, /* cmp_matches */
133 value_hash, /* hash */
134 NULL,
135 NULL,
136 NULL,
137 NULL,
138 NULL,
139 NULL, /* unary_minus */
140 NULL, /* add */
141 NULL, /* subtract */
142 NULL, /* multiply */
143 NULL, /* divide */
144 NULL, /* modulo */
147 ftype_register(FT_GUID, &guid_type);
150 void
151 ftype_register_pseudofields_guid(int proto)
153 static int hf_ft_guid;
155 static hf_register_info hf_ftypes[] = {
156 { &hf_ft_guid,
157 { "FT_GUID", "_ws.ftypes.guid",
158 FT_GUID, BASE_NONE, NULL, 0x00,
159 NULL, HFILL }
163 proto_register_field_array(proto, hf_ftypes, array_length(hf_ftypes));
167 * Editor modelines - https://www.wireshark.org/tools/modelines.html
169 * Local variables:
170 * c-basic-offset: 4
171 * tab-width: 8
172 * indent-tabs-mode: nil
173 * End:
175 * vi: set shiftwidth=4 tabstop=8 expandtab:
176 * :indentSize=4:tabSize=8:noTabs=true: