Skip to content

Commit

Permalink
Chunk size bytes in compressed data
Browse files Browse the repository at this point in the history
  • Loading branch information
iqqmuT committed Sep 2, 2019
1 parent ab24df3 commit 0998d4b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
26 changes: 18 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,24 +296,34 @@ function askMoney(rl, saveFile, result, playerNum) {
});
}

// Compresses given data and adds markers after every 64kB chunk.
// Compresses given data and adds length markers after every (max 64kB) chunk.
function compressData(data) {
// use deflate algorithm
const compressed = zlib.deflateSync(data, {
finishFlush: zlib.constants.Z_SYNC_FLUSH,
});

let pos = 0;
const chunks = [];
chunks.push(END_UNCOMPRESSED);

const chunkSize = 64 * 1024;
let pos = 0;
while (pos + chunkSize < compressed.length) {
chunks.push(compressed.slice(pos, pos + chunkSize));
pos += chunkSize;
chunks.push(END_UNCOMPRESSED);
const addLengthBytes = (len) => {
const buf = Buffer.alloc(4);
buf.writeUInt32LE(len);
chunks.push(buf);
};

// add data in chunks
const maxChunkSize = 64 * 1024;
while (pos + maxChunkSize < compressed.length) {
addLengthBytes(maxChunkSize);
chunks.push(compressed.slice(pos, pos + maxChunkSize));
pos += maxChunkSize;
}

// add last chunk
addLengthBytes(compressed.length - pos);
chunks.push(compressed.slice(pos));

return Buffer.concat(chunks);
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "civ6-cheat",
"version": "0.4.0",
"version": "0.5.0",
"description": "Cheat Tool for Civilization VI.",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 0998d4b

Please sign in to comment.