Files
omnisearch/src/Utility/Unescape.c

223 lines
4.9 KiB
C

#include "Unescape.h"
#include "Utility.h"
#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;
const char *key = NULL;
const char *start = NULL;
const char *end = NULL;
size_t len = 0;
if (strstr(input, "uddg=")) {
key = "uddg=";
start = strstr(input, key);
if (!start)
return NULL;
start += strlen(key);
end = strchr(start, '&');
len = end ? (size_t)(end - start) : strlen(start);
}
else if (strstr(input, "RU=")) {
key = "RU=";
start = strstr(input, key);
if (!start)
return strdup(input);
start += strlen(key);
end = strchr(start, '/');
len = end ? (size_t)(end - start) : strlen(start);
}
else {
return strdup(input);
}
char *output = (char *)malloc(len * 3 + 1);
if (!output)
return NULL;
size_t i = 0, j = 0;
while (i < len) {
if (start[i] == '%' && i + 2 < len) {
int high = hex_to_int(start[i + 1]);
int low = hex_to_int(start[i + 2]);
if (high != -1 && low != -1) {
output[j++] = (char)((high << 4) | low);
i += 3;
} else {
output[j++] = start[i++];
}
} else if (start[i] == '+') {
output[j++] = ' ';
i++;
} else {
output[j++] = start[i++];
}
}
output[j] = '\0';
return output;
}
char *url_decode_query(const char *src) {
if (!src)
return NULL;
char *res = strdup(src);
char *p = res;
while (*src) {
if (*src == '+') {
*p++ = ' ';
} else if (*src == '%' && src[1] && src[2]) {
char hex[3] = {src[1], src[2], '\0'};
*p++ = (char)strtol(hex, NULL, 16);
src += 2;
} else {
*p++ = *src;
}
src++;
}
*p = '\0';
return res;
}