2002-12-06 22:06:25 +00:00
|
|
|
|
/* data-stream.c - A stream based data object.
|
2018-11-16 12:27:33 +00:00
|
|
|
|
* Copyright (C) 2002, 2004 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 <stdio.h>
|
2010-11-03 09:56:27 +00:00
|
|
|
|
#ifdef HAVE_SYS_TYPES_H
|
|
|
|
|
# include <sys/types.h>
|
|
|
|
|
#endif
|
2002-10-09 12:26:00 +00:00
|
|
|
|
|
2009-10-26 17:38:39 +00:00
|
|
|
|
#include "debug.h"
|
2002-10-09 12:26:00 +00:00
|
|
|
|
#include "data.h"
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
stream_read (gpgme_data_t dh, void *buffer, size_t size)
|
2002-10-09 12:26:00 +00:00
|
|
|
|
{
|
|
|
|
|
size_t amt = fread (buffer, 1, size, dh->data.stream);
|
|
|
|
|
if (amt > 0)
|
|
|
|
|
return amt;
|
|
|
|
|
return ferror (dh->data.stream) ? -1 : 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
stream_write (gpgme_data_t dh, const void *buffer, size_t size)
|
2002-10-09 12:26:00 +00:00
|
|
|
|
{
|
|
|
|
|
size_t amt = fwrite (buffer, 1, size, dh->data.stream);
|
|
|
|
|
if (amt > 0)
|
|
|
|
|
return amt;
|
|
|
|
|
return ferror (dh->data.stream) ? -1 : 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
stream_seek (gpgme_data_t dh, gpgme_off_t offset, int whence)
|
2002-10-09 12:26:00 +00:00
|
|
|
|
{
|
2004-09-23 17:54:26 +00:00
|
|
|
|
int err;
|
|
|
|
|
|
2004-02-01 13:39:45 +00:00
|
|
|
|
#ifdef HAVE_FSEEKO
|
2004-09-23 17:54:26 +00:00
|
|
|
|
err = fseeko (dh->data.stream, offset, whence);
|
2004-02-01 13:39:45 +00:00
|
|
|
|
#else
|
|
|
|
|
/* FIXME: Check for overflow, or at least bail at compilation. */
|
2004-09-23 17:54:26 +00:00
|
|
|
|
err = fseek (dh->data.stream, offset, whence);
|
2004-02-01 13:39:45 +00:00
|
|
|
|
#endif
|
2004-09-23 17:54:26 +00:00
|
|
|
|
|
|
|
|
|
if (err)
|
|
|
|
|
return -1;
|
|
|
|
|
|
2005-08-19 16:11:46 +00:00
|
|
|
|
#ifdef HAVE_FSEEKO
|
2004-09-23 17:54:26 +00:00
|
|
|
|
return ftello (dh->data.stream);
|
2005-08-19 16:11:46 +00:00
|
|
|
|
#else
|
|
|
|
|
return ftell (dh->data.stream);
|
|
|
|
|
#endif
|
2002-10-09 12:26:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-01-18 17:59:26 +00:00
|
|
|
|
static int
|
|
|
|
|
stream_get_fd (gpgme_data_t dh)
|
|
|
|
|
{
|
2007-01-18 18:02:54 +00:00
|
|
|
|
fflush (dh->data.stream);
|
2007-01-18 17:59:26 +00:00
|
|
|
|
return fileno (dh->data.stream);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-05-18 20:45:24 +00:00
|
|
|
|
static struct _gpgme_data_cbs stream_cbs =
|
2002-10-09 12:26:00 +00:00
|
|
|
|
{
|
|
|
|
|
stream_read,
|
|
|
|
|
stream_write,
|
|
|
|
|
stream_seek,
|
2007-01-18 17:59:26 +00:00
|
|
|
|
NULL,
|
|
|
|
|
stream_get_fd
|
2002-10-09 12:26:00 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2003-05-18 20:45:24 +00:00
|
|
|
|
gpgme_error_t
|
2009-10-26 17:38:39 +00:00
|
|
|
|
gpgme_data_new_from_stream (gpgme_data_t *r_dh, FILE *stream)
|
2002-10-09 12:26:00 +00:00
|
|
|
|
{
|
2009-10-26 17:38:39 +00:00
|
|
|
|
gpgme_error_t err;
|
2018-11-16 15:25:49 +00:00
|
|
|
|
TRACE_BEG (DEBUG_DATA, "gpgme_data_new_from_stream", r_dh, "stream=%p",
|
2009-10-26 17:38:39 +00:00
|
|
|
|
stream);
|
|
|
|
|
|
|
|
|
|
err = _gpgme_data_new (r_dh, &stream_cbs);
|
2002-10-09 12:26:00 +00:00
|
|
|
|
if (err)
|
2009-10-26 17:38:39 +00:00
|
|
|
|
return TRACE_ERR (err);
|
2002-10-09 12:26:00 +00:00
|
|
|
|
|
2009-10-26 17:38:39 +00:00
|
|
|
|
(*r_dh)->data.stream = stream;
|
2018-11-16 17:17:22 +00:00
|
|
|
|
TRACE_SUC ("dh=%p", *r_dh);
|
|
|
|
|
return 0;
|
2002-10-09 12:26:00 +00:00
|
|
|
|
}
|