java_asm_macro 0.2.1

Java bytecode reader & writer in rust
Documentation
# java-asm

[![Crates.io Version](https://img.shields.io/crates/v/java_asm)](https://crates.io/crates/java_asm)

Java bytecode reader & writer, maybe a rust implementation for [ASM](https://gitlab.ow2.org/asm/asm)

There are some similar projects in GitHub, but they didn't actually implement all JVM Bytecode format, and also not
implements all ASM nodes/features in rust. 
So I want to build this library to fully read and write Java bytecode information.

This project supports much newer LTS Java version(Java 21 currently) than other rust implementations. Only supports 
`asm-tree` api currently, not supports visitor api because Tree API is much easier to use than visitor api.

## GUI

We have a simple GUI built with egui to show decompiled files. It currently supports APK, standalone DEX, and
multi-selected DEX/package inputs.

![GUI](docs/egui.png)

### Web / WASM ๐ŸŒ

Online demo: [GitHub Pages](https://zsqw123.github.io/rust-java-asm/)

The browser version lets you select or drop an APK, standalone DEX, or Android package archive such as APKS for
inspection. Multiple DEX/package files can be selected together. Parsing and inspection stay local to the browser;
the selected file is not uploaded to a server. It also avoids the macOS Gatekeeper or "Privacy & Security" exception
needed by an unsigned desktop application.

The browser version currently supports APK, standalone DEX, and nested Android package archives such as APKS;
JAR/class-file accessors will be exposed once they are ready.

The web/WASM and native builds share the same UI and parsing code, but have some differences:

| Area                             | Web / WASM                                              | Native desktop                                                                                                               |
|----------------------------------|---------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------|
| ๐Ÿ“ฆ Installation                  | ๐ŸŒ Open in a browser; no installation required.         | ๐Ÿ“ฅ Download and run a desktop executable.                                                                                    |
| โšก APK loading speed             | ๐Ÿงต Browser execution in single thread.                  | ๐Ÿš€ Real OS multi-threads; generally faster for large or multi-DEX APKs.                                                      |
| ๐Ÿ—œ๏ธ APK compression compatibility | ๐Ÿงฉ Browser-compatible ZIP codec set. (deflate, zstd...) | โœ… Broader ZIP/APK codec support. (additional supports for xz & bzip2)                                                       |
| ๐Ÿ›ก๏ธ Security & Signing            | ๐Ÿ”’ Browser sandbox, no signing required                 | โš ๏ธ I can't afford a Apple developer seat. Unsigned binaries may be blocked by Gatekeeper, SmartScreen, or enterprise policy. |

Both builds inspect the selected files locally; the file is not uploaded to a server. (BTW, I have no money to buy a
server.) For a quick inspection, the web version is usually the easiest option. For large APKs, unusual compression
formats, or the best loading performance, the native build is generally the better choice.

### CLI

The `java_asm_cli` executable provides these commands:

- `find-classes`: find classes and list their basic method and field structure.
- `export-class`: export one class as Smali.
- `export-all`: export multiple classes as Smali, with optional class filtering.

See the [CLI skill guide](asm_cli/SKILL.md) for installation, complete usage, options, and examples.

## Current Stage

After version 0.0.6, you can try to use `ClassNode::from_jvms` to read a class file into a `ClassNode`, 
and it is pretty useful to now, check [tests](asm/tests/node/read_test.rs) in this project to 
see some examples.

- [x] Implement **Read** Java class file with **[JVMS]https://docs.oracle.com/javase/specs/jvms/se21/html/jvms-4.html** format
- [x] Implement **Write** Java class file with **[JVMS]https://docs.oracle.com/javase/specs/jvms/se21/html/jvms-4.html** format
- [ ] **WIP**, `ClassNode` reader
  - [x] Constant pool
  - [x] Attributes
  - [x] Class / Field / Method metadata
  - [x] Method instructions
  - [ ] Method frames (read as an attribute is available, need a better format)
  - [ ] Method local variables / stacks / try-catches (read as an attribute is available, need a better format)
- [ ] **Not Start**, Nodes writer (low priority currently)
  - [ ] Append constant pool if needed
  - [ ] Write back attributes into Class / Field / Method / Code
  - [ ] Method frames
- [ ] **Not Start**, Implement ASM features (eg. auto calculate frame/stack etc.)
- [x] Smali liked output.
- [ ] Dex interop. (https://source.android.com/docs/core/runtime/dex-format)
  - [x] Basic structure for dex file metadata
  - [x] Instructions
  - [x] Annotations
  - [ ] Debug info
  - [ ] Other formats
  - [x] Separation for metadata with real data (e.g. instructions)
- [ ] Isolate AsmResult as multiple different errors, and provide a better error message.
- [ ] GUI interactions
  - [ ] GUI backend:
    - [x] add progress when loading files
    - [ ] unzip (whatever jar or dex) & parallel read
    - [x] retrieve metadata and combine multiple metadata for better indexing
    - [x] using metadata to get the real data if needed (e.g. method instructions)
    - [ ] search content, quick search for metadata and slow search for instructions.
    - [ ] export sources / fake smali?
  - [ ] EGUI frontend. (`asm_egui` folder)
    - [x] basic window with egui.
    - [x] load files from the backend
    - [x] add progress bar UI when loading files
    - [x] show metadata in a tree view
    - [x] show instructions in a list view
    - [x] quick jump to specific metadata
    - [x] search classes
    - [x] jump to offset or type descriptor
    - [ ] Settings (e.g. custom fonts, theme, etc.)
    - [ ] decompiling by using mapping file.
  - [x] WASM Support (egui web build and static deployment)
    As we know, Apple is a shit, it must let us to use a mac machine to develop, and cost 100 USD per year for a developer account, 
    otherwise your app will be reported as broken. Deploy it on web will bring a good user experience for user who wants to
    use it as fast as possible.
  - [ ] **\[Experimental\]** Tauri frontend. (`ta` folder)  
    Recent days, I found using tauri will bring us a better UX, so I decided to use tauri later for 
flexible interactions and share web technologies to boost the development. Using pure rust for frontend (egui or iced solution) 
is complex for handle so many things (like font fallbacks, search bars, code highlighting and copy & paste, etc.)

### Goals

1. Fully read and write Java class file with JVMS format.
2. Partially implement ASM features in rust, but provides some better operations in rust.
3. Support much newer Java version (higher priority for LTS, Java 21 currently).
4. For parser core, not depends on any of other rust libraries at runtime, build everything from std only. (but some proc marco's
   dependencies are used for generate some template codes. e.g. `quote` and `syn`)
5. Simple GUI to show decompiled jar like files. e.g. dex, apk, jar, class files.

---

Some similar projects:

- [rjvm]https://github.com/andreabergia/rjvm
  - read jvm bytecode and run it in a rust vm
  - support JVM7
- [jvm-assembler]https://github.com/kenpratt/jvm-assembler
- [Ka-Pi]https://github.com/ChAoSUnItY/Ka-Pi
- [cfsp]https://github.com/ChAoSUnItY/cfsp