added url_escape from curl

SVN revision: 48223
This commit is contained in:
Hannes Janetzek 2010-04-22 18:11:20 +00:00
parent e80438d801
commit 9b861c788b
2 changed files with 120 additions and 49 deletions

View File

@ -420,6 +420,7 @@ EAPI Evas_Object *evry_icon_theme_get(const char *icon, Evas *e);
EAPI int evry_fuzzy_match(const char *str, const char *match);
EAPI Eina_List *evry_fuzzy_match_sort(Eina_List *items);
EAPI int evry_util_exec_app(const Evry_Item *it_app, const Evry_Item *it_file);
EAPI char *evry_util_url_escape(const char *string, int inlength);
EAPI char *evry_util_unescape(const char *string, int length);
EAPI void evry_util_file_detail_set(Evry_Item_File *file);
EAPI Eina_Bool evry_util_module_config_check(const char *module_name, int conf, int epoch, int version);

View File

@ -447,60 +447,6 @@ evry_util_exec_app(const Evry_Item *it_app, const Evry_Item *it_file)
return 1;
}
/* taken from curl:
*
* Copyright (C) 1998 - 2010, Daniel Stenberg, <daniel@haxx.se>, et
* al.
*
* Unescapes the given URL escaped string of given length. Returns a
* pointer to a malloced string with length given in *olen.
* If length == 0, the length is assumed to be strlen(string).
* If olen == NULL, no output length is stored.
*/
#define ISXDIGIT(x) (isxdigit((int) ((unsigned char)x)))
EAPI char *
evry_util_unescape(const char *string, int length)
{
int alloc = (length?length:(int)strlen(string))+1;
char *ns = malloc(alloc);
unsigned char in;
int strindex=0;
unsigned long hex;
if( !ns )
return NULL;
while(--alloc > 0) {
in = *string;
if(('%' == in) && ISXDIGIT(string[1]) && ISXDIGIT(string[2])) {
/* this is two hexadecimal digits following a '%' */
char hexstr[3];
char *ptr;
hexstr[0] = string[1];
hexstr[1] = string[2];
hexstr[2] = 0;
hex = strtoul(hexstr, &ptr, 16);
in = (unsigned char)(hex & (unsigned long) 0xFF);
// in = ultouc(hex); /* this long is never bigger than 255 anyway */
string+=2;
alloc-=2;
}
ns[strindex++] = in;
string++;
}
ns[strindex]=0; /* terminate it */
return ns;
}
#undef ISXDIGIT
static int
_conf_timer(void *data)
{
@ -555,3 +501,127 @@ evry_util_module_config_check(const char *module_name, int conf, int epoch, int
return EINA_TRUE;
}
/* taken from curl:
*
* Copyright (C) 1998 - 2010, Daniel Stenberg, <daniel@haxx.se>, et
* al.
*
* Unescapes the given URL escaped string of given length. Returns a
* pointer to a malloced string with length given in *olen.
* If length == 0, the length is assumed to be strlen(string).
* If olen == NULL, no output length is stored.
*/
#define ISXDIGIT(x) (isxdigit((int) ((unsigned char)x)))
EAPI char *
evry_util_unescape(const char *string, int length)
{
int alloc = (length?length:(int)strlen(string))+1;
char *ns = malloc(alloc);
unsigned char in;
int strindex=0;
unsigned long hex;
if( !ns )
return NULL;
while(--alloc > 0) {
in = *string;
if(('%' == in) && ISXDIGIT(string[1]) && ISXDIGIT(string[2])) {
/* this is two hexadecimal digits following a '%' */
char hexstr[3];
char *ptr;
hexstr[0] = string[1];
hexstr[1] = string[2];
hexstr[2] = 0;
hex = strtoul(hexstr, &ptr, 16);
in = (unsigned char)(hex & (unsigned long) 0xFF);
// in = ultouc(hex); /* this long is never bigger than 255 anyway */
string+=2;
alloc-=2;
}
ns[strindex++] = in;
string++;
}
ns[strindex]=0; /* terminate it */
return ns;
}
#undef ISXDIGIT
static Eina_Bool
_isalnum(unsigned char in)
{
switch (in) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
case 'a': case 'b': case 'c': case 'd': case 'e':
case 'f': case 'g': case 'h': case 'i': case 'j':
case 'k': case 'l': case 'm': case 'n': case 'o':
case 'p': case 'q': case 'r': case 's': case 't':
case 'u': case 'v': case 'w': case 'x': case 'y': case 'z':
case 'A': case 'B': case 'C': case 'D': case 'E':
case 'F': case 'G': case 'H': case 'I': case 'J':
case 'K': case 'L': case 'M': case 'N': case 'O':
case 'P': case 'Q': case 'R': case 'S': case 'T':
case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z':
return EINA_TRUE;
default:
break;
}
return EINA_FALSE;
}
EAPI char *
evry_util_url_escape(const char *string, int inlength)
{
size_t alloc = (inlength?(size_t)inlength:strlen(string))+1;
char *ns;
char *testing_ptr = NULL;
unsigned char in; /* we need to treat the characters unsigned */
size_t newlen = alloc;
int strindex=0;
size_t length;
ns = malloc(alloc);
if(!ns)
return NULL;
length = alloc-1;
while(length--) {
in = *string;
if (_isalnum(in)) {
/* just copy this */
ns[strindex++]=in;
}
else {
/* encode it */
newlen += 2; /* the size grows with two, since this'll become a %XX */
if(newlen > alloc) {
alloc *= 2;
testing_ptr = realloc(ns, alloc);
if(!testing_ptr) {
free( ns );
return NULL;
}
else {
ns = testing_ptr;
}
}
snprintf(&ns[strindex], 4, "%%%02X", in);
strindex+=3;
}
string++;
}
ns[strindex]=0; /* terminate it */
return ns;
}