def read(self, filename): zinfo = self.getinfo(filename) fp = self.get_fp() try: filepos = fp.tell() fp.seek(zinfo.file_offset, 0) bytes = fp.read(intmask(zinfo.compress_size)) fp.seek(filepos, 0) if zinfo.compress_type == ZIP_STORED: pass elif zinfo.compress_type == ZIP_DEFLATED and rzlib is not None: stream = rzlib.inflateInit(wbits=-15) try: bytes, _, _ = rzlib.decompress(stream, bytes) # need to feed in unused pad byte so that zlib won't choke ex, _, _ = rzlib.decompress(stream, 'Z') if ex: bytes = bytes + ex finally: rzlib.inflateEnd(stream) elif zinfo.compress_type == ZIP_DEFLATED: raise BadZipfile("Cannot decompress file, zlib not installed") else: raise BadZipfile("Unsupported compression method %d for " "file %s" % (zinfo.compress_type, filename)) crc = crc32(bytes) if crc != zinfo.CRC: raise BadZipfile("Bad CRC-32 for file %s" % filename) return bytes finally: fp.close()
def test_decompress_copy(): """ inflateCopy produces an independent copy of a stream. """ stream = rzlib.inflateInit() bytes1, finished1, unused1 = rzlib.decompress(stream, compressed[:10]) assert bytes1 assert finished1 is False copied = rzlib.inflateCopy(stream) bytes_stream, finished_stream, unused_stream = rzlib.decompress( stream, compressed[10:], rzlib.Z_FINISH, ) assert bytes1 + bytes_stream == expanded assert finished_stream is True assert unused1 == 0 assert unused_stream == 0 rzlib.inflateEnd(stream) bytes_copy, finished_copy, unused_copy = rzlib.decompress( copied, compressed[10:], rzlib.Z_FINISH, ) rzlib.inflateEnd(copied) assert bytes1 + bytes_copy == expanded assert finished_copy is True assert unused_copy == 0
def read(self, filename): zinfo = self.getinfo(filename) fp = self.get_fp() try: filepos = fp.tell() fp.seek(zinfo.file_offset, 0) bytes = fp.read(intmask(zinfo.compress_size)) fp.seek(filepos, 0) if zinfo.compress_type == ZIP_STORED: pass elif zinfo.compress_type == ZIP_DEFLATED and rzlib is not None: stream = rzlib.inflateInit(wbits=-15) try: bytes, _, _ = rzlib.decompress(stream, bytes) # need to feed in unused pad byte so that zlib won't choke ex, _, _ = rzlib.decompress(stream, 'Z') if ex: bytes = bytes + ex finally: rzlib.inflateEnd(stream) elif zinfo.compress_type == ZIP_DEFLATED: raise BadZipfile, \ "Cannot decompress file, zlib not installed" else: raise BadZipfile, \ "Unsupported compression method %d for file %s" % \ (zinfo.compress_type, filename) crc = crc32(bytes) if crc != zinfo.CRC: raise BadZipfile, "Bad CRC-32 for file %s" % filename return bytes finally: fp.close()
def test_decompression(): """ Once we have got a inflate stream, rzlib.decompress() should allow us to decompress bytes. """ stream = rzlib.inflateInit() bytes1, finished1, unused1 = rzlib.decompress(stream, compressed) bytes2, finished2, unused2 = rzlib.decompress(stream, "", rzlib.Z_FINISH) rzlib.inflateEnd(stream) assert bytes1 + bytes2 == expanded assert finished1 is True assert finished2 is True assert unused1 == 0 assert unused2 == 0
def test_decompress_max_length(): """ Test the max_length argument of decompress(). """ stream = rzlib.inflateInit() data1, finished1, unused1 = rzlib.decompress(stream, compressed, max_length = 17) assert data1 == expanded[:17] assert finished1 is False assert unused1 > 0 data2, finished2, unused2 = rzlib.decompress(stream, compressed[-unused1:]) assert data2 == expanded[17:] assert finished2 is True assert unused2 == 0 rzlib.deflateEnd(stream)
def decompress(self, space, data, max_length=0): """ decompress(data[, max_length]) -- Return a string containing the decompressed version of the data. If the max_length parameter is specified then the return value will be no longer than max_length. Unconsumed input data will be stored in the unconsumed_tail attribute. """ if max_length == 0: max_length = sys.maxint elif max_length < 0: raise oefmt(space.w_ValueError, "max_length must be greater than zero") try: self.lock() try: result = rzlib.decompress(self.stream, data, max_length=max_length) finally: self.unlock() except rzlib.RZlibError as e: raise zlib_error(space, e.msg) string, finished, unused_len = result self._save_unconsumed_input(data, finished, unused_len) return space.wrap(string)
def decompress(self, space, data, max_length=0): """ decompress(data[, max_length]) -- Return a string containing the decompressed version of the data. If the max_length parameter is specified then the return value will be no longer than max_length. Unconsumed input data will be stored in the unconsumed_tail attribute. """ if max_length == 0: max_length = sys.maxint elif max_length < 0: raise oefmt(space.w_ValueError, "max_length must be greater than zero") try: self.lock() try: result = rzlib.decompress(self.stream, data, max_length=max_length) finally: self.unlock() except rzlib.RZlibError as e: raise zlib_error(space, e.msg) string, finished, unused_len = result self._save_unconsumed_input(data, finished, unused_len) return space.newbytes(string)
def flush(self, space, w_length=None): """ flush( [length] ) -- This is kept for backward compatibility, because each call to decompress() immediately returns as much data as possible. """ if w_length is not None: length = space.int_w(w_length) if length <= 0: raise oefmt(space.w_ValueError, "length must be greater than zero") data = self.unconsumed_tail try: self.lock() try: result = rzlib.decompress(self.stream, data, rzlib.Z_FINISH) finally: self.unlock() except rzlib.RZlibError: string = "" else: string, finished, unused_len = result self._save_unconsumed_input(data, finished, unused_len) if finished: rzlib.inflateEnd(self.stream) self.stream = rzlib.null_stream return space.newbytes(string)
def test_decompression_too_much_input(): """ Check the case where we feed extra data to decompress(). """ stream = rzlib.inflateInit() data1, finished1, unused1 = rzlib.decompress(stream, compressed[:-5]) assert finished1 is False assert unused1 == 0 data2, finished2, unused2 = rzlib.decompress(stream, compressed[-5:] + 'garbage') assert finished2 is True assert unused2 == len('garbage') assert data1 + data2 == expanded data3, finished3, unused3 = rzlib.decompress(stream, 'more_garbage') assert finished3 is True assert unused3 == len('more_garbage') assert data3 == '' rzlib.deflateEnd(stream)
def Decompress_finish(self): try: # lock result = rzlib.decompress(self.stream, '', rzlib.Z_FINISH) # finally unlock except rzlib.RZlibError as e: raise zlib_error(e.msg) string, finished, unused_len = result assert unused_len == 0 return to_uint8array(string)
def Decompress_decompress(self, array): data = array.to_str() try: # lock result = rzlib.decompress(self.stream, data) # finally unlock except rzlib.RZlibError as e: raise zlib_error(e.msg) string, finished, unused_len = result assert unused_len == 0 return to_uint8array(string)
def test_decompression_truncated_input(): """ Test that we can accept incomplete input when inflating, but also detect this situation when using Z_FINISH. """ expanded = repr(range(20000)) compressed = zlib.compress(expanded) print len(compressed), '=>', len(expanded) stream = rzlib.inflateInit() data, finished1, unused1 = rzlib.decompress(stream, compressed[:1000]) assert expanded.startswith(data) assert finished1 is False assert unused1 == 0 data2, finished2, unused2 = rzlib.decompress(stream, compressed[1000:2000]) data += data2 assert finished2 is False assert unused2 == 0 assert expanded.startswith(data) exc = py.test.raises(rzlib.RZlibError, rzlib.decompress, stream, compressed[2000:-500], rzlib.Z_FINISH) msg = "Error -5 while decompressing data: incomplete or truncated stream" assert str(exc.value) == msg rzlib.inflateEnd(stream)
def test_decompression_lots_of_data(): """ Test compression of more data that fits in a single internal output buffer. """ expanded = repr(range(20000)) compressed = zlib.compress(expanded) print len(compressed), '=>', len(expanded) stream = rzlib.inflateInit() bytes, finished, unused = rzlib.decompress(stream, compressed, rzlib.Z_FINISH) rzlib.inflateEnd(stream) assert bytes == expanded assert finished is True assert unused == 0
def test_cornercases(): """ Test degenerate arguments. """ stream = rzlib.deflateInit() bytes = rzlib.compress(stream, "") bytes += rzlib.compress(stream, "") bytes += rzlib.compress(stream, "", rzlib.Z_FINISH) assert zlib.decompress(bytes) == "" rzlib.deflateEnd(stream) stream = rzlib.inflateInit() data, finished, unused = rzlib.decompress(stream, "") assert data == "" assert finished is False assert unused == 0 buf = compressed for i in range(10): data, finished, unused = rzlib.decompress(stream, buf, max_length=0) assert data == "" assert finished is False assert unused > 0 buf = buf[-unused:] rzlib.deflateEnd(stream)
def test_decompression_truncated_input(): """ Test that we can accept incomplete input when inflating, but also detect this situation when using Z_FINISH. """ expanded = repr(range(20000)) compressed = zlib.compress(expanded) print len(compressed), '=>', len(expanded) stream = rzlib.inflateInit() data, finished1, unused1 = rzlib.decompress(stream, compressed[:1000]) assert expanded.startswith(data) assert finished1 is False assert unused1 == 0 data2, finished2, unused2 = rzlib.decompress(stream, compressed[1000:2000]) data += data2 assert finished2 is False assert unused2 == 0 assert expanded.startswith(data) exc = py.test.raises( rzlib.RZlibError, rzlib.decompress, stream, compressed[2000:-500], rzlib.Z_FINISH) msg = "Error -5 while decompressing data: incomplete or truncated stream" assert str(exc.value) == msg rzlib.inflateEnd(stream)
def decompress(space, string, wbits=rzlib.MAX_WBITS, bufsize=0): """ decompress(string[, wbits[, bufsize]]) -- Return decompressed string. Optional arg wbits is the window buffer size. Optional arg bufsize is only for compatibility with CPython and is ignored. """ try: try: stream = rzlib.inflateInit(wbits) except ValueError: raise zlib_error(space, "Bad window buffer size") try: result, _, _ = rzlib.decompress(stream, string, rzlib.Z_FINISH) finally: rzlib.inflateEnd(stream) except rzlib.RZlibError, e: raise zlib_error(space, e.msg)
def flush(self, space, w_length=None): """ flush( [length] ) -- This is kept for backward compatibility, because each call to decompress() immediately returns as much data as possible. """ if w_length is not None: length = space.c_int_w(w_length) if length <= 0: raise oefmt(space.w_ValueError, "length must be greater than zero") data = self.unconsumed_tail try: self.lock() try: result = rzlib.decompress(self.stream, data, rzlib.Z_FINISH) finally: self.unlock() except rzlib.RZlibError: string = "" else: string, finished, unused_len = result self._save_unconsumed_input(data, finished, unused_len) return space.wrap(string)
def decompress(self, data, max_length=0): """ decompress(data[, max_length]) -- Return a string containing the decompressed version of the data. If the max_length parameter is specified then the return value will be no longer than max_length. Unconsumed input data will be stored in the unconsumed_tail attribute. """ if max_length == 0: max_length = sys.maxint elif max_length < 0: raise OperationError(self.space.w_ValueError, self.space.wrap("max_length must be " "greater than zero")) try: self.lock() try: result = rzlib.decompress(self.stream, data, max_length = max_length) finally: self.unlock() except rzlib.RZlibError, e: raise zlib_error(self.space, e.msg)
def decompress(self,s): stream = rzlib.inflateInit() bytes, finished, unused = rzlib.decompress(stream, s) return bytes
def _decode(data, encoding): stream = rzlib.inflateInit(wbits=encoding) bytes, finished, unused = rzlib.decompress(stream, data, rzlib.Z_FINISH) rzlib.inflateEnd(stream) return bytes