Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[utils] add url_encode() function
  • Loading branch information
Jalle19 authored and perexg committed Jan 9, 2015
1 parent f163175 commit 3967007
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/tvheadend.h
Expand Up @@ -711,6 +711,10 @@ int rmtree ( const char *path );

char *regexp_escape ( const char *str );

/* URL decoding */
char to_hex(char code);
char *url_encode(char *str);

static inline int32_t deltaI32(int32_t a, int32_t b) { return (a > b) ? (a - b) : (b - a); }
static inline uint32_t deltaU32(uint32_t a, uint32_t b) { return (a > b) ? (a - b) : (b - a); }

Expand Down
27 changes: 27 additions & 0 deletions src/utils.c
Expand Up @@ -25,6 +25,7 @@
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <ctype.h>
#include "tvheadend.h"

#if defined(PLATFORM_DARWIN)
Expand Down Expand Up @@ -588,3 +589,29 @@ regexp_escape(const char* str)
*b = 0;
return tmp;
}

/* Converts an integer value to its hex character
http://www.geekhideout.com/urlcode.shtml */
char to_hex(char code) {
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
http://www.geekhideout.com/urlcode.shtml */
char *url_encode(char *str) {
char *pstr = str, *buf = malloc(strlen(str) * 3 + 1), *pbuf = buf;
while (*pstr) {
if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~')
*pbuf++ = *pstr;
/*else if (*pstr == ' ')
*pbuf++ = '+';*/
else
*pbuf++ = '%', *pbuf++ = to_hex(*pstr >> 4), *pbuf++ = to_hex(*pstr & 15);
pstr++;
}
*pbuf = '\0';
return buf;
}

0 comments on commit 3967007

Please sign in to comment.