1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2015, 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 http://curl.haxx.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 ***************************************************************************/
22
23 /* Escape and unescape URL encoding in strings. The functions return a new
24 * allocated string or NULL if an error occurred. */
25
26 #include "curl_setup.h"
27
28 #include <curl/curl.h>
29
30 #include "urldata.h"
31 #include "warnless.h"
32 #include "non-ascii.h"
33 #include "escape.h"
34 #include "curl_printf.h"
35
36 /* The last #include files should be: */
37 #include "curl_memory.h"
38 #include "memdebug.h"
39
40 /* Portable character check (remember EBCDIC). Do not use isalnum() because
41 its behavior is altered by the current locale.
42 See http://tools.ietf.org/html/rfc3986#section-2.3
43 */
Curl_isunreserved(unsigned char in)44 static bool Curl_isunreserved(unsigned char in)
45 {
46 switch (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 '~':
60 return TRUE;
61 default:
62 break;
63 }
64 return FALSE;
65 }
66
67 /* for ABI-compatibility with previous versions */
curl_escape(const char * string,int inlength)68 char *curl_escape(const char *string, int inlength)
69 {
70 return curl_easy_escape(NULL, string, inlength);
71 }
72
73 /* for ABI-compatibility with previous versions */
curl_unescape(const char * string,int length)74 char *curl_unescape(const char *string, int length)
75 {
76 return curl_easy_unescape(NULL, string, length, NULL);
77 }
78
curl_easy_escape(CURL * handle,const char * string,int inlength)79 char *curl_easy_escape(CURL *handle, const char *string, int inlength)
80 {
81 size_t alloc = (inlength?(size_t)inlength:strlen(string))+1;
82 char *ns;
83 char *testing_ptr = NULL;
84 unsigned char in; /* we need to treat the characters unsigned */
85 size_t newlen = alloc;
86 size_t strindex=0;
87 size_t length;
88 CURLcode result;
89
90 ns = malloc(alloc);
91 if(!ns)
92 return NULL;
93
94 length = alloc-1;
95 while(length--) {
96 in = *string;
97
98 if(Curl_isunreserved(in))
99 /* just copy this */
100 ns[strindex++]=in;
101 else {
102 /* encode it */
103 newlen += 2; /* the size grows with two, since this'll become a %XX */
104 if(newlen > alloc) {
105 alloc *= 2;
106 testing_ptr = realloc(ns, alloc);
107 if(!testing_ptr) {
108 free( ns );
109 return NULL;
110 }
111 else {
112 ns = testing_ptr;
113 }
114 }
115
116 result = Curl_convert_to_network(handle, &in, 1);
117 if(result) {
118 /* Curl_convert_to_network calls failf if unsuccessful */
119 free(ns);
120 return NULL;
121 }
122
123 snprintf(&ns[strindex], 4, "%%%02X", in);
124
125 strindex+=3;
126 }
127 string++;
128 }
129 ns[strindex]=0; /* terminate it */
130 return ns;
131 }
132
133 /*
134 * Curl_urldecode() URL decodes the given string.
135 *
136 * Optionally detects control characters (byte codes lower than 32) in the
137 * data and rejects such data.
138 *
139 * Returns a pointer to a malloced string in *ostring with length given in
140 * *olen. If length == 0, the length is assumed to be strlen(string).
141 *
142 */
Curl_urldecode(struct SessionHandle * data,const char * string,size_t length,char ** ostring,size_t * olen,bool reject_ctrl)143 CURLcode Curl_urldecode(struct SessionHandle *data,
144 const char *string, size_t length,
145 char **ostring, size_t *olen,
146 bool reject_ctrl)
147 {
148 size_t alloc = (length?length:strlen(string))+1;
149 char *ns = malloc(alloc);
150 unsigned char in;
151 size_t strindex=0;
152 unsigned long hex;
153 CURLcode result;
154
155 if(!ns)
156 return CURLE_OUT_OF_MEMORY;
157
158 while(--alloc > 0) {
159 in = *string;
160 if(('%' == in) && (alloc > 2) &&
161 ISXDIGIT(string[1]) && ISXDIGIT(string[2])) {
162 /* this is two hexadecimal digits following a '%' */
163 char hexstr[3];
164 char *ptr;
165 hexstr[0] = string[1];
166 hexstr[1] = string[2];
167 hexstr[2] = 0;
168
169 hex = strtoul(hexstr, &ptr, 16);
170
171 in = curlx_ultouc(hex); /* this long is never bigger than 255 anyway */
172
173 result = Curl_convert_from_network(data, &in, 1);
174 if(result) {
175 /* Curl_convert_from_network calls failf if unsuccessful */
176 free(ns);
177 return result;
178 }
179
180 string+=2;
181 alloc-=2;
182 }
183
184 if(reject_ctrl && (in < 0x20)) {
185 free(ns);
186 return CURLE_URL_MALFORMAT;
187 }
188
189 ns[strindex++] = in;
190 string++;
191 }
192 ns[strindex]=0; /* terminate it */
193
194 if(olen)
195 /* store output size */
196 *olen = strindex;
197
198 /* store output string */
199 *ostring = ns;
200
201 return CURLE_OK;
202 }
203
204 /*
205 * Unescapes the given URL escaped string of given length. Returns a
206 * pointer to a malloced string with length given in *olen.
207 * If length == 0, the length is assumed to be strlen(string).
208 * If olen == NULL, no output length is stored.
209 */
curl_easy_unescape(CURL * handle,const char * string,int length,int * olen)210 char *curl_easy_unescape(CURL *handle, const char *string, int length,
211 int *olen)
212 {
213 char *str = NULL;
214 size_t inputlen = length;
215 size_t outputlen;
216 CURLcode res = Curl_urldecode(handle, string, inputlen, &str, &outputlen,
217 FALSE);
218 if(res)
219 return NULL;
220 if(olen)
221 *olen = curlx_uztosi(outputlen);
222 return str;
223 }
224
225 /* For operating systems/environments that use different malloc/free
226 systems for the app and for this library, we provide a free that uses
227 the library's memory system */
curl_free(void * p)228 void curl_free(void *p)
229 {
230 free(p);
231 }
232