3"""The .rabook on-disk constants, kept in lockstep with apps/shared_libs/book/inc/book.h.
5Everything here is a contract with the FIRMWARE, not with this tool: a value
6changed on one side and not the other produces a blob the device parses into
7nonsense rather than a build error. Isolated in one small module so that
8contract is one file to read next to the header it mirrors.
10@copyright Copyright (c) 2026 Brighton Sikarskie
11SPDX-License-Identifier: MIT
45CONTAINER_MAGIC = b
"RBKC"
46CONTAINER_CHUNK_BYTES = 65536
49def wrap_container(blob: bytes, chunk_bytes: int = CONTAINER_CHUNK_BYTES) -> bytes:
50 """Wrap a flat RABOOK1 blob in the chunked RBKC container."""
55 msg =
"chunk_bytes must be positive"
57 count = (len(blob) + chunk_bytes - 1) // chunk_bytes
59 zlib.compress(blob[i * chunk_bytes : (i + 1) * chunk_bytes], 9)
for i
in range(count)
62 for stream
in streams:
63 offsets.append(offsets[-1] + len(stream))
64 header = CONTAINER_MAGIC + struct.pack(
"<IQII", chunk_bytes, len(blob), count, 0)
65 table = b
"".join(struct.pack(
"<Q", off)
for off
in offsets)
66 return header + table + b
"".join(streams)