1 /* data-compat.c - Compatibility interfaces for data objects.
2 Copyright (C) 2002, 2003, 2004, 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
26 #ifdef HAVE_SYS_TIME_H
27 # include <sys/time.h>
29 #ifdef HAVE_SYS_STAT_H
30 # include <sys/stat.h>
39 /* Create a new data buffer filled with LENGTH bytes starting from
40 OFFSET within the file FNAME or stream STREAM (exactly one must be
43 gpgme_data_new_from_filepart (gpgme_data_t *r_dh, const char *fname,
44 FILE *stream, off_t offset, size_t length)
46 #if defined (HAVE_W32CE_SYSTEM) && defined (_MSC_VER)
47 return gpgme_error (GPG_ERR_NOT_IMPLEMENTED);
53 TRACE_BEG4 (DEBUG_DATA, "gpgme_data_new_from_filepart", r_dh,
54 "file_name=%s, stream=%p, offset=%lli, length=%u",
55 fname, stream, offset, length);
58 return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
61 stream = fopen (fname, "rb");
63 return TRACE_ERR (gpg_error_from_errno (errno));
66 res = fseeko (stream, offset, SEEK_SET);
68 /* FIXME: Check for overflow, or at least bail at compilation. */
69 res = fseek (stream, offset, SEEK_SET);
74 int saved_errno = errno;
77 return TRACE_ERR (gpg_error_from_errno (saved_errno));
80 buf = malloc (length);
83 int saved_errno = errno;
86 return TRACE_ERR (gpg_error_from_errno (saved_errno));
89 while (fread (buf, length, 1, stream) < 1
90 && ferror (stream) && errno == EINTR);
93 int saved_errno = errno;
98 return TRACE_ERR (gpg_error_from_errno (saved_errno));
104 err = gpgme_data_new (r_dh);
112 (*r_dh)->data.mem.buffer = buf;
113 (*r_dh)->data.mem.size = length;
114 (*r_dh)->data.mem.length = length;
116 return TRACE_SUC1 ("r_dh=%p", *r_dh);
121 /* Create a new data buffer filled with the content of file FNAME.
122 COPY must be non-zero (delayed reads are not supported yet). */
124 gpgme_data_new_from_file (gpgme_data_t *r_dh, const char *fname, int copy)
126 #if defined (HAVE_W32CE_SYSTEM) && defined (_MSC_VER)
127 return gpgme_error (GPG_ERR_NOT_IMPLEMENTED);
131 TRACE_BEG3 (DEBUG_DATA, "gpgme_data_new_from_filepart", r_dh,
132 "file_name=%s, copy=%i (%s)", fname, copy, copy ? "yes" : "no");
135 return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
137 if (stat (fname, &statbuf) < 0)
138 return TRACE_ERR (gpg_error_from_errno (errno));
140 err = gpgme_data_new_from_filepart (r_dh, fname, NULL, 0, statbuf.st_size);
141 return TRACE_ERR (err);
147 gpgme_error_to_errno (gpgme_error_t err)
149 int res = gpg_err_code_to_errno (err);
153 switch (gpg_err_code (err))
158 case GPG_ERR_INV_VALUE:
161 case GPG_ERR_NOT_SUPPORTED:
165 /* FIXME: Yeah, well. */
170 TRACE3 (DEBUG_DATA, "gpgme:gpgme_error_to_errno", 0,
171 "mapping %s <%s> to: %s", gpgme_strerror (err),
172 gpgme_strsource (err), strerror (res));
173 gpg_err_set_errno (res);
179 old_user_read (gpgme_data_t dh, void *buffer, size_t size)
183 TRACE_BEG2 (DEBUG_DATA, "gpgme:old_user_read", dh,
184 "buffer=%p, size=%u", buffer, size);
186 err = (*dh->data.old_user.cb) (dh->data.old_user.handle,
189 return TRACE_SYSRES (gpgme_error_to_errno (err));
190 return TRACE_SYSRES (amt);
195 old_user_seek (gpgme_data_t dh, off_t offset, int whence)
198 TRACE_BEG2 (DEBUG_DATA, "gpgme:old_user_seek", dh,
199 "offset=%llu, whence=%i", offset, whence);
201 if (whence != SEEK_SET || offset)
203 gpg_err_set_errno (EINVAL);
204 return TRACE_SYSRES (-1);
206 err = (*dh->data.old_user.cb) (dh->data.old_user.handle, NULL, 0, NULL);
208 return TRACE_SYSRES (gpgme_error_to_errno (err));
209 return TRACE_SYSRES (0);
213 static struct _gpgme_data_cbs old_user_cbs =
222 /* Create a new data buffer which retrieves the data from the callback
225 gpgme_data_new_with_read_cb (gpgme_data_t *r_dh,
226 int (*read_cb) (void *, char *, size_t, size_t *),
230 TRACE_BEG2 (DEBUG_DATA, "gpgme_data_new_with_read_cb", r_dh,
231 "read_cb=%p/%p", read_cb, read_cb_value);
233 err = _gpgme_data_new (r_dh, &old_user_cbs);
236 return TRACE_ERR (err);
238 (*r_dh)->data.old_user.cb = read_cb;
239 (*r_dh)->data.old_user.handle = read_cb_value;
240 return TRACE_ERR (0);
245 gpgme_data_rewind (gpgme_data_t dh)
248 TRACE_BEG (DEBUG_DATA, "gpgme_data_rewind", dh);
250 err = (gpgme_data_seek (dh, 0, SEEK_SET) == -1)
251 ? gpg_error_from_errno (errno) : 0;
253 return TRACE_ERR (err);