aboutsummaryrefslogtreecommitdiffstats
path: root/strings.py
diff options
context:
space:
mode:
authors-ol <s+removethis@s-ol.nu>2021-10-10 18:55:51 +0000
committers-ol <s+removethis@s-ol.nu>2021-10-10 18:55:51 +0000
commit9d02be627e041fad1df24e78a3041cb33fbbbd39 (patch)
tree9a25c85155616180dae861f0f14a26f7c13ca6df /strings.py
parentfix bswapw, print_hex_word (diff)
downloadsubv-9d02be627e041fad1df24e78a3041cb33fbbbd39.tar.gz
subv-9d02be627e041fad1df24e78a3041cb33fbbbd39.zip
add optional strings stage
Diffstat (limited to 'strings.py')
-rwxr-xr-xstrings.py47
1 files changed, 47 insertions, 0 deletions
diff --git a/strings.py b/strings.py
new file mode 100755
index 0000000..9de6c9c
--- /dev/null
+++ b/strings.py
@@ -0,0 +1,47 @@
+#!/usr/bin/env python3
+""" strings.py
+Replaces literal strings with byte-slices
+
+>>> from io import StringIO
+>>> # doctest: +REPORT_NDIFF
+... print(subv.join_all(strings(StringIO('''
+... == data 0x80500000
+... E_MAGIC:
+... @@/string "Invalid DTB magic!\\\\n\\\\0"
+... S_TOTALSZ:
+... @@/string "Total Size: \\\\0"
+... S_DASH:
+... @@/string " - \\\\0"
+... '''[1:-1]))))
+== data 0x80500000
+E_MAGIC:
+49/8 6e/8 76/8 61/8 6c/8 69/8 64/8 20/8 44/8 54/8 42/8 20/8 6d/8 61/8 67/8 69/8 63/8 21/8 a/8 0/8
+S_TOTALSZ:
+54/8 6f/8 74/8 61/8 6c/8 20/8 53/8 69/8 7a/8 65/8 3a/8 20/8 0/8
+S_DASH:
+20/8 2d/8 20/8 0/8
+"""
+
+import subv
+from ast import literal_eval
+
+@subv.with_parsed_lines
+def strings(iter):
+ for segment, line in iter:
+ if line["type"] == "special" and line["special"][0] == ('@@', 'string'):
+ string = literal_eval(line["special"][1])
+ result = {
+ "type": "instr",
+ "instr": [(byte, 8) for byte in string.encode('utf-8')],
+ "comment": None,
+ }
+
+ yield subv.format(result)
+ else:
+ yield line["raw"]
+
+if __name__ == "__main__":
+ import sys
+
+ for line in strings(sys.stdin):
+ print(line)