Coverage Report

Created: 2025-10-30 06:17

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