Exemple #1
0
    def __init__(self, root, filename, compress):
        rel_name = os.path.relpath(filename, root)
        rel_name = rel_name.replace('\\', '/')

        size = os.stat(filename)[stat.ST_SIZE]
        f = open(filename, 'rb')
        self.filename = '/' + rel_name
        if compress == True:
            tmp_buf = f.read()
            max_compressed_size = dlib.dmLZ4MaxCompressedSize(size)
            self.resource = dlib.dmLZ4CompressBuffer(tmp_buf, size,
                                                     max_compressed_size)
            self.compressed_size = len(self.resource)
            # Store uncompressed if gain is less than 5%
            # We believe that the shorter load time will compensate in this case.
            comp_ratio = sys.float_info.max
            if size == 0:
                print "Warning! Size of %s is 0" % (self.filename)
            else:
                comp_ratio = float(self.compressed_size) / float(size)
            if comp_ratio > 0.95:
                self.resource = tmp_buf
                self.compressed_size = 0xFFFFFFFFL
        else:
            self.resource = f.read()
            self.compressed_size = 0xFFFFFFFFL

        if os.path.splitext(filename)[-1] in ENCRYPTED_EXTS:
            self.flags = 1
            self.resource = dlib.dmEncryptXTeaCTR(self.resource, KEY)
        else:
            self.flags = 0
        self.size = size
        f.close()
Exemple #2
0
    def __init__(self, root, filename, compress, hashpostfix):
        rel_name = os.path.relpath(filename, root)
        rel_name = rel_name.replace('\\', '/')

        size = os.stat(filename)[stat.ST_SIZE]
        f = open(filename, 'rb')
        self.path = '/' + rel_name
        self.path_size = len(self.path)
        the_hash_str = ('awesome hash here' +
                        str(hashpostfix)).ljust(HASH_MAX_LENGTH)
        the_hash_bytes = bytearray(the_hash_str)  #b'awesomehash'
        self.hash = the_hash_bytes
        self.hash_size = len(the_hash_bytes)
        if compress == True:
            tmp_buf = f.read()
            max_compressed_size = dlib.dmLZ4MaxCompressedSize(size)
            self.resource = dlib.dmLZ4CompressBuffer(tmp_buf, size,
                                                     max_compressed_size)
            self.compressed_size = len(self.resource)
            # Store uncompressed if gain is less than 5%
            # We believe that the shorter load time will compensate in this case.
            comp_ratio = sys.float_info.max
            if size == 0:
                print "Warning! Size of %s is 0" % (self.path)
            else:
                comp_ratio = float(self.compressed_size) / float(size)
            if comp_ratio > 0.95:
                self.resource = tmp_buf
                self.compressed_size = 0xFFFFFFFFL
        else:
            self.resource = f.read()
            self.compressed_size = 0xFFFFFFFFL

        if os.path.splitext(filename)[-1] in ENCRYPTED_EXTS:
            self.flags = 1
            self.resource = dlib.dmEncryptXTeaCTR(self.resource, KEY)
        else:
            self.flags = 0
        self.size = size
        f.close()
Exemple #3
0
    def testLZ4(self):
        #
        # Test to compress/decompress a short text...
        #
        uncompressed = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas ornare non massa a imperdiet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus lorem diam, ultricies et dapibus vel, interdum consequat metus. Cras suscipit bibendum dignissim. Maecenas non lectus enim. Integer rhoncus fringilla felis in dictum. Nunc justo risus, volutpat id elementum molestie, eleifend sit amet tellus. Mauris accumsan ornare justo, sed cursus justo bibendum et. Vestibulum massa metus, rutrum id libero a, ultrices varius arcu. Sed sed condimentum enim. Mauris ac sodales lacus, at lacinia est. Nunc sed libero ac neque cursus venenatis. Phasellus laoreet est quis massa cursus."
        uncompressed_len = len(uncompressed)
        max_compressed_size = dlib.dmLZ4MaxCompressedSize(uncompressed_len)
        self.assertGreater(max_compressed_size, 0)

        compressed = dlib.dmLZ4CompressBuffer(uncompressed, uncompressed_len,
                                              max_compressed_size)
        decompressed = dlib.dmLZ4DecompressBuffer(compressed, uncompressed_len)
        self.assertEqual(decompressed, uncompressed)

        #
        # Test to decompress lz4 encoded file
        #
        foo_file_size = path.getsize("data/foo.lz4")
        foo_f = open("data/foo.lz4", "rb")
        foo_compressed = foo_f.read(foo_file_size)
        foo_f.close()
        foo_decompressed = dlib.dmLZ4DecompressBuffer(foo_compressed, 3)
        self.assertEqual(foo_decompressed, "foo")