Include the Binary Files in Your Source Code


Sometimes, you might want to wrap a binary file (image, executable) in your source code, to eliminate the hassle when deploying your application. You might consider using resource file (*.rc) to build a *.res file which can be finally staticlly built into the executable.

Here is another way to have the data embeded into your application: translate the data into the source code byte array.

For example, in C++, you can have an byte array defined as follows.

unsigned char data[] = { 0x01, 0x02, 0x03, ... };

Below is a python script that easily converts the binary data or string into byte array, in Assembly, Pascal, PHP and C/C++ format. You can also download the file here: data.py

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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env python
"""
    https://helloacm.com
    Convert Files to *.h, *.pas or *.asm
    And Include it in Your Applications.
"""
 
from sys import argv
from os import path
 
def gethex(c):
    """
        return hex str: char[4]
        padding zero e.g. 0xa => 0x0a 
    """
    s = hex(ord(c)).upper()
    if len(s) == 3:
        return s[0:2] + '0' + s[2:]
    return s
 
def getpashex(c):
    """
        return pas hex
    """
    s = gethex(c)
    return "$" + s[2:]  
 
def getbytes(filename):
    """
        return byte array of a file
    """
    if not path.isfile(filename):
        return ""
    try:
        fh = open(filename, 'rb')
    except IOError as e:
        print "({})".format(e)
    else:
        return fh.read()
    finally:
        fh.close()
   
def converttoasm(s, f):
    """
        return asm include 
    """
    j = len(s)
    if j == 0:
        return ""
    output = f + ": \n" + chr(9) + "db "
    i = 0
    while i < j - 1:
        if i % 16 == 15:
            output += gethex(s[i]) + ";" + str(i + 1) + "\n" + chr(9) + "db "
        else:
            output += gethex(s[i]) + ","
        i += 1
    output += gethex(s[j - 1]) + ";" + str(i + 1) + "\n"
    output += "Output_End:\n"
    output += f + "_Size EQU " + str(j)
    return output
 
def converttopas(s, f):
    """
        return *.pas include
    """
    j = len(s)
    if j == 0:
        return ""
    output = "const " + f + ": packed array [0 .. " + str(j - 1) + "] of Byte = (\n" + chr(9)
    i = 0
    while i < j - 1:
        output += getpashex(s[i]) + ","
        if i % 16 == 15:
            output += "//" + str(i + 1) +  "\n" + chr(9)
        i += 1
    output += getpashex(s[j - 1]) + "//" + str(i + 1) + "\n);\n"
    output += "const " + f + "_size = " + str(j) + ";\n"
    return output
 
def converttocpp(s, f):
    """
        return *.h include
    """
    j = len(s)
    if j == 0:
        return ""
    output = "unsigned char " + f + "[] = {\n" + chr(9)
    i = 0
    while i < j - 1:
        output += gethex(s[i]) + ","
        if i % 16 == 15:
            output += "//" + str(i + 1) +  "\n" + chr(9)
        i += 1
    output += gethex(s[j - 1]) + "//" + str(i + 1) + "\n};\n"
    output += "#define " + f + "_size " + str(j) + "\n"
    return output
 
def converttophp(s, f):
    """
        return *.php include
    """
    j = len(s)
    if j == 0:
        return ""
    output = "<?php\n$" + f + " = array(\n" + chr(9)
    i = 0
    while i < j - 1:
        output += gethex(s[i]) + ","
        if i % 16 == 15:
            output += "//" + str(i + 1) +  "\n" + chr(9)
        i += 1
    output += gethex(s[j - 1]) + "//" + str(i + 1) + "\n);\n"
    output += "define ('" + f + "_size', " + str(j) + ");\n?>\n"
    return output
 
def usage():
    print "Usage: %s asm|pas|cpp|php filename|string" % argv[0] 
    print "Filename will be treated as string if not found."
 
if __name__ == "__main__":
    if len(argv) != 3:
        usage()
    elif not path.isfile(argv[2]):
        if argv[1] == 'asm':
            print converttoasm(argv[2], "OutputString" + str(len(argv[2])))
        elif argv[1] == 'cpp':
            print converttocpp(argv[2], "OutputString" + str(len(argv[2])))
        elif argv[1] == 'pas':
            print converttopas(argv[2], "OutputString" + str(len(argv[2])))
        elif argv[1] == 'php':
            print converttophp(argv[2], "OutputString" + str(len(argv[2])))
        else:
            usage()
    elif argv[1] == 'asm':
        print converttoasm(getbytes(argv[2]), "OutputFile" + str(len(argv[2])))
    elif argv[1] == 'cpp':
        print converttocpp(getbytes(argv[2]), "OutputFile" + str(len(argv[2])))
    elif argv[1] == 'pas':
        print converttopas(getbytes(argv[2]), "OutputFile" + str(len(argv[2])))
    elif argv[1] == 'php':
        print converttopas(getbytes(argv[2]), "OutputFile" + str(len(argv[2])))
    else:
        usage()
