Might now build for CE using MSC.
[gpgme.git] / src / data-fd.c
1 /* data-fd.c - A file descripor based data object.
2    Copyright (C) 2002, 2004 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 #ifdef HAVE_UNISTD_H
26 # include <unistd.h>
27 #endif
28 #include <sys/types.h>
29
30 #include "debug.h"
31 #include "data.h"
32
33
34 \f
35 #if defined(HAVE_W32CE_SYSTEM) && !defined(__MINGW32CE__)
36 /* We need to provide replacements for read, write and lseek.  They
37    are taken from the cegcc runtime files, written by Pedro Alves
38    <pedro_alves@portugalmail.pt> in Feb 2007 and placed in the public
39    domain. (cf. cegcc/src/mingw/mingwex/wince/)  */
40
41 #include <windows.h>
42
43 static int
44 read (int fildes, void *buf, unsigned int bufsize)
45 {
46   DWORD NumberOfBytesRead;
47   if (bufsize > 0x7fffffff)
48     bufsize = 0x7fffffff;
49   if (!ReadFile ((HANDLE) fildes, buf, bufsize, &NumberOfBytesRead, NULL))
50     return -1;
51   return (int) NumberOfBytesRead;
52 }
53
54 static int
55 write (int fildes, const void *buf, unsigned int bufsize)
56 {
57   DWORD NumberOfBytesWritten;
58   if (bufsize > 0x7fffffff)
59     bufsize = 0x7fffffff;
60   if (!WriteFile ((HANDLE) fildes, buf, bufsize, &NumberOfBytesWritten, NULL))
61     return -1;
62   return (int) NumberOfBytesWritten;
63 }
64
65 static long
66 lseek (int fildes, long offset, int whence)
67 {
68   DWORD mode;
69   switch (whence)
70     {
71     case SEEK_SET:
72       mode = FILE_BEGIN;
73       break;
74     case SEEK_CUR:
75       mode = FILE_CURRENT;
76       break;
77     case SEEK_END:
78       mode = FILE_END;
79       break;
80     default:
81       /* Specify an invalid mode so SetFilePointer catches it.  */
82       mode = (DWORD)-1;
83     }
84   return (long) SetFilePointer ((HANDLE) fildes, offset, NULL, mode);
85 }
86 #endif /*HAVE_W32CE_SYSTEM && !__MINGW32CE__*/
87
88
89 \f
90 static ssize_t
91 fd_read (gpgme_data_t dh, void *buffer, size_t size)
92 {
93   return read (dh->data.fd, buffer, size);
94 }
95
96
97 static ssize_t
98 fd_write (gpgme_data_t dh, const void *buffer, size_t size)
99 {
100   return write (dh->data.fd, buffer, size);
101 }
102
103
104 static off_t
105 fd_seek (gpgme_data_t dh, off_t offset, int whence)
106 {
107   return lseek (dh->data.fd, offset, whence);
108 }
109
110
111 static int
112 fd_get_fd (gpgme_data_t dh)
113 {
114   return (dh->data.fd);
115 }
116
117
118 static struct _gpgme_data_cbs fd_cbs =
119   {
120     fd_read,
121     fd_write,
122     fd_seek,
123     NULL,
124     fd_get_fd
125   };
126
127 \f
128 gpgme_error_t
129 gpgme_data_new_from_fd (gpgme_data_t *r_dh, int fd)
130 {
131   gpgme_error_t err;
132   TRACE_BEG1 (DEBUG_DATA, "gpgme_data_new_from_fd", r_dh, "fd=0x%x", fd);
133
134   err = _gpgme_data_new (r_dh, &fd_cbs);
135   if (err)
136     return TRACE_ERR (err);
137
138   (*r_dh)->data.fd = fd;
139   return TRACE_SUC1 ("dh=%p", *r_dh);
140 }