2007-12-31 03:41:45 +00:00
|
|
|
#!/usr/bin/env python
|
2007-12-31 05:02:15 +00:00
|
|
|
from struct import pack, unpack
|
2007-12-31 03:41:45 +00:00
|
|
|
|
|
|
|
## Utilities
|
|
|
|
##
|
|
|
|
def choplist(n, seq):
|
|
|
|
'''Groups every n elements of the list.'''
|
|
|
|
r = []
|
|
|
|
for x in seq:
|
|
|
|
r.append(x)
|
|
|
|
if len(r) == n:
|
|
|
|
yield tuple(r)
|
|
|
|
r = []
|
|
|
|
return
|
|
|
|
|
|
|
|
def nunpack(s, default=0):
|
2008-01-09 14:21:24 +00:00
|
|
|
'''Unpacks up to 4 bytes big endian.'''
|
2007-12-31 03:41:45 +00:00
|
|
|
l = len(s)
|
|
|
|
if not l:
|
|
|
|
return default
|
|
|
|
elif l == 1:
|
|
|
|
return ord(s)
|
|
|
|
elif l == 2:
|
|
|
|
return unpack('>H', s)[0]
|
|
|
|
elif l == 3:
|
|
|
|
return unpack('>L', '\x00'+s)[0]
|
|
|
|
elif l == 4:
|
|
|
|
return unpack('>L', s)[0]
|
|
|
|
else:
|
|
|
|
return TypeError('invalid length: %d' % l)
|