[PATCH 1/8] virCryptoHashBuf: return the length of the hash in bytes

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

 



virCryptoHashString also needs to know the size of the returned hash.
Return it if the hash conversion succeeded so the caller does not need
to access the hashinfo array.

This should make virCryptoHashString build without gnutls.
Also fixes the missing return value for the virCryptoHashBuf stub.

Signed-off-by: Ján Tomko <jtomko@xxxxxxxxxx>
Suggested-by: Stefan Berger <stefanb@xxxxxxxxxxxxxxxxxx>
---
 src/util/vircrypto.c | 14 ++++++++------
 src/util/vircrypto.h |  2 +-
 2 files changed, 9 insertions(+), 7 deletions(-)

diff --git a/src/util/vircrypto.c b/src/util/vircrypto.c
index 62a027353b..d110adfe59 100644
--- a/src/util/vircrypto.c
+++ b/src/util/vircrypto.c
@@ -54,7 +54,7 @@ struct virHashInfo {
 
 verify(ARRAY_CARDINALITY(hashinfo) == VIR_CRYPTO_HASH_LAST);
 
-int
+ssize_t
 virCryptoHashBuf(virCryptoHash hash,
                  const char *input,
                  unsigned char *output)
@@ -74,16 +74,17 @@ virCryptoHashBuf(virCryptoHash hash,
         return -1;
     }
 
-    return 0;
+    return hashinfo[hash].hashlen;
 }
 #else
-int
+ssize_t
 virCryptoHashBuf(virCryptoHash hash,
                  const char *input ATTRIBUTE_UNUSED,
                  unsigned char *output ATTRIBUTE_UNUSED)
 {
     virReportError(VIR_ERR_INVALID_ARG,
                    _("algorithm=%d is not supported"), hash);
+    return -1;
 }
 #endif
 
@@ -93,18 +94,19 @@ virCryptoHashString(virCryptoHash hash,
                     char **output)
 {
     unsigned char buf[VIR_CRYPTO_LARGEST_DIGEST_SIZE];
+    ssize_t rc;
     size_t hashstrlen;
     size_t i;
 
-    if (virCryptoHashBuf(hash, input, buf) < 0)
+    if ((rc = virCryptoHashBuf(hash, input, buf)) < 0)
         return -1;
 
-    hashstrlen = (hashinfo[hash].hashlen * 2) + 1;
+    hashstrlen = (rc * 2) + 1;
 
     if (VIR_ALLOC_N(*output, hashstrlen) < 0)
         return -1;
 
-    for (i = 0; i < hashinfo[hash].hashlen; i++) {
+    for (i = 0; i < rc; i++) {
         (*output)[i * 2] = hex[(buf[i] >> 4) & 0xf];
         (*output)[(i * 2) + 1] = hex[buf[i] & 0xf];
     }
diff --git a/src/util/vircrypto.h b/src/util/vircrypto.h
index 64984006be..9b5dada53d 100644
--- a/src/util/vircrypto.h
+++ b/src/util/vircrypto.h
@@ -41,7 +41,7 @@ typedef enum {
     VIR_CRYPTO_CIPHER_LAST
 } virCryptoCipher;
 
-int
+ssize_t
 virCryptoHashBuf(virCryptoHash hash,
                  const char *input,
                  unsigned char *output)
-- 
2.16.1

--
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