Pārlūkot izejas kodu

add urlcode driver

balbekova 8 gadi atpakaļ
vecāks
revīzija
cd4a2364da
2 mainītis faili ar 87 papildinājumiem un 0 dzēšanām
  1. 65 0
      modules/urlcode.c
  2. 22 0
      modules/urlcode.h

+ 65 - 0
modules/urlcode.c

@@ -0,0 +1,65 @@
+/*
+ * urlcode.c
+ *
+ *  Created on: 18.01.2016
+ *      Author: pavel
+ */
+
+#include <ctype.h>
+#include <stdint.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 that outbuf is big enougth */
+char *url_encode(char *outbuf, uint32_t outlen, char *inbuf) {
+  char *pstr = inbuf, *buf = outbuf, *pbuf = buf;
+  while ((*pstr) && outlen > 3) {
+    if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~')
+      *pbuf++ = *pstr;
+    else if (*pstr == ' ') {
+      *pbuf++ = '+';
+      outlen--;
+    }
+    else {
+      *pbuf++ = '%', *pbuf++ = to_hex(*pstr >> 4), *pbuf++ = to_hex(*pstr & 15);
+      outlen-=3;
+    }
+    pstr++;
+  }
+  *pbuf = '\0';
+  return buf;
+}
+
+/* Returns a url-decoded version of str */
+/* IMPORTANT:  be sure that outbuf is big enougth */
+char *url_decode(char *outbuf, uint32_t outlen, char *inbuf) {
+  char *pstr = inbuf, *buf = outbuf, *pbuf = buf;
+  while ((*pstr) && outlen > 1) {
+    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++;
+    outlen--;
+  }
+  *pbuf = '\0';
+  return buf;
+}
+
+

+ 22 - 0
modules/urlcode.h

@@ -0,0 +1,22 @@
+/*
+ * urlcode.h
+ *
+ *  Created on: 18.01.2016
+ *      Author: pavel
+ */
+
+#ifndef URLCODE_H_
+#define URLCODE_H_
+
+#include <stdint.h>
+
+/* Returns a url-encoded version of str */
+/* IMPORTANT: be sure that outbuf is big enougth */
+char *url_encode(char *outbuf, uint32_t outlen, char *inbuf);
+
+/* Returns a url-decoded version of str */
+/* IMPORTANT:  be sure that outbuf is big enougth */
+char *url_decode(char *outbuf, uint32_t outlen, char *inbuf);
+
+
+#endif /* URLCODE_H_ */