SelfDefendingTemplate.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { IEscapeSequenceEncoder } from '../../../interfaces/utils/IEscapeSequenceEncoder';
  2. /**
  3. * @param {IEscapeSequenceEncoder} escapeSequenceEncoder
  4. * @returns {string}
  5. */
  6. export function SelfDefendingTemplate (escapeSequenceEncoder: IEscapeSequenceEncoder): string {
  7. return `
  8. var StatesClass = function (rc4Bytes) {
  9. this.rc4Bytes = rc4Bytes;
  10. this.states = [1, 0, 0];
  11. this.newState = function(){return 'newState';};
  12. this.firstState = '${
  13. escapeSequenceEncoder.encode(`\\w+ *\\(\\) *{\\w+ *`, true)
  14. }';
  15. this.secondState = '${
  16. escapeSequenceEncoder.encode(`['|"].+['|"];? *}`, true)
  17. }';
  18. };
  19. StatesClass.prototype.checkState = function () {
  20. var regExp = new RegExp(this.firstState + this.secondState);
  21. return this.runState(regExp.test(this.newState.toString()) ? --this.states[1] : --this.states[0]);
  22. };
  23. StatesClass.prototype.runState = function (stateResult) {
  24. if (!Boolean(~stateResult)) {
  25. return stateResult;
  26. }
  27. return this.getState(this.rc4Bytes);
  28. };
  29. StatesClass.prototype.getState = function (rc4Bytes) {
  30. for (var i = 0, len = this.states.length; i < len; i++) {
  31. this.states.push(Math.round(Math.random()));
  32. len = this.states.length;
  33. }
  34. return rc4Bytes(this.states[0]);
  35. };
  36. new StatesClass({stringArrayCallsWrapperName}).checkState();
  37. `;
  38. }