unleashed-firmware/scripts/flipper/assets/heatshrink_stream.py
hedger fcbcb6b5a8
Updater: resource compression (#3716)
* toolbox: compress: moved decompressor implementation to separate func
* toolbox: compress: callback-based api; cli: storage unpack command
* toolbox: compress: separate r/w contexts for stream api
* targets: f18: sync API
* compress: naming fixes & cleanup
* toolbox: compress: using hs buffer size for stream buffers
* toolbox: tar: heatshrink stream mode
* toolbox: compress: docs & small cleanup
* toolbox: tar: header support for .hs; updater: now uses .hs for resources; .hs.tar: now rewindable
* toolbox: compress: fixed hs stream tail handling
* updater: reworked progress for resources cleanup; rebalanced stage weights
* updater: single-pass decompression; scripts: print resources compression ratio
* updater: fixed warnings
* toolbox: tar: doxygen
* docs: update
* docs: info or tarhs format; scripts: added standalone compression/decompression tool for heatshrink-formatted streams
* scripts: tarhs: fixed parameter handling
* cli: storage extract command; toolbox: tar: guess type based on extension
* unit_tests: added test for streamed raw hs decompressor `compress_decode_streamed`
* unit_tests: compress: added extraction test for .tar.hs
* rpc: autodetect compressed archives
* scripts: minor cleanup of common parts
* scripts: update: now using in-memory intermediate tar stream
* scripts: added hs.py wrapper for heatshrink-related ops (single object and directory-as-tar compression)
* scripts: naming fixes
* Toolbox: export compress_config_heatshrink_default as const symbol
* Toolbox: fix various types naming
* Toolbox: more of types naming fixes
* Toolbox: use size_t in compress io callbacks and structures
* UnitTests: update to match new compress API
* Toolbox: proper path_extract_extension usage

Co-authored-by: あく <alleteam@gmail.com>
2024-06-30 11:38:48 +01:00

26 lines
860 B
Python

import struct
class HeatshrinkDataStreamHeader:
MAGIC = 0x53445348
VERSION = 1
def __init__(self, window_size, lookahead_size):
self.window_size = window_size
self.lookahead_size = lookahead_size
def pack(self):
return struct.pack(
"<IBBB", self.MAGIC, self.VERSION, self.window_size, self.lookahead_size
)
@staticmethod
def unpack(data):
if len(data) != 7:
raise ValueError("Invalid header length")
magic, version, window_size, lookahead_size = struct.unpack("<IBBB", data)
if magic != HeatshrinkDataStreamHeader.MAGIC:
raise ValueError("Invalid magic number")
if version != HeatshrinkDataStreamHeader.VERSION:
raise ValueError("Invalid version")
return HeatshrinkDataStreamHeader(window_size, lookahead_size)