laforge has uploaded this change for review. ( https://gerrit.osmocom.org/c/pysim/+/35493?usp=email )
Change subject: add contrib/unber.py utility ......................................................................
add contrib/unber.py utility
This tool is a replacement for asn1c 'unber' program with a much more useful/readable output: * contains hexadecimal raw tag values * contains hexdump of value, rather than HTML entities in pseudo-XML
Change-Id: I22c1a461ccba04c2c8caaab7ca29ea6ae76e2ea3 --- A contrib/unber.py 1 file changed, 53 insertions(+), 0 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/pysim refs/changes/93/35493/1
diff --git a/contrib/unber.py b/contrib/unber.py new file mode 100755 index 0000000..65262e1 --- /dev/null +++ b/contrib/unber.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python3 + +# A more useful verion of the 'unber' tool provided with asn1c: +# Give a hierarchical decode of BER/DER-encoded ASN.1 TLVs + +import sys +import argparse + +from pySim.utils import bertlv_parse_one, bertlv_encode_tag, b2h, h2b + +def process_one_level(content: bytes, indent: int): + remainder = content + while len(remainder): + tdict, l, v, remainder = bertlv_parse_one(remainder) + #print(tdict) + rawtag = bertlv_encode_tag(tdict) + if tdict['constructed']: + print("%s%s l=%d" % (indent*" ", b2h(rawtag), l)) + process_one_level(v, indent + 1) + else: + print("%s%s l=%d %s" % (indent*" ", b2h(rawtag), l, b2h(v))) + + +option_parser = argparse.ArgumentParser(description='BER/DER data dumper') +group = option_parser.add_mutually_exclusive_group(required=True) +group.add_argument('--file', help='Input file') +group.add_argument('--hex', help='Input hexstring') + + +if __name__ == '__main__': + opts = option_parser.parse_args() + + if opts.file: + with open(opts.file, 'rb') as f: + content = f.read() + elif opts.hex: + content = h2b(opts.hex) + + process_one_level(content, 0)