1 /* data-fd.c - A file descripor based data object.
2 Copyright (C) 2002, 2004 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
28 #ifdef HAVE_SYS_TYPES_H
29 # include <sys/types.h>
37 #if defined(HAVE_W32CE_SYSTEM) && !defined(__MINGW32CE__)
38 /* We need to provide replacements for read, write and lseek. They
39 are taken from the cegcc runtime files, written by Pedro Alves
40 <pedro_alves@portugalmail.pt> in Feb 2007 and placed in the public
41 domain. (cf. cegcc/src/mingw/mingwex/wince/) */
46 read (int fildes, void *buf, unsigned int bufsize)
48 DWORD NumberOfBytesRead;
49 if (bufsize > 0x7fffffff)
51 if (!ReadFile ((HANDLE) fildes, buf, bufsize, &NumberOfBytesRead, NULL))
53 return (int) NumberOfBytesRead;
57 write (int fildes, const void *buf, unsigned int bufsize)
59 DWORD NumberOfBytesWritten;
60 if (bufsize > 0x7fffffff)
62 if (!WriteFile ((HANDLE) fildes, buf, bufsize, &NumberOfBytesWritten, NULL))
64 return (int) NumberOfBytesWritten;
68 lseek (int fildes, long offset, int whence)
83 /* Specify an invalid mode so SetFilePointer catches it. */
86 return (long) SetFilePointer ((HANDLE) fildes, offset, NULL, mode);
88 #endif /*HAVE_W32CE_SYSTEM && !__MINGW32CE__*/
93 fd_read (gpgme_data_t dh, void *buffer, size_t size)
95 return read (dh->data.fd, buffer, size);
100 fd_write (gpgme_data_t dh, const void *buffer, size_t size)
102 return write (dh->data.fd, buffer, size);
107 fd_seek (gpgme_data_t dh, off_t offset, int whence)
109 return lseek (dh->data.fd, offset, whence);
114 fd_get_fd (gpgme_data_t dh)
116 return (dh->data.fd);
120 static struct _gpgme_data_cbs fd_cbs =
131 gpgme_data_new_from_fd (gpgme_data_t *r_dh, int fd)
134 TRACE_BEG1 (DEBUG_DATA, "gpgme_data_new_from_fd", r_dh, "fd=0x%x", fd);
136 err = _gpgme_data_new (r_dh, &fd_cbs);
138 return TRACE_ERR (err);
140 (*r_dh)->data.fd = fd;
141 return TRACE_SUC1 ("dh=%p", *r_dh);