aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortenox <as@tenoware.com>2016-08-27 01:56:37 -0700
committertenox <as@tenoware.com>2016-08-27 01:56:37 -0700
commite9f8a0ee7a7778685f5af9dbc5c287662adfcbce (patch)
tree80a74f169b5c6980fefd2947e9296bcb9b65c75d
parent0284f7cc574b34a78da1151254225c0f6bdb6834 (diff)
downloadwfm-e9f8a0ee7a7778685f5af9dbc5c287662adfcbce.tar.gz
added urlencode1.1.1
-rw-r--r--urlencode.c56
1 files changed, 56 insertions, 0 deletions
diff --git a/urlencode.c b/urlencode.c
new file mode 100644
index 0000000..ea2e3f5
--- /dev/null
+++ b/urlencode.c
@@ -0,0 +1,56 @@
+/* To the extent possible under law, Fred Bulback has waived all copyright and related or neighboring rights to URL Encoding/Decoding in C. This work is published from: United States. */
+/* Source: http://geekhideout.com/urlcode.shtml */
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <ctype.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;
+} \ No newline at end of file