1 /*****************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 2000, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * In order to be useful for every potential user, curl and libcurl are
11 * dual-licensed under the MPL and the MIT/X-derivate licenses.
13 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
14 * copies of the Software, and permit persons to whom the Software is
15 * furnished to do so, under the terms of the MPL or the MIT/X-derivate
16 * licenses. You may pick one of these licenses.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
22 *****************************************************************************/
24 /* Escape and unescape URL encoding in strings. The functions return a new
25 * allocated string or NULL if an error occurred. */
29 #include <curl/curl.h>
35 /* The last #include file should be: */
40 char *curl_escape(const char *string, int length)
42 int alloc = (length?length:(int)strlen(string))+1;
43 char *ns = malloc(alloc);
53 else if(!(in >= 'a' && in <= 'z') &&
54 !(in >= 'A' && in <= 'Z') &&
55 !(in >= '0' && in <= '9')) {
57 newlen += 2; /* the size grows with two, since this'll become a %XX */
60 ns = realloc(ns, alloc);
64 sprintf(&ns[index], "%%%02X", in);
74 ns[index]=0; /* terminate it */
78 char *curl_unescape(const char *string, int length)
80 int alloc = (length?length:(int)strlen(string))+1;
81 char *ns = malloc(alloc);
85 char querypart=FALSE; /* everything to the right of a '?' letter is
86 the "query part" where '+' should become ' '.
87 RFC 2316, section 3.10 */
91 if(querypart && ('+' == in))
93 else if(!querypart && ('?' == in)) {
94 /* we have "walked in" to the query part */
99 if(sscanf(string+1, "%02X", &hex)) {
109 ns[index]=0; /* terminate it */