Shifted indexes of string array wrappers #3: added ability to generate a negative shifted index

This commit is contained in:
sanex
2020-09-17 01:21:51 +03:00
parent 3036e8be1a
commit 33441ebf36
8 changed files with 98 additions and 49 deletions
+7 -7
View File
File diff suppressed because one or more lines are too long
+1 -1
View File
File diff suppressed because one or more lines are too long
+1 -1
View File
File diff suppressed because one or more lines are too long
@@ -0,0 +1,77 @@
import { inject, injectable, } from 'inversify';
import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
import * as ESTree from 'estree';
import { TIdentifierNamesGeneratorFactory } from '../../types/container/generators/TIdentifierNamesGeneratorFactory';
import { ICustomCodeHelperFormatter } from '../../interfaces/custom-code-helpers/ICustomCodeHelperFormatter';
import { IOptions } from '../../interfaces/options/IOptions';
import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
import { AbstractCustomNode } from '../AbstractCustomNode';
import { NodeFactory } from '../../node/NodeFactory';
import { NodeMetadata } from '../../node/NodeMetadata';
import { NodeUtils } from '../../node/NodeUtils';
import { NumberUtils } from '../../utils/NumberUtils';
@injectable()
export abstract class AbstractStringArrayCallNode extends AbstractCustomNode {
/**
* @param {TIdentifierNamesGeneratorFactory} identifierNamesGeneratorFactory
* @param {ICustomCodeHelperFormatter} customCodeHelperFormatter
* @param {IRandomGenerator} randomGenerator
* @param {IOptions} options
*/
protected constructor (
@inject(ServiceIdentifiers.Factory__IIdentifierNamesGenerator)
identifierNamesGeneratorFactory: TIdentifierNamesGeneratorFactory,
@inject(ServiceIdentifiers.ICustomCodeHelperFormatter) customCodeHelperFormatter: ICustomCodeHelperFormatter,
@inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
@inject(ServiceIdentifiers.IOptions) options: IOptions
) {
super(
identifierNamesGeneratorFactory,
customCodeHelperFormatter,
randomGenerator,
options
);
}
/**
* @param {number} index
* @returns {Expression}
*/
protected getHexadecimalNode (index: number): ESTree.Expression {
const isPositive: boolean = index >= 0;
const normalizedIndex: number = Math.abs(index);
const hexadecimalIndex: string = NumberUtils.toHex(normalizedIndex);
const hexadecimalLiteralNode: ESTree.Literal = NodeFactory.literalNode(hexadecimalIndex);
NodeMetadata.set(hexadecimalLiteralNode, { replacedLiteral: true });
const hexadecimalNode: ESTree.Expression = isPositive
? hexadecimalLiteralNode
: NodeFactory.unaryExpressionNode(
'-',
hexadecimalLiteralNode
);
NodeUtils.parentizeAst(hexadecimalNode);
return hexadecimalNode;
}
/**
* @param {string} decodeKey
* @returns {Literal}
*/
protected getRc4KeyLiteralNode (decodeKey: string): ESTree.Literal {
const rc4KeyLiteralNode: ESTree.Literal = NodeFactory.literalNode(decodeKey);
NodeMetadata.set(rc4KeyLiteralNode, { replacedLiteral: true });
return rc4KeyLiteralNode;
}
}
@@ -12,14 +12,12 @@ import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
import { initializable } from '../../decorators/Initializable';
import { AbstractCustomNode } from '../AbstractCustomNode';
import { AbstractStringArrayCallNode } from './AbstractStringArrayCallNode';
import { NodeFactory } from '../../node/NodeFactory';
import { NodeMetadata } from '../../node/NodeMetadata';
import { NodeUtils } from '../../node/NodeUtils';
import { NumberUtils } from '../../utils/NumberUtils';
@injectable()
export class StringArrayCallNode extends AbstractCustomNode {
export class StringArrayCallNode extends AbstractStringArrayCallNode {
/**
* @type {string | null}
*/
@@ -60,31 +58,6 @@ export class StringArrayCallNode extends AbstractCustomNode {
);
}
/**
* @param {string} index
* @returns {Literal}
*/
public static getHexadecimalLiteralNode (index: number): ESTree.Literal {
const hexadecimalIndex: string = NumberUtils.toHex(index);
const hexadecimalLiteralNode: ESTree.Literal = NodeFactory.literalNode(hexadecimalIndex);
NodeMetadata.set(hexadecimalLiteralNode, { replacedLiteral: true });
return hexadecimalLiteralNode;
}
/**
* @param {string} decodeKey
* @returns {Literal}
*/
private static getRc4KeyLiteralNode (decodeKey: string): ESTree.Literal {
const rc4KeyLiteralNode: ESTree.Literal = NodeFactory.literalNode(decodeKey);
NodeMetadata.set(rc4KeyLiteralNode, { replacedLiteral: true });
return rc4KeyLiteralNode;
}
/**
* @param {string} stringArrayCallsWrapperName
* @param {number} index
@@ -104,12 +77,12 @@ export class StringArrayCallNode extends AbstractCustomNode {
* @returns {TStatement[]}
*/
protected getNodeStructure (): TStatement[] {
const callExpressionArgs: ESTree.Literal[] = [
StringArrayCallNode.getHexadecimalLiteralNode(this.index)
const callExpressionArgs: ESTree.Expression[] = [
this.getHexadecimalNode(this.index)
];
if (this.decodeKey) {
callExpressionArgs.push(StringArrayCallNode.getRc4KeyLiteralNode(this.decodeKey));
callExpressionArgs.push(this.getRc4KeyLiteralNode(this.decodeKey));
}
const stringArrayIdentifierNode: ESTree.Identifier =
@@ -12,13 +12,12 @@ import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
import { initializable } from '../../decorators/Initializable';
import { AbstractCustomNode } from '../AbstractCustomNode';
import { AbstractStringArrayCallNode } from './AbstractStringArrayCallNode';
import { NodeFactory } from '../../node/NodeFactory';
import { NodeUtils } from '../../node/NodeUtils';
import { StringArrayCallNode } from './StringArrayCallNode';
@injectable()
export class StringArrayScopeCallsWrapperFunctionNode extends AbstractCustomNode {
export class StringArrayScopeCallsWrapperFunctionNode extends AbstractStringArrayCallNode {
/**
* @type {number}
*/
@@ -102,7 +101,7 @@ export class StringArrayScopeCallsWrapperFunctionNode extends AbstractCustomNode
NodeFactory.binaryExpressionNode(
'-',
firstCallArgumentIdentifierNode,
StringArrayCallNode.getHexadecimalLiteralNode(this.shiftedIndex)
this.getHexadecimalNode(this.shiftedIndex)
),
secondCallArgumentIdentifierNode
]
@@ -10,12 +10,12 @@ import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
import { initializable } from '../../decorators/Initializable';
import { AbstractCustomNode } from '../AbstractCustomNode';
import { AbstractStringArrayCallNode } from './AbstractStringArrayCallNode';
import { NodeFactory } from '../../node/NodeFactory';
import { NodeUtils } from '../../node/NodeUtils';
@injectable()
export class StringArrayScopeCallsWrapperVariableNode extends AbstractCustomNode {
export class StringArrayScopeCallsWrapperVariableNode extends AbstractStringArrayCallNode {
/**
* @type {string}
*/
@@ -42,12 +42,12 @@ export class StringArrayTransformer extends AbstractNodeTransformer {
/**
* @type {number}
*/
private static readonly minShiftedIndexValue: number = 10;
private static readonly minShiftedIndexValue: number = -1000;
/**
* @type {number}
*/
private static readonly maxShiftedIndexValue: number = 100;
private static readonly maxShiftedIndexValue: number = 1000;
/**
* @type {IEscapeSequenceEncoder}