1 /* data.c - An abstraction for data objects.
2 Copyright (C) 2002, 2003, 2004, 2005, 2007 g10 Code GmbH
4 This file is part of GPGME.
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.
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.
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
41 _gpgme_data_new (gpgme_data_t *r_dh, struct _gpgme_data_cbs *cbs)
46 return gpg_error (GPG_ERR_INV_VALUE);
49 dh = calloc (1, sizeof (*dh));
51 return gpg_error_from_syserror ();
61 _gpgme_data_release (gpgme_data_t dh)
72 /* Read up to SIZE bytes into buffer BUFFER from the data object with
73 the handle DH. Return the number of characters read, 0 on EOF and
74 -1 on error. If an error occurs, errno is set. */
76 gpgme_data_read (gpgme_data_t dh, void *buffer, size_t size)
79 TRACE_BEG2 (DEBUG_DATA, "gpgme_data_read", dh,
80 "buffer=%p, size=%u", buffer, size);
84 gpg_err_set_errno (EINVAL);
85 return TRACE_SYSRES (-1);
89 gpg_err_set_errno (ENOSYS);
90 return TRACE_SYSRES (-1);
93 res = (*dh->cbs->read) (dh, buffer, size);
94 while (res < 0 && errno == EINTR);
96 return TRACE_SYSRES (res);
100 /* Write up to SIZE bytes from buffer BUFFER to the data object with
101 the handle DH. Return the number of characters written, or -1 on
102 error. If an error occurs, errno is set. */
104 gpgme_data_write (gpgme_data_t dh, const void *buffer, size_t size)
107 TRACE_BEG2 (DEBUG_DATA, "gpgme_data_write", dh,
108 "buffer=%p, size=%u", buffer, size);
112 gpg_err_set_errno (EINVAL);
113 return TRACE_SYSRES (-1);
117 gpg_err_set_errno (ENOSYS);
118 return TRACE_SYSRES (-1);
121 res = (*dh->cbs->write) (dh, buffer, size);
122 while (res < 0 && errno == EINTR);
124 return TRACE_SYSRES (res);
128 /* Set the current position from where the next read or write starts
129 in the data object with the handle DH to OFFSET, relativ to
132 gpgme_data_seek (gpgme_data_t dh, off_t offset, int whence)
134 TRACE_BEG2 (DEBUG_DATA, "gpgme_data_seek", dh,
135 "offset=%lli, whence=%i", offset, whence);
139 gpg_err_set_errno (EINVAL);
140 return TRACE_SYSRES (-1);
144 gpg_err_set_errno (ENOSYS);
145 return TRACE_SYSRES (-1);
148 /* For relative movement, we must take into account the actual
149 position of the read counter. */
150 if (whence == SEEK_CUR)
151 offset -= dh->pending_len;
153 offset = (*dh->cbs->seek) (dh, offset, whence);
157 return TRACE_SYSRES (offset);
161 /* Release the data object with the handle DH. */
163 gpgme_data_release (gpgme_data_t dh)
165 TRACE (DEBUG_DATA, "gpgme_data_release", dh);
170 if (dh->cbs->release)
171 (*dh->cbs->release) (dh);
172 _gpgme_data_release (dh);
176 /* Get the current encoding meta information for the data object with
178 gpgme_data_encoding_t
179 gpgme_data_get_encoding (gpgme_data_t dh)
181 TRACE1 (DEBUG_DATA, "gpgme_data_get_encoding", dh,
182 "dh->encoding=%i", dh ? dh->encoding : GPGME_DATA_ENCODING_NONE);
183 return dh ? dh->encoding : GPGME_DATA_ENCODING_NONE;
187 /* Set the encoding meta information for the data object with handle
190 gpgme_data_set_encoding (gpgme_data_t dh, gpgme_data_encoding_t enc)
192 TRACE_BEG1 (DEBUG_DATA, "gpgme_data_set_encoding", dh,
195 return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
196 if (enc < 0 || enc > GPGME_DATA_ENCODING_URL0)
197 return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
199 return TRACE_ERR (0);
203 /* Set the file name associated with the data object with handle DH to
206 gpgme_data_set_file_name (gpgme_data_t dh, const char *file_name)
208 TRACE_BEG1 (DEBUG_DATA, "gpgme_data_set_file_name", dh,
209 "file_name=%s", file_name);
212 return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
215 free (dh->file_name);
219 dh->file_name = strdup (file_name);
221 return TRACE_ERR (gpg_error_from_syserror ());
226 return TRACE_ERR (0);
230 /* Get the file name associated with the data object with handle DH,
231 or NULL if there is none. */
233 gpgme_data_get_file_name (gpgme_data_t dh)
237 TRACE (DEBUG_DATA, "gpgme_data_get_file_name", dh);
241 TRACE1 (DEBUG_DATA, "gpgme_data_get_file_name", dh,
242 "dh->file_name=%s", dh->file_name);
243 return dh->file_name;
247 /* Functions to support the wait interface. */
250 _gpgme_data_inbound_handler (void *opaque, int fd)
252 struct io_cb_data *data = (struct io_cb_data *) opaque;
253 gpgme_data_t dh = (gpgme_data_t) data->handler_value;
254 char buffer[BUFFER_SIZE];
257 TRACE_BEG1 (DEBUG_CTX, "_gpgme_data_inbound_handler", dh,
260 buflen = _gpgme_io_read (fd, buffer, BUFFER_SIZE);
262 return gpg_error_from_syserror ();
265 _gpgme_io_close (fd);
266 return TRACE_ERR (0);
271 ssize_t amt = gpgme_data_write (dh, bufp, buflen);
272 if (amt == 0 || (amt < 0 && errno != EINTR))
273 return TRACE_ERR (gpg_error_from_syserror ());
278 return TRACE_ERR (0);
283 _gpgme_data_outbound_handler (void *opaque, int fd)
285 struct io_cb_data *data = (struct io_cb_data *) opaque;
286 gpgme_data_t dh = (gpgme_data_t) data->handler_value;
288 TRACE_BEG1 (DEBUG_CTX, "_gpgme_data_outbound_handler", dh,
291 if (!dh->pending_len)
293 ssize_t amt = gpgme_data_read (dh, dh->pending, BUFFER_SIZE);
295 return TRACE_ERR (gpg_error_from_syserror ());
298 _gpgme_io_close (fd);
299 return TRACE_ERR (0);
301 dh->pending_len = amt;
304 nwritten = _gpgme_io_write (fd, dh->pending, dh->pending_len);
305 if (nwritten == -1 && errno == EAGAIN)
306 return TRACE_ERR (0);
308 if (nwritten == -1 && errno == EPIPE)
310 /* Not much we can do. The other end closed the pipe, but we
311 still have data. This should only ever happen if the other
312 end is going to tell us what happened on some other channel.
313 Silently close our end. */
314 _gpgme_io_close (fd);
315 return TRACE_ERR (0);
319 return TRACE_ERR (gpg_error_from_syserror ());
321 if (nwritten < dh->pending_len)
322 memmove (dh->pending, dh->pending + nwritten, dh->pending_len - nwritten);
323 dh->pending_len -= nwritten;
324 return TRACE_ERR (0);
328 /* Get the file descriptor associated with DH, if possible. Otherwise
331 _gpgme_data_get_fd (gpgme_data_t dh)
333 if (!dh || !dh->cbs->get_fd)
335 return (*dh->cbs->get_fd) (dh);