aboutsummaryrefslogtreecommitdiff
path: root/engines/sci/tools/scipack.cpp
blob: a5b7a8d8a1f28a6ec32cfefedfa7251bc0d11ca3 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/***************************************************************************
 scipack.c Copyright (C) 2002 Christoph Reichenbach


 This program may be modified and copied freely according to the terms of
 the GNU general public license (GPL), as long as the above copyright
 notice and the licensing information contained herein are preserved.

 Please refer to www.gnu.org for licensing details.

 This work is provided AS IS, without warranty of any kind, expressed or
 implied, including but not limited to the warranties of merchantibility,
 noninfringement, and fitness for a specific purpose. The author will not
 be held liable for any damage caused by this work or derivatives of it.

 By using this source code, you agree to the licensing terms as stated
 above.


 Please contact the maintainer for bug reports or inquiries.

 Current Maintainer:

    Christoph Reichenbach (CR) <jameson@linuxgames.com>

***************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sciresource.h>
#include <resource.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifdef HAVE_UNISTD_H
#  include <unistd.h>
#endif

#define COPY_BLOCK_SIZE 512

unsigned short *resource_ids = NULL;

void
help()
{
	printf("Usage:\n\tscipack <file_0> ... <file_n>\n"
	       "\nBuilds an uncompressed SCI0 resource.000 and a resource.map\n");
}

int /* Returns resource ID on success, -1 on error */
test_file(char *filename)
{
	char *dot = strchr(filename, '.');
	char *endptr;
	FILE *f;
	int res_type, res_index;

	if (!dot) {
		fprintf(stderr, "Must contain a period");
		return -1;
	}

	*dot = 0;

	for (res_type = 0; res_type < sci_invalid_resource
		     && strcasecmp(filename, sci_resource_types[res_type]); res_type++);

	*dot = '.';

	if (res_type == sci_invalid_resource) {
		fprintf(stderr, "Invalid resource type");
		return -1;
	}

	++dot;
	res_index = strtol(dot, &endptr, 10);

	if (!*dot || *endptr) {
		fprintf(stderr, "Invalid resource index");
		return -1;
	}

	if (res_index < 0) {
		fprintf(stderr, "Negative resource index");
		return -1;
	}

	if (res_index >= 1000) {
		fprintf(stderr, "Resource index too large");
		return -1;
	}

	f = fopen(filename, "r");
	if (!f) {
		perror("While asserting file");
		return -1;
	}
	fclose(f);

	return (res_type << 11) | res_index;
}

int
build_file_ids(int count, char **names)
{
	int i;
	int error = 0;

	resource_ids = (unsigned short*) malloc(sizeof(unsigned short) * count);

	for (i = 0; i < count; i++) {
		int id = test_file(names[i]);
		if (id < 0) {
			error = -1;
			fprintf(stderr, ": %s\n", names[i]);
		}
		else resource_ids[i] = id;
	}

	return error;
}


static inline void
write_uint16(int fd, unsigned int uint)
{
	unsigned char upper = (uint >> 8) & 0xff;
	unsigned char lower = (uint) & 0xff;

	if ((write(fd, &upper, 1) < 1) 
	    || (write(fd, &lower, 1) < 1)) {
		perror("While writing");
		exit(1);
	}
}

int
write_files(int count, char **names)
{
	int resource_000, resource_map;
	int i;

	resource_000 = creat("resource.000", 0644);
	if (resource_000 < 0) {
		perror("While creating 'resource.000'");
		return -1;
	}

	resource_map = creat("resource.map", 0644);
	if (resource_map < 0) {
		perror("While creating 'resource.map'");
		return -1;
	}

	for (i = 0; i < count; i++) {
		int fd = open(names[i], O_RDONLY);
		struct stat fdstat;
		int fdsize;
		unsigned char buf[512];
		int j;
		long offset = lseek(resource_000, SEEK_CUR, 0);
		int top_offset = (offset >> 16) & 0xffff;
		int bot_offset = offset & 0xffff;

		if (fd < 0) {
			perror(names[i]);
			return -1;
		}
		fstat(fd, &fdstat);
		fdsize = fdstat.st_size;

		write_uint16(resource_000, resource_ids[i]);
		write_uint16(resource_000, fdsize);
		write_uint16(resource_000, fdsize);
		write_uint16(resource_000, 0);

		do {
			j = read(fd, buf, COPY_BLOCK_SIZE);
			write(resource_000, buf, j);
		} while (j == COPY_BLOCK_SIZE);
		close(fd);

		write_uint16(resource_map, resource_ids[i]);
		write_uint16(resource_map, bot_offset);
		write_uint16(resource_map, top_offset);
	}

	/* Terminate resource 000 */
	write_uint16(resource_000, 0);

	/* Terminate resource map */
	write_uint16(resource_map, 0xffff);
	write_uint16(resource_map, 0xffff);

	close(resource_000);
	close(resource_map);
}

	
int
main(int argc, char **argv)
{
	printf("scipack.c Copyright (C) 2002 Christoph Reichenbach\n"
	       "This program is FREE SOFTWARE. You may copy it and/or re-distribute it\n"
	       "according to the terms of the GNU General Public License. See LICENSING\n"
	       "for details.\n");

	if (argc < 1)
		help();

	if (build_file_ids(argc - 1, argv + 1))
		return -1;

	if (write_files(argc - 1, argv + 1))
		return -1;
	free(resource_ids);
}