Coverage Report

Created: 2026-03-11 07:23

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
12.7k
{
52
12.7k
  size_t length;
53
12.7k
  struct dynbuf d;
54
12.7k
  (void)data;
55
56
12.7k
  if(!string || (inlength < 0))
57
0
    return NULL;
58
59
12.7k
  length = (inlength ? (size_t)inlength : strlen(string));
60
12.7k
  if(!length)
61
0
    return curlx_strdup("");
62
63
12.7k
  if(length > SIZE_MAX / 16)
64
0
    return NULL;
65
66
12.7k
  curlx_dyn_init(&d, (length * 3) + 1);
67
68
76.2k
  while(length--) {
69
    /* treat the characters unsigned */
70
63.5k
    unsigned char in = (unsigned char)*string++;
71
72
63.5k
    if(ISUNRESERVED(in)) {
73
      /* append this */
74
63.2k
      if(curlx_dyn_addn(&d, &in, 1))
75
0
        return NULL;
76
63.2k
    }
77
297
    else {
78
      /* encode it */
79
297
      unsigned char out[3] = { '%' };
80
297
      Curl_hexbyte(&out[1], in);
81
297
      if(curlx_dyn_addn(&d, out, 3))
82
0
        return NULL;
83
297
    }
84
63.5k
  }
85
86
12.7k
  return curlx_dyn_ptr(&d);
87
12.7k
}
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
113k
{
109
113k
  size_t alloc;
110
113k
  char *ns;
111
112
113k
  DEBUGASSERT(string);
113
113k
  DEBUGASSERT(ctrl >= REJECT_NADA); /* crash on TRUE/FALSE */
114
115
113k
  alloc = (length ? length : strlen(string));
116
113k
  ns = curlx_malloc(alloc + 1);
117
118
113k
  if(!ns)
119
0
    return CURLE_OUT_OF_MEMORY;
120
121
  /* store output string */
122
113k
  *ostring = ns;
123
124
155M
  while(alloc) {
125
155M
    unsigned char in = (unsigned char)*string;
126
155M
    if(('%' == in) && (alloc > 2) &&
127
64.6M
       ISXDIGIT(string[1]) && ISXDIGIT(string[2])) {
128
      /* this is two hexadecimal digits following a '%' */
129
28.5M
      in = (unsigned char)((curlx_hexval(string[1]) << 4) |
130
28.5M
                           curlx_hexval(string[2]));
131
28.5M
      string += 3;
132
28.5M
      alloc -= 3;
133
28.5M
    }
134
126M
    else {
135
126M
      string++;
136
126M
      alloc--;
137
126M
    }
138
139
155M
    if(((ctrl == REJECT_CTRL) && (in < 0x20)) ||
140
155M
       ((ctrl == REJECT_ZERO) && (in == 0))) {
141
287
      Curl_safefree(*ostring);
142
287
      return CURLE_URL_MALFORMAT;
143
287
    }
144
145
155M
    *ns++ = (char)in;
146
155M
  }
147
112k
  *ns = 0; /* terminate it */
148
149
112k
  if(olen)
150
    /* store output size */
151
76.1k
    *olen = ns - *ostring;
152
153
112k
  return CURLE_OK;
154
113k
}
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
169k
{
191
169k
  curlx_free(p);
192
169k
}
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
29.5k
{
203
29.5k
  DEBUGASSERT(src && len && (olen >= 3));
204
29.5k
  if(src && len && (olen >= 3)) {
205
976k
    while(len-- && (olen >= 3)) {
206
947k
      out[0] = Curl_ldigits[*src >> 4];
207
947k
      out[1] = Curl_ldigits[*src & 0x0F];
208
947k
      ++src;
209
947k
      out += 2;
210
947k
      olen -= 2;
211
947k
    }
212
29.5k
    *out = 0;
213
29.5k
  }
214
0
  else if(olen)
215
0
    *out = 0;
216
29.5k
}
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
69.5M
{
225
69.5M
  dest[0] = Curl_udigits[val >> 4];
226
69.5M
  dest[1] = Curl_udigits[val & 0x0F];
227
69.5M
}