-import struct as _S
-
-###--------------------------------------------------------------------------
-### Utilities.
-
-class Buffer (object):
- """
- I am a simple gadget for parsing binary strings.
-
- You should use Catacomb's ReadBuffer instead.
- """
-
- def __init__(me, s):
- """
- Initialize the buffer with a string S.
- """
- me.str = s
- me.i = 0
-
- def get(me, n):
- """
- Fetch and return the next N bytes from the buffer.
- """
- i = me.i
- if n + i > len(me.str):
- raise IndexError, 'buffer underflow'
- me.i += n
- return me.str[i:i + n]
-
- def getbyte(me):
- """
- Fetch and return (as a small integer) the next byte from the buffer.
- """
- return ord(me.get(1))
-
- def unpack(me, fmt):
- """
- Unpack a structure described by FMT from the next bytes of the buffer.
-
- Return a tuple containing the unpacked items.
- """
- return _S.unpack(fmt, me.get(_S.calcsize(fmt)))
-
- def getstring(me):
- """
- Fetch and return a counted string from the buffer.
-
- The string is expected to be preceded by its 16-bit length, in network
- byte order.
- """
- return me.get(me.unpack('>H')[0])
-
- def checkend(me):
- """
- Raise an error if the buffer has not been completely consumed.
- """
- if me.i != len(me.str):
- raise ValueError, 'junk at end of buffer'
-
-def _wrapstr(s):
- """
- Prefix the string S with its 16-bit length.
-
- It can be read using Buffer.getstring. You should use Catacomb's
- WriteBuffer.putblk16() function instead.
- """
- return _S.pack('>H', len(s)) + s