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