/********************************************************************
*                                                                   *
*  PowerPacker DATA file support function V2.0                      *
*  -------------------------------------------                      *
*                       (Read PowerPacker.doc for more information) *
*                                                                   *
*    error = PP_LoadData (file, col, typeofmem, buffer, length, pw) *
*    with:                                                          *
*       char *file;     filename                                    *
*       UBYTE col;      color (see ppdata.h)                        *
*       ULONG typeofmem type of memory that will be allocated       *
*       UBYTE **buffer  pointer to pointer to buffer                *
*       ULONG *length   pointer to buffer length                    *
*       char *pw;       pointer to password or NULL                 *
*                                                                   *
*  NOTE: - After loading you must free the allocated memory:        *
*          DO NOT FORGET !!!!!                                      *
*             FreeMem (buffer, length);                             *
*        - Errors are defined in ppdata.h                           *
*        - For encrypted data call first with pw = NULL, then       *
*          if error is PP_CRYPTED you know file is crypted.         *
*          Prompt the user for a password and call again with       *
*          pw pointing to this password. If the password is         *
*          incorrect error is PP_PASSERR, otherwise the file will   *
*          be loaded and decrypted.                                 *
*                                                                   *
*    Example:                                                       *
*                                                                   *
*      #include <ppdata.h>                                          *
*      ...                                                          *
*                                                                   *
*      UBYTE *mymem = NULL;                                         *
*      ULONG mylen = 0;                                             *
*                                                                   *
*      err = PP_LoadData ("df0:myfile.pp", DECR_POINTER,            *
*                     MEMF_PUBLIC+MEMF_CHIP, &mymem, &mylen, NULL); *
*      if (err == PP_LOADOK) {                                      *
*         DoSomething (mymem, mylen);                               *
*         FreeMem (mymem, mylen);                                   *
*         }                                                         *
*      else switch (err) {                                          *
*         case PP_CRYPTED:                                          *
*            puts ("File is encrypted !");                          *
*            break;                                                 *
*         case PP_READERR:                                          *
*            puts ("Loading error !!!");                            *
*            break;                                                 *
*         ...                                                       *
*         }                                                         *
*                                                                   *
********************************************************************/
/********************************************************************
*                                                                   *
*  'PP_LoadData' PowerPacker DATA file support function V2.0        *
*                                                                   *
*  You may use this code for non-commercial purposes provided this  *  
*  copyright notice is left intact !                                *
*                                                                   *
*               Copyright (c) Apr 1990 by Nico François (PowerPeak) *
********************************************************************/

#include <exec/types.h>
#include <exec/io.h>
#include <exec/memory.h>
#include <libraries/dos.h>
#include <proto/exec.h>
#include <proto/dos.h>

#include "ppdata.h"

#define SAFETY_MARGIN	8L
#define SIZEOF				(ULONG)sizeof
#define myRead(to,len)	if (Read (pp_lock, (char *)to, len) != len) {\
									pp_FreeStuff(); return (PP_READERR); }

extern UWORD pp_CalcCheckSum();
extern ULONG pp_CalcPasskey();
extern void pp_DecrunchBuffer();
extern void pp_Decrypt();

static BPTR pp_lock;
static struct FileInfoBlock *pp_FileInfoBlock;
static UBYTE *pp_filestart;
static ULONG pp_bufferlen;
static ULONG pp_coladdr[5] = { 0xdff180, 0xdff182, 0xdff1a2, 0xdff102, 0 };

void pp_FreeStuff();

PP_LoadData (pp_file, color, typeofmem, buffer, length, pw)		/* Version 2.0 */
char *pp_file;
UBYTE color;
ULONG typeofmem;
UBYTE **buffer;
ULONG *length;
char *pw;
{
	ULONG pp_passchecksum, pp_filelen, pp_crunlen, pp_efficiency;
	UBYTE pp_crunched, pp_crypt = FALSE;
	ULONG pp_seek, pp_hdr, dummy;

	pp_coladdr[4] = (ULONG)&dummy;		/* dummy for decrunch color 'None' */
	pp_filestart = NULL;
	if (!(pp_FileInfoBlock = (struct FileInfoBlock *)AllocMem
		(SIZEOF(*pp_FileInfoBlock), MEMF_PUBLIC))) return (PP_NOMEMORY);

	if (!(pp_lock = (BPTR)Lock (pp_file, ACCESS_READ))) {
		pp_FreeStuff();
		return (PP_LOCKERR);
		}
	Examine (pp_lock, pp_FileInfoBlock);
	UnLock (pp_lock);
	pp_crunlen = pp_FileInfoBlock->fib_Size;

	/* read decrunched length */
	if (!(pp_lock = (BPTR)Open (pp_file, MODE_OLDFILE))) {
		pp_FreeStuff();
		return (PP_OPENERR);
		}
	myRead (&pp_hdr, 4L);

	/* check if crunched */
	if (pp_hdr == 'PX20' || pp_hdr == 'PP20') {
		if (pp_hdr == 'PX20') {
			if (!pw) {
				pp_FreeStuff();
				return (PP_CRYPTED);
				}
			myRead (&pp_passchecksum, 2L);
			if (pp_CalcCheckSum (pw) != pp_passchecksum) {
				pp_FreeStuff();
				return (PP_PASSERR);
				}
			pp_crypt = TRUE;
			pp_seek = 6L;
			}
		else pp_seek = 4L;
		Seek (pp_lock, pp_crunlen - 4L, OFFSET_BEGINNING);
		myRead (&pp_filelen, 4L);
		pp_filelen >>= 8L;
		pp_crunlen -= 4L + pp_seek;
		Seek (pp_lock, pp_seek, OFFSET_BEGINNING);
		myRead (&pp_efficiency, 4L);
		pp_bufferlen = pp_filelen + SAFETY_MARGIN;
		pp_crunched = TRUE;
		}
	else {
		Seek (pp_lock, 0L, OFFSET_BEGINNING);
		pp_bufferlen = pp_filelen = pp_crunlen;
		pp_crunched = FALSE;
		}
	if (!(pp_filestart=(UBYTE *)AllocMem (pp_bufferlen, typeofmem))) {
		pp_FreeStuff();
		return (PP_NOMEMORY);
		}
	/* load file */
	myRead (pp_filestart, pp_crunlen);

	Close (pp_lock);
	FreeMem (pp_FileInfoBlock, SIZEOF(*pp_FileInfoBlock));
	if (pp_crunched) {
		if (pp_crypt)
			pp_Decrypt (pp_filestart, pp_crunlen-4L, pp_CalcPasskey (pw));
		pp_DecrunchBuffer (pp_filestart + pp_crunlen,
					pp_filestart + SAFETY_MARGIN, &pp_efficiency, pp_coladdr[color]);
		FreeMem (pp_filestart, SAFETY_MARGIN);
		pp_filestart += SAFETY_MARGIN;
		}
	*buffer = pp_filestart;
	*length = pp_filelen;
	return (PP_LOADOK);
}

void pp_FreeStuff()
{
	if (pp_lock) Close (pp_lock);
	if (pp_filestart) FreeMem (pp_filestart, pp_bufferlen);
	if (pp_FileInfoBlock) FreeMem (pp_FileInfoBlock, SIZEOF(*pp_FileInfoBlock));
	pp_lock = NULL; pp_filestart = NULL; pp_FileInfoBlock = NULL;
}