#!/usr/bin/env python
"""
    https://helloacm.com
    Convert Files to *.h, *.pas or *.asm
    And Include it in Your Applications.
"""

from sys import argv
from os import path

def gethex(c):
    """
        return hex str: char[4]
        padding zero e.g. 0xa => 0x0a 
    """
    s = hex(ord(c)).upper()
    if len(s) == 3:
        return s[0:2] + '0' + s[2:]
    return s

def getpashex(c):
    """
        return pas hex
    """
    s = gethex(c)
    return "$" + s[2:]  

def getbytes(filename):
    """
        return byte array of a file
    """
    if not path.isfile(filename):
        return ""
    try:
        fh = open(filename, 'rb')
    except IOError as e:
        print "({})".format(e)
    else:
        return fh.read()
    finally:
        fh.close()
   
def converttoasm(s, f):
    """
        return asm include 
    """
    j = len(s)
    if j == 0:
        return ""
    output = f + ": \n" + chr(9) + "db "
    i = 0
    while i < j - 1:
        if i % 16 == 15:
            output += gethex(s[i]) + ";" + str(i + 1) + "\n" + chr(9) + "db "
        else:
            output += gethex(s[i]) + ","
        i += 1
    output += gethex(s[j - 1]) + ";" + str(i + 1) + "\n"
    output += "Output_End:\n"
    output += f + "_Size EQU " + str(j)
    return output

def converttopas(s, f):
    """
        return *.pas include
    """
    j = len(s)
    if j == 0:
        return ""
    output = "const " + f + ": packed array [0 .. " + str(j - 1) + "] of Byte = (\n" + chr(9)
    i = 0
    while i < j - 1:
        output += getpashex(s[i]) + ","
        if i % 16 == 15:
            output += "//" + str(i + 1) +  "\n" + chr(9)
        i += 1
    output += getpashex(s[j - 1]) + "//" + str(i + 1) + "\n);\n"
    output += "const " + f + "_size = " + str(j) + ";\n"
    return output

def converttocpp(s, f):
    """
        return *.h include
    """
    j = len(s)
    if j == 0:
        return ""
    output = "unsigned char " + f + "[] = {\n" + chr(9)
    i = 0
    while i < j - 1:
        output += gethex(s[i]) + ","
        if i % 16 == 15:
            output += "//" + str(i + 1) +  "\n" + chr(9)
        i += 1
    output += gethex(s[j - 1]) + "//" + str(i + 1) + "\n};\n"
    output += "#define " + f + "_size " + str(j) + "\n"
    return output

def converttophp(s, f):
    """
        return *.php include
    """
    j = len(s)
    if j == 0:
        return ""
    output = "<?php\n$" + f + " = array(\n" + chr(9)
    i = 0
    while i < j - 1:
        output += gethex(s[i]) + ","
        if i % 16 == 15:
            output += "//" + str(i + 1) +  "\n" + chr(9)
        i += 1
    output += gethex(s[j - 1]) + "//" + str(i + 1) + "\n);\n"
    output += "define ('" + f + "_size', " + str(j) + ");\n?>\n"
    return output

def usage():
    print "Usage: %s asm|pas|cpp|php filename|string" % argv[0] 
    print "Filename will be treated as string if not found."

if __name__ == "__main__":
    if len(argv) != 3:
        usage()
    elif not path.isfile(argv[2]):
        if argv[1] == 'asm':
            print converttoasm(argv[2], "OutputString" + str(len(argv[2])))
        elif argv[1] == 'cpp':
            print converttocpp(argv[2], "OutputString" + str(len(argv[2])))
        elif argv[1] == 'pas':
            print converttopas(argv[2], "OutputString" + str(len(argv[2])))
        elif argv[1] == 'php':
            print converttophp(argv[2], "OutputString" + str(len(argv[2])))
        else:
            usage()
    elif argv[1] == 'asm':
        print converttoasm(getbytes(argv[2]), "OutputFile" + str(len(argv[2])))
    elif argv[1] == 'cpp':
        print converttocpp(getbytes(argv[2]), "OutputFile" + str(len(argv[2])))
    elif argv[1] == 'pas':
        print converttopas(getbytes(argv[2]), "OutputFile" + str(len(argv[2])))
    elif argv[1] == 'php':
        print converttopas(getbytes(argv[2]), "OutputFile" + str(len(argv[2])))
    else:
        usage()

In the future, more languages can be added easily. Maybe, if I have time, I will also put this online by creating a PHP page. The sample usage is given below:

data Include the Binary Files in Your Source Code algorithms assembly language implementation php python technical tools / utilities

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
738 words
Last Post: Command Line Arguments in Python Script
Next Post: Simulated Annealing

The Permanent URL is: Include the Binary Files in Your Source Code

Leave a Reply