[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"
|
---|
[211fd68] | 41 | print("%s [--deflate] <DESTINATION> <TYPENAME> <LABEL> <AS_PROLOG> <SECTION> [SOURCE ...]" % prname)
|
---|
[67b05ff] | 42 |
|
---|
[9ce911d] | 43 | def arg_check():
|
---|
[211fd68] | 44 | if (len(sys.argv) < 6):
|
---|
[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()
|
---|
[a35b458] | 59 |
|
---|
[9ce911d] | 60 | if sys.argv[1] == "--deflate":
|
---|
| 61 | sys.argv.pop(1)
|
---|
| 62 | arg_check()
|
---|
| 63 | compress = True
|
---|
| 64 | else:
|
---|
| 65 | compress = False
|
---|
[a35b458] | 66 |
|
---|
[67b05ff] | 67 | dest = sys.argv[1]
|
---|
[211fd68] | 68 | typename = sys.argv[2]
|
---|
| 69 | label = sys.argv[3]
|
---|
| 70 | as_prolog = sys.argv[4]
|
---|
| 71 | section = sys.argv[5]
|
---|
[a35b458] | 72 |
|
---|
[bc6cdc4] | 73 | timestamp = (1980, 1, 1, 0, 0, 0)
|
---|
[a35b458] | 74 |
|
---|
[67b05ff] | 75 | header_ctx = []
|
---|
[9ce911d] | 76 | desc_ctx = []
|
---|
| 77 | size_ctx = []
|
---|
[67b05ff] | 78 | data_ctx = []
|
---|
[a35b458] | 79 |
|
---|
[9ce911d] | 80 | src_cnt = 0
|
---|
[a35b458] | 81 |
|
---|
[94c05b89] | 82 | archive = zipfile.ZipFile("%s.zip" % dest, "w", zipfile.ZIP_STORED)
|
---|
[a35b458] | 83 |
|
---|
[211fd68] | 84 | for src in sys.argv[6:]:
|
---|
[67b05ff] | 85 | basename = os.path.basename(src)
|
---|
[9ce911d] | 86 | plainname = os.path.splitext(basename)[0]
|
---|
[67b05ff] | 87 | symbol = basename.replace(".", "_")
|
---|
[a35b458] | 88 |
|
---|
[67b05ff] | 89 | print("%s -> %s" % (src, symbol))
|
---|
[a35b458] | 90 |
|
---|
[67b05ff] | 91 | src_in = open(src, "rb")
|
---|
| 92 | src_data = src_in.read()
|
---|
| 93 | src_in.close()
|
---|
[a35b458] | 94 |
|
---|
[9ce911d] | 95 | length = len(src_data)
|
---|
[a35b458] | 96 |
|
---|
[9ce911d] | 97 | if compress:
|
---|
| 98 | src_data = deflate(src_data)
|
---|
[94c05b89] | 99 | src_fname = os.path.basename("%s.deflate" % src)
|
---|
[bc6cdc4] | 100 | zipinfo = zipfile.ZipInfo(src_fname, timestamp)
|
---|
| 101 | archive.writestr(zipinfo, src_data)
|
---|
[94c05b89] | 102 | else:
|
---|
| 103 | src_fname = src
|
---|
[a35b458] | 104 |
|
---|
[9ce911d] | 105 | if sys.version_info < (3,):
|
---|
| 106 | src_data = bytearray(src_data)
|
---|
[a35b458] | 107 |
|
---|
[9ce911d] | 108 | length_out = len(src_data)
|
---|
[a35b458] | 109 |
|
---|
[2628642] | 110 | header_ctx.append("extern uint8_t %s[];" % symbol)
|
---|
[9ce911d] | 111 | header_ctx.append("extern size_t %s_size;" % symbol)
|
---|
[a35b458] | 112 |
|
---|
[2628642] | 113 | data_ctx.append(".globl %s" % symbol)
|
---|
| 114 | data_ctx.append(".balign 8")
|
---|
[9ce911d] | 115 | data_ctx.append(".size %s, %u" % (symbol, length_out))
|
---|
[2628642] | 116 | data_ctx.append("%s:" % symbol)
|
---|
[94c05b89] | 117 | data_ctx.append("\t.incbin \"%s\"\n" % src_fname)
|
---|
[a35b458] | 118 |
|
---|
[9ce911d] | 119 | desc_field = []
|
---|
| 120 | desc_field.append("\t{")
|
---|
| 121 | desc_field.append("\t\t.name = \"%s\"," % plainname)
|
---|
| 122 | desc_field.append("\t\t.addr = (void *) %s," % symbol)
|
---|
| 123 | desc_field.append("\t\t.size = %u," % length_out)
|
---|
[94c05b89] | 124 | desc_field.append("\t\t.inflated = %u," % length)
|
---|
[a35b458] | 125 |
|
---|
[94c05b89] | 126 | if compress:
|
---|
| 127 | desc_field.append("\t\t.compressed = true")
|
---|
| 128 | else:
|
---|
| 129 | desc_field.append("\t\t.compressed = false")
|
---|
[a35b458] | 130 |
|
---|
[9ce911d] | 131 | desc_field.append("\t}")
|
---|
[a35b458] | 132 |
|
---|
[9ce911d] | 133 | desc_ctx.append("\n".join(desc_field))
|
---|
[a35b458] | 134 |
|
---|
[9ce911d] | 135 | size_ctx.append("size_t %s_size = %u;" % (symbol, length_out))
|
---|
[a35b458] | 136 |
|
---|
[9ce911d] | 137 | src_cnt += 1
|
---|
[a35b458] | 138 |
|
---|
[9ce911d] | 139 | data = ''
|
---|
[81c6365] | 140 | data += '/*\n'
|
---|
| 141 | data += ' * AUTO-GENERATED FILE, DO NOT EDIT!!!\n'
|
---|
| 142 | data += ' * Generated by: tools/mkarray.py\n'
|
---|
| 143 | data += ' */\n\n'
|
---|
[9ce911d] | 144 | data += "#ifndef %sS_H_\n" % label.upper()
|
---|
| 145 | data += "#define %sS_H_\n\n" % label.upper()
|
---|
| 146 | data += "#include <stddef.h>\n"
|
---|
[94c05b89] | 147 | data += "#include <stdint.h>\n"
|
---|
| 148 | data += "#include <stdbool.h>\n\n"
|
---|
[9ce911d] | 149 | data += "#define %sS %u\n\n" % (label.upper(), src_cnt)
|
---|
[211fd68] | 150 | data += "#ifndef %sS_T_\n" % typename.upper()
|
---|
| 151 | data += "#define %sS_T_\n\n" % typename.upper()
|
---|
[9ce911d] | 152 | data += "typedef struct {\n"
|
---|
| 153 | data += "\tconst char *name;\n"
|
---|
| 154 | data += "\tvoid *addr;\n"
|
---|
| 155 | data += "\tsize_t size;\n"
|
---|
| 156 | data += "\tsize_t inflated;\n"
|
---|
[94c05b89] | 157 | data += "\tbool compressed;\n"
|
---|
[211fd68] | 158 | data += "} %s_t;\n\n" % typename
|
---|
| 159 | data += "#endif\n"
|
---|
| 160 | data += "extern %s_t %ss[];\n\n" % (typename, label)
|
---|
[9ce911d] | 161 | data += "\n".join(header_ctx)
|
---|
| 162 | data += "\n\n"
|
---|
| 163 | data += "#endif\n"
|
---|
[bc6cdc4] | 164 | zipinfo = zipfile.ZipInfo("%s.h" % dest, timestamp)
|
---|
| 165 | archive.writestr(zipinfo, data)
|
---|
[a35b458] | 166 |
|
---|
[9ce911d] | 167 | data = ''
|
---|
[81c6365] | 168 | data += '/*\n'
|
---|
| 169 | data += ' * AUTO-GENERATED FILE, DO NOT EDIT!!!\n'
|
---|
| 170 | data += ' * Generated by: tools/mkarray.py\n'
|
---|
| 171 | data += ' */\n\n'
|
---|
[9ce911d] | 172 | data += as_prolog
|
---|
[22299ed] | 173 | data += "%s\n\n" % section
|
---|
[9ce911d] | 174 | data += "\n".join(data_ctx)
|
---|
| 175 | data += "\n"
|
---|
[bc6cdc4] | 176 | zipinfo = zipfile.ZipInfo("%s.s" % dest, timestamp)
|
---|
| 177 | archive.writestr(zipinfo, data)
|
---|
[a35b458] | 178 |
|
---|
[9ce911d] | 179 | data = ''
|
---|
[81c6365] | 180 | data += '/*\n'
|
---|
| 181 | data += ' * AUTO-GENERATED FILE, DO NOT EDIT!!!\n'
|
---|
| 182 | data += ' * Generated by: tools/mkarray.py\n'
|
---|
| 183 | data += ' */\n\n'
|
---|
[9ce911d] | 184 | data += "#include \"%s.h\"\n\n" % dest
|
---|
[211fd68] | 185 | data += "%s_t %ss[] = {\n" % (typename, label)
|
---|
[9ce911d] | 186 | data += ",\n".join(desc_ctx)
|
---|
| 187 | data += "\n"
|
---|
| 188 | data += "};\n\n"
|
---|
| 189 | data += "\n".join(size_ctx)
|
---|
| 190 | data += "\n"
|
---|
[bc6cdc4] | 191 | zipinfo = zipfile.ZipInfo("%s_desc.c" % dest, timestamp)
|
---|
| 192 | archive.writestr(zipinfo, data)
|
---|
[a35b458] | 193 |
|
---|
[9ce911d] | 194 | archive.close()
|
---|
[67b05ff] | 195 |
|
---|
| 196 | if __name__ == '__main__':
|
---|
| 197 | main()
|
---|