diff --git a/src/sfs/wadcvt.dpr b/src/sfs/wadcvt.dpr
index 47a9ffb0ea381940f74e9d5037c073f278579f56..4885b327e10e3a45ba4e22ee0d72228b2a68319a 100644 (file)
--- a/src/sfs/wadcvt.dpr
+++ b/src/sfs/wadcvt.dpr
// returs crc
// 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;
const
IBSize = 65536;
OBSize = 65536;
var
zst: TZStream;
ib, ob: PByte;
- ibpos: Cardinal;
err: Integer;
err: Integer;
- rd, f: Integer;
+ rd: Integer;
eof: Boolean;
crc: LongWord;
eof: Boolean;
crc: LongWord;
+ dstp, srcsize: Int64;
begin
result := 0;
begin
result := 0;
+ //aborted := true; exit;
+ aborted := false;
crc := crc32(0, nil, 0);
GetMem(ib, IBSize);
GetMem(ob, OBSize);
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;
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
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;
eof := false;
- while true do
- begin
- while not eof and (ibpos < IBSize) do
+ repeat
+ if zst.avail_in = 0 then
begin
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');
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;
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
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
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;
end;
- ibpos := rd;
- //writeln(' rd: ', zst.avail_in);
end;
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;
while true do
begin
zst.avail_in := 0;
- zst.next_out := ob;
- zst.avail_out := OBSize;
err := deflate(zst, Z_FINISH);
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
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);
ds.writeBuffer(ob^, OBSize-zst.avail_out);
+ zst.next_out := ob;
+ zst.avail_out := OBSize;
end;
if err <> Z_OK then break;
end;
end;
if err <> Z_OK then break;
end;
sz: Word;
begin
fu := toUtf8(fname);
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);
crc := crc32(0, @fname[1], length(fname));
sz := 2+2+1+4+length(fu);
SetLength(result, sz);
const UtfFlags = (1 shl 10); // bit 11
{$ENDIF}
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
var
- oldofs, nfoofs, pkdpos: Int64;
+ oldofs, nfoofs, pkdpos, rd: Int64;
sign: packed array [0..3] of Char;
sign: packed array [0..3] of Char;
+ buf: PChar;
+ bufsz: Integer;
+ aborted: Boolean = false;
{$IFDEF UTFEXTRA}
ef: TByteArray;
{$ENDIF}
{$IFDEF UTFEXTRA}
ef: TByteArray;
{$ENDIF}
result.pkofs := ds.position;
result.size := st.size;
if result.size > 0 then result.method := 8 else result.method := 0;
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);
{$IFDEF UTFEXTRA}
result.name := fname;
ef := buildUtfExtra(result.name);
{$IFDEF UTFEXTRA}
if length(ef) > 0 then ds.writeBuffer(ef[0], length(ef));
{$ENDIF}
{$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
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
// fix header
oldofs := ds.position;
ds.position := nfoofs;
writeInt(ds, LongWord(result.crc)); // crc32
- writeInt(ds, LongWord(result.pksize)); // crc32
ds.position := oldofs;
ds.position := oldofs;
+ write('(S) ');
end;
end;
end;
end;
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);
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;
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);
nfo := ZipOne(fo, fl[f].fPath+newname, fs);
+ writeln('DONE');
SetLength(files, length(files)+1);
files[high(files)] := nfo;
end;
SetLength(files, length(files)+1);
files[high(files)] := nfo;
end;