Decompiler fix and compiler fix (not works for now)

This commit is contained in:
Michael Wain 2025-01-05 07:16:53 +03:00
parent 859938143e
commit 829b1342a7
7 changed files with 133 additions and 82 deletions
pom.xml
src/main/java/com/alterdekim/flash/decompiler

60
pom.xml

@ -6,12 +6,36 @@
<groupId>com.alterdekim.game</groupId> <groupId>com.alterdekim.game</groupId>
<artifactId>actionScriptDecompiler</artifactId> <artifactId>actionScriptDecompiler</artifactId>
<version>1.0-SNAPSHOT</version> <version>0.0.2</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>SWFDissect</name>
<description>
This project is a library for swf editing in Java.
DON'T USE THIS IN PRODUCTION. IT WAS CREATED FOR INTERNAL PURPOSES ONLY.
</description>
<url>https://blog.awain.net</url>
<developers>
<developer>
<name>Michael Wain</name>
<email>alterwain@protonmail.com</email>
<organization>The God</organization>
<organizationUrl>https://blog.awain.net</organizationUrl>
</developer>
</developers>
<licenses>
<license>
<name>The Apache License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
</license>
</licenses>
<properties> <properties>
<maven.compiler.source>11</maven.compiler.source> <maven.compiler.source>14</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target> <maven.compiler.target>14</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<exec.mainClass>com.alterdekim.flash.decompiler.Main</exec.mainClass> <exec.mainClass>com.alterdekim.flash.decompiler.Main</exec.mainClass>
</properties> </properties>
@ -54,15 +78,35 @@
<version>2.17.2</version> <version>2.17.2</version>
</dependency> </dependency>
</dependencies> </dependencies>
<build> <build>
<plugins> <plugins>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <artifactId>maven-clean-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<configuration> </plugin>
<source>14</source> <plugin>
<target>14</target> <artifactId>maven-surefire-plugin</artifactId>
</configuration> </plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-site-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
</plugin> </plugin>
</plugins> </plugins>
</build> </build>

