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:
Diffstat (limited to 'tools/rbf0_encode.py')
-rw-r--r--tools/rbf0_encode.py19
1 files changed, 16 insertions, 3 deletions
diff --git a/tools/rbf0_encode.py b/tools/rbf0_encode.py
index 8ec622c2c..9408d2568 100644
--- a/tools/rbf0_encode.py
+++ b/tools/rbf0_encode.py
@@ -15,8 +15,8 @@ instead of writing something else:
15 its attribute count has a value in its content, which would decode as an attribute 15 its attribute count has a value in its content, which would decode as an attribute
16 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
17 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
18 value a value the record cannot hold (an integer out of range, a float that overflows the layout, an 18 value a value the record cannot hold (a non-string name, an integer out of range, a float that overflows
19 unknown type, more attribute records than the element's attribute count) 19 the layout, an unknown type, more attribute records than the element's attribute count)
20 20
21Usage: 21Usage:
22 python -I rbf0_encode.py --check <carve-dir | resource> [--limit N] 22 python -I rbf0_encode.py --check <carve-dir | resource> [--limit N]
@@ -53,6 +53,15 @@ class EncodeError(ValueError):
53 self.reason = reason 53 self.reason = reason
54 54
55 55
56def _name_bytes(index, name):
57 if not isinstance(name, str):
58 raise EncodeError("value", f"descriptor {index} name must be a string, got {type(name).__name__}")
59 try:
60 return name.encode("utf-8")
61 except UnicodeEncodeError:
62 raise EncodeError("utf8", f"descriptor {index} name contains an unpaired Unicode surrogate") from None
63
64
56class _Writer: 65class _Writer:
57 def __init__(self, doc): 66 def __init__(self, doc):
58 self.wide = doc.get("layout") == "wide-64" 67 self.wide = doc.get("layout") == "wide-64"
@@ -72,18 +81,19 @@ class _Writer:
72 if index == self.emitted: 81 if index == self.emitted:
73 if index in self.bad_names: 82 if index in self.bad_names:
74 raise EncodeError("utf8", f"descriptor {index} name had invalid UTF-8") 83 raise EncodeError("utf8", f"descriptor {index} name had invalid UTF-8")
84 raw = _name_bytes(index, name)
75 if index < len(self.names) and self.names[index] not in (None, name): 85 if index < len(self.names) and self.names[index] not in (None, name):
76 raise EncodeError("descriptor", f"descriptor {index} is named both {self.names[index]!r} and {name!r}") 86 raise EncodeError("descriptor", f"descriptor {index} is named both {self.names[index]!r} and {name!r}")
77 if index == len(self.names): 87 if index == len(self.names):
78 self.names.append(name) 88 self.names.append(name)
79 else: 89 else:
80 self.names[index] = name 90 self.names[index] = name
81 raw = name.encode("utf-8")
82 if len(raw) > 0xFFFF: 91 if len(raw) > 0xFFFF:
83 raise EncodeError("length", f"descriptor {index} name is {len(raw)} bytes") 92 raise EncodeError("length", f"descriptor {index} name is {len(raw)} bytes")
84 self.out += struct.pack("<H", len(raw)) + raw 93 self.out += struct.pack("<H", len(raw)) + raw
85 self.emitted += 1 94 self.emitted += 1
86 elif index < len(self.names) and self.names[index] not in (None, name): 95 elif index < len(self.names) and self.names[index] not in (None, name):
96 _name_bytes(index, name)
87 raise EncodeError("descriptor", f"descriptor {index} is named both {self.names[index]!r} and {name!r}") 97 raise EncodeError("descriptor", f"descriptor {index} is named both {self.names[index]!r} and {name!r}")
88 98
89 def float_bits(self, value, special): 99 def float_bits(self, value, special):
@@ -194,6 +204,9 @@ def encode(doc):
194 if not isinstance(tree, dict) or tree.get("kind") != "element": 204 if not isinstance(tree, dict) or tree.get("kind") != "element":
195 raise EncodeError("status", "no root element") 205 raise EncodeError("status", "no root element")
196 w = _Writer(doc) 206 w = _Writer(doc)
207 for index, descriptor in enumerate(doc.get("descriptors") or []):
208 if "name" in descriptor:
209 _name_bytes(index, descriptor["name"])
197 try: 210 try:
198 w.element(tree) 211 w.element(tree)
199 except UnicodeEncodeError: 212 except UnicodeEncodeError: