utils_params.[ch]: Implement "uri_escape".
authorFlorian Forster <ff@octo.it>
Wed, 9 Jun 2010 17:01:25 +0000 (19:01 +0200)
committerFlorian Forster <octo@leeloo.lan.home.verplant.org>
Wed, 9 Jun 2010 17:01:25 +0000 (19:01 +0200)
utils_params.c
utils_params.h

index ec943b5..6e5dc89 100644 (file)
@@ -215,4 +215,45 @@ const char *param (const char *key) /* {{{ */
   return (parameter_lookup (key));
 } /* }}} const char *param */
 
+int uri_escape (char *dst, const char *src, size_t size) /* {{{ */
+{
+  size_t in;
+  size_t out;
+
+  in = 0;
+  out = 0;
+  while (42)
+  {
+    if (src[in] == 0)
+    {
+      dst[out] = 0;
+      return (0);
+    }
+    else if ((src[in] < 32)
+        || (src[in] == '&')
+        || (src[in] == ';')
+        || (src[in] >= 128))
+    {
+      char esc[4];
+
+      if ((size - out) < 4)
+        break;
+      
+      snprintf (esc, sizeof (esc), "%%%02x", (unsigned int) src[in]);
+      dst[out] = esc[0];
+      dst[out+1] = esc[1];
+      dst[out+2] = esc[2];
+
+      out += 3;
+      in++;
+    }
+    else
+    {
+      dst[out] = src[in];
+      out++;
+      in++;
+    }
+  } /* while (42) */
+} /* }}} int uri_escape */
+
 /* vim: set sw=2 sts=2 et fdm=marker : */
index b68ad4d..3c7ea0a 100644 (file)
@@ -6,4 +6,6 @@ void param_finish (void);
 
 const char *param (const char *key);
 
+int uri_escape (char *dst, const char *src, size_t size);
+
 #endif /* UTILS_PARAMS_H */