From 47969704c6eb724ef56eb5d40c5ce3b4e9ef723c Mon Sep 17 00:00:00 2001 From: Ronnie Sahlberg Date: Fri, 18 Aug 2006 09:27:40 +0000 Subject: [PATCH] change the guid mapping code to use a tree isntead of a hashtable svn path=/trunk/; revision=18946 --- epan/guid-utils.c | 225 +++++++++++++++++++++++++--------------------- 1 file changed, 124 insertions(+), 101 deletions(-) diff --git a/epan/guid-utils.c b/epan/guid-utils.c index eb2f8e7f42..0cd36b0ebf 100644 --- a/epan/guid-utils.c +++ b/epan/guid-utils.c @@ -32,6 +32,7 @@ #include #include #include +#include #include "guid-utils.h" #ifdef _WIN32 @@ -39,33 +40,126 @@ #include #endif - -/* GUID "registry" */ -typedef struct _guid_key { - e_guid_t guid; -} guid_key; - -typedef struct _guid_value { - const gchar *name; -} guid_value; +static emem_tree_t *guid_to_name_tree = NULL; -/* global guid to name collection */ -GHashTable *guids = NULL; +/* store a guid to name mapping */ +void +guids_add_guid(e_guid_t *guid, const gchar *name) +{ + emem_tree_key_t guidkey[2]; + guint32 g[4]; + + g[0]=guid->data1; + + g[1]=guid->data2; + g[1]<<=16; + g[1]|=guid->data3; + + g[2]=guid->data4[0]; + g[2]<<=8; + g[2]|=guid->data4[1]; + g[2]<<=8; + g[2]|=guid->data4[2]; + g[2]<<=8; + g[2]|=guid->data4[3]; + + g[3]=guid->data4[4]; + g[3]<<=8; + g[3]|=guid->data4[5]; + g[3]<<=8; + g[3]|=guid->data4[6]; + g[3]<<=8; + g[3]|=guid->data4[7]; + + guidkey[0].key=g; + guidkey[0].length=4; + guidkey[1].length=0; + + pe_tree_insert32_array(guid_to_name_tree, &guidkey[0], name); +} + + +/* retreive the registered name for this GUID */ +const gchar * +guids_get_guid_name(e_guid_t *guid) +{ + emem_tree_key_t guidkey[2]; + guint32 g[4]; + char *name; +#ifdef _WIN32 + static char *uuid_name; +#endif + + g[0]=guid->data1; + + g[1]=guid->data2; + g[1]<<=16; + g[1]|=guid->data3; + + g[2]=guid->data4[0]; + g[2]<<=8; + g[2]|=guid->data4[1]; + g[2]<<=8; + g[2]|=guid->data4[2]; + g[2]<<=8; + g[2]|=guid->data4[3]; + + g[3]=guid->data4[4]; + g[3]<<=8; + g[3]|=guid->data4[5]; + g[3]<<=8; + g[3]|=guid->data4[6]; + g[3]<<=8; + g[3]|=guid->data4[7]; + + guidkey[0].key=g; + guidkey[0].length=4; + guidkey[1].length=0; + + if((name = pe_tree_lookup32_array(guid_to_name_tree, &guidkey[0]))){ + return name; + } + +#ifdef _WIN32 + /* try to resolve the mapping from the Windows registry */ + /* XXX - prefill the resolving database with all the Windows registry entries once at init only (instead of searching each time)? */ + uuid_name=ep_alloc(128); + if(ResolveWin32UUID(*guid, uuid_name, 128)) { + return uuid_name; + } +#endif + + return NULL; +} + + +void +guids_init(void) +{ + guid_to_name_tree=pe_tree_create(EMEM_TREE_TYPE_RED_BLACK, "guid_to_name"); + /* XXX here is a good place to read a config file with wellknown guids */ +} + #ifdef _WIN32 /* try to resolve an DCE/RPC interface name to it's name using the Windows registry entries */ /* XXX - might be better to fill all interfaces into our database at startup instead of searching each time */ -int ResolveWin32UUID(e_guid_t if_id, char *uuid_name, int uuid_name_max_len) +int +ResolveWin32UUID(e_guid_t if_id, char *uuid_name, int uuid_name_max_len) { - TCHAR reg_uuid_name[MAX_PATH]; + TCHAR *reg_uuid_name; HKEY hKey = NULL; DWORD uuid_max_size = MAX_PATH; - TCHAR reg_uuid_str[MAX_PATH]; + TCHAR *reg_uuid_str; - if(uuid_name_max_len < 2) + reg_uuid_name=ep_alloc(MAX_PATH*sizeof(TCHAR)); + reg_uuid_str=ep_alloc(MAX_PATH*sizeof(TCHAR)); + + if(uuid_name_max_len < 2){ return 0; + } reg_uuid_name[0] = '\0'; _snwprintf(reg_uuid_str, MAX_PATH, _T("SOFTWARE\\Classes\\Interface\\{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}"), if_id.data1, if_id.data2, if_id.data3, @@ -73,10 +167,8 @@ int ResolveWin32UUID(e_guid_t if_id, char *uuid_name, int uuid_name_max_len) if_id.data4[2], if_id.data4[3], if_id.data4[4], if_id.data4[5], if_id.data4[6], if_id.data4[7]); - if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, reg_uuid_str, 0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS) - { - if (RegQueryValueEx(hKey, NULL, NULL, NULL, (LPBYTE)reg_uuid_name, &uuid_max_size) == ERROR_SUCCESS && uuid_max_size <= MAX_PATH) - { + if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, reg_uuid_str, 0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS) { + if (RegQueryValueEx(hKey, NULL, NULL, NULL, (LPBYTE)reg_uuid_name, &uuid_max_size) == ERROR_SUCCESS && uuid_max_size <= MAX_PATH) { g_snprintf(uuid_name, uuid_name_max_len, "%s", utf_16to8(reg_uuid_name)); RegCloseKey(hKey); return strlen(uuid_name); @@ -89,99 +181,30 @@ int ResolveWin32UUID(e_guid_t if_id, char *uuid_name, int uuid_name_max_len) #endif + /* Tries to match a guid against its name. Returns the associated string ptr on a match. Formats uuid number and returns the resulting string, if name is unknown. (derived from val_to_str) */ -const gchar* guids_resolve_guid_to_str(e_guid_t *guid) { - const gchar *ret; - static gchar str[3][64]; - static gchar *cur; +const gchar * +guids_resolve_guid_to_str(e_guid_t *guid) +{ + const gchar *name; + + name=guids_get_guid_name(guid); + if(name){ + return name; + } - ret = guids_get_guid_name(guid); - if (ret != NULL) - return ret; - if (cur == &str[0][0]) { - cur = &str[1][0]; - } else if (cur == &str[1][0]) { - cur = &str[2][0]; - } else { - cur = &str[0][0]; - } - g_snprintf(cur, 64, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x", + name=ep_alloc(64); + g_snprintf(name, 64, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x", guid->data1, guid->data2, guid->data3, guid->data4[0], guid->data4[1], guid->data4[2], guid->data4[3], guid->data4[4], guid->data4[5], guid->data4[6], guid->data4[7]); - return cur; + return name; } -static gint -guid_equal (gconstpointer k1, gconstpointer k2) -{ - const guid_key *key1 = (const guid_key *)k1; - const guid_key *key2 = (const guid_key *)k2; - return ((memcmp (&key1->guid, &key2->guid, sizeof (e_guid_t)) == 0)); -} - -static guint -guid_hash (gconstpointer k) -{ - const guid_key *key = (const guid_key *)k; - /* This isn't perfect, but the Data1 part of these is almost always - unique. */ - return key->guid.data1; -} - -void guids_init(void) -{ - g_assert(guids == NULL); - - guids = g_hash_table_new (guid_hash, guid_equal); -} - -void guids_add_guid(e_guid_t *guid, const gchar *name) -{ - guid_key *key = g_malloc (sizeof (*key)); - guid_value *value = g_malloc (sizeof (*value)); - - key->guid = *guid; - - /* XXX - do we need to copy the name? */ - value->name = name; - - g_hash_table_insert (guids, key, value); -} - - -/* try to get registered name for this GUID */ -const gchar *guids_get_guid_name(e_guid_t *guid) -{ - guid_key key; - guid_value *value; -#ifdef _WIN32 - /* XXX - we need three time circulating buffer here */ - /* XXX - is there a maximum length of the name? */ - static char uuid_name[128]; -#endif - - /* try to get registered guid "name" of guid */ - key.guid = *guid; - - if ((value = g_hash_table_lookup (guids, &key)) != NULL) { - return value->name; - } - -#ifdef _WIN32 - /* try to resolve the mapping from the Windows registry */ - /* XXX - prefill the resolving database with all the Windows registry entries once at init only (instead of searching each time)? */ - if(ResolveWin32UUID(*guid, uuid_name, 128)) { - return uuid_name; - } -#endif - - return NULL; -}