lws_json_escape
authorAndy Green <andy@warmcat.com>
Fri, 17 Jun 2016 02:05:23 +0000 (10:05 +0800)
committerAndy Green <andy@warmcat.com>
Fri, 17 Jun 2016 22:43:30 +0000 (06:43 +0800)
Signed-off-by: Andy Green <andy@warmcat.com>
lib/libwebsockets.c
lib/libwebsockets.h

index 51da8b4..cac78db 100755 (executable)
@@ -1800,6 +1800,42 @@ lws_sql_purify(char *escaped, const char *string, int len)
 }
 
 /**
+ * lws_json_purify() - like strncpy but with escaping for json chars
+ *
+ * @escaped: output buffer
+ * @string: input buffer ('/0' terminated)
+ * @len: output buffer max length
+ *
+ * Because escaping expands the output string, it's not
+ * possible to do it in-place, ie, with escaped == string
+ */
+
+LWS_VISIBLE LWS_EXTERN const char *
+lws_json_purify(char *escaped, const char *string, int len)
+{
+       const char *p = string;
+       char *q = escaped;
+
+       while (*p && len-- > 6) {
+               if (*p == '\"' || *p == '\\' || *p < 0x20) {
+                       *q++ = '\\';
+                       *q++ = 'u';
+                       *q++ = '0';
+                       *q++ = '0';
+                       *q++ = hex[((*p) >> 4) & 15];
+                       *q++ = hex[(*p) & 15];
+                       len -= 5;
+                       p++;
+               } else
+                       *q++ = *p++;
+       }
+       *q = '\0';
+
+       return escaped;
+}
+
+
+/**
  * lws_urlencode() - like strncpy but with urlencoding
  *
  * @escaped: output buffer
index 96829a0..b6ec9bb 100644 (file)
@@ -1810,7 +1810,8 @@ lws_urlencode(char *escaped, const char *string, int len);
 LWS_VISIBLE LWS_EXTERN const char *
 lws_sql_purify(char *escaped, const char *string, int len);
 
-
+LWS_VISIBLE LWS_EXTERN const char *
+lws_json_purify(char *escaped, const char *string, int len);
 
 /*
  * URLDECODE 1 / 2