-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathres2header.py
More file actions
77 lines (55 loc) · 1.69 KB
/
Copy pathres2header.py
File metadata and controls
77 lines (55 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env python
import random
import string
import struct
import binascii
import argparse
xor_key = b'\xcc'
USAGE_TMPL = b'''
#include "%(header)s"
...
for(int i = 0; i < %(reslen_name)s; i++)
{
%(res_name)s[i] = %(res_name)s[i] ^ %(key_name)s;
}
'''
HEADER_TMPL = b'''
#define %(key_name)s '%(xor_key)s'
#define %(reslen_name)s %(res_len)d
static unsigned char %(res_name)s[] = {
%(resource)s
};
'''
def res2header(res_file, header_file, xor_key):
xor_data = b""
with open(res_file, "rb") as rr:
data = rr.read()
for d in data:
xor_data += struct.pack("B", (d if isinstance(d, int) else ord(d)) ^ ord(xor_key))
i = 0
fmt = []
for d in xor_data:
fmt.append(str(d if isinstance(d, int) else ord(d)))
res_name = "RES_" + ''.join(random.sample(string.ascii_uppercase, 4))
res_name = res_name.encode()
res = {
b"res_name": res_name,
b"reslen_name": res_name + b"_LEN",
b"key_name": res_name + b"_KEY",
b"resource": (", ".join(fmt)).encode(),
b"header": header_file.encode(),
b"xor_key": b"\\x%s" % binascii.hexlify(xor_key),
b"res_len": len(xor_data)
}
header = HEADER_TMPL % res
with open(header_file, "wb") as wh:
wh.write(header)
return res
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Convert resource to C/CXX header.")
parser.add_argument('resource', help="Resource file as input.")
parser.add_argument('header', help="Header file as output.")
args = parser.parse_args()
res = res2header(args.resource, args.header, xor_key)
usage = USAGE_TMPL % res
print("Success! Usage: \n" + usage.decode())