ff5887508e60b6d74e6dd9ab3b1640658a777cb8
[platform/upstream/cmake.git] / Utilities / cmcurl / lib / escape.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2022, 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  ***************************************************************************/
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 "escape.h"
33 #include "strdup.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://datatracker.ietf.org/doc/html/rfc3986#section-2.3
42 */
43 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 */
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 */
73 char *curl_unescape(const char *string, int length)
74 {
75   return curl_easy_unescape(NULL, string, length, NULL);
76 }
77
78 char *curl_easy_escape(struct Curl_easy *data, const char *string,
79                        int inlength)
80 {
81   size_t length;
82   struct dynbuf d;
83   (void)data;
84
85   if(inlength < 0)
86     return NULL;
87
88   Curl_dyn_init(&d, CURL_MAX_INPUT_LENGTH * 3);
89
90   length = (inlength?(size_t)inlength:strlen(string));
91   if(!length)
92     return strdup("");
93
94   while(length--) {
95     unsigned char in = *string; /* we need to treat the characters unsigned */
96
97     if(Curl_isunreserved(in)) {
98       /* append this */
99       if(Curl_dyn_addn(&d, &in, 1))
100         return NULL;
101     }
102     else {
103       /* encode it */
104       if(Curl_dyn_addf(&d, "%%%02X", in))
105         return NULL;
106     }
107     string++;
108   }
109
110   return Curl_dyn_ptr(&d);
111 }
112
113 /*
114  * Curl_urldecode() URL decodes the given string.
115  *
116  * Returns a pointer to a malloced string in *ostring with length given in
117  * *olen. If length == 0, the length is assumed to be strlen(string).
118  *
119  * 'data' can be set to NULL
120  *
121  * ctrl options:
122  * - REJECT_NADA: accept everything
123  * - REJECT_CTRL: rejects control characters (byte codes lower than 32) in
124  *                the data
125  * - REJECT_ZERO: rejects decoded zero bytes
126  *
127  * The values for the enum starts at 2, to make the assert detect legacy
128  * invokes that used TRUE/FALSE (0 and 1).
129  */
130
131 CURLcode Curl_urldecode(const char *string, size_t length,
132                         char **ostring, size_t *olen,
133                         enum urlreject ctrl)
134 {
135   size_t alloc;
136   char *ns;
137   size_t strindex = 0;
138   unsigned long hex;
139
140   DEBUGASSERT(string);
141   DEBUGASSERT(ctrl >= REJECT_NADA); /* crash on TRUE/FALSE */
142
143   alloc = (length?length:strlen(string)) + 1;
144   ns = malloc(alloc);
145
146   if(!ns)
147     return CURLE_OUT_OF_MEMORY;
148
149   while(--alloc > 0) {
150     unsigned char in = *string;
151     if(('%' == in) && (alloc > 2) &&
152        ISXDIGIT(string[1]) && ISXDIGIT(string[2])) {
153       /* this is two hexadecimal digits following a '%' */
154       char hexstr[3];
155       char *ptr;
156       hexstr[0] = string[1];
157       hexstr[1] = string[2];
158       hexstr[2] = 0;
159
160       hex = strtoul(hexstr, &ptr, 16);
161
162       in = curlx_ultouc(hex); /* this long is never bigger than 255 anyway */
163
164       string += 2;
165       alloc -= 2;
166     }
167
168     if(((ctrl == REJECT_CTRL) && (in < 0x20)) ||
169        ((ctrl == REJECT_ZERO) && (in == 0))) {
170       free(ns);
171       return CURLE_URL_MALFORMAT;
172     }
173
174     ns[strindex++] = in;
175     string++;
176   }
177   ns[strindex] = 0; /* terminate it */
178
179   if(olen)
180     /* store output size */
181     *olen = strindex;
182
183   /* store output string */
184   *ostring = ns;
185
186   return CURLE_OK;
187 }
188
189 /*
190  * Unescapes the given URL escaped string of given length. Returns a
191  * pointer to a malloced string with length given in *olen.
192  * If length == 0, the length is assumed to be strlen(string).
193  * If olen == NULL, no output length is stored.
194  */
195 char *curl_easy_unescape(struct Curl_easy *data, const char *string,
196                          int length, int *olen)
197 {
198   char *str = NULL;
199   (void)data;
200   if(length >= 0) {
201     size_t inputlen = length;
202     size_t outputlen;
203     CURLcode res = Curl_urldecode(string, inputlen, &str, &outputlen,
204                                   REJECT_NADA);
205     if(res)
206       return NULL;
207
208     if(olen) {
209       if(outputlen <= (size_t) INT_MAX)
210         *olen = curlx_uztosi(outputlen);
211       else
212         /* too large to return in an int, fail! */
213         Curl_safefree(str);
214     }
215   }
216   return str;
217 }
218
219 /* For operating systems/environments that use different malloc/free
220    systems for the app and for this library, we provide a free that uses
221    the library's memory system */
222 void curl_free(void *p)
223 {
224   free(p);
225 }