diff options
author | Werner Koch <[email protected]> | 2008-12-05 16:31:39 +0000 |
---|---|---|
committer | Werner Koch <[email protected]> | 2008-12-05 16:31:39 +0000 |
commit | 5bc9948f699b70c76dc0c7c406817d077b61317d (patch) | |
tree | 2c59e77471cbf6f02fea58466c4b5544a9b74cad /common | |
parent | Add option --card-timeout. (diff) | |
download | gnupg-5bc9948f699b70c76dc0c7c406817d077b61317d.tar.gz gnupg-5bc9948f699b70c76dc0c7c406817d077b61317d.zip |
Add a custom prompt for the CSR generation.
Add a new percent escape fucntion.
Diffstat (limited to 'common')
-rw-r--r-- | common/ChangeLog | 2 | ||||
-rw-r--r-- | common/Makefile.am | 4 | ||||
-rw-r--r-- | common/percent.c | 76 | ||||
-rw-r--r-- | common/t-percent.c | 97 | ||||
-rw-r--r-- | common/util.h | 3 |
5 files changed, 181 insertions, 1 deletions
diff --git a/common/ChangeLog b/common/ChangeLog index b1390d599..4dd772d7e 100644 --- a/common/ChangeLog +++ b/common/ChangeLog @@ -1,5 +1,7 @@ 2008-12-05 Werner Koch <[email protected]> + * percent.c, t-percent.c: New. + * exechelp.c (gnupg_spawn_process, gnupg_spawn_process_fd) (gnupg_spawn_process_detached) [W32]: Remove debug output. diff --git a/common/Makefile.am b/common/Makefile.am index 5a5d6ba71..d1a2d4822 100644 --- a/common/Makefile.am +++ b/common/Makefile.am @@ -52,6 +52,7 @@ common_sources = \ yesno.c \ b64enc.c b64dec.c \ convert.c \ + percent.c \ miscellaneous.c \ xasprintf.c \ xreadline.c \ @@ -107,13 +108,14 @@ status-codes.h: Makefile mkstrtable.awk exstatus.awk status.h # # Module tests # -module_tests = t-convert t-gettime t-sysutils t-sexputil +module_tests = t-convert t-percent t-gettime t-sysutils t-sexputil module_maint_tests = t-helpfile t-b64 t_common_ldadd = libcommon.a ../jnlib/libjnlib.a ../gl/libgnu.a \ $(LIBGCRYPT_LIBS) $(GPG_ERROR_LIBS) $(LIBINTL) $(LIBICONV) t_convert_LDADD = $(t_common_ldadd) +t_percent_LDADD = $(t_common_ldadd) t_gettime_LDADD = $(t_common_ldadd) t_sysutils_LDADD = $(t_common_ldadd) t_helpfile_LDADD = $(t_common_ldadd) diff --git a/common/percent.c b/common/percent.c new file mode 100644 index 000000000..a0c78ec7b --- /dev/null +++ b/common/percent.c @@ -0,0 +1,76 @@ +/* percent.c - Percent escaping + * Copyright (C) 2008 Free Software Foundation, Inc. + * + * 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 <stdlib.h> +#include <errno.h> +#include <ctype.h> + +#include "util.h" + + +/* Create a newly alloced string from STRING with all spaces and + control characters converted to plus signs or %xx sequences. The + function returns the new string or NULL in case of a malloc + failure. + + Note that we also escape the quote character to work around a bug + in the mingw32 runtime which does not correcty handle command line + quoting. We correctly double the quote mark when calling a program + (i.e. gpg-protect-tool), but the pre-main code does not notice the + double quote as an escaped quote. We do this also on POSIX systems + for consistency. */ +char * +percent_plus_escape (const char *string) +{ + char *buffer, *p; + const char *s; + size_t length; + + for (length=1, s=string; *s; s++) + { + if (*s == '+' || *s == '\"' || *s == '%' + || *(const unsigned char *)s < 0x20) + length += 3; + else + length++; + } + + buffer = p = xtrymalloc (length); + if (!buffer) + return NULL; + + for (s=string; *s; s++) + { + if (*s == '+' || *s == '\"' || *s == '%' + || *(const unsigned char *)s < 0x20) + { + snprintf (p, 4, "%%%02X", *(unsigned char *)s); + p += 3; + } + else if (*s == ' ') + *p++ = '+'; + else + *p++ = *s; + } + *p = 0; + + return buffer; + +} diff --git a/common/t-percent.c b/common/t-percent.c new file mode 100644 index 000000000..93d95f78d --- /dev/null +++ b/common/t-percent.c @@ -0,0 +1,97 @@ +/* t-percent.c - Module test for percent.c + * Copyright (C) 2008 Free Software Foundation, Inc. + * + * 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 <assert.h> + +#include "util.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 +test_percent_plus_escape (void) +{ + static struct { + const char *string; + const char *expect; + } tbl[] = { + { + "", + "" + }, { + "a", + "a", + }, { + " ", + "+", + }, { + " ", + "++" + }, { + "+ +", + "%2B+%2B" + }, { + "\" \"", + "%22+%22" + }, { + "%22", + "%2522" + }, { + "%% ", + "%25%25+" + }, { + "\n ABC\t", + "%0A+ABC%09" + }, { NULL, NULL } + }; + char *buf; + int i; + + for (i=0; tbl[i].string; i++) + { + buf = percent_plus_escape (tbl[i].string); + if (!buf) + { + fprintf (stderr, "out of core: %s\n", strerror (errno)); + exit (2); + } + if (strcmp (buf, tbl[i].expect)) + fail (i); + xfree (buf); + } +} + + +int +main (int argc, char **argv) +{ + (void)argc; + (void)argv; + + test_percent_plus_escape (); + + return 0; +} + diff --git a/common/util.h b/common/util.h index 29955a494..66569e27e 100644 --- a/common/util.h +++ b/common/util.h @@ -199,6 +199,9 @@ const char *hex2str (const char *hexstring, char *buffer, size_t bufsize, size_t *buflen); char *hex2str_alloc (const char *hexstring, size_t *r_count); +/*-- percent.c --*/ +char *percent_plus_escape (const char *string); + /*-- homedir.c --*/ const char *standard_homedir (void); |