From nobody Sun Feb 8 19:35:33 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1533048256280858.5141533339067; Tue, 31 Jul 2018 07:44:16 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 2E53A3167684; Tue, 31 Jul 2018 14:44:14 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id E4C7470BB9; Tue, 31 Jul 2018 14:44:13 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 98A6A24F64; Tue, 31 Jul 2018 14:44:13 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w6VEhx83018310 for ; Tue, 31 Jul 2018 10:43:59 -0400 Received: by smtp.corp.redhat.com (Postfix) id 5AE44167FA; Tue, 31 Jul 2018 14:43:59 +0000 (UTC) Received: from icr.brq.redhat.com (unknown [10.43.2.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id ED6501687D for ; Tue, 31 Jul 2018 14:43:58 +0000 (UTC) From: =?UTF-8?q?J=C3=A1n=20Tomko?= To: libvir-list@redhat.com Date: Tue, 31 Jul 2018 16:43:45 +0200 Message-Id: In-Reply-To: References: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 07/10] Revert "Remove functions using yajl" X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.41]); Tue, 31 Jul 2018 14:44:14 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Switch back to yajl until we figure out a solution for the json_object_iter_next conflict with json-glib. This reverts commit bf114decb34f21cd225ead6dc4d929d35a8c5fe5. Signed-off-by: J=C3=A1n Tomko --- src/util/virjson.c | 529 +++++++++++++++++++++++++++++++++++++++++++++++++= +++- 1 file changed, 528 insertions(+), 1 deletion(-) diff --git a/src/util/virjson.c b/src/util/virjson.c index 80274bc6c5..608ba85d67 100644 --- a/src/util/virjson.c +++ b/src/util/virjson.c @@ -29,6 +29,22 @@ #include "virstring.h" #include "virutil.h" =20 +#if WITH_YAJL +# include +# include + +# ifdef WITH_YAJL2 +# define yajl_size_t size_t +# define VIR_YAJL_STATUS_OK(status) ((status) =3D=3D yajl_status_ok) +# else +# define yajl_size_t unsigned int +# define yajl_complete_parse yajl_parse_complete +# define VIR_YAJL_STATUS_OK(status) \ + ((status) =3D=3D yajl_status_ok || (status) =3D=3D yajl_status_insuffi= cient_data) +# endif + +#endif + /* XXX fixme */ #define VIR_FROM_THIS VIR_FROM_NONE =20 @@ -72,6 +88,23 @@ struct _virJSONValue { }; =20 =20 +typedef struct _virJSONParserState virJSONParserState; +typedef virJSONParserState *virJSONParserStatePtr; +struct _virJSONParserState { + virJSONValuePtr value; + char *key; +}; + +typedef struct _virJSONParser virJSONParser; +typedef virJSONParser *virJSONParserPtr; +struct _virJSONParser { + virJSONValuePtr head; + virJSONParserStatePtr state; + size_t nstate; + int wrap; +}; + + virJSONType virJSONValueGetType(const virJSONValue *value) { @@ -1458,7 +1491,501 @@ virJSONValueCopy(const virJSONValue *in) } =20 =20 -#if WITH_JANSSON +#if WITH_YAJL +static int +virJSONParserInsertValue(virJSONParserPtr parser, + virJSONValuePtr value) +{ + if (!parser->head) { + parser->head =3D value; + } else { + virJSONParserStatePtr state; + if (!parser->nstate) { + VIR_DEBUG("got a value to insert without a container"); + return -1; + } + + state =3D &parser->state[parser->nstate-1]; + + switch (state->value->type) { + case VIR_JSON_TYPE_OBJECT: { + if (!state->key) { + VIR_DEBUG("missing key when inserting object value"); + return -1; + } + + if (virJSONValueObjectAppend(state->value, + state->key, + value) < 0) + return -1; + + VIR_FREE(state->key); + } break; + + case VIR_JSON_TYPE_ARRAY: { + if (state->key) { + VIR_DEBUG("unexpected key when inserting array value"); + return -1; + } + + if (virJSONValueArrayAppend(state->value, + value) < 0) + return -1; + } break; + + default: + VIR_DEBUG("unexpected value type, not a container"); + return -1; + } + } + + return 0; +} + + +static int +virJSONParserHandleNull(void *ctx) +{ + virJSONParserPtr parser =3D ctx; + virJSONValuePtr value =3D virJSONValueNewNull(); + + VIR_DEBUG("parser=3D%p", parser); + + if (!value) + return 0; + + if (virJSONParserInsertValue(parser, value) < 0) { + virJSONValueFree(value); + return 0; + } + + return 1; +} + + +static int +virJSONParserHandleBoolean(void *ctx, + int boolean_) +{ + virJSONParserPtr parser =3D ctx; + virJSONValuePtr value =3D virJSONValueNewBoolean(boolean_); + + VIR_DEBUG("parser=3D%p boolean=3D%d", parser, boolean_); + + if (!value) + return 0; + + if (virJSONParserInsertValue(parser, value) < 0) { + virJSONValueFree(value); + return 0; + } + + return 1; +} + + +static int +virJSONParserHandleNumber(void *ctx, + const char *s, + yajl_size_t l) +{ + virJSONParserPtr parser =3D ctx; + char *str; + virJSONValuePtr value; + + if (VIR_STRNDUP(str, s, l) < 0) + return -1; + value =3D virJSONValueNewNumber(str); + VIR_FREE(str); + + VIR_DEBUG("parser=3D%p str=3D%s", parser, str); + + if (!value) + return 0; + + if (virJSONParserInsertValue(parser, value) < 0) { + virJSONValueFree(value); + return 0; + } + + return 1; +} + + +static int +virJSONParserHandleString(void *ctx, + const unsigned char *stringVal, + yajl_size_t stringLen) +{ + virJSONParserPtr parser =3D ctx; + virJSONValuePtr value =3D virJSONValueNewStringLen((const char *)strin= gVal, + stringLen); + + VIR_DEBUG("parser=3D%p str=3D%p", parser, (const char *)stringVal); + + if (!value) + return 0; + + if (virJSONParserInsertValue(parser, value) < 0) { + virJSONValueFree(value); + return 0; + } + + return 1; +} + + +static int +virJSONParserHandleMapKey(void *ctx, + const unsigned char *stringVal, + yajl_size_t stringLen) +{ + virJSONParserPtr parser =3D ctx; + virJSONParserStatePtr state; + + VIR_DEBUG("parser=3D%p key=3D%p", parser, (const char *)stringVal); + + if (!parser->nstate) + return 0; + + state =3D &parser->state[parser->nstate-1]; + if (state->key) + return 0; + if (VIR_STRNDUP(state->key, (const char *)stringVal, stringLen) < 0) + return 0; + return 1; +} + + +static int +virJSONParserHandleStartMap(void *ctx) +{ + virJSONParserPtr parser =3D ctx; + virJSONValuePtr value =3D virJSONValueNewObject(); + + VIR_DEBUG("parser=3D%p", parser); + + if (!value) + return 0; + + if (virJSONParserInsertValue(parser, value) < 0) { + virJSONValueFree(value); + return 0; + } + + if (VIR_REALLOC_N(parser->state, + parser->nstate + 1) < 0) { + return 0; + } + + parser->state[parser->nstate].value =3D value; + parser->state[parser->nstate].key =3D NULL; + parser->nstate++; + + return 1; +} + + +static int +virJSONParserHandleEndMap(void *ctx) +{ + virJSONParserPtr parser =3D ctx; + virJSONParserStatePtr state; + + VIR_DEBUG("parser=3D%p", parser); + + if (!parser->nstate) + return 0; + + state =3D &(parser->state[parser->nstate-1]); + if (state->key) { + VIR_FREE(state->key); + return 0; + } + + VIR_DELETE_ELEMENT(parser->state, parser->nstate - 1, parser->nstate); + + return 1; +} + + +static int +virJSONParserHandleStartArray(void *ctx) +{ + virJSONParserPtr parser =3D ctx; + virJSONValuePtr value =3D virJSONValueNewArray(); + + VIR_DEBUG("parser=3D%p", parser); + + if (!value) + return 0; + + if (virJSONParserInsertValue(parser, value) < 0) { + virJSONValueFree(value); + return 0; + } + + if (VIR_REALLOC_N(parser->state, + parser->nstate + 1) < 0) + return 0; + + parser->state[parser->nstate].value =3D value; + parser->state[parser->nstate].key =3D NULL; + parser->nstate++; + + return 1; +} + + +static int +virJSONParserHandleEndArray(void *ctx) +{ + virJSONParserPtr parser =3D ctx; + virJSONParserStatePtr state; + + VIR_DEBUG("parser=3D%p", parser); + + if (!(parser->nstate - parser->wrap)) + return 0; + + state =3D &(parser->state[parser->nstate-1]); + if (state->key) { + VIR_FREE(state->key); + return 0; + } + + VIR_DELETE_ELEMENT(parser->state, parser->nstate - 1, parser->nstate); + + return 1; +} + + +static const yajl_callbacks parserCallbacks =3D { + virJSONParserHandleNull, + virJSONParserHandleBoolean, + NULL, + NULL, + virJSONParserHandleNumber, + virJSONParserHandleString, + virJSONParserHandleStartMap, + virJSONParserHandleMapKey, + virJSONParserHandleEndMap, + virJSONParserHandleStartArray, + virJSONParserHandleEndArray +}; + + +/* XXX add an incremental streaming parser - yajl trivially supports it */ +virJSONValuePtr +virJSONValueFromString(const char *jsonstring) +{ + yajl_handle hand; + virJSONParser parser =3D { NULL, NULL, 0, 0 }; + virJSONValuePtr ret =3D NULL; + int rc; + size_t len =3D strlen(jsonstring); +# ifndef WITH_YAJL2 + yajl_parser_config cfg =3D { 0, 1 }; /* Match yajl 2 default behavior = */ + VIR_AUTOPTR(virJSONValue) tmp =3D NULL; +# endif + + VIR_DEBUG("string=3D%s", jsonstring); + +# ifdef WITH_YAJL2 + hand =3D yajl_alloc(&parserCallbacks, NULL, &parser); +# else + hand =3D yajl_alloc(&parserCallbacks, &cfg, NULL, &parser); +# endif + if (!hand) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Unable to create JSON parser")); + goto cleanup; + } + + /* Yajl 2 is nice enough to default to rejecting trailing garbage. + * Yajl 1.0.12 has yajl_get_bytes_consumed to make that detection + * simpler. But we're stuck with yajl 1.0.7 on RHEL 6, which + * happily quits parsing at the end of a valid JSON construct, + * with no visibility into how much more input remains. Wrapping + * things in an array forces yajl to confess the truth. */ +# ifdef WITH_YAJL2 + rc =3D yajl_parse(hand, (const unsigned char *)jsonstring, len); +# else + rc =3D yajl_parse(hand, (const unsigned char *)"[", 1); + parser.wrap =3D 1; + if (VIR_YAJL_STATUS_OK(rc)) + rc =3D yajl_parse(hand, (const unsigned char *)jsonstring, len); + parser.wrap =3D 0; + if (VIR_YAJL_STATUS_OK(rc)) + rc =3D yajl_parse(hand, (const unsigned char *)"]", 1); +# endif + if (!VIR_YAJL_STATUS_OK(rc) || + yajl_complete_parse(hand) !=3D yajl_status_ok) { + unsigned char *errstr =3D yajl_get_error(hand, 1, + (const unsigned char*)jsons= tring, + strlen(jsonstring)); + + virReportError(VIR_ERR_INTERNAL_ERROR, + _("cannot parse json %s: %s"), + jsonstring, (const char*) errstr); + yajl_free_error(hand, errstr); + virJSONValueFree(parser.head); + goto cleanup; + } + + if (parser.nstate !=3D 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("cannot parse json %s: unterminated string/map/ar= ray"), + jsonstring); + virJSONValueFree(parser.head); + } else { + ret =3D parser.head; +# ifndef WITH_YAJL2 + /* Undo the array wrapping above */ + tmp =3D ret; + ret =3D NULL; + if (virJSONValueArraySize(tmp) > 1) + virReportError(VIR_ERR_INTERNAL_ERROR, + _("cannot parse json %s: too many items present= "), + jsonstring); + else + ret =3D virJSONValueArraySteal(tmp, 0); +# endif + } + + cleanup: + yajl_free(hand); + + if (parser.nstate) { + size_t i; + for (i =3D 0; i < parser.nstate; i++) + VIR_FREE(parser.state[i].key); + VIR_FREE(parser.state); + } + + VIR_DEBUG("result=3D%p", ret); + + return ret; +} + + +static int +virJSONValueToStringOne(virJSONValuePtr object, + yajl_gen g) +{ + size_t i; + + VIR_DEBUG("object=3D%p type=3D%d gen=3D%p", object, object->type, g); + + switch (object->type) { + case VIR_JSON_TYPE_OBJECT: + if (yajl_gen_map_open(g) !=3D yajl_gen_status_ok) + return -1; + for (i =3D 0; i < object->data.object.npairs; i++) { + if (yajl_gen_string(g, + (unsigned char *)object->data.object.pairs= [i].key, + strlen(object->data.object.pairs[i].key)) + !=3D yajl_gen_status_ok) + return -1; + if (virJSONValueToStringOne(object->data.object.pairs[i].value= , g) < 0) + return -1; + } + if (yajl_gen_map_close(g) !=3D yajl_gen_status_ok) + return -1; + break; + case VIR_JSON_TYPE_ARRAY: + if (yajl_gen_array_open(g) !=3D yajl_gen_status_ok) + return -1; + for (i =3D 0; i < object->data.array.nvalues; i++) { + if (virJSONValueToStringOne(object->data.array.values[i], g) <= 0) + return -1; + } + if (yajl_gen_array_close(g) !=3D yajl_gen_status_ok) + return -1; + break; + + case VIR_JSON_TYPE_STRING: + if (yajl_gen_string(g, (unsigned char *)object->data.string, + strlen(object->data.string)) !=3D yajl_gen_sta= tus_ok) + return -1; + break; + + case VIR_JSON_TYPE_NUMBER: + if (yajl_gen_number(g, object->data.number, + strlen(object->data.number)) !=3D yajl_gen_sta= tus_ok) + return -1; + break; + + case VIR_JSON_TYPE_BOOLEAN: + if (yajl_gen_bool(g, object->data.boolean) !=3D yajl_gen_status_ok) + return -1; + break; + + case VIR_JSON_TYPE_NULL: + if (yajl_gen_null(g) !=3D yajl_gen_status_ok) + return -1; + break; + + default: + return -1; + } + + return 0; +} + + +char * +virJSONValueToString(virJSONValuePtr object, + bool pretty) +{ + yajl_gen g; + const unsigned char *str; + char *ret =3D NULL; + yajl_size_t len; +# ifndef WITH_YAJL2 + yajl_gen_config conf =3D { pretty ? 1 : 0, pretty ? " " : " "}; +# endif + + VIR_DEBUG("object=3D%p", object); + +# ifdef WITH_YAJL2 + g =3D yajl_gen_alloc(NULL); + if (g) { + yajl_gen_config(g, yajl_gen_beautify, pretty ? 1 : 0); + yajl_gen_config(g, yajl_gen_indent_string, pretty ? " " : " "); + yajl_gen_config(g, yajl_gen_validate_utf8, 1); + } +# else + g =3D yajl_gen_alloc(&conf, NULL); +# endif + if (!g) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Unable to create JSON formatter")); + goto cleanup; + } + + if (virJSONValueToStringOne(object, g) < 0) { + virReportOOMError(); + goto cleanup; + } + + if (yajl_gen_get_buf(g, &str, &len) !=3D yajl_gen_status_ok) { + virReportOOMError(); + goto cleanup; + } + + ignore_value(VIR_STRDUP(ret, (const char *)str)); + + cleanup: + yajl_gen_free(g); + + VIR_DEBUG("result=3D%s", NULLSTR(ret)); + + return ret; +} + + +#elif WITH_JANSSON # include =20 static virJSONValuePtr --=20 2.16.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list