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 2E15B6DE15A1 for ; Mon, 14 Dec 2015 05:39:35 -0800 (PST) X-Virus-Scanned: Debian amavisd-new at cworth.org X-Spam-Flag: NO X-Spam-Score: -0.316 X-Spam-Level: X-Spam-Status: No, score=-0.316 tagged_above=-999 required=5 tests=[AWL=0.235, 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 vn_lFs7QCbiH for ; Mon, 14 Dec 2015 05:39:33 -0800 (PST) Received: from fethera.tethera.net (fethera.tethera.net [198.245.60.197]) by arlo.cworth.org (Postfix) with ESMTPS id 8F8DE6DE1613 for ; Mon, 14 Dec 2015 05:39:14 -0800 (PST) Received: from remotemail by fethera.tethera.net with local (Exim 4.84) (envelope-from ) id 1a8TLJ-0008FP-ML; Mon, 14 Dec 2015 08:39:09 -0500 Received: (nullmailer pid 31796 invoked by uid 1000); Mon, 14 Dec 2015 13:39:04 -0000 From: David Bremner To: notmuch@notmuchmail.org Subject: [Patch v3 2/8] crypto: make crypto ctx initialization an array Date: Mon, 14 Dec 2015 09:38:51 -0400 Message-Id: <1450100337-31655-3-git-send-email-david@tethera.net> X-Mailer: git-send-email 2.6.2 In-Reply-To: <1450100337-31655-1-git-send-email-david@tethera.net> References: <1450100337-31655-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: Mon, 14 Dec 2015 13:39:35 -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