From Tyson Key:
[obnox/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  * 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.
15  *
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.
20  *
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include <string.h>
31
32 #include <glib.h>
33 #include <epan/epan.h>
34 #include <wsutil/unicode-utils.h>
35 #include <epan/emem.h>
36 #include "guid-utils.h"
37
38 #ifdef _WIN32
39 #include <tchar.h>
40 #include <windows.h>
41 #endif
42
43 static emem_tree_t *guid_to_name_tree = NULL;
44
45
46 #ifdef _WIN32
47 /* try to resolve an DCE/RPC interface name to it's name using the Windows registry entries */
48 /* XXX - might be better to fill all interfaces into our database at startup instead of searching each time */
49 int
50 ResolveWin32UUID(e_guid_t if_id, char *uuid_name, int uuid_name_max_len)
51 {
52         TCHAR *reg_uuid_name;
53         HKEY hKey = NULL;
54         DWORD uuid_max_size = MAX_PATH;
55         TCHAR *reg_uuid_str;
56
57         reg_uuid_name=ep_alloc(MAX_PATH*sizeof(TCHAR));
58         reg_uuid_str=ep_alloc(MAX_PATH*sizeof(TCHAR));
59
60         if(uuid_name_max_len < 2){
61                 return 0;
62         }
63         reg_uuid_name[0] = '\0';
64         _snwprintf(reg_uuid_str, MAX_PATH, _T("SOFTWARE\\Classes\\Interface\\{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}"),
65                         if_id.data1, if_id.data2, if_id.data3,
66                         if_id.data4[0], if_id.data4[1],
67                         if_id.data4[2], if_id.data4[3],
68                         if_id.data4[4], if_id.data4[5],
69                         if_id.data4[6], if_id.data4[7]);
70         if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, reg_uuid_str, 0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS) {
71                 if (RegQueryValueEx(hKey, NULL, NULL, NULL, (LPBYTE)reg_uuid_name, &uuid_max_size) == ERROR_SUCCESS && uuid_max_size <= MAX_PATH) {
72                         g_snprintf(uuid_name, uuid_name_max_len, "%s", utf_16to8(reg_uuid_name));
73                         RegCloseKey(hKey);
74                         return (int) strlen(uuid_name);
75                 }
76                 RegCloseKey(hKey);
77         }
78         return 0; /* we didn't find anything anyhow. Please don't use the string! */
79
80 }
81 #endif
82
83
84 /* store a guid to name mapping */
85 void
86 guids_add_guid(e_guid_t *guid, const gchar *name)
87 {
88         emem_tree_key_t guidkey[2];
89         guint32 g[4];
90
91         g[0]=guid->data1;
92
93         g[1]=guid->data2;
94         g[1]<<=16;
95         g[1]|=guid->data3;
96
97         g[2]=guid->data4[0];
98         g[2]<<=8;
99         g[2]|=guid->data4[1];
100         g[2]<<=8;
101         g[2]|=guid->data4[2];
102         g[2]<<=8;
103         g[2]|=guid->data4[3];
104
105         g[3]=guid->data4[4];
106         g[3]<<=8;
107         g[3]|=guid->data4[5];
108         g[3]<<=8;
109         g[3]|=guid->data4[6];
110         g[3]<<=8;
111         g[3]|=guid->data4[7];
112
113         guidkey[0].key=g;
114         guidkey[0].length=4;
115         guidkey[1].length=0;
116
117         pe_tree_insert32_array(guid_to_name_tree, &guidkey[0], (gchar *) name);
118 }
119
120
121 /* retrieve the registered name for this GUID */
122 const gchar *
123 guids_get_guid_name(e_guid_t *guid)
124 {
125         emem_tree_key_t guidkey[2];
126         guint32 g[4];
127         char *name;
128 #ifdef _WIN32
129         static char *uuid_name;
130 #endif
131
132         g[0]=guid->data1;
133
134         g[1]=guid->data2;
135         g[1]<<=16;
136         g[1]|=guid->data3;
137
138         g[2]=guid->data4[0];
139         g[2]<<=8;
140         g[2]|=guid->data4[1];
141         g[2]<<=8;
142         g[2]|=guid->data4[2];
143         g[2]<<=8;
144         g[2]|=guid->data4[3];
145
146         g[3]=guid->data4[4];
147         g[3]<<=8;
148         g[3]|=guid->data4[5];
149         g[3]<<=8;
150         g[3]|=guid->data4[6];
151         g[3]<<=8;
152         g[3]|=guid->data4[7];
153
154         guidkey[0].key=g;
155         guidkey[0].length=4;
156         guidkey[1].length=0;
157
158         if((name = pe_tree_lookup32_array(guid_to_name_tree, &guidkey[0]))){
159                 return name;
160         }
161
162 #ifdef _WIN32
163         /* try to resolve the mapping from the Windows registry */
164         /* XXX - prefill the resolving database with all the Windows registry entries once at init only (instead of searching each time)? */
165         uuid_name=ep_alloc(128);
166         if(ResolveWin32UUID(*guid, uuid_name, 128)) {
167                 return uuid_name;
168         }
169 #endif
170
171         return NULL;
172 }
173
174
175 void
176 guids_init(void)
177 {
178         guid_to_name_tree=pe_tree_create(EMEM_TREE_TYPE_RED_BLACK, "guid_to_name");
179         /* XXX here is a good place to read a config file with wellknown guids */
180 }
181
182
183 /* Tries to match a guid against its name.
184    Returns the associated string ptr on a match.
185    Formats uuid number and returns the resulting string, if name is unknown.
186    (derived from val_to_str) */
187 const gchar *
188 guids_resolve_guid_to_str(e_guid_t *guid)
189 {
190         const gchar *name;
191
192         name=guids_get_guid_name(guid);
193         if(name){
194                 return name;
195         }
196
197         return ep_strdup_printf("%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
198                       guid->data1, guid->data2, guid->data3,
199                       guid->data4[0], guid->data4[1],
200                       guid->data4[2], guid->data4[3],
201                       guid->data4[4], guid->data4[5],
202                       guid->data4[6], guid->data4[7]);
203 }
204