Preparations for updated CMIP dissector:
[obnox/wireshark/wip.git] / epan / oid_resolv.c
1 /* oid_resolv.c
2  * Routines for OBJECT IDENTIFIER name resolution
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <glib.h>
30
31 #include "oid_resolv.h"
32 #include "to_str.h"
33 #include "strutil.h"
34 #include "epan/dissectors/format-oid.h"
35
36 static GHashTable *oid_table = NULL;
37
38 void oid_resolv_init(void) {
39   oid_table = g_hash_table_new(g_str_hash, g_str_equal);
40 }
41
42 static void free_oid_str(gpointer key, gpointer value _U_,
43                          gpointer user_data _U_) {
44   g_free(key);
45 }
46
47 void oid_resolv_cleanup(void) {
48   g_hash_table_foreach(oid_table, free_oid_str, NULL);
49   g_hash_table_destroy(oid_table);
50   oid_table = NULL;
51 }
52
53 gboolean oid_resolv_enabled(void) {
54   return TRUE;
55 }
56
57 const gchar *get_oid_name(const guint8 *oid, gint oid_len) {
58   const gchar *name;
59   subid_t *subid_oid;
60   guint subid_oid_length;
61   gchar *decoded_oid;
62   gchar *non_decoded_oid;
63
64   name = g_hash_table_lookup(oid_table, oid_to_str(oid, oid_len));
65   if (name) return name;
66   subid_oid = g_malloc((oid_len+1) * sizeof(gulong));
67   subid_oid_length = oid_to_subid_buf(oid, oid_len, subid_oid, ((oid_len+1) * sizeof(gulong)));
68   new_format_oid(subid_oid, subid_oid_length, &non_decoded_oid, &decoded_oid);
69   g_free(subid_oid);
70   return decoded_oid;
71 }
72
73 const gchar *get_oid_str_name(const gchar *oid_str) {
74   const gchar *name;
75   GByteArray *bytes;
76   gboolean res;
77
78   bytes = g_byte_array_new();
79   res = oid_str_to_bytes(oid_str, bytes);
80   if (!res)  {
81     /* just try a direct lookup - this allows backward compatibility
82        with non-OIDs used for X.411 standard extensions and DISP initiators */
83     return g_hash_table_lookup(oid_table, oid_str);
84   }
85   name = get_oid_name(bytes->data, bytes->len);
86   g_byte_array_free(bytes, TRUE);
87   return name;
88 }
89
90
91 extern void add_oid_name(const guint8 *oid, gint oid_len, const gchar *name) {
92   add_oid_str_name(oid_to_str(oid, oid_len), name);
93 }
94
95 extern void add_oid_str_name(const gchar *oid_str, const gchar *name) {
96   g_hash_table_insert(oid_table, (gpointer)g_strdup(oid_str), (gpointer)name);
97 }