Qt: Fix leak on CaptureFileDialog preview of file with errors
[wireshark.git] / sync_pipe_write.c
blobc279e5765f496a670ef0f6fe8ffaa7cf88fff606
1 /* sync_pipe_write.c
3 * Wireshark - Network traffic analyzer
4 * By Gerald Combs <gerald@wireshark.org>
5 * Copyright 1998 Gerald Combs
7 * SPDX-License-Identifier: GPL-2.0-or-later
8 */
10 #include <config.h>
12 #include <string.h>
14 #include <glib.h>
16 #include <wsutil/file_util.h>
17 #include <wsutil/ws_assert.h>
19 #include "sync_pipe.h"
21 /****************************************************************************************************************/
22 /* sync_pipe handling */
25 /* write a single message header to the recipient pipe */
26 static ssize_t
27 sync_pipe_write_header(int pipe_fd, char indicator, unsigned int length)
29 guchar header[1+3]; /* indicator + 3-byte len */
31 ws_assert(length <= SP_MAX_MSG_LEN);
33 /* write header (indicator + 3-byte len) */
34 header[0] = indicator;
35 header[1] = (length >> 16) & 0xFF;
36 header[2] = (length >> 8) & 0xFF;
37 header[3] = (length >> 0) & 0xFF;
39 /* write header */
40 return ws_write(pipe_fd, header, sizeof header);
44 /* Write a message, with a string body, to the recipient pipe in the
45 standard format (1-byte message indicator, 3-byte message length
46 (excluding length and indicator field), and the string.
47 If msg is NULL, the message has only a length and indicator. */
48 void
49 sync_pipe_write_string_msg(int pipe_fd, char indicator, const char *msg)
51 ssize_t ret;
52 int len;
54 /*ws_warning("write %d enter", pipe_fd);*/
56 if(msg != NULL) {
57 len = (int) strlen(msg) + 1; /* including the terminating '\0'! */
58 } else {
59 len = 0;
62 /* write header (indicator + 3-byte len) */
63 ret = sync_pipe_write_header(pipe_fd, indicator, len);
64 if(ret == -1) {
65 return;
68 /* write value (if we have one) */
69 if(len) {
70 /*ws_warning("write %d indicator: %c value len: %u msg: %s", pipe_fd, indicator, len, msg);*/
71 ret = ws_write(pipe_fd, msg, len);
72 if(ret == -1) {
73 return;
75 } else {
76 /*ws_warning("write %d indicator: %c no value", pipe_fd, indicator);*/
79 /*ws_warning("write %d leave", pipe_fd);*/
83 /* Size of buffer to hold decimal representation of
84 signed/unsigned 64-bit int */
85 #define SP_DECISIZE 20
87 /* Write a message, with an unsigned integer body, to the recipient
88 pipe in the standard format (1-byte message indicator, 3-byte
89 message length (excluding length and indicator field), and the
90 unsigned integer, as a string. */
91 void
92 sync_pipe_write_uint_msg(int pipe_fd, char indicator, unsigned int num)
94 char count_str[SP_DECISIZE+1+1];
96 snprintf(count_str, sizeof(count_str), "%u", num);
97 sync_pipe_write_string_msg(pipe_fd, indicator, count_str);
100 /* Write a message, with an integer body, to the recipient pipe in the
101 standard format (1-byte message indicator, 3-byte message length
102 (excluding length and indicator field), and the unsigned integer,
103 as a string. */
104 void
105 sync_pipe_write_int_msg(int pipe_fd, char indicator, int num)
107 char count_str[SP_DECISIZE+1+1];
109 snprintf(count_str, sizeof(count_str), "%d", num);
110 sync_pipe_write_string_msg(pipe_fd, indicator, count_str);
113 /* Write a message, with a primary and secondary error message as the body,
114 to the recipient pipe. The header is an SP_ERROR_MSG header, with the
115 length being the length of two string submessages; the submessages
116 are the body of the message, with each submessage being a message
117 with an indicator of SP_ERROR_MSG, the first message having the
118 primary error message string and the second message having the secondary
119 error message string. */
120 void
121 sync_pipe_write_errmsgs_to_parent(int pipe_fd, const char *error_msg,
122 const char *secondary_error_msg)
124 sync_pipe_write_header(pipe_fd, SP_ERROR_MSG,
125 (unsigned int) (strlen(error_msg) + 1 + 4 + strlen(secondary_error_msg) + 1 + 4));
126 sync_pipe_write_string_msg(pipe_fd, SP_ERROR_MSG, error_msg);
127 sync_pipe_write_string_msg(pipe_fd, SP_ERROR_MSG, secondary_error_msg);
131 * Editor modelines - https://www.wireshark.org/tools/modelines.html
133 * Local variables:
134 * c-basic-offset: 4
135 * tab-width: 8
136 * indent-tabs-mode: nil
137 * End:
139 * vi: set shiftwidth=4 tabstop=8 expandtab:
140 * :indentSize=4:tabSize=8:noTabs=true: