1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2006, 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.2 2007-03-15 19:22:13 andy 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
;
63 size_t newlen
= alloc
;
67 #ifndef CURL_DOES_CONVERSIONS
68 /* avoid compiler warnings */
78 if(!(in
>= 'a' && in
<= 'z') &&
79 !(in
>= 'A' && in
<= 'Z') &&
80 !(in
>= '0' && in
<= '9')) {
82 newlen
+= 2; /* the size grows with two, since this'll become a %XX */
85 testing_ptr
= realloc(ns
, alloc
);
95 #ifdef CURL_DOES_CONVERSIONS
96 /* escape sequences are always in ASCII so convert them on non-ASCII hosts */
98 (Curl_convert_to_network(handle
, &in
, 1) != CURLE_OK
)) {
99 /* Curl_convert_to_network calls failf if unsuccessful */
103 #endif /* CURL_DOES_CONVERSIONS */
105 snprintf(&ns
[strindex
], 4, "%%%02X", in
);
115 ns
[strindex
]=0; /* terminate it */
119 char *curl_easy_unescape(CURL
*handle
, const char *string
, int length
,
122 int alloc
= (length
?length
:(int)strlen(string
))+1;
123 char *ns
= malloc(alloc
);
128 #ifndef CURL_DOES_CONVERSIONS
129 /* avoid compiler warnings */
137 if(('%' == in
) && ISXDIGIT(string
[1]) && ISXDIGIT(string
[2])) {
138 /* this is two hexadecimal digits following a '%' */
141 hexstr
[0] = string
[1];
142 hexstr
[1] = string
[2];
145 hex
= strtol(hexstr
, &ptr
, 16);
147 in
= (unsigned char)hex
; /* this long is never bigger than 255 anyway */
149 #ifdef CURL_DOES_CONVERSIONS
150 /* escape sequences are always in ASCII so convert them on non-ASCII hosts */
152 (Curl_convert_from_network(handle
, &in
, 1) != CURLE_OK
)) {
153 /* Curl_convert_from_network calls failf if unsuccessful */
157 #endif /* CURL_DOES_CONVERSIONS */
166 ns
[strindex
]=0; /* terminate it */
169 /* store output size */
174 /* For operating systems/environments that use different malloc/free
175 ssystems for the app and for this library, we provide a free that uses
176 the library's memory system */
177 void curl_free(void *p
)