This repository was archived by the owner on Jan 31, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblte.js
More file actions
54 lines (51 loc) · 1.8 KB
/
blte.js
File metadata and controls
54 lines (51 loc) · 1.8 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
const assert = require('node:assert');
const md5 = require('md5');
const zlib = require('node:zlib');
function parseDataChunk(buf) {
const encodingMode = buf.readUInt8(0);
switch (encodingMode) {
case 0x4e: // N
return buf.subarray(1);
case 0x5a: // Z
return zlib.inflateSync(buf.subarray(1));
default:
assert(false, "BLTE encoding mode");
}
}
function parseBLTE(buf, hash) {
assert.strictEqual(buf.readUInt32BE(0), 0x424c5445, "BLTE magic");
const headerSize = buf.readUInt32BE(4);
assert.strictEqual(md5(buf.subarray(0, headerSize)), hash, "BLTE checksum");
if (headerSize == 0) {
return parseDataChunk(buf.subarray(8));
} else {
assert.strictEqual(buf.readUInt8(8), 0xf, "BLTE flags");
const chunkCount = buf.readUInt32BE(8) & 0x00ffffff;
assert.strictEqual(chunkCount * 24 + 12, headerSize, "BLTE header");
const chunks = [];
for (let i = 0; i < chunkCount; ++i) {
const p = i * 24 + 12;
chunks.push({
compressedSize: buf.readUInt32BE(p),
decompressedSize: buf.readUInt32BE(p + 4),
checksum: buf.subarray(p + 8, p + 24).toString('hex'),
});
}
const compressedSize = chunks.reduce((t, c) => t + c.compressedSize, 0);
assert.strictEqual(buf.length - headerSize, compressedSize, "BLTE size");
let cursor = headerSize;
const buffers = [];
for (const chunk of chunks) {
const cbuf = buf.subarray(cursor, cursor + chunk.compressedSize);
assert.strictEqual(md5(cbuf), chunk.checksum, "BLTE chunk checksum");
const data = parseDataChunk(cbuf);
assert.strictEqual(data.length, chunk.decompressedSize, "BLTE decompressed size");
buffers.push(data);
cursor += chunk.compressedSize;
}
return Buffer.concat(buffers);
}
}
module.exports = {
parse: parseBLTE,
};