2 * Common routines for tracking & saving objects found in streams of data
3 * Copyright 2007, Stephen Fisher (see AUTHORS file)
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,
41 #include <epan/packet_info.h>
44 #include <wsutil/file_util.h>
46 #include <ui/alert_box.h>
48 #include "export_object.h"
51 eo_save_entry(const gchar
*save_as_filename
, export_object_entry_t
*entry
, gboolean show_err
)
56 ssize_t bytes_written
;
60 to_fd
= ws_open(save_as_filename
, O_WRONLY
| O_CREAT
| O_EXCL
|
62 if(to_fd
== -1) { /* An error occurred */
64 open_failure_alert_box(save_as_filename
, errno
, TRUE
);
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
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;
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)
93 err
= WTAP_ERR_SHORT_WRITE
;
95 write_failure_alert_box(save_as_filename
, err
);
99 bytes_left
-= bytes_written
;
100 ptr
+= bytes_written
;
102 if (ws_close(to_fd
) < 0) {
104 write_failure_alert_box(save_as_filename
, errno
);
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
)
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
);
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
);
145 eo_massage_str(const gchar
*in_str
, gsize maxlen
, int dupn
)
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";
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
);
179 out_str
= g_string_truncate(out_str
, maxlen
);
182 out_str
= eo_rename(out_str
, dupn
);
187 ct2ext(const char *content_type
)
189 /* TODO: Map the content type string to an extension string. If no match,
200 * indent-tabs-mode: nil
203 * ex: set shiftwidth=4 tabstop=8 expandtab:
204 * :indentSize=4:tabSize=8:noTabs=true: