1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2011, 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 http://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. */
28 #include <curl/curl.h>
33 #include "curl_memory.h"
36 #include "non-ascii.h"
38 #define _MPRINTF_REPLACE /* use our functions only */
39 #include <curl/mprintf.h>
41 /* The last #include file should be: */
44 /* Portable character check (remember EBCDIC). Do not use isalnum() because
45 its behavior is altered by the current locale.
46 See http://tools.ietf.org/html/rfc3986#section-2.3
48 static bool Curl_isunreserved(unsigned char in)
51 case '0': case '1': case '2': case '3': case '4':
52 case '5': case '6': case '7': case '8': case '9':
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 'A': case 'B': case 'C': case 'D': case 'E':
59 case 'F': case 'G': case 'H': case 'I': case 'J':
60 case 'K': case 'L': case 'M': case 'N': case 'O':
61 case 'P': case 'Q': case 'R': case 'S': case 'T':
62 case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z':
63 case '-': case '.': case '_': case '~':
71 /* for ABI-compatibility with previous versions */
72 char *curl_escape(const char *string, int inlength)
74 return curl_easy_escape(NULL, string, inlength);
77 /* for ABI-compatibility with previous versions */
78 char *curl_unescape(const char *string, int length)
80 return curl_easy_unescape(NULL, string, length, NULL);
83 char *curl_easy_escape(CURL *handle, const char *string, int inlength)
85 size_t alloc = (inlength?(size_t)inlength:strlen(string))+1;
87 char *testing_ptr = NULL;
88 unsigned char in; /* we need to treat the characters unsigned */
89 size_t newlen = alloc;
102 if(Curl_isunreserved(in))
107 newlen += 2; /* the size grows with two, since this'll become a %XX */
110 testing_ptr = realloc(ns, alloc);
120 res = Curl_convert_to_network(handle, &in, 1);
122 /* Curl_convert_to_network calls failf if unsuccessful */
127 snprintf(&ns[strindex], 4, "%%%02X", in);
133 ns[strindex]=0; /* terminate it */
138 * Unescapes the given URL escaped string of given length. Returns a
139 * pointer to a malloced string with length given in *olen.
140 * If length == 0, the length is assumed to be strlen(string).
141 * If olen == NULL, no output length is stored.
143 char *curl_easy_unescape(CURL *handle, const char *string, int length,
146 int alloc = (length?length:(int)strlen(string))+1;
147 char *ns = malloc(alloc);
158 if(('%' == in) && ISXDIGIT(string[1]) && ISXDIGIT(string[2])) {
159 /* this is two hexadecimal digits following a '%' */
162 hexstr[0] = string[1];
163 hexstr[1] = string[2];
166 hex = strtoul(hexstr, &ptr, 16);
168 in = curlx_ultouc(hex); /* this long is never bigger than 255 anyway */
170 res = Curl_convert_from_network(handle, &in, 1);
172 /* Curl_convert_from_network calls failf if unsuccessful */
184 ns[strindex]=0; /* terminate it */
187 /* store output size */
192 /* For operating systems/environments that use different malloc/free
193 systems for the app and for this library, we provide a free that uses
194 the library's memory system */
195 void curl_free(void *p)