DEADSOFTWARE

wadcvt: don't compress uncompressible data
[d2df-sdl.git] / src / sfs / wadcvt.dpr
index 47a9ffb0ea381940f74e9d5037c073f278579f56..4885b327e10e3a45ba4e22ee0d72228b2a68319a 100644 (file)
@@ -24,93 +24,87 @@ end;
 
 
 // returs crc
-function zpack (ds: TStream; ss: TStream): LongWord;
+function zpack (ds: TStream; ss: TStream; var aborted: Boolean): LongWord;
 const
   IBSize = 65536;
   OBSize = 65536;
 var
   zst: TZStream;
   ib, ob: PByte;
-  ibpos: Cardinal;
   err: Integer;
-  rd, f: Integer;
+  rd: Integer;
   eof: Boolean;
   crc: LongWord;
+  dstp, srcsize: Int64;
 begin
   result := 0;
+  //aborted := true; exit;
+  aborted := false;
   crc := crc32(0, nil, 0);
   GetMem(ib, IBSize);
   GetMem(ob, OBSize);
+  ss.position := 0;
+  dstp := ds.position;
+  srcsize := ss.size;
   try
     zst.next_out := ob;
     zst.avail_out := OBSize;
+    zst.next_in := ib;
+    zst.avail_in := 0;
     err := deflateInit2(zst, Z_BEST_COMPRESSION, Z_DEFLATED, -15, 9, 0);
     if err <> Z_OK then raise Exception.Create(zerror(err));
     try
-      ibpos := 0;
-      zst.next_out := ob;
-      zst.avail_out := OBSize;
       eof := false;
-      while true do
-      begin
-        while not eof and (ibpos < IBSize) do
+      repeat
+        if zst.avail_in = 0 then
         begin
-          rd := ss.read((ib+ibpos)^, IBSize-ibpos);
+          // read input buffer part
+          rd := ss.read(ib^, IBSize);
           if rd < 0 then raise Exception.Create('reading error');
-          eof := (rd <> IBSize-ibpos);
-          if rd <> 0 then begin crc := crc32(crc, Pointer(ib+ibpos), rd); result := crc; end;
-          Inc(ibpos, rd);
-          if rd <> 0 then processed(rd);
+          //writeln('  read ', rd, ' bytes');
+          eof := (rd = 0);
+          if rd <> 0 then begin crc := crc32(crc, Pointer(ib), rd); result := crc; end;
+          zst.next_in := ib;
+          zst.avail_in := rd;
         end;
-        zst.next_in := ib;
-        zst.avail_in := ibpos;
-        if eof then break;
-        err := deflate(zst, Z_NO_FLUSH);
-        if (err <> Z_OK) and (err <> Z_STREAM_END) then raise Exception.Create(zerror(err));
-        if zst.avail_out < OBSize then
+        // now process the whole input
+        while zst.avail_in > 0 do
         begin
-          //writeln('  written ', OBSize-zst.avail_out, ' bytes');
-          ds.writeBuffer(ob^, OBSize-zst.avail_out);
-          zst.next_out := ob;
-          zst.avail_out := OBSize;
-        end;
-        // shift input buffer
-        if zst.avail_in < ibpos then
-        begin
-          rd := 0;
-          for f := ibpos-zst.avail_in to ibpos-1 do
+          err := deflate(zst, Z_NO_FLUSH);
+          if err <> Z_OK then raise Exception.Create(zerror(err));
+          if zst.avail_out < OBSize then
           begin
-            ib[rd] := ib[f];
-            Inc(rd);
+            //writeln('  written ', OBSize-zst.avail_out, ' bytes');
+            if ds.position+(OBSize-zst.avail_out)-dstp >= srcsize then
+            begin
+              // this will be overwritten anyway
+              aborted := true;
+              exit;
+            end;
+            ds.writeBuffer(ob^, OBSize-zst.avail_out);
+            zst.next_out := ob;
+            zst.avail_out := OBSize;
           end;
-          ibpos := rd;
-          //writeln(' rd: ', zst.avail_in);
         end;
-      end;
-      // pack leftovers
-      while zst.avail_in > 0 do
-      begin
-        err := deflate(zst, Z_NO_FLUSH);
-        if (err <> Z_OK) and (err <> Z_STREAM_END) then raise Exception.Create(zerror(err));
-        if zst.avail_out < OBSize then
-        begin
-          //writeln('  written ', OBSize-zst.avail_out, ' bytes');
-          ds.writeBuffer(ob^, OBSize-zst.avail_out);
-          zst.next_out := ob;
-          zst.avail_out := OBSize;
-        end;
-      end;
-      // stream compressed, flush zstream
+      until eof;
+      // do leftovers
       while true do
       begin
         zst.avail_in := 0;
-        zst.next_out := ob;
-        zst.avail_out := OBSize;
         err := deflate(zst, Z_FINISH);
+        if (err <> Z_OK) and (err <> Z_STREAM_END) then raise Exception.Create(zerror(err));
         if zst.avail_out < OBSize then
         begin
