From robionekenobi via https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=9375 :
[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  * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24  */
25
26 #include "config.h"
27
28 #include <string.h>
29
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"
36
37 #ifdef _WIN32
38 #include <tchar.h>
39 #include <windows.h>
40 #endif
41
42 static wmem_tree_t *guid_to_name_tree = NULL;
43
44
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)
50 {
51         TCHAR *reg_uuid_name;
52         HKEY hKey = NULL;
53         DWORD uuid_max_size = MAX_PATH;
54         TCHAR *reg_uuid_str;
55
56         reg_uuid_name=ep_alloc(MAX_PATH*sizeof(TCHAR));
57         reg_uuid_str=ep_alloc(MAX_PATH*sizeof(TCHAR));
58
59         if(uuid_name_max_len < 2){
60                 return 0;
61         }
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);
74                 }
75                 RegCloseKey(hKey);
76         }
77         return 0; /* we didn't find anything anyhow. Please don't use the string! */
78
79 }
80 #endif
81
82
83 /* store a guid to name mapping */
84 void
85 guids_add_guid(e_guid_t *guid, const gchar *name)
86 {
87         wmem_tree_key_t guidkey[2];
88         guint32 g[4];
89
90         g[0]=guid->data1;
91
92         g[1]=guid->data2;
93         g[1]<<=16;
94         g[1]|=guid->data3;
95
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];
103
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];
111
112         guidkey[0].key=g;
113         guidkey[0].length=4;
114         guidkey[1].length=0;
115
116         wmem_tree_insert32_array(guid_to_name_tree, &guidkey[0], (gchar *) name);
117 }
118
119
120 /* retrieve the registered name for this GUID */
121 const gchar *
122 guids_get_guid_name(e_guid_t *guid)
123 {
124         wmem_tree_key_t guidkey[2];
125         guint32 g[4];
126         char *name;
127 #ifdef _WIN32
128         static char *uuid_name;
129 #endif
130
131         g[0]=guid->data1;
132
133         g[1]=guid->data2;
134         g[1]<<=16;
135         g[1]|=guid->data3;
136
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];
144
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];
152
153         guidkey[0].key=g;
154         guidkey[0].length=4;
155         guidkey[1].length=0;
156
157         if((name = (char *)wmem_tree_lookup32_array(guid_to_name_tree, &guidkey[0]))){
158                 return name;
159         }
160
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;
167         }
168 #endif
169
170         return NULL;
171 }
172
173
174 void
175 guids_init(void)
176 {
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 */
179 }
180
181
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)
188 {
189         const gchar *name;
190
191         name=guids_get_guid_name(guid);
192         if(name){
193                 return name;
194         }
195
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]);
202 }
203