2003-04-25 Marcus Brinkmann <marcus@g10code.de>
authorMarcus Brinkmann <mb@g10code.com>
Fri, 25 Apr 2003 14:24:17 +0000 (14:24 +0000)
committerMarcus Brinkmann <mb@g10code.com>
Fri, 25 Apr 2003 14:24:17 +0000 (14:24 +0000)
* ops.h (_gpgme_passphrase_start): Remove prototype.
* passphrase.c: Do not include <assert.h>, "util.h" or
"debug.h", but "gpgme.h".
(struct passphrase_result): Change to typedef for op_data_t.
(release_passphrase_result): Rename to release_op_data.
(_gpgme_passphrase_status_handler): Change first argument to void *.
Use new op_data_t type.
(_gpgme_passphrase_command_handler): Use new op_data_t type.
(_gpgme_passphrase_start): Remove function.
* decrypt.c (_gpgme_decrypt_start): Rewrite error handling.  Do
not call _gpgme_passphrase_start, but install command handler.
* encrypt.c (_gpgme_op_encrypt_start): Likewise.
* encrypt-sign.c (_gpgme_op_encrypt_sign_start): Likewise.
* sign.c (_gpgme_op_sign_start): Likewise.

gpgme/ChangeLog
gpgme/decrypt.c
gpgme/encrypt-sign.c
gpgme/encrypt.c
gpgme/ops.h
gpgme/passphrase.c
gpgme/sign.c

index f0a4010..39347e4 100644 (file)
@@ -1,5 +1,20 @@
 2003-04-25  Marcus Brinkmann  <marcus@g10code.de>
 
+       * ops.h (_gpgme_passphrase_start): Remove prototype.
+       * passphrase.c: Do not include <assert.h>, "util.h" or
+       "debug.h", but "gpgme.h".
+       (struct passphrase_result): Change to typedef for op_data_t.
+       (release_passphrase_result): Rename to release_op_data.
+       (_gpgme_passphrase_status_handler): Change first argument to void *.
+       Use new op_data_t type.
+       (_gpgme_passphrase_command_handler): Use new op_data_t type.
+       (_gpgme_passphrase_start): Remove function.
+       * decrypt.c (_gpgme_decrypt_start): Rewrite error handling.  Do
+       not call _gpgme_passphrase_start, but install command handler.
+       * encrypt.c (_gpgme_op_encrypt_start): Likewise.
+       * encrypt-sign.c (_gpgme_op_encrypt_sign_start): Likewise.
+       * sign.c (_gpgme_op_sign_start): Likewise.
+
        * context.h (struct gpgme_context_s): Remove member initialized,
        use_cms and help_data_1.  Add member protocol.  Make use_armor and
        use_textmode bit flags.  Make keylist_mode, include_certs,
index a27e156..e10a16d 100644 (file)
@@ -163,41 +163,34 @@ _gpgme_decrypt_status_handler (GpgmeCtx ctx, GpgmeStatusCode code, char *args)
 
 GpgmeError
 _gpgme_decrypt_start (GpgmeCtx ctx, int synchronous,
-                     GpgmeData ciph, GpgmeData plain, void *status_handler)
+                     GpgmeData cipher, GpgmeData plain, void *status_handler)
 {
-  GpgmeError err = 0;
+  GpgmeError err;
 
   err = _gpgme_op_reset (ctx, synchronous);
   if (err)
-    goto leave;
+    return err;
 
-  /* Check the supplied data.  */
-  if (!ciph)
-    {
-      err = GPGME_No_Data;
-      goto leave;
-    }
+  if (!cipher)
+    return GPGME_No_Data;
   if (!plain)
-    {
-      err = GPGME_Invalid_Value;
-      goto leave;
-    }
+    return GPGME_Invalid_Value;
 
-  err = _gpgme_passphrase_start (ctx);
   if (err)
-    goto leave;
-
-  _gpgme_engine_set_status_handler (ctx->engine, status_handler, ctx);
-
-  err = _gpgme_engine_op_decrypt (ctx->engine, ciph, plain);
+    return err;
 
- leave:
-  if (err)
+  if (ctx->passphrase_cb)
     {
-      _gpgme_engine_release (ctx->engine);
-      ctx->engine = NULL;
+      err = _gpgme_engine_set_command_handler (ctx->engine,
+                                              _gpgme_passphrase_command_handler,
+                                              ctx, NULL);
+      if (err)
+       return err;
     }
-  return err;
+
+  _gpgme_engine_set_status_handler (ctx->engine, status_handler, ctx);
+
+  return _gpgme_engine_op_decrypt (ctx->engine, cipher, plain);
 }
 
 
index 2ed1b1c..68ab115 100644 (file)
 
 
 static GpgmeError
-encrypt_sign_status_handler (GpgmeCtx ctx, GpgmeStatusCode code, char *args)
+encrypt_sign_status_handler (void *priv, GpgmeStatusCode code, char *args)
 {
-  GpgmeError err = _gpgme_encrypt_status_handler (ctx, code, args);
-  if (err)
-    return err;
-  return _gpgme_sign_status_handler (ctx, code, args);
+  return _gpgme_encrypt_status_handler (priv, code, args)
+    || _gpgme_sign_status_handler (priv, code, args);
 }
 
 
@@ -47,41 +45,31 @@ _gpgme_op_encrypt_sign_start (GpgmeCtx ctx, int synchronous,
                              GpgmeRecipients recp,
                              GpgmeData plain, GpgmeData cipher)
 {
-  GpgmeError err = 0;
+  GpgmeError err;
 
   err = _gpgme_op_reset (ctx, synchronous);
   if (err)
-    goto leave;
-
-  err = _gpgme_passphrase_start (ctx);
-  if (err)
-    goto leave;
-
-  _gpgme_engine_set_status_handler (ctx->engine,
-                                   encrypt_sign_status_handler, ctx);
+    return err;
 
-  /* Check the supplied data */
   if (!plain)
-    {
-      err = GPGME_No_Data;
-      goto leave;
-    }
+    return GPGME_No_Data;
   if (!cipher)
+    return GPGME_Invalid_Value;
+
+  if (ctx->passphrase_cb)
     {
-      err = GPGME_Invalid_Value;
-      goto leave;
+      err = _gpgme_engine_set_command_handler (ctx->engine,
+                                              _gpgme_passphrase_command_handler,
+                                              ctx, NULL);
+      if (err)
+       return err;
     }
 
-  err = _gpgme_engine_op_encrypt_sign (ctx->engine, recp, plain, cipher,
-                                      ctx->use_armor, ctx /* FIXME */);
+  _gpgme_engine_set_status_handler (ctx->engine,
+                                   encrypt_sign_status_handler, ctx);
 
- leave:
-  if (err)
-    {
-      _gpgme_engine_release (ctx->engine);
-      ctx->engine = NULL;
-    }
-  return err;
+  return _gpgme_engine_op_encrypt_sign (ctx->engine, recp, plain, cipher,
+                                       ctx->use_armor, ctx /* FIXME */);
 }
 
 GpgmeError
