1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 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.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 #define ENABLE_CURLX_PRINTF
27 /* use our own printf() functions */
28 #include "curlx.h"
29
30 #include "tool_cfgable.h"
31 #include "tool_cb_see.h"
32
33 #include "memdebug.h" /* keep this as LAST include */
34
35 /* OUR_MAX_SEEK_L has 'long' data type, OUR_MAX_SEEK_O has 'curl_off_t,
36 both represent the same value. Maximum offset used here when we lseek
37 using a 'long' data type offset */
38
39 #define OUR_MAX_SEEK_L 2147483647L - 1L
40 #define OUR_MAX_SEEK_O CURL_OFF_T_C(0x7FFFFFFF) - CURL_OFF_T_C(0x1)
41
42 /*
43 ** callback for CURLOPT_SEEKFUNCTION
44 **
45 ** Notice that this is not supposed to return the resulting offset. This
46 ** shall only return CURL_SEEKFUNC_* return codes.
47 */
48
tool_seek_cb(void * userdata,curl_off_t offset,int whence)49 int tool_seek_cb(void *userdata, curl_off_t offset, int whence)
50 {
51 struct InStruct *in = userdata;
52
53 #if(SIZEOF_CURL_OFF_T > SIZEOF_OFF_T) && !defined(USE_WIN32_LARGE_FILES)
54
55 /* The offset check following here is only interesting if curl_off_t is
56 larger than off_t and we are not using the WIN32 large file support
57 macros that provide the support to do 64bit seeks correctly */
58
59 if(offset > OUR_MAX_SEEK_O) {
60 /* Some precaution code to work around problems with different data sizes
61 to allow seeking >32bit even if off_t is 32bit. Should be very rare and
62 is really valid on weirdo-systems. */
63 curl_off_t left = offset;
64
65 if(whence != SEEK_SET)
66 /* this code path doesn't support other types */
67 return CURL_SEEKFUNC_FAIL;
68
69 if(LSEEK_ERROR == lseek(in->fd, 0, SEEK_SET))
70 /* couldn't rewind to beginning */
71 return CURL_SEEKFUNC_FAIL;
72
73 while(left) {
74 long step = (left > OUR_MAX_SEEK_O) ? OUR_MAX_SEEK_L : (long)left;
75 if(LSEEK_ERROR == lseek(in->fd, step, SEEK_CUR))
76 /* couldn't seek forwards the desired amount */
77 return CURL_SEEKFUNC_FAIL;
78 left -= step;
79 }
80 return CURL_SEEKFUNC_OK;
81 }
82 #endif
83
84 if(LSEEK_ERROR == lseek(in->fd, offset, whence))
85 /* couldn't rewind, the reason is in errno but errno is just not portable
86 enough and we don't actually care that much why we failed. We'll let
87 libcurl know that it may try other means if it wants to. */
88 return CURL_SEEKFUNC_CANTSEEK;
89
90 return CURL_SEEKFUNC_OK;
91 }
92
93 #ifdef USE_TOOL_FTRUNCATE
94
95 #ifdef __BORLANDC__
96 /* 64-bit lseek-like function unavailable */
97 # define _lseeki64(hnd,ofs,whence) lseek(hnd,ofs,whence)
98 #endif
99
100 #ifdef __POCC__
101 # if(__POCC__ < 450)
102 /* 64-bit lseek-like function unavailable */
103 # define _lseeki64(hnd,ofs,whence) _lseek(hnd,ofs,whence)
104 # else
105 # undef _lseeki64
106 # define _lseeki64(hnd,ofs,whence) _lseek64(hnd,ofs,whence)
107 # endif
108 #endif
109
110 #ifdef _WIN32_WCE
111 /* 64-bit lseek-like function unavailable */
112 # undef _lseeki64
113 # define _lseeki64(hnd,ofs,whence) lseek(hnd,ofs,whence)
114 # undef _get_osfhandle
115 # define _get_osfhandle(fd) (fd)
116 #endif
117
118 /*
119 * Truncate a file handle at a 64-bit position 'where'.
120 */
121
tool_ftruncate64(int fd,curl_off_t where)122 int tool_ftruncate64(int fd, curl_off_t where)
123 {
124 intptr_t handle = _get_osfhandle(fd);
125
126 if(_lseeki64(fd, where, SEEK_SET) < 0)
127 return -1;
128
129 if(!SetEndOfFile((HANDLE)handle))
130 return -1;
131
132 return 0;
133 }
134
135 #endif /* USE_TOOL_FTRUNCATE */
136