Skip to content

Commit

Permalink
✨ feat: Add jsfuck decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed May 16, 2024
1 parent 7b2a122 commit 74958f4
Show file tree
Hide file tree
Showing 13 changed files with 409 additions and 126 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ public Swc4jAstExprOrSpread setSpread(Swc4jSpan spread) {
return this;
}

@Override
public String toString() {
String str = spread.map(span -> "...").orElse("");
str += expr.toString();
return str;
}

@Override
public Swc4jAstVisitorResponse visit(ISwc4jAstVisitor visitor) {
switch (visitor.visitExprOrSpread(this)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.caoccao.javet.swc4j.ast.enums.Swc4jAstType;
import com.caoccao.javet.swc4j.ast.expr.Swc4jAstExprOrSpread;
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAst;
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAstCoercionPrimitive;
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAstExpr;
import com.caoccao.javet.swc4j.ast.visitors.ISwc4jAstVisitor;
import com.caoccao.javet.swc4j.ast.visitors.Swc4jAstVisitorResponse;
Expand All @@ -37,7 +38,7 @@
@Jni2RustClass(filePath = Jni2RustFilePath.AstUtils)
public class Swc4jAstArrayLit
extends Swc4jAst
implements ISwc4jAstExpr {
implements ISwc4jAstExpr, ISwc4jAstCoercionPrimitive {
protected final List<Optional<Swc4jAstExprOrSpread>> elems;

@Jni2RustMethod
Expand All @@ -51,6 +52,56 @@ public Swc4jAstArrayLit(
this.elems.stream().filter(Optional::isPresent).map(Optional::get).forEach(node -> node.setParent(this));
}

@Override
public boolean asBoolean() {
return false;
}

@Override
public byte asByte() {
return Double.valueOf(asDouble()).byteValue();
}

@Override
public double asDouble() {
if (!elems.isEmpty()) {
if (elems.size() == 1) {
return elems.get(0)
.map(Swc4jAstExprOrSpread::getExpr)
.filter(n -> n.getType() == Swc4jAstType.Number)
.map(n -> (Swc4jAstNumber) n)
.map(Swc4jAstNumber::getValue)
.orElse(Double.NaN);
}
}
return 0;
}

@Override
public float asFloat() {
return Double.valueOf(asDouble()).floatValue();
}

@Override
public int asInt() {
return Double.valueOf(asDouble()).intValue();
}

@Override
public long asLong() {
return Double.valueOf(asDouble()).longValue();
}

@Override
public short asShort() {
return Double.valueOf(asDouble()).shortValue();
}

@Override
public String asString() {
return toString();
}

@Override
public List<ISwc4jAst> getChildNodes() {
List<ISwc4jAst> childNodes = SimpleList.of();
Expand Down Expand Up @@ -88,6 +139,15 @@ public boolean replaceNode(ISwc4jAst oldNode, ISwc4jAst newNode) {
return false;
}

@Override
public String toString() {
return elems.stream()
.map(optionalElem -> optionalElem
.map(Swc4jAstExprOrSpread::toString)
.orElse(""))
.collect(Collectors.joining(","));
}

@Override
public Swc4jAstVisitorResponse visit(ISwc4jAstVisitor visitor) {
switch (visitor.visitArrayLit(this)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.caoccao.javet.swc4j.ast.enums.Swc4jAstType;
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAst;
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAstLit;
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAstPrimitiveCoercion;
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAstCoercionPrimitive;
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAstTsLit;
import com.caoccao.javet.swc4j.ast.visitors.ISwc4jAstVisitor;
import com.caoccao.javet.swc4j.ast.visitors.Swc4jAstVisitorResponse;
Expand All @@ -34,7 +34,7 @@
@Jni2RustClass(filePath = Jni2RustFilePath.AstUtils)
public class Swc4jAstBool
extends Swc4jAst
implements ISwc4jAstLit, ISwc4jAstTsLit, ISwc4jAstPrimitiveCoercion {
implements ISwc4jAstLit, ISwc4jAstTsLit, ISwc4jAstCoercionPrimitive {
protected boolean value;

@Jni2RustMethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
@Jni2RustClass(filePath = Jni2RustFilePath.AstUtils)
public class Swc4jAstNumber
extends Swc4jAst
implements ISwc4jAstLit, ISwc4jAstPropName, ISwc4jAstTsLit, ISwc4jAstPrimitiveCoercion {
implements ISwc4jAstLit, ISwc4jAstPropName, ISwc4jAstTsLit, ISwc4jAstCoercionPrimitive {
@Jni2RustField(componentAtom = true)
protected Optional<String> raw;
protected double value;
Expand All @@ -53,6 +53,10 @@ public static Swc4jAstNumber create(double value) {
return new Swc4jAstNumber(value, Double.toString(value), Swc4jSpan.DUMMY);
}

public static Swc4jAstNumber create(double value, String raw) {
return new Swc4jAstNumber(value, raw, Swc4jSpan.DUMMY);
}

@Override
public boolean asBoolean() {
return value != 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.caoccao.javet.swc4j.jni2rust.*;
import com.caoccao.javet.swc4j.span.Swc4jSpan;
import com.caoccao.javet.swc4j.utils.AssertionUtils;
import com.caoccao.javet.swc4j.utils.StringUtils;

import java.util.List;
import java.util.Optional;
Expand All @@ -32,7 +33,7 @@
public class Swc4jAstStr
extends Swc4jAst
implements ISwc4jAstLit, ISwc4jAstModuleExportName, ISwc4jAstPropName, ISwc4jAstTsModuleName, ISwc4jAstTsLit,
ISwc4jAstTsEnumMemberId {
ISwc4jAstTsEnumMemberId, ISwc4jAstCoercionPrimitive {
@Jni2RustField(componentAtom = true)
protected Optional<String> raw;
@Jni2RustField(atom = true)
Expand All @@ -48,6 +49,54 @@ public Swc4jAstStr(
setValue(value);
}

public static Swc4jAstStr create(String value) {
return new Swc4jAstStr(value, "\"" + value + "\"", Swc4jSpan.DUMMY);
}

@Override
public boolean asBoolean() {
return StringUtils.isNotEmpty(value);
}

@Override
public byte asByte() {
return Double.valueOf(asDouble()).byteValue();
}

@Override
public double asDouble() {
try {
return Double.parseDouble(value);
} catch (Throwable t) {
return Double.NaN;
}
}

@Override
public float asFloat() {
return Double.valueOf(asDouble()).floatValue();
}

@Override
public int asInt() {
return Double.valueOf(asDouble()).intValue();
}

@Override
public long asLong() {
return Double.valueOf(asDouble()).longValue();
}

@Override
public short asShort() {
return Double.valueOf(asDouble()).shortValue();
}

@Override
public String asString() {
return toString();
}

@Override
public List<ISwc4jAst> getChildNodes() {
return EMPTY_CHILD_NODES;
Expand Down Expand Up @@ -85,7 +134,7 @@ public Swc4jAstStr setValue(String value) {

@Override
public String toString() {
return raw.orElse(null);
return value;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public interface ISwc4jAst {
* @since 0.4.0
*/
@SuppressWarnings("unchecked")
default <T extends ISwc4jAst> T as(Class<T> clazz) {
default <T> T as(Class<T> clazz) {
return clazz.isAssignableFrom(getClass()) ? (T) this : null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.caoccao.javet.swc4j.ast.interfaces;

public interface ISwc4jAstPrimitiveCoercion {
public interface ISwc4jAstCoercionPrimitive {
boolean asBoolean();

byte asByte();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.caoccao.javet.swc4j.ast.interfaces;

import com.caoccao.javet.swc4j.ast.clazz.Swc4jAstPrivateName;
import com.caoccao.javet.swc4j.ast.enums.Swc4jAstType;
import com.caoccao.javet.swc4j.ast.expr.*;
import com.caoccao.javet.swc4j.ast.expr.lit.Swc4jAstArrayLit;
import com.caoccao.javet.swc4j.ast.expr.lit.Swc4jAstObjectLit;
Expand Down Expand Up @@ -69,4 +70,11 @@
public interface ISwc4jAstExpr
extends ISwc4jAstVarDeclOrExpr, ISwc4jAstPat, ISwc4jAstJsxExpr, ISwc4jAstCallee, ISwc4jAstBlockStmtOrExpr,
ISwc4jAstAssignTarget {
default ISwc4jAstExpr unParenExpr() {
ISwc4jAstExpr expr = this;
while (expr.getType() == Swc4jAstType.ParenExpr) {
expr = expr.as(Swc4jAstParenExpr.class).getExpr();
}
return expr;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2024. caoccao.com Sam Cao
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.caoccao.javet.swc4j.plugins.jsfuck;

import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAstProgram;
import com.caoccao.javet.swc4j.plugins.ISwc4jPluginHost;

public class Swc4jPluginHostJsFuckDecoder implements ISwc4jPluginHost {
@Override
public boolean process(ISwc4jAstProgram<?> program) {
Swc4jPluginVisitorJsFuckDecoder jsFuckDecoder = new Swc4jPluginVisitorJsFuckDecoder();
do {
jsFuckDecoder.reset();
program.visit(jsFuckDecoder);
} while (jsFuckDecoder.getCount() > 0);
return true;
}
}
Loading

0 comments on commit 74958f4

Please sign in to comment.