Extended crc32c.py to support hex sequences and strings
So now the following forms are supported: $ ./scripts/crc32c.py -x 41 42 43 44 fb9f8872 $ ./scripts/crc32c.py -s abcd fb9f8872 $ echo '00: 41 42 43 44' | xxd -r | ./scripts/crc32c.py fb9f8872 Hopefully this will make crc32c.py more useful. It hasn't seen very much use, though that may just be because of the difficulty marshalling data into a format crc32c.py can operate on. That and dbgblock.py's -x/--cksum flag covering one of the main use cases.
This commit is contained in:
+23
-3
@@ -1,8 +1,9 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
import os
|
|
||||||
import sys
|
|
||||||
import io
|
import io
|
||||||
|
import os
|
||||||
|
import struct
|
||||||
|
import sys
|
||||||
|
|
||||||
|
|
||||||
def openio(path, mode='r', buffering=-1):
|
def openio(path, mode='r', buffering=-1):
|
||||||
@@ -92,7 +93,18 @@ def crc32c(data, crc=0):
|
|||||||
return crc ^ 0xffffffff
|
return crc ^ 0xffffffff
|
||||||
|
|
||||||
|
|
||||||
def main(paths):
|
def main(paths, **args):
|
||||||
|
# interpret as sequence of hex bytes
|
||||||
|
if args.get('hex'):
|
||||||
|
print('%08x' % crc32c(bytes(int(path, 16) for path in paths)))
|
||||||
|
|
||||||
|
# interpret as strings
|
||||||
|
elif args.get('string'):
|
||||||
|
for path in paths:
|
||||||
|
print('%08x' % crc32c(path.encode('utf8')))
|
||||||
|
|
||||||
|
# default to interpreting as paths
|
||||||
|
else:
|
||||||
if not paths:
|
if not paths:
|
||||||
paths = [None]
|
paths = [None]
|
||||||
|
|
||||||
@@ -123,6 +135,14 @@ if __name__ == "__main__":
|
|||||||
'paths',
|
'paths',
|
||||||
nargs='*',
|
nargs='*',
|
||||||
help="Paths to read. Reads stdin by default.")
|
help="Paths to read. Reads stdin by default.")
|
||||||
|
parser.add_argument(
|
||||||
|
'-x', '--hex',
|
||||||
|
action='store_true',
|
||||||
|
help="Interpret as a sequence of hex bytes.")
|
||||||
|
parser.add_argument(
|
||||||
|
'-s', '--string',
|
||||||
|
action='store_true',
|
||||||
|
help="Interpret as strings.")
|
||||||
sys.exit(main(**{k: v
|
sys.exit(main(**{k: v
|
||||||
for k, v in vars(parser.parse_intermixed_args()).items()
|
for k, v in vars(parser.parse_intermixed_args()).items()
|
||||||
if v is not None}))
|
if v is not None}))
|
||||||
|
|||||||
Reference in New Issue
Block a user