summaryrefslogtreecommitdiff
path: root/makerun.c
blob: 8871b8f733128c217d22958922e1cd2c9d4b3327 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <stdio.h>
#include <string.h>

#ifdef __riscos
  #include <unixlib/local.h>
  #include <oslib/osfile.h>
#endif

struct header {
	unsigned int decompress;
	unsigned int selfreloc;
	unsigned int zeroinit;
	unsigned int entry;
	unsigned int exit;
	unsigned int rosize;
	unsigned int rwsize;
	unsigned int dbsize;
	unsigned int zisize;
	unsigned int dbtype;
	unsigned int base;
	unsigned int wkspace;
};

int main(int argc, char **argv)
{
	FILE *f, *g;
	struct header header;
	unsigned int file_size;
	char buf[256];

	if (argc != 4) {
		fprintf(stderr, "Usage: %s <runimage> <input> <output>\n", 
				argv[0]);
		return 1;
	}

	f = fopen(argv[1], "rb");
	if (f == NULL) {
		fprintf(stderr, "Failed opening %s\n", argv[1]);
		return 1;
	}

	fread((void*)&header, sizeof(struct header), 1, f);

	fseek(f, 0, SEEK_END);
	file_size = ftell(f);

	fclose(f);

	if (header.decompress == 0x464c457f /* FLE\x7f */) {
		/* ELF binary */
		file_size += 0x8000; /* Add 32k of scratch space */
	} else {
		if ((header.entry >> 24) != 0xEB) {
			fprintf(stderr, "%s not binary\n", argv[1]);
			return 1;
		}

		if (header.rosize + header.rwsize + header.dbsize != 
				file_size) {
			if ((header.decompress >> 24) != 0xEB) {
				fprintf(stderr, "Mismatched field sizes\n");
				return 1;
			}
		}

		file_size = header.rosize + header.rwsize + 
				header.dbsize + header.zisize + 
				0x8000 /* 32k of scratch space */;
	}

	f = fopen(argv[2], "r");
	if (f == NULL) {
		fprintf(stderr, "Failed opening %s\n", argv[2]);
		return 1;
	}

	g = fopen(argv[3], "w");
	if (g == NULL) {
		fclose(f);
		fprintf(stderr, "Failed opening %s\n", argv[3]);
		return 1;
	}

	while (fgets(buf, sizeof(buf), f) != NULL) {
		if (strncmp(buf, "WIMPSLOT\n", 9) == 0) {
			fprintf(g, "WimpSlot -min %dk -max %dk\n",
					(file_size / 1024), (file_size / 1024));
		} else {
			fputs(buf, g);
		}
	}

	fclose(g);
	fclose(f);

#ifdef __riscos
	if (__riscosify(argv[3], 0, __RISCOSIFY_STRICT_UNIX_SPECS | 
			__RISCOSIFY_NO_SUFFIX | __RISCOSIFY_FILETYPE_NOT_SET, 
			buf, sizeof(buf), NULL) == NULL) {
		fprintf(stderr, "Riscosify failed\n");
		return 1;
	}

	osfile_set_type(buf, osfile_TYPE_OBEY);
#endif

	return 0;
}