@ -5,14 +5,12 @@ import com.alterdekim.flash.decompiler.item.*;
import com.alterdekim.flash.decompiler.tag.DoAction; import com.alterdekim.flash.decompiler.tag.DoAction;
import com.alterdekim.flash.decompiler.tag.ShockwaveTag; import com.alterdekim.flash.decompiler.tag.ShockwaveTag;
import com.alterdekim.flash.decompiler.tag.TagType; import com.alterdekim.flash.decompiler.tag.TagType;
import com.alterdekim.flash.decompiler.util.ActionField; import com.alterdekim.flash.decompiler.util.*;
import com.alterdekim.flash.decompiler.util.Bits;
import com.alterdekim.flash.decompiler.util.PayloadCompression;
import com.alterdekim.flash.decompiler.util.StringUtils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.javatuples.Triplet; import org.javatuples.Triplet;
import java.io.*; import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -48,6 +46,21 @@ public class FlashDecompiler {
return processPayload(data); return processPayload(data);
} }
public byte[] uncompressSWF(byte[] swfBytes) throws IOException {
compression = PayloadCompression.getByTag(swfBytes[0]);
byte[] data = Arrays.copyOfRange(swfBytes, 8, swfBytes.length);
switch (compression) {
case DEFLATE:
data = decompressDEFLATE(data);
break;
}
byte[] o = new byte[8 + data.length];
byte[] b = Arrays.copyOfRange(swfBytes, 0, 8);
System.arraycopy(b, 0, o, 0, 8);
System.arraycopy(data, 0, o, 8, data.length);
return o;
}
private ShockwaveFile processPayload(byte[] payload) { private ShockwaveFile processPayload(byte[] payload) {
BitSet bitSet = BitSet.valueOf(payload); BitSet bitSet = BitSet.valueOf(payload);
int c = (payload.length * 8); int c = (payload.length * 8);
@ -74,6 +87,7 @@ public class FlashDecompiler {
len = Bits.convert(bitSet.get(i, i+32), 32, true); len = Bits.convert(bitSet.get(i, i+32), 32, true);
i += 32; i += 32;
} }
log.info("Len: {}", len);
TagType tagType = TagType.fromId(type); TagType tagType = TagType.fromId(type);
if( tagType == TagType.DoAction ) { if( tagType == TagType.DoAction ) {
int g = i / 8; int g = i / 8;
@ -131,7 +145,13 @@ public class FlashDecompiler {
i++; i++;
tag.getActions().add(new ActionGetMember()); tag.getActions().add(new ActionGetMember());
break; break;
case Add:
log.info("Add");
i++;
tag.getActions().add(new ActionAdd());
break;
case Unknown: case Unknown:
log.info("Unknown tag has been hit: {} / {}", i, tag.getActions().get(tag.getActions().size()-1));
return tag; return tag;
default: default:
log.error("Unknown action field type: {}", StringUtils.bytesToHex(new byte[] {data[i]})); log.error("Unknown action field type: {}", StringUtils.bytesToHex(new byte[] {data[i]}));
@ -139,20 +159,22 @@ public class FlashDecompiler {
break; break;
} }
} }
log.info("Out of loop");
return tag; return tag;
} }
private Triplet<byte[], Integer, Integer> findNull(byte[] data, int i, int u) { private Triplet<byte[], Integer, Integer> findNull(byte[] data, int i, int u) {
int s = i; int s = i;
while( data[i] != 0x00 ) { while(data[i] != 0x00 ) {
i++; i++;
u++; u++;
} }
return Triplet.with(Arrays.copyOfRange(data, s, i), i, u); i++;
u++;
return Triplet.with(Arrays.copyOfRange(data, s, i-1), i, u);
} }
private int parseActionPush(byte[] data, int i, DoAction tag) { private int parseActionPush(byte[] data, int i, DoAction tag) {
//int start_action_index = i; // for debug
i++; i++;
int len_in_bytes = bytesToIntRev(Arrays.copyOfRange(data, i, i+2)); int len_in_bytes = bytesToIntRev(Arrays.copyOfRange(data, i, i+2));
int u = 0; int u = 0;
@ -166,8 +188,11 @@ public class FlashDecompiler {
// implement other types (float especially) // implement other types (float especially)
case STRING: case STRING:
i++; i++;
u++;
log.info("S0: {}", i);
var result = findNull(data, i, u); var result = findNull(data, i, u);
push.getItems().add(new StringItem(new String(result.getValue0()))); push.getItems().add(new StringItem(new String(result.getValue0(), StandardCharsets.UTF_8)));
log.info("STRING: {} {}", new String(result.getValue0(), StandardCharsets.UTF_8), result.getValue1());
i = result.getValue1(); i = result.getValue1();
u = result.getValue2(); u = result.getValue2();
break; break;
@ -215,12 +240,6 @@ public class FlashDecompiler {
} }
} }
tag.getActions().add(push); tag.getActions().add(push);
/*byte[] bv = Arrays.copyOfRange(data, start_action_index, i);
List<String> s = IntStream.range(0, bv.length)
.map(v -> bv[v])
.mapToObj(v -> Integer.toHexString(v))
.collect(Collectors.toList());
log.info("Push data block: {}", s);*/
return i; return i;
} }
@ -229,19 +248,18 @@ public class FlashDecompiler {
int bytes_len = bytesToIntRev(Arrays.copyOfRange(data, i, i+2)); int bytes_len = bytesToIntRev(Arrays.copyOfRange(data, i, i+2));
i += 2; i += 2;
int count = bytesToIntRev(Arrays.copyOfRange(data, i, i+2)); int count = bytesToIntRev(Arrays.copyOfRange(data, i, i+2));
log.info("ActionPoolSize: {}", count);
i += 2; i += 2;
int u = 1; int u = 1;
StringBuilder s = new StringBuilder(); List<Byte> s = new ArrayList<>();
while( u <= count ) { while( u <= count ) {
if( data[i] == 0x00 ) { if( data[i] == 0x00 ) {
actionPool.add(s.toString()); actionPool.add(new String( ByteUtil.toPrimitive(s), StandardCharsets.UTF_8 ));
s = new StringBuilder(); s.clear();
u++; u++;
i++; i++;
continue; continue;
} }
s.append((char) data[i]); s.add(data[i]);
i++; i++;
} }
tag.setActionPool(actionPool); tag.setActionPool(actionPool);

@ -17,7 +17,12 @@ import java.nio.file.StandardOpenOption;
public class Main { public class Main {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
FlashDecompiler decompiler = new FlashDecompiler(); FlashDecompiler decompiler = new FlashDecompiler();
ShockwaveFile file = decompiler.loadFromFile(new File("D:\\Documents\\FlashProjects\\Untitled-2.swf")); //
/*byte[] g = decompiler.uncompressSWF(Files.readAllBytes(new File("D:\\Documents\\rtmpSpring\\static\\swf\\cache\\rus\\resources[19].swf").toPath()));
Files.write(new File("res.swf").toPath(), g, StandardOpenOption.CREATE);*/
ShockwaveFile file = decompiler.loadFromFile(new File("D:\\Documents\\rtmpSpring\\static\\swf\\cache\\rus\\resources[19].swf"));
DoAction da = (DoAction) file.getTags() DoAction da = (DoAction) file.getTags()
.stream() .stream()

@ -9,8 +9,8 @@ import lombok.extern.slf4j.Slf4j;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Queue;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream;
@RequiredArgsConstructor @RequiredArgsConstructor
@Slf4j @Slf4j
@ -18,21 +18,19 @@ public class FlashCompiler {
private final Java2Flash pcode; private final Java2Flash pcode;
public byte[] compile() { public byte[] compile() {
List<Byte> data = new ArrayList<>(); List<Byte> data = new ArrayList<>(List.of((byte) 0x46, (byte) 0x57, (byte) 0x53));
data.addAll(List.of((byte) 0x46, (byte) 0x57, (byte) 0x53));
data.add((byte) 0x08); // swf version (8) data.add((byte) 0x08); // swf version (8)
List<Byte> d1 = new ArrayList<>(); List<Byte> d1 = new ArrayList<>();
d1.addAll(List.of((byte) 0x30, (byte) 0x0A, (byte) 0x00, (byte) 0xA0)); // rect d1.addAll(List.of((byte) 0x30, (byte) 0x0A, (byte) 0x00, (byte) 0xA0)); // rect
d1.addAll(List.of((byte) 0x00, (byte) 0x19)); // frameRate d1.addAll(List.of((byte) 0x00, (byte) 0x19)); // frameRate
d1.addAll(ByteUtil.intToBytes(1)); // frames Count d1.addAll(ByteUtil.intToBytes(1)); // frames Count
//List<CompileObject> tags = List.of(new SetBackgroundColor(new byte[] {0,0,0}), new DoAction(pcode.getPool(), pcode.getActions()), new ShowFrame(), new End()); d1.addAll(
//d1.addAll(tags.stream().map(CompileObject::compile).flatMap(Collection::stream).collect(Collectors.toList())); Stream.of(new SetBackgroundColor(new byte[] {0,0,0}), new DoAction(pcode.getPool(), pcode.getActions()), new ShowFrame(), new End())
d1.addAll(new SetBackgroundColor(new byte[] {0,0,0}).compile()); .map(CompileObject::compile)
d1.addAll(new DoAction(pcode.getPool(), pcode.getActions()).compile()); .flatMap(Collection::stream)
d1.addAll(new ShowFrame().compile()); .collect(Collectors.toList())
d1.addAll(new End().compile()); );
// fileSize u32 // fileSize u32
log.info("fileSize u32: {}", d1.size());
data.addAll(ByteUtil.intToBytes32(d1.size()+data.size()+4)); data.addAll(ByteUtil.intToBytes32(d1.size()+data.size()+4));
data.addAll(d1); data.addAll(d1);
return ByteUtil.toPrimitive(data); return ByteUtil.toPrimitive(data);

@ -48,7 +48,7 @@ public class DoAction extends ShockwaveTag implements CompileObject {
case INTEGER: case INTEGER:
data.addAll(ByteUtil.intToBytes32((Integer) item.getValue())); data.addAll(ByteUtil.intToBytes32((Integer) item.getValue()));
break; break;
case POOL_INDEX: case POOL_INDEX: // todo: make distiction between small POOL_INDEX and large POOL_INDEX
Integer index = (Integer) item.getValue(); Integer index = (Integer) item.getValue();
data.addAll(index < 256 ? ByteUtil.intToBytes8(index) : ByteUtil.intToBytes(index)); data.addAll(index < 256 ? ByteUtil.intToBytes8(index) : ByteUtil.intToBytes(index));
break; break;
@ -66,9 +66,7 @@ public class DoAction extends ShockwaveTag implements CompileObject {
case ConstantPool: case ConstantPool:
ActionConstantPool pool = (ActionConstantPool) action; ActionConstantPool pool = (ActionConstantPool) action;
data.addAll(ByteUtil.intToBytes(pool.getPool().size())); data.addAll(ByteUtil.intToBytes(pool.getPool().size()));
pool.getPool().forEach(s -> { pool.getPool().forEach(s -> data.addAll(fromString(s)));
data.addAll(fromString(s));
});
bytes.addAll(ByteUtil.intToBytes(data.size())); bytes.addAll(ByteUtil.intToBytes(data.size()));
bytes.addAll(data); bytes.addAll(data);
break; break;
@ -82,8 +80,8 @@ public class DoAction extends ShockwaveTag implements CompileObject {
private List<Byte> fromString(String s) { private List<Byte> fromString(String s) {
List<Byte> l = new ArrayList<>(); List<Byte> l = new ArrayList<>();
for( char c : s.toCharArray() ) { for( byte c : s.getBytes() ) {
l.add((byte) c); l.add(c);
} }
l.add((byte) 0x0); l.add((byte) 0x0);
return l; return l;

@ -61,15 +61,13 @@ public class Flash2Java {
case GetMember: case GetMember:
getMember(); getMember();
break; break;
default:
// ignore
} }
} }
return this.vars; return this.vars;
} }
private void initObject() { private void initObject() {
int numberOfEntries = (int) Double.parseDouble(this.stack.pop().getValue()+""); // harsh way int numberOfEntries = (int) Double.parseDouble(this.stack.pop().getValue()+""); // harsh way todo: rewrite
HashMap<String, Object> obj = new HashMap<>(); HashMap<String, Object> obj = new HashMap<>();
for( int i = 0; i < numberOfEntries; i++ ) { for( int i = 0; i < numberOfEntries; i++ ) {
Object val = valConverter( this.stack.pop() ); Object val = valConverter( this.stack.pop() );
@ -80,7 +78,7 @@ public class Flash2Java {
} }
private void initArray() { private void initArray() {
int numberOfElements = (int) Double.parseDouble(this.stack.pop().getValue()+""); // harsh way int numberOfElements = (int) Double.parseDouble(this.stack.pop().getValue()+""); // harsh way todo: rewrite
HashMap<String, Object> obj = new HashMap<>(); HashMap<String, Object> obj = new HashMap<>();
for( int i = 0; i < numberOfElements; i++ ) { for( int i = 0; i < numberOfElements; i++ ) {
Object val = valConverter( this.stack.pop() ); Object val = valConverter( this.stack.pop() );
@ -90,22 +88,9 @@ public class Flash2Java {
} }
private void reduceViaAdder() { private void reduceViaAdder() {
/* String s1 = valToString( this.stack.pop() );
// get last 2 elements from stack String s2 = valToString( this.stack.pop() );
List<Integer> si = new ArrayList<>(); this.stack.push(new StringItem(s2 + s1));
List<ByteCodeItem> g = new ArrayList<>();
for( int i = this.stack.size()-1; i >= 0; i-- ) {
ByteCodeAction e = this.stack.get(i);
if( e.getType() != ActionField.Push ) continue;
ActionPush push = (ActionPush) e;
if( push.getItems().size() >= 2 ) {
StringItem from = (StringItem) push.getItems().get(push.getItems().size()-1);
StringItem to = (StringItem) push.getItems().get(push.getItems().size()-2);
/*push.getItems().remove()
((String) to.getValue()) + ((String) from.getValue())*//*
}
}
*/
} }
private void getMember() { private void getMember() {
@ -117,7 +102,7 @@ public class Flash2Java {
this.stack.push(new GetMemberItem(l)); this.stack.push(new GetMemberItem(l));
return; return;
} }
String key = valToString(val); String key = (String) val.getValue();
this.stack.push(new GetMemberItem(new ArrayDeque<>(List.of(key, property)))); this.stack.push(new GetMemberItem(new ArrayDeque<>(List.of(key, property))));
} }
@ -126,11 +111,7 @@ public class Flash2Java {
String index = valToString( this.stack.pop() ); String index = valToString( this.stack.pop() );
ByteCodeItem varOrMember = this.stack.pop(); ByteCodeItem varOrMember = this.stack.pop();
if( varOrMember.getType() == ActionPushType.GET_VARIABLE ) { if( varOrMember.getType() == ActionPushType.GET_VARIABLE ) {
Map<String, Object> m = (Map<String, Object>) this.vars.get(valToString( varOrMember )); Map<String, Object> m = (Map<String, Object>) valConverter(varOrMember);
if( val.getType() == ActionPushType.GET_VARIABLE ) {
m.put(index, this.vars.get(valToString(val)));
return;
}
m.put(index, valConverter(val)); m.put(index, valConverter(val));
return; return;
} }
@ -156,16 +137,19 @@ public class Flash2Java {
return fromPool( (Integer) item.getValue() ); return fromPool( (Integer) item.getValue() );
case STRING: case STRING:
return (String) item.getValue(); return (String) item.getValue();
case GET_VARIABLE:
return (String) vars.get((String) item.getValue());
default: default:
return String.valueOf( item.getValue() ); return String.valueOf( item.getValue() );
} }
} }
private Object valConverter(ByteCodeItem item) { private Object valConverter(ByteCodeItem item) {
if( item.getType() == ActionPushType.POOL_INDEX ) { return switch (item.getType()) {
return fromPool((Integer) item.getValue()); case POOL_INDEX -> fromPool((Integer) item.getValue());
} case GET_VARIABLE -> vars.get((String) item.getValue());
return item.getValue(); default -> item.getValue();
};
} }
private void setVariable() { private void setVariable() {
@ -182,10 +166,6 @@ public class Flash2Java {
private void defineLocal() { private void defineLocal() {
ByteCodeItem val = this.stack.pop(); ByteCodeItem val = this.stack.pop();
String name = valToString(this.stack.pop()); String name = valToString(this.stack.pop());
if( val.getType() == ActionPushType.GET_VARIABLE ) {
this.vars.put(name, this.vars.get(valToString(val)));
return;
}
this.vars.put(name, valConverter(val)); this.vars.put(name, valConverter(val));
} }

@ -13,13 +13,14 @@ import java.util.*;
@Getter @Getter
public class Java2Flash { public class Java2Flash {
private int cnt = 0; // abolish
private List<String> pool; private List<String> pool;
private List<ByteCodeAction> actions; private List<ByteCodeAction> actions;
public Java2Flash convert(Map<String, Object> map) { public Java2Flash convert(Map<String, Object> map) {
this.actions = new ArrayList<>(); this.actions = new ArrayList<>();
this.pool = new ArrayList<>(); this.pool = new ArrayList<>();
this.pool.addAll(extractAllStrings(new HashSet<>(65535), map)); // maybe make a check if hashset size is bigger than initialCapacity this.pool.addAll(extractAllStrings(new HashSet<>(65535), map));
this.actions.add(new ActionConstantPool(this.pool)); this.actions.add(new ActionConstantPool(this.pool));
map.keySet().forEach(k -> createVariable(map, k)); map.keySet().forEach(k -> createVariable(map, k));
map.keySet().stream().filter(k -> map.get(k).getClass().getSimpleName().equals("HashMap")).forEach(k -> setMembers(map, k)); map.keySet().stream().filter(k -> map.get(k).getClass().getSimpleName().equals("HashMap")).forEach(k -> setMembers(map, k));
@ -77,7 +78,6 @@ public class Java2Flash {
private void createVariable(Map<String, Object> map, String key) { private void createVariable(Map<String, Object> map, String key) {
List<ByteCodeItem> items = new ArrayList<>(); List<ByteCodeItem> items = new ArrayList<>();
items.add(fromString(key)); items.add(fromString(key));
log.info("Type: {}", map.get(key).getClass().getSimpleName());
items.add(switch (map.get(key).getClass().getSimpleName()) { items.add(switch (map.get(key).getClass().getSimpleName()) {
case "String" -> fromString((String) map.get(key)); case "String" -> fromString((String) map.get(key));
case "Integer" -> new IntegerItem((Integer) map.get(key)); case "Integer" -> new IntegerItem((Integer) map.get(key));
@ -102,13 +102,21 @@ public class Java2Flash {
try { try {
Integer.parseInt(k); Integer.parseInt(k);
} catch (Exception e) { } catch (Exception e) {
pool.add(k); if( pool.size() < 3000 ) {
pool.add(k);
cnt++;
}
} }
}); });
map.values().forEach(o -> { map.values().forEach(o -> {
switch (o.getClass().getSimpleName()) { switch (o.getClass().getSimpleName()) {
case "HashMap" -> extractAllStrings(pool, (HashMap<String, Object>) o); case "HashMap" -> extractAllStrings(pool, (HashMap<String, Object>) o);
case "String" -> pool.add((String) o); case "String" -> {
if( pool.size() < 3000 ) {
pool.add((String) o);
cnt++;
}
}
} }
}); });
return pool; return pool;