Beispiel #1
0
    def __compress(self, compression: str, data: bytes) -> bytes:
        """
        Given data and an optional compression scheme, compress the data.

        Parameters:
            compression - A string specifying the compression used. Should
                          be of the form 'l7zz' or 'none'. The python value
                          None will also be recognized as 'none'.
            data - Binary string representing data to transform.

        Returns:
            binary string representing transformed data
        """
        if data is None:
            return None

        if compression:
            if compression is None or compression == 'none':
                # This isn't compressed
                return data
            elif compression == 'lz77':
                # This is a compressed new-style packet
                lz = Lz77()
                return lz.compress(data)
            else:
                raise EAmuseException(f'Unknown compression {compression}')

        # No compression
        return data
Beispiel #2
0
    def test_huge_data_random(self) -> None:
        lz77 = Lz77()
        data = os.urandom(1 * 1024 * 1024)

        compresseddata = lz77.compress(data)
        self.assertNotEqual(data, compresseddata)

        decompresseddata = lz77.decompress(compresseddata)
        self.assertEqual(data, decompresseddata)
Beispiel #3
0
    def test_large_data_random(self) -> None:
        lz77 = Lz77()
        data = bytes([random.randint(0, 255) for _ in range(100 * 1024)])

        compresseddata = lz77.compress(data)
        self.assertNotEqual(data, compresseddata)

        decompresseddata = lz77.decompress(compresseddata)
        self.assertEqual(data, decompresseddata)
Beispiel #4
0
    def read_file(self, filename: str) -> bytes:
        (fileoffset, uncompressedsize, compressedsize) = self.__files[filename]

        if compressedsize == uncompressedsize:
            # Just stored
            return self.__data[fileoffset:(fileoffset + compressedsize)]
        else:
            # Compressed
            lz77 = Lz77()
            return lz77.decompress(self.__data[fileoffset:(fileoffset + compressedsize)])
Beispiel #5
0
    def test_texture(self) -> None:
        lz77 = Lz77()
        data = get_fixture("rawdata")

        compresseddata = lz77.compress(data)
        self.assertNotEqual(data, compresseddata)
        self.assertTrue(len(compresseddata) < len(data))

        decompresseddata = lz77.decompress(compresseddata)
        self.assertEqual(data, decompresseddata)
Beispiel #6
0
    def test_lorem_ipsum(self) -> None:
        lz77 = Lz77()
        data = get_fixture("lorem.txt")

        compresseddata = lz77.compress(data)
        self.assertNotEqual(data, compresseddata)
        self.assertTrue(len(compresseddata) < len(data))

        decompresseddata = lz77.decompress(compresseddata)
        self.assertEqual(data, decompresseddata)
Beispiel #7
0
    def test_declaration(self) -> None:
        lz77 = Lz77()
        data = get_fixture("declaration.txt")

        compresseddata = lz77.compress(data)
        self.assertNotEqual(data, compresseddata)
        self.assertTrue(len(compresseddata) < len(data))

        decompresseddata = lz77.decompress(compresseddata)
        self.assertEqual(data, decompresseddata)
Beispiel #8
0
    def test_known_compression(self) -> None:
        """
        Specifically tests for ability to compress an overlap,
        verifies that we don't regress on known compressions.
        """
        lz77 = Lz77()
        data = b"abcabcabcabc"
        compresseddata = lz77.compress(data)
        self.assertEqual(b"\x07abc\x006\x00\x00", compresseddata)

        decompresseddata = lz77.decompress(compresseddata)
        self.assertEqual(data, decompresseddata)
Beispiel #9
0
    def read_file(self, filename: str) -> bytes:
        # If this is a texture folder, first we need to grab the texturelist.xml file
        # to figure out if this is compressed or not.
        decompress = False
        texlist = self.__get_texlist_for_file(filename)
        if texlist is not None and texlist.name == 'texturelist':
            if texlist.attribute('compress') == 'avslz':
                # We should decompress!
                decompress = True

        filedata = self.__files[filename]
        if decompress:
            uncompressed_size, compressed_size = struct.unpack(
                '>II', filedata[0:8])
            if len(filedata) == compressed_size + 8:
                lz77 = Lz77()
                filedata = lz77.decompress(filedata[8:])
            else:
                raise Exception('Unrecognized compression!')

        if self.__decode_binxml and os.path.splitext(filename)[1] == '.xml':
            benc = BinaryEncoding()
            filexml = benc.decode(filedata)
            if filexml is not None:
                filedata = str(filexml).encode('utf-8')

        if self.__decode_textures and filename in self.__mappings and filename in self.__sizes:
            fmt = self.__mappings.get(filename)
            wh = self.__sizes.get(filename)
            if fmt == "argb8888rev":
                if len(filedata) < (wh[0] * wh[1] * 4):
                    left = (wh[0] * wh[1] * 4) - len(filedata)
                    filedata = filedata + b'\x00' * left
                img = Image.frombytes('RGBA', wh, filedata, 'raw', 'BGRA')
                b = io.BytesIO()
                img.save(b, format='PNG')
                filedata = b.getvalue()

        return filedata