tools: add script for byte endianness swapping
authorDaniel Schwierzeck <daniel.schwierzeck@gmail.com>
Sat, 6 Jun 2020 20:21:47 +0000 (22:21 +0200)
committerDaniel Schwierzeck <daniel.schwierzeck@gmail.com>
Mon, 29 Jun 2020 20:36:35 +0000 (22:36 +0200)
This can be used to swap the byte endianness of a binary file
from Little-Endian to Big-Endian or vice-versa.

Signed-off-by: Daniel Schwierzeck <daniel.schwierzeck@gmail.com>
tools/endian-swap.py [new file with mode: 0755]

diff --git a/tools/endian-swap.py b/tools/endian-swap.py
new file mode 100755 (executable)
index 0000000..5990efa
--- /dev/null
@@ -0,0 +1,55 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0+
+
+"""
+Simple tool to swap the byte endianness of a binary file.
+"""
+
+import argparse
+import io
+
+def parse_args():
+    """Parse command line arguments."""
+    description = "Swap endianness of given input binary and write to output binary."
+
+    parser = argparse.ArgumentParser(description=description)
+    parser.add_argument("input_bin", type=str, help="input binary")
+    parser.add_argument("output_bin", type=str, help="output binary")
+    parser.add_argument("-c", action="store", dest="chunk_size", type=int,
+        default=io.DEFAULT_BUFFER_SIZE, help="chunk size for reading")
+
+    return parser.parse_args()
+
+def swap_chunk(chunk_orig):
+    """Swap byte endianness of the given chunk.
+
+    Returns:
+        swapped chunk
+    """
+    chunk = bytearray(chunk_orig)
+
+    # align to 4 bytes and pad with 0x0
+    chunk_len = len(chunk)
+    pad_len = chunk_len % 4
+    if pad_len > 0:
+        chunk += b'\x00' * (4 - pad_len)
+
+    chunk[0::4], chunk[1::4], chunk[2::4], chunk[3::4] =\
+        chunk[3::4], chunk[2::4], chunk[1::4], chunk[0::4]
+
+    return chunk
+
+def main():
+    args = parse_args()
+
+    with open(args.input_bin, "rb") as input_bin:
+        with open(args.output_bin, "wb") as output_bin:
+            while True:
+                chunk = bytearray(input_bin.read(args.chunk_size))
+                if not chunk:
+                    break
+
+                output_bin.write(swap_chunk(chunk))
+
+if __name__ == '__main__':
+    main()