-
-
Save vetzki/d61907f6ff7576febb31be9720cf9cfd to your computer and use it in GitHub Desktop.
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 python3 | |
""" | |
by Michael Vetter | |
This is free and unencumbered software released into the public domain. | |
Anyone is free to copy, modify, publish, use, compile, sell, or | |
distribute this software, either in source code form or as a compiled | |
binary, for any purpose, commercial or non-commercial, and by any | |
means. | |
In jurisdictions that recognize copyright laws, the author or authors | |
of this software dedicate any and all copyright interest in the | |
software to the public domain. We make this dedication for the benefit | |
of the public at large and to the detriment of our heirs and | |
successors. We intend this dedication to be an overt act of | |
relinquishment in perpetuity of all present and future rights to this | |
software under copyright law. | |
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 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. | |
For more information, please refer to <http://unlicense.org/> | |
""" | |
import os | |
import argparse | |
from typing import Union | |
gx_copy_filter = { | |
"set" : b",\x03\x00\x00A\x82\x00\xf8\x89\x04\x00\x008\x00\x00\x00\x89D\x00\x018`\x00\x00Q\x00\x07>\x88\xe4\x00\x06Q@&6\x89\x04\x00\x0cP\xe3\x07>8\xe0\x00\x00Q\x07\x07>\x89D\x00\r\x89d\x00\x079\x00\x00\x00QG&6\x89D\x00\x02\x89$\x00\x12Qc&6Q@E.\x89D\x00\x0eQ(\x07>\x89$\x00\x13QGE.\x89D\x00\x03Q(&6\x89$\x00\x14Q@d&\x89D\x00\x0fQ(E.\x89$\x00\x15QGd&\x89D\x00\x04\x89d\x00\x08Q(d&Q@\x83\x1e\x89D\x00\x10\x89$\x00\x16QcE.\x89d\x00\tQG\x83\x1e\x89D\x00\x05Q(\x83\x1e\x89$\x00\x11Qcd&\x89d\x00\nQ@\xa2\x16\x89D\x00\x0bQ'\xa2\x16\x88\x84\x00\x179 \x00\x01Qc\x83\x1eQC\xa2\x16P\x88\xa2\x16Q \xc0\x0e9@\x00\x029 \x00\x038\x80\x00\x04QC\xc0\x0eQ'\xc0\x0eP\x88\xc0\x0eH\x00\x00$=\x00\x01f<`\x02f<\xe0\x03f<\x80\x04f8\x08ff8cff8\xe7ff9\x04ff= \xcc\x019@\x00a\x99I\x80\x00,\x05\x00\x008\x80\x00S9`\x00\x00\x90\t\x80\x008\x00\x00T9\x80\x00\x00P\x8b\xc0\x0e\x99I\x80\x00P\x0c\xc0\x0e\x90i\x80\x00\x99I\x80\x00\x90\xe9\x80\x00\x99I\x80\x00\x91\t\x80\x00A\x82\x00@", | |
"disabled" : b",\x03\x00\x00A\x82\x00\xf8\x89\x04\x00\x008\x00\x00\x00\x89D\x00\x018`\x00\x00Q\x00\x07>\x88\xe4\x00\x06Q@&6\x89\x04\x00\x0cP\xe3\x07>8\xe0\x00\x00Q\x07\x07>\x89D\x00\r\x89d\x00\x079\x00\x00\x00QG&6\x89D\x00\x02\x89$\x00\x12Qc&6Q@E.\x89D\x00\x0eQ(\x07>\x89$\x00\x13QGE.\x89D\x00\x03Q(&6\x89$\x00\x14Q@d&\x89D\x00\x0fQ(E.\x89$\x00\x15QGd&\x89D\x00\x04\x89d\x00\x08Q(d&Q@\x83\x1e\x89D\x00\x10\x89$\x00\x16QcE.\x89d\x00\tQG\x83\x1e\x89D\x00\x05Q(\x83\x1e\x89$\x00\x11Qcd&\x89d\x00\nQ@\xa2\x16\x89D\x00\x0bQ'\xa2\x16\x88\x84\x00\x179 \x00\x01Qc\x83\x1eQC\xa2\x16P\x88\xa2\x16Q \xc0\x0e9@\x00\x029 \x00\x038\x80\x00\x04QC\xc0\x0eQ'\xc0\x0eP\x88\xc0\x0eH\x00\x00$=\x00\x01f<`\x02f<\xe0\x03f<\x80\x04f8\x08ff8cff8\xe7ff9\x04ff= \xcc\x019@\x00a\x99I\x80\x00,\x05\x00\x008\x80\x00S9`\x00\x00\x90\t\x80\x008\x00\x00T9\x80\x00\x00P\x8b\xc0\x0e\x99I\x80\x00P\x0c\xc0\x0e\x90i\x80\x00\x99I\x80\x00\x90\xe9\x80\x00\x99I\x80\x00\x91\t\x80\x00H\x00\x00@" | |
} | |
BytesNotFoundWarning = Warning("bytes not found") | |
BytesNotFoundWarning.errno = 99 | |
def find_gx_offset(data:bytes,searchbytes:bytes) -> Union[int, None]: | |
offset = data.find(searchbytes) | |
if offset == -1: | |
return(None) | |
else: | |
return(offset) | |
def create_new_data(data:bytes,gx_oldbytes:bytes,gx_newbytes:bytes) -> bytes: | |
offset = find_gx_offset(data,gx_oldbytes) | |
if offset is not None: | |
print("GXSetCopyFilter at {0} (hex 0x{0:X}) ".format(offset)) | |
newdata = data[0:offset] | |
newdata += gx_newbytes | |
newdata += data[offset+len(gx_newbytes):] | |
if len(newdata) != len(data): | |
raise ValueError("newdata not same size (newdata size: %i, olddata size: %i)" %(len(newdata),len(data))) | |
return(newdata) | |
else: | |
raise BytesNotFoundWarning | |
def change_filter(fn:str,choice:str,out_fn=None) -> None: | |
try: | |
with open(fn,"r+b") as f: | |
data = f.read() | |
if choice == "disable": | |
filter_data = create_new_data(data,gx_copy_filter["set"],gx_copy_filter["disabled"]) | |
elif choice == "reenable": | |
filter_data = create_new_data(data,gx_copy_filter["disabled"],gx_copy_filter["set"]) | |
else: | |
raise ValueError("use either disabled or reeanbled not '%s'" %(choice)) | |
if out_fn is None: | |
f.seek(0) | |
f.write(filter_data) | |
else: | |
f.close() | |
with open(out_fn,"w+b") as newfile: | |
newfile.write(filter_data) | |
except PermissionError: | |
os.chmod(fn,0o0644) | |
change_filter(fn,choice,out_fn) | |
except Exception as e: | |
print(e) | |
exit(e.errno) | |
def create_parser() -> argparse.ArgumentParser: | |
parser = argparse.ArgumentParser() | |
parser.add_argument("-i","--input",help="input file",required=True) | |
parser.add_argument("-o","--output",help="output file (if none is specified input file gets ovewritten)") | |
parser.add_argument("-c","--choice",help="disable or reenable filter",choices=("disable","reenable"),required=True) | |
return(parser.parse_args()) | |
if __name__ == "__main__": | |
args = create_parser() | |
change_filter(args.input,args.choice,args.output) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment