Created
January 2, 2023 06:08
-
-
Save HTV04/4b61339441a02c9aea31ccecee24e064 to your computer and use it in GitHub Desktop.
Quick and Dirty Picotron .P64 Recursive Extractor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Quick and Dirty Picotron .P64 Recursive Extractor v1.0 | |
# | |
# MIT License | |
# | |
# Copyright (c) 2023 HTV04 | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a copy | |
# of this software and associated documentation files (the "Software"), to deal | |
# in the Software without restriction, including without limitation the rights | |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
# copies of the Software, and to permit persons to whom the Software is | |
# furnished to do so, subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included in all | |
# copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
# SOFTWARE. | |
import sys | |
import os | |
import re | |
def p64_extract(dir, p64): | |
cwd = os.getcwd() | |
os.mkdir(dir) | |
os.chdir(dir) | |
for line in p64.split('\n'): | |
m = re.search(r'version (\d+)', line) | |
if m: | |
ver = m.group(1) | |
if int(ver) > 1: | |
print('Unsupported .P64 version: ' + ver) | |
m = re.search(r'(.+) 00000000:00000001', line) | |
if m: # Directory | |
os.mkdir(os.path.normcase(m.group(1))) | |
else: | |
m = re.search(r'(.+) .+:.+ \[h (.+)\]', line) | |
if m: # File | |
file_data = bytearray.fromhex(m.group(2)) | |
if os.path.splitext(m.group(1))[1] == '.p64': | |
p64_extract(os.path.normcase(m.group(1)), file_data.decode()) | |
else: | |
with open(os.path.normcase(m.group(1)), 'wb') as f: | |
f.write(file_data) | |
os.chdir(cwd) | |
p64_extract(sys.argv[1] + '.out', open(sys.argv[1], 'r').read()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment