#ifndef XPKMASTER_XBUF_C
#define XPKMASTER_XBUF_C

/* Routinesheader

	Name:		xbuf.c
	Main:		xpkmaster
	Versionstring:	$VER: xbuf.c 1.1 (31.03.97)
	Author:		SDI
	Distribution:	PD
	Description:	Xpk buffer handling

 1.0   09.10.96 : first real version
 1.1   31.02.97 : now free's password buffer
*/

#include <pragma/exec_lib.h>
#include <exec/memory.h>
#include <exec/libraries.h>
#include <dos/dos.h>
#include "xpkmaster.h"

/************************* alloc and init xbuf ***************************/

struct XpkBuffer *initxbuf(void)
{
  struct XpkBuffer *xbuf;

  if(!(xbuf = (struct XpkBuffer *) AllocMem(sizeof(struct XpkBuffer),
  MEMF_CLEAR)))
    return 0;
  /* Save the original task priority in case we change it during an
     operation */

  xbuf->xb_Priority = FindTask(0L)->tc_Node.ln_Pri;

  xbuf->xb_InLen = -1;

  return xbuf;
}

/***************************** free bufs *******************************/
LONG freebufs(struct XpkBuffer *xbuf)
{				/* Free an XpkBuffer */
  LONG error = xbuf->xb_Result;

  closesub(xbuf);		/* Free any open sub-library */

  if(xbuf->xb_Flags & XMF_OWNTASKPRI)
    SetTaskPri(FindTask (NULL), xbuf->xb_Priority);
  if(xbuf->xb_Flags & XMF_OWNPASSWORD)
    FreeMem(xbuf->xb_Password, xbuf->xb_PasswordSize);

  FreeMem(xbuf, sizeof(struct XpkBuffer));
				/* Finally, free the handle itself */
  return error;
}

#endif /* XPKMASTER_XBUF_C */
