JADecomp 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- jadecomp-0.1.0/LICENSE +21 -0
- jadecomp-0.1.0/PKG-INFO +65 -0
- jadecomp-0.1.0/README.md +46 -0
- jadecomp-0.1.0/pyproject.toml +39 -0
- jadecomp-0.1.0/setup.cfg +4 -0
- jadecomp-0.1.0/src/JADecomp.egg-info/PKG-INFO +65 -0
- jadecomp-0.1.0/src/JADecomp.egg-info/SOURCES.txt +14 -0
- jadecomp-0.1.0/src/JADecomp.egg-info/dependency_links.txt +1 -0
- jadecomp-0.1.0/src/JADecomp.egg-info/entry_points.txt +2 -0
- jadecomp-0.1.0/src/JADecomp.egg-info/requires.txt +2 -0
- jadecomp-0.1.0/src/JADecomp.egg-info/top_level.txt +1 -0
- jadecomp-0.1.0/src/jadecomp/__init__.py +4 -0
- jadecomp-0.1.0/src/jadecomp/__main__.py +4 -0
- jadecomp-0.1.0/src/jadecomp/cli.py +35 -0
- jadecomp-0.1.0/src/jadecomp/core.py +90 -0
- jadecomp-0.1.0/src/jadecomp/engine.py +130 -0
jadecomp-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Quaniries
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
jadecomp-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: JADecomp
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Java Archive Decompiler
|
|
5
|
+
Author: Quaniries
|
|
6
|
+
License: MIT
|
|
7
|
+
Keywords: java,decompiler,jar,class,bytecode
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Classifier: Topic :: Software Development :: Disassemblers
|
|
12
|
+
Classifier: Topic :: Utilities
|
|
13
|
+
Requires-Python: >=3.7
|
|
14
|
+
Description-Content-Type: text/markdown
|
|
15
|
+
License-File: LICENSE
|
|
16
|
+
Requires-Dist: tqdm
|
|
17
|
+
Requires-Dist: colorama
|
|
18
|
+
Dynamic: license-file
|
|
19
|
+
|
|
20
|
+
# JADecomp
|
|
21
|
+
|
|
22
|
+
JADecomp (Java Archive Decompiler) is a zero dependency Python tool for stripping apart .jar and .class files. It's built for speed and works entirely without a Java Runtime (JRE). If you need to peek inside a JAR without setting up a full Java environment, this is for you.
|
|
23
|
+
|
|
24
|
+
## Why use this?
|
|
25
|
+
|
|
26
|
+
Most Java decompilers are written in Java, which means you need a JRE just to run the tool. JADecomp is pure Python. It parses the JVM binary format directly and reconstructs the class structure and method bytecode.
|
|
27
|
+
|
|
28
|
+
## Installation
|
|
29
|
+
|
|
30
|
+
You can install JADecomp directly from PyPI:
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
pip install JADecomp
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## How to use it
|
|
37
|
+
|
|
38
|
+
The main command is `jad`. It handles single files or entire directories.
|
|
39
|
+
|
|
40
|
+
### Decompile a single JAR
|
|
41
|
+
```bash
|
|
42
|
+
jad appserve.jar
|
|
43
|
+
```
|
|
44
|
+
This creates an `appserve_decompiled` folder with the source.
|
|
45
|
+
|
|
46
|
+
### Bulk decompile a directory
|
|
47
|
+
If you have a folder full of Java libraries or legacy archives:
|
|
48
|
+
```bash
|
|
49
|
+
jad "C:\path\to\java\libs"
|
|
50
|
+
```
|
|
51
|
+
It will recursively find every .jar and .class file and decompile them into one output tree.
|
|
52
|
+
|
|
53
|
+
### Custom output folder
|
|
54
|
+
```bash
|
|
55
|
+
jad appserve.jar -o ./out
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## How it works (The Tech)
|
|
59
|
+
|
|
60
|
+
JADecomp uses a custom native engine (`engine.py`) to map out the constant pool, fields, and methods of a class file.
|
|
61
|
+
|
|
62
|
+
Instead of just giving you empty method bodies, it disassembles the JVM bytecode into human readable opcodes (like `invokevirtual`, `getstatic`, `ldc`). This gives you a low level view of what the code is doing without needing the original source.
|
|
63
|
+
|
|
64
|
+
## License
|
|
65
|
+
MIT
|
jadecomp-0.1.0/README.md
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# JADecomp
|
|
2
|
+
|
|
3
|
+
JADecomp (Java Archive Decompiler) is a zero dependency Python tool for stripping apart .jar and .class files. It's built for speed and works entirely without a Java Runtime (JRE). If you need to peek inside a JAR without setting up a full Java environment, this is for you.
|
|
4
|
+
|
|
5
|
+
## Why use this?
|
|
6
|
+
|
|
7
|
+
Most Java decompilers are written in Java, which means you need a JRE just to run the tool. JADecomp is pure Python. It parses the JVM binary format directly and reconstructs the class structure and method bytecode.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
You can install JADecomp directly from PyPI:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
pip install JADecomp
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## How to use it
|
|
18
|
+
|
|
19
|
+
The main command is `jad`. It handles single files or entire directories.
|
|
20
|
+
|
|
21
|
+
### Decompile a single JAR
|
|
22
|
+
```bash
|
|
23
|
+
jad appserve.jar
|
|
24
|
+
```
|
|
25
|
+
This creates an `appserve_decompiled` folder with the source.
|
|
26
|
+
|
|
27
|
+
### Bulk decompile a directory
|
|
28
|
+
If you have a folder full of Java libraries or legacy archives:
|
|
29
|
+
```bash
|
|
30
|
+
jad "C:\path\to\java\libs"
|
|
31
|
+
```
|
|
32
|
+
It will recursively find every .jar and .class file and decompile them into one output tree.
|
|
33
|
+
|
|
34
|
+
### Custom output folder
|
|
35
|
+
```bash
|
|
36
|
+
jad appserve.jar -o ./out
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## How it works (The Tech)
|
|
40
|
+
|
|
41
|
+
JADecomp uses a custom native engine (`engine.py`) to map out the constant pool, fields, and methods of a class file.
|
|
42
|
+
|
|
43
|
+
Instead of just giving you empty method bodies, it disassembles the JVM bytecode into human readable opcodes (like `invokevirtual`, `getstatic`, `ldc`). This gives you a low level view of what the code is doing without needing the original source.
|
|
44
|
+
|
|
45
|
+
## License
|
|
46
|
+
MIT
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61.0"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "JADecomp"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Java Archive Decompiler"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
authors = [
|
|
11
|
+
{ name = "Quaniries" }
|
|
12
|
+
]
|
|
13
|
+
license = { text = "MIT" }
|
|
14
|
+
requires-python = ">=3.7"
|
|
15
|
+
dependencies = [
|
|
16
|
+
"tqdm",
|
|
17
|
+
"colorama",
|
|
18
|
+
]
|
|
19
|
+
keywords = ["java", "decompiler", "jar", "class", "bytecode"]
|
|
20
|
+
classifiers = [
|
|
21
|
+
"Programming Language :: Python :: 3",
|
|
22
|
+
"License :: OSI Approved :: MIT License",
|
|
23
|
+
"Operating System :: OS Independent",
|
|
24
|
+
"Topic :: Software Development :: Disassemblers",
|
|
25
|
+
"Topic :: Utilities",
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
[project.scripts]
|
|
29
|
+
jad = "jadecomp.cli:main"
|
|
30
|
+
|
|
31
|
+
[tool.setuptools]
|
|
32
|
+
package-dir = {"" = "src"}
|
|
33
|
+
|
|
34
|
+
[tool.setuptools.packages.find]
|
|
35
|
+
where = ["src"]
|
|
36
|
+
exclude = ["tests*", "docs*", "example*"]
|
|
37
|
+
|
|
38
|
+
[tool.setuptools.package-data]
|
|
39
|
+
"*" = ["*.md"]
|
jadecomp-0.1.0/setup.cfg
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: JADecomp
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Java Archive Decompiler
|
|
5
|
+
Author: Quaniries
|
|
6
|
+
License: MIT
|
|
7
|
+
Keywords: java,decompiler,jar,class,bytecode
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Classifier: Topic :: Software Development :: Disassemblers
|
|
12
|
+
Classifier: Topic :: Utilities
|
|
13
|
+
Requires-Python: >=3.7
|
|
14
|
+
Description-Content-Type: text/markdown
|
|
15
|
+
License-File: LICENSE
|
|
16
|
+
Requires-Dist: tqdm
|
|
17
|
+
Requires-Dist: colorama
|
|
18
|
+
Dynamic: license-file
|
|
19
|
+
|
|
20
|
+
# JADecomp
|
|
21
|
+
|
|
22
|
+
JADecomp (Java Archive Decompiler) is a zero dependency Python tool for stripping apart .jar and .class files. It's built for speed and works entirely without a Java Runtime (JRE). If you need to peek inside a JAR without setting up a full Java environment, this is for you.
|
|
23
|
+
|
|
24
|
+
## Why use this?
|
|
25
|
+
|
|
26
|
+
Most Java decompilers are written in Java, which means you need a JRE just to run the tool. JADecomp is pure Python. It parses the JVM binary format directly and reconstructs the class structure and method bytecode.
|
|
27
|
+
|
|
28
|
+
## Installation
|
|
29
|
+
|
|
30
|
+
You can install JADecomp directly from PyPI:
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
pip install JADecomp
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## How to use it
|
|
37
|
+
|
|
38
|
+
The main command is `jad`. It handles single files or entire directories.
|
|
39
|
+
|
|
40
|
+
### Decompile a single JAR
|
|
41
|
+
```bash
|
|
42
|
+
jad appserve.jar
|
|
43
|
+
```
|
|
44
|
+
This creates an `appserve_decompiled` folder with the source.
|
|
45
|
+
|
|
46
|
+
### Bulk decompile a directory
|
|
47
|
+
If you have a folder full of Java libraries or legacy archives:
|
|
48
|
+
```bash
|
|
49
|
+
jad "C:\path\to\java\libs"
|
|
50
|
+
```
|
|
51
|
+
It will recursively find every .jar and .class file and decompile them into one output tree.
|
|
52
|
+
|
|
53
|
+
### Custom output folder
|
|
54
|
+
```bash
|
|
55
|
+
jad appserve.jar -o ./out
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## How it works (The Tech)
|
|
59
|
+
|
|
60
|
+
JADecomp uses a custom native engine (`engine.py`) to map out the constant pool, fields, and methods of a class file.
|
|
61
|
+
|
|
62
|
+
Instead of just giving you empty method bodies, it disassembles the JVM bytecode into human readable opcodes (like `invokevirtual`, `getstatic`, `ldc`). This gives you a low level view of what the code is doing without needing the original source.
|
|
63
|
+
|
|
64
|
+
## License
|
|
65
|
+
MIT
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
src/JADecomp.egg-info/PKG-INFO
|
|
5
|
+
src/JADecomp.egg-info/SOURCES.txt
|
|
6
|
+
src/JADecomp.egg-info/dependency_links.txt
|
|
7
|
+
src/JADecomp.egg-info/entry_points.txt
|
|
8
|
+
src/JADecomp.egg-info/requires.txt
|
|
9
|
+
src/JADecomp.egg-info/top_level.txt
|
|
10
|
+
src/jadecomp/__init__.py
|
|
11
|
+
src/jadecomp/__main__.py
|
|
12
|
+
src/jadecomp/cli.py
|
|
13
|
+
src/jadecomp/core.py
|
|
14
|
+
src/jadecomp/engine.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
jadecomp
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import argparse
|
|
2
|
+
import sys
|
|
3
|
+
from .core import Decompiler
|
|
4
|
+
|
|
5
|
+
def main():
|
|
6
|
+
parser = argparse.ArgumentParser(
|
|
7
|
+
prog="jad",
|
|
8
|
+
description="Java Archive Decompiler (JADecomp)"
|
|
9
|
+
)
|
|
10
|
+
parser.add_argument(
|
|
11
|
+
"input",
|
|
12
|
+
help="Path to a .jar file, .class file, or a directory containing them"
|
|
13
|
+
)
|
|
14
|
+
parser.add_argument(
|
|
15
|
+
"-o", "--output",
|
|
16
|
+
help="Directory where decompiled source should be saved"
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
args = parser.parse_args()
|
|
20
|
+
|
|
21
|
+
try:
|
|
22
|
+
decompiler = Decompiler()
|
|
23
|
+
print(f"Analyzing {args.input}...")
|
|
24
|
+
success, message = decompiler.decompile(args.input, args.output)
|
|
25
|
+
if success:
|
|
26
|
+
print(f"\n{message}")
|
|
27
|
+
else:
|
|
28
|
+
print(f"\nError: {message}", file=sys.stderr)
|
|
29
|
+
sys.exit(1)
|
|
30
|
+
except Exception as e:
|
|
31
|
+
print(f"\nFatal error: {e}", file=sys.stderr)
|
|
32
|
+
sys.exit(1)
|
|
33
|
+
|
|
34
|
+
if __name__ == "__main__":
|
|
35
|
+
main()
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import zipfile
|
|
3
|
+
import sys
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
from tqdm import tqdm
|
|
6
|
+
from colorama import init
|
|
7
|
+
from .engine import NativeEngine
|
|
8
|
+
|
|
9
|
+
class Decompiler:
|
|
10
|
+
def __init__(self):
|
|
11
|
+
self.engine = NativeEngine()
|
|
12
|
+
init()
|
|
13
|
+
|
|
14
|
+
def _get_files(self, input_path):
|
|
15
|
+
input_path = Path(input_path).resolve()
|
|
16
|
+
if input_path.is_file():
|
|
17
|
+
if input_path.suffix.lower() == ".jar":
|
|
18
|
+
with zipfile.ZipFile(input_path, "r") as jar:
|
|
19
|
+
return [(input_path, f) for f in jar.namelist() if f.endswith(".class")]
|
|
20
|
+
elif input_path.suffix.lower() == ".class":
|
|
21
|
+
return [(input_path, None)]
|
|
22
|
+
elif input_path.is_dir():
|
|
23
|
+
files = []
|
|
24
|
+
for root, _, filenames in os.walk(input_path):
|
|
25
|
+
for f in filenames:
|
|
26
|
+
f_path = Path(root) / f
|
|
27
|
+
if f.endswith(".jar"):
|
|
28
|
+
with zipfile.ZipFile(f_path, "r") as jar:
|
|
29
|
+
files.extend([(f_path, jf) for jf in jar.namelist() if jf.endswith(".class")])
|
|
30
|
+
elif f.endswith(".class"):
|
|
31
|
+
files.append((f_path, None))
|
|
32
|
+
return files
|
|
33
|
+
return []
|
|
34
|
+
|
|
35
|
+
def decompile(self, input_path, output_dir=None):
|
|
36
|
+
input_path = Path(input_path).resolve()
|
|
37
|
+
if not input_path.exists():
|
|
38
|
+
raise FileNotFoundError(f"Input path not found: {input_path}")
|
|
39
|
+
|
|
40
|
+
all_tasks = self._get_files(input_path)
|
|
41
|
+
if not all_tasks:
|
|
42
|
+
return True, "No Java files found to decompile."
|
|
43
|
+
|
|
44
|
+
if not output_dir:
|
|
45
|
+
output_dir = input_path.parent / f"{input_path.stem}_decompiled"
|
|
46
|
+
output_dir = Path(output_dir).resolve()
|
|
47
|
+
output_dir.mkdir(parents=True, exist_ok=True)
|
|
48
|
+
|
|
49
|
+
pbar = tqdm(
|
|
50
|
+
total=len(all_tasks),
|
|
51
|
+
bar_format="{percentage:3.0f}% |{bar}|",
|
|
52
|
+
ncols=80
|
|
53
|
+
)
|
|
54
|
+
sys.stdout.write("\n\033[F")
|
|
55
|
+
|
|
56
|
+
for container, member in all_tasks:
|
|
57
|
+
try:
|
|
58
|
+
if member:
|
|
59
|
+
with zipfile.ZipFile(container, "r") as jar:
|
|
60
|
+
data = jar.read(member)
|
|
61
|
+
rel_path = member.replace(".class", ".java")
|
|
62
|
+
display_name = member.replace("/", ".").replace(".class", "")
|
|
63
|
+
else:
|
|
64
|
+
with open(container, "rb") as f:
|
|
65
|
+
data = f.read()
|
|
66
|
+
rel_path = container.name.replace(".class", ".java")
|
|
67
|
+
display_name = container.stem
|
|
68
|
+
|
|
69
|
+
# Update UI
|
|
70
|
+
sys.stdout.write("\033[F\033[K")
|
|
71
|
+
sys.stdout.write(f"{display_name}\n")
|
|
72
|
+
sys.stdout.flush()
|
|
73
|
+
|
|
74
|
+
# Decompile
|
|
75
|
+
source = self.engine.decompile(data)
|
|
76
|
+
|
|
77
|
+
# Save
|
|
78
|
+
target_path = output_dir / rel_path
|
|
79
|
+
target_path.parent.mkdir(parents=True, exist_ok=True)
|
|
80
|
+
with open(target_path, "w", encoding="utf-8") as f:
|
|
81
|
+
f.write(source)
|
|
82
|
+
|
|
83
|
+
pbar.update(1)
|
|
84
|
+
except Exception as e:
|
|
85
|
+
pbar.write(f"Error decompiling {member or container}: {e}")
|
|
86
|
+
|
|
87
|
+
pbar.n = pbar.total
|
|
88
|
+
pbar.refresh()
|
|
89
|
+
pbar.close()
|
|
90
|
+
return True, "Decompilation complete."
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import struct
|
|
2
|
+
import io
|
|
3
|
+
|
|
4
|
+
class ConstantPool:
|
|
5
|
+
UTF8 = 1
|
|
6
|
+
INTEGER = 3
|
|
7
|
+
FLOAT = 4
|
|
8
|
+
LONG = 5
|
|
9
|
+
DOUBLE = 6
|
|
10
|
+
CLASS = 7
|
|
11
|
+
STRING = 8
|
|
12
|
+
FIELDREF = 9
|
|
13
|
+
METHODREF = 10
|
|
14
|
+
INTERFACE_METHODREF = 11
|
|
15
|
+
NAME_AND_TYPE = 12
|
|
16
|
+
METHOD_HANDLE = 15
|
|
17
|
+
METHOD_TYPE = 16
|
|
18
|
+
DYNAMIC = 17
|
|
19
|
+
INVOKE_DYNAMIC = 18
|
|
20
|
+
MODULE = 19
|
|
21
|
+
PACKAGE = 20
|
|
22
|
+
|
|
23
|
+
def __init__(self, reader):
|
|
24
|
+
self.count = reader.read_u2()
|
|
25
|
+
self.entries = [None] * self.count
|
|
26
|
+
i = 1
|
|
27
|
+
while i < self.count:
|
|
28
|
+
tag = reader.read_u1()
|
|
29
|
+
if tag == self.UTF8:
|
|
30
|
+
length = reader.read_u2()
|
|
31
|
+
self.entries[i] = (tag, reader.read_bytes(length).decode('utf-8', errors='replace'))
|
|
32
|
+
elif tag in (self.INTEGER, self.FLOAT, self.FIELDREF, self.METHODREF, self.INTERFACE_METHODREF, self.NAME_AND_TYPE, self.DYNAMIC, self.INVOKE_DYNAMIC):
|
|
33
|
+
self.entries[i] = (tag, reader.read_u4())
|
|
34
|
+
elif tag in (self.LONG, self.DOUBLE):
|
|
35
|
+
self.entries[i] = (tag, reader.read_u8())
|
|
36
|
+
i += 1
|
|
37
|
+
elif tag in (self.CLASS, self.STRING, self.METHOD_TYPE, self.MODULE, self.PACKAGE):
|
|
38
|
+
self.entries[i] = (tag, reader.read_u2())
|
|
39
|
+
elif tag == self.METHOD_HANDLE:
|
|
40
|
+
self.entries[i] = (tag, reader.read_u1(), reader.read_u2())
|
|
41
|
+
i += 1
|
|
42
|
+
|
|
43
|
+
def get(self, index):
|
|
44
|
+
if 0 < index < self.count:
|
|
45
|
+
return self.entries[index]
|
|
46
|
+
return None
|
|
47
|
+
|
|
48
|
+
def get_utf8(self, index):
|
|
49
|
+
entry = self.get(index)
|
|
50
|
+
if entry and entry[0] == self.UTF8: return entry[1]
|
|
51
|
+
return ""
|
|
52
|
+
|
|
53
|
+
def get_class_name(self, index):
|
|
54
|
+
entry = self.get(index)
|
|
55
|
+
if entry and entry[0] == self.CLASS: return self.get_utf8(entry[1])
|
|
56
|
+
return ""
|
|
57
|
+
|
|
58
|
+
def resolve(self, index):
|
|
59
|
+
entry = self.get(index)
|
|
60
|
+
if not entry: return f"#{index}"
|
|
61
|
+
tag = entry[0]
|
|
62
|
+
if tag == self.UTF8: return entry[1]
|
|
63
|
+
if tag == self.CLASS: return self.get_class_name(index).replace("/", ".")
|
|
64
|
+
if tag == self.STRING: return f"\"{self.get_utf8(entry[1])}\""
|
|
65
|
+
if tag in (self.FIELDREF, self.METHODREF, self.INTERFACE_METHODREF):
|
|
66
|
+
nt_idx = self.get(entry[1])[1] if self.get(entry[1])[0] == self.CLASS else 0
|
|
67
|
+
name_and_type = self.get(entry[1]) # Simplified for brevity
|
|
68
|
+
return f"Ref#{index}"
|
|
69
|
+
return f"CP#{index}"
|
|
70
|
+
|
|
71
|
+
class NativeEngine:
|
|
72
|
+
OPCODES = {
|
|
73
|
+
0x00: "nop", 0x01: "aconst_null", 0x12: "ldc", 0x19: "aload", 0x2a: "aload_0",
|
|
74
|
+
0x2b: "aload_1", 0x2c: "aload_2", 0x2d: "aload_3", 0xb1: "return", 0xb2: "getstatic",
|
|
75
|
+
0xb4: "getfield", 0xb5: "putfield", 0xb6: "invokevirtual", 0xb7: "invokespecial",
|
|
76
|
+
0xb8: "invokestatic", 0xbb: "new", 0xbf: "athrow"
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
def decompile(self, data):
|
|
80
|
+
stream = io.BytesIO(data)
|
|
81
|
+
def read_u1(): return struct.unpack('>B', stream.read(1))[0]
|
|
82
|
+
def read_u2(): return struct.unpack('>H', stream.read(2))[0]
|
|
83
|
+
def read_u4(): return struct.unpack('>I', stream.read(4))[0]
|
|
84
|
+
|
|
85
|
+
if read_u4() != 0xCAFEBABE: return "Error: Invalid class."
|
|
86
|
+
read_u4() # minor/major
|
|
87
|
+
cp = ConstantPool(type('Reader', (), {'read_u1': read_u1, 'read_u2': read_u2, 'read_u4': read_u4, 'read_u8': lambda: struct.unpack('>Q', stream.read(8))[0], 'read_bytes': lambda n: stream.read(n)}))
|
|
88
|
+
|
|
89
|
+
access_flags = read_u2()
|
|
90
|
+
this_class = cp.get_class_name(read_u2())
|
|
91
|
+
super_class = cp.get_class_name(read_u2())
|
|
92
|
+
|
|
93
|
+
for _ in range(read_u2()): read_u2() # interfaces
|
|
94
|
+
|
|
95
|
+
output = []
|
|
96
|
+
pkg = ".".join(this_class.split("/")[:-1])
|
|
97
|
+
if pkg: output.extend([f"package {pkg};", ""])
|
|
98
|
+
|
|
99
|
+
output.append(f"public class {this_class.split('/')[-1]} {{")
|
|
100
|
+
|
|
101
|
+
for _ in range(read_u2()): # fields
|
|
102
|
+
f_acc, f_name, f_desc = read_u2(), cp.get_utf8(read_u2()), cp.get_utf8(read_u2())
|
|
103
|
+
for _ in range(read_u2()): read_bytes_attr = stream.read(read_u4()) # skip attributes
|
|
104
|
+
output.append(f" {f_name};")
|
|
105
|
+
|
|
106
|
+
for _ in range(read_u2()): # methods
|
|
107
|
+
m_acc, m_name, m_desc = read_u2(), cp.get_utf8(read_u2()), cp.get_utf8(read_u2())
|
|
108
|
+
output.append(f"\n {m_name}{m_desc} {{")
|
|
109
|
+
for _ in range(read_u2()):
|
|
110
|
+
attr_name = cp.get_utf8(read_u2())
|
|
111
|
+
attr_len = read_u4()
|
|
112
|
+
if attr_name == "Code":
|
|
113
|
+
stream.read(4) # max stack/locals
|
|
114
|
+
code_len = read_u4()
|
|
115
|
+
code = stream.read(code_len)
|
|
116
|
+
# Simple disassembly
|
|
117
|
+
pc = 0
|
|
118
|
+
while pc < code_len:
|
|
119
|
+
op = code[pc]
|
|
120
|
+
name = self.OPCODES.get(op, f"0x{op:02x}")
|
|
121
|
+
output.append(f" /* {pc:3} */ {name}")
|
|
122
|
+
pc += 1 # Very basic step
|
|
123
|
+
stream.read(read_u2() * 8) # exceptions
|
|
124
|
+
for _ in range(read_u2()): stream.read(6 + read_u4()) # code attributes
|
|
125
|
+
else:
|
|
126
|
+
stream.read(attr_len)
|
|
127
|
+
output.append(" }")
|
|
128
|
+
|
|
129
|
+
output.append("}")
|
|
130
|
+
return "\n".join(output)
|