add per functions

This commit is contained in:
speyrefitte
2013-10-21 18:02:04 +02:00
parent 19158040f4
commit 8a884c8c80
6 changed files with 206 additions and 40 deletions

View File

@@ -4,7 +4,6 @@ Created on 4 sept. 2013
''' '''
from PyQt4 import QtGui from PyQt4 import QtGui
from rdpy.protocol.rfb.observer import RfbObserver from rdpy.protocol.rfb.observer import RfbObserver
from rdpy.protocol.network.type import UInt8
class QAdaptor(object): class QAdaptor(object):
''' '''
@@ -40,16 +39,15 @@ class RfbAdaptor(RfbObserver, QAdaptor):
''' '''
implement RfbAdaptor interface implement RfbAdaptor interface
''' '''
imageFormat = None 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 imageFormat = QtGui.QImage.Format_RGB32
else: else:
print "Receive image in bad format" print "Receive image in bad format"
return return
image = QtGui.QImage(data, width.value, height.value, imageFormat) image = QtGui.QImage(data, width, height, imageFormat)
self.notifyImage(x.value, y.value, image) self.notifyImage(x, y, image)
def sendMouseEvent(self, e): def sendMouseEvent(self, e):
''' '''

View File

@@ -122,8 +122,8 @@ class RawLayer(protocol.Protocol, LayerAutomata):
def expect(self, expectedLen, callback = None): def expect(self, expectedLen, callback = None):
''' '''
configura layer to change nextsatte with callback only configure layer to change next state with callback only
when expectLen byte is received from transport layer when expectLen bytes is received from transport layer
@param expectedLen: in bytes len use to call nextstate @param expectedLen: in bytes len use to call nextstate
@param callback: callback call when expectedlen bytes is received @param callback: callback call when expectedlen bytes is received
''' '''

View File

@@ -73,7 +73,10 @@ class SimpleType(Type):
shortcut to access inner value shortcut to access inner value
@return: inner value(python type value) @return: inner value(python type value)
''' '''
return self._value if self._signed:
return self._value
else:
return self._value & self.mask()
@value.setter @value.setter
def value(self, value): def value(self, value):
@@ -93,7 +96,7 @@ class SimpleType(Type):
@param other: Type value which will be compared with self value @param other: Type value which will be compared with self value
@return: python value compare @return: python value compare
''' '''
return self._value.__cmp__(other.value) return self.value.__cmp__(other.value)
def write(self, s): def write(self, s):
''' '''
@@ -160,7 +163,7 @@ class SimpleType(Type):
@return: self.__class__ object with add result @return: self.__class__ object with add result
@raise InvalidValue: if new value is out of bound @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): def __sub__(self, other):
''' '''
@@ -170,7 +173,7 @@ class SimpleType(Type):
@return: self.__class__ object with sub result @return: self.__class__ object with sub result
@raise InvalidValue: if new value is out of bound @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): def __and__(self, other):
''' '''
@@ -178,7 +181,7 @@ class SimpleType(Type):
@param other: SimpleType value @param other: SimpleType value
@return: self.__class__ object with and result @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): def __or__(self, other):
''' '''
@@ -186,7 +189,7 @@ class SimpleType(Type):
@param other: SimpleType value @param other: SimpleType value
@return: self.__class__ object with or result @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): class CompositeType(Type):

View File

