summaryrefslogtreecommitdiffstats
path: root/epan/ftypes/ftype-guid.c
blob: 311a0177c2f33ff94e92dc23d97bb35fb3bc42f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 2001 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "config.h"

#include <string.h>
#include <stdlib.h>

#include <ftypes-int.h>
#include <epan/guid-utils.h>
#include <epan/to_str.h>

static void
guid_fvalue_set_guid(fvalue_t *fv, const e_guid_t *value)
{
    fv->value.guid = *value;
}

static const e_guid_t *
value_get(fvalue_t *fv)
{
    return &(fv->value.guid);
}

static bool
get_guid(const char *s, e_guid_t *guid)
{
    size_t i, n;
    const char *p;
    char digits[3];
    static const char fmt[] = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX";
    const size_t fmtchars = sizeof(fmt) - 1;

    n = strnlen(s, fmtchars);
    if (n != fmtchars)
        return false;
    for (i=0; i<n; i++) {
        if (fmt[i] == 'X') {
            if (!g_ascii_isxdigit(s[i]))
                return false;
        } else {
            if (s[i] != fmt[i])
                return false;
        }
    }

    p = s;
    guid->data1 = (uint32_t)strtoul(p, NULL, 16);
    p += 9;
    guid->data2 = (uint16_t)strtoul(p, NULL, 16);
    p += 5;
    guid->data3 = (uint16_t)strtoul(p, NULL, 16);
    p += 5;
    for (i=0; i < sizeof(guid->data4); i++) {
        if (*p == '-') p++;
        digits[0] = *(p++);
        digits[1] = *(p++);
        digits[2] = '\0';
        guid->data4[i] = (uint8_t)strtoul(digits, NULL, 16);
    }
    return true;
}

static bool
guid_from_literal(fvalue_t *fv, const char *s, bool allow_partial_value _U_, char **err_msg)
{
     e_guid_t guid;

    if (!get_guid(s, &guid)) {
        if (err_msg != NULL)
            *err_msg = ws_strdup_printf("\"%s\" is not a valid GUID.", s);
        return false;
    }

    fv->value.guid = guid;
    return true;
}

static char *
guid_to_repr(wmem_allocator_t *scope, const fvalue_t *fv, ftrepr_t rtype _U_, int field_display _U_)
{
    return guid_to_str(scope, &fv->value.guid);
}

static enum ft_result
cmp_order(const fvalue_t *a, const fvalue_t *b, int *cmp)
{
    *cmp = memcmp(&a->value.guid, &b->value.guid, sizeof(e_guid_t));
    return FT_OK;
}

static unsigned
value_hash(const fvalue_t *fv)
{
    return guid_hash(&fv->value.guid);
}

void
ftype_register_guid(void)
{

    static ftype_t guid_type = {
        FT_GUID,              /* ftype */
        "FT_GUID",           /* name */
        "Globally Unique Identifier",            /* pretty_name */
        GUID_LEN,            /* wire_size */
        NULL,                /* new_value */
        NULL,                /* copy_value */
        NULL,                /* free_value */
        guid_from_literal,   /* val_from_literal */
        NULL,                /* val_from_string */
        NULL,                /* val_from_charconst */
        guid_to_repr,        /* val_to_string_repr */

        NULL,                /* val_to_uinteger64 */
        NULL,                /* val_to_sinteger64 */

        { .set_value_guid = guid_fvalue_set_guid }, /* union set_value */
        { .get_value_guid = value_get },             /* union get_value */

        cmp_order,
        NULL,
        NULL,                /* cmp_matches */

        value_hash,          /* hash */
        NULL,
        NULL,
        NULL,
        NULL,
        NULL,
        NULL,                /* unary_minus */
        NULL,                /* add */
        NULL,                /* subtract */
        NULL,                /* multiply */
        NULL,                /* divide */
        NULL,                /* modulo */
    };

    ftype_register(FT_GUID, &guid_type);
}

void
ftype_register_pseudofields_guid(int proto)
{
    static int hf_ft_guid;

    static hf_register_info hf_ftypes[] = {
            { &hf_ft_guid,
                { "FT_GUID", "_ws.ftypes.guid",
                    FT_GUID, BASE_NONE, NULL, 0x00,
                    NULL, HFILL }
            },
    };

    proto_register_field_array(proto, hf_ftypes, array_length(hf_ftypes));
}

/*
 * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
 *
 * Local variables:
 * c-basic-offset: 4
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * vi: set shiftwidth=4 tabstop=8 expandtab:
 * :indentSize=4:tabSize=8:noTabs=true:
 */