From nobody Sat Apr 27 21:43:20 2024 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 152872691502578.45377791206533; Mon, 11 Jun 2018 07:21:55 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 05D4730832F0; Mon, 11 Jun 2018 14:21:53 +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 1A22B18A6C; Mon, 11 Jun 2018 14:21:52 +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 E52224CA80; Mon, 11 Jun 2018 14:21:50 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w5BELmBo006297 for ; Mon, 11 Jun 2018 10:21:48 -0400 Received: by smtp.corp.redhat.com (Postfix) id 4CDAC20357CD; Mon, 11 Jun 2018 14:21:48 +0000 (UTC) Received: from antique-work.brq.redhat.com (unknown [10.43.2.152]) by smtp.corp.redhat.com (Postfix) with ESMTP id E21D920357CC for ; Mon, 11 Jun 2018 14:21:47 +0000 (UTC) From: Pavel Hrdina To: libvir-list@redhat.com Date: Mon, 11 Jun 2018 16:21:47 +0200 Message-Id: X-Scanned-By: MIMEDefang 2.78 on 10.11.54.6 X-loop: libvir-list@redhat.com Subject: [libvirt] [dbus PATCH] util: Introduce virtDBusUtil(En|De)codeStr helpers 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: , MIME-Version: 1.0 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.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.44]); Mon, 11 Jun 2018 14:21:53 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" D-Bus object path element can contain only [a-zA-Z0-9_] characters so we need to encode existing unique IDs. In case of UUID it's simple, we just change '-' into '_' but in case of storage volumes we need to use 'key' which is arbitrary string. This helpers encode the string using this algorithm: [a-zA-Z0-9] > [a-zA-Z0-9] anything else > _XX where XX is hex representation Signed-off-by: Pavel Hrdina Reviewed-by: Katerina Koukiou --- .gitignore | 4 ++- src/util.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++ src/util.h | 6 +++++ tests/Makefile.am | 20 +++++++++++++++ tests/test_util.c | 47 ++++++++++++++++++++++++++++++++++ 5 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 tests/test_util.c diff --git a/.gitignore b/.gitignore index 0bf09cf..c5e16a9 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ *Makefile *Makefile.in *~ +.deps __pycache__ vgcore.* =20 @@ -31,6 +32,7 @@ vgcore.* =20 /docs/*.1 =20 -/src/.deps/ /src/libvirt-dbus /src/org.libvirt.service + +/tests/test_util diff --git a/src/util.c b/src/util.c index 53dbc57..1268736 100644 --- a/src/util.c +++ b/src/util.c @@ -181,6 +181,70 @@ virtDBusUtilDecodeUUID(const gchar *uuid) return g_strdelimit(ret, "_", '-'); } =20 +static guchar +virtDBusUtilNumToHexchar(const guchar c) +{ + if (c < 10) + return '0' + c; + return 'a' + (c & 0x0f) - 10; +} + +static guchar +virtDBusUtilHexcharToNum(const guchar c) +{ + if (c >=3D 'a') + return 10 + c - 'a'; + return c - '0'; +} + +gchar * +virtDBusUtilEncodeStr(const gchar *str) +{ + gint len =3D strlen(str); + gint j =3D 0; + gchar *ret =3D g_new(gchar, len * 3 + 1); + + for (gint i =3D 0; i < len; i++) { + guchar c =3D str[i]; + if ((c >=3D 'A' && c <=3D 'Z') || + (c >=3D 'a' && c <=3D 'z') || + (c >=3D '0' && c <=3D '9')) { + ret[j++] =3D c; + } else { + ret[j] =3D '_'; + ret[j + 1] =3D virtDBusUtilNumToHexchar(c >> 4); + ret[j + 2] =3D virtDBusUtilNumToHexchar(c); + j +=3D 3; + } + } + ret[j] =3D 0; + + return ret; +} + +gchar * +virtDBusUtilDecodeStr(const gchar *str) +{ + gint len =3D strlen(str); + gint j =3D 0; + gchar *ret =3D g_new(gchar, len + 1); + + for (gint i =3D 0; i < len; i++) { + gchar c =3D str[i]; + if (c !=3D '_' || (i + 2) >=3D len) { + ret[j++] =3D c; + } else { + guchar a =3D virtDBusUtilHexcharToNum(str[i + 1]); + guchar b =3D virtDBusUtilHexcharToNum(str[i + 2]); + ret[j++] =3D (a << 4) + b; + i +=3D 2; + } + } + ret[j] =3D 0; + + return ret; +} + gchar * virtDBusUtilBusPathForVirDomain(virDomainPtr domain, const gchar *domainPath) diff --git a/src/util.h b/src/util.h index 4d87549..56e0409 100644 --- a/src/util.h +++ b/src/util.h @@ -42,6 +42,12 @@ virtDBusUtilGVariantToTypedParams(GVariantIter *iter, void virtDBusUtilSetLastVirtError(GError **error); =20 +gchar * +virtDBusUtilEncodeStr(const gchar *str); + +gchar * +virtDBusUtilDecodeStr(const gchar *str); + gchar * virtDBusUtilBusPathForVirDomain(virDomainPtr domain, const gchar *domainPath); diff --git a/tests/Makefile.am b/tests/Makefile.am index 5e224f8..4cae303 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -3,11 +3,31 @@ test_helpers =3D \ conftest.py =20 test_programs =3D \ + $(check_PROGRAMS) \ test_connect.py \ test_domain.py \ test_network.py \ test_storage.py =20 +check_PROGRAMS =3D \ + test_util + +test_util_SOURCES =3D \ + test_util.c $(top_srcdir)/src/util.c +test_util_CFLAGS =3D \ + -I$(top_srcdir)/src \ + $(GIO2_CFLAGS) \ + $(GLIB2_CFLAGS) \ + $(LIBVIRT_CFLAGS) +test_util_LDFLAGS =3D \ + $(GIO2_LDFLAGS) \ + $(GLIB2_LDFLAGS) \ + $(LIBVIRT_LDFLAGS) +test_util_LDADD =3D \ + $(GIO2_LIBS) \ + $(GLIB2_LIBS) \ + $(LIBVIRT_LIBS) + EXTRA_DIST =3D \ $(test_helpers) \ $(test_programs) \ diff --git a/tests/test_util.c b/tests/test_util.c new file mode 100644 index 0000000..9611192 --- /dev/null +++ b/tests/test_util.c @@ -0,0 +1,47 @@ +#include "util.h" + +static gint +virtTestEncodeStr(const gchar *input, + const gchar *expected) +{ + g_autofree gchar *encoded =3D virtDBusUtilEncodeStr(input); + + if (!g_str_equal(encoded, expected)) { + g_printerr("encode failed: expected '%s' actual '%s'\n", + expected, encoded); + return -1; + } + + return 0; +} + +static gint +virtTestDecodeStr(const gchar *input, + const gchar *expected) +{ + g_autofree gchar *decoded =3D virtDBusUtilDecodeStr(input); + + if (!g_str_equal(decoded, expected)) { + g_printerr("decode failed: expected '%s' actual '%s'\n", + expected, decoded); + return -1; + } + + return 0; +} + +gint +main(void) +{ +#define TEST_ENCODE_DECODE(input, output) \ + if (virtTestEncodeStr(input, output) < 0) \ + return EXIT_FAILURE; \ + if (virtTestDecodeStr(output, input) < 0) \ + return EXIT_FAILURE; + + TEST_ENCODE_DECODE("foobar", "foobar"); + TEST_ENCODE_DECODE("_", "_5f"); + TEST_ENCODE_DECODE("/path/to/some/file.img", "_2fpath_2fto_2fsome_2ffi= le_2eimg"); + + return EXIT_SUCCESS; +} --=20 2.17.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list