MSWSP: add parse_CNatLanguageRestriction()
[wireshark-wip.git] / ui / export_object.c
blob5a07651075b7addf4b599604c611a52243d4bb18
1 /* export_object.c
2 * Common routines for tracking & saving objects found in streams of data
3 * Copyright 2007, Stephen Fisher (see AUTHORS file)
5 * $Id$
7 * Wireshark - Network traffic analyzer
8 * By Gerald Combs <gerald@wireshark.org>
9 * Copyright 1998 Gerald Combs
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
24 * USA.
27 #include "config.h"
29 #include <string.h>
31 #ifdef HAVE_FCNTL_H
32 #include <fcntl.h>
33 #endif
35 #ifdef HAVE_UNISTD_H
36 #include <unistd.h>
37 #endif
39 #include <errno.h>
41 #include <epan/packet_info.h>
42 #include <epan/tap.h>
44 #include <wsutil/file_util.h>
46 #include <ui/alert_box.h>
48 #include "export_object.h"
50 gboolean
51 eo_save_entry(const gchar *save_as_filename, export_object_entry_t *entry, gboolean show_err)
53 int to_fd;
54 gint64 bytes_left;
55 int bytes_to_write;
56 ssize_t bytes_written;
57 guint8 *ptr;
58 int err;
60 to_fd = ws_open(save_as_filename, O_WRONLY | O_CREAT | O_EXCL |
61 O_BINARY, 0644);
62 if(to_fd == -1) { /* An error occurred */
63 if (show_err)
64 open_failure_alert_box(save_as_filename, errno, TRUE);
65 return FALSE;
69 * The third argument to _write() on Windows is an unsigned int,
70 * so, on Windows, that's the size of the third argument to
71 * ws_write().
73 * The third argument to write() on UN*X is a size_t, although
74 * the return value is an ssize_t, so one probably shouldn't
75 * write more than the max value of an ssize_t.
77 * In either case, there's no guarantee that a gint64 such as
78 * payload_len can be passed to ws_write(), so we write in
79 * chunks of, at most 2^31 bytes.
81 ptr = entry->payload_data;
82 bytes_left = entry->payload_len;
83 while (bytes_left != 0) {
84 if (bytes_left > 0x40000000)
85 bytes_to_write = 0x40000000;
86 else
87 bytes_to_write = (int)bytes_left;
88 bytes_written = ws_write(to_fd, ptr, bytes_to_write);
89 if(bytes_written <= 0) {
90 if (bytes_written < 0)
91 err = errno;
92 else
93 err = WTAP_ERR_SHORT_WRITE;
94 if (show_err)
95 write_failure_alert_box(save_as_filename, err);
96 ws_close(to_fd);
97 return FALSE;
99 bytes_left -= bytes_written;
100 ptr += bytes_written;
102 if (ws_close(to_fd) < 0) {
103 if (show_err)
104 write_failure_alert_box(save_as_filename, errno);
105 return FALSE;
108 return TRUE;
112 #define HINIBBLE(x) (((x) >> 4) & 0xf)
113 #define LONIBBLE(x) ((x) & 0xf)
114 #define HEXTOASCII(x) (((x) < 10) ? ((x) + '0') : ((x) - 10 + 'a'))
115 #define MAXFILELEN 255
117 static GString *eo_rename(GString *gstr, int dupn)
119 GString *gstr_tmp;
120 gchar *tmp_ptr;
121 GString *ext_str;
123 gstr_tmp = g_string_new("(");
124 g_string_append_printf (gstr_tmp, "%d)", dupn);
125 if ( (tmp_ptr = strrchr(gstr->str, '.')) != NULL ) {
126 /* Retain the extension */
127 ext_str = g_string_new(tmp_ptr);
128 gstr = g_string_truncate(gstr, gstr->len - ext_str->len);
129 if ( gstr->len >= (MAXFILELEN - (strlen(gstr_tmp->str) + ext_str->len)) )
130 gstr = g_string_truncate(gstr, MAXFILELEN - (strlen(gstr_tmp->str) + ext_str->len));
131 gstr = g_string_append(gstr, gstr_tmp->str);
132 gstr = g_string_append(gstr, ext_str->str);
133 g_string_free(ext_str, TRUE);
135 else {
136 if ( gstr->len >= (MAXFILELEN - strlen(gstr_tmp->str)) )
137 gstr = g_string_truncate(gstr, MAXFILELEN - strlen(gstr_tmp->str));
138 gstr = g_string_append(gstr, gstr_tmp->str);
140 g_string_free(gstr_tmp, TRUE);
141 return gstr;
144 GString *
145 eo_massage_str(const gchar *in_str, gsize maxlen, int dupn)
147 gchar *tmp_ptr;
148 /* The characters in "reject" come from:
149 * http://msdn.microsoft.com/en-us/library/aa365247%28VS.85%29.aspx.
150 * Add to the list as necessary for other OS's.
152 const gchar *reject = "<>:\"/\\|?*"
153 "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a"
154 "\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14"
155 "\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f";
156 GString *out_str;
157 GString *ext_str;
159 out_str = g_string_new("");
161 /* Find all disallowed characters/bytes and replace them with %xx */
162 while ( (tmp_ptr = strpbrk(in_str, reject)) != NULL ) {
163 out_str = g_string_append_len(out_str, in_str, tmp_ptr - in_str);
164 out_str = g_string_append_c(out_str, '%');
165 out_str = g_string_append_c(out_str, HEXTOASCII(HINIBBLE(*tmp_ptr)));
166 out_str = g_string_append_c(out_str, HEXTOASCII(LONIBBLE(*tmp_ptr)));
167 in_str = tmp_ptr + 1;
169 out_str = g_string_append(out_str, in_str);
170 if ( out_str->len > maxlen ) {
171 if ( (tmp_ptr = strrchr(out_str->str, '.')) != NULL ) {
172 /* Retain the extension */
173 ext_str = g_string_new(tmp_ptr);
174 out_str = g_string_truncate(out_str, maxlen - ext_str->len);
175 out_str = g_string_append(out_str, ext_str->str);
176 g_string_free(ext_str, TRUE);
178 else
179 out_str = g_string_truncate(out_str, maxlen);
181 if ( dupn != 0 )
182 out_str = eo_rename(out_str, dupn);
183 return out_str;
186 const char *
187 ct2ext(const char *content_type)
189 /* TODO: Map the content type string to an extension string. If no match,
190 * return NULL. */
191 return content_type;
195 * Editor modelines
197 * Local Variables:
198 * c-basic-offset: 4
199 * tab-width: 8
200 * indent-tabs-mode: nil
201 * End:
203 * ex: set shiftwidth=4 tabstop=8 expandtab:
204 * :indentSize=4:tabSize=8:noTabs=true: