Ejemplo n.º 1
0
def test_deflate_set_dictionary():
    text = 'abcabc'
    zdict = 'abc'
    stream = rzlib.deflateInit()
    rzlib.deflateSetDictionary(stream, zdict)
    bytes = rzlib.compress(stream, text, rzlib.Z_FINISH)
    rzlib.deflateEnd(stream)

    stream2 = rzlib.inflateInit()

    from rpython.rtyper.lltypesystem import lltype, rffi, rstr
    from rpython.rtyper.annlowlevel import llstr
    from rpython.rlib.rstring import StringBuilder
    with lltype.scoped_alloc(rffi.CCHARP.TO, len(bytes)) as inbuf:
        rstr.copy_string_to_raw(llstr(bytes), inbuf, 0, len(bytes))
        stream2.c_next_in = rffi.cast(rzlib.Bytefp, inbuf)
        rffi.setintfield(stream2, 'c_avail_in', len(bytes))
        with lltype.scoped_alloc(rffi.CCHARP.TO, 100) as outbuf:
            stream2.c_next_out = rffi.cast(rzlib.Bytefp, outbuf)
            bufsize = 100
            rffi.setintfield(stream2, 'c_avail_out', bufsize)
            err = rzlib._inflate(stream2, rzlib.Z_SYNC_FLUSH)
            assert err == rzlib.Z_NEED_DICT
            rzlib.inflateSetDictionary(stream2, zdict)
            rzlib._inflate(stream2, rzlib.Z_SYNC_FLUSH)
            avail_out = rffi.cast(lltype.Signed, stream2.c_avail_out)
            result = StringBuilder()
            result.append_charpsize(outbuf, bufsize - avail_out)

    rzlib.inflateEnd(stream2)
    assert result.build() == text
Ejemplo n.º 2
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()
Ejemplo n.º 3
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()
Ejemplo n.º 4
0
def test_deflate_set_dictionary():
    text = 'abcabc'
    zdict = 'abc'
    stream = rzlib.deflateInit()
    rzlib.deflateSetDictionary(stream, zdict)
    bytes = rzlib.compress(stream, text, rzlib.Z_FINISH)
    rzlib.deflateEnd(stream)
    
    stream2 = rzlib.inflateInit()

    from rpython.rtyper.lltypesystem import lltype, rffi, rstr
    from rpython.rtyper.annlowlevel import llstr
    from rpython.rlib.rstring import StringBuilder
    with lltype.scoped_alloc(rffi.CCHARP.TO, len(bytes)) as inbuf:
        rstr.copy_string_to_raw(llstr(bytes), inbuf, 0, len(bytes))
        stream2.c_next_in = rffi.cast(rzlib.Bytefp, inbuf)
        rffi.setintfield(stream2, 'c_avail_in', len(bytes))
        with lltype.scoped_alloc(rffi.CCHARP.TO, 100) as outbuf:
            stream2.c_next_out = rffi.cast(rzlib.Bytefp, outbuf)
            bufsize = 100
            rffi.setintfield(stream2, 'c_avail_out', bufsize)
            err = rzlib._inflate(stream2, rzlib.Z_SYNC_FLUSH)
            assert err == rzlib.Z_NEED_DICT
            rzlib.inflateSetDictionary(stream2, zdict)
            rzlib._inflate(stream2, rzlib.Z_SYNC_FLUSH)
            avail_out = rffi.cast(lltype.Signed, stream2.c_avail_out)
            result = StringBuilder()
            result.append_charpsize(outbuf, bufsize - avail_out)

    rzlib.inflateEnd(stream2)
    assert result.build() == text
Ejemplo n.º 5
0
 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)
Ejemplo n.º 6
0
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
Ejemplo n.º 7
0
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
Ejemplo n.º 8
0
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
Ejemplo n.º 9
0
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
Ejemplo n.º 10
0
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
Ejemplo n.º 11
0
def test_unsuccessful_decompress_copy():
    """
    Errors during unsuccesful inflateCopy operations raise RZlibErrors.
    """
    stream = rzlib.inflateInit()

    # From zlib.h:
    #
    # "inflateCopy returns [...] Z_STREAM_ERROR if the source stream
    #  state was inconsistent (such as zalloc being Z_NULL)"
    from rpython.rtyper.lltypesystem import rffi, lltype
    stream.c_zalloc = rffi.cast(lltype.typeOf(stream.c_zalloc), rzlib.Z_NULL)

    exc = py.test.raises(rzlib.RZlibError, rzlib.inflateCopy, stream)
    msg = "Error -2 while copying decompression object: inconsistent stream state"
    assert str(exc.value) == msg
    rzlib.inflateEnd(stream)
Ejemplo n.º 12
0
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)
Ejemplo n.º 13
0
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)
Ejemplo n.º 14
0
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)
Ejemplo n.º 15
0
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)
Ejemplo n.º 16
0
def test_inflate_init_end():
    """
    inflateInit() followed by inflateEnd() should work and do nothing.
    """
    stream = rzlib.inflateInit()
    rzlib.inflateEnd(stream)
Ejemplo n.º 17
0
 def __del__(self):
     """Automatically free the resources used by the stream."""
     if self.stream:
         rzlib.inflateEnd(self.stream)
         self.stream = rzlib.null_stream
Ejemplo n.º 18
0
def _decode(data, encoding):
    stream = rzlib.inflateInit(wbits=encoding)
    bytes, finished, unused = rzlib.decompress(stream, data,
                                               rzlib.Z_FINISH)
    rzlib.inflateEnd(stream)
    return bytes
Ejemplo n.º 19
0
 def _finalize_(self):
     """Automatically free the resources used by the stream."""
     if self.stream:
         rzlib.inflateEnd(self.stream)
         self.stream = rzlib.null_stream
Ejemplo n.º 20
0
Archivo: zlib.py Proyecto: gordol/lever
 def __del__(self):
     if self.stream:
         rzlib.inflateEnd(self.stream)
         self.stream = rzlib.null_stream
Ejemplo n.º 21
0
def test_inflate_init_end():
    """
    inflateInit() followed by inflateEnd() should work and do nothing.
    """
    stream = rzlib.inflateInit()
    rzlib.inflateEnd(stream)