Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/pkg
Path: blob/main/external/curl/src/tool_cb_wrt.c
2645 views
1
/***************************************************************************
2
* _ _ ____ _
3
* Project ___| | | | _ \| |
4
* / __| | | | |_) | |
5
* | (__| |_| | _ <| |___
6
* \___|\___/|_| \_\_____|
7
*
8
* Copyright (C) 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.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
* SPDX-License-Identifier: curl
22
*
23
***************************************************************************/
24
#include "tool_setup.h"
25
26
#include "tool_cfgable.h"
27
#include "tool_msgs.h"
28
#include "tool_cb_wrt.h"
29
#include "tool_operate.h"
30
31
#include "memdebug.h" /* keep this as LAST include */
32
33
#ifdef _WIN32
34
#define OPENMODE S_IREAD | S_IWRITE
35
#else
36
#define OPENMODE S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH
37
#endif
38
39
/* create/open a local file for writing, return TRUE on success */
40
bool tool_create_output_file(struct OutStruct *outs,
41
struct OperationConfig *config)
42
{
43
FILE *file = NULL;
44
const char *fname = outs->filename;
45
DEBUGASSERT(outs);
46
DEBUGASSERT(config);
47
DEBUGASSERT(fname && *fname);
48
49
if(config->file_clobber_mode == CLOBBER_ALWAYS ||
50
(config->file_clobber_mode == CLOBBER_DEFAULT &&
51
!outs->is_cd_filename)) {
52
/* open file for writing */
53
file = curlx_fopen(fname, "wb");
54
}
55
else {
56
int fd;
57
do {
58
fd = curlx_open(fname, O_CREAT | O_WRONLY | O_EXCL | CURL_O_BINARY,
59
OPENMODE);
60
/* Keep retrying in the hope that it is not interrupted sometime */
61
/* !checksrc! disable ERRNOVAR 1 */
62
} while(fd == -1 && errno == EINTR);
63
if(config->file_clobber_mode == CLOBBER_NEVER && fd == -1) {
64
int next_num = 1;
65
struct dynbuf fbuffer;
66
curlx_dyn_init(&fbuffer, 1025);
67
/* !checksrc! disable ERRNOVAR 1 */
68
while(fd == -1 && /* have not successfully opened a file */
69
(errno == EEXIST || errno == EISDIR) &&
70
/* because we keep having files that already exist */
71
next_num < 100 /* and we have not reached the retry limit */ ) {
72
curlx_dyn_reset(&fbuffer);
73
if(curlx_dyn_addf(&fbuffer, "%s.%d", fname, next_num))
74
return FALSE;
75
next_num++;
76
do {
77
fd = curlx_open(curlx_dyn_ptr(&fbuffer),
78
O_CREAT | O_WRONLY | O_EXCL | CURL_O_BINARY,
79
OPENMODE);
80
/* Keep retrying in the hope that it is not interrupted sometime */
81
} while(fd == -1 && errno == EINTR);
82
}
83
outs->filename = curlx_dyn_ptr(&fbuffer); /* remember the new one */
84
outs->alloc_filename = TRUE;
85
}
86
/* An else statement to not overwrite existing files and not retry with
87
new numbered names (which would cover
88
config->file_clobber_mode == CLOBBER_DEFAULT && outs->is_cd_filename)
89
is not needed because we would have failed earlier, in the while loop
90
and `fd` would now be -1 */
91
if(fd != -1) {
92
file = curlx_fdopen(fd, "wb");
93
if(!file)
94
close(fd);
95
}
96
}
97
98
if(!file) {
99
char errbuf[STRERROR_LEN];
100
warnf("Failed to open the file %s: %s", fname,
101
curlx_strerror(errno, errbuf, sizeof(errbuf)));
102
return FALSE;
103
}
104
outs->s_isreg = TRUE;
105
outs->fopened = TRUE;
106
outs->stream = file;
107
outs->bytes = 0;
108
outs->init = 0;
109
return TRUE;
110
}
111
112
#if defined(_WIN32) && !defined(UNDER_CE)
113
static size_t win_console(intptr_t fhnd, struct OutStruct *outs,
114
char *buffer, size_t bytes,
115
size_t *retp)
116
{
117
DWORD chars_written;
118
unsigned char *rbuf = (unsigned char *)buffer;
119
DWORD rlen = (DWORD)bytes;
120
121
#define IS_TRAILING_BYTE(x) (0x80 <= (x) && (x) < 0xC0)
122
123
/* attempt to complete an incomplete UTF-8 sequence from previous call. the
124
sequence does not have to be well-formed. */
125
if(outs->utf8seq[0] && rlen) {
126
bool complete = false;
127
/* two byte sequence (lead byte 110yyyyy) */
128
if(0xC0 <= outs->utf8seq[0] && outs->utf8seq[0] < 0xE0) {
129
outs->utf8seq[1] = *rbuf++;
130
--rlen;
131
complete = true;
132
}
133
/* three byte sequence (lead byte 1110zzzz) */
134
else if(0xE0 <= outs->utf8seq[0] && outs->utf8seq[0] < 0xF0) {
135
if(!outs->utf8seq[1]) {
136
outs->utf8seq[1] = *rbuf++;
137
--rlen;
138
}
139
if(rlen && !outs->utf8seq[2]) {
140
outs->utf8seq[2] = *rbuf++;
141
--rlen;
142
complete = true;
143
}
144
}
145
/* four byte sequence (lead byte 11110uuu) */
146
else if(0xF0 <= outs->utf8seq[0] && outs->utf8seq[0] < 0xF8) {
147
if(!outs->utf8seq[1]) {
148
outs->utf8seq[1] = *rbuf++;
149
--rlen;
150
}
151
if(rlen && !outs->utf8seq[2]) {
152
outs->utf8seq[2] = *rbuf++;
153
--rlen;
154
}
155
if(rlen && !outs->utf8seq[3]) {
156
outs->utf8seq[3] = *rbuf++;
157
--rlen;
158
complete = true;
159
}
160
}
161
162
if(complete) {
163
WCHAR prefix[3] = {0}; /* UTF-16 (1-2 WCHARs) + NUL */
164
165
if(MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)outs->utf8seq, -1,
166
prefix, CURL_ARRAYSIZE(prefix))) {
167
DEBUGASSERT(prefix[2] == L'\0');
168
if(!WriteConsoleW((HANDLE) fhnd, prefix, prefix[1] ? 2 : 1,
169
&chars_written, NULL)) {
170
return CURL_WRITEFUNC_ERROR;
171
}
172
}
173
/* else: UTF-8 input was not well formed and OS is pre-Vista which drops
174
invalid characters instead of writing U+FFFD to output. */
175
memset(outs->utf8seq, 0, sizeof(outs->utf8seq));
176
}
177
}
178
179
/* suppress an incomplete utf-8 sequence at end of rbuf */
180
if(!outs->utf8seq[0] && rlen && (rbuf[rlen - 1] & 0x80)) {
181
/* check for lead byte from a two, three or four byte sequence */
182
if(0xC0 <= rbuf[rlen - 1] && rbuf[rlen - 1] < 0xF8) {
183
outs->utf8seq[0] = rbuf[rlen - 1];
184
rlen -= 1;
185
}
186
else if(rlen >= 2 && IS_TRAILING_BYTE(rbuf[rlen - 1])) {
187
/* check for lead byte from a three or four byte sequence */
188
if(0xE0 <= rbuf[rlen - 2] && rbuf[rlen - 2] < 0xF8) {
189
outs->utf8seq[0] = rbuf[rlen - 2];
190
outs->utf8seq[1] = rbuf[rlen - 1];
191
rlen -= 2;
192
}
193
else if(rlen >= 3 && IS_TRAILING_BYTE(rbuf[rlen - 2])) {
194
/* check for lead byte from a four byte sequence */
195
if(0xF0 <= rbuf[rlen - 3] && rbuf[rlen - 3] < 0xF8) {
196
outs->utf8seq[0] = rbuf[rlen - 3];
197
outs->utf8seq[1] = rbuf[rlen - 2];
198
outs->utf8seq[2] = rbuf[rlen - 1];
199
rlen -= 3;
200
}
201
}
202
}
203
}
204
205
if(rlen) {
206
/* calculate buffer size for wide characters */
207
DWORD len = (DWORD)MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)rbuf,
208
(int)rlen, NULL, 0);
209
if(!len)
210
return CURL_WRITEFUNC_ERROR;
211
212
/* grow the buffer if needed */
213
if(len > global->term.len) {
214
wchar_t *buf = (wchar_t *) realloc(global->term.buf,
215
len * sizeof(wchar_t));
216
if(!buf)
217
return CURL_WRITEFUNC_ERROR;
218
global->term.len = len;
219
global->term.buf = buf;
220
}
221
222
len = (DWORD)MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)rbuf, (int)rlen,
223
global->term.buf,
224
(int)len);
225
if(!len)
226
return CURL_WRITEFUNC_ERROR;
227
228
if(!WriteConsoleW((HANDLE) fhnd, global->term.buf,
229
len, &chars_written, NULL))
230
return CURL_WRITEFUNC_ERROR;
231
}
232
233
*retp = bytes;
234
return 0;
235
}
236
#endif
237
238
/*
239
** callback for CURLOPT_WRITEFUNCTION
240
*/
241
242
size_t tool_write_cb(char *buffer, size_t sz, size_t nmemb, void *userdata)
243
{
244
size_t rc;
245
struct per_transfer *per = userdata;
246
struct OutStruct *outs = &per->outs;
247
struct OperationConfig *config = per->config;
248
size_t bytes = sz * nmemb;
249
bool is_tty = global->isatty;
250
#if defined(_WIN32) && !defined(UNDER_CE)
251
CONSOLE_SCREEN_BUFFER_INFO console_info;
252
intptr_t fhnd;
253
#endif
254
255
if(outs->out_null)
256
return bytes;
257
258
#ifdef DEBUGBUILD
259
{
260
char *tty = curl_getenv("CURL_ISATTY");
261
if(tty) {
262
is_tty = TRUE;
263
curl_free(tty);
264
}
265
}
266
267
if(config->show_headers) {
268
if(bytes > (size_t)CURL_MAX_HTTP_HEADER) {
269
warnf("Header data size exceeds write limit");
270
return CURL_WRITEFUNC_ERROR;
271
}
272
}
273
else {
274
if(bytes > (size_t)CURL_MAX_WRITE_SIZE) {
275
warnf("Data size exceeds write limit");
276
return CURL_WRITEFUNC_ERROR;
277
}
278
}
279
280
{
281
/* Some internal congruency checks on received OutStruct */
282
bool check_fails = FALSE;
283
if(outs->filename) {
284
/* regular file */
285
if(!*outs->filename)
286
check_fails = TRUE;
287
if(!outs->s_isreg)
288
check_fails = TRUE;
289
if(outs->fopened && !outs->stream)
290
check_fails = TRUE;
291
if(!outs->fopened && outs->stream)
292
check_fails = TRUE;
293
if(!outs->fopened && outs->bytes)
294
check_fails = TRUE;
295
}
296
else {
297
/* standard stream */
298
if(!outs->stream || outs->s_isreg || outs->fopened)
299
check_fails = TRUE;
300
if(outs->alloc_filename || outs->is_cd_filename || outs->init)
301
check_fails = TRUE;
302
}
303
if(check_fails) {
304
warnf("Invalid output struct data for write callback");
305
return CURL_WRITEFUNC_ERROR;
306
}
307
}
308
#endif
309
310
if(!outs->stream && !tool_create_output_file(outs, per->config))
311
return CURL_WRITEFUNC_ERROR;
312
313
if(is_tty && (outs->bytes < 2000) && !config->terminal_binary_ok) {
314
/* binary output to terminal? */
315
if(memchr(buffer, 0, bytes)) {
316
warnf("Binary output can mess up your terminal. "
317
"Use \"--output -\" to tell curl to output it to your terminal "
318
"anyway, or consider \"--output <FILE>\" to save to a file.");
319
config->synthetic_error = TRUE;
320
return CURL_WRITEFUNC_ERROR;
321
}
322
}
323
324
#if defined(_WIN32) && !defined(UNDER_CE)
325
fhnd = _get_osfhandle(fileno(outs->stream));
326
/* if Windows console then UTF-8 must be converted to UTF-16 */
327
if(isatty(fileno(outs->stream)) &&
328
GetConsoleScreenBufferInfo((HANDLE)fhnd, &console_info)) {
329
size_t retval = win_console(fhnd, outs, buffer, bytes, &rc);
330
if(retval)
331
return retval;
332
}
333
else
334
#endif
335
{
336
if(per->hdrcbdata.headlist) {
337
if(tool_write_headers(&per->hdrcbdata, outs->stream))
338
return CURL_WRITEFUNC_ERROR;
339
}
340
rc = fwrite(buffer, sz, nmemb, outs->stream);
341
}
342
343
if(bytes == rc)
344
/* we added this amount of data to the output */
345
outs->bytes += bytes;
346
347
if(config->readbusy) {
348
config->readbusy = FALSE;
349
curl_easy_pause(per->curl, CURLPAUSE_CONT);
350
}
351
352
if(config->nobuffer) {
353
/* output buffering disabled */
354
int res;
355
do {
356
res = fflush(outs->stream);
357
/* Keep retrying in the hope that it is not interrupted sometime */
358
/* !checksrc! disable ERRNOVAR 1 */
359
} while(res && errno == EINTR);
360
if(res)
361
return CURL_WRITEFUNC_ERROR;
362
}
363
364
return rc;
365
}
366
367