annotate mysteryffs/extract.c @ 205:8bdc87c0fc03

memwrite-grep hack-utility written
author Mychaela Falconia <falcon@ivan.Harhan.ORG>
date Thu, 10 Dec 2015 07:29:35 +0000
parents d19b4e20ff9f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
25
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
1 /*
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
2 * This program is the logical culmination of the MysteryFFS reverse eng
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
3 * experiments: it walks the FFS directory tree from the root down,
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
4 * recreates a matching tree in the local Unix file system, and
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
5 * extracts the full content of all data files.
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
6 *
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
7 * All acquired understanding of the MysteryFFS structure is tested
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
8 * in the process.
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
9 */
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
10
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
11 #include <sys/types.h>
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
12 #include <sys/file.h>
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
13 #include <sys/stat.h>
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
14 #include <endian.h>
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
15 #include <ctype.h>
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
16 #include <stdio.h>
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
17 #include <string.h>
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
18 #include <strings.h>
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
19 #include <stdlib.h>
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
20 #include <unistd.h>
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
21
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
22 typedef unsigned char u8;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
23 typedef unsigned short u16;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
24 typedef unsigned int u32;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
25
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
26 u8 mysteryffs_hdr[6] = {'F', 'f', 's', '#', 0x10, 0x02};
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
27
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
28 /* actual MysteryFFS on-media structure */
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
29 struct mysteryffs_index {
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
30 u16 len;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
31 u8 unknown_b1;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
32 u8 type;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
33 u16 descend;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
34 u16 sibling;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
35 u32 dataptr;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
36 u16 unknown_w1;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
37 u16 unknown_w2;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
38 };
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
39
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
40 /* our own struct for convenience */
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
41 struct objinfo {
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
42 u16 entryno;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
43 struct mysteryffs_index *idxrec;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
44 u8 *dataptr;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
45 u32 offset;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
46 u16 len;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
47 u8 type;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
48 u16 descend;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
49 u16 sibling;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
50 };
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
51
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
52 char *imgfile;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
53 u32 eraseblk_size;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
54 int total_blocks;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
55 u32 total_img_size;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
56 u8 *image, *indexblk;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
57
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
58 char workpath[512];
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
59
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
60 read_img_file()
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
61 {
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
62 int fd;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
63 struct stat st;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
64
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
65 fd = open(imgfile, O_RDONLY);
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
66 if (fd < 0) {
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
67 perror(imgfile);
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
68 exit(1);
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
69 }
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
70 fstat(fd, &st);
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
71 if (!S_ISREG(st.st_mode)) {
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
72 fprintf(stderr, "%s is not a regular file\n", imgfile);
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
73 exit(1);
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
74 }
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
75 if (st.st_size < total_img_size) {
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
76 fprintf(stderr, "%s has fewer than 0x%x bytes\n", imgfile,
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
77 total_img_size);
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
78 exit(1);
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
79 }
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
80 image = malloc(total_img_size);
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
81 if (!image) {
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
82 perror("malloc");
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
83 exit(1);
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
84 }
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
85 read(fd, image, total_img_size);
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
86 close(fd);
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
87 }
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
88
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
89 find_index_block()
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
90 {
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
91 int i;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
92 u8 *ptr;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
93
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
94 for (ptr = image, i = 0; i < total_blocks; i++, ptr += eraseblk_size) {
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
95 if (bcmp(ptr, mysteryffs_hdr, 6))
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
96 continue;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
97 if (ptr[8] != 0xAB)
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
98 continue;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
99 printf("Found index in erase block #%d (offset %x)\n", i,
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
100 ptr - image);
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
101 indexblk = ptr;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
102 return(0);
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
103 }
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
104 fprintf(stderr, "could not find a MysteryFFS index block in %s\n",
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
105 imgfile);
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
106 exit(1);
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
107 }
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
108
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
109 get_index_entry(oi)
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
110 struct objinfo *oi;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
111 {
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
112 struct mysteryffs_index *le;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
113
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
114 if (oi->entryno >= (eraseblk_size >> 4)) {
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
115 fprintf(stderr,
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
116 "error: index block pointer %x past the erase block size!\n",
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
117 oi->entryno);
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
118 exit(1);
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
119 }
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
120 le = (struct mysteryffs_index *) indexblk + oi->entryno;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
121 oi->idxrec = le;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
122 oi->len = le16toh(le->len);
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
123 oi->type = le->type;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
124 oi->descend = le16toh(le->descend);
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
125 oi->sibling = le16toh(le->sibling);
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
126 return(0);
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
127 }
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
128
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
129 validate_chunk(oi)
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
130 struct objinfo *oi;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
131 {
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
132 u32 dptr;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
133
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
134 if (oi->len & 0xF || !oi->len) {
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
135 fprintf(stderr, "index entry #%x: invalid chunk length\n",
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
136 oi->entryno);
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
137 exit(1);
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
138 }
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
139 dptr = le32toh(oi->idxrec->dataptr);
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
140 if (dptr > 0x0FFFFFFF) {
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
141 invdptr: fprintf(stderr, "index entry #%x: invalid data pointer\n",
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
142 oi->entryno);
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
143 exit(1);
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
144 }
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
145 dptr <<= 4;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
146 if (dptr >= total_img_size - oi->len)
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
147 goto invdptr;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
148 oi->offset = dptr;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
149 oi->dataptr = image + dptr;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
150 return(0);
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
151 }
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
152
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
153 validate_obj_name(oi)
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
154 struct objinfo *oi;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
155 {
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
156 u8 *cp;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
157 int cnt;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
158
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
159 for (cp = oi->dataptr, cnt = 0; ; cp++, cnt++) {
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
160 if (cnt >= oi->len) {
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
161 fprintf(stderr,
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
162 "object at index %x: name expected at %x: length overrun\n",
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
163 oi->entryno, oi->offset);
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
164 exit(1);
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
165 }
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
166 if (!*cp)
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
167 break;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
168 if (*cp < '!' || *cp > '~') {
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
169 fprintf(stderr,
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
170 "object at index %x: name expected at %x: bad character\n",
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
171 oi->entryno, oi->offset);
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
172 exit(1);
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
173 }
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
174 }
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
175 if (!cnt) {
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
176 fprintf(stderr,
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
177 "object at index %x: name expected at %x: null string\n",
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
178 oi->entryno, oi->offset);
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
179 exit(1);
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
180 }
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
181 return(0);
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
182 }
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
183
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
184 name_safe_for_extract(oi)
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
185 struct objinfo *oi;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
186 {
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
187 char *s;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
188
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
189 s = (char *)oi->dataptr;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
190 if (!isalnum(*s) && *s != '_')
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
191 return(0);
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
192 for (s++; *s; s++)
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
193 if (!isalnum(*s) && *s != '_' && *s != '.')
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
194 return(0);
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
195 return(1);
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
196 }
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
197
26
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
198 u8 *
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
199 find_end_of_chunk(ch)
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
200 struct objinfo *ch;
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
201 {
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
202 u8 *p;
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
203 int i;
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
204
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
205 p = ch->dataptr + ch->len;
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
206 for (i = 1; i <= 16; i++) {
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
207 if (!p[-i])
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
208 return(p - i);
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
209 if (p[-1] != 0xFF)
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
210 break;
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
211 }
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
212 fprintf(stderr,
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
213 "chunk starting at %x (index entry %x): no valid termination found\n",
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
214 ch->offset, ch->entryno);
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
215 exit(1);
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
216 }
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
217
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
218 void
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
219 dump_head_chunk(fd, ch)
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
220 struct objinfo *ch;
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
221 {
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
222 u8 *endname, *endchunk;
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
223
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
224 endname = (u8 *) index((char *)ch->dataptr, '\0') + 1;
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
225 endchunk = find_end_of_chunk(ch);
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
226 if (endchunk <= endname)
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
227 return;
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
228 write(fd, endname, endchunk - endname);
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
229 }
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
230
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
231 void
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
232 dump_extra_chunk(fd, ch)
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
233 struct objinfo *ch;
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
234 {
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
235 u8 *endchunk;
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
236
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
237 endchunk = find_end_of_chunk(ch);
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
238 write(fd, ch->dataptr, endchunk - ch->dataptr);
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
239 }
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
240
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
241 extract_file(head)
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
242 struct objinfo *head;
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
243 {
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
244 int fd;
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
245 int ent;
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
246 struct objinfo ch;
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
247
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
248 fd = open(workpath + 1, O_WRONLY|O_CREAT|O_TRUNC, 0666);
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
249 if (fd < 0) {
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
250 perror(workpath + 1);
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
251 exit(1);
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
252 }
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
253 dump_head_chunk(fd, head);
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
254 for (ent = head->descend; ent != 0xFFFF; ent = ch.descend) {
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
255 ch.entryno = ent;
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
256 get_index_entry(&ch);
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
257 if (ch.type != 0xF4) {
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
258 fprintf(stderr,
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
259 "file continuation object at index %x: type %02X != expected F4\n",
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
260 ent, ch.type);
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
261 exit(1);
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
262 }
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
263 validate_chunk(&ch);
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
264 dump_extra_chunk(fd, &ch);
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
265 if (ch.sibling != 0xFFFF)
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
266 printf("warning: file continuation object (index %x) has a non-nil sibling pointer\n",
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
267 ent);
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
268 }
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
269 close(fd);
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
270 }
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
271
25
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
272 dump_dir(firstent, path_prefix)
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
273 {
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
274 int ent;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
275 struct objinfo obj;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
276
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
277 for (ent = firstent; ent != 0xFFFF; ent = obj.sibling) {
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
278 obj.entryno = ent;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
279 get_index_entry(&obj);
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
280 if (!obj.type) /* skip deleted objects w/o further validation */
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
281 continue;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
282 validate_chunk(&obj);
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
283 validate_obj_name(&obj);
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
284 if (path_prefix + strlen(obj.dataptr) + 2 > sizeof workpath) {
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
285 fprintf(stderr,
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
286 "handling object at index %x, name \"%s\": path buffer overflow\n",
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
287 obj.entryno, (char *)obj.dataptr);
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
288 exit(1);
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
289 }
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
290 sprintf(workpath + path_prefix, "/%s", (char *)obj.dataptr);
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
291 switch (obj.type) {
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
292 case 0xF2:
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
293 /* directory */
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
294 printf("dir: %s\n", workpath);
26
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
295 if (!name_safe_for_extract(&obj)) {
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
296 printf("name contains unsafe characters; subtree skipped\n");
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
297 continue;
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
298 }
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
299 if (mkdir(workpath + 1, 0777) < 0) {
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
300 perror(workpath + 1);
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
301 exit(1);
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
302 }
25
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
303 dump_dir(obj.descend, strlen(workpath));
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
304 continue;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
305 case 0xF1:
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
306 /* regular file */
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
307 printf("file: %s\n", workpath);
26
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
308 if (!name_safe_for_extract(&obj)) {
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
309 printf("name contains unsafe characters; file skipped\n");
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
310 continue;
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
311 }
d19b4e20ff9f MysteryFFS: extract utility seems to work
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 25
diff changeset
312 extract_file(&obj);
25
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
313 continue;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
314 case 0xE1:
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
315 /* special .journal file */
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
316 if (path_prefix == 0 &&
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
317 !strcmp((char *)obj.dataptr, ".journal"))
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
318 printf("skipping /.journal\n");
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
319 else
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
320 printf("skipping unexpected E1 file: %s\n",
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
321 workpath);
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
322 continue;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
323 default:
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
324 printf("%s (index entry #%x): unexpected type %02X; skipping\n",
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
325 workpath, obj.entryno, obj.type);
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
326 continue;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
327 }
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
328 }
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
329 }
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
330
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
331 dump_root()
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
332 {
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
333 struct objinfo root;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
334
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
335 root.entryno = 1;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
336 get_index_entry(&root);
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
337 validate_chunk(&root);
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
338 validate_obj_name(&root);
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
339 printf("Root node name: %s\n", (char *)root.dataptr);
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
340 if (root.type != 0xF2) {
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
341 fprintf(stderr,
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
342 "error: index entry #1 (expected root dir) is not a directory\n");
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
343 exit(1);
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
344 }
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
345 if (root.sibling != 0xFFFF)
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
346 printf("warning: root entry has a non-nil sibling pointer\n");
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
347 dump_dir(root.descend, 0);
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
348 }
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
349
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
350 main(argc, argv)
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
351 char **argv;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
352 {
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
353 if (argc != 4) {
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
354 fprintf(stderr, "usage: %s imgfile blksize nblocks\n", argv[0]);
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
355 exit(1);
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
356 }
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
357 imgfile = argv[1];
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
358 eraseblk_size = strtoul(argv[2], 0, 0);
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
359 total_blocks = strtoul(argv[3], 0, 0);
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
360 total_img_size = eraseblk_size * total_blocks;
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
361 read_img_file();
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
362 find_index_block();
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
363 dump_root();
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
364 exit(0);
ae5337f881e3 MysteryFFS: beginning of the extract utility
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
365 }