2002-10-09 12:26:00 +00:00
|
|
|
|
/* data-mem.c - A memory based data object.
|
2018-11-16 12:27:33 +00:00
|
|
|
|
* Copyright (C) 2002, 2003, 2004, 2007 g10 Code GmbH
|
|
|
|
|
*
|
|
|
|
|
* This file is part of GPGME.
|
|
|
|
|
*
|
|
|
|
|
* GPGME is free software; you can redistribute it and/or modify it
|
|
|
|
|
* under the terms of the GNU Lesser General Public License as
|
|
|
|
|
* published by the Free Software Foundation; either version 2.1 of
|
|
|
|
|
* the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* GPGME 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
|
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
* License along with this program; if not, see <https://gnu.org/licenses/>.
|
|
|
|
|
* SPDX-License-Identifier: LGPL-2.1-or-later
|
|
|
|
|
*/
|
2002-10-09 12:26:00 +00:00
|
|
|
|
|
|
|
|
|
#if HAVE_CONFIG_H
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <stdlib.h>
|
2010-11-02 16:27:46 +00:00
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
|
# include <unistd.h>
|
|
|
|
|
#endif
|
2002-10-09 12:26:00 +00:00
|
|
|
|
#include <assert.h>
|
2003-01-06 14:07:56 +00:00
|
|
|
|
#include <string.h>
|
2002-10-09 12:26:00 +00:00
|
|
|
|
|
|
|
|
|
#include "data.h"
|
|
|
|
|
#include "util.h"
|
2007-07-17 Marcus Brinkmann <marcus@g10code.de>
* debug.c:;5B Include <errno.h> and "debug.h".
(_gpgme_debug): Save and restore ERRNO.
(TOHEX): New macro.
(_gpgme_debug_buffer): New function.
* conversion.c, data-compat.c, data-mem.c, data.c, engine-gpgsm.c,
gpgme.c, keylist.c, posix-io.c, rungpg.c, sign.c, version.c,
w32-io.c, wait.c: Replace DEBUG macros by TRACE_* variants. In
most of these files, add many more tracepoints.
2007-07-17 12:36:04 +00:00
|
|
|
|
#include "debug.h"
|
2002-10-09 12:26:00 +00:00
|
|
|
|
|
|
|
|
|
|
Make definition of off_t robust against misbehaving w32 toolchains.
* configure.ac (NEED__FILE_OFFSET_BITS): Change to define gpgme_off_t
and gpgme_ssize_t.
(API__OFF_T, API__SSIZE_T): New ac_subst.
* src/gpgme.h.in: Replace all ssize_t and off_t by ac_subst macros.
* src/assuan-support.c, src/ath-pthread.c, src/ath.c, src/ath.h
* src/data-compat.c, src/data-fd.c, src/data-mem.c, src/data-stream.c
* src/data-user.c, src/data.c, src/data.h, src/engine-gpgsm.c
* src/engine-uiserver.c, src/gpgme-tool.c, src/gpgme.c: Replace off_t
by gpgme_off_t and sszie_t by gpgme_ssize_t.
* src/ath-pthread.c, src/ath.h: Include gpgme.h.
--
For a detailed description, see the gpgme.texi diff.
2013-04-25 11:00:16 +00:00
|
|
|
|
static gpgme_ssize_t
|
2003-05-18 20:45:24 +00:00
|
|
|
|
mem_read (gpgme_data_t dh, void *buffer, size_t size)
|
2002-10-09 12:26:00 +00:00
|
|
|
|
{
|
|
|
|
|
size_t amt = dh->data.mem.length - dh->data.mem.offset;
|
|
|
|
|
const char *src;
|
|
|
|
|
|
|
|
|
|
if (!amt)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
if (size < amt)
|
|
|
|
|
amt = size;
|
|
|
|
|
|
|
|
|
|
src = dh->data.mem.buffer ? dh->data.mem.buffer : dh->data.mem.orig_buffer;
|
|
|
|
|
memcpy (buffer, src + dh->data.mem.offset, amt);
|
|
|
|
|
dh->data.mem.offset += amt;
|
|
|
|
|
return amt;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
Make definition of off_t robust against misbehaving w32 toolchains.
* configure.ac (NEED__FILE_OFFSET_BITS): Change to define gpgme_off_t
and gpgme_ssize_t.
(API__OFF_T, API__SSIZE_T): New ac_subst.
* src/gpgme.h.in: Replace all ssize_t and off_t by ac_subst macros.
* src/assuan-support.c, src/ath-pthread.c, src/ath.c, src/ath.h
* src/data-compat.c, src/data-fd.c, src/data-mem.c, src/data-stream.c
* src/data-user.c, src/data.c, src/data.h, src/engine-gpgsm.c
* src/engine-uiserver.c, src/gpgme-tool.c, src/gpgme.c: Replace off_t
by gpgme_off_t and sszie_t by gpgme_ssize_t.
* src/ath-pthread.c, src/ath.h: Include gpgme.h.
--
For a detailed description, see the gpgme.texi diff.
2013-04-25 11:00:16 +00:00
|
|
|
|
static gpgme_ssize_t
|
2003-05-18 20:45:24 +00:00
|
|
|
|
mem_write (gpgme_data_t dh, const void *buffer, size_t size)
|
2002-10-09 12:26:00 +00:00
|
|
|
|
{
|
|
|
|
|
size_t unused;
|
|
|
|
|
|
|
|
|
|
if (!dh->data.mem.buffer && dh->data.mem.orig_buffer)
|
|
|
|
|
{
|
|
|
|
|
size_t new_size = dh->data.mem.size;
|
|
|
|
|
char *new_buffer;
|
|
|
|
|
|
|
|
|
|
if (new_size < dh->data.mem.offset + size)
|
|
|
|
|
new_size = dh->data.mem.offset + size;
|
|
|
|
|
|
|
|
|
|
new_buffer = malloc (new_size);
|
|
|
|
|
if (!new_buffer)
|
|
|
|
|
return -1;
|
2003-06-22 20:56:48 +00:00
|
|
|
|
memcpy (new_buffer, dh->data.mem.orig_buffer, dh->data.mem.length);
|
|
|
|
|
|
2012-09-25 13:29:49 +00:00
|
|
|
|
dh->data.mem.buffer = new_buffer;
|
2002-10-09 12:26:00 +00:00
|
|
|
|
dh->data.mem.size = new_size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unused = dh->data.mem.size - dh->data.mem.offset;
|
|
|
|
|
if (unused < size)
|
|
|
|
|
{
|
|
|
|
|
/* Allocate a large enough buffer with exponential backoff. */
|
|
|
|
|
#define INITIAL_ALLOC 512
|
|
|
|
|
size_t new_size = dh->data.mem.size
|
|
|
|
|
? (2 * dh->data.mem.size) : INITIAL_ALLOC;
|
|
|
|
|
char *new_buffer;
|
|
|
|
|
|
|
|
|
|
if (new_size < dh->data.mem.offset + size)
|
|
|
|
|
new_size = dh->data.mem.offset + size;
|
|
|
|
|
|
|
|
|
|
new_buffer = realloc (dh->data.mem.buffer, new_size);
|
|
|
|
|
if (!new_buffer && new_size > dh->data.mem.offset + size)
|
|
|
|
|
{
|
|
|
|
|
/* Maybe we were too greedy, try again. */
|
|
|
|
|
new_size = dh->data.mem.offset + size;
|
|
|
|
|
new_buffer = realloc (dh->data.mem.buffer, new_size);
|
|
|
|
|
}
|
|
|
|
|
if (!new_buffer)
|
|
|
|
|
return -1;
|
|
|
|
|
dh->data.mem.buffer = new_buffer;
|
|
|
|
|
dh->data.mem.size = new_size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memcpy (dh->data.mem.buffer + dh->data.mem.offset, buffer, size);
|
|
|
|
|
dh->data.mem.offset += size;
|
|
|
|
|
if (dh->data.mem.length < dh->data.mem.offset)
|
|
|
|
|
dh->data.mem.length = dh->data.mem.offset;
|
|
|
|
|
return size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
Make definition of off_t robust against misbehaving w32 toolchains.
* configure.ac (NEED__FILE_OFFSET_BITS): Change to define gpgme_off_t
and gpgme_ssize_t.
(API__OFF_T, API__SSIZE_T): New ac_subst.
* src/gpgme.h.in: Replace all ssize_t and off_t by ac_subst macros.
* src/assuan-support.c, src/ath-pthread.c, src/ath.c, src/ath.h
* src/data-compat.c, src/data-fd.c, src/data-mem.c, src/data-stream.c
* src/data-user.c, src/data.c, src/data.h, src/engine-gpgsm.c
* src/engine-uiserver.c, src/gpgme-tool.c, src/gpgme.c: Replace off_t
by gpgme_off_t and sszie_t by gpgme_ssize_t.
* src/ath-pthread.c, src/ath.h: Include gpgme.h.
--
For a detailed description, see the gpgme.texi diff.
2013-04-25 11:00:16 +00:00
|
|
|
|
static gpgme_off_t
|
|
|
|
|
mem_seek (gpgme_data_t dh, gpgme_off_t offset, int whence)
|
2002-10-09 12:26:00 +00:00
|
|
|
|
{
|
|
|
|
|
switch (whence)
|
|
|
|
|
{
|
|
|
|
|
case SEEK_SET:
|
|
|
|
|
if (offset < 0 || offset > dh->data.mem.length)
|
|
|
|
|
{
|
2010-05-06 Marcus Brinkmann <marcus@g10code.de>
* configure.ac: Require libgpg-error 1.8.
src/
2010-05-06 Marcus Brinkmann <marcus@g10code.de>
* sign.c, data-user.c, conversion.c, debug.c, verify.c, data.c,
decrypt.c, delete.c, assuan-support.c, import.c, engine-gpgsm.c,
data-mem.c, op-support.c, w32-io.c, w32-util.c, data-compat.c: Use
gpg_error_from_syserror instead gpg_error_from_errno, and use
gpg_err_set_errno to set error number.
* setenv.c: Include <gpg-error.h> and define __set_errno to use
gpg_err_set_errno.
* gpgme-tool.c (ARGP_ERR_UNKNOWN): Define to EDEADLOCK (which is
mapped in Windows CE) instead of E2BIG (which is not).
(gt_import_keys): Initialize err.
2010-05-06 13:39:55 +00:00
|
|
|
|
gpg_err_set_errno (EINVAL);
|
2002-10-09 12:26:00 +00:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
dh->data.mem.offset = offset;
|
|
|
|
|
break;
|
|
|
|
|
case SEEK_CUR:
|
|
|
|
|
if ((offset > 0 && dh->data.mem.length - dh->data.mem.offset < offset)
|
2012-09-25 13:29:49 +00:00
|
|
|
|
|| (offset < 0 && dh->data.mem.offset < -offset))
|
2002-10-09 12:26:00 +00:00
|
|
|
|
{
|
2010-05-06 Marcus Brinkmann <marcus@g10code.de>
* configure.ac: Require libgpg-error 1.8.
src/
2010-05-06 Marcus Brinkmann <marcus@g10code.de>
* sign.c, data-user.c, conversion.c, debug.c, verify.c, data.c,
decrypt.c, delete.c, assuan-support.c, import.c, engine-gpgsm.c,
data-mem.c, op-support.c, w32-io.c, w32-util.c, data-compat.c: Use
gpg_error_from_syserror instead gpg_error_from_errno, and use
gpg_err_set_errno to set error number.
* setenv.c: Include <gpg-error.h> and define __set_errno to use
gpg_err_set_errno.
* gpgme-tool.c (ARGP_ERR_UNKNOWN): Define to EDEADLOCK (which is
mapped in Windows CE) instead of E2BIG (which is not).
(gt_import_keys): Initialize err.
2010-05-06 13:39:55 +00:00
|
|
|
|
gpg_err_set_errno (EINVAL);
|
2002-10-09 12:26:00 +00:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
dh->data.mem.offset += offset;
|
|
|
|
|
break;
|
|
|
|
|
case SEEK_END:
|
|
|
|
|
if (offset > 0 || -offset > dh->data.mem.length)
|
|
|
|
|
{
|
2010-05-06 Marcus Brinkmann <marcus@g10code.de>
* configure.ac: Require libgpg-error 1.8.
src/
2010-05-06 Marcus Brinkmann <marcus@g10code.de>
* sign.c, data-user.c, conversion.c, debug.c, verify.c, data.c,
decrypt.c, delete.c, assuan-support.c, import.c, engine-gpgsm.c,
data-mem.c, op-support.c, w32-io.c, w32-util.c, data-compat.c: Use
gpg_error_from_syserror instead gpg_error_from_errno, and use
gpg_err_set_errno to set error number.
* setenv.c: Include <gpg-error.h> and define __set_errno to use
gpg_err_set_errno.
* gpgme-tool.c (ARGP_ERR_UNKNOWN): Define to EDEADLOCK (which is
mapped in Windows CE) instead of E2BIG (which is not).
(gt_import_keys): Initialize err.
2010-05-06 13:39:55 +00:00
|
|
|
|
gpg_err_set_errno (EINVAL);
|
2002-10-09 12:26:00 +00:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
2012-07-28 20:06:09 +00:00
|
|
|
|
dh->data.mem.offset = dh->data.mem.length + offset;
|
2002-10-09 12:26:00 +00:00
|
|
|
|
break;
|
|
|
|
|
default:
|
2010-05-06 Marcus Brinkmann <marcus@g10code.de>
* configure.ac: Require libgpg-error 1.8.
src/
2010-05-06 Marcus Brinkmann <marcus@g10code.de>
* sign.c, data-user.c, conversion.c, debug.c, verify.c, data.c,
decrypt.c, delete.c, assuan-support.c, import.c, engine-gpgsm.c,
data-mem.c, op-support.c, w32-io.c, w32-util.c, data-compat.c: Use
gpg_error_from_syserror instead gpg_error_from_errno, and use
gpg_err_set_errno to set error number.
* setenv.c: Include <gpg-error.h> and define __set_errno to use
gpg_err_set_errno.
* gpgme-tool.c (ARGP_ERR_UNKNOWN): Define to EDEADLOCK (which is
mapped in Windows CE) instead of E2BIG (which is not).
(gt_import_keys): Initialize err.
2010-05-06 13:39:55 +00:00
|
|
|
|
gpg_err_set_errno (EINVAL);
|
2002-10-09 12:26:00 +00:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return dh->data.mem.offset;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2002-12-06 22:06:25 +00:00
|
|
|
|
static void
|
2003-05-18 20:45:24 +00:00
|
|
|
|
mem_release (gpgme_data_t dh)
|
2002-10-09 12:26:00 +00:00
|
|
|
|
{
|
|
|
|
|
if (dh->data.mem.buffer)
|
|
|
|
|
free (dh->data.mem.buffer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-05-18 20:45:24 +00:00
|
|
|
|
static struct _gpgme_data_cbs mem_cbs =
|
2002-10-09 12:26:00 +00:00
|
|
|
|
{
|
|
|
|
|
mem_read,
|
|
|
|
|
mem_write,
|
|
|
|
|
mem_seek,
|
2007-01-18 17:59:26 +00:00
|
|
|
|
mem_release,
|
|
|
|
|
NULL
|
2002-10-09 12:26:00 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2005-10-06 10:44:26 +00:00
|
|
|
|
/* Create a new data buffer and return it in R_DH. */
|
2003-05-18 20:45:24 +00:00
|
|
|
|
gpgme_error_t
|
2007-07-17 Marcus Brinkmann <marcus@g10code.de>
* debug.c:;5B Include <errno.h> and "debug.h".
(_gpgme_debug): Save and restore ERRNO.
(TOHEX): New macro.
(_gpgme_debug_buffer): New function.
* conversion.c, data-compat.c, data-mem.c, data.c, engine-gpgsm.c,
gpgme.c, keylist.c, posix-io.c, rungpg.c, sign.c, version.c,
w32-io.c, wait.c: Replace DEBUG macros by TRACE_* variants. In
most of these files, add many more tracepoints.
2007-07-17 12:36:04 +00:00
|
|
|
|
gpgme_data_new (gpgme_data_t *r_dh)
|
2002-10-09 12:26:00 +00:00
|
|
|
|
{
|
2007-07-17 Marcus Brinkmann <marcus@g10code.de>
* debug.c:;5B Include <errno.h> and "debug.h".
(_gpgme_debug): Save and restore ERRNO.
(TOHEX): New macro.
(_gpgme_debug_buffer): New function.
* conversion.c, data-compat.c, data-mem.c, data.c, engine-gpgsm.c,
gpgme.c, keylist.c, posix-io.c, rungpg.c, sign.c, version.c,
w32-io.c, wait.c: Replace DEBUG macros by TRACE_* variants. In
most of these files, add many more tracepoints.
2007-07-17 12:36:04 +00:00
|
|
|
|
gpgme_error_t err;
|
2018-11-16 15:25:49 +00:00
|
|
|
|
TRACE_BEG (DEBUG_DATA, "gpgme_data_new", r_dh, "");
|
2007-07-17 Marcus Brinkmann <marcus@g10code.de>
* debug.c:;5B Include <errno.h> and "debug.h".
(_gpgme_debug): Save and restore ERRNO.
(TOHEX): New macro.
(_gpgme_debug_buffer): New function.
* conversion.c, data-compat.c, data-mem.c, data.c, engine-gpgsm.c,
gpgme.c, keylist.c, posix-io.c, rungpg.c, sign.c, version.c,
w32-io.c, wait.c: Replace DEBUG macros by TRACE_* variants. In
most of these files, add many more tracepoints.
2007-07-17 12:36:04 +00:00
|
|
|
|
|
|
|
|
|
err = _gpgme_data_new (r_dh, &mem_cbs);
|
|
|
|
|
|
2002-10-09 12:26:00 +00:00
|
|
|
|
if (err)
|
2007-07-17 Marcus Brinkmann <marcus@g10code.de>
* debug.c:;5B Include <errno.h> and "debug.h".
(_gpgme_debug): Save and restore ERRNO.
(TOHEX): New macro.
(_gpgme_debug_buffer): New function.
* conversion.c, data-compat.c, data-mem.c, data.c, engine-gpgsm.c,
gpgme.c, keylist.c, posix-io.c, rungpg.c, sign.c, version.c,
w32-io.c, wait.c: Replace DEBUG macros by TRACE_* variants. In
most of these files, add many more tracepoints.
2007-07-17 12:36:04 +00:00
|
|
|
|
return TRACE_ERR (err);
|
2002-10-09 12:26:00 +00:00
|
|
|
|
|
2018-11-16 17:17:22 +00:00
|
|
|
|
TRACE_SUC ("dh=%p", *r_dh);
|
|
|
|
|
return 0;
|
2002-10-09 12:26:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Create a new data buffer filled with SIZE bytes starting from
|
|
|
|
|
BUFFER. If COPY is zero, copying is delayed until necessary, and
|
|
|
|
|
the data is taken from the original location when needed. */
|
2003-05-18 20:45:24 +00:00
|
|
|
|
gpgme_error_t
|
2007-07-17 Marcus Brinkmann <marcus@g10code.de>
* debug.c:;5B Include <errno.h> and "debug.h".
(_gpgme_debug): Save and restore ERRNO.
(TOHEX): New macro.
(_gpgme_debug_buffer): New function.
* conversion.c, data-compat.c, data-mem.c, data.c, engine-gpgsm.c,
gpgme.c, keylist.c, posix-io.c, rungpg.c, sign.c, version.c,
w32-io.c, wait.c: Replace DEBUG macros by TRACE_* variants. In
most of these files, add many more tracepoints.
2007-07-17 12:36:04 +00:00
|
|
|
|
gpgme_data_new_from_mem (gpgme_data_t *r_dh, const char *buffer,
|
2002-10-09 12:26:00 +00:00
|
|
|
|
size_t size, int copy)
|
|
|
|
|
{
|
2007-07-17 Marcus Brinkmann <marcus@g10code.de>
* debug.c:;5B Include <errno.h> and "debug.h".
(_gpgme_debug): Save and restore ERRNO.
(TOHEX): New macro.
(_gpgme_debug_buffer): New function.
* conversion.c, data-compat.c, data-mem.c, data.c, engine-gpgsm.c,
gpgme.c, keylist.c, posix-io.c, rungpg.c, sign.c, version.c,
w32-io.c, wait.c: Replace DEBUG macros by TRACE_* variants. In
most of these files, add many more tracepoints.
2007-07-17 12:36:04 +00:00
|
|
|
|
gpgme_error_t err;
|
2018-11-16 15:25:49 +00:00
|
|
|
|
TRACE_BEG (DEBUG_DATA, "gpgme_data_new_from_mem", r_dh,
|
2018-11-16 15:57:09 +00:00
|
|
|
|
"buffer=%p, size=%zu, copy=%i (%s)", buffer, size,
|
2007-07-17 Marcus Brinkmann <marcus@g10code.de>
* debug.c:;5B Include <errno.h> and "debug.h".
(_gpgme_debug): Save and restore ERRNO.
(TOHEX): New macro.
(_gpgme_debug_buffer): New function.
* conversion.c, data-compat.c, data-mem.c, data.c, engine-gpgsm.c,
gpgme.c, keylist.c, posix-io.c, rungpg.c, sign.c, version.c,
w32-io.c, wait.c: Replace DEBUG macros by TRACE_* variants. In
most of these files, add many more tracepoints.
2007-07-17 12:36:04 +00:00
|
|
|
|
copy, copy ? "yes" : "no");
|
|
|
|
|
|
|
|
|
|
err = _gpgme_data_new (r_dh, &mem_cbs);
|
2002-10-09 12:26:00 +00:00
|
|
|
|
if (err)
|
2007-07-17 Marcus Brinkmann <marcus@g10code.de>
* debug.c:;5B Include <errno.h> and "debug.h".
(_gpgme_debug): Save and restore ERRNO.
(TOHEX): New macro.
(_gpgme_debug_buffer): New function.
* conversion.c, data-compat.c, data-mem.c, data.c, engine-gpgsm.c,
gpgme.c, keylist.c, posix-io.c, rungpg.c, sign.c, version.c,
w32-io.c, wait.c: Replace DEBUG macros by TRACE_* variants. In
most of these files, add many more tracepoints.
2007-07-17 12:36:04 +00:00
|
|
|
|
return TRACE_ERR (err);
|
2002-10-09 12:26:00 +00:00
|
|
|
|
|
|
|
|
|
if (copy)
|
|
|
|
|
{
|
|
|
|
|
char *bufcpy = malloc (size);
|
|
|
|
|
if (!bufcpy)
|
2007-07-17 Marcus Brinkmann <marcus@g10code.de>
* debug.c:;5B Include <errno.h> and "debug.h".
(_gpgme_debug): Save and restore ERRNO.
(TOHEX): New macro.
(_gpgme_debug_buffer): New function.
* conversion.c, data-compat.c, data-mem.c, data.c, engine-gpgsm.c,
gpgme.c, keylist.c, posix-io.c, rungpg.c, sign.c, version.c,
w32-io.c, wait.c: Replace DEBUG macros by TRACE_* variants. In
most of these files, add many more tracepoints.
2007-07-17 12:36:04 +00:00
|
|
|
|
{
|
2013-02-06 16:35:40 +00:00
|
|
|
|
int saved_err = gpg_error_from_syserror ();
|
2007-07-17 Marcus Brinkmann <marcus@g10code.de>
* debug.c:;5B Include <errno.h> and "debug.h".
(_gpgme_debug): Save and restore ERRNO.
(TOHEX): New macro.
(_gpgme_debug_buffer): New function.
* conversion.c, data-compat.c, data-mem.c, data.c, engine-gpgsm.c,
gpgme.c, keylist.c, posix-io.c, rungpg.c, sign.c, version.c,
w32-io.c, wait.c: Replace DEBUG macros by TRACE_* variants. In
most of these files, add many more tracepoints.
2007-07-17 12:36:04 +00:00
|
|
|
|
_gpgme_data_release (*r_dh);
|
2013-02-06 16:35:40 +00:00
|
|
|
|
return TRACE_ERR (saved_err);
|
2007-07-17 Marcus Brinkmann <marcus@g10code.de>
* debug.c:;5B Include <errno.h> and "debug.h".
(_gpgme_debug): Save and restore ERRNO.
(TOHEX): New macro.
(_gpgme_debug_buffer): New function.
* conversion.c, data-compat.c, data-mem.c, data.c, engine-gpgsm.c,
gpgme.c, keylist.c, posix-io.c, rungpg.c, sign.c, version.c,
w32-io.c, wait.c: Replace DEBUG macros by TRACE_* variants. In
most of these files, add many more tracepoints.
2007-07-17 12:36:04 +00:00
|
|
|
|
}
|
2002-10-09 12:26:00 +00:00
|
|
|
|
memcpy (bufcpy, buffer, size);
|
2007-07-17 Marcus Brinkmann <marcus@g10code.de>
* debug.c:;5B Include <errno.h> and "debug.h".
(_gpgme_debug): Save and restore ERRNO.
(TOHEX): New macro.
(_gpgme_debug_buffer): New function.
* conversion.c, data-compat.c, data-mem.c, data.c, engine-gpgsm.c,
gpgme.c, keylist.c, posix-io.c, rungpg.c, sign.c, version.c,
w32-io.c, wait.c: Replace DEBUG macros by TRACE_* variants. In
most of these files, add many more tracepoints.
2007-07-17 12:36:04 +00:00
|
|
|
|
(*r_dh)->data.mem.buffer = bufcpy;
|
2002-10-09 12:26:00 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
2007-07-17 Marcus Brinkmann <marcus@g10code.de>
* debug.c:;5B Include <errno.h> and "debug.h".
(_gpgme_debug): Save and restore ERRNO.
(TOHEX): New macro.
(_gpgme_debug_buffer): New function.
* conversion.c, data-compat.c, data-mem.c, data.c, engine-gpgsm.c,
gpgme.c, keylist.c, posix-io.c, rungpg.c, sign.c, version.c,
w32-io.c, wait.c: Replace DEBUG macros by TRACE_* variants. In
most of these files, add many more tracepoints.
2007-07-17 12:36:04 +00:00
|
|
|
|
(*r_dh)->data.mem.orig_buffer = buffer;
|
2012-09-25 13:29:49 +00:00
|
|
|
|
|
2007-07-17 Marcus Brinkmann <marcus@g10code.de>
* debug.c:;5B Include <errno.h> and "debug.h".
(_gpgme_debug): Save and restore ERRNO.
(TOHEX): New macro.
(_gpgme_debug_buffer): New function.
* conversion.c, data-compat.c, data-mem.c, data.c, engine-gpgsm.c,
gpgme.c, keylist.c, posix-io.c, rungpg.c, sign.c, version.c,
w32-io.c, wait.c: Replace DEBUG macros by TRACE_* variants. In
most of these files, add many more tracepoints.
2007-07-17 12:36:04 +00:00
|
|
|
|
(*r_dh)->data.mem.size = size;
|
|
|
|
|
(*r_dh)->data.mem.length = size;
|
2018-11-16 17:17:22 +00:00
|
|
|
|
TRACE_SUC ("dh=%p", *r_dh);
|
|
|
|
|
return 0;
|
2002-10-09 12:26:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2005-10-06 10:44:26 +00:00
|
|
|
|
/* Destroy the data buffer DH and return a pointer to its content.
|
|
|
|
|
The memory has be to released with gpgme_free() by the user. It's
|
|
|
|
|
size is returned in R_LEN. */
|
2002-10-09 12:26:00 +00:00
|
|
|
|
char *
|
2003-05-18 20:45:24 +00:00
|
|
|
|
gpgme_data_release_and_get_mem (gpgme_data_t dh, size_t *r_len)
|
2002-10-09 12:26:00 +00:00
|
|
|
|
{
|
2018-07-19 15:38:50 +00:00
|
|
|
|
gpg_error_t err;
|
2002-10-09 12:26:00 +00:00
|
|
|
|
char *str = NULL;
|
2018-07-19 15:38:50 +00:00
|
|
|
|
size_t len;
|
|
|
|
|
int blankout;
|
2002-10-09 12:26:00 +00:00
|
|
|
|
|
2018-11-16 15:25:49 +00:00
|
|
|
|
TRACE_BEG (DEBUG_DATA, "gpgme_data_release_and_get_mem", dh,
|
2007-07-17 Marcus Brinkmann <marcus@g10code.de>
* debug.c:;5B Include <errno.h> and "debug.h".
(_gpgme_debug): Save and restore ERRNO.
(TOHEX): New macro.
(_gpgme_debug_buffer): New function.
* conversion.c, data-compat.c, data-mem.c, data.c, engine-gpgsm.c,
gpgme.c, keylist.c, posix-io.c, rungpg.c, sign.c, version.c,
w32-io.c, wait.c: Replace DEBUG macros by TRACE_* variants. In
most of these files, add many more tracepoints.
2007-07-17 12:36:04 +00:00
|
|
|
|
"r_len=%p", r_len);
|
|
|
|
|
|
2002-10-09 12:26:00 +00:00
|
|
|
|
if (!dh || dh->cbs != &mem_cbs)
|
2006-09-25 14:57:00 +00:00
|
|
|
|
{
|
|
|
|
|
gpgme_data_release (dh);
|
2007-07-17 Marcus Brinkmann <marcus@g10code.de>
* debug.c:;5B Include <errno.h> and "debug.h".
(_gpgme_debug): Save and restore ERRNO.
(TOHEX): New macro.
(_gpgme_debug_buffer): New function.
* conversion.c, data-compat.c, data-mem.c, data.c, engine-gpgsm.c,
gpgme.c, keylist.c, posix-io.c, rungpg.c, sign.c, version.c,
w32-io.c, wait.c: Replace DEBUG macros by TRACE_* variants. In
most of these files, add many more tracepoints.
2007-07-17 12:36:04 +00:00
|
|
|
|
TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
|
2006-09-25 14:57:00 +00:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2002-10-09 12:26:00 +00:00
|
|
|
|
|
2018-07-19 15:38:50 +00:00
|
|
|
|
err = _gpgme_data_get_prop (dh, 0, DATA_PROP_BLANKOUT, &blankout);
|
|
|
|
|
if (err)
|
|
|
|
|
{
|
|
|
|
|
gpgme_data_release (dh);
|
|
|
|
|
TRACE_ERR (err);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2002-10-09 12:26:00 +00:00
|
|
|
|
str = dh->data.mem.buffer;
|
2018-07-19 15:38:50 +00:00
|
|
|
|
len = dh->data.mem.length;
|
|
|
|
|
if (blankout && len)
|
|
|
|
|
len = 1;
|
|
|
|
|
|
2002-10-09 12:26:00 +00:00
|
|
|
|
if (!str && dh->data.mem.orig_buffer)
|
|
|
|
|
{
|
2018-07-19 15:38:50 +00:00
|
|
|
|
str = malloc (len);
|
2002-10-09 12:26:00 +00:00
|
|
|
|
if (!str)
|
2006-09-25 14:57:00 +00:00
|
|
|
|
{
|
2013-02-06 16:35:40 +00:00
|
|
|
|
int saved_err = gpg_error_from_syserror ();
|
2006-09-25 14:57:00 +00:00
|
|
|
|
gpgme_data_release (dh);
|
2013-02-06 16:35:40 +00:00
|
|
|
|
TRACE_ERR (saved_err);
|
2006-09-25 14:57:00 +00:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2018-07-19 15:38:50 +00:00
|
|
|
|
if (blankout)
|
|
|
|
|
memset (str, 0, len);
|
|
|
|
|
else
|
|
|
|
|
memcpy (str, dh->data.mem.orig_buffer, len);
|
2002-10-09 12:26:00 +00:00
|
|
|
|
}
|
2006-09-25 14:57:00 +00:00
|
|
|
|
else
|
2018-07-19 15:38:50 +00:00
|
|
|
|
{
|
|
|
|
|
if (blankout && len)
|
|
|
|
|
*str = 0;
|
|
|
|
|
/* Prevent mem_release from releasing the buffer memory. We
|
|
|
|
|
* must not fail from this point. */
|
|
|
|
|
dh->data.mem.buffer = NULL;
|
|
|
|
|
}
|
2002-10-09 12:26:00 +00:00
|
|
|
|
|
|
|
|
|
if (r_len)
|
2018-07-19 15:38:50 +00:00
|
|
|
|
*r_len = len;
|
2002-10-09 12:26:00 +00:00
|
|
|
|
|
2006-09-25 14:57:00 +00:00
|
|
|
|
gpgme_data_release (dh);
|
|
|
|
|
|
2007-09-14 12:27:54 +00:00
|
|
|
|
if (r_len)
|
2018-11-16 17:17:22 +00:00
|
|
|
|
TRACE_SUC ("buffer=%p, len=%zu", str, *r_len);
|
2007-09-14 12:27:54 +00:00
|
|
|
|
else
|
2018-11-16 17:17:22 +00:00
|
|
|
|
TRACE_SUC ("buffer=%p", str);
|
2002-10-09 12:26:00 +00:00
|
|
|
|
return str;
|
|
|
|
|
}
|
2005-10-06 10:44:26 +00:00
|
|
|
|
|
|
|
|
|
|
2015-08-30 17:04:44 +00:00
|
|
|
|
/* Release the memory returned by gpgme_data_release_and_get_mem() and
|
|
|
|
|
some other functions. */
|
2005-10-06 10:44:26 +00:00
|
|
|
|
void
|
|
|
|
|
gpgme_free (void *buffer)
|
|
|
|
|
{
|
2019-06-05 13:48:33 +00:00
|
|
|
|
TRACE (DEBUG_DATA, "gpgme_free", NULL, "p=%p", buffer);
|
2007-07-17 Marcus Brinkmann <marcus@g10code.de>
* debug.c:;5B Include <errno.h> and "debug.h".
(_gpgme_debug): Save and restore ERRNO.
(TOHEX): New macro.
(_gpgme_debug_buffer): New function.
* conversion.c, data-compat.c, data-mem.c, data.c, engine-gpgsm.c,
gpgme.c, keylist.c, posix-io.c, rungpg.c, sign.c, version.c,
w32-io.c, wait.c: Replace DEBUG macros by TRACE_* variants. In
most of these files, add many more tracepoints.
2007-07-17 12:36:04 +00:00
|
|
|
|
|
2005-10-06 10:44:26 +00:00
|
|
|
|
if (buffer)
|
|
|
|
|
free (buffer);
|
|
|
|
|
}
|