forked from realpython/materials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecoder.py
More file actions
39 lines (27 loc) · 954 Bytes
/
Copy pathdecoder.py
File metadata and controls
39 lines (27 loc) · 954 Bytes
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
"""
Secret file decoder.
"""
from itertools import islice, takewhile
from pathlib import Path
from typing import Iterator
from .bitmap import Bitmap
class DecodingError(Exception):
pass
def decode(bitmap: Bitmap) -> None:
"""Extract a secret file from the bitmap."""
if bitmap.reserved_field <= 0:
raise DecodingError("Secret file not found in the bitmap")
iterator = secret_bytes(bitmap)
filename = "".join(map(chr, takewhile(lambda x: x != 0, iterator)))
with Path(filename).open(mode="wb") as file:
file.write(bytes(islice(iterator, bitmap.reserved_field)))
print(f"Extracted a secret file: {filename}")
def secret_bytes(bitmap) -> Iterator[int]:
"""Return an iterator over secret bytes."""
for eight_bytes in bitmap.byte_slices:
yield sum(
[
(byte & 1) << (7 - i)
for i, byte in enumerate(bitmap[eight_bytes])
]
)