/*
 * Copyright (c) 2021, RISC OS Open Ltd
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of RISC OS Open Ltd nor the names of its contributors
 *       may be used to endorse or promote products derived from this software
 *       without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

static FILE *out = NULL;
static const char *deleteme = NULL;

static void cleanup(void)
{
	if (out)
	{
		fclose(out);
		out = NULL;
	}
	if (deleteme)
	{
		remove(deleteme);
	}
}

static void must_read(void *buf,size_t size,FILE *f)
{
	size_t amt = fread(buf,1,size,f);
	if (amt != size)
	{
		fprintf(stderr,"Failed reading input\n");
		exit(1);
	}
}

static void must_write(void *buf,size_t size,FILE *f)
{
	size_t amt = fwrite(buf,1,size,f);
	if (amt != size)
	{
		fprintf(stderr,"Failed writing output\n");
		exit(1);
	}
}

#define W(X) (header[X] + (header[X+1]<<8) + (header[X+2]<<16) + (header[X+3]<<24))

/* Load an AIF and extract just the RO and RW segments */

int main(int argc,char **argv)
{
	if (argc != 3)
	{
		fprintf(stderr,"Usage: kstrip <in> <out>\n");
		exit(1);
	}
	FILE *in = fopen(argv[1],"rb");
	out = fopen(argv[2],"wb");
	if (out)
	{
		deleteme = argv[2];
		atexit(cleanup);
	}
	if (!in || !out)
	{
		fprintf(stderr,"Failed to open input/output\n");
		exit(1);
	}
	uint8_t header[128];
	must_read(header,128,in);
	uint32_t ro_size = W(0x14);
	uint32_t rw_size = W(0x18);
	printf("RO size %8x\n",ro_size);
	printf("RW size %8x\n",rw_size);
	uint32_t total = ro_size + rw_size;
	printf("Total   %8x\n",total);
	void *buf = malloc(total);
	if (!buf)
	{
		fprintf(stderr,"Failed to get memory\n");
		exit(1);
	}
	must_read(buf,total,in);
	must_write(buf,total,out);
	deleteme = NULL;
	fclose(in);
	fclose(out);
	free(buf);
	return 0;
}