HACK: pinfo->private_data points to smb_info again
[wireshark-wip.git] / epan / guid-utils.c
blob91d81669b224f2fb261c9b9bffc03278024b5861
1 /* guid-utils.c
2 * GUID handling
4 * $Id$
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
9 * Copyright 1998 Gerald Combs
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include "config.h"
28 #include <string.h>
30 #include <glib.h>
31 #include <epan/epan.h>
32 #include <wsutil/unicode-utils.h>
33 #include <epan/emem.h>
34 #include <epan/wmem/wmem.h>
35 #include "guid-utils.h"
37 #ifdef _WIN32
38 #include <tchar.h>
39 #include <windows.h>
40 #endif
42 static wmem_tree_t *guid_to_name_tree = NULL;
45 #ifdef _WIN32
46 /* try to resolve an DCE/RPC interface name to its name using the Windows registry entries */
47 /* XXX - might be better to fill all interfaces into our database at startup instead of searching each time */
48 int
49 ResolveWin32UUID(e_guid_t if_id, char *uuid_name, int uuid_name_max_len)
51 TCHAR *reg_uuid_name;
52 HKEY hKey = NULL;
53 DWORD uuid_max_size = MAX_PATH;
54 TCHAR *reg_uuid_str;
56 reg_uuid_name=ep_alloc(MAX_PATH*sizeof(TCHAR));
57 reg_uuid_str=ep_alloc(MAX_PATH*sizeof(TCHAR));
59 if(uuid_name_max_len < 2){
60 return 0;
62 reg_uuid_name[0] = '\0';
63 _snwprintf(reg_uuid_str, MAX_PATH, _T("SOFTWARE\\Classes\\Interface\\{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}"),
64 if_id.data1, if_id.data2, if_id.data3,
65 if_id.data4[0], if_id.data4[1],
66 if_id.data4[2], if_id.data4[3],
67 if_id.data4[4], if_id.data4[5],
68 if_id.data4[6], if_id.data4[7]);
69 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, reg_uuid_str, 0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS) {
70 if (RegQueryValueEx(hKey, NULL, NULL, NULL, (LPBYTE)reg_uuid_name, &uuid_max_size) == ERROR_SUCCESS && uuid_max_size <= MAX_PATH) {
71 g_snprintf(uuid_name, uuid_name_max_len, "%s", utf_16to8(reg_uuid_name));
72 RegCloseKey(hKey);
73 return (int) strlen(uuid_name);
75 RegCloseKey(hKey);
77 return 0; /* we didn't find anything anyhow. Please don't use the string! */
80 #endif
83 /* store a guid to name mapping */
84 void
85 guids_add_guid(e_guid_t *guid, const gchar *name)
87 wmem_tree_key_t guidkey[2];
88 guint32 g[4];
90 g[0]=guid->data1;
92 g[1]=guid->data2;
93 g[1]<<=16;
94 g[1]|=guid->data3;
96 g[2]=guid->data4[0];
97 g[2]<<=8;
98 g[2]|=guid->data4[1];
99 g[2]<<=8;
100 g[2]|=guid->data4[2];
101 g[2]<<=8;
102 g[2]|=guid->data4[3];
104 g[3]=guid->data4[4];
105 g[3]<<=8;
106 g[3]|=guid->data4[5];
107 g[3]<<=8;
108 g[3]|=guid->data4[6];
109 g[3]<<=8;
110 g[3]|=guid->data4[7];
112 guidkey[0].key=g;
113 guidkey[0].length=4;
114 guidkey[1].length=0;
116 wmem_tree_insert32_array(guid_to_name_tree, &guidkey[0], (gchar *) name);
120 /* retrieve the registered name for this GUID */
121 const gchar *
122 guids_get_guid_name(e_guid_t *guid)
124 wmem_tree_key_t guidkey[2];
125 guint32 g[4];
126 char *name;
127 #ifdef _WIN32
128 static char *uuid_name;
129 #endif
131 g[0]=guid->data1;
133 g[1]=guid->data2;
134 g[1]<<=16;
135 g[1]|=guid->data3;
137 g[2]=guid->data4[0];
138 g[2]<<=8;
139 g[2]|=guid->data4[1];
140 g[2]<<=8;
141 g[2]|=guid->data4[2];
142 g[2]<<=8;
143 g[2]|=guid->data4[3];
145 g[3]=guid->data4[4];
146 g[3]<<=8;
147 g[3]|=guid->data4[5];
148 g[3]<<=8;
149 g[3]|=guid->data4[6];
150 g[3]<<=8;
151 g[3]|=guid->data4[7];
153 guidkey[0].key=g;
154 guidkey[0].length=4;
155 guidkey[1].length=0;
157 if((name = (char *)wmem_tree_lookup32_array(guid_to_name_tree, &guidkey[0]))){
158 return name;
161 #ifdef _WIN32
162 /* try to resolve the mapping from the Windows registry */
163 /* XXX - prefill the resolving database with all the Windows registry entries once at init only (instead of searching each time)? */
164 uuid_name=ep_alloc(128);
165 if(ResolveWin32UUID(*guid, uuid_name, 128)) {
166 return uuid_name;
168 #endif
170 return NULL;
174 void
175 guids_init(void)
177 guid_to_name_tree=wmem_tree_new(wmem_epan_scope());
178 /* XXX here is a good place to read a config file with wellknown guids */
182 /* Tries to match a guid against its name.
183 Returns the associated string ptr on a match.
184 Formats uuid number and returns the resulting string, if name is unknown.
185 (derived from val_to_str) */
186 const gchar *
187 guids_resolve_guid_to_str(e_guid_t *guid)
189 const gchar *name;
191 name=guids_get_guid_name(guid);
192 if(name){
193 return name;
196 return ep_strdup_printf("%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
197 guid->data1, guid->data2, guid->data3,
198 guid->data4[0], guid->data4[1],
199 guid->data4[2], guid->data4[3],
200 guid->data4[4], guid->data4[5],
201 guid->data4[6], guid->data4[7]);
204 int guid_cmp(const e_guid_t *g1, const e_guid_t *g2)
206 if (g1->data1 != g2->data1) {
207 return (g1->data1 < g2->data1) ? -1 : 1;
210 if (g1->data2 != g2->data2) {
211 return (g1->data2 < g2->data2) ? -1 : 1;
214 if (g1->data3 != g2->data3) {
215 return (g1->data3 < g2->data3) ? -1 : 1;
218 return memcmp(&g1->data4[0], &g2->data4[0], 8);