1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2003, 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.
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);
44 char *testing_ptr = NULL;
52 if(!(in >= 'a' && in <= 'z') &&
53 !(in >= 'A' && in <= 'Z') &&
54 !(in >= '0' && in <= '9')) {
56 newlen += 2; /* the size grows with two, since this'll become a %XX */
59 testing_ptr = realloc(ns, alloc);
68 sprintf(&ns[index], "%%%02X", in);
78 ns[index]=0; /* terminate it */
82 char *curl_unescape(const char *string, int length)
84 int alloc = (length?length:(int)strlen(string))+1;
85 char *ns = malloc(alloc);
98 if(sscanf(string+1, "%02X", &hex)) {
108 ns[index]=0; /* terminate it */
112 void curl_free(void *p)