2015-09-21 19:53:37 +00:00
|
|
|
#!/usr/bin/env python
|
2016-11-29 16:53:39 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2015-09-21 19:53:37 +00:00
|
|
|
import sys
|
|
|
|
import binascii
|
2016-11-29 16:53:39 +00:00
|
|
|
import pylzma
|
|
|
|
import struct
|
2015-09-21 19:53:37 +00:00
|
|
|
|
|
|
|
MAX_CHAR_PER_LINE=50
|
|
|
|
|
|
|
|
if __name__=="__main__":
|
|
|
|
h_file=""
|
|
|
|
file_bytes=b""
|
|
|
|
with open(sys.argv[1], "rb") as f:
|
|
|
|
file_bytes=f.read()
|
2016-11-29 16:53:39 +00:00
|
|
|
|
2017-03-04 16:07:38 +00:00
|
|
|
attribute = '\n'.join([
|
|
|
|
'__attribute__(({}))'.format(x) for x in sys.argv[2:]
|
|
|
|
])
|
|
|
|
|
2016-11-29 16:53:39 +00:00
|
|
|
payload_len = len(file_bytes)
|
|
|
|
payload = struct.pack('>I', payload_len) + pylzma.compress(
|
|
|
|
file_bytes,dictionary=24,fastBytes=255)
|
|
|
|
|
|
|
|
h_file += "static const int %s_size = %s;"%(sys.argv[1].replace(".","_").replace("\\","_").replace("/","_"), len(payload))
|
2017-03-04 16:07:38 +00:00
|
|
|
h_file += attribute
|
2016-11-29 16:53:39 +00:00
|
|
|
h_file += "\nstatic const char %s_start[] = {\n"%sys.argv[1].replace(".","_").replace("\\","_").replace("/","_")
|
2015-09-21 19:53:37 +00:00
|
|
|
current_size=0
|
|
|
|
|
2016-11-29 16:53:39 +00:00
|
|
|
for c in payload:
|
2015-09-21 19:53:37 +00:00
|
|
|
h_file+="'\\x%s',"%binascii.hexlify(c)
|
|
|
|
current_size+=1
|
|
|
|
if current_size>MAX_CHAR_PER_LINE:
|
|
|
|
current_size=0
|
|
|
|
h_file+="\n"
|
2016-11-29 16:53:39 +00:00
|
|
|
|
2015-09-21 19:53:37 +00:00
|
|
|
h_file += "'\\x00' };\n"
|
|
|
|
|
|
|
|
with open(sys.argv[1].replace(".","_").replace("\\","_").replace("/","_")+".c",'w') as w:
|
|
|
|
w.write(h_file)
|