Skip to content

Instantly share code, notes, and snippets.

@michiboo
Created May 11, 2022 20:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michiboo/07a9537a199af563b2eda330dc044ae2 to your computer and use it in GitHub Desktop.
Save michiboo/07a9537a199af563b2eda330dc044ae2 to your computer and use it in GitHub Desktop.
import io
mapping_table = {}
def bigsize_decode(r):
"""Decode an integer from reader `r`"""
raw = r.read(1)
if len(raw) != 1:
return None
i = int(raw,16)
if i < 0xFD:
return i
elif i == 0xFD:
return r.read(2).reverse()
elif i == 0xFE:
return r.read(4).reverse()
else:
return r.read(8).reverse()
def decodeinitMessage(rawMsg):
res = {}
res['data'] = {}
binary = io.BytesIO(rawMsg)
res['data']['type'] = int(binary.read(2),2)
res['data']['gflen'] = int(binary.read(2),2)
res['data']['globalFeatures'] = binary.read(res['gflen'])
res['data']['flen'] = int(binary.read(2),2)
res['data']['features'] = binary.read(res['flen'])
raw = binary.read(1)
res['tlv_stream']['init_tlvs'] = []
while len(raw) != 1:
tmp = []
tmp.append(bigsize_decode(binary))
tmp.append(bigsize_decode(binary))
tmp.append(bigsize_decode(binary))
raw = binary.read(1)
res['tlv_stream']['init_tlvs'].append(tmp)
return res
print(decodeinitMessage(b''))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment