/***************************************************************************1* _ _ ____ _2* Project ___| | | | _ \| |3* / __| | | | |_) | |4* | (__| |_| | _ <| |___5* \___|\___/|_| \_\_____|6*7* Copyright (C) Daniel Stenberg, <[email protected]>, et al.8*9* This software is licensed as described in the file COPYING, which10* you should have received as part of this distribution. The terms11* are also available at https://curl.se/docs/copyright.html.12*13* You may opt to use, copy, modify, merge, publish, distribute and/or sell14* copies of the Software, and permit persons to whom the Software is15* furnished to do so, under the terms of the COPYING file.16*17* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY18* KIND, either express or implied.19*20* SPDX-License-Identifier: curl21*22***************************************************************************/23#include "tool_setup.h"2425#include "tool_cfgable.h"26#include "tool_operate.h"27#include "tool_cb_see.h"2829#include "memdebug.h" /* keep this as LAST include */3031/*32** callback for CURLOPT_SEEKFUNCTION33**34** Notice that this is not supposed to return the resulting offset. This35** shall only return CURL_SEEKFUNC_* return codes.36*/3738int tool_seek_cb(void *userdata, curl_off_t offset, int whence)39{40struct per_transfer *per = userdata;4142#if (SIZEOF_CURL_OFF_T > SIZEOF_OFF_T) && !defined(USE_WIN32_LARGE_FILES)4344/* OUR_MAX_SEEK_L has 'long' data type, OUR_MAX_SEEK_O has 'curl_off_t,45both represent the same value. Maximum offset used here when we lseek46using a 'long' data type offset */4748#define OUR_MAX_SEEK_L 2147483647L - 1L49#define OUR_MAX_SEEK_O 0x7FFFFFFF - 0x15051/* The offset check following here is only interesting if curl_off_t is52larger than off_t and we are not using the Win32 large file support53macros that provide the support to do 64-bit seeks correctly */5455if(offset > OUR_MAX_SEEK_O) {56/* Some precaution code to work around problems with different data sizes57to allow seeking >32-bit even if off_t is 32-bit. Should be very rare58and is really valid on weirdo-systems. */59curl_off_t left = offset;6061if(whence != SEEK_SET)62/* this code path does not support other types */63return CURL_SEEKFUNC_FAIL;6465if(LSEEK_ERROR == lseek(per->infd, 0, SEEK_SET))66/* could not rewind to beginning */67return CURL_SEEKFUNC_FAIL;6869while(left) {70long step = (left > OUR_MAX_SEEK_O) ? OUR_MAX_SEEK_L : (long)left;71if(LSEEK_ERROR == lseek(per->infd, step, SEEK_CUR))72/* could not seek forwards the desired amount */73return CURL_SEEKFUNC_FAIL;74left -= step;75}76return CURL_SEEKFUNC_OK;77}78#endif7980#if defined(__AMIGA__) || defined(__MINGW32CE__)81if(LSEEK_ERROR == lseek(per->infd, (off_t)offset, whence))82#else83if(LSEEK_ERROR == lseek(per->infd, offset, whence))84#endif85/* could not rewind, the reason is in errno but errno is just not portable86enough and we do not actually care that much why we failed. We will let87libcurl know that it may try other means if it wants to. */88return CURL_SEEKFUNC_CANTSEEK;8990return CURL_SEEKFUNC_OK;91}929394