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 2 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, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 #ifndef GNUPG_COMMON_IOBUF_H
23 #define GNUPG_COMMON_IOBUF_H
25 #include "../include/types.h" /* fixme: should be moved elsewhere. */
28 #define DBG_IOBUF iobuf_debug_mode
31 #define IOBUFCTRL_INIT 1
32 #define IOBUFCTRL_FREE 2
33 #define IOBUFCTRL_UNDERFLOW 3
34 #define IOBUFCTRL_FLUSH 4
35 #define IOBUFCTRL_DESC 5
36 #define IOBUFCTRL_CANCEL 6
37 #define IOBUFCTRL_USER 16
39 typedef struct iobuf_struct
*iobuf_t
;
40 typedef struct iobuf_struct
*IOBUF
; /* Compatibility with gpg 1.4. */
42 /* fixme: we should hide most of this stuff */
45 int use
; /* 1 input , 2 output, 3 temp */
47 off_t nbytes
; /* Used together with nlimit. */
48 off_t ntotal
; /* Total bytes read (position of stream). */
49 int nofast
; /* Used by the iobuf_get (). */
53 size_t size
; /* Allocated size */
54 size_t start
; /* Number of invalid bytes at the
55 begin of the buffer */
56 size_t len
; /* Currently filled to this size */
62 int (*filter
) (void *opaque
, int control
,
63 iobuf_t chain
, byte
* buf
, size_t * len
);
64 void *filter_ov
; /* Value for opaque */
67 iobuf_t chain
; /* Next iobuf used for i/o if any
71 void *opaque
; /* Can be used to hold any information
72 this value is copied to all
76 #ifndef EXTERN_UNLESS_MAIN_MODULE
77 #if defined (__riscos__) && !defined (INCLUDED_BY_MAIN_MODULE)
78 #define EXTERN_UNLESS_MAIN_MODULE extern
80 #define EXTERN_UNLESS_MAIN_MODULE
83 EXTERN_UNLESS_MAIN_MODULE
int iobuf_debug_mode
;
85 void iobuf_enable_special_filenames (int yes
);
86 int iobuf_is_pipe_filename (const char *fname
);
87 iobuf_t
iobuf_alloc (int use
, size_t bufsize
);
88 iobuf_t
iobuf_temp (void);
89 iobuf_t
iobuf_temp_with_content (const char *buffer
, size_t length
);
90 iobuf_t
iobuf_open (const char *fname
);
91 iobuf_t
iobuf_fdopen (int fd
, const char *mode
);
92 iobuf_t
iobuf_sockopen (int fd
, const char *mode
);
93 iobuf_t
iobuf_create (const char *fname
);
94 iobuf_t
iobuf_append (const char *fname
);
95 iobuf_t
iobuf_openrw (const char *fname
);
96 int iobuf_ioctl (iobuf_t a
, int cmd
, int intval
, void *ptrval
);
97 int iobuf_close (iobuf_t iobuf
);
98 int iobuf_cancel (iobuf_t iobuf
);
100 int iobuf_push_filter (iobuf_t a
, int (*f
) (void *opaque
, int control
,
101 iobuf_t chain
, byte
* buf
,
102 size_t * len
), void *ov
);
103 int iobuf_push_filter2 (iobuf_t a
,
104 int (*f
) (void *opaque
, int control
, iobuf_t chain
,
105 byte
* buf
, size_t * len
), void *ov
,
107 int iobuf_flush (iobuf_t a
);
108 void iobuf_clear_eof (iobuf_t a
);
109 #define iobuf_set_error(a) do { (a)->error = 1; } while(0)
110 #define iobuf_error(a) ((a)->error)
112 void iobuf_set_limit (iobuf_t a
, off_t nlimit
);
114 off_t
iobuf_tell (iobuf_t a
);
115 int iobuf_seek (iobuf_t a
, off_t newpos
);
117 int iobuf_readbyte (iobuf_t a
);
118 int iobuf_read (iobuf_t a
, void *buf
, unsigned buflen
);
119 void iobuf_unread (iobuf_t a
, const unsigned char *buf
, unsigned int buflen
);
120 unsigned iobuf_read_line (iobuf_t a
, byte
** addr_of_buffer
,
121 unsigned *length_of_buffer
, unsigned *max_length
);
122 int iobuf_peek (iobuf_t a
, byte
* buf
, unsigned buflen
);
123 int iobuf_writebyte (iobuf_t a
, unsigned c
);
124 int iobuf_write (iobuf_t a
, const void *buf
, unsigned buflen
);
125 int iobuf_writestr (iobuf_t a
, const char *buf
);
127 void iobuf_flush_temp (iobuf_t temp
);
128 int iobuf_write_temp (iobuf_t a
, iobuf_t temp
);
129 size_t iobuf_temp_to_buffer (iobuf_t a
, byte
* buffer
, size_t buflen
);
131 off_t
iobuf_get_filelength (iobuf_t a
, int *overflow
);
132 #define IOBUF_FILELENGTH_LIMIT 0xffffffff
133 int iobuf_get_fd (iobuf_t a
);
134 const char *iobuf_get_real_fname (iobuf_t a
);
135 const char *iobuf_get_fname (iobuf_t a
);
137 void iobuf_set_partial_block_mode (iobuf_t a
, size_t len
);
139 int iobuf_translate_file_handle (int fd
, int for_write
);
141 void iobuf_skip_rest (iobuf_t a
, unsigned long n
, int partial
);
144 /* get a byte form the iobuf; must check for eof prior to this function
145 * this function returns values in the range 0 .. 255 or -1 to indicate EOF
146 * iobuf_get_noeof() does not return -1 to indicate EOF, but masks the
147 * returned value to be in the range 0 ..255.
149 #define iobuf_get(a) \
150 ( ((a)->nofast || (a)->d.start >= (a)->d.len )? \
151 iobuf_readbyte((a)) : ( (a)->nbytes++, (a)->d.buf[(a)->d.start++] ) )
152 #define iobuf_get_noeof(a) (iobuf_get((a))&0xff)
154 /* write a byte to the iobuf and return true on write error
155 * This macro does only write the low order byte
157 #define iobuf_put(a,c) iobuf_writebyte(a,c)
159 #define iobuf_where(a) "[don't know]"
160 #define iobuf_id(a) ((a)->no)
162 #define iobuf_get_temp_buffer(a) ( (a)->d.buf )
163 #define iobuf_get_temp_length(a) ( (a)->d.len )
164 #define iobuf_is_temp(a) ( (a)->use == 3 )
166 #endif /*GNUPG_COMMON_IOBUF_H*/