forked from javascript-obfuscator/javascript-obfuscator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSplitStringTransformer.ts
More file actions
199 lines (170 loc) · 6.61 KB
/
Copy pathSplitStringTransformer.ts
File metadata and controls
199 lines (170 loc) · 6.61 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import { inject, injectable, } from 'inversify';
import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
import * as estraverse from '@javascript-obfuscator/estraverse';
import * as ESTree from 'estree';
import * as stringz from 'stringz';
import { IOptions } from '../../interfaces/options/IOptions';
import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
import { IVisitor } from '../../interfaces/node-transformers/IVisitor';
import { NodeTransformer } from '../../enums/node-transformers/NodeTransformer';
import { NodeTransformationStage } from '../../enums/node-transformers/NodeTransformationStage';
import { AbstractNodeTransformer } from '../AbstractNodeTransformer';
import { NodeFactory } from '../../node/NodeFactory';
import { NodeGuards } from '../../node/NodeGuards';
import { NodeLiteralUtils } from '../../node/NodeLiteralUtils';
import { NodeUtils } from '../../node/NodeUtils';
/**
* Splits strings into parts
*/
@injectable()
export class SplitStringTransformer extends AbstractNodeTransformer {
/**
* @type {number}
*/
private static readonly firstPassChunkLength: number = 1000;
/**
* @type {NodeTransformer[]}
*/
public override runAfter: NodeTransformer[] = [
NodeTransformer.ObjectExpressionKeysTransformer,
NodeTransformer.TemplateLiteralTransformer
];
/**
* @param {IRandomGenerator} randomGenerator
* @param {IOptions} options
*/
public constructor (
@inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
@inject(ServiceIdentifiers.IOptions) options: IOptions
) {
super(randomGenerator, options);
}
/**
* @param {string} string
* @param {number} stringLength
* @param {number} chunkSize
* @returns {string[]}
*/
private static chunkString (
string: string,
stringLength: number,
chunkSize: number
): string[] {
const chunksCount: number = Math.ceil(stringLength / chunkSize);
const chunks: string[] = [];
let nextChunkStartIndex: number = 0;
for (
let chunkIndex: number = 0;
chunkIndex < chunksCount;
++chunkIndex, nextChunkStartIndex += chunkSize
) {
// eslint-disable-next-line unicorn/prefer-string-slice
chunks[chunkIndex] = stringz.substr(string, nextChunkStartIndex, chunkSize);
}
return chunks;
}
/**
* @param {NodeTransformationStage} nodeTransformationStage
* @returns {IVisitor | null}
*/
public getVisitor (nodeTransformationStage: NodeTransformationStage): IVisitor | null {
if (!this.options.splitStrings) {
return null;
}
switch (nodeTransformationStage) {
case NodeTransformationStage.Converting:
return {
enter: (node: ESTree.Node, parentNode: ESTree.Node | null): ESTree.Node | undefined => {
if (parentNode && NodeGuards.isLiteralNode(node)) {
return this.transformNode(node, parentNode);
}
}
};
default:
return null;
}
}
/**
* Needs to split string on chunks of length `splitStringsChunkLength` in two pass, because of
* `Maximum call stack size exceeded` error in `esrecurse` package
*
* @param {Literal} literalNode
* @param {Node} parentNode
* @returns {Node}
*/
public transformNode (literalNode: ESTree.Literal, parentNode: ESTree.Node): ESTree.Node {
if (NodeLiteralUtils.isProhibitedLiteralNode(literalNode, parentNode)) {
return literalNode;
}
// pass #1: split string on a large chunks with length of `firstPassChunkLength`
const firstPassChunksNode: ESTree.Node = this.transformLiteralNodeByChunkLength(
literalNode,
SplitStringTransformer.firstPassChunkLength
);
// pass #2: split large chunks on a chunks with length of `splitStringsChunkLength`
const secondPassChunksNode: ESTree.Node = estraverse.replace(firstPassChunksNode, {
// eslint-disable-next-line @typescript-eslint/no-shadow
enter: (node: ESTree.Node, parentNode: ESTree.Node | null) => {
if (NodeGuards.isLiteralNode(node)) {
return this.transformLiteralNodeByChunkLength(
node,
this.options.splitStringsChunkLength
);
}
}
});
NodeUtils.parentizeNode(secondPassChunksNode, parentNode);
NodeUtils.parentizeAst(secondPassChunksNode);
return secondPassChunksNode;
}
/**
* @param {Literal} literalNode
* @param {number} chunkLength
* @returns {Node}
*/
private transformLiteralNodeByChunkLength (
literalNode: ESTree.Literal,
chunkLength: number
): ESTree.Node {
if (!NodeLiteralUtils.isStringLiteralNode(literalNode)) {
return literalNode;
}
const valueLength: number = stringz.length(literalNode.value);
if (chunkLength >= valueLength) {
return literalNode;
}
const stringChunks: string[] = SplitStringTransformer.chunkString(
literalNode.value,
valueLength,
chunkLength
);
return this.transformStringChunksToBinaryExpressionNode(stringChunks);
}
/**
* @param {string[]} chunks
* @returns {BinaryExpression}
*/
private transformStringChunksToBinaryExpressionNode (chunks: string[]): ESTree.BinaryExpression {
const firstChunk: string | undefined = chunks.shift();
const secondChunk: string | undefined = chunks.shift();
if (!firstChunk || !secondChunk) {
throw new Error('First and second chunks values should not be empty');
}
const initialBinaryExpressionNode: ESTree.BinaryExpression = NodeFactory.binaryExpressionNode(
'+',
NodeFactory.literalNode(firstChunk),
NodeFactory.literalNode(secondChunk)
);
return chunks.reduce<ESTree.BinaryExpression>(
(binaryExpressionNode: ESTree.BinaryExpression, chunk: string) => {
const chunkLiteralNode: ESTree.Literal = NodeFactory.literalNode(chunk);
return NodeFactory.binaryExpressionNode(
'+',
binaryExpressionNode,
chunkLiteralNode
);
},
initialBinaryExpressionNode
);
}
}