Prechádzať zdrojové kódy

String array now stored as a function

sanex 3 rokov pred
rodič
commit
9da81caf3d

+ 0 - 1
src/custom-code-helpers/string-array/StringArrayCallsWrapperBase64CodeHelper.ts

@@ -25,7 +25,6 @@ export class StringArrayCallsWrapperBase64CodeHelper extends StringArrayCallsWra
                 atobPolyfill,
                 atobFunctionName,
                 selfDefendingCode,
-                stringArrayName: this.stringArrayName,
                 stringArrayCallsWrapperName: this.stringArrayCallsWrapperName,
                 stringArrayCacheName: this.stringArrayCacheName
             }

+ 8 - 9
src/custom-code-helpers/string-array/StringArrayCallsWrapperCodeHelper.ts

@@ -30,13 +30,13 @@ export class StringArrayCallsWrapperCodeHelper extends AbstractCustomCodeHelper
      * @type {string}
      */
     @initializable()
-    protected stringArrayName!: string;
+    protected stringArrayCallsWrapperName!: string;
 
     /**
      * @type {string}
      */
     @initializable()
-    protected stringArrayCallsWrapperName!: string;
+    protected stringArrayFunctionName!: string;
 
     /**
      * @type {string}
@@ -78,16 +78,16 @@ export class StringArrayCallsWrapperCodeHelper extends AbstractCustomCodeHelper
     }
 
     /**
-     * @param {string} stringArrayName
+     * @param {string} stringArrayFunctionName
      * @param {string} stringArrayCallsWrapperName
      * @param {number} indexShiftAmount
      */
     public initialize (
-        stringArrayName: string,
+        stringArrayFunctionName: string,
         stringArrayCallsWrapperName: string,
         indexShiftAmount: number
     ): void {
-        this.stringArrayName = stringArrayName;
+        this.stringArrayFunctionName = stringArrayFunctionName;
         this.stringArrayCallsWrapperName = stringArrayCallsWrapperName;
         this.indexShiftAmount = indexShiftAmount;
 
@@ -108,14 +108,14 @@ export class StringArrayCallsWrapperCodeHelper extends AbstractCustomCodeHelper
     protected override getCodeHelperTemplate (): string {
         const decodeCodeHelperTemplate: string = this.getDecodeStringArrayTemplate();
 
-        const preservedNames: string[] = [`^${this.stringArrayName}$`];
+        const preservedNames: string[] = [`^${this.stringArrayFunctionName}$`];
 
         return this.customCodeHelperObfuscator.obfuscateTemplate(
             this.customCodeHelperFormatter.formatTemplate(StringArrayCallsWrapperTemplate(), {
                 decodeCodeHelperTemplate,
                 stringArrayCallsWrapperName: this.stringArrayCallsWrapperName,
-                stringArrayName: this.stringArrayName,
                 stringArrayCacheName: this.stringArrayCacheName,
+                stringArrayFunctionName: this.stringArrayFunctionName,
                 indexShiftAmount: this.indexShiftAmount
             }),
             {
@@ -145,8 +145,7 @@ export class StringArrayCallsWrapperCodeHelper extends AbstractCustomCodeHelper
                 this.escapeSequenceEncoder
             ),
             {
-                stringArrayCallsWrapperName: this.stringArrayCallsWrapperName,
-                stringArrayName: this.stringArrayName
+                stringArrayCallsWrapperName: this.stringArrayCallsWrapperName
             }
         );
     }

+ 0 - 1
src/custom-code-helpers/string-array/StringArrayCallsWrapperRc4CodeHelper.ts

@@ -32,7 +32,6 @@ export class StringArrayCallsWrapperRc4CodeHelper extends StringArrayCallsWrappe
                 rc4FunctionName,
                 rc4Polyfill,
                 selfDefendingCode,
-                stringArrayName: this.stringArrayName,
                 stringArrayCallsWrapperName: this.stringArrayCallsWrapperName,
                 stringArrayCacheName: this.stringArrayCacheName
             }

+ 8 - 5
src/custom-code-helpers/string-array/StringArrayCodeHelper.ts

