1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
|
import std.stream;
import std.stdio;
import std.c.stdlib;
import std.system;
import std.c.string;
import std.c.time;
import std.regexp;
import std.string;
struct Header
{
align(4)
{
uint id;
uint chunksize;
uint datasize;
uint next;
}
};
struct ChunkInfo
{
uint next;
uint datasize;
long fileoffset;
};
class Chunk
{
static bool differ(ChunkInfo info1, ChunkInfo info2, Stream s)
{
if (memcmp(&info1, &info2, 8) !=0) return true;
if (data(info1, s) != data(info2, s)) return true;
return false;
}
static char[] data(ChunkInfo info, Stream s)
{
long p = s.position();
s.seekSet(info.fileoffset);
char[] o = new char[info.datasize];
o.length = s.readBlock(cast(void*)o, info.datasize);
s.seekSet(p);
return o;
}
}
class ChainFinder
{
private:
Stream s;
ChunkInfo[][uint] infos;
bool[uint] first;
int walk(uint id, int delegate(inout Message) dg)
{
auto m = new Message(s);
return walk(id, &infos[id], dg, m, 1);
}
int walk(uint id, ChunkInfo[]* infosp, int delegate(inout Message) dg, Message m, uint combinations)
{
if (combinations > 16)
{
chainstats.dropped++;
return 0;
}
foreach(ChunkInfo info; *infosp)
{
m.push(id, info);
bool seen = m.seen(info.next);
ChunkInfo[]* nextp = info.next in infos;
if (nextp && (!seen))
{
walk(info.next, nextp, dg, m, combinations * nextp.length);
}
else
{
if (seen) chainstats.loops++;
if (m.broken()) chainstats.broken++;
chainstats.count++;
int result = dg(m);
if (result) return result;
}
m.pop(id);
}
return 0;
}
public:
struct ChunkStatistics
{
uint count;
uint duplicates;
uint id_duplicates;
};
struct ChainStatistics
{
uint count;
uint loops;
uint dropped;
uint broken;
uint progress_total;
uint progress_current;
};
ChunkStatistics chunkstats;
ChainStatistics chainstats;
this(Stream s)
{
this.s = s;
}
void add(uint id, ChunkInfo info)
{
chunkstats.count++;
if (id in infos) {
foreach (ChunkInfo info2; infos[id])
{
if (!Chunk.differ(info, info2, s))
{
chunkstats.duplicates++;
return;
}
}
}
else
{
ChunkInfo[] tmp;
infos[id] = tmp;
}
int len = (infos[id].length = infos[id].length + 1);
infos[id][len-1] = info;
if (len > 1) chunkstats.id_duplicates++;
if (!(id in first)) first[id] = true;
first[info.next] = false;
}
int opApply(int delegate(inout Message) dg)
{
uint[] ids = first.keys;
chainstats.progress_total = ids.length+1;
chainstats.progress_current = 1;
foreach(uint id; ids)
{
if (first[id])
{
int result = walk(id, dg);
if (result) return result;
}
chainstats.progress_current++;
}
return 0;
}
};
class Message
{
private:
ChunkInfo infos[];
bool[uint] seen_tab;
Stream s;
public:
this(Stream s)
{
this.s = s;
}
void push(uint id, ChunkInfo info)
{
infos.length = infos.length + 1;
infos[infos.length-1] = info;
seen_tab[id] = true;
}
void pop(uint id)
{
seen_tab.remove(id);
infos.length = infos.length - 1;
}
bool seen(uint id)
{
return (id in seen_tab != null);
}
bool broken()
{
return (infos[infos.length-1].next != 0);
}
int opApply(int delegate(inout char[]) dg)
{
char[] line = "";
foreach (ChunkInfo info; infos)
{
int index;
int index2;
char[] data = Chunk.data(info, s);
while ((index2 = 1+std.string.find(data[index..length], 10)) != 0)
{
line ~= data[index..index+index2];
int result = dg(line);
if (result) return result;
line.length = 0;
index += index2;
}
line ~= data[index..length];
}
int result = dg(line);
if (result) return result;
return 0;
}
};
int main (char[][] args)
{
if (args.length != 2)
{
fwritef(stderr, "Usage: %s input.dbx >output.mbox\n", args[0]);
exit(1);
}
auto input = new EndianStream(new BufferedFile(args[1], FileMode.In), Endian.LittleEndian);
auto cf = new ChainFinder(input);
void chunkstats() {
fwritef(stderr,"Bytes: %d; Chunks: %d; Duplicates: %d; Duplicate IDs: %d; %d%% done\r",
input.position(), cf.chunkstats.count, cf.chunkstats.duplicates, cf.chunkstats.id_duplicates, (input.position()+1)*100/(input.size+1));
}
time_t time1;
void every(uint seconds, lazy void func)
{
time_t time2 = time(null);
if (time2 - time1 >= seconds)
{
time1 = time2;
func();
}
}
fwritef(stderr,"Pass 1/2...\n");
while(true)
{
Header h;
if (input.readBlock(&h, 16) != 16) break;
input.fixBlockBO(&h, 4, 4);
with(h)
{
if (
(chunksize == 512) &&
(datasize <= chunksize) &&
(datasize > 0) &&
(id != 0) &&
((next == 0) || (datasize == chunksize))
)
{
ChunkInfo info;
info.next = h.next;
info.datasize = h.datasize;
info.fileoffset = input.position();
input.seekCur(chunksize);
cf.add(h.id, info);
every(2, chunkstats());
}
else input.seekCur(-12);
}
}
chunkstats();
fwritef(stderr,"\n");
void chainstats() {
fwritef(stderr,"Chains: %d; Broken: %d; Loops: %d; Dropped: %d; %d%% done\r",
cf.chainstats.count, cf.chainstats.broken, cf.chainstats.loops, cf.chainstats.dropped, cf.chainstats.progress_current*100/cf.chainstats.progress_total);
}
fwritef(stderr,"Pass 2/2...\n");
foreach (Message m; cf)
{
writef("From unknown@unknown.invalid Mon Jan 1 00:00:00 1970\r\n");
foreach (char[] line; m)
{
line = sub(line, "^>*From ", ">$&");
fwrite(cast(void*)line, line.length, char.sizeof, stdout);
}
writef("\r\n\r\n");
every(2, chainstats());
}
chainstats();
fwritef(stderr,"\n");
return 0;
};
|