no krb5_cksumtype_to_string
[metze/wireshark/wip.git] / epan / export_object.c
1 /* export_object.c
2  * GUI independent helper routines common to all export object taps.
3  *
4  * Wireshark - Network traffic analyzer
5  * By Gerald Combs <gerald@wireshark.org>
6  * Copyright 1998 Gerald Combs
7  *
8  * SPDX-License-Identifier: GPL-2.0-or-later
9  */
10
11 #include "config.h"
12
13 #include <string.h>
14
15 #include "proto.h"
16 #include "packet_info.h"
17 #include "export_object.h"
18
19 struct register_eo {
20     int proto_id;                        /* protocol id (0-indexed) */
21     const char* tap_listen_str;          /* string used in register_tap_listener (NULL to use protocol name) */
22     tap_packet_cb eo_func;               /* function to be called for new incoming packets for SRT */
23     export_object_gui_reset_cb reset_cb; /* function to parse parameters of optional arguments of tap string */
24 };
25
26 static wmem_tree_t *registered_eo_tables = NULL;
27
28 int
29 register_export_object(const int proto_id, tap_packet_cb export_packet_func, export_object_gui_reset_cb reset_cb)
30 {
31     register_eo_t *table;
32     DISSECTOR_ASSERT(export_packet_func);
33
34     table = wmem_new(wmem_epan_scope(), register_eo_t);
35
36     table->proto_id      = proto_id;
37     table->tap_listen_str = wmem_strdup_printf(wmem_epan_scope(), "%s_eo", proto_get_protocol_filter_name(proto_id));
38     table->eo_func = export_packet_func;
39     table->reset_cb = reset_cb;
40
41     if (registered_eo_tables == NULL)
42         registered_eo_tables = wmem_tree_new(wmem_epan_scope());
43
44     wmem_tree_insert_string(registered_eo_tables, proto_get_protocol_filter_name(proto_id), table, 0);
45     return register_tap(table->tap_listen_str);
46 }
47
48 int get_eo_proto_id(register_eo_t* eo)
49 {
50     if (!eo) {
51         return -1;
52     }
53     return eo->proto_id;
54 }
55
56 const char* get_eo_tap_listener_name(register_eo_t* eo)
57 {
58     return eo->tap_listen_str;
59 }
60
61 tap_packet_cb get_eo_packet_func(register_eo_t* eo)
62 {
63     return eo->eo_func;
64 }
65
66 export_object_gui_reset_cb get_eo_reset_func(register_eo_t* eo)
67 {
68     return eo->reset_cb;
69 }
70
71 register_eo_t* get_eo_by_name(const char* name)
72 {
73     return (register_eo_t*)wmem_tree_lookup_string(registered_eo_tables, name, 0);
74 }
75
76 void eo_iterate_tables(wmem_foreach_func func, gpointer user_data)
77 {
78     wmem_tree_foreach(registered_eo_tables, func, user_data);
79 }
80
81 static GString *eo_rename(GString *gstr, gsize maxlen, int dupn)
82 {
83     GString *gstr_tmp;
84     gchar *tmp_ptr;
85     GString *ext_str;
86
87     gstr_tmp = g_string_new("");
88     if (dupn != 0) {
89         g_string_append_printf (gstr_tmp, "(%d)", dupn);
90     }
91     if ( (tmp_ptr = strrchr(gstr->str, '.')) != NULL && ((ext_str = g_string_new(tmp_ptr))->len + strlen(gstr_tmp->str) < maxlen) ) {
92         /* Retain the extension */
93         gstr = g_string_truncate(gstr, gstr->len - ext_str->len);
94         if ( gstr->len >= (maxlen - (strlen(gstr_tmp->str) + ext_str->len)) )
95             gstr = g_string_truncate(gstr, maxlen - (strlen(gstr_tmp->str) + ext_str->len));
96         gstr = g_string_append(gstr, gstr_tmp->str);
97         gstr = g_string_append(gstr, ext_str->str);
98         g_string_free(ext_str, TRUE);
99     }
100     else {
101         if ( gstr->len >= (maxlen - strlen(gstr_tmp->str)) )
102             gstr = g_string_truncate(gstr, maxlen - strlen(gstr_tmp->str));
103         gstr = g_string_append(gstr, gstr_tmp->str);
104     }
105     g_string_free(gstr_tmp, TRUE);
106     return gstr;
107 }
108
109 GString *
110 eo_massage_str(const gchar *in_str, gsize maxlen, int dupn)
111 {
112     gchar *tmp_ptr;
113     /* The characters in "reject" come from:
114      * http://msdn.microsoft.com/en-us/library/aa365247%28VS.85%29.aspx.
115      * Add to the list as necessary for other OS's.
116      */
117     const gchar *reject = "<>:\"/\\|?*"
118         "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a"
119     "\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14"
120     "\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f";
121     GString *out_str;
122
123     out_str = g_string_new("");
124
125     /* Find all disallowed characters/bytes and replace them with %xx */
126     while ( (tmp_ptr = strpbrk(in_str, reject)) != NULL ) {
127         out_str = g_string_append_len(out_str, in_str, tmp_ptr - in_str);
128         g_string_append_printf(out_str, "%%%02x", *tmp_ptr);
129         in_str = tmp_ptr + 1;
130     }
131     out_str = g_string_append(out_str, in_str);
132     if ( dupn != 0 || out_str->len > maxlen )
133         out_str = eo_rename(out_str, maxlen, dupn);
134     return out_str;
135 }
136
137 const char *
138 eo_ct2ext(const char *content_type)
139 {
140     /* TODO: Map the content type string to an extension string.  If no match,
141      * return NULL. */
142     return content_type;
143 }
144
145 void eo_free_entry(export_object_entry_t *entry)
146 {
147     g_free(entry->hostname);
148     g_free(entry->content_type);
149     g_free(entry->filename);
150     g_free(entry->payload_data);
151
152     g_free(entry);
153 }
154
155 /*
156  * Editor modelines
157  *
158  * Local Variables:
159  * c-basic-offset: 4
160  * tab-width: 8
161  * indent-tabs-mode: nil
162  * End:
163  *
164  * ex: set shiftwidth=4 tabstop=8 expandtab:
165  * :indentSize=4:tabSize=8:noTabs=true:
166  */