-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecode_xlog_nocrypt.js
More file actions
271 lines (235 loc) · 8.56 KB
/
decode_xlog_nocrypt.js
File metadata and controls
271 lines (235 loc) · 8.56 KB
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
const fs = require('fs');
const zlib = require('zlib');
const { parentPort, workerData } = require('worker_threads');
const MAGIC_NO_COMPRESS_START = 3;
const MAGIC_NO_COMPRESS_START1 = 6;
const MAGIC_NO_COMPRESS_NO_CRYPT_START = 8;
const MAGIC_COMPRESS_START = 4;
const MAGIC_COMPRESS_START1 = 5;
const MAGIC_COMPRESS_START2 = 7;
const MAGIC_COMPRESS_NO_CRYPT_START = 9;
const MAGIC_END = 0;
let gLastSeq;
function ReadIntFromBytes(bytesBuf, startPosition) {
return bytesBuf.readInt32LE(startPosition);
}
function ReadShortFromBytes(bytesBuf, startPosition) {
return bytesBuf.readInt16LE(startPosition);
}
// function ReadCharFromBytes(bytesBuf, startPosition) {
// return bytesBuf.readUint8(startPosition);
// }
function isGoodLogBuffer(rawBuffer, offset, count) {
if (offset === rawBuffer.length) {
return [true, ''];
}
const magicStart = rawBuffer[offset];
let cryptKeyLen;
if (MAGIC_NO_COMPRESS_START === magicStart
|| MAGIC_COMPRESS_START === magicStart
|| MAGIC_COMPRESS_START1 === magicStart) {
cryptKeyLen = 4;
} else if (MAGIC_COMPRESS_START2 === magicStart
|| MAGIC_NO_COMPRESS_START1 === magicStart
|| MAGIC_NO_COMPRESS_NO_CRYPT_START === magicStart
|| MAGIC_COMPRESS_NO_CRYPT_START === magicStart) {
cryptKeyLen = 64;
} else {
return [false, '_buffer[%d]:%d != MAGIC_NUM_START' % [offset, rawBuffer[offset]]];
}
const headerLen = 1 + 2 + 1 + 1 + 4 + cryptKeyLen;
if (offset + headerLen + 1 + 1 > rawBuffer.length) {
return [false, 'offset:%d > len(buffer):%d' % [offset, rawBuffer.length]];
}
const length = ReadIntFromBytes(rawBuffer, offset + headerLen - 4 - cryptKeyLen);
if (offset + headerLen + length + 1 > rawBuffer.length) {
return [false, 'log length:%d, end pos %d > len(buffer):%d' % [length, offset + headerLen + length + 1, rawBuffer.length]];
}
if (MAGIC_END !== rawBuffer[offset + headerLen + length]) {
return [false, 'log length:%d, buffer[%d]:%d != MAGIC_END' % [length, offset + headerLen + length, rawBuffer[offset + headerLen + length]]];
}
if (count <= 1) {
return [true, ''];
}
return isGoodLogBuffer(rawBuffer, offset + headerLen + length + 1, count - 1);
}
function getLogStartPos(buffer, count) {
let offset = 0;
while (true) {
if (offset >= buffer.length) {
break;
}
const magicStart = buffer[offset];
if (MAGIC_NO_COMPRESS_START === magicStart
|| MAGIC_NO_COMPRESS_START1 === magicStart
|| MAGIC_COMPRESS_START === magicStart
|| MAGIC_COMPRESS_START1 === magicStart
|| MAGIC_COMPRESS_START2 === magicStart
|| MAGIC_COMPRESS_NO_CRYPT_START === magicStart
|| MAGIC_NO_COMPRESS_NO_CRYPT_START === magicStart) {
if (isGoodLogBuffer(buffer, offset, count)[0]) {
return offset;
}
}
offset += 1;
}
return -1;
}
async function doDecompress(inputData) {
return new Promise((resolve) => {
// the decompressor
// important: must set a large chunkSize, otherwise the output is truncated
let decompressor = zlib.createInflateRaw({ chunkSize: 1024 * 1024 });
decompressor.on('error', (err) => {
if (err.code !== 'Z_BUF_ERROR') {
// Note that Z_BUF_ERROR is not fatal
console.log(err);
// do not terminate, try continue
resolve(`${err.code}, ${err.message}\n`);
decompressor.removeAllListeners();
decompressor = null;
}
}).on('data', (chunk) => {
// console.log(chunk.toString());
resolve(chunk);
decompressor.removeAllListeners();
decompressor = null;
}).on('close', () => {
decompressor.removeAllListeners();
decompressor = null;
});
decompressor.write(inputData);
}).catch((e) => {
console.log(e);
});
}
async function decodeBuffer(rawBuffer, offset, outStream) {
return new Promise(async (resolve) => {
if (offset >= rawBuffer.length) {
resolve(-1);
return;
}
const ret = isGoodLogBuffer(rawBuffer, offset, 1);
if (!ret[0]) {
const fixPos = getLogStartPos(rawBuffer.slice(offset), 1);
if (fixPos === -1) {
resolve(-1);
return;
}
outStream.write(`[F]decode_xlog_nocrypt.js decode error len=${fixPos}, result:${ret[1]} \n`);
offset += fixPos;
}
const magicStart = rawBuffer[offset];
let cryptKeyLen;
if (MAGIC_NO_COMPRESS_START === magicStart
|| MAGIC_COMPRESS_START === magicStart
|| MAGIC_COMPRESS_START1 === magicStart) {
cryptKeyLen = 4;
} else if (MAGIC_COMPRESS_START2 === magicStart
|| MAGIC_NO_COMPRESS_START1 === magicStart
|| MAGIC_NO_COMPRESS_NO_CRYPT_START === magicStart
|| MAGIC_COMPRESS_NO_CRYPT_START === magicStart) {
cryptKeyLen = 64;
} else {
outStream.write(`in DecodeBuffer _buffer[${offset}]: ${magicStart} != MAGIC_NUM_START`);
resolve(-1);
return;
}
const headerLen = 1 + 2 + 1 + 1 + 4 + cryptKeyLen;
const length = ReadIntFromBytes(rawBuffer, offset + headerLen - 4 - cryptKeyLen);
const seq = ReadShortFromBytes(rawBuffer, offset + headerLen - 4 - cryptKeyLen - 2 - 2);
// const beginHour = ReadCharFromBytes(rawBuffer, offset + headerLen - 4 - cryptKeyLen - 1 - 1);
// const endHour = ReadCharFromBytes(rawBuffer, offset + headerLen - 4 - cryptKeyLen - 1);
if (seq !== 0 && seq !== 1 && gLastSeq !== 0 && seq !== (gLastSeq + 1)) {
outStream.write(`[F]decode_xlog_nocrypt.js log seq:${gLastSeq + 1}-${seq - 1} is missing\n`);
}
if (seq !== 0) {
gLastSeq = seq;
}
let tmpBuffer = rawBuffer.slice(offset + headerLen, offset + headerLen + length);
if (MAGIC_NO_COMPRESS_START1 === magicStart
|| MAGIC_COMPRESS_START2 === magicStart) {
// console.log('use wrong decode script');
outStream.write('use wrong decode script\n');
} else if (MAGIC_COMPRESS_START === magicStart
|| MAGIC_COMPRESS_NO_CRYPT_START === magicStart) {
// give it data to inflate
const decompressedData = await doDecompress(tmpBuffer);
outStream.write(decompressedData);
} else if (MAGIC_COMPRESS_START1 === magicStart) {
let dataToDecompress = Buffer.alloc(0);
while (tmpBuffer.length > 0) {
const singleLogLen = ReadShortFromBytes(tmpBuffer, 0);
if (singleLogLen <= 0) break;
dataToDecompress = Buffer.concat([dataToDecompress, tmpBuffer.slice(2, singleLogLen + 2)]);
tmpBuffer = tmpBuffer.slice(singleLogLen + 2, tmpBuffer.length);
}
// give it data to inflate
const decompressedData = await doDecompress(dataToDecompress);
outStream.write(decompressedData);
} else if (MAGIC_NO_COMPRESS_NO_CRYPT_START === magicStart) {
outStream.write(tmpBuffer);
} else {
// nothing
console.log(`unsupported magic start: ${magicStart}`);
outStream.write(`unsupported magic start: ${magicStart}`);
}
resolve(offset + headerLen + length + 1);
});
}
async function handleRawBuffer(data, outputPath) {
return new Promise(async (resolve) => {
// The stream to write
const writeStream = fs.createWriteStream(outputPath);
let startPos = getLogStartPos(data, 2);
if (startPos === -1) {
console.error(`invalid xlog file: ${outputPath}!`);
writeStream.write('invalid xlog file!');
resolve(false);
}
do {
// eslint-disable-next-line no-await-in-loop
startPos = await decodeBuffer(data, startPos, writeStream);
} while (startPos !== -1);
// close stream
writeStream.end();
writeStream.close();
// console.log('write stream has closed!');
resolve(true);
});
}
async function parseFileAsync(filePath, outputPath) {
// console.log(`start parse: ${filePath}, out: ${outputPath}`);
// first, read file
return new Promise((resolve) => {
fs.readFile(filePath, async (err, data) => {
if (err) {
console.log(err);
} else {
gLastSeq = 0;
const result = await handleRawBuffer(data, outputPath);
// console.log(`parse result = ${result}`);
if (result) {
fs.unlinkSync(filePath); // remove original file
}
}
resolve();
});
});
}
// test code
// const args = process.argv.slice(2);
// if (args.length < 1) {
// console.error('Error: path param not found!');
// process.exit(-1);
// }
//
// console.log('Path list is:', args);
// decodeFileOrDir(args[0]).then(() => console.log('process end!!!!')).
// catch((e) => { console.log(e); });
parseFileAsync(workerData, `${workerData}.log`).then(() => {
console.log(`decode ${workerData} finish.`);
// use worker threads
parentPort.postMessage(1);
});
module.exports = parseFileAsync;