upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/tools/rbf0_encode.py
diff options
context:
space:
mode:
authorItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com>2026-11-22 17:03:56 -0500
committerItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com>2026-11-22 17:03:56 -0500
commit13c8a8fed9d43f1f2b23f8febd915d023844cd09 (patch)
tree525a0643c474f66a7acd8b27af61d79f390a507e /tools/rbf0_encode.py
parent39f5fd3d9b70b0a8a5866d413c7d582f45461fb4 (diff)
parenta2f8aae55c22c3ba1d4cb8f8d39c5eb960543c1c (diff)
Merge pull request #3811 from gbezzuoli
Diffstat (limited to 'tools/rbf0_encode.py')
-rw-r--r--tools/rbf0_encode.py33
1 files changed, 24 insertions, 9 deletions
diff --git a/tools/rbf0_encode.py b/tools/rbf0_encode.py
index 4a6e1861d..077888181 100644
--- a/tools/rbf0_encode.py
+++ b/tools/rbf0_encode.py
@@ -11,16 +11,18 @@ instead of writing something else:
11 status the parse was not complete (bad, truncated, or bytes after the root closed) 11 status the parse was not complete (bad, truncated, or bytes after the root closed)
12 utf8 a descriptor name, string or text had invalid UTF-8, or an edit contains an unpaired surrogate 12 utf8 a descriptor name, string or text had invalid UTF-8, or an edit contains an unpaired surrogate
13 nan a float or vec3 component was NaN, whose bits the tree renders as text (infinities are exact) 13 nan a float or vec3 component was NaN, whose bits the tree renders as text (infinities are exact)
14 order a value counted as an attribute came after the element's content started 14 order a value counted as an attribute came after the element's content started, or an element short of
15 its attribute count has a value in its content, which would decode as an attribute
15 descriptor a descriptor index outside the table, or a first use out of order 16 descriptor a descriptor index outside the table, or a first use out of order
16 length a string or text no longer has its recorded byte length 17 length a string or text no longer has its recorded byte length
17 value a value the record cannot hold (an integer out of range, an unknown type) 18 value a value the record cannot hold (an integer out of range, a float that overflows the layout, an
19 unknown type, more attribute records than the element's attribute count)
18 20
19Usage: 21Usage:
20 python -I rbf0_encode.py --check <carve-dir | resource> [--limit N] 22 python -I rbf0_encode.py --check <carve-dir | resource> [--limit N]
21 Decode every RBF0 resource (joining 64 KiB continuations as rbf0_tree.py does), encode it, and compare the 23 Decode every RBF0 resource (joining 64 KiB continuations as rbf0_tree.py does), encode it, and compare the
22 result with the original bytes. Prints counts only: exact, different, and refused by reason. Exit 1 when a 24 result with the original bytes. Prints counts only: exact, different, and refused by reason. Exit 1 when a
23 resource encodes to different bytes. 25 resource encodes to different bytes or was refused: only a run where every resource is exact exits 0.
24 python -I rbf0_encode.py <rbf0_tree.json> <new_out_dir> 26 python -I rbf0_encode.py <rbf0_tree.json> <new_out_dir>
25 Write each resource of an rbf0_tree.py JSON report (possibly edited) as <offset>.rbf. The directory must be 27 Write each resource of an rbf0_tree.py JSON report (possibly edited) as <offset>.rbf. The directory must be
26 new or empty. 28 new or empty.
@@ -155,6 +157,14 @@ class _Writer:
155 attrs = node.get("attrs_declared", len(node.get("attributes", []))) 157 attrs = node.get("attrs_declared", len(node.get("attributes", [])))
156 if not isinstance(attrs, int) or isinstance(attrs, bool) or not 0 <= attrs <= 0xFFFF: 158 if not isinstance(attrs, int) or isinstance(attrs, bool) or not 0 <= attrs <= 0xFFFF:
157 raise EncodeError("value", f"attribute count {attrs!r}") 159 raise EncodeError("value", f"attribute count {attrs!r}")
160 # The header word says how many of the following value records are attributes. One record more would decode
161 # as the element's first content value; with fewer, the next content value would decode as an attribute.
162 listed = len(node.get("attributes", []))
163 if listed > attrs:
164 raise EncodeError("value", f"element {node.get('name')!r} has {listed} attribute records, count {attrs}")
165 if listed < attrs and any(c.get("kind") == "value" for c in node.get("content", [])):
166 raise EncodeError("order", f"element {node.get('name')!r} is short of its {attrs} attributes and has a "
167 "value in its content")
158 self.record(node, 0x00) 168 self.record(node, 0x00)
159 self.out += struct.pack("<HHH", 0, 0, attrs) 169 self.out += struct.pack("<HHH", 0, 0, attrs)
160 for child in node.get("attributes", []): 170 for child in node.get("attributes", []):
@@ -194,14 +204,19 @@ def encode(doc):
194 204
195 205
196def resources(source, limit=None): 206def resources(source, limit=None):
197 """Yield (doc, original bytes) for every RBF0 resource in a carve directory or a single file.""" 207 """Yield (doc, original bytes) for every RBF0 resource in a carve directory or a single file. The bytes are
208 the resource's chunks joined as rbf0_tree.decode_source joined them; check() compares their length with the
209 size the document records."""
198 docs, _summary = rbf0_tree.decode_source(source, limit=limit) 210 docs, _summary = rbf0_tree.decode_source(source, limit=limit)
199 rows, read = rbf0_tree._rows(source) 211 rows, read = rbf0_tree._rows(source)
200 by_name = {row["name"]: i for i, row in enumerate(rows)} 212 by_name = {}
213 for i, row in enumerate(rows):
214 if row["name"] in by_name: # a document names its first chunk; two rows of one name cannot be told apart
215 raise rbf0_tree.SelectionError(f"two carved streams are named {row['name']!r}")
216 by_name[row["name"]] = i
201 for doc in docs: 217 for doc in docs:
202 i = by_name[doc["source"]] 218 i = by_name[doc["source"]]
203 blob = b"".join(read(rows[j]) for j in range(i, i + doc["chunks"])) 219 yield doc, b"".join(read(rows[j]) for j in range(i, i + doc["chunks"]))
204 yield doc, blob[:doc["size"]]
205 220
206 221
207def check(source, limit=None): 222def check(source, limit=None):
@@ -219,7 +234,7 @@ def check(source, limit=None):
219 counts["refused"] += 1 234 counts["refused"] += 1
220 reasons[e.reason] += 1 235 reasons[e.reason] += 1
221 continue 236 continue
222 if encoded == blob: 237 if encoded == blob and doc.get("size") == len(blob):
223 counts["exact"] += 1 238 counts["exact"] += 1
224 layout["exact"] += 1 239 layout["exact"] += 1
225 else: 240 else:
@@ -290,7 +305,7 @@ def main(argv=None):
290 print(f" refused ({reason}): {n}") 305 print(f" refused ({reason}): {n}")
291 for layout, c in sorted(r["by_layout"].items()): 306 for layout, c in sorted(r["by_layout"].items()):
292 print(f" {layout}: {c['exact']} of {c['resources']} exact") 307 print(f" {layout}: {c['exact']} of {c['resources']} exact")
293 return 1 if r["different"] else 0 308 return 1 if r["different"] or r["refused"] else 0
294 if not a.report or not a.out: 309 if not a.report or not a.out:
295 ap.error("give --check SOURCE, or a report and an output directory") 310 ap.error("give --check SOURCE, or a report and an output directory")
296 if a.limit is not None: 311 if a.limit is not None: