1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 * SPDX-License-Identifier: curl
23 ***************************************************************************/
25 /* Escape and unescape URL encoding in strings. The functions return a new
26 * allocated string or NULL if an error occurred. */
28 #include "curl_setup.h"
30 #include <curl/curl.h>
36 /* The last 3 #include files should be in this order */
37 #include "curl_printf.h"
38 #include "curl_memory.h"
41 /* Portable character check (remember EBCDIC). Do not use isalnum() because
42 its behavior is altered by the current locale.
43 See https://datatracker.ietf.org/doc/html/rfc3986#section-2.3
45 bool Curl_isunreserved(unsigned char in)
48 case '0': case '1': case '2': case '3': case '4':
49 case '5': case '6': case '7': case '8': case '9':
50 case 'a': case 'b': case 'c': case 'd': case 'e':
51 case 'f': case 'g': case 'h': case 'i': case 'j':
52 case 'k': case 'l': case 'm': case 'n': case 'o':
53 case 'p': case 'q': case 'r': case 's': case 't':
54 case 'u': case 'v': case 'w': case 'x': case 'y': case 'z':
55 case 'A': case 'B': case 'C': case 'D': case 'E':
56 case 'F': case 'G': case 'H': case 'I': case 'J':
57 case 'K': case 'L': case 'M': case 'N': case 'O':
58 case 'P': case 'Q': case 'R': case 'S': case 'T':
59 case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z':
60 case '-': case '.': case '_': case '~':
68 /* for ABI-compatibility with previous versions */
69 char *curl_escape(const char *string, int inlength)
71 return curl_easy_escape(NULL, string, inlength);
74 /* for ABI-compatibility with previous versions */
75 char *curl_unescape(const char *string, int length)
77 return curl_easy_unescape(NULL, string, length, NULL);
80 /* Escapes for URL the given unescaped string of given length.
81 * 'data' is ignored since 7.82.0.
83 char *curl_easy_escape(struct Curl_easy *data, const char *string,
93 Curl_dyn_init(&d, CURL_MAX_INPUT_LENGTH * 3);
95 length = (inlength?(size_t)inlength:strlen(string));
100 unsigned char in = *string; /* we need to treat the characters unsigned */
102 if(Curl_isunreserved(in)) {
104 if(Curl_dyn_addn(&d, &in, 1))
109 if(Curl_dyn_addf(&d, "%%%02X", in))
115 return Curl_dyn_ptr(&d);
119 * Curl_urldecode() URL decodes the given string.
121 * Returns a pointer to a malloced string in *ostring with length given in
122 * *olen. If length == 0, the length is assumed to be strlen(string).
125 * - REJECT_NADA: accept everything
126 * - REJECT_CTRL: rejects control characters (byte codes lower than 32) in
128 * - REJECT_ZERO: rejects decoded zero bytes
130 * The values for the enum starts at 2, to make the assert detect legacy
131 * invokes that used TRUE/FALSE (0 and 1).
134 CURLcode Curl_urldecode(const char *string, size_t length,
135 char **ostring, size_t *olen,
144 DEBUGASSERT(ctrl >= REJECT_NADA); /* crash on TRUE/FALSE */
146 alloc = (length?length:strlen(string)) + 1;
150 return CURLE_OUT_OF_MEMORY;
153 unsigned char in = *string;
154 if(('%' == in) && (alloc > 2) &&
155 ISXDIGIT(string[1]) && ISXDIGIT(string[2])) {
156 /* this is two hexadecimal digits following a '%' */
159 hexstr[0] = string[1];
160 hexstr[1] = string[2];
163 hex = strtoul(hexstr, &ptr, 16);
165 in = curlx_ultouc(hex); /* this long is never bigger than 255 anyway */
171 if(((ctrl == REJECT_CTRL) && (in < 0x20)) ||
172 ((ctrl == REJECT_ZERO) && (in == 0))) {
174 return CURLE_URL_MALFORMAT;
180 ns[strindex] = 0; /* terminate it */
183 /* store output size */
186 /* store output string */
193 * Unescapes the given URL escaped string of given length. Returns a
194 * pointer to a malloced string with length given in *olen.
195 * If length == 0, the length is assumed to be strlen(string).
196 * If olen == NULL, no output length is stored.
197 * 'data' is ignored since 7.82.0.
199 char *curl_easy_unescape(struct Curl_easy *data, const char *string,
200 int length, int *olen)
205 size_t inputlen = length;
207 CURLcode res = Curl_urldecode(string, inputlen, &str, &outputlen,
213 if(outputlen <= (size_t) INT_MAX)
214 *olen = curlx_uztosi(outputlen);
216 /* too large to return in an int, fail! */
223 /* For operating systems/environments that use different malloc/free
224 systems for the app and for this library, we provide a free that uses
225 the library's memory system */
226 void curl_free(void *p)