diff options
author | hploetz <hploetz@f711b948-2313-0410-aaa9-d29f33439f0b> | 2006-11-23 15:02:23 +0000 |
---|---|---|
committer | hploetz <hploetz@f711b948-2313-0410-aaa9-d29f33439f0b> | 2006-11-23 15:02:23 +0000 |
commit | 124c2c3b601eae6bd7fc536b18606e9b17453071 (patch) | |
tree | 2528246337ed6f2ae8850d43c1b0e1de8c9c1675 /TLV_utils.py | |
parent | aa5653027968b58b2ffe64445edc56accf9130da (diff) |
implement pack()
git-svn-id: svn+ssh://localhost/home/henryk/svn/cyberflex-shell/trunk@142 f711b948-2313-0410-aaa9-d29f33439f0b
Diffstat (limited to 'TLV_utils.py')
-rw-r--r-- | TLV_utils.py | 52 |
1 files changed, 44 insertions, 8 deletions
diff --git a/TLV_utils.py b/TLV_utils.py index ea1719a..7a7f4b0 100644 --- a/TLV_utils.py +++ b/TLV_utils.py @@ -283,12 +283,12 @@ def tlv_unpack(data): if length < 0x80: data = data[1:] elif length & 0x80 == 0x80: - length_ = 0 - data = data[1:] - for i in range(0,length & 0x7F): - length_ = length_ * 256 + ord(data[0]) - data = data[1:] - length = length_ + length_ = 0 + data = data[1:] + for i in range(0,length & 0x7F): + length_ = length_ * 256 + ord(data[0]) + data = data[1:] + length = length_ value = data[:length] rest = data[length:] @@ -370,6 +370,38 @@ def unpack(data, with_marks = None, offset = 0): return result +def pack(tlv_data, recalculate_length = False): + result = [] + + for data in tlv_data: + tag, length, value = data[:3] + if not isinstance(value, str): + value = pack(value, recalculate_length) + + if recalculate_length: + length = len(value) + + t = "" + while tag > 0: + t = chr( tag & 0xff ) + t + tag = tag >> 8 + + if length < 0x7F: + l = chr(length) + else: + l = "" + while length > 0: + l = chr( length & 0xff ) + l + length = length >> 8 + assert len(l) < 0x7f + l = chr( 0x80 | len(l) ) + l + + result.append(t) + result.append(l) + result.append(value) + + return "".join(result) + if __name__ == "__main__": test = binascii.unhexlify("".join(("6f 2b 83 02 2f 00 81 02 01 00 82 03 05 41 26 85" \ +"02 01 00 86 18 60 00 00 00 ff ff b2 00 00 00 ff" \ @@ -380,5 +412,9 @@ if __name__ == "__main__": #print decode(file("c100").read()) marks = [ ('[', 5, 8) ] - print unpack( binascii.a2b_hex( "".join( "80 01 aa b0 03 81 01 bb ".split() ) ), with_marks=marks) - + a = binascii.a2b_hex( "".join( "80 01 aa b0 03 81 01 bb ".split() ) ) + b = unpack( a, with_marks=marks) + print b + c = pack(b, recalculate_length = True) + print utils.hexdump(a) + print utils.hexdump(c) |