[PATCH 06/11] util: use glib base64 encoding/decoding APIs

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Replace use of the gnulib base64 module with glib's own base64 API family.

Signed-off-by: Daniel P. Berrangé <berrange@xxxxxxxxxx>
---
 bootstrap.conf                    |  1 -
 src/conf/virsecretobj.c           | 38 +++++++------------------------
 src/libvirt_private.syms          |  1 -
 src/libxl/libxl_conf.c            |  3 +--
 src/qemu/qemu_agent.c             |  9 +++-----
 src/qemu/qemu_command.c           |  5 ++--
 src/qemu/qemu_domain.c            |  8 +++----
 src/secret/secret_driver.c        |  1 -
 src/storage/storage_backend_rbd.c |  4 +---
 src/util/virstring.c              | 21 -----------------
 src/util/virstring.h              |  2 --
 tools/virsh-secret.c              | 17 ++++----------
 12 files changed, 22 insertions(+), 88 deletions(-)

diff --git a/bootstrap.conf b/bootstrap.conf
index b6b75f9301..383e19fa70 100644
--- a/bootstrap.conf
+++ b/bootstrap.conf
@@ -21,7 +21,6 @@ gnulib_modules='
 accept
 areadlink
 autobuild
-base64
 bind
 bitrotate
 byteswap
diff --git a/src/conf/virsecretobj.c b/src/conf/virsecretobj.c
index 7800912bff..4fecb33cd3 100644
--- a/src/conf/virsecretobj.c
+++ b/src/conf/virsecretobj.c
@@ -31,7 +31,6 @@
 #include "virhash.h"
 #include "virlog.h"
 #include "virstring.h"
-#include "base64.h"
 
 #define VIR_FROM_THIS VIR_FROM_SECRET
 
@@ -698,23 +697,17 @@ virSecretObjSaveConfig(virSecretObjPtr obj)
 int
 virSecretObjSaveData(virSecretObjPtr obj)
 {
-    char *base64 = NULL;
-    int ret = -1;
+    g_autofree char *base64 = NULL;
 
     if (!obj->value)
         return 0;
 
-    if (!(base64 = virStringEncodeBase64(obj->value, obj->value_size)))
-        goto cleanup;
+    base64 = g_base64_encode(obj->value, obj->value_size);
 
     if (virFileRewriteStr(obj->base64File, S_IRUSR | S_IWUSR, base64) < 0)
-        goto cleanup;
-
-    ret = 0;
+        return -1;
 
- cleanup:
-    VIR_FREE(base64);
-    return ret;
+    return 0;
 }
 
 
@@ -835,8 +828,7 @@ virSecretLoadValue(virSecretObjPtr obj)
 {
     int ret = -1, fd = -1;
     struct stat st;
-    char *contents = NULL, *value = NULL;
-    size_t value_size;
+    char *contents = NULL;
 
     if ((fd = open(obj->base64File, O_RDONLY)) == -1) {
         if (errno == ENOENT) {
@@ -861,7 +853,7 @@ virSecretLoadValue(virSecretObjPtr obj)
         goto cleanup;
     }
 
-    if (VIR_ALLOC_N(contents, st.st_size) < 0)
+    if (VIR_ALLOC_N(contents, st.st_size + 1) < 0)
         goto cleanup;
 
     if (saferead(fd, contents, st.st_size) != st.st_size) {
@@ -869,29 +861,15 @@ virSecretLoadValue(virSecretObjPtr obj)
                              obj->base64File);
         goto cleanup;
     }
+    contents[st.st_size] = '\0';
 
     VIR_FORCE_CLOSE(fd);
 
-    if (!base64_decode_alloc(contents, st.st_size, &value, &value_size)) {
-        virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("invalid base64 in '%s'"),
-                       obj->base64File);
-        goto cleanup;
-    }
-    if (value == NULL)
-        goto cleanup;
-
-    obj->value = (unsigned char *)value;
-    value = NULL;
-    obj->value_size = value_size;
+    obj->value = g_base64_decode(contents, &obj->value_size);
 
     ret = 0;
 
  cleanup:
