From 66d14a1d5b4557143266c0eac99f5fa8d9297c42 Mon Sep 17 00:00:00 2001 From: C-3PO Date: Thu, 5 Jul 2018 13:13:05 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20Improve=20streamToString=20perfo?= =?UTF-8?q?rmance?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ssn/decryption/decryptChunk.ts | 6 +++++- src/ssn/extractFileStream.ts | 4 +++- src/ssn/streams/streamToString.ts | 8 ++++---- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/ssn/decryption/decryptChunk.ts b/src/ssn/decryption/decryptChunk.ts index 3d194b3..b625c70 100644 --- a/src/ssn/decryption/decryptChunk.ts +++ b/src/ssn/decryption/decryptChunk.ts @@ -27,11 +27,15 @@ export default function getDecryptor([key0, key1, key2]: [number, number, number const dvOut = new DataView(decryptedChunk.buffer); for (let i = 0; i < encryptedChunk.length; i += 1) { - //read and decrypt byte + //read byte let curChar = encryptedChunk.readUInt8(i); + + //decrypt byte const keyPart = (key2 | 2) & 0xFFFF; const decryptedByte = (keyPart * (keyPart ^ 1)) >>> 8; curChar ^= decryptedByte & 0xFF; + + //write byte if (position + i < 12) { //do nothing } else if (position + i >= 12 && position < 12) { diff --git a/src/ssn/extractFileStream.ts b/src/ssn/extractFileStream.ts index 085541b..1b4f267 100644 --- a/src/ssn/extractFileStream.ts +++ b/src/ssn/extractFileStream.ts @@ -25,7 +25,9 @@ export default function extractFileStream(file: ISsnFileEntry, inputStream: stre const localExtraSize = localFileHeader.readUInt16LE(28); //skip local file name and extra field - inputStream.read(localFilenameSize + localExtraSize); + if (localFilenameSize + localExtraSize > 0) { + inputStream.read(localFilenameSize + localExtraSize); + } //------------------------------------------------- diff --git a/src/ssn/streams/streamToString.ts b/src/ssn/streams/streamToString.ts index 5f77126..3917170 100644 --- a/src/ssn/streams/streamToString.ts +++ b/src/ssn/streams/streamToString.ts @@ -5,17 +5,17 @@ const decoder = new TextDecoder('utf-8'); export default function streamToString(inputStream: stream.Readable): Promise { return new Promise((resolve, reject) => { - let outputString = ''; + const stringParts: string[] = []; //Convert chunks to string inputStream.on('data', (chunk: Buffer) => { - outputString += decoder.decode(chunk, { stream: true }); + stringParts.push(decoder.decode(chunk, { stream: true })); }); //Output final string inputStream.on('end', () => { - outputString += decoder.decode(); - resolve(outputString); + stringParts.push(decoder.decode()); + resolve(stringParts.join('')); }); //Exit on error