@@ -31,7 +31,7 @@ export class StringArrayCodeHelper extends AbstractCustomCodeHelper {
      * @type {string}
      */
     @initializable()
-    private stringArrayName!: string;
+    private stringArrayFunctionName!: string;
 
     /**
      * @param {TIdentifierNamesGeneratorFactory} identifierNamesGeneratorFactory
@@ -59,14 +59,14 @@ export class StringArrayCodeHelper extends AbstractCustomCodeHelper {
 
     /**
      * @param {IStringArrayStorage} stringArrayStorage
-     * @param {string} stringArrayName
+     * @param {string} stringArrayFunctionName
      */
     public initialize (
         stringArrayStorage: IStringArrayStorage,
-        stringArrayName: string
+        stringArrayFunctionName: string
     ): void {
         this.stringArrayStorage = stringArrayStorage;
-        this.stringArrayName = stringArrayName;
+        this.stringArrayFunctionName = stringArrayFunctionName;
     }
 
     /**
@@ -81,8 +81,11 @@ export class StringArrayCodeHelper extends AbstractCustomCodeHelper {
      * @returns {string}
      */
     protected override getCodeHelperTemplate (): string {
+        const stringArrayName: string = this.identifierNamesGenerator.generateNext();
+
         return this.customCodeHelperFormatter.formatTemplate(StringArrayTemplate(), {
-            stringArrayName: this.stringArrayName,
+            stringArrayFunctionName: this.stringArrayFunctionName,
+            stringArrayName: stringArrayName,
             stringArrayStorageItems: this.getEncodedStringArrayStorageItems()
         });
     }

+ 5 - 5
src/custom-code-helpers/string-array/StringArrayRotateFunctionCodeHelper.ts

@@ -35,7 +35,7 @@ export class StringArrayRotateFunctionCodeHelper extends AbstractCustomCodeHelpe
      * @type {string}
      */
     @initializable()
-    private stringArrayName!: string;
+    private stringArrayFunctionName!: string;
 
     /**
      * @param {TIdentifierNamesGeneratorFactory} identifierNamesGeneratorFactory
@@ -62,16 +62,16 @@ export class StringArrayRotateFunctionCodeHelper extends AbstractCustomCodeHelpe
     }
 
     /**
-     * @param {string} stringArrayName
+     * @param {string} stringArrayFunctionName
      * @param {number} comparisonValue
      * @param {Expression} comparisonExpressionNode
      */
     public initialize (
-        stringArrayName: string,
+        stringArrayFunctionName: string,
         comparisonValue: number,
         comparisonExpressionNode: Expression
     ): void {
-        this.stringArrayName = stringArrayName;
+        this.stringArrayFunctionName = stringArrayFunctionName;
         this.comparisonValue = comparisonValue;
         this.comparisonExpressionNode = comparisonExpressionNode;
     }
@@ -95,7 +95,7 @@ export class StringArrayRotateFunctionCodeHelper extends AbstractCustomCodeHelpe
             {
                 comparisonExpressionCode,
                 comparisonValue: this.comparisonValue,
-                stringArrayName: this.stringArrayName
+                stringArrayFunctionName: this.stringArrayFunctionName
             }
         );
     }

+ 28 - 17
src/custom-code-helpers/string-array/group/StringArrayCodeHelperGroup.ts

@@ -5,6 +5,7 @@ import { TCustomCodeHelperFactory } from '../../../types/container/custom-code-h
 import { TIdentifierNamesGeneratorFactory } from '../../../types/container/generators/TIdentifierNamesGeneratorFactory';
 import { TInitialData } from '../../../types/TInitialData';
 import { TNodeWithStatements } from '../../../types/node/TNodeWithStatements';
+import { TStatement } from '../../../types/node/TStatement';
 import { TStringArrayEncoding } from '../../../types/options/TStringArrayEncoding';
 
 import { ICallsGraphData } from '../../../interfaces/analyzers/calls-graph-analyzer/ICallsGraphData';
@@ -22,7 +23,6 @@ import { AbstractCustomCodeHelperGroup } from '../../AbstractCustomCodeHelperGro
 import { NodeAppender } from '../../../node/NodeAppender';
 import { StringArrayCallsWrapperCodeHelper } from '../StringArrayCallsWrapperCodeHelper';
 import { StringArrayCodeHelper } from '../StringArrayCodeHelper';
-import { TStatement } from '../../../types/node/TStatement';
 
 @injectable()
 export class StringArrayCodeHelperGroup extends AbstractCustomCodeHelperGroup {
@@ -82,31 +82,31 @@ export class StringArrayCodeHelperGroup extends AbstractCustomCodeHelperGroup {
         }
 
         // stringArray helper nodes append
+        const scopeStatements: TStatement[] = NodeAppender.getScopeStatements(nodeWithStatements);
+
         this.appendCustomNodeIfExist(
             CustomCodeHelper.StringArray,
             (customCodeHelper: ICustomCodeHelper<TInitialData<StringArrayCodeHelper>>) => {
-                NodeAppender.prepend(nodeWithStatements, customCodeHelper.getNode());
+                NodeAppender.insertAtIndex(
+                    nodeWithStatements,
+                    customCodeHelper.getNode(),
+                    this.getScopeStatementRandomIndex(scopeStatements)
+                );
             }
         );
 
         // stringArrayCallsWrapper helper nodes append
-        const stringArrayEncodingsLength: number = this.options.stringArrayEncoding.length;
-        // Stating from index 1 and forward. 0 index is reserved for string array itself.
-        let randomIndex: number = 1;
-        for (let i = 0; i < stringArrayEncodingsLength; i++, randomIndex++) {
-            const stringArrayEncoding: TStringArrayEncoding = this.options.stringArrayEncoding[i];
+        for (const stringArrayEncoding of this.options.stringArrayEncoding) {
             const stringArrayCallsWrapperCodeHelperName: CustomCodeHelper = this.getStringArrayCallsWrapperCodeHelperName(stringArrayEncoding);
 
-            const scopeStatements: TStatement[] = NodeAppender.getScopeStatements(nodeWithStatements);
-            randomIndex = this.randomGenerator.getRandomInteger(
-                randomIndex,
-                scopeStatements.length - 1
-            );
-
             this.appendCustomNodeIfExist(
                 stringArrayCallsWrapperCodeHelperName,
                 (customCodeHelper: ICustomCodeHelper<TInitialData<StringArrayCallsWrapperCodeHelper>>) => {
-                    NodeAppender.insertAtIndex(nodeWithStatements, customCodeHelper.getNode(), randomIndex);
+                    NodeAppender.insertAtIndex(
+                        nodeWithStatements,
+                        customCodeHelper.getNode(),
+                        this.getScopeStatementRandomIndex(scopeStatements)
+                    );
                 }
             );
         }
@@ -122,9 +122,9 @@ export class StringArrayCodeHelperGroup extends AbstractCustomCodeHelperGroup {
         // stringArray helper initialize
         const stringArrayCodeHelper: ICustomCodeHelper<TInitialData<StringArrayCodeHelper>> =
             this.customCodeHelperFactory(CustomCodeHelper.StringArray);
-        const stringArrayName: string = this.stringArrayStorage.getStorageName();
+        const stringArrayFunctionName: string = this.stringArrayStorage.getStorageName();
 
-        stringArrayCodeHelper.initialize(this.stringArrayStorage, stringArrayName);
+        stringArrayCodeHelper.initialize(this.stringArrayStorage, stringArrayFunctionName);
         this.customCodeHelpers.set(CustomCodeHelper.StringArray, stringArrayCodeHelper);
 
         // stringArrayCallsWrapper helper initialize
@@ -134,7 +134,7 @@ export class StringArrayCodeHelperGroup extends AbstractCustomCodeHelperGroup {
                 this.customCodeHelperFactory(stringArrayCallsWrapperCodeHelperName);
             const stringArrayCallsWrapperName: string = this.stringArrayStorage.getStorageCallsWrapperName(stringArrayEncoding);
             stringArrayCallsWrapperCodeHelper.initialize(
-                stringArrayName,
+                stringArrayFunctionName,
                 stringArrayCallsWrapperName,
                 this.stringArrayStorage.getIndexShiftAmount()
             );
@@ -152,4 +152,15 @@ export class StringArrayCodeHelperGroup extends AbstractCustomCodeHelperGroup {
                 .stringArrayCallsWrapperCodeHelperMap.get(stringArrayEncoding)
             ?? CustomCodeHelper.StringArrayCallsWrapper;
     }
+
+    /**
+     * @param {TStatement[]} scopeStatements
+     * @returns {number}
+     */
+    private getScopeStatementRandomIndex (scopeStatements: TStatement[]): number {
+        return this.randomGenerator.getRandomInteger(
+            0,
+            Math.max(0, scopeStatements.length - 1)
+        );
+    }
 }

+ 1 - 1
src/custom-code-helpers/string-array/templates/string-array-calls-wrapper/StringArrayBase64DecodeTemplate.ts

@@ -22,7 +22,7 @@ export function StringArrayBase64DecodeTemplate (
             {stringArrayCallsWrapperName}.${initializedIdentifier} = true;
         }
                   
-        const firstValue = {stringArrayName}[0];
+        const firstValue = stringArray[0];
         const cacheKey = index + firstValue;
         const cachedValue = {stringArrayCacheName}[cacheKey];
         

+ 3 - 1
src/custom-code-helpers/string-array/templates/string-array-calls-wrapper/StringArrayCallsWrapperTemplate.ts

@@ -7,10 +7,12 @@
 export function StringArrayCallsWrapperTemplate (): string {
     return `
         function {stringArrayCallsWrapperName} ({stringArrayCacheName}, key) {
+            const stringArray = {stringArrayFunctionName}();
+            
             {stringArrayCallsWrapperName} = function (index, key) {
                 index = index - {indexShiftAmount};
                 
-                let value = {stringArrayName}[index];
+                let value = stringArray[index];
                 
                 {decodeCodeHelperTemplate}
             

+ 1 - 1
src/custom-code-helpers/string-array/templates/string-array-calls-wrapper/StringArrayRC4DecodeTemplate.ts

@@ -24,7 +24,7 @@ export function StringArrayRC4DecodeTemplate (
             {stringArrayCallsWrapperName}.${initializedIdentifier} = true;
         }
   
-        const firstValue = {stringArrayName}[0];
+        const firstValue = stringArray[0];
         const cacheKey = index + firstValue;
         const cachedValue = {stringArrayCacheName}[cacheKey];
 

+ 6 - 4
src/custom-code-helpers/string-array/templates/string-array-rotate-function/StringArrayRotateFunctionTemplate.ts

@@ -3,7 +3,9 @@
  */
 export function StringArrayRotateFunctionTemplate (): string {
     return `
-        (function (array, comparisonValue) {
+        (function (stringArrayFunction, comparisonValue) {
+            const stringArray = stringArrayFunction();
+        
             while (true) {
                 try {
                     const expression = {comparisonExpressionCode};
@@ -11,12 +13,12 @@ export function StringArrayRotateFunctionTemplate (): string {
                     if (expression === comparisonValue) {
                         break;
                     } else {
-                        array['push'](array['shift']());
+                        stringArray['push'](stringArray['shift']());
                     }
                 } catch (e) {
-                    array['push'](array['shift']());
+                    stringArray['push'](stringArray['shift']());
                 }
             }
-        })({stringArrayName}, {comparisonValue});
+        })({stringArrayFunctionName}, {comparisonValue});
     `;
 }

+ 9 - 1
src/custom-code-helpers/string-array/templates/string-array/StringArrayTemplate.ts

@@ -3,6 +3,14 @@
  */
 export function StringArrayTemplate (): string {
     return `
-        const {stringArrayName} = [{stringArrayStorageItems}];
+        function {stringArrayFunctionName} () {
+            const {stringArrayName} = [{stringArrayStorageItems}];
+            
+            {stringArrayFunctionName} = function () {
+                return {stringArrayName};
+            };
+            
+            return {stringArrayFunctionName}();
+        }
     `;
 }

+ 3 - 3
src/storages/string-array-transformers/StringArrayStorage.ts

@@ -52,7 +52,7 @@ export class StringArrayStorage extends MapStorage <`${string}-${TStringArrayEnc
     /**
      * @type {number}
      */
-    private static readonly stringArrayNameLength: number = 4;
+    private static readonly stringArrayFunctionNameLength: number = 4;
 
     /**
      * @type {IArrayUtils}
@@ -181,7 +181,7 @@ export class StringArrayStorage extends MapStorage <`${string}-${TStringArrayEnc
     public override getStorageId (): string {
         if (!this.stringArrayStorageName) {
             this.stringArrayStorageName = this.identifierNamesGenerator
-                .generateForGlobalScope(StringArrayStorage.stringArrayNameLength);
+                .generateForGlobalScope(StringArrayStorage.stringArrayFunctionNameLength);
         }
 
         return this.stringArrayStorageName;
@@ -200,7 +200,7 @@ export class StringArrayStorage extends MapStorage <`${string}-${TStringArrayEnc
         }
 
         const newStorageCallsWrapperName: string = this.identifierNamesGenerator
-            .generateForGlobalScope(StringArrayStorage.stringArrayNameLength);
+            .generateForGlobalScope(StringArrayStorage.stringArrayFunctionNameLength);
 
         this.stringArrayStorageCallsWrapperNamesMap.set(
             stringArrayEncoding,

+ 6 - 1
test/dev/dev.ts

@@ -30,9 +30,14 @@ import { NO_ADDITIONAL_NODES_PRESET } from '../../src/options/presets/NoCustomNo
         `,
         {
             ...NO_ADDITIONAL_NODES_PRESET,
+            compact: false,
+            simplify: false,
             stringArray: true,
             stringArrayThreshold: 1,
-            selfDefending: true
+            stringArrayEncoding: ['rc4', 'base64'],
+            stringArrayWrappersCount: 5,
+            rotateStringArray: true,
+            identifierNamesGenerator: 'mangled'
         }
     ).getObfuscatedCode();