#ifndef XADMASTER_XADIO_H
#define XADMASTER_XADIO_H

/* Programmheader

	Name:		xadIO.h
	Main:		xadmaster
	Versionstring:	$VER: xadIO.h 1.4 (24.03.2001)
	Author:		SDI
	Distribution:	Freeware
	Description:	input/output function header

 1.0   16.03.00 : first version
 1.1   23.10.00 : added XIDFLAG_REACHEDEND
 1.2   24.10.00 : renamed from xadInput.h
 1.3   07.11.00 : moved xadIOPutChar, xadIOGetChar into structure to
	support different functions
 1.4   24.03.01 : added ReadBits versions
*/

#include <exec/types.h>
#include <proto/xadmaster.h>

/* These are used to keep they in sync when called in source directly mode */
#ifndef XADIODIRECTMODE
#define XADIOFUNCMODE     extern
#define XADIOFUNCMODEBITS extern
#endif

typedef void (*XADINOUTFUNC)(struct xadInOut *io, ULONG size);
typedef UBYTE (*XADINOUTPUT)(struct xadInOut *io, UBYTE data);
typedef UBYTE (*XADINOUTGET)(struct xadInOut *io);

struct xadInOut {
  struct xadArchiveInfo * xio_ArchiveInfo;		/* filled by xadIOAlloc */
  struct xadMasterBase *  xio_xadMasterBase;		/* filled by xadIOAlloc */
  LONG			  xio_Error;			/* cleared */
  ULONG			  xio_Flags;			/* filled by xadIOAlloc, functions or user */

  XADINOUTGET		  xio_GetFunc;			/* filled by xadIOAlloc or user */
  APTR			  xio_GetFuncPrivate;
  XADINOUTPUT		  xio_PutFunc;			/* filled by xadIOAlloc or user */
  APTR			  xio_PutFuncPrivate;

  XADINOUTFUNC	          xio_InFunc;
  APTR			  xio_InFuncPrivate;
  ULONG                   xio_InSize;
  ULONG			  xio_InBufferSize;
  ULONG			  xio_InBufferPos;
  STRPTR		  xio_InBuffer;
  ULONG			  xio_BitBuf;			/* for xadIOGetBits functions */
  UWORD			  xio_BitNum;			/* for xadIOGetBits functions */

  UWORD			  xio_CRC16;			/* crc16 from output functions */
  ULONG			  xio_CRC32;			/* crc32 from output functions */

  XADINOUTFUNC	          xio_OutFunc;
  APTR			  xio_OutFuncPrivate;
  ULONG                   xio_OutSize;
  ULONG			  xio_OutBufferSize;
  ULONG			  xio_OutBufferPos;
  STRPTR		  xio_OutBuffer;
};

/* setting BufferPos to buffer size activates first time read! */

#define XADIOF_ALLOCINBUFFER	(1<<0)	/* allocate input buffer */
#define XADIOF_ALLOCOUTBUFFER	(1<<1)	/* allocate output buffer */
#define XADIOF_NOINENDERR	(1<<2)	/* xadIOGetChar does not produce err at buffer end */
#define XADIOF_NOOUTENDERR      (1<<3)  /* xadIOPutChar does not check out size */
#define XADIOF_LASTINBYTE       (1<<4)  /* last byte was read, set by xadIOGetChar */
#define XADIOF_LASTOUTBYTE      (1<<5)  /* output length was reached, set by xadIOPutChar */
#define XADIOF_ERROR            (1<<6)	/* an error occured */
#define XADIOF_NOCRC16		(1<<7)  /* calculate no CRC16 */
#define XADIOF_NOCRC32		(1<<8)  /* calculate no CRC32 */
#define XADIOF_COMPLETEOUTFUNC	(1<<9)	/* outfunc completely replaces write stuff */

/* allocates the xadInOut structure and the buffers */
XADIOFUNCMODE struct xadInOut *xadIOAlloc(ULONG flags, struct xadArchiveInfo *ai, struct xadMasterBase *xadMasterBase);

/* writes the buffer out */
XADIOFUNCMODE LONG xadIOWriteBuf(struct xadInOut *io);

#define xadIOGetChar(io)	(io)->xio_GetFunc((io))		/* reads one byte */
#define xadIOPutChar(io,a)	(io)->xio_PutFunc((io), (a))	/* stores one byte */

/* The read bits function only read the bits without flushing from buffer. This is
done by DropBits. Some compressors need this method, as the flush different amount
of data than they read in. Normally the GetBits functions are used.
When including the source file directly, do not forget to set the correct defines
to include the necessary functions. */

#if !defined(XADIODIRECTMODE) || defined(XADIOGETBITSLOW)
/* new bytes inserted from left, get bits from right end, max 32 bits, no checks */
XADIOFUNCMODEBITS ULONG xadIOGetBitsLow(struct xadInOut *io, UBYTE bits);
#endif

#if !defined(XADIODIRECTMODE) || defined(XADIOREADBITSLOW)
XADIOFUNCMODEBITS ULONG xadIOReadBitsLow(struct xadInOut *io, UBYTE bits);
XADIOFUNCMODEBITS void xadIODropBitsLow(struct xadInOut *io, UBYTE bits);
#endif

#if !defined(XADIODIRECTMODE) || defined(XADIOGETBITSHIGH)
/* new bytes inserted from right, get bits from left end, max 32 bits, no checks */
XADIOFUNCMODEBITS ULONG xadIOGetBitsHigh(struct xadInOut *io, UBYTE bits);
#endif

#if !defined(XADIODIRECTMODE) || defined(XADIOREADBITSHIGH)
XADIOFUNCMODEBITS ULONG xadIOReadBitsHigh(struct xadInOut *io, UBYTE bits);
XADIOFUNCMODEBITS void xadIODropBitsHigh(struct xadInOut *io, UBYTE bits);
#endif

#endif /* XADMASTER_XADIO_H */
