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