@@ -48,7 +48,7 @@ def readLength(s):
read length of ber structure read length of ber structure
length be on 1 2 or 3 bytes length be on 1 2 or 3 bytes
@param s: stream @param s: stream
@return: Uint8 or UInt16Be length @return: int or python long
''' '''
size = None size = None
byte = UInt8() byte = UInt8()
@@ -64,18 +64,18 @@ def readLength(s):
s.readType(size) s.readType(size)
else: else:
size = byte size = byte
return size return size.value
def writeLength(size): def writeLength(size):
''' '''
return strcture length as expected in Ber specification 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) @return: UInt8 or (UInt8(0x82), UInt16Be)
''' '''
if size > UInt16Be(0x7f): if size > 0x7f:
return (UInt8(0x82), UInt16Be(size.value)) return (UInt8(0x82), UInt16Be(size))
else: else:
return UInt8(size.value) return UInt8(size)
def readUniversalTag(s, tag, pc): def readUniversalTag(s, tag, pc):
''' '''
@@ -102,7 +102,7 @@ def readApplicationTag(s, tag):
read application tag read application tag
@param s: stream @param s: stream
@param tag: tag class attributes @param tag: tag class attributes
@return: true if tag is read with success @return: length of application packet
''' '''
byte = UInt8() byte = UInt8()
s.readType(byte) s.readType(byte)
@@ -138,7 +138,7 @@ def readBoolean(s):
if not readUniversalTag(s, Tag.BER_TAG_BOOLEAN, False): if not readUniversalTag(s, Tag.BER_TAG_BOOLEAN, False):
raise InvalidExpectedDataException("bad boolean tag") raise InvalidExpectedDataException("bad boolean tag")
size = readLength(s) size = readLength(s)
if size != UInt8(1): if size != 1:
raise InvalidExpectedDataException("bad boolean size") raise InvalidExpectedDataException("bad boolean size")
b = UInt8() b = UInt8()
s.readType(b) s.readType(b)
@@ -150,37 +150,37 @@ def writeBoolean(b):
@param b: boolean @param b: boolean
@return: ber boolean structure @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): def readInteger(s):
''' '''
read integer structure from stream read integer structure from stream
@param s: stream @param s: stream
@return: UInt8, UInt16Be, UInt32Be @return: int or long python
''' '''
if not readUniversalTag(s, Tag.BER_TAG_INTEGER, False): if not readUniversalTag(s, Tag.BER_TAG_INTEGER, False):
raise InvalidExpectedDataException("bad integer tag") raise InvalidExpectedDataException("bad integer tag")
size = readLength(s) size = readLength(s)
if size == UInt16Be(1): if size == 1:
integer = UInt8() integer = UInt8()
s.readType(integer) s.readType(integer)
return integer return integer.value
elif size == UInt16Be(2): elif size == 2:
integer = UInt16Be() integer = UInt16Be()
s.readType(integer) s.readType(integer)
return integer return integer.value
elif size == UInt16Be(3): elif size == 3:
integer1 = UInt8() integer1 = UInt8()
integer2 = UInt16Be() integer2 = UInt16Be()
s.readType(integer1) s.readType(integer1)
s.readType(integer2) s.readType(integer2)
return UInt32Be(integer2.value + (integer1.value << 16)) return integer2.value + (integer1.value << 16)
elif size == UInt16Be(4): elif size == 4:
integer = UInt32Be() integer = UInt32Be()
s.readType(integer) s.readType(integer)
return integer return integer.value
else: else:
raise InvalidExpectedDataException("wrong integer size") raise InvalidExpectedDataException("wrong integer size")
@@ -191,11 +191,11 @@ def writeInteger(value):
@return ber interger structure @return ber interger structure
''' '''
if value < UInt32Be(0xff): 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): 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: 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): def readOctetString(s):
''' '''
@@ -214,13 +214,13 @@ def writeOctetstring(value):
@param value: String @param value: String
@return: string ber structure @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): def readEnumerated(s):
''' '''
read enumerated structure read enumerated structure
@param s: Stream @param s: Stream
@return: UInt8 that represent ber enumerate @return: int or long
''' '''
if not readUniversalTag(s, Tag.BER_TAG_ENUMERATED, False): if not readUniversalTag(s, Tag.BER_TAG_ENUMERATED, False):
raise InvalidExpectedDataException("invalid ber tag") raise InvalidExpectedDataException("invalid ber tag")
@@ -229,4 +229,4 @@ def readEnumerated(s):
raise InvalidExpectedDataException("enumerate size is wrong") raise InvalidExpectedDataException("enumerate size is wrong")
enumer = UInt8() enumer = UInt8()
s.readType(enumer) s.readType(enumer)
return enumer return enumer.value

View File

@@ -3,12 +3,13 @@
''' '''
from rdpy.protocol.network.type import UInt8, UInt16Be, UInt32Be from rdpy.protocol.network.type import UInt8, UInt16Be, UInt32Be
from rdpy.protocol.network.error import InvalidValue, InvalidExpectedDataException
def readLength(s): def readLength(s):
''' '''
read length use in per specification read length use in per specification
@param s: Stream @param s: Stream
@return: UInt16Be @return: int python
''' '''
byte = UInt8() byte = UInt8()
s.readType(byte) s.readType(byte)
@@ -20,4 +21,168 @@ def readLength(s):
size += s.value + byte size += s.value + byte
else: else:
size = UInt16Be(byte.value) size = UInt16Be(byte.value)
return size 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")

View File

@@ -216,7 +216,7 @@ class Rfb(RawLayer):
read body of rect read body of rect
''' '''
for observer in self._observer: 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 self._nbRect = self._nbRect - 1
#if there is another rect to read #if there is another rect to read
if self._nbRect == 0: if self._nbRect == 0: