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