Remove all trailing whitespace from source files
[gpgme.git] / src / data.h
1 /* data.h - Internal data object abstraction interface.
2    Copyright (C) 2002, 2004, 2005 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 #ifndef DATA_H
22 #define DATA_H
23
24 #if HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #ifdef HAVE_SYS_TYPES_H
29 # include <sys/types.h>
30 #endif
31 #include <limits.h>
32
33 #include "gpgme.h"
34
35 \f
36 /* Read up to SIZE bytes into buffer BUFFER from the data object with
37    the handle DH.  Return the number of characters read, 0 on EOF and
38    -1 on error.  If an error occurs, errno is set.  */
39 typedef ssize_t (*gpgme_data_read_cb) (gpgme_data_t dh, void *buffer,
40                                        size_t size);
41
42 /* Write up to SIZE bytes from buffer BUFFER to the data object with
43    the handle DH.  Return the number of characters written, or -1 on
44    error.  If an error occurs, errno is set.  */
45 typedef ssize_t (*gpgme_data_write_cb) (gpgme_data_t dh, const void *buffer,
46                                         size_t size);
47
48 /* Set the current position from where the next read or write starts
49    in the data object with the handle DH to OFFSET, relativ to
50    WHENCE.  */
51 typedef off_t (*gpgme_data_seek_cb) (gpgme_data_t dh, off_t offset,
52                                      int whence);
53
54 /* Release the data object with the handle DH.  */
55 typedef void (*gpgme_data_release_cb) (gpgme_data_t dh);
56
57 /* Get the FD associated with the handle DH, or -1.  */
58 typedef int (*gpgme_data_get_fd_cb) (gpgme_data_t dh);
59
60 struct _gpgme_data_cbs
61 {
62   gpgme_data_read_cb read;
63   gpgme_data_write_cb write;
64   gpgme_data_seek_cb seek;
65   gpgme_data_release_cb release;
66   gpgme_data_get_fd_cb get_fd;
67 };
68
69 struct gpgme_data
70 {
71   struct _gpgme_data_cbs *cbs;
72   gpgme_data_encoding_t encoding;
73
74 #ifdef PIPE_BUF
75 #define BUFFER_SIZE PIPE_BUF
76 #else
77 #ifdef _POSIX_PIPE_BUF
78 #define BUFFER_SIZE _POSIX_PIPE_BUF
79 #else
80 #define BUFFER_SIZE 512
81 #endif
82 #endif
83   char pending[BUFFER_SIZE];
84   int pending_len;
85
86   /* File name of the data object.  */
87   char *file_name;
88
89   union
90   {
91     /* For gpgme_data_new_from_fd.  */
92     int fd;
93
94     /* For gpgme_data_new_from_stream.  */
95     FILE *stream;
96
97     /* For gpgme_data_new_from_cbs.  */
98     struct
99     {
100       gpgme_data_cbs_t cbs;
101       void *handle;
102     } user;
103
104     /* For gpgme_data_new_from_mem.  */
105     struct
106     {
107       char *buffer;
108       const char *orig_buffer;
109       /* Allocated size of BUFFER.  */
110       size_t size;
111       size_t length;
112       off_t offset;
113     } mem;
114
115     /* For gpgme_data_new_from_read_cb.  */
116     struct
117     {
118       int (*cb) (void *, char *, size_t, size_t *);
119       void *handle;
120     } old_user;
121   } data;
122 };
123
124 \f
125 gpgme_error_t _gpgme_data_new (gpgme_data_t *r_dh,
126                                struct _gpgme_data_cbs *cbs);
127
128 void _gpgme_data_release (gpgme_data_t dh);
129
130 /* Get the file descriptor associated with DH, if possible.  Otherwise
131    return -1.  */
132 int _gpgme_data_get_fd (gpgme_data_t dh);
133
134 #endif  /* DATA_H */