From 6aba608f3686d9fd023c3c4c5b7cd6169e9d46db Mon Sep 17 00:00:00 2001 From: sanex3339 Date: Sun, 25 Dec 2016 15:00:45 +0300 Subject: [PATCH] slightly improved speed with rc4 encoding --- dist/index.js | 29 ++++++++---- .../replacers/StringLiteralReplacer.ts | 45 ++++++++++++++----- 2 files changed, 53 insertions(+), 21 deletions(-) diff --git a/dist/index.js b/dist/index.js index d5518038..312da0df 100644 --- a/dist/index.js +++ b/dist/index.js @@ -5852,6 +5852,7 @@ var StringLiteralReplacer = StringLiteralReplacer_1 = function (_AbstractReplace var _this = (0, _possibleConstructorReturn3.default)(this, (StringLiteralReplacer.__proto__ || (0, _getPrototypeOf2.default)(StringLiteralReplacer)).call(this, options)); _this.stringLiteralCache = new _map2.default(); + _this.stringLiteralHexadecimalIndexCache = new _map2.default(); _this.customNodeGroupStorage = customNodeGroupStorage; _this.stringArrayStorage = stringArrayStorage; return _this; @@ -5876,6 +5877,24 @@ var StringLiteralReplacer = StringLiteralReplacer_1 = function (_AbstractReplace this.stringLiteralCache.set(nodeValue, result); return result; } + }, { + key: "getArrayHexadecimalIndex", + value: function getArrayHexadecimalIndex(value) { + if (this.stringLiteralHexadecimalIndexCache.has(value)) { + return this.stringLiteralHexadecimalIndexCache.get(value); + } + var indexOfExistingValue = this.stringArrayStorage.getKeyOf(value); + var indexOfValue = void 0; + if (indexOfExistingValue >= 0) { + indexOfValue = indexOfExistingValue; + } else { + indexOfValue = this.stringArrayStorage.getLength(); + this.stringArrayStorage.set(null, value); + } + var hexadecimalIndex = "" + Utils_1.Utils.hexadecimalPrefix + Utils_1.Utils.decToHex(indexOfValue); + this.stringLiteralHexadecimalIndexCache.set(value, hexadecimalIndex); + return hexadecimalIndex; + } }, { key: "replaceStringLiteralWithStringArrayCall", value: function replaceStringLiteralWithStringArrayCall(value) { @@ -5890,17 +5909,9 @@ var StringLiteralReplacer = StringLiteralReplacer_1 = function (_AbstractReplace break; } value = Utils_1.Utils.stringToUnicodeEscapeSequence(value, !this.options.unicodeEscapeSequence); - var indexOfExistingValue = this.stringArrayStorage.getKeyOf(value); - var indexOfValue = void 0; - if (indexOfExistingValue >= 0) { - indexOfValue = indexOfExistingValue; - } else { - indexOfValue = this.stringArrayStorage.getLength(); - this.stringArrayStorage.set(null, value); - } + var hexadecimalIndex = this.getArrayHexadecimalIndex(value); var rotatedStringArrayStorageId = Utils_1.Utils.stringRotate(this.stringArrayStorage.getStorageId(), 1); var stringArrayStorageCallsWrapperName = "_" + Utils_1.Utils.hexadecimalPrefix + rotatedStringArrayStorageId; - var hexadecimalIndex = "" + Utils_1.Utils.hexadecimalPrefix + Utils_1.Utils.decToHex(indexOfValue); if (this.options.stringArrayEncoding === StringArrayEncoding_1.StringArrayEncoding.rc4) { return stringArrayStorageCallsWrapperName + "('" + hexadecimalIndex + "', '" + Utils_1.Utils.stringToUnicodeEscapeSequence(rc4Key, !this.options.unicodeEscapeSequence) + "')"; } diff --git a/src/node-transformers/node-obfuscators/replacers/StringLiteralReplacer.ts b/src/node-transformers/node-obfuscators/replacers/StringLiteralReplacer.ts index c1812997..b88f7aad 100644 --- a/src/node-transformers/node-obfuscators/replacers/StringLiteralReplacer.ts +++ b/src/node-transformers/node-obfuscators/replacers/StringLiteralReplacer.ts @@ -30,6 +30,11 @@ export class StringLiteralReplacer extends AbstractReplacer { */ private readonly stringLiteralCache: Map = new Map(); + /** + * @type {Map} + */ + private readonly stringLiteralHexadecimalIndexCache: Map = new Map(); + /** * @type {IStorage} */ @@ -91,6 +96,33 @@ export class StringLiteralReplacer extends AbstractReplacer { return result; } + /** + * @param value + * @return {string} + */ + private getArrayHexadecimalIndex (value: string): string { + if (this.stringLiteralHexadecimalIndexCache.has(value)) { + return this.stringLiteralHexadecimalIndexCache.get(value); + } + + const indexOfExistingValue: number = this.stringArrayStorage.getKeyOf(value); + + let indexOfValue: number; + + if (indexOfExistingValue >= 0) { + indexOfValue = indexOfExistingValue; + } else { + indexOfValue = this.stringArrayStorage.getLength(); + this.stringArrayStorage.set(null, value); + } + + const hexadecimalIndex: string = `${Utils.hexadecimalPrefix}${Utils.decToHex(indexOfValue)}`; + + this.stringLiteralHexadecimalIndexCache.set(value, hexadecimalIndex); + + return hexadecimalIndex; + } + /** * @param value * @returns {string} @@ -113,20 +145,9 @@ export class StringLiteralReplacer extends AbstractReplacer { value = Utils.stringToUnicodeEscapeSequence(value, !this.options.unicodeEscapeSequence); - const indexOfExistingValue: number = this.stringArrayStorage.getKeyOf(value); - - let indexOfValue: number; - - if (indexOfExistingValue >= 0) { - indexOfValue = indexOfExistingValue; - } else { - indexOfValue = this.stringArrayStorage.getLength(); - this.stringArrayStorage.set(null, value); - } - + const hexadecimalIndex: string = this.getArrayHexadecimalIndex(value); const rotatedStringArrayStorageId: string = Utils.stringRotate(this.stringArrayStorage.getStorageId(), 1); const stringArrayStorageCallsWrapperName: string = `_${Utils.hexadecimalPrefix}${rotatedStringArrayStorageId}`; - const hexadecimalIndex: string = `${Utils.hexadecimalPrefix}${Utils.decToHex(indexOfValue)}`; if (this.options.stringArrayEncoding === StringArrayEncoding.rc4) { return `${stringArrayStorageCallsWrapperName}('${hexadecimalIndex}', '${Utils.stringToUnicodeEscapeSequence(rc4Key, !this.options.unicodeEscapeSequence)}')`;