-
Notifications
You must be signed in to change notification settings - Fork 1
/
detect.c
259 lines (215 loc) · 6.38 KB
/
detect.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
* detect.c
* Detection dispatching
*
* Copyright (c) 2003 Christoph Pfisterer
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "global.h"
/*
* external detection functions
*/
/* in amiga.c */
void detect_amiga_partmap(SECTION *section, int level);
void detect_amiga_fs(SECTION *section, int level);
/* in apple.c */
void detect_apple_partmap(SECTION *section, int level);
void detect_apple_volume(SECTION *section, int level);
void detect_udif(SECTION *section, int level);
/* in atari.c */
void detect_atari_partmap(SECTION *section, int level);
/* in dos.c */
void detect_dos_partmap(SECTION *section, int level);
void detect_gpt_partmap(SECTION *section, int level);
void detect_fat(SECTION *section, int level);
void detect_ntfs(SECTION *section, int level);
void detect_hpfs(SECTION *section, int level);
void detect_dos_loader(SECTION *section, int level);
/* in cdrom.c */
void detect_iso(SECTION *section, int level);
void detect_cdrom_misc(SECTION *section, int level);
/* in udf.c */
void detect_udf(SECTION *section, int level);
/* in linux.c */
void detect_ext23(SECTION *section, int level);
void detect_reiser(SECTION *section, int level);
void detect_reiser4(SECTION *section, int level);
void detect_linux_raid(SECTION *section, int level);
void detect_linux_lvm(SECTION *section, int level);
void detect_linux_lvm2(SECTION *section, int level);
void detect_linux_swap(SECTION *section, int level);
void detect_linux_misc(SECTION *section, int level);
void detect_linux_loader(SECTION *section, int level);
/* in unix.c */
void detect_jfs(SECTION *section, int level);
void detect_xfs(SECTION *section, int level);
void detect_ufs(SECTION *section, int level);
void detect_sysv(SECTION *section, int level);
void detect_bsd_disklabel(SECTION *section, int level);
void detect_bsd_loader(SECTION *section, int level);
void detect_solaris_disklabel(SECTION *section, int level);
void detect_solaris_vtoc(SECTION *section, int level);
void detect_qnx(SECTION *section, int level);
void detect_vxfs(SECTION *section, int level);
/* in beos.c */
void detect_bfs(SECTION *section, int level);
void detect_beos_loader(SECTION *section, int level);
/* in compressed.c */
void detect_compressed(SECTION *section, int level);
/* in cdimage.c */
void detect_cdimage(SECTION *section, int level);
/* in vpc.c */
void detect_vhd(SECTION *section, int level);
/* in cloop.c */
void detect_cloop(SECTION *section, int level);
/* in archives.c */
void detect_archive(SECTION *section, int level);
/* in blank.c */
void detect_blank(SECTION *section, int level);
/*
* list of detectors
*/
DETECTOR detectors[] = {
/* 1: disk image formats */
detect_vhd, /* may stop */
detect_cdimage, /* may stop */
detect_cloop,
detect_udif,
/* 2: boot code */
detect_linux_loader,
detect_bsd_loader,
detect_dos_loader,
detect_beos_loader,
/* 3: partition tables */
detect_bsd_disklabel, /* may stop, recurses with FLAG_IN_DISKLABEL */
detect_solaris_disklabel, /* may stop, recurses with FLAG_IN_DISKLABEL */
detect_solaris_vtoc,
detect_amiga_partmap,
detect_apple_partmap,
detect_atari_partmap,
detect_dos_partmap,
detect_gpt_partmap,
/* 4: file systems */
detect_amiga_fs,
detect_apple_volume,
detect_fat,
detect_ntfs,
detect_hpfs,
detect_udf,
detect_cdrom_misc,
detect_iso,
detect_ext23,
detect_reiser,
detect_reiser4,
detect_linux_raid,
detect_linux_lvm,
detect_linux_lvm2,
detect_linux_swap,
detect_linux_misc,
detect_jfs,
detect_xfs,
detect_ufs,
detect_sysv,
detect_qnx,
detect_vxfs,
detect_bfs,
/* 5: file formats */
detect_archive,
detect_compressed, /* this is here because of boot disks */
/* 6: blank formatted disk */
detect_blank,
NULL };
/*
* internal stuff
*/
static void detect(SECTION *section, int level);
static int stop_flag = 0;
/*
* analyze a given source
*/
void analyze_source(SOURCE *s, int level)
{
SECTION section;
/* Allow custom analyzing using special info available to the
data source implementation. The analyze() function must either
call through to analyze_source_special() or return zero. */
if (s->analyze != NULL) {
if ((*s->analyze)(s, level))
return;
}
section.source = s;
section.pos = 0;
section.size = s->size_known ? s->size : 0;
section.flags = 0;
detect(§ion, level);
}
/*
* analyze part of a given source
*/
void analyze_source_special(SOURCE *s, int level, u8 pos, u8 size)
{
SECTION section;
section.source = s;
section.pos = pos;
section.size = size;
section.flags = 0;
detect(§ion, level);
}
/*
* recursively analyze a portion of a SECTION
*/
void analyze_recursive(SECTION *section, int level,
u8 rel_pos, u8 size, int flags)
{
SOURCE *s;
SECTION rs;
/* sanity */
if (rel_pos == 0 && (flags & FLAG_IN_DISKLABEL) == 0)
return;
s = section->source;
if (s->size_known && (section->pos + rel_pos >= s->size))
return;
rs.source = s;
rs.pos = section->pos + rel_pos;
rs.size = size;
rs.flags = section->flags | flags;
detect(&rs, level);
}
/*
* detection dispatching
*/
static void detect(SECTION *section, int level)
{
int i;
/* run the modularized detectors */
for (i = 0; detectors[i] && !stop_flag; i++)
(*detectors[i])(section, level);
stop_flag = 0;
}
/*
* break the detection loop
*/
void stop_detect(void)
{
stop_flag = 1;
}
/* EOF */