wslua: fix nstime memory leak after passing unknown encoding to TvbRange_nstime()
[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  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 #include "config.h"
24
25 #include <string.h>
26
27 #include "proto.h"
28 #include "packet_info.h"
29 #include "export_object.h"
30
31 struct register_eo {
32     int proto_id;                        /* protocol id (0-indexed) */
33     const char* tap_listen_str;          /* string used in register_tap_listener (NULL to use protocol name) */
34     tap_packet_cb eo_func;               /* function to be called for new incoming packets for SRT */
35     export_object_gui_reset_cb reset_cb; /* function to parse parameters of optional arguments of tap string */
36 };
37
38 static wmem_tree_t *registered_eo_tables = NULL;
39
40 int
41 register_export_object(const int proto_id, tap_packet_cb export_packet_func, export_object_gui_reset_cb reset_cb)
42 {
43     register_eo_t *table;
44     DISSECTOR_ASSERT(export_packet_func);
45
46     table = wmem_new(wmem_epan_scope(), register_eo_t);
47
48     table->proto_id      = proto_id;
49     table->tap_listen_str = wmem_strdup_printf(wmem_epan_scope(), "%s_eo", proto_get_protocol_filter_name(proto_id));
50     table->eo_func = export_packet_func;
51     table->reset_cb = reset_cb;
52
53     if (registered_eo_tables == NULL)
54         registered_eo_tables = wmem_tree_new(wmem_epan_scope());
55
56     wmem_tree_insert_string(registered_eo_tables, proto_get_protocol_filter_name(proto_id), table, 0);
57     return register_tap(table->tap_listen_str);
58 }
59
60 int get_eo_proto_id(register_eo_t* eo)
61 {
62     if (!eo) {
63         return -1;
64     }
65     return eo->proto_id;
66 }
67
68 const char* get_eo_tap_listener_name(register_eo_t* eo)
69 {
70     return eo->tap_listen_str;
71 }
72
73 tap_packet_cb get_eo_packet_func(register_eo_t* eo)
74 {
75     return eo->eo_func;
76 }
77
78 export_object_gui_reset_cb get_eo_reset_func(register_eo_t* eo)
79 {
80     return eo->reset_cb;
81 }
82
83 register_eo_t* get_eo_by_name(const char* name)
84 {
85     return (register_eo_t*)wmem_tree_lookup_string(registered_eo_tables, name, 0);
86 }
87
88 void eo_iterate_tables(wmem_foreach_func func, gpointer user_data)
89 {
90     wmem_tree_foreach(registered_eo_tables, func, user_data);
91 }
92
93 static GString *eo_rename(GString *gstr, gsize maxlen, int dupn)
94 {
95     GString *gstr_tmp;
96     gchar *tmp_ptr;
97     GString *ext_str;
98
99     gstr_tmp = g_string_new("");
100     if (dupn != 0) {
101         g_string_append_printf (gstr_tmp, "(%d)", dupn);
102     }
103     if ( (tmp_ptr = strrchr(gstr->str, '.')) != NULL && ((ext_str = g_string_new(tmp_ptr))->len + strlen(gstr_tmp->str) < maxlen) ) {
104         /* Retain the extension */
105         gstr = g_string_truncate(gstr, gstr->len - ext_str->len);
106         if ( gstr->len >= (maxlen - (strlen(gstr_tmp->str) + ext_str->len)) )
107             gstr = g_string_truncate(gstr, maxlen - (strlen(gstr_tmp->str) + ext_str->len));
108         gstr = g_string_append(gstr, gstr_tmp->str);
109         gstr = g_string_append(gstr, ext_str->str);
110         g_string_free(ext_str, TRUE);
111     }
112     else {
113         if ( gstr->len >= (maxlen - strlen(gstr_tmp->str)) )
114             gstr = g_string_truncate(gstr, maxlen - strlen(gstr_tmp->str));
115         gstr = g_string_append(gstr, gstr_tmp->str);
116     }
117     g_string_free(gstr_tmp, TRUE);
118     return gstr;
119 }
120
121 GString *
122 eo_massage_str(const gchar *in_str, gsize maxlen, int dupn)
123 {
124     gchar *tmp_ptr;
125     /* The characters in "reject" come from:
126      * http://msdn.microsoft.com/en-us/library/aa365247%28VS.85%29.aspx.
127      * Add to the list as necessary for other OS's.
128      */
129     const gchar *reject = "<>:\"/\\|?*"
130         "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a"
131     "\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14"
132     "\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f";
133     GString *out_str;
134
135     out_str = g_string_new("");
136
137     /* Find all disallowed characters/bytes and replace them with %xx */
138     while ( (tmp_ptr = strpbrk(in_str, reject)) != NULL ) {
139         out_str = g_string_append_len(out_str, in_str, tmp_ptr - in_str);
140         g_string_append_printf(out_str, "%%%02x", *tmp_ptr);
141         in_str = tmp_ptr + 1;
142     }
143     out_str = g_string_append(out_str, in_str);
144     if ( dupn != 0 || out_str->len > maxlen )
145         out_str = eo_rename(out_str, maxlen, dupn);
146     return out_str;
147 }
148
149 const char *
150 eo_ct2ext(const char *content_type)
151 {
152     /* TODO: Map the content type string to an extension string.  If no match,
153      * return NULL. */
154     return content_type;
155 }
156
157 void eo_free_entry(export_object_entry_t *entry)
158 {
159     g_free(entry->hostname);
160     g_free(entry->content_type);
161     g_free(entry->filename);
162     g_free(entry->payload_data);
163
164     g_free(entry);
165 }
166
167 /*
168  * Editor modelines
169  *
170  * Local Variables:
171  * c-basic-offset: 4
172  * tab-width: 8
173  * indent-tabs-mode: nil
174  * End:
175  *
176  * ex: set shiftwidth=4 tabstop=8 expandtab:
177  * :indentSize=4:tabSize=8:noTabs=true:
178  */