1 /*****************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * The contents of this file are subject to the Mozilla Public License
9 * Version 1.0 (the "License"); you may not use this file except in
10 * compliance with the License. You may obtain a copy of the License at
11 * http://www.mozilla.org/MPL/
13 * Software distributed under the License is distributed on an "AS IS"
14 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
15 * License for the specific language governing rights and limitations
18 * The Original Code is Curl.
20 * The Initial Developer of the Original Code is Daniel Stenberg.
22 * Portions created by the Initial Developer are Copyright (C) 1998.
23 * All Rights Reserved.
25 * ------------------------------------------------------------
27 * - Daniel Stenberg <daniel@haxx.se>
38 * ------------------------------------------------------------
39 ****************************************************************************/
41 /* Escape and unescape URL encoding in strings. The functions return a new
42 * allocated string or NULL if an error occurred. */
45 #include <curl/curl.h>
51 /* The last #include file should be: */
56 char *curl_escape(char *string)
58 int alloc=strlen(string)+1;
59 char *ns = malloc(alloc);
68 else if(!(in >= 'a' && in <= 'z') &&
69 !(in >= 'A' && in <= 'Z') &&
70 !(in >= '0' && in <= '9')) {
72 newlen += 2; /* the size grows with two, since this'll become a %XX */
75 ns = realloc(ns, alloc);
79 sprintf(&ns[index], "%%%02X", in);
88 ns[index]=0; /* terminate it */
92 char *curl_unescape(char *string, int length)
94 int alloc = (length?length:strlen(string))+1;
95 char *ns = malloc(alloc);
99 char querypart=FALSE; /* everything to the right of a '?' letter is
100 the "query part" where '+' should become ' '.
101 RFC 2316, section 3.10 */
105 if(querypart && ('+' == in))
107 else if(!querypart && ('?' == in)) {
108 /* we have "walked in" to the query part */
113 if(sscanf(string+1, "%02X", &hex)) {
123 ns[index]=0; /* terminate it */