From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:37710) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1avzy1-00076o-FB for qemu-devel@nongnu.org; Fri, 29 Apr 2016 00:23:50 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1avzy0-0000ra-3J for qemu-devel@nongnu.org; Fri, 29 Apr 2016 00:23:49 -0400 Received: from mx1.redhat.com ([209.132.183.28]:59608) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1avzxz-0000rV-TD for qemu-devel@nongnu.org; Fri, 29 Apr 2016 00:23:48 -0400 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 98D1862640 for ; Fri, 29 Apr 2016 04:23:47 +0000 (UTC) From: Eric Blake Date: Thu, 28 Apr 2016 22:23:24 -0600 Message-Id: <1461903820-3092-4-git-send-email-eblake@redhat.com> In-Reply-To: <1461903820-3092-1-git-send-email-eblake@redhat.com> References: <1461903820-3092-1-git-send-email-eblake@redhat.com> Subject: [Qemu-devel] [PATCH v3 03/18] qapi: Factor out JSON string escaping List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: armbru@redhat.com, famz@redhat.com, Luiz Capitulino Pull out a new qstring_append_json_string() helper, so that all JSON output producers can use the same output escaping rules. While it appears that vmstate's use of the simpler qjson.c formatter is not currently encountering any string that needs escapes to be valid JSON, it is better to be safe than sorry. Signed-off-by: Eric Blake Reviewed-by: Fam Zheng --- v3: rebase to include cleanups in master v2: no change --- include/qapi/qmp/qstring.h | 1 + qjson.c | 9 +++---- qobject/qobject-json.c | 59 ++------------------------------------------- qobject/qstring.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 63 deletions(-) diff --git a/include/qapi/qmp/qstring.h b/include/qapi/qmp/qstring.h index 10076b7..a254ee3 100644 --- a/include/qapi/qmp/qstring.h +++ b/include/qapi/qmp/qstring.h @@ -30,6 +30,7 @@ const char *qstring_get_str(const QString *qstring); void qstring_append_int(QString *qstring, int64_t value); void qstring_append(QString *qstring, const char *str); void qstring_append_chr(QString *qstring, int c); +void qstring_append_json_string(QString *qstring, const char *raw); QString *qobject_to_qstring(const QObject *obj); void qstring_destroy_obj(QObject *obj); diff --git a/qjson.c b/qjson.c index b65ca6e..b9a9a36 100644 --- a/qjson.c +++ b/qjson.c @@ -36,9 +36,8 @@ static void json_emit_element(QJSON *json, const char *name) } if (name) { - qstring_append(json->str, "\""); - qstring_append(json->str, name); - qstring_append(json->str, "\" : "); + qstring_append_json_string(json->str, name); + qstring_append(json->str, " : "); } } @@ -77,9 +76,7 @@ void json_prop_int(QJSON *json, const char *name, int64_t val) void json_prop_str(QJSON *json, const char *name, const char *str) { json_emit_element(json, name); - qstring_append_chr(json->str, '"'); - qstring_append(json->str, str); - qstring_append_chr(json->str, '"'); + qstring_append_json_string(json->str, str); } const char *qjson_get_str(QJSON *json) diff --git a/qobject/qobject-json.c b/qobject/qobject-json.c index 24e7d80..6fed1ee 100644 --- a/qobject/qobject-json.c +++ b/qobject/qobject-json.c @@ -16,7 +16,6 @@ #include "qapi/qmp/json-parser.h" #include "qapi/qmp/json-streamer.h" #include "qapi/qmp/qobject-json.h" -#include "qemu/unicode.h" #include "qapi/qmp/types.h" typedef struct JSONParsingState @@ -81,7 +80,6 @@ static void to_json(const QObject *obj, QString *str, int pretty, int indent); static void to_json_dict_iter(const char *key, QObject *obj, void *opaque) { ToJsonIterState *s = opaque; - QString *qkey; int j; if (s->count) { @@ -94,9 +92,7 @@ static void to_json_dict_iter(const char *key, QObject *obj, void *opaque) qstring_append(s->str, " "); } - qkey = qstring_from_str(key); - to_json(QOBJECT(qkey), s->str, s->pretty, s->indent); - QDECREF(qkey); + qstring_append_json_string(s->str, key); qstring_append(s->str, ": "); to_json(obj, s->str, s->pretty, s->indent); @@ -138,58 +134,7 @@ static void to_json(const QObject *obj, QString *str, int pretty, int indent) } case QTYPE_QSTRING: { QString *val = qobject_to_qstring(obj); - const char *ptr; - int cp; - char buf[16]; - char *end; - - ptr = qstring_get_str(val); - qstring_append(str, "\""); - - for (; *ptr; ptr = end) { - cp = mod_utf8_codepoint(ptr, 6, &end); - switch (cp) { - case '\"': - qstring_append(str, "\\\""); - break; - case '\\': - qstring_append(str, "\\\\"); - break; - case '\b': - qstring_append(str, "\\b"); - break; - case '\f': - qstring_append(str, "\\f"); - break; - case '\n': - qstring_append(str, "\\n"); - break; - case '\r': - qstring_append(str, "\\r"); - break; - case '\t': - qstring_append(str, "\\t"); - break; - default: - if (cp < 0) { - cp = 0xFFFD; /* replacement character */ - } - if (cp > 0xFFFF) { - /* beyond BMP; need a surrogate pair */ - snprintf(buf, sizeof(buf), "\\u%04X\\u%04X", - 0xD800 + ((cp - 0x10000) >> 10), - 0xDC00 + ((cp - 0x10000) & 0x3FF)); - } else if (cp < 0x20 || cp >= 0x7F) { - snprintf(buf, sizeof(buf), "\\u%04X", cp); - } else { - buf[0] = cp; - buf[1] = 0; - } - qstring_append(str, buf); - } - }; - - qstring_append(str, "\""); + qstring_append_json_string(str, qstring_get_str(val)); break; } case QTYPE_QDICT: { diff --git a/qobject/qstring.c b/qobject/qstring.c index 5da7b5f..9f0df5b 100644 --- a/qobject/qstring.c +++ b/qobject/qstring.c @@ -14,6 +14,7 @@ #include "qapi/qmp/qobject.h" #include "qapi/qmp/qstring.h" #include "qemu-common.h" +#include "qemu/unicode.h" /** * qstring_new(): Create a new empty QString @@ -107,6 +108,65 @@ void qstring_append_chr(QString *qstring, int c) } /** + * qstring_append_json_string(): Append a raw string to a QString, while + * adding outer "" and JSON escape sequences. + */ +void qstring_append_json_string(QString *qstring, const char *raw) +{ + const char *ptr = raw; + int cp; + char buf[16]; + char *end; + + qstring_append(qstring, "\""); + + for (; *ptr; ptr = end) { + cp = mod_utf8_codepoint(ptr, 6, &end); + switch (cp) { + case '\"': + qstring_append(qstring, "\\\""); + break; + case '\\': + qstring_append(qstring, "\\\\"); + break; + case '\b': + qstring_append(qstring, "\\b"); + break; + case '\f': + qstring_append(qstring, "\\f"); + break; + case '\n': + qstring_append(qstring, "\\n"); + break; + case '\r': + qstring_append(qstring, "\\r"); + break; + case '\t': + qstring_append(qstring, "\\t"); + break; + default: + if (cp < 0) { + cp = 0xFFFD; /* replacement character */ + } + if (cp > 0xFFFF) { + /* beyond BMP; need a surrogate pair */ + snprintf(buf, sizeof(buf), "\\u%04X\\u%04X", + 0xD800 + ((cp - 0x10000) >> 10), + 0xDC00 + ((cp - 0x10000) & 0x3FF)); + } else if (cp < 0x20 || cp >= 0x7F) { + snprintf(buf, sizeof(buf), "\\u%04X", cp); + } else { + buf[0] = cp; + buf[1] = 0; + } + qstring_append(qstring, buf); + } + }; + + qstring_append(qstring, "\""); +} + +/** * qobject_to_qstring(): Convert a QObject to a QString */ QString *qobject_to_qstring(const QObject *obj) -- 2.5.5