2015-12-18

ZDecompressStream 進入負數無窮回圈

//  ZLib.pas (zlib 1.2.8)                                                     *
unit System.ZLib;

procedure ZDecompressStream(inStream, outStream: TStream);

呼叫 ZDecompressStream 萬一/一萬 inStream 丟進去的 Size = 0,內部最後一段的 repeat 會很努力的幫你算到負數回圈下去。

因該會一值算下去!

小心點,有地雷!

  try
    inSize := inStream.Read(inBuffer, bufferSize);
    while inSize > 0 do // 噹噹噹
    begin
      zstream.next_in := @inBuffer[0];
      zstream.avail_in := inSize;

      repeat
        zstream.next_out := @outBuffer[0];
        zstream.avail_out := bufferSize;

        ZDecompressCheckWithoutBufferError(inflate(zstream, Z_NO_FLUSH));

        outSize := bufferSize - zstream.avail_out;

        outStream.Write(outBuffer, outSize);
      until (zstream.avail_in = 0) and (zstream.avail_out > 0);

      inSize := inStream.Read(inBuffer, bufferSize);
    end;

    repeat
      zstream.next_out := @outBuffer[0];
      zstream.avail_out := bufferSize;

      zresult := ZDecompressCheckWithoutBufferError(inflate(zstream, Z_FINISH));

      outSize := bufferSize - zstream.avail_out;

      outStream.Write(outBuffer, outSize);
    until (zresult = Z_STREAM_END) and (zstream.avail_out > 0); // 叮叮叮
  finally
    ZDecompressCheck(inflateEnd(zstream));
  end;
[ZDecompressStream]

沒有留言: