TODO epan/dissectors/asn1/kerberos/packet-kerberos-template.c new GSS flags
[wireshark-sm.git] / wsutil / codecs.c
blobdfd5c0e016c3ae8eb9d6f246b3e11028af25b039
1 /* codecs.c
2 * codecs interface 2007 Tomas Kukosa
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 1998 Gerald Combs
8 * SPDX-License-Identifier: GPL-2.0-or-later
9 */
11 #include "config.h"
13 #include "codecs.h"
15 #include <wsutil/wslog.h>
16 #ifdef HAVE_PLUGINS
17 #include <wsutil/plugins.h>
18 #endif
20 #ifdef HAVE_PLUGINS
21 static plugins_t *libwscodecs_plugins;
22 #endif
24 static GSList *codecs_plugins;
26 #ifdef HAVE_PLUGINS
27 void
28 codecs_register_plugin(const codecs_plugin *plug)
30 codecs_plugins = g_slist_prepend(codecs_plugins, (codecs_plugin *)plug);
32 #else /* HAVE_PLUGINS */
33 void
34 codecs_register_plugin(const codecs_plugin *plug _U_)
36 ws_warning("codecs_register_plugin: built without support for binary plugins");
38 #endif /* HAVE_PLUGINS */
40 static void
41 call_plugin_register_codec_module(void * data, void * user_data _U_)
43 codecs_plugin *plug = (codecs_plugin *)data;
45 if (plug->register_codec_module) {
46 plug->register_codec_module();
52 * For all codec plugins, call their register routines.
54 void
55 codecs_init(void)
57 #ifdef HAVE_PLUGINS
58 libwscodecs_plugins = plugins_init(WS_PLUGIN_CODEC);
59 #endif
60 g_slist_foreach(codecs_plugins, call_plugin_register_codec_module, NULL);
63 void
64 codecs_cleanup(void)
66 g_slist_free(codecs_plugins);
67 codecs_plugins = NULL;
68 #ifdef HAVE_PLUGINS
69 plugins_cleanup(libwscodecs_plugins);
70 libwscodecs_plugins = NULL;
71 #endif
75 struct codec_handle {
76 const char *name;
77 codec_init_fn init_fn;
78 codec_release_fn release_fn;
79 codec_get_channels_fn channels_fn;
80 codec_get_frequency_fn frequency_fn;
81 codec_decode_fn decode_fn;
85 * List of registered codecs.
87 static GHashTable *registered_codecs;
90 /* Find a registered codec by name. */
91 codec_handle_t
92 find_codec(const char *name)
94 codec_handle_t ret;
95 char *key = g_ascii_strup(name, -1);
97 ret = (registered_codecs) ? (codec_handle_t)g_hash_table_lookup(registered_codecs, key) : NULL;
98 g_free(key);
99 return ret;
102 /* Register a codec by name. */
103 bool
104 register_codec(const char *name, codec_init_fn init_fn, codec_release_fn release_fn,
105 codec_get_channels_fn channels_fn, codec_get_frequency_fn frequency_fn,
106 codec_decode_fn decode_fn)
108 struct codec_handle *handle;
109 char *key;
111 /* Create our hash table if it doesn't already exist */
112 if (registered_codecs == NULL)
113 registered_codecs = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
115 /* RFC 4855 3. Mapping to SDP Parameters "Note that the payload format
116 * (encoding) names... are commonly shown in upper case. These names
117 * are case-insensitive in both places."
119 key = g_ascii_strup(name, -1);
121 /* Make sure the registration is unique */
122 if (g_hash_table_lookup(registered_codecs, key) != NULL) {
123 g_free(key);
124 return false; /* report an error, or have our caller do it? */
127 handle = g_new(struct codec_handle, 1);
128 handle->name = name;
129 handle->init_fn = init_fn;
130 handle->release_fn = release_fn;
131 handle->channels_fn = channels_fn;
132 handle->frequency_fn = frequency_fn;
133 handle->decode_fn = decode_fn;
135 g_hash_table_insert(registered_codecs, (void *)key, (void *) handle);
136 return true;
139 /* Deregister a codec by name. */
140 bool
141 deregister_codec(const char *name)
143 bool ret = false;
144 if (registered_codecs) {
145 char *key = g_ascii_strup(name, -1);
147 ret = g_hash_table_remove(registered_codecs, key);
148 g_free(key);
150 return ret;
153 void *codec_init(codec_handle_t codec, codec_context_t *context)
155 if (!codec) return NULL;
156 return (codec->init_fn)(context);
159 void codec_release(codec_handle_t codec, codec_context_t *context)
161 if (!codec) return;
162 (codec->release_fn)(context);
165 unsigned codec_get_channels(codec_handle_t codec, codec_context_t *context)
167 if (!codec) return 0;
168 return (codec->channels_fn)(context);
171 unsigned codec_get_frequency(codec_handle_t codec, codec_context_t *context)
173 if (!codec) return 0;
174 return (codec->frequency_fn)(context);
177 size_t codec_decode(codec_handle_t codec, codec_context_t *context, const void *input, size_t inputSizeBytes, void *output, size_t *outputSizeBytes)
179 if (!codec) return 0;
180 return (codec->decode_fn)(context, input, inputSizeBytes, output, outputSizeBytes);
184 * Editor modelines - https://www.wireshark.org/tools/modelines.html
186 * Local variables:
187 * c-basic-offset: 4
188 * tab-width: 8
189 * indent-tabs-mode: nil
190 * End:
192 * vi: set shiftwidth=4 tabstop=8 expandtab:
193 * :indentSize=4:tabSize=8:noTabs=true: