#!/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)