asc2cas.py 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/usr/bin/env python3
  2. # Written by Pedro Gimeno
  3. # args: infile outfile savename
  4. import sys, struct
  5. def main():
  6. if len(sys.argv) < 4:
  7. sys.stderr.write("Usage: asc2cas.py infile outfile filename6chars\n")
  8. return
  9. h = b'\x1F\xA6\xDE\xBA\xCC\x13\x7D\x74'
  10. if sys.argv[1] != '-':
  11. f = open(sys.argv[1], 'rb')
  12. try:
  13. data = f.read()
  14. finally:
  15. f.close()
  16. else:
  17. data = sys.stdin.buffer.read()
  18. # Pad it to a multiple of 256 bytes, adding at least one EOF
  19. data += b'\x1A'
  20. data += b'\x1A' * (-len(data) % 256)
  21. # Not the right encoding, so just in case, don't use special characters
  22. nam = ('%-6.6s' % sys.argv[3]).encode('latin1')
  23. if sys.argv[2] != '-':
  24. g = open(sys.argv[2], 'wb')
  25. else:
  26. g = sys.stdout.buffer
  27. try:
  28. # Block 1 (header)
  29. g.write(h)
  30. g.write(b'\xEA' * 10)
  31. g.write(nam)
  32. # Subsequent blocks
  33. for i in range(0, len(data), 256):
  34. g.write(h)
  35. g.write(data[i:i+256])
  36. finally:
  37. if sys.argv[2] != '-':
  38. g.close()
  39. main()