-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGlobal_DocumentIncrementTable_Decode_V1.py
More file actions
288 lines (237 loc) · 8.08 KB
/
Copy pathGlobal_DocumentIncrementTable_Decode_V1.py
File metadata and controls
288 lines (237 loc) · 8.08 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import argparse
import gzip
import pathlib
import re
import struct
import sys
STDOUT_ENCODING = sys.stdout.encoding or "utf-8"
def safe_print(text: str = ""):
"""Print without the console crashing on odd Unicode."""
try:
print(text)
except UnicodeEncodeError:
alt = text.encode(
STDOUT_ENCODING, errors="backslashreplace"
).decode(STDOUT_ENCODING, errors="backslashreplace")
print(alt)
def hex_dump(data: bytes, width: int = 16, limit: int | None = None) -> str:
"""Simple hexdump helper."""
if limit is not None:
data = data[:limit]
lines = []
for i in range(0, len(data), width):
chunk = data[i : i + width]
hexs = " ".join(f"{b:02X}" for b in chunk)
ascii_s = "".join(chr(b) if 32 <= b <= 126 else "." for b in chunk)
lines.append(f"{i:04X} {hexs:<{width*3}} {ascii_s}")
return "\n".join(lines)
def parse_uints_le(blob: bytes, unit: int, max_count: int):
"""
Interpret the first bytes as little endian integers:
unit = 2 (uint16) or 4 (uint32).
"""
count = min(max_count, len(blob) // unit)
vals = []
fmt = "<H" if unit == 2 else "<I"
for i in range(count):
off = i * unit
val = struct.unpack_from(fmt, blob, off)[0]
vals.append((off, val))
return vals
def ascii_strings_from_bytes(blob: bytes, min_len: int = 4):
"""Search for normal ASCII strings in raw bytes."""
results = []
cur = bytearray()
def flush():
nonlocal cur
if len(cur) >= min_len:
results.append(cur.decode("ascii", errors="ignore"))
cur = bytearray()
for b in blob:
if 32 <= b <= 126:
cur.append(b)
else:
flush()
flush()
return results
def utf16le_strings_all_alignments(blob: bytes, min_len: int = 4):
"""
Search for UTF-16LE strings at both possible alignments (offset 0 and 1).
This captures as much readable text as possible.
"""
results = []
for start in (0, 1):
cur = []
def flush():
nonlocal cur
if len(cur) >= min_len:
results.append("".join(cur))
cur = []
i = start
while i + 1 < len(blob):
ch = blob[i]
nul = blob[i + 1]
if 32 <= ch <= 126 and nul == 0:
cur.append(chr(ch))
else:
flush()
i += 2
flush()
# dedup
uniq = []
seen = set()
for s in results:
if s not in seen:
uniq.append(s)
seen.add(s)
return uniq
def extract_guids(text: str):
pat = re.compile(
r"[0-9A-Fa-f]{8}-"
r"[0-9A-Fa-f]{4}-"
r"[0-9A-Fa-f]{4}-"
r"[0-9A-Fa-f]{4}-"
r"[0-9A-Fa-f]{12}"
)
return sorted(set(pat.findall(text)))
def find_and_decompress_gzip(blob: bytes):
"""
Search for a gzip header (1F 8B 08) and try to decompress a valid
gzip block by moving the end backward.
Returns (start_offset, end_offset, decompressed_bytes)
or (None, None, None) if nothing succeeds.
"""
magic = b"\x1f\x8b\x08"
for start in range(len(blob) - 2):
if blob[start : start + 3] != magic:
continue
# move the end backward until gzip.decompress succeeds
for end in range(len(blob), start + 10, -1):
chunk = blob[start:end]
try:
decomp = gzip.decompress(chunk)
return start, end, decomp
except Exception:
continue
return None, None, None
def inspect_uint32_pairs(decomp: bytes, max_pairs: int = 16):
"""
Interpret decompressed data as a table of (uint32, uint32) pairs.
Handy for something called 'DocumentIncrementTable'.
"""
pairs = []
pair_size = 8
total_pairs = min(max_pairs, len(decomp) // pair_size)
for i in range(total_pairs):
off = i * pair_size
a, b = struct.unpack_from("<II", decomp, off)
pairs.append((off, a, b))
return pairs
def main():
parser = argparse.ArgumentParser(
description="Decode racbasicsamplefamily/Global_DocumentIncrementTable.bin"
)
parser.add_argument(
"path",
nargs="?",
default=r"racbasicsamplefamily/Global_DocumentIncrementTable.bin",
help="Path to Global_DocumentIncrementTable.bin "
"(default: racbasicsamplefamily/Global_DocumentIncrementTable.bin)",
)
parser.add_argument(
"--dump-hex",
action="store_true",
help="Show full hexdump of the source file",
)
args = parser.parse_args()
path = pathlib.Path(args.path)
if not path.exists():
safe_print(f"File not found: {path}")
return 1
blob = path.read_bytes()
safe_print(f"File: {path}")
safe_print(f"Size: {len(blob)} bytes\n")
# 1. Header analysis
safe_print("=== Header hexdump (first 0x80 bytes) ===")
safe_print(hex_dump(blob[:0x80]))
safe_print()
safe_print("=== Header as 32-bit little endian integers ===")
for off, val in parse_uints_le(blob[:0x80], unit=4, max_count=16):
safe_print(f" offset 0x{off:04X}: {val}")
safe_print()
safe_print("=== Header as 16-bit little endian integers ===")
for off, val in parse_uints_le(blob[:0x80], unit=2, max_count=32):
safe_print(f" offset 0x{off:04X}: {val}")
safe_print()
if args.dump_hex:
safe_print("=== Full hexdump of Global_DocumentIncrementTable.bin ===")
safe_print(hex_dump(blob))
safe_print()
# 2. Find gzip segment and decompress
start, end, decomp = find_and_decompress_gzip(blob)
if decomp is None:
safe_print("No valid gzip segment found in Global_DocumentIncrementTable.bin")
return 0
safe_print("=== Gzip segment found ===")
safe_print(f" start offset: {start} (0x{start:04X})")
safe_print(f" end offset: {end} (0x{end:04X})")
safe_print(f" compressed size: {end - start} bytes")
safe_print(f" decompressed size: {len(decomp)} bytes")
safe_print()
# 3. Write decompressed data
out_path = path.with_name("Global_DocumentIncrementTable_decompressed.bin")
out_path.write_bytes(decomp)
safe_print(f"Decompressed data saved as: {out_path}")
safe_print()
# 4. Decompressed hexdump
safe_print("=== Decompressed hex (first 0x80 bytes) ===")
safe_print(hex_dump(decomp, limit=0x80))
safe_print()
safe_print("=== Decompressed as 32-bit little endian integers (first 16) ===")
for off, val in parse_uints_le(decomp, unit=4, max_count=16):
safe_print(f" offset 0x{off:04X}: {val}")
safe_print()
# 5. Table of uint32 pairs (assumption: doc_id / increment)
safe_print("=== Decompressed as (uint32, uint32) pairs (first 16) ===")
pairs = inspect_uint32_pairs(decomp, max_pairs=16)
if pairs:
for off, a, b in pairs:
safe_print(f" offset 0x{off:04X}: ({a}, {b})")
else:
safe_print(" <not enough data for pairs>")
safe_print()
# 6. Strings and GUIDs from decompressed data
utf16 = utf16le_strings_all_alignments(decomp, min_len=4)
ascii_s = ascii_strings_from_bytes(decomp, min_len=4)
safe_print("=== UTF-16-LE strings (all alignments) ===")
if utf16:
for s in utf16:
safe_print(f" {s}")
else:
safe_print(" <none>")
safe_print()
safe_print("=== ASCII strings ===")
if ascii_s:
for s in ascii_s:
safe_print(f" {s}")
else:
safe_print(" <none>")
safe_print()
joined = "\n".join(utf16 + ascii_s)
guids = extract_guids(joined)
safe_print("=== Possible GUIDs in decompressed data ===")
if guids:
for g in guids:
safe_print(f" {g}")
else:
safe_print(" <none>")
safe_print()
safe_print(
"Note: the shown integers/pairs most likely form a document-increment "
"table (indices/version numbers), but the exact meaning requires "
"knowledge of the internal Revit format."
)
safe_print()
return 0
if __name__ == "__main__":
raise SystemExit(main())