work on bitwise operator

This commit is contained in:
speyrefitte
2013-10-18 16:48:47 +02:00
parent 2085498e73
commit 8831ca3596
5 changed files with 119 additions and 82 deletions

View File

@@ -47,9 +47,10 @@ class SimpleType(Type):
'''
simple type
'''
def __init__(self, structFormat, typeSize, value):
def __init__(self, structFormat, typeSize, signed, value):
self._typeSize = typeSize
self._structFormat = structFormat
self._signed = signed
self.value = value
@property
@@ -64,7 +65,7 @@ class SimpleType(Type):
'''
setter of value after check it
'''
if self.__class__.__dict__.has_key("isInRange") and not self.__class__.isInRange(value):
if not self.isInRange(value):
raise InvalidValue("value is out of range for %s"%self.__class__)
self._value = value
@@ -85,12 +86,69 @@ class SimpleType(Type):
read value from stream
'''
self._value = struct.unpack(self._structFormat,s.read(self._typeSize))[0]
def mask(self):
'''
compute bit mask for type
beacause in python all numbers are int long or float
'''
if not self.__dict__.has_key("_mask"):
mask = 0xff
for i in range(1, self._typeSize):
mask = mask << 8
mask |= 0xff
self._mask = mask
return self._mask
def isInRange(self, value):
'''
check if value is in mask range
'''
if self._signed:
return not (value < -(self.mask() >> 1) or value > (self.mask() >> 1))
else:
return not (value < 0 or value > self.mask())
def __sizeof__(self):
'''
return size of type
'''
return self._typeSize
def __invert__(self):
'''
implement not operator
'''
invert = ~self._value
if not self._signed:
invert &= self.mask()
return self.__class__(invert)
def __add__(self, other):
'''
implement addition operator
'''
return self.__class__(self._value.__add__(other._value))
def __sub__(self, other):
'''
implement sub operator
'''
return self.__class__(self._value.__sub__(other._value))
def __and__(self, other):
'''
implement bitwise and operator
'''
return self.__class__(self._value.__and__(other._value))
def __or__(self, other):
'''
implement bitwise and operator
'''
return self.__class__(self._value.__or__(other._value))
class CompositeType(Type):
'''
@@ -107,7 +165,7 @@ class CompositeType(Type):
'''
magic function to update type list
'''
if name[0] != '_' and (isinstance(value, Type) or isinstance(value, tuple)):
if name[0] != '_' and (isinstance(value, Type) or isinstance(value, tuple)) and not self.__dict__.has_key(name):
self._type.append(value)
self.__dict__[name] = value
@@ -142,14 +200,17 @@ class UInt8(SimpleType):
'''
constructor check value range
'''
SimpleType.__init__(self, "B", 1, value)
@staticmethod
def isInRange(value):
SimpleType.__init__(self, "B", 1, False, value)
class SInt8(SimpleType):
'''
unsigned byte
'''
def __init__(self, value = 0):
'''
return true if value is in UInt8 range
constructor check value range
'''
return not (value < 0 or value > 0xff)
SimpleType.__init__(self, "b", 1, True, value)
class UInt16Be(SimpleType):
@@ -160,14 +221,7 @@ class UInt16Be(SimpleType):
'''
constructor check value range
'''
SimpleType.__init__(self, ">H", 2, value)
@staticmethod
def isInRange(value):
'''
return true if value is in UInt8 range
'''
return not (value < 0 or value > 0xffff)
SimpleType.__init__(self, ">H", 2, False, value)
class UInt16Le(SimpleType):
'''
@@ -177,14 +231,7 @@ class UInt16Le(SimpleType):
'''
constructor check value range
'''
SimpleType.__init__(self, "<H", 2, value)
@staticmethod
def isInRange(value):
'''
return true if value is in UInt8 range
'''
return not (value < 0 or value > 0xffff)
SimpleType.__init__(self, "<H", 2, False, value)
class UInt32Be(SimpleType):
'''
@@ -194,14 +241,7 @@ class UInt32Be(SimpleType):
'''
constructor check value range
'''
SimpleType.__init__(self, ">I", 4, value)
@staticmethod
def isInRange(value):
'''
return true if value is in UInt8 range
'''
return not (value < 0 or value > 0xffffffff)
SimpleType.__init__(self, ">I", 4, False, value)
class UInt32Le(SimpleType):
'''
@@ -211,15 +251,8 @@ class UInt32Le(SimpleType):
'''
constructor check value range
'''
SimpleType.__init__(self, "<I", 4, value)
SimpleType.__init__(self, "<I", 4, False, value)
@staticmethod
def isInRange(value):
'''
return true if value is in UInt8 range
'''
return not (value < 0 or value > 0xffffffff)
class SInt32Le(SimpleType):
'''
unsigned int with little endian representation
@@ -228,14 +261,7 @@ class SInt32Le(SimpleType):
'''
constructor check value range
'''
SimpleType.__init__(self, "<I", 4, value)
@staticmethod
def isInRange(value):
'''
return true if value is in UInt8 range
'''
return not (value < ~0x7fffffff or value > 0x7fffffff)
SimpleType.__init__(self, "<I", 4, True, value)
class SInt32Be(SimpleType):
'''
@@ -245,15 +271,8 @@ class SInt32Be(SimpleType):
'''
constructor check value range
'''
SimpleType.__init__(self, ">I", 4, value)
SimpleType.__init__(self, ">I", 4, True, value)
@staticmethod
def isInRange(value):
'''
return true if value is in UInt8 range
'''
return not (value < ~0x7fffffff or value > 0x7fffffff)
class UInt24Be(SimpleType):
'''
unsigned int with big endian representation
@@ -262,14 +281,7 @@ class UInt24Be(SimpleType):
'''
constructor check value range
'''
SimpleType.__init__(self, ">I", 3, value)
@staticmethod
def isInRange(value):
'''
return true if value is in UInt8 range
'''
return not (value < 0 or value > 0xffffff)
SimpleType.__init__(self, ">I", 3, False, value)
def write(self, s):
'''
@@ -291,14 +303,7 @@ class UInt24Le(SimpleType):
'''
constructor check value range
'''
SimpleType.__init__(self, "<I", 3, value)
@staticmethod
def isInRange(value):
'''
return true if value is in UInt8 range
'''
return not (value < 0 or value > 0xffffff)
SimpleType.__init__(self, "<I", 3, False, value)
def write(self, s):
'''

