mirror of
https://github.com/eried/portapack-mayhem.git
synced 2024-10-01 01:26:06 -04:00
82 lines
2.0 KiB
Python
Executable File
82 lines
2.0 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
#
|
|
# Copyright (C) 2015 Jared Boone, ShareBrained Technology, Inc.
|
|
#
|
|
# This file is part of PortaPack.
|
|
#
|
|
# This program is free software; you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation; either version 2, or (at your option)
|
|
# any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program; see the file COPYING. If not, write to
|
|
# the Free Software Foundation, Inc., 51 Franklin Street,
|
|
# Boston, MA 02110-1301, USA.
|
|
#
|
|
|
|
import sys
|
|
import struct
|
|
|
|
usage_message = """
|
|
PortaPack image chunk writer
|
|
|
|
Usage: <command> <input_binary> <four-characer tag> <output_tagged_binary> [<chunk max size>]
|
|
"""
|
|
|
|
def read_image(path):
|
|
f = open(path, 'rb')
|
|
data = f.read()
|
|
f.close()
|
|
return data
|
|
|
|
def write_image(data, path):
|
|
f = open(path, 'wb')
|
|
f.write(data)
|
|
f.close()
|
|
|
|
compressed_data_size = 0
|
|
|
|
if len(sys.argv) == 4:
|
|
input_image = read_image(sys.argv[1])
|
|
tag = tuple(map(ord, sys.argv[2]))
|
|
output_path = sys.argv[3]
|
|
|
|
if input_image[5] & 4 == 4:
|
|
input_image = input_image[19:]
|
|
else:
|
|
input_image = input_image[11:]
|
|
|
|
compressed_data_size = len(input_image)
|
|
|
|
elif len(sys.argv) == 2:
|
|
input_image = bytearray()
|
|
tag = (0, 0, 0, 0)
|
|
output_path = sys.argv[1]
|
|
else:
|
|
print(usage_message)
|
|
sys.exit(-1)
|
|
|
|
if len(tag) != 4:
|
|
print(usage_message)
|
|
sys.exit(-2)
|
|
|
|
if (len(input_image) & 3) != 0:
|
|
input_image = bytearray(input_image)
|
|
|
|
for i in range(4 - (len(input_image) & 3)):
|
|
input_image.append(0)
|
|
|
|
output_image = bytearray()
|
|
output_image += struct.pack('<4BII', tag[0], tag[1], tag[2], tag[3], len(input_image), compressed_data_size)
|
|
|
|
output_image += input_image
|
|
|
|
write_image(output_image, output_path)
|