-    if (value != NULL) {
-        memset(value, 0, value_size);
-        VIR_FREE(value);
-    }
     if (contents != NULL) {
         memset(contents, 0, st.st_size);
         VIR_FREE(contents);
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 7b681fac64..3b5bb35375 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -3068,7 +3068,6 @@ virSkipSpacesBackwards;
 virStrcpy;
 virStrdup;
 virStringBufferIsPrintable;
-virStringEncodeBase64;
 virStringFilterChars;
 virStringHasCaseSuffix;
 virStringHasChars;
diff --git a/src/libxl/libxl_conf.c b/src/libxl/libxl_conf.c
index c76704a11d..de56567cf0 100644
--- a/src/libxl/libxl_conf.c
+++ b/src/libxl/libxl_conf.c
@@ -999,8 +999,7 @@ libxlMakeNetworkDiskSrc(virStorageSourcePtr src, char **srcstr)
             goto cleanup;
 
         /* RBD expects an encoded secret */
-        if (!(base64secret = virStringEncodeBase64(secret, secretlen)))
-            goto cleanup;
+        base64secret = g_base64_encode(secret, secretlen);
     }
 
     if (!(*srcstr = libxlMakeNetworkDiskSrcStr(src, username, base64secret)))
diff --git a/src/qemu/qemu_agent.c b/src/qemu/qemu_agent.c
index 34e1a85d64..f1978ad6f1 100644
--- a/src/qemu/qemu_agent.c
+++ b/src/qemu/qemu_agent.c
@@ -39,7 +39,6 @@
 #include "virtime.h"
 #include "virobject.h"
 #include "virstring.h"
-#include "base64.h"
 #include "virenum.h"
 
 #define VIR_FROM_THIS VIR_FROM_QEMU
