Updated estream.

More changes for Windows.
This commit is contained in:
Werner Koch 2007-08-22 10:55:07 +00:00
parent a5743d1017
commit f81f521a72
23 changed files with 508 additions and 281 deletions

View File

@ -33,7 +33,7 @@ Release process:
* Run configure as usual.
* Run "make distcheck"
* Build and test the new tarball (best on a different machine).
* [1.4 only] Build and test the W32 version.
* Build and test the W32 version.
* Using the final test build run a "make -C doc online".
* Sign the tarball
* Get the previous tarball and run "mkdiff gnupg".

View File

@ -1,3 +1,8 @@
2007-08-22 Werner Koch <wk@g10code.com>
* protect-tool.c (import_p12_file): Add hack to allow importing of
gnupg 2.0.4 generated files.
2007-08-06 Werner Koch <wk@g10code.com>
* trustlist.c (read_one_trustfile): Add flag "cm".

View File

@ -638,7 +638,7 @@ import_p12_file (const char *fname)
{
char *buf;
unsigned char *result;
size_t buflen, resultlen;
size_t buflen, resultlen, buf_off;
int i;
int rc;
gcry_mpi_t *kparms;
@ -654,7 +654,22 @@ import_p12_file (const char *fname)
if (!buf)
return;
kparms = p12_parse ((unsigned char*)buf, buflen, (pw=get_passphrase (2, 0)),
/* GnuPG 2.0.4 accidently created binary P12 files with the string
"The passphrase is %s encoded.\n\n" prepended to the ASN.1 data.
We fix that here. */
if (buflen > 29 && !memcmp (buf, "The passphrase is ", 18))
{
for (buf_off=18; buf_off < buflen && buf[buf_off] != '\n'; buf_off++)
;
buf_off++;
if (buf_off < buflen && buf[buf_off] == '\n')
buf_off++;
}
else
buf_off = 0;
kparms = p12_parse ((unsigned char*)buf+buf_off, buflen-buf_off,
(pw=get_passphrase (2, 0)),
import_p12_cert_cb, NULL);
release_passphrase (pw);
xfree (buf);

View File

@ -1,3 +1,32 @@
2007-08-22 Werner Koch <wk@g10code.com>
Updated estream from libestream.
* estream.c (mem_malloc, mem_realloc, mem_free): New. Use them
instead of the ES_MEM_foo.
* estream.c (estream_cookie_mem): Remove members DONT_FREE,
APPEND_ZERO, PTR and SIZE. Add MEMORY_LIMIT. Put GROW into a new
FLAGS struct.
(es_func_mem_create): Remove APPEND_ZERO, DONT_FREE, PTR and
SIZE. Add MEMORY_LIMIT.
(es_func_mem_write, es_func_mem_seek, es_func_mem_destroy): Revamp.
(es_open_memstream): Change API to just take a memory limit and a
mode argument. Rename to ..
(es_fopenmem): .. this.
(HAVE_W32_SYSTEM) [_WIN32]: Define if not defined.
(tmpfd) [W32]: Implement directly using the W32 API.
(es_fgets): Rewrite without using doreadline.
2007-08-21 Werner Koch <wk@g10code.com>
* sysutils.c (gnupg_tmpfile): New.
* t-sysutils.c: New.
* Makefile.am (module_tests): Add t-sysutils.
2007-08-20 Werner Koch <wk@g10code.com>
* exechelp.c [W32]: Redefine X_OK to F_OK.
2007-08-16 Werner Koch <wk@g10code.com>
* Makefile.am (t_convert_DEPENDENCIES): Remove

View File

@ -83,12 +83,12 @@ libgpgrl_a_SOURCES = \
#
# Module tests
#
module_tests = t-convert t-gettime
module_tests = t-convert t-gettime t-sysutils
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_gettime_LDADD = $(t_common_ldadd)
t_sysutils_LDADD = $(t_common_ldadd)

View File

@ -5,7 +5,7 @@
*
* Libestream 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,
* by the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
*
* Libestream is distributed in the hope that it will be useful, but
@ -15,8 +15,6 @@
*
* You should have received a copy of the GNU General Public License
* along with Libestream; if not, see <http://www.gnu.org/licenses/>.
*
* $Id: estream-printf.c 56 2007-05-15 18:38:43Z wk $
*/
/* Required autoconf tests:

View File

@ -5,7 +5,7 @@
*
* Libestream 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,
* by the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
*
* Libestream is distributed in the hope that it will be useful, but
@ -15,8 +15,6 @@
*
* You should have received a copy of the GNU General Public License
* along with Libestream; if not, see <http://www.gnu.org/licenses/>.
*
* $Id: estream-printf.h 56 2007-05-15 18:38:43Z wk $
*/
#ifndef ESTREAM_PRINTF_H

View File

@ -5,7 +5,7 @@
*
* Libestream 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,
* by the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
*
* Libestream is distributed in the hope that it will be useful, but
@ -25,6 +25,10 @@
# include <config.h>
#endif
#if defined(_WIN32) && !defined(HAVE_W32_SYSTEM)
# define HAVE_W32_SYSTEM 1
#endif
#include <sys/types.h>
#include <sys/file.h>
#include <sys/stat.h>
@ -37,10 +41,13 @@
#include <errno.h>
#include <stddef.h>
#include <assert.h>
#ifdef HAVE_W32_SYSTEM
# include <windows.h>
#endif
#ifdef WITHOUT_GNU_PTH /* Give the Makefile a chance to build without Pth. */
#undef HAVE_PTH
#undef USE_GNU_PTH
# undef HAVE_PTH
# undef USE_GNU_PTH
#endif
#ifdef HAVE_PTH
@ -49,7 +56,7 @@
/* This is for the special hack to use estream.c in GnuPG. */
#ifdef GNUPG_MAJOR_VERSION
#include "../common/util.h"
# include "../common/util.h"
#endif
#ifndef HAVE_MKSTEMP
@ -74,12 +81,6 @@ void *memrchr (const void *block, int c, size_t size);
typedef void *(*func_realloc_t) (void *mem, size_t size);
typedef void (*func_free_t) (void *mem);
#ifdef HAVE_FOPENCOOKIE
typedef ssize_t my_funopen_hook_ret_t;
#else
typedef int my_funopen_hook_ret_t;
#endif
@ -93,7 +94,6 @@ typedef int my_funopen_hook_ret_t;
/* Macros. */
#define BUFFER_ROUND_TO_BLOCK(size, block_size) \
(((size) + (block_size - 1)) / block_size)
@ -121,12 +121,6 @@ typedef void *estream_mutex_t;
# define ESTREAM_MUTEX_INITIALIZE(mutex) (void) 0
#endif
/* Memory allocator functions. */
#define ES_MEM_ALLOC malloc
#define ES_MEM_REALLOC realloc
#define ES_MEM_FREE free
/* Primitive system I/O. */
#ifdef HAVE_PTH
@ -212,6 +206,9 @@ static estream_mutex_t estream_list_lock;
#define DIM(array) (sizeof (array) / sizeof (*array))
#endif
#define tohex(n) ((n) < 10 ? ((n) + '0') : (((n) - 10) + 'A'))
/* Evaluate EXPRESSION, setting VARIABLE to the return code, if
VARIABLE is zero. */
#define SET_UNLESS_NONZERO(variable, tmp_variable, expression) \
@ -223,6 +220,33 @@ static estream_mutex_t estream_list_lock;
} \
while (0)
/* Malloc wrappers to overcvome problems on some older OSes. */
static void *
mem_alloc (size_t n)
{
if (!n)
n++;
return malloc (n);
}
static void *
mem_realloc (void *p, size_t n)
{
if (!p)
return mem_alloc (n);
return realloc (p, n);
}
static void
mem_free (void *p)
{
if (p)
free (p);
}
/*
* List manipulation.
*/
@ -234,7 +258,7 @@ es_list_add (estream_t stream)
estream_list_t list_obj;
int ret;
list_obj = ES_MEM_ALLOC (sizeof (*list_obj));
list_obj = mem_alloc (sizeof (*list_obj));
if (! list_obj)
ret = -1;
else
@ -266,7 +290,7 @@ es_list_remove (estream_t stream)
*list_obj->prev_cdr = list_obj->cdr;
if (list_obj->cdr)
list_obj->cdr->prev_cdr = list_obj->prev_cdr;
ES_MEM_FREE (list_obj);
mem_free (list_obj);
break;
}
ESTREAM_LIST_UNLOCK;
@ -326,53 +350,49 @@ es_init_do (void)
typedef struct estream_cookie_mem
{
unsigned int modeflags; /* Open flags. */
unsigned char *memory; /* Data. */
size_t memory_size; /* Size of MEMORY. */
unsigned char *memory; /* Allocated data buffer. */
size_t memory_size; /* Allocated size of memory. */
size_t memory_limit; /* Maximum allowed allocation size or
0 for no limit. */
size_t offset; /* Current offset in MEMORY. */
size_t data_len; /* Length of data in MEMORY. */
size_t block_size; /* Block size. */
unsigned int grow: 1; /* MEMORY is allowed to grow. */
unsigned int append_zero: 1; /* Append zero after data. */
unsigned int dont_free: 1; /* Append zero after data. */
char **ptr;
size_t *size;
struct {
unsigned int grow: 1; /* MEMORY is allowed to grow. */
} flags;
func_realloc_t func_realloc;
func_free_t func_free;
} *estream_cookie_mem_t;
/* Create function for memory objects. */
static int
es_func_mem_create (void *ES__RESTRICT *ES__RESTRICT cookie,
unsigned char *ES__RESTRICT data, size_t data_n,
size_t data_len,
size_t block_size, unsigned int grow,
unsigned int append_zero, unsigned int dont_free,
char **ptr, size_t *size,
func_realloc_t func_realloc, func_free_t func_free,
unsigned int modeflags)
unsigned int modeflags,
size_t memory_limit)
{
estream_cookie_mem_t mem_cookie;
int err;
mem_cookie = ES_MEM_ALLOC (sizeof (*mem_cookie));
if (! mem_cookie)
mem_cookie = mem_alloc (sizeof (*mem_cookie));
if (!mem_cookie)
err = -1;
else
{
mem_cookie->modeflags = modeflags;
mem_cookie->memory = data;
mem_cookie->memory_size = data_n;
mem_cookie->memory_limit = memory_limit;
mem_cookie->offset = 0;
mem_cookie->data_len = data_len;
mem_cookie->block_size = block_size;
mem_cookie->grow = grow ? 1 : 0;
mem_cookie->append_zero = append_zero ? 1 : 0;
mem_cookie->dont_free = dont_free ? 1 : 0;
mem_cookie->ptr = ptr;
mem_cookie->size = size;
mem_cookie->func_realloc = func_realloc ? func_realloc : ES_MEM_REALLOC;
mem_cookie->func_free = func_free ? func_free : ES_MEM_FREE;
mem_cookie->offset = 0;
mem_cookie->flags.grow = !!grow;
mem_cookie->func_realloc = func_realloc ? func_realloc : mem_realloc;
mem_cookie->func_free = func_free ? func_free : mem_free;
*cookie = mem_cookie;
err = 0;
}
@ -380,6 +400,7 @@ es_func_mem_create (void *ES__RESTRICT *ES__RESTRICT cookie,
return err;
}
/* Read function for memory objects. */
static ssize_t
es_func_mem_read (void *cookie, void *buffer, size_t size)
@ -397,115 +418,89 @@ es_func_mem_read (void *cookie, void *buffer, size_t size)
}
ret = size;
return ret;
}
/* Write function for memory objects. */
static ssize_t
es_func_mem_write (void *cookie, const void *buffer, size_t size)
{
estream_cookie_mem_t mem_cookie = cookie;
func_realloc_t func_realloc = mem_cookie->func_realloc;
unsigned char *memory_new;
size_t newsize;
ssize_t ret;
int err;
if (size)
if (!size)
return 0; /* A flush is a NOP for memory objects. */
if (mem_cookie->modeflags & O_APPEND)
{
/* Regular write. */
if (mem_cookie->modeflags & O_APPEND)
/* Append to data. */
mem_cookie->offset = mem_cookie->data_len;
if (! mem_cookie->grow)
if (size > mem_cookie->memory_size - mem_cookie->offset)
size = mem_cookie->memory_size - mem_cookie->offset;
err = 0;
while (size > (mem_cookie->memory_size - mem_cookie->offset))
{
memory_new = (*func_realloc) (mem_cookie->memory,
mem_cookie->memory_size
+ mem_cookie->block_size);
if (! memory_new)
{
err = -1;
break;
}
else
{
if (mem_cookie->memory != memory_new)
mem_cookie->memory = memory_new;
mem_cookie->memory_size += mem_cookie->block_size;
}
}
if (err)
goto out;
if (size)
{
memcpy (mem_cookie->memory + mem_cookie->offset, buffer, size);
if (mem_cookie->offset + size > mem_cookie->data_len)
mem_cookie->data_len = mem_cookie->offset + size;
mem_cookie->offset += size;
}
/* Append to data. */
mem_cookie->offset = mem_cookie->data_len;
}
else
{
/* Flush. */
err = 0;
if (mem_cookie->append_zero)
{
if (mem_cookie->data_len >= mem_cookie->memory_size)
{
newsize = BUFFER_ROUND_TO_BLOCK (mem_cookie->data_len + 1,
mem_cookie->block_size)
* mem_cookie->block_size;
memory_new = (*func_realloc) (mem_cookie->memory, newsize);
if (! memory_new)
{
err = -1;
goto out;
}
if (mem_cookie->memory != memory_new)
mem_cookie->memory = memory_new;
mem_cookie->memory_size = newsize;
}
mem_cookie->memory[mem_cookie->data_len + 1] = 0;
}
/* Return information to user if necessary. */
if (mem_cookie->ptr)
*mem_cookie->ptr = (char *) mem_cookie->memory;
if (mem_cookie->size)
*mem_cookie->size = mem_cookie->data_len;
if (!mem_cookie->flags.grow)
{
/* We are not alloew to grow, thus limit the size to the left
space. FIXME: Does the grow flag an its semtics make sense
at all? */
if (size > mem_cookie->memory_size - mem_cookie->offset)
size = mem_cookie->memory_size - mem_cookie->offset;
}
out:
if (err)
ret = -1;
else
ret = size;
if (size > (mem_cookie->memory_size - mem_cookie->offset))
{
unsigned char *newbuf;
size_t newsize;
newsize = mem_cookie->memory_size + mem_cookie->block_size;
newsize = mem_cookie->offset + size;
if (newsize < mem_cookie->offset)
{
errno = EINVAL;
return -1;
}
newsize += mem_cookie->block_size - 1;
if (newsize < mem_cookie->offset)
{
errno = EINVAL;
return -1;
}
newsize /= mem_cookie->block_size;
newsize *= mem_cookie->block_size;
if (mem_cookie->memory_limit && newsize > mem_cookie->memory_limit)
{
errno = ENOSPC;
return -1;
}
newbuf = mem_cookie->func_realloc (mem_cookie->memory, newsize);
if (!newbuf)
return -1;
mem_cookie->memory = newbuf;
mem_cookie->memory_size = newsize;
assert (!(size > (mem_cookie->memory_size - mem_cookie->offset)));
}
memcpy (mem_cookie->memory + mem_cookie->offset, buffer, size);
if (mem_cookie->offset + size > mem_cookie->data_len)
mem_cookie->data_len = mem_cookie->offset + size;
mem_cookie->offset += size;
ret = size;
return ret;
}
/* Seek function for memory objects. */
static int
es_func_mem_seek (void *cookie, off_t *offset, int whence)
{
estream_cookie_mem_t mem_cookie = cookie;
off_t pos_new;
int err = 0;
switch (whence)
{
@ -522,69 +517,74 @@ es_func_mem_seek (void *cookie, off_t *offset, int whence)
break;
default:
/* Never reached. */
pos_new = 0;
errno = EINVAL;
return -1;
}
if (pos_new > mem_cookie->memory_size)
{
/* Grow buffer if possible. */
size_t newsize;
void *newbuf;
if (mem_cookie->grow)
if (!mem_cookie->flags.grow)
{
func_realloc_t func_realloc = mem_cookie->func_realloc;
size_t newsize;
void *p;
errno = ENOSPC;
return -1;
newsize = BUFFER_ROUND_TO_BLOCK (pos_new, mem_cookie->block_size);
p = (*func_realloc) (mem_cookie->memory, newsize);
if (! p)
{
err = -1;
goto out;
}
else
{
if (mem_cookie->memory != p)
mem_cookie->memory = p;
mem_cookie->memory_size = newsize;
}
}
else
{
errno = EINVAL;
err = -1;
goto out;
}
}
newsize = pos_new + mem_cookie->block_size - 1;
if (newsize < pos_new)
{
errno = EINVAL;
return -1;
}
newsize /= mem_cookie->block_size;
newsize *= mem_cookie->block_size;
if (mem_cookie->memory_limit && newsize > mem_cookie->memory_limit)
{
errno = ENOSPC;
return -1;
}
newbuf = mem_cookie->func_realloc (mem_cookie->memory, newsize);
if (!newbuf)
return -1;
mem_cookie->memory = newbuf;
mem_cookie->memory_size = newsize;
}
if (pos_new > mem_cookie->data_len)
/* Fill spare space with zeroes. */
memset (mem_cookie->memory + mem_cookie->data_len,
0, pos_new - mem_cookie->data_len);
{
/* Fill spare space with zeroes. */
memset (mem_cookie->memory + mem_cookie->data_len,
0, pos_new - mem_cookie->data_len);
mem_cookie->data_len = pos_new;
}
mem_cookie->offset = pos_new;
*offset = pos_new;
out:
return err;
return 0;
}
/* Destroy function for memory objects. */
static int
es_func_mem_destroy (void *cookie)
{
estream_cookie_mem_t mem_cookie = cookie;
func_free_t func_free = mem_cookie->func_free;
if (! mem_cookie->dont_free)
(*func_free) (mem_cookie->memory);
ES_MEM_FREE (mem_cookie);
if (cookie)
{
mem_cookie->func_free (mem_cookie->memory);
mem_free (mem_cookie);
}
return 0;
}
static es_cookie_io_functions_t estream_functions_mem =
{
es_func_mem_read,
@ -611,7 +611,7 @@ es_func_fd_create (void **cookie, int fd, unsigned int modeflags, int no_close)
estream_cookie_fd_t fd_cookie;
int err;
fd_cookie = ES_MEM_ALLOC (sizeof (*fd_cookie));
fd_cookie = mem_alloc (sizeof (*fd_cookie));
if (! fd_cookie)
err = -1;
else
@ -690,7 +690,7 @@ es_func_fd_destroy (void *cookie)
if (fd_cookie)
{
err = fd_cookie->no_close? 0 : close (fd_cookie->fd);
ES_MEM_FREE (fd_cookie);
mem_free (fd_cookie);
}
else
err = 0;
@ -726,7 +726,7 @@ es_func_fp_create (void **cookie, FILE *fp, unsigned int modeflags, int no_close
estream_cookie_fp_t fp_cookie;
int err;
fp_cookie = ES_MEM_ALLOC (sizeof *fp_cookie);
fp_cookie = mem_alloc (sizeof *fp_cookie);
if (!fp_cookie)
err = -1;
else
@ -807,7 +807,7 @@ es_func_fp_destroy (void *cookie)
{
fflush (fp_cookie->fp);
err = fp_cookie->no_close? 0 : fclose (fp_cookie->fp);
ES_MEM_FREE (fp_cookie);
mem_free (fp_cookie);
}
else
err = 0;
@ -841,7 +841,7 @@ es_func_file_create (void **cookie, int *filedes,
err = 0;
fd = -1;
file_cookie = ES_MEM_ALLOC (sizeof (*file_cookie));
file_cookie = mem_alloc (sizeof (*file_cookie));
if (! file_cookie)
{
err = -1;
@ -868,7 +868,7 @@ es_func_file_create (void **cookie, int *filedes,
out:
if (err)
ES_MEM_FREE (file_cookie);
mem_free (file_cookie);
return err;
}
@ -1149,14 +1149,14 @@ es_create (estream_t *stream, void *cookie, int fd,
stream_new = NULL;
stream_internal_new = NULL;
stream_new = ES_MEM_ALLOC (sizeof (*stream_new));
stream_new = mem_alloc (sizeof (*stream_new));
if (! stream_new)
{
err = -1;
goto out;
}
stream_internal_new = ES_MEM_ALLOC (sizeof (*stream_internal_new));
stream_internal_new = mem_alloc (sizeof (*stream_internal_new));
if (! stream_internal_new)
{
err = -1;
@ -1185,7 +1185,7 @@ es_create (estream_t *stream, void *cookie, int fd,
if (stream_new)
{
es_deinitialize (stream_new);
ES_MEM_FREE (stream_new);
mem_free (stream_new);
}
}
@ -1202,8 +1202,8 @@ es_destroy (estream_t stream)
{
es_list_remove (stream);
err = es_deinitialize (stream);
ES_MEM_FREE (stream->intern);
ES_MEM_FREE (stream);
mem_free (stream->intern);
mem_free (stream);
}
return err;
@ -1694,8 +1694,8 @@ es_skip (estream_t stream, size_t size)
static int
doreadline (estream_t ES__RESTRICT stream, size_t max_length,
char *ES__RESTRICT *ES__RESTRICT line,
size_t *ES__RESTRICT line_length)
char *ES__RESTRICT *ES__RESTRICT line,
size_t *ES__RESTRICT line_length)
{
size_t space_left;
size_t line_size;
@ -1711,9 +1711,11 @@ doreadline (estream_t ES__RESTRICT stream, size_t max_length,
line_stream = NULL;
line_stream_cookie = NULL;
err = es_func_mem_create (&line_stream_cookie, NULL, 0, 0, BUFFER_BLOCK_SIZE,
1, 0, 0, NULL, 0, ES_MEM_REALLOC, ES_MEM_FREE,
O_RDWR);
err = es_func_mem_create (&line_stream_cookie, NULL, 0, 0,
BUFFER_BLOCK_SIZE, 1,
mem_realloc, mem_free,
O_RDWR,
0);
if (err)
goto out;
@ -1779,7 +1781,7 @@ doreadline (estream_t ES__RESTRICT stream, size_t max_length,
if (! *line)
{
line_new = ES_MEM_ALLOC (line_size + 1);
line_new = mem_alloc (line_size + 1);
if (! line_new)
{
err = -1;
@ -1810,7 +1812,7 @@ doreadline (estream_t ES__RESTRICT stream, size_t max_length,
if (err)
{
if (! *line)
ES_MEM_FREE (line_new);
mem_free (line_new);
stream->intern->indicators.err = 1;
}
@ -1894,7 +1896,7 @@ es_set_buffering (estream_t ES__RESTRICT stream,
if (stream->intern->deallocate_buffer)
{
stream->intern->deallocate_buffer = 0;
ES_MEM_FREE (stream->buffer);
mem_free (stream->buffer);
stream->buffer = NULL;
}
@ -1908,7 +1910,7 @@ es_set_buffering (estream_t ES__RESTRICT stream,
buffer_new = buffer;
else
{
buffer_new = ES_MEM_ALLOC (size);
buffer_new = mem_alloc (size);
if (! buffer_new)
{
err = -1;
@ -2034,8 +2036,8 @@ es_mopen (unsigned char *ES__RESTRICT data, size_t data_n, size_t data_len,
goto out;
err = es_func_mem_create (&cookie, data, data_n, data_len,
BUFFER_BLOCK_SIZE, grow, 0, 0,
NULL, 0, func_realloc, func_free, modeflags);
BUFFER_BLOCK_SIZE, grow,
func_realloc, func_free, modeflags, 0);
if (err)
goto out;
@ -2052,37 +2054,33 @@ es_mopen (unsigned char *ES__RESTRICT data, size_t data_n, size_t data_len,
estream_t
es_open_memstream (char **ptr, size_t *size)
es_fopenmem (size_t memlimit, const char *ES__RESTRICT mode)
{
unsigned int modeflags;
int create_called;
estream_t stream;
void *cookie;
int err;
estream_t stream = NULL;
void *cookie = NULL;
/* Memory streams are always read/write. We use MODE only to get
the append flag. */
if (es_convert_mode (mode, &modeflags))
return NULL;
modeflags |= O_RDWR;
modeflags = O_RDWR;
create_called = 0;
stream = NULL;
cookie = 0;
err = es_func_mem_create (&cookie, NULL, 0, 0,
BUFFER_BLOCK_SIZE, 1, 1, 1,
ptr, size, ES_MEM_REALLOC, ES_MEM_FREE, modeflags);
if (err)
goto out;
if (es_func_mem_create (&cookie, NULL, 0, 0,
BUFFER_BLOCK_SIZE, 1,
mem_realloc, mem_free, modeflags,
memlimit))
return NULL;
create_called = 1;
err = es_create (&stream, cookie, -1, estream_functions_mem, modeflags);
out:
if (err && create_called)
if (es_create (&stream, cookie, -1, estream_functions_mem, modeflags))
(*estream_functions_mem.func_close) (cookie);
return stream;
}
estream_t
es_fopencookie (void *ES__RESTRICT cookie,
const char *ES__RESTRICT mode,
@ -2613,22 +2611,31 @@ es_fwrite (const void *ES__RESTRICT ptr, size_t size, size_t nitems,
char *
es_fgets (char *ES__RESTRICT s, int n, estream_t ES__RESTRICT stream)
es_fgets (char *ES__RESTRICT buffer, int length, estream_t ES__RESTRICT stream)
{
char *ret = NULL;
if (n)
unsigned char *s = (unsigned char*)buffer;
int c;
if (!length)
return NULL;
c = EOF;
ESTREAM_LOCK (stream);
while (length > 1 && (c = es_getc_unlocked (stream)) != EOF && c != '\n')
{
int err;
ESTREAM_LOCK (stream);
err = doreadline (stream, n, &s, NULL);
ESTREAM_UNLOCK (stream);
if (! err)
ret = s;
*s++ = c;
length--;
}
return ret;
ESTREAM_UNLOCK (stream);
if (c == EOF && s == (unsigned char*)buffer)
return NULL; /* Nothing read. */
if (c != EOF && length > 1)
*s++ = c;
*s = 0;
return buffer;
}
@ -2671,7 +2678,7 @@ es_getline (char *ES__RESTRICT *ES__RESTRICT lineptr, size_t *ES__RESTRICT n,
void *p;
p = ES_MEM_REALLOC (*lineptr, line_n + 1);
p = mem_realloc (*lineptr, line_n + 1);
if (! p)
err = -1;
else
@ -2687,7 +2694,7 @@ es_getline (char *ES__RESTRICT *ES__RESTRICT lineptr, size_t *ES__RESTRICT n,
if (*n != line_n)
*n = line_n;
}
ES_MEM_FREE (line);
mem_free (line);
}
else
{
@ -2747,7 +2754,7 @@ es_read_line (estream_t stream,
{
/* No buffer given - allocate a new one. */
length = 256;
buffer = ES_MEM_ALLOC (length);
buffer = mem_alloc (length);
*addr_of_buffer = buffer;
if (!buffer)
{
@ -2761,7 +2768,7 @@ es_read_line (estream_t stream,
if (length < 4)
{
/* This should never happen. If it does, the fucntion has been
/* This should never happen. If it does, the function has been
called with wrong arguments. */
errno = EINVAL;
return -1;
@ -2788,11 +2795,11 @@ es_read_line (estream_t stream,
}
length += 3; /* Adjust for the reserved bytes. */
length += length < 1024? 256 : 1024;
*addr_of_buffer = ES_MEM_REALLOC (buffer, length);
*addr_of_buffer = mem_realloc (buffer, length);
if (!*addr_of_buffer)
{
int save_errno = errno;
ES_MEM_FREE (buffer);
mem_free (buffer);
*length_of_buffer = *max_length = 0;
ESTREAM_UNLOCK (stream);
errno = save_errno;
@ -2820,8 +2827,7 @@ es_read_line (estream_t stream,
void
es_free (void *a)
{
if (a)
ES_MEM_FREE (a);
mem_free (a);
}
@ -2870,9 +2876,66 @@ es_fprintf (estream_t ES__RESTRICT stream,
return ret;
}
static int
tmpfd (void)
{
#ifdef HAVE_W32_SYSTEM
int attempts, n;
char buffer[MAX_PATH+9+12+1];
char *name, *p;
HANDLE file;
int pid = GetCurrentProcessId ();
unsigned int value;
int i;
n = GetTempPath (MAX_PATH+1, buffer);
if (!n || n > MAX_PATH || strlen (buffer) > MAX_PATH)
{
errno = ENOENT;
return -1;
}
p = buffer + strlen (buffer);
strcpy (p, "_estream");
p += 8;
/* We try to create the directory but don't care about an error as
it may already exist and the CreateFile would throw an error
anyway. */
CreateDirectory (buffer, NULL);
*p++ = '\\';
name = p;
for (attempts=0; attempts < 10; attempts++)
{
p = name;
value = (GetTickCount () ^ ((pid<<16) & 0xffff0000));
for (i=0; i < 8; i++)
{
*p++ = tohex (((value >> 28) & 0x0f));
value <<= 4;
}
strcpy (p, ".tmp");
file = CreateFile (buffer,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
CREATE_NEW,
FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE,
NULL);
if (file != INVALID_HANDLE_VALUE)
{
int fd = _open_osfhandle ((long)file, 0);
if (fd == -1)
{
CloseHandle (file);
return -1;
}
return fd;
}
Sleep (1); /* One ms as this is the granularity of GetTickCount. */
}
errno = ENOENT;
return -1;
#else /*!HAVE_W32_SYSTEM*/
FILE *fp;
int fp_fd;
int fd;
@ -2893,6 +2956,7 @@ tmpfd (void)
fclose (fp);
return fd;
#endif /*!HAVE_W32_SYSTEM*/
}
estream_t

View File

@ -1,11 +1,11 @@
/* estream.h - Extended stream I/O/ Library
/* estream.h - Extended stream I/O Library
* Copyright (C) 2004, 2005, 2006, 2007 g10 Code GmbH
*
* This file is part of Libestream.
*
* Libestream 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,
* by the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
*
* Libestream is distributed in the hope that it will be useful, but
@ -41,7 +41,7 @@
#endif /*_ESTREAM_PREFIX*/
#define es_fopen _ESTREAM_PREFIX(es_fopen)
#define es_mopen _ESTREAM_PREFIX(es_mopen)
#define es_open_memstream _ESTREAM_PREFIX(es_open_memstream)
#define es_fopenmem _ESTREAM_PREFIX(es_fopenmem)
#define es_fdopen _ESTREAM_PREFIX(es_fdopen)
#define es_fdopen_nc _ESTREAM_PREFIX(es_fdopen_nc)
#define es_fpopen _ESTREAM_PREFIX(es_fpopen)
@ -198,7 +198,7 @@ estream_t es_mopen (unsigned char *ES__RESTRICT data,
void *(*func_realloc) (void *mem, size_t size),
void (*func_free) (void *mem),
const char *ES__RESTRICT mode);
estream_t es_open_memstream (char **ptr, size_t *size);
estream_t es_fopenmem (size_t memlimit, const char *ES__RESTRICT mode);
estream_t es_fdopen (int filedes, const char *mode);
estream_t es_fdopen_nc (int filedes, const char *mode);
estream_t es_fpopen (FILE *fp, const char *mode);

View File

@ -64,6 +64,14 @@
#pragma weak pth_waitpid
#endif
#ifdef HAVE_W32_SYSTEM
/* It seems Vista doesn't grok X_OK and so fails access() tests.
Previous versions interpreted X_OK as F_OK anyway, so we'll just
use F_OK directly. */
#undef X_OK
#define X_OK F_OK
#endif /* HAVE_W32_SYSTEM */
#ifdef HAVE_W32_SYSTEM
/* We assume that a HANDLE can be represented by an int which should

View File

@ -1,5 +1,6 @@
/* sysutils.c - system helpers
* Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
* Copyright (C) 1998, 1999, 2000, 2001, 2003, 2004,
* 2007 Free Software Foundation, Inc.
*
* This file is part of GnuPG.
*
@ -53,6 +54,9 @@
#include "sysutils.h"
#define tohex(n) ((n) < 10 ? ((n) + '0') : (((n) - 10) + 'A'))
#if defined(__linux__) && defined(__alpha__) && __GLIBC__ < 2
#warning using trap_unaligned
static int
@ -298,7 +302,7 @@ translate_sys2libc_fd (gnupg_fd_t fd, int for_write)
}
/* This is the same as translate_sys2libc_fd but takes an integer
which is assumet to be such an system handle. */
which is assumed to be such an system handle. */
int
translate_sys2libc_fd_int (int fd, int for_write)
{
@ -311,3 +315,85 @@ translate_sys2libc_fd_int (int fd, int for_write)
return fd;
#endif
}
/* Replacement for tmpfile(). This is required because the tmpfile
function of Windows' runtime library is broken, insecure, ignores
TMPDIR and so on. In addition we create a file with an inheritable
handle. */
FILE *
gnupg_tmpfile (void)
{
#ifdef HAVE_W32_SYSTEM
int attempts, n;
char buffer[MAX_PATH+7+12+1];
char *name, *p;
HANDLE file;
int pid = GetCurrentProcessId ();
unsigned int value;
int i;
SECURITY_ATTRIBUTES sec_attr;
memset (&sec_attr, 0, sizeof sec_attr );
sec_attr.nLength = sizeof sec_attr;
sec_attr.bInheritHandle = TRUE;
n = GetTempPath (MAX_PATH+1, buffer);
if (!n || n > MAX_PATH || strlen (buffer) > MAX_PATH)
{
errno = ENOENT;
return NULL;
}
p = buffer + strlen (buffer);
p = stpcpy (p, "_gnupg");
/* We try to create the directory but don't care about an error as
it may already exist and the CreateFile would throw an error
anyway. */
CreateDirectory (buffer, NULL);
*p++ = '\\';
name = p;
for (attempts=0; attempts < 10; attempts++)
{
p = name;
value = (GetTickCount () ^ ((pid<<16) & 0xffff0000));
for (i=0; i < 8; i++)
{
*p++ = tohex (((value >> 28) & 0x0f));
value <<= 4;
}
strcpy (p, ".tmp");
file = CreateFile (buffer,
GENERIC_READ | GENERIC_WRITE,
0,
&sec_attr,
CREATE_NEW,
FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE,
NULL);
if (file != INVALID_HANDLE_VALUE)
{
FILE *fp;
int fd = _open_osfhandle ((long)file, 0);
if (fd == -1)
{
CloseHandle (file);
return NULL;
}
fp = fdopen (fd, "w+b");
if (!fp)
{
int save = errno;
close (fd);
errno = save;
return NULL;
}
return fp;
}
Sleep (1); /* One ms as this is the granularity of GetTickCount. */
}
errno = ENOENT;
return NULL;
#else /*!HAVE_W32_SYSTEM*/
return tmpfile ();
#endif /*!HAVE_W32_SYSTEM*/
}

View File

@ -41,6 +41,7 @@ const unsigned char *get_session_marker (size_t *rlen);
void gnupg_sleep (unsigned int seconds);
int translate_sys2libc_fd (gnupg_fd_t fd, int for_write);
int translate_sys2libc_fd_int (int fd, int for_write);
FILE *gnupg_tmpfile (void);
#ifdef HAVE_W32_SYSTEM

View File

@ -1,3 +1,8 @@
2007-08-21 Werner Koch <wk@g10code.com>
* misc.c (openpgp_md_test_algo): Remove rfc2440bis hash algorithms.
(openpgp_cipher_test_algo): Likewise for algos 5 and 6.
2007-08-02 Werner Koch <wk@g10code.com>
* gpg.c: Include gc-opt-flags.h and remove their definition here.

View File

@ -1,6 +1,6 @@
/* misc.c - miscellaneous functions
* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
* 2005, 2006 Free Software Foundation, Inc.
* 2005, 2006, 2007 Free Software Foundation, Inc.
*
* This file is part of GnuPG.
*
@ -331,7 +331,8 @@ print_digest_algo_note( int algo )
int
openpgp_cipher_test_algo( int algo )
{
if ( algo < 0 || algo > 110 )
/* 5 and 6 are marked reserved by rfc2440bis. */
if ( algo < 0 || algo > 110 || algo == 5 || algo == 6 )
return gpg_error (GPG_ERR_CIPHER_ALGO);
return gcry_cipher_test_algo (algo);
}
@ -396,8 +397,9 @@ openpgp_md_test_algo( int algo )
/* Note: If the list of actual supported OpenPGP algorithms changes,
make sure that our hard coded values at
print_status_begin_signing() gets updated. */
if (algo < 0 || algo > 110)
/* 4, 5, 6, 7 are defined by rfc2440 but will be removed from the
next revision of the standard. */
if (algo < 0 || algo > 110 || (algo >= 4 && algo <= 7))
return gpg_error (GPG_ERR_DIGEST_ALGO);
return gcry_md_test_algo (algo);
}

View File

@ -1,3 +1,20 @@
2007-08-22 Werner Koch <wk@g10code.com>
* certreqgen-ui.c (gpgsm_gencertreq_tty): Use es_fopenmem.
2007-08-21 Werner Koch <wk@g10code.com>
* import.c (parse_p12): Use gnupg_tmpfile.
* export.c (export_p12): Ditto.
2007-08-20 Werner Koch <wk@g10code.com>
* certreqgen.c (read_parameters): Change FP to an estream_t.
(gpgsm_genkey): Replace in_fd and in_stream by a estream_t.
* server.c (cmd_genkey): Adjust for that.
* certreqgen-ui.c (gpgsm_gencertreq_tty): Use es_open_memstream
instead of a temporary file.
2007-08-14 Werner Koch <wk@g10code.com>
* call-dirmngr.c (start_dirmngr): Use dirmngr_socket_name. change

View File

@ -97,7 +97,7 @@ gpgsm_gencertreq_tty (ctrl_t ctrl, FILE *output_fp)
gpg_error_t err;
char *answer;
int selection;
FILE *fp = NULL;
estream_t fp = NULL;
int method;
char *keytype;
char *keygrip = NULL;
@ -278,20 +278,20 @@ gpgsm_gencertreq_tty (ctrl_t ctrl, FILE *output_fp)
goto leave;
/* Now create a parameter file and generate the key. */
fp = tmpfile ();
fp = es_fopenmem (0, "w+");
if (!fp)
{
log_error (_("error creating temporary file: %s\n"), strerror (errno));
goto leave;
}
fputs (result, fp);
rewind (fp);
es_fputs (result, fp);
es_rewind (fp);
tty_printf (_("Now creating certificate request. "
"This may take a while ...\n"));
{
int save_pem = ctrl->create_pem;
ctrl->create_pem = 1; /* Force creation of PEM. */
err = gpgsm_genkey (ctrl, -1, fp, output_fp);
err = gpgsm_genkey (ctrl, fp, output_fp);
ctrl->create_pem = save_pem;
}
if (!err)
@ -302,8 +302,7 @@ gpgsm_gencertreq_tty (ctrl_t ctrl, FILE *output_fp)
mem_error:
log_error (_("resource problem: out or core\n"));
leave:
if (fp)
fclose (fp);
es_fclose (fp);
xfree (keytype);
xfree (subject_name);
xfree (keygrip);

View File

@ -247,7 +247,7 @@ get_parameter_uint (struct para_data_s *para, enum para_name key)
/* Read the certificate generation parameters from FP and generate
(all) certificate requests. */
static int
read_parameters (ctrl_t ctrl, FILE *fp, ksba_writer_t writer)
read_parameters (ctrl_t ctrl, estream_t fp, ksba_writer_t writer)
{
static struct {
const char *name;
@ -275,7 +275,7 @@ read_parameters (ctrl_t ctrl, FILE *fp, ksba_writer_t writer)
err = NULL;
para = NULL;
while (fgets (line, DIM(line)-1, fp) )
while (es_fgets (line, DIM(line)-1, fp) )
{
char *keyword, *value;
@ -391,7 +391,7 @@ read_parameters (ctrl_t ctrl, FILE *fp, ksba_writer_t writer)
log_error ("line %d: %s\n", outctrl.lnr, err);
rc = gpg_error (GPG_ERR_GENERAL);
}
else if (ferror(fp))
else if (es_ferror(fp))
{
log_error ("line %d: read error: %s\n", outctrl.lnr, strerror(errno) );
rc = gpg_error (GPG_ERR_GENERAL);
@ -829,27 +829,15 @@ create_request (ctrl_t ctrl,
/* Create a new key by reading the parameters from in_fd or in_stream.
Multiple keys may be created */
/* Create a new key by reading the parameters from IN_FP. Multiple
keys may be created */
int
gpgsm_genkey (ctrl_t ctrl, int in_fd, FILE *in_stream, FILE *out_fp)
gpgsm_genkey (ctrl_t ctrl, estream_t in_stream, FILE *out_fp)
{
int rc;
FILE *in_fp;
Base64Context b64writer = NULL;
ksba_writer_t writer;
if (in_stream)
in_fp = in_stream;
else
in_fp = fdopen (dup (in_fd), "rb");
if (!in_fp)
{
gpg_error_t tmperr = gpg_error (gpg_err_code_from_errno (errno));
log_error ("fdopen() failed: %s\n", strerror (errno));
return tmperr;
}
ctrl->pem_name = "CERTIFICATE REQUEST";
rc = gpgsm_create_writer (&b64writer, ctrl, out_fp, NULL, &writer);
if (rc)
@ -858,7 +846,7 @@ gpgsm_genkey (ctrl_t ctrl, int in_fd, FILE *in_stream, FILE *out_fp)
goto leave;
}
rc = read_parameters (ctrl, in_fp, writer);
rc = read_parameters (ctrl, in_stream, writer);
if (rc)
{
log_error ("error creating certificate request: %s <%s>\n",
@ -878,8 +866,6 @@ gpgsm_genkey (ctrl_t ctrl, int in_fd, FILE *in_stream, FILE *out_fp)
leave:
gpgsm_destroy_writer (b64writer);
if (!in_stream)
fclose (in_fp);
return rc;
}

View File

@ -32,6 +32,7 @@
#include "keydb.h"
#include "exechelp.h"
#include "i18n.h"
#include "sysutils.h"
@ -606,7 +607,7 @@ export_p12 (ctrl_t ctrl, const unsigned char *certimg, size_t certimglen,
else
pgmname = opt.protect_tool_program;
infp = tmpfile ();
infp = gnupg_tmpfile ();
if (!infp)
{
err = gpg_error_from_syserror ();
@ -622,7 +623,7 @@ export_p12 (ctrl_t ctrl, const unsigned char *certimg, size_t certimglen,
goto cleanup;
}
outfp = tmpfile ();
outfp = gnupg_tmpfile ();
if (!outfp)
{
err = gpg_error_from_syserror ();

View File

@ -325,7 +325,7 @@ int gpgsm_encrypt (ctrl_t ctrl, certlist_t recplist, int in_fd, FILE *out_fp);
int gpgsm_decrypt (ctrl_t ctrl, int in_fd, FILE *out_fp);
/*-- certreqgen.c --*/
int gpgsm_genkey (ctrl_t ctrl, int in_fd, FILE *in_stream, FILE *out_fp);
int gpgsm_genkey (ctrl_t ctrl, estream_t in_stream, FILE *out_fp);
/*-- certreqgen-ui.c --*/
void gpgsm_gencertreq_tty (ctrl_t ctrl, FILE *out_fp);

View File

@ -33,6 +33,7 @@
#include "keydb.h"
#include "exechelp.h"
#include "i18n.h"
#include "sysutils.h"
struct stats_s {
unsigned long count;
@ -517,7 +518,7 @@ parse_p12 (ctrl_t ctrl, ksba_reader_t reader,
gpg-protect-tool will anyway parse the entire pkcs#12 message in
memory, we simply use tempfiles here and pass them to
the gpg-protect-tool. */
tmpfp = tmpfile ();
tmpfp = gnupg_tmpfile ();
if (!tmpfp)
{
err = gpg_error_from_syserror ();
@ -542,7 +543,7 @@ parse_p12 (ctrl_t ctrl, ksba_reader_t reader,
goto cleanup;
}
certfp = tmpfile ();
certfp = gnupg_tmpfile ();
if (!certfp)
{
err = gpg_error_from_syserror ();

View File

@ -863,6 +863,7 @@ cmd_genkey (assuan_context_t ctx, char *line)
int inp_fd, out_fd;
FILE *out_fp;
int rc;
estream_t in_stream;
inp_fd = translate_sys2libc_fd (assuan_get_input_fd (ctx), 0);
if (inp_fd == -1)
@ -871,10 +872,17 @@ cmd_genkey (assuan_context_t ctx, char *line)
if (out_fd == -1)
return set_error (GPG_ERR_ASS_NO_OUTPUT, NULL);
in_stream = es_fdopen_nc (inp_fd, "r");
if (!in_stream)
return set_error (GPG_ERR_ASS_GENERAL, "es_fdopen failed");
out_fp = fdopen ( dup(out_fd), "w");
if (!out_fp)
return set_error (GPG_ERR_ASS_GENERAL, "fdopen() failed");
rc = gpgsm_genkey (ctrl, inp_fd, NULL, out_fp);
{
es_fclose (in_stream);
return set_error (GPG_ERR_ASS_GENERAL, "fdopen() failed");
}
rc = gpgsm_genkey (ctrl, in_stream, out_fp);
fclose (out_fp);
/* close and reset the fds */

View File

@ -1,3 +1,7 @@
2007-08-21 Werner Koch <wk@g10code.com>
* gpgkey2ssh.c (key_to_blob): Use gnupg_tmpfile().
2007-08-02 Werner Koch <wk@g10code.com>
* gpgconf-comp.c: Factor the public GC_OPT_FLAG constants out and

View File

@ -184,7 +184,7 @@ key_to_blob (unsigned char **blob, size_t *blob_n, const char *identifier, ...)
int ret;
pkdbuf_t *pkd;
stream = tmpfile ();
stream = gnupg_tmpfile ();
assert (stream);
identifier_n = strlen (identifier);