replace SPDX identifier GPL-2.0+ with GPL-2.0-or-later.
[metze/wireshark/wip.git] / ui / export_object_ui.c
1 /* export_object_ui.c
2  * Common routines for tracking & saving objects found in streams of data
3  * Copyright 2007, Stephen Fisher (see AUTHORS file)
4  *
5  * Wireshark - Network traffic analyzer
6  * By Gerald Combs <gerald@wireshark.org>
7  * Copyright 1998 Gerald Combs
8  *
9  * SPDX-License-Identifier: GPL-2.0-or-later */
10
11 #include "config.h"
12
13 #include <string.h>
14
15 #include <errno.h>
16
17 #include <glib.h>
18
19 #include <epan/packet_info.h>
20 #include <epan/tap.h>
21
22 #include <wiretap/wtap.h>
23
24 #include <wsutil/file_util.h>
25
26 #include <ui/alert_box.h>
27
28 #include "export_object_ui.h"
29
30 gboolean
31 eo_save_entry(const gchar *save_as_filename, export_object_entry_t *entry, gboolean show_err)
32 {
33     int to_fd;
34     gint64 bytes_left;
35     int bytes_to_write;
36     ssize_t bytes_written;
37     guint8 *ptr;
38     int err;
39
40     to_fd = ws_open(save_as_filename, O_WRONLY | O_CREAT | O_EXCL |
41              O_BINARY, 0644);
42     if(to_fd == -1) { /* An error occurred */
43         if (show_err)
44             open_failure_alert_box(save_as_filename, errno, TRUE);
45         return FALSE;
46     }
47
48     /*
49      * The third argument to _write() on Windows is an unsigned int,
50      * so, on Windows, that's the size of the third argument to
51      * ws_write().
52      *
53      * The third argument to write() on UN*X is a size_t, although
54      * the return value is an ssize_t, so one probably shouldn't
55      * write more than the max value of an ssize_t.
56      *
57      * In either case, there's no guarantee that a gint64 such as
58      * payload_len can be passed to ws_write(), so we write in
59      * chunks of, at most 2^31 bytes.
60      */
61     ptr = entry->payload_data;
62     bytes_left = entry->payload_len;
63     while (bytes_left != 0) {
64         if (bytes_left > 0x40000000)
65             bytes_to_write = 0x40000000;
66         else
67             bytes_to_write = (int)bytes_left;
68         bytes_written = ws_write(to_fd, ptr, bytes_to_write);
69         if(bytes_written <= 0) {
70             if (bytes_written < 0)
71                 err = errno;
72             else
73                 err = WTAP_ERR_SHORT_WRITE;
74             if (show_err)
75                 write_failure_alert_box(save_as_filename, err);
76             ws_close(to_fd);
77             return FALSE;
78         }
79         bytes_left -= bytes_written;
80         ptr += bytes_written;
81     }
82     if (ws_close(to_fd) < 0) {
83         if (show_err)
84             write_failure_alert_box(save_as_filename, errno);
85         return FALSE;
86     }
87
88     return TRUE;
89 }
90
91 /*
92  * Editor modelines
93  *
94  * Local Variables:
95  * c-basic-offset: 4
96  * tab-width: 8
97  * indent-tabs-mode: nil
98  * End:
99  *
100  * ex: set shiftwidth=4 tabstop=8 expandtab:
101  * :indentSize=4:tabSize=8:noTabs=true:
102  */