DEADSOFTWARE

wadcvt: fixed compressor bug
[d2df-sdl.git] / src / sfs / wadcvt.dpr
1 {$MODE OBJFPC}
2 {$IFDEF WINDOWS}
3 {$APPTYPE CONSOLE}
4 {$ENDIF}
5 {$DEFINE UTFEXTRA}
6 program __wadcvt__;
8 uses
9 SysUtils,
10 Classes,
11 utils in '../shared/utils.pas',
12 xstreams in '../shared/xstreams.pas',
13 crc,
14 sfs,
15 sfsPlainFS,
16 sfsZipFS,
17 paszlib;
20 procedure processed (count: Cardinal);
21 begin
22 //writeln(' read ', count, ' bytes');
23 end;
26 // returs crc
27 function zpack (ds: TStream; ss: TStream): LongWord;
28 const
29 IBSize = 65536;
30 OBSize = 65536;
31 var
32 zst: TZStream;
33 ib, ob: PByte;
34 err: Integer;
35 rd: Integer;
36 eof: Boolean;
37 crc: LongWord;
38 begin
39 result := 0;
40 crc := crc32(0, nil, 0);
41 GetMem(ib, IBSize);
42 GetMem(ob, OBSize);
43 ss.position := 0;
44 try
45 zst.next_out := ob;
46 zst.avail_out := OBSize;
47 zst.next_in := ib;
48 zst.avail_in := 0;
49 err := deflateInit2(zst, Z_BEST_COMPRESSION, Z_DEFLATED, -15, 9, 0);
50 if err <> Z_OK then raise Exception.Create(zerror(err));
51 try
52 eof := false;
53 repeat
54 if zst.avail_in = 0 then
55 begin
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');
60 eof := (rd = 0);
61 if rd <> 0 then begin crc := crc32(crc, Pointer(ib), rd); result := crc; end;
62 zst.next_in := ib;
63 zst.avail_in := rd;
64 end;
65 // now process the whole input
66 while zst.avail_in > 0 do
67 begin
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
71 begin
72 //writeln(' written ', OBSize-zst.avail_out, ' bytes');
73 ds.writeBuffer(ob^, OBSize-zst.avail_out);
74 zst.next_out := ob;
75 zst.avail_out := OBSize;
76 end;
77 end;
78 until eof;
79 // do leftovers
80 while true do
81 begin
82 zst.avail_in := 0;
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
86 begin
87 //writeln(' .written ', OBSize-zst.avail_out, ' bytes');
88 ds.writeBuffer(ob^, OBSize-zst.avail_out);
89 zst.next_out := ob;
90 zst.avail_out := OBSize;
91 end;
92 if err <> Z_OK then break;
93 end;
94 // succesfully flushed?
95 if (err <> Z_STREAM_END) then raise Exception.Create(zerror(err));
96 finally
97 deflateEnd(zst);
98 end;
99 finally
100 FreeMem(ob);
101 FreeMem(ib);
102 end;
103 end;
107 procedure TProg.putStr (const s: AnsiString; newline: Boolean=false);
108 begin
109 write(#13, s);
110 while lastlen > length(s) do
111 begin
112 write(' ');
113 Dec(lastlen);
114 end;
115 if newline then
116 begin
117 writeln;
118 lastlen := 0;
119 end
120 else
121 begin
122 lastlen := length(s);
123 end;
124 end;
126 procedure TProg.onProgress (sender: TObject; const percent: double);
127 var
128 prc: Integer;
129 begin
130 prc := trunc(percent*100.0);
131 putStr(Format('compressing %-33s %3d%%', [lastname, prc]));
132 end;
134 procedure TProg.onFileStart (sender: TObject; const fileName: AnsiString);
135 begin
136 lastname := fileName;
137 putStr(Format('compressing %-33s %3d%%', [lastname, 0]));
138 end;
140 procedure TProg.onFileEnd (sender: TObject; const ratio: double);
141 begin
142 putStr(Format('compressed %-33s %f', [lastname, ratio]), true);
143 end;
147 // returns new file name
148 function detectExt (fpath, fname: AnsiString; fs: TStream): AnsiString;
149 var
150 buf: PChar;
151 buflen: Integer;
152 f: Integer;
153 st: string[24];
154 begin
155 result := fname;
156 if length(ExtractFileExt(fname)) <> 0 then exit;
157 if fs.size < 16 then exit;
158 buflen := Integer(fs.size);
159 GetMem(buf, buflen);
160 try
161 fs.ReadBuffer(buf^, buflen);
162 // xm
163 Move(buf^, (PChar(@st[1]))^, 16);
164 st[0] := #16;
165 if (st = 'Extended Module:') then
166 begin
167 result := result+'.xm';
168 exit;
169 end;
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
172 begin
173 result := result+'.wad';
174 exit;
175 end;
176 if (buf[0] = 'M') and (buf[1] = 'A') and (buf[2] = 'P') and (buf[3] = #$1) then
177 begin
178 result := result+'.dfmap';
179 exit;
180 end;
181 if (buf[0] = 'M') and (buf[1] = 'T') and (buf[2] = 'h') and (buf[3] = 'd') then
182 begin
183 result := result+'.mid';
184 exit;
185 end;
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
188 begin
189 result := result+'.wav';
190 exit;
191 end;
192 // mp3 (stupid hack)
193 for f := 0 to 128-6 do
194 begin
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
198 begin
199 result := result+'.mp3';
200 exit;
201 end;
202 end;
203 // more mp3 hacks
204 if (buf[0] = 'I') and (buf[1] = 'D') and (buf[2] = '3') and (buf[3] <= #4) then
205 begin
206 result := result+'.mp3';
207 exit;
208 end;
209 if buflen > 128 then
210 begin
211 if (buf[buflen-128] = 'T') and (buf[buflen-127] = 'A') and (buf[buflen-126] = 'G') then
212 begin
213 result := result+'.mp3';
214 exit;
215 end;
216 end;
217 // targa (stupid hack; this "signature" is not required by specs)
218 if buflen >= 18 then
219 begin
220 Move((buf+buflen-18)^, (PChar(@st[1]))^, 16);
221 st[0] := #16;
222 if st = 'TRUEVISION-XFILE' then
223 begin
224 result := result+'.tga';
225 exit;
226 end;
227 end;
228 finally
229 FreeMem(buf);
230 end;
231 end;
234 type
235 TFileInfo = class
236 public
237 name: AnsiString;
238 pkofs: Int64; // offset of file header
239 size: Int64;
240 pksize: Int64;
241 crc: LongWord;
242 method: Word;
244 constructor Create ();
245 end;
247 constructor TFileInfo.Create ();
248 begin
249 name := '';
250 pkofs := 0;
251 size := 0;
252 pksize := 0;
253 crc := crc32(0, nil, 0);
254 method := 0;
255 end;
258 const
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
268 );
271 function toUtf8 (const s: AnsiString): AnsiString;
272 var
273 uc: PUnicodeChar;
274 xdc: PChar;
275 pos, f: Integer;
276 begin
277 GetMem(uc, length(s)*8);
278 GetMem(xdc, length(s)*8);
279 try
280 FillChar(uc^, length(s)*8, 0);
281 FillChar(xdc^, length(s)*8, 0);
282 pos := 0;
283 for f := 1 to length(s) do
284 begin
285 if ord(s[f]) < 128 then
286 uc[pos] := UnicodeChar(ord(s[f]))
287 else
288 uc[pos] := UnicodeChar(uni2wint[ord(s[f])]);
289 Inc(pos);
290 end;
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);
296 finally
297 FreeMem(xdc);
298 FreeMem(uc);
299 end;
300 end;
302 // this will write "extra field length" and extra field itself
303 {$IFDEF UTFEXTRA}
304 const UtfFlags = 0;
306 type
307 TByteArray = array of Byte;
309 function buildUtfExtra (fname: AnsiString): TByteArray;
310 var
311 crc: LongWord;
312 fu: AnsiString;
313 sz: Word;
314 begin
315 fu := toUtf8(fname);
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');
321 Dec(sz, 4);
322 result[2] := sz and $ff;
323 result[3] := (sz shr 8) and $ff;
324 result[4] := 1;
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));
330 end;
331 {$ELSE}
332 const UtfFlags = (1 shl 10); // bit 11
333 {$ENDIF}
335 function ZipOne (ds: TStream; fname: AnsiString; st: TStream): TFileInfo;
336 var
337 oldofs, nfoofs, pkdpos: Int64;
338 sign: packed array [0..3] of Char;
339 {$IFDEF UTFEXTRA}
340 ef: TByteArray;
341 {$ENDIF}
342 begin
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;
347 {$IFDEF UTFEXTRA}
348 result.name := fname;
349 ef := buildUtfExtra(result.name);
350 {$ELSE}
351 result.name := toUtf8(fname);
352 {$ENDIF}
353 // write local header
354 sign := 'PK'#3#4;
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
366 {$IFDEF UTFEXTRA}
367 writeInt(ds, Word(length(ef))); // extra field length
368 {$ELSE}
369 writeInt(ds, Word(0)); // extra field length
370 {$ENDIF}
371 ds.writeBuffer(fname[1], length(fname));
372 {$IFDEF UTFEXTRA}
373 if length(ef) > 0 then ds.writeBuffer(ef[0], length(ef));
374 {$ENDIF}
375 // now write packed data
376 if result.size > 0 then
377 begin
378 pkdpos := ds.position;
379 st.position := 0;
380 result.crc := zpack(ds, st);
381 result.pksize := ds.position-pkdpos;
382 // fix header
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;
388 end;
389 end;
392 procedure writeCentralDir (ds: TStream; files: array of TFileInfo);
393 var
394 cdofs, cdend: Int64;
395 sign: packed array [0..3] of Char;
396 f: Integer;
397 {$IFDEF UTFEXTRA}
398 ef: TByteArray;
399 {$ENDIF}
400 begin
401 cdofs := ds.position;
402 for f := 0 to high(files) do
403 begin
404 {$IFDEF UTFEXTRA}
405 ef := buildUtfExtra(files[f].name);
406 {$ENDIF}
407 sign := 'PK'#1#2;
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
419 {$IFDEF UTFEXTRA}
420 writeInt(ds, Word(length(ef))); // extra field length
421 {$ELSE}
422 writeInt(ds, Word(0)); // extra field length
423 {$ENDIF}
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));
430 {$IFDEF UTFEXTRA}
431 if length(ef) > 0 then ds.writeBuffer(ef[0], length(ef));
432 {$ENDIF}
433 end;
434 cdend := ds.position;
435 // write end of central dir
436 sign := 'PK'#5#6;
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
445 end;
448 var
449 fs, fo: TStream;
450 fl: TSFSFileList;
451 f: Integer;
452 infname: AnsiString;
453 outfname: AnsiString;
454 dvfn: AnsiString;
455 newname: AnsiString;
456 files: array of TFileInfo;
457 nfo: TFileInfo;
458 begin
459 if ParamCount() < 1 then
460 begin
461 WriteLn('usage: wadcvt file.wad');
462 Halt(1);
463 end;
465 infname := ParamStr(1);
466 if not StrEquCI1251(ExtractFileExt(infname), '.wad') and not StrEquCI1251(ExtractFileExt(infname), '.dfwad') then
467 begin
468 writeln('wtf?!');
469 Halt(1);
470 end;
472 if ParamCount() > 1 then
473 begin
474 outfname := ParamStr(2);
475 end
476 else
477 begin
478 outfname := ChangeFileExt(infname, '.pk3');
479 end;
481 if not SFSAddDataFile(infname) then begin WriteLn('shit!'); Halt(1); end;
482 dvfn := SFSGetLastVirtualName(infname);
484 files := nil;
486 fl := SFSFileList(dvfn);
487 if fl = nil then
488 begin
489 writeln('wtf?!');
490 Halt(1);
491 end;
493 fo := TFileStream.Create(outfname, fmCreate);
494 try
495 for f := 0 to fl.Count-1 do
496 begin
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);
500 //fs.Free();
501 //fs := SFSFileOpen(dvfn+'::'+fl[f].fPath+fl[f].fName);
502 fs.position := 0;
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;
507 end;
508 writeCentralDir(fo, files);
509 except
510 fo.Free();
511 fo := nil;
512 DeleteFile(outfname);
513 end;
514 if fo <> nil then fo.Free();
515 end.