Line data Source code
1 : /* data-user.c - A user callback based data object.
2 : Copyright (C) 2002, 2004 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, write to the Free Software
18 : Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 : 02111-1307, USA. */
20 :
21 : #if HAVE_CONFIG_H
22 : #include <config.h>
23 : #endif
24 :
25 : #ifdef HAVE_SYS_TYPES_H
26 : # include <sys/types.h>
27 : #endif
28 : #include <errno.h>
29 :
30 : #include "debug.h"
31 : #include "data.h"
32 :
33 :
34 : static gpgme_ssize_t
35 26 : user_read (gpgme_data_t dh, void *buffer, size_t size)
36 : {
37 26 : if (!dh->data.user.cbs->read)
38 : {
39 0 : gpg_err_set_errno (EBADF);
40 0 : return -1;
41 : }
42 :
43 26 : return (*dh->data.user.cbs->read) (dh->data.user.handle, buffer, size);
44 : }
45 :
46 :
47 : static gpgme_ssize_t
48 26 : user_write (gpgme_data_t dh, const void *buffer, size_t size)
49 : {
50 26 : if (!dh->data.user.cbs->write)
51 : {
52 0 : gpg_err_set_errno (EBADF);
53 0 : return -1;
54 : }
55 :
56 26 : return (*dh->data.user.cbs->write) (dh->data.user.handle, buffer, size);
57 : }
58 :
59 :
60 : static gpgme_off_t
61 0 : user_seek (gpgme_data_t dh, gpgme_off_t offset, int whence)
62 : {
63 0 : if (!dh->data.user.cbs->seek)
64 : {
65 0 : gpg_err_set_errno (EBADF);
66 0 : return -1;
67 : }
68 :
69 0 : return (*dh->data.user.cbs->seek) (dh->data.user.handle, offset, whence);
70 : }
71 :
72 :
73 : static void
74 2 : user_release (gpgme_data_t dh)
75 : {
76 2 : if (dh->data.user.cbs->release)
77 0 : (*dh->data.user.cbs->release) (dh->data.user.handle);
78 2 : }
79 :
80 :
81 : static struct _gpgme_data_cbs user_cbs =
82 : {
83 : user_read,
84 : user_write,
85 : user_seek,
86 : user_release,
87 : NULL
88 : };
89 :
90 :
91 : gpgme_error_t
92 2 : gpgme_data_new_from_cbs (gpgme_data_t *r_dh, gpgme_data_cbs_t cbs, void *handle)
93 : {
94 : gpgme_error_t err;
95 2 : TRACE_BEG1 (DEBUG_DATA, "gpgme_data_new_from_cbs", r_dh, "handle=%p", handle);
96 :
97 2 : err = _gpgme_data_new (r_dh, &user_cbs);
98 2 : if (err)
99 0 : return TRACE_ERR (err);
100 :
101 2 : (*r_dh)->data.user.cbs = cbs;
102 2 : (*r_dh)->data.user.handle = handle;
103 2 : return TRACE_SUC1 ("dh=%p", *r_dh);
104 : }
|