binary-to-bytearray.py 768 B

123456789101112131415161718192021222324252627282930313233
  1. #!/usr/bin/env python3
  2. """
  3. Take a binary file as input and prints its bytes as ordinals, formatted as an
  4. array. The goal is to take a file that has caused a crash during binary fuzzing
  5. and convert it into a unit test that can detect panic regressioins.
  6. See darkfi/src/zkas/decoder.rs for example unit tests.
  7. input:
  8. - binary.bin
  9. output
  10. - [1, 2, 3, 5, 8, 11]
  11. Now the output can be easily pasted into a unit test.
  12. """
  13. import os.path
  14. import sys
  15. if len(sys.argv) != 2:
  16. print(f"Usage: {__file__} <binary_file>")
  17. exit(1)
  18. if not os.path.isfile(sys.argv[1]):
  19. print("Argument is not a file")
  20. exit(2)
  21. bytes = []
  22. with open(sys.argv[1], "rb") as f:
  23. while (byte := f.read(1)):
  24. bytes.append(str(ord(byte)))
  25. print(f"[{', '.join(bytes)}]")