diff options
| author | s-ol <s+removethis@s-ol.nu> | 2021-10-10 18:55:51 +0000 |
|---|---|---|
| committer | s-ol <s+removethis@s-ol.nu> | 2021-10-10 18:55:51 +0000 |
| commit | 9d02be627e041fad1df24e78a3041cb33fbbbd39 (patch) | |
| tree | 9a25c85155616180dae861f0f14a26f7c13ca6df | |
| parent | fix bswapw, print_hex_word (diff) | |
| download | subv-9d02be627e041fad1df24e78a3041cb33fbbbd39.tar.gz subv-9d02be627e041fad1df24e78a3041cb33fbbbd39.zip | |
add optional strings stage
| -rwxr-xr-x | strings.py | 47 | ||||
| -rw-r--r-- | subv.py | 23 | ||||
| -rwxr-xr-x | subv.sh | 4 | ||||
| -rwxr-xr-x | validate.py | 4 |
4 files changed, 74 insertions, 4 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) @@ -77,6 +77,19 @@ def parse_segment(line): raise ValueError("invalid segment line") +def parse_special(line): + """parse a special-line. + + >>> parse_special('@@/test "hello/123 asd"') + [('@@', 'test'), '"hello/123 asd"'] + >>> parse_special('@@/test @@/123 @@/uuu') + [('@@', 'test'), '@@/123 @@/uuu'] + """ + parts = white.split(line, maxsplit=1) + parts[0] = parse_part(parts[0]) + return parts + + def parse_label(line): """parse a label-line. @@ -156,6 +169,10 @@ def classify(line): 'instr' >>> classify('ff/8 0/3 2/5') 'instr' + >>> classify('@@/8') + 'special' + >>> classify('@@/test 123 "asdx"') + 'special' """ if line == "": return "empty" @@ -163,6 +180,8 @@ def classify(line): return "segment" elif line.endswith(":"): # label return "label" + elif line.startswith("@@/"): + return "special" else: return "instr" @@ -185,6 +204,8 @@ def parse(line): parsed = parse_label(clean) elif type == "instr": parsed = parse_instr(clean) + elif type == "special": + parsed = parse_special(clean) else: parsed = None @@ -324,7 +345,7 @@ def format(line): if line["comment"]: packed = packed + " # " + line["comment"] return packed - elif type == "empty": + elif type == "empty" or type == "special": return line["raw"] else: raise NotImplementedError("type {}".format(type)) @@ -20,8 +20,10 @@ msg() { msg "OUTPUTTING TO $BASE.*" +msg UNSTRINGING... +./strings.py <"$INPUT" >"$BASE.string" msg VALIDATING... -./validate.py <"$INPUT" >"$BASE.valid" +./validate.py <"$BASE.string" >"$BASE.valid" msg SURVEYING... ./survey.py <"$BASE.valid" >"$BASE.survey" msg FORMATTING... diff --git a/validate.py b/validate.py index 2b3cf92..17818ce 100755 --- a/validate.py +++ b/validate.py @@ -613,9 +613,9 @@ def validate(iter): ) line["instr"] = validator(line["instr"][:]) - line = yield subv.format(line) + yield subv.format(line) else: - line = yield line["raw"] + yield line["raw"] if __name__ == "__main__": |
