1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2007, 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 * $Id: escape.c,v 1.1.1.1 2008-09-23 16:32:05 hoffman Exp $
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 /* urldata.h and easyif.h are included for Curl_convert_... prototypes */
39 #define _MPRINTF_REPLACE /* use our functions only */
40 #include <curl/mprintf.h>
42 /* The last #include file should be: */
45 /* for ABI-compatibility with previous versions */
46 char *curl_escape(const char *string
, int inlength
)
48 return curl_easy_escape(NULL
, string
, inlength
);
51 /* for ABI-compatibility with previous versions */
52 char *curl_unescape(const char *string
, int length
)
54 return curl_easy_unescape(NULL
, string
, length
, NULL
);
57 char *curl_easy_escape(CURL
*handle
, const char *string
, int inlength
)
59 size_t alloc
= (inlength
?(size_t)inlength
:strlen(string
))+1;
61 char *testing_ptr
= NULL
;
62 unsigned char in
; /* we need to treat the characters unsigned */
63 size_t newlen
= alloc
;
67 #ifndef CURL_DOES_CONVERSIONS
68 /* avoid compiler warnings */
79 /* Portable character check (remember EBCDIC). Do not use isalnum() because
80 its behavior is altered by the current locale. */
83 case '0': case '1': case '2': case '3': case '4':
84 case '5': case '6': case '7': case '8': case '9':
85 case 'a': case 'b': case 'c': case 'd': case 'e':
86 case 'f': case 'g': case 'h': case 'i': case 'j':
87 case 'k': case 'l': case 'm': case 'n': case 'o':
88 case 'p': case 'q': case 'r': case 's': case 't':
89 case 'u': case 'v': case 'w': case 'x': case 'y': case 'z':
90 case 'A': case 'B': case 'C': case 'D': case 'E':
91 case 'F': case 'G': case 'H': case 'I': case 'J':
92 case 'K': case 'L': case 'M': case 'N': case 'O':
93 case 'P': case 'Q': case 'R': case 'S': case 'T':
94 case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z':
100 newlen
+= 2; /* the size grows with two, since this'll become a %XX */
103 testing_ptr
= realloc(ns
, alloc
);
113 #ifdef CURL_DOES_CONVERSIONS
114 /* escape sequences are always in ASCII so convert them on non-ASCII hosts */
116 (Curl_convert_to_network(handle
, &in
, 1) != CURLE_OK
)) {
117 /* Curl_convert_to_network calls failf if unsuccessful */
121 #endif /* CURL_DOES_CONVERSIONS */
123 snprintf(&ns
[strindex
], 4, "%%%02X", in
);
130 ns
[strindex
]=0; /* terminate it */
134 char *curl_easy_unescape(CURL
*handle
, const char *string
, int length
,
137 int alloc
= (length
?length
:(int)strlen(string
))+1;
138 char *ns
= malloc(alloc
);
143 #ifndef CURL_DOES_CONVERSIONS
144 /* avoid compiler warnings */
152 if(('%' == in
) && ISXDIGIT(string
[1]) && ISXDIGIT(string
[2])) {
153 /* this is two hexadecimal digits following a '%' */
156 hexstr
[0] = string
[1];
157 hexstr
[1] = string
[2];
160 hex
= strtol(hexstr
, &ptr
, 16);
162 in
= (unsigned char)hex
; /* this long is never bigger than 255 anyway */
164 #ifdef CURL_DOES_CONVERSIONS
165 /* escape sequences are always in ASCII so convert them on non-ASCII hosts */
167 (Curl_convert_from_network(handle
, &in
, 1) != CURLE_OK
)) {
168 /* Curl_convert_from_network calls failf if unsuccessful */
172 #endif /* CURL_DOES_CONVERSIONS */
181 ns
[strindex
]=0; /* terminate it */
184 /* store output size */
189 /* For operating systems/environments that use different malloc/free
190 ssystems for the app and for this library, we provide a free that uses
191 the library's memory system */
192 void curl_free(void *p
)