From 58505948eaf4d4259b32536ccdb3bb0bf92cbbf2 Mon Sep 17 00:00:00 2001 From: hploetz Date: Sun, 26 Nov 2006 05:09:30 +0000 Subject: [PATCH] allow 00 and ff filler bytes to be returned optionally git-svn-id: svn+ssh://localhost/home/henryk/svn/cyberflex-shell/trunk@147 f711b948-2313-0410-aaa9-d29f33439f0b --- TLV_utils.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/TLV_utils.py b/TLV_utils.py index 7a7f4b0..220f431 100644 --- a/TLV_utils.py +++ b/TLV_utils.py @@ -339,10 +339,15 @@ def decode(data, context = None, level = 0, tags=tags): return "\n".join(result) -def unpack(data, with_marks = None, offset = 0): +def unpack(data, with_marks = None, offset = 0, include_filler=False): result = [] while len(data) > 0: if ord(data[0]) in (0x00, 0xFF): + if include_filler: + if with_marks is None: + result.append( (ord(data[0]), None, None) ) + else: + result.append( (ord(data[0]), None, None, () ) ) data = data[1:] offset = offset + 1 continue @@ -375,6 +380,10 @@ def pack(tlv_data, recalculate_length = False): for data in tlv_data: tag, length, value = data[:3] + if tag in (0xff, 0x00): + result.append( chr(tag) ) + continue + if not isinstance(value, str): value = pack(value, recalculate_length) @@ -412,8 +421,8 @@ if __name__ == "__main__": #print decode(file("c100").read()) marks = [ ('[', 5, 8) ] - a = binascii.a2b_hex( "".join( "80 01 aa b0 03 81 01 bb ".split() ) ) - b = unpack( a, with_marks=marks) + a = binascii.a2b_hex( "".join( "80 01 aa b0 03 81 01 bb ff ff 00".split() ) ) + b = unpack( a, with_marks=marks, include_filler=True) print b c = pack(b, recalculate_length = True) print utils.hexdump(a)