1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2016, 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.haxx.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 ***************************************************************************/
23 /* Escape and unescape URL encoding in strings. The functions return a new
24 * allocated string or NULL if an error occurred. */
26 #include "curl_setup.h"
28 #include <curl/curl.h>
32 #include "non-ascii.h"
35 /* The last 3 #include files should be in this order */
36 #include "curl_printf.h"
37 #include "curl_memory.h"
40 /* Portable character check (remember EBCDIC). Do not use isalnum() because
41 its behavior is altered by the current locale.
42 See https://tools.ietf.org/html/rfc3986#section-2.3
44 static bool Curl_isunreserved(unsigned char in)
47 case '0': case '1': case '2': case '3': case '4':
48 case '5': case '6': case '7': case '8': case '9':
49 case 'a': case 'b': case 'c': case 'd': case 'e':
50 case 'f': case 'g': case 'h': case 'i': case 'j':
51 case 'k': case 'l': case 'm': case 'n': case 'o':
52 case 'p': case 'q': case 'r': case 's': case 't':
53 case 'u': case 'v': case 'w': case 'x': case 'y': case 'z':
54 case 'A': case 'B': case 'C': case 'D': case 'E':
55 case 'F': case 'G': case 'H': case 'I': case 'J':
56 case 'K': case 'L': case 'M': case 'N': case 'O':
57 case 'P': case 'Q': case 'R': case 'S': case 'T':
58 case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z':
59 case '-': case '.': case '_': case '~':
67 /* for ABI-compatibility with previous versions */
68 char *curl_escape(const char *string, int inlength)
70 return curl_easy_escape(NULL, string, inlength);
73 /* for ABI-compatibility with previous versions */
74 char *curl_unescape(const char *string, int length)
76 return curl_easy_unescape(NULL, string, length, NULL);
79 char *curl_easy_escape(struct Curl_easy *data, const char *string,
84 char *testing_ptr = NULL;
85 unsigned char in; /* we need to treat the characters unsigned */
94 alloc = (inlength?(size_t)inlength:strlen(string))+1;
105 if(Curl_isunreserved(in))
110 newlen += 2; /* the size grows with two, since this'll become a %XX */
113 testing_ptr = Curl_saferealloc(ns, alloc);
121 result = Curl_convert_to_network(data, &in, 1);
123 /* Curl_convert_to_network calls failf if unsuccessful */
128 snprintf(&ns[strindex], 4, "%%%02X", in);
134 ns[strindex]=0; /* terminate it */
139 * Curl_urldecode() URL decodes the given string.
141 * Optionally detects control characters (byte codes lower than 32) in the
142 * data and rejects such data.
144 * Returns a pointer to a malloced string in *ostring with length given in
145 * *olen. If length == 0, the length is assumed to be strlen(string).
148 CURLcode Curl_urldecode(struct Curl_easy *data,
149 const char *string, size_t length,
150 char **ostring, size_t *olen,
153 size_t alloc = (length?length:strlen(string))+1;
154 char *ns = malloc(alloc);
161 return CURLE_OUT_OF_MEMORY;
165 if(('%' == in) && (alloc > 2) &&
166 ISXDIGIT(string[1]) && ISXDIGIT(string[2])) {
167 /* this is two hexadecimal digits following a '%' */
170 hexstr[0] = string[1];
171 hexstr[1] = string[2];
174 hex = strtoul(hexstr, &ptr, 16);
176 in = curlx_ultouc(hex); /* this long is never bigger than 255 anyway */
178 result = Curl_convert_from_network(data, &in, 1);
180 /* Curl_convert_from_network calls failf if unsuccessful */
189 if(reject_ctrl && (in < 0x20)) {
191 return CURLE_URL_MALFORMAT;
197 ns[strindex]=0; /* terminate it */
200 /* store output size */
203 /* store output string */
210 * Unescapes the given URL escaped string of given length. Returns a
211 * pointer to a malloced string with length given in *olen.
212 * If length == 0, the length is assumed to be strlen(string).
213 * If olen == NULL, no output length is stored.
215 char *curl_easy_unescape(struct Curl_easy *data, const char *string,
216 int length, int *olen)
220 size_t inputlen = length;
222 CURLcode res = Curl_urldecode(data, string, inputlen, &str, &outputlen,
228 if(outputlen <= (size_t) INT_MAX)
229 *olen = curlx_uztosi(outputlen);
231 /* too large to return in an int, fail! */
238 /* For operating systems/environments that use different malloc/free
239 systems for the app and for this library, we provide a free that uses
240 the library's memory system */
241 void curl_free(void *p)