helper function to search through TLV trees. (TODO: Create proper class for TLV data)
git-svn-id: svn+ssh://localhost/home/henryk/svn/cyberflex-shell/trunk@215 f711b948-2313-0410-aaa9-d29f33439f0b
This commit is contained in:
parent
a024e73aa6
commit
9d071e7451
21
TLV_utils.py
21
TLV_utils.py
|
@ -375,6 +375,27 @@ def decode(data, context = None, level = 0, tags=tags):
|
||||||
|
|
||||||
return "\n".join(result)
|
return "\n".join(result)
|
||||||
|
|
||||||
|
def tlv_find_tag(tlv_data, tag, num_results = None):
|
||||||
|
"""Find (and return) all instances of tag in the given tlv structure (as returned by unpack).
|
||||||
|
If num_results is specified then at most that many results will be returned."""
|
||||||
|
|
||||||
|
results = []
|
||||||
|
def find_recursive(tlv_data):
|
||||||
|
for d in tlv_data:
|
||||||
|
t,l,v = d[:3]
|
||||||
|
if t == tag:
|
||||||
|
results.append(d)
|
||||||
|
else:
|
||||||
|
if isinstance(v, list): # FIXME Refactor the whole TLV code into a class
|
||||||
|
find_recursive(v)
|
||||||
|
|
||||||
|
if num_results is not None and len(results) >= num_results:
|
||||||
|
return
|
||||||
|
|
||||||
|
find_recursive(tlv_data)
|
||||||
|
|
||||||
|
return results
|
||||||
|
|
||||||
def unpack(data, with_marks = None, offset = 0, include_filler=False):
|
def unpack(data, with_marks = None, offset = 0, include_filler=False):
|
||||||
result = []
|
result = []
|
||||||
while len(data) > 0:
|
while len(data) > 0:
|
||||||
|
|
Loading…
Reference in New Issue