2008-02-09 Marcus Brinkmann <marcus@g10code.de>
[gnupg.git] / common / iobuf.h
blob8a3671ebc6b75cefc2dd41956faafe463a2041f6
1 /* iobuf.h - I/O buffer
2 * Copyright (C) 1998, 1999, 2000, 2001, 2003 Free Software Foundation, Inc.
4 * This file is part of GNUPG.
6 * GNUPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * GNUPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #ifndef GNUPG_COMMON_IOBUF_H
21 #define GNUPG_COMMON_IOBUF_H
23 #include "../include/types.h" /* fixme: should be moved elsewhere. */
26 #define DBG_IOBUF iobuf_debug_mode
29 #define IOBUFCTRL_INIT 1
30 #define IOBUFCTRL_FREE 2
31 #define IOBUFCTRL_UNDERFLOW 3
32 #define IOBUFCTRL_FLUSH 4
33 #define IOBUFCTRL_DESC 5
34 #define IOBUFCTRL_CANCEL 6
35 #define IOBUFCTRL_USER 16
37 typedef struct iobuf_struct *iobuf_t;
38 typedef struct iobuf_struct *IOBUF; /* Compatibility with gpg 1.4. */
40 /* fixme: we should hide most of this stuff */
41 struct iobuf_struct
43 int use; /* 1 input , 2 output, 3 temp */
44 off_t nlimit;
45 off_t nbytes; /* Used together with nlimit. */
46 off_t ntotal; /* Total bytes read (position of stream). */
47 int nofast; /* Used by the iobuf_get (). */
48 void *directfp;
49 struct
51 size_t size; /* Allocated size */
52 size_t start; /* Number of invalid bytes at the
53 begin of the buffer */
54 size_t len; /* Currently filled to this size */
55 byte *buf;
56 } d;
58 int filter_eof;
59 int error;
60 int (*filter) (void *opaque, int control,
61 iobuf_t chain, byte * buf, size_t * len);
62 void *filter_ov; /* Value for opaque */
63 int filter_ov_owner;
64 char *real_fname;
65 iobuf_t chain; /* Next iobuf used for i/o if any
66 (passed to filter) */
67 int no, subno;
68 const char *desc;
69 void *opaque; /* Can be used to hold any information
70 this value is copied to all
71 instances */
74 #ifndef EXTERN_UNLESS_MAIN_MODULE
75 #if defined (__riscos__) && !defined (INCLUDED_BY_MAIN_MODULE)
76 #define EXTERN_UNLESS_MAIN_MODULE extern
77 #else
78 #define EXTERN_UNLESS_MAIN_MODULE
79 #endif
80 #endif
81 EXTERN_UNLESS_MAIN_MODULE int iobuf_debug_mode;
83 void iobuf_enable_special_filenames (int yes);
84 int iobuf_is_pipe_filename (const char *fname);
85 iobuf_t iobuf_alloc (int use, size_t bufsize);
86 iobuf_t iobuf_temp (void);
87 iobuf_t iobuf_temp_with_content (const char *buffer, size_t length);
88 iobuf_t iobuf_open (const char *fname);
89 iobuf_t iobuf_fdopen (int fd, const char *mode);
90 iobuf_t iobuf_sockopen (int fd, const char *mode);
91 iobuf_t iobuf_create (const char *fname);
92 iobuf_t iobuf_append (const char *fname);
93 iobuf_t iobuf_openrw (const char *fname);
94 int iobuf_ioctl (iobuf_t a, int cmd, int intval, void *ptrval);
95 int iobuf_close (iobuf_t iobuf);
96 int iobuf_cancel (iobuf_t iobuf);
98 int iobuf_push_filter (iobuf_t a, int (*f) (void *opaque, int control,
99 iobuf_t chain, byte * buf,
100 size_t * len), void *ov);
101 int iobuf_push_filter2 (iobuf_t a,
102 int (*f) (void *opaque, int control, iobuf_t chain,
103 byte * buf, size_t * len), void *ov,
104 int rel_ov);
105 int iobuf_flush (iobuf_t a);
106 void iobuf_clear_eof (iobuf_t a);
107 #define iobuf_set_error(a) do { (a)->error = 1; } while(0)
108 #define iobuf_error(a) ((a)->error)
110 void iobuf_set_limit (iobuf_t a, off_t nlimit);
112 off_t iobuf_tell (iobuf_t a);
113 int iobuf_seek (iobuf_t a, off_t newpos);
115 int iobuf_readbyte (iobuf_t a);
116 int iobuf_read (iobuf_t a, void *buf, unsigned buflen);
117 void iobuf_unread (iobuf_t a, const unsigned char *buf, unsigned int buflen);
118 unsigned iobuf_read_line (iobuf_t a, byte ** addr_of_buffer,
119 unsigned *length_of_buffer, unsigned *max_length);
120 int iobuf_peek (iobuf_t a, byte * buf, unsigned buflen);
121 int iobuf_writebyte (iobuf_t a, unsigned c);
122 int iobuf_write (iobuf_t a, const void *buf, unsigned buflen);
123 int iobuf_writestr (iobuf_t a, const char *buf);
125 void iobuf_flush_temp (iobuf_t temp);
126 int iobuf_write_temp (iobuf_t a, iobuf_t temp);
127 size_t iobuf_temp_to_buffer (iobuf_t a, byte * buffer, size_t buflen);
129 off_t iobuf_get_filelength (iobuf_t a, int *overflow);
130 #define IOBUF_FILELENGTH_LIMIT 0xffffffff
131 int iobuf_get_fd (iobuf_t a);
132 const char *iobuf_get_real_fname (iobuf_t a);
133 const char *iobuf_get_fname (iobuf_t a);
135 void iobuf_set_partial_block_mode (iobuf_t a, size_t len);
137 void iobuf_skip_rest (iobuf_t a, unsigned long n, int partial);
140 /* get a byte form the iobuf; must check for eof prior to this function
141 * this function returns values in the range 0 .. 255 or -1 to indicate EOF
142 * iobuf_get_noeof() does not return -1 to indicate EOF, but masks the
143 * returned value to be in the range 0 ..255.
145 #define iobuf_get(a) \
146 ( ((a)->nofast || (a)->d.start >= (a)->d.len )? \
147 iobuf_readbyte((a)) : ( (a)->nbytes++, (a)->d.buf[(a)->d.start++] ) )
148 #define iobuf_get_noeof(a) (iobuf_get((a))&0xff)
150 /* write a byte to the iobuf and return true on write error
151 * This macro does only write the low order byte
153 #define iobuf_put(a,c) iobuf_writebyte(a,c)
155 #define iobuf_where(a) "[don't know]"
156 #define iobuf_id(a) ((a)->no)
158 #define iobuf_get_temp_buffer(a) ( (a)->d.buf )
159 #define iobuf_get_temp_length(a) ( (a)->d.len )
160 #define iobuf_is_temp(a) ( (a)->use == 3 )
162 #endif /*GNUPG_COMMON_IOBUF_H*/