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 <wk@gnupg.org>
This commit is contained in:
Werner Koch 2017-07-12 15:59:12 +02:00
parent 111cd562d8
commit 87703dbb86
No known key found for this signature in database
GPG Key ID: E3FDFF218E45B72B
2 changed files with 40 additions and 42 deletions

View File

@ -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 /* 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 * stored in ARRAY. Stop splitting at ARRAYSIZE fields. The function
* modifies STRING. The number of parsed fields is returned. * modifies STRING. The number of parsed fields is returned.
*/ */

View File

@ -124,7 +124,43 @@ gpgme_op_decrypt_result (gpgme_ctx_t ctx)
return &opd->result; 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 static gpgme_error_t
parse_enc_to (char *args, gpgme_recipient_t *recp, gpgme_protocol_t protocol) 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 /* Note that this is an informational status code which should
not lead to an error return unless it is something not not lead to an error return unless it is something not
related to the backend. */ related to the backend. */
{ err = parse_status_error (args, opd);
const char d_alg[] = "decrypt.algorithm"; if (err)
const char k_alg[] = "decrypt.keyusage"; return err;
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;
}
}
break; break;
case GPGME_STATUS_ENC_TO: case GPGME_STATUS_ENC_TO: