From b17e16ca5647e3b1f9d13d60a36d8f50dead20fc Mon Sep 17 00:00:00 2001 From: David Bremner Date: Sat, 12 Dec 2015 09:54:41 +2000 Subject: [PATCH] [Patch v2 2/8] crypto: make crypto ctx initialization an array --- 3f/dd48ad2e0c4c3e767dd0ace601dd014162cea7 | 110 ++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 3f/dd48ad2e0c4c3e767dd0ace601dd014162cea7 diff --git a/3f/dd48ad2e0c4c3e767dd0ace601dd014162cea7 b/3f/dd48ad2e0c4c3e767dd0ace601dd014162cea7 new file mode 100644 index 000000000..e237bd4d6 --- /dev/null +++ b/3f/dd48ad2e0c4c3e767dd0ace601dd014162cea7 @@ -0,0 +1,110 @@ +Return-Path: +X-Original-To: notmuch@notmuchmail.org +Delivered-To: notmuch@notmuchmail.org +Received: from localhost (localhost [127.0.0.1]) + by arlo.cworth.org (Postfix) with ESMTP id F3AE46DE17A0 + for ; Fri, 11 Dec 2015 05:55:07 -0800 (PST) +X-Virus-Scanned: Debian amavisd-new at cworth.org +X-Spam-Flag: NO +X-Spam-Score: -0.319 +X-Spam-Level: +X-Spam-Status: No, score=-0.319 tagged_above=-999 required=5 tests=[AWL=0.232, + RP_MATCHES_RCVD=-0.55, SPF_PASS=-0.001] autolearn=disabled +Received: from arlo.cworth.org ([127.0.0.1]) + by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024) + with ESMTP id g5cwXdcAEN1d for ; + Fri, 11 Dec 2015 05:55:06 -0800 (PST) +Received: from fethera.tethera.net (fethera.tethera.net [198.245.60.197]) + by arlo.cworth.org (Postfix) with ESMTPS id AC90B6DE00DD + for ; Fri, 11 Dec 2015 05:54:58 -0800 (PST) +Received: from remotemail by fethera.tethera.net with local (Exim 4.84) + (envelope-from ) + id 1a7O9u-0000Rs-DA; Fri, 11 Dec 2015 08:54:54 -0500 +Received: (nullmailer pid 11204 invoked by uid 1000); + Fri, 11 Dec 2015 13:54:52 -0000 +From: David Bremner +To: notmuch@notmuchmail.org +Subject: [Patch v2 2/8] crypto: make crypto ctx initialization an array +Date: Fri, 11 Dec 2015 09:54:41 -0400 +Message-Id: <1449842087-10972-3-git-send-email-david@tethera.net> +X-Mailer: git-send-email 2.6.2 +In-Reply-To: <1449842087-10972-1-git-send-email-david@tethera.net> +References: <1449842087-10972-1-git-send-email-david@tethera.net> +X-BeenThere: notmuch@notmuchmail.org +X-Mailman-Version: 2.1.20 +Precedence: list +List-Id: "Use and development of the notmuch mail system." + +List-Unsubscribe: , + +List-Archive: +List-Post: +List-Help: +List-Subscribe: , + +X-List-Received-Date: Fri, 11 Dec 2015 13:55:08 -0000 + +From: Jani Nikula + +Make it trivial to add handlers for new protocols without duplicating +code. No functional changes. +--- + crypto.c | 27 +++++++++++++++++++++------ + 1 file changed, 21 insertions(+), 6 deletions(-) + +diff --git a/crypto.c b/crypto.c +index 1187ad7..da0289d 100644 +--- a/crypto.c ++++ b/crypto.c +@@ -43,12 +43,27 @@ create_gpg_context (notmuch_crypto_t *crypto) + return gpgctx; + } + ++static const struct { ++ const char *protocol; ++ notmuch_crypto_context_t *(*get_context) (notmuch_crypto_t *crypto); ++} protocols[] = { ++ { ++ .protocol = "application/pgp-signature", ++ .get_context = create_gpg_context, ++ }, ++ { ++ .protocol = "application/pgp-encrypted", ++ .get_context = create_gpg_context, ++ }, ++}; ++ + /* for the specified protocol return the context pointer (initializing + * if needed) */ + notmuch_crypto_context_t * + notmuch_crypto_get_context (notmuch_crypto_t *crypto, const char *protocol) + { + notmuch_crypto_context_t *cryptoctx = NULL; ++ size_t i; + + if (! protocol) { + fprintf (stderr, "Cryptographic protocol is empty.\n"); +@@ -61,14 +76,14 @@ notmuch_crypto_get_context (notmuch_crypto_t *crypto, const char *protocol) + * parameter names as defined in this document are + * case-insensitive." Thus, we use strcasecmp for the protocol. + */ +- if (strcasecmp (protocol, "application/pgp-signature") == 0 || +- strcasecmp (protocol, "application/pgp-encrypted") == 0) { +- cryptoctx = create_gpg_context (crypto); +- } else { +- fprintf (stderr, "Unknown or unsupported cryptographic protocol.\n"); ++ for (i = 0; i < ARRAY_SIZE (protocols); i++) { ++ if (strcasecmp (protocol, protocols[i].protocol) == 0) ++ return protocols[i].get_context (crypto); + } + +- return cryptoctx; ++ fprintf (stderr, "Unknown or unsupported cryptographic protocol.\n"); ++ ++ return NULL; + } + + int +-- +2.6.2 + -- 2.26.2