epan/dissectors/packet-xml.c try to decrypt data, but the data doesn't look correct yet
[metze/wireshark/wip.git] / epan / guid-utils.c
1 /* guid-utils.c
2  * GUID handling
3  *
4  * Wireshark - Network traffic analyzer
5  * By Gerald Combs <gerald@wireshark.org>
6  *
7  * Copyright 1998 Gerald Combs
8  *
9  * SPDX-License-Identifier: GPL-2.0-or-later
10  */
11
12 #include "config.h"
13
14 #include <string.h>
15
16 #include <glib.h>
17 #include <epan/epan.h>
18 #include <wsutil/unicode-utils.h>
19 #include <epan/wmem/wmem.h>
20 #include "guid-utils.h"
21
22 #ifdef _WIN32
23 #include <tchar.h>
24 #include <windows.h>
25 #include <strsafe.h>
26 #endif
27
28 static wmem_tree_t *guid_to_name_tree = NULL;
29
30
31 #ifdef _WIN32
32 /* try to resolve an DCE/RPC interface name to its name using the Windows registry entries */
33 /* XXX - might be better to fill all interfaces into our database at startup instead of searching each time */
34 static int
35 ResolveWin32UUID(e_guid_t if_id, char *uuid_name, int uuid_name_max_len)
36 {
37         TCHAR *reg_uuid_name;
38         HKEY hKey = NULL;
39         DWORD uuid_max_size = MAX_PATH;
40         TCHAR *reg_uuid_str;
41
42         reg_uuid_name=wmem_alloc(wmem_packet_scope(), (MAX_PATH*sizeof(TCHAR))+1);
43         reg_uuid_str=wmem_alloc(wmem_packet_scope(), (MAX_PATH*sizeof(TCHAR))+1);
44
45         if(uuid_name_max_len < 2){
46                 return 0;
47         }
48         reg_uuid_name[0] = '\0';
49         StringCchPrintf(reg_uuid_str, MAX_PATH, _T("SOFTWARE\\Classes\\Interface\\{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}"),
50                         if_id.data1, if_id.data2, if_id.data3,
51                         if_id.data4[0], if_id.data4[1],
52                         if_id.data4[2], if_id.data4[3],
53                         if_id.data4[4], if_id.data4[5],
54                         if_id.data4[6], if_id.data4[7]);
55         if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, reg_uuid_str, 0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS) {
56                 if (RegQueryValueEx(hKey, NULL, NULL, NULL, (LPBYTE)reg_uuid_name, &uuid_max_size) == ERROR_SUCCESS && uuid_max_size <= MAX_PATH) {
57                         g_snprintf(uuid_name, uuid_name_max_len, "%s", utf_16to8(reg_uuid_name));
58                         RegCloseKey(hKey);
59                         return (int) strlen(uuid_name);
60                 }
61                 RegCloseKey(hKey);
62         }
63         return 0; /* we didn't find anything anyhow. Please don't use the string! */
64
65 }
66 #endif
67
68
69 /* store a guid to name mapping */
70 void
71 guids_add_guid(const e_guid_t *guid, const gchar *name)
72 {
73         wmem_tree_key_t guidkey[2];
74         guint32 g[4];
75
76         g[0]=guid->data1;
77
78         g[1]=guid->data2;
79         g[1]<<=16;
80         g[1]|=guid->data3;
81
82         g[2]=guid->data4[0];
83         g[2]<<=8;
84         g[2]|=guid->data4[1];
85         g[2]<<=8;
86         g[2]|=guid->data4[2];
87         g[2]<<=8;
88         g[2]|=guid->data4[3];
89
90         g[3]=guid->data4[4];
91         g[3]<<=8;
92         g[3]|=guid->data4[5];
93         g[3]<<=8;
94         g[3]|=guid->data4[6];
95         g[3]<<=8;
96         g[3]|=guid->data4[7];
97
98         guidkey[0].key=g;
99         guidkey[0].length=4;
100         guidkey[1].length=0;
101
102         wmem_tree_insert32_array(guid_to_name_tree, &guidkey[0], (gchar *) name);
103 }
104
105
106 /* retrieve the registered name for this GUID */
107 const gchar *
108 guids_get_guid_name(const e_guid_t *guid)
109 {
110         wmem_tree_key_t guidkey[2];
111         guint32 g[4];
112         char *name;
113 #ifdef _WIN32
114         static char *uuid_name;
115 #endif
116
117         g[0]=guid->data1;
118
119         g[1]=guid->data2;
120         g[1]<<=16;
121         g[1]|=guid->data3;
122
123         g[2]=guid->data4[0];
124         g[2]<<=8;
125         g[2]|=guid->data4[1];
126         g[2]<<=8;
127         g[2]|=guid->data4[2];
128         g[2]<<=8;
129         g[2]|=guid->data4[3];
130
131         g[3]=guid->data4[4];
132         g[3]<<=8;
133         g[3]|=guid->data4[5];
134         g[3]<<=8;
135         g[3]|=guid->data4[6];
136         g[3]<<=8;
137         g[3]|=guid->data4[7];
138
139         guidkey[0].key=g;
140         guidkey[0].length=4;
141         guidkey[1].length=0;
142
143         if((name = (char *)wmem_tree_lookup32_array(guid_to_name_tree, &guidkey[0]))){
144                 return name;
145         }
146
147 #ifdef _WIN32
148         /* try to resolve the mapping from the Windows registry */
149         /* XXX - prefill the resolving database with all the Windows registry entries once at init only (instead of searching each time)? */
150         uuid_name=wmem_alloc(wmem_packet_scope(), 128);
151         if(ResolveWin32UUID(*guid, uuid_name, 128)) {
152                 return uuid_name;
153         }
154 #endif
155
156         return NULL;
157 }
158
159
160 void
161 guids_init(void)
162 {
163         guid_to_name_tree=wmem_tree_new(wmem_epan_scope());
164         /* XXX here is a good place to read a config file with wellknown guids */
165 }
166
167
168 /* Tries to match a guid against its name.
169    Returns the associated string ptr on a match.
170    Formats uuid number and returns the resulting string, if name is unknown.
171    (derived from val_to_str) */
172 const gchar *
173 guids_resolve_guid_to_str(const e_guid_t *guid)
174 {
175         const gchar *name;
176
177         name=guids_get_guid_name(guid);
178         if(name){
179                 return name;
180         }
181
182         return wmem_strdup_printf(wmem_packet_scope(), "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
183                                 guid->data1, guid->data2, guid->data3,
184                                 guid->data4[0], guid->data4[1],
185                                 guid->data4[2], guid->data4[3],
186                                 guid->data4[4], guid->data4[5],
187                                 guid->data4[6], guid->data4[7]);
188 }
189
190 int guid_cmp(const e_guid_t *g1, const e_guid_t *g2)
191 {
192         if (g1->data1 != g2->data1) {
193                 return (g1->data1 < g2->data1) ? -1 : 1;
194         }
195
196         if (g1->data2 != g2->data2) {
197                 return (g1->data2 < g2->data2) ? -1 : 1;
198         }
199
200         if (g1->data3 != g2->data3) {
201                 return (g1->data3 < g2->data3) ? -1 : 1;
202         }
203
204         return memcmp(&g1->data4[0], &g2->data4[0], 8);
205 }
206
207 /*
208  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
209  *
210  * Local variables:
211  * c-basic-offset: 8
212  * tab-width: 8
213  * indent-tabs-mode: t
214  * End:
215  *
216  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
217  * :indentSize=8:tabSize=8:noTabs=false:
218  */