浏览代码

slightly improved speed with rc4 encoding

sanex3339 8 年之前
父节点
当前提交
6aba608f36
共有 2 个文件被更改,包括 53 次插入21 次删除
  1. 20 9
      dist/index.js
  2. 33 12
      src/node-transformers/node-obfuscators/replacers/StringLiteralReplacer.ts

+ 20 - 9
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) + "')";
             }

+ 33 - 12
src/node-transformers/node-obfuscators/replacers/StringLiteralReplacer.ts

@@ -30,6 +30,11 @@ export class StringLiteralReplacer extends AbstractReplacer {
      */
     private readonly stringLiteralCache: Map <string, string> = new Map();
 
+    /**
+     * @type {Map<string, string>}
+     */
+    private readonly stringLiteralHexadecimalIndexCache: Map <string, string> = new Map();
+
     /**
      * @type {IStorage<ICustomNodeGroup>}
      */
@@ -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 <string>this.stringLiteralHexadecimalIndexCache.get(value);
+        }
+
+        const indexOfExistingValue: number = <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 = <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)}')`;