From 088c044792fd352b0dcb00bb31cc40aec38d3ebf Mon Sep 17 00:00:00 2001 From: Marcus Brinkmann Date: Wed, 30 Jan 2002 21:52:32 +0000 Subject: [PATCH] doc/ 2002-01-30 Marcus Brinkmann * gpgme.texi (Importing Keys): Add reference to gpgme_get_op_info. gpgme/ 2002-01-30 Marcus Brinkmann * import.c (struct import_result_s): New structure. (_gpgme_release_import_result): New function. (append_xml_impinfo): Likewise. (import_status_handler): Implement. * gpgme.c (_gpgme_release_result): Add call to _gpgme_release_import_result. * ops.h (_gpgme_release_impoer_result): Add prototype. * types.h (ImportResult): New type. * context.h (gpgme_context_s): Add ImportResult to member result. * encrypt.c (gpgme_op_encrypt): Code clean up. tests/ 2002-01-30 Marcus Brinkmann * gpg/t-import.c (print_op_info): New function. (main): Use it. --- doc/ChangeLog | 4 ++ doc/gpgme.texi | 3 + gpgme/ChangeLog | 14 ++++ gpgme/context.h | 1 + gpgme/encrypt.c | 154 ++++++++++++++++++++++--------------------- gpgme/gpgme.c | 1 + gpgme/import.c | 148 ++++++++++++++++++++++++++++++++++++++--- gpgme/ops.h | 3 + gpgme/types.h | 4 +- tests/ChangeLog | 5 ++ tests/gpg/t-import.c | 17 +++++ 11 files changed, 267 insertions(+), 87 deletions(-) diff --git a/doc/ChangeLog b/doc/ChangeLog index c385020..bfe382e 100644 --- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -1,3 +1,7 @@ +2002-01-30 Marcus Brinkmann + + * gpgme.texi (Importing Keys): Add reference to gpgme_get_op_info. + 2002-01-30 Marcus Brinkmann * gpgme.texi: Some spell checking. diff --git a/doc/gpgme.texi b/doc/gpgme.texi index e473127..44b6997 100644 --- a/doc/gpgme.texi +++ b/doc/gpgme.texi @@ -1529,6 +1529,9 @@ The function @code{gpgme_op_import} adds the keys in the data buffer The format of @var{keydata} can be @var{ASCII} armored, for example, but the details are specific to the crypto engine. +More information about the import is available with +@code{gpgme_get_op_info}. @xref{Detailed Results}. + The function returns @code{GPGME_No_Error} if the import was completed successfully, @code{GPGME_Invalid_Value} if @var{keydata} if @var{ctx} or @var{keydata} is not a valid pointer, and @code{GPGME_No_Data} if diff --git a/gpgme/ChangeLog b/gpgme/ChangeLog index 2c0851f..010c21e 100644 --- a/gpgme/ChangeLog +++ b/gpgme/ChangeLog @@ -1,3 +1,17 @@ +2002-01-30 Marcus Brinkmann + + * import.c (struct import_result_s): New structure. + (_gpgme_release_import_result): New function. + (append_xml_impinfo): Likewise. + (import_status_handler): Implement. + * gpgme.c (_gpgme_release_result): Add call to + _gpgme_release_import_result. + * ops.h (_gpgme_release_impoer_result): Add prototype. + * types.h (ImportResult): New type. + * context.h (gpgme_context_s): Add ImportResult to member result. + + * encrypt.c (gpgme_op_encrypt): Code clean up. + 2002-01-30 Marcus Brinkmann * gpgme.h: Add lots of comment and fix the formatting. Add diff --git a/gpgme/context.h b/gpgme/context.h index df2f40a..02d3954 100644 --- a/gpgme/context.h +++ b/gpgme/context.h @@ -68,6 +68,7 @@ struct gpgme_context_s { SignResult sign; EncryptResult encrypt; PassphraseResult passphrase; + ImportResult import; } result; GpgmeData notation; /* last signature notation */ diff --git a/gpgme/encrypt.c b/gpgme/encrypt.c index 3c457f0..43d1b2c 100644 --- a/gpgme/encrypt.c +++ b/gpgme/encrypt.c @@ -59,84 +59,84 @@ _gpgme_release_encrypt_result (EncryptResult result) static void append_xml_encinfo (GpgmeData *rdh, char *args) { - GpgmeData dh; - char helpbuf[100]; + GpgmeData dh; + char helpbuf[100]; - if ( !*rdh ) { - if (gpgme_data_new (rdh)) { - return; /* fixme: We are ignoring out-of-core */ - } - dh = *rdh; - _gpgme_data_append_string (dh, "\n"); + if (!*rdh) + { + if (gpgme_data_new (rdh)) + return; /* FIXME: We are ignoring out-of-core. */ + dh = *rdh; + _gpgme_data_append_string (dh, "\n"); } - else { - dh = *rdh; - _gpgme_data_append_string (dh, " \n"); + else + { + dh = *rdh; + _gpgme_data_append_string (dh, " \n"); } - if (!args) { /* just close the XML containter */ - _gpgme_data_append_string (dh, "\n"); - return; + if (!args) + { + /* Just close the XML containter. */ + _gpgme_data_append_string (dh, "\n"); + return; } - _gpgme_data_append_string (dh, " \n" - " \n" - " \n"); + _gpgme_data_append_string (dh, " \n" + " \n" + " \n"); - sprintf (helpbuf, " %d\n", atoi (args)); - _gpgme_data_append_string (dh, helpbuf); - SKIP_TOKEN_OR_RETURN (args); - - _gpgme_data_append_string (dh, " "); - _gpgme_data_append_percentstring_for_xml (dh, args); - _gpgme_data_append_string (dh, "\n" - " \n"); + sprintf (helpbuf, " %d\n", atoi (args)); + _gpgme_data_append_string (dh, helpbuf); + SKIP_TOKEN_OR_RETURN (args); + + _gpgme_data_append_string (dh, " "); + _gpgme_data_append_percentstring_for_xml (dh, args); + _gpgme_data_append_string (dh, "\n" + " \n"); } - - - static void encrypt_status_handler (GpgmeCtx ctx, GpgStatusCode code, char *args) { - if (ctx->out_of_core) - return; - if (!ctx->result.encrypt) - { - ctx->result.encrypt = xtrycalloc (1, sizeof *ctx->result.encrypt); - if (!ctx->result.encrypt) - { - ctx->out_of_core = 1; - return; - } - } - - switch (code) { - case STATUS_EOF: - if (ctx->result.encrypt->xmlinfo) { - append_xml_encinfo (&ctx->result.encrypt->xmlinfo, NULL); - _gpgme_set_op_info (ctx, ctx->result.encrypt->xmlinfo); - ctx->result.encrypt->xmlinfo = NULL; + if (ctx->out_of_core) + return; + if (!ctx->result.encrypt) + { + ctx->result.encrypt = xtrycalloc (1, sizeof *ctx->result.encrypt); + if (!ctx->result.encrypt) + { + ctx->out_of_core = 1; + return; + } + } + + switch (code) + { + case STATUS_EOF: + if (ctx->result.encrypt->xmlinfo) + { + append_xml_encinfo (&ctx->result.encrypt->xmlinfo, NULL); + _gpgme_set_op_info (ctx, ctx->result.encrypt->xmlinfo); + ctx->result.encrypt->xmlinfo = NULL; } - break; + break; - case STATUS_INV_RECP: - append_xml_encinfo (&ctx->result.encrypt->xmlinfo, args); - break; + case STATUS_INV_RECP: + append_xml_encinfo (&ctx->result.encrypt->xmlinfo, args); + break; - case STATUS_NO_RECP: - ctx->result.encrypt->no_recipients = 1; /* i.e. no usable ones */ - break; + case STATUS_NO_RECP: + ctx->result.encrypt->no_recipients = 1; /* i.e. no usable ones */ + break; - default: - break; + default: + break; } - } - GpgmeError gpgme_op_encrypt_start (GpgmeCtx ctx, GpgmeRecipients recp, GpgmeData plain, GpgmeData ciph) @@ -213,28 +213,30 @@ gpgme_op_encrypt_start (GpgmeCtx ctx, GpgmeRecipients recp, GpgmeData plain, * Return value: 0 on success or an errorcode. **/ GpgmeError -gpgme_op_encrypt ( GpgmeCtx c, GpgmeRecipients recp, - GpgmeData in, GpgmeData out ) +gpgme_op_encrypt (GpgmeCtx ctx, GpgmeRecipients recp, + GpgmeData plain, GpgmeData cipher) { - int err = gpgme_op_encrypt_start ( c, recp, in, out ); - if ( !err ) { - gpgme_wait (c, 1); - if (!c->result.encrypt) - err = mk_error (General_Error); - else if (c->out_of_core) - err = mk_error (Out_Of_Core); - else { - if (c->result.encrypt->no_recipients) - err = mk_error (No_Recipients); + int err = gpgme_op_encrypt_start (ctx, recp, plain, cipher); + if (!err) + { + gpgme_wait (ctx, 1); + if (!ctx->result.encrypt) + err = mk_error (General_Error); + else if (ctx->out_of_core) + err = mk_error (Out_Of_Core); + else + { + if (ctx->result.encrypt->no_recipients) + err = mk_error (No_Recipients); } - /* Old gpg versions don't return status info for invalid - * recipients, so we simply check whether we got any output at - * all and if not assume that we don't have valid recipients - * */ - if (!err && gpgme_data_get_type (out) == GPGME_DATA_TYPE_NONE) - err = mk_error (No_Recipients); + /* Old gpg versions don't return status info for invalid + recipients, so we simply check whether we got any output at + all, and if not we assume that we don't have valid + recipients. */ + if (!err && gpgme_data_get_type (cipher) == GPGME_DATA_TYPE_NONE) + err = mk_error (No_Recipients); } - return err; + return err; } diff --git a/gpgme/gpgme.c b/gpgme/gpgme.c index fe84832..25e6b61 100644 --- a/gpgme/gpgme.c +++ b/gpgme/gpgme.c @@ -86,6 +86,7 @@ _gpgme_release_result (GpgmeCtx ctx) _gpgme_release_sign_result (ctx->result.sign); _gpgme_release_encrypt_result (ctx->result.encrypt); _gpgme_release_passphrase_result (ctx->result.passphrase); + _gpgme_release_import_result (ctx->result.import); memset (&ctx->result, 0, sizeof (ctx->result)); _gpgme_set_op_info (ctx, NULL); } diff --git a/gpgme/import.c b/gpgme/import.c index 2f830c7..8199057 100644 --- a/gpgme/import.c +++ b/gpgme/import.c @@ -1,6 +1,6 @@ /* import.c - encrypt functions * Copyright (C) 2000 Werner Koch (dd9jn) - * Copyright (C) 2001 g10 Code GmbH + * Copyright (C) 2001, 2002 g10 Code GmbH * * This file is part of GPGME. * @@ -29,15 +29,146 @@ #include "context.h" #include "ops.h" + +struct import_result_s +{ + GpgmeData xmlinfo; +}; + + +void +_gpgme_release_import_result (ImportResult result) +{ + if (!result) + return; + gpgme_data_release (result->xmlinfo); + xfree (result); +} + + +/* Parse the args and append the information to the XML structure in + the data buffer. With args of NULL the xml structure is + closed. */ static void -import_status_handler ( GpgmeCtx ctx, GpgStatusCode code, char *args ) +append_xml_impinfo (GpgmeData *rdh, GpgStatusCode code, char *args) { - DEBUG2 ("import_status: code=%d args=`%s'\n", code, args ); - /* FIXME: We have to check here whether the import actually worked - * and maybe it is a good idea to save some statistics and provide - * a progress callback */ +#define MAX_IMPORTED_FIELDS 13 + static char *imported_fields[MAX_IMPORTED_FIELDS] + = { "keyid", "username", 0 }; + static char *import_res_fields[MAX_IMPORTED_FIELDS] + = { "count", "no_user_id", "imported", "imported_rsa", + "unchanged", "n_uids", "n_subk", "n_sigs", "s_sigsn_revoc", + "sec_read", "sec_imported", "sec_dups", "skipped_new", 0 }; + char *field[MAX_IMPORTED_FIELDS]; + char **field_name = 0; + GpgmeData dh; + int i; + + /* Verify that we can use the args. */ + if (code != STATUS_EOF) + { + if (!args) + return; + + if (code == STATUS_IMPORTED) + field_name = imported_fields; + else if (code == STATUS_IMPORT_RES) + field_name = import_res_fields; + else + return; + + for (i = 0; field_name[i]; i++) + { + field[i] = args; + if (field_name[i + 1]) + { + args = strchr (args, ' '); + if (!args) + return; /* Invalid line. */ + *args++ = '\0'; + } + } + } + + /* Initialize the data buffer if necessary. */ + if (!*rdh) + { + if (gpgme_data_new (rdh)) + return; /* FIXME: We are ignoring out-of-core. */ + dh = *rdh; + _gpgme_data_append_string (dh, "\n"); + } + else + dh = *rdh; + + if (code == STATUS_EOF) + { + /* Just close the XML containter. */ + _gpgme_data_append_string (dh, "\n"); + } + else + { + if (code == STATUS_IMPORTED) + _gpgme_data_append_string (dh, " \n"); + else if (code == STATUS_IMPORT_RES) + _gpgme_data_append_string (dh, " \n"); + + for (i = 0; field_name[i]; i++) + { + _gpgme_data_append_string (dh, " <"); + _gpgme_data_append_string (dh, field_name[i]); + _gpgme_data_append_string (dh, ">"); + _gpgme_data_append_string (dh, field[i]); + _gpgme_data_append_string (dh, "\n"); + } + + if (code == STATUS_IMPORTED) + _gpgme_data_append_string (dh, " \n"); + else if (code == STATUS_IMPORT_RES) + _gpgme_data_append_string (dh, " \n"); + } } + +static void +import_status_handler (GpgmeCtx ctx, GpgStatusCode code, char *args) +{ + if (ctx->out_of_core) + return; + if (!ctx->result.import) + { + ctx->result.import = xtrycalloc (1, sizeof *ctx->result.import); + if (!ctx->result.import) + { + ctx->out_of_core = 1; + return; + } + } + + switch (code) + { + case STATUS_EOF: + if (ctx->result.import->xmlinfo) + { + append_xml_impinfo (&ctx->result.import->xmlinfo, code, NULL); + _gpgme_set_op_info (ctx, ctx->result.import->xmlinfo); + ctx->result.import->xmlinfo = NULL; + } + break; + + case STATUS_IMPORTED: + case STATUS_IMPORT_RES: + append_xml_impinfo (&ctx->result.import->xmlinfo, code, args); + break; + + default: + break; + } +} + + GpgmeError gpgme_op_import_start (GpgmeCtx ctx, GpgmeData keydata) { @@ -79,6 +210,7 @@ gpgme_op_import_start (GpgmeCtx ctx, GpgmeData keydata) return err; } + /** * gpgme_op_import: * @c: Context @@ -96,7 +228,3 @@ gpgme_op_import (GpgmeCtx ctx, GpgmeData keydata) gpgme_wait (ctx, 1); return err; } - - - - diff --git a/gpgme/ops.h b/gpgme/ops.h index 93103b5..dcdeba7 100644 --- a/gpgme/ops.h +++ b/gpgme/ops.h @@ -95,6 +95,9 @@ void _gpgme_passphrase_status_handler (GpgmeCtx ctx, GpgStatusCode code, GpgmeError _gpgme_passphrase_start (GpgmeCtx ctx); GpgmeError _gpgme_passphrase_result (GpgmeCtx ctx); +/*-- import.c --*/ +void _gpgme_release_import_result (ImportResult res); + /*-- version.c --*/ const char *_gpgme_compare_versions (const char *my_version, const char *req_version); diff --git a/gpgme/types.h b/gpgme/types.h index 8f80a0b..71dab9a 100644 --- a/gpgme/types.h +++ b/gpgme/types.h @@ -76,7 +76,9 @@ typedef struct encrypt_result_s *EncryptResult; struct passphrase_result_s; typedef struct passphrase_result_s *PassphraseResult; -/*-- key.c --*/ +/*-- import.c --*/ +struct import_result_s; +typedef struct import_result_s *ImportResult; #endif /* TYPES_H */ diff --git a/tests/ChangeLog b/tests/ChangeLog index 3fec7f7..e736952 100644 --- a/tests/ChangeLog +++ b/tests/ChangeLog @@ -1,3 +1,8 @@ +2002-01-30 Marcus Brinkmann + + * gpg/t-import.c (print_op_info): New function. + (main): Use it. + 2002-01-22 Marcus Brinkmann * gpg/t-signers.c (passphrase_cb): Change type of r_hd to void**. diff --git a/tests/gpg/t-import.c b/tests/gpg/t-import.c index 25ea20f..7bf2124 100644 --- a/tests/gpg/t-import.c +++ b/tests/gpg/t-import.c @@ -54,6 +54,21 @@ mk_fname ( const char *fname ) return buf; } + +static void +print_op_info (GpgmeCtx c) +{ + char *s = gpgme_get_op_info (c, 0); + + if (!s) + puts (""); + else { + puts (s); + free (s); + } +} + + int main (int argc, char **argv ) { @@ -72,6 +87,7 @@ main (int argc, char **argv ) err = gpgme_op_import (ctx, in ); fail_if_err (err); + print_op_info (ctx); gpgme_data_release (in); @@ -80,6 +96,7 @@ main (int argc, char **argv ) err = gpgme_op_import (ctx, in ); fail_if_err (err); + print_op_info (ctx); gpgme_data_release (in); gpgme_release (ctx); -- 2.26.2