/***************************************************************************
 *                                  _   _ ____  _
 *  Project                     ___| | | |  _ \| |
 *                             / __| | | | |_) | |
 *                            | (__| |_| |  _ <| |___
 *                             \___|\___/|_| \_\_____|
 *
 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
 *
 * This software is licensed as described in the file COPYING, which
 * you should have received as part of this distribution. The terms
 * are also available at https://curl.se/docs/copyright.html.
 *
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
 * copies of the Software, and permit persons to whom the Software is
 * furnished to do so, under the terms of the COPYING file.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 * SPDX-License-Identifier: curl
 *
 ***************************************************************************/
#include "curl_setup.h"

#include "curl_range.h"
#include "curl_trc.h"
#include "curlx/strparse.h"

/* Only include this function if one or more of FTP, FILE are enabled. */
#if !defined(CURL_DISABLE_FTP) || !defined(CURL_DISABLE_FILE)

/* Check if this is a range download, and if so, set the internal variables
   properly. */
CURLcode Curl_range(struct Curl_easy *data)
{
  if(data->state.use_range && data->state.range) {
    curl_off_t from, to;
    bool first_num = TRUE;
    const char *p = data->state.range;
    if(curlx_str_number(&p, &from, CURL_OFF_T_MAX))
      first_num = FALSE;

    if(curlx_str_single(&p, '-'))
      /* no leading dash or after the first number is an error */
      return CURLE_RANGE_ERROR;

    if(curlx_str_number(&p, &to, CURL_OFF_T_MAX)) {
      /* no second number */
      /* X - */
