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, see <https://gnu.org/licenses/>.
18 * SPDX-License-Identifier: LGPL-2.1-or-later
41 /* The property table which has an entry for each active data object.
42 * The data object itself uses an index into this table and the table
43 * has a pointer back to the data object. All access to that table is
44 * controlled by the property_table_lock.
46 * We use a separate table instead of linking all data objects
47 * together for faster locating properties of the data object using
48 * the data objects serial number. We use 64 bit for the serial
49 * number which is good enough to create a new data object every
50 * nanosecond for more than 500 years. Thus no wrap around will ever
55 gpgme_data_t dh; /* The data objcet or NULL if the slot is not used. */
56 uint64_t dserial; /* The serial number of the data object. */
58 unsigned int blankout : 1; /* Void the held data. */
61 typedef struct property_s *property_t;
63 static property_t property_table;
64 static unsigned int property_table_size;
65 DEFINE_STATIC_LOCK (property_table_lock);
66 #define PROPERTY_TABLE_ALLOCATION_CHUNK 32
70 /* Insert the newly created data object DH into the property table and
71 * store the index of it at R_IDX. An error code is returned on error
72 * and the table is not changed. */
74 insert_into_property_table (gpgme_data_t dh, unsigned int *r_idx)
76 static uint64_t last_dserial;
80 LOCK (property_table_lock);
83 property_table_size = PROPERTY_TABLE_ALLOCATION_CHUNK;
84 property_table = calloc (property_table_size, sizeof *property_table);
87 err = gpg_error_from_syserror ();
92 /* Find an empty slot. */
93 for (idx = 0; idx < property_table_size; idx++)
94 if (!property_table[idx].dh)
96 if (!(idx < property_table_size))
98 /* No empty slot found. Enlarge the table. */
100 unsigned int newsize;
102 newsize = property_table_size + PROPERTY_TABLE_ALLOCATION_CHUNK;;
103 if ((newsize * sizeof *property_table)
104 < (property_table_size * sizeof *property_table))
106 err = gpg_error (GPG_ERR_ENOMEM);
109 newtbl = realloc (property_table, newsize * sizeof *property_table);
112 err = gpg_error_from_syserror ();
115 property_table = newtbl;
116 for (idx = property_table_size; idx < newsize; idx++)
117 property_table[idx].dh = NULL;
118 idx = property_table_size;
119 property_table_size = newsize;
123 property_table[idx].dh = dh;
124 property_table[idx].dserial = ++last_dserial;
125 memset (&property_table[idx].flags, 0, sizeof property_table[idx].flags);
130 UNLOCK (property_table_lock);
135 /* Remove the data object at PROPIDX from the table. DH is only used
136 * for cross checking. */
138 remove_from_property_table (gpgme_data_t dh, unsigned int propidx)
140 LOCK (property_table_lock);
141 assert (property_table);
142 assert (propidx < property_table_size);
143 assert (property_table[propidx].dh == dh);
144 property_table[propidx].dh = NULL;
145 UNLOCK (property_table_lock);
149 /* Return the data object's serial number for handle DH. This is a
150 * unique serial number for each created data object. */
152 _gpgme_data_get_dserial (gpgme_data_t dh)
161 LOCK (property_table_lock);
162 assert (property_table);
163 assert (idx < property_table_size);
164 assert (property_table[idx].dh == dh);
165 dserial = property_table[idx].dserial;
166 UNLOCK (property_table_lock);
172 /* Set an internal property of a data object. The data object may
173 * either be identified by the usual DH or by using the data serial
176 _gpgme_data_set_prop (gpgme_data_t dh, uint64_t dserial,
177 data_prop_t name, int value)
181 TRACE_BEG (DEBUG_DATA, "gpgme_data_set_prop", dh,
182 "dserial=%llu %lu=%d",
183 (unsigned long long)dserial,
184 (unsigned long)name, value);
186 LOCK (property_table_lock);
187 if ((!dh && !dserial) || (dh && dserial))
189 err = gpg_error (GPG_ERR_INV_VALUE);
192 if (dh) /* Lookup via handle. */
195 assert (property_table);
196 assert (idx < property_table_size);
197 assert (property_table[idx].dh == dh);
199 else /* Lookup via DSERIAL. */
203 err = gpg_error (GPG_ERR_NOT_FOUND);
206 for (idx = 0; idx < property_table_size; idx++)
207 if (property_table[idx].dh && property_table[idx].dserial == dserial)
209 if (!(idx < property_table_size))
211 err = gpg_error (GPG_ERR_NOT_FOUND);
218 case DATA_PROP_NONE: /* Nothing to to do. */
220 case DATA_PROP_BLANKOUT:
221 property_table[idx].flags.blankout = !!value;
225 err = gpg_error (GPG_ERR_UNKNOWN_NAME);
230 UNLOCK (property_table_lock);
231 return TRACE_ERR (err);
235 /* Get an internal property of a data object. This is the counter
236 * part to _gpgme_data_set_property. The value of the property is
237 * stored at R_VALUE. On error 0 is stored at R_VALUE. */
239 _gpgme_data_get_prop (gpgme_data_t dh, uint64_t dserial,
240 data_prop_t name, int *r_value)
244 TRACE_BEG (DEBUG_DATA, "gpgme_data_get_prop", dh,
246 (unsigned long long)dserial,
247 (unsigned long)name);
251 LOCK (property_table_lock);
252 if ((!dh && !dserial) || (dh && dserial))
254 err = gpg_error (GPG_ERR_INV_VALUE);
257 if (dh) /* Lookup via handle. */
260 assert (property_table);
261 assert (idx < property_table_size);
262 assert (property_table[idx].dh == dh);
264 else /* Lookup via DSERIAL. */
268 err = gpg_error (GPG_ERR_NOT_FOUND);
271 for (idx = 0; idx < property_table_size; idx++)
272 if (property_table[idx].dh && property_table[idx].dserial == dserial)
274 if (!(idx < property_table_size))
276 err = gpg_error (GPG_ERR_NOT_FOUND);
283 case DATA_PROP_NONE: /* Nothing to to do. */
285 case DATA_PROP_BLANKOUT:
286 *r_value = property_table[idx].flags.blankout;
290 err = gpg_error (GPG_ERR_UNKNOWN_NAME);
295 UNLOCK (property_table_lock);
296 return TRACE_ERR (err);
302 _gpgme_data_new (gpgme_data_t *r_dh, struct _gpgme_data_cbs *cbs)
308 return gpg_error (GPG_ERR_INV_VALUE);
313 return _gpgme_selftest;
315 dh = calloc (1, sizeof (*dh));
317 return gpg_error_from_syserror ();
321 err = insert_into_property_table (dh, &dh->propidx);
334 _gpgme_data_release (gpgme_data_t dh)
339 remove_from_property_table (dh, dh->propidx);
341 free (dh->file_name);
347 /* Read up to SIZE bytes into buffer BUFFER from the data object with
348 the handle DH. Return the number of characters read, 0 on EOF and
349 -1 on error. If an error occurs, errno is set. */
351 gpgme_data_read (gpgme_data_t dh, void *buffer, size_t size)
355 TRACE_BEG (DEBUG_DATA, "gpgme_data_read", dh,
356 "buffer=%p, size=%zu", buffer, size);
360 gpg_err_set_errno (EINVAL);
361 return TRACE_SYSRES (-1);
365 gpg_err_set_errno (ENOSYS);
366 return TRACE_SYSRES (-1);
369 if (_gpgme_data_get_prop (dh, 0, DATA_PROP_BLANKOUT, &blankout)
375 res = (*dh->cbs->read) (dh, buffer, size);
376 while (res < 0 && errno == EINTR);
379 return TRACE_SYSRES ((int)res);
383 /* Write up to SIZE bytes from buffer BUFFER to the data object with
384 the handle DH. Return the number of characters written, or -1 on
385 error. If an error occurs, errno is set. */
387 gpgme_data_write (gpgme_data_t dh, const void *buffer, size_t size)
390 TRACE_BEG (DEBUG_DATA, "gpgme_data_write", dh,
391 "buffer=%p, size=%zu", buffer, size);
395 gpg_err_set_errno (EINVAL);
396 return TRACE_SYSRES (-1);
400 gpg_err_set_errno (ENOSYS);
401 return TRACE_SYSRES (-1);
404 res = (*dh->cbs->write) (dh, buffer, size);
405 while (res < 0 && errno == EINTR);
407 return TRACE_SYSRES ((int)res);
411 /* Set the current position from where the next read or write starts
412 in the data object with the handle DH to OFFSET, relative to
415 gpgme_data_seek (gpgme_data_t dh, gpgme_off_t offset, int whence)
417 TRACE_BEG (DEBUG_DATA, "gpgme_data_seek", dh,
418 "offset=%lli, whence=%i", (long long int)offset, whence);
422 gpg_err_set_errno (EINVAL);
423 return TRACE_SYSRES (-1);
427 gpg_err_set_errno (ENOSYS);
428 return TRACE_SYSRES (-1);
431 /* For relative movement, we must take into account the actual
432 position of the read counter. */
433 if (whence == SEEK_CUR)
434 offset -= dh->pending_len;
436 offset = (*dh->cbs->seek) (dh, offset, whence);
440 return TRACE_SYSRES ((int)offset);
444 /* Convenience function to do a gpgme_data_seek (dh, 0, SEEK_SET). */
446 gpgme_data_rewind (gpgme_data_t dh)
449 TRACE_BEG (DEBUG_DATA, "gpgme_data_rewind", dh, "");
451 err = ((gpgme_data_seek (dh, 0, SEEK_SET) == -1)
452 ? gpg_error_from_syserror () : 0);
454 return TRACE_ERR (err);
458 /* Release the data object with the handle DH. */
460 gpgme_data_release (gpgme_data_t dh)
462 TRACE (DEBUG_DATA, "gpgme_data_release", dh, "");
467 if (dh->cbs->release)
468 (*dh->cbs->release) (dh);
469 _gpgme_data_release (dh);
473 /* Get the current encoding meta information for the data object with
475 gpgme_data_encoding_t
476 gpgme_data_get_encoding (gpgme_data_t dh)
478 TRACE (DEBUG_DATA, "gpgme_data_get_encoding", dh,
479 "dh->encoding=%i", dh ? dh->encoding : GPGME_DATA_ENCODING_NONE);
480 return dh ? dh->encoding : GPGME_DATA_ENCODING_NONE;
484 /* Set the encoding meta information for the data object with handle
487 gpgme_data_set_encoding (gpgme_data_t dh, gpgme_data_encoding_t enc)
489 TRACE_BEG (DEBUG_DATA, "gpgme_data_set_encoding", dh,
492 return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
493 if (enc < 0 || enc > GPGME_DATA_ENCODING_MIME)
494 return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
496 return TRACE_ERR (0);
500 /* Set the file name associated with the data object with handle DH to
503 gpgme_data_set_file_name (gpgme_data_t dh, const char *file_name)
505 TRACE_BEG (DEBUG_DATA, "gpgme_data_set_file_name", dh,
506 "file_name=%s", file_name);
509 return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
512 free (dh->file_name);
516 dh->file_name = strdup (file_name);
518 return TRACE_ERR (gpg_error_from_syserror ());
523 return TRACE_ERR (0);
527 /* Get the file name associated with the data object with handle DH,
528 or NULL if there is none. */
530 gpgme_data_get_file_name (gpgme_data_t dh)
534 TRACE (DEBUG_DATA, "gpgme_data_get_file_name", dh, "");
538 TRACE (DEBUG_DATA, "gpgme_data_get_file_name", dh,
539 "dh->file_name=%s", dh->file_name);
540 return dh->file_name;
544 /* Set a flag for the data object DH. See the manual for details. */
546 gpgme_data_set_flag (gpgme_data_t dh, const char *name, const char *value)
548 TRACE_BEG (DEBUG_DATA, "gpgme_data_set_flag", dh,
549 "%s=%s", name, value);
552 return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
554 if (!strcmp (name, "size-hint"))
556 dh->size_hint= value? _gpgme_string_to_off (value) : 0;
559 return gpg_error (GPG_ERR_UNKNOWN_NAME);
566 /* Functions to support the wait interface. */
569 _gpgme_data_inbound_handler (void *opaque, int fd)
571 struct io_cb_data *data = (struct io_cb_data *) opaque;
572 gpgme_data_t dh = (gpgme_data_t) data->handler_value;
573 char buffer[BUFFER_SIZE];
575 gpgme_ssize_t buflen;
576 TRACE_BEG (DEBUG_CTX, "_gpgme_data_inbound_handler", dh,
579 buflen = _gpgme_io_read (fd, buffer, BUFFER_SIZE);
581 return gpg_error_from_syserror ();
584 _gpgme_io_close (fd);
585 return TRACE_ERR (0);
590 gpgme_ssize_t amt = gpgme_data_write (dh, bufp, buflen);
591 if (amt == 0 || (amt < 0 && errno != EINTR))
592 return TRACE_ERR (gpg_error_from_syserror ());
597 return TRACE_ERR (0);
602 _gpgme_data_outbound_handler (void *opaque, int fd)
604 struct io_cb_data *data = (struct io_cb_data *) opaque;
605 gpgme_data_t dh = (gpgme_data_t) data->handler_value;
606 gpgme_ssize_t nwritten;
607 TRACE_BEG (DEBUG_CTX, "_gpgme_data_outbound_handler", dh,
610 if (!dh->pending_len)
612 gpgme_ssize_t amt = gpgme_data_read (dh, dh->pending, BUFFER_SIZE);
614 return TRACE_ERR (gpg_error_from_syserror ());
617 _gpgme_io_close (fd);
618 return TRACE_ERR (0);
620 dh->pending_len = amt;
623 nwritten = _gpgme_io_write (fd, dh->pending, dh->pending_len);
624 if (nwritten == -1 && errno == EAGAIN)
625 return TRACE_ERR (0);
627 if (nwritten == -1 && errno == EPIPE)
629 /* Not much we can do. The other end closed the pipe, but we
630 still have data. This should only ever happen if the other
631 end is going to tell us what happened on some other channel.
632 Silently close our end. */
633 _gpgme_io_close (fd);
634 return TRACE_ERR (0);
638 return TRACE_ERR (gpg_error_from_syserror ());
640 if (nwritten < dh->pending_len)
641 memmove (dh->pending, dh->pending + nwritten, dh->pending_len - nwritten);
642 dh->pending_len -= nwritten;
643 return TRACE_ERR (0);
647 /* Get the file descriptor associated with DH, if possible. Otherwise
650 _gpgme_data_get_fd (gpgme_data_t dh)
652 if (!dh || !dh->cbs->get_fd)
654 return (*dh->cbs->get_fd) (dh);
658 /* Get the size-hint value for DH or 0 if not available. */
660 _gpgme_data_get_size_hint (gpgme_data_t dh)
662 return dh ? dh->size_hint : 0;