367 files, from working commit b1a079a. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
32 lines
1.4 KiB
Python
32 lines
1.4 KiB
Python
"""Native 70334 ADT MLLD body framing for one far-liquid entry.
|
|
|
|
The 12-byte prefix is flags:u32, depth bytes:u16, alpha bytes:u16,
|
|
opaque:u32. The two encoded payloads follow, then 0-3 zero bytes to align
|
|
the MLLD *body* to four bytes. The opaque word is zero in the pinned native
|
|
set but remains a round-tripped field here.
|
|
"""
|
|
|
|
import struct
|
|
|
|
|
|
def pack_mlld(flags: int, depth: bytes, alpha: bytes, opaque: int = 0) -> bytes:
|
|
"""Build a native MLLD body; payloads are already run-length encoded."""
|
|
if not 0 <= flags <= 0xffffffff or not 0 <= opaque <= 0xffffffff:
|
|
raise ValueError('MLLD word outside uint32')
|
|
if len(depth) > 0xffff or len(alpha) > 0xffff:
|
|
raise ValueError('MLLD encoded payload exceeds uint16 size')
|
|
body = struct.pack('<IHHI', flags, len(depth), len(alpha), opaque) + depth + alpha
|
|
return body + bytes(-len(body) % 4)
|
|
|
|
|
|
def unpack_mlld(body: bytes) -> tuple[int, bytes, bytes, int]:
|
|
"""Parse exactly one MLLD body, rejecting missing or nonzero padding."""
|
|
if len(body) < 12 or len(body) % 4:
|
|
raise ValueError('MLLD body size/alignment')
|
|
flags, depth_size, alpha_size, opaque = struct.unpack_from('<IHHI', body)
|
|
end = 12 + depth_size + alpha_size
|
|
pad = -end % 4
|
|
if len(body) != end + pad or body[end:] != bytes(pad):
|
|
raise ValueError('MLLD payload/padding mismatch')
|
|
return flags, body[12:12 + depth_size], body[12 + depth_size:end], opaque
|