StringArrayCustomNodeGroup.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import { inject, injectable, } from 'inversify';
  2. import { ServiceIdentifiers } from '../../../container/ServiceIdentifiers';
  3. import { TCustomNodeFactory } from '../../../types/container/custom-nodes/TCustomNodeFactory';
  4. import { TIdentifierNamesGeneratorFactory } from '../../../types/container/generators/TIdentifierNamesGeneratorFactory';
  5. import { TInitialData } from '../../../types/TInitialData';
  6. import { TNodeWithStatements } from '../../../types/node/TNodeWithStatements';
  7. import { ICallsGraphData } from '../../../interfaces/analyzers/calls-graph-analyzer/ICallsGraphData';
  8. import { ICustomNode } from '../../../interfaces/custom-nodes/ICustomNode';
  9. import { IOptions } from '../../../interfaces/options/IOptions';
  10. import { IRandomGenerator } from '../../../interfaces/utils/IRandomGenerator';
  11. import { IStringArrayStorage } from '../../../interfaces/storages/string-array-storage/IStringArrayStorage';
  12. import { initializable } from '../../../decorators/Initializable';
  13. import { CustomNode } from '../../../enums/custom-nodes/CustomNode';
  14. import { ObfuscationEvent } from '../../../enums/event-emitters/ObfuscationEvent';
  15. import { AbstractCustomNodeGroup } from '../../AbstractCustomNodeGroup';
  16. import { NodeAppender } from '../../../node/NodeAppender';
  17. import { StringArrayNode } from '../StringArrayNode';
  18. import { StringArrayCallsWrapper } from '../StringArrayCallsWrapper';
  19. import { StringArrayRotateFunctionNode } from '../StringArrayRotateFunctionNode';
  20. @injectable()
  21. export class StringArrayCustomNodeGroup extends AbstractCustomNodeGroup {
  22. /**
  23. * @type {ObfuscationEvent}
  24. */
  25. protected appendEvent: ObfuscationEvent = ObfuscationEvent.AfterObfuscation;
  26. /**
  27. * @type {Map<CustomNode, ICustomNode>}
  28. */
  29. @initializable()
  30. protected customNodes!: Map <CustomNode, ICustomNode>;
  31. /**
  32. * @type {TCustomNodeFactory}
  33. */
  34. private readonly customNodeFactory: TCustomNodeFactory;
  35. /**
  36. * @type {IStringArrayStorage}
  37. */
  38. private readonly stringArrayStorage: IStringArrayStorage;
  39. /**
  40. * @param {TCustomNodeFactory} customNodeFactory
  41. * @param {IStringArrayStorage} stringArrayStorage
  42. * @param {TIdentifierNamesGeneratorFactory} identifierNamesGeneratorFactory
  43. * @param {IRandomGenerator} randomGenerator
  44. * @param {IOptions} options
  45. */
  46. public constructor (
  47. @inject(ServiceIdentifiers.Factory__ICustomNode) customNodeFactory: TCustomNodeFactory,
  48. @inject(ServiceIdentifiers.IStringArrayStorage) stringArrayStorage: IStringArrayStorage,
  49. @inject(ServiceIdentifiers.Factory__IIdentifierNamesGenerator)
  50. identifierNamesGeneratorFactory: TIdentifierNamesGeneratorFactory,
  51. @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
  52. @inject(ServiceIdentifiers.IOptions) options: IOptions
  53. ) {
  54. super(identifierNamesGeneratorFactory, randomGenerator, options);
  55. this.customNodeFactory = customNodeFactory;
  56. this.stringArrayStorage = stringArrayStorage;
  57. }
  58. /**
  59. * @param {TNodeWithStatements} nodeWithStatements
  60. * @param {ICallsGraphData[]} callsGraphData
  61. */
  62. public appendCustomNodes (nodeWithStatements: TNodeWithStatements, callsGraphData: ICallsGraphData[]): void {
  63. if (!this.stringArrayStorage.getLength()) {
  64. return;
  65. }
  66. // stringArrayNode append
  67. this.appendCustomNodeIfExist(CustomNode.StringArrayNode, (customNode: ICustomNode) => {
  68. NodeAppender.prepend(nodeWithStatements, customNode.getNode());
  69. });
  70. // stringArrayCallsWrapper append
  71. this.appendCustomNodeIfExist(CustomNode.StringArrayCallsWrapper, (customNode: ICustomNode) => {
  72. NodeAppender.insertAtIndex(nodeWithStatements, customNode.getNode(), 1);
  73. });
  74. // stringArrayRotateFunctionNode append
  75. this.appendCustomNodeIfExist(CustomNode.StringArrayRotateFunctionNode, (customNode: ICustomNode) => {
  76. NodeAppender.insertAtIndex(nodeWithStatements, customNode.getNode(), 1);
  77. });
  78. }
  79. public initialize (): void {
  80. this.customNodes = new Map <CustomNode, ICustomNode>();
  81. if (!this.options.stringArray) {
  82. return;
  83. }
  84. const stringArrayNode: ICustomNode<TInitialData<StringArrayNode>> =
  85. this.customNodeFactory(CustomNode.StringArrayNode);
  86. const stringArrayCallsWrapper: ICustomNode<TInitialData<StringArrayCallsWrapper>> =
  87. this.customNodeFactory(CustomNode.StringArrayCallsWrapper);
  88. const stringArrayRotateFunctionNode: ICustomNode<TInitialData<StringArrayRotateFunctionNode>> =
  89. this.customNodeFactory(CustomNode.StringArrayRotateFunctionNode);
  90. const stringArrayName: string = this.stringArrayStorage.getStorageName();
  91. const stringArrayCallsWrapperName: string = this.stringArrayStorage.getStorageCallsWrapperName();
  92. const stringArrayRotationAmount: number = this.stringArrayStorage.getRotationAmount();
  93. stringArrayNode.initialize(this.stringArrayStorage, stringArrayName);
  94. stringArrayCallsWrapper.initialize(stringArrayName, stringArrayCallsWrapperName);
  95. stringArrayRotateFunctionNode.initialize(stringArrayName, stringArrayRotationAmount);
  96. this.customNodes.set(CustomNode.StringArrayNode, stringArrayNode);
  97. this.customNodes.set(CustomNode.StringArrayCallsWrapper, stringArrayCallsWrapper);
  98. if (this.options.rotateStringArray) {
  99. this.customNodes.set(CustomNode.StringArrayRotateFunctionNode, stringArrayRotateFunctionNode);
  100. }
  101. }
  102. }