From b67635e47d40b313cef228283e28185ac31cbf35 Mon Sep 17 00:00:00 2001 From: sylvain Date: Tue, 15 Oct 2013 22:14:18 +0200 Subject: [PATCH] change type management --- rdpy/protocol/common/network.py | 238 ++++++++++++++++++++++++++++++++ rdpy/protocol/common/stream.py | 172 ----------------------- rdpy/protocol/rfb/rfb.py | 13 +- 3 files changed, 245 insertions(+), 178 deletions(-) create mode 100644 rdpy/protocol/common/network.py delete mode 100644 rdpy/protocol/common/stream.py diff --git a/rdpy/protocol/common/network.py b/rdpy/protocol/common/network.py new file mode 100644 index 0000000..4013481 --- /dev/null +++ b/rdpy/protocol/common/network.py @@ -0,0 +1,238 @@ +''' +@author: sylvain +''' + +import struct +from StringIO import StringIO +from error import InvalidValue + +class Type(object): + ''' + root type + ''' + def write(self, s): + ''' + interface definition of write function + ''' + pass + + def read(self, s): + ''' + interface definition of read value + ''' + pass + +class SimpleType(Type): + ''' + simple type + ''' + def __init__(self, typeSize, structFormat, value): + self._typeSize = typeSize + self._structFormat = structFormat + self._value = value + + @property + def value(self): + return self._value; + + 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,s.read(self._typeSize))[0] + +class CompositeType(Type): + ''' + keep ordering declaration of simple type + in list and transparent for other type + ''' + def __init__(self): + ''' + init list of simple value + ''' + self._type = [] + + def __setattr__(self, name, value): + ''' + magic function to update type list + ''' + if isinstance(value, Type): + self._type.append(value) + self.__dict__[name] = value + + def __iter__(self): + ''' + iteration over object + ''' + for i in self._type: + yield i + + def write(self, s): + ''' + call format on each ordered subtype + ''' + for i in self._type: + i.write(s) + +class UInt8(SimpleType): + ''' + unsigned byte + ''' + def __init__(self, value = 0): + ''' + constructor check value range + ''' + if value < 0 or value > 0xff: + raise InvalidValue("invalid UInt8 value") + SimpleType.__init__(self, "B", 1, value) + +class UInt16Be(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 UInt16Be value") + SimpleType.__init__(self, ">H", 2, value) + +class UInt16Le(SimpleType): + ''' + unsigned short with little 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, " 0xffffffff: + raise InvalidValue("invalid UInt32Be value") + SimpleType.__init__(self, ">I", 4, value) + +class UInt32Le(SimpleType): + ''' + unsigned int with little endian representation + ''' + def __init__(self, value = 0): + ''' + constructor check value range + ''' + if value < 0 or value > 0xffffffff: + raise InvalidValue("invalid UInt32Le value") + SimpleType.__init__(self, " 0x7fffffff: + raise InvalidValue("invalid UInt32Le value") + SimpleType.__init__(self, " 0x7fffffff: + raise InvalidValue("invalid UInt32Be value") + SimpleType.__init__(self, ">I", 4, value) + +class UInt24Be(SimpleType): + ''' + unsigned int with big endian representation + ''' + def __init__(self, value = 0): + ''' + constructor check value range + ''' + if value < 0 or value > 0xffffff: + raise InvalidValue("invalid UInt24Be value") + SimpleType.__init__(self, ">I", 3, value) + + def write(self, s): + ''' + special write for a special type + ''' + s.write(struct.pack(">I", self._value)[1:]) + + def read(self, s): + ''' + special read for a special type + ''' + self._value = struct.unpack(">I",s.read(3))[0] + +class UInt24Le(SimpleType): + ''' + unsigned int with little endian representation + ''' + def __init__(self, value = 0): + ''' + constructor check value range + ''' + if value < 0 or value > 0xffffff: + raise InvalidValue("invalid UInt24Le value") + SimpleType.__init__(self, " 0xff: - raise InvalidValue("invalid uint8 value") - SimpleType.__init__(self, "B", 1, value) - -class Uint16Be(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 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, "I",'\x00'+self.read(3))[0] - - def read_leuint24(self): - return struct.unpack("I",self.read(4))[0] - - def read_leuint32(self): - return struct.unpack("i",self.read(4))[0] - - def read_lesint32(self): - return struct.unpack("I", value)[1:]) - - def write_beuint32(self, value): - self.write(struct.pack(">I", value)) - - def write_leuint32(self, value): - self.write(struct.pack("i", value)) - - def write_lesint32(self, value): - self.write(struct.pack("