From 3a4b3873d1198ae11d6161bfb0c60d5f47bf9f6c Mon Sep 17 00:00:00 2001 From: WerWolv Date: Wed, 27 Jul 2022 15:58:03 +0200 Subject: [PATCH] scripts: Added script to transform 010 editor CSV encoding files to table files --- README.md | 1 + scripts/csv2tbl.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 scripts/csv2tbl.py diff --git a/README.md b/README.md index 50eede1..2ec9c5c 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,7 @@ Hex patterns, include patterns and magic files for the use with the ImHex Hex Ed | Name | Path | Description | |------|------|-------------| | svd2pat | `scripts/svd2pat.py` | Converts a ARM .svd register MMIO definition file into a pattern | +| csv2tbl | `scripts/csv2tbl.py` | Converts a 010 editor CSV encoding file into a table file | ### Pattern Libraries diff --git a/scripts/csv2tbl.py b/scripts/csv2tbl.py new file mode 100644 index 0000000..4c0b17b --- /dev/null +++ b/scripts/csv2tbl.py @@ -0,0 +1,31 @@ +import csv +import sys + +if len(sys.argv) != 2: + print(f"Usage: {sys.argv[0]} ") + exit() + +fileName = sys.argv[1] + +index = 0x00 +with open(f"{fileName}.tbl", "w", encoding="utf-8") as outFile: + with open(fileName) as file: + reader = csv.reader(file) + for row in reader: + for cell in row: + if cell != "": + try: + number = int(cell, 16) + if number <= 0x1F: + lut = [ "NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "BEL", "BS", "TAB", "LF", "VT", "FF", "CR", "SO", "SI", "DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB", "CAN", "EM", "SUB", "ESC", "FS", "GS", "RS", "US"] + print(f"{index:02X}={lut[number]}", file=outFile) + elif number == 0x7F: + print(f"{index:02X}=DEL", file=outFile) + else: + if chr(number) == chr(0xFFFD): + raise Exception + + print(f"{index:02X}={chr(number)}", file=outFile) + except Exception: + print(f"{index:02X}", file=outFile) + index += 1 \ No newline at end of file