Beispiel #1
0
    def decode(self, data):
        #print('index= ' + str(self.index))
        length = util.getInt(data[self.index:self.index+4]) & 0xffffffff
        self.index += 4
        doff = util.getByte(data,self.index) & 0xff
        self.index += 1
        type = util.getByte(data,self.index) & 0xff
        self.index += 1
        channel = util.getShort(data[self.index:self.index+2]) & 0xffff
        self.index += 2

        if length == 8 and doff == 2 and (type == 0 or type == 1) and channel == 0:
            if self.index >= len(data):
                self.index = 0
                return AMQPPing()
            else:
                raise ValueError("Received malformed ping-header with invalid length")

        if length == 1095586128 and (doff == 3 or doff == 0) and type == 1 and channel == 0:
            if self.index >= len(data):
                self.index = 0
                return AMQPProtoHeader(doff)
            else:
                raise ValueError("Received malformed protocol-header with invalid length")

        header = None
        headerFactory = HeaderFactory(self.index)
        if type == 0:
            header = headerFactory.getAMQP(data)
        elif type == 1:
            header = headerFactory.getSASL(data)
        else:
            raise ValueError("Received malformed header with invalid type: " + type)

        self.index = headerFactory.getIndex()

        if isinstance(header, AMQPHeader):
            header.setDoff(doff)
            header.setType(type)
            header.setChannel(channel)

        if header.getCode() == HeaderCode.TRANSFER:
            if isinstance(header,AMQPTransfer):
                header.setSections({})
            while self.index < len(data):
                headerFactory.setIndex(self.index)
                section = headerFactory.getSection(data)
                header.sections[section.getCode()] = section
                self.index = headerFactory.getIndex()

        self.index = 0
        return header
Beispiel #2
0
 def getConstructor(self, buf):
     code = None
     constructor = None
     codeByte = util.getByte(buf, self.index)
     self.index += 1
     if codeByte == 0:
         descriptor = self.getTlv(buf)
         code = AMQPType(util.getByte(buf, self.index) & 0xff)
         self.index += 1
         constructor = DescribedConstructor(code, descriptor)
     else:
         code = AMQPType(codeByte & 0xff)
         constructor = SimpleConstructor(code)
     return constructor
 def initValue(self, value):
     arr = bytearray()
     arr = util.addInt(arr, value)
     mf = bytearray(1)
     mf += arr[0:3]
     self.messageFormat = util.getInt(mf)
     self.version = util.getByte(arr[3:4], 0) & 0xff
Beispiel #4
0
    def next(self, data, index):
        length = util.getInt(data[self.index:self.index + 4]) & 0xffffffff
        #print('Parser.next length= ' + str(length) + ' index= ' + str(index))
        self.index = index + 4
        if length == 1095586128:
            protocolId = util.getByte(data, self.index)
            self.index += 1
            versionMajor = util.getByte(data, self.index)
            self.index += 1
            versionMinor = util.getByte(data, self.index)
            self.index += 1
            versionRevision = util.getByte(data, self.index)
            self.index += 1
            if (protocolId == 0 or protocolId == 3) and versionMajor == 1 and versionMinor == 0 and versionRevision == 0:
                self.index = 0
                return data[index: 8]

        self.index = 0
        return data[index:length]
