Ejemplo n.º 1
0
 def read(self, filename):
     zinfo = self.getinfo(filename)
     filepos = self.fp.tell()
     self.fp.seek(zinfo.file_offset, 0)
     bytes = self.fp.read(intmask(zinfo.compress_size))
     self.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
Ejemplo n.º 2
0
 def read(self, filename):
     zinfo = self.getinfo(filename)
     filepos = self.fp.tell()
     self.fp.seek(zinfo.file_offset, 0)
     bytes = self.fp.read(intmask(zinfo.compress_size))
     self.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
Ejemplo n.º 3
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
Ejemplo n.º 4
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.º 5
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.º 6
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
Ejemplo n.º 7
0
    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)
Ejemplo n.º 8
0
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 == ''
Ejemplo n.º 9
0
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 == ''
Ejemplo n.º 10
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)
    py.test.raises(rzlib.RZlibError, rzlib.decompress, stream,
                   compressed[2000:-500], rzlib.Z_FINISH)
    rzlib.inflateEnd(stream)
Ejemplo n.º 11
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) == ""

    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:]
Ejemplo n.º 12
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)
    py.test.raises(rzlib.RZlibError,
                   rzlib.decompress, stream, compressed[2000:-500],
                   rzlib.Z_FINISH)
    rzlib.inflateEnd(stream)
Ejemplo n.º 13
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) == ""

    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:]
Ejemplo n.º 14
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.º 15
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.º 16
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.º 17
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.º 18
0
    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)