From f8d883355d8a0965534ef8a6da99bd7195cf56d0 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Thu, 22 Mar 2001 11:40:58 +0000 Subject: the new escape/unescape function setup --- lib/escape.c | 120 +++++++++++++++++++++++++++++------------------------------ lib/escape.h | 2 +- 2 files changed, 61 insertions(+), 61 deletions(-) (limited to 'lib') 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 -- cgit v1.2.3