From 2d5f92a0275889f2df1f711ce63b9f396deec451 Mon Sep 17 00:00:00 2001 From: speyrefitte Date: Tue, 15 Oct 2013 18:03:31 +0200 Subject: [PATCH] add new primitif types --- rdpy/protocol/common/stream.py | 73 ++++++++++++++++++++-------------- 1 file changed, 43 insertions(+), 30 deletions(-) diff --git a/rdpy/protocol/common/stream.py b/rdpy/protocol/common/stream.py index 02e6196..024a83c 100644 --- a/rdpy/protocol/common/stream.py +++ b/rdpy/protocol/common/stream.py @@ -26,10 +26,24 @@ class SimpleType(Type): ''' simple type ''' - def __init__(self, value): + def __init__(self, typeSize, structFormat, value): + self._typeSize = typeSize + self._structFormat = structFormat self._value = value -class ComplexType(Type): + def write(self, s): + ''' + write value in stream s + ''' + s.write(struct.pack(self._structFormat, self._value)) + + def read(self, s): + ''' + read value from stream + ''' + self._value = struct.unpack(self._structFormat,self.read(self._typeSize))[0] + +class CompositeType(Type): ''' keep ordering declaration of simple type in list and transparent for other type @@ -66,25 +80,38 @@ class Uint8(SimpleType): ''' unsigned byte ''' - def __init__(self, value): + def __init__(self, value = 0): ''' constructor check value range ''' if value < 0 or value > 0xff: raise InvalidValue("invalid uint8 value") - SimpleType.__init__(self, value) - - def write(self, s): - ''' - write value in stream s - ''' - s.write(struct.pack("B", self._value)) + SimpleType.__init__(self, "B", 1, value) - def read(self, s): +class Uint16Be(SimpleType): + ''' + unsigned short with big endian representation + ''' + def __init__(self, value = 0): ''' - read value from stream + constructor check value range ''' - self._value = struct.unpack("B",self.read(1))[0] + if value < 0 or value > 0xffff: + raise InvalidValue("invalid Uint16Be value") + SimpleType.__init__(self, ">H", 2, value) + +class Uint16Le(SimpleType): + ''' + unsigned short with big endian representation + ''' + def __init__(self, value = 0): + ''' + constructor check value range + ''' + if value < 0 or value > 0xffff: + raise InvalidValue("invalid Uint16Le value") + SimpleType.__init__(self, "H",self.read(2))[0] - - def read_leuint16(self): - return struct.unpack("I",'\x00'+self.read(3))[0] @@ -124,17 +142,12 @@ class Stream(StringIO): def read_lesint32(self): return struct.unpack("H", value)) - def write_leuint16(self, value): - self.write(struct.pack("I", value)[1:])