File: embedded_file.py

package info (click to toggle)
kmscon 9.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,388 kB
  • sloc: ansic: 30,604; sh: 99; python: 89; makefile: 16
file content (118 lines) | stat: -rwxr-xr-x 3,150 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/env python3
# SPDX-FileCopyrightText: 2022 Aetf <aetf@unlimited-code.works>
#
# SPDX-License-Identifier: MIT
import inspect
import re
from pathlib import Path
from typing import List, Tuple
from itertools import islice


def chunk(it, size):
    it = iter(it)
    return iter(lambda: tuple(islice(it, size)), ())


def make_c_identifier(s):
    for ptn, repl in [
        (re.compile(r'[^a-zA-Z0-9_]'), '_'),
        (re.compile(r'^(\d)'), r'_\1'),
    ]:
        s = re.sub(ptn, repl, s)
    return s


def generate(
    src: Path,
    dest_dir: Path,
    regex: List[Tuple[re.Pattern, str]],
    zero_term: bool,
):
    # prepare
    name = src.name
    c_name = make_c_identifier(name)

    # read content
    if regex:
        with src.open() as f:
            lines = []
            for line in f:
                for ptn, sub in regex:
                    line = ptn.sub(sub, line)
                lines.append(line)
            content = '\n'.join(lines).encode('utf-8')
    else:
        with src.open('rb') as f:
            content = f.read()

    if zero_term:
        content = content + b'\x00'

    # format content to c array and 16 bytes per row
    content_output = ',\n            '.join(
        ', '.join(f'{b:#04x}' for b in row)
        for row in chunk(content, 16)
    )

    content_c = inspect.cleandoc(f'''
        #include <stddef.h>
        static const unsigned char data[] = {{
            {content_output}
        }};
        const char *const _binary_{c_name}_start = (const char*)data;
        const char *const _binary_{c_name}_end = (const char*)data + sizeof(data);
        const size_t _binary_{c_name}_size = sizeof(data);
    ''')
    content_h = inspect.cleandoc(f'''
        #ifndef {c_name}_H
        #define {c_name}_H
        #include <stddef.h>
        extern const char *const _binary_{c_name}_start;
        extern const char *const _binary_{c_name}_end;
        extern size_t _binary_{c_name}_size;
        #endif // {c_name}_H
    ''')

    # generate the file
    with (dest_dir / f'{name}.bin.c').open('w') as f:
        f.write(content_c)
    with (dest_dir / f'{name}.bin.h').open('w') as f:
        f.write(content_h)
    dest_dir.mkdir(exist_ok=True, parents=True)


def main():
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument(
        '--zero_term',
        action='store_true',
        help='Add an additional 0 byte at the end of data'
    )
    parser.add_argument(
        '--regex', metavar=('pattern', 'replace'),
        type=str, nargs = 2, action='append', default=[],
        help='Treat the binary as text, and for each line replace all occurance matching the regex'
    )
    parser.add_argument(
        'src',
        type=Path,
        help='The source binary file'
    )
    parser.add_argument(
        'dest_dir',
        type=Path,
        help='The destination directory to put output files'
    )
    args = parser.parse_args()

    args.regex = [
        (re.compile(ptn), sub)
        for ptn, sub in args.regex
    ]
    generate(args.src, args.dest_dir, args.regex, args.zero_term)


if __name__ == '__main__':
    main()