mirror of
https://github.com/moparisthebest/curl
synced 2024-12-21 15:48:49 -05:00
the new escape/unescape function setup
This commit is contained in:
parent
c23df41d83
commit
f8d883355d
@ -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
|
||||
|
120
lib/escape.c
120
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;
|
||||
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;
|
||||
}
|
||||
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;
|
||||
|
||||
}
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user