|
13 | 13 | So the library keeps a tiny schema-free parser for the fields it needs, while |
14 | 14 | this script provides a convenient place to inspect unknown payloads during |
15 | 15 | future debugging. |
| 16 | +
|
| 17 | +This helper is intentionally standalone and does not import private runtime |
| 18 | +helpers. That keeps it useful for debugging without coupling test/dev tooling to |
| 19 | +internal implementation details. |
16 | 20 | """ |
17 | 21 |
|
18 | 22 | from __future__ import annotations |
19 | 23 |
|
20 | 24 | import argparse |
| 25 | +import base64 |
| 26 | +import binascii |
21 | 27 | import gzip |
| 28 | +import hashlib |
| 29 | +import zlib |
22 | 30 | from pathlib import Path |
23 | 31 |
|
24 | | -from roborock.map.b01_map_parser import ( |
25 | | - _decode_b01_map_payload, |
26 | | - _parse_scmap_payload, |
27 | | - _read_len_delimited, |
28 | | - _read_varint, |
29 | | -) |
| 32 | +from Crypto.Cipher import AES |
| 33 | +from Crypto.Util.Padding import pad, unpad |
| 34 | + |
| 35 | +_B64_CHARS = set(b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=") |
| 36 | + |
| 37 | + |
| 38 | +def _derive_map_key(serial: str, model: str) -> bytes: |
| 39 | + model_suffix = model.split(".")[-1] |
| 40 | + model_key = (model_suffix + "0" * 16)[:16].encode() |
| 41 | + material = f"{serial}+{model_suffix}+{serial}".encode() |
| 42 | + encrypted = AES.new(model_key, AES.MODE_ECB).encrypt(pad(material, AES.block_size)) |
| 43 | + md5 = hashlib.md5(base64.b64encode(encrypted), usedforsecurity=False).hexdigest() |
| 44 | + return md5[8:24].encode() |
| 45 | + |
| 46 | + |
| 47 | +def _decode_base64_payload(raw_payload: bytes) -> bytes: |
| 48 | + blob = raw_payload.strip() |
| 49 | + if len(blob) < 32 or any(b not in _B64_CHARS for b in blob): |
| 50 | + raise ValueError("Unexpected B01 map payload format") |
| 51 | + |
| 52 | + padded = blob + b"=" * (-len(blob) % 4) |
| 53 | + try: |
| 54 | + return base64.b64decode(padded, validate=True) |
| 55 | + except binascii.Error as err: |
| 56 | + raise ValueError("Failed to decode B01 map payload") from err |
| 57 | + |
| 58 | + |
| 59 | +def _decode_b01_map_payload(raw_payload: bytes, *, serial: str, model: str) -> bytes: |
| 60 | + encrypted_payload = _decode_base64_payload(raw_payload) |
| 61 | + if len(encrypted_payload) % AES.block_size != 0: |
| 62 | + raise ValueError("Unexpected encrypted B01 map payload length") |
| 63 | + |
| 64 | + map_key = _derive_map_key(serial, model) |
| 65 | + decrypted_hex = AES.new(map_key, AES.MODE_ECB).decrypt(encrypted_payload) |
| 66 | + |
| 67 | + try: |
| 68 | + compressed_hex = unpad(decrypted_hex, AES.block_size).decode("ascii") |
| 69 | + compressed_payload = bytes.fromhex(compressed_hex) |
| 70 | + return zlib.decompress(compressed_payload) |
| 71 | + except (ValueError, UnicodeDecodeError, zlib.error) as err: |
| 72 | + raise ValueError("Failed to decode B01 map payload") from err |
| 73 | + |
| 74 | + |
| 75 | +def _read_varint(buf: bytes, idx: int) -> tuple[int, int]: |
| 76 | + value = 0 |
| 77 | + shift = 0 |
| 78 | + while True: |
| 79 | + if idx >= len(buf): |
| 80 | + raise ValueError("Truncated varint") |
| 81 | + byte = buf[idx] |
| 82 | + idx += 1 |
| 83 | + value |= (byte & 0x7F) << shift |
| 84 | + if not byte & 0x80: |
| 85 | + return value, idx |
| 86 | + shift += 7 |
| 87 | + if shift > 63: |
| 88 | + raise ValueError("Invalid varint") |
| 89 | + |
| 90 | + |
| 91 | +def _read_len_delimited(buf: bytes, idx: int) -> tuple[bytes, int]: |
| 92 | + length, idx = _read_varint(buf, idx) |
| 93 | + end = idx + length |
| 94 | + if end > len(buf): |
| 95 | + raise ValueError("Invalid length-delimited field") |
| 96 | + return buf[idx:end], end |
| 97 | + |
| 98 | + |
| 99 | +def _parse_map_data_info(blob: bytes) -> bytes: |
| 100 | + idx = 0 |
| 101 | + while idx < len(blob): |
| 102 | + key, idx = _read_varint(blob, idx) |
| 103 | + field_no = key >> 3 |
| 104 | + wire = key & 0x07 |
| 105 | + if wire == 0: |
| 106 | + _, idx = _read_varint(blob, idx) |
| 107 | + elif wire == 2: |
| 108 | + value, idx = _read_len_delimited(blob, idx) |
| 109 | + if field_no == 1: |
| 110 | + try: |
| 111 | + return zlib.decompress(value) |
| 112 | + except zlib.error: |
| 113 | + return value |
| 114 | + elif wire == 5: |
| 115 | + idx += 4 |
| 116 | + else: |
| 117 | + raise ValueError(f"Unsupported wire type {wire} in mapDataInfo") |
| 118 | + raise ValueError("SCMap missing mapData") |
| 119 | + |
| 120 | + |
| 121 | +def _parse_room_data_info(blob: bytes) -> tuple[int | None, str | None]: |
| 122 | + room_id: int | None = None |
| 123 | + room_name: str | None = None |
| 124 | + idx = 0 |
| 125 | + while idx < len(blob): |
| 126 | + key, idx = _read_varint(blob, idx) |
| 127 | + field_no = key >> 3 |
| 128 | + wire = key & 0x07 |
| 129 | + if wire == 0: |
| 130 | + value, idx = _read_varint(blob, idx) |
| 131 | + if field_no == 1: |
| 132 | + room_id = int(value) |
| 133 | + elif wire == 2: |
| 134 | + value, idx = _read_len_delimited(blob, idx) |
| 135 | + if field_no == 2: |
| 136 | + room_name = value.decode("utf-8", errors="replace") |
| 137 | + elif wire == 5: |
| 138 | + idx += 4 |
| 139 | + else: |
| 140 | + raise ValueError(f"Unsupported wire type {wire} in roomDataInfo") |
| 141 | + return room_id, room_name |
| 142 | + |
| 143 | + |
| 144 | +def _parse_scmap_payload(payload: bytes) -> tuple[int, int, bytes, dict[int, str]]: |
| 145 | + size_x = 0 |
| 146 | + size_y = 0 |
| 147 | + grid = b"" |
| 148 | + room_names: dict[int, str] = {} |
| 149 | + |
| 150 | + idx = 0 |
| 151 | + while idx < len(payload): |
| 152 | + key, idx = _read_varint(payload, idx) |
| 153 | + field_no = key >> 3 |
| 154 | + wire = key & 0x07 |
| 155 | + |
| 156 | + if wire == 0: |
| 157 | + _, idx = _read_varint(payload, idx) |
| 158 | + continue |
| 159 | + |
| 160 | + if wire != 2: |
| 161 | + if wire == 5: |
| 162 | + idx += 4 |
| 163 | + continue |
| 164 | + raise ValueError(f"Unsupported wire type {wire} in SCMap payload") |
| 165 | + |
| 166 | + value, idx = _read_len_delimited(payload, idx) |
| 167 | + if field_no == 3: |
| 168 | + hidx = 0 |
| 169 | + while hidx < len(value): |
| 170 | + hkey, hidx = _read_varint(value, hidx) |
| 171 | + hfield = hkey >> 3 |
| 172 | + hwire = hkey & 0x07 |
| 173 | + if hwire == 0: |
| 174 | + hvalue, hidx = _read_varint(value, hidx) |
| 175 | + if hfield == 2: |
| 176 | + size_x = int(hvalue) |
| 177 | + elif hfield == 3: |
| 178 | + size_y = int(hvalue) |
| 179 | + elif hwire == 5: |
| 180 | + hidx += 4 |
| 181 | + elif hwire == 2: |
| 182 | + _, hidx = _read_len_delimited(value, hidx) |
| 183 | + else: |
| 184 | + raise ValueError(f"Unsupported wire type {hwire} in map header") |
| 185 | + elif field_no == 4: |
| 186 | + grid = _parse_map_data_info(value) |
| 187 | + elif field_no == 12: |
| 188 | + room_id, room_name = _parse_room_data_info(value) |
| 189 | + if room_id is not None: |
| 190 | + room_names[room_id] = room_name or f"Room {room_id}" |
| 191 | + |
| 192 | + return size_x, size_y, grid, room_names |
30 | 193 |
|
31 | 194 |
|
32 | 195 | def _looks_like_message(blob: bytes) -> bool: |
33 | | - """Return True if the blob plausibly looks like a protobuf-style message.""" |
34 | 196 | if not blob or len(blob) > 4096: |
35 | 197 | return False |
36 | 198 |
|
|
0 commit comments