aboutsummaryrefslogtreecommitdiff
path: root/src/Utility/Unescape.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/Utility/Unescape.c')
-rw-r--r--src/Utility/Unescape.c137
1 files changed, 137 insertions, 0 deletions
diff --git a/src/Utility/Unescape.c b/src/Utility/Unescape.c
index 57ac333..b2c09e5 100644
--- a/src/Utility/Unescape.c
+++ b/src/Utility/Unescape.c
@@ -3,6 +3,143 @@
#include <stdlib.h>
#include <string.h>
+static unsigned int hexval(char c) {
+ if (c >= '0' && c <= '9')
+ return c - '0';
+ if (c >= 'a' && c <= 'f')
+ return c - 'a' + 10;
+ if (c >= 'A' && c <= 'F')
+ return c - 'A' + 10;
+ return 0;
+}
+
+static unsigned int hex4(const char *s) {
+ return (hexval(s[0]) << 12) | (hexval(s[1]) << 8) | (hexval(s[2]) << 4) |
+ hexval(s[3]);
+}
+
+static size_t utf8_len(unsigned int cp) {
+ if (cp < 0x80)
+ return 1;
+ if (cp < 0x800)
+ return 2;
+ if (cp < 0x10000)
+ return 3;
+ return 4;
+}
+
+static void utf8_write(char *buf, size_t *i, unsigned int cp) {
+ if (cp < 0x80) {
+ buf[(*i)++] = (char)cp;
+ } else if (cp < 0x800) {
+ buf[(*i)++] = (char)(0xC0 | (cp >> 6));
+ buf[(*i)++] = (char)(0x80 | (cp & 0x3F));
+ } else if (cp < 0x10000) {
+ buf[(*i)++] = (char)(0xE0 | (cp >> 12));
+ buf[(*i)++] = (char)(0x80 | ((cp >> 6) & 0x3F));
+ buf[(*i)++] = (char)(0x80 | (cp & 0x3F));
+ } else {
+ buf[(*i)++] = (char)(0xF0 | (cp >> 18));
+ buf[(*i)++] = (char)(0x80 | ((cp >> 12) & 0x3F));
+ buf[(*i)++] = (char)(0x80 | ((cp >> 6) & 0x3F));
+ buf[(*i)++] = (char)(0x80 | (cp & 0x3F));
+ }
+}
+
+char *json_string_unescape(const char *s, const char *end) {
+ if (!s || !end || end < s)
+ return NULL;
+
+ size_t len = 0;
+ const char *p = s;
+ while (p < end) {
+ if (*p == '\\' && p + 1 < end) {
+ if (p[1] == 'u' && p + 5 < end) {
+ unsigned int cp = hex4(p + 2);
+ if (cp >= 0xD800 && cp <= 0xDBFF && p + 11 < end && p[6] == '\\' &&
+ p[7] == 'u') {
+ unsigned int cp2 = hex4(p + 8);
+ if (cp2 >= 0xDC00 && cp2 <= 0xDFFF) {
+ len += 4;
+ p += 12;
+ continue;
+ }
+ }
+ len += utf8_len(cp);
+ p += 6;
+ continue;
+ }
+ len += 1;
+ p += 2;
+ continue;
+ }
+ len += 1;
+ p += 1;
+ }
+
+ char *out = malloc(len + 1);
+ if (!out)
+ return NULL;
+ size_t i = 0;
+ p = s;
+ while (p < end) {
+ if (*p == '\\' && p + 1 < end) {
+ if (p[1] == 'u' && p + 5 < end) {
+ unsigned int cp = hex4(p + 2);
+ if (cp >= 0xD800 && cp <= 0xDBFF && p + 11 < end && p[6] == '\\' &&
+ p[7] == 'u') {
+ unsigned int cp2 = hex4(p + 8);
+ if (cp2 >= 0xDC00 && cp2 <= 0xDFFF) {
+ utf8_write(out, &i,
+ 0x10000 + ((cp - 0xD800) << 10) + (cp2 - 0xDC00));
+ p += 12;
+ continue;
+ }
+ }
+ if (cp < 0xD800 || cp > 0xDFFF)
+ utf8_write(out, &i, cp);
+ p += 6;
+ continue;
+ }
+ if (p[1] == 'n') {
+ out[i++] = '\n';
+ p += 2;
+ continue;
+ }
+ if (p[1] == 'r') {
+ out[i++] = '\r';
+ p += 2;
+ continue;
+ }
+ if (p[1] == 't') {
+ out[i++] = '\t';
+ p += 2;
+ continue;
+ }
+ if (p[1] == 'b') {
+ out[i++] = '\b';
+ p += 2;
+ continue;
+ }
+ if (p[1] == 'f') {
+ out[i++] = '\f';
+ p += 2;
+ continue;
+ }
+ if (p[1] == '"' || p[1] == '\\' || p[1] == '/') {
+ out[i++] = p[1];
+ p += 2;
+ continue;
+ }
+ p += 2;
+ continue;
+ }
+ out[i++] = *p++;
+ }
+ out[i] = '\0';
+ return out;
+}
+
char *unescape_search_url(const char *input) {
if (!input)
return NULL;