index 86de371..e0fcc57 100644 (file)
@@ -158,29 +158,33 @@ _gpgme_encrypt_sym_status_handler (GpgmeCtx ctx, GpgmeStatusCode code,
 
 static GpgmeError
 _gpgme_op_encrypt_start (GpgmeCtx ctx, int synchronous,
-                        GpgmeRecipients recp, GpgmeData plain, GpgmeData ciph)
+                        GpgmeRecipients recp, GpgmeData plain, GpgmeData cipher)
 {
-  GpgmeError err = 0;
+  GpgmeError err;
   int symmetric = 0;
 
-  /* Do some checks.  */
+  err = _gpgme_op_reset (ctx, synchronous);
+  if (err)
+    return err;
+
   if (!recp)
     symmetric = 1;
-  else if (!gpgme_recipients_count (recp))
-    {
-      err = GPGME_No_UserID;
-      goto leave;
-    }
+  else if (gpgme_recipients_count (recp) == 0)
+    return GPGME_No_UserID;
 
-  err = _gpgme_op_reset (ctx, synchronous);
-  if (err)
-    goto leave;
+  if (!plain)
+    return GPGME_No_Data;
+  if (!cipher)
+    return GPGME_Invalid_Value;
 
-  if (symmetric)
+  if (symmetric && ctx->passphrase_cb)
     {
-      err = _gpgme_passphrase_start (ctx);
+      /* Symmetric encryption requires a passphrase.  */
+      err = _gpgme_engine_set_command_handler (ctx->engine,
+                                              _gpgme_passphrase_command_handler,
+                                              ctx, NULL);
       if (err)
-       goto leave;
+       return err;
     }
 
   _gpgme_engine_set_status_handler (ctx->engine,
@@ -189,28 +193,8 @@ _gpgme_op_encrypt_start (GpgmeCtx ctx, int synchronous,
                                    : _gpgme_encrypt_status_handler,
                                    ctx);
 
-  /* Check the supplied data */
-  if (!plain)
-    {
-      err = GPGME_No_Data;
-      goto leave;
-    }
-  if (!ciph)
-    {
-      err = GPGME_Invalid_Value;
-      goto leave;
-    }
-
-  err = _gpgme_engine_op_encrypt (ctx->engine, recp, plain, ciph,
-                                 ctx->use_armor);
-
- leave:
-  if (err)
-    {
-      _gpgme_engine_release (ctx->engine);
-      ctx->engine = NULL;
-    }
-  return err;
+  return _gpgme_engine_op_encrypt (ctx->engine, recp, plain, cipher,
+                                  ctx->use_armor);
 }
 
 
index c21de0b..61ec8d7 100644 (file)
@@ -83,12 +83,11 @@ GpgmeError _gpgme_encrypt_status_handler (GpgmeCtx ctx, GpgmeStatusCode code,
                                          char *args);
 
 /*-- passphrase.c --*/
-GpgmeError _gpgme_passphrase_status_handler (GpgmeCtx ctx, GpgmeStatusCode code,
+GpgmeError _gpgme_passphrase_status_handler (void *priv, GpgmeStatusCode code,
                                             char *args);
 GpgmeError _gpgme_passphrase_command_handler (void *opaque,
                                              GpgmeStatusCode code,
                                              const char *key, const char **result);
-GpgmeError _gpgme_passphrase_start (GpgmeCtx ctx);
 
 /*-- progress.c --*/
 GpgmeError _gpgme_progress_status_handler (GpgmeCtx ctx, GpgmeStatusCode code,
index d56562f..4493d5b 100644 (file)
@@ -1,4 +1,4 @@
-/* passphrase.c -  passphrase functions
+/* passphrase.c - Passphrase callback.
    Copyright (C) 2000 Werner Koch (dd9jn)
    Copyright (C) 2001, 2002, 2003 g10 Code GmbH
  
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <assert.h>
 
-#include "util.h"
+#include "gpgme.h"
 #include "context.h"
 #include "ops.h"
-#include "debug.h"
 
 \f
-struct passphrase_result
+typedef struct
 {
   int no_passphrase;
   void *last_pw_handle;
   char *userid_hint;
   char *passphrase_info;
   int bad_passphrase;
-};
-typedef struct passphrase_result *PassphraseResult;
+} *op_data_t;
+
 
 static void
-release_passphrase_result (void *hook)
+release_op_data (void *hook)
 {
-  PassphraseResult result = (PassphraseResult) hook;
+  op_data_t opd = (op_data_t) hook;
 
-  free (result->passphrase_info);
-  free (result->userid_hint);
+  free (opd->passphrase_info);
+  free (opd->userid_hint);
 }
 
 \f
 GpgmeError
-_gpgme_passphrase_status_handler (GpgmeCtx ctx, GpgmeStatusCode code, char *args)
+_gpgme_passphrase_status_handler (void *priv, GpgmeStatusCode code, char *args)
 {
+  GpgmeCtx ctx = (GpgmeCtx) priv;
   GpgmeError err;
-  PassphraseResult result;
+  op_data_t opd;
+
+  if (!ctx->passphrase_cb)
+    return 0;
 
-  err = _gpgme_op_data_lookup (ctx, OPDATA_PASSPHRASE, (void **) &result,
-                              sizeof (*result), release_passphrase_result);
+  err = _gpgme_op_data_lookup (ctx, OPDATA_PASSPHRASE, (void **) &opd,
+                              sizeof (*opd), release_op_data);
   if (err)
     return err;
 
   switch (code)
     {
     case GPGME_STATUS_USERID_HINT:
-      free (result->userid_hint);
-      if (!(result->userid_hint = strdup (args)))
+      if (opd->userid_hint)
+       free (opd->userid_hint);
+      if (!(opd->userid_hint = strdup (args)))
        return GPGME_Out_Of_Core;
       break;
 
     case GPGME_STATUS_BAD_PASSPHRASE:
-      result->bad_passphrase++;
-      result->no_passphrase = 0;
+      opd->bad_passphrase++;
+      opd->no_passphrase = 0;
       break;
 
     case GPGME_STATUS_GOOD_PASSPHRASE:
-      result->bad_passphrase = 0;
-      result->no_passphrase = 0;
+      opd->bad_passphrase = 0;
+      opd->no_passphrase = 0;
       break;
 
     case GPGME_STATUS_NEED_PASSPHRASE:
     case GPGME_STATUS_NEED_PASSPHRASE_SYM:
-      free (result->passphrase_info);
-      result->passphrase_info = strdup (args);
-      if (!result->passphrase_info)
+      if (opd->passphrase_info)
+       free (opd->passphrase_info);
+      opd->passphrase_info = strdup (args);
+      if (!opd->passphrase_info)
        return GPGME_Out_Of_Core;
       break;
 
     case GPGME_STATUS_MISSING_PASSPHRASE:
-      DEBUG0 ("missing passphrase - stop\n");;
-      result->no_passphrase = 1;
+      opd->no_passphrase = 1;
       break;
 
     case GPGME_STATUS_EOF:
-      if (result->no_passphrase
-         || result->bad_passphrase)
+      if (opd->no_passphrase || opd->bad_passphrase)
        return GPGME_Bad_Passphrase;
       break;
 
@@ -109,15 +111,18 @@ _gpgme_passphrase_status_handler (GpgmeCtx ctx, GpgmeStatusCode code, char *args
 
 
 GpgmeError
-_gpgme_passphrase_command_handler (void *opaque, GpgmeStatusCode code,
-                                  const char *key, const char **result_r)
+_gpgme_passphrase_command_handler (void *priv, GpgmeStatusCode code,
+                                  const char *key, const char **result)
 {
-  GpgmeCtx ctx = opaque;
+  GpgmeCtx ctx = (GpgmeCtx) priv;
   GpgmeError err;
-  PassphraseResult result;
+  op_data_t opd;
+
+  if (!ctx->passphrase_cb)
+    return 0;
 
-  err = _gpgme_op_data_lookup (ctx, OPDATA_PASSPHRASE, (void **) &result,
-                              sizeof (*result), release_passphrase_result);
+  err = _gpgme_op_data_lookup (ctx, OPDATA_PASSPHRASE, (void **) &opd,
+                              sizeof (*opd), release_op_data);
   if (err)
     return err;
 
@@ -125,58 +130,45 @@ _gpgme_passphrase_command_handler (void *opaque, GpgmeStatusCode code,
     {
       /* We have been called for cleanup.  */
       if (ctx->passphrase_cb)
-       /* Fixme: Take the key in account.  */
-       err = ctx->passphrase_cb (ctx->passphrase_cb_value, NULL, 
-                                 &result->last_pw_handle, NULL);
-      *result_r = NULL;
+       /* FIXME: Take the key in account.  */
+       err = ctx->passphrase_cb (ctx->passphrase_cb_value, NULL,
+                                 &opd->last_pw_handle, NULL);
+      *result = NULL;
       return err;
     }
 
   if (!key || !ctx->passphrase_cb)
     {
-      *result_r = NULL;
+      *result = NULL;
       return 0;
     }
     
   if (code == GPGME_STATUS_GET_HIDDEN && !strcmp (key, "passphrase.enter"))
     {
-      const char *userid_hint = result->userid_hint;
-      const char *passphrase_info = result->passphrase_info;
-      int bad_passphrase = result->bad_passphrase;
+      const char *userid_hint = opd->userid_hint;
+      const char *passphrase_info = opd->passphrase_info;
+      int bad_passphrase = opd->bad_passphrase;
       char *buf;
 
-      result->bad_passphrase = 0;
+      opd->bad_passphrase = 0;
       if (!userid_hint)
        userid_hint = "[User ID hint missing]";
       if (!passphrase_info)
        passphrase_info = "[passphrase info missing]";
       buf = malloc (20 + strlen (userid_hint)
-                       + strlen (passphrase_info) + 3);
+                   + strlen (passphrase_info) + 3);
       if (!buf)
        return GPGME_Out_Of_Core;
       sprintf (buf, "%s\n%s\n%s",
               bad_passphrase ? "TRY_AGAIN":"ENTER",
               userid_hint, passphrase_info);
 
-      err  = ctx->passphrase_cb (ctx->passphrase_cb_value, buf,
-                                &result->last_pw_handle, result_r);
+      err = ctx->passphrase_cb (ctx->passphrase_cb_value, buf,
+                               &opd->last_pw_handle, result);
       free (buf);
       return err;
     }
 
-  *result_r = NULL;
+  *result = NULL;
   return 0;
 }
