#!/bin/python3
import struct
import sys
import os
if sys.argv.__len__() > 1:
src_file = sys.argv[1]
if sys.argv.__len__() > 2:
dst_file = sys.argv[2]
else:
dst_file = src_file[0:(src_file.__len__()-4)]
dst_file += "_covert.bin"
else:
print('Usage: ' + sys.argv[0] + ' input.bin output.bin')
exit(1)
if not os.path.exists(src_file):
print('File Path Invalid! Exiting...')
exit(1)
if src_file == dst_file:
print(src_file + ' ' + dst_file + ' same')
exit(1)
#print("Source file: {0}\nTarget File: {1}\n".format(src_file, dst_file))
try:
sf = open(src_file, "rb")
df = open(dst_file, "wb")
buf_tmp = [b'0' for x in range(0, 4)]
contents = sf.read()
buf_size = contents.__len__()
extra_size = (buf_size % 4)
if extra_size > 0:
buf_size += (4 - extra_size)
contents = contents + b'0000'
for i in range(0, buf_size, 4):
buf_tmp[3] = contents[i]
buf_tmp[2] = contents[i+1]
buf_tmp[1] = contents[i+2]
buf_tmp[0] = contents[i+3]
tmp_bytes = struct.pack("4B", buf_tmp[0], buf_tmp[1], buf_tmp[2], buf_tmp[3])
df.write(tmp_bytes)
finally:
if sf:
sf.close()
if df:
df.close()
exit(0)