pupy/client/gen_resource_header.py

61 lines
1.5 KiB
Python
Raw Normal View History

2015-09-21 19:53:37 +00:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
2015-09-21 19:53:37 +00:00
import sys
import binascii
import pylzma
import struct
import os
2015-09-21 19:53:37 +00:00
MAX_CHAR_PER_LINE = 50
2015-09-21 19:53:37 +00:00
if __name__ == "__main__":
h_file = ""
file_bytes = b""
output = os.path.basename(sys.argv[2]).replace('.', '_')
2017-07-02 13:23:33 +00:00
with open(sys.argv[1], "rb") as f:
file_bytes = f.read()
2017-07-02 13:23:33 +00:00
compressed = int(sys.argv[3])
2017-07-02 13:23:33 +00:00
attribute = ''
pragma = ''
if len(sys.argv) > 5:
compiler = sys.argv[4]
if compiler == 'cl':
print "USING MSVC pragmas, const_seg: {}".format(sys.argv[5])
attribute = '\n#pragma const_seg(push, stack1, "{}")\n'.format(
sys.argv[5])
pragma = '\n#pragma const_seg(pop, stack1)'
else:
attribute = '\n'.join([
'__attribute__(({}))'.format(x) for x in sys.argv[5:]
])
2017-07-02 13:23:33 +00:00
payload_len = len(file_bytes)
payload = struct.pack('>I', payload_len) + (
pylzma.compress(
file_bytes, dictionary=24, fastBytes=255
) if compressed else file_bytes
)
h_file += "static const int %s_size = %s;" % (output, len(payload))
2017-07-02 13:23:33 +00:00
h_file += attribute
h_file += "\nstatic const char %s_start[] = {\n" % (output)
current_size = 0
2017-07-02 13:23:33 +00:00
for c in payload:
h_file += "'\\x%s'," % binascii.hexlify(c)
current_size += 1
if current_size > MAX_CHAR_PER_LINE:
current_size = 0
h_file += "\n"
2017-07-02 13:23:33 +00:00
h_file += "'\\x00' };\n"
h_file += pragma
2017-07-02 13:23:33 +00:00
with open(sys.argv[2], 'w') as w:
2017-07-02 13:23:33 +00:00
w.write(h_file)