2003-08-05 19:11:04 +02:00
|
|
|
/* iobuf.h - I/O buffer
|
2010-03-08 18:05:37 +01:00
|
|
|
* Copyright (C) 1998, 1999, 2000, 2001, 2003,
|
|
|
|
* 2010 Free Software Foundation, Inc.
|
2003-08-05 19:11:04 +02:00
|
|
|
*
|
Change license for some files in common to LGPLv3+/GPLv2+.
Having the LGPL on the common GnuPG code helps to share code
between GnuPG and related projects (like GPGME and Libassuan). This
is good for interoperability and to reduces bugs.
* common/asshelp.c, common/asshelp.h, common/asshelp2.c, common/b64dec.c
* common/b64enc.c, common/convert.c, common/dns-cert.c
* common/dns-cert.h common/exechelp-posix.c, common/exechelp-w32.c
* common/exechelp-w32ce.c, common/exechelp.h, common/get-passphrase.c
* common/get-passphrase.h, common/gettime.c, common/gpgrlhelp.c
* common/helpfile.c, common/homedir.c, common/http.c, common/http.h
* common/i18n.c, common/init.c, common/init.h, common/iobuf.c
* common/iobuf.h, common/localename.c, common/membuf.c, common/membuf.h
* common/miscellaneous.c, common/openpgp-oid.c, common/openpgpdefs.h
* common/percent.c, common/pka.c, common/pka.h, common/session-env.c
* common/session-env.h, common/sexp-parse.h, common/sexputil.c
* common/signal.c, common/srv.c, common/srv.h, common/ssh-utils.c
* common/ssh-utils.h, common/sysutils.c, common/sysutils.h
* common/tlv.c, common/tlv.h, common/ttyio.c, common/ttyio.h
* common/userids.c, common/userids.h, common/xasprintf.c: Change
license to LGPLv3+/GPLv2+/
2012-04-20 15:43:06 +02:00
|
|
|
* This file is part of GnuPG.
|
2003-08-05 19:11:04 +02:00
|
|
|
*
|
Change license for some files in common to LGPLv3+/GPLv2+.
Having the LGPL on the common GnuPG code helps to share code
between GnuPG and related projects (like GPGME and Libassuan). This
is good for interoperability and to reduces bugs.
* common/asshelp.c, common/asshelp.h, common/asshelp2.c, common/b64dec.c
* common/b64enc.c, common/convert.c, common/dns-cert.c
* common/dns-cert.h common/exechelp-posix.c, common/exechelp-w32.c
* common/exechelp-w32ce.c, common/exechelp.h, common/get-passphrase.c
* common/get-passphrase.h, common/gettime.c, common/gpgrlhelp.c
* common/helpfile.c, common/homedir.c, common/http.c, common/http.h
* common/i18n.c, common/init.c, common/init.h, common/iobuf.c
* common/iobuf.h, common/localename.c, common/membuf.c, common/membuf.h
* common/miscellaneous.c, common/openpgp-oid.c, common/openpgpdefs.h
* common/percent.c, common/pka.c, common/pka.h, common/session-env.c
* common/session-env.h, common/sexp-parse.h, common/sexputil.c
* common/signal.c, common/srv.c, common/srv.h, common/ssh-utils.c
* common/ssh-utils.h, common/sysutils.c, common/sysutils.h
* common/tlv.c, common/tlv.h, common/ttyio.c, common/ttyio.h
* common/userids.c, common/userids.h, common/xasprintf.c: Change
license to LGPLv3+/GPLv2+/
2012-04-20 15:43:06 +02:00
|
|
|
* This file is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of either
|
2003-08-05 19:11:04 +02:00
|
|
|
*
|
Change license for some files in common to LGPLv3+/GPLv2+.
Having the LGPL on the common GnuPG code helps to share code
between GnuPG and related projects (like GPGME and Libassuan). This
is good for interoperability and to reduces bugs.
* common/asshelp.c, common/asshelp.h, common/asshelp2.c, common/b64dec.c
* common/b64enc.c, common/convert.c, common/dns-cert.c
* common/dns-cert.h common/exechelp-posix.c, common/exechelp-w32.c
* common/exechelp-w32ce.c, common/exechelp.h, common/get-passphrase.c
* common/get-passphrase.h, common/gettime.c, common/gpgrlhelp.c
* common/helpfile.c, common/homedir.c, common/http.c, common/http.h
* common/i18n.c, common/init.c, common/init.h, common/iobuf.c
* common/iobuf.h, common/localename.c, common/membuf.c, common/membuf.h
* common/miscellaneous.c, common/openpgp-oid.c, common/openpgpdefs.h
* common/percent.c, common/pka.c, common/pka.h, common/session-env.c
* common/session-env.h, common/sexp-parse.h, common/sexputil.c
* common/signal.c, common/srv.c, common/srv.h, common/ssh-utils.c
* common/ssh-utils.h, common/sysutils.c, common/sysutils.h
* common/tlv.c, common/tlv.h, common/ttyio.c, common/ttyio.h
* common/userids.c, common/userids.h, common/xasprintf.c: Change
license to LGPLv3+/GPLv2+/
2012-04-20 15:43:06 +02:00
|
|
|
* - the GNU Lesser General Public License as published by the Free
|
|
|
|
* Software Foundation; either version 3 of the License, or (at
|
|
|
|
* your option) any later version.
|
|
|
|
*
|
|
|
|
* or
|
|
|
|
*
|
|
|
|
* - the GNU General Public License as published by the Free
|
|
|
|
* Software Foundation; either version 2 of the License, or (at
|
|
|
|
* your option) any later version.
|
|
|
|
*
|
|
|
|
* or both in parallel, as here.
|
|
|
|
*
|
|
|
|
* This file is distributed in the hope that it will be useful,
|
2003-08-05 19:11:04 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2007-07-04 21:49:40 +02:00
|
|
|
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
2003-08-05 19:11:04 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef GNUPG_COMMON_IOBUF_H
|
|
|
|
#define GNUPG_COMMON_IOBUF_H
|
|
|
|
|
2014-01-29 17:39:22 +01:00
|
|
|
#include "../common/types.h"
|
2009-09-30 17:28:38 +02:00
|
|
|
#include "../common/sysutils.h"
|
2011-01-18 12:50:02 +01:00
|
|
|
#include "../common/estream.h"
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
|
|
#define DBG_IOBUF iobuf_debug_mode
|
|
|
|
|
2010-03-08 18:05:37 +01:00
|
|
|
/* Filter control modes. */
|
2003-08-05 19:11:04 +02:00
|
|
|
#define IOBUFCTRL_INIT 1
|
|
|
|
#define IOBUFCTRL_FREE 2
|
|
|
|
#define IOBUFCTRL_UNDERFLOW 3
|
|
|
|
#define IOBUFCTRL_FLUSH 4
|
|
|
|
#define IOBUFCTRL_DESC 5
|
|
|
|
#define IOBUFCTRL_CANCEL 6
|
|
|
|
#define IOBUFCTRL_USER 16
|
|
|
|
|
2010-03-08 18:05:37 +01:00
|
|
|
|
|
|
|
/* Command codes for iobuf_ioctl. */
|
2011-02-04 12:57:53 +01:00
|
|
|
typedef enum
|
2010-03-08 18:05:37 +01:00
|
|
|
{
|
|
|
|
IOBUF_IOCTL_KEEP_OPEN = 1, /* Uses intval. */
|
|
|
|
IOBUF_IOCTL_INVALIDATE_CACHE = 2, /* Uses ptrval. */
|
|
|
|
IOBUF_IOCTL_NO_CACHE = 3, /* Uses intval. */
|
|
|
|
IOBUF_IOCTL_FSYNC = 4 /* Uses ptrval. */
|
|
|
|
} iobuf_ioctl_t;
|
|
|
|
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
typedef struct iobuf_struct *iobuf_t;
|
2006-04-19 13:26:11 +02:00
|
|
|
typedef struct iobuf_struct *IOBUF; /* Compatibility with gpg 1.4. */
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
|
|
/* fixme: we should hide most of this stuff */
|
|
|
|
struct iobuf_struct
|
|
|
|
{
|
|
|
|
int use; /* 1 input , 2 output, 3 temp */
|
|
|
|
off_t nlimit;
|
2006-09-27 15:58:13 +02:00
|
|
|
off_t nbytes; /* Used together with nlimit. */
|
|
|
|
off_t ntotal; /* Total bytes read (position of stream). */
|
|
|
|
int nofast; /* Used by the iobuf_get (). */
|
2003-08-05 19:11:04 +02:00
|
|
|
void *directfp;
|
|
|
|
struct
|
|
|
|
{
|
2006-09-27 15:58:13 +02:00
|
|
|
size_t size; /* Allocated size */
|
|
|
|
size_t start; /* Number of invalid bytes at the
|
|
|
|
begin of the buffer */
|
|
|
|
size_t len; /* Currently filled to this size */
|
2003-08-05 19:11:04 +02:00
|
|
|
byte *buf;
|
2006-09-27 15:58:13 +02:00
|
|
|
} d;
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
int filter_eof;
|
|
|
|
int error;
|
|
|
|
int (*filter) (void *opaque, int control,
|
|
|
|
iobuf_t chain, byte * buf, size_t * len);
|
2006-10-02 13:54:35 +02:00
|
|
|
void *filter_ov; /* Value for opaque */
|
2003-08-05 19:11:04 +02:00
|
|
|
int filter_ov_owner;
|
|
|
|
char *real_fname;
|
2006-10-02 13:54:35 +02:00
|
|
|
iobuf_t chain; /* Next iobuf used for i/o if any
|
2003-08-05 19:11:04 +02:00
|
|
|
(passed to filter) */
|
|
|
|
int no, subno;
|
|
|
|
const char *desc;
|
2006-10-02 13:54:35 +02:00
|
|
|
void *opaque; /* Can be used to hold any information
|
2003-08-05 19:11:04 +02:00
|
|
|
this value is copied to all
|
|
|
|
instances */
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifndef EXTERN_UNLESS_MAIN_MODULE
|
|
|
|
#if defined (__riscos__) && !defined (INCLUDED_BY_MAIN_MODULE)
|
|
|
|
#define EXTERN_UNLESS_MAIN_MODULE extern
|
|
|
|
#else
|
|
|
|
#define EXTERN_UNLESS_MAIN_MODULE
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
EXTERN_UNLESS_MAIN_MODULE int iobuf_debug_mode;
|
|
|
|
|
|
|
|
void iobuf_enable_special_filenames (int yes);
|
2006-04-21 14:56:40 +02:00
|
|
|
int iobuf_is_pipe_filename (const char *fname);
|
2003-08-05 19:11:04 +02:00
|
|
|
iobuf_t iobuf_alloc (int use, size_t bufsize);
|
|
|
|
iobuf_t iobuf_temp (void);
|
|
|
|
iobuf_t iobuf_temp_with_content (const char *buffer, size_t length);
|
2009-09-30 17:28:38 +02:00
|
|
|
iobuf_t iobuf_open_fd_or_name (gnupg_fd_t fd, const char *fname,
|
|
|
|
const char *mode);
|
2003-08-05 19:11:04 +02:00
|
|
|
iobuf_t iobuf_open (const char *fname);
|
|
|
|
iobuf_t iobuf_fdopen (int fd, const char *mode);
|
2010-03-08 18:05:37 +01:00
|
|
|
iobuf_t iobuf_fdopen_nc (int fd, const char *mode);
|
2011-01-18 12:50:02 +01:00
|
|
|
iobuf_t iobuf_esopen (estream_t estream, const char *mode, int keep_open);
|
2003-08-05 19:11:04 +02:00
|
|
|
iobuf_t iobuf_sockopen (int fd, const char *mode);
|
|
|
|
iobuf_t iobuf_create (const char *fname);
|
|
|
|
iobuf_t iobuf_append (const char *fname);
|
|
|
|
iobuf_t iobuf_openrw (const char *fname);
|
2010-03-08 18:05:37 +01:00
|
|
|
int iobuf_ioctl (iobuf_t a, iobuf_ioctl_t cmd, int intval, void *ptrval);
|
2003-08-05 19:11:04 +02:00
|
|
|
int iobuf_close (iobuf_t iobuf);
|
|
|
|
int iobuf_cancel (iobuf_t iobuf);
|
|
|
|
|
|
|
|
int iobuf_push_filter (iobuf_t a, int (*f) (void *opaque, int control,
|
|
|
|
iobuf_t chain, byte * buf,
|
|
|
|
size_t * len), void *ov);
|
|
|
|
int iobuf_push_filter2 (iobuf_t a,
|
|
|
|
int (*f) (void *opaque, int control, iobuf_t chain,
|
|
|
|
byte * buf, size_t * len), void *ov,
|
|
|
|
int rel_ov);
|
|
|
|
int iobuf_flush (iobuf_t a);
|
|
|
|
void iobuf_clear_eof (iobuf_t a);
|
|
|
|
#define iobuf_set_error(a) do { (a)->error = 1; } while(0)
|
|
|
|
#define iobuf_error(a) ((a)->error)
|
|
|
|
|
|
|
|
void iobuf_set_limit (iobuf_t a, off_t nlimit);
|
|
|
|
|
|
|
|
off_t iobuf_tell (iobuf_t a);
|
|
|
|
int iobuf_seek (iobuf_t a, off_t newpos);
|
|
|
|
|
|
|
|
int iobuf_readbyte (iobuf_t a);
|
2005-06-16 10:12:03 +02:00
|
|
|
int iobuf_read (iobuf_t a, void *buf, unsigned buflen);
|
2006-09-27 15:58:13 +02:00
|
|
|
void iobuf_unread (iobuf_t a, const unsigned char *buf, unsigned int buflen);
|
2003-08-05 19:11:04 +02:00
|
|
|
unsigned iobuf_read_line (iobuf_t a, byte ** addr_of_buffer,
|
|
|
|
unsigned *length_of_buffer, unsigned *max_length);
|
|
|
|
int iobuf_peek (iobuf_t a, byte * buf, unsigned buflen);
|
|
|
|
int iobuf_writebyte (iobuf_t a, unsigned c);
|
2005-06-16 10:12:03 +02:00
|
|
|
int iobuf_write (iobuf_t a, const void *buf, unsigned buflen);
|
2003-08-05 19:11:04 +02:00
|
|
|
int iobuf_writestr (iobuf_t a, const char *buf);
|
|
|
|
|
|
|
|
void iobuf_flush_temp (iobuf_t temp);
|
|
|
|
int iobuf_write_temp (iobuf_t a, iobuf_t temp);
|
|
|
|
size_t iobuf_temp_to_buffer (iobuf_t a, byte * buffer, size_t buflen);
|
|
|
|
|
2006-04-21 14:56:40 +02:00
|
|
|
off_t iobuf_get_filelength (iobuf_t a, int *overflow);
|
2003-08-05 19:11:04 +02:00
|
|
|
#define IOBUF_FILELENGTH_LIMIT 0xffffffff
|
2006-04-21 14:56:40 +02:00
|
|
|
int iobuf_get_fd (iobuf_t a);
|
2003-08-05 19:11:04 +02:00
|
|
|
const char *iobuf_get_real_fname (iobuf_t a);
|
|
|
|
const char *iobuf_get_fname (iobuf_t a);
|
2009-09-30 17:28:38 +02:00
|
|
|
const char *iobuf_get_fname_nonnull (iobuf_t a);
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
|
|
void iobuf_set_partial_block_mode (iobuf_t a, size_t len);
|
|
|
|
|
2006-05-23 18:19:43 +02:00
|
|
|
void iobuf_skip_rest (iobuf_t a, unsigned long n, int partial);
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
|
2010-03-08 18:05:37 +01:00
|
|
|
/* Get a byte from the iobuf; must check for eof prior to this
|
|
|
|
* function. This function returns values in the range 0 .. 255 or -1
|
|
|
|
* to indicate EOF. iobuf_get_noeof() does not return -1 to indicate
|
|
|
|
* EOF, but masks the returned value to be in the range 0 .. 255.
|
2003-08-05 19:11:04 +02:00
|
|
|
*/
|
|
|
|
#define iobuf_get(a) \
|
|
|
|
( ((a)->nofast || (a)->d.start >= (a)->d.len )? \
|
|
|
|
iobuf_readbyte((a)) : ( (a)->nbytes++, (a)->d.buf[(a)->d.start++] ) )
|
|
|
|
#define iobuf_get_noeof(a) (iobuf_get((a))&0xff)
|
|
|
|
|
|
|
|
/* write a byte to the iobuf and return true on write error
|
|
|
|
* This macro does only write the low order byte
|
|
|
|
*/
|
|
|
|
#define iobuf_put(a,c) iobuf_writebyte(a,c)
|
|
|
|
|
|
|
|
#define iobuf_where(a) "[don't know]"
|
|
|
|
#define iobuf_id(a) ((a)->no)
|
|
|
|
|
|
|
|
#define iobuf_get_temp_buffer(a) ( (a)->d.buf )
|
|
|
|
#define iobuf_get_temp_length(a) ( (a)->d.len )
|
|
|
|
#define iobuf_is_temp(a) ( (a)->use == 3 )
|
|
|
|
|
|
|
|
#endif /*GNUPG_COMMON_IOBUF_H*/
|