[git] GnuPG - branch, master, updated. gnupg-2.1.12-26-g91bc783
by Werner Koch
cvs at cvs.gnupg.org
Tue May 24 16:08:31 CEST 2016
This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "The GNU Privacy Guard".
The branch, master has been updated
via 91bc7833836f19256d56984c94cacf44853ff5c8 (commit)
via 2421f7f7ed74ed20372efd63a2efd58d3b55005c (commit)
from f7426b73ce3176f2bd3ea02120be1c70d145542e (commit)
Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.
- Log -----------------------------------------------------------------
commit 91bc7833836f19256d56984c94cacf44853ff5c8
Author: Werner Koch <wk at gnupg.org>
Date: Tue May 24 15:54:48 2016 +0200
gpgtar: Simplify code by using ccparray.
* tools/gpgtar-create.c (gpgtar_create): Use ccparray functions.
* tools/gpgtar-extract.c (gpgtar_extract): Ditto.
* tools/gpgtar-list.c (gpgtar_list): Ditto.
Signed-off-by: Werner Koch <wk at gnupg.org>
diff --git a/tools/gpgtar-create.c b/tools/gpgtar-create.c
index 968dca6..f7c8b1a 100644
--- a/tools/gpgtar-create.c
+++ b/tools/gpgtar-create.c
@@ -38,6 +38,7 @@
#include "i18n.h"
#include "../common/exectool.h"
#include "../common/sysutils.h"
+#include "../common/ccparray.h"
#include "gpgtar.h"
#ifndef HAVE_LSTAT
@@ -888,8 +889,8 @@ gpgtar_create (char **inpattern, int encrypt, int sign)
if (encrypt || sign)
{
- int i;
strlist_t arg;
+ ccparray_t ccp;
const char **argv;
err = es_fseek (outstream, 0, SEEK_SET);
@@ -899,43 +900,36 @@ gpgtar_create (char **inpattern, int encrypt, int sign)
/* '--encrypt' may be combined with '--symmetric', but 'encrypt'
is set either way. Clear it if no recipients are specified.
XXX: Fix command handling. */
- if (opt.symmetric && opt.recipients == NULL)
- encrypt = 0;
-
- argv = xtrycalloc (strlist_length (opt.gpg_arguments)
- + 2 * strlist_length (opt.recipients)
- + 1 + !!encrypt + !!sign + 2 * !!opt.user
- + !!opt.symmetric,
- sizeof *argv);
- if (argv == NULL)
- {
- err = gpg_error_from_syserror ();
- goto leave;
- }
- i = 0;
+ if (opt.symmetric && opt.recipients == NULL)
+ encrypt = 0;
+
+ ccparray_init (&ccp, 0);
if (encrypt)
- argv[i++] = "--encrypt";
+ ccparray_put (&ccp, "--encrypt");
if (sign)
- argv[i++] = "--sign";
+ ccparray_put (&ccp, "--sign");
if (opt.user)
{
- argv[i++] = "--local-user";
- argv[i++] = opt.user;
+ ccparray_put (&ccp, "--local-user");
+ ccparray_put (&ccp, opt.user);
}
if (opt.symmetric)
- argv[i++] = "--symmetric";
+ ccparray_put (&ccp, "--symmetric");
for (arg = opt.recipients; arg; arg = arg->next)
{
- argv[i++] = "--recipient";
- argv[i++] = arg->d;
+ ccparray_put (&ccp, "--recipient");
+ ccparray_put (&ccp, arg->d);
}
for (arg = opt.gpg_arguments; arg; arg = arg->next)
- argv[i++] = arg->d;
- argv[i++] = NULL;
- assert (i == strlist_length (opt.gpg_arguments)
- + 2 * strlist_length (opt.recipients)
- + 1 + !!encrypt + !!sign + 2 * !!opt.user
- + !!opt.symmetric);
+ ccparray_put (&ccp, arg->d);
+
+ ccparray_put (&ccp, NULL);
+ argv = ccparray_get (&ccp, NULL);
+ if (!argv)
+ {
+ err = gpg_error_from_syserror ();
+ goto leave;
+ }
err = gnupg_exec_tool_stream (opt.gpg_program, argv,
outstream, cipher_stream);
diff --git a/tools/gpgtar-extract.c b/tools/gpgtar-extract.c
index 95f2b56..3ee73ef 100644
--- a/tools/gpgtar-extract.c
+++ b/tools/gpgtar-extract.c
@@ -30,6 +30,7 @@
#include "i18n.h"
#include "../common/exectool.h"
#include "../common/sysutils.h"
+#include "../common/ccparray.h"
#include "gpgtar.h"
@@ -299,8 +300,8 @@ gpgtar_extract (const char *filename, int decrypt)
if (decrypt)
{
- int i;
strlist_t arg;
+ ccparray_t ccp;
const char **argv;
cipher_stream = stream;
@@ -311,19 +312,19 @@ gpgtar_extract (const char *filename, int decrypt)
goto leave;
}
- argv = xtrycalloc (strlist_length (opt.gpg_arguments) + 2,
- sizeof *argv);
- if (argv == NULL)
+ ccparray_init (&ccp, 0);
+
+ ccparray_put (&ccp, "--decrypt");
+ for (arg = opt.gpg_arguments; arg; arg = arg->next)
+ ccparray_put (&ccp, arg->d);
+
+ ccparray_put (&ccp, NULL);
+ argv = ccparray_get (&ccp, NULL);
+ if (!argv)
{
err = gpg_error_from_syserror ();
goto leave;
}
- i = 0;
- argv[i++] = "--decrypt";
- for (arg = opt.gpg_arguments; arg; arg = arg->next)
- argv[i++] = arg->d;
- argv[i++] = NULL;
- assert (i == strlist_length (opt.gpg_arguments) + 2);
err = gnupg_exec_tool_stream (opt.gpg_program, argv,
cipher_stream, stream);
diff --git a/tools/gpgtar-list.c b/tools/gpgtar-list.c
index 25c70d2..930712a 100644
--- a/tools/gpgtar-list.c
+++ b/tools/gpgtar-list.c
@@ -27,6 +27,7 @@
#include "i18n.h"
#include "gpgtar.h"
#include "../common/exectool.h"
+#include "../common/ccparray.h"
@@ -299,8 +300,8 @@ gpgtar_list (const char *filename, int decrypt)
if (decrypt)
{
- int i;
strlist_t arg;
+ ccparray_t ccp;
const char **argv;
cipher_stream = stream;
@@ -311,19 +312,19 @@ gpgtar_list (const char *filename, int decrypt)
goto leave;
}
- argv = xtrycalloc (strlist_length (opt.gpg_arguments) + 2,
- sizeof *argv);
- if (argv == NULL)
+ ccparray_init (&ccp, 0);
+
+ ccparray_put (&ccp, "--decrypt");
+ for (arg = opt.gpg_arguments; arg; arg = arg->next)
+ ccparray_put (&ccp, arg->d);
+
+ ccparray_put (&ccp, NULL);
+ argv = ccparray_get (&ccp, NULL);
+ if (!argv)
{
err = gpg_error_from_syserror ();
goto leave;
}
- i = 0;
- argv[i++] = "--decrypt";
- for (arg = opt.gpg_arguments; arg; arg = arg->next)
- argv[i++] = arg->d;
- argv[i++] = NULL;
- assert (i == strlist_length (opt.gpg_arguments) + 2);
err = gnupg_exec_tool_stream (opt.gpg_program, argv,
cipher_stream, stream);
commit 2421f7f7ed74ed20372efd63a2efd58d3b55005c
Author: Werner Koch <wk at gnupg.org>
Date: Tue May 24 15:43:16 2016 +0200
common: Add simple dynamic array function.
* common/ccparray.c: New.
* common/ccparray.h: New.
* common/t-ccparray.c: New.
* common/Makefile.am (common_sources): Add files.
(module_tests): Add test file.
(t_ccparray_LDADD): New.
Signed-off-by: Werner Koch <wk at gnupg.org>
diff --git a/common/Makefile.am b/common/Makefile.am
index 4a35f64..884c966 100644
--- a/common/Makefile.am
+++ b/common/Makefile.am
@@ -72,6 +72,7 @@ common_sources = \
xasprintf.c \
xreadline.c \
membuf.c membuf.h \
+ ccparray.c ccparray.h \
iobuf.c iobuf.h \
ttyio.c ttyio.h \
asshelp.c asshelp2.c asshelp.h \
@@ -156,7 +157,7 @@ module_tests = t-stringhelp t-timestuff \
t-convert t-percent t-gettime t-sysutils t-sexputil \
t-session-env t-openpgp-oid t-ssh-utils \
t-mapstrings t-zb32 t-mbox-util t-iobuf t-strlist \
- t-private-keys
+ t-private-keys t-ccparray
if !HAVE_W32CE_SYSTEM
module_tests += t-exechelp
endif
@@ -206,6 +207,7 @@ t_mbox_util_LDADD = $(t_common_ldadd)
t_iobuf_LDADD = $(t_common_ldadd)
t_strlist_LDADD = $(t_common_ldadd)
t_private_keys_LDADD = $(t_common_ldadd)
+t_ccparray_LDADD = $(t_common_ldadd)
# System specific test
if HAVE_W32_SYSTEM
diff --git a/common/ccparray.c b/common/ccparray.c
new file mode 100644
index 0000000..490dbf5
--- /dev/null
+++ b/common/ccparray.c
@@ -0,0 +1,147 @@
+/* ccparray.c - A simple dynamic array for character pointer.
+ * Copyright (C) 2016 g10 Code GmbH
+ *
+ * This file is part of GnuPG.
+ *
+ * This file is free software; you can redistribute it and/or modify
+ * it under the terms of either
+ *
+ * - the GNU Lesser General Public License as published by the Free
+ * Software Foundation; either version 3 of the License, or (at
+ * your option) any later version.
+ *
+ * or
+ *
+ * - the GNU General Public License as published by the Free
+ * Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * or both in parallel, as here.
+ *
+ * This file is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <config.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <stdarg.h>
+
+#include "util.h"
+#include "ccparray.h"
+
+
+/* A simple implementation of a dynamic array of const char pointers.
+ * The example code:
+ *
+ * ccparray_t ccp;
+ * const char **argv;
+ * int i;
+ *
+ * ccparray_init (&ccp, 0);
+ * ccparray_put (&ccp, "First arg");
+ * ccparray_put (&ccp, "Second arg");
+ * ccparray_put (&ccp, NULL);
+ * ccparray_put (&ccp, "Fourth arg");
+ * argv = ccparray_get (&ccp, NULL);
+ * if (!argv)
+ * die ("error building array: %s\n", strerror (errno));
+ * for (i=0; argv[i]; i++)
+ * printf ("[%d] = '%s'\n", i, argv[i]);
+ * xfree (argv);
+ *
+ * will result in this output:
+ *
+ * [0] = 'First arg'
+ * [1] = 'Second arg'
+ *
+ * Note that allocation errors are detected but only returned with the
+ * final ccparray_get(); this helps not to clutter the code with out
+ * of core checks.
+ */
+
+void
+ccparray_init (ccparray_t *cpa, unsigned int initialsize)
+{
+ if (!initialsize)
+ cpa->size = 16;
+ else if (initialsize < (1<<16))
+ cpa->size = initialsize;
+ else
+ cpa->size = (1<<16);
+
+ cpa->count = 0;
+ cpa->out_of_core = 0;
+ cpa->array = xtrycalloc (cpa->size, sizeof *cpa->array);
+ if (!cpa->array)
+ cpa->out_of_core = errno;
+}
+
+
+void
+ccparray_put (ccparray_t *cpa, const char *value)
+{
+ if (cpa->out_of_core)
+ return;
+
+ if (cpa->count + 1 >= cpa->size)
+ {
+ const char **newarray;
+ size_t n, newsize;
+
+ if (cpa->size < 8)
+ newsize = 16;
+ else if (cpa->size < 4096)
+ newsize = 2 * cpa->size;
+ else if (cpa->size < (1<<16))
+ newsize = cpa->size + 2048;
+ else
+ {
+ cpa->out_of_core = ENOMEM;
+ return;
+ }
+
+ newarray = xtrycalloc (newsize, sizeof *newarray);
+ if (!newarray)
+ {
+ cpa->out_of_core = errno ? errno : ENOMEM;
+ return;
+ }
+ for (n=0; n < cpa->size; n++)
+ newarray[n] = cpa->array[n];
+ cpa->array = newarray;
+ cpa->size = newsize;
+
+ }
+ cpa->array[cpa->count++] = value;
+}
+
+
+const char **
+ccparray_get (ccparray_t *cpa, size_t *r_count)
+{
+ const char **result;
+
+ if (cpa->out_of_core)
+ {
+ if (cpa->array)
+ {
+ xfree (cpa->array);
+ cpa->array = NULL;
+ }
+ gpg_err_set_errno (cpa->out_of_core);
+ return NULL;
+ }
+
+ result= cpa->array;
+ if (r_count)
+ *r_count = cpa->count;
+ cpa->array = NULL;
+ cpa->out_of_core = ENOMEM; /* hack to make sure it won't get reused. */
+ return result;
+}
diff --git a/common/ccparray.h b/common/ccparray.h
new file mode 100644
index 0000000..241d42d
--- /dev/null
+++ b/common/ccparray.h
@@ -0,0 +1,51 @@
+/* ccparray.c - A simple dynamic array for character pointer.
+ * Copyright (C) 2016 g10 Code GmbH
+ *
+ * This file is part of GnuPG.
+ *
+ * This file is free software; you can redistribute it and/or modify
+ * it under the terms of either
+ *
+ * - the GNU Lesser General Public License as published by the Free
+ * Software Foundation; either version 3 of the License, or (at
+ * your option) any later version.
+ *
+ * or
+ *
+ * - the GNU General Public License as published by the Free
+ * Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * or both in parallel, as here.
+ *
+ * This file is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef GNUPG_COMMON_CCPARRAY_H
+#define GNUPG_COMMON_CCPARRAY_H
+
+/* The definition of the structure is private, we only need it here,
+ * so it can be allocated on the stack. */
+struct _ccparray_private_s
+{
+ unsigned int count;
+ unsigned int size;
+ int out_of_core;
+ const char **array;
+};
+
+typedef struct _ccparray_private_s ccparray_t;
+
+
+void ccparray_init (ccparray_t *cpa, unsigned int initialsize);
+void ccparray_put (ccparray_t *cpa, const char *value);
+const char **ccparray_get (ccparray_t *cpa, size_t *r_nelems);
+
+
+#endif /*GNUPG_COMMON_CCPARRAY_H*/
diff --git a/common/t-ccparray.c b/common/t-ccparray.c
new file mode 100644
index 0000000..0512346
--- /dev/null
+++ b/common/t-ccparray.c
@@ -0,0 +1,93 @@
+/* t-ccparray.c - Module test for ccparray.c
+ * Copyright (C) 2016 g10 Code GmbH
+ *
+ * This file is part of GnuPG.
+ *
+ * GnuPG is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * GnuPG is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <config.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "util.h"
+#include "ccparray.h"
+
+#define pass() do { ; } while(0)
+#define fail(a) do { fprintf (stderr, "%s:%d: test %d failed\n",\
+ __FILE__,__LINE__, (a)); \
+ exit (1); \
+ } while(0)
+
+
+static void
+run_test_1 (void)
+{
+ ccparray_t ccp;
+ const char **argv;
+ size_t nelem;
+
+ ccparray_init (&ccp, 0);
+ ccparray_put (&ccp, "First arg");
+ ccparray_put (&ccp, "Second arg");
+ ccparray_put (&ccp, NULL);
+ ccparray_put (&ccp, "Fourth arg");
+ argv = ccparray_get (&ccp, &nelem);
+ if (!argv)
+ {
+ fprintf (stderr, "error building array: %s\n", strerror (errno));
+ exit (1);
+ }
+
+ if (nelem != 4)
+ fail (1);
+
+ /* for (i=0; argv[i]; i++) */
+ /* printf ("[%d] = '%s'\n", i, argv[i]); */
+ xfree (argv);
+}
+
+
+static void
+run_test_var (int count)
+{
+ ccparray_t ccp;
+ size_t nelem;
+ int i;
+
+ ccparray_init (&ccp, 0);
+ for (i=0; i < count; i++)
+ ccparray_put (&ccp, "An arg");
+ xfree (ccparray_get (&ccp, &nelem));
+ if (nelem != i)
+ fail (2);
+}
+
+
+int
+main (int argc, char **argv)
+{
+ (void)argc;
+ (void)argv;
+
+ run_test_1 ();
+ run_test_var (0);
+ run_test_var (7);
+ run_test_var (8);
+ run_test_var (9);
+ run_test_var (4096);
+
+ return 0;
+}
-----------------------------------------------------------------------
Summary of changes:
common/Makefile.am | 4 +-
common/ccparray.c | 147 +++++++++++++++++++++++++++++++++++++
common/{ssh-utils.h => ccparray.h} | 26 +++++--
common/t-ccparray.c | 93 +++++++++++++++++++++++
tools/gpgtar-create.c | 50 ++++++-------
tools/gpgtar-extract.c | 21 +++---
tools/gpgtar-list.c | 21 +++---
7 files changed, 306 insertions(+), 56 deletions(-)
create mode 100644 common/ccparray.c
copy common/{ssh-utils.h => ccparray.h} (58%)
create mode 100644 common/t-ccparray.c
hooks/post-receive
--
The GNU Privacy Guard
http://git.gnupg.org
More information about the Gnupg-commits
mailing list