aboutsummaryrefslogtreecommitdiffstats
path: root/utils.py
diff options
context:
space:
mode:
authorhploetz <hploetz@f711b948-2313-0410-aaa9-d29f33439f0b>2006-05-19 02:24:48 +0000
committerhploetz <hploetz@f711b948-2313-0410-aaa9-d29f33439f0b>2006-05-19 02:24:48 +0000
commit60c80e35c7abbfcacd01f64fe42726e7155eac16 (patch)
tree2b1dc34ef4a999d4f6614e4b51c3873a32625ef7 /utils.py
parent80c19becfac73076cb7e36c7f232dea4be735860 (diff)
nicer TLV parsing, (more ugly code)
git-svn-id: svn+ssh://localhost/home/henryk/svn/cyberflex-shell/trunk@59 f711b948-2313-0410-aaa9-d29f33439f0b
Diffstat (limited to 'utils.py')
-rw-r--r--utils.py36
1 files changed, 36 insertions, 0 deletions
diff --git a/utils.py b/utils.py
index eae4982..62902a8 100644
--- a/utils.py
+++ b/utils.py
@@ -1,5 +1,41 @@
import pycsc, string, binascii, sys
+def represent_binary_fancy(len, value, mask = 0):
+ result = []
+ for i in range(len):
+ if i%4 == 0:
+ result.append( " " )
+ if i%8 == 0:
+ result.append( " " )
+ if mask & 0x01:
+ result.append( str(value & 0x01) )
+ else:
+ result.append( "." )
+ mask = mask >> 1
+ value = value >> 2
+ result.reverse()
+
+ return "".join(result).strip()
+
+def parse_binary(value, bytemasks, verbose = False, value_len = 8):
+ ## Parses a binary structure and gives information back
+ ## bytemasks is a sequence of (mask, value, string_if_no_match, string_if_match) tuples
+ result = []
+ for mask, byte, nonmatch, match in bytemasks:
+
+ if verbose:
+ prefix = represent_binary_fancy(value_len, byte, mask) + ": "
+ else:
+ prefix = ""
+ if (value & mask) == (byte & mask):
+ if match is not None:
+ result.append(prefix + match)
+ else:
+ if nonmatch is not None:
+ result.append(prefix + nonmatch)
+
+ return result
+
_myprintable = " " + string.letters + string.digits + string.punctuation
def hexdump(data, indent = 0, short = False):
r"""Generates a nice hexdump of data and returns it. Consecutive lines will