From 87703dbb86ac8fd8abd23170f8038ea6e3dbde28 Mon Sep 17 00:00:00 2001 From: Werner Koch Date: Wed, 12 Jul 2017 15:59:12 +0200 Subject: [PATCH] core: Simplify parsing of STATUS_ERROR in decrypt.c * src/decrypt.c (_gpgme_decrypt_status_handler): Factor some code out to ... (parse_status_error): new. Modernize parsing. Signed-off-by: Werner Koch --- src/conversion.c | 2 +- src/decrypt.c | 80 +++++++++++++++++++++++------------------------- 2 files changed, 40 insertions(+), 42 deletions(-) diff --git a/src/conversion.c b/src/conversion.c index 92dd2141..5b84f672 100644 --- a/src/conversion.c +++ b/src/conversion.c @@ -374,7 +374,7 @@ _gpgme_encode_percent_string (const char *src, char **destp, size_t len) /* Split a string into space delimited fields and remove leading and - * trailing spaces from each field. A pointer to the each field is + * trailing spaces from each field. A pointer to each field is * stored in ARRAY. Stop splitting at ARRAYSIZE fields. The function * modifies STRING. The number of parsed fields is returned. */ diff --git a/src/decrypt.c b/src/decrypt.c index 3b189093..91a32aef 100644 --- a/src/decrypt.c +++ b/src/decrypt.c @@ -124,7 +124,43 @@ gpgme_op_decrypt_result (gpgme_ctx_t ctx) return &opd->result; } + +/* Parse the ARGS of an error status line and record some error + * conditions at OPD. Returns 0 on success. */ +static gpgme_error_t +parse_status_error (char *args, op_data_t opd) +{ + gpgme_error_t err; + char *field[3]; + int nfields; + + nfields = _gpgme_split_fields (args, field, DIM (field)); + if (nfields < 1) + return trace_gpg_error (GPG_ERR_INV_ENGINE); /* Required arg missing. */ + err = nfields < 2 ? 0 : atoi (field[1]); + + if (!strcmp (field[0], "decrypt.algorithm")) + { + if (gpg_err_code (err) == GPG_ERR_UNSUPPORTED_ALGORITHM + && nfields > 2 + && strcmp (field[2], "?")) + { + opd->result.unsupported_algorithm = strdup (field[2]); + if (!opd->result.unsupported_algorithm) + return gpg_error_from_syserror (); + } + } + else if (!strcmp (field[0], "decrypt.keyusage")) + { + if (gpg_err_code (err) == GPG_ERR_WRONG_KEY_USAGE) + opd->result.wrong_key_usage = 1; + } + + return 0; +} + + static gpgme_error_t parse_enc_to (char *args, gpgme_recipient_t *recp, gpgme_protocol_t protocol) { @@ -230,47 +266,9 @@ _gpgme_decrypt_status_handler (void *priv, gpgme_status_code_t code, /* Note that this is an informational status code which should not lead to an error return unless it is something not related to the backend. */ - { - const char d_alg[] = "decrypt.algorithm"; - const char k_alg[] = "decrypt.keyusage"; - - if (!strncmp (args, d_alg, sizeof (d_alg) - 1)) - { - args += sizeof (d_alg) - 1; - while (*args == ' ') - args++; - - if (gpg_err_code (atoi (args)) == GPG_ERR_UNSUPPORTED_ALGORITHM) - { - char *end; - - while (*args && *args != ' ') - args++; - while (*args == ' ') - args++; - - end = strchr (args, ' '); - if (end) - *end = '\0'; - - if (!(*args == '?' && *(args + 1) == '\0')) - { - opd->result.unsupported_algorithm = strdup (args); - if (!opd->result.unsupported_algorithm) - return gpg_error_from_syserror (); - } - } - } - else if (!strncmp (args, k_alg, sizeof (k_alg) - 1)) - { - args += sizeof (k_alg) - 1; - while (*args == ' ') - args++; - - if (gpg_err_code (atoi (args)) == GPG_ERR_WRONG_KEY_USAGE) - opd->result.wrong_key_usage = 1; - } - } + err = parse_status_error (args, opd); + if (err) + return err; break; case GPGME_STATUS_ENC_TO: