2009-04-01 12:51:53 +02:00
|
|
|
/* membuf.c - A simple implementation of a dynamic buffer.
|
2011-01-18 12:50:02 +01:00
|
|
|
* Copyright (C) 2001, 2003, 2009, 2011 Free Software Foundation, Inc.
|
2013-11-29 15:37:23 +01:00
|
|
|
* Copyright (C) 2013 Werner Koch
|
2003-08-05 19:11:04 +02:00
|
|
|
*
|
|
|
|
* This file is part of GnuPG.
|
|
|
|
*
|
Change license for some files in common to LGPLv3+/GPLv2+.
Having the LGPL on the common GnuPG code helps to share code
between GnuPG and related projects (like GPGME and Libassuan). This
is good for interoperability and to reduces bugs.
* common/asshelp.c, common/asshelp.h, common/asshelp2.c, common/b64dec.c
* common/b64enc.c, common/convert.c, common/dns-cert.c
* common/dns-cert.h common/exechelp-posix.c, common/exechelp-w32.c
* common/exechelp-w32ce.c, common/exechelp.h, common/get-passphrase.c
* common/get-passphrase.h, common/gettime.c, common/gpgrlhelp.c
* common/helpfile.c, common/homedir.c, common/http.c, common/http.h
* common/i18n.c, common/init.c, common/init.h, common/iobuf.c
* common/iobuf.h, common/localename.c, common/membuf.c, common/membuf.h
* common/miscellaneous.c, common/openpgp-oid.c, common/openpgpdefs.h
* common/percent.c, common/pka.c, common/pka.h, common/session-env.c
* common/session-env.h, common/sexp-parse.h, common/sexputil.c
* common/signal.c, common/srv.c, common/srv.h, common/ssh-utils.c
* common/ssh-utils.h, common/sysutils.c, common/sysutils.h
* common/tlv.c, common/tlv.h, common/ttyio.c, common/ttyio.h
* common/userids.c, common/userids.h, common/xasprintf.c: Change
license to LGPLv3+/GPLv2+/
2012-04-20 15:43:06 +02:00
|
|
|
* This file is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of either
|
2003-08-05 19:11:04 +02:00
|
|
|
*
|
Change license for some files in common to LGPLv3+/GPLv2+.
Having the LGPL on the common GnuPG code helps to share code
between GnuPG and related projects (like GPGME and Libassuan). This
is good for interoperability and to reduces bugs.
* common/asshelp.c, common/asshelp.h, common/asshelp2.c, common/b64dec.c
* common/b64enc.c, common/convert.c, common/dns-cert.c
* common/dns-cert.h common/exechelp-posix.c, common/exechelp-w32.c
* common/exechelp-w32ce.c, common/exechelp.h, common/get-passphrase.c
* common/get-passphrase.h, common/gettime.c, common/gpgrlhelp.c
* common/helpfile.c, common/homedir.c, common/http.c, common/http.h
* common/i18n.c, common/init.c, common/init.h, common/iobuf.c
* common/iobuf.h, common/localename.c, common/membuf.c, common/membuf.h
* common/miscellaneous.c, common/openpgp-oid.c, common/openpgpdefs.h
* common/percent.c, common/pka.c, common/pka.h, common/session-env.c
* common/session-env.h, common/sexp-parse.h, common/sexputil.c
* common/signal.c, common/srv.c, common/srv.h, common/ssh-utils.c
* common/ssh-utils.h, common/sysutils.c, common/sysutils.h
* common/tlv.c, common/tlv.h, common/ttyio.c, common/ttyio.h
* common/userids.c, common/userids.h, common/xasprintf.c: Change
license to LGPLv3+/GPLv2+/
2012-04-20 15:43:06 +02:00
|
|
|
* - 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,
|
2003-08-05 19:11:04 +02:00
|
|
|
* 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
|
2016-11-05 12:02:19 +01:00
|
|
|
* along with this program; if not, see <https://www.gnu.org/licenses/>.
|
2003-08-05 19:11:04 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
2013-11-29 15:37:23 +01:00
|
|
|
#include <stdarg.h>
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
|
|
#include "util.h"
|
2015-10-08 16:42:14 +02:00
|
|
|
#include "membuf.h"
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* A simple implementation of a dynamic buffer. Use init_membuf() to
|
|
|
|
create a buffer, put_membuf to append bytes and get_membuf to
|
|
|
|
release and return the buffer. Allocation errors are detected but
|
|
|
|
only returned at the final get_membuf(), this helps not to clutter
|
|
|
|
the code with out of core checks. */
|
|
|
|
|
|
|
|
void
|
|
|
|
init_membuf (membuf_t *mb, int initiallen)
|
|
|
|
{
|
|
|
|
mb->len = 0;
|
|
|
|
mb->size = initiallen;
|
|
|
|
mb->out_of_core = 0;
|
|
|
|
mb->buf = xtrymalloc (initiallen);
|
|
|
|
if (!mb->buf)
|
2006-10-04 18:45:04 +02:00
|
|
|
mb->out_of_core = errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Same as init_membuf but allocates the buffer in secure memory. */
|
|
|
|
void
|
|
|
|
init_membuf_secure (membuf_t *mb, int initiallen)
|
|
|
|
{
|
|
|
|
mb->len = 0;
|
|
|
|
mb->size = initiallen;
|
|
|
|
mb->out_of_core = 0;
|
2007-06-21 20:44:48 +02:00
|
|
|
mb->buf = xtrymalloc_secure (initiallen);
|
2006-10-04 18:45:04 +02:00
|
|
|
if (!mb->buf)
|
|
|
|
mb->out_of_core = errno;
|
2003-08-05 19:11:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-20 22:19:50 +01:00
|
|
|
/* Shift the content of the membuf MB by AMOUNT bytes. The next
|
2011-01-18 12:50:02 +01:00
|
|
|
operation will then behave as if AMOUNT bytes had not been put into
|
|
|
|
the buffer. If AMOUNT is greater than the actual accumulated
|
|
|
|
bytes, the membuf is basically reset to its initial state. */
|
|
|
|
void
|
|
|
|
clear_membuf (membuf_t *mb, size_t amount)
|
|
|
|
{
|
|
|
|
/* No need to clear if we are already out of core. */
|
|
|
|
if (mb->out_of_core)
|
|
|
|
return;
|
|
|
|
if (amount >= mb->len)
|
|
|
|
mb->len = 0;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mb->len -= amount;
|
|
|
|
memmove (mb->buf, mb->buf+amount, mb->len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
void
|
|
|
|
put_membuf (membuf_t *mb, const void *buf, size_t len)
|
|
|
|
{
|
2010-06-17 17:44:44 +02:00
|
|
|
if (mb->out_of_core || !len)
|
2003-08-05 19:11:04 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (mb->len + len >= mb->size)
|
|
|
|
{
|
|
|
|
char *p;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
mb->size += len + 1024;
|
|
|
|
p = xtryrealloc (mb->buf, mb->size);
|
|
|
|
if (!p)
|
|
|
|
{
|
2006-10-04 18:45:04 +02:00
|
|
|
mb->out_of_core = errno ? errno : ENOMEM;
|
2004-12-20 09:32:56 +01:00
|
|
|
/* Wipe out what we already accumulated. This is required
|
|
|
|
in case we are storing sensitive data here. The membuf
|
|
|
|
API does not provide another way to cleanup after an
|
2011-02-04 12:57:53 +01:00
|
|
|
error. */
|
2009-04-01 12:51:53 +02:00
|
|
|
wipememory (mb->buf, mb->len);
|
2003-08-05 19:11:04 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
mb->buf = p;
|
|
|
|
}
|
|
|
|
memcpy (mb->buf + mb->len, buf, len);
|
|
|
|
mb->len += len;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-08 06:33:27 +01:00
|
|
|
/* A variant of put_membuf accepting a void * and returning a
|
|
|
|
gpg_error_t (which will always return 0) to be used as a generic
|
|
|
|
callback handler. This function also allows buffer to be NULL. */
|
|
|
|
gpg_error_t
|
|
|
|
put_membuf_cb (void *opaque, const void *buf, size_t len)
|
|
|
|
{
|
|
|
|
membuf_t *data = opaque;
|
|
|
|
|
|
|
|
if (buf)
|
|
|
|
put_membuf (data, buf, len);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-06-21 20:44:48 +02:00
|
|
|
void
|
|
|
|
put_membuf_str (membuf_t *mb, const char *string)
|
|
|
|
{
|
|
|
|
put_membuf (mb, string, strlen (string));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-29 15:37:23 +01:00
|
|
|
void
|
|
|
|
put_membuf_printf (membuf_t *mb, const char *format, ...)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
va_list arg_ptr;
|
|
|
|
char *buf;
|
|
|
|
|
|
|
|
va_start (arg_ptr, format);
|
2014-08-26 17:47:22 +02:00
|
|
|
rc = gpgrt_vasprintf (&buf, format, arg_ptr);
|
2013-11-29 15:37:23 +01:00
|
|
|
if (rc < 0)
|
|
|
|
mb->out_of_core = errno ? errno : ENOMEM;
|
|
|
|
va_end (arg_ptr);
|
|
|
|
if (rc >= 0)
|
|
|
|
{
|
|
|
|
put_membuf (mb, buf, strlen (buf));
|
|
|
|
xfree (buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
void *
|
|
|
|
get_membuf (membuf_t *mb, size_t *len)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
if (mb->out_of_core)
|
|
|
|
{
|
2009-04-01 12:51:53 +02:00
|
|
|
if (mb->buf)
|
|
|
|
{
|
|
|
|
wipememory (mb->buf, mb->len);
|
|
|
|
xfree (mb->buf);
|
|
|
|
mb->buf = NULL;
|
|
|
|
}
|
2010-03-02 22:25:08 +01:00
|
|
|
gpg_err_set_errno (mb->out_of_core);
|
2003-08-05 19:11:04 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = mb->buf;
|
2006-10-04 18:45:04 +02:00
|
|
|
if (len)
|
|
|
|
*len = mb->len;
|
2003-08-05 19:11:04 +02:00
|
|
|
mb->buf = NULL;
|
|
|
|
mb->out_of_core = ENOMEM; /* hack to make sure it won't get reused. */
|
|
|
|
return p;
|
|
|
|
}
|
2011-01-18 12:50:02 +01:00
|
|
|
|
|
|
|
|
2014-02-26 16:16:30 +01:00
|
|
|
/* Same as get_membuf but shrinks the reallocated space to the
|
|
|
|
required size. */
|
|
|
|
void *
|
|
|
|
get_membuf_shrink (membuf_t *mb, size_t *len)
|
|
|
|
{
|
|
|
|
void *p, *pp;
|
|
|
|
size_t dummylen;
|
|
|
|
|
|
|
|
if (!len)
|
|
|
|
len = &dummylen;
|
|
|
|
|
2014-03-06 15:28:42 +01:00
|
|
|
p = get_membuf (mb, len);
|
2014-02-26 16:16:30 +01:00
|
|
|
if (!p)
|
|
|
|
return NULL;
|
2014-03-06 15:28:42 +01:00
|
|
|
if (*len)
|
2014-02-26 16:16:30 +01:00
|
|
|
{
|
2014-03-06 15:28:42 +01:00
|
|
|
pp = xtryrealloc (p, *len);
|
2014-02-26 16:16:30 +01:00
|
|
|
if (pp)
|
|
|
|
p = pp;
|
|
|
|
}
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-18 12:50:02 +01:00
|
|
|
/* Peek at the membuf MB. On success a pointer to the buffer is
|
|
|
|
returned which is valid until the next operation on MB. If LEN is
|
|
|
|
not NULL the current LEN of the buffer is stored there. On error
|
|
|
|
NULL is returned and ERRNO is set. */
|
|
|
|
const void *
|
|
|
|
peek_membuf (membuf_t *mb, size_t *len)
|
|
|
|
{
|
|
|
|
const char *p;
|
|
|
|
|
|
|
|
if (mb->out_of_core)
|
|
|
|
{
|
|
|
|
gpg_err_set_errno (mb->out_of_core);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = mb->buf;
|
|
|
|
if (len)
|
|
|
|
*len = mb->len;
|
|
|
|
return p;
|
|
|
|
}
|