various UUID/GUID based changes.
[metze/wireshark/wip.git] / epan / guid-utils.c
1 /* guid-utils.c
2  * GUID handling
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  *
9  * Copyright 1998 Gerald Combs
10  *
11  * MobileIPv6 support added by Tomislav Borosa <tomislav.borosa@siemens.hr>
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26  */
27
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #include <string.h>
33
34 #include <glib.h>
35 #include "guid-utils.h"
36
37 static gint
38 guid_equal (gconstpointer k1, gconstpointer k2)
39 {
40     const guid_key *key1 = (const guid_key *)k1;
41     const guid_key *key2 = (const guid_key *)k2;
42     return ((memcmp (&key1->guid, &key2->guid, sizeof (e_guid_t)) == 0));
43 }
44
45 static guint
46 guid_hash (gconstpointer k)
47 {
48     const guid_key *key = (const guid_key *)k;
49     /* This isn't perfect, but the Data1 part of these is almost always
50        unique. */
51     return key->guid.data1;
52 }
53
54
55 GHashTable *guids_new(void)
56 {
57     return g_hash_table_new (guid_hash, guid_equal);
58 }
59
60 void guids_add_guid(GHashTable *guids, e_guid_t *guid, gchar *name, void *private_data)
61 {
62     guid_key *key = g_malloc (sizeof (*key));
63     guid_value *value = g_malloc (sizeof (*value));
64
65     key->guid = *guid;
66
67     value->name = name;
68
69     g_hash_table_insert (guids, key, value);
70 }
71
72
73 /* try to get registered name for this guid */
74 const gchar *guids_get_guid_name(GHashTable *guids, e_guid_t *guid)
75 {
76     guid_key key;
77     guid_value *value;
78
79
80         /* try to get registered guid "name" of if_id */
81         key.guid = *guid;
82
83     if ((value = g_hash_table_lookup (guids, &key)) != NULL) {
84                 return value->name;
85         }
86
87         return NULL;
88 }