util/xxd.py: Add an option for binary files
authorJason Ekstrand <jason@jlekstrand.net>
Wed, 7 Oct 2020 17:40:42 +0000 (12:40 -0500)
committerMarge Bot <eric+marge@anholt.net>
Wed, 7 Oct 2020 21:52:04 +0000 (21:52 +0000)
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>

src/util/xxd.py

index b9559bf..efff14d 100644 (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