1
0
mirror of https://github.com/moparisthebest/curl synced 2024-12-21 23:58:49 -05:00

the new escape/unescape function setup

This commit is contained in:
Daniel Stenberg 2001-03-22 11:40:58 +00:00
parent c23df41d83
commit f8d883355d
3 changed files with 66 additions and 61 deletions

View File

@ -475,6 +475,11 @@ char *curl_getenv(char *variable);
/* Returns a static ascii string of the libcurl version. */ /* Returns a static ascii string of the libcurl version. */
char *curl_version(void); 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 */ /* This is the version number */
#define LIBCURL_VERSION "7.7-beta5" #define LIBCURL_VERSION "7.7-beta5"
#define LIBCURL_VERSION_NUM 0x070700 #define LIBCURL_VERSION_NUM 0x070700

View File

@ -36,74 +36,74 @@
#include "memdebug.h" #include "memdebug.h"
#endif #endif
char *curl_escape(char *string) char *curl_escape(char *string, int length)
{ {
int alloc=strlen(string)+1; int alloc = (length?length:strlen(string))+1;
char *ns = malloc(alloc); char *ns = malloc(alloc);
unsigned char in; unsigned char in;
int newlen = alloc; int newlen = alloc;
int index=0; int index=0;
while(*string) { while(length--) {
in = *string; in = *string;
if(' ' == in) if(' ' == in)
ns[index++] = '+'; ns[index++] = '+';
else if(!(in >= 'a' && in <= 'z') && else if(!(in >= 'a' && in <= 'z') &&
!(in >= 'A' && in <= 'Z') && !(in >= 'A' && in <= 'Z') &&
!(in >= '0' && in <= '9')) { !(in >= '0' && in <= '9')) {
/* encode it */ /* encode it */
newlen += 2; /* the size grows with two, since this'll become a %XX */ newlen += 2; /* the size grows with two, since this'll become a %XX */
if(newlen > alloc) { if(newlen > alloc) {
alloc *= 2; alloc *= 2;
ns = realloc(ns, alloc); ns = realloc(ns, alloc);
if(!ns) if(!ns)
return NULL; return NULL;
}
sprintf(&ns[index], "%%%02X", in);
index+=3;
} }
else { sprintf(&ns[index], "%%%02X", in);
/* just copy this */ index+=3;
ns[index++]=in; }
} else {
string++; /* just copy this */
} ns[index++]=in;
ns[index]=0; /* terminate it */ }
return ns; string++;
}
ns[index]=0; /* terminate it */
return ns;
} }
char *curl_unescape(char *string, int length) char *curl_unescape(char *string, int length)
{ {
int alloc = (length?length:strlen(string))+1; int alloc = (length?length:strlen(string))+1;
char *ns = malloc(alloc); char *ns = malloc(alloc);
unsigned char in; unsigned char in;
int index=0; int index=0;
unsigned int hex; unsigned int hex;
char querypart=FALSE; /* everything to the right of a '?' letter is char querypart=FALSE; /* everything to the right of a '?' letter is
the "query part" where '+' should become ' '. the "query part" where '+' should become ' '.
RFC 2316, section 3.10 */ RFC 2316, section 3.10 */
while(--alloc > 0) { while(--alloc > 0) {
in = *string; in = *string;
if(querypart && ('+' == in)) if(querypart && ('+' == in))
in = ' '; in = ' ';
else if(!querypart && ('?' == in)) { else if(!querypart && ('?' == in)) {
/* we have "walked in" to the query part */ /* we have "walked in" to the query part */
querypart=TRUE; querypart=TRUE;
}
else if('%' == in) {
/* encoded part */
if(sscanf(string+1, "%02X", &hex)) {
in = hex;
string+=2;
alloc-=2;
} }
else if('%' == in) { }
/* encoded part */
if(sscanf(string+1, "%02X", &hex)) { ns[index++] = in;
in = hex; string++;
string+=2; }
alloc-=2; ns[index]=0; /* terminate it */
} return ns;
}
ns[index++] = in;
string++;
}
ns[index]=0; /* terminate it */
return ns;
} }

View File

@ -26,7 +26,7 @@
/* Escape and unescape URL encoding in strings. The functions return a new /* Escape and unescape URL encoding in strings. The functions return a new
* allocated string or NULL if an error occurred. */ * 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); char *curl_unescape(char *string, int length);
#endif #endif