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