@@ -2516,11 +2515,10 @@ qemuAgentSetUserPassword(qemuAgentPtr mon,
     int ret = -1;
     virJSONValuePtr cmd = NULL;
     virJSONValuePtr reply = NULL;
-    char *password64 = NULL;
+    g_autofree char *password64 = NULL;
 
-    if (!(password64 = virStringEncodeBase64((unsigned char *)password,
-                                             strlen(password))))
-        goto cleanup;
+    password64 = g_base64_encode((unsigned char *)password,
+                                 strlen(password));
 
     if (!(cmd = qemuAgentMakeCommand("guest-set-user-password",
                                      "b:crypted", crypted,
@@ -2538,7 +2536,6 @@ qemuAgentSetUserPassword(qemuAgentPtr mon,
  cleanup:
     virJSONValueFree(cmd);
     virJSONValueFree(reply);
-    VIR_FREE(password64);
     return ret;
 }
 
diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
index 77470a6037..3d9198c608 100644
--- a/src/qemu/qemu_command.c
+++ b/src/qemu/qemu_command.c
@@ -834,9 +834,8 @@ qemuBuildRBDSecinfoURI(virBufferPtr buf,
 
     switch ((qemuDomainSecretInfoType) secinfo->type) {
     case VIR_DOMAIN_SECRET_INFO_TYPE_PLAIN:
-        if (!(base64secret = virStringEncodeBase64(secinfo->s.plain.secret,
-                                                   secinfo->s.plain.secretlen)))
-            return -1;
+        base64secret = g_base64_encode(secinfo->s.plain.secret,
+                                       secinfo->s.plain.secretlen);
         virBufferEscape(buf, '\\', ":", ":id=%s", secinfo->s.plain.username);
         virBufferEscape(buf, '\\', ":",
                         ":key=%s:auth_supported=cephx\\;none",
diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c
index 2aa2164953..bfd49beb21 100644
--- a/src/qemu/qemu_domain.c
+++ b/src/qemu/qemu_domain.c
@@ -1436,8 +1436,7 @@ qemuDomainSecretAESSetup(qemuDomainObjPrivatePtr priv,
         goto cleanup;
 
     /* Encode the IV and save that since qemu will need it */
-    if (!(secinfo->s.aes.iv = virStringEncodeBase64(raw_iv, ivlen)))
-        goto cleanup;
+    secinfo->s.aes.iv = g_base64_encode(raw_iv, ivlen);
 
     /* Grab the unencoded secret */
     if (virSecretGetSecretString(conn, seclookupdef, usageType,
@@ -1454,9 +1453,8 @@ qemuDomainSecretAESSetup(qemuDomainObjPrivatePtr priv,
     memset(secret, 0, secretlen);
 
     /* Now encode the ciphertext and store to be passed to qemu */
-    if (!(secinfo->s.aes.ciphertext = virStringEncodeBase64(ciphertext,
-                                                            ciphertextlen)))
-        goto cleanup;
+    secinfo->s.aes.ciphertext = g_base64_encode(ciphertext,
+                                                ciphertextlen);
 
     ret = 0;
 
diff --git a/src/secret/secret_driver.c b/src/secret/secret_driver.c
index ed3bd3c751..13f75ee4fa 100644
--- a/src/secret/secret_driver.c
+++ b/src/secret/secret_driver.c
@@ -25,7 +25,6 @@
 #include <unistd.h>
 
 #include "internal.h"
-#include "base64.h"
 #include "datatypes.h"
 #include "driver.h"
 #include "virlog.h"
diff --git a/src/storage/storage_backend_rbd.c b/src/storage/storage_backend_rbd.c
index c4781debd8..b10ca1503d 100644
--- a/src/storage/storage_backend_rbd.c
+++ b/src/storage/storage_backend_rbd.c
@@ -28,7 +28,6 @@
 #include "storage_conf.h"
 #include "viralloc.h"
 #include "virlog.h"
-#include "base64.h"
 #include "viruuid.h"
 #include "virstring.h"
 #include "virrandom.h"
@@ -218,8 +217,7 @@ virStorageBackendRBDOpenRADOSConn(virStorageBackendRBDStatePtr ptr,
                                      &secret_value, &secret_value_size) < 0)
             goto cleanup;
 
-        if (!(rados_key = virStringEncodeBase64(secret_value, secret_value_size)))
-            goto cleanup;
+        rados_key = g_base64_encode(secret_value, secret_value_size);
 
         if (virStorageBackendRBDRADOSConfSet(ptr->cluster,
                                              "key", rados_key) < 0)
diff --git a/src/util/virstring.c b/src/util/virstring.c
index c8c888b2a0..0499904eb3 100644
--- a/src/util/virstring.c
+++ b/src/util/virstring.c
@@ -21,7 +21,6 @@
 #include <regex.h>
 #include <locale.h>
 
-#include "base64.h"
 #include "c-ctype.h"
 #include "virstring.h"
 #include "virthread.h"
@@ -1417,26 +1416,6 @@ virStringBufferIsPrintable(const uint8_t *buf,
 }
 
 
-/**
- * virStringEncodeBase64:
- * @buf: buffer of bytes to encode
- * @buflen: number of bytes to encode
- *
- * Encodes @buf to base 64 and returns the resulting string. The caller is
- * responsible for freeing the result.
- */
-char *
-virStringEncodeBase64(const uint8_t *buf, size_t buflen)
-{
-    char *ret;
-
-    base64_encode_alloc((const char *) buf, buflen, &ret);
-    if (!ret)
-        abort();
-
-    return ret;
-}
-
 /**
  * virStringTrimOptionalNewline:
  * @str: the string to modify in-place
diff --git a/src/util/virstring.h b/src/util/virstring.h
index f537f3472e..f5cbc7f91a 100644
--- a/src/util/virstring.h
+++ b/src/util/virstring.h
@@ -283,8 +283,6 @@ void virStringFilterChars(char *str, const char *valid);
 bool virStringIsPrintable(const char *str);
 bool virStringBufferIsPrintable(const uint8_t *buf, size_t buflen);
 
-char *virStringEncodeBase64(const uint8_t *buf, size_t buflen);
-
 void virStringTrimOptionalNewline(char *str);
 
 int virStringParsePort(const char *str,
diff --git a/tools/virsh-secret.c b/tools/virsh-secret.c
index b34ae12bbe..48058bea05 100644
--- a/tools/virsh-secret.c
+++ b/tools/virsh-secret.c
@@ -22,7 +22,6 @@
 #include "virsh-secret.h"
 
 #include "internal.h"
-#include "base64.h"
 #include "virbuffer.h"
 #include "viralloc.h"
 #include "virfile.h"
@@ -192,7 +191,7 @@ cmdSecretSetValue(vshControl *ctl, const vshCmd *cmd)
     virSecretPtr secret;
     size_t value_size;
     const char *base64 = NULL;
-    char *value;
+    unsigned char *value;
     int res;
     bool ret = false;
 
@@ -202,16 +201,9 @@ cmdSecretSetValue(vshControl *ctl, const vshCmd *cmd)
     if (vshCommandOptStringReq(ctl, cmd, "base64", &base64) < 0)
         goto cleanup;
 
-    if (!base64_decode_alloc(base64, strlen(base64), &value, &value_size)) {
-        vshError(ctl, "%s", _("Invalid base64 data"));
-        goto cleanup;
-    }
-    if (value == NULL) {
-        vshError(ctl, "%s", _("Failed to allocate memory"));
-        goto cleanup;
-    }
+    value = g_base64_decode(base64, &value_size);
 
-    res = virSecretSetValue(secret, (unsigned char *)value, value_size, 0);
+    res = virSecretSetValue(secret, value, value_size, 0);
     memset(value, 0, value_size);
     VIR_FREE(value);
 
@@ -267,8 +259,7 @@ cmdSecretGetValue(vshControl *ctl, const vshCmd *cmd)
     if (value == NULL)
         goto cleanup;
 
-    if (!(base64 = virStringEncodeBase64(value, value_size)))
-        goto cleanup;
+    base64 = g_base64_encode(value, value_size);
 
     vshPrint(ctl, "%s", base64);
     ret = true;
-- 
2.21.0

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux