Line data Source code
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 : #ifdef HAVE_SYS_TYPES_H
29 : # include <sys/types.h>
30 : #endif
31 :
32 : #include "debug.h"
33 : #include "data.h"
34 :
35 :
36 :
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/) */
42 :
43 : #include <windows.h>
44 :
45 : static int
46 : read (int fildes, void *buf, unsigned int bufsize)
47 : {
48 : DWORD NumberOfBytesRead;
49 : if (bufsize > 0x7fffffff)
50 : bufsize = 0x7fffffff;
51 : if (!ReadFile ((HANDLE) fildes, buf, bufsize, &NumberOfBytesRead, NULL))
52 : return -1;
53 : return (int) NumberOfBytesRead;
54 : }
55 :
56 : static int
57 : write (int fildes, const void *buf, unsigned int bufsize)
58 : {
59 : DWORD NumberOfBytesWritten;
60 : if (bufsize > 0x7fffffff)
61 : bufsize = 0x7fffffff;
62 : if (!WriteFile ((HANDLE) fildes, buf, bufsize, &NumberOfBytesWritten, NULL))
63 : return -1;
64 : return (int) NumberOfBytesWritten;
65 : }
66 :
67 : static long
68 : lseek (int fildes, long offset, int whence)
69 : {
70 : DWORD mode;
71 : switch (whence)
72 : {
73 : case SEEK_SET:
74 : mode = FILE_BEGIN;
75 : break;
76 : case SEEK_CUR:
77 : mode = FILE_CURRENT;
78 : break;
79 : case SEEK_END:
80 : mode = FILE_END;
81 : break;
82 : default:
83 : /* Specify an invalid mode so SetFilePointer catches it. */
84 : mode = (DWORD)-1;
85 : }
86 : return (long) SetFilePointer ((HANDLE) fildes, offset, NULL, mode);
87 : }
88 : #endif /*HAVE_W32CE_SYSTEM && !__MINGW32CE__*/
89 :
90 :
91 :
92 : static gpgme_ssize_t
93 0 : fd_read (gpgme_data_t dh, void *buffer, size_t size)
94 : {
95 0 : return read (dh->data.fd, buffer, size);
96 : }
97 :
98 :
99 : static gpgme_ssize_t
100 0 : fd_write (gpgme_data_t dh, const void *buffer, size_t size)
101 : {
102 0 : return write (dh->data.fd, buffer, size);
103 : }
104 :
105 :
106 : static gpgme_off_t
107 0 : fd_seek (gpgme_data_t dh, gpgme_off_t offset, int whence)
108 : {
109 0 : return lseek (dh->data.fd, offset, whence);
110 : }
111 :
112 :
113 : static int
114 0 : fd_get_fd (gpgme_data_t dh)
115 : {
116 0 : return (dh->data.fd);
117 : }
118 :
119 :
120 : static struct _gpgme_data_cbs fd_cbs =
121 : {
122 : fd_read,
123 : fd_write,
124 : fd_seek,
125 : NULL,
126 : fd_get_fd
127 : };
128 :
129 :
130 : gpgme_error_t
131 0 : gpgme_data_new_from_fd (gpgme_data_t *r_dh, int fd)
132 : {
133 : gpgme_error_t err;
134 0 : TRACE_BEG1 (DEBUG_DATA, "gpgme_data_new_from_fd", r_dh, "fd=0x%x", fd);
135 :
136 0 : err = _gpgme_data_new (r_dh, &fd_cbs);
137 0 : if (err)
138 0 : return TRACE_ERR (err);
139 :
140 0 : (*r_dh)->data.fd = fd;
141 0 : return TRACE_SUC1 ("dh=%p", *r_dh);
142 : }
|