-
Notifications
You must be signed in to change notification settings - Fork 58
/
extract_blocks.py
executable file
·67 lines (54 loc) · 1.82 KB
/
extract_blocks.py
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
#!/bin/env python3
"""
A very rudimentary DwarFS image parser
This currently doesn't do very much apart from extracting the block data
(*without* headers) and writing the blocks to individual files.
Note that this *cannot* currently handle DwarFS image offsets.
The main idea behind this script is to allow experimenting with different
compression algorithms without having to have full-fledged integration
into DwarFS. Essentially, this allows you to individually compress the
blocks externally and see which algorithms/configurations work best.
"""
import sys
from struct import unpack
if len(sys.argv) != 3:
print(f"USAGE: {__file__} <image> <basename>")
raise SystemExit(1)
sectypes = {
0: "block",
7: "schema",
8: "metadata",
9: "index",
10: "history",
}
compalgs = {
0: "none",
1: "lzma",
2: "zstd",
3: "lz4",
4: "lz4hc",
}
_, image, basename = sys.argv
with open(image, "rb") as image:
while True:
header = image.read(0x40)
if len(header) < 0x40:
break
ident, major, minor, sha512, xxh3, secno, sectype, compalg, blocklen = unpack(
"<6sBB32s8sLHHQ", header
)
if ident != b"DWARFS":
print("error: expected dwarfs header")
raise SystemExit(1)
if sectype not in sectypes:
print(f"error: unexpected section type ({sectype})")
raise SystemExit(1)
if compalg not in compalgs:
print(f"error: unexpected compression algorithm ({compalg})")
raise SystemExit(1)
print(
f"{ident.decode('ascii')} v{major}.{minor} [{secno}] {sectypes[sectype]} ({compalgs[compalg]}) {blocklen} bytes"
)
block = image.read(blocklen)
with open(f"{basename}{sectypes[sectype]}{secno}", "wb") as out:
out.write(block)