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