X-Git-Url: https://arthur.barton.de/gitweb/?a=blobdiff_plain;f=src%2Furl.c;h=6be4d96489351d84374642169f38e94d7f132996;hb=ee80daa6b5ed0b4d360966b514ced05dce6e4ce1;hp=c8c0921020a7ca564c5a69818afd0877c2576140;hpb=71b72e4f59b129ced0a1d15e5ce3b21370edfbbd;p=netdata.git diff --git a/src/url.c b/src/url.c old mode 100755 new mode 100644 index c8c09210..6be4d964 --- a/src/url.c +++ b/src/url.c @@ -1,10 +1,4 @@ -#include -#include -#include - #include "common.h" -#include "log.h" -#include "url.h" // ---------------------------------------------------------------------------- // URL encode / decode @@ -12,67 +6,72 @@ /* Converts a hex character to its integer value */ char from_hex(char ch) { - return (char)(isdigit(ch) ? ch - '0' : tolower(ch) - 'a' + 10); + return (char)(isdigit(ch) ? ch - '0' : tolower(ch) - 'a' + 10); } /* Converts an integer value to its hex character*/ char to_hex(char code) { - static char hex[] = "0123456789abcdef"; - return hex[code & 15]; + static char hex[] = "0123456789abcdef"; + return hex[code & 15]; } /* Returns a url-encoded version of str */ /* IMPORTANT: be sure to free() the returned string after use */ char *url_encode(char *str) { - char *pstr = str, - *buf = malloc(strlen(str) * 3 + 1), - *pbuf = buf; + char *buf, *pbuf; - while (*pstr) { - if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~') - *pbuf++ = *pstr; + pbuf = buf = mallocz(strlen(str) * 3 + 1); - else if (*pstr == ' ') - *pbuf++ = '+'; + while (*str) { + if (isalnum(*str) || *str == '-' || *str == '_' || *str == '.' || *str == '~') + *pbuf++ = *str; - else - *pbuf++ = '%', *pbuf++ = to_hex(*pstr >> 4), *pbuf++ = to_hex(*pstr & 15); + else if (*str == ' ') + *pbuf++ = '+'; - pstr++; - } + else + *pbuf++ = '%', *pbuf++ = to_hex(*str >> 4), *pbuf++ = to_hex(*str & 15); - *pbuf = '\0'; + str++; + } + *pbuf = '\0'; - return buf; + pbuf = strdupz(buf); + freez(buf); + return pbuf; } /* Returns a url-decoded version of str */ /* IMPORTANT: be sure to free() the returned string after use */ char *url_decode(char *str) { - char *pstr = str, - *buf = malloc(strlen(str) + 1), - *pbuf = buf; - - if(!buf) fatal("Cannot allocate memory."); - - while (*pstr) { - if (*pstr == '%') { - if (pstr[1] && pstr[2]) { - *pbuf++ = from_hex(pstr[1]) << 4 | from_hex(pstr[2]); - pstr += 2; - } - } - else if (*pstr == '+') - *pbuf++ = ' '; - - else - *pbuf++ = *pstr; - - pstr++; - } - - *pbuf = '\0'; - - return buf; + size_t size = strlen(str) + 1; + + char *buf = mallocz(size); + return url_decode_r(buf, str, size); } +char *url_decode_r(char *to, char *url, size_t size) { + char *s = url, // source + *d = to, // destination + *e = &to[size - 1]; // destination end + + while(*s && d < e) { + if(unlikely(*s == '%')) { + if(likely(s[1] && s[2])) { + *d++ = from_hex(s[1]) << 4 | from_hex(s[2]); + s += 2; + } + } + else if(unlikely(*s == '+')) + *d++ = ' '; + + else + *d++ = *s; + + s++; + } + + *d = '\0'; + + return to; +}