/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 | | #include "curl_setup.h" |
25 | | |
26 | | #include "curl_range.h" |
27 | | #include "curl_trc.h" |
28 | | #include "curlx/strparse.h" |
29 | | |
30 | | /* Only include this function if one or more of FTP, FILE are enabled. */ |
31 | | #if !defined(CURL_DISABLE_FTP) || !defined(CURL_DISABLE_FILE) |
32 | | |
33 | | /* Check if this is a range download, and if so, set the internal variables |
34 | | properly. */ |
35 | | CURLcode Curl_range(struct Curl_easy *data) |
36 | 0 | { |
37 | 0 | if(data->state.use_range && data->state.range) { |
38 | 0 | curl_off_t from, to; |
39 | 0 | bool first_num = TRUE; |
40 | 0 | const char *p = data->state.range; |
41 | 0 | if(curlx_str_number(&p, &from, CURL_OFF_T_MAX)) |
42 | 0 | first_num = FALSE; |
43 | |
|
44 | 0 | if(curlx_str_single(&p, '-')) |
45 | | /* no leading dash or after the first number is an error */ |
46 | 0 | return CURLE_RANGE_ERROR; |
47 | | |
48 | 0 | if(curlx_str_number(&p, &to, CURL_OFF_T_MAX)) { |
49 | | /* no second number */ |
50 | | /* X - */ |
51 | 0 | data->state.resume_from = from; |
52 | 0 | DEBUGF(infof(data, "RANGE %" FMT_OFF_T " to end of file", from)); |
53 | 0 | } |
54 | 0 | else if(!first_num) { |
55 | | /* -Y */ |
56 | 0 | if(!to) |
57 | | /* "-0" is wrong */ |
58 | 0 | return CURLE_RANGE_ERROR; |
59 | | |
60 | 0 | data->req.maxdownload = to; |
61 | 0 | data->state.resume_from = -to; |
62 | 0 | DEBUGF(infof(data, "RANGE the last %" FMT_OFF_T " bytes", to)); |
63 | 0 | } |
64 | 0 | else { |
65 | | /* X-Y */ |
66 | 0 | curl_off_t totalsize; |
67 | | |
68 | | /* Ensure the range is sensible - to should follow from. */ |
69 | 0 | if(from > to) |
70 | 0 | return CURLE_RANGE_ERROR; |
71 | | |
72 | 0 | totalsize = to - from; |
73 | 0 | if(totalsize == CURL_OFF_T_MAX) |
74 | 0 | return CURLE_RANGE_ERROR; |
75 | | |
76 | 0 | data->req.maxdownload = totalsize + 1; /* include last byte */ |
77 | 0 | data->state.resume_from = from; |
78 | 0 | DEBUGF(infof(data, "RANGE from %" FMT_OFF_T |
79 | 0 | " getting %" FMT_OFF_T " bytes", |
80 | 0 | from, data->req.maxdownload)); |
81 | 0 | } |
82 | 0 | DEBUGF(infof(data, "range-download from %" FMT_OFF_T |
83 | 0 | " to %" FMT_OFF_T ", totally %" FMT_OFF_T " bytes", |
84 | 0 | from, to, data->req.maxdownload)); |
85 | 0 | } |
86 | 0 | else |
87 | 0 | data->req.maxdownload = -1; |
88 | 0 | return CURLE_OK; |
89 | 0 | } |
90 | | |
91 | | #endif |