Jukka Rissanen | 0ff4c25 | 2017-09-13 10:43:30 +0300 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # |
| 3 | # Copyright (c) 2017 Intel Corporation |
Irfan Ahmad | f3d513d | 2025-01-17 02:47:43 +0500 | [diff] [blame] | 4 | # Copyright (c) 2025 Siemens AG |
Jukka Rissanen | 0ff4c25 | 2017-09-13 10:43:30 +0300 | [diff] [blame] | 5 | # |
| 6 | # SPDX-License-Identifier: Apache-2.0 |
| 7 | |
Ruslan Mstoi | 1bae767 | 2020-06-01 12:30:19 +0300 | [diff] [blame] | 8 | |
| 9 | """Convert a file to a list of hex characters |
| 10 | |
| 11 | The list of hex characters can then be included to a source file. Optionally, |
| 12 | the output can be compressed. |
| 13 | |
| 14 | """ |
Jukka Rissanen | 0ff4c25 | 2017-09-13 10:43:30 +0300 | [diff] [blame] | 15 | |
| 16 | import argparse |
| 17 | import codecs |
| 18 | import gzip |
| 19 | import io |
| 20 | |
Anas Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 21 | |
Jukka Rissanen | 0ff4c25 | 2017-09-13 10:43:30 +0300 | [diff] [blame] | 22 | def parse_args(): |
| 23 | global args |
| 24 | |
Anas Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 25 | parser = argparse.ArgumentParser( |
| 26 | description=__doc__, |
Jamie McCrae | ec70444 | 2023-01-04 16:08:36 +0000 | [diff] [blame] | 27 | formatter_class=argparse.RawDescriptionHelpFormatter, allow_abbrev=False) |
Jukka Rissanen | 0ff4c25 | 2017-09-13 10:43:30 +0300 | [diff] [blame] | 28 | |
| 29 | parser.add_argument("-f", "--file", required=True, help="Input file") |
Pieter De Gendt | ae8c724 | 2023-09-13 16:22:42 +0200 | [diff] [blame] | 30 | parser.add_argument("-o", "--offset", type=lambda x: int(x, 0), default=0, |
| 31 | help="Byte offset in the input file") |
| 32 | parser.add_argument("-l", "--length", type=lambda x: int(x, 0), default=-1, |
| 33 | help="""Length in bytes to read from the input file. |
| 34 | Defaults to reading till the end of the input file.""") |
Irfan Ahmad | f3d513d | 2025-01-17 02:47:43 +0500 | [diff] [blame] | 35 | parser.add_argument("-m", "--format", default="list", |
| 36 | help="Output format: 'list' (default) or 'literal' (string literal)") |
Jukka Rissanen | 0ff4c25 | 2017-09-13 10:43:30 +0300 | [diff] [blame] | 37 | parser.add_argument("-g", "--gzip", action="store_true", |
Anas Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 38 | help="Compress the file using gzip before output") |
Marc Herbert | 3061c92 | 2019-03-21 14:40:45 -0700 | [diff] [blame] | 39 | parser.add_argument("-t", "--gzip-mtime", type=int, default=0, |
Ruslan Mstoi | a8e0655 | 2020-06-01 14:29:46 +0300 | [diff] [blame] | 40 | nargs='?', const=None, |
Marc Herbert | 3061c92 | 2019-03-21 14:40:45 -0700 | [diff] [blame] | 41 | help="""mtime seconds in the gzip header. |
| 42 | Defaults to zero to keep builds deterministic. For |
| 43 | current date and time (= "now") use this option |
| 44 | without any value.""") |
Jukka Rissanen | 0ff4c25 | 2017-09-13 10:43:30 +0300 | [diff] [blame] | 45 | args = parser.parse_args() |
| 46 | |
Anas Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 47 | |
Jukka Rissanen | 0ff4c25 | 2017-09-13 10:43:30 +0300 | [diff] [blame] | 48 | def get_nice_string(list_or_iterator): |
Irfan Ahmad | e30c108 | 2025-01-23 00:26:04 +0500 | [diff] [blame] | 49 | # Convert into comma separated list form. |
| 50 | s = ", ".join("0x" + str(x) for x in list_or_iterator) |
| 51 | |
| 52 | # Format the list to eight values per line. |
| 53 | return "\n".join(s[i:i+47] for i in range(0, len(s), 48)) |
Anas Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 54 | |
Jukka Rissanen | 0ff4c25 | 2017-09-13 10:43:30 +0300 | [diff] [blame] | 55 | |
| 56 | def make_hex(chunk): |
| 57 | hexdata = codecs.encode(chunk, 'hex').decode("utf-8") |
Anas Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 58 | hexlist = map(''.join, zip(*[iter(hexdata)] * 2)) |
Jukka Rissanen | 0ff4c25 | 2017-09-13 10:43:30 +0300 | [diff] [blame] | 59 | print(get_nice_string(hexlist) + ',') |
| 60 | |
Anas Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 61 | |
Irfan Ahmad | f3d513d | 2025-01-17 02:47:43 +0500 | [diff] [blame] | 62 | def make_string_literal(chunk): |
| 63 | hexdata = codecs.encode(chunk, 'hex').decode("utf-8") |
| 64 | hexlist = map(''.join, zip(*[iter(hexdata)] * 2)) |
| 65 | print(''.join("\\x" + str(x) for x in hexlist), end='') |
| 66 | |
| 67 | |
Jukka Rissanen | 0ff4c25 | 2017-09-13 10:43:30 +0300 | [diff] [blame] | 68 | def main(): |
| 69 | parse_args() |
| 70 | |
| 71 | if args.gzip: |
Marc Herbert | 195195a | 2019-03-21 14:24:00 -0700 | [diff] [blame] | 72 | with io.BytesIO() as content: |
| 73 | with open(args.file, 'rb') as fg: |
Pieter De Gendt | ae8c724 | 2023-09-13 16:22:42 +0200 | [diff] [blame] | 74 | fg.seek(args.offset) |
Marc Herbert | 195195a | 2019-03-21 14:24:00 -0700 | [diff] [blame] | 75 | with gzip.GzipFile(fileobj=content, mode='w', |
Marc Herbert | 3061c92 | 2019-03-21 14:40:45 -0700 | [diff] [blame] | 76 | mtime=args.gzip_mtime, |
Marc Herbert | 195195a | 2019-03-21 14:24:00 -0700 | [diff] [blame] | 77 | compresslevel=9) as gz_obj: |
Pieter De Gendt | ae8c724 | 2023-09-13 16:22:42 +0200 | [diff] [blame] | 78 | gz_obj.write(fg.read(args.length)) |
Marc Herbert | 195195a | 2019-03-21 14:24:00 -0700 | [diff] [blame] | 79 | |
| 80 | content.seek(0) |
Irfan Ahmad | 8d42cd0 | 2025-02-18 05:02:28 +0500 | [diff] [blame] | 81 | if args.format == "literal": |
| 82 | print('"', end='') |
| 83 | for chunk in iter(lambda: content.read(1024), b''): |
| 84 | make_string_literal(chunk) |
| 85 | print('"', end='') |
| 86 | else: |
| 87 | for chunk in iter(lambda: content.read(1024), b''): |
| 88 | make_hex(chunk) |
Jukka Rissanen | 0ff4c25 | 2017-09-13 10:43:30 +0300 | [diff] [blame] | 89 | else: |
| 90 | with open(args.file, "rb") as fp: |
Pieter De Gendt | ae8c724 | 2023-09-13 16:22:42 +0200 | [diff] [blame] | 91 | fp.seek(args.offset) |
Irfan Ahmad | f3d513d | 2025-01-17 02:47:43 +0500 | [diff] [blame] | 92 | |
| 93 | if args.format == "literal": |
| 94 | if args.length < 0: |
| 95 | print('"', end='') |
| 96 | for chunk in iter(lambda: fp.read(1024), b''): |
| 97 | make_string_literal(chunk) |
| 98 | print('"', end='') |
| 99 | else: |
| 100 | print('"', end='') |
| 101 | remainder = args.length |
| 102 | for chunk in iter(lambda: fp.read(min(1024, remainder)), b''): |
| 103 | make_string_literal(chunk) |
| 104 | remainder = remainder - len(chunk) |
| 105 | print('"', end='') |
| 106 | |
Pieter De Gendt | ae8c724 | 2023-09-13 16:22:42 +0200 | [diff] [blame] | 107 | else: |
Irfan Ahmad | f3d513d | 2025-01-17 02:47:43 +0500 | [diff] [blame] | 108 | if args.length < 0: |
| 109 | for chunk in iter(lambda: fp.read(1024), b''): |
| 110 | make_hex(chunk) |
| 111 | else: |
| 112 | remainder = args.length |
| 113 | for chunk in iter(lambda: fp.read(min(1024, remainder)), b''): |
| 114 | make_hex(chunk) |
| 115 | remainder = remainder - len(chunk) |
Jukka Rissanen | 0ff4c25 | 2017-09-13 10:43:30 +0300 | [diff] [blame] | 116 | |
Anas Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 117 | |
Jukka Rissanen | 0ff4c25 | 2017-09-13 10:43:30 +0300 | [diff] [blame] | 118 | if __name__ == "__main__": |
| 119 | main() |