#include "tn_ssf.h"
#include "TN_SSF.bin_rev.h"
#include <stdlib.h>
#include <string.h>

const char verstag[] = VERSTAG;

struct ExecIFace *IExec;
struct Interface *INewlib;

static int32 ssf_open (struct TNDecHandle *handle, void *buf, uint32 len);
static void ssf_close (struct TNDecHandle *handle);
static int32 ssf_process (struct TNDecHandle *handle, void *buf, uint32 len);

ssfiface_t issf = {
	ssf_open,
	ssf_close,
	ssf_process,
};

ssfiface_t *_start (char *argstr, int32 arglen, struct ExecBase *SysBase) {
	IExec = (struct ExecIFace *)SysBase->MainInterface;
	return &issf;
}

static int32 ssf_open (struct TNDecHandle *handle, void *buf, uint32 len) {
	INewlib = handle->newlib;

	if (ssf_start(buf, len) != AO_SUCCESS) {
		return FALSE;
	}

	if (ssf_fill_info(&handle->info) != AO_SUCCESS) {
		return FALSE;
	}

	return TRUE;
}

static void ssf_close (struct TNDecHandle *handle) {
	ssf_stop();
	INewlib = NULL;
}

static int32 ssf_process (struct TNDecHandle *handle, void *buf, uint32 len) {
	if (ssf_gen(buf, len) == AO_SUCCESS) {
		return len;
	}
	return 0;
}
