From 213fcf91f751e20de31ff50b879be2c75b407747 Mon Sep 17 00:00:00 2001 From: C-3PO Date: Thu, 5 Jul 2018 03:01:45 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20decrypt=20chunk?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ssn/decryption/decryptChunk.ts | 4 +--- src/ssn/extractFileStream.ts | 5 ++++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/ssn/decryption/decryptChunk.ts b/src/ssn/decryption/decryptChunk.ts index 5289c53..5079f64 100644 --- a/src/ssn/decryption/decryptChunk.ts +++ b/src/ssn/decryption/decryptChunk.ts @@ -1,14 +1,12 @@ import updateKeys from './lib/updateKeys'; export default function decryptChunk(encryptedChunk: Buffer, [key0, key1, key2]: [number, number, number]): {decryptedChunk: Buffer, keys: [number, number, number]} { - const dv = new DataView(encryptedChunk.buffer); - const decryptedChunk = Buffer.alloc(encryptedChunk.length); const dvOut = new DataView(decryptedChunk.buffer); for (let i = 0; i < encryptedChunk.length; i += 1) { //read and decrypt byte - let curChar = dv.getUint8(i); + let curChar = encryptedChunk.readUInt8(i); const keyPart = (key2 | 2) & 0xFFFF; const decryptedByte = (keyPart * (keyPart ^ 1)) >>> 8; curChar ^= decryptedByte & 0xFF; diff --git a/src/ssn/extractFileStream.ts b/src/ssn/extractFileStream.ts index 8b6689b..a934d85 100644 --- a/src/ssn/extractFileStream.ts +++ b/src/ssn/extractFileStream.ts @@ -16,7 +16,7 @@ export default function extractFileStream(file: ISsnFileEntry, inputStream: stre //Local file header signature const magic = localFileHeader.readUInt32LE(0); if (magic !== 0x04034B50) { - throw new Error(`Local file header had wrong magic; expected 0x04034B50 but got 0x${magic.toString(16).padStart(8, '0')}`); + throw new Error(`Local file header had wrong magic; expected 0x04034B50 but got 0x${magic.toString(16).padStart(8, '0')}.`); } //All fields in the local file header are copies of the central file header, so we can skip them. //FIXME: Maybe we should actually read these fields to verify that they are identical? @@ -44,6 +44,9 @@ export default function extractFileStream(file: ISsnFileEntry, inputStream: stre //pipe into decompression const decompressTransform = zlib.createInflateRaw(); + decompressTransform.on('error', (error) => { + throw new Error(`Error during decompression: ${error.message}`); + }); curStream.pipe(decompressTransform); curStream = decompressTransform;