diff options
-rw-r--r-- | include/curl/curl.h | 5 | ||||
-rw-r--r-- | lib/escape.c | 120 | ||||
-rw-r--r-- | lib/escape.h | 2 |
3 files changed, 66 insertions, 61 deletions
diff --git a/include/curl/curl.h b/include/curl/curl.h index b98f3050f..29f77db5a 100644 --- a/include/curl/curl.h +++ b/include/curl/curl.h @@ -475,6 +475,11 @@ char *curl_getenv(char *variable); /* Returns a static ascii string of the libcurl version. */ char *curl_version(void); +/* Escape and unescape URL encoding in strings. The functions return a new + * allocated string or NULL if an error occurred. */ +char *curl_escape(char *string, int length); +char *curl_unescape(char *string, int length); + /* This is the version number */ #define LIBCURL_VERSION "7.7-beta5" #define LIBCURL_VERSION_NUM 0x070700 diff --git a/lib/escape.c b/lib/escape.c index ddb364a9c..f0e67d38f 100644 --- a/lib/escape.c +++ b/lib/escape.c @@ -36,74 +36,74 @@ #include "memdebug.h" #endif -char *curl_escape(char *string) +char *curl_escape(char *string, int length) { - int alloc=strlen(string)+1; - char *ns = malloc(alloc); - unsigned char in; - int newlen = alloc; - int index=0; + int alloc = (length?length:strlen(string))+1; + char *ns = malloc(alloc); + unsigned char in; + int newlen = alloc; + int index=0; - while(*string) { - in = *string; - if(' ' == in) - ns[index++] = '+'; - else if(!(in >= 'a' && in <= 'z') && - !(in >= 'A' && in <= 'Z') && - !(in >= '0' && in <= '9')) { - /* encode it */ - newlen += 2; /* the size grows with two, since this'll become a %XX */ - if(newlen > alloc) { - alloc *= 2; - ns = realloc(ns, alloc); - if(!ns) - return NULL; - } - sprintf(&ns[index], "%%%02X", in); - index+=3; + while(length--) { + in = *string; + if(' ' == in) + ns[index++] = '+'; + else if(!(in >= 'a' && in <= 'z') && + !(in >= 'A' && in <= 'Z') && + !(in >= '0' && in <= '9')) { + /* encode it */ + newlen += 2; /* the size grows with two, since this'll become a %XX */ + if(newlen > alloc) { + alloc *= 2; + ns = realloc(ns, alloc); + if(!ns) + return NULL; } - else { - /* just copy this */ - ns[index++]=in; - } - string++; - } - ns[index]=0; /* terminate it */ - return ns; + sprintf(&ns[index], "%%%02X", in); + index+=3; + } + else { + /* just copy this */ + ns[index++]=in; + } + string++; + } + ns[index]=0; /* terminate it */ + return ns; } char *curl_unescape(char *string, int length) { - int alloc = (length?length:strlen(string))+1; - char *ns = malloc(alloc); - unsigned char in; - int index=0; - unsigned int hex; - char querypart=FALSE; /* everything to the right of a '?' letter is - the "query part" where '+' should become ' '. - RFC 2316, section 3.10 */ + int alloc = (length?length:strlen(string))+1; + char *ns = malloc(alloc); + unsigned char in; + int index=0; + unsigned int hex; + char querypart=FALSE; /* everything to the right of a '?' letter is + the "query part" where '+' should become ' '. + RFC 2316, section 3.10 */ - while(--alloc > 0) { - in = *string; - if(querypart && ('+' == in)) - in = ' '; - else if(!querypart && ('?' == in)) { - /* we have "walked in" to the query part */ - querypart=TRUE; - } - else if('%' == in) { - /* encoded part */ - if(sscanf(string+1, "%02X", &hex)) { - in = hex; - string+=2; - alloc-=2; - } + while(--alloc > 0) { + in = *string; + if(querypart && ('+' == in)) + in = ' '; + else if(!querypart && ('?' == in)) { + /* we have "walked in" to the query part */ + querypart=TRUE; + } + else if('%' == in) { + /* encoded part */ + if(sscanf(string+1, "%02X", &hex)) { + in = hex; + string+=2; + alloc-=2; } - - ns[index++] = in; - string++; - } - ns[index]=0; /* terminate it */ - return ns; + } + + ns[index++] = in; + string++; + } + ns[index]=0; /* terminate it */ + return ns; } diff --git a/lib/escape.h b/lib/escape.h index 45a862aaf..1ec5c7ddd 100644 --- a/lib/escape.h +++ b/lib/escape.h @@ -26,7 +26,7 @@ /* Escape and unescape URL encoding in strings. The functions return a new * allocated string or NULL if an error occurred. */ -char *curl_escape(char *string); +char *curl_escape(char *string, int length); char *curl_unescape(char *string, int length); #endif |