From 48770dd34179b41c5d00e07b7b217172ef4a0514 Mon Sep 17 00:00:00 2001 From: gb <741021719@qq.com> Date: Tue, 7 Feb 2023 16:51:53 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0cJSON=E5=9F=9Futf8=E8=BD=ACun?= =?UTF-8?q?icode=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hgsane/cJSON.c | 38 ++++++++++++++++++++++++++++++++++++++ hgsane/cJSON.h | 3 +++ 2 files changed, 41 insertions(+) diff --git a/hgsane/cJSON.c b/hgsane/cJSON.c index f3dc5bc..cd60134 100644 --- a/hgsane/cJSON.c +++ b/hgsane/cJSON.c @@ -253,6 +253,44 @@ static const char *parse_string(cJSON *item,const char *str) item->type=cJSON_String; return ptr; } +char* cJSON_utf8_2_unic(const char* utf8) +{ + char* unic = (char*)malloc(strlen(utf8) * 3 + 8); + unsigned char * cur = unic; + + while (*cur = *utf8++) + { + if ((*cur & 0x0f0) == 0x0e0) + { + if (((unsigned char)utf8[0] & 0x0c0) == 0x80 && + ((unsigned char)utf8[1] & 0x0c0) == 0x80) + { + char* hex = "0123456789ABCDEF"; + unsigned short us = *cur & 0x0f; + us <<= 6; + us += utf8[0] & 0x3f; + us <<= 6; + us += utf8[1] & 0x3f; + + *cur++ = '\\'; + *cur++ = 'u'; + cur[3] = hex[us & 0x0f]; + us >>= 4; + cur[2] = hex[us & 0x0f]; + us >>= 4; + cur[1] = hex[us & 0x0f]; + us >>= 4; + cur[0] = hex[us & 0x0f]; + cur += 3; + utf8 += 2; + } + } + cur++; + } + *cur++ = 0; + + return unic; +} /* Render the cstring provided to an escaped version that can be printed. */ static char *print_string_ptr(const char *str,printbuffer *p) diff --git a/hgsane/cJSON.h b/hgsane/cJSON.h index 58b91ef..913bc1e 100644 --- a/hgsane/cJSON.h +++ b/hgsane/cJSON.h @@ -135,6 +135,9 @@ extern cJSON *cJSON_ParseWithOpts(const char *value,const char **return_parse_en extern void cJSON_Minify(char *json); +// convert e681a2 to \u6062, call 'free' to free the returned value +extern char* cJSON_utf8_2_unic(const char* utf8); + /* Macros for creating things quickly. */ #define cJSON_AddNullToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateNull()) #define cJSON_AddTrueToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateTrue())