From 8a884c8c8006130cffaec122d020689491c4f32a Mon Sep 17 00:00:00 2001 From: speyrefitte Date: Mon, 21 Oct 2013 18:02:04 +0200 Subject: [PATCH] add per functions --- rdpy/display/qt/adaptor.py | 8 +- rdpy/protocol/network/layer.py | 4 +- rdpy/protocol/network/type.py | 15 +-- rdpy/protocol/rdp/ber.py | 48 +++++----- rdpy/protocol/rdp/per.py | 169 ++++++++++++++++++++++++++++++++- rdpy/protocol/rfb/rfb.py | 2 +- 6 files changed, 206 insertions(+), 40 deletions(-) diff --git a/rdpy/display/qt/adaptor.py b/rdpy/display/qt/adaptor.py index 9570ded..0f31028 100644 --- a/rdpy/display/qt/adaptor.py +++ b/rdpy/display/qt/adaptor.py @@ -4,7 +4,6 @@ Created on 4 sept. 2013 ''' from PyQt4 import QtGui from rdpy.protocol.rfb.observer import RfbObserver -from rdpy.protocol.network.type import UInt8 class QAdaptor(object): ''' @@ -40,16 +39,15 @@ class RfbAdaptor(RfbObserver, QAdaptor): ''' implement RfbAdaptor interface ''' - imageFormat = None - if pixelFormat.BitsPerPixel == UInt8(32) and pixelFormat.RedShift == UInt8(16): + if pixelFormat.BitsPerPixel.value == 32 and pixelFormat.RedShift.value == 16: imageFormat = QtGui.QImage.Format_RGB32 else: print "Receive image in bad format" return - image = QtGui.QImage(data, width.value, height.value, imageFormat) - self.notifyImage(x.value, y.value, image) + image = QtGui.QImage(data, width, height, imageFormat) + self.notifyImage(x, y, image) def sendMouseEvent(self, e): ''' diff --git a/rdpy/protocol/network/layer.py b/rdpy/protocol/network/layer.py index e83c0e8..15b1c9e 100644 --- a/rdpy/protocol/network/layer.py +++ b/rdpy/protocol/network/layer.py @@ -122,8 +122,8 @@ class RawLayer(protocol.Protocol, LayerAutomata): def expect(self, expectedLen, callback = None): ''' - configura layer to change nextsatte with callback only - when expectLen byte is received from transport layer + configure layer to change next state with callback only + when expectLen bytes is received from transport layer @param expectedLen: in bytes len use to call nextstate @param callback: callback call when expectedlen bytes is received ''' diff --git a/rdpy/protocol/network/type.py b/rdpy/protocol/network/type.py index 3fa91e1..2ee4fbe 100644 --- a/rdpy/protocol/network/type.py +++ b/rdpy/protocol/network/type.py @@ -73,7 +73,10 @@ class SimpleType(Type): shortcut to access inner value @return: inner value(python type value) ''' - return self._value + if self._signed: + return self._value + else: + return self._value & self.mask() @value.setter def value(self, value): @@ -93,7 +96,7 @@ class SimpleType(Type): @param other: Type value which will be compared with self value @return: python value compare ''' - return self._value.__cmp__(other.value) + return self.value.__cmp__(other.value) def write(self, s): ''' @@ -160,7 +163,7 @@ class SimpleType(Type): @return: self.__class__ object with add result @raise InvalidValue: if new value is out of bound ''' - return self.__class__(self._value.__add__(other._value)) + return self.__class__(self.value.__add__(other.value)) def __sub__(self, other): ''' @@ -170,7 +173,7 @@ class SimpleType(Type): @return: self.__class__ object with sub result @raise InvalidValue: if new value is out of bound ''' - return self.__class__(self._value.__sub__(other._value)) + return self.__class__(self.value.__sub__(other.value)) def __and__(self, other): ''' @@ -178,7 +181,7 @@ class SimpleType(Type): @param other: SimpleType value @return: self.__class__ object with and result ''' - return self.__class__(self._value.__and__(other._value)) + return self.__class__(self.value.__and__(other.value)) def __or__(self, other): ''' @@ -186,7 +189,7 @@ class SimpleType(Type): @param other: SimpleType value @return: self.__class__ object with or result ''' - return self.__class__(self._value.__or__(other._value)) + return self.__class__(self.value.__or__(other.value)) class CompositeType(Type): diff --git a/rdpy/protocol/rdp/ber.py b/rdpy/protocol/rdp/ber.py index 6dd9e80..3257d26 100644 --- a/rdpy/protocol/rdp/ber.py +++ b/rdpy/protocol/rdp/ber.py @@ -48,7 +48,7 @@ def readLength(s): read length of ber structure length be on 1 2 or 3 bytes @param s: stream - @return: Uint8 or UInt16Be length + @return: int or python long ''' size = None byte = UInt8() @@ -64,18 +64,18 @@ def readLength(s): s.readType(size) else: size = byte - return size + return size.value def writeLength(size): ''' return strcture length as expected in Ber specification - @param size: UInt8 or UInt16Be size to write + @param size: int or python long @return: UInt8 or (UInt8(0x82), UInt16Be) ''' - if size > UInt16Be(0x7f): - return (UInt8(0x82), UInt16Be(size.value)) + if size > 0x7f: + return (UInt8(0x82), UInt16Be(size)) else: - return UInt8(size.value) + return UInt8(size) def readUniversalTag(s, tag, pc): ''' @@ -102,7 +102,7 @@ def readApplicationTag(s, tag): read application tag @param s: stream @param tag: tag class attributes - @return: true if tag is read with success + @return: length of application packet ''' byte = UInt8() s.readType(byte) @@ -138,7 +138,7 @@ def readBoolean(s): if not readUniversalTag(s, Tag.BER_TAG_BOOLEAN, False): raise InvalidExpectedDataException("bad boolean tag") size = readLength(s) - if size != UInt8(1): + if size != 1: raise InvalidExpectedDataException("bad boolean size") b = UInt8() s.readType(b) @@ -150,37 +150,37 @@ def writeBoolean(b): @param b: boolean @return: ber boolean structure ''' - return (writeUniversalTag(Tag.BER_TAG_BOOLEAN, False), writeLength(UInt8(1)), UInt8(int(b))) + return (writeUniversalTag(Tag.BER_TAG_BOOLEAN, False), writeLength(1), UInt8(int(b))) def readInteger(s): ''' read integer structure from stream @param s: stream - @return: UInt8, UInt16Be, UInt32Be + @return: int or long python ''' if not readUniversalTag(s, Tag.BER_TAG_INTEGER, False): raise InvalidExpectedDataException("bad integer tag") size = readLength(s) - if size == UInt16Be(1): + if size == 1: integer = UInt8() s.readType(integer) - return integer - elif size == UInt16Be(2): + return integer.value + elif size == 2: integer = UInt16Be() s.readType(integer) - return integer - elif size == UInt16Be(3): + return integer.value + elif size == 3: integer1 = UInt8() integer2 = UInt16Be() s.readType(integer1) s.readType(integer2) - return UInt32Be(integer2.value + (integer1.value << 16)) - elif size == UInt16Be(4): + return integer2.value + (integer1.value << 16) + elif size == 4: integer = UInt32Be() s.readType(integer) - return integer + return integer.value else: raise InvalidExpectedDataException("wrong integer size") @@ -191,11 +191,11 @@ def writeInteger(value): @return ber interger structure ''' if value < UInt32Be(0xff): - return (writeUniversalTag(Tag.BER_TAG_INTEGER, False), writeLength(UInt8(1)), UInt8(value.value)) + return (writeUniversalTag(Tag.BER_TAG_INTEGER, False), writeLength(1), UInt8(value.value)) elif value < UInt32Be(0xff80): - return (writeUniversalTag(Tag.BER_TAG_INTEGER, False), writeLength(UInt8(2)), UInt16Be(value.value)) + return (writeUniversalTag(Tag.BER_TAG_INTEGER, False), writeLength(2), UInt16Be(value.value)) else: - return (writeUniversalTag(Tag.BER_TAG_INTEGER, False), writeLength(UInt8(4)), UInt32Be(value.value)) + return (writeUniversalTag(Tag.BER_TAG_INTEGER, False), writeLength(4), UInt32Be(value.value)) def readOctetString(s): ''' @@ -214,13 +214,13 @@ def writeOctetstring(value): @param value: String @return: string ber structure ''' - return (writeUniversalTag(Tag.BER_TAG_OCTET_STRING, False), writeLength(UInt32Be(len(value.value))), value) + return (writeUniversalTag(Tag.BER_TAG_OCTET_STRING, False), writeLength(len(value.value)), value) def readEnumerated(s): ''' read enumerated structure @param s: Stream - @return: UInt8 that represent ber enumerate + @return: int or long ''' if not readUniversalTag(s, Tag.BER_TAG_ENUMERATED, False): raise InvalidExpectedDataException("invalid ber tag") @@ -229,4 +229,4 @@ def readEnumerated(s): raise InvalidExpectedDataException("enumerate size is wrong") enumer = UInt8() s.readType(enumer) - return enumer + return enumer.value diff --git a/rdpy/protocol/rdp/per.py b/rdpy/protocol/rdp/per.py index cfe651c..98ab8fb 100644 --- a/rdpy/protocol/rdp/per.py +++ b/rdpy/protocol/rdp/per.py @@ -3,12 +3,13 @@ ''' from rdpy.protocol.network.type import UInt8, UInt16Be, UInt32Be +from rdpy.protocol.network.error import InvalidValue, InvalidExpectedDataException def readLength(s): ''' read length use in per specification @param s: Stream - @return: UInt16Be + @return: int python ''' byte = UInt8() s.readType(byte) @@ -20,4 +21,168 @@ def readLength(s): size += s.value + byte else: size = UInt16Be(byte.value) - return size \ No newline at end of file + return size.value + +def writeLength(value): + ''' + write length as expected in per specification + @param value: int or long python + @return: UInt8, UInt16Be depend on value + ''' + if value > 0x7f: + return UInt16Be(value | 0x8000) + else: + return UInt8(value) + +def readChoice(s): + ''' + read per choice format + @param s: Stream + @return: int that represent choice + ''' + choice = UInt8() + s.readType(choice) + return choice.value + +def writeChoice(choice): + ''' + read per choice structure + @param choice: int choice value + @return: UInt8 + ''' + return UInt8(choice) + +def readSelection(s): + ''' + read per selection format + @param s: Stream + @return: int that represent selection + ''' + choice = UInt8() + s.readType(choice) + return choice.value + +def writeSelection(selection): + ''' + read per selection structure + @param selection: int selection value + @return: UInt8 + ''' + return UInt8(selection) + +def readNumberOfSet(s): + ''' + read per numberOfSet format + @param s: Stream + @return: int that represent numberOfSet + ''' + choice = UInt8() + s.readType(choice) + return choice.value + +def writeNumberOfSet(numberOfSet): + ''' + read per numberOfSet structure + @param numberOfSet: int numberOfSet value + @return: UInt8 + ''' + return UInt8(numberOfSet) + +def readEnumerates(s): + ''' + read per enumerate format + @param s: Stream + @return: int that represent enumerate + ''' + choice = UInt8() + s.readType(choice) + return choice.value + +def writeEnumerates(enumer): + ''' + read per enumerate structure + @param enumer: int enumerate value + @return: UInt8 + ''' + return UInt8(enumer) + +def readInteger(s): + ''' + read interger per format from stream + @param s: Stream + @return: python int or long + @raise InvalidValue: if size of integer is not correct + ''' + result = None + size = readLength(s) + if size == 1: + result = UInt8() + elif size == 2: + result = UInt16Be() + elif size == 4: + result = UInt32Be() + else: + raise InvalidValue("invalid integer size %d"%size) + s.readType(result) + return result.value + +def writeInteger(value): + ''' + write python long or int into per integer format + @param value: int or long python value + @return: UInt8, UInt16Be or UInt32Be + ''' + if value < 0xff: + return (writeLength(1), UInt8(value)) + elif value < 0xffff: + return (writeLength(2), UInt16Be(value)) + else: + return (writeLength(4), UInt32Be(value)) + +def readInteger16(s, minimum): + ''' + read UInt16Be from stream s and add minimum + @param s: Stream + @param minimum: minimum added to real value + @return: int or long python value + ''' + result = UInt16Be() + s.readType(result) + return result.value + minimum + +def writeInteger16(value, minimum): + ''' + write UInt16Be minus minimum + @param value: value to write + @param minimum: value subtracted to real value + @return: UInt16Be + ''' + return UInt16Be(value - minimum) + +def readObjectIdentifier(s, oid): + ''' + read object identifier + @param oid: must be a tuple of 6 elements + @param s: Stream + @return: true if oid is same as in stream + ''' + size = readLength(s) + if size != 5: + raise InvalidValue("size of stream oid is wrong %d != 5"%size) + a_oid = (0, 0, 0, 0, 0, 0) + t12 = UInt8() + s.readType(t12) + a_oid[0] = t12.value >> 4 + a_oid[1] = t12.value & 0x0f + s.readType(t12) + a_oid[3] = t12.value + s.readType(t12) + a_oid[4] = t12.value + s.readType(t12) + a_oid[5] = t12.value + s.readType(t12) + a_oid[6] = t12.value + + if oid != a_oid: + raise InvalidExpectedDataException("invalid object identifier") + \ No newline at end of file diff --git a/rdpy/protocol/rfb/rfb.py b/rdpy/protocol/rfb/rfb.py index 842c346..fe6b979 100644 --- a/rdpy/protocol/rfb/rfb.py +++ b/rdpy/protocol/rfb/rfb.py @@ -216,7 +216,7 @@ class Rfb(RawLayer): read body of rect ''' for observer in self._observer: - observer.notifyFramebufferUpdate(self._currentRect.width, self._currentRect.height, self._currentRect.x, self._currentRect.y, self._pixelFormat, self._currentRect.encoding, data.getvalue()) + observer.notifyFramebufferUpdate(self._currentRect.width.value, self._currentRect.height.value, self._currentRect.x.value, self._currentRect.y.value, self._pixelFormat, self._currentRect.encoding, data.getvalue()) self._nbRect = self._nbRect - 1 #if there is another rect to read if self._nbRect == 0: