Fix WINNT build.
[debian-nspark.git] / arcfs.c
blob1a41a24b6fe636a9779e28a78656666eb57fec88
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 #if defined(RISCOS) || defined(__MSDOS__)
41 #include <stdlib.h>
42 #include <string.h>
43 #endif /* RISCOS || __MSDOS__ */
44 #include "arcfs.h"
46 #include "nsparkio.h"
48 #include "misc.h"
50 #ifndef SEEK_SET
51 #define SEEK_SET 0
52 #endif
55 * Public flag to indicate whether the current archive is ArcFS format
57 int arcfs = 0;
61 * Public number of compression bits, used in compress.c
63 int arcfs_maxbits = 0;
67 * Public size of file being extracted, used in io.c, crc.c
69 long writesize;
70 long crcsize;
74 * ArcFS header list element
76 struct arcfs_header_s
78 struct arcfs_header_s *next;
79 Byte maxbits;
80 Byte is_dir;
81 Byte info_byte;
82 Word info_word;
83 Word seek;
84 Header *header;
86 typedef struct arcfs_header_s *arcfs_header;
90 * Info byte
92 #define AFS_ENDDIR 0
93 #define AFS_DELETED 1
94 #define AFS_STORE 0x82
95 #define AFS_PACK 0x83
96 #define AFS_CRUNCH 0x88
97 #define AFS_COMPRESS 0xFF
101 * Static data
103 static int arcfs_initialised = 0;
104 static arcfs_header header_list = NULL;
105 static arcfs_header header_ptr = NULL;
109 * Convert RISC OS time to UNIX time.
110 * RISC OS time is five bytes of centiseconds since 1900.
111 * UNIX time is seconds since 1970.
112 * MSB of RISC OS time is LSB of `load' plus `exec'.
115 /* BB added extra prototype for Borland C/C++ */
117 struct tm *
118 rotm(Word load, Word exec)
120 Word low, high;
121 time_t t;
123 high = (load & 0xff) - 0x33l;
124 low = exec - 0x6e996a00l;
125 /* BB changed constant in next line to long */
126 /* cast to Word, then time_t as date stamps will all be 32 bits and time_t
127 * might be 64 bits */
128 t = (time_t)(Word)(high * 42949673L + low / 100L);
129 return (localtime(&t));
134 * Convert RISC OS time to SPARK time
137 /* BB added extra prototype for Borland C/C++ */
139 void
140 arcfs_fixtime(Header *hdr)
142 /* BB next line commented out, variable ti never used */
143 /* time_t ti; */
144 struct tm *tim;
146 /* Convert to UNIX time first (as it is easy) */
147 tim = rotm(hdr->load, hdr->exec);
149 /* Convert UNIX time to SPARK time */
150 hdr->date = (tim->tm_year - 80) << 9;
151 hdr->date |= (tim->tm_mon + 1) << 5;
152 hdr->date |= (tim->tm_mday);
153 hdr->time = (tim->tm_hour) << 11;
154 hdr->time |= (tim->tm_min) << 5;
155 hdr->time |= (tim->tm_sec);
160 * Read ArcFS header
162 Header *
163 arcfs_read_header(FILE *ifp)
165 static Header null_header;
166 static Word data_start;
167 Word header_length = 0;
168 Header *header;
169 Word version;
170 int i;
171 Byte info_byte, name[12];
172 Word length, load, exec, attr, complen, info_word;
173 arcfs_header header_prev = NULL;
174 int j;
176 /* Return next header from list */
177 if (arcfs_initialised)
179 /* If end of list return an empty header structure to indicate EOF */
180 if (header_ptr == NULL)
181 return (&null_header);
183 /* Return next header in list */
184 header = header_ptr->header;
185 /* Seek to start of compressed data */
186 if ((!header_ptr->is_dir)
187 && (fseek(ifp, (long) header_ptr->seek, SEEK_SET)))
189 printf("Cannot seek compressed data in this file\n");
190 return (&null_header);
192 /* Set up number of compression bits */
193 arcfs_maxbits = header_ptr->maxbits;
194 /*if (header_ptr->is_dir) header = &null_header; */
195 header_ptr = header_ptr->next;
196 return (header);
199 /* Header list not constructed yet, so read all headers from file */
200 arcfs_initialised = 1;
201 memset((char *) &null_header, '\0', sizeof(null_header));
202 null_header.comptype = 0;
203 header_length = read_word(ifp);
204 data_start = read_word(ifp);
205 if ((version = read_word(ifp)) > 40)
207 /* BB removed floating point routines from next line
208 This saves linking the floating point routines under DOS
209 which yields quite a reduction in executable size.
210 And it removes the need to have the FPE present under RISC OS. */
211 /* printf("Archive created by a newer version of ArcFS (%.2f)\n",((float)version)/100); */
212 printf("Archive created by a newer version of ArcFS (%d.%02d)\n",
213 version / 100, version % 100);
214 return (&null_header);
216 read_word(ifp); /* read/write version */
217 if ((version = read_word(ifp)) > 0)
219 printf("Archive format %d not understood\n", version);
220 return (&null_header);
222 for (i = 0; i < 17; i++)
223 read_word(ifp); /* reserved */
225 /* Read list of headers */
226 for (i = 0; i < header_length / 36; i++)
228 /* Create list item */
229 header = (Header *) malloc(sizeof(Header));
230 header_ptr = (arcfs_header) malloc(sizeof(struct arcfs_header_s));
231 if ((header == NULL) || (header_ptr == NULL))
233 printf("Out of memory\n");
234 return (&null_header);
237 /* Read ArcFS file header */
238 info_byte = read_byte(ifp);
239 for (j = 0; j < 11; j++)
241 name[j] = read_byte(ifp);
242 if (name[j] == PATHSEP)
243 name[j] = '_';
244 if (name[j] < ' ' || name[j] > '~')
245 name[j] = '\0';
247 name[j] = '\0';
248 length = read_word(ifp);
249 load = read_word(ifp);
250 exec = read_word(ifp);
251 attr = read_word(ifp);
252 complen = read_word(ifp);
253 info_word = read_word(ifp);
255 /* Examine, and create nspark header */
256 if (info_byte == AFS_DELETED)
258 free(header);
259 free(header_ptr);
260 continue;
262 /* BB changed next line for Borland C/C++ 4 */
263 /* header_ptr->is_dir = (info_word >> 31); */
264 #ifdef __MSDOS__
265 header_ptr->is_dir = (Halfword) (info_word >> 31);
266 #else
267 header_ptr->is_dir = (info_word >> 31);
268 #endif
269 header_ptr->info_byte = info_byte;
270 header_ptr->info_word = info_word;
271 /* BB changed next line for Borland C/C++ 4 */
272 /* header_ptr->maxbits = (attr & 0xff00) >> 8; */
273 #ifdef __MSDOS__
274 header_ptr->maxbits = (Halfword) (attr & 0xff00) >> 8;
275 #else
276 header_ptr->maxbits = (attr & 0xff00) >> 8;
277 #endif
278 /* BB changed constant in next line to long. */
279 header_ptr->seek = (info_word & 0x7fffffffL) + data_start;
280 header->comptype = info_byte;
281 strcpy(header->name, (char *) name);
282 header->complen = complen;
283 header->date = 0;
284 header->time = 0;
285 header->crc = (Halfword) (attr >> 16);
286 header->origlen = length;
287 header->load = load;
288 header->exec = exec;
289 header->attr = attr & 0xff;
291 arcfs_fixtime(header);
293 if (info_byte == AFS_ENDDIR)
295 /* Just return comptype == 0 */
296 *header = null_header;
297 header_ptr->is_dir = 0;
298 header_ptr->seek = 0;
301 /* If it is an ArcFS directory then convert to a Spark directory */
302 if (header_ptr->is_dir)
304 /* Make sure filetype is DDC */
305 header->comptype = CT_NOTCOMP2;
306 /* BB changed constant in next line to long. */
307 header->load = 0xfffddcffL;
310 /* Add list item to list */
311 /* Doing it here ensures that deleted items are not added */
312 header_ptr->header = header;
313 if (header_list == NULL)
314 header_list = header_ptr;
315 else
316 header_prev->next = header_ptr;
317 header_prev = header_ptr;
318 #ifdef DEBUGGING
319 print_header(header);
320 #endif
323 /* Return first element */
324 header_ptr = header_list;
325 header = header_ptr->header;
326 /* Seek to start of data for first element */
327 if ((!header_ptr->is_dir)
328 && (fseek(ifp, (long) header_ptr->seek, SEEK_SET)))
330 printf("Cannot seek compressed data in this file\n");
331 return (&null_header);
333 /* Set up number of compression bits */
334 arcfs_maxbits = header_ptr->maxbits;
335 /*if (header_ptr->is_dir) header = &null_header; */
336 header_ptr = header_ptr->next;
337 return (header);