-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathidm_payloads.py
More file actions
33 lines (24 loc) · 1.04 KB
/
idm_payloads.py
File metadata and controls
33 lines (24 loc) · 1.04 KB
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
"""
Helpers for building iDotMatrix image payloads.
These are intentionally kept compatible with idotmatrix.modules.image.Image._createPayloads
so they can be swapped out later if the library exposes a public API.
"""
from typing import List
import struct
def split_into_chunks(data: bytes, chunk_size: int) -> List[bytes]:
return [data[i : i + chunk_size] for i in range(0, len(data), chunk_size)]
def create_payloads(png_data: bytes) -> bytearray:
"""
Wrap PNG bytes into the device's custom payload framing.
"""
png_chunks = split_into_chunks(png_data, 4096)
# Matches the library's behavior: len(data) + number of chunks
idk = len(png_data) + len(png_chunks)
idk_bytes = struct.pack("h", idk) # 16‑bit signed int
png_len_bytes = struct.pack("i", len(png_data)) # 32‑bit signed int
payloads = bytearray()
for i, chunk in enumerate(png_chunks):
flag = 2 if i > 0 else 0
payload = idk_bytes + bytearray([0, 0, flag]) + png_len_bytes + chunk
payloads.extend(payload)
return payloads