Line data Source code
1 : /* data-stream.c - A stream based data object.
2 : * Copyright (C) 2002, 2004, 2018 g10 Code GmbH
3 : *
4 : * This file is part of GPGME.
5 : *
6 : * GPGME is free software; you can redistribute it and/or modify it
7 : * under the terms of the GNU Lesser General Public License as
8 : * published by the Free Software Foundation; either version 2.1 of
9 : * the License, or (at your option) any later version.
10 : *
11 : * GPGME is distributed in the hope that it will be useful, but
12 : * WITHOUT ANY WARRANTY; without even the implied warranty of
13 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 : * Lesser General Public License for more details.
15 : *
16 : * You should have received a copy of the GNU Lesser General Public
17 : * License along with this program; if not, see <https://www.gnu.org/licenses/>.
18 : * SPDX-License-Identifier: LGPL-2.1+
19 : */
20 :
21 : #if HAVE_CONFIG_H
22 : #include <config.h>
23 : #endif
24 :
25 : #include <stdio.h>
26 : #ifdef HAVE_SYS_TYPES_H
27 : # include <sys/types.h>
28 : #endif
29 :
30 : #include "debug.h"
31 : #include "data.h"
32 :
33 :
34 : static gpgme_ssize_t
35 0 : stream_es_read (gpgme_data_t dh, void *buffer, size_t size)
36 : {
37 0 : size_t amt = gpgrt_fread (buffer, 1, size, dh->data.e_stream);
38 0 : if (amt > 0)
39 0 : return amt;
40 0 : return gpgrt_ferror (dh->data.e_stream) ? -1 : 0;
41 : }
42 :
43 :
44 : static gpgme_ssize_t
45 0 : stream_es_write (gpgme_data_t dh, const void *buffer, size_t size)
46 : {
47 0 : size_t amt = gpgrt_fwrite (buffer, 1, size, dh->data.e_stream);
48 0 : if (amt > 0)
49 0 : return amt;
50 0 : return gpgrt_ferror (dh->data.e_stream) ? -1 : 0;
51 : }
52 :
53 :
54 : static gpgme_off_t
55 0 : stream_es_seek (gpgme_data_t dh, gpgme_off_t offset, int whence)
56 : {
57 : int err;
58 :
59 0 : err = gpgrt_fseeko (dh->data.e_stream, offset, whence);
60 0 : if (err)
61 0 : return -1;
62 :
63 0 : return gpgrt_ftello (dh->data.e_stream);
64 : }
65 :
66 :
67 : static int
68 0 : stream_es_get_fd (gpgme_data_t dh)
69 : {
70 0 : gpgrt_fflush (dh->data.e_stream);
71 0 : return gpgrt_fileno (dh->data.e_stream);
72 : }
73 :
74 :
75 : static struct _gpgme_data_cbs stream_es_cbs =
76 : {
77 : stream_es_read,
78 : stream_es_write,
79 : stream_es_seek,
80 : NULL,
81 : stream_es_get_fd
82 : };
83 :
84 :
85 :
86 : gpgme_error_t
87 0 : gpgme_data_new_from_estream (gpgme_data_t *r_dh, gpgrt_stream_t stream)
88 : {
89 : gpgme_error_t err;
90 0 : TRACE_BEG1 (DEBUG_DATA, "gpgme_data_new_from_estream", r_dh, "estream=%p",
91 : stream);
92 :
93 0 : err = _gpgme_data_new (r_dh, &stream_es_cbs);
94 0 : if (err)
95 0 : return TRACE_ERR (err);
96 :
97 0 : (*r_dh)->data.e_stream = stream;
98 0 : return TRACE_SUC1 ("dh=%p", *r_dh);
99 : }
|