-
-
-GpgmeError
-_gpgme_passphrase_start (GpgmeCtx ctx)
-{
-  GpgmeError err = 0;
-
-  if (ctx->passphrase_cb)
-    err = _gpgme_engine_set_command_handler (ctx->engine,
-                                            _gpgme_passphrase_command_handler,
-                                            ctx, NULL);
-  return err;
-}
index 68f6855..f416d10 100644 (file)
@@ -182,50 +182,39 @@ _gpgme_sign_status_handler (GpgmeCtx ctx, GpgmeStatusCode code, char *args)
 
 static GpgmeError
 _gpgme_op_sign_start (GpgmeCtx ctx, int synchronous,
-                     GpgmeData in, GpgmeData out,
+                     GpgmeData plain, GpgmeData sig,
                      GpgmeSigMode mode)
 {
-  GpgmeError err = 0;
+  GpgmeError err;
+
+  err = _gpgme_op_reset (ctx, synchronous);
+  if (err)
+    return err;
 
-  if (mode != GPGME_SIG_MODE_NORMAL
-      && mode != GPGME_SIG_MODE_DETACH
+  if (mode != GPGME_SIG_MODE_NORMAL && mode != GPGME_SIG_MODE_DETACH
       && mode != GPGME_SIG_MODE_CLEAR)
     return GPGME_Invalid_Value;
 
-  err = _gpgme_op_reset (ctx, synchronous);
-  if (err)
-    goto leave;
+  if (!plain)
+    return GPGME_No_Data;
+  if (!sig)
+    return GPGME_Invalid_Value;
 
-  /* Check the supplied data.  */
-  if (!in)
+  if (ctx->passphrase_cb)
     {
-      err = GPGME_No_Data;
-      goto leave;
+      err = _gpgme_engine_set_command_handler (ctx->engine,
+                                              _gpgme_passphrase_command_handler,
+                                              ctx, NULL);
+      if (err)
+       return err;
     }
-  if (!out)
-    {
-      err = GPGME_Invalid_Value;
-      goto leave;
-    }
-
-  err = _gpgme_passphrase_start (ctx);
-  if (err)
-    goto leave;
 
   _gpgme_engine_set_status_handler (ctx->engine, _gpgme_sign_status_handler,
                                    ctx);
 
-  err = _gpgme_engine_op_sign (ctx->engine, in, out, mode, ctx->use_armor,
-                              ctx->use_textmode, ctx->include_certs,
-                              ctx /* FIXME */);
-
- leave:
-  if (err)
-    {
-      _gpgme_engine_release (ctx->engine);
-      ctx->engine = NULL;
-    }
-  return err;
+  return _gpgme_engine_op_sign (ctx->engine, plain, sig, mode, ctx->use_armor,
+                               ctx->use_textmode, ctx->include_certs,
+                               ctx /* FIXME */);
 }
 
 GpgmeError