Coverage Report

Created: 2025-08-29 06:10

/src/curl/lib/curl_range.c
Line
Count
Source
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 "curlx/strparse.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
2.76k
{
40
2.76k
  if(data->state.use_range && data->state.range) {
41
1.55k
    curl_off_t from, to;
42
1.55k
    bool first_num = TRUE;
43
1.55k
    const char *p = data->state.range;
44
1.55k
    if(curlx_str_number(&p, &from, CURL_OFF_T_MAX))
45
770
      first_num = FALSE;
46
47
1.55k
    if(curlx_str_single(&p, '-'))
48
      /* no leading dash or after the first number is an error */
49
10
      return CURLE_RANGE_ERROR;
50
51
1.54k
    if(curlx_str_number(&p, &to, CURL_OFF_T_MAX)) {
52
      /* no second number */
53
      /* X - */
54
219
      data->state.resume_from = from;
55
219
      DEBUGF(infof(data, "RANGE %" FMT_OFF_T " to end of file", from));
56
219
    }
57
1.32k
    else if(!first_num) {
58
      /* -Y */
59
745
      if(!to)
60
        /* "-0" is just wrong */
61
2
        return CURLE_RANGE_ERROR;
62
63
743
      data->req.maxdownload = to;
64
743
      data->state.resume_from = -to;
65
743
      DEBUGF(infof(data, "RANGE the last %" FMT_OFF_T " bytes", to));
66
743
    }
67
579
    else {
68
      /* X-Y */
69
579
      curl_off_t totalsize;
70
71
      /* Ensure the range is sensible - to should follow from. */
72
579
      if(from > to)
73
103
        return CURLE_RANGE_ERROR;
74
75
476
      totalsize = to - from;
76
476
      if(totalsize == CURL_OFF_T_MAX)
77
2
        return CURLE_RANGE_ERROR;
78
79
474
      data->req.maxdownload = totalsize + 1; /* include last byte */
80
474
      data->state.resume_from = from;
81
474
      DEBUGF(infof(data, "RANGE from %" FMT_OFF_T
82
474
                   " getting %" FMT_OFF_T " bytes",
83
474
                   from, data->req.maxdownload));
84
474
    }
85
1.43k
    DEBUGF(infof(data, "range-download from %" FMT_OFF_T
86
1.43k
                 " to %" FMT_OFF_T ", totally %" FMT_OFF_T " bytes",
87
1.43k
                 from, to, data->req.maxdownload));
88
1.43k
  }
89
1.20k
  else
90
1.20k
    data->req.maxdownload = -1;
91
2.64k
  return CURLE_OK;
92
2.76k
}
93
94
#endif