dissect.fat 3.14.dev2__tar.gz → 3.14.dev4__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {dissect_fat-3.14.dev2 → dissect_fat-3.14.dev4}/.git-blame-ignore-revs +2 -0
- {dissect_fat-3.14.dev2 → dissect_fat-3.14.dev4}/PKG-INFO +3 -3
- {dissect_fat-3.14.dev2 → dissect_fat-3.14.dev4}/dissect/fat/__init__.py +15 -6
- dissect_fat-3.14.dev4/dissect/fat/base.py +333 -0
- dissect_fat-3.14.dev4/dissect/fat/c_exfat.py +176 -0
- dissect_fat-3.14.dev4/dissect/fat/c_exfat.pyi +341 -0
- dissect_fat-3.14.dev4/dissect/fat/c_fat.py +133 -0
- dissect_fat-3.14.dev4/dissect/fat/c_fat.pyi +212 -0
- dissect_fat-3.14.dev2/dissect/fat/exceptions.py → dissect_fat-3.14.dev4/dissect/fat/exception.py +2 -2
- dissect_fat-3.14.dev4/dissect/fat/exfat.py +352 -0
- dissect_fat-3.14.dev4/dissect/fat/fat.py +364 -0
- {dissect_fat-3.14.dev2 → dissect_fat-3.14.dev4}/dissect.fat.egg-info/PKG-INFO +3 -3
- {dissect_fat-3.14.dev2 → dissect_fat-3.14.dev4}/dissect.fat.egg-info/SOURCES.txt +16 -9
- dissect_fat-3.14.dev4/dissect.fat.egg-info/requires.txt +6 -0
- dissect_fat-3.14.dev4/dissect.fat.egg-info/scm_file_list.json +39 -0
- dissect_fat-3.14.dev4/dissect.fat.egg-info/scm_version.json +8 -0
- {dissect_fat-3.14.dev2 → dissect_fat-3.14.dev4}/pyproject.toml +3 -2
- dissect_fat-3.14.dev4/tests/_data/exfat_32m_cluster.bin.gz +0 -0
- dissect_fat-3.14.dev4/tests/_data/exfat_4k_sector.bin.gz +0 -0
- {dissect_fat-3.14.dev2/tests/data → dissect_fat-3.14.dev4/tests/_data}/fat32.bin +1 -1
- dissect_fat-3.14.dev4/tests/_util.py +7 -0
- dissect_fat-3.14.dev4/tests/test_exception.py +21 -0
- dissect_fat-3.14.dev4/tests/test_exfat.py +179 -0
- dissect_fat-3.14.dev4/tests/test_fat.py +116 -0
- dissect_fat-3.14.dev2/dissect/fat/c_exfat.py +0 -158
- dissect_fat-3.14.dev2/dissect/fat/c_fat.py +0 -108
- dissect_fat-3.14.dev2/dissect/fat/exfat.py +0 -320
- dissect_fat-3.14.dev2/dissect/fat/fat.py +0 -467
- dissect_fat-3.14.dev2/dissect.fat.egg-info/requires.txt +0 -6
- dissect_fat-3.14.dev2/tests/conftest.py +0 -43
- dissect_fat-3.14.dev2/tests/test_exceptions.py +0 -21
- dissect_fat-3.14.dev2/tests/test_exfat.py +0 -78
- dissect_fat-3.14.dev2/tests/test_fat.py +0 -73
- {dissect_fat-3.14.dev2 → dissect_fat-3.14.dev4}/COPYRIGHT +0 -0
- {dissect_fat-3.14.dev2 → dissect_fat-3.14.dev4}/LICENSE +0 -0
- {dissect_fat-3.14.dev2 → dissect_fat-3.14.dev4}/MANIFEST.in +0 -0
- {dissect_fat-3.14.dev2 → dissect_fat-3.14.dev4}/README.md +0 -0
- {dissect_fat-3.14.dev2 → dissect_fat-3.14.dev4}/dissect.fat.egg-info/dependency_links.txt +0 -0
- {dissect_fat-3.14.dev2 → dissect_fat-3.14.dev4}/dissect.fat.egg-info/top_level.txt +0 -0
- {dissect_fat-3.14.dev2 → dissect_fat-3.14.dev4}/setup.cfg +0 -0
- {dissect_fat-3.14.dev2 → dissect_fat-3.14.dev4}/tests/__init__.py +0 -0
- {dissect_fat-3.14.dev2/tests/data → dissect_fat-3.14.dev4/tests/_data}/exfat.bin +0 -0
- {dissect_fat-3.14.dev2/tests/data → dissect_fat-3.14.dev4/tests/_data}/exfat4m.bin +0 -0
- {dissect_fat-3.14.dev2/tests/data → dissect_fat-3.14.dev4/tests/_data}/fat12.bin +0 -0
- {dissect_fat-3.14.dev2/tests/data → dissect_fat-3.14.dev4/tests/_data}/fat16.bin +0 -0
- {dissect_fat-3.14.dev2 → dissect_fat-3.14.dev4}/tests/_docs/Makefile +0 -0
- {dissect_fat-3.14.dev2 → dissect_fat-3.14.dev4}/tests/_docs/conf.py +0 -0
- {dissect_fat-3.14.dev2 → dissect_fat-3.14.dev4}/tests/_docs/index.rst +0 -0
- {dissect_fat-3.14.dev2 → dissect_fat-3.14.dev4}/tox.ini +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: dissect.fat
|
|
3
|
-
Version: 3.14.
|
|
3
|
+
Version: 3.14.dev4
|
|
4
4
|
Summary: A Dissect module implementing parsers for the FAT and exFAT file systems, commonly used on flash memory based storage devices and UEFI partitions
|
|
5
5
|
Author-email: Dissect Team <dissect@fox-it.com>
|
|
6
6
|
License-Expression: AGPL-3.0-or-later
|
|
@@ -21,10 +21,10 @@ Requires-Python: >=3.10
|
|
|
21
21
|
Description-Content-Type: text/markdown
|
|
22
22
|
License-File: LICENSE
|
|
23
23
|
License-File: COPYRIGHT
|
|
24
|
-
Requires-Dist: dissect.cstruct<5
|
|
24
|
+
Requires-Dist: dissect.cstruct<6,>=5.dev
|
|
25
25
|
Requires-Dist: dissect.util<4,>=3
|
|
26
26
|
Provides-Extra: dev
|
|
27
|
-
Requires-Dist: dissect.cstruct<
|
|
27
|
+
Requires-Dist: dissect.cstruct<6.0.dev,>=5.0.dev; extra == "dev"
|
|
28
28
|
Requires-Dist: dissect.util<4.0.dev,>=3.0.dev; extra == "dev"
|
|
29
29
|
Dynamic: license-file
|
|
30
30
|
|
|
@@ -1,31 +1,40 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
|
-
from dissect.fat.
|
|
3
|
+
from dissect.fat.base import FatType
|
|
4
|
+
from dissect.fat.c_exfat import c_exfat
|
|
5
|
+
from dissect.fat.c_fat import c_fat
|
|
6
|
+
from dissect.fat.exception import (
|
|
4
7
|
BadClusterError,
|
|
5
|
-
|
|
8
|
+
DeletedDirectoryError,
|
|
6
9
|
Error,
|
|
7
10
|
FileNotFoundError,
|
|
8
11
|
FreeClusterError,
|
|
9
|
-
|
|
12
|
+
InvalidBootSector,
|
|
10
13
|
InvalidDirectoryError,
|
|
11
14
|
InvalidHeaderMagic,
|
|
12
15
|
LastEmptyDirectoryError,
|
|
13
16
|
NotADirectoryError,
|
|
14
17
|
)
|
|
15
18
|
from dissect.fat.exfat import ExFAT
|
|
16
|
-
from dissect.fat.fat import FATFS
|
|
19
|
+
from dissect.fat.fat import FAT12, FAT16, FAT32, FATFS
|
|
17
20
|
|
|
18
21
|
__all__ = [
|
|
22
|
+
"FAT12",
|
|
23
|
+
"FAT16",
|
|
24
|
+
"FAT32",
|
|
19
25
|
"FATFS",
|
|
20
26
|
"BadClusterError",
|
|
21
|
-
"
|
|
27
|
+
"DeletedDirectoryError",
|
|
22
28
|
"Error",
|
|
23
29
|
"ExFAT",
|
|
30
|
+
"FatType",
|
|
24
31
|
"FileNotFoundError",
|
|
25
32
|
"FreeClusterError",
|
|
26
|
-
"
|
|
33
|
+
"InvalidBootSector",
|
|
27
34
|
"InvalidDirectoryError",
|
|
28
35
|
"InvalidHeaderMagic",
|
|
29
36
|
"LastEmptyDirectoryError",
|
|
30
37
|
"NotADirectoryError",
|
|
38
|
+
"c_exfat",
|
|
39
|
+
"c_fat",
|
|
31
40
|
]
|
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import datetime
|
|
4
|
+
import math
|
|
5
|
+
import struct
|
|
6
|
+
from enum import Enum, auto
|
|
7
|
+
from functools import cache, cached_property, lru_cache
|
|
8
|
+
from operator import itemgetter
|
|
9
|
+
from typing import TYPE_CHECKING, Any, BinaryIO, TypeVar
|
|
10
|
+
|
|
11
|
+
from dissect.util.stream import RunlistStream
|
|
12
|
+
|
|
13
|
+
from dissect.fat.c_fat import c_fat
|
|
14
|
+
from dissect.fat.exception import (
|
|
15
|
+
BadClusterError,
|
|
16
|
+
DeletedDirectoryError,
|
|
17
|
+
FileNotFoundError,
|
|
18
|
+
FreeClusterError,
|
|
19
|
+
LastEmptyDirectoryError,
|
|
20
|
+
NotADirectoryError,
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
if TYPE_CHECKING:
|
|
24
|
+
from collections.abc import Iterator
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class FatType(Enum):
|
|
28
|
+
FAT12 = auto()
|
|
29
|
+
FAT16 = auto()
|
|
30
|
+
FAT32 = auto()
|
|
31
|
+
EXFAT = auto()
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class FS:
|
|
35
|
+
type: FatType
|
|
36
|
+
|
|
37
|
+
sector_size: int
|
|
38
|
+
cluster_size: int
|
|
39
|
+
|
|
40
|
+
fat: FAT
|
|
41
|
+
data: BinaryIO
|
|
42
|
+
|
|
43
|
+
volume_label: str
|
|
44
|
+
volume_id: str
|
|
45
|
+
|
|
46
|
+
root: BaseDirectoryEntry
|
|
47
|
+
|
|
48
|
+
def get(self, path: str, dirent: BaseDirectoryEntry | None = None) -> BaseDirectoryEntry:
|
|
49
|
+
"""Get a directory entry by path.
|
|
50
|
+
|
|
51
|
+
Args:
|
|
52
|
+
path: The path to the directory entry.
|
|
53
|
+
dirent: The directory entry to start the search from. Defaults to the root directory.
|
|
54
|
+
"""
|
|
55
|
+
dirent = dirent or self.root
|
|
56
|
+
|
|
57
|
+
# Programmatically we will often use the `/` separator, so replace it with the native path separator of FAT
|
|
58
|
+
# `/` is an illegal character in FAT filenames, so it's safe to replace
|
|
59
|
+
parts = path.replace("/", "\\").split("\\")
|
|
60
|
+
for part in parts:
|
|
61
|
+
if not part:
|
|
62
|
+
continue
|
|
63
|
+
|
|
64
|
+
if dirent is self.root and part in (".", ".."):
|
|
65
|
+
continue
|
|
66
|
+
|
|
67
|
+
part_upper = part.upper()
|
|
68
|
+
for child in dirent.iterdir():
|
|
69
|
+
if self._match(part_upper, child):
|
|
70
|
+
dirent = child
|
|
71
|
+
break
|
|
72
|
+
else:
|
|
73
|
+
raise FileNotFoundError(f"File not found: {path}")
|
|
74
|
+
|
|
75
|
+
return dirent
|
|
76
|
+
|
|
77
|
+
def _match(self, name: str, dirent: BaseDirectoryEntry) -> bool:
|
|
78
|
+
"""Return whether the given name matches the directory entry's name."""
|
|
79
|
+
return name == dirent.name.upper()
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
class BaseDirectoryEntry:
|
|
83
|
+
"""Base class for FAT directory entries.
|
|
84
|
+
|
|
85
|
+
Args:
|
|
86
|
+
fs: The filesystem this directory entry belongs to.
|
|
87
|
+
fh: File-like object positioned at the start of the directory entry to read.
|
|
88
|
+
"""
|
|
89
|
+
|
|
90
|
+
def __init__(self, fs: FS, fh: BinaryIO | None):
|
|
91
|
+
self.fs = fs
|
|
92
|
+
|
|
93
|
+
if fh is not None:
|
|
94
|
+
self.dirent, self.secondary_dirents = self._read_dirent(fh)
|
|
95
|
+
else:
|
|
96
|
+
self.dirent, self.secondary_dirents = None, []
|
|
97
|
+
|
|
98
|
+
self.dataruns = cache(self.dataruns)
|
|
99
|
+
|
|
100
|
+
def __repr__(self) -> str:
|
|
101
|
+
return f"<{self.__class__.__name__} name={self.name!r}>"
|
|
102
|
+
|
|
103
|
+
def _read_dirent(self, fh: BinaryIO | None) -> tuple[Any | None, list[Any]]:
|
|
104
|
+
"""Read a directory entry from the given file handle, handling secondary entries if present.
|
|
105
|
+
|
|
106
|
+
Args:
|
|
107
|
+
fh: File-like object positioned at the start of the directory entry to read.
|
|
108
|
+
|
|
109
|
+
Returns:
|
|
110
|
+
A tuple containing the directory entry and a list of secondary directory entries.
|
|
111
|
+
"""
|
|
112
|
+
raise NotImplementedError
|
|
113
|
+
|
|
114
|
+
@cached_property
|
|
115
|
+
def name(self) -> str:
|
|
116
|
+
"""Return the canonical name of the directory entry."""
|
|
117
|
+
raise NotImplementedError
|
|
118
|
+
|
|
119
|
+
@cached_property
|
|
120
|
+
def attr(self) -> int:
|
|
121
|
+
"""Return the attribute value of the directory entry."""
|
|
122
|
+
raise NotImplementedError
|
|
123
|
+
|
|
124
|
+
@cached_property
|
|
125
|
+
def size(self) -> int:
|
|
126
|
+
"""Return the size of the directory entry in bytes."""
|
|
127
|
+
return sum(map(itemgetter(1), self.dataruns())) * self.fs.cluster_size
|
|
128
|
+
|
|
129
|
+
@cached_property
|
|
130
|
+
def cluster(self) -> int:
|
|
131
|
+
"""Return the starting cluster number of the directory entry."""
|
|
132
|
+
raise NotImplementedError
|
|
133
|
+
|
|
134
|
+
@cached_property
|
|
135
|
+
def ctime(self) -> datetime.datetime:
|
|
136
|
+
"""Return the creation time of the directory entry."""
|
|
137
|
+
return datetime.datetime(1980, 1, 1) # noqa: DTZ001
|
|
138
|
+
|
|
139
|
+
@cached_property
|
|
140
|
+
def atime(self) -> datetime.datetime:
|
|
141
|
+
"""Return the last access time of the directory entry."""
|
|
142
|
+
return datetime.datetime(1980, 1, 1) # noqa: DTZ001
|
|
143
|
+
|
|
144
|
+
@cached_property
|
|
145
|
+
def mtime(self) -> datetime.datetime:
|
|
146
|
+
"""Return the last modification time of the directory entry."""
|
|
147
|
+
return datetime.datetime(1980, 1, 1) # noqa: DTZ001
|
|
148
|
+
|
|
149
|
+
def is_readonly(self) -> bool:
|
|
150
|
+
"""Return whether the directory entry is read-only."""
|
|
151
|
+
return bool(self.attr & c_fat.FAT_DIRENT_ATTR_READ_ONLY)
|
|
152
|
+
|
|
153
|
+
def is_hidden(self) -> bool:
|
|
154
|
+
"""Return whether the directory entry is hidden."""
|
|
155
|
+
return bool(self.attr & c_fat.FAT_DIRENT_ATTR_HIDDEN)
|
|
156
|
+
|
|
157
|
+
def is_system(self) -> bool:
|
|
158
|
+
"""Return whether the directory entry is a system file."""
|
|
159
|
+
return bool(self.attr & c_fat.FAT_DIRENT_ATTR_SYSTEM)
|
|
160
|
+
|
|
161
|
+
def is_volume_id(self) -> bool:
|
|
162
|
+
"""Return whether the directory entry is a volume ID."""
|
|
163
|
+
return bool(self.attr & c_fat.FAT_DIRENT_ATTR_VOLUME_ID)
|
|
164
|
+
|
|
165
|
+
def is_directory(self) -> bool:
|
|
166
|
+
"""Return whether the directory entry is a directory."""
|
|
167
|
+
return bool(self.attr & c_fat.FAT_DIRENT_ATTR_DIRECTORY)
|
|
168
|
+
|
|
169
|
+
def is_archive(self) -> bool:
|
|
170
|
+
"""Return whether the directory entry has the archive attribute set."""
|
|
171
|
+
return bool(self.attr & c_fat.FAT_DIRENT_ATTR_ARCHIVE)
|
|
172
|
+
|
|
173
|
+
def listdir(self) -> list[str]:
|
|
174
|
+
"""Return a list of names of the entries in the directory."""
|
|
175
|
+
return [entry.name for entry in self.iterdir()]
|
|
176
|
+
|
|
177
|
+
def iterdir(self) -> Iterator[BaseDirectoryEntry]:
|
|
178
|
+
"""Yield the directory entries in the directory."""
|
|
179
|
+
if not self.is_directory():
|
|
180
|
+
raise NotADirectoryError(self.name)
|
|
181
|
+
|
|
182
|
+
with self.open() as fh:
|
|
183
|
+
yield from self._iterdir(fh)
|
|
184
|
+
|
|
185
|
+
def _iterdir(self, fh: BinaryIO) -> Iterator[BaseDirectoryEntry]:
|
|
186
|
+
"""Yield the directory entries in the directory from the given file handle.
|
|
187
|
+
|
|
188
|
+
Args:
|
|
189
|
+
fh: File-like object positioned at the start of the directory data to read.
|
|
190
|
+
"""
|
|
191
|
+
raise NotImplementedError
|
|
192
|
+
|
|
193
|
+
def dataruns(self) -> list[tuple[int, int]]:
|
|
194
|
+
"""Return the runlist of the directory entry."""
|
|
195
|
+
return [] if self.cluster == FAT.FREE_CLUSTER else list(self.fs.fat.runlist(self.cluster))
|
|
196
|
+
|
|
197
|
+
def open(self) -> RunlistStream:
|
|
198
|
+
"""Open the directory entry for reading."""
|
|
199
|
+
return RunlistStream(self.fs.data, self.dataruns(), self.size, self.fs.cluster_size)
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
T = TypeVar("T", bound=BaseDirectoryEntry)
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
def iter_dirent(cls: type[T], fs: FS, fh: BinaryIO) -> Iterator[T]:
|
|
206
|
+
while True:
|
|
207
|
+
try:
|
|
208
|
+
yield cls(fs, fh)
|
|
209
|
+
except DeletedDirectoryError: # noqa: PERF203
|
|
210
|
+
continue
|
|
211
|
+
except (LastEmptyDirectoryError, EOFError):
|
|
212
|
+
break
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
_H = struct.Struct("<H")
|
|
216
|
+
_I = struct.Struct("<I")
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
class FAT:
|
|
220
|
+
"""File Allocation Table (FAT) implementation.
|
|
221
|
+
|
|
222
|
+
Args:
|
|
223
|
+
fh: File-like object containing the FAT.
|
|
224
|
+
size: Size of the FAT in bytes.
|
|
225
|
+
bits_per_entry: Number of bits per FAT entry.
|
|
226
|
+
"""
|
|
227
|
+
|
|
228
|
+
DATA_CLUSTER_MIN = 0x00000002
|
|
229
|
+
DATA_CLUSTER_MAX = 0xFFFFFFEF
|
|
230
|
+
END_OF_CLUSTER_MIN = 0xFFFFFFF8
|
|
231
|
+
END_OF_CLUSTER_MAX = 0xFFFFFFFF
|
|
232
|
+
BAD_CLUSTER = 0xFFFFFFF7
|
|
233
|
+
FREE_CLUSTER = 0x00000000
|
|
234
|
+
|
|
235
|
+
FAT12_EOC = 0x000000FF0
|
|
236
|
+
|
|
237
|
+
def __init__(self, fh: BinaryIO, size: int, bits_per_entry: int):
|
|
238
|
+
self.fh = fh
|
|
239
|
+
self.bits_per_entry = bits_per_entry
|
|
240
|
+
self.entry_count = int(size // math.ceil(self.bits_per_entry / 8))
|
|
241
|
+
|
|
242
|
+
self.get = lru_cache(4096)(self.get)
|
|
243
|
+
|
|
244
|
+
def get(self, cluster: int) -> int | None:
|
|
245
|
+
"""Get the FAT entry for a given cluster.
|
|
246
|
+
|
|
247
|
+
Args:
|
|
248
|
+
cluster: The cluster number to get the FAT entry for.
|
|
249
|
+
"""
|
|
250
|
+
if cluster >= self.entry_count:
|
|
251
|
+
raise ValueError(f"Cluster exceeds FAT entry count: {cluster} >= {self.entry_count}")
|
|
252
|
+
|
|
253
|
+
if self.bits_per_entry == 12:
|
|
254
|
+
offset_in_fat = cluster + (cluster // 2)
|
|
255
|
+
self.fh.seek(offset_in_fat)
|
|
256
|
+
value = _H.unpack(self.fh.read(2))[0]
|
|
257
|
+
return value >> 4 if cluster & 1 else value & 0x0FFF
|
|
258
|
+
|
|
259
|
+
if self.bits_per_entry == 16:
|
|
260
|
+
offset_in_fat = cluster * 2
|
|
261
|
+
self.fh.seek(offset_in_fat)
|
|
262
|
+
return _H.unpack(self.fh.read(2))[0]
|
|
263
|
+
|
|
264
|
+
if self.bits_per_entry == 28:
|
|
265
|
+
offset_in_fat = cluster * 4
|
|
266
|
+
self.fh.seek(offset_in_fat)
|
|
267
|
+
return _I.unpack(self.fh.read(4))[0] & 0x0FFFFFFF
|
|
268
|
+
|
|
269
|
+
if self.bits_per_entry == 32:
|
|
270
|
+
offset_in_fat = cluster * 4
|
|
271
|
+
self.fh.seek(offset_in_fat)
|
|
272
|
+
return _I.unpack(self.fh.read(4))[0]
|
|
273
|
+
|
|
274
|
+
raise ValueError("Unsupported FAT type")
|
|
275
|
+
|
|
276
|
+
def chain(self, cluster: int) -> Iterator[int]:
|
|
277
|
+
"""Yield the cluster chain starting from a given cluster.
|
|
278
|
+
|
|
279
|
+
Args:
|
|
280
|
+
cluster: The starting cluster number.
|
|
281
|
+
"""
|
|
282
|
+
bits = self.bits_per_entry
|
|
283
|
+
|
|
284
|
+
while True:
|
|
285
|
+
value = self.get(cluster)
|
|
286
|
+
if self.DATA_CLUSTER_MIN <= value <= mask(self.DATA_CLUSTER_MAX, bits):
|
|
287
|
+
yield cluster
|
|
288
|
+
|
|
289
|
+
# FAT12 special EOC
|
|
290
|
+
if self.bits_per_entry == 12 and value == self.FAT12_EOC:
|
|
291
|
+
yield cluster
|
|
292
|
+
break
|
|
293
|
+
|
|
294
|
+
if mask(self.END_OF_CLUSTER_MIN, bits) <= value <= mask(self.END_OF_CLUSTER_MAX, bits):
|
|
295
|
+
yield cluster
|
|
296
|
+
break
|
|
297
|
+
|
|
298
|
+
if value == mask(self.BAD_CLUSTER, bits):
|
|
299
|
+
raise BadClusterError(cluster)
|
|
300
|
+
|
|
301
|
+
if value == self.FREE_CLUSTER:
|
|
302
|
+
raise FreeClusterError(cluster)
|
|
303
|
+
|
|
304
|
+
cluster = value
|
|
305
|
+
|
|
306
|
+
def runlist(self, cluster: int, *, reserved: int = 2) -> Iterator[tuple[int, int]]:
|
|
307
|
+
"""Create a runlist from a cluster chain.
|
|
308
|
+
|
|
309
|
+
On normal FAT filesystems, the first two clusters are reserved, so substract those.
|
|
310
|
+
Also combine consecutive clusters for a more efficient runlist.
|
|
311
|
+
|
|
312
|
+
Args:
|
|
313
|
+
cluster: The starting cluster number.
|
|
314
|
+
"""
|
|
315
|
+
chain = self.chain(cluster)
|
|
316
|
+
|
|
317
|
+
run_start = next(chain) - reserved
|
|
318
|
+
run_size = 1
|
|
319
|
+
|
|
320
|
+
for cl in chain:
|
|
321
|
+
if cl == run_start + run_size:
|
|
322
|
+
run_size += 1
|
|
323
|
+
else:
|
|
324
|
+
yield (run_start, run_size)
|
|
325
|
+
run_start = cl - reserved
|
|
326
|
+
run_size = 1
|
|
327
|
+
else:
|
|
328
|
+
yield (run_start, run_size)
|
|
329
|
+
|
|
330
|
+
|
|
331
|
+
@lru_cache(128)
|
|
332
|
+
def mask(v: int, bits: int) -> int:
|
|
333
|
+
return v & ((1 << bits) - 1)
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
# Resources:
|
|
2
|
+
# - https://learn.microsoft.com/en-us/windows/win32/fileio/exfat-specification
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
from dissect.cstruct import cstruct
|
|
7
|
+
|
|
8
|
+
exfat_def = """
|
|
9
|
+
typedef struct _BOOT_SECTOR {
|
|
10
|
+
UCHAR JumpBoot[3];
|
|
11
|
+
UCHAR FileSystemName[8];
|
|
12
|
+
UCHAR MustBeZero[53];
|
|
13
|
+
ULONGLONG PartitionOffset;
|
|
14
|
+
ULONGLONG VolumeLength;
|
|
15
|
+
ULONG FatOffset;
|
|
16
|
+
ULONG FatLength;
|
|
17
|
+
ULONG ClusterHeapOffset;
|
|
18
|
+
ULONG ClusterCount;
|
|
19
|
+
ULONG FirstClusterOfRootDirectory;
|
|
20
|
+
ULONG VolumeSerialNumber;
|
|
21
|
+
USHORT FileSystemRevision;
|
|
22
|
+
USHORT VolumeFlags;
|
|
23
|
+
UCHAR BytesPerSectorShift;
|
|
24
|
+
UCHAR SectorsPerClusterShift;
|
|
25
|
+
UCHAR NumberOfFats;
|
|
26
|
+
UCHAR DriveSelect;
|
|
27
|
+
UCHAR PercentInUse;
|
|
28
|
+
UCHAR Reserved[7];
|
|
29
|
+
UCHAR BootCode[390];
|
|
30
|
+
USHORT BootSignature;
|
|
31
|
+
} BOOT_SECTOR;
|
|
32
|
+
|
|
33
|
+
#define EXFAT_DIRENT_SIZE 32
|
|
34
|
+
|
|
35
|
+
#define EXFAT_DIRENT_TYPE_END 0x00
|
|
36
|
+
#define EXFAT_DIRENT_TYPE_UNUSED 0x80
|
|
37
|
+
#define EXFAT_DIRENT_TYPE_ALLOC_BITMAP 0x81
|
|
38
|
+
#define EXFAT_DIRENT_TYPE_UPCASE 0x82
|
|
39
|
+
#define EXFAT_DIRENT_TYPE_VOLUME_LABEL 0x83
|
|
40
|
+
#define EXFAT_DIRENT_TYPE_FILE 0x85
|
|
41
|
+
#define EXFAT_DIRENT_TYPE_VOLUME_GUID 0xA0
|
|
42
|
+
#define EXFAT_DIRENT_TYPE_TEXFAT_PADDING 0xA1
|
|
43
|
+
#define EXFAT_DIRENT_TYPE_STREAM_EXT 0xC0
|
|
44
|
+
#define EXFAT_DIRENT_TYPE_FILE_NAME 0xC1
|
|
45
|
+
#define EXFAT_DIRENT_TYPE_VENDOR_EXT 0xE0
|
|
46
|
+
#define EXFAT_DIRENT_TYPE_VENDOR_ALLOC 0xE1
|
|
47
|
+
|
|
48
|
+
#define EXFAT_DIRENT_FLAG_ALLOC_POSSIBLE 0x01
|
|
49
|
+
#define EXFAT_DIRENT_FLAG_NO_FAT_CHAIN 0x02
|
|
50
|
+
|
|
51
|
+
typedef struct _DIRENT {
|
|
52
|
+
UCHAR EntryType;
|
|
53
|
+
UCHAR CustomDefined[31];
|
|
54
|
+
} DIRENT;
|
|
55
|
+
|
|
56
|
+
typedef struct _GENERIC_PRIMARY_DIRENT {
|
|
57
|
+
UCHAR EntryType;
|
|
58
|
+
UCHAR SecondaryCount;
|
|
59
|
+
USHORT SetChecksum;
|
|
60
|
+
USHORT GeneralPrimaryFlags;
|
|
61
|
+
UCHAR CustomDefined[14];
|
|
62
|
+
ULONG FirstCluster;
|
|
63
|
+
ULONGLONG DataLength;
|
|
64
|
+
} GENERIC_PRIMARY_DIRENT;
|
|
65
|
+
|
|
66
|
+
typedef struct _GENERIC_SECONDARY_DIRENT {
|
|
67
|
+
UCHAR EntryType;
|
|
68
|
+
UCHAR GeneralSecondaryFlags;
|
|
69
|
+
UCHAR CustomDefined[18];
|
|
70
|
+
ULONG FirstCluster;
|
|
71
|
+
ULONGLONG DataLength;
|
|
72
|
+
} GENERIC_SECONDARY_DIRENT;
|
|
73
|
+
|
|
74
|
+
typedef struct _ALLOC_BITMAP_DIRENT {
|
|
75
|
+
UCHAR EntryType;
|
|
76
|
+
UCHAR BitmapFlags;
|
|
77
|
+
UCHAR Reserved[18];
|
|
78
|
+
ULONG FirstCluster;
|
|
79
|
+
ULONGLONG DataLength;
|
|
80
|
+
} ALLOC_BITMAP_DIRENT;
|
|
81
|
+
|
|
82
|
+
typedef struct _UPCASE_DIRENT {
|
|
83
|
+
UCHAR EntryType;
|
|
84
|
+
UCHAR Reserved1[3];
|
|
85
|
+
ULONG TableChecksum;
|
|
86
|
+
UCHAR Reserved2[12];
|
|
87
|
+
ULONG FirstCluster;
|
|
88
|
+
ULONGLONG DataLength;
|
|
89
|
+
} UPCASE_DIRENT;
|
|
90
|
+
|
|
91
|
+
typedef struct _VOLUME_LABEL_DIRENT {
|
|
92
|
+
UCHAR EntryType;
|
|
93
|
+
UCHAR CharacterCount;
|
|
94
|
+
UCHAR VolumeLabel[22];
|
|
95
|
+
UCHAR Reserved[8];
|
|
96
|
+
} VOLUME_LABEL_DIRENT;
|
|
97
|
+
|
|
98
|
+
typedef struct _FILE_DIRENT {
|
|
99
|
+
UCHAR EntryType;
|
|
100
|
+
UCHAR SecondaryCount;
|
|
101
|
+
USHORT SetChecksum;
|
|
102
|
+
USHORT FileAttributes;
|
|
103
|
+
UCHAR Reserved1[2];
|
|
104
|
+
ULONG CreateTimestamp;
|
|
105
|
+
ULONG LastModifiedTimestamp;
|
|
106
|
+
ULONG LastAccessedTimestamp;
|
|
107
|
+
UCHAR Create10msIncrement;
|
|
108
|
+
UCHAR LastModified10msIncrement;
|
|
109
|
+
UCHAR CreateUtcOffset;
|
|
110
|
+
UCHAR LastModifiedUtcOffset;
|
|
111
|
+
UCHAR LastAccessedUtcOffset;
|
|
112
|
+
UCHAR Reserved2[7];
|
|
113
|
+
} FILE_DIRENT;
|
|
114
|
+
|
|
115
|
+
typedef struct _VOLUME_GUID_DIRENT {
|
|
116
|
+
UCHAR EntryType;
|
|
117
|
+
UCHAR SecondaryCount;
|
|
118
|
+
USHORT SetChecksum;
|
|
119
|
+
USHORT GeneralPrimaryFlags;
|
|
120
|
+
UCHAR VolumeGuid[16];
|
|
121
|
+
UCHAR Reserved[10];
|
|
122
|
+
} VOLUME_GUID_DIRENT;
|
|
123
|
+
|
|
124
|
+
typedef struct _STREAM_EXT_DIRENT {
|
|
125
|
+
UCHAR EntryType;
|
|
126
|
+
UCHAR GeneralSecondaryFlags;
|
|
127
|
+
UCHAR Reserved1;
|
|
128
|
+
UCHAR NameLength;
|
|
129
|
+
USHORT NameHash;
|
|
130
|
+
UCHAR Reserved2[2];
|
|
131
|
+
ULONGLONG ValidDataLength;
|
|
132
|
+
UCHAR Reserved3[4];
|
|
133
|
+
ULONG FirstCluster;
|
|
134
|
+
ULONGLONG DataLength;
|
|
135
|
+
} STREAM_EXT_DIRENT;
|
|
136
|
+
|
|
137
|
+
typedef struct _FILE_NAME_DIRENT {
|
|
138
|
+
UCHAR EntryType;
|
|
139
|
+
UCHAR GeneralSecondaryFlags;
|
|
140
|
+
UCHAR FileName[30];
|
|
141
|
+
} FILE_NAME_DIRENT;
|
|
142
|
+
|
|
143
|
+
typedef struct _VENDOR_EXT_DIRENT {
|
|
144
|
+
UCHAR EntryType;
|
|
145
|
+
UCHAR GeneralSecondaryFlags;
|
|
146
|
+
UCHAR VendorGuid[16];
|
|
147
|
+
UCHAR VendorDefined[14];
|
|
148
|
+
} VENDOR_EXT_DIRENT;
|
|
149
|
+
|
|
150
|
+
typedef struct _VENDOR_ALLOC_DIRENT {
|
|
151
|
+
UCHAR EntryType;
|
|
152
|
+
UCHAR GeneralSecondaryFlags;
|
|
153
|
+
UCHAR VendorGuid[16];
|
|
154
|
+
UCHAR VendorDefined[2];
|
|
155
|
+
ULONG FirstCluster;
|
|
156
|
+
ULONGLONG DataLength;
|
|
157
|
+
} VENDOR_ALLOC_DIRENT;
|
|
158
|
+
"""
|
|
159
|
+
|
|
160
|
+
c_exfat = cstruct().load(exfat_def)
|
|
161
|
+
|
|
162
|
+
PRIMARY_DIRENT = (
|
|
163
|
+
c_exfat.ALLOC_BITMAP_DIRENT
|
|
164
|
+
| c_exfat.UPCASE_DIRENT
|
|
165
|
+
| c_exfat.VOLUME_LABEL_DIRENT
|
|
166
|
+
| c_exfat.FILE_DIRENT
|
|
167
|
+
| c_exfat.VOLUME_GUID_DIRENT
|
|
168
|
+
| c_exfat.GENERIC_PRIMARY_DIRENT
|
|
169
|
+
)
|
|
170
|
+
SECONDARY_DIRENT = (
|
|
171
|
+
c_exfat.STREAM_EXT_DIRENT
|
|
172
|
+
| c_exfat.FILE_NAME_DIRENT
|
|
173
|
+
| c_exfat.VENDOR_EXT_DIRENT
|
|
174
|
+
| c_exfat.VENDOR_ALLOC_DIRENT
|
|
175
|
+
| c_exfat.GENERIC_SECONDARY_DIRENT
|
|
176
|
+
)
|