Skip to content

Commit 68c6009

Browse files
committed
standardise uniqueID strings
strings that are going to be replaced by my code are in the format `$<uniqueID>$<NAME>$` or `$<uniqueID>$<NAME>$<...data>$` names that may appear in generated code are in the format `_<uniqueID>_<NAME>_`
1 parent 3c27edd commit 68c6009

File tree

6 files changed

+85
-85
lines changed

6 files changed

+85
-85
lines changed

src/processScript/minify.ts

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export async function minify(file: File, autocomplete?: string, {
5959
}
6060

6161
for (const global in (program.scope as any).globals as Record<string, any>) {
62-
if (global == `arguments` || global.startsWith(`$${uniqueID}`))
62+
if (global == `arguments` || global.startsWith(`$${uniqueID}$`))
6363
continue
6464

6565
const referencePaths = getReferencePathsToGlobal(global, program)
@@ -68,34 +68,34 @@ export async function minify(file: File, autocomplete?: string, {
6868
continue
6969

7070
for (const path of referencePaths)
71-
path.replaceWith(t.identifier(`_GLOBAL_${global}_${uniqueID}_`))
71+
path.replaceWith(t.identifier(`_${uniqueID}_GLOBAL_${global}_`))
7272

7373
mainFunctionPath.node.body.body.unshift(
7474
t.variableDeclaration(
7575
`let`,
7676
[
7777
t.variableDeclarator(
78-
t.identifier(`_GLOBAL_${global}_${uniqueID}_`),
78+
t.identifier(`_${uniqueID}_GLOBAL_${global}_`),
7979
t.identifier(global)
8080
)
8181
]
8282
)
8383
)
8484
}
8585

86-
const hashGReferencePaths = getReferencePathsToGlobal(`$${uniqueID}$GLOBAL`, program)
86+
const hashGReferencePaths = getReferencePathsToGlobal(`$${uniqueID}$GLOBAL$`, program)
8787

8888
if (hashGReferencePaths.length > 3) {
8989
for (const path of hashGReferencePaths)
90-
path.replaceWith(t.identifier(`_G_${uniqueID}_`))
90+
path.replaceWith(t.identifier(`_${uniqueID}_G_`))
9191

9292
mainFunctionPath.node.body.body.unshift(
9393
t.variableDeclaration(
9494
`let`,
9595
[
9696
t.variableDeclarator(
97-
t.identifier(`_G_${uniqueID}_`),
98-
t.identifier(`$${uniqueID}$GLOBAL`)
97+
t.identifier(`_${uniqueID}_G_`),
98+
t.identifier(`$${uniqueID}$GLOBAL$`)
9999
)
100100
]
101101
)
@@ -117,10 +117,10 @@ export async function minify(file: File, autocomplete?: string, {
117117

118118
if (memberExpression.property.name == `prototype`) {
119119
memberExpression.computed = true
120-
memberExpression.property = t.identifier(`_PROTOTYPE_PROPERTY_${uniqueID}_`)
120+
memberExpression.property = t.identifier(`_${uniqueID}_PROTOTYPE_PROPERTY_`)
121121
} else if (memberExpression.property.name == `__proto__`) {
122122
memberExpression.computed = true
123-
memberExpression.property = t.identifier(`_PROTO_PROPERTY_${uniqueID}_`)
123+
memberExpression.property = t.identifier(`_${uniqueID}_PROTO_PROPERTY_`)
124124
} else if (includesIllegalString(memberExpression.property.name)) {
125125
memberExpression.computed = true
126126

@@ -181,8 +181,8 @@ export async function minify(file: File, autocomplete?: string, {
181181
keep_classnames: !mangleNames,
182182
keep_fnames: !mangleNames
183183
})).code!
184-
.replace(new RegExp(`_PROTOTYPE_PROPERTY_${uniqueID}_`, `g`), `"prototype"`)
185-
.replace(new RegExp(`_PROTO_PROPERTY_${uniqueID}_`, `g`), `"__proto__"`)
184+
.replace(new RegExp(`_${uniqueID}_PROTOTYPE_PROPERTY_`, `g`), `"prototype"`)
185+
.replace(new RegExp(`_${uniqueID}_PROTO_PROPERTY_`, `g`), `"__proto__"`)
186186

187187
let comment: string | undefined
188188
let hasComment = false
@@ -207,14 +207,14 @@ export async function minify(file: File, autocomplete?: string, {
207207
const o: Record<string, unknown> = {}
208208

209209
if (parseObjectExpression(path.node, o))
210-
path.replaceWith(t.identifier(`_JSON_VALUE_${jsonValues.push(o) - 1}_${uniqueID}_`))
210+
path.replaceWith(t.identifier(`_${uniqueID}_JSON_VALUE_${jsonValues.push(o) - 1}_`))
211211
},
212212

213213
ArrayExpression(path) {
214214
const o: unknown[] = []
215215

216216
if (parseArrayExpression(path.node, o))
217-
path.replaceWith(t.identifier(`_JSON_VALUE_${jsonValues.push(o) - 1}_${uniqueID}_`))
217+
path.replaceWith(t.identifier(`_${uniqueID}_JSON_VALUE_${jsonValues.push(o) - 1}_`))
218218
}
219219
})
220220

@@ -265,7 +265,7 @@ export async function minify(file: File, autocomplete?: string, {
265265
UnaryExpression(path) {
266266
if (path.node.operator == `void`) {
267267
if (path.node.argument.type == `NumericLiteral` && !path.node.argument.value) {
268-
path.replaceWith(t.identifier(`_UNDEFINED_${uniqueID}_`))
268+
path.replaceWith(t.identifier(`_${uniqueID}_UNDEFINED_`))
269269
undefinedIsReferenced = true
270270
}
271271
} else if (path.node.operator == `-` && path.node.argument.type == `NumericLiteral`) {
@@ -283,7 +283,7 @@ export async function minify(file: File, autocomplete?: string, {
283283
if (jsonValueIndex == -1)
284284
jsonValueIndex += jsonValues.push(value)
285285

286-
path.replaceWith(t.identifier(`_JSON_VALUE_${jsonValueIndex}_${uniqueID}_`))
286+
path.replaceWith(t.identifier(`_${uniqueID}_JSON_VALUE_${jsonValueIndex}_`))
287287
})())
288288

289289
path.skip()
@@ -297,7 +297,7 @@ export async function minify(file: File, autocomplete?: string, {
297297
if (jsonValueIndex == -1)
298298
jsonValueIndex += jsonValues.push(null)
299299

300-
path.replaceWith(t.identifier(`_JSON_VALUE_${jsonValueIndex}_${uniqueID}_`))
300+
path.replaceWith(t.identifier(`_${uniqueID}_JSON_VALUE_${jsonValueIndex}_`))
301301
/* eslint-enable unicorn/no-null */
302302
},
303303

@@ -307,7 +307,7 @@ export async function minify(file: File, autocomplete?: string, {
307307
if (jsonValueIndex == -1)
308308
jsonValueIndex += jsonValues.push(path.node.value)
309309

310-
path.replaceWith(t.identifier(`_JSON_VALUE_${jsonValueIndex}_${uniqueID}_`))
310+
path.replaceWith(t.identifier(`_${uniqueID}_JSON_VALUE_${jsonValueIndex}_`))
311311
},
312312

313313
NumericLiteral(path) {
@@ -323,7 +323,7 @@ export async function minify(file: File, autocomplete?: string, {
323323
if (jsonValueIndex == -1)
324324
jsonValueIndex += jsonValues.push(path.node.value)
325325

326-
path.replaceWith(t.identifier(`_JSON_VALUE_${jsonValueIndex}_${uniqueID}_`))
326+
path.replaceWith(t.identifier(`_${uniqueID}_JSON_VALUE_${jsonValueIndex}_`))
327327
})())
328328
},
329329

@@ -342,7 +342,7 @@ export async function minify(file: File, autocomplete?: string, {
342342
if (jsonValueIndex == -1)
343343
jsonValueIndex += jsonValues.push(path.node.value)
344344

345-
path.replaceWith(t.identifier(`_JSON_VALUE_${jsonValueIndex}_${uniqueID}_`))
345+
path.replaceWith(t.identifier(`_${uniqueID}_JSON_VALUE_${jsonValueIndex}_`))
346346
},
347347

348348
ObjectProperty({ node }) {
@@ -355,7 +355,7 @@ export async function minify(file: File, autocomplete?: string, {
355355
jsonValueIndex += jsonValues.push(node.key.name)
356356

357357
node.computed = true
358-
node.key = t.identifier(`_JSON_VALUE_${jsonValueIndex}_${uniqueID}_`)
358+
node.key = t.identifier(`_${uniqueID}_JSON_VALUE_${jsonValueIndex}_`)
359359
},
360360

361361
RegExpLiteral(path) {
@@ -383,24 +383,24 @@ export async function minify(file: File, autocomplete?: string, {
383383
`let`,
384384
[
385385
t.variableDeclarator(
386-
t.identifier(`_JSON_VALUE_0_${uniqueID}_`),
386+
t.identifier(`_${uniqueID}_JSON_VALUE_0_`),
387387
t.memberExpression(
388388
t.taggedTemplateExpression(
389389
t.memberExpression(
390-
t.callExpression(t.identifier(`$${uniqueID}$SUBSCRIPT$scripts$quine`), []),
390+
t.callExpression(t.identifier(`$${uniqueID}$SUBSCRIPT$scripts$quine$`), []),
391391
t.identifier(`split`)
392392
),
393393
t.templateLiteral([ t.templateElement({ raw: `\t`, cooked: `\t` }, true) ], [])
394394
),
395-
t.identifier(`$${uniqueID}$SPLIT_INDEX`),
395+
t.identifier(`$${uniqueID}$SPLIT_INDEX$`),
396396
true
397397
)
398398
)
399399
]
400400
)
401401

402402
if (undefinedIsReferenced)
403-
variableDeclaration.declarations.push(t.variableDeclarator(t.identifier(`_UNDEFINED_${uniqueID}_`)))
403+
variableDeclaration.declarations.push(t.variableDeclarator(t.identifier(`_${uniqueID}_UNDEFINED_`)))
404404

405405
functionDeclaration.body.body.unshift(variableDeclaration)
406406
comment = jsonValues[0]
@@ -409,7 +409,7 @@ export async function minify(file: File, autocomplete?: string, {
409409
`let`,
410410
[
411411
t.variableDeclarator(
412-
t.identifier(`_JSON_VALUE_0_${uniqueID}_`),
412+
t.identifier(`_${uniqueID}_JSON_VALUE_0_`),
413413
t.callExpression(
414414
t.memberExpression(
415415
t.identifier(`JSON`),
@@ -419,12 +419,12 @@ export async function minify(file: File, autocomplete?: string, {
419419
t.memberExpression(
420420
t.taggedTemplateExpression(
421421
t.memberExpression(
422-
t.callExpression(t.identifier(`$${uniqueID}$SUBSCRIPT$scripts$quine`), []),
422+
t.callExpression(t.identifier(`$${uniqueID}$SUBSCRIPT$scripts$quine$`), []),
423423
t.identifier(`split`)
424424
),
425425
t.templateLiteral([ t.templateElement({ raw: `\t`, cooked: `\t` }, true) ], [])
426426
),
427-
t.identifier(`$${uniqueID}$SPLIT_INDEX`),
427+
t.identifier(`$${uniqueID}$SPLIT_INDEX$`),
428428
true
429429
)
430430
]
@@ -434,7 +434,7 @@ export async function minify(file: File, autocomplete?: string, {
434434
)
435435

436436
if (undefinedIsReferenced)
437-
variableDeclaration.declarations.push(t.variableDeclarator(t.identifier(`_UNDEFINED_${uniqueID}_`)))
437+
variableDeclaration.declarations.push(t.variableDeclarator(t.identifier(`_${uniqueID}_UNDEFINED_`)))
438438

439439
functionDeclaration.body.body.unshift(variableDeclaration)
440440
comment = JSON.stringify(jsonValues[0])
@@ -444,7 +444,7 @@ export async function minify(file: File, autocomplete?: string, {
444444
`let`,
445445
[
446446
t.variableDeclarator(
447-
t.arrayPattern(jsonValues.map((_, index) => t.identifier(`_JSON_VALUE_${index}_${uniqueID}_`))),
447+
t.arrayPattern(jsonValues.map((_, index) => t.identifier(`_${uniqueID}_JSON_VALUE_${index}_`))),
448448
t.callExpression(
449449
t.memberExpression(
450450
t.identifier(`JSON`),
@@ -454,12 +454,12 @@ export async function minify(file: File, autocomplete?: string, {
454454
t.memberExpression(
455455
t.taggedTemplateExpression(
456456
t.memberExpression(
457-
t.callExpression(t.identifier(`$${uniqueID}$SUBSCRIPT$scripts$quine`), []),
457+
t.callExpression(t.identifier(`$${uniqueID}$SUBSCRIPT$scripts$quine$`), []),
458458
t.identifier(`split`)
459459
),
460460
t.templateLiteral([ t.templateElement({ raw: `\t`, cooked: `\t` }, true) ], [])
461461
),
462-
t.identifier(`$${uniqueID}$SPLIT_INDEX`),
462+
t.identifier(`$${uniqueID}$SPLIT_INDEX$`),
463463
true
464464
)
465465
]
@@ -469,7 +469,7 @@ export async function minify(file: File, autocomplete?: string, {
469469
)
470470

471471
if (undefinedIsReferenced)
472-
variableDeclaration.declarations.push(t.variableDeclarator(t.identifier(`_UNDEFINED_${uniqueID}_`)))
472+
variableDeclaration.declarations.push(t.variableDeclarator(t.identifier(`_${uniqueID}_UNDEFINED_`)))
473473

474474
functionDeclaration.body.body.unshift(variableDeclaration)
475475
comment = JSON.stringify(jsonValues)
@@ -478,7 +478,7 @@ export async function minify(file: File, autocomplete?: string, {
478478
functionDeclaration.body.body.unshift(
479479
t.variableDeclaration(
480480
`let`,
481-
[ t.variableDeclarator(t.identifier(`_UNDEFINED_${uniqueID}_`)) ]
481+
[ t.variableDeclarator(t.identifier(`_${uniqueID}_UNDEFINED_`)) ]
482482
)
483483
)
484484
}
@@ -508,7 +508,7 @@ export async function minify(file: File, autocomplete?: string, {
508508
// this step affects the character count and can't happen after the count comparison
509509
if (comment != undefined) {
510510
code = spliceString(code, `${autocomplete ? `//${autocomplete}\n` : ``}\n//\t${comment}\t\n`, getFunctionBodyStart(code) + 1)
511-
code = code.replace(`$${uniqueID}$SPLIT_INDEX`, await minifyNumber(code.split(`\t`).findIndex(part => part == comment)))
511+
code = code.replace(`$${uniqueID}$SPLIT_INDEX$`, await minifyNumber(code.split(`\t`).findIndex(part => part == comment)))
512512
}
513513

514514
// if the script has a comment, it's gonna contain `SC$scripts$quine()`

src/processScript/postprocess.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ export function postprocess(code: string, seclevel: number, uniqueID: string) {
66
.replace(new RegExp(`\\$${uniqueID}\\$\\\\(?:\\\\)?\\$D\\$`, `g`), `_\\_D_S`)
77
.replace(new RegExp(`\\$${uniqueID}\\$\\\\(?:\\\\)?\\$FMCL\\$`, `g`), `_\\_FMCL_`)
88
.replace(new RegExp(`\\$${uniqueID}\\$\\\\(?:\\\\)?\\$G\\$`, `g`), `_\\_G_`)
9-
.replace(new RegExp(`\\$${uniqueID}\\$SUBSCRIPT\\$(\\w+)\\$(\\w+)`, `g`), `#${`nlmhf`[seclevel]}s.$1.$2`)
10-
.replace(new RegExp(`\\$${uniqueID}\\$DEBUG`, `g`), `#D`)
11-
.replace(new RegExp(`\\$${uniqueID}\\$FMCL`, `g`), `#FMCL`)
12-
.replace(new RegExp(`\\$${uniqueID}\\$GLOBAL`, `g`), `#G`)
13-
.replace(new RegExp(`\\$${uniqueID}\\$DB\\$(\\w+)`, `g`), `#db.$1`)
9+
.replace(new RegExp(`\\$${uniqueID}\\$SUBSCRIPT\\$(\\w+)\\$(\\w+)\\$`, `g`), `#${`nlmhf`[seclevel]}s.$1.$2`)
10+
.replace(new RegExp(`\\$${uniqueID}\\$DEBUG\\$`, `g`), `#D`)
11+
.replace(new RegExp(`\\$${uniqueID}\\$FMCL\\$`, `g`), `#FMCL`)
12+
.replace(new RegExp(`\\$${uniqueID}\\$GLOBAL\\$`, `g`), `#G`)
13+
.replace(new RegExp(`\\$${uniqueID}\\$DB\\$(\\w+)\\$`, `g`), `#db.$1`)
1414
.replace(new RegExp(`\\$${uniqueID}\\$SLASH_SLASH\\$`, `g`), `/\\/`)
1515
.replace(new RegExp(`\\$${uniqueID}\\$NOT_A_SUBSCRIPT\\$(#[\\w\\.]+)\\(\\$`, `g`), `$1\\(`)
1616
.replace(new RegExp(`\\$${uniqueID}\\$NOT_A_DB_CALL\\$(\\w+)\\$`, `g`), `#db.$1\\(`)

src/processScript/preprocess.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,9 @@ export async function preprocess(code: string, { uniqueID = `00000000000` }: Par
9191
else if ((match = /^#D[^\w$]/.exec(codeSlice)))
9292
code = spliceString(code, `$`, error.pos, 1)
9393
else if ((match = /^#FMCL/.exec(codeSlice)))
94-
code = spliceString(code, `$${uniqueID}$FMCL`, error.pos, match[0]!.length)
94+
code = spliceString(code, `$${uniqueID}$FMCL$`, error.pos, match[0]!.length)
9595
else if ((match = /^#G/.exec(codeSlice)))
96-
code = spliceString(code, `$${uniqueID}$GLOBAL`, error.pos, match[0]!.length)
96+
code = spliceString(code, `$${uniqueID}$GLOBAL$`, error.pos, match[0]!.length)
9797
else if ((match = /^#db\./.exec(codeSlice)))
9898
code = spliceString(code, `$`, error.pos, 1)
9999
else

0 commit comments

Comments
 (0)