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