xref: /aosp_15_r20/external/curl/src/tool_cb_see.c (revision 6236dae45794135f37c4eb022389c904c8b0090d)
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 "curlx.h"
27 
28 #include "tool_cfgable.h"
29 #include "tool_operate.h"
30 #include "tool_cb_see.h"
31 
32 #include "memdebug.h" /* keep this as LAST include */
33 
34 /* OUR_MAX_SEEK_L has 'long' data type, OUR_MAX_SEEK_O has 'curl_off_t,
35    both represent the same value. Maximum offset used here when we lseek
36    using a 'long' data type offset */
37 
38 #define OUR_MAX_SEEK_L  2147483647L - 1L
39 #define OUR_MAX_SEEK_O  CURL_OFF_T_C(0x7FFFFFFF) - CURL_OFF_T_C(0x1)
40 
41 /*
42 ** callback for CURLOPT_SEEKFUNCTION
43 **
44 ** Notice that this is not supposed to return the resulting offset. This
45 ** shall only return CURL_SEEKFUNC_* return codes.
46 */
47 
tool_seek_cb(void * userdata,curl_off_t offset,int whence)48 int tool_seek_cb(void *userdata, curl_off_t offset, int whence)
49 {
50   struct per_transfer *per = userdata;
51 
52 #if(SIZEOF_CURL_OFF_T > SIZEOF_OFF_T) && !defined(USE_WIN32_LARGE_FILES)
53 
54   /* The offset check following here is only interesting if curl_off_t is
55      larger than off_t and we are not using the Win32 large file support
56      macros that provide the support to do 64-bit seeks correctly */
57 
58   if(offset > OUR_MAX_SEEK_O) {
59     /* Some precaution code to work around problems with different data sizes
60        to allow seeking >32-bit even if off_t is 32-bit. Should be very rare
61        and is really valid on weirdo-systems. */
62     curl_off_t left = offset;
63 
64     if(whence != SEEK_SET)
65       /* this code path does not support other types */
66       return CURL_SEEKFUNC_FAIL;
67 
68     if(LSEEK_ERROR == lseek(per->infd, 0, SEEK_SET))
69       /* could not rewind to beginning */
70       return CURL_SEEKFUNC_FAIL;
71 
72     while(left) {
73       long step = (left > OUR_MAX_SEEK_O) ? OUR_MAX_SEEK_L : (long)left;
74       if(LSEEK_ERROR == lseek(per->infd, step, SEEK_CUR))
75         /* could not seek forwards the desired amount */
76         return CURL_SEEKFUNC_FAIL;
77       left -= step;
78     }
79     return CURL_SEEKFUNC_OK;
80   }
81 #endif
82 
83   if(LSEEK_ERROR == lseek(per->infd, offset, whence))
84     /* could not rewind, the reason is in errno but errno is just not portable
85        enough and we do not actually care that much why we failed. We will let
86        libcurl know that it may try other means if it wants to. */
87     return CURL_SEEKFUNC_CANTSEEK;
88 
89   return CURL_SEEKFUNC_OK;
90 }
91