def Decompress_decompress_beta(self, inp, out): stream = self.stream i_data = cast(inp.getattr(u"data"), Uint8Data, u"inp.data") i_start = cast(inp.getattr(u"start"), Integer, u"inp.start").value i_stop = cast(inp.getattr(u"stop"), Integer, u"inp.stop").value if not (0 <= i_start <= i_stop <= i_data.length): raise unwind(LTypeError(u"inp.start/stop invalid")) o_data = cast(out.getattr(u"data"), Uint8Data, u"out.data") o_start = cast(out.getattr(u"start"), Integer, u"out.start").value o_stop = cast(out.getattr(u"stop"), Integer, u"out.stop").value if not (0 <= o_start <= o_stop <= o_data.length): raise unwind(LTypeError(u"out.start/stop invalid")) stream.c_next_in = rffi.ptradd(i_data.uint8data, i_start) stream.c_avail_in = rffi.r_uint(i_stop - i_start) stream.c_next_out = rffi.ptradd(o_data.uint8data, o_stop) stream.c_avail_out = rffi.r_uint(o_data.length - o_stop) if i_start == i_stop: err = rzlib._inflate(stream, rzlib.Z_FINISH) else: err = rzlib._inflate(stream, 0) if err == rzlib.Z_NEED_DICT: raise unwind(LError(u"Z_NEED_DICT")) if err == rzlib.Z_BUF_ERROR: raise unwind(LError(u"Z_BUF_ERROR")) if err == rzlib.Z_OK or err == rzlib.Z_STREAM_END: inp.setattr(u"start", Integer(i_stop - rffi.r_long(stream.c_avail_in))) out.setattr(u"stop", Integer(rffi.r_long(stream.c_avail_out) + o_stop)) return null
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