11 utils in '../shared/utils.pas',
12 xstreams in '../shared/xstreams.pas',
18 ImagingTypes, Imaging, ImagingUtility;
21 procedure processed (count: Cardinal);
23 //writeln(' read ', count, ' bytes');
28 function zpack (ds: TStream; ss: TStream; var aborted: Boolean): LongWord;
42 //aborted := true; exit;
44 crc := crc32(0, nil, 0);
52 zst.avail_out := OBSize;
55 err := deflateInit2(zst, Z_BEST_COMPRESSION, Z_DEFLATED, -15, 9, 0);
56 if err <> Z_OK then raise Exception.Create(zerror(err));
60 if zst.avail_in = 0 then
62 // read input buffer part
63 rd := ss.read(ib^, IBSize);
64 if rd < 0 then raise Exception.Create('reading error');
65 //writeln(' read ', rd, ' bytes');
67 if rd <> 0 then begin crc := crc32(crc, Pointer(ib), rd); result := crc; end;
71 // now process the whole input
72 while zst.avail_in > 0 do
74 err := deflate(zst, Z_NO_FLUSH);
75 if err <> Z_OK then raise Exception.Create(zerror(err));
76 if zst.avail_out < OBSize then
78 //writeln(' written ', OBSize-zst.avail_out, ' bytes');
79 if ds.position+(OBSize-zst.avail_out)-dstp >= srcsize then
81 // this will be overwritten anyway
85 ds.writeBuffer(ob^, OBSize-zst.avail_out);
87 zst.avail_out := OBSize;
95 err := deflate(zst, Z_FINISH);
96 if (err <> Z_OK) and (err <> Z_STREAM_END) then raise Exception.Create(zerror(err));
97 if zst.avail_out < OBSize then
99 //writeln(' .written ', OBSize-zst.avail_out, ' bytes');
100 if ds.position+(OBSize-zst.avail_out)-dstp >= srcsize then
102 // this will be overwritten anyway
106 ds.writeBuffer(ob^, OBSize-zst.avail_out);
108 zst.avail_out := OBSize;
110 if err <> Z_OK then break;
112 // succesfully flushed?
113 if (err <> Z_STREAM_END) then raise Exception.Create(zerror(err));
125 procedure TProg.putStr (const s: AnsiString; newline: Boolean=false);
128 while lastlen > length(s) do
140 lastlen := length(s);
144 procedure TProg.onProgress (sender: TObject; const percent: double);
148 prc := trunc(percent*100.0);
149 putStr(Format('compressing %-33s %3d%%', [lastname, prc]));
152 procedure TProg.onFileStart (sender: TObject; const fileName: AnsiString);
154 lastname := fileName;
155 putStr(Format('compressing %-33s %3d%%', [lastname, 0]));
158 procedure TProg.onFileEnd (sender: TObject; const ratio: double);
160 putStr(Format('compressed %-33s %f', [lastname, ratio]), true);
165 // returns new file name
166 function detectExt (fpath, fname: AnsiString; fs: TStream): AnsiString;
175 if length(ExtractFileExt(fname)) <> 0 then exit;
176 if fs.size < 16 then exit;
177 buflen := Integer(fs.size);
180 fs.ReadBuffer(buf^, buflen);
182 Move(buf^, (PChar(@st[1]))^, 16);
184 if (st = 'Extended Module:') then
186 result := result+'.xm';
189 if (buf[0] = 'D') and (buf[1] = 'F') and (buf[2] = 'W') and
190 (buf[3] = 'A') and (buf[4] = 'D') and (buf[5] = #$1) then
192 result := result+'.wad';
195 if (buf[0] = 'M') and (buf[1] = 'A') and (buf[2] = 'P') and (buf[3] = #$1) then
197 result := result+'.dfmap';
200 if (buf[0] = 'M') and (buf[1] = 'T') and (buf[2] = 'h') and (buf[3] = 'd') then
202 result := result+'.mid';
205 if (buf[0] = 'R') and (buf[1] = 'I') and (buf[2] = 'F') and (buf[3] = 'F') and
206 (buf[8] = 'W') and (buf[9] = 'A') and (buf[10] = 'V') and (buf[11] = 'E') then
208 result := result+'.wav';
212 for f := 0 to 128-6 do
214 if (buf[f+0] = #$4) and (buf[f+1] = 'L') and
215 (buf[f+2] = 'A') and (buf[f+3] = 'M') and
216 (buf[f+4] = 'E') and (buf[f+5] = '3') then
218 result := result+'.mp3';
223 if (buf[0] = 'I') and (buf[1] = 'D') and (buf[2] = '3') and (buf[3] <= #4) then
225 result := result+'.mp3';
230 if (buf[buflen-128] = 'T') and (buf[buflen-127] = 'A') and (buf[buflen-126] = 'G') then
232 result := result+'.mp3';
236 // targa (stupid hack; this "signature" is not required by specs)
240 Move((buf+buflen-18)^, (PChar(@st[1]))^, 16);
242 if st = 'TRUEVISION-XFILE' then
244 result := result+'.tga';
249 // detect image format
250 img := DetermineMemoryFormat(buf, buflen);
251 if length(img) > 0 then
253 result := result+'.'+img;
256 // check if this is text file
259 for f := 0 to buflen-1 do
261 if buf[f] = #127 then exit;
264 if (buf[f] <> #9) and (buf[f] <> #10) and (buf[f] <> #13) then exit;
267 result := result+'.txt';
279 pkofs: Int64; // offset of file header
285 constructor Create ();
288 constructor TFileInfo.Create ();
294 crc := crc32(0, nil, 0);
300 uni2wint: array [128..255] of Word = (
301 $0402,$0403,$201A,$0453,$201E,$2026,$2020,$2021,$20AC,$2030,$0409,$2039,$040A,$040C,$040B,$040F,
302 $0452,$2018,$2019,$201C,$201D,$2022,$2013,$2014,$003F,$2122,$0459,$203A,$045A,$045C,$045B,$045F,
303 $00A0,$040E,$045E,$0408,$00A4,$0490,$00A6,$00A7,$0401,$00A9,$0404,$00AB,$00AC,$00AD,$00AE,$0407,
304 $00B0,$00B1,$0406,$0456,$0491,$00B5,$00B6,$00B7,$0451,$2116,$0454,$00BB,$0458,$0405,$0455,$0457,
305 $0410,$0411,$0412,$0413,$0414,$0415,$0416,$0417,$0418,$0419,$041A,$041B,$041C,$041D,$041E,$041F,
306 $0420,$0421,$0422,$0423,$0424,$0425,$0426,$0427,$0428,$0429,$042A,$042B,$042C,$042D,$042E,$042F,
307 $0430,$0431,$0432,$0433,$0434,$0435,$0436,$0437,$0438,$0439,$043A,$043B,$043C,$043D,$043E,$043F,
308 $0440,$0441,$0442,$0443,$0444,$0445,$0446,$0447,$0448,$0449,$044A,$044B,$044C,$044D,$044E,$044F
312 function toUtf8 (const s: AnsiString): AnsiString;
318 GetMem(uc, length(s)*8);
319 GetMem(xdc, length(s)*8);
321 FillChar(uc^, length(s)*8, 0);
322 FillChar(xdc^, length(s)*8, 0);
324 for f := 1 to length(s) do
326 if ord(s[f]) < 128 then
327 uc[pos] := UnicodeChar(ord(s[f]))
329 uc[pos] := UnicodeChar(uni2wint[ord(s[f])]);
332 FillChar(xdc^, length(s)*8, 0);
333 f := UnicodeToUtf8(xdc, length(s)*8, uc, pos);
334 while (f > 0) and (xdc[f-1] = #0) do Dec(f);
335 SetLength(result, f);
336 Move(xdc^, result[1], f);
343 // this will write "extra field length" and extra field itself
348 TByteArray = array of Byte;
350 function buildUtfExtra (fname: AnsiString): TByteArray;
357 if fu = fname then begin result := nil; exit; end; // no need to write anything
358 crc := crc32(0, @fname[1], length(fname));
359 sz := 2+2+1+4+length(fu);
360 SetLength(result, sz);
361 result[0] := ord('u');
362 result[1] := ord('p');
364 result[2] := sz and $ff;
365 result[3] := (sz shr 8) and $ff;
367 result[5] := crc and $ff;
368 result[6] := (crc shr 8) and $ff;
369 result[7] := (crc shr 16) and $ff;
370 result[8] := (crc shr 24) and $ff;
371 Move(fu[1], result[9], length(fu));
374 const UtfFlags = (1 shl 10); // bit 11
377 function ZipOne (ds: TStream; fname: AnsiString; st: TStream; dopack: Boolean=true): TFileInfo;
379 oldofs, nfoofs, pkdpos, rd: Int64;
380 sign: packed array [0..3] of Char;
383 aborted: Boolean = false;
388 result := TFileInfo.Create();
389 result.pkofs := ds.position;
390 result.size := st.size;
391 if result.size > 0 then result.method := 8 else result.method := 0;
395 result.pksize := result.size;
398 result.name := fname;
399 ef := buildUtfExtra(result.name);
401 result.name := toUtf8(fname);
403 // write local header
405 ds.writeBuffer(sign, 4);
406 writeInt(ds, Word($0A10)); // version to extract
407 writeInt(ds, Word(UtfFlags)); // flags
408 writeInt(ds, Word(result.method)); // compression method
409 writeInt(ds, Word(0)); // file time
410 writeInt(ds, Word(0)); // file date
411 nfoofs := ds.position;
412 writeInt(ds, LongWord(result.crc)); // crc32
413 writeInt(ds, LongWord(result.pksize)); // packed size
414 writeInt(ds, LongWord(result.size)); // unpacked size
415 writeInt(ds, Word(length(fname))); // name length
417 writeInt(ds, Word(length(ef))); // extra field length
419 writeInt(ds, Word(0)); // extra field length
421 ds.writeBuffer(fname[1], length(fname));
423 if length(ef) > 0 then ds.writeBuffer(ef[0], length(ef));
427 // now write packed data
428 if result.size > 0 then
430 pkdpos := ds.position;
432 result.crc := zpack(ds, st, aborted);
433 result.pksize := ds.position-pkdpos;
434 if {result.pksize >= result.size} aborted then
436 // there's no sence to pack this file, so just store it
438 ds.position := result.pkofs;
441 result := ZipOne(ds, fname, st, false);
447 oldofs := ds.position;
448 ds.position := nfoofs;
449 writeInt(ds, LongWord(result.crc)); // crc32
450 writeInt(ds, LongWord(result.pksize)); // crc32
451 ds.position := oldofs;
461 result.crc := crc32(0, nil, 0);
463 while result.pksize < result.size do
465 rd := result.size-result.pksize;
466 if rd > bufsz then rd := bufsz;
467 st.readBuffer(buf^, rd);
468 ds.writeBuffer(buf^, rd);
469 Inc(result.pksize, rd);
470 result.crc := crc32(result.crc, buf, rd);
476 oldofs := ds.position;
477 ds.position := nfoofs;
478 writeInt(ds, LongWord(result.crc)); // crc32
479 ds.position := oldofs;
485 procedure writeCentralDir (ds: TStream; files: array of TFileInfo);
488 sign: packed array [0..3] of Char;
494 cdofs := ds.position;
495 for f := 0 to high(files) do
498 ef := buildUtfExtra(files[f].name);
501 ds.writeBuffer(sign, 4);
502 writeInt(ds, Word($0A10)); // version made by
503 writeInt(ds, Word($0010)); // version to extract
504 writeInt(ds, Word(UtfFlags)); // flags
505 writeInt(ds, Word(files[f].method)); // compression method
506 writeInt(ds, Word(0)); // file time
507 writeInt(ds, Word(0)); // file date
508 writeInt(ds, LongWord(files[f].crc));
509 writeInt(ds, LongWord(files[f].pksize));
510 writeInt(ds, LongWord(files[f].size));
511 writeInt(ds, Word(length(files[f].name))); // name length
513 writeInt(ds, Word(length(ef))); // extra field length
515 writeInt(ds, Word(0)); // extra field length
517 writeInt(ds, Word(0)); // comment length
518 writeInt(ds, Word(0)); // disk start
519 writeInt(ds, Word(0)); // internal attributes
520 writeInt(ds, LongWord(0)); // external attributes
521 writeInt(ds, LongWord(files[f].pkofs)); // header offset
522 ds.writeBuffer(files[f].name[1], length(files[f].name));
524 if length(ef) > 0 then ds.writeBuffer(ef[0], length(ef));
527 cdend := ds.position;
528 // write end of central dir
530 ds.writeBuffer(sign, 4);
531 writeInt(ds, Word(0)); // disk number
532 writeInt(ds, Word(0)); // disk with central dir
533 writeInt(ds, Word(length(files))); // number of files on this dist
534 writeInt(ds, Word(length(files))); // number of files total
535 writeInt(ds, LongWord(cdend-cdofs)); // size of central directory
536 writeInt(ds, LongWord(cdofs)); // central directory offset
537 writeInt(ds, Word(0)); // archive comment length
546 outfname: AnsiString;
549 files: array of TFileInfo;
552 if ParamCount() < 1 then
554 WriteLn('usage: wadcvt file.wad');
558 infname := ParamStr(1);
559 if not StrEquCI1251(ExtractFileExt(infname), '.wad') and not StrEquCI1251(ExtractFileExt(infname), '.dfwad') then
565 if ParamCount() > 1 then
567 outfname := ParamStr(2);
571 outfname := ChangeFileExt(infname, '.pk3');
574 if not SFSAddDataFile(infname) then begin WriteLn('shit!'); Halt(1); end;
575 dvfn := SFSGetLastVirtualName(infname);
579 fl := SFSFileList(dvfn);
586 fo := TFileStream.Create(outfname, fmCreate);
588 for f := 0 to fl.Count-1 do
590 if length(fl[f].fName) = 0 then continue;
591 fs := SFSFileOpen(dvfn+'::'+fl[f].fPath+fl[f].fName);
592 newname := detectExt(fl[f].fPath, fl[f].fName, fs);
594 //fs := SFSFileOpen(dvfn+'::'+fl[f].fPath+fl[f].fName);
597 write(#13'[', f+1, '/', fl.Count, ']: ', fl[f].fPath, newname, ' ', fs.size, ' ... '#27'[K');
599 write('[', f+1, '/', fl.Count, ']: ', fl[f].fPath, newname, ' ', fs.size, ' ... ');
601 nfo := ZipOne(fo, fl[f].fPath+newname, fs);
606 SetLength(files, length(files)+1);
607 files[high(files)] := nfo;
610 writeln(#13, fl.Count, ' files processed'#27'[K');
612 writeCentralDir(fo, files);
616 DeleteFile(outfname);
618 if fo <> nil then fo.Free();