Coverage Report

Created: 2026-01-17 06:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/escape.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
/* Escape and unescape URL encoding in strings. The functions return a new
25
 * allocated string or NULL if an error occurred. */
26
#include "curl_setup.h"
27
28
struct Curl_easy;
29
30
#include "urldata.h"
31
#include "escape.h"
32
#include "curlx/strparse.h"
33
#include "curl_printf.h"
34
35
/* for ABI-compatibility with previous versions */
36
char *curl_escape(const char *string, int inlength)
37
0
{
38
0
  return curl_easy_escape(NULL, string, inlength);
39
0
}
40
41
/* for ABI-compatibility with previous versions */
42
char *curl_unescape(const char *string, int length)
43
0
{
44
0
  return curl_easy_unescape(NULL, string, length, NULL);
45
0
}
46
47
/* Escapes for URL the given unescaped string of given length.
48
 * 'data' is ignored since 7.82.0.
49
 */
50
char *curl_easy_escape(CURL *data, const char *string, int inlength)
51
0
{
52
0
  size_t length;
53
0
  struct dynbuf d;
54
0
  (void)data;
55
56
0
  if(!string || (inlength < 0))
57
0
    return NULL;
58
59
0
  length = (inlength ? (size_t)inlength : strlen(string));
60
0
  if(!length)
61
0
    return curlx_strdup("");
62
63
0
  if(length > SIZE_MAX / 16)
64
0
    return NULL;
65
66
0
  curlx_dyn_init(&d, length * 3 + 1);
67
68
0
  while(length--) {
69
    /* treat the characters unsigned */
70
0
    unsigned char in = (unsigned char)*string++;
71
72
0
    if(ISUNRESERVED(in)) {
73
      /* append this */
74
0
      if(curlx_dyn_addn(&d, &in, 1))
75
0
        return NULL;
76
0
    }
77
0
    else {
78
      /* encode it */
79
0
      unsigned char out[3] = { '%' };
80
0
      Curl_hexbyte(&out[1], in);
81
0
      if(curlx_dyn_addn(&d, out, 3))
82
0
        return NULL;
83
0
    }
84
0
  }
85
86
0
  return curlx_dyn_ptr(&d);
87
0
}
88
89
/*
90
 * Curl_urldecode() URL decodes the given string.
91
 *
92
 * Returns a pointer to a malloced string in *ostring with length given in
93
 * *olen. If length == 0, the length is assumed to be strlen(string).
94
 *
95
 * ctrl options:
96
 * - REJECT_NADA: accept everything
97
 * - REJECT_CTRL: rejects control characters (byte codes lower than 32) in
98
 *                the data
99
 * - REJECT_ZERO: rejects decoded zero bytes
100
 *
101
 * The values for the enum starts at 2, to make the assert detect legacy
102
 * invokes that used TRUE/FALSE (0 and 1).
103
 */
104
105
CURLcode Curl_urldecode(const char *string, size_t length,
106
                        char **ostring, size_t *olen,
107
                        enum urlreject ctrl)
108
177
{
109
177
  size_t alloc;
110
177
  char *ns;
111
112
177
  DEBUGASSERT(string);
113
177
  DEBUGASSERT(ctrl >= REJECT_NADA); /* crash on TRUE/FALSE */
114
115
177
  alloc = (length ? length : strlen(string));
116
177
  ns = curlx_malloc(alloc + 1);
117
118
177
  if(!ns)
119
0
    return CURLE_OUT_OF_MEMORY;
120
121
  /* store output string */
122
177
  *ostring = ns;
123
124
38.0k
  while(alloc) {
125
37.8k
    unsigned char in = (unsigned char)*string;
126
37.8k
    if(('%' == in) && (alloc > 2) &&
127
14.8k
       ISXDIGIT(string[1]) && ISXDIGIT(string[2])) {
128
      /* this is two hexadecimal digits following a '%' */
129
2.62k
      in = (unsigned char)((curlx_hexval(string[1]) << 4) |
130
2.62k
                           curlx_hexval(string[2]));
131
2.62k
      string += 3;
132
2.62k
      alloc -= 3;
133
2.62k
    }
134
35.2k
    else {
135
35.2k
      string++;
136
35.2k
      alloc--;
137
35.2k
    }
138
139
37.8k
    if(((ctrl == REJECT_CTRL) && (in < 0x20)) ||
140
37.8k
       ((ctrl == REJECT_ZERO) && (in == 0))) {
141
3
      Curl_safefree(*ostring);
142
3
      return CURLE_URL_MALFORMAT;
143
3
    }
144
145
37.8k
    *ns++ = (char)in;
146
37.8k
  }
147
174
  *ns = 0; /* terminate it */
148
149
174
  if(olen)
150
    /* store output size */
151
0
    *olen = ns - *ostring;
152
153
174
  return CURLE_OK;
154
177
}
155
156
/*
157
 * Unescapes the given URL escaped string of given length. Returns a
158
 * pointer to a malloced string with length given in *olen.
159
 * If length == 0, the length is assumed to be strlen(string).
160
 * If olen == NULL, no output length is stored.
161
 * 'data' is ignored since 7.82.0.
162
 */
163
char *curl_easy_unescape(CURL *data, const char *string, int length, int *olen)
164
0
{
165
0
  char *str = NULL;
166
0
  (void)data;
167
0
  if(string && (length >= 0)) {
168
0
    size_t inputlen = (size_t)length;
169
0
    size_t outputlen;
170
0
    CURLcode res = Curl_urldecode(string, inputlen, &str, &outputlen,
171
0
                                  REJECT_NADA);
172
0
    if(res)
173
0
      return NULL;
174
175
0
    if(olen) {
176
0
      if(outputlen <= (size_t)INT_MAX)
177
0
        *olen = curlx_uztosi(outputlen);
178
0
      else
179
        /* too large to return in an int, fail! */
180
0
        Curl_safefree(str);
181
0
    }
182
0
  }
183
0
  return str;
184
0
}
185
186
/* For operating systems/environments that use different malloc/free
187
   systems for the app and for this library, we provide a free that uses
188
   the library's memory system */
189
void curl_free(void *p)
190
0
{
191
0
  curlx_free(p);
192
0
}
193
194
/*
195
 * Curl_hexencode()
196
 *
197
 * Converts binary input to lowercase hex-encoded ASCII output.
198
 * Null-terminated.
199
 */
200
void Curl_hexencode(const unsigned char *src, size_t len, /* input length */
201
                    unsigned char *out, size_t olen) /* output buffer size */
202
0
{
203
0
  DEBUGASSERT(src && len && (olen >= 3));
204
0
  if(src && len && (olen >= 3)) {
205
0
    while(len-- && (olen >= 3)) {
206
0
      out[0] = Curl_ldigits[*src >> 4];
207
0
      out[1] = Curl_ldigits[*src & 0x0F];
208
0
      ++src;
209
0
      out += 2;
210
0
      olen -= 2;
211
0
    }
212
0
    *out = 0;
213
0
  }
214
0
  else if(olen)
215
0
    *out = 0;
216
0
}
217
218
/* Curl_hexbyte
219
 *
220
 * Output a single unsigned char as a two-digit UPPERCASE hex number.
221
 */
222
void Curl_hexbyte(unsigned char *dest, /* must fit two bytes */
223
                  unsigned char val)
224
0
{
225
0
  dest[0] = Curl_udigits[val >> 4];
226
0
  dest[1] = Curl_udigits[val & 0x0F];
227
0
}