Debian Codegraph @

terps/tads/tads3/test/data/unpackt3.t

raw
1 #include <tads.h>
2 #include <file.h>
3
4 main(args)
5 {
6 if (args.length < 2)
7 {
8 "usage: unpackt3 &lt;t3-file&gt;\n";
9 return;
10 }
11
12 local fp = File.openRawFile(args[2], FileAccessRead);
13
14 local sig = fp.unpackBytes('a11 CC a32 a24');
15 "Format version: <<sig[3]>>.<<sig[2]>>\n";
16 "Timestamp: <<sig[5]>>\n";
17
18 for (local i = 1 ; ; ++i)
19 {
20 local blk = fp.unpackBytes('a4 L~ S');
21 "Block <<i>>: id=<<blk[1]>>, <<blk[2]>> bytes, flags=<<
22 toString(blk[3], 16)>>\n";
23
24 if (blk[1] == 'EOF ')
25 break;
26
27 local endpos = fp.getPos() + blk[2];
28
29 if (blk[1] == 'CPPG')
30 {
31 local pg = fp.unpackBytes('S L~ C');
32 if (pg[1] == 2)
33 {
34 local arr = fp.unpackBytes('b<<blk[2] - 7>>')[1];
35 local x = pg[3];
36 for (local j = 1 ; j <= arr.length() ; ++j)
37 arr[j] ^= x;
38
39 for (local j = 1 ; j <= arr.length() ; )
40 {
41 local v;
42 try
43 {
44 v = arr.unpackBytes(j, 'S/a @?');
45 }
46 catch (Exception exc)
47 {
48 break;
49 }
50 j += v[2];
51 if (rexSearch('[\x00-\x08\x0c\x0e-\x1f]', v[1]) == nil)
52 "\t<<rexReplace('[\n\r\t\b\\]', v[1], new function(m) {
53 return ['\n'->'\\n', '\r'->'\\b', '\t'->'\\t',
54 '\b'->'\\b', '\\'->'\\\\'][m];
55 })>>\n";
56 }
57 }
58 }
59
60 fp.setPos(endpos);
61 }
62 }
63