View File

@@ -1,14 +1,15 @@
'''
@author: sylvain
'''
from rdpy.protocol.network.type import UInt16Le
from rdpy.protocol.network.type import UInt8, UInt16Be
from rdpy.utils.const import ConstAttributes
from rdpy.protocol.network.error import InvalidExpectedDataException
@ConstAttributes
class BerPc(object):
BER_PC_MASK = UInt16Le(0x20)
BER_PRIMITIVE = UInt16Le(0x00)
BER_CONSTRUCT = UInt16Le(0x20)
BER_PC_MASK = UInt8(0x20)
BER_PRIMITIVE = UInt8(0x00)
BER_CONSTRUCT = UInt8(0x20)
def berPC(pc):
'''
@@ -19,4 +20,33 @@ def berPC(pc):
return BerPc.BER_CONSTRUCT
else:
return BerPc.BER_PRIMITIVE
def readLength(s):
'''
read length of ber structure
length be on 1 2 or 3 bytes
'''
size = None
byte = UInt8()
s.readType(byte)
if (byte & UInt8(0x80)) == UInt8(0x80):
byte &= ~UInt8(0x80)
if byte == UInt8(1):
size = UInt8()
elif byte == UInt8(2):
size = UInt16Be()
else:
raise InvalidExpectedDataException("ber length may be 1 or 2")
s.readType(size)
else:
size = byte
return size
def writeLength(size):
'''
write length as expected in Ber specification
'''
if size > UInt16Be(0x7f):
return (UInt8(0x82), size)
else:
return UInt8(size.value)

3
rdpy/protocol/rdp/per.py Normal file
View File

@@ -0,0 +1,3 @@
'''
@author: sylvain
'''

View File

@@ -136,12 +136,10 @@ class TPDU(LayerAutomata):
if negResp.len != UInt16Le(0x0008):
raise InvalidExpectedDataException("invalid size of negotiation response")
protocol = negResp.protocol
if protocol != self._protocol:
raise NegotiationFailure("protocol negotiation failure")
self._protocol = negResp.protocol
#_transport is TPKT and transport is TCP layer of twisted
if self._protocol == Protocols.PROTOCOL_SSL:
#_transport is TPKT and transport is TCP layer of twisted
self._transport.transport.startTLS(ClientTLSContext())
else:
raise NegotiationFailure("protocol negociation failure")

View File

@@ -118,6 +118,7 @@ class Rfb(RawLayer):
security handshake for 33 rfb version
server imposed security level
'''
#TODO!!!
self._version = data.read_beuint32()