コード例 #1
0
    def test_boyer_moore(self):
        t = [1, 2, 3, 1, 2, 3, 1, 2, 3]
        p = [1]
        p_bm = BoyerMoore(p)
        occurrences = boyer_moore(p, p_bm, t)
        self.assertListEqual([0, 3, 6], occurrences,
                             "Test Case 1: " + str(p) + " in " + str(t))

        t = [1, 2, 3, 1, 2, 3, 1, 2, 3]
        p = [1, 2]
        p_bm = BoyerMoore(p)
        occurrences = boyer_moore(p, p_bm, t)
        self.assertListEqual([0, 3, 6], occurrences,
                             "Test Case 2: " + str(p) + " in " + str(t))

        t = [1, 2, 3, 1, 2, 3, 1, 2, 3]
        p = [1, 2, 3]
        p_bm = BoyerMoore(p)
        occurrences = boyer_moore(p, p_bm, t)
        self.assertListEqual([0, 3, 6], occurrences,
                             "Test Case 3: " + str(p) + " in " + str(t))

        t = [1, 2, 3, 4, 5]
        p = [6, 6, 6]
        p_bm = BoyerMoore(p)
        occurrences = boyer_moore(p, p_bm, t)
        self.assertListEqual([], occurrences,
                             "test case 4: " + str(p) + " in " + str(t))

        t = [1, 2, 3, 4, 5]
        p = [6]
        p_bm = BoyerMoore(p)
        occurrences = boyer_moore(p, p_bm, t)
        self.assertListEqual([], occurrences,
                             "test case 5: " + str(p) + " in " + str(t))
コード例 #2
0
    def test_boyer_moore(self):
        t = [1, 2, 3, 1, 2, 3, 1, 2, 3]
        p = [1]
        p_bm = BoyerMoore(p)
        occurrences = boyer_moore(p, p_bm, t)
        self.assertListEqual([0, 3, 6], occurrences, "Test Case 1: " + str(p) + " in " + str(t))

        t = [1, 2, 3, 1, 2, 3, 1, 2, 3]
        p = [1, 2]
        p_bm = BoyerMoore(p)
        occurrences = boyer_moore(p, p_bm, t)
        self.assertListEqual([0, 3, 6], occurrences, "Test Case 2: " + str(p) + " in " + str(t))

        t = [1, 2, 3, 1, 2, 3, 1, 2, 3]
        p = [1, 2, 3]
        p_bm = BoyerMoore(p)
        occurrences = boyer_moore(p, p_bm, t)
        self.assertListEqual([0, 3, 6], occurrences, "Test Case 3: " + str(p) + " in " + str(t))

        t = [1, 2, 3, 4, 5]
        p = [6, 6, 6]
        p_bm = BoyerMoore(p)
        occurrences = boyer_moore(p, p_bm, t)
        self.assertListEqual([], occurrences, "test case 4: " + str(p) + " in " + str(t))

        t = [1, 2, 3, 4, 5]
        p = [6]
        p_bm = BoyerMoore(p)
        occurrences = boyer_moore(p, p_bm, t)
        self.assertListEqual([], occurrences, "test case 5: " + str(p) + " in " + str(t))
コード例 #3
0
ファイル: protoSBN1.py プロジェクト: rogierhofboer/SanBot
    def parse(self, buffer, verify_index=True, update_index=True):
        packet_list = []
        original_buffer_length = len(buffer)
        byte_consumed = 0

        # header structure of possible packet
        index_header = 0
        index_header_iter = 0
        index = 0
        payload = None
        operand = None

        found = False

        if len(buffer) < protoSBN1_constants.MIN_PACKET_LENGTH:
            return packet_list, byte_consumed

        p_bm = BoyerMoore(protoSBN1_constants.PROTOSBN1_HEADER_BYTES)
        index_header_occurrences = boyer_moore(
            protoSBN1_constants.PROTOSBN1_HEADER_BYTES, p_bm, buffer)
        index_header_occurrences_num = len(index_header_occurrences)

        while True:
            # Find next header
            if index_header_iter == index_header_occurrences_num:
                # we do not have an header next
                byte_consumed = original_buffer_length
                break
            index_header = index_header_occurrences[index_header_iter]
            index_header_iter += 1

            # We have a header now
            if original_buffer_length - index_header < protoSBN1_constants.MIN_PACKET_LENGTH:
                byte_consumed = index_header
                break

            # Check length
            # expected packet length
            # = len(op+payload) + 4 (SBN1) + 1 (index) + 1 (len) + 1 (checksum)
            expected_packet_length = \
                buffer[
                    index_header + protoSBN1_constants.LENGTH_OFFSET] \
                + protoSBN1_constants.MIN_PACKET_LENGTH
            # Packet has a valid header, check payload length
            if original_buffer_length - index_header < expected_packet_length:
                # The packet is not long enough, keep these bytes
                byte_consumed = index_header
                break
            elif expected_packet_length == protoSBN1_constants.MIN_PACKET_LENGTH:
                # Zero length packet, drop
                byte_consumed = index_header + protoSBN1_constants.MIN_PACKET_LENGTH
                continue

            # Check checksum
            # We are separating operand and payload
            payload = [0] * (
                buffer[index_header + protoSBN1_constants.LENGTH_OFFSET] - 1)
            payload = buffer[index_header +
                             protoSBN1_constants.PAYLOAD_OFFSET:index_header +
                             protoSBN1_constants.PAYLOAD_OFFSET + len(payload)]
            checksum = buffer[index_header +
                              protoSBN1_constants.CHECKSUM_OFFSET]
            operand = ProtoSBN1Operand(
                buffer[index_header + protoSBN1_constants.OPERAND_OFFSET])
            checksum_correct = self.verify_checksum(operand, payload, checksum)
            # If the checksum is not correct, consume the header and move on
            if not checksum_correct:
                byte_consumed = index_header + len(
                    protoSBN1_constants.PROTOSBN1_HEADER_BYTES)
                continue

            # Verify index
            index = buffer[index_header + protoSBN1_constants.INDEX_OFFSET]
            if verify_index:
                index_correct = self.verify_rx_index(index)
                if index_correct:
                    found = True
                else:
                    byte_consumed = index_header + protoSBN1_constants.MIN_PACKET_LENGTH
            elif update_index:
                # Update index accordingly
                self.__rx_index = (index + 1) % 256
                found = True

            # Found a valid packet
            if found:
                packet = ProtoSBN1Packet(index, operand, payload)
                packet_list.append(packet)
                found = False
                byte_consumed = index_header + len(packet.generate_bytes())
        return packet_list, byte_consumed