Beispiel #5
0
    def getElement(self, constructor, buf):
        tlv = None
        code = constructor.getCode()
        if isinstance(code, AMQPType):
            if code == AMQPType.NULL:
                tlv = TLVNull()
            elif code in (AMQPType.BOOLEAN_TRUE, AMQPType.BOOLEAN_FALSE,
                          AMQPType.UINT_0, AMQPType.ULONG_0):
                tlv = TLVFixed(code, bytearray())
            elif code in (AMQPType.BOOLEAN, AMQPType.UBYTE, AMQPType.BYTE,
                          AMQPType.SMALL_UINT, AMQPType.SMALL_INT,
                          AMQPType.SMALL_ULONG, AMQPType.SMALL_LONG):
                value1 = util.getByte(buf, self.index)
                self.index += 1
                tlv = TLVFixed(code, value1)
            elif code in (AMQPType.SHORT, AMQPType.USHORT):
                value2 = buf[self.index:self.index + 2]
                self.index += 2
                tlv = TLVFixed(code, value2)
            elif code in (AMQPType.UINT, AMQPType.INT, AMQPType.FLOAT,
                          AMQPType.DECIMAL_32, AMQPType.CHAR):
                value4 = buf[self.index:self.index + 4]
                self.index += 4
                tlv = TLVFixed(code, value4)
            elif code in (AMQPType.ULONG, AMQPType.LONG, AMQPType.DECIMAL_64,
                          AMQPType.DOUBLE, AMQPType.TIMESTAMP):
                value8 = buf[self.index:self.index + 8]
                self.index += 8
                tlv = TLVFixed(code, value8)
            elif code in (AMQPType.DECIMAL_128, AMQPType.UUID):
                value16 = buf[self.index:self.index + 16]
                self.index += 16
                tlv = TLVFixed(code, value16)
            elif code in (AMQPType.STRING_8, AMQPType.SYMBOL_8,
                          AMQPType.BINARY_8):
                varlen = util.getByte(buf, self.index) & 0xff
                self.index += 1
                varValue8 = buf[self.index:self.index + int(varlen)]
                self.index += int(varlen)
                tlv = TLVVariable(code, varValue8)
            elif code in (AMQPType.STRING_32, AMQPType.SYMBOL_32,
                          AMQPType.BINARY_32):
                var32len = util.getInt(buf[self.index:self.index + 4])
                self.index += 4
                varValue32 = buf[self.index:self.index + int(var32len)]
                self.index += int(var32len)
                tlv = TLVVariable(code, varValue32)
            elif code is AMQPType.LIST_0:
                tlv = TLVList(None, None)
            elif code is AMQPType.LIST_8:
                list8size = util.getByte(buf, self.index) & 0xff
                self.index += 1
                list8count = util.getByte(buf, self.index) & 0xff
                self.index += 1
                list8values = []
                for i in range(0, list8count):
                    entity = self.getTlv(buf)
                    list8values.append(entity)
                tlv = TLVList(code, list8values)
            elif code is AMQPType.LIST_32:
                list32size = util.getInt(buf[self.index:self.index + 4])
                self.index += 4
                list32count = util.getInt(buf[self.index:self.index + 4])
                self.index += 4
                list32values = []
                for i in range(0, list32count):
                    list32values.append(self.getTlv(buf))
                tlv = TLVList(code, list32values)
            elif code is AMQPType.MAP_8:
                map8size = util.getByte(buf, self.index) & 0xff
                self.index += 1
                map8count = util.getByte(buf, self.index) & 0xff
                self.index += 1
                stop8 = self.index + map8size - 1
                map8 = TLVMap(None, None)
                while self.index < stop8:
                    map8.putElement(self.getTlv(buf), self.getTlv(buf))
                tlv = TLVMap(code, map8)
            elif code is AMQPType.MAP_32:
                map32size = util.getInt(buf[self.index:self.index + 4])
                self.index += 4
                map32count = util.getInt(buf[self.index:self.index + 4])
                self.index += 4
                stop32 = self.index + map32size - 4
                map32 = TLVMap(None, None)
                while self.index < stop32:
                    map32.putElement(self.getTlv(buf), self.getTlv(buf))
                tlv = TLVMap(code, map32)
            elif code is AMQPType.ARRAY_8:
                array8size = util.getByte(buf, self.index) & 0xff
                self.index += 1
                array8count = util.getByte(buf, self.index) & 0xff
                self.index += 1
                arr8 = []
                arr8constructor = self.getConstructor(buf)
                for i in range(0, array8count):
                    arr8.append(self.getElement(arr8constructor, buf))
                tlv = TLVArray(code, arr8)
            elif code is AMQPType.ARRAY_32:
                arr32size = util.getInt(buf[self.index:self.index + 4])
                self.index += 4
                arr32count = util.getInt(buf[self.index:self.index + 4])
                self.index += 4
                arr32 = []
                arr32constructor = self.getConstructor(buf)
                for i in range(0, arr32count):
                    arr32.append(self.getElement(arr32constructor, buf))
                tlv = TLVArray(code, arr32)

            if isinstance(constructor, DescribedConstructor):
                tlv.setConstructor(constructor)

            return tlv
 def getByte(self):
     return util.getByte(self.value, 0)