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 <jenatali@microsoft.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/7034>
This commit is contained in:
Jason Ekstrand 2020-10-07 12:40:42 -05:00 committed by Marge Bot
parent 82bbf4c3f2
commit d3dcb1184b
1 changed files with 8 additions and 2 deletions

View File

@ -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