Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Azq2 committed Aug 21, 2024
1 parent c68c718 commit 75801b9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
9 changes: 5 additions & 4 deletions src/exe.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ function extractFromServiceExe(buffer, version) {
size = readBits(buffer.slice(offset));
out[3] = { size, offset: offset - size };

let cipherLength = buffer.length - (out[0].size + out[1].size + out[2].size + out[3].size + verions[version][0]);
debug(`cipherLength=${cipherLength}`);
let cipherKeyLength = buffer.length - (out[0].size + out[1].size + out[2].size + out[3].size + verions[version][1]);
let cipherKey = buffer.subarray(0, cipherKeyLength);
debug(`cipherKeyLength=${cipherKeyLength}`);

// Decrypt blocks
let payloads = [];
Expand All @@ -96,8 +97,8 @@ function extractFromServiceExe(buffer, version) {

if (out[i].size) {
debug(sprintf("BLOCK %08X %08X", out[i].offset, out[i].size));
let payload = buffer.subarray(out[i].offset, out[i].offset + out[i].size);
applyXor(payload, buffer.slice(0, cipherLength));
let payload = Buffer.from(buffer.subarray(out[i].offset, out[i].offset + out[i].size));
applyXor(payload, cipherKey);
payloads[i] = payload;
}
}
Expand Down
14 changes: 10 additions & 4 deletions src/xbz.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ export function isXbz(buffer) {

export function parseXbz(buffer) {
let xbzFormat = detectXbzFormat(buffer);
if (!xbzFormat)
return null;

debug("XBZ version: " + xbzFormat.version);
debug("XBZ signed: " + xbzFormat.signed);
Expand Down Expand Up @@ -177,18 +179,20 @@ function decodeXbzWriteFrame(version, buffer, offset) {
let addr = (buffer.readUInt8(0) << 16) | (buffer.readUInt8(1) << 8) | (buffer.readUInt8(2));
let size = buffer.readUInt8(3);
let chk = buffer.readUInt8(4 + size);
let actualChk = calcChecksum(buffer, 4 + size);

if (chk != calcChecksum(buffer, 4 + size))
throw new Error(`Invalid chk: ${chk}`);
if (chk != actualChk)
throw new Error(`Invalid chk: ${sprintf("%08X %04X CHK:%02X != %02X", addr, size, chk, actualChk)} at ${buffer.byteOffset}`);

return [ 4 + 1 + size, { addr, size, offset: offset + 4 } ];
} else if (version == 32) {
let addr = buffer.readUInt32BE(0);
let size = buffer.readUInt16BE(4);
let chk = buffer.readUInt8(6 + size);
let actualChk = calcChecksum(buffer, 6 + size);

if (chk != calcChecksum(buffer, 6 + size))
throw new Error(`Invalid chk: ${chk}`);
if (chk != actualChk)
throw new Error(`Invalid chk: ${sprintf("%08X %04X CHK:%02X != %02X", addr, size, chk, actualChk)} at ${buffer.byteOffset}`);

return [ 6 + 1 + size, { addr, size, offset: offset + 6 } ];
}
Expand Down Expand Up @@ -236,6 +240,8 @@ function decodeXbzFrame(frameType, version, buffer) {

function calcChecksum(buffer, size) {
let chk = 0;
if (size > buffer.length)
throw new Error(`Truncated file! [${size} > ${buffer.length}]`);
for (let i = 0; i < size; i++)
chk ^= buffer[i];
return chk;
Expand Down

0 comments on commit 75801b9

Please sign in to comment.