Merge pull request #12 from davel/davel/sqsh
[debian-nspark.git] / arcfs.c
blobfd6213c3316353161b3982b2e30ce870def29a04
2 /*
3 * Handle ArcFS format archives
5 * Author: Andrew Brooks, arb@comp.lancs.ac.uk
7 * $Header: arcfs.c 1.5 95/08/01 $
8 * $Log: arcfs.c,v $
9 * Revision 1.5 95/08/01 xx:xx:xx BB
10 * Fixes for Borland C/C++
11 * Removed use of floating point routines.
13 * Revision 1.4 95/01/06 11:58:45 arb
14 * Fixes for Alpha.
16 * Revision 1.3 94/12/12 17:25:25 arb
17 * Fixes for writesize.
19 * Revision 1.2 94/10/26 15:06:35 arb
20 * Fixed date and time conversion.
22 * Revision 1.1 94/02/28 21:41:23 arb
23 * Fixed header, fixed include ordering, fixed directory check,
24 * added seek to start of compressed data, fixed maxbits, ...
25 * ie. got the thing working at last!
27 * Revision 1.0 93/08/20 12:40:15 arb
28 * Initial revision
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <time.h>
34 #include <string.h>
35 #include "spark.h"
36 #ifdef POSIX
37 #include <sys/types.h>
38 #endif /* POSIX */
40 #include "arcfs.h"
42 #include "nsparkio.h"
44 #include "misc.h"
45 #include "error.h"
47 #ifndef SEEK_SET
48 #define SEEK_SET 0
49 #endif
52 * Public flag to indicate whether the current archive is ArcFS format
54 int arcfs = 0;
58 * Public number of compression bits, used in compress.c
60 int arcfs_maxbits = 0;
64 * Public size of file being extracted, used in io.c, crc.c
66 long writesize;
67 long crcsize;
71 * ArcFS header list element
73 struct arcfs_header_s
75 struct arcfs_header_s *next;
76 Byte maxbits;
77 Byte is_dir;
78 Byte info_byte;
79 Word info_word;
80 Word seek;
81 Header *header;
83 typedef struct arcfs_header_s *arcfs_header;
87 * Info byte
89 #define AFS_ENDDIR 0
90 #define AFS_DELETED 1
91 #define AFS_STORE 0x82
92 #define AFS_PACK 0x83
93 #define AFS_CRUNCH 0x88
94 #define AFS_COMPRESS 0xFF
98 * Static data
100 static int arcfs_initialised = 0;
101 static arcfs_header header_list = NULL;
102 static arcfs_header header_ptr = NULL;
106 * Convert RISC OS time to UNIX time.
107 * RISC OS time is five bytes of centiseconds since 1900.
108 * UNIX time is seconds since 1970.
109 * MSB of RISC OS time is LSB of `load' plus `exec'.
112 /* BB added extra prototype for Borland C/C++ */
114 struct tm *
115 rotm(Word load, Word exec)
117 Word low, high;
118 time_t t;
120 high = (load & 0xff) - 0x33l;
121 low = exec - 0x6e996a00l;
122 if (low > exec)
124 high--;
126 /* BB changed constant in next line to long */
127 /* cast to Word, then time_t as date stamps will all be 32 bits and time_t
128 * might be 64 bits */
129 t = (time_t)(Word)(high * 42949673L + low / 100L);
130 t -= (high / 25);
132 return (localtime(&t));
137 * Convert RISC OS time to SPARK time
140 /* BB added extra prototype for Borland C/C++ */
142 void
143 arcfs_fixtime(Header *hdr)
145 /* BB next line commented out, variable ti never used */
146 /* time_t ti; */
147 struct tm *tim;
149 /* Convert to UNIX time first (as it is easy) */
150 tim = rotm(hdr->load, hdr->exec);
152 /* Convert UNIX time to SPARK time */
153 hdr->date = (tim->tm_year - 80) << 9;
154 hdr->date |= (tim->tm_mon + 1) << 5;
155 hdr->date |= (tim->tm_mday);
156 hdr->time = (tim->tm_hour) << 11;
157 hdr->time |= (tim->tm_min) << 5;
158 hdr->time |= tim->tm_sec / 2;
163 * Read ArcFS header
165 Header *
166 arcfs_read_header(FILE *ifp)
168 static Header null_header;
169 static Word data_start;
170 Word header_length = 0;
171 Header *header;
172 Word version;
173 Word i;
174 Byte info_byte, name[12];
175 Word length, load, exec, attr, complen, info_word;
176 arcfs_header header_prev = NULL;
177 int j;
179 /* Return next header from list */
180 if (arcfs_initialised)
182 /* If end of list return an empty header structure to indicate EOF */
183 if (header_ptr == NULL)
184 return (&null_header);
186 /* Return next header in list */
187 header = header_ptr->header;
188 /* Seek to start of compressed data */
189 if ((!header_ptr->is_dir)
190 && (fseek(ifp, (long) header_ptr->seek, SEEK_SET)))
192 error("Cannot seek compressed data in this file");
193 return (&null_header);
195 /* Set up number of compression bits */
196 arcfs_maxbits = header_ptr->maxbits;
197 /*if (header_ptr->is_dir) header = &null_header; */
198 header_ptr = header_ptr->next;
199 return (header);
202 /* Header list not constructed yet, so read all headers from file */
203 arcfs_initialised = 1;
204 memset((char *) &null_header, '\0', sizeof(null_header));
205 null_header.comptype = 0;
206 header_length = read_word(ifp);
207 data_start = read_word(ifp);
208 if ((version = read_word(ifp)) > 40)
210 /* BB removed floating point routines from next line
211 This saves linking the floating point routines under DOS
212 which yields quite a reduction in executable size.
213 And it removes the need to have the FPE present under RISC OS. */
214 /* error("Archive created by a newer version of ArcFS (%.2f)",((float)version)/100); */
215 error("Archive created by a newer version of ArcFS (%d.%02d)",
216 version / 100, version % 100);
217 return (&null_header);
219 read_word(ifp); /* read/write version */
220 if ((version = read_word(ifp)) > 0)
222 error("Archive format %d not understood", version);
223 return (&null_header);
225 for (i = 0; i < 17; i++)
226 read_word(ifp); /* reserved */
228 /* Read list of headers */
229 for (i = 0; i < header_length / 36; i++)
231 /* Create list item */
232 header = (Header *) malloc(sizeof(Header));
233 header_ptr = (arcfs_header) malloc(sizeof(struct arcfs_header_s));
234 if ((header == NULL) || (header_ptr == NULL))
236 error("Out of memory");
237 return (&null_header);
240 /* Read ArcFS file header */
241 info_byte = read_byte(ifp);
242 for (j = 0; j < 11; j++)
244 name[j] = read_byte(ifp);
245 if (name[j] == PATHSEP)
246 name[j] = '_';
247 if (name[j] < ' ' || name[j] > '~')
248 name[j] = '\0';
250 name[j] = '\0';
251 length = read_word(ifp);
252 load = read_word(ifp);
253 exec = read_word(ifp);
254 attr = read_word(ifp);
255 complen = read_word(ifp);
256 info_word = read_word(ifp);
258 /* Examine, and create nspark header */
259 if (info_byte == AFS_DELETED)
261 free(header);
262 free(header_ptr);
263 continue;
265 /* BB changed next line for Borland C/C++ 4 */
266 /* header_ptr->is_dir = (info_word >> 31); */
267 #ifdef __MSDOS__
268 header_ptr->is_dir = (Halfword) (info_word >> 31);
269 #else
270 header_ptr->is_dir = (info_word >> 31);
271 #endif
272 header_ptr->info_byte = info_byte;
273 header_ptr->info_word = info_word;
274 /* BB changed next line for Borland C/C++ 4 */
275 /* header_ptr->maxbits = (attr & 0xff00) >> 8; */
276 #ifdef __MSDOS__
277 header_ptr->maxbits = (Halfword) (attr & 0xff00) >> 8;
278 #else
279 header_ptr->maxbits = (attr & 0xff00) >> 8;
280 #endif
281 /* BB changed constant in next line to long. */
282 header_ptr->seek = (info_word & 0x7fffffffL) + data_start;
283 header->comptype = info_byte;
284 strcpy(header->name, (char *) name);
285 header->complen = complen;
286 header->date = 0;
287 header->time = 0;
288 header->crc = (Halfword) (attr >> 16);
289 header->origlen = length;
290 header->load = load;
291 header->exec = exec;
292 header->attr = attr & 0xff;
294 arcfs_fixtime(header);
296 if (info_byte == AFS_ENDDIR)
298 /* Just return comptype == 0 */
299 *header = null_header;
300 header_ptr->is_dir = 0;
301 header_ptr->seek = 0;
304 /* If it is an ArcFS directory then convert to a Spark directory */
305 if (header_ptr->is_dir)
307 /* Make sure filetype is DDC */
308 header->comptype = CT_NOTCOMP2;
309 /* BB changed constant in next line to long. */
310 header->load = 0xfffddcffL;
313 /* Add list item to list */
314 /* Doing it here ensures that deleted items are not added */
315 header_ptr->header = header;
316 if (header_list == NULL)
317 header_list = header_ptr;
318 else
319 header_prev->next = header_ptr;
320 header_prev = header_ptr;
321 #ifdef DEBUGGING
322 print_header(header);
323 #endif
326 /* Return first element */
327 header_ptr = header_list;
328 header = header_ptr->header;
329 /* Seek to start of data for first element */
330 if ((!header_ptr->is_dir)
331 && (fseek(ifp, (long) header_ptr->seek, SEEK_SET)))
333 error("Cannot seek compressed data in this file");
334 return (&null_header);
336 /* Set up number of compression bits */
337 arcfs_maxbits = header_ptr->maxbits;
338 /*if (header_ptr->is_dir) header = &null_header; */
339 header_ptr = header_ptr->next;
340 return (header);