Fixes for the MSC build
[gpgme.git] / src / data-compat.c
1 /* data-compat.c - Compatibility interfaces for data objects.
2    Copyright (C) 2002, 2003, 2004, 2007 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 #include <errno.h>
26 #ifdef HAVE_SYS_TIME_H
27 # include <sys/time.h>
28 #endif
29 #ifdef HAVE_SYS_STAT_H
30 # include <sys/stat.h>
31 #endif
32 #include <stdlib.h>
33
34 #include "data.h"
35 #include "util.h"
36 #include "debug.h"
37
38 \f
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
41    non-zero).  */
42 gpgme_error_t
43 gpgme_data_new_from_filepart (gpgme_data_t *r_dh, const char *fname,
44                               FILE *stream, off_t offset, size_t length)
45 {
46 #if defined (HAVE_W32CE_SYSTEM) && defined (_MSC_VER)
47   return gpgme_error (GPG_ERR_NOT_IMPLEMENTED);
48 #else
49   gpgme_error_t err;
50   char *buf = NULL;
51   int res;
52
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);
56
57   if (stream && fname)
58     return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
59
60   if (fname)
61     stream = fopen (fname, "rb");
62   if (!stream)
63     return TRACE_ERR (gpg_error_from_errno (errno));
64
65 #ifdef HAVE_FSEEKO
66   res = fseeko (stream, offset, SEEK_SET);
67 #else
68   /* FIXME: Check for overflow, or at least bail at compilation.  */
69   res = fseek (stream, offset, SEEK_SET);
70 #endif
71
72   if (res)
73     {
74       int saved_errno = errno;
75       if (fname)
76         fclose (stream);
77       return TRACE_ERR (gpg_error_from_errno (saved_errno));
78     }
79
80   buf = malloc (length);
81   if (!buf)
82     {
83       int saved_errno = errno;
84       if (fname)
85         fclose (stream);
86       return TRACE_ERR (gpg_error_from_errno (saved_errno));
87     }
88
89   while (fread (buf, length, 1, stream) < 1
90          && ferror (stream) && errno == EINTR);
91   if (ferror (stream))
92     {
93       int saved_errno = errno;
94       if (buf)
95         free (buf);
96       if (fname)
97         fclose (stream);
98       return TRACE_ERR (gpg_error_from_errno (saved_errno));
99     }
100
101   if (fname)
102     fclose (stream);
103
104   err = gpgme_data_new (r_dh);
105   if (err)
106     {
107       if (buf)
108         free (buf);
109       return err;
110     }
111
112   (*r_dh)->data.mem.buffer = buf;
113   (*r_dh)->data.mem.size = length;
114   (*r_dh)->data.mem.length = length;
115
116   return TRACE_SUC1 ("r_dh=%p", *r_dh);
117 #endif
118 }
119
120 \f
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).  */
123 gpgme_error_t
124 gpgme_data_new_from_file (gpgme_data_t *r_dh, const char *fname, int copy)
125 {
126 #if defined (HAVE_W32CE_SYSTEM) && defined (_MSC_VER)
127   return gpgme_error (GPG_ERR_NOT_IMPLEMENTED);
128 #else
129   gpgme_error_t err;
130   struct stat statbuf;
131   TRACE_BEG3 (DEBUG_DATA, "gpgme_data_new_from_filepart", r_dh,
132               "file_name=%s, copy=%i (%s)", fname, copy, copy ? "yes" : "no");
133
134   if (!fname || !copy)
135     return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
136
137   if (stat (fname, &statbuf) < 0)
138     return TRACE_ERR (gpg_error_from_errno (errno));
139
140   err = gpgme_data_new_from_filepart (r_dh, fname, NULL, 0, statbuf.st_size);
141   return TRACE_ERR (err);
142 #endif
143 }
144
145 \f
146 static int
147 gpgme_error_to_errno (gpgme_error_t err)
148 {
149   int res = gpg_err_code_to_errno (err);
150
151   if (!err)
152     {
153       switch (gpg_err_code (err))
154         {
155         case GPG_ERR_EOF:
156           res = 0;
157           break;
158         case GPG_ERR_INV_VALUE:
159           res = EINVAL;
160           break;
161         case GPG_ERR_NOT_SUPPORTED:
162           res = ENOSYS;
163           break;
164         default:
165           /* FIXME: Yeah, well.  */
166           res = EINVAL;
167           break;
168         }
169     }
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);
174   return res ? -1 : 0;
175 }
176
177
178 static ssize_t
179 old_user_read (gpgme_data_t dh, void *buffer, size_t size)
180 {
181   gpgme_error_t err;
182   size_t amt;
183   TRACE_BEG2 (DEBUG_DATA, "gpgme:old_user_read", dh,
184               "buffer=%p, size=%u", buffer, size);
185
186   err = (*dh->data.old_user.cb) (dh->data.old_user.handle,
187                                  buffer, size, &amt);
188   if (err)
189     return TRACE_SYSRES (gpgme_error_to_errno (err));
190   return TRACE_SYSRES (amt);
191 }
192
193
194 static off_t
195 old_user_seek (gpgme_data_t dh, off_t offset, int whence)
196 {
197   gpgme_error_t err;
198   TRACE_BEG2 (DEBUG_DATA, "gpgme:old_user_seek", dh,
199               "offset=%llu, whence=%i", offset, whence);
200
201   if (whence != SEEK_SET || offset)
202     {
203       gpg_err_set_errno (EINVAL);
204       return TRACE_SYSRES (-1);
205     }
206   err = (*dh->data.old_user.cb) (dh->data.old_user.handle, NULL, 0, NULL);
207   if (err)
208     return TRACE_SYSRES (gpgme_error_to_errno (err));
209   return TRACE_SYSRES (0);
210 }
211
212
213 static struct _gpgme_data_cbs old_user_cbs =
214   {
215     old_user_read,
216     NULL,
217     old_user_seek,
218     NULL
219   };
220
221
222 /* Create a new data buffer which retrieves the data from the callback
223    function READ_CB.  */
224 gpgme_error_t
225 gpgme_data_new_with_read_cb (gpgme_data_t *r_dh,
226                              int (*read_cb) (void *, char *, size_t, size_t *),
227                              void *read_cb_value)
228 {
229   gpgme_error_t err;
230   TRACE_BEG2 (DEBUG_DATA, "gpgme_data_new_with_read_cb", r_dh,
231               "read_cb=%p/%p", read_cb, read_cb_value);
232
233   err = _gpgme_data_new (r_dh, &old_user_cbs);
234
235   if (err)
236     return TRACE_ERR (err);
237
238   (*r_dh)->data.old_user.cb = read_cb;
239   (*r_dh)->data.old_user.handle = read_cb_value;
240   return TRACE_ERR (0);
241 }
242
243 \f
244 gpgme_error_t
245 gpgme_data_rewind (gpgme_data_t dh)
246 {
247   gpgme_error_t err;
248   TRACE_BEG (DEBUG_DATA, "gpgme_data_rewind", dh);
249
250   err = (gpgme_data_seek (dh, 0, SEEK_SET) == -1)
251     ? gpg_error_from_errno (errno) : 0;
252
253   return TRACE_ERR (err);
254 }