Created
May 12, 2012 02:53
-
-
Save exallium/2663841 to your computer and use it in GitHub Desktop.
URL Encoder / Decoder in C, (C) Geek Hideout
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @file urlencode.c | |
* @brief URL Encoder and Decoder | |
* @author Geek Hideout | |
* @date 2011 | |
*/ | |
/* Copyright 2011 Geek Hideout. http://geekhideout.com | |
* Retrieved May 11th, 2012 | |
*/ | |
#include "urlencode.h" | |
/* Converts a hex character to its integer value */ | |
char from_hex(char ch) { | |
return 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]; | |
} | |
/* 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; | |
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; | |
} | |
/* 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; | |
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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* @file urlencode.h | |
* @breif URL Encoder and Decoder Interface | |
* @author Alex Hart | |
* @date May 11th, 2012 | |
*/ | |
/** | |
* @brief Converts an integer value into hex char | |
* @param code The character to encode | |
* @return The encoded character | |
*/ | |
char from_hex(char code); | |
/** | |
* @brief Returns a url-encoded version of str | |
* @warning Returned string is dynamically allocated, and must be released with free() | |
* @param str A pointer to the string to be encoded | |
* @return The encoded string | |
*/ | |
char *url_encode(char *str); | |
/** | |
* @brief Decodes a string | |
* @warning Returned string is dynamically allocated, and must be released with free() | |
* @param str The string to decode | |
* @return The URL decoded string | |
*/ | |
char *url_decode(char *str); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment