All files / src/node-obfuscators/replacers StringLiteralReplacer.ts

94.74% Statements 36/38
88.89% Branches 16/18
100% Functions 1/1
94.74% Lines 36/38
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98      1x   1x 1x   1x   1x       1x         1x 50x               76264x       76264x 33680x     42584x               33680x   33680x       33680x       1x   1x     1x 1x   1x     33680x 33679x     33680x 33680x 33680x 33680x   33680x 26511x   7169x 7169x     33680x     33680x   33680x       33680x 1x     33679x      
import { TStringArrayCallsWrapper } from '../../types/custom-nodes/TStringArrayCallsWrapper';
import { TStringArrayNode } from '../../types/custom-nodes/TStringArrayNode';
 
import { StringArrayEncoding } from '../../enums/StringArrayEncoding';
 
import { AbstractReplacer } from './AbstractReplacer';
import { NumberLiteralReplacer } from './NumberLiteralReplacer';
import { StringArray } from '../../StringArray';
import { Utils } from '../../Utils';
 
export class StringLiteralReplacer extends AbstractReplacer {
    /**
     * @type {number}
     */
    private static minimumLengthForStringArray: number = 3;
 
    /**
     * @type {string[]}
     */
    private static rc4Keys: string[] = Utils.getRandomGenerator()
        .n(() => Utils.getRandomGenerator().string({length: 4}), 50);
 
    /**
     * @param nodeValue
     * @returns {string}
     */
    public replace (nodeValue: string): string {
        const replaceWithStringArrayFlag: boolean = (
            nodeValue.length >= StringLiteralReplacer.minimumLengthForStringArray
            && Math.random() <= this.options.stringArrayThreshold
        );
 
        if (this.options.stringArray && replaceWithStringArrayFlag) {
            return this.replaceStringLiteralWithStringArrayCall(nodeValue);
        }
 
        return `'${Utils.stringToUnicodeEscapeSequence(nodeValue)}'`;
    }
 
    /**
     * @param value
     * @returns {string}
     */
    private replaceStringLiteralWithStringArrayCall (value: string): string {
        const stringArrayNode: TStringArrayNode = <TStringArrayNode>this.nodes.get('stringArrayNode');
 
        Iif (!stringArrayNode) {
            throw new ReferenceError('`stringArrayNode` node is not found in Map with custom node.');
        }
 
        let rc4Key: string = '';
 
        switch (this.options.stringArrayEncoding) {
            case StringArrayEncoding.base64:
                value = Utils.btoa(value);
 
                break;
 
            case StringArrayEncoding.rc4:
                rc4Key = Utils.getRandomGenerator().pickone(StringLiteralReplacer.rc4Keys);
                value = Utils.btoa(Utils.rc4(value, rc4Key));
 
                break;
        }
 
        if (this.options.unicodeEscapeSequence) {
            value = Utils.stringToUnicodeEscapeSequence(value);
        }
 
        let stringArray: StringArray = stringArrayNode.getNodeData(),
            indexOfExistingValue: number = stringArray.getIndexOf(value),
            indexOfValue: number,
            hexadecimalIndex: string;
 
        if (indexOfExistingValue >= 0) {
            indexOfValue = indexOfExistingValue;
        } else {
            indexOfValue = stringArray.getLength();
            stringArrayNode.updateNodeData(value);
        }
 
        hexadecimalIndex = new NumberLiteralReplacer(this.nodes, this.options)
            .replace(indexOfValue);
 
        const stringArrayCallsWrapper: TStringArrayCallsWrapper = <TStringArrayCallsWrapper>this.nodes.get('stringArrayCallsWrapper');
 
        Iif (!stringArrayCallsWrapper) {
            throw new ReferenceError('`stringArrayCallsWrapper` node is not found in Map with custom node.');
        }
 
        if (this.options.stringArrayEncoding === StringArrayEncoding.rc4) {
            return `${stringArrayCallsWrapper.getNodeIdentifier()}('${hexadecimalIndex}', '${Utils.stringToUnicodeEscapeSequence(rc4Key)}')`;
        }
 
        return `${stringArrayCallsWrapper.getNodeIdentifier()}('${hexadecimalIndex}')`;
    }
}