aboutsummaryrefslogtreecommitdiff
path: root/lib/escape.c
diff options
context:
space:
mode:
authorDaniel Stenberg <daniel@haxx.se>2006-04-07 21:50:47 +0000
committerDaniel Stenberg <daniel@haxx.se>2006-04-07 21:50:47 +0000
commit5a4b43848ac21b3d831f00ce11136e20f820f0a0 (patch)
tree981b8520e1f9aa683da489c8a283fb513dd26f16 /lib/escape.c
parentd98869a0889195a9e0d353cf691219f78186ac93 (diff)
First commit of David McCreedy's EBCDIC and TPF changes.
Diffstat (limited to 'lib/escape.c')
-rw-r--r--lib/escape.c54
1 files changed, 52 insertions, 2 deletions
diff --git a/lib/escape.c b/lib/escape.c
index 4466d4cc9..c569902f6 100644
--- a/lib/escape.c
+++ b/lib/escape.c
@@ -5,7 +5,7 @@
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
- * Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al.
+ * Copyright (C) 1998 - 2006, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
@@ -32,6 +32,9 @@
#include <stdlib.h>
#include <string.h>
#include "memory.h"
+/* urldata.h and easyif.h are included for Curl_convert_... prototypes */
+#include "urldata.h"
+#include "easyif.h"
#define _MPRINTF_REPLACE /* use our functions only */
#include <curl/mprintf.h>
@@ -39,8 +42,20 @@
/* The last #include file should be: */
#include "memdebug.h"
+/* for ABI-compatibility with previous versions */
char *curl_escape(const char *string, int inlength)
{
+ return curl_easy_escape(NULL, string, inlength);
+}
+
+/* for ABI-compatibility with previous versions */
+char *curl_unescape(const char *string, int length)
+{
+ return curl_easy_unescape(NULL, string, length, NULL);
+}
+
+char *curl_easy_escape(CURL *handle, const char *string, int inlength)
+{
size_t alloc = (inlength?(size_t)inlength:strlen(string))+1;
char *ns;
char *testing_ptr = NULL;
@@ -49,6 +64,10 @@ char *curl_escape(const char *string, int inlength)
int strindex=0;
size_t length;
+#ifndef CURL_DOES_CONVERSIONS
+ /* avoid compiler warnings */
+ (void)handle;
+#endif
ns = malloc(alloc);
if(!ns)
return NULL;
@@ -72,6 +91,17 @@ char *curl_escape(const char *string, int inlength)
ns = testing_ptr;
}
}
+
+#ifdef CURL_DOES_CONVERSIONS
+/* escape sequences are always in ASCII so convert them on non-ASCII hosts */
+ if (!handle ||
+ (Curl_convert_to_network(handle, &in, 1) != CURLE_OK)) {
+ /* Curl_convert_to_network calls failf if unsuccessful */
+ free(ns);
+ return NULL;
+ }
+#endif /* CURL_DOES_CONVERSIONS */
+
snprintf(&ns[strindex], 4, "%%%02X", in);
strindex+=3;
@@ -90,7 +120,8 @@ char *curl_escape(const char *string, int inlength)
(in >= 'A' && in <= 'F') || \
(in >= '0' && in <= '9'))
-char *curl_unescape(const char *string, int length)
+char *curl_easy_unescape(CURL *handle, const char *string, int length,
+ int *olen)
{
int alloc = (length?length:(int)strlen(string))+1;
char *ns = malloc(alloc);
@@ -98,6 +129,10 @@ char *curl_unescape(const char *string, int length)
int strindex=0;
long hex;
+#ifndef CURL_DOES_CONVERSIONS
+ /* avoid compiler warnings */
+ (void)handle;
+#endif
if( !ns )
return NULL;
@@ -114,6 +149,17 @@ char *curl_unescape(const char *string, int length)
hex = strtol(hexstr, &ptr, 16);
in = (unsigned char)hex; /* this long is never bigger than 255 anyway */
+
+#ifdef CURL_DOES_CONVERSIONS
+/* escape sequences are always in ASCII so convert them on non-ASCII hosts */
+ if (!handle ||
+ (Curl_convert_from_network(handle, &in, 1) != CURLE_OK)) {
+ /* Curl_convert_from_network calls failf if unsuccessful */
+ free(ns);
+ return NULL;
+ }
+#endif /* CURL_DOES_CONVERSIONS */
+
string+=2;
alloc-=2;
}
@@ -122,6 +168,10 @@ char *curl_unescape(const char *string, int length)
string++;
}
ns[strindex]=0; /* terminate it */
+
+ if(olen)
+ /* store output size */
+ *olen = strindex;
return ns;
}