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