fix: properly handle UTF-8 and JSON unescaping
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
#include "ImageScraping.h"
|
#include "ImageScraping.h"
|
||||||
#include "../Utility/HttpClient.h"
|
#include "../Utility/HttpClient.h"
|
||||||
|
#include "../Utility/Unescape.h"
|
||||||
#include "Config.h"
|
#include "Config.h"
|
||||||
#include <libxml/HTMLparser.h>
|
#include <libxml/HTMLparser.h>
|
||||||
#include <libxml/xpath.h>
|
#include <libxml/xpath.h>
|
||||||
@@ -29,51 +30,30 @@ static char *build_proxy_url(const char *image_url) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static char *extract_json_string(const char *json, const char *key) {
|
static char *extract_json_string(const char *json, const char *key) {
|
||||||
if (!json || !key)
|
if (!json || !key) return NULL;
|
||||||
return NULL;
|
|
||||||
|
|
||||||
char search_key[64];
|
char search_key[64];
|
||||||
snprintf(search_key, sizeof(search_key), "\"%s\"", key);
|
snprintf(search_key, sizeof(search_key), "\"%s\"", key);
|
||||||
|
|
||||||
const char *key_pos = strstr(json, search_key);
|
const char *key_pos = strstr(json, search_key);
|
||||||
if (!key_pos)
|
if (!key_pos) return NULL;
|
||||||
return NULL;
|
|
||||||
|
|
||||||
const char *colon = strchr(key_pos + strlen(search_key), ':');
|
const char *colon = strchr(key_pos + strlen(search_key), ':');
|
||||||
if (!colon)
|
if (!colon) return NULL;
|
||||||
return NULL;
|
|
||||||
|
|
||||||
colon++;
|
colon++;
|
||||||
while (*colon == ' ' || *colon == '\t' || *colon == '\n' || *colon == '\r')
|
while (*colon == ' ' || *colon == '\t' || *colon == '\n' || *colon == '\r') colon++;
|
||||||
|
if (*colon != '"') return NULL;
|
||||||
colon++;
|
colon++;
|
||||||
|
|
||||||
if (*colon != '"')
|
const char *end = colon;
|
||||||
return NULL;
|
while (*end && *end != '"') {
|
||||||
colon++;
|
if (*end == '\\' && end[1]) end += 2;
|
||||||
|
else end += 1;
|
||||||
size_t len = 0;
|
|
||||||
const char *start = colon;
|
|
||||||
while (*colon && *colon != '"') {
|
|
||||||
if (*colon == '\\' && *(colon + 1))
|
|
||||||
colon++;
|
|
||||||
colon++;
|
|
||||||
len++;
|
|
||||||
}
|
}
|
||||||
|
if (*end != '"') return NULL;
|
||||||
|
|
||||||
char *result = malloc(len + 1);
|
return json_string_unescape(colon, end);
|
||||||
if (!result)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
colon = start;
|
|
||||||
size_t i = 0;
|
|
||||||
while (*colon && *colon != '"') {
|
|
||||||
if (*colon == '\\' && *(colon + 1))
|
|
||||||
colon++;
|
|
||||||
result[i++] = *colon++;
|
|
||||||
}
|
|
||||||
result[i] = '\0';
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int parse_iusc_node(xmlNodePtr node, ImageResult *result) {
|
static int parse_iusc_node(xmlNodePtr node, ImageResult *result) {
|
||||||
@@ -140,7 +120,7 @@ int scrape_images(const char *query, int page, ImageResult **out_results,
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
htmlDocPtr doc = htmlReadMemory(resp.memory, resp.size, NULL, NULL,
|
htmlDocPtr doc = htmlReadMemory(resp.memory, resp.size, NULL, "UTF-8",
|
||||||
HTML_PARSE_RECOVER | HTML_PARSE_NOERROR);
|
HTML_PARSE_RECOVER | HTML_PARSE_NOERROR);
|
||||||
if (!doc) {
|
if (!doc) {
|
||||||
http_response_free(&resp);
|
http_response_free(&resp);
|
||||||
|
|||||||
@@ -3,6 +3,143 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.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) {
|
char *unescape_search_url(const char *input) {
|
||||||
if (!input)
|
if (!input)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|||||||
@@ -5,5 +5,6 @@
|
|||||||
|
|
||||||
char *unescape_search_url(const char *input);
|
char *unescape_search_url(const char *input);
|
||||||
char *url_decode_query(const char *src);
|
char *url_decode_query(const char *src);
|
||||||
|
char *json_string_unescape(const char *s, const char *end);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user