[67b05ff] | 1 | #!/usr/bin/env python
|
---|
| 2 | #
|
---|
| 3 | # Copyright (c) 2011 Martin Decky
|
---|
| 4 | # All rights reserved.
|
---|
| 5 | #
|
---|
| 6 | # Redistribution and use in source and binary forms, with or without
|
---|
| 7 | # modification, are permitted provided that the following conditions
|
---|
| 8 | # are met:
|
---|
| 9 | #
|
---|
| 10 | # - Redistributions of source code must retain the above copyright
|
---|
| 11 | # notice, this list of conditions and the following disclaimer.
|
---|
| 12 | # - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | # notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | # documentation and/or other materials provided with the distribution.
|
---|
| 15 | # - The name of the author may not be used to endorse or promote products
|
---|
| 16 | # derived from this software without specific prior written permission.
|
---|
| 17 | #
|
---|
| 18 | # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | #
|
---|
| 29 | """
|
---|
[9ce911d] | 30 | Binary inline data packer
|
---|
[67b05ff] | 31 | """
|
---|
| 32 |
|
---|
| 33 | import sys
|
---|
| 34 | import os
|
---|
[9ce911d] | 35 | import zlib
|
---|
| 36 | import zipfile
|
---|
| 37 | import binascii
|
---|
[67b05ff] | 38 |
|
---|
| 39 | def usage(prname):
|
---|
| 40 | "Print usage syntax"
|
---|
[22299ed] | 41 | print("%s [--deflate] <DESTINATION> <LABEL> <AS_PROLOG> <SECTION> [SOURCE ...]" % prname)
|
---|
[67b05ff] | 42 |
|
---|
[9ce911d] | 43 | def arg_check():
|
---|
[22299ed] | 44 | if (len(sys.argv) < 5):
|
---|
[67b05ff] | 45 | usage(sys.argv[0])
|
---|
[9ce911d] | 46 | sys.exit()
|
---|
| 47 |
|
---|
| 48 | def deflate(data):
|
---|
| 49 | "Compress using deflate algorithm (without any headers)"
|
---|
| 50 | return zlib.compress(data, 9)[2:-4]
|
---|
| 51 |
|
---|
| 52 | def chunks(string, length):
|
---|
| 53 | "Produce string chunks"
|
---|
| 54 | for start in range(0, len(string), length):
|
---|
| 55 | yield string[start:start + length]
|
---|
| 56 |
|
---|
| 57 | def main():
|
---|
| 58 | arg_check()
|
---|
| 59 |
|
---|
| 60 | if sys.argv[1] == "--deflate":
|
---|
| 61 | sys.argv.pop(1)
|
---|
| 62 | arg_check()
|
---|
| 63 | compress = True
|
---|
| 64 | else:
|
---|
| 65 | compress = False
|
---|
[67b05ff] | 66 |
|
---|
| 67 | dest = sys.argv[1]
|
---|
| 68 | label = sys.argv[2]
|
---|
[2628642] | 69 | as_prolog = sys.argv[3]
|
---|
[22299ed] | 70 | section = sys.argv[4]
|
---|
[67b05ff] | 71 |
|
---|
| 72 | header_ctx = []
|
---|
[9ce911d] | 73 | desc_ctx = []
|
---|
| 74 | size_ctx = []
|
---|
[67b05ff] | 75 | data_ctx = []
|
---|
[2628642] | 76 |
|
---|
[9ce911d] | 77 | src_cnt = 0
|
---|
| 78 |
|
---|
[22299ed] | 79 | for src in sys.argv[5:]:
|
---|
[67b05ff] | 80 | basename = os.path.basename(src)
|
---|
[9ce911d] | 81 | plainname = os.path.splitext(basename)[0]
|
---|
[67b05ff] | 82 | symbol = basename.replace(".", "_")
|
---|
| 83 |
|
---|
| 84 | print("%s -> %s" % (src, symbol))
|
---|
| 85 |
|
---|
| 86 | src_in = open(src, "rb")
|
---|
| 87 | src_data = src_in.read()
|
---|
| 88 | src_in.close()
|
---|
| 89 |
|
---|
[9ce911d] | 90 | length = len(src_data)
|
---|
[67b05ff] | 91 |
|
---|
[9ce911d] | 92 | if compress:
|
---|
| 93 | src_data = deflate(src_data)
|
---|
[67b05ff] | 94 |
|
---|
[9ce911d] | 95 | if sys.version_info < (3,):
|
---|
| 96 | src_data = bytearray(src_data)
|
---|
[67b05ff] | 97 |
|
---|
[9ce911d] | 98 | length_out = len(src_data)
|
---|
[2628642] | 99 |
|
---|
| 100 | header_ctx.append("extern uint8_t %s[];" % symbol)
|
---|
[9ce911d] | 101 | header_ctx.append("extern size_t %s_size;" % symbol)
|
---|
[c6f08726] | 102 |
|
---|
[2628642] | 103 | data_ctx.append(".globl %s" % symbol)
|
---|
| 104 | data_ctx.append(".balign 8")
|
---|
[9ce911d] | 105 | data_ctx.append(".size %s, %u" % (symbol, length_out))
|
---|
[2628642] | 106 | data_ctx.append("%s:" % symbol)
|
---|
[9ce911d] | 107 | data_ctx.append("\t.byte 0x" + ', 0x'.join(chunks(binascii.b2a_hex(src_data), 2)) + "\n")
|
---|
| 108 |
|
---|
| 109 | desc_field = []
|
---|
| 110 | desc_field.append("\t{")
|
---|
| 111 | desc_field.append("\t\t.name = \"%s\"," % plainname)
|
---|
| 112 | desc_field.append("\t\t.addr = (void *) %s," % symbol)
|
---|
| 113 | desc_field.append("\t\t.size = %u," % length_out)
|
---|
| 114 | desc_field.append("\t\t.inflated = %u" % length)
|
---|
| 115 | desc_field.append("\t}")
|
---|
| 116 |
|
---|
| 117 | desc_ctx.append("\n".join(desc_field))
|
---|
| 118 |
|
---|
| 119 | size_ctx.append("size_t %s_size = %u;" % (symbol, length_out))
|
---|
| 120 |
|
---|
| 121 | src_cnt += 1
|
---|
[67b05ff] | 122 |
|
---|
[9ce911d] | 123 | archive = zipfile.ZipFile("%s.zip" % dest, "w", zipfile.ZIP_STORED)
|
---|
[67b05ff] | 124 |
|
---|
[9ce911d] | 125 | data = ''
|
---|
| 126 | data += '/***************************************\n'
|
---|
| 127 | data += ' * AUTO-GENERATED FILE, DO NOT EDIT!!! *\n'
|
---|
| 128 | data += ' * Generated by: tools/mkarray.py *\n'
|
---|
| 129 | data += ' ***************************************/\n\n'
|
---|
| 130 | data += "#ifndef %sS_H_\n" % label.upper()
|
---|
| 131 | data += "#define %sS_H_\n\n" % label.upper()
|
---|
| 132 | data += "#include <stddef.h>\n"
|
---|
| 133 | data += "#include <stdint.h>\n\n"
|
---|
| 134 | data += "#define %sS %u\n\n" % (label.upper(), src_cnt)
|
---|
| 135 | data += "typedef struct {\n"
|
---|
| 136 | data += "\tconst char *name;\n"
|
---|
| 137 | data += "\tvoid *addr;\n"
|
---|
| 138 | data += "\tsize_t size;\n"
|
---|
| 139 | data += "\tsize_t inflated;\n"
|
---|
| 140 | data += "} %s_t;\n\n" % label
|
---|
| 141 | data += "extern %s_t %ss[];\n\n" % (label, label)
|
---|
| 142 | data += "\n".join(header_ctx)
|
---|
| 143 | data += "\n\n"
|
---|
| 144 | data += "#endif\n"
|
---|
| 145 | archive.writestr("%s.h" % dest, data)
|
---|
[67b05ff] | 146 |
|
---|
[9ce911d] | 147 | data = ''
|
---|
| 148 | data += '/***************************************\n'
|
---|
| 149 | data += ' * AUTO-GENERATED FILE, DO NOT EDIT!!! *\n'
|
---|
| 150 | data += ' * Generated by: tools/mkarray.py *\n'
|
---|
| 151 | data += ' ***************************************/\n\n'
|
---|
| 152 | data += as_prolog
|
---|
[22299ed] | 153 | data += "%s\n\n" % section
|
---|
[9ce911d] | 154 | data += "\n".join(data_ctx)
|
---|
| 155 | data += "\n"
|
---|
| 156 | archive.writestr("%s.s" % dest, data)
|
---|
[67b05ff] | 157 |
|
---|
[9ce911d] | 158 | data = ''
|
---|
| 159 | data += '/***************************************\n'
|
---|
| 160 | data += ' * AUTO-GENERATED FILE, DO NOT EDIT!!! *\n'
|
---|
| 161 | data += ' * Generated by: tools/mkarray.py *\n'
|
---|
| 162 | data += ' ***************************************/\n\n'
|
---|
| 163 | data += "#include \"%s.h\"\n\n" % dest
|
---|
| 164 | data += "%s_t %ss[] = {\n" % (label, label)
|
---|
| 165 | data += ",\n".join(desc_ctx)
|
---|
| 166 | data += "\n"
|
---|
| 167 | data += "};\n\n"
|
---|
| 168 | data += "\n".join(size_ctx)
|
---|
| 169 | data += "\n"
|
---|
| 170 | archive.writestr("%s_desc.c" % dest, data)
|
---|
[67b05ff] | 171 |
|
---|
[9ce911d] | 172 | archive.close()
|
---|
[67b05ff] | 173 |
|
---|
| 174 | if __name__ == '__main__':
|
---|
| 175 | main()
|
---|