From d3dcb1184bfd8ae865af051fdb917b0345dde378 Mon Sep 17 00:00:00 2001 From: Jason Ekstrand Date: Wed, 7 Oct 2020 12:40:42 -0500 Subject: [PATCH] util/xxd.py: Add an option for binary files If -b is specified, we don't add a null to the end of the char array. If -b is not specified, we assert that there are no nulls in the middle. Reviewed-by: Jesse Natalie Part-of: --- src/util/xxd.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/util/xxd.py b/src/util/xxd.py index b9559bf..efff14d 100644 --- a/src/util/xxd.py +++ b/src/util/xxd.py @@ -34,6 +34,8 @@ def get_args(): parser.add_argument('output', help="Name of output file") parser.add_argument("-n", "--name", help="Name of C variable") + parser.add_argument("-b", "--binary", dest='binary', action='store_const', + const=True, default=False) args = parser.parse_args() return args @@ -69,13 +71,17 @@ def process_file(args): if byte == b"": break + if not args.binary: + assert(ord(byte) != 0) + emit_byte(outfile, byte) linecount = linecount + 1 if linecount > 20: outfile.write(b"\n ") linecount = 0 - - outfile.write(b"\n0 };\n") + if not args.binary: + outfile.write(b"\n0") + outfile.write(b"\n};\n\n") except Exception: # In the event that anything goes wrong, delete the output file, # then re-raise the exception. Deleteing the output file should -- 2.7.4