GOOSE Messages don't use the length field to perform the dissection.
[obnox/wireshark/wip.git] / epan / guid-utils.c
index eb2f8e7f420ffe533f3ada6ea865b8270f7cf2b3..3e44f46a98e58f2cab5b981287678cf60c170c03 100644 (file)
@@ -31,7 +31,8 @@
 
 #include <glib.h>
 #include <epan/epan.h>
-#include <epan/strutil.h>
+#include <wsutil/unicode-utils.h>
+#include <epan/emem.h>
 #include "guid-utils.h"
 
 #ifdef _WIN32
 #include <windows.h>
 #endif
 
-
-/* GUID "registry" */
-typedef struct _guid_key {
-    e_guid_t guid;
-} guid_key;
-
-typedef struct _guid_value {
-    const gchar *name;
-} guid_value;
-
-
-/* global guid to name collection */
-GHashTable *guids = NULL;
+static emem_tree_t *guid_to_name_tree = NULL;
 
 
 #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;
+
+       reg_uuid_name=ep_alloc(MAX_PATH*sizeof(TCHAR));
+       reg_uuid_str=ep_alloc(MAX_PATH*sizeof(TCHAR));
 
-       if(uuid_name_max_len < 2)
+       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,13 +67,11 @@ 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);
+                       return (int) strlen(uuid_name);
                }
                RegCloseKey(hKey);
        }
@@ -89,99 +81,124 @@ 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;
-
-
-  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",
-                      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;
-}
-
-
-static gint
-guid_equal (gconstpointer k1, gconstpointer k2)
+/* store a guid to name mapping */
+void
+guids_add_guid(e_guid_t *guid, const gchar *name)
 {
-    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));
+       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], (gchar *) name);
 }
 
-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)
+/* retrieve the registered name for this GUID */
+const gchar *
+guids_get_guid_name(e_guid_t *guid)
 {
-    g_assert(guids == NULL);
+       emem_tree_key_t guidkey[2];
+       guint32 g[4];
+       char *name;
+#ifdef _WIN32
+       static char *uuid_name;
+#endif
 
-    guids = g_hash_table_new (guid_hash, guid_equal);
-}
+       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;
+       }
 
-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));
+#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
 
-    key->guid = *guid;
+       return NULL;
+}
 
-    /* XXX - do we need to copy the name? */
-    value->name = name;
 
-    g_hash_table_insert (guids, key, value);
+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 */
 }
 
 
-/* try to get registered name for this GUID */
-const gchar *guids_get_guid_name(e_guid_t *guid)
+/* 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)
 {
-    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;
+       const gchar *name;
 
-    if ((value = g_hash_table_lookup (guids, &key)) != NULL) {
-               return value->name;
+       name=guids_get_guid_name(guid);
+       if(name){
+               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)? */
-    if(ResolveWin32UUID(*guid, uuid_name, 128)) {
-        return uuid_name;
-    }
-#endif
-
-       return NULL;
+       return ep_strdup_printf("%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]);
 }
+