-          //writeln('  written ', OBSize-zst.avail_out, ' bytes');
+          //writeln('  .written ', OBSize-zst.avail_out, ' bytes');
+          if ds.position+(OBSize-zst.avail_out)-dstp >= srcsize then
+          begin
+            // this will be overwritten anyway
+            aborted := true;
+            exit;
+          end;
           ds.writeBuffer(ob^, OBSize-zst.avail_out);
+          zst.next_out := ob;
+          zst.avail_out := OBSize;
         end;
         if err <> Z_OK then break;
       end;
@@ -336,6 +330,7 @@ var
   sz: Word;
 begin
   fu := toUtf8(fname);
+  if fu = fname then begin result := nil; exit; end; // no need to write anything
   crc := crc32(0, @fname[1], length(fname));
   sz := 2+2+1+4+length(fu);
   SetLength(result, sz);
@@ -355,10 +350,13 @@ end;
 const UtfFlags = (1 shl 10); // bit 11
 {$ENDIF}
 
-function ZipOne (ds: TStream; fname: AnsiString; st: TStream): TFileInfo;
+function ZipOne (ds: TStream; fname: AnsiString; st: TStream; dopack: Boolean=true): TFileInfo;
 var
-  oldofs, nfoofs, pkdpos: Int64;
+  oldofs, nfoofs, pkdpos, rd: Int64;
   sign: packed array [0..3] of Char;
+  buf: PChar;
+  bufsz: Integer;
+  aborted: Boolean = false;
 {$IFDEF UTFEXTRA}
   ef: TByteArray;
 {$ENDIF}
@@ -367,6 +365,11 @@ begin
   result.pkofs := ds.position;
   result.size := st.size;
   if result.size > 0 then result.method := 8 else result.method := 0;
+  if not dopack then
+  begin
+    result.method := 0;
+    result.pksize := result.size;
+  end;
 {$IFDEF UTFEXTRA}
   result.name := fname;
   ef := buildUtfExtra(result.name);
@@ -395,19 +398,62 @@ begin
 {$IFDEF UTFEXTRA}
   if length(ef) > 0 then ds.writeBuffer(ef[0], length(ef));
 {$ENDIF}
-  // now write packed data
-  if result.size > 0 then
+  if dopack then
   begin
-    pkdpos := ds.position;
-    st.position := 0;
-    result.crc := zpack(ds, st);
-    result.pksize := ds.position-pkdpos;
+    // now write packed data
+    if result.size > 0 then
+    begin
+      pkdpos := ds.position;
+      st.position := 0;
+      result.crc := zpack(ds, st, aborted);
+      result.pksize := ds.position-pkdpos;
+      if {result.pksize >= result.size} aborted then
+      begin
+        // there's no sence to pack this file, so just store it
+        st.position := 0;
+        ds.position := result.pkofs;
+        result.Free();
+        // store it
+        result := ZipOne(ds, fname, st, false);
+        exit;
+      end
+      else
+      begin
+        // fix header
+        oldofs := ds.position;
+        ds.position := nfoofs;
+        writeInt(ds, LongWord(result.crc)); // crc32
+        writeInt(ds, LongWord(result.pksize)); // crc32
+        ds.position := oldofs;
+      end;
+    end;
+  end
+  else
+  begin
+    bufsz := 1024*1024;
+    GetMem(buf, bufsz);
+    try
+      st.position := 0;
+      result.crc := crc32(0, nil, 0);
+      result.pksize := 0;
+      while result.pksize < result.size do
+      begin
+        rd := result.size-result.pksize;
+        if rd > bufsz then rd := bufsz;
+        st.readBuffer(buf^, rd);
+        ds.writeBuffer(buf^, rd);
+        Inc(result.pksize, rd);
+        result.crc := crc32(result.crc, buf, rd);
+      end;
+    finally
+      FreeMem(buf);
+    end;
     // fix header
     oldofs := ds.position;
     ds.position := nfoofs;
     writeInt(ds, LongWord(result.crc)); // crc32
-    writeInt(ds, LongWord(result.pksize)); // crc32
     ds.position := oldofs;
+    write('(S) ');
   end;
 end;
 
@@ -520,9 +566,12 @@ begin
       if length(fl[f].fName) = 0 then continue;
       fs := SFSFileOpen(dvfn+'::'+fl[f].fPath+fl[f].fName);
       newname := detectExt(fl[f].fPath, fl[f].fName, fs);
+      //fs.Free();
+      //fs := SFSFileOpen(dvfn+'::'+fl[f].fPath+fl[f].fName);
       fs.position := 0;
-      writeln('[', f+1, '/', fl.Count, ']: ', fl[f].fPath, newname, '  ', fs.size);
+      write('[', f+1, '/', fl.Count, ']: ', fl[f].fPath, newname, '  ', fs.size, ' ... ');
       nfo := ZipOne(fo, fl[f].fPath+newname, fs);
+      writeln('DONE');
       SetLength(files, length(files)+1);
       files[high(files)] := nfo;
     end;