113498266Sopenharmony_ci/***************************************************************************
213498266Sopenharmony_ci *                                  _   _ ____  _
313498266Sopenharmony_ci *  Project                     ___| | | |  _ \| |
413498266Sopenharmony_ci *                             / __| | | | |_) | |
513498266Sopenharmony_ci *                            | (__| |_| |  _ <| |___
613498266Sopenharmony_ci *                             \___|\___/|_| \_\_____|
713498266Sopenharmony_ci *
813498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
913498266Sopenharmony_ci *
1013498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which
1113498266Sopenharmony_ci * you should have received as part of this distribution. The terms
1213498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html.
1313498266Sopenharmony_ci *
1413498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell
1513498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is
1613498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file.
1713498266Sopenharmony_ci *
1813498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
1913498266Sopenharmony_ci * KIND, either express or implied.
2013498266Sopenharmony_ci *
2113498266Sopenharmony_ci * SPDX-License-Identifier: curl
2213498266Sopenharmony_ci *
2313498266Sopenharmony_ci ***************************************************************************/
2413498266Sopenharmony_ci
2513498266Sopenharmony_ci/* Escape and unescape URL encoding in strings. The functions return a new
2613498266Sopenharmony_ci * allocated string or NULL if an error occurred.  */
2713498266Sopenharmony_ci
2813498266Sopenharmony_ci#include "curl_setup.h"
2913498266Sopenharmony_ci
3013498266Sopenharmony_ci#include <curl/curl.h>
3113498266Sopenharmony_ci
3213498266Sopenharmony_ci#include "urldata.h"
3313498266Sopenharmony_ci#include "warnless.h"
3413498266Sopenharmony_ci#include "escape.h"
3513498266Sopenharmony_ci#include "strdup.h"
3613498266Sopenharmony_ci/* The last 3 #include files should be in this order */
3713498266Sopenharmony_ci#include "curl_printf.h"
3813498266Sopenharmony_ci#include "curl_memory.h"
3913498266Sopenharmony_ci#include "memdebug.h"
4013498266Sopenharmony_ci
4113498266Sopenharmony_ci/* for ABI-compatibility with previous versions */
4213498266Sopenharmony_cichar *curl_escape(const char *string, int inlength)
4313498266Sopenharmony_ci{
4413498266Sopenharmony_ci  return curl_easy_escape(NULL, string, inlength);
4513498266Sopenharmony_ci}
4613498266Sopenharmony_ci
4713498266Sopenharmony_ci/* for ABI-compatibility with previous versions */
4813498266Sopenharmony_cichar *curl_unescape(const char *string, int length)
4913498266Sopenharmony_ci{
5013498266Sopenharmony_ci  return curl_easy_unescape(NULL, string, length, NULL);
5113498266Sopenharmony_ci}
5213498266Sopenharmony_ci
5313498266Sopenharmony_ci/* Escapes for URL the given unescaped string of given length.
5413498266Sopenharmony_ci * 'data' is ignored since 7.82.0.
5513498266Sopenharmony_ci */
5613498266Sopenharmony_cichar *curl_easy_escape(struct Curl_easy *data, const char *string,
5713498266Sopenharmony_ci                       int inlength)
5813498266Sopenharmony_ci{
5913498266Sopenharmony_ci  size_t length;
6013498266Sopenharmony_ci  struct dynbuf d;
6113498266Sopenharmony_ci  (void)data;
6213498266Sopenharmony_ci
6313498266Sopenharmony_ci  if(inlength < 0)
6413498266Sopenharmony_ci    return NULL;
6513498266Sopenharmony_ci
6613498266Sopenharmony_ci  Curl_dyn_init(&d, CURL_MAX_INPUT_LENGTH * 3);
6713498266Sopenharmony_ci
6813498266Sopenharmony_ci  length = (inlength?(size_t)inlength:strlen(string));
6913498266Sopenharmony_ci  if(!length)
7013498266Sopenharmony_ci    return strdup("");
7113498266Sopenharmony_ci
7213498266Sopenharmony_ci  while(length--) {
7313498266Sopenharmony_ci    unsigned char in = *string++; /* treat the characters unsigned */
7413498266Sopenharmony_ci
7513498266Sopenharmony_ci    if(ISUNRESERVED(in)) {
7613498266Sopenharmony_ci      /* append this */
7713498266Sopenharmony_ci      if(Curl_dyn_addn(&d, &in, 1))
7813498266Sopenharmony_ci        return NULL;
7913498266Sopenharmony_ci    }
8013498266Sopenharmony_ci    else {
8113498266Sopenharmony_ci      /* encode it */
8213498266Sopenharmony_ci      const char hex[] = "0123456789ABCDEF";
8313498266Sopenharmony_ci      char out[3]={'%'};
8413498266Sopenharmony_ci      out[1] = hex[in>>4];
8513498266Sopenharmony_ci      out[2] = hex[in & 0xf];
8613498266Sopenharmony_ci      if(Curl_dyn_addn(&d, out, 3))
8713498266Sopenharmony_ci        return NULL;
8813498266Sopenharmony_ci    }
8913498266Sopenharmony_ci  }
9013498266Sopenharmony_ci
9113498266Sopenharmony_ci  return Curl_dyn_ptr(&d);
9213498266Sopenharmony_ci}
9313498266Sopenharmony_ci
9413498266Sopenharmony_cistatic const unsigned char hextable[] = {
9513498266Sopenharmony_ci  0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0,       /* 0x30 - 0x3f */
9613498266Sopenharmony_ci  0, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x40 - 0x4f */
9713498266Sopenharmony_ci  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,       /* 0x50 - 0x5f */
9813498266Sopenharmony_ci  0, 10, 11, 12, 13, 14, 15                             /* 0x60 - 0x66 */
9913498266Sopenharmony_ci};
10013498266Sopenharmony_ci
10113498266Sopenharmony_ci/* the input is a single hex digit */
10213498266Sopenharmony_ci#define onehex2dec(x) hextable[x - '0']
10313498266Sopenharmony_ci
10413498266Sopenharmony_ci/*
10513498266Sopenharmony_ci * Curl_urldecode() URL decodes the given string.
10613498266Sopenharmony_ci *
10713498266Sopenharmony_ci * Returns a pointer to a malloced string in *ostring with length given in
10813498266Sopenharmony_ci * *olen. If length == 0, the length is assumed to be strlen(string).
10913498266Sopenharmony_ci *
11013498266Sopenharmony_ci * ctrl options:
11113498266Sopenharmony_ci * - REJECT_NADA: accept everything
11213498266Sopenharmony_ci * - REJECT_CTRL: rejects control characters (byte codes lower than 32) in
11313498266Sopenharmony_ci *                the data
11413498266Sopenharmony_ci * - REJECT_ZERO: rejects decoded zero bytes
11513498266Sopenharmony_ci *
11613498266Sopenharmony_ci * The values for the enum starts at 2, to make the assert detect legacy
11713498266Sopenharmony_ci * invokes that used TRUE/FALSE (0 and 1).
11813498266Sopenharmony_ci */
11913498266Sopenharmony_ci
12013498266Sopenharmony_ciCURLcode Curl_urldecode(const char *string, size_t length,
12113498266Sopenharmony_ci                        char **ostring, size_t *olen,
12213498266Sopenharmony_ci                        enum urlreject ctrl)
12313498266Sopenharmony_ci{
12413498266Sopenharmony_ci  size_t alloc;
12513498266Sopenharmony_ci  char *ns;
12613498266Sopenharmony_ci
12713498266Sopenharmony_ci  DEBUGASSERT(string);
12813498266Sopenharmony_ci  DEBUGASSERT(ctrl >= REJECT_NADA); /* crash on TRUE/FALSE */
12913498266Sopenharmony_ci
13013498266Sopenharmony_ci  alloc = (length?length:strlen(string));
13113498266Sopenharmony_ci  ns = malloc(alloc + 1);
13213498266Sopenharmony_ci
13313498266Sopenharmony_ci  if(!ns)
13413498266Sopenharmony_ci    return CURLE_OUT_OF_MEMORY;
13513498266Sopenharmony_ci
13613498266Sopenharmony_ci  /* store output string */
13713498266Sopenharmony_ci  *ostring = ns;
13813498266Sopenharmony_ci
13913498266Sopenharmony_ci  while(alloc) {
14013498266Sopenharmony_ci    unsigned char in = *string;
14113498266Sopenharmony_ci    if(('%' == in) && (alloc > 2) &&
14213498266Sopenharmony_ci       ISXDIGIT(string[1]) && ISXDIGIT(string[2])) {
14313498266Sopenharmony_ci      /* this is two hexadecimal digits following a '%' */
14413498266Sopenharmony_ci      in = (unsigned char)(onehex2dec(string[1]) << 4) | onehex2dec(string[2]);
14513498266Sopenharmony_ci
14613498266Sopenharmony_ci      string += 3;
14713498266Sopenharmony_ci      alloc -= 3;
14813498266Sopenharmony_ci    }
14913498266Sopenharmony_ci    else {
15013498266Sopenharmony_ci      string++;
15113498266Sopenharmony_ci      alloc--;
15213498266Sopenharmony_ci    }
15313498266Sopenharmony_ci
15413498266Sopenharmony_ci    if(((ctrl == REJECT_CTRL) && (in < 0x20)) ||
15513498266Sopenharmony_ci       ((ctrl == REJECT_ZERO) && (in == 0))) {
15613498266Sopenharmony_ci      Curl_safefree(*ostring);
15713498266Sopenharmony_ci      return CURLE_URL_MALFORMAT;
15813498266Sopenharmony_ci    }
15913498266Sopenharmony_ci
16013498266Sopenharmony_ci    *ns++ = in;
16113498266Sopenharmony_ci  }
16213498266Sopenharmony_ci  *ns = 0; /* terminate it */
16313498266Sopenharmony_ci
16413498266Sopenharmony_ci  if(olen)
16513498266Sopenharmony_ci    /* store output size */
16613498266Sopenharmony_ci    *olen = ns - *ostring;
16713498266Sopenharmony_ci
16813498266Sopenharmony_ci  return CURLE_OK;
16913498266Sopenharmony_ci}
17013498266Sopenharmony_ci
17113498266Sopenharmony_ci/*
17213498266Sopenharmony_ci * Unescapes the given URL escaped string of given length. Returns a
17313498266Sopenharmony_ci * pointer to a malloced string with length given in *olen.
17413498266Sopenharmony_ci * If length == 0, the length is assumed to be strlen(string).
17513498266Sopenharmony_ci * If olen == NULL, no output length is stored.
17613498266Sopenharmony_ci * 'data' is ignored since 7.82.0.
17713498266Sopenharmony_ci */
17813498266Sopenharmony_cichar *curl_easy_unescape(struct Curl_easy *data, const char *string,
17913498266Sopenharmony_ci                         int length, int *olen)
18013498266Sopenharmony_ci{
18113498266Sopenharmony_ci  char *str = NULL;
18213498266Sopenharmony_ci  (void)data;
18313498266Sopenharmony_ci  if(length >= 0) {
18413498266Sopenharmony_ci    size_t inputlen = (size_t)length;
18513498266Sopenharmony_ci    size_t outputlen;
18613498266Sopenharmony_ci    CURLcode res = Curl_urldecode(string, inputlen, &str, &outputlen,
18713498266Sopenharmony_ci                                  REJECT_NADA);
18813498266Sopenharmony_ci    if(res)
18913498266Sopenharmony_ci      return NULL;
19013498266Sopenharmony_ci
19113498266Sopenharmony_ci    if(olen) {
19213498266Sopenharmony_ci      if(outputlen <= (size_t) INT_MAX)
19313498266Sopenharmony_ci        *olen = curlx_uztosi(outputlen);
19413498266Sopenharmony_ci      else
19513498266Sopenharmony_ci        /* too large to return in an int, fail! */
19613498266Sopenharmony_ci        Curl_safefree(str);
19713498266Sopenharmony_ci    }
19813498266Sopenharmony_ci  }
19913498266Sopenharmony_ci  return str;
20013498266Sopenharmony_ci}
20113498266Sopenharmony_ci
20213498266Sopenharmony_ci/* For operating systems/environments that use different malloc/free
20313498266Sopenharmony_ci   systems for the app and for this library, we provide a free that uses
20413498266Sopenharmony_ci   the library's memory system */
20513498266Sopenharmony_civoid curl_free(void *p)
20613498266Sopenharmony_ci{
20713498266Sopenharmony_ci  free(p);
20813498266Sopenharmony_ci}
20913498266Sopenharmony_ci
21013498266Sopenharmony_ci/*
21113498266Sopenharmony_ci * Curl_hexencode()
21213498266Sopenharmony_ci *
21313498266Sopenharmony_ci * Converts binary input to lowercase hex-encoded ASCII output.
21413498266Sopenharmony_ci * Null-terminated.
21513498266Sopenharmony_ci */
21613498266Sopenharmony_civoid Curl_hexencode(const unsigned char *src, size_t len, /* input length */
21713498266Sopenharmony_ci                    unsigned char *out, size_t olen) /* output buffer size */
21813498266Sopenharmony_ci{
21913498266Sopenharmony_ci  const char *hex = "0123456789abcdef";
22013498266Sopenharmony_ci  DEBUGASSERT(src && len && (olen >= 3));
22113498266Sopenharmony_ci  if(src && len && (olen >= 3)) {
22213498266Sopenharmony_ci    while(len-- && (olen >= 3)) {
22313498266Sopenharmony_ci      /* clang-tidy warns on this line without this comment: */
22413498266Sopenharmony_ci      /* NOLINTNEXTLINE(clang-analyzer-core.UndefinedBinaryOperatorResult) */
22513498266Sopenharmony_ci      *out++ = hex[(*src & 0xF0)>>4];
22613498266Sopenharmony_ci      *out++ = hex[*src & 0x0F];
22713498266Sopenharmony_ci      ++src;
22813498266Sopenharmony_ci      olen -= 2;
22913498266Sopenharmony_ci    }
23013498266Sopenharmony_ci    *out = 0;
23113498266Sopenharmony_ci  }
23213498266Sopenharmony_ci  else if(olen)
23313498266Sopenharmony_ci    *out = 0;
23413498266Sopenharmony_ci}
235