Skip to content

Commit 67dd53a

Browse files
committed
Made code a little cleaner and easier to use. Especially when dealing with modules.
1 parent 003a9eb commit 67dd53a

7 files changed

Lines changed: 179 additions & 135 deletions

File tree

.idea/compiler.xml

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 1 addition & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/java/com/beaudoin/jmm/process/Module.java

Lines changed: 74 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -25,55 +25,82 @@
2525
package com.beaudoin.jmm.process;
2626

2727

28+
import com.beaudoin.jmm.misc.Cacheable;
2829
import com.beaudoin.jmm.misc.MemoryBuffer;
30+
import com.beaudoin.jmm.natives.win32.Kernel32;
31+
import com.beaudoin.jmm.process.impl.win32.Win32Process;
32+
import com.sun.jna.Native;
2933
import com.sun.jna.Pointer;
34+
import com.sun.jna.platform.win32.Win32Exception;
3035

31-
public final class Module {
32-
33-
private final NativeProcess process;
34-
private final String name;
35-
private final long address;
36-
private final int size;
37-
private final Pointer pointer;
38-
private MemoryBuffer data;
39-
40-
public Module(NativeProcess process, String name, Pointer pointer, long size) {
41-
this.process = process;
42-
this.name = name;
43-
this.address = Pointer.nativeValue(pointer);
44-
this.size = (int) size;
45-
this.pointer = pointer;
46-
}
47-
48-
public NativeProcess process() {
49-
return process;
50-
}
51-
52-
public Pointer pointer() {
53-
return pointer;
54-
}
55-
56-
public String name() {
57-
return name;
58-
}
59-
60-
public int size() {
61-
return size;
62-
}
63-
64-
public long address() {
65-
return address;
66-
}
67-
68-
public MemoryBuffer data() {
69-
return data(false);
70-
}
71-
72-
public MemoryBuffer data(boolean forceNew) {
73-
if (forceNew || data == null) {
74-
data = process().read(pointer(), size());
75-
}
76-
return data;
77-
}
36+
public final class Module implements ReadableRegion {
37+
38+
private final NativeProcess process;
39+
private final String name;
40+
private final long address;
41+
private final int size;
42+
private final Pointer pointer;
43+
private MemoryBuffer data;
44+
45+
public Module(NativeProcess process, String name, Pointer pointer, long size) {
46+
this.process = process;
47+
this.name = name;
48+
this.address = Pointer.nativeValue(pointer);
49+
this.size = (int) size;
50+
this.pointer = pointer;
51+
}
52+
53+
public NativeProcess process() {
54+
return process;
55+
}
56+
57+
public Pointer pointer() {
58+
return pointer;
59+
}
60+
61+
public String name() {
62+
return name;
63+
}
64+
65+
public int size() {
66+
return size;
67+
}
68+
69+
public long address() {
70+
return address;
71+
}
72+
73+
public MemoryBuffer data() {
74+
return data(false);
75+
}
76+
77+
public MemoryBuffer data(boolean forceNew) {
78+
if (forceNew || data == null) {
79+
data = process().read(pointer(), size());
80+
}
81+
return data;
82+
}
83+
84+
@Override
85+
public MemoryBuffer read(Pointer offset, int size) {
86+
MemoryBuffer buffer = Cacheable.buffer(size);
87+
if (Kernel32.ReadProcessMemory(((Win32Process) process()).pointer(), Cacheable.pointer(address() + Pointer.nativeValue(offset)), buffer, size, 0) == 0) {
88+
throw new Win32Exception(Native.getLastError());
89+
}
90+
return buffer;
91+
}
92+
93+
@Override
94+
public NativeProcess write(Pointer offset, MemoryBuffer buffer) {
95+
if (Kernel32.WriteProcessMemory(((Win32Process) process()).pointer(), Cacheable.pointer(address() + Pointer.nativeValue(offset)), buffer, buffer.size(), 0) == 0) {
96+
throw new Win32Exception(Native.getLastError());
97+
}
98+
return process();
99+
}
100+
101+
@Override
102+
public boolean canRead(Pointer offset, int size) {
103+
return Kernel32.ReadProcessMemory(((Win32Process) process()).pointer(), Cacheable.pointer(address() + Pointer.nativeValue(offset)), Cacheable.buffer(size), size, 0) != 0;
104+
}
78105

79106
}

src/main/java/com/beaudoin/jmm/process/NativeProcess.java

Lines changed: 4 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,9 @@
2424

2525
package com.beaudoin.jmm.process;
2626

27-
import com.beaudoin.jmm.misc.Cacheable;
28-
import com.beaudoin.jmm.misc.MemoryBuffer;
29-
import com.beaudoin.jmm.misc.Strings;
3027
import com.beaudoin.jmm.misc.Utils;
3128
import com.beaudoin.jmm.natives.mac.mac;
3229
import com.beaudoin.jmm.natives.unix.libc;
33-
import com.beaudoin.jmm.natives.unix.unix;
3430
import com.beaudoin.jmm.natives.win32.Kernel32;
3531
import com.beaudoin.jmm.process.impl.mac.MacProcess;
3632
import com.beaudoin.jmm.process.impl.unix.UnixProcess;
@@ -41,12 +37,10 @@
4137
import com.sun.jna.platform.win32.Tlhelp32;
4238
import com.sun.jna.ptr.IntByReference;
4339

44-
import static com.beaudoin.jmm.misc.Cacheable.buffer;
45-
4640
/**
4741
* Created by Jonathan on 12/12/15.
4842
*/
49-
public interface NativeProcess {
43+
public interface NativeProcess extends ReadableRegion {
5044

5145
static NativeProcess byName(String name) {
5246
if (Platform.isWindows()) {
@@ -63,11 +57,11 @@ static NativeProcess byName(String name) {
6357
Kernel32.CloseHandle(snapshot);
6458
}
6559
} else if (Platform.isMac() || Platform.isLinux()) {
66-
return byId(Utils.exec("bash", "-c", "ps -A | grep -m1 \""+name+"\" | awk '{print $1}'"));
60+
return byId(Utils.exec("bash", "-c", "ps -A | grep -m1 \"" + name + "\" | awk '{print $1}'"));
6761
} else {
6862
throw new UnsupportedOperationException("Unknown operating system! (" + System.getProperty("os.name") + ")");
6963
}
70-
throw new IllegalStateException("Process " + name + " was not found. Are you sure its running?");
64+
return null;
7165
}
7266

7367
static NativeProcess byId(int id) {
@@ -99,75 +93,4 @@ static boolean checkSudo() {
9993

10094
Module findModule(String moduleName);
10195

102-
MemoryBuffer read(Pointer address, int size);
103-
104-
NativeProcess write(Pointer address, MemoryBuffer buffer);
105-
106-
boolean canRead(Pointer address, int size);
107-
108-
default int readByte(long address) {
109-
return read(address, 1).getByte();
110-
}
111-
112-
default int readShort(long address) {
113-
return read(address, 2).getShort();
114-
}
115-
116-
default int readInt(long address) {
117-
return read(address, 4).getInt();
118-
}
119-
120-
default long readLong(long address) {
121-
return read(address, 8).getLong();
122-
}
123-
124-
default float readFloat(long address) {
125-
return read(address, 4).getFloat();
126-
}
127-
128-
default double readDouble(long address) {
129-
return read(address, 8).getDouble();
130-
}
131-
132-
default String readString(long address, int length) {
133-
byte[] bytes = new byte[length];
134-
read(address, bytes.length).get(bytes);
135-
return Strings.transform(bytes);
136-
}
137-
138-
default MemoryBuffer read(long address, int size) {
139-
return read(Cacheable.pointer(address), size);
140-
}
141-
142-
default NativeProcess writeBoolean(long address, boolean value) {
143-
return write(Cacheable.pointer(address), buffer(1).putBoolean(value));
144-
}
145-
146-
default NativeProcess writeByte(long address, int value) {
147-
return write(Cacheable.pointer(address), buffer(1).putByte(value));
148-
}
149-
150-
default NativeProcess writeShort(long address, int value) {
151-
return write(Cacheable.pointer(address), buffer(2).putShort(value));
152-
}
153-
154-
default NativeProcess writeInt(long address, int value) {
155-
return write(Cacheable.pointer(address), buffer(4).putInt(value));
156-
}
157-
158-
default NativeProcess writeLong(long address, long value) {
159-
return write(Cacheable.pointer(address), buffer(8).putLong(value));
160-
}
161-
162-
default NativeProcess writeFloat(long address, float value) {
163-
return write(Cacheable.pointer(address), buffer(4).putFloat(value));
164-
}
165-
166-
default NativeProcess writeDouble(long address, double value) {
167-
return write(Cacheable.pointer(address), buffer(8).putDouble(value));
168-
}
169-
170-
default boolean canRead(long address, int size) {
171-
return canRead(Cacheable.pointer(address), size);
172-
}
173-
}
96+
}

0 commit comments

Comments
 (0)