0a15b6132736fcdb817e3e61f67d533321a1eb6d
[platform/upstream/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 gpgme_ssize_t (*gpgme_data_read_cb) (gpgme_data_t dh,
40                                              void *buffer,
41                                              size_t size);
42
43 /* Write up to SIZE bytes from buffer BUFFER to the data object with
44    the handle DH.  Return the number of characters written, or -1 on
45    error.  If an error occurs, errno is set.  */
46 typedef gpgme_ssize_t (*gpgme_data_write_cb) (gpgme_data_t dh,
47                                               const void *buffer,
48                                               size_t size);
49
50 /* Set the current position from where the next read or write starts
51    in the data object with the handle DH to OFFSET, relativ to
52    WHENCE.  */
53 typedef gpgme_off_t (*gpgme_data_seek_cb) (gpgme_data_t dh,
54                                             gpgme_off_t offset,
55                                             int whence);
56
57 /* Release the data object with the handle DH.  */
58 typedef void (*gpgme_data_release_cb) (gpgme_data_t dh);
59
60 /* Get the FD associated with the handle DH, or -1.  */
61 typedef int (*gpgme_data_get_fd_cb) (gpgme_data_t dh);
62
63 struct _gpgme_data_cbs
64 {
65   gpgme_data_read_cb read;
66   gpgme_data_write_cb write;
67   gpgme_data_seek_cb seek;
68   gpgme_data_release_cb release;
69   gpgme_data_get_fd_cb get_fd;
70 };
71
72 struct gpgme_data
73 {
74   struct _gpgme_data_cbs *cbs;
75   gpgme_data_encoding_t encoding;
76
77 #ifdef PIPE_BUF
78 #define BUFFER_SIZE PIPE_BUF
79 #else
80 #ifdef _POSIX_PIPE_BUF
81 #define BUFFER_SIZE _POSIX_PIPE_BUF
82 #else
83 #define BUFFER_SIZE 512
84 #endif
85 #endif
86   char pending[BUFFER_SIZE];
87   int pending_len;
88
89   /* File name of the data object.  */
90   char *file_name;
91
92   /* Hint on the to be expected toatl size of the data.  */
93   gpgme_off_t size_hint;
94
95   union
96   {
97     /* For gpgme_data_new_from_fd.  */
98     int fd;
99
100     /* For gpgme_data_new_from_stream.  */
101     FILE *stream;
102
103     /* For gpgme_data_new_from_cbs.  */
104     struct
105     {
106       gpgme_data_cbs_t cbs;
107       void *handle;
108     } user;
109
110     /* For gpgme_data_new_from_mem.  */
111     struct
112     {
113       char *buffer;
114       const char *orig_buffer;
115       /* Allocated size of BUFFER.  */
116       size_t size;
117       size_t length;
118       gpgme_off_t offset;
119     } mem;
120
121     /* For gpgme_data_new_from_read_cb.  */
122     struct
123     {
124       int (*cb) (void *, char *, size_t, size_t *);
125       void *handle;
126     } old_user;
127   } data;
128 };
129
130 \f
131 gpgme_error_t _gpgme_data_new (gpgme_data_t *r_dh,
132                                struct _gpgme_data_cbs *cbs);
133
134 void _gpgme_data_release (gpgme_data_t dh);
135
136 /* Get the file descriptor associated with DH, if possible.  Otherwise
137    return -1.  */
138 int _gpgme_data_get_fd (gpgme_data_t dh);
139
140 /* Get the size-hint value for DH or 0 if not available.  */
141 gpgme_off_t _gpgme_data_get_size_hint (gpgme_data_t dh);
142
143 #endif  /* DATA_H */