• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2018, Daniel Stenberg, <daniel@haxx.se>, 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 
32 #include "memdebug.h" /* keep this as LAST include */
33 
34 /* create a local file for writing, return TRUE on success */
tool_create_output_file(struct OutStruct * outs)35 bool tool_create_output_file(struct OutStruct *outs)
36 {
37   struct GlobalConfig *global = outs->config->global;
38   FILE *file;
39 
40   if(!outs->filename || !*outs->filename) {
41     warnf(global, "Remote filename has no length!\n");
42     return FALSE;
43   }
44 
45   if(outs->is_cd_filename) {
46     /* don't overwrite existing files */
47     file = fopen(outs->filename, "rb");
48     if(file) {
49       fclose(file);
50       warnf(global, "Refusing to overwrite %s: %s\n", outs->filename,
51             strerror(EEXIST));
52       return FALSE;
53     }
54   }
55 
56   /* open file for writing */
57   file = fopen(outs->filename, "wb");
58   if(!file) {
59     warnf(global, "Failed to create the file %s: %s\n", outs->filename,
60           strerror(errno));
61     return FALSE;
62   }
63   outs->s_isreg = TRUE;
64   outs->fopened = TRUE;
65   outs->stream = file;
66   outs->bytes = 0;
67   outs->init = 0;
68   return TRUE;
69 }
70 
71 /*
72 ** callback for CURLOPT_WRITEFUNCTION
73 */
74 
tool_write_cb(char * buffer,size_t sz,size_t nmemb,void * userdata)75 size_t tool_write_cb(char *buffer, size_t sz, size_t nmemb, void *userdata)
76 {
77   size_t rc;
78   struct OutStruct *outs = userdata;
79   struct OperationConfig *config = outs->config;
80   size_t bytes = sz * nmemb;
81   bool is_tty = config->global->isatty;
82 #ifdef WIN32
83   CONSOLE_SCREEN_BUFFER_INFO console_info;
84 #endif
85 
86   /*
87    * Once that libcurl has called back tool_write_cb() the returned value
88    * is checked against the amount that was intended to be written, if
89    * it does not match then it fails with CURLE_WRITE_ERROR. So at this
90    * point returning a value different from sz*nmemb indicates failure.
91    */
92   const size_t failure = bytes ? 0 : 1;
93 
94 #ifdef DEBUGBUILD
95   {
96     char *tty = curlx_getenv("CURL_ISATTY");
97     if(tty) {
98       is_tty = TRUE;
99       curl_free(tty);
100     }
101   }
102 
103   if(config->show_headers) {
104     if(bytes > (size_t)CURL_MAX_HTTP_HEADER) {
105       warnf(config->global, "Header data size exceeds single call write "
106             "limit!\n");
107       return failure;
108     }
109   }
110   else {
111     if(bytes > (size_t)CURL_MAX_WRITE_SIZE) {
112       warnf(config->global, "Data size exceeds single call write limit!\n");
113       return failure;
114     }
115   }
116 
117   {
118     /* Some internal congruency checks on received OutStruct */
119     bool check_fails = FALSE;
120     if(outs->filename) {
121       /* regular file */
122       if(!*outs->filename)
123         check_fails = TRUE;
124       if(!outs->s_isreg)
125         check_fails = TRUE;
126       if(outs->fopened && !outs->stream)
127         check_fails = TRUE;
128       if(!outs->fopened && outs->stream)
129         check_fails = TRUE;
130       if(!outs->fopened && outs->bytes)
131         check_fails = TRUE;
132     }
133     else {
134       /* standard stream */
135       if(!outs->stream || outs->s_isreg || outs->fopened)
136         check_fails = TRUE;
137       if(outs->alloc_filename || outs->is_cd_filename || outs->init)
138         check_fails = TRUE;
139     }
140     if(check_fails) {
141       warnf(config->global, "Invalid output struct data for write callback\n");
142       return failure;
143     }
144   }
145 #endif
146 
147   if(!outs->stream && !tool_create_output_file(outs))
148     return failure;
149 
150   if(is_tty && (outs->bytes < 2000) && !config->terminal_binary_ok) {
151     /* binary output to terminal? */
152     if(memchr(buffer, 0, bytes)) {
153       warnf(config->global, "Binary output can mess up your terminal. "
154             "Use \"--output -\" to tell curl to output it to your terminal "
155             "anyway, or consider \"--output <FILE>\" to save to a file.\n");
156       config->synthetic_error = ERR_BINARY_TERMINAL;
157       return failure;
158     }
159   }
160 
161 #ifdef _WIN32
162   if(isatty(fileno(outs->stream)) &&
163      GetConsoleScreenBufferInfo(
164        (HANDLE)_get_osfhandle(fileno(outs->stream)), &console_info)) {
165     DWORD in_len = (DWORD)(sz * nmemb);
166     wchar_t* wc_buf;
167     DWORD wc_len;
168     intptr_t fhnd;
169 
170     /* calculate buffer size for wide characters */
171     wc_len = MultiByteToWideChar(CP_UTF8, 0, buffer, in_len,  NULL, 0);
172     wc_buf = (wchar_t*) malloc(wc_len * sizeof(wchar_t));
173     if(!wc_buf)
174       return failure;
175 
176     /* calculate buffer size for multi-byte characters */
177     wc_len = MultiByteToWideChar(CP_UTF8, 0, buffer, in_len, wc_buf, wc_len);
178     if(!wc_len) {
179       free(wc_buf);
180       return failure;
181     }
182 
183     fhnd = _get_osfhandle(fileno(outs->stream));
184 
185     if(!WriteConsoleW(
186         (HANDLE) fhnd,
187         wc_buf,
188         wc_len,
189         &wc_len,
190         NULL)) {
191       free(wc_buf);
192       return failure;
193     }
194     free(wc_buf);
195     rc = bytes;
196   }
197   else
198 #endif
199     rc = fwrite(buffer, sz, nmemb, outs->stream);
200 
201   if(bytes == rc)
202     /* we added this amount of data to the output */
203     outs->bytes += bytes;
204 
205   if(config->readbusy) {
206     config->readbusy = FALSE;
207     curl_easy_pause(config->easy, CURLPAUSE_CONT);
208   }
209 
210   if(config->nobuffer) {
211     /* output buffering disabled */
212     int res = fflush(outs->stream);
213     if(res)
214       return failure;
215   }
216 
217   return rc;
218 }
219