1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2004, 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>
36 /* The last #include file should be: */
39 char *curl_escape(const char *string, int length)
41 size_t alloc = (length?(size_t)length:strlen(string))+1;
43 char *testing_ptr = NULL;
45 size_t newlen = alloc;
55 if(!(in >= 'a' && in <= 'z') &&
56 !(in >= 'A' && in <= 'Z') &&
57 !(in >= '0' && in <= '9')) {
59 newlen += 2; /* the size grows with two, since this'll become a %XX */
62 testing_ptr = realloc(ns, alloc);
71 sprintf(&ns[strindex], "%%%02X", in);
81 ns[strindex]=0; /* terminate it */
85 #define ishex(in) ((in >= 'a' && in <= 'f') || \
86 (in >= 'A' && in <= 'F') || \
87 (in >= '0' && in <= '9'))
89 char *curl_unescape(const char *string, int length)
91 int alloc = (length?length:(int)strlen(string))+1;
92 char *ns = malloc(alloc);
102 if(('%' == in) && ishex(string[1]) && ishex(string[2])) {
103 /* this is two hexadecimal digits following a '%' */
106 hexstr[0] = string[1];
107 hexstr[1] = string[2];
110 hex = strtol(hexstr, &ptr, 16);
112 in = (unsigned char)hex; /* this long is never bigger than 255 anyway */
120 ns[strindex]=0; /* terminate it */
124 /* For operating systems/environments that use different malloc/free
125 ssystems for the app and for this library, we provide a free that uses
126 the library's memory system */
127 void curl_free(void *p)