コード例 #4
0
ファイル: protoSBN1.py プロジェクト: BHFaction/SanBot
    def parse(self, buffer, verify_index=True, update_index=True):
        packet_list = []
        original_buffer_length = len(buffer)
        byte_consumed = 0

        # header structure of possible packet
        index_header = 0
        index_header_iter = 0
        index = 0
        payload = None
        operand = None

        found = False

        if len(buffer) < protoSBN1_constants.MIN_PACKET_LENGTH:
            return packet_list, byte_consumed

        p_bm = BoyerMoore(protoSBN1_constants.PROTOSBN1_HEADER_BYTES)
        index_header_occurrences = boyer_moore(
            protoSBN1_constants.PROTOSBN1_HEADER_BYTES, p_bm,
            buffer)
        index_header_occurrences_num = len(index_header_occurrences)

        while True:
            # Find next header
            if index_header_iter == index_header_occurrences_num:
                # we do not have an header next
                byte_consumed = original_buffer_length
                break
            index_header = index_header_occurrences[index_header_iter]
            index_header_iter += 1

            # We have a header now
            if original_buffer_length - index_header < protoSBN1_constants.MIN_PACKET_LENGTH:
                byte_consumed = index_header
                break

            # Check length
            # expected packet length
            # = len(op+payload) + 4 (SBN1) + 1 (index) + 1 (len) + 1 (checksum)
            expected_packet_length = \
                buffer[
                    index_header + protoSBN1_constants.LENGTH_OFFSET] \
                + protoSBN1_constants.MIN_PACKET_LENGTH
            # Packet has a valid header, check payload length
            if original_buffer_length - index_header < expected_packet_length:
                # The packet is not long enough, keep these bytes
                byte_consumed = index_header
                break
            elif expected_packet_length == protoSBN1_constants.MIN_PACKET_LENGTH:
                # Zero length packet, drop
                byte_consumed = index_header + protoSBN1_constants.MIN_PACKET_LENGTH
                continue

            # Check checksum
            # We are separating operand and payload
            payload = [0] * (buffer[
                                 index_header + protoSBN1_constants.LENGTH_OFFSET] - 1)
            payload = buffer[
                      index_header + protoSBN1_constants.PAYLOAD_OFFSET:index_header + protoSBN1_constants.PAYLOAD_OFFSET + len(
                          payload)]
            checksum = buffer[
                index_header + protoSBN1_constants.CHECKSUM_OFFSET]
            operand = ProtoSBN1Operand(buffer[
                                           index_header + protoSBN1_constants.OPERAND_OFFSET])
            checksum_correct = self.verify_checksum(operand, payload, checksum)
            # If the checksum is not correct, consume the header and move on
            if not checksum_correct:
                byte_consumed = index_header + len(
                    protoSBN1_constants.PROTOSBN1_HEADER_BYTES)
                continue

            # Verify index
            index = buffer[
                index_header + protoSBN1_constants.INDEX_OFFSET]
            if verify_index:
                index_correct = self.verify_rx_index(index)
                if index_correct:
                    found = True
                else:
                    byte_consumed = index_header + protoSBN1_constants.MIN_PACKET_LENGTH
            elif update_index:
                # Update index accordingly
                self.__rx_index = (index + 1) % 256
                found = True

            # Found a valid packet
            if found:
                packet = ProtoSBN1Packet(index, operand, payload)
                packet_list.append(packet)
                found = False
                byte_consumed = index_header + len(packet.generate_bytes())
        return packet_list, byte_consumed