11 utils in '../shared/utils.pas',
12 xstreams in '../shared/xstreams.pas',
20 procedure processed (count: Cardinal);
22 //writeln(' read ', count, ' bytes');
27 function zpack (ds: TStream; ss: TStream; var aborted: Boolean): LongWord;
41 //aborted := true; exit;
43 crc := crc32(0, nil, 0);
51 zst.avail_out := OBSize;
54 err := deflateInit2(zst, Z_BEST_COMPRESSION, Z_DEFLATED, -15, 9, 0);
55 if err <> Z_OK then raise Exception.Create(zerror(err));
59 if zst.avail_in = 0 then
61 // read input buffer part
62 rd := ss.read(ib^, IBSize);
63 if rd < 0 then raise Exception.Create('reading error');
64 //writeln(' read ', rd, ' bytes');
66 if rd <> 0 then begin crc := crc32(crc, Pointer(ib), rd); result := crc; end;
70 // now process the whole input
71 while zst.avail_in > 0 do
73 err := deflate(zst, Z_NO_FLUSH);
74 if err <> Z_OK then raise Exception.Create(zerror(err));
75 if zst.avail_out < OBSize then
77 //writeln(' written ', OBSize-zst.avail_out, ' bytes');
78 if ds.position+(OBSize-zst.avail_out)-dstp >= srcsize then
80 // this will be overwritten anyway
84 ds.writeBuffer(ob^, OBSize-zst.avail_out);
86 zst.avail_out := OBSize;
94 err := deflate(zst, Z_FINISH);
95 if (err <> Z_OK) and (err <> Z_STREAM_END) then raise Exception.Create(zerror(err));
96 if zst.avail_out < OBSize then
98 //writeln(' .written ', OBSize-zst.avail_out, ' bytes');
99 if ds.position+(OBSize-zst.avail_out)-dstp >= srcsize then
101 // this will be overwritten anyway
105 ds.writeBuffer(ob^, OBSize-zst.avail_out);
107 zst.avail_out := OBSize;
109 if err <> Z_OK then break;
111 // succesfully flushed?
112 if (err <> Z_STREAM_END) then raise Exception.Create(zerror(err));
124 procedure TProg.putStr (const s: AnsiString; newline: Boolean=false);
127 while lastlen > length(s) do
139 lastlen := length(s);
143 procedure TProg.onProgress (sender: TObject; const percent: double);
147 prc := trunc(percent*100.0);
148 putStr(Format('compressing %-33s %3d%%', [lastname, prc]));
151 procedure TProg.onFileStart (sender: TObject; const fileName: AnsiString);
153 lastname := fileName;
154 putStr(Format('compressing %-33s %3d%%', [lastname, 0]));
157 procedure TProg.onFileEnd (sender: TObject; const ratio: double);
159 putStr(Format('compressed %-33s %f', [lastname, ratio]), true);
164 // returns new file name
165 function detectExt (fpath, fname: AnsiString; fs: TStream): AnsiString;
173 if length(ExtractFileExt(fname)) <> 0 then exit;
174 if fs.size < 16 then exit;
175 buflen := Integer(fs.size);
178 fs.ReadBuffer(buf^, buflen);
180 Move(buf^, (PChar(@st[1]))^, 16);
182 if (st = 'Extended Module:') then
184 result := result+'.xm';
187 if (buf[0] = 'D') and (buf[1] = 'F') and (buf[2] = 'W') and
188 (buf[3] = 'A') and (buf[4] = 'D') and (buf[5] = #$1) then
190 result := result+'.wad';
193 if (buf[0] = 'M') and (buf[1] = 'A') and (buf[2] = 'P') and (buf[3] = #$1) then
195 result := result+'.dfmap';
198 if (buf[0] = 'M') and (buf[1] = 'T') and (buf[2] = 'h') and (buf[3] = 'd') then
200 result := result+'.mid';
203 if (buf[0] = 'R') and (buf[1] = 'I') and (buf[2] = 'F') and (buf[3] = 'F') and
204 (buf[8] = 'W') and (buf[9] = 'A') and (buf[10] = 'V') and (buf[11] = 'E') then
206 result := result+'.wav';
210 for f := 0 to 128-6 do
212 if (buf[f+0] = #$4) and (buf[f+1] = 'L') and
213 (buf[f+2] = 'A') and (buf[f+3] = 'M') and
214 (buf[f+4] = 'E') and (buf[f+5] = '3') then
216 result := result+'.mp3';
221 if (buf[0] = 'I') and (buf[1] = 'D') and (buf[2] = '3') and (buf[3] <= #4) then
223 result := result+'.mp3';
228 if (buf[buflen-128] = 'T') and (buf[buflen-127] = 'A') and (buf[buflen-126] = 'G') then
230 result := result+'.mp3';
234 // targa (stupid hack; this "signature" is not required by specs)
237 Move((buf+buflen-18)^, (PChar(@st[1]))^, 16);
239 if st = 'TRUEVISION-XFILE' then
241 result := result+'.tga';
255 pkofs: Int64; // offset of file header
261 constructor Create ();
264 constructor TFileInfo.Create ();
270 crc := crc32(0, nil, 0);
276 uni2wint: array [128..255] of Word = (
277 $0402,$0403,$201A,$0453,$201E,$2026,$2020,$2021,$20AC,$2030,$0409,$2039,$040A,$040C,$040B,$040F,
278 $0452,$2018,$2019,$201C,$201D,$2022,$2013,$2014,$003F,$2122,$0459,$203A,$045A,$045C,$045B,$045F,
279 $00A0,$040E,$045E,$0408,$00A4,$0490,$00A6,$00A7,$0401,$00A9,$0404,$00AB,$00AC,$00AD,$00AE,$0407,
280 $00B0,$00B1,$0406,$0456,$0491,$00B5,$00B6,$00B7,$0451,$2116,$0454,$00BB,$0458,$0405,$0455,$0457,
281 $0410,$0411,$0412,$0413,$0414,$0415,$0416,$0417,$0418,$0419,$041A,$041B,$041C,$041D,$041E,$041F,
282 $0420,$0421,$0422,$0423,$0424,$0425,$0426,$0427,$0428,$0429,$042A,$042B,$042C,$042D,$042E,$042F,
283 $0430,$0431,$0432,$0433,$0434,$0435,$0436,$0437,$0438,$0439,$043A,$043B,$043C,$043D,$043E,$043F,
284 $0440,$0441,$0442,$0443,$0444,$0445,$0446,$0447,$0448,$0449,$044A,$044B,$044C,$044D,$044E,$044F
288 function toUtf8 (const s: AnsiString): AnsiString;
294 GetMem(uc, length(s)*8);
295 GetMem(xdc, length(s)*8);
297 FillChar(uc^, length(s)*8, 0);
298 FillChar(xdc^, length(s)*8, 0);
300 for f := 1 to length(s) do
302 if ord(s[f]) < 128 then
303 uc[pos] := UnicodeChar(ord(s[f]))
305 uc[pos] := UnicodeChar(uni2wint[ord(s[f])]);
308 FillChar(xdc^, length(s)*8, 0);
309 f := UnicodeToUtf8(xdc, length(s)*8, uc, pos);
310 while (f > 0) and (xdc[f-1] = #0) do Dec(f);
311 SetLength(result, f);
312 Move(xdc^, result[1], f);
319 // this will write "extra field length" and extra field itself
324 TByteArray = array of Byte;
326 function buildUtfExtra (fname: AnsiString): TByteArray;
333 if fu = fname then begin result := nil; exit; end; // no need to write anything
334 crc := crc32(0, @fname[1], length(fname));
335 sz := 2+2+1+4+length(fu);
336 SetLength(result, sz);
337 result[0] := ord('u');
338 result[1] := ord('p');
340 result[2] := sz and $ff;
341 result[3] := (sz shr 8) and $ff;
343 result[5] := crc and $ff;
344 result[6] := (crc shr 8) and $ff;
345 result[7] := (crc shr 16) and $ff;
346 result[8] := (crc shr 24) and $ff;
347 Move(fu[1], result[9], length(fu));
350 const UtfFlags = (1 shl 10); // bit 11
353 function ZipOne (ds: TStream; fname: AnsiString; st: TStream; dopack: Boolean=true): TFileInfo;
355 oldofs, nfoofs, pkdpos, rd: Int64;
356 sign: packed array [0..3] of Char;
359 aborted: Boolean = false;
364 result := TFileInfo.Create();
365 result.pkofs := ds.position;
366 result.size := st.size;
367 if result.size > 0 then result.method := 8 else result.method := 0;
371 result.pksize := result.size;
374 result.name := fname;
375 ef := buildUtfExtra(result.name);
377 result.name := toUtf8(fname);
379 // write local header
381 ds.writeBuffer(sign, 4);
382 writeInt(ds, Word($0A10)); // version to extract
383 writeInt(ds, Word(UtfFlags)); // flags
384 writeInt(ds, Word(result.method)); // compression method
385 writeInt(ds, Word(0)); // file time
386 writeInt(ds, Word(0)); // file date
387 nfoofs := ds.position;
388 writeInt(ds, LongWord(result.crc)); // crc32
389 writeInt(ds, LongWord(result.pksize)); // packed size
390 writeInt(ds, LongWord(result.size)); // unpacked size
391 writeInt(ds, Word(length(fname))); // name length
393 writeInt(ds, Word(length(ef))); // extra field length
395 writeInt(ds, Word(0)); // extra field length
397 ds.writeBuffer(fname[1], length(fname));
399 if length(ef) > 0 then ds.writeBuffer(ef[0], length(ef));
403 // now write packed data
404 if result.size > 0 then
406 pkdpos := ds.position;
408 result.crc := zpack(ds, st, aborted);
409 result.pksize := ds.position-pkdpos;
410 if {result.pksize >= result.size} aborted then
412 // there's no sence to pack this file, so just store it
414 ds.position := result.pkofs;
417 result := ZipOne(ds, fname, st, false);
423 oldofs := ds.position;
424 ds.position := nfoofs;
425 writeInt(ds, LongWord(result.crc)); // crc32
426 writeInt(ds, LongWord(result.pksize)); // crc32
427 ds.position := oldofs;
437 result.crc := crc32(0, nil, 0);
439 while result.pksize < result.size do
441 rd := result.size-result.pksize;
442 if rd > bufsz then rd := bufsz;
443 st.readBuffer(buf^, rd);
444 ds.writeBuffer(buf^, rd);
445 Inc(result.pksize, rd);
446 result.crc := crc32(result.crc, buf, rd);
452 oldofs := ds.position;
453 ds.position := nfoofs;
454 writeInt(ds, LongWord(result.crc)); // crc32
455 ds.position := oldofs;
461 procedure writeCentralDir (ds: TStream; files: array of TFileInfo);
464 sign: packed array [0..3] of Char;
470 cdofs := ds.position;
471 for f := 0 to high(files) do
474 ef := buildUtfExtra(files[f].name);
477 ds.writeBuffer(sign, 4);
478 writeInt(ds, Word($0A10)); // version made by
479 writeInt(ds, Word($0010)); // version to extract
480 writeInt(ds, Word(UtfFlags)); // flags
481 writeInt(ds, Word(files[f].method)); // compression method
482 writeInt(ds, Word(0)); // file time
483 writeInt(ds, Word(0)); // file date
484 writeInt(ds, LongWord(files[f].crc));
485 writeInt(ds, LongWord(files[f].pksize));
486 writeInt(ds, LongWord(files[f].size));
487 writeInt(ds, Word(length(files[f].name))); // name length
489 writeInt(ds, Word(length(ef))); // extra field length
491 writeInt(ds, Word(0)); // extra field length
493 writeInt(ds, Word(0)); // comment length
494 writeInt(ds, Word(0)); // disk start
495 writeInt(ds, Word(0)); // internal attributes
496 writeInt(ds, LongWord(0)); // external attributes
497 writeInt(ds, LongWord(files[f].pkofs)); // header offset
498 ds.writeBuffer(files[f].name[1], length(files[f].name));
500 if length(ef) > 0 then ds.writeBuffer(ef[0], length(ef));
503 cdend := ds.position;
504 // write end of central dir
506 ds.writeBuffer(sign, 4);
507 writeInt(ds, Word(0)); // disk number
508 writeInt(ds, Word(0)); // disk with central dir
509 writeInt(ds, Word(length(files))); // number of files on this dist
510 writeInt(ds, Word(length(files))); // number of files total
511 writeInt(ds, LongWord(cdend-cdofs)); // size of central directory
512 writeInt(ds, LongWord(cdofs)); // central directory offset
513 writeInt(ds, Word(0)); // archive comment length
522 outfname: AnsiString;
525 files: array of TFileInfo;
528 if ParamCount() < 1 then
530 WriteLn('usage: wadcvt file.wad');
534 infname := ParamStr(1);
535 if not StrEquCI1251(ExtractFileExt(infname), '.wad') and not StrEquCI1251(ExtractFileExt(infname), '.dfwad') then
541 if ParamCount() > 1 then
543 outfname := ParamStr(2);
547 outfname := ChangeFileExt(infname, '.pk3');
550 if not SFSAddDataFile(infname) then begin WriteLn('shit!'); Halt(1); end;
551 dvfn := SFSGetLastVirtualName(infname);
555 fl := SFSFileList(dvfn);
562 fo := TFileStream.Create(outfname, fmCreate);
564 for f := 0 to fl.Count-1 do
566 if length(fl[f].fName) = 0 then continue;
567 fs := SFSFileOpen(dvfn+'::'+fl[f].fPath+fl[f].fName);
568 newname := detectExt(fl[f].fPath, fl[f].fName, fs);
570 //fs := SFSFileOpen(dvfn+'::'+fl[f].fPath+fl[f].fName);
572 write('[', f+1, '/', fl.Count, ']: ', fl[f].fPath, newname, ' ', fs.size, ' ... ');
573 nfo := ZipOne(fo, fl[f].fPath+newname, fs);
575 SetLength(files, length(files)+1);
576 files[high(files)] := nfo;
578 writeCentralDir(fo, files);
582 DeleteFile(outfname);
584 if fo <> nil then fo.Free();