1 /* ScummVM Tools
2  *
3  * ScummVM Tools is the legal property of its developers, whose
4  * names are too numerous to list here. Please refer to the
5  * COPYRIGHT file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  */
21 
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sciresource.h>
26 #include <resource.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 
30 #define COPY_BLOCK_SIZE 512
31 
32 unsigned short *resource_ids = NULL;
33 
34 void
help()35 help() {
36 	printf("Usage:\n\tscipack <file_0> ... <file_n>\n"
37 	       "\nBuilds an uncompressed SCI0 resource.000 and a resource.map\n");
38 }
39 
40 int /* Returns resource ID on success, -1 on error */
test_file(char * filename)41 test_file(char *filename) {
42 	char *dot = strchr(filename, '.');
43 	char *endptr;
44 	FILE *f;
45 	int res_type, res_index;
46 
47 	if (!dot) {
48 		fprintf(stderr, "Must contain a period");
49 		return -1;
50 	}
51 
52 	*dot = 0;
53 
54 	for (res_type = 0; res_type < sci_invalid_resource
55 	        && scumm_stricmp(filename, sci_resource_types[res_type]); res_type++);
56 
57 	*dot = '.';
58 
59 	if (res_type == sci_invalid_resource) {
60 		fprintf(stderr, "Invalid resource type");
61 		return -1;
62 	}
63 
64 	++dot;
65 	res_index = strtol(dot, &endptr, 10);
66 
67 	if (!*dot || *endptr) {
68 		fprintf(stderr, "Invalid resource index");
69 		return -1;
70 	}
71 
72 	if (res_index < 0) {
73 		fprintf(stderr, "Negative resource index");
74 		return -1;
75 	}
76 
77 	if (res_index >= 1000) {
78 		fprintf(stderr, "Resource index too large");
79 		return -1;
80 	}
81 
82 	f = fopen(filename, "r");
83 	if (!f) {
84 		perror("While asserting file");
85 		return -1;
86 	}
87 	fclose(f);
88 
89 	return (res_type << 11) | res_index;
90 }
91 
92 int
build_file_ids(int count,char ** names)93 build_file_ids(int count, char **names) {
94 	int i;
95 	int error = 0;
96 
97 	resource_ids = (unsigned short*) malloc(sizeof(unsigned short) * count);
98 
99 	for (i = 0; i < count; i++) {
100 		int id = test_file(names[i]);
101 		if (id < 0) {
102 			error = -1;
103 			fprintf(stderr, ": %s\n", names[i]);
104 		} else resource_ids[i] = id;
105 	}
106 
107 	return error;
108 }
109 
110 
111 static inline void
write_uint16(int fd,unsigned int uint)112 write_uint16(int fd, unsigned int uint) {
113 	unsigned char upper = (uint >> 8) & 0xff;
114 	unsigned char lower = (uint) & 0xff;
115 
116 	if ((write(fd, &upper, 1) < 1)
117 	        || (write(fd, &lower, 1) < 1)) {
118 		perror("While writing");
119 		exit(1);
120 	}
121 }
122 
123 int
write_files(int count,char ** names)124 write_files(int count, char **names) {
125 	int resource_000, resource_map;
126 	int i;
127 
128 	resource_000 = creat("resource.000", 0644);
129 	if (resource_000 < 0) {
130 		perror("While creating 'resource.000'");
131 		return -1;
132 	}
133 
134 	resource_map = creat("resource.map", 0644);
135 	if (resource_map < 0) {
136 		close(resource_000);
137 		perror("While creating 'resource.map'");
138 		return -1;
139 	}
140 
141 	for (i = 0; i < count; i++) {
142 		int fd = open(names[i], O_RDONLY);
143 		struct stat fdstat;
144 		int fdsize;
145 		unsigned char buf[512];
146 		int j;
147 		long offset = lseek(resource_000, SEEK_CUR, 0);
148 		int top_offset = (offset >> 16) & 0xffff;
149 		int bot_offset = offset & 0xffff;
150 
151 		if (fd < 0) {
152 			close(resource_000);
153 			close(resource_map);
154 			perror(names[i]);
155 			return -1;
156 		}
157 		fstat(fd, &fdstat);
158 		fdsize = fdstat.st_size;
159 
160 		write_uint16(resource_000, resource_ids[i]);
161 		write_uint16(resource_000, fdsize);
162 		write_uint16(resource_000, fdsize);
163 		write_uint16(resource_000, 0);
164 
165 		do {
166 			j = read_noThrow(fd, buf, COPY_BLOCK_SIZE);
167 			write(resource_000, buf, j);
168 		} while (j == COPY_BLOCK_SIZE);
169 		close(fd);
170 
171 		write_uint16(resource_map, resource_ids[i]);
172 		write_uint16(resource_map, bot_offset);
173 		write_uint16(resource_map, top_offset);
174 	}
175 
176 	/* Terminate resource 000 */
177 	write_uint16(resource_000, 0);
178 
179 	/* Terminate resource map */
180 	write_uint16(resource_map, 0xffff);
181 	write_uint16(resource_map, 0xffff);
182 
183 	close(resource_000);
184 	close(resource_map);
185 }
186 
187 
188 int
main(int argc,char ** argv)189 main(int argc, char **argv) {
190 	printf("scipack.c Copyright (C) 2002 Christoph Reichenbach\n"
191 	       "This program is FREE SOFTWARE. You may copy it and/or re-distribute it\n"
192 	       "according to the terms of the GNU General Public License. See LICENSING\n"
193 	       "for details.\n");
194 
195 	if (argc < 1)
196 		help();
197 
198 	if (build_file_ids(argc - 1, argv + 1))
199 		return -1;
200 
201 	if (write_files(argc - 1, argv + 1))
202 		return -1;
203 	free(resource_ids);
204 }
205