From 9ae33079c2061d73c88dbe2dd79373dedc50f693 Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Thu, 24 Aug 2017 10:46:04 +0200 Subject: crypto: Use qapi_enum_parse() in qcrypto_block_luks_name_lookup() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: "Daniel P. Berrange" Signed-off-by: Markus Armbruster Message-Id: <1503564371-26090-10-git-send-email-armbru@redhat.com> Reviewed-by: Marc-André Lureau Acked-by: Daniel P. Berrange --- crypto/block-luks.c | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) (limited to 'crypto/block-luks.c') diff --git a/crypto/block-luks.c b/crypto/block-luks.c index afb8543108..c3cacdb1ed 100644 --- a/crypto/block-luks.c +++ b/crypto/block-luks.c @@ -20,6 +20,7 @@ #include "qemu/osdep.h" #include "qapi/error.h" +#include "qapi/util.h" #include "qemu/bswap.h" #include "crypto/block-luks.h" @@ -265,39 +266,33 @@ qcrypto_block_luks_cipher_alg_lookup(QCryptoCipherAlgorithm alg, * make that function emit a more friendly error message */ static int qcrypto_block_luks_name_lookup(const char *name, const char *const *map, - size_t maplen, const char *type, Error **errp) { - size_t i; - for (i = 0; i < maplen; i++) { - if (g_str_equal(map[i], name)) { - return i; - } - } + int ret = qapi_enum_parse(map, name, -1, NULL); - error_setg(errp, "%s %s not supported", type, name); - return 0; + if (ret < 0) { + error_setg(errp, "%s %s not supported", type, name); + return 0; + } + return ret; } #define qcrypto_block_luks_cipher_mode_lookup(name, errp) \ qcrypto_block_luks_name_lookup(name, \ QCryptoCipherMode_lookup, \ - QCRYPTO_CIPHER_MODE__MAX, \ "Cipher mode", \ errp) #define qcrypto_block_luks_hash_name_lookup(name, errp) \ qcrypto_block_luks_name_lookup(name, \ QCryptoHashAlgorithm_lookup, \ - QCRYPTO_HASH_ALG__MAX, \ "Hash algorithm", \ errp) #define qcrypto_block_luks_ivgen_name_lookup(name, errp) \ qcrypto_block_luks_name_lookup(name, \ QCryptoIVGenAlgorithm_lookup, \ - QCRYPTO_IVGEN_ALG__MAX, \ "IV generator", \ errp) -- cgit v1.2.3 From 5b5f825d44306b18509cd10ba9ac6983e90d6e0f Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Thu, 24 Aug 2017 10:46:07 +0200 Subject: qapi: Generate FOO_str() macro for QAPI enum FOO MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The next commit will put it to use. May look pointless now, but we're going to change the FOO_lookup's type, and then it'll help. Signed-off-by: Markus Armbruster Message-Id: <1503564371-26090-13-git-send-email-armbru@redhat.com> Reviewed-by: Marc-André Lureau --- crypto/block-luks.c | 1 - 1 file changed, 1 deletion(-) (limited to 'crypto/block-luks.c') diff --git a/crypto/block-luks.c b/crypto/block-luks.c index c3cacdb1ed..84d189a426 100644 --- a/crypto/block-luks.c +++ b/crypto/block-luks.c @@ -20,7 +20,6 @@ #include "qemu/osdep.h" #include "qapi/error.h" -#include "qapi/util.h" #include "qemu/bswap.h" #include "crypto/block-luks.h" -- cgit v1.2.3 From 977c736f80c232de7c6476d793ed575359c049d8 Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Thu, 24 Aug 2017 10:46:08 +0200 Subject: qapi: Mechanically convert FOO_lookup[...] to FOO_str(...) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Markus Armbruster Message-Id: <1503564371-26090-14-git-send-email-armbru@redhat.com> Reviewed-by: Marc-André Lureau --- crypto/block-luks.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'crypto/block-luks.c') diff --git a/crypto/block-luks.c b/crypto/block-luks.c index 84d189a426..4e828951d4 100644 --- a/crypto/block-luks.c +++ b/crypto/block-luks.c @@ -257,7 +257,7 @@ qcrypto_block_luks_cipher_alg_lookup(QCryptoCipherAlgorithm alg, } error_setg(errp, "Algorithm '%s' not supported", - QCryptoCipherAlgorithm_lookup[alg]); + QCryptoCipherAlgorithm_str(alg)); return NULL; } @@ -392,7 +392,7 @@ qcrypto_block_luks_essiv_cipher(QCryptoCipherAlgorithm cipher, break; default: error_setg(errp, "Cipher %s not supported with essiv", - QCryptoCipherAlgorithm_lookup[cipher]); + QCryptoCipherAlgorithm_str(cipher)); return 0; } } @@ -962,16 +962,16 @@ qcrypto_block_luks_create(QCryptoBlock *block, goto error; } - cipher_mode = QCryptoCipherMode_lookup[luks_opts.cipher_mode]; - ivgen_alg = QCryptoIVGenAlgorithm_lookup[luks_opts.ivgen_alg]; + cipher_mode = QCryptoCipherMode_str(luks_opts.cipher_mode); + ivgen_alg = QCryptoIVGenAlgorithm_str(luks_opts.ivgen_alg); if (luks_opts.has_ivgen_hash_alg) { - ivgen_hash_alg = QCryptoHashAlgorithm_lookup[luks_opts.ivgen_hash_alg]; + ivgen_hash_alg = QCryptoHashAlgorithm_str(luks_opts.ivgen_hash_alg); cipher_mode_spec = g_strdup_printf("%s-%s:%s", cipher_mode, ivgen_alg, ivgen_hash_alg); } else { cipher_mode_spec = g_strdup_printf("%s-%s", cipher_mode, ivgen_alg); } - hash_alg = QCryptoHashAlgorithm_lookup[luks_opts.hash_alg]; + hash_alg = QCryptoHashAlgorithm_str(luks_opts.hash_alg); if (strlen(cipher_alg) >= QCRYPTO_BLOCK_LUKS_CIPHER_NAME_LEN) { -- cgit v1.2.3 From f7abe0ecd4973dfe36944b916c5b9cf8ec199b8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Date: Thu, 24 Aug 2017 10:46:10 +0200 Subject: qapi: Change data type of the FOO_lookup generated for enum FOO MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Currently, a FOO_lookup is an array of strings terminated by a NULL sentinel. A future patch will generate enums with "holes". NULL-termination will cease to work then. To prepare for that, store the length in the FOO_lookup by wrapping it in a struct and adding a member for the length. The sentinel will be dropped next. Signed-off-by: Marc-André Lureau Message-Id: <20170822132255.23945-13-marcandre.lureau@redhat.com> [Basically redone] Signed-off-by: Markus Armbruster Message-Id: <1503564371-26090-16-git-send-email-armbru@redhat.com> [Rebased] --- crypto/block-luks.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'crypto/block-luks.c') diff --git a/crypto/block-luks.c b/crypto/block-luks.c index 4e828951d4..36bc856084 100644 --- a/crypto/block-luks.c +++ b/crypto/block-luks.c @@ -264,7 +264,7 @@ qcrypto_block_luks_cipher_alg_lookup(QCryptoCipherAlgorithm alg, /* XXX replace with qapi_enum_parse() in future, when we can * make that function emit a more friendly error message */ static int qcrypto_block_luks_name_lookup(const char *name, - const char *const *map, + const QEnumLookup *map, const char *type, Error **errp) { @@ -279,19 +279,19 @@ static int qcrypto_block_luks_name_lookup(const char *name, #define qcrypto_block_luks_cipher_mode_lookup(name, errp) \ qcrypto_block_luks_name_lookup(name, \ - QCryptoCipherMode_lookup, \ + &QCryptoCipherMode_lookup, \ "Cipher mode", \ errp) #define qcrypto_block_luks_hash_name_lookup(name, errp) \ qcrypto_block_luks_name_lookup(name, \ - QCryptoHashAlgorithm_lookup, \ + &QCryptoHashAlgorithm_lookup, \ "Hash algorithm", \ errp) #define qcrypto_block_luks_ivgen_name_lookup(name, errp) \ qcrypto_block_luks_name_lookup(name, \ - QCryptoIVGenAlgorithm_lookup, \ + &QCryptoIVGenAlgorithm_lookup, \ "IV generator", \ errp) -- cgit v1.2.3