Debian Codegraph @

deps/curl/src/tool_cb_wrt.c

raw
1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2020, Daniel Stenberg, <[email protected]>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.haxx.se/docs/copyright.html.
13 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 ***************************************************************************/
22 #include "tool_setup.h"
23
24 #define ENABLE_CURLX_PRINTF
25 /* use our own printf() functions */
26 #include "curlx.h"
27
28 #include "tool_cfgable.h"
29 #include "tool_msgs.h"
30 #include "tool_cb_wrt.h"
31 #include "tool_operate.h"
32
33 #include "memdebug.h" /* keep this as LAST include */
34
35 /* create a local file for writing, return TRUE on success */
36 bool tool_create_output_file(struct OutStruct *outs,
37 struct OperationConfig *config)
38 {
39 struct GlobalConfig *global;
40 FILE *file;
41 DEBUGASSERT(outs);
42 DEBUGASSERT(config);
43 global = config->global;
44 if(!outs->filename || !*outs->filename) {
45 warnf(global, "Remote filename has no length!\n");
46 return FALSE;
47 }
48
49 if(outs->is_cd_filename) {
50 /* don't overwrite existing files */
51 file = fopen(outs->filename, "rb");
52 if(file) {
53 fclose(file);
54 warnf(global, "Refusing to overwrite %s: %s\n", outs->filename,
55 strerror(EEXIST));
56 return FALSE;
57 }
58 }
59
60 /* open file for writing */
61 file = fopen(outs->filename, "wb");
62 if(!file) {
63 warnf(global, "Failed to create the file %s: %s\n", outs->filename,
64 strerror(errno));
65 return FALSE;
66 }
67 outs->s_isreg = TRUE;
68 outs->fopened = TRUE;
69 outs->stream = file;
70 outs->bytes = 0;
71 outs->init = 0;
72 return TRUE;
73 }
74
75 /*
76 ** callback for CURLOPT_WRITEFUNCTION
77 */
78
79 size_t tool_write_cb(char *buffer, size_t sz, size_t nmemb, void *userdata)
80 {
81 size_t rc;
82 struct per_transfer *per = userdata;
83 struct OutStruct *outs = &per->outs;
84 struct OperationConfig *config = per->config;
85 size_t bytes = sz * nmemb;
86 bool is_tty = config->global->isatty;
87 #ifdef WIN32
88 CONSOLE_SCREEN_BUFFER_INFO console_info;
89 intptr_t fhnd;
90 #endif
91
92 /*
93 * Once that libcurl has called back tool_write_cb() the returned value
94 * is checked against the amount that was intended to be written, if
95 * it does not match then it fails with CURLE_WRITE_ERROR. So at this
96 * point returning a value different from sz*nmemb indicates failure.
97 */
98 const size_t failure = bytes ? 0 : 1;
99
100 #ifdef DEBUGBUILD
101 {
102 char *tty = curlx_getenv("CURL_ISATTY");
103 if(tty) {
104 is_tty = TRUE;
105 curl_free(tty);
106 }
107 }
108
109 if(config->show_headers) {
110 if(bytes > (size_t)CURL_MAX_HTTP_HEADER) {
111 warnf(config->global, "Header data size exceeds single call write "
112 "limit!\n");
113 return failure;
114 }
115 }
116 else {
117 if(bytes > (size_t)CURL_MAX_WRITE_SIZE) {
118 warnf(config->global, "Data size exceeds single call write limit!\n");
119 return failure;
120 }
121 }
122
123 {
124 /* Some internal congruency checks on received OutStruct */
125 bool check_fails = FALSE;
126 if(outs->filename) {
127 /* regular file */
128 if(!*outs->filename)
129 check_fails = TRUE;
130 if(!outs->s_isreg)
131 check_fails = TRUE;
132 if(outs->fopened && !outs->stream)
133 check_fails = TRUE;
134 if(!outs->fopened && outs->stream)
135 check_fails = TRUE;
136 if(!outs->fopened && outs->bytes)
137 check_fails = TRUE;
138 }
139 else {
140 /* standard stream */
141 if(!outs->stream || outs->s_isreg || outs->fopened)
142 check_fails = TRUE;
143 if(outs->alloc_filename || outs->is_cd_filename || outs->init)
144 check_fails = TRUE;
145 }
146 if(check_fails) {
147 warnf(config->global, "Invalid output struct data for write callback\n");
148 return failure;
149 }
150 }
151 #endif
152
153 if(!outs->stream && !tool_create_output_file(outs, per->config))
154 return failure;
155
156 if(is_tty && (outs->bytes < 2000) && !config->terminal_binary_ok) {
157 /* binary output to terminal? */
158 if(memchr(buffer, 0, bytes)) {
159 warnf(config->global, "Binary output can mess up your terminal. "
160 "Use \"--output -\" to tell curl to output it to your terminal "
161 "anyway, or consider \"--output <FILE>\" to save to a file.\n");
162 config->synthetic_error = ERR_BINARY_TERMINAL;
163 return failure;
164 }
165 }
166
167 #ifdef WIN32
168 fhnd = _get_osfhandle(fileno(outs->stream));
169 if(isatty(fileno(outs->stream)) &&
170 GetConsoleScreenBufferInfo((HANDLE)fhnd, &console_info)) {
171 DWORD in_len = (DWORD)(sz * nmemb);
172 wchar_t* wc_buf;
173 DWORD wc_len;
174
175 /* calculate buffer size for wide characters */
176 wc_len = MultiByteToWideChar(CP_UTF8, 0, buffer, in_len, NULL, 0);
177 wc_buf = (wchar_t*) malloc(wc_len * sizeof(wchar_t));
178 if(!wc_buf)
179 return failure;
180
181 /* calculate buffer size for multi-byte characters */
182 wc_len = MultiByteToWideChar(CP_UTF8, 0, buffer, in_len, wc_buf, wc_len);
183 if(!wc_len) {
184 free(wc_buf);
185 return failure;
186 }
187
188 if(!WriteConsoleW(
189 (HANDLE) fhnd,
190 wc_buf,
191 wc_len,
192 &wc_len,
193 NULL)) {
194 free(wc_buf);
195 return failure;
196 }
197 free(wc_buf);
198 rc = bytes;
199 }
200 else
201 #endif
202 rc = fwrite(buffer, sz, nmemb, outs->stream);
203
204 if(bytes == rc)
205 /* we added this amount of data to the output */
206 outs->bytes += bytes;
207
208 if(config->readbusy) {
209 config->readbusy = FALSE;
210 curl_easy_pause(per->curl, CURLPAUSE_CONT);
211 }
212
213 if(config->nobuffer) {
214 /* output buffering disabled */
215 int res = fflush(outs->stream);
216 if(res)
217 return failure;
218 }
219
220 return rc;
221 }