Coverage Report

Created: 2024-02-25 06:14

/src/PROJ/curl/lib/curl_range.c
Line
Count
Source (jump to first uncovered line)
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
25
#include "curl_setup.h"
26
#include <curl/curl.h>
27
#include "curl_range.h"
28
#include "sendf.h"
29
#include "strtoofft.h"
30
31
/* Only include this function if one or more of FTP, FILE are enabled. */
32
#if !defined(CURL_DISABLE_FTP) || !defined(CURL_DISABLE_FILE)
33
34
 /*
35
  Check if this is a range download, and if so, set the internal variables
36
  properly.
37
 */
38
CURLcode Curl_range(struct Curl_easy *data)
39
0
{
40
0
  curl_off_t from, to;
41
0
  char *ptr;
42
0
  char *ptr2;
43
44
0
  if(data->state.use_range && data->state.range) {
45
0
    CURLofft from_t;
46
0
    CURLofft to_t;
47
0
    from_t = curlx_strtoofft(data->state.range, &ptr, 10, &from);
48
0
    if(from_t == CURL_OFFT_FLOW)
49
0
      return CURLE_RANGE_ERROR;
50
0
    while(*ptr && (ISBLANK(*ptr) || (*ptr == '-')))
51
0
      ptr++;
52
0
    to_t = curlx_strtoofft(ptr, &ptr2, 10, &to);
53
0
    if(to_t == CURL_OFFT_FLOW)
54
0
      return CURLE_RANGE_ERROR;
55
0
    if((to_t == CURL_OFFT_INVAL) && !from_t) {
56
      /* X - */
57
0
      data->state.resume_from = from;
58
0
      DEBUGF(infof(data, "RANGE %" CURL_FORMAT_CURL_OFF_T " to end of file",
59
0
                   from));
60
0
    }
61
0
    else if((from_t == CURL_OFFT_INVAL) && !to_t) {
62
      /* -Y */
63
0
      data->req.maxdownload = to;
64
0
      data->state.resume_from = -to;
65
0
      DEBUGF(infof(data, "RANGE the last %" CURL_FORMAT_CURL_OFF_T " bytes",
66
0
                   to));
67
0
    }
68
0
    else {
69
      /* X-Y */
70
0
      curl_off_t totalsize;
71
72
      /* Ensure the range is sensible - to should follow from. */
73
0
      if(from > to)
74
0
        return CURLE_RANGE_ERROR;
75
76
0
      totalsize = to - from;
77
0
      if(totalsize == CURL_OFF_T_MAX)
78
0
        return CURLE_RANGE_ERROR;
79
80
0
      data->req.maxdownload = totalsize + 1; /* include last byte */
81
0
      data->state.resume_from = from;
82
0
      DEBUGF(infof(data, "RANGE from %" CURL_FORMAT_CURL_OFF_T
83
0
                   " getting %" CURL_FORMAT_CURL_OFF_T " bytes",
84
0
                   from, data->req.maxdownload));
85
0
    }
86
0
    DEBUGF(infof(data, "range-download from %" CURL_FORMAT_CURL_OFF_T
87
0
                 " to %" CURL_FORMAT_CURL_OFF_T ", totally %"
88
0
                 CURL_FORMAT_CURL_OFF_T " bytes",
89
0
                 from, to, data->req.maxdownload));
90
0
  }
91
0
  else
92
0
    data->req.maxdownload = -1;
93
0
  return CURLE_OK;
94
0
}
95
96
#endif