DEADSOFTWARE

added license info
[d2df-sdl.git] / src / shared / xstreams.pas
index e7e70e70ef79769651fb825edf1243e1f243ef3d..17a575b8a328489783cc230aa523bdda0bc62359 100644 (file)
@@ -1,3 +1,18 @@
+(* Copyright (C)  DooM 2D:Forever Developers
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *)
 // special stream classes
 {$MODE DELPHI}
 {$R+}
@@ -301,9 +316,10 @@ begin
   fSize := aSize;
   GetMem(fBuffer, ZBufSize);
   fSkipHeader := aSkipHeader;
+  fSrcStPos := fSrcSt.position;
+  FillChar(fZlibSt, sizeof(fZlibSt), 0);
   if fSkipHeader then err := inflateInit2(fZlibSt, -MAX_WBITS) else err := inflateInit(fZlibSt);
   if err <> Z_OK then raise XStreamError.Create(zerror(err));
-  fSrcStPos := fSrcSt.position;
 end;
 
 
@@ -322,6 +338,7 @@ var
   sz: LongInt;
 begin
   result := 0;
+  if (fSize >= 0) and (fPos >= fSize) then exit;
   if count > 0 then
   begin
     fZlibSt.next_out := @buffer;
@@ -360,7 +377,7 @@ begin
     //writeln('  reading ', rd, ' bytes...');
     rr := readBuf(buf, rd);
     //writeln('  got ', rr, ' bytes; fPos=', fPos, '; fSkipToPos=', fSkipToPos);
-    if rd <> rr then raise XStreamError.Create('seek error');
+    if rr <= 0 then raise XStreamError.Create('seek error');
   end;
   //writeln('  pos: fPos=', fPos, '; fSkipToPos=', fSkipToPos);
   fSkipToPos := -1;
@@ -380,12 +397,12 @@ begin
     while true do
     begin
       rd := readBuf(buf, 4096);
-      if rd <> 4096 then break;
+      if rd = 0 then break;
     end;
     fSize := fPos;
     //writeln('  unzstream size is ', fSize);
   finally
-    fSkipToPos := opos;
+    if fSkipToPos < 0 then fSkipToPos := opos;
   end;
 end;
 
@@ -408,9 +425,11 @@ procedure TUnZStream.reset ();
 var
   err: Integer;
 begin
+  //writeln('doing RESET');
   fSrcSt.position := fSrcStPos;
   fPos := 0;
   inflateEnd(fZlibSt);
+  FillChar(fZlibSt, sizeof(fZlibSt), 0);
   if fSkipHeader then err := inflateInit2(fZlibSt, -MAX_WBITS) else err := inflateInit(fZlibSt);
   if err <> Z_OK then raise XStreamError.Create(zerror(err));
 end;