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