From nobody Thu May 15 06:51:02 2025 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 Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1510563055561434.100222464259; Mon, 13 Nov 2017 00:50:55 -0800 (PST) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 38504C0546D0; Mon, 13 Nov 2017 08:50:54 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 131285F909; Mon, 13 Nov 2017 08:50:54 +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 D44561800C87; Mon, 13 Nov 2017 08:50:53 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id vAD8okA7016278 for ; Mon, 13 Nov 2017 03:50:46 -0500 Received: by smtp.corp.redhat.com (Postfix) id 21A7C62531; Mon, 13 Nov 2017 08:50:46 +0000 (UTC) Received: from caroline.localdomain (unknown [10.43.2.67]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C312C6253A for ; Mon, 13 Nov 2017 08:50:45 +0000 (UTC) Received: from caroline.brq.redhat.com (caroline.brq.redhat.com [127.0.0.1]) by caroline.localdomain (Postfix) with ESMTP id 6B9D7123A7B for ; Mon, 13 Nov 2017 09:50:41 +0100 (CET) From: Martin Kletzander To: libvir-list@redhat.com Date: Mon, 13 Nov 2017 09:50:22 +0100 Message-Id: <5a045a5100f77a31d8fdc281c373c88bfdf0aaf0.1510560300.git.mkletzan@redhat.com> In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 07/21] util: Introduce virBitmapNewString 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.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.32]); Mon, 13 Nov 2017 08:50:54 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Our bitmaps can be represented as data (raw bytes for which we have virBitmapNewData() and virBitmapToData()), human representation (list of numbers in a string for which we have virBitmapParse() and virBitmapFormat()) and hexadecimal string (for which we have only virBitmapToString()). So let's add the missing complement for the last one so that we can parse hexadecimal strings. Signed-off-by: Martin Kletzander Reviewed-by: John Ferlan --- src/libvirt_private.syms | 1 + src/util/virbitmap.c | 36 ++++++++++++++++++++++++++++++++++++ src/util/virbitmap.h | 4 ++++ tests/virbitmaptest.c | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 79 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index e535167e28fc..57df411602b9 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1359,6 +1359,7 @@ virBitmapNewCopy; virBitmapNewData; virBitmapNewEmpty; virBitmapNewQuiet; +virBitmapNewString; virBitmapNextClearBit; virBitmapNextSetBit; virBitmapOverlaps; diff --git a/src/util/virbitmap.c b/src/util/virbitmap.c index 0a7d3452b90c..02d1f264d859 100644 --- a/src/util/virbitmap.c +++ b/src/util/virbitmap.c @@ -36,6 +36,7 @@ #include "c-ctype.h" #include "count-one-bits.h" #include "virstring.h" +#include "virutil.h" #include "virerror.h" =20 #define VIR_FROM_THIS VIR_FROM_NONE @@ -1068,6 +1069,41 @@ virBitmapCountBits(virBitmapPtr bitmap) return ret; } =20 +/** + * virBitmapNewString: + * @string: the string + * + * Allocate a bitmap from a string of hexadecimal data. + * + * Returns a pointer to the allocated bitmap or NULL if + * memory cannot be allocated. + */ +virBitmapPtr +virBitmapNewString(const char *string) +{ + virBitmapPtr bitmap; + size_t i =3D 0; + size_t len =3D strlen(string); + + if (strspn(string, "0123456789abcdefABCDEF") !=3D len) { + virReportError(VIR_ERR_INVALID_ARG, + _("Invalid hexadecimal string '%s'"), string); + return NULL; + } + + bitmap =3D virBitmapNew(len * 4); + if (!bitmap) + return NULL; + + for (i =3D 0; i < len; i++) { + unsigned long nibble =3D virHexToBin(string[len - i - 1]); + nibble <<=3D VIR_BITMAP_BIT_OFFSET(i * 4); + bitmap->map[VIR_BITMAP_UNIT_OFFSET(i * 4)] |=3D nibble; + } + + return bitmap; +} + /** * virBitmapDataFormat: * @data: the data diff --git a/src/util/virbitmap.h b/src/util/virbitmap.h index 02acb7519d37..e964a3edc9cb 100644 --- a/src/util/virbitmap.h +++ b/src/util/virbitmap.h @@ -80,6 +80,10 @@ bool virBitmapIsBitSet(virBitmapPtr bitmap, size_t b) int virBitmapGetBit(virBitmapPtr bitmap, size_t b, bool *result) ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3) ATTRIBUTE_RETURN_CHECK; =20 +virBitmapPtr +virBitmapNewString(const char *string) + ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK; + char *virBitmapToString(virBitmapPtr bitmap, bool prefix, bool trim) ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK; =20 diff --git a/tests/virbitmaptest.c b/tests/virbitmaptest.c index 488796719dd9..3ea63e1295af 100644 --- a/tests/virbitmaptest.c +++ b/tests/virbitmaptest.c @@ -663,6 +663,42 @@ test12(const void *opaque ATTRIBUTE_UNUSED) return ret; } =20 + +/* virBitmap(New/To)String */ +static int +test13(const void *opaque ATTRIBUTE_UNUSED) +{ + virBitmapPtr map =3D NULL; + const char *strings[] =3D { "1234feebee", "000c0fefe" }; + char *str =3D NULL; + size_t i =3D 0; + int ret =3D -1; + + for (i =3D 0; i < ARRAY_CARDINALITY(strings); i++) { + map =3D virBitmapNewString(strings[i]); + str =3D virBitmapToString(map, false, true); + + if (!map || !str) + goto cleanup; + + if (STRNEQ(strings[i], str)) { + fprintf(stderr, "\n expected bitmap string '%s' actual string " + "'%s'\n", NULLSTR(strings[i]), NULLSTR(str)); + goto cleanup; + } + + VIR_FREE(str); + virBitmapFree(map); + map =3D NULL; + } + + ret =3D 0; + cleanup: + VIR_FREE(str); + virBitmapFree(map); + return ret; +} + #undef TEST_MAP =20 =20 @@ -711,6 +747,8 @@ mymain(void) =20 if (virTestRun("test12", test12, NULL) < 0) ret =3D -1; + if (virTestRun("test13", test13, NULL) < 0) + ret =3D -1; =20 return ret; } --=20 2.15.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list