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): LongWord;
40 crc := crc32(0, nil, 0);
46 zst.avail_out := OBSize;
49 err := deflateInit2(zst, Z_BEST_COMPRESSION, Z_DEFLATED, -15, 9, 0);
50 if err <> Z_OK then raise Exception.Create(zerror(err));
54 if zst.avail_in = 0 then
56 // read input buffer part
57 rd := ss.read(ib^, IBSize);
58 if rd < 0 then raise Exception.Create('reading error');
59 //writeln(' read ', rd, ' bytes');
61 if rd <> 0 then begin crc := crc32(crc, Pointer(ib), rd); result := crc; end;
65 // now process the whole input
66 while zst.avail_in > 0 do
68 err := deflate(zst, Z_NO_FLUSH);
69 if err <> Z_OK then raise Exception.Create(zerror(err));
70 if zst.avail_out < OBSize then
72 //writeln(' written ', OBSize-zst.avail_out, ' bytes');
73 ds.writeBuffer(ob^, OBSize-zst.avail_out);
75 zst.avail_out := OBSize;
83 err := deflate(zst, Z_FINISH);
84 if (err <> Z_OK) and (err <> Z_STREAM_END) then raise Exception.Create(zerror(err));
85 if zst.avail_out < OBSize then
87 //writeln(' .written ', OBSize-zst.avail_out, ' bytes');
88 ds.writeBuffer(ob^, OBSize-zst.avail_out);
90 zst.avail_out := OBSize;
92 if err <> Z_OK then break;
94 // succesfully flushed?
95 if (err <> Z_STREAM_END) then raise Exception.Create(zerror(err));
107 procedure TProg.putStr (const s: AnsiString; newline: Boolean=false);
110 while lastlen > length(s) do
122 lastlen := length(s);
126 procedure TProg.onProgress (sender: TObject; const percent: double);
130 prc := trunc(percent*100.0);
131 putStr(Format('compressing %-33s %3d%%', [lastname, prc]));
134 procedure TProg.onFileStart (sender: TObject; const fileName: AnsiString);
136 lastname := fileName;
137 putStr(Format('compressing %-33s %3d%%', [lastname, 0]));
140 procedure TProg.onFileEnd (sender: TObject; const ratio: double);
142 putStr(Format('compressed %-33s %f', [lastname, ratio]), true);
147 // returns new file name
148 function detectExt (fpath, fname: AnsiString; fs: TStream): AnsiString;
156 if length(ExtractFileExt(fname)) <> 0 then exit;
157 if fs.size < 16 then exit;
158 buflen := Integer(fs.size);
161 fs.ReadBuffer(buf^, buflen);
163 Move(buf^, (PChar(@st[1]))^, 16);
165 if (st = 'Extended Module:') then
167 result := result+'.xm';
170 if (buf[0] = 'D') and (buf[1] = 'F') and (buf[2] = 'W') and
171 (buf[3] = 'A') and (buf[4] = 'D') and (buf[5] = #$1) then
173 result := result+'.wad';
176 if (buf[0] = 'M') and (buf[1] = 'A') and (buf[2] = 'P') and (buf[3] = #$1) then
178 result := result+'.dfmap';
181 if (buf[0] = 'M') and (buf[1] = 'T') and (buf[2] = 'h') and (buf[3] = 'd') then
183 result := result+'.mid';
186 if (buf[0] = 'R') and (buf[1] = 'I') and (buf[2] = 'F') and (buf[3] = 'F') and
187 (buf[8] = 'W') and (buf[9] = 'A') and (buf[10] = 'V') and (buf[11] = 'E') then
189 result := result+'.wav';
193 for f := 0 to 128-6 do
195 if (buf[f+0] = #$4) and (buf[f+1] = 'L') and
196 (buf[f+2] = 'A') and (buf[f+3] = 'M') and
197 (buf[f+4] = 'E') and (buf[f+5] = '3') then
199 result := result+'.mp3';
204 if (buf[0] = 'I') and (buf[1] = 'D') and (buf[2] = '3') and (buf[3] <= #4) then
206 result := result+'.mp3';
211 if (buf[buflen-128] = 'T') and (buf[buflen-127] = 'A') and (buf[buflen-126] = 'G') then
213 result := result+'.mp3';
217 // targa (stupid hack; this "signature" is not required by specs)
220 Move((buf+buflen-18)^, (PChar(@st[1]))^, 16);
222 if st = 'TRUEVISION-XFILE' then
224 result := result+'.tga';
238 pkofs: Int64; // offset of file header
244 constructor Create ();
247 constructor TFileInfo.Create ();
253 crc := crc32(0, nil, 0);
259 uni2wint: array [128..255] of Word = (
260 $0402,$0403,$201A,$0453,$201E,$2026,$2020,$2021,$20AC,$2030,$0409,$2039,$040A,$040C,$040B,$040F,
261 $0452,$2018,$2019,$201C,$201D,$2022,$2013,$2014,$003F,$2122,$0459,$203A,$045A,$045C,$045B,$045F,
262 $00A0,$040E,$045E,$0408,$00A4,$0490,$00A6,$00A7,$0401,$00A9,$0404,$00AB,$00AC,$00AD,$00AE,$0407,
263 $00B0,$00B1,$0406,$0456,$0491,$00B5,$00B6,$00B7,$0451,$2116,$0454,$00BB,$0458,$0405,$0455,$0457,
264 $0410,$0411,$0412,$0413,$0414,$0415,$0416,$0417,$0418,$0419,$041A,$041B,$041C,$041D,$041E,$041F,
265 $0420,$0421,$0422,$0423,$0424,$0425,$0426,$0427,$0428,$0429,$042A,$042B,$042C,$042D,$042E,$042F,
266 $0430,$0431,$0432,$0433,$0434,$0435,$0436,$0437,$0438,$0439,$043A,$043B,$043C,$043D,$043E,$043F,
267 $0440,$0441,$0442,$0443,$0444,$0445,$0446,$0447,$0448,$0449,$044A,$044B,$044C,$044D,$044E,$044F
271 function toUtf8 (const s: AnsiString): AnsiString;
277 GetMem(uc, length(s)*8);
278 GetMem(xdc, length(s)*8);
280 FillChar(uc^, length(s)*8, 0);
281 FillChar(xdc^, length(s)*8, 0);
283 for f := 1 to length(s) do
285 if ord(s[f]) < 128 then
286 uc[pos] := UnicodeChar(ord(s[f]))
288 uc[pos] := UnicodeChar(uni2wint[ord(s[f])]);
291 FillChar(xdc^, length(s)*8, 0);
292 f := UnicodeToUtf8(xdc, length(s)*8, uc, pos);
293 while (f > 0) and (xdc[f-1] = #0) do Dec(f);
294 SetLength(result, f);
295 Move(xdc^, result[1], f);
302 // this will write "extra field length" and extra field itself
307 TByteArray = array of Byte;
309 function buildUtfExtra (fname: AnsiString): TByteArray;
316 crc := crc32(0, @fname[1], length(fname));
317 sz := 2+2+1+4+length(fu);
318 SetLength(result, sz);
319 result[0] := ord('u');
320 result[1] := ord('p');
322 result[2] := sz and $ff;
323 result[3] := (sz shr 8) and $ff;
325 result[5] := crc and $ff;
326 result[6] := (crc shr 8) and $ff;
327 result[7] := (crc shr 16) and $ff;
328 result[8] := (crc shr 24) and $ff;
329 Move(fu[1], result[9], length(fu));
332 const UtfFlags = (1 shl 10); // bit 11
335 function ZipOne (ds: TStream; fname: AnsiString; st: TStream): TFileInfo;
337 oldofs, nfoofs, pkdpos: Int64;
338 sign: packed array [0..3] of Char;
343 result := TFileInfo.Create();
344 result.pkofs := ds.position;
345 result.size := st.size;
346 if result.size > 0 then result.method := 8 else result.method := 0;
348 result.name := fname;
349 ef := buildUtfExtra(result.name);
351 result.name := toUtf8(fname);
353 // write local header
355 ds.writeBuffer(sign, 4);
356 writeInt(ds, Word($0A10)); // version to extract
357 writeInt(ds, Word(UtfFlags)); // flags
358 writeInt(ds, Word(result.method)); // compression method
359 writeInt(ds, Word(0)); // file time
360 writeInt(ds, Word(0)); // file date
361 nfoofs := ds.position;
362 writeInt(ds, LongWord(result.crc)); // crc32
363 writeInt(ds, LongWord(result.pksize)); // packed size
364 writeInt(ds, LongWord(result.size)); // unpacked size
365 writeInt(ds, Word(length(fname))); // name length
367 writeInt(ds, Word(length(ef))); // extra field length
369 writeInt(ds, Word(0)); // extra field length
371 ds.writeBuffer(fname[1], length(fname));
373 if length(ef) > 0 then ds.writeBuffer(ef[0], length(ef));
375 // now write packed data
376 if result.size > 0 then
378 pkdpos := ds.position;
380 result.crc := zpack(ds, st);
381 result.pksize := ds.position-pkdpos;
383 oldofs := ds.position;
384 ds.position := nfoofs;
385 writeInt(ds, LongWord(result.crc)); // crc32
386 writeInt(ds, LongWord(result.pksize)); // crc32
387 ds.position := oldofs;
392 procedure writeCentralDir (ds: TStream; files: array of TFileInfo);
395 sign: packed array [0..3] of Char;
401 cdofs := ds.position;
402 for f := 0 to high(files) do
405 ef := buildUtfExtra(files[f].name);
408 ds.writeBuffer(sign, 4);
409 writeInt(ds, Word($0A10)); // version made by
410 writeInt(ds, Word($0010)); // version to extract
411 writeInt(ds, Word(UtfFlags)); // flags
412 writeInt(ds, Word(files[f].method)); // compression method
413 writeInt(ds, Word(0)); // file time
414 writeInt(ds, Word(0)); // file date
415 writeInt(ds, LongWord(files[f].crc));
416 writeInt(ds, LongWord(files[f].pksize));
417 writeInt(ds, LongWord(files[f].size));
418 writeInt(ds, Word(length(files[f].name))); // name length
420 writeInt(ds, Word(length(ef))); // extra field length
422 writeInt(ds, Word(0)); // extra field length
424 writeInt(ds, Word(0)); // comment length
425 writeInt(ds, Word(0)); // disk start
426 writeInt(ds, Word(0)); // internal attributes
427 writeInt(ds, LongWord(0)); // external attributes
428 writeInt(ds, LongWord(files[f].pkofs)); // header offset
429 ds.writeBuffer(files[f].name[1], length(files[f].name));
431 if length(ef) > 0 then ds.writeBuffer(ef[0], length(ef));
434 cdend := ds.position;
435 // write end of central dir
437 ds.writeBuffer(sign, 4);
438 writeInt(ds, Word(0)); // disk number
439 writeInt(ds, Word(0)); // disk with central dir
440 writeInt(ds, Word(length(files))); // number of files on this dist
441 writeInt(ds, Word(length(files))); // number of files total
442 writeInt(ds, LongWord(cdend-cdofs)); // size of central directory
443 writeInt(ds, LongWord(cdofs)); // central directory offset
444 writeInt(ds, Word(0)); // archive comment length
453 outfname: AnsiString;
456 files: array of TFileInfo;
459 if ParamCount() < 1 then
461 WriteLn('usage: wadcvt file.wad');
465 infname := ParamStr(1);
466 if not StrEquCI1251(ExtractFileExt(infname), '.wad') and not StrEquCI1251(ExtractFileExt(infname), '.dfwad') then
472 if ParamCount() > 1 then
474 outfname := ParamStr(2);
478 outfname := ChangeFileExt(infname, '.pk3');
481 if not SFSAddDataFile(infname) then begin WriteLn('shit!'); Halt(1); end;
482 dvfn := SFSGetLastVirtualName(infname);
486 fl := SFSFileList(dvfn);
493 fo := TFileStream.Create(outfname, fmCreate);
495 for f := 0 to fl.Count-1 do
497 if length(fl[f].fName) = 0 then continue;
498 fs := SFSFileOpen(dvfn+'::'+fl[f].fPath+fl[f].fName);
499 newname := detectExt(fl[f].fPath, fl[f].fName, fs);
501 //fs := SFSFileOpen(dvfn+'::'+fl[f].fPath+fl[f].fName);
503 writeln('[', f+1, '/', fl.Count, ']: ', fl[f].fPath, newname, ' ', fs.size);
504 nfo := ZipOne(fo, fl[f].fPath+newname, fs);
505 SetLength(files, length(files)+1);
506 files[high(files)] := nfo;
508 writeCentralDir(fo, files);
512 DeleteFile(outfname);
514 if fo <> nil then fo.Free();