4
from io import StringIO
5
from io import BufferedReader
9
# bin2cc - embedding compressed data in C files.
11
# 1) Do what the fuck you want with this code.
15
# Create the C file with the compressed data:
16
# $ python3 bin2cc.py ../xorg.png > test.c
18
# Add this to the end of the file:
20
#int main (int argc, char ** argv) {
22
# unsigned char * out_buff = malloc(_xorg_png_real_len);
26
# uncompress (out_buff, &outlen, _xorg_png, _xorg_png_len);
28
# FILE * f = fopen ("../xorg_2.png", "wb");
29
# fwrite (out_buff, 1, _xorg_png_real_len, f);
37
file_name = sys.argv[1]
39
print ("Must provide a file...");
42
f = open (file_name, "rb")
44
buff_bytes = f.read ()
46
compressed_buff = zlib.compress (buff_bytes)
47
compressed_buff_len = len (compressed_buff)
52
sane_file_name = file_name.replace (".","_").replace("/","_").replace("__","")
53
out_str.write ("const unsigned char ")
54
out_str.write (sane_file_name)
55
out_str.write ("[] = {")
59
for i in range (0, len (compressed_buff)):
60
byte = compressed_buff[i]
62
out_str.write ("0x%02x," % byte)
64
if (counter % 10 == 0):
70
out_str.write ("\n\nsize_t %s_len = %i;" % (sane_file_name, compressed_buff_len) )
71
out_str.write ("\n\nsize_t %s_real_len = %i;" % (sane_file_name, len (buff_bytes)) )
73
print (out_str.getvalue())