comparison lwlink/readfiles.c @ 171:d610b8aef91b

Added LWAR skeleton
author lost
date Sun, 01 Mar 2009 19:37:03 +0000
parents 106c2fe3c9d9
children 0395e6fd67e9
comparison
equal deleted inserted replaced
170:bf69160da467 171:d610b8aef91b
28 28
29 #include <argp.h> 29 #include <argp.h>
30 #include <errno.h> 30 #include <errno.h>
31 #include <stdio.h> 31 #include <stdio.h>
32 #include <stdlib.h> 32 #include <stdlib.h>
33 #include <string.h>
33 34
34 #include "lwlink.h" 35 #include "lwlink.h"
35 #include "util.h" 36 #include "util.h"
36 37
37 void read_lwobj16v0(fileinfo_t *fn); 38 void read_lwobj16v0(fileinfo_t *fn);
39 void read_lwar1v(fileinfo_t *fn);
38 40
39 /* 41 /*
40 The logic of reading the entire file into memory is simple. All the symbol 42 The logic of reading the entire file into memory is simple. All the symbol
41 names in the file are NUL terminated strings and can be used directly without 43 names in the file are NUL terminated strings and can be used directly without
42 making additional copies. 44 making additional copies.
43 */ 45 */
46 void read_file(fileinfo_t *fn)
47 {
48 if (!memcmp(fn -> filedata, "LWOBJ16", 8))
49 {
50 // read v0 LWOBJ16 file
51 read_lwobj16v0(fn);
52 }
53 else if (!memcmp(fn -> filedata, "LWAR1V", 6))
54 {
55 // archive file
56 read_lwar1v(fn);
57 }
58 else
59 {
60 fprintf(stderr, "%s: unknown file format\n", fn -> filename);
61 exit(1);
62 }
63 }
64
44 void read_files(void) 65 void read_files(void)
45 { 66 {
46 int i; 67 int i;
47 long size; 68 long size;
48 FILE *f; 69 FILE *f;
71 exit(1); 92 exit(1);
72 } 93 }
73 94
74 fclose(f); 95 fclose(f);
75 96
76 if (!memcmp(inputfiles[i] -> filedata, "LWOBJ16", 8)) 97 read_file(inputfiles[i]);
77 {
78 // read v0 LWOBJ16 file
79 read_lwobj16v0(inputfiles[i]);
80 }
81 else
82 {
83 fprintf(stderr, "%s: unknown file format\n", inputfiles[i] -> filename);
84 exit(1);
85 }
86 } 98 }
87 } 99 }
88 100
89 // this macro is used to bail out if we run off the end of the file data 101 // this macro is used to bail out if we run off the end of the file data
90 // while parsing - it keeps the code below cleaner 102 // while parsing - it keeps the code below cleaner
105 return fp; 117 return fp;
106 } 118 }
107 // the function below can be switched to dealing with data coming from a 119 // the function below can be switched to dealing with data coming from a
108 // source other than an in-memory byte pool by adjusting the input data 120 // source other than an in-memory byte pool by adjusting the input data
109 // in "fn" and the above two macros 121 // in "fn" and the above two macros
122
110 void read_lwobj16v0(fileinfo_t *fn) 123 void read_lwobj16v0(fileinfo_t *fn)
111 { 124 {
112 unsigned char *fp; 125 unsigned char *fp;
113 long cc; 126 long cc;
114 section_t *s; 127 section_t *s;
300 for (i = 0; i < s -> codesize; i++) 313 for (i = 0; i < s -> codesize; i++)
301 NEXTBYTE(); 314 NEXTBYTE();
302 } 315 }
303 } 316 }
304 } 317 }
318
319 /*
320 Read an archive file - this will create a "sub" record and farm out the
321 parsing of the sub files to the regular file parsers
322
323 The archive file format consists of the 6 byte magic number followed by a
324 series of records as follows:
325
326 - NUL terminated file name
327 - 32 bit file length in big endian order
328 - the file data
329
330 An empty file name indicates the end of the file.
331
332 */
333 void read_lwar1v(fileinfo_t *fn)
334 {
335 unsigned long cc = 6;
336 unsigned long flen;
337 unsigned long l;
338 for (;;)
339 {
340 if (cc >= fn -> filesize || !(fn -> filedata[cc]))
341 return;
342
343 for (l = cc; cc < fn -> filesize && fn -> filedata[cc]; l++)
344 /* do nothing */ ;
345
346 if (cc >= fn -> filesize)
347 {
348 fprintf(stderr, "Malformed archive file %s.\n", fn -> filename);
349 exit(1);
350 }
351
352 if (cc + 4 > fn -> filesize)
353 return;
354
355 flen = (fn -> filedata[cc++] << 24) | (fn -> filedata[cc++] << 16)
356 | (fn -> filedata[cc++] << 8) | (fn -> filedata[cc]);
357
358 if (flen == 0)
359 return;
360
361 if (cc + flen > fn -> filesize)
362 {
363 fprintf(stderr, "Malformed archive file %s.\n", fn -> filename);
364 exit(1);
365 }
366
367 // add the "sub" input file
368 fn -> subs = lw_realloc(fn -> subs, sizeof(fileinfo_t *) * (fn -> nsubs + 1));
369 fn -> subs[fn -> nsubs] = lw_malloc(sizeof(fileinfo_t));
370 memset(fn -> subs[fn -> nsubs], 0, sizeof(fileinfo_t));
371 fn -> subs[fn -> nsubs] -> filedata = fn -> filedata + cc;
372 fn -> subs[fn -> nsubs] -> filesize = flen;
373 fn -> subs[fn -> nsubs] -> filename = lw_strdup(fn -> filedata + l);
374 fn -> subs[fn -> nsubs] -> parent = fn;
375 read_file(fn -> subs[fn -> nsubs]);
376 fn -> nsubs